diff options
Diffstat (limited to 'src/video_core/swrasterizer/texturing.cpp')
| -rw-r--r-- | src/video_core/swrasterizer/texturing.cpp | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/src/video_core/swrasterizer/texturing.cpp b/src/video_core/swrasterizer/texturing.cpp new file mode 100644 index 000000000..eb18e4ba4 --- /dev/null +++ b/src/video_core/swrasterizer/texturing.cpp | |||
| @@ -0,0 +1,228 @@ | |||
| 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 <algorithm> | ||
| 6 | |||
| 7 | #include "common/assert.h" | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "common/math_util.h" | ||
| 10 | #include "common/vector_math.h" | ||
| 11 | #include "video_core/regs_texturing.h" | ||
| 12 | #include "video_core/swrasterizer/texturing.h" | ||
| 13 | |||
| 14 | namespace Pica { | ||
| 15 | namespace Rasterizer { | ||
| 16 | |||
| 17 | using TevStageConfig = TexturingRegs::TevStageConfig; | ||
| 18 | |||
| 19 | int GetWrappedTexCoord(TexturingRegs::TextureConfig::WrapMode mode, int val, unsigned size) { | ||
| 20 | switch (mode) { | ||
| 21 | case TexturingRegs::TextureConfig::ClampToEdge: | ||
| 22 | val = std::max(val, 0); | ||
| 23 | val = std::min(val, (int)size - 1); | ||
| 24 | return val; | ||
| 25 | |||
| 26 | case TexturingRegs::TextureConfig::ClampToBorder: | ||
| 27 | return val; | ||
| 28 | |||
| 29 | case TexturingRegs::TextureConfig::Repeat: | ||
| 30 | return (int)((unsigned)val % size); | ||
| 31 | |||
| 32 | case TexturingRegs::TextureConfig::MirroredRepeat: { | ||
| 33 | unsigned int coord = ((unsigned)val % (2 * size)); | ||
| 34 | if (coord >= size) | ||
| 35 | coord = 2 * size - 1 - coord; | ||
| 36 | return (int)coord; | ||
| 37 | } | ||
| 38 | |||
| 39 | default: | ||
| 40 | LOG_ERROR(HW_GPU, "Unknown texture coordinate wrapping mode %x", (int)mode); | ||
| 41 | UNIMPLEMENTED(); | ||
| 42 | return 0; | ||
| 43 | } | ||
| 44 | }; | ||
| 45 | |||
| 46 | Math::Vec3<u8> GetColorModifier(TevStageConfig::ColorModifier factor, | ||
| 47 | const Math::Vec4<u8>& values) { | ||
| 48 | using ColorModifier = TevStageConfig::ColorModifier; | ||
| 49 | |||
| 50 | switch (factor) { | ||
| 51 | case ColorModifier::SourceColor: | ||
| 52 | return values.rgb(); | ||
| 53 | |||
| 54 | case ColorModifier::OneMinusSourceColor: | ||
| 55 | return (Math::Vec3<u8>(255, 255, 255) - values.rgb()).Cast<u8>(); | ||
| 56 | |||
| 57 | case ColorModifier::SourceAlpha: | ||
| 58 | return values.aaa(); | ||
| 59 | |||
| 60 | case ColorModifier::OneMinusSourceAlpha: | ||
| 61 | return (Math::Vec3<u8>(255, 255, 255) - values.aaa()).Cast<u8>(); | ||
| 62 | |||
| 63 | case ColorModifier::SourceRed: | ||
| 64 | return values.rrr(); | ||
| 65 | |||
| 66 | case ColorModifier::OneMinusSourceRed: | ||
| 67 | return (Math::Vec3<u8>(255, 255, 255) - values.rrr()).Cast<u8>(); | ||
| 68 | |||
| 69 | case ColorModifier::SourceGreen: | ||
| 70 | return values.ggg(); | ||
| 71 | |||
| 72 | case ColorModifier::OneMinusSourceGreen: | ||
| 73 | return (Math::Vec3<u8>(255, 255, 255) - values.ggg()).Cast<u8>(); | ||
| 74 | |||
| 75 | case ColorModifier::SourceBlue: | ||
| 76 | return values.bbb(); | ||
| 77 | |||
| 78 | case ColorModifier::OneMinusSourceBlue: | ||
| 79 | return (Math::Vec3<u8>(255, 255, 255) - values.bbb()).Cast<u8>(); | ||
| 80 | } | ||
| 81 | }; | ||
| 82 | |||
| 83 | u8 GetAlphaModifier(TevStageConfig::AlphaModifier factor, const Math::Vec4<u8>& values) { | ||
| 84 | using AlphaModifier = TevStageConfig::AlphaModifier; | ||
| 85 | |||
| 86 | switch (factor) { | ||
| 87 | case AlphaModifier::SourceAlpha: | ||
| 88 | return values.a(); | ||
| 89 | |||
| 90 | case AlphaModifier::OneMinusSourceAlpha: | ||
| 91 | return 255 - values.a(); | ||
| 92 | |||
| 93 | case AlphaModifier::SourceRed: | ||
| 94 | return values.r(); | ||
| 95 | |||
| 96 | case AlphaModifier::OneMinusSourceRed: | ||
| 97 | return 255 - values.r(); | ||
| 98 | |||
| 99 | case AlphaModifier::SourceGreen: | ||
| 100 | return values.g(); | ||
| 101 | |||
| 102 | case AlphaModifier::OneMinusSourceGreen: | ||
| 103 | return 255 - values.g(); | ||
| 104 | |||
| 105 | case AlphaModifier::SourceBlue: | ||
| 106 | return values.b(); | ||
| 107 | |||
| 108 | case AlphaModifier::OneMinusSourceBlue: | ||
| 109 | return 255 - values.b(); | ||
| 110 | } | ||
| 111 | }; | ||
| 112 | |||
| 113 | Math::Vec3<u8> ColorCombine(TevStageConfig::Operation op, const Math::Vec3<u8> input[3]) { | ||
| 114 | using Operation = TevStageConfig::Operation; | ||
| 115 | |||
| 116 | switch (op) { | ||
| 117 | case Operation::Replace: | ||
| 118 | return input[0]; | ||
| 119 | |||
| 120 | case Operation::Modulate: | ||
| 121 | return ((input[0] * input[1]) / 255).Cast<u8>(); | ||
| 122 | |||
| 123 | case Operation::Add: { | ||
| 124 | auto result = input[0] + input[1]; | ||
| 125 | result.r() = std::min(255, result.r()); | ||
| 126 | result.g() = std::min(255, result.g()); | ||
| 127 | result.b() = std::min(255, result.b()); | ||
| 128 | return result.Cast<u8>(); | ||
| 129 | } | ||
| 130 | |||
| 131 | case Operation::AddSigned: { | ||
| 132 | // TODO(bunnei): Verify that the color conversion from (float) 0.5f to | ||
| 133 | // (byte) 128 is correct | ||
| 134 | auto result = | ||
| 135 | input[0].Cast<int>() + input[1].Cast<int>() - Math::MakeVec<int>(128, 128, 128); | ||
| 136 | result.r() = MathUtil::Clamp<int>(result.r(), 0, 255); | ||
| 137 | result.g() = MathUtil::Clamp<int>(result.g(), 0, 255); | ||
| 138 | result.b() = MathUtil::Clamp<int>(result.b(), 0, 255); | ||
| 139 | return result.Cast<u8>(); | ||
| 140 | } | ||
| 141 | |||
| 142 | case Operation::Lerp: | ||
| 143 | return ((input[0] * input[2] + | ||
| 144 | input[1] * (Math::MakeVec<u8>(255, 255, 255) - input[2]).Cast<u8>()) / | ||
| 145 | 255) | ||
| 146 | .Cast<u8>(); | ||
| 147 | |||
| 148 | case Operation::Subtract: { | ||
| 149 | auto result = input[0].Cast<int>() - input[1].Cast<int>(); | ||
| 150 | result.r() = std::max(0, result.r()); | ||
| 151 | result.g() = std::max(0, result.g()); | ||
| 152 | result.b() = std::max(0, result.b()); | ||
| 153 | return result.Cast<u8>(); | ||
| 154 | } | ||
| 155 | |||
| 156 | case Operation::MultiplyThenAdd: { | ||
| 157 | auto result = (input[0] * input[1] + 255 * input[2].Cast<int>()) / 255; | ||
| 158 | result.r() = std::min(255, result.r()); | ||
| 159 | result.g() = std::min(255, result.g()); | ||
| 160 | result.b() = std::min(255, result.b()); | ||
| 161 | return result.Cast<u8>(); | ||
| 162 | } | ||
| 163 | |||
| 164 | case Operation::AddThenMultiply: { | ||
| 165 | auto result = input[0] + input[1]; | ||
| 166 | result.r() = std::min(255, result.r()); | ||
| 167 | result.g() = std::min(255, result.g()); | ||
| 168 | result.b() = std::min(255, result.b()); | ||
| 169 | result = (result * input[2].Cast<int>()) / 255; | ||
| 170 | return result.Cast<u8>(); | ||
| 171 | } | ||
| 172 | case Operation::Dot3_RGB: { | ||
| 173 | // Not fully accurate. Worst case scenario seems to yield a +/-3 error. Some HW results | ||
| 174 | // indicate that the per-component computation can't have a higher precision than 1/256, | ||
| 175 | // while dot3_rgb((0x80,g0,b0), (0x7F,g1,b1)) and dot3_rgb((0x80,g0,b0), (0x80,g1,b1)) give | ||
| 176 | // different results. | ||
| 177 | int result = ((input[0].r() * 2 - 255) * (input[1].r() * 2 - 255) + 128) / 256 + | ||
| 178 | ((input[0].g() * 2 - 255) * (input[1].g() * 2 - 255) + 128) / 256 + | ||
| 179 | ((input[0].b() * 2 - 255) * (input[1].b() * 2 - 255) + 128) / 256; | ||
| 180 | result = std::max(0, std::min(255, result)); | ||
| 181 | return {(u8)result, (u8)result, (u8)result}; | ||
| 182 | } | ||
| 183 | default: | ||
| 184 | LOG_ERROR(HW_GPU, "Unknown color combiner operation %d", (int)op); | ||
| 185 | UNIMPLEMENTED(); | ||
| 186 | return {0, 0, 0}; | ||
| 187 | } | ||
| 188 | }; | ||
| 189 | |||
| 190 | u8 AlphaCombine(TevStageConfig::Operation op, const std::array<u8, 3>& input) { | ||
| 191 | switch (op) { | ||
| 192 | using Operation = TevStageConfig::Operation; | ||
| 193 | case Operation::Replace: | ||
| 194 | return input[0]; | ||
| 195 | |||
| 196 | case Operation::Modulate: | ||
| 197 | return input[0] * input[1] / 255; | ||
| 198 | |||
| 199 | case Operation::Add: | ||
| 200 | return std::min(255, input[0] + input[1]); | ||
| 201 | |||
| 202 | case Operation::AddSigned: { | ||
| 203 | // TODO(bunnei): Verify that the color conversion from (float) 0.5f to (byte) 128 is correct | ||
| 204 | auto result = static_cast<int>(input[0]) + static_cast<int>(input[1]) - 128; | ||
| 205 | return static_cast<u8>(MathUtil::Clamp<int>(result, 0, 255)); | ||
| 206 | } | ||
| 207 | |||
| 208 | case Operation::Lerp: | ||
| 209 | return (input[0] * input[2] + input[1] * (255 - input[2])) / 255; | ||
| 210 | |||
| 211 | case Operation::Subtract: | ||
| 212 | return std::max(0, (int)input[0] - (int)input[1]); | ||
| 213 | |||
| 214 | case Operation::MultiplyThenAdd: | ||
| 215 | return std::min(255, (input[0] * input[1] + 255 * input[2]) / 255); | ||
| 216 | |||
| 217 | case Operation::AddThenMultiply: | ||
| 218 | return (std::min(255, (input[0] + input[1])) * input[2]) / 255; | ||
| 219 | |||
| 220 | default: | ||
| 221 | LOG_ERROR(HW_GPU, "Unknown alpha combiner operation %d", (int)op); | ||
| 222 | UNIMPLEMENTED(); | ||
| 223 | return 0; | ||
| 224 | } | ||
| 225 | }; | ||
| 226 | |||
| 227 | } // namespace Rasterizer | ||
| 228 | } // namespace Pica | ||