Sampler.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. using AK::SIMD::truncate_int_range;
  26. static f32x4 wrap_repeat(f32x4 value)
  27. {
  28. return frac_int_range(value);
  29. }
  30. [[maybe_unused]] static f32x4 wrap_clamp(f32x4 value)
  31. {
  32. return clamp(value, expand4(0.0f), expand4(1.0f));
  33. }
  34. static f32x4 wrap_clamp_to_edge(f32x4 value, u32x4 num_texels)
  35. {
  36. f32x4 const clamp_limit = 1.f / to_f32x4(2 * num_texels);
  37. return clamp(value, clamp_limit, 1.0f - clamp_limit);
  38. }
  39. static f32x4 wrap_mirrored_repeat(f32x4 value, u32x4 num_texels)
  40. {
  41. f32x4 integer = floor_int_range(value);
  42. f32x4 frac = value - integer;
  43. auto is_odd = to_i32x4(integer) & 1;
  44. return wrap_clamp_to_edge(is_odd ? 1 - frac : frac, num_texels);
  45. }
  46. static f32x4 wrap(f32x4 value, TextureWrapMode mode, u32x4 num_texels)
  47. {
  48. switch (mode) {
  49. case TextureWrapMode::Repeat:
  50. return wrap_repeat(value);
  51. case TextureWrapMode::MirroredRepeat:
  52. return wrap_mirrored_repeat(value, num_texels);
  53. case TextureWrapMode::Clamp:
  54. if constexpr (CLAMP_DEPRECATED_BEHAVIOR) {
  55. return wrap_clamp(value);
  56. }
  57. return wrap_clamp_to_edge(value, num_texels);
  58. case TextureWrapMode::ClampToBorder:
  59. case TextureWrapMode::ClampToEdge:
  60. return wrap_clamp_to_edge(value, num_texels);
  61. default:
  62. VERIFY_NOT_REACHED();
  63. }
  64. }
  65. ALWAYS_INLINE static Vector4<f32x4> texel4(Image const& image, u32x4 layer, u32x4 level, u32x4 x, u32x4 y, u32x4 z)
  66. {
  67. auto t0 = image.texel(layer[0], level[0], x[0], y[0], z[0]);
  68. auto t1 = image.texel(layer[1], level[1], x[1], y[1], z[1]);
  69. auto t2 = image.texel(layer[2], level[2], x[2], y[2], z[2]);
  70. auto t3 = image.texel(layer[3], level[3], x[3], y[3], z[3]);
  71. return Vector4<f32x4> {
  72. f32x4 { t0.x(), t1.x(), t2.x(), t3.x() },
  73. f32x4 { t0.y(), t1.y(), t2.y(), t3.y() },
  74. f32x4 { t0.z(), t1.z(), t2.z(), t3.z() },
  75. f32x4 { t0.w(), t1.w(), t2.w(), t3.w() },
  76. };
  77. }
  78. 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)
  79. {
  80. auto border_mask = maskbits(x < 0 || x >= w || y < 0 || y >= h);
  81. auto t0 = border_mask & 1 ? border : image.texel(layer[0], level[0], x[0], y[0], z[0]);
  82. auto t1 = border_mask & 2 ? border : image.texel(layer[1], level[1], x[1], y[1], z[1]);
  83. auto t2 = border_mask & 4 ? border : image.texel(layer[2], level[2], x[2], y[2], z[2]);
  84. auto t3 = border_mask & 8 ? border : image.texel(layer[3], level[3], x[3], y[3], z[3]);
  85. return Vector4<f32x4> {
  86. f32x4 { t0.x(), t1.x(), t2.x(), t3.x() },
  87. f32x4 { t0.y(), t1.y(), t2.y(), t3.y() },
  88. f32x4 { t0.z(), t1.z(), t2.z(), t3.z() },
  89. f32x4 { t0.w(), t1.w(), t2.w(), t3.w() },
  90. };
  91. }
  92. Vector4<AK::SIMD::f32x4> Sampler::sample_2d(Vector2<AK::SIMD::f32x4> const& uv) const
  93. {
  94. if (m_config.bound_image.is_null())
  95. return expand4(FloatVector4 { 1, 0, 0, 1 });
  96. auto const& image = *m_config.bound_image;
  97. u32x4 const layer = expand4(0u);
  98. // FIXME: calculate actual mipmap level to use
  99. u32x4 const level = expand4(0u);
  100. u32x4 const width = {
  101. image.level_width(level[0]),
  102. image.level_width(level[1]),
  103. image.level_width(level[2]),
  104. image.level_width(level[3]),
  105. };
  106. u32x4 const height = {
  107. image.level_height(level[0]),
  108. image.level_height(level[1]),
  109. image.level_height(level[2]),
  110. image.level_height(level[3]),
  111. };
  112. u32x4 width_mask = width - 1;
  113. u32x4 height_mask = height - 1;
  114. f32x4 s = wrap(uv.x(), m_config.texture_wrap_u, width);
  115. f32x4 t = wrap(uv.y(), m_config.texture_wrap_v, height);
  116. f32x4 u = s * to_f32x4(width);
  117. f32x4 v = t * to_f32x4(height);
  118. if (m_config.texture_mag_filter == TextureFilter::Nearest) {
  119. u32x4 i = to_u32x4(u);
  120. u32x4 j = to_u32x4(v);
  121. u32x4 k = expand4(0u);
  122. i = image.width_is_power_of_two() ? i & width_mask : i % width;
  123. j = image.height_is_power_of_two() ? j & height_mask : j % height;
  124. return texel4(image, layer, level, i, j, k);
  125. }
  126. u -= 0.5f;
  127. v -= 0.5f;
  128. i32x4 i0 = to_i32x4(floor_int_range(u));
  129. i32x4 i1 = i0 + 1;
  130. i32x4 j0 = to_i32x4(floor_int_range(v));
  131. i32x4 j1 = j0 + 1;
  132. if (m_config.texture_wrap_u == TextureWrapMode::Repeat) {
  133. if (image.width_is_power_of_two()) {
  134. i0 = (i32x4)(i0 & width_mask);
  135. i1 = (i32x4)(i1 & width_mask);
  136. } else {
  137. i0 = (i32x4)(i0 % width);
  138. i1 = (i32x4)(i1 % width);
  139. }
  140. }
  141. if (m_config.texture_wrap_v == TextureWrapMode::Repeat) {
  142. if (image.height_is_power_of_two()) {
  143. j0 = (i32x4)(j0 & height_mask);
  144. j1 = (i32x4)(j1 & height_mask);
  145. } else {
  146. j0 = (i32x4)(j0 % height);
  147. j1 = (i32x4)(j1 % height);
  148. }
  149. }
  150. u32x4 k = expand4(0u);
  151. Vector4<f32x4> t0, t1, t2, t3;
  152. if (m_config.texture_wrap_u == TextureWrapMode::Repeat && m_config.texture_wrap_v == TextureWrapMode::Repeat) {
  153. t0 = texel4(image, layer, level, to_u32x4(i0), to_u32x4(j0), k);
  154. t1 = texel4(image, layer, level, to_u32x4(i1), to_u32x4(j0), k);
  155. t2 = texel4(image, layer, level, to_u32x4(i0), to_u32x4(j1), k);
  156. t3 = texel4(image, layer, level, to_u32x4(i1), to_u32x4(j1), k);
  157. } else {
  158. t0 = texel4border(image, layer, level, to_u32x4(i0), to_u32x4(j0), k, m_config.border_color, width, height);
  159. t1 = texel4border(image, layer, level, to_u32x4(i1), to_u32x4(j0), k, m_config.border_color, width, height);
  160. t2 = texel4border(image, layer, level, to_u32x4(i0), to_u32x4(j1), k, m_config.border_color, width, height);
  161. t3 = texel4border(image, layer, level, to_u32x4(i1), to_u32x4(j1), k, m_config.border_color, width, height);
  162. }
  163. f32x4 const alpha = frac_int_range(u);
  164. f32x4 const beta = frac_int_range(v);
  165. auto const lerp_0 = mix(t0, t1, alpha);
  166. auto const lerp_1 = mix(t2, t3, alpha);
  167. return mix(lerp_0, lerp_1, beta);
  168. }
  169. }