Sampler.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibSoftGPU/Image.h>
  7. #include <LibSoftGPU/Sampler.h>
  8. #include <math.h>
  9. namespace SoftGPU {
  10. // See: https://www.khronos.org/opengl/wiki/Common_Mistakes#Texture_edge_color_problem
  11. // FIXME: make this dynamically configurable through ConfigServer
  12. static constexpr bool CLAMP_DEPRECATED_BEHAVIOR = false;
  13. static constexpr float fracf(float value)
  14. {
  15. return value - floorf(value);
  16. }
  17. static constexpr float wrap_repeat(float value)
  18. {
  19. return fracf(value);
  20. }
  21. static constexpr float wrap_clamp(float value)
  22. {
  23. return clamp(value, 0.0f, 1.0f);
  24. }
  25. static constexpr float wrap_clamp_to_edge(float value, unsigned num_texels)
  26. {
  27. float const clamp_limit = 1.f / (2 * num_texels);
  28. return clamp(value, clamp_limit, 1.0f - clamp_limit);
  29. }
  30. static constexpr float wrap_mirrored_repeat(float value, unsigned num_texels)
  31. {
  32. float integer = floorf(value);
  33. float frac = value - integer;
  34. bool iseven = fmodf(integer, 2.0f) == 0.0f;
  35. return wrap_clamp_to_edge(iseven ? frac : 1 - frac, num_texels);
  36. }
  37. static constexpr float wrap(float value, TextureWrapMode mode, unsigned num_texels)
  38. {
  39. switch (mode) {
  40. case TextureWrapMode::Repeat:
  41. return wrap_repeat(value);
  42. case TextureWrapMode::MirroredRepeat:
  43. return wrap_mirrored_repeat(value, num_texels);
  44. case TextureWrapMode::Clamp:
  45. if constexpr (CLAMP_DEPRECATED_BEHAVIOR) {
  46. return wrap_clamp(value);
  47. }
  48. return wrap_clamp_to_edge(value, num_texels);
  49. case TextureWrapMode::ClampToBorder:
  50. case TextureWrapMode::ClampToEdge:
  51. return wrap_clamp_to_edge(value, num_texels);
  52. default:
  53. VERIFY_NOT_REACHED();
  54. }
  55. }
  56. FloatVector4 Sampler::sample_2d(FloatVector2 const& uv) const
  57. {
  58. if (m_config.bound_image.is_null())
  59. return { 0, 0, 0, 1 };
  60. auto const& image = *m_config.bound_image;
  61. unsigned const layer = 0;
  62. // FIXME: calculate actual mipmap level to use
  63. unsigned const level = 0;
  64. unsigned width = image.level_width(level);
  65. unsigned height = image.level_height(level);
  66. float s = wrap(uv.x(), m_config.texture_wrap_u, width);
  67. float t = wrap(uv.y(), m_config.texture_wrap_v, height);
  68. float u = s * width;
  69. float v = t * height;
  70. if (m_config.texture_mag_filter == TextureFilter::Nearest) {
  71. unsigned i = min(static_cast<unsigned>(u), width - 1);
  72. unsigned j = min(static_cast<unsigned>(v), height - 1);
  73. return image.texel(layer, level, i, j, 0);
  74. }
  75. int i0 = m_config.texture_wrap_u == TextureWrapMode::Repeat ? static_cast<unsigned>(floorf(u - 0.5f)) % width : floorf(u - 0.5f);
  76. int j0 = m_config.texture_wrap_v == TextureWrapMode::Repeat ? static_cast<unsigned>(floorf(v - 0.5f)) % height : floorf(v - 0.5f);
  77. int i1 = m_config.texture_wrap_u == TextureWrapMode::Repeat ? (i0 + 1) % width : i0 + 1;
  78. int j1 = m_config.texture_wrap_v == TextureWrapMode::Repeat ? (j0 + 1) % height : j0 + 1;
  79. FloatVector4 t0, t1, t2, t3;
  80. if (m_config.texture_wrap_u == TextureWrapMode::Repeat && m_config.texture_wrap_v == TextureWrapMode::Repeat) {
  81. t0 = image.texel(layer, level, i0, j0, 0);
  82. t1 = image.texel(layer, level, i1, j0, 0);
  83. t2 = image.texel(layer, level, i0, j1, 0);
  84. t3 = image.texel(layer, level, i1, j1, 0);
  85. } else {
  86. int w = static_cast<int>(width);
  87. int h = static_cast<int>(height);
  88. t0 = (i0 < 0 || i0 >= w || j0 < 0 || j0 >= h) ? m_config.border_color : image.texel(layer, level, i0, j0, 0);
  89. t1 = (i1 < 0 || i1 >= w || j0 < 0 || j0 >= h) ? m_config.border_color : image.texel(layer, level, i1, j0, 0);
  90. t2 = (i0 < 0 || i0 >= w || j1 < 0 || j1 >= h) ? m_config.border_color : image.texel(layer, level, i0, j1, 0);
  91. t3 = (i1 < 0 || i1 >= w || j1 < 0 || j1 >= h) ? m_config.border_color : image.texel(layer, level, i1, j1, 0);
  92. }
  93. float alpha = fracf(u - 0.5f);
  94. float beta = fracf(v - 0.5f);
  95. float one_minus_alpha = 1 - alpha;
  96. float one_minus_beta = 1 - beta;
  97. auto lerp_0 = t0 * one_minus_alpha + t1 * alpha;
  98. auto lerp_1 = t2 * one_minus_alpha + t3 * alpha;
  99. return lerp_0 * one_minus_beta + lerp_1 * beta;
  100. }
  101. }