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