Sampler.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/SIMDExtras.h>
  7. #include <AK/SIMDMath.h>
  8. #include <LibSoftGPU/Config.h>
  9. #include <LibSoftGPU/Image.h>
  10. #include <LibSoftGPU/SIMD.h>
  11. #include <LibSoftGPU/Sampler.h>
  12. #include <math.h>
  13. namespace SoftGPU {
  14. using AK::SIMD::f32x4;
  15. using AK::SIMD::i32x4;
  16. using AK::SIMD::u32x4;
  17. using AK::SIMD::clamp;
  18. using AK::SIMD::expand4;
  19. using AK::SIMD::floor_int_range;
  20. using AK::SIMD::frac_int_range;
  21. using AK::SIMD::maskbits;
  22. using AK::SIMD::to_f32x4;
  23. using AK::SIMD::to_i32x4;
  24. using AK::SIMD::to_u32x4;
  25. static f32x4 wrap_repeat(f32x4 value)
  26. {
  27. return frac_int_range(value);
  28. }
  29. [[maybe_unused]] static f32x4 wrap_clamp(f32x4 value)
  30. {
  31. return clamp(value, expand4(0.0f), expand4(1.0f));
  32. }
  33. static f32x4 wrap_clamp_to_edge(f32x4 value, u32x4 num_texels)
  34. {
  35. f32x4 const clamp_limit = 1.f / to_f32x4(2 * num_texels);
  36. return clamp(value, clamp_limit, 1.0f - clamp_limit);
  37. }
  38. static f32x4 wrap_mirrored_repeat(f32x4 value, u32x4 num_texels)
  39. {
  40. f32x4 integer = floor_int_range(value);
  41. f32x4 frac = value - integer;
  42. auto is_odd = to_i32x4(integer) & 1;
  43. return wrap_clamp_to_edge(is_odd ? 1 - frac : frac, num_texels);
  44. }
  45. static f32x4 wrap(f32x4 value, GPU::TextureWrapMode mode, u32x4 num_texels)
  46. {
  47. switch (mode) {
  48. case GPU::TextureWrapMode::Repeat:
  49. return wrap_repeat(value);
  50. case GPU::TextureWrapMode::MirroredRepeat:
  51. return wrap_mirrored_repeat(value, num_texels);
  52. case GPU::TextureWrapMode::Clamp:
  53. if constexpr (CLAMP_DEPRECATED_BEHAVIOR) {
  54. return wrap_clamp(value);
  55. }
  56. return wrap_clamp_to_edge(value, num_texels);
  57. case GPU::TextureWrapMode::ClampToBorder:
  58. case GPU::TextureWrapMode::ClampToEdge:
  59. return wrap_clamp_to_edge(value, num_texels);
  60. default:
  61. VERIFY_NOT_REACHED();
  62. }
  63. }
  64. ALWAYS_INLINE static Vector4<f32x4> texel4(Image const& image, u32x4 layer, u32x4 level, u32x4 x, u32x4 y, u32x4 z)
  65. {
  66. auto t0 = image.texel(layer[0], level[0], x[0], y[0], z[0]);
  67. auto t1 = image.texel(layer[1], level[1], x[1], y[1], z[1]);
  68. auto t2 = image.texel(layer[2], level[2], x[2], y[2], z[2]);
  69. auto t3 = image.texel(layer[3], level[3], x[3], y[3], z[3]);
  70. return Vector4<f32x4> {
  71. f32x4 { t0.x(), t1.x(), t2.x(), t3.x() },
  72. f32x4 { t0.y(), t1.y(), t2.y(), t3.y() },
  73. f32x4 { t0.z(), t1.z(), t2.z(), t3.z() },
  74. f32x4 { t0.w(), t1.w(), t2.w(), t3.w() },
  75. };
  76. }
  77. ALWAYS_INLINE static Vector4<f32x4> texel4border(Image const& image, u32x4 layer, u32x4 level, u32x4 x, u32x4 y, u32x4 z, FloatVector4 const& border, u32x4 w, u32x4 h)
  78. {
  79. auto border_mask = maskbits(x < 0 || x >= w || y < 0 || y >= h);
  80. auto t0 = border_mask & 1 ? border : image.texel(layer[0], level[0], x[0], y[0], z[0]);
  81. auto t1 = border_mask & 2 ? border : image.texel(layer[1], level[1], x[1], y[1], z[1]);
  82. auto t2 = border_mask & 4 ? border : image.texel(layer[2], level[2], x[2], y[2], z[2]);
  83. auto t3 = border_mask & 8 ? border : image.texel(layer[3], level[3], x[3], y[3], z[3]);
  84. return Vector4<f32x4> {
  85. f32x4 { t0.x(), t1.x(), t2.x(), t3.x() },
  86. f32x4 { t0.y(), t1.y(), t2.y(), t3.y() },
  87. f32x4 { t0.z(), t1.z(), t2.z(), t3.z() },
  88. f32x4 { t0.w(), t1.w(), t2.w(), t3.w() },
  89. };
  90. }
  91. Vector4<AK::SIMD::f32x4> Sampler::sample_2d(Vector2<AK::SIMD::f32x4> const& uv) const
  92. {
  93. if (m_config.bound_image.is_null())
  94. return expand4(FloatVector4 { 1, 0, 0, 1 });
  95. auto const& image = *static_ptr_cast<Image>(m_config.bound_image);
  96. // FIXME: Make base level configurable with glTexParameteri(GL_TEXTURE_BASE_LEVEL, base_level)
  97. constexpr unsigned base_level = 0;
  98. // Determine the texture scale factor. See OpenGL 1.5 spec chapter 3.8.8.
  99. // FIXME: Static casting from u32 to float could silently truncate here.
  100. // u16 should be plenty enough for texture dimensions and would allow textures of up to 65536x65536x65536 pixels.
  101. auto texel_coordinates = uv;
  102. texel_coordinates.set_x(texel_coordinates.x() * static_cast<float>(image.level_width(base_level)));
  103. texel_coordinates.set_y(texel_coordinates.y() * static_cast<float>(image.level_height(base_level)));
  104. auto dtdx = ddx(texel_coordinates);
  105. auto dtdy = ddy(texel_coordinates);
  106. auto scale_factor = max(dtdx.dot(dtdx), dtdy.dot(dtdy));
  107. // FIXME: Here we simply determine the filter based on the single scale factor of the upper left pixel.
  108. // Actually, we could end up with different scale factors for each pixel. This however would break our
  109. // parallelisation as we could also end up with different filter modes per pixel.
  110. // Note: scale_factor approximates texels per pixel. This means a scale factor less than 1 indicates texture magnification.
  111. if (scale_factor[0] <= 1.f)
  112. return sample_2d_lod(uv, expand4(base_level), m_config.texture_mag_filter);
  113. if (m_config.mipmap_filter == GPU::MipMapFilter::None)
  114. return sample_2d_lod(uv, expand4(base_level), m_config.texture_min_filter);
  115. // FIXME: Instead of clamping to num_levels - 1, actually make the max mipmap level configurable with glTexParameteri(GL_TEXTURE_MAX_LEVEL, max_level)
  116. auto min_level = expand4(static_cast<float>(base_level));
  117. auto max_level = expand4(image.num_levels() - 1.0f);
  118. auto level = min(max(log2_approximate(scale_factor) * 0.5f, min_level), max_level);
  119. auto lower_level_texel = sample_2d_lod(uv, to_u32x4(level), m_config.texture_min_filter);
  120. if (m_config.mipmap_filter == GPU::MipMapFilter::Nearest)
  121. return lower_level_texel;
  122. auto higher_level_texel = sample_2d_lod(uv, to_u32x4(min(level + 1.f, max_level)), m_config.texture_min_filter);
  123. return mix(lower_level_texel, higher_level_texel, frac_int_range(level));
  124. }
  125. Vector4<AK::SIMD::f32x4> Sampler::sample_2d_lod(Vector2<AK::SIMD::f32x4> const& uv, AK::SIMD::u32x4 level, GPU::TextureFilter filter) const
  126. {
  127. auto const& image = *static_ptr_cast<Image>(m_config.bound_image);
  128. u32x4 const layer = expand4(0u);
  129. u32x4 const width = {
  130. image.level_width(level[0]),
  131. image.level_width(level[1]),
  132. image.level_width(level[2]),
  133. image.level_width(level[3]),
  134. };
  135. u32x4 const height = {
  136. image.level_height(level[0]),
  137. image.level_height(level[1]),
  138. image.level_height(level[2]),
  139. image.level_height(level[3]),
  140. };
  141. u32x4 width_mask = width - 1;
  142. u32x4 height_mask = height - 1;
  143. f32x4 s = wrap(uv.x(), m_config.texture_wrap_u, width);
  144. f32x4 t = wrap(uv.y(), m_config.texture_wrap_v, height);
  145. f32x4 u = s * to_f32x4(width);
  146. f32x4 v = t * to_f32x4(height);
  147. if (filter == GPU::TextureFilter::Nearest) {
  148. u32x4 i = to_u32x4(u);
  149. u32x4 j = to_u32x4(v);
  150. u32x4 k = expand4(0u);
  151. i = image.width_is_power_of_two() ? i & width_mask : i % width;
  152. j = image.height_is_power_of_two() ? j & height_mask : j % height;
  153. return texel4(image, layer, level, i, j, k);
  154. }
  155. u -= 0.5f;
  156. v -= 0.5f;
  157. u32x4 i0 = to_u32x4(floor_int_range(u));
  158. u32x4 i1 = i0 + 1;
  159. u32x4 j0 = to_u32x4(floor_int_range(v));
  160. u32x4 j1 = j0 + 1;
  161. if (m_config.texture_wrap_u == GPU::TextureWrapMode::Repeat) {
  162. if (image.width_is_power_of_two()) {
  163. i0 = i0 & width_mask;
  164. i1 = i1 & width_mask;
  165. } else {
  166. i0 = i0 % width;
  167. i1 = i1 % width;
  168. }
  169. }
  170. if (m_config.texture_wrap_v == GPU::TextureWrapMode::Repeat) {
  171. if (image.height_is_power_of_two()) {
  172. j0 = j0 & height_mask;
  173. j1 = j1 & height_mask;
  174. } else {
  175. j0 = j0 % height;
  176. j1 = j1 % height;
  177. }
  178. }
  179. u32x4 k = expand4(0u);
  180. Vector4<f32x4> t0, t1, t2, t3;
  181. if (m_config.texture_wrap_u == GPU::TextureWrapMode::Repeat && m_config.texture_wrap_v == GPU::TextureWrapMode::Repeat) {
  182. t0 = texel4(image, layer, level, i0, j0, k);
  183. t1 = texel4(image, layer, level, i1, j0, k);
  184. t2 = texel4(image, layer, level, i0, j1, k);
  185. t3 = texel4(image, layer, level, i1, j1, k);
  186. } else {
  187. t1 = texel4border(image, layer, level, i1, j0, k, m_config.border_color, width, height);
  188. t0 = texel4border(image, layer, level, i0, j0, k, m_config.border_color, width, height);
  189. t2 = texel4border(image, layer, level, i0, j1, k, m_config.border_color, width, height);
  190. t3 = texel4border(image, layer, level, i1, j1, k, m_config.border_color, width, height);
  191. }
  192. f32x4 const alpha = frac_int_range(u);
  193. f32x4 const beta = frac_int_range(v);
  194. auto const lerp_0 = mix(t0, t1, alpha);
  195. auto const lerp_1 = mix(t2, t3, alpha);
  196. return mix(lerp_0, lerp_1, beta);
  197. }
  198. }