Sampler.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. constexpr static float fracf(float value)
  11. {
  12. return value - floorf(value);
  13. }
  14. constexpr static float wrap_repeat(float value)
  15. {
  16. return fracf(value);
  17. }
  18. static constexpr float wrap_clamp(float value)
  19. {
  20. return clamp(value, 0.0f, 1.0f);
  21. }
  22. static constexpr float wrap_clamp_to_edge(float value, unsigned num_texels)
  23. {
  24. float const clamp_limit = 1.f / (2 * num_texels);
  25. return clamp(value, clamp_limit, 1.0f - clamp_limit);
  26. }
  27. static constexpr float wrap_mirrored_repeat(float value, unsigned num_texels)
  28. {
  29. float integer = floorf(value);
  30. float frac = value - integer;
  31. bool iseven = fmodf(integer, 2.0f) == 0.0f;
  32. return wrap_clamp_to_edge(iseven ? frac : 1 - frac, num_texels);
  33. }
  34. constexpr static float wrap(float value, TextureWrapMode mode, unsigned num_texels)
  35. {
  36. switch (mode) {
  37. case TextureWrapMode::Repeat:
  38. return wrap_repeat(value);
  39. case TextureWrapMode::MirroredRepeat:
  40. return wrap_mirrored_repeat(value, num_texels);
  41. case TextureWrapMode::Clamp:
  42. return wrap_clamp(value);
  43. case TextureWrapMode::ClampToBorder:
  44. case TextureWrapMode::ClampToEdge:
  45. return wrap_clamp_to_edge(value, num_texels);
  46. default:
  47. VERIFY_NOT_REACHED();
  48. }
  49. }
  50. FloatVector4 Sampler::sample_2d(FloatVector2 const& uv) const
  51. {
  52. if (m_config.bound_image.is_null())
  53. return { 0, 0, 0, 1 };
  54. auto const& image = *m_config.bound_image;
  55. unsigned const layer = 0;
  56. // FIXME: calculate actual mipmap level to use
  57. unsigned const level = 0;
  58. unsigned width = image.level_width(level);
  59. unsigned height = image.level_height(level);
  60. float s = wrap(uv.x(), m_config.texture_wrap_u, width);
  61. float t = wrap(uv.y(), m_config.texture_wrap_v, height);
  62. float u = s * width;
  63. float v = t * height;
  64. if (m_config.texture_mag_filter == TextureFilter::Nearest) {
  65. unsigned i = min(static_cast<unsigned>(u), width - 1);
  66. unsigned j = min(static_cast<unsigned>(v), height - 1);
  67. return image.texel(layer, level, i, j, 0);
  68. }
  69. int i0 = m_config.texture_wrap_u == TextureWrapMode::Repeat ? static_cast<unsigned>(floorf(u - 0.5f)) % width : floorf(u - 0.5f);
  70. int j0 = m_config.texture_wrap_v == TextureWrapMode::Repeat ? static_cast<unsigned>(floorf(v - 0.5f)) % height : floorf(v - 0.5f);
  71. int i1 = m_config.texture_wrap_u == TextureWrapMode::Repeat ? (i0 + 1) % width : i0 + 1;
  72. int j1 = m_config.texture_wrap_v == TextureWrapMode::Repeat ? (j0 + 1) % height : j0 + 1;
  73. FloatVector4 t0, t1, t2, t3;
  74. if (m_config.texture_wrap_u == TextureWrapMode::Repeat && m_config.texture_wrap_v == TextureWrapMode::Repeat) {
  75. t0 = image.texel(layer, level, i0, j0, 0);
  76. t1 = image.texel(layer, level, i1, j0, 0);
  77. t2 = image.texel(layer, level, i0, j1, 0);
  78. t3 = image.texel(layer, level, i1, j1, 0);
  79. } else {
  80. int w = static_cast<int>(width);
  81. int h = static_cast<int>(height);
  82. t0 = (i0 < 0 || i0 >= w || j0 < 0 || j0 >= h) ? m_config.border_color : image.texel(layer, level, i0, j0, 0);
  83. t1 = (i1 < 0 || i1 >= w || j0 < 0 || j0 >= h) ? m_config.border_color : image.texel(layer, level, i1, j0, 0);
  84. t2 = (i0 < 0 || i0 >= w || j1 < 0 || j1 >= h) ? m_config.border_color : image.texel(layer, level, i0, j1, 0);
  85. t3 = (i1 < 0 || i1 >= w || j1 < 0 || j1 >= h) ? m_config.border_color : image.texel(layer, level, i1, j1, 0);
  86. }
  87. float alpha = fracf(u - 0.5f);
  88. float beta = fracf(v - 0.5f);
  89. float one_minus_alpha = 1 - alpha;
  90. float one_minus_beta = 1 - beta;
  91. auto lerp_0 = t0 * one_minus_alpha + t1 * alpha;
  92. auto lerp_1 = t2 * one_minus_alpha + t3 * alpha;
  93. return lerp_0 * one_minus_beta + lerp_1 * beta;
  94. }
  95. }