From ade45b5b9930b52b6a1d399306539073e8e2196d Mon Sep 17 00:00:00 2001 From: wwylele Date: Mon, 17 Apr 2017 10:01:45 +0300 Subject: [PATCH] pica/swrasterizer: implement procedural texture --- src/common/vector_math.h | 10 + src/video_core/CMakeLists.txt | 2 + src/video_core/command_processor.cpp | 31 +++ src/video_core/pica_state.h | 54 +++++ src/video_core/regs.h | 7 + src/video_core/regs_texturing.h | 96 ++++++++- src/video_core/swrasterizer/proctex.cpp | 223 +++++++++++++++++++++ src/video_core/swrasterizer/proctex.h | 16 ++ src/video_core/swrasterizer/rasterizer.cpp | 13 +- 9 files changed, 448 insertions(+), 4 deletions(-) create mode 100644 src/video_core/swrasterizer/proctex.cpp create mode 100644 src/video_core/swrasterizer/proctex.h 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 return (begin * (base - t) + end * t) / base; } +// bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second +// interpolation. +template +inline auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, const float s, + const float t) { + auto y0 = Lerp(x00, x01, s); + auto y1 = Lerp(x10, x11, s); + return Lerp(y0, y1, t); +} + // Utility vector factories template static inline Vec2 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 shader/shader_interpreter.cpp swrasterizer/clipper.cpp swrasterizer/framebuffer.cpp + swrasterizer/proctex.cpp swrasterizer/rasterizer.cpp swrasterizer/swrasterizer.cpp swrasterizer/texturing.cpp @@ -54,6 +55,7 @@ set(HEADERS shader/shader_interpreter.h swrasterizer/clipper.h swrasterizer/framebuffer.h + swrasterizer/proctex.h swrasterizer/rasterizer.h swrasterizer/swrasterizer.h 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) { break; } + case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[0], 0xb0): + case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[1], 0xb1): + case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[2], 0xb2): + case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[3], 0xb3): + case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[4], 0xb4): + case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[5], 0xb5): + case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[6], 0xb6): + case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[7], 0xb7): { + auto& index = regs.texturing.proctex_lut_config.index; + auto& pt = g_state.proctex; + + switch (regs.texturing.proctex_lut_config.ref_table.Value()) { + case TexturingRegs::ProcTexLutTable::Noise: + pt.noise_table[index % pt.noise_table.size()].raw = value; + break; + case TexturingRegs::ProcTexLutTable::ColorMap: + pt.color_map_table[index % pt.color_map_table.size()].raw = value; + break; + case TexturingRegs::ProcTexLutTable::AlphaMap: + pt.alpha_map_table[index % pt.alpha_map_table.size()].raw = value; + break; + case TexturingRegs::ProcTexLutTable::Color: + pt.color_table[index % pt.color_table.size()].raw = value; + break; + case TexturingRegs::ProcTexLutTable::ColorDiff: + pt.color_diff_table[index % pt.color_diff_table.size()].raw = value; + break; + } + index.Assign(index + 1); + break; + } default: break; } 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 @@ #include #include "common/bit_field.h" #include "common/common_types.h" +#include "common/vector_math.h" #include "video_core/primitive_assembly.h" #include "video_core/regs.h" #include "video_core/shader/shader.h" @@ -25,6 +26,59 @@ struct State { Shader::AttributeBuffer input_default_attributes; + struct ProcTex { + union ValueEntry { + u32 raw; + + // LUT value, encoded as 12-bit fixed point, with 12 fraction bits + BitField<0, 12, u32> value; // 0.0.12 fixed point + + // Difference between two entry values. Used for efficient interpolation. + // 0.0.12 fixed point with two's complement. The range is [-0.5, 0.5). + // Note: the type of this is different from the one of lighting LUT + BitField<12, 12, s32> difference; + + float ToFloat() const { + return static_cast(value) / 4095.f; + } + + float DiffToFloat() const { + return static_cast(difference) / 4095.f; + } + }; + + union ColorEntry { + u32 raw; + BitField<0, 8, u32> r; + BitField<8, 8, u32> g; + BitField<16, 8, u32> b; + BitField<24, 8, u32> a; + + Math::Vec4 ToVector() const { + return {static_cast(r), static_cast(g), static_cast(b), + static_cast(a)}; + } + }; + + union ColorDifferenceEntry { + u32 raw; + BitField<0, 8, s32> r; // half of the difference between two ColorEntry + BitField<8, 8, s32> g; + BitField<16, 8, s32> b; + BitField<24, 8, s32> a; + + Math::Vec4 ToVector() const { + return Math::Vec4{r, g, b, a} * 2; + } + }; + + std::array noise_table; + std::array color_map_table; + std::array alpha_map_table; + std::array color_table; + std::array color_diff_table; + } proctex; + struct { union LutEntry { // 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); ASSERT_REG_POSITION(texturing.texture1_format, 0x96); ASSERT_REG_POSITION(texturing.texture2, 0x99); ASSERT_REG_POSITION(texturing.texture2_format, 0x9e); +ASSERT_REG_POSITION(texturing.proctex, 0xa8); +ASSERT_REG_POSITION(texturing.proctex_noise_u, 0xa9); +ASSERT_REG_POSITION(texturing.proctex_noise_v, 0xaa); +ASSERT_REG_POSITION(texturing.proctex_noise_frequency, 0xab); +ASSERT_REG_POSITION(texturing.proctex_lut, 0xac); +ASSERT_REG_POSITION(texturing.proctex_lut_offset, 0xad); +ASSERT_REG_POSITION(texturing.proctex_lut_config, 0xaf); ASSERT_REG_POSITION(texturing.tev_stage0, 0xc0); ASSERT_REG_POSITION(texturing.tev_stage1, 0xc8); 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 { BitField<0, 1, u32> texture0_enable; BitField<1, 1, u32> texture1_enable; BitField<2, 1, u32> texture2_enable; - BitField<8, 2, u32> texture3_coordinates; // TODO: unimplemented - BitField<10, 1, u32> texture3_enable; // TODO: unimplemented + BitField<8, 2, u32> texture3_coordinates; + BitField<10, 1, u32> texture3_enable; BitField<13, 1, u32> texture2_use_coord1; BitField<16, 1, u32> clear_texture_cache; // TODO: unimplemented } main_config; @@ -137,7 +137,7 @@ struct TexturingRegs { INSERT_PADDING_WORDS(0x2); TextureConfig texture2; BitField<0, 4, TextureFormat> texture2_format; - INSERT_PADDING_WORDS(0x21); + INSERT_PADDING_WORDS(0x9); struct FullTextureConfig { const bool enabled; @@ -152,6 +152,96 @@ struct TexturingRegs { }}; } + // 0xa8-0xad: ProcTex Config + enum class ProcTexClamp : u32 { + ToZero = 0, + ToEdge = 1, + SymmetricalRepeat = 2, + MirroredRepeat = 3, + Pulse = 4, + }; + + enum class ProcTexCombiner : u32 { + U = 0, // u + U2 = 1, // u * u + V = 2, // v + V2 = 3, // v * v + Add = 4, // (u + v) / 2 + Add2 = 5, // (u * u + v * v) / 2 + SqrtAdd2 = 6, // sqrt(u * u + v * v) + Min = 7, // min(u, v) + Max = 8, // max(u, v) + RMax = 9, // Average of Max and SqrtAdd2 + }; + + enum class ProcTexShift : u32 { + None = 0, + Odd = 1, + Even = 2, + }; + + union { + BitField<0, 3, ProcTexClamp> u_clamp; + BitField<3, 3, ProcTexClamp> v_clamp; + BitField<6, 4, ProcTexCombiner> color_combiner; + BitField<10, 4, ProcTexCombiner> alpha_combiner; + BitField<14, 1, u32> separate_alpha; + BitField<15, 1, u32> noise_enable; + BitField<16, 2, ProcTexShift> u_shift; + BitField<18, 2, ProcTexShift> v_shift; + BitField<20, 8, u32> bias_low; // float16 TODO: unimplemented + } proctex; + + union ProcTexNoiseConfig { + BitField<0, 16, s32> amplitude; // fixed1.3.12 + BitField<16, 16, u32> phase; // float16 + }; + + ProcTexNoiseConfig proctex_noise_u; + ProcTexNoiseConfig proctex_noise_v; + + union { + BitField<0, 16, u32> u; // float16 + BitField<16, 16, u32> v; // float16 + } proctex_noise_frequency; + + enum class ProcTexFilter : u32 { + Nearest = 0, + Linear = 1, + NearestMipmapNearest = 2, + LinearMipmapNearest = 3, + NearestMipmapLinear = 4, + LinearMipmapLinear = 5, + }; + + union { + BitField<0, 3, ProcTexFilter> filter; + BitField<11, 8, u32> width; + BitField<19, 8, u32> bias_high; // TODO: unimplemented + } proctex_lut; + + BitField<0, 8, u32> proctex_lut_offset; + + INSERT_PADDING_WORDS(0x1); + + // 0xaf-0xb7: ProcTex LUT + enum class ProcTexLutTable : u32 { + Noise = 0, + ColorMap = 2, + AlphaMap = 3, + Color = 4, + ColorDiff = 5, + }; + + union { + BitField<0, 8, u32> index; + BitField<8, 4, ProcTexLutTable> ref_table; + } proctex_lut_config; + + u32 proctex_lut_data[8]; + + INSERT_PADDING_WORDS(0x8); + // 0xc0-0xff: Texture Combiner (akin to glTexEnv) struct TevStageConfig { 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 @@ +// Copyright 2017 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include +#include "common/math_util.h" +#include "video_core/swrasterizer/proctex.h" + +namespace Pica { +namespace Rasterizer { + +using ProcTexClamp = TexturingRegs::ProcTexClamp; +using ProcTexShift = TexturingRegs::ProcTexShift; +using ProcTexCombiner = TexturingRegs::ProcTexCombiner; +using ProcTexFilter = TexturingRegs::ProcTexFilter; + +static float LookupLUT(const std::array& lut, float coord) { + // For NoiseLUT/ColorMap/AlphaMap, coord=0.0 is lut[0], coord=127.0/128.0 is lut[127] and + // coord=1.0 is lut[127]+lut_diff[127]. For other indices, the result is interpolated using + // value entries and difference entries. + coord *= 128; + const int index_int = std::min(static_cast(coord), 127); + const float frac = coord - index_int; + return lut[index_int].ToFloat() + frac * lut[index_int].DiffToFloat(); +} + +// These function are used to generate random noise for procedural texture. Their results are +// verified against real hardware, but it's not known if the algorithm is the same as hardware. +static unsigned int NoiseRand1D(unsigned int v) { + static constexpr std::array table{ + {0, 4, 10, 8, 4, 9, 7, 12, 5, 15, 13, 14, 11, 15, 2, 11}}; + return ((v % 9 + 2) * 3 & 0xF) ^ table[(v / 9) & 0xF]; +} + +static float NoiseRand2D(unsigned int x, unsigned int y) { + static constexpr std::array table{ + {10, 2, 15, 8, 0, 7, 4, 5, 5, 13, 2, 6, 13, 9, 3, 14}}; + unsigned int u2 = NoiseRand1D(x); + unsigned int v2 = NoiseRand1D(y); + v2 += ((u2 & 3) == 1) ? 4 : 0; + v2 ^= (u2 & 1) * 6; + v2 += 10 + u2; + v2 &= 0xF; + v2 ^= table[u2]; + return -1.0f + v2 * 2.0f / 15.0f; +} + +static float NoiseCoef(float u, float v, TexturingRegs regs, State::ProcTex state) { + const float freq_u = float16::FromRaw(regs.proctex_noise_frequency.u).ToFloat32(); + const float freq_v = float16::FromRaw(regs.proctex_noise_frequency.v).ToFloat32(); + const float phase_u = float16::FromRaw(regs.proctex_noise_u.phase).ToFloat32(); + const float phase_v = float16::FromRaw(regs.proctex_noise_v.phase).ToFloat32(); + const float x = 9 * freq_u * std::abs(u + phase_u); + const float y = 9 * freq_v * std::abs(v + phase_v); + const int x_int = static_cast(x); + const int y_int = static_cast(y); + const float x_frac = x - x_int; + const float y_frac = y - y_int; + + const float g0 = NoiseRand2D(x_int, y_int) * (x_frac + y_frac); + const float g1 = NoiseRand2D(x_int + 1, y_int) * (x_frac + y_frac - 1); + const float g2 = NoiseRand2D(x_int, y_int + 1) * (x_frac + y_frac - 1); + const float g3 = NoiseRand2D(x_int + 1, y_int + 1) * (x_frac + y_frac - 2); + const float x_noise = LookupLUT(state.noise_table, x_frac); + const float y_noise = LookupLUT(state.noise_table, y_frac); + return Math::BilinearInterp(g0, g1, g2, g3, x_noise, y_noise); +} + +static float GetShiftOffset(float v, ProcTexShift mode, ProcTexClamp clamp_mode) { + const float offset = (clamp_mode == ProcTexClamp::MirroredRepeat) ? 1 : 0.5f; + switch (mode) { + case ProcTexShift::None: + return 0; + case ProcTexShift::Odd: + return offset * (((int)v / 2) % 2); + case ProcTexShift::Even: + return offset * ((((int)v + 1) / 2) % 2); + default: + LOG_CRITICAL(HW_GPU, "Unknown shift mode %u", static_cast(mode)); + return 0; + } +}; + +static void ClampCoord(float& coord, ProcTexClamp mode) { + switch (mode) { + case ProcTexClamp::ToZero: + if (coord > 1.0f) + coord = 0.0f; + break; + case ProcTexClamp::ToEdge: + coord = std::min(coord, 1.0f); + break; + case ProcTexClamp::SymmetricalRepeat: + coord = coord - std::floor(coord); + break; + case ProcTexClamp::MirroredRepeat: { + int integer = static_cast(coord); + float frac = coord - integer; + coord = (integer % 2) == 0 ? frac : (1.0f - frac); + break; + } + case ProcTexClamp::Pulse: + if (coord <= 0.5f) + coord = 0.0f; + else + coord = 1.0f; + break; + default: + LOG_CRITICAL(HW_GPU, "Unknown clamp mode %u", static_cast(mode)); + coord = std::min(coord, 1.0f); + break; + } +} + +float CombineAndMap(float u, float v, ProcTexCombiner combiner, + const std::array& map_table) { + float f; + switch (combiner) { + case ProcTexCombiner::U: + f = u; + break; + case ProcTexCombiner::U2: + f = u * u; + break; + case TexturingRegs::ProcTexCombiner::V: + f = v; + break; + case TexturingRegs::ProcTexCombiner::V2: + f = v * v; + break; + case TexturingRegs::ProcTexCombiner::Add: + f = (u + v) * 0.5f; + break; + case TexturingRegs::ProcTexCombiner::Add2: + f = (u * u + v * v) * 0.5f; + break; + case TexturingRegs::ProcTexCombiner::SqrtAdd2: + f = std::min(std::sqrt(u * u + v * v), 1.0f); + break; + case TexturingRegs::ProcTexCombiner::Min: + f = std::min(u, v); + break; + case TexturingRegs::ProcTexCombiner::Max: + f = std::max(u, v); + break; + case TexturingRegs::ProcTexCombiner::RMax: + f = std::min(((u + v) * 0.5f + std::sqrt(u * u + v * v)) * 0.5f, 1.0f); + break; + default: + LOG_CRITICAL(HW_GPU, "Unknown combiner %u", static_cast(combiner)); + f = 0.0f; + break; + } + return LookupLUT(map_table, f); +} + +Math::Vec4 ProcTex(float u, float v, TexturingRegs regs, State::ProcTex state) { + u = std::abs(u); + v = std::abs(v); + + // Get shift offset before noise generation + const float u_shift = GetShiftOffset(v, regs.proctex.u_shift, regs.proctex.u_clamp); + const float v_shift = GetShiftOffset(u, regs.proctex.v_shift, regs.proctex.v_clamp); + + // Generate noise + if (regs.proctex.noise_enable) { + float noise = NoiseCoef(u, v, regs, state); + u += noise * regs.proctex_noise_u.amplitude / 4095.0f; + v += noise * regs.proctex_noise_v.amplitude / 4095.0f; + u = std::abs(u); + v = std::abs(v); + } + + // Shift + u += u_shift; + v += v_shift; + + // Clamp + ClampCoord(u, regs.proctex.u_clamp); + ClampCoord(v, regs.proctex.v_clamp); + + // Combine and map + const float lut_coord = CombineAndMap(u, v, regs.proctex.color_combiner, state.color_map_table); + + // Look up the color + // For the color lut, coord=0.0 is lut[offset] and coord=1.0 is lut[offset+width-1] + const u32 offset = regs.proctex_lut_offset; + const u32 width = regs.proctex_lut.width; + const float index = offset + (lut_coord * (width - 1)); + Math::Vec4 final_color; + // TODO(wwylele): implement mipmap + switch (regs.proctex_lut.filter) { + case ProcTexFilter::Linear: + case ProcTexFilter::LinearMipmapLinear: + case ProcTexFilter::LinearMipmapNearest: { + const int index_int = static_cast(index); + const float frac = index - index_int; + const auto color_value = state.color_table[index_int].ToVector().Cast(); + const auto color_diff = state.color_diff_table[index_int].ToVector().Cast(); + final_color = (color_value + frac * color_diff).Cast(); + break; + } + case ProcTexFilter::Nearest: + case ProcTexFilter::NearestMipmapLinear: + case ProcTexFilter::NearestMipmapNearest: + final_color = state.color_table[static_cast(std::round(index))].ToVector(); + break; + } + + if (regs.proctex.separate_alpha) { + // Note: in separate alpha mode, the alpha channel skips the color LUT look up stage. It + // uses the output of CombineAndMap directly instead. + const float final_alpha = + CombineAndMap(u, v, regs.proctex.alpha_combiner, state.alpha_map_table); + return Math::MakeVec(final_color.rgb(), static_cast(final_alpha * 255)); + } else { + return final_color; + } +} + +} // namespace Rasterizer +} // 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 @@ +// Copyright 2017 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/common_types.h" +#include "common/vector_math.h" +#include "video_core/pica_state.h" + +namespace Pica { +namespace Rasterizer { + +/// Generates procedural texture color for the given coordinates +Math::Vec4 ProcTex(float u, float v, TexturingRegs regs, State::ProcTex state); + +} // namespace Rasterizer +} // 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 @@ #include "video_core/regs_texturing.h" #include "video_core/shader/shader.h" #include "video_core/swrasterizer/framebuffer.h" +#include "video_core/swrasterizer/proctex.h" #include "video_core/swrasterizer/rasterizer.h" #include "video_core/swrasterizer/texturing.h" #include "video_core/texture/texture_decode.h" @@ -268,7 +269,7 @@ static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Ve uv[2].u() = GetInterpolatedAttribute(v0.tc2.u(), v1.tc2.u(), v2.tc2.u()); uv[2].v() = GetInterpolatedAttribute(v0.tc2.v(), v1.tc2.v(), v2.tc2.v()); - Math::Vec4 texture_color[3]{}; + Math::Vec4 texture_color[4]{}; for (int i = 0; i < 3; ++i) { const auto& texture = textures[i]; if (!texture.enabled) @@ -334,6 +335,13 @@ static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Ve } } + // sample procedural texture + if (regs.texturing.main_config.texture3_enable) { + const auto& proctex_uv = uv[regs.texturing.main_config.texture3_coordinates]; + texture_color[3] = ProcTex(proctex_uv.u().ToFloat32(), proctex_uv.v().ToFloat32(), + g_state.regs.texturing, g_state.proctex); + } + // Texture environment - consists of 6 stages of color and alpha combining. // // 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 case Source::Texture2: return texture_color[2]; + case Source::Texture3: + return texture_color[3]; + case Source::PreviousBuffer: return combiner_buffer;