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