proctex.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <cmath>
  6. #include "common/math_util.h"
  7. #include "video_core/swrasterizer/proctex.h"
  8. namespace Pica {
  9. namespace Rasterizer {
  10. using ProcTexClamp = TexturingRegs::ProcTexClamp;
  11. using ProcTexShift = TexturingRegs::ProcTexShift;
  12. using ProcTexCombiner = TexturingRegs::ProcTexCombiner;
  13. using ProcTexFilter = TexturingRegs::ProcTexFilter;
  14. static float LookupLUT(const std::array<State::ProcTex::ValueEntry, 128>& lut, float coord) {
  15. // For NoiseLUT/ColorMap/AlphaMap, coord=0.0 is lut[0], coord=127.0/128.0 is lut[127] and
  16. // coord=1.0 is lut[127]+lut_diff[127]. For other indices, the result is interpolated using
  17. // value entries and difference entries.
  18. coord *= 128;
  19. const int index_int = std::min(static_cast<int>(coord), 127);
  20. const float frac = coord - index_int;
  21. return lut[index_int].ToFloat() + frac * lut[index_int].DiffToFloat();
  22. }
  23. // These function are used to generate random noise for procedural texture. Their results are
  24. // verified against real hardware, but it's not known if the algorithm is the same as hardware.
  25. static unsigned int NoiseRand1D(unsigned int v) {
  26. static constexpr std::array<unsigned int, 16> table{
  27. {0, 4, 10, 8, 4, 9, 7, 12, 5, 15, 13, 14, 11, 15, 2, 11}};
  28. return ((v % 9 + 2) * 3 & 0xF) ^ table[(v / 9) & 0xF];
  29. }
  30. static float NoiseRand2D(unsigned int x, unsigned int y) {
  31. static constexpr std::array<unsigned int, 16> table{
  32. {10, 2, 15, 8, 0, 7, 4, 5, 5, 13, 2, 6, 13, 9, 3, 14}};
  33. unsigned int u2 = NoiseRand1D(x);
  34. unsigned int v2 = NoiseRand1D(y);
  35. v2 += ((u2 & 3) == 1) ? 4 : 0;
  36. v2 ^= (u2 & 1) * 6;
  37. v2 += 10 + u2;
  38. v2 &= 0xF;
  39. v2 ^= table[u2];
  40. return -1.0f + v2 * 2.0f / 15.0f;
  41. }
  42. static float NoiseCoef(float u, float v, TexturingRegs regs, State::ProcTex state) {
  43. const float freq_u = float16::FromRaw(regs.proctex_noise_frequency.u).ToFloat32();
  44. const float freq_v = float16::FromRaw(regs.proctex_noise_frequency.v).ToFloat32();
  45. const float phase_u = float16::FromRaw(regs.proctex_noise_u.phase).ToFloat32();
  46. const float phase_v = float16::FromRaw(regs.proctex_noise_v.phase).ToFloat32();
  47. const float x = 9 * freq_u * std::abs(u + phase_u);
  48. const float y = 9 * freq_v * std::abs(v + phase_v);
  49. const int x_int = static_cast<int>(x);
  50. const int y_int = static_cast<int>(y);
  51. const float x_frac = x - x_int;
  52. const float y_frac = y - y_int;
  53. const float g0 = NoiseRand2D(x_int, y_int) * (x_frac + y_frac);
  54. const float g1 = NoiseRand2D(x_int + 1, y_int) * (x_frac + y_frac - 1);
  55. const float g2 = NoiseRand2D(x_int, y_int + 1) * (x_frac + y_frac - 1);
  56. const float g3 = NoiseRand2D(x_int + 1, y_int + 1) * (x_frac + y_frac - 2);
  57. const float x_noise = LookupLUT(state.noise_table, x_frac);
  58. const float y_noise = LookupLUT(state.noise_table, y_frac);
  59. return Math::BilinearInterp(g0, g1, g2, g3, x_noise, y_noise);
  60. }
  61. static float GetShiftOffset(float v, ProcTexShift mode, ProcTexClamp clamp_mode) {
  62. const float offset = (clamp_mode == ProcTexClamp::MirroredRepeat) ? 1 : 0.5f;
  63. switch (mode) {
  64. case ProcTexShift::None:
  65. return 0;
  66. case ProcTexShift::Odd:
  67. return offset * (((int)v / 2) % 2);
  68. case ProcTexShift::Even:
  69. return offset * ((((int)v + 1) / 2) % 2);
  70. default:
  71. LOG_CRITICAL(HW_GPU, "Unknown shift mode %u", static_cast<u32>(mode));
  72. return 0;
  73. }
  74. };
  75. static void ClampCoord(float& coord, ProcTexClamp mode) {
  76. switch (mode) {
  77. case ProcTexClamp::ToZero:
  78. if (coord > 1.0f)
  79. coord = 0.0f;
  80. break;
  81. case ProcTexClamp::ToEdge:
  82. coord = std::min(coord, 1.0f);
  83. break;
  84. case ProcTexClamp::SymmetricalRepeat:
  85. coord = coord - std::floor(coord);
  86. break;
  87. case ProcTexClamp::MirroredRepeat: {
  88. int integer = static_cast<int>(coord);
  89. float frac = coord - integer;
  90. coord = (integer % 2) == 0 ? frac : (1.0f - frac);
  91. break;
  92. }
  93. case ProcTexClamp::Pulse:
  94. if (coord <= 0.5f)
  95. coord = 0.0f;
  96. else
  97. coord = 1.0f;
  98. break;
  99. default:
  100. LOG_CRITICAL(HW_GPU, "Unknown clamp mode %u", static_cast<u32>(mode));
  101. coord = std::min(coord, 1.0f);
  102. break;
  103. }
  104. }
  105. float CombineAndMap(float u, float v, ProcTexCombiner combiner,
  106. const std::array<State::ProcTex::ValueEntry, 128>& map_table) {
  107. float f;
  108. switch (combiner) {
  109. case ProcTexCombiner::U:
  110. f = u;
  111. break;
  112. case ProcTexCombiner::U2:
  113. f = u * u;
  114. break;
  115. case TexturingRegs::ProcTexCombiner::V:
  116. f = v;
  117. break;
  118. case TexturingRegs::ProcTexCombiner::V2:
  119. f = v * v;
  120. break;
  121. case TexturingRegs::ProcTexCombiner::Add:
  122. f = (u + v) * 0.5f;
  123. break;
  124. case TexturingRegs::ProcTexCombiner::Add2:
  125. f = (u * u + v * v) * 0.5f;
  126. break;
  127. case TexturingRegs::ProcTexCombiner::SqrtAdd2:
  128. f = std::min(std::sqrt(u * u + v * v), 1.0f);
  129. break;
  130. case TexturingRegs::ProcTexCombiner::Min:
  131. f = std::min(u, v);
  132. break;
  133. case TexturingRegs::ProcTexCombiner::Max:
  134. f = std::max(u, v);
  135. break;
  136. case TexturingRegs::ProcTexCombiner::RMax:
  137. f = std::min(((u + v) * 0.5f + std::sqrt(u * u + v * v)) * 0.5f, 1.0f);
  138. break;
  139. default:
  140. LOG_CRITICAL(HW_GPU, "Unknown combiner %u", static_cast<u32>(combiner));
  141. f = 0.0f;
  142. break;
  143. }
  144. return LookupLUT(map_table, f);
  145. }
  146. Math::Vec4<u8> ProcTex(float u, float v, TexturingRegs regs, State::ProcTex state) {
  147. u = std::abs(u);
  148. v = std::abs(v);
  149. // Get shift offset before noise generation
  150. const float u_shift = GetShiftOffset(v, regs.proctex.u_shift, regs.proctex.u_clamp);
  151. const float v_shift = GetShiftOffset(u, regs.proctex.v_shift, regs.proctex.v_clamp);
  152. // Generate noise
  153. if (regs.proctex.noise_enable) {
  154. float noise = NoiseCoef(u, v, regs, state);
  155. u += noise * regs.proctex_noise_u.amplitude / 4095.0f;
  156. v += noise * regs.proctex_noise_v.amplitude / 4095.0f;
  157. u = std::abs(u);
  158. v = std::abs(v);
  159. }
  160. // Shift
  161. u += u_shift;
  162. v += v_shift;
  163. // Clamp
  164. ClampCoord(u, regs.proctex.u_clamp);
  165. ClampCoord(v, regs.proctex.v_clamp);
  166. // Combine and map
  167. const float lut_coord = CombineAndMap(u, v, regs.proctex.color_combiner, state.color_map_table);
  168. // Look up the color
  169. // For the color lut, coord=0.0 is lut[offset] and coord=1.0 is lut[offset+width-1]
  170. const u32 offset = regs.proctex_lut_offset;
  171. const u32 width = regs.proctex_lut.width;
  172. const float index = offset + (lut_coord * (width - 1));
  173. Math::Vec4<u8> final_color;
  174. // TODO(wwylele): implement mipmap
  175. switch (regs.proctex_lut.filter) {
  176. case ProcTexFilter::Linear:
  177. case ProcTexFilter::LinearMipmapLinear:
  178. case ProcTexFilter::LinearMipmapNearest: {
  179. const int index_int = static_cast<int>(index);
  180. const float frac = index - index_int;
  181. const auto color_value = state.color_table[index_int].ToVector().Cast<float>();
  182. const auto color_diff = state.color_diff_table[index_int].ToVector().Cast<float>();
  183. final_color = (color_value + frac * color_diff).Cast<u8>();
  184. break;
  185. }
  186. case ProcTexFilter::Nearest:
  187. case ProcTexFilter::NearestMipmapLinear:
  188. case ProcTexFilter::NearestMipmapNearest:
  189. final_color = state.color_table[static_cast<int>(std::round(index))].ToVector();
  190. break;
  191. }
  192. if (regs.proctex.separate_alpha) {
  193. // Note: in separate alpha mode, the alpha channel skips the color LUT look up stage. It
  194. // uses the output of CombineAndMap directly instead.
  195. const float final_alpha =
  196. CombineAndMap(u, v, regs.proctex.alpha_combiner, state.alpha_map_table);
  197. return Math::MakeVec<u8>(final_color.rgb(), static_cast<u8>(final_alpha * 255));
  198. } else {
  199. return final_color;
  200. }
  201. }
  202. } // namespace Rasterizer
  203. } // namespace Pica