Sampler2D.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Sampler2D.h"
  7. #include <LibGL/Tex/Texture2D.h>
  8. #include <math.h>
  9. namespace GL {
  10. static constexpr float wrap_repeat(float value)
  11. {
  12. return value - floorf(value);
  13. }
  14. static constexpr float wrap_clamp_to_edge(float value, int num_texels)
  15. {
  16. float const clamp_limit = 1.f / (2 * num_texels);
  17. return clamp(value, clamp_limit, 1.0f - clamp_limit);
  18. }
  19. static constexpr float wrap_mirrored_repeat(float value, int num_texels)
  20. {
  21. float integer = floorf(value);
  22. float frac = value - integer;
  23. bool iseven = fmodf(integer, 2.0f) == 0.0f;
  24. return wrap_clamp_to_edge(iseven ? frac : 1 - frac, num_texels);
  25. }
  26. static constexpr float wrap(float value, GLint mode, int num_texels)
  27. {
  28. switch (mode) {
  29. case GL_REPEAT:
  30. return wrap_repeat(value);
  31. // FIXME: These clamp modes actually have slightly different behavior. Currently we use GL_CLAMP_TO_EDGE for all of them.
  32. case GL_CLAMP:
  33. case GL_CLAMP_TO_BORDER:
  34. case GL_CLAMP_TO_EDGE:
  35. return wrap_clamp_to_edge(value, num_texels);
  36. case GL_MIRRORED_REPEAT:
  37. return wrap_mirrored_repeat(value, num_texels);
  38. default:
  39. VERIFY_NOT_REACHED();
  40. }
  41. }
  42. FloatVector4 Sampler2D::sample(FloatVector4 const& uv) const
  43. {
  44. // FIXME: Calculate the correct mipmap level here, need to receive uv derivatives for that
  45. unsigned lod = 0;
  46. MipMap const& mip = m_texture.mipmap(lod);
  47. if (mip.width() < 1 || mip.height() < 1)
  48. return { 1, 1, 1, 1 };
  49. float x = wrap(uv.x(), m_wrap_s_mode, mip.width());
  50. float y = wrap(uv.y(), m_wrap_t_mode, mip.height());
  51. x *= mip.width();
  52. y *= mip.height();
  53. // Sampling implemented according to https://www.khronos.org/registry/OpenGL/specs/gl/glspec121.pdf Chapter 3.8
  54. if (m_mag_filter == GL_NEAREST) {
  55. return mip.texel(static_cast<unsigned>(x), static_cast<unsigned>(y));
  56. } else if (m_mag_filter == GL_LINEAR) {
  57. // FIXME: Implement different sampling points for wrap modes other than GL_REPEAT
  58. x -= 0.5f;
  59. y -= 0.5f;
  60. unsigned i0 = static_cast<unsigned>(x) % mip.width();
  61. unsigned j0 = static_cast<unsigned>(y) % mip.height();
  62. unsigned i1 = (i0 + 1) % mip.width();
  63. unsigned j1 = (j0 + 1) % mip.height();
  64. auto t0 = mip.texel(i0, j0);
  65. auto t1 = mip.texel(i1, j0);
  66. auto t2 = mip.texel(i0, j1);
  67. auto t3 = mip.texel(i1, j1);
  68. float frac_x = x - floorf(x);
  69. float frac_y = y - floorf(y);
  70. float one_minus_frac_x = 1 - frac_x;
  71. auto h1 = t0 * one_minus_frac_x + t1 * frac_x;
  72. auto h2 = t2 * one_minus_frac_x + t3 * frac_x;
  73. return h1 * (1 - frac_y) + h2 * frac_y;
  74. } else {
  75. VERIFY_NOT_REACHED();
  76. }
  77. }
  78. }