diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/vector_math.h | 10 | ||||
| -rw-r--r-- | src/video_core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/video_core/command_processor.cpp | 31 | ||||
| -rw-r--r-- | src/video_core/pica_state.h | 54 | ||||
| -rw-r--r-- | src/video_core/regs.h | 7 | ||||
| -rw-r--r-- | src/video_core/regs_texturing.h | 96 | ||||
| -rw-r--r-- | src/video_core/swrasterizer/proctex.cpp | 223 | ||||
| -rw-r--r-- | src/video_core/swrasterizer/proctex.h | 16 | ||||
| -rw-r--r-- | src/video_core/swrasterizer/rasterizer.cpp | 13 |
9 files changed, 448 insertions, 4 deletions
diff --git a/src/common/vector_math.h b/src/common/vector_math.h index 7ca8e15f5..c7a461a1e 100644 --- a/src/common/vector_math.h +++ b/src/common/vector_math.h | |||
| @@ -652,6 +652,16 @@ static inline decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begi | |||
| 652 | return (begin * (base - t) + end * t) / base; | 652 | return (begin * (base - t) + end * t) / base; |
| 653 | } | 653 | } |
| 654 | 654 | ||
| 655 | // bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second | ||
| 656 | // interpolation. | ||
| 657 | template <typename X> | ||
| 658 | inline auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, const float s, | ||
| 659 | const float t) { | ||
| 660 | auto y0 = Lerp(x00, x01, s); | ||
| 661 | auto y1 = Lerp(x10, x11, s); | ||
| 662 | return Lerp(y0, y1, t); | ||
| 663 | } | ||
| 664 | |||
| 655 | // Utility vector factories | 665 | // Utility vector factories |
| 656 | template <typename T> | 666 | template <typename T> |
| 657 | static inline Vec2<T> MakeVec(const T& x, const T& y) { | 667 | static inline Vec2<T> MakeVec(const T& x, const T& y) { |
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index 5317719e8..e00b88f71 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt | |||
| @@ -15,6 +15,7 @@ set(SRCS | |||
| 15 | shader/shader_interpreter.cpp | 15 | shader/shader_interpreter.cpp |
| 16 | swrasterizer/clipper.cpp | 16 | swrasterizer/clipper.cpp |
| 17 | swrasterizer/framebuffer.cpp | 17 | swrasterizer/framebuffer.cpp |
| 18 | swrasterizer/proctex.cpp | ||
| 18 | swrasterizer/rasterizer.cpp | 19 | swrasterizer/rasterizer.cpp |
| 19 | swrasterizer/swrasterizer.cpp | 20 | swrasterizer/swrasterizer.cpp |
| 20 | swrasterizer/texturing.cpp | 21 | swrasterizer/texturing.cpp |
| @@ -54,6 +55,7 @@ set(HEADERS | |||
| 54 | shader/shader_interpreter.h | 55 | shader/shader_interpreter.h |
| 55 | swrasterizer/clipper.h | 56 | swrasterizer/clipper.h |
| 56 | swrasterizer/framebuffer.h | 57 | swrasterizer/framebuffer.h |
| 58 | swrasterizer/proctex.h | ||
| 57 | swrasterizer/rasterizer.h | 59 | swrasterizer/rasterizer.h |
| 58 | swrasterizer/swrasterizer.h | 60 | swrasterizer/swrasterizer.h |
| 59 | swrasterizer/texturing.h | 61 | swrasterizer/texturing.h |
diff --git a/src/video_core/command_processor.cpp b/src/video_core/command_processor.cpp index 2e32ff905..49a93e980 100644 --- a/src/video_core/command_processor.cpp +++ b/src/video_core/command_processor.cpp | |||
| @@ -458,6 +458,37 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) { | |||
| 458 | break; | 458 | break; |
| 459 | } | 459 | } |
| 460 | 460 | ||
| 461 | case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[0], 0xb0): | ||
| 462 | case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[1], 0xb1): | ||
| 463 | case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[2], 0xb2): | ||
| 464 | case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[3], 0xb3): | ||
| 465 | case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[4], 0xb4): | ||
| 466 | case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[5], 0xb5): | ||
| 467 | case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[6], 0xb6): | ||
| 468 | case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[7], 0xb7): { | ||
| 469 | auto& index = regs.texturing.proctex_lut_config.index; | ||
| 470 | auto& pt = g_state.proctex; | ||
| 471 | |||
| 472 | switch (regs.texturing.proctex_lut_config.ref_table.Value()) { | ||
| 473 | case TexturingRegs::ProcTexLutTable::Noise: | ||
| 474 | pt.noise_table[index % pt.noise_table.size()].raw = value; | ||
| 475 | break; | ||
| 476 | case TexturingRegs::ProcTexLutTable::ColorMap: | ||
| 477 | pt.color_map_table[index % pt.color_map_table.size()].raw = value; | ||
| 478 | break; | ||
| 479 | case TexturingRegs::ProcTexLutTable::AlphaMap: | ||
| 480 | pt.alpha_map_table[index % pt.alpha_map_table.size()].raw = value; | ||
| 481 | break; | ||
| 482 | case TexturingRegs::ProcTexLutTable::Color: | ||
| 483 | pt.color_table[index % pt.color_table.size()].raw = value; | ||
| 484 | break; | ||
| 485 | case TexturingRegs::ProcTexLutTable::ColorDiff: | ||
| 486 | pt.color_diff_table[index % pt.color_diff_table.size()].raw = value; | ||
| 487 | break; | ||
| 488 | } | ||
| 489 | index.Assign(index + 1); | ||
| 490 | break; | ||
| 491 | } | ||
| 461 | default: | 492 | default: |
| 462 | break; | 493 | break; |
| 463 | } | 494 | } |
diff --git a/src/video_core/pica_state.h b/src/video_core/pica_state.h index af7536d11..f46db09fb 100644 --- a/src/video_core/pica_state.h +++ b/src/video_core/pica_state.h | |||
| @@ -7,6 +7,7 @@ | |||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include "common/bit_field.h" | 8 | #include "common/bit_field.h" |
| 9 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 10 | #include "common/vector_math.h" | ||
| 10 | #include "video_core/primitive_assembly.h" | 11 | #include "video_core/primitive_assembly.h" |
| 11 | #include "video_core/regs.h" | 12 | #include "video_core/regs.h" |
| 12 | #include "video_core/shader/shader.h" | 13 | #include "video_core/shader/shader.h" |
| @@ -25,6 +26,59 @@ struct State { | |||
| 25 | 26 | ||
| 26 | Shader::AttributeBuffer input_default_attributes; | 27 | Shader::AttributeBuffer input_default_attributes; |
| 27 | 28 | ||
| 29 | struct ProcTex { | ||
| 30 | union ValueEntry { | ||
| 31 | u32 raw; | ||
| 32 | |||
| 33 | // LUT value, encoded as 12-bit fixed point, with 12 fraction bits | ||
| 34 | BitField<0, 12, u32> value; // 0.0.12 fixed point | ||
| 35 | |||
| 36 | // Difference between two entry values. Used for efficient interpolation. | ||
| 37 | // 0.0.12 fixed point with two's complement. The range is [-0.5, 0.5). | ||
| 38 | // Note: the type of this is different from the one of lighting LUT | ||
| 39 | BitField<12, 12, s32> difference; | ||
| 40 | |||
| 41 | float ToFloat() const { | ||
| 42 | return static_cast<float>(value) / 4095.f; | ||
| 43 | } | ||
| 44 | |||
| 45 | float DiffToFloat() const { | ||
| 46 | return static_cast<float>(difference) / 4095.f; | ||
| 47 | } | ||
| 48 | }; | ||
| 49 | |||
| 50 | union ColorEntry { | ||
| 51 | u32 raw; | ||
| 52 | BitField<0, 8, u32> r; | ||
| 53 | BitField<8, 8, u32> g; | ||
| 54 | BitField<16, 8, u32> b; | ||
| 55 | BitField<24, 8, u32> a; | ||
| 56 | |||
| 57 | Math::Vec4<u8> ToVector() const { | ||
| 58 | return {static_cast<u8>(r), static_cast<u8>(g), static_cast<u8>(b), | ||
| 59 | static_cast<u8>(a)}; | ||
| 60 | } | ||
| 61 | }; | ||
| 62 | |||
| 63 | union ColorDifferenceEntry { | ||
| 64 | u32 raw; | ||
| 65 | BitField<0, 8, s32> r; // half of the difference between two ColorEntry | ||
| 66 | BitField<8, 8, s32> g; | ||
| 67 | BitField<16, 8, s32> b; | ||
| 68 | BitField<24, 8, s32> a; | ||
| 69 | |||
| 70 | Math::Vec4<s32> ToVector() const { | ||
| 71 | return Math::Vec4<s32>{r, g, b, a} * 2; | ||
| 72 | } | ||
| 73 | }; | ||
| 74 | |||
| 75 | std::array<ValueEntry, 128> noise_table; | ||
| 76 | std::array<ValueEntry, 128> color_map_table; | ||
| 77 | std::array<ValueEntry, 128> alpha_map_table; | ||
| 78 | std::array<ColorEntry, 256> color_table; | ||
| 79 | std::array<ColorDifferenceEntry, 256> color_diff_table; | ||
| 80 | } proctex; | ||
| 81 | |||
| 28 | struct { | 82 | struct { |
| 29 | union LutEntry { | 83 | union LutEntry { |
| 30 | // Used for raw access | 84 | // Used for raw access |
diff --git a/src/video_core/regs.h b/src/video_core/regs.h index 1776dad89..6d5f98cac 100644 --- a/src/video_core/regs.h +++ b/src/video_core/regs.h | |||
| @@ -101,6 +101,13 @@ ASSERT_REG_POSITION(texturing.texture1, 0x91); | |||
| 101 | ASSERT_REG_POSITION(texturing.texture1_format, 0x96); | 101 | ASSERT_REG_POSITION(texturing.texture1_format, 0x96); |
| 102 | ASSERT_REG_POSITION(texturing.texture2, 0x99); | 102 | ASSERT_REG_POSITION(texturing.texture2, 0x99); |
| 103 | ASSERT_REG_POSITION(texturing.texture2_format, 0x9e); | 103 | ASSERT_REG_POSITION(texturing.texture2_format, 0x9e); |
| 104 | ASSERT_REG_POSITION(texturing.proctex, 0xa8); | ||
| 105 | ASSERT_REG_POSITION(texturing.proctex_noise_u, 0xa9); | ||
| 106 | ASSERT_REG_POSITION(texturing.proctex_noise_v, 0xaa); | ||
| 107 | ASSERT_REG_POSITION(texturing.proctex_noise_frequency, 0xab); | ||
| 108 | ASSERT_REG_POSITION(texturing.proctex_lut, 0xac); | ||
| 109 | ASSERT_REG_POSITION(texturing.proctex_lut_offset, 0xad); | ||
| 110 | ASSERT_REG_POSITION(texturing.proctex_lut_config, 0xaf); | ||
| 104 | ASSERT_REG_POSITION(texturing.tev_stage0, 0xc0); | 111 | ASSERT_REG_POSITION(texturing.tev_stage0, 0xc0); |
| 105 | ASSERT_REG_POSITION(texturing.tev_stage1, 0xc8); | 112 | ASSERT_REG_POSITION(texturing.tev_stage1, 0xc8); |
| 106 | ASSERT_REG_POSITION(texturing.tev_stage2, 0xd0); | 113 | ASSERT_REG_POSITION(texturing.tev_stage2, 0xd0); |
diff --git a/src/video_core/regs_texturing.h b/src/video_core/regs_texturing.h index 8a7c6efe4..20f9495ed 100644 --- a/src/video_core/regs_texturing.h +++ b/src/video_core/regs_texturing.h | |||
| @@ -122,8 +122,8 @@ struct TexturingRegs { | |||
| 122 | BitField<0, 1, u32> texture0_enable; | 122 | BitField<0, 1, u32> texture0_enable; |
| 123 | BitField<1, 1, u32> texture1_enable; | 123 | BitField<1, 1, u32> texture1_enable; |
| 124 | BitField<2, 1, u32> texture2_enable; | 124 | BitField<2, 1, u32> texture2_enable; |
| 125 | BitField<8, 2, u32> texture3_coordinates; // TODO: unimplemented | 125 | BitField<8, 2, u32> texture3_coordinates; |
| 126 | BitField<10, 1, u32> texture3_enable; // TODO: unimplemented | 126 | BitField<10, 1, u32> texture3_enable; |
| 127 | BitField<13, 1, u32> texture2_use_coord1; | 127 | BitField<13, 1, u32> texture2_use_coord1; |
| 128 | BitField<16, 1, u32> clear_texture_cache; // TODO: unimplemented | 128 | BitField<16, 1, u32> clear_texture_cache; // TODO: unimplemented |
| 129 | } main_config; | 129 | } main_config; |
| @@ -137,7 +137,7 @@ struct TexturingRegs { | |||
| 137 | INSERT_PADDING_WORDS(0x2); | 137 | INSERT_PADDING_WORDS(0x2); |
| 138 | TextureConfig texture2; | 138 | TextureConfig texture2; |
| 139 | BitField<0, 4, TextureFormat> texture2_format; | 139 | BitField<0, 4, TextureFormat> texture2_format; |
| 140 | INSERT_PADDING_WORDS(0x21); | 140 | INSERT_PADDING_WORDS(0x9); |
| 141 | 141 | ||
| 142 | struct FullTextureConfig { | 142 | struct FullTextureConfig { |
| 143 | const bool enabled; | 143 | const bool enabled; |
| @@ -152,6 +152,96 @@ struct TexturingRegs { | |||
| 152 | }}; | 152 | }}; |
| 153 | } | 153 | } |
| 154 | 154 | ||
| 155 | // 0xa8-0xad: ProcTex Config | ||
| 156 | enum class ProcTexClamp : u32 { | ||
| 157 | ToZero = 0, | ||
| 158 | ToEdge = 1, | ||
| 159 | SymmetricalRepeat = 2, | ||
| 160 | MirroredRepeat = 3, | ||
| 161 | Pulse = 4, | ||
| 162 | }; | ||
| 163 | |||
| 164 | enum class ProcTexCombiner : u32 { | ||
| 165 | U = 0, // u | ||
| 166 | U2 = 1, // u * u | ||
| 167 | V = 2, // v | ||
| 168 | V2 = 3, // v * v | ||
| 169 | Add = 4, // (u + v) / 2 | ||
| 170 | Add2 = 5, // (u * u + v * v) / 2 | ||
| 171 | SqrtAdd2 = 6, // sqrt(u * u + v * v) | ||
| 172 | Min = 7, // min(u, v) | ||
| 173 | Max = 8, // max(u, v) | ||
| 174 | RMax = 9, // Average of Max and SqrtAdd2 | ||
| 175 | }; | ||
| 176 | |||
| 177 | enum class ProcTexShift : u32 { | ||
| 178 | None = 0, | ||
| 179 | Odd = 1, | ||
| 180 | Even = 2, | ||
| 181 | }; | ||
| 182 | |||
| 183 | union { | ||
| 184 | BitField<0, 3, ProcTexClamp> u_clamp; | ||
| 185 | BitField<3, 3, ProcTexClamp> v_clamp; | ||
| 186 | BitField<6, 4, ProcTexCombiner> color_combiner; | ||
| 187 | BitField<10, 4, ProcTexCombiner> alpha_combiner; | ||
| 188 | BitField<14, 1, u32> separate_alpha; | ||
| 189 | BitField<15, 1, u32> noise_enable; | ||
| 190 | BitField<16, 2, ProcTexShift> u_shift; | ||
| 191 | BitField<18, 2, ProcTexShift> v_shift; | ||
| 192 | BitField<20, 8, u32> bias_low; // float16 TODO: unimplemented | ||
| 193 | } proctex; | ||
| 194 | |||
| 195 | union ProcTexNoiseConfig { | ||
| 196 | BitField<0, 16, s32> amplitude; // fixed1.3.12 | ||
| 197 | BitField<16, 16, u32> phase; // float16 | ||
| 198 | }; | ||
| 199 | |||
| 200 | ProcTexNoiseConfig proctex_noise_u; | ||
| 201 | ProcTexNoiseConfig proctex_noise_v; | ||
| 202 | |||
| 203 | union { | ||
| 204 | BitField<0, 16, u32> u; // float16 | ||
| 205 | BitField<16, 16, u32> v; // float16 | ||
| 206 | } proctex_noise_frequency; | ||
| 207 | |||
| 208 | enum class ProcTexFilter : u32 { | ||
| 209 | Nearest = 0, | ||
| 210 | Linear = 1, | ||
| 211 | NearestMipmapNearest = 2, | ||
| 212 | LinearMipmapNearest = 3, | ||
| 213 | NearestMipmapLinear = 4, | ||
| 214 | LinearMipmapLinear = 5, | ||
| 215 | }; | ||
| 216 | |||
| 217 | union { | ||
| 218 | BitField<0, 3, ProcTexFilter> filter; | ||
| 219 | BitField<11, 8, u32> width; | ||
| 220 | BitField<19, 8, u32> bias_high; // TODO: unimplemented | ||
| 221 | } proctex_lut; | ||
| 222 | |||
| 223 | BitField<0, 8, u32> proctex_lut_offset; | ||
| 224 | |||
| 225 | INSERT_PADDING_WORDS(0x1); | ||
| 226 | |||
| 227 | // 0xaf-0xb7: ProcTex LUT | ||
| 228 | enum class ProcTexLutTable : u32 { | ||
| 229 | Noise = 0, | ||
| 230 | ColorMap = 2, | ||
| 231 | AlphaMap = 3, | ||
| 232 | Color = 4, | ||
| 233 | ColorDiff = 5, | ||
| 234 | }; | ||
| 235 | |||
| 236 | union { | ||
| 237 | BitField<0, 8, u32> index; | ||
| 238 | BitField<8, 4, ProcTexLutTable> ref_table; | ||
| 239 | } proctex_lut_config; | ||
| 240 | |||
| 241 | u32 proctex_lut_data[8]; | ||
| 242 | |||
| 243 | INSERT_PADDING_WORDS(0x8); | ||
| 244 | |||
| 155 | // 0xc0-0xff: Texture Combiner (akin to glTexEnv) | 245 | // 0xc0-0xff: Texture Combiner (akin to glTexEnv) |
| 156 | struct TevStageConfig { | 246 | struct TevStageConfig { |
| 157 | enum class Source : u32 { | 247 | enum class Source : u32 { |
diff --git a/src/video_core/swrasterizer/proctex.cpp b/src/video_core/swrasterizer/proctex.cpp new file mode 100644 index 000000000..b69892778 --- /dev/null +++ b/src/video_core/swrasterizer/proctex.cpp | |||
| @@ -0,0 +1,223 @@ | |||
| 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 | ||
diff --git a/src/video_core/swrasterizer/proctex.h b/src/video_core/swrasterizer/proctex.h new file mode 100644 index 000000000..036e4620e --- /dev/null +++ b/src/video_core/swrasterizer/proctex.h | |||
| @@ -0,0 +1,16 @@ | |||
| 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 "common/common_types.h" | ||
| 6 | #include "common/vector_math.h" | ||
| 7 | #include "video_core/pica_state.h" | ||
| 8 | |||
| 9 | namespace Pica { | ||
| 10 | namespace Rasterizer { | ||
| 11 | |||
| 12 | /// Generates procedural texture color for the given coordinates | ||
| 13 | Math::Vec4<u8> ProcTex(float u, float v, TexturingRegs regs, State::ProcTex state); | ||
| 14 | |||
| 15 | } // namespace Rasterizer | ||
| 16 | } // namespace Pica | ||
diff --git a/src/video_core/swrasterizer/rasterizer.cpp b/src/video_core/swrasterizer/rasterizer.cpp index 20addf0bd..e9edf0360 100644 --- a/src/video_core/swrasterizer/rasterizer.cpp +++ b/src/video_core/swrasterizer/rasterizer.cpp | |||
| @@ -23,6 +23,7 @@ | |||
| 23 | #include "video_core/regs_texturing.h" | 23 | #include "video_core/regs_texturing.h" |
| 24 | #include "video_core/shader/shader.h" | 24 | #include "video_core/shader/shader.h" |
| 25 | #include "video_core/swrasterizer/framebuffer.h" | 25 | #include "video_core/swrasterizer/framebuffer.h" |
| 26 | #include "video_core/swrasterizer/proctex.h" | ||
| 26 | #include "video_core/swrasterizer/rasterizer.h" | 27 | #include "video_core/swrasterizer/rasterizer.h" |
| 27 | #include "video_core/swrasterizer/texturing.h" | 28 | #include "video_core/swrasterizer/texturing.h" |
| 28 | #include "video_core/texture/texture_decode.h" | 29 | #include "video_core/texture/texture_decode.h" |
| @@ -268,7 +269,7 @@ static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Ve | |||
| 268 | uv[2].u() = GetInterpolatedAttribute(v0.tc2.u(), v1.tc2.u(), v2.tc2.u()); | 269 | uv[2].u() = GetInterpolatedAttribute(v0.tc2.u(), v1.tc2.u(), v2.tc2.u()); |
| 269 | uv[2].v() = GetInterpolatedAttribute(v0.tc2.v(), v1.tc2.v(), v2.tc2.v()); | 270 | uv[2].v() = GetInterpolatedAttribute(v0.tc2.v(), v1.tc2.v(), v2.tc2.v()); |
| 270 | 271 | ||
| 271 | Math::Vec4<u8> texture_color[3]{}; | 272 | Math::Vec4<u8> texture_color[4]{}; |
| 272 | for (int i = 0; i < 3; ++i) { | 273 | for (int i = 0; i < 3; ++i) { |
| 273 | const auto& texture = textures[i]; | 274 | const auto& texture = textures[i]; |
| 274 | if (!texture.enabled) | 275 | if (!texture.enabled) |
| @@ -334,6 +335,13 @@ static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Ve | |||
| 334 | } | 335 | } |
| 335 | } | 336 | } |
| 336 | 337 | ||
| 338 | // sample procedural texture | ||
| 339 | if (regs.texturing.main_config.texture3_enable) { | ||
| 340 | const auto& proctex_uv = uv[regs.texturing.main_config.texture3_coordinates]; | ||
| 341 | texture_color[3] = ProcTex(proctex_uv.u().ToFloat32(), proctex_uv.v().ToFloat32(), | ||
| 342 | g_state.regs.texturing, g_state.proctex); | ||
| 343 | } | ||
| 344 | |||
| 337 | // Texture environment - consists of 6 stages of color and alpha combining. | 345 | // Texture environment - consists of 6 stages of color and alpha combining. |
| 338 | // | 346 | // |
| 339 | // Color combiners take three input color values from some source (e.g. interpolated | 347 | // Color combiners take three input color values from some source (e.g. interpolated |
| @@ -376,6 +384,9 @@ static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Ve | |||
| 376 | case Source::Texture2: | 384 | case Source::Texture2: |
| 377 | return texture_color[2]; | 385 | return texture_color[2]; |
| 378 | 386 | ||
| 387 | case Source::Texture3: | ||
| 388 | return texture_color[3]; | ||
| 389 | |||
| 379 | case Source::PreviousBuffer: | 390 | case Source::PreviousBuffer: |
| 380 | return combiner_buffer; | 391 | return combiner_buffer; |
| 381 | 392 | ||