blob: b53c031b231863179a95134e68c2b34379482aa0 [file] [log] [blame]
John Bauman89401822014-05-06 15:04:28 -04001// SwiftShader Software Renderer
2//
John Bauman66b8ab22014-05-06 15:57:45 -04003// Copyright(c) 2005-2013 TransGaming Inc.
John Bauman89401822014-05-06 15:04:28 -04004//
5// All rights reserved. No part of this software may be copied, distributed, transmitted,
6// transcribed, stored in a retrieval system, translated into any human or computer
7// language by any means, or disclosed to third parties without the explicit written
8// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9// or implied, including but not limited to any patent rights, are granted to you.
10//
11
12#include "PixelRoutine.hpp"
13
14#include "Renderer.hpp"
John Bauman89401822014-05-06 15:04:28 -040015#include "QuadRasterizer.hpp"
16#include "Surface.hpp"
17#include "Primitive.hpp"
18#include "CPUID.hpp"
19#include "SamplerCore.hpp"
20#include "Constants.hpp"
21#include "Debug.hpp"
22
John Bauman89401822014-05-06 15:04:28 -040023namespace sw
24{
25 extern bool complementaryDepthBuffer;
26 extern bool postBlendSRGB;
27 extern bool exactColorRounding;
Alexis Hetuf2a8c372015-07-13 11:08:41 -040028 extern bool forceClearRegisters;
John Bauman89401822014-05-06 15:04:28 -040029
Alexis Hetuf2a8c372015-07-13 11:08:41 -040030 PixelRoutine::Registers::Registers(const PixelShader *shader) :
31 QuadRasterizer::Registers(),
32 rf(shader && shader->dynamicallyIndexedTemporaries),
33 vf(shader && shader->dynamicallyIndexedInput)
John Bauman89401822014-05-06 15:04:28 -040034 {
Alexis Hetuf2a8c372015-07-13 11:08:41 -040035 if(!shader || shader->getVersion() < 0x0200 || forceClearRegisters)
John Bauman89401822014-05-06 15:04:28 -040036 {
Alexis Hetuf2a8c372015-07-13 11:08:41 -040037 for(int i = 0; i < 10; i++)
38 {
39 vf[i].x = Float4(0.0f);
40 vf[i].y = Float4(0.0f);
41 vf[i].z = Float4(0.0f);
42 vf[i].w = Float4(0.0f);
43 }
John Bauman89401822014-05-06 15:04:28 -040044 }
45 }
46
Alexis Hetuf2a8c372015-07-13 11:08:41 -040047 PixelRoutine::PixelRoutine(const PixelProcessor::State &state, const PixelShader *shader) : QuadRasterizer(state, shader)
48 {
49 }
50
John Bauman89401822014-05-06 15:04:28 -040051 PixelRoutine::~PixelRoutine()
52 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040053 for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040054 {
55 delete sampler[i];
56 }
57 }
58
Alexis Hetuf2a8c372015-07-13 11:08:41 -040059 void PixelRoutine::quad(QuadRasterizer::Registers &rBase, Pointer<Byte> cBuffer[4], Pointer<Byte> &zBuffer, Pointer<Byte> &sBuffer, Int cMask[4], Int &x, Int &y)
John Bauman89401822014-05-06 15:04:28 -040060 {
Alexis Hetuf2a8c372015-07-13 11:08:41 -040061 Registers& r = *static_cast<Registers*>(&rBase);
62
John Bauman89401822014-05-06 15:04:28 -040063 #if PERF_PROFILE
64 Long pipeTime = Ticks();
65 #endif
66
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040067 for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040068 {
69 sampler[i] = new SamplerCore(r.constants, state.sampler[i]);
70 }
71
72 const bool earlyDepthTest = !state.depthOverride && !state.alphaTestActive();
John Bauman89401822014-05-06 15:04:28 -040073
74 Int zMask[4]; // Depth mask
75 Int sMask[4]; // Stencil mask
76
77 for(unsigned int q = 0; q < state.multiSample; q++)
78 {
79 zMask[q] = cMask[q];
80 sMask[q] = cMask[q];
81 }
82
83 for(unsigned int q = 0; q < state.multiSample; q++)
84 {
85 stencilTest(r, sBuffer, q, x, sMask[q], cMask[q]);
86 }
87
88 Float4 f;
89
John Bauman89401822014-05-06 15:04:28 -040090 Float4 (&z)[4] = r.z;
John Bauman19bac1e2014-05-06 15:23:49 -040091 Float4 &w = r.w;
John Bauman89401822014-05-06 15:04:28 -040092 Float4 &rhw = r.rhw;
93 Float4 rhwCentroid;
94
95 Float4 xxxx = Float4(Float(x)) + *Pointer<Float4>(r.primitive + OFFSET(Primitive,xQuad), 16);
John Bauman89401822014-05-06 15:04:28 -040096
John Bauman19bac1e2014-05-06 15:23:49 -040097 if(interpolateZ())
John Bauman89401822014-05-06 15:04:28 -040098 {
99 for(unsigned int q = 0; q < state.multiSample; q++)
100 {
101 Float4 x = xxxx;
102
103 if(state.multiSample > 1)
104 {
105 x -= *Pointer<Float4>(r.constants + OFFSET(Constants,X) + q * sizeof(float4));
106 }
107
108 z[q] = interpolate(x, r.Dz[q], z[q], r.primitive + OFFSET(Primitive,z), false, false);
109 }
110 }
111
112 Bool depthPass = false;
113
114 if(earlyDepthTest)
115 {
116 for(unsigned int q = 0; q < state.multiSample; q++)
117 {
118 depthPass = depthPass || depthTest(r, zBuffer, q, x, z[q], sMask[q], zMask[q], cMask[q]);
119 }
120 }
121
122 If(depthPass || Bool(!earlyDepthTest))
123 {
124 #if PERF_PROFILE
125 Long interpTime = Ticks();
126 #endif
127
Nicolas Capens66be2452015-01-27 14:58:57 -0500128 Float4 yyyy = Float4(Float(y)) + *Pointer<Float4>(r.primitive + OFFSET(Primitive,yQuad), 16);
Nicolas Capenscbefe532014-10-16 00:16:01 -0400129
John Bauman89401822014-05-06 15:04:28 -0400130 // Centroid locations
131 Float4 XXXX = Float4(0.0f);
132 Float4 YYYY = Float4(0.0f);
133
134 if(state.centroid)
135 {
136 Float4 WWWW(1.0e-9f);
137
138 for(unsigned int q = 0; q < state.multiSample; q++)
139 {
140 XXXX += *Pointer<Float4>(r.constants + OFFSET(Constants,sampleX[q]) + 16 * cMask[q]);
141 YYYY += *Pointer<Float4>(r.constants + OFFSET(Constants,sampleY[q]) + 16 * cMask[q]);
142 WWWW += *Pointer<Float4>(r.constants + OFFSET(Constants,weight) + 16 * cMask[q]);
143 }
144
145 WWWW = Rcp_pp(WWWW);
146 XXXX *= WWWW;
147 YYYY *= WWWW;
148
149 XXXX += xxxx;
150 YYYY += yyyy;
151 }
152
John Bauman19bac1e2014-05-06 15:23:49 -0400153 if(interpolateW())
John Bauman89401822014-05-06 15:04:28 -0400154 {
John Bauman19bac1e2014-05-06 15:23:49 -0400155 w = interpolate(xxxx, r.Dw, rhw, r.primitive + OFFSET(Primitive,w), false, false);
156 rhw = reciprocal(w);
John Bauman89401822014-05-06 15:04:28 -0400157
158 if(state.centroid)
159 {
160 rhwCentroid = reciprocal(interpolateCentroid(XXXX, YYYY, rhwCentroid, r.primitive + OFFSET(Primitive,w), false, false));
161 }
162 }
163
164 for(int interpolant = 0; interpolant < 10; interpolant++)
165 {
166 for(int component = 0; component < 4; component++)
167 {
John Bauman89401822014-05-06 15:04:28 -0400168 if(state.interpolant[interpolant].component & (1 << component))
169 {
170 if(!state.interpolant[interpolant].centroid)
171 {
Alexis Hetuf2a8c372015-07-13 11:08:41 -0400172 r.vf[interpolant][component] = interpolate(xxxx, r.Dv[interpolant][component], rhw, r.primitive + OFFSET(Primitive, V[interpolant][component]), (state.interpolant[interpolant].flat & (1 << component)) != 0, state.perspective);
John Bauman89401822014-05-06 15:04:28 -0400173 }
174 else
175 {
Alexis Hetuf2a8c372015-07-13 11:08:41 -0400176 r.vf[interpolant][component] = interpolateCentroid(XXXX, YYYY, rhwCentroid, r.primitive + OFFSET(Primitive, V[interpolant][component]), (state.interpolant[interpolant].flat & (1 << component)) != 0, state.perspective);
John Bauman89401822014-05-06 15:04:28 -0400177 }
178 }
179 }
180
181 Float4 rcp;
182
183 switch(state.interpolant[interpolant].project)
184 {
185 case 0:
186 break;
187 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400188 rcp = reciprocal(r.vf[interpolant].y);
189 r.vf[interpolant].x = r.vf[interpolant].x * rcp;
John Bauman89401822014-05-06 15:04:28 -0400190 break;
191 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400192 rcp = reciprocal(r.vf[interpolant].z);
193 r.vf[interpolant].x = r.vf[interpolant].x * rcp;
194 r.vf[interpolant].y = r.vf[interpolant].y * rcp;
John Bauman89401822014-05-06 15:04:28 -0400195 break;
196 case 3:
John Bauman19bac1e2014-05-06 15:23:49 -0400197 rcp = reciprocal(r.vf[interpolant].w);
198 r.vf[interpolant].x = r.vf[interpolant].x * rcp;
199 r.vf[interpolant].y = r.vf[interpolant].y * rcp;
200 r.vf[interpolant].z = r.vf[interpolant].z * rcp;
John Bauman89401822014-05-06 15:04:28 -0400201 break;
202 }
203 }
204
205 if(state.fog.component)
206 {
207 f = interpolate(xxxx, r.Df, rhw, r.primitive + OFFSET(Primitive,f), state.fog.flat & 0x01, state.perspective);
208 }
209
Alexis Hetuf2a8c372015-07-13 11:08:41 -0400210 setBuiltins(r, x, y, z, w);
John Bauman89401822014-05-06 15:04:28 -0400211
212 #if PERF_PROFILE
213 r.cycles[PERF_INTERP] += Ticks() - interpTime;
214 #endif
215
216 Bool alphaPass = true;
217
218 if(colorUsed())
219 {
220 #if PERF_PROFILE
221 Long shaderTime = Ticks();
222 #endif
223
Alexis Hetuf2a8c372015-07-13 11:08:41 -0400224 applyShader(r, cMask);
John Bauman89401822014-05-06 15:04:28 -0400225
226 #if PERF_PROFILE
227 r.cycles[PERF_SHADER] += Ticks() - shaderTime;
228 #endif
229
Alexis Hetuf2a8c372015-07-13 11:08:41 -0400230 alphaPass = alphaTest(r, cMask);
John Bauman89401822014-05-06 15:04:28 -0400231
John Bauman19bac1e2014-05-06 15:23:49 -0400232 if((shader && shader->containsKill()) || state.alphaTestActive())
John Bauman89401822014-05-06 15:04:28 -0400233 {
234 for(unsigned int q = 0; q < state.multiSample; q++)
235 {
236 zMask[q] &= cMask[q];
237 sMask[q] &= cMask[q];
238 }
239 }
240 }
241
242 If(alphaPass)
243 {
244 if(!earlyDepthTest)
245 {
246 for(unsigned int q = 0; q < state.multiSample; q++)
247 {
248 depthPass = depthPass || depthTest(r, zBuffer, q, x, z[q], sMask[q], zMask[q], cMask[q]);
249 }
250 }
251
252 #if PERF_PROFILE
253 Long ropTime = Ticks();
254 #endif
255
256 If(depthPass || Bool(earlyDepthTest))
257 {
258 for(unsigned int q = 0; q < state.multiSample; q++)
259 {
260 if(state.multiSampleMask & (1 << q))
261 {
262 writeDepth(r, zBuffer, q, x, z[q], zMask[q]);
263
264 if(state.occlusionEnabled)
265 {
266 r.occlusion += *Pointer<UInt>(r.constants + OFFSET(Constants,occlusionCount) + 4 * (zMask[q] & sMask[q]));
267 }
268 }
269 }
270
271 if(colorUsed())
272 {
273 #if PERF_PROFILE
John Bauman66b8ab22014-05-06 15:57:45 -0400274 AddAtomic(Pointer<Long>(&profiler.ropOperations), 4);
John Bauman89401822014-05-06 15:04:28 -0400275 #endif
276
Alexis Hetuf2a8c372015-07-13 11:08:41 -0400277 rasterOperation(r, f, cBuffer, x, sMask, zMask, cMask);
John Bauman89401822014-05-06 15:04:28 -0400278 }
279 }
280
281 #if PERF_PROFILE
282 r.cycles[PERF_ROP] += Ticks() - ropTime;
283 #endif
284 }
285 }
286
287 for(unsigned int q = 0; q < state.multiSample; q++)
288 {
289 if(state.multiSampleMask & (1 << q))
290 {
291 writeStencil(r, sBuffer, q, x, sMask[q], zMask[q], cMask[q]);
292 }
293 }
294
295 #if PERF_PROFILE
296 r.cycles[PERF_PIPE] += Ticks() - pipeTime;
297 #endif
298 }
299
John Bauman89401822014-05-06 15:04:28 -0400300 Float4 PixelRoutine::interpolateCentroid(Float4 &x, Float4 &y, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective)
301 {
302 Float4 interpolant = *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,C), 16);
303
304 if(!flat)
305 {
306 interpolant += x * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,A), 16) +
307 y * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,B), 16);
308
309 if(perspective)
310 {
311 interpolant *= rhw;
312 }
313 }
314
315 return interpolant;
316 }
317
318 void PixelRoutine::stencilTest(Registers &r, Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &cMask)
319 {
320 if(!state.stencilActive)
321 {
322 return;
323 }
324
325 // (StencilRef & StencilMask) CompFunc (StencilBufferValue & StencilMask)
326
327 Pointer<Byte> buffer = sBuffer + 2 * x;
328
329 if(q > 0)
330 {
331 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,stencilSliceB));
332 }
333
334 Byte8 value = As<Byte8>(Long1(*Pointer<UInt>(buffer)));
335 Byte8 valueCCW = value;
336
337 if(!state.noStencilMask)
338 {
339 value &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[0].testMaskQ));
340 }
341
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400342 stencilTest(r, value, state.stencilCompareMode, false);
John Bauman89401822014-05-06 15:04:28 -0400343
344 if(state.twoSidedStencil)
345 {
346 if(!state.noStencilMaskCCW)
347 {
348 valueCCW &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[1].testMaskQ));
349 }
350
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400351 stencilTest(r, valueCCW, state.stencilCompareModeCCW, true);
John Bauman89401822014-05-06 15:04:28 -0400352
353 value &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,clockwiseMask));
354 valueCCW &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,invClockwiseMask));
355 value |= valueCCW;
356 }
357
358 sMask = SignMask(value) & cMask;
359 }
360
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400361 void PixelRoutine::stencilTest(Registers &r, Byte8 &value, StencilCompareMode stencilCompareMode, bool CCW)
John Bauman89401822014-05-06 15:04:28 -0400362 {
363 Byte8 equal;
364
365 switch(stencilCompareMode)
366 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400367 case STENCIL_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400368 value = Byte8(0xFFFFFFFFFFFFFFFF);
369 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400370 case STENCIL_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400371 value = Byte8(0x0000000000000000);
372 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400373 case STENCIL_LESS: // a < b ~ b > a
John Bauman89401822014-05-06 15:04:28 -0400374 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
375 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
376 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400377 case STENCIL_EQUAL:
John Bauman89401822014-05-06 15:04:28 -0400378 value = CmpEQ(value, *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
379 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400380 case STENCIL_NOTEQUAL: // a != b ~ !(a == b)
John Bauman89401822014-05-06 15:04:28 -0400381 value = CmpEQ(value, *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
382 value ^= Byte8(0xFFFFFFFFFFFFFFFF);
383 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400384 case STENCIL_LESSEQUAL: // a <= b ~ (b > a) || (a == b)
John Bauman89401822014-05-06 15:04:28 -0400385 equal = value;
386 equal = CmpEQ(equal, *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
387 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
388 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
389 value |= equal;
390 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400391 case STENCIL_GREATER: // a > b
John Bauman89401822014-05-06 15:04:28 -0400392 equal = *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ));
393 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
394 equal = CmpGT(As<SByte8>(equal), As<SByte8>(value));
395 value = equal;
396 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400397 case STENCIL_GREATEREQUAL: // a >= b ~ !(a < b) ~ !(b > a)
John Bauman89401822014-05-06 15:04:28 -0400398 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
399 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
400 value ^= Byte8(0xFFFFFFFFFFFFFFFF);
401 break;
402 default:
403 ASSERT(false);
404 }
405 }
406
407 Bool PixelRoutine::depthTest(Registers &r, Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &sMask, Int &zMask, Int &cMask)
408 {
409 if(!state.depthTestActive)
410 {
411 return true;
412 }
413
414 Float4 Z = z;
415
John Bauman19bac1e2014-05-06 15:23:49 -0400416 if(shader && shader->depthOverride())
John Bauman89401822014-05-06 15:04:28 -0400417 {
418 if(complementaryDepthBuffer)
419 {
John Bauman19bac1e2014-05-06 15:23:49 -0400420 Z = Float4(1.0f) - r.oDepth;
John Bauman89401822014-05-06 15:04:28 -0400421 }
422 else
423 {
424 Z = r.oDepth;
425 }
426 }
427
428 Pointer<Byte> buffer;
429 Int pitch;
430
431 if(!state.quadLayoutDepthBuffer)
432 {
433 buffer = zBuffer + 4 * x;
434 pitch = *Pointer<Int>(r.data + OFFSET(DrawData,depthPitchB));
435 }
436 else
437 {
438 buffer = zBuffer + 8 * x;
439 }
440
441 if(q > 0)
442 {
443 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,depthSliceB));
444 }
445
446 Float4 zValue;
447
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400448 if(state.depthCompareMode != DEPTH_NEVER || (state.depthCompareMode != DEPTH_ALWAYS && !state.depthWriteEnable))
John Bauman89401822014-05-06 15:04:28 -0400449 {
450 if(!state.quadLayoutDepthBuffer)
451 {
452 // FIXME: Properly optimizes?
453 zValue.xy = *Pointer<Float4>(buffer);
454 zValue.zw = *Pointer<Float4>(buffer + pitch - 8);
455 }
456 else
457 {
458 zValue = *Pointer<Float4>(buffer, 16);
459 }
460 }
461
462 Int4 zTest;
463
464 switch(state.depthCompareMode)
465 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400466 case DEPTH_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400467 // Optimized
468 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400469 case DEPTH_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400470 // Optimized
471 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400472 case DEPTH_EQUAL:
John Bauman89401822014-05-06 15:04:28 -0400473 zTest = CmpEQ(zValue, Z);
474 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400475 case DEPTH_NOTEQUAL:
John Bauman89401822014-05-06 15:04:28 -0400476 zTest = CmpNEQ(zValue, Z);
477 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400478 case DEPTH_LESS:
John Bauman89401822014-05-06 15:04:28 -0400479 if(complementaryDepthBuffer)
480 {
481 zTest = CmpLT(zValue, Z);
482 }
483 else
484 {
485 zTest = CmpNLE(zValue, Z);
486 }
487 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400488 case DEPTH_GREATEREQUAL:
John Bauman89401822014-05-06 15:04:28 -0400489 if(complementaryDepthBuffer)
490 {
491 zTest = CmpNLT(zValue, Z);
492 }
493 else
494 {
495 zTest = CmpLE(zValue, Z);
496 }
497 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400498 case DEPTH_LESSEQUAL:
John Bauman89401822014-05-06 15:04:28 -0400499 if(complementaryDepthBuffer)
500 {
501 zTest = CmpLE(zValue, Z);
502 }
503 else
504 {
505 zTest = CmpNLT(zValue, Z);
506 }
507 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400508 case DEPTH_GREATER:
John Bauman89401822014-05-06 15:04:28 -0400509 if(complementaryDepthBuffer)
510 {
511 zTest = CmpNLE(zValue, Z);
512 }
513 else
514 {
515 zTest = CmpLT(zValue, Z);
516 }
517 break;
518 default:
519 ASSERT(false);
520 }
521
522 switch(state.depthCompareMode)
523 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400524 case DEPTH_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400525 zMask = cMask;
526 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400527 case DEPTH_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400528 zMask = 0x0;
529 break;
530 default:
531 zMask = SignMask(zTest) & cMask;
532 break;
533 }
534
535 if(state.stencilActive)
536 {
537 zMask &= sMask;
538 }
539
540 return zMask != 0;
541 }
542
John Bauman89401822014-05-06 15:04:28 -0400543 void PixelRoutine::alphaTest(Registers &r, Int &aMask, Short4 &alpha)
544 {
545 Short4 cmp;
546 Short4 equal;
547
548 switch(state.alphaCompareMode)
549 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400550 case ALPHA_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400551 aMask = 0xF;
552 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400553 case ALPHA_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400554 aMask = 0x0;
555 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400556 case ALPHA_EQUAL:
John Bauman89401822014-05-06 15:04:28 -0400557 cmp = CmpEQ(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
558 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
559 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400560 case ALPHA_NOTEQUAL: // a != b ~ !(a == b)
John Bauman89401822014-05-06 15:04:28 -0400561 cmp = CmpEQ(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4))) ^ Short4((short)0xFFFF, (short)0xFFFF, (short)0xFFFF, (short)0xFFFF); // FIXME
562 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
563 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400564 case ALPHA_LESS: // a < b ~ b > a
John Bauman89401822014-05-06 15:04:28 -0400565 cmp = CmpGT(*Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)), alpha);
566 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
567 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400568 case ALPHA_GREATEREQUAL: // a >= b ~ (a > b) || (a == b) ~ !(b > a) // TODO: Approximate
John Bauman89401822014-05-06 15:04:28 -0400569 equal = CmpEQ(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
570 cmp = CmpGT(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
571 cmp |= equal;
572 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
573 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400574 case ALPHA_LESSEQUAL: // a <= b ~ !(a > b)
John Bauman89401822014-05-06 15:04:28 -0400575 cmp = CmpGT(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4))) ^ Short4((short)0xFFFF, (short)0xFFFF, (short)0xFFFF, (short)0xFFFF); // FIXME
576 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
577 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400578 case ALPHA_GREATER: // a > b
John Bauman89401822014-05-06 15:04:28 -0400579 cmp = CmpGT(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
580 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
581 break;
582 default:
583 ASSERT(false);
584 }
585 }
586
587 void PixelRoutine::alphaToCoverage(Registers &r, Int cMask[4], Float4 &alpha)
588 {
589 Int4 coverage0 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c0)));
590 Int4 coverage1 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c1)));
591 Int4 coverage2 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c2)));
592 Int4 coverage3 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c3)));
593
594 Int aMask0 = SignMask(coverage0);
595 Int aMask1 = SignMask(coverage1);
596 Int aMask2 = SignMask(coverage2);
597 Int aMask3 = SignMask(coverage3);
598
599 cMask[0] &= aMask0;
600 cMask[1] &= aMask1;
601 cMask[2] &= aMask2;
602 cMask[3] &= aMask3;
603 }
604
John Bauman19bac1e2014-05-06 15:23:49 -0400605 void PixelRoutine::fogBlend(Registers &r, Vector4f &c0, Float4 &fog, Float4 &z, Float4 &rhw)
John Bauman89401822014-05-06 15:04:28 -0400606 {
607 if(!state.fogActive)
608 {
609 return;
610 }
611
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400612 if(state.pixelFogMode != FOG_NONE)
John Bauman89401822014-05-06 15:04:28 -0400613 {
614 pixelFog(r, fog, z, rhw);
615
John Bauman19bac1e2014-05-06 15:23:49 -0400616 fog = Min(fog, Float4(1.0f));
617 fog = Max(fog, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400618 }
619
John Bauman19bac1e2014-05-06 15:23:49 -0400620 c0.x -= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[0]));
621 c0.y -= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[1]));
622 c0.z -= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[2]));
John Bauman89401822014-05-06 15:04:28 -0400623
John Bauman19bac1e2014-05-06 15:23:49 -0400624 c0.x *= fog;
625 c0.y *= fog;
626 c0.z *= fog;
John Bauman89401822014-05-06 15:04:28 -0400627
John Bauman19bac1e2014-05-06 15:23:49 -0400628 c0.x += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[0]));
629 c0.y += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[1]));
630 c0.z += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[2]));
John Bauman89401822014-05-06 15:04:28 -0400631 }
632
633 void PixelRoutine::pixelFog(Registers &r, Float4 &visibility, Float4 &z, Float4 &rhw)
634 {
635 Float4 &zw = visibility;
636
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400637 if(state.pixelFogMode != FOG_NONE)
John Bauman89401822014-05-06 15:04:28 -0400638 {
639 if(state.wBasedFog)
640 {
641 zw = rhw;
642 }
643 else
644 {
645 if(complementaryDepthBuffer)
646 {
John Bauman19bac1e2014-05-06 15:23:49 -0400647 zw = Float4(1.0f) - z;
John Bauman89401822014-05-06 15:04:28 -0400648 }
649 else
650 {
651 zw = z;
652 }
653 }
654 }
655
656 switch(state.pixelFogMode)
657 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400658 case FOG_NONE:
John Bauman89401822014-05-06 15:04:28 -0400659 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400660 case FOG_LINEAR:
John Bauman89401822014-05-06 15:04:28 -0400661 zw *= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.scale));
662 zw += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.offset));
663 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400664 case FOG_EXP:
John Bauman89401822014-05-06 15:04:28 -0400665 zw *= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.densityE));
John Bauman19bac1e2014-05-06 15:23:49 -0400666 zw = exponential2(zw, true);
John Bauman89401822014-05-06 15:04:28 -0400667 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400668 case FOG_EXP2:
John Bauman89401822014-05-06 15:04:28 -0400669 zw *= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.densityE2));
670 zw *= zw;
John Bauman19bac1e2014-05-06 15:23:49 -0400671 zw = exponential2(zw, true);
John Bauman89401822014-05-06 15:04:28 -0400672 zw = Rcp_pp(zw);
673 break;
674 default:
675 ASSERT(false);
676 }
677 }
678
John Bauman89401822014-05-06 15:04:28 -0400679 void PixelRoutine::writeDepth(Registers &r, Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &zMask)
680 {
681 if(!state.depthWriteEnable)
682 {
683 return;
684 }
685
686 Float4 Z = z;
687
John Bauman19bac1e2014-05-06 15:23:49 -0400688 if(shader && shader->depthOverride())
John Bauman89401822014-05-06 15:04:28 -0400689 {
690 if(complementaryDepthBuffer)
691 {
John Bauman19bac1e2014-05-06 15:23:49 -0400692 Z = Float4(1.0f) - r.oDepth;
John Bauman89401822014-05-06 15:04:28 -0400693 }
694 else
695 {
696 Z = r.oDepth;
697 }
698 }
699
700 Pointer<Byte> buffer;
701 Int pitch;
702
703 if(!state.quadLayoutDepthBuffer)
704 {
705 buffer = zBuffer + 4 * x;
706 pitch = *Pointer<Int>(r.data + OFFSET(DrawData,depthPitchB));
707 }
708 else
709 {
710 buffer = zBuffer + 8 * x;
711 }
712
713 if(q > 0)
714 {
715 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,depthSliceB));
716 }
717
718 Float4 zValue;
719
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400720 if(state.depthCompareMode != DEPTH_NEVER || (state.depthCompareMode != DEPTH_ALWAYS && !state.depthWriteEnable))
John Bauman89401822014-05-06 15:04:28 -0400721 {
722 if(!state.quadLayoutDepthBuffer)
723 {
724 // FIXME: Properly optimizes?
725 zValue.xy = *Pointer<Float4>(buffer);
726 zValue.zw = *Pointer<Float4>(buffer + pitch - 8);
727 }
728 else
729 {
730 zValue = *Pointer<Float4>(buffer, 16);
731 }
732 }
733
734 Z = As<Float4>(As<Int4>(Z) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X) + zMask * 16, 16));
735 zValue = As<Float4>(As<Int4>(zValue) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X) + zMask * 16, 16));
736 Z = As<Float4>(As<Int4>(Z) | As<Int4>(zValue));
737
738 if(!state.quadLayoutDepthBuffer)
739 {
740 // FIXME: Properly optimizes?
741 *Pointer<Float2>(buffer) = Float2(Z.xy);
742 *Pointer<Float2>(buffer + pitch) = Float2(Z.zw);
743 }
744 else
745 {
746 *Pointer<Float4>(buffer, 16) = Z;
747 }
748 }
749
750 void PixelRoutine::writeStencil(Registers &r, Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &zMask, Int &cMask)
751 {
752 if(!state.stencilActive)
753 {
754 return;
755 }
756
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400757 if(state.stencilPassOperation == OPERATION_KEEP && state.stencilZFailOperation == OPERATION_KEEP && state.stencilFailOperation == OPERATION_KEEP)
John Bauman89401822014-05-06 15:04:28 -0400758 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400759 if(!state.twoSidedStencil || (state.stencilPassOperationCCW == OPERATION_KEEP && state.stencilZFailOperationCCW == OPERATION_KEEP && state.stencilFailOperationCCW == OPERATION_KEEP))
John Bauman89401822014-05-06 15:04:28 -0400760 {
761 return;
762 }
763 }
764
765 if(state.stencilWriteMasked && (!state.twoSidedStencil || state.stencilWriteMaskedCCW))
766 {
767 return;
768 }
769
770 Pointer<Byte> buffer = sBuffer + 2 * x;
771
772 if(q > 0)
773 {
774 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,stencilSliceB));
775 }
776
777 Byte8 bufferValue = As<Byte8>(Long1(*Pointer<UInt>(buffer)));
778
779 Byte8 newValue;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400780 stencilOperation(r, newValue, bufferValue, state.stencilPassOperation, state.stencilZFailOperation, state.stencilFailOperation, false, zMask, sMask);
John Bauman89401822014-05-06 15:04:28 -0400781
782 if(!state.noStencilWriteMask)
783 {
784 Byte8 maskedValue = bufferValue;
785 newValue &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[0].writeMaskQ));
786 maskedValue &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[0].invWriteMaskQ));
787 newValue |= maskedValue;
788 }
789
790 if(state.twoSidedStencil)
791 {
792 Byte8 newValueCCW;
793
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400794 stencilOperation(r, newValueCCW, bufferValue, state.stencilPassOperationCCW, state.stencilZFailOperationCCW, state.stencilFailOperationCCW, true, zMask, sMask);
John Bauman89401822014-05-06 15:04:28 -0400795
796 if(!state.noStencilWriteMaskCCW)
797 {
798 Byte8 maskedValue = bufferValue;
799 newValueCCW &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[1].writeMaskQ));
800 maskedValue &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[1].invWriteMaskQ));
801 newValueCCW |= maskedValue;
802 }
803
804 newValue &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,clockwiseMask));
805 newValueCCW &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,invClockwiseMask));
806 newValue |= newValueCCW;
807 }
808
809 newValue &= *Pointer<Byte8>(r.constants + OFFSET(Constants,maskB4Q) + 8 * cMask);
810 bufferValue &= *Pointer<Byte8>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * cMask);
811 newValue |= bufferValue;
812
813 *Pointer<UInt>(buffer) = UInt(As<Long>(newValue));
814 }
815
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400816 void PixelRoutine::stencilOperation(Registers &r, Byte8 &newValue, Byte8 &bufferValue, StencilOperation stencilPassOperation, StencilOperation stencilZFailOperation, StencilOperation stencilFailOperation, bool CCW, Int &zMask, Int &sMask)
John Bauman89401822014-05-06 15:04:28 -0400817 {
818 Byte8 &pass = newValue;
819 Byte8 fail;
820 Byte8 zFail;
821
822 stencilOperation(r, pass, bufferValue, stencilPassOperation, CCW);
823
824 if(stencilZFailOperation != stencilPassOperation)
825 {
826 stencilOperation(r, zFail, bufferValue, stencilZFailOperation, CCW);
827 }
828
829 if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
830 {
831 stencilOperation(r, fail, bufferValue, stencilFailOperation, CCW);
832 }
833
834 if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
835 {
836 if(state.depthTestActive && stencilZFailOperation != stencilPassOperation) // zMask valid and values not the same
837 {
838 pass &= *Pointer<Byte8>(r.constants + OFFSET(Constants,maskB4Q) + 8 * zMask);
839 zFail &= *Pointer<Byte8>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * zMask);
840 pass |= zFail;
841 }
842
843 pass &= *Pointer<Byte8>(r.constants + OFFSET(Constants,maskB4Q) + 8 * sMask);
844 fail &= *Pointer<Byte8>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * sMask);
845 pass |= fail;
846 }
847 }
848
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400849 void PixelRoutine::stencilOperation(Registers &r, Byte8 &output, Byte8 &bufferValue, StencilOperation operation, bool CCW)
John Bauman89401822014-05-06 15:04:28 -0400850 {
851 switch(operation)
852 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400853 case OPERATION_KEEP:
John Bauman89401822014-05-06 15:04:28 -0400854 output = bufferValue;
855 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400856 case OPERATION_ZERO:
John Bauman89401822014-05-06 15:04:28 -0400857 output = Byte8(0x0000000000000000);
858 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400859 case OPERATION_REPLACE:
John Bauman89401822014-05-06 15:04:28 -0400860 output = *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceQ));
861 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400862 case OPERATION_INCRSAT:
John Bauman89401822014-05-06 15:04:28 -0400863 output = AddSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));
864 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400865 case OPERATION_DECRSAT:
John Bauman89401822014-05-06 15:04:28 -0400866 output = SubSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));
867 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400868 case OPERATION_INVERT:
John Bauman89401822014-05-06 15:04:28 -0400869 output = bufferValue ^ Byte8(0xFFFFFFFFFFFFFFFF);
870 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400871 case OPERATION_INCR:
John Bauman89401822014-05-06 15:04:28 -0400872 output = bufferValue + Byte8(1, 1, 1, 1, 1, 1, 1, 1);
873 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400874 case OPERATION_DECR:
John Bauman89401822014-05-06 15:04:28 -0400875 output = bufferValue - Byte8(1, 1, 1, 1, 1, 1, 1, 1);
876 break;
877 default:
878 ASSERT(false);
879 }
880 }
881
Alexis Hetu96517182015-04-15 10:30:23 -0400882 void PixelRoutine::blendFactor(Registers &r, const Vector4s &blendFactor, const Vector4s &current, const Vector4s &pixel, BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -0400883 {
884 switch(blendFactorActive)
885 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400886 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -0400887 // Optimized
888 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400889 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -0400890 // Optimized
891 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400892 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -0400893 blendFactor.x = current.x;
894 blendFactor.y = current.y;
895 blendFactor.z = current.z;
John Bauman89401822014-05-06 15:04:28 -0400896 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400897 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -0400898 blendFactor.x = Short4(0xFFFFu) - current.x;
899 blendFactor.y = Short4(0xFFFFu) - current.y;
900 blendFactor.z = Short4(0xFFFFu) - current.z;
John Bauman89401822014-05-06 15:04:28 -0400901 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400902 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -0400903 blendFactor.x = pixel.x;
904 blendFactor.y = pixel.y;
905 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -0400906 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400907 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -0400908 blendFactor.x = Short4(0xFFFFu) - pixel.x;
909 blendFactor.y = Short4(0xFFFFu) - pixel.y;
910 blendFactor.z = Short4(0xFFFFu) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -0400911 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400912 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400913 blendFactor.x = current.w;
914 blendFactor.y = current.w;
915 blendFactor.z = current.w;
John Bauman89401822014-05-06 15:04:28 -0400916 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400917 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400918 blendFactor.x = Short4(0xFFFFu) - current.w;
919 blendFactor.y = Short4(0xFFFFu) - current.w;
920 blendFactor.z = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -0400921 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400922 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400923 blendFactor.x = pixel.w;
924 blendFactor.y = pixel.w;
925 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400926 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400927 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400928 blendFactor.x = Short4(0xFFFFu) - pixel.w;
929 blendFactor.y = Short4(0xFFFFu) - pixel.w;
930 blendFactor.z = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400931 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400932 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -0400933 blendFactor.x = Short4(0xFFFFu) - pixel.w;
934 blendFactor.x = Min(As<UShort4>(blendFactor.x), As<UShort4>(current.w));
935 blendFactor.y = blendFactor.x;
936 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -0400937 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400938 case BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -0400939 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[0]));
940 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[1]));
941 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[2]));
John Bauman89401822014-05-06 15:04:28 -0400942 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400943 case BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -0400944 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[0]));
945 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[1]));
946 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[2]));
John Bauman89401822014-05-06 15:04:28 -0400947 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400948 case BLEND_CONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400949 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
950 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
951 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -0400952 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400953 case BLEND_INVCONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400954 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
955 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
956 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -0400957 break;
958 default:
959 ASSERT(false);
960 }
961 }
962
Alexis Hetu96517182015-04-15 10:30:23 -0400963 void PixelRoutine::blendFactorAlpha(Registers &r, const Vector4s &blendFactor, const Vector4s &current, const Vector4s &pixel, BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -0400964 {
965 switch(blendFactorAlphaActive)
966 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400967 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -0400968 // Optimized
969 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400970 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -0400971 // Optimized
972 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400973 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -0400974 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -0400975 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400976 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -0400977 blendFactor.w = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -0400978 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400979 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -0400980 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400981 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400982 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -0400983 blendFactor.w = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400984 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400985 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400986 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -0400987 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400988 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400989 blendFactor.w = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -0400990 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400991 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400992 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400993 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400994 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400995 blendFactor.w = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400996 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400997 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -0400998 blendFactor.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -0400999 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001000 case BLEND_CONSTANT:
1001 case BLEND_CONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001002 blendFactor.w = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04001003 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001004 case BLEND_INVCONSTANT:
1005 case BLEND_INVCONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001006 blendFactor.w = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04001007 break;
1008 default:
1009 ASSERT(false);
1010 }
1011 }
1012
Maxime Grégoired9762742015-07-08 16:43:48 -04001013 void PixelRoutine::readPixel(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4s &current, Int &x, Vector4s &pixel)
John Bauman89401822014-05-06 15:04:28 -04001014 {
John Bauman89401822014-05-06 15:04:28 -04001015 Short4 c01;
1016 Short4 c23;
Maxime Grégoired9762742015-07-08 16:43:48 -04001017 Pointer<Byte> buffer;
John Bauman89401822014-05-06 15:04:28 -04001018
John Bauman89401822014-05-06 15:04:28 -04001019 switch(state.targetFormat[index])
1020 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001021 case FORMAT_R5G6B5:
1022 buffer = cBuffer + 2 * x;
1023 c01 = As<Short4>(Insert(As<Int2>(c01), *Pointer<Int>(buffer), 0));
Maxime Grégoired9762742015-07-08 16:43:48 -04001024 buffer += *Pointer<Int>(r.data + OFFSET(DrawData, colorPitchB[index]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001025 c01 = As<Short4>(Insert(As<Int2>(c01), *Pointer<Int>(buffer), 1));
1026
1027 pixel.x = c01 & Short4(0xF800u);
1028 pixel.y = (c01 & Short4(0x07E0u)) << 5;
1029 pixel.z = (c01 & Short4(0x001Fu)) << 11;
1030 pixel.w = Short4(0xFFFFu);
1031 break;
John Bauman89401822014-05-06 15:04:28 -04001032 case FORMAT_A8R8G8B8:
1033 buffer = cBuffer + 4 * x;
1034 c01 = *Pointer<Short4>(buffer);
Maxime Grégoired9762742015-07-08 16:43:48 -04001035 buffer += *Pointer<Int>(r.data + OFFSET(DrawData, colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04001036 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04001037 pixel.z = c01;
1038 pixel.y = c01;
1039 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
1040 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
1041 pixel.x = pixel.z;
1042 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
1043 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
1044 pixel.y = pixel.z;
1045 pixel.w = pixel.x;
1046 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
1047 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
1048 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
1049 pixel.w = UnpackHigh(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001050 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001051 case FORMAT_A8B8G8R8:
1052 buffer = cBuffer + 4 * x;
1053 c01 = *Pointer<Short4>(buffer);
Maxime Grégoired9762742015-07-08 16:43:48 -04001054 buffer += *Pointer<Int>(r.data + OFFSET(DrawData, colorPitchB[index]));
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001055 c23 = *Pointer<Short4>(buffer);
1056 pixel.z = c01;
1057 pixel.y = c01;
1058 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
1059 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
1060 pixel.x = pixel.z;
1061 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
1062 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
1063 pixel.y = pixel.z;
1064 pixel.w = pixel.x;
1065 pixel.x = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
1066 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
1067 pixel.z = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
1068 pixel.w = UnpackHigh(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
1069 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001070 case FORMAT_A8:
1071 buffer = cBuffer + 1 * x;
1072 pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 0);
Maxime Grégoired9762742015-07-08 16:43:48 -04001073 buffer += *Pointer<Int>(r.data + OFFSET(DrawData, colorPitchB[index]));
John Bauman66b8ab22014-05-06 15:57:45 -04001074 pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 1);
1075 pixel.w = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
1076 pixel.x = Short4(0x0000);
1077 pixel.y = Short4(0x0000);
1078 pixel.z = Short4(0x0000);
1079 break;
John Bauman89401822014-05-06 15:04:28 -04001080 case FORMAT_X8R8G8B8:
1081 buffer = cBuffer + 4 * x;
1082 c01 = *Pointer<Short4>(buffer);
Maxime Grégoired9762742015-07-08 16:43:48 -04001083 buffer += *Pointer<Int>(r.data + OFFSET(DrawData, colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04001084 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04001085 pixel.z = c01;
1086 pixel.y = c01;
1087 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
1088 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
1089 pixel.x = pixel.z;
1090 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
1091 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
1092 pixel.y = pixel.z;
1093 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
1094 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
1095 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
1096 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04001097 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001098 case FORMAT_X8B8G8R8:
1099 buffer = cBuffer + 4 * x;
1100 c01 = *Pointer<Short4>(buffer);
Maxime Grégoired9762742015-07-08 16:43:48 -04001101 buffer += *Pointer<Int>(r.data + OFFSET(DrawData, colorPitchB[index]));
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001102 c23 = *Pointer<Short4>(buffer);
1103 pixel.z = c01;
1104 pixel.y = c01;
1105 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
1106 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
1107 pixel.x = pixel.z;
1108 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
1109 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
1110 pixel.y = pixel.z;
1111 pixel.w = pixel.x;
1112 pixel.x = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
1113 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
1114 pixel.z = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
1115 pixel.w = Short4(0xFFFFu);
1116 break;
John Bauman89401822014-05-06 15:04:28 -04001117 case FORMAT_A8G8R8B8Q:
1118 UNIMPLEMENTED();
Maxime Grégoired9762742015-07-08 16:43:48 -04001119 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
1120 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
1121 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
1122 // pixel.w = UnpackHigh(As<Byte8>(pixel.w), *Pointer<Byte8>(cBuffer + 8 * x + 8));
John Bauman89401822014-05-06 15:04:28 -04001123 break;
1124 case FORMAT_X8G8R8B8Q:
1125 UNIMPLEMENTED();
Maxime Grégoired9762742015-07-08 16:43:48 -04001126 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
1127 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
1128 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
1129 // pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04001130 break;
1131 case FORMAT_A16B16G16R16:
Maxime Grégoired9762742015-07-08 16:43:48 -04001132 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04001133 pixel.x = *Pointer<Short4>(buffer + 8 * x);
1134 pixel.y = *Pointer<Short4>(buffer + 8 * x + 8);
Maxime Grégoired9762742015-07-08 16:43:48 -04001135 buffer += *Pointer<Int>(r.data + OFFSET(DrawData, colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04001136 pixel.z = *Pointer<Short4>(buffer + 8 * x);
1137 pixel.w = *Pointer<Short4>(buffer + 8 * x + 8);
1138 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04001139 break;
1140 case FORMAT_G16R16:
1141 buffer = cBuffer;
Maxime Grégoired9762742015-07-08 16:43:48 -04001142 pixel.x = *Pointer<Short4>(buffer + 4 * x);
1143 buffer += *Pointer<Int>(r.data + OFFSET(DrawData, colorPitchB[index]));
1144 pixel.y = *Pointer<Short4>(buffer + 4 * x);
John Bauman19bac1e2014-05-06 15:23:49 -04001145 pixel.z = pixel.x;
1146 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.y));
1147 pixel.z = As<Short4>(UnpackHigh(pixel.z, pixel.y));
1148 pixel.y = pixel.z;
1149 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.z));
1150 pixel.y = As<Short4>(UnpackHigh(pixel.y, pixel.z));
1151 pixel.z = Short4(0xFFFFu);
1152 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04001153 break;
1154 default:
1155 ASSERT(false);
1156 }
1157
1158 if(postBlendSRGB && state.writeSRGB)
1159 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001160 sRGBtoLinear16_12_16(r, pixel);
John Bauman89401822014-05-06 15:04:28 -04001161 }
Maxime Grégoired9762742015-07-08 16:43:48 -04001162 }
1163
1164 void PixelRoutine::alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4s &current, Int &x)
1165 {
1166 if(!state.alphaBlendActive)
1167 {
1168 return;
1169 }
1170
1171 Vector4s pixel;
1172 Short4 c01;
1173 Short4 c23;
1174
1175 readPixel(r, index, cBuffer, current, x, pixel);
John Bauman89401822014-05-06 15:04:28 -04001176
1177 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
Alexis Hetu96517182015-04-15 10:30:23 -04001178 Vector4s sourceFactor;
1179 Vector4s destFactor;
John Bauman89401822014-05-06 15:04:28 -04001180
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001181 blendFactor(r, sourceFactor, current, pixel, state.sourceBlendFactor);
1182 blendFactor(r, destFactor, current, pixel, state.destBlendFactor);
John Bauman89401822014-05-06 15:04:28 -04001183
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001184 if(state.sourceBlendFactor != BLEND_ONE && state.sourceBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04001185 {
John Bauman19bac1e2014-05-06 15:23:49 -04001186 current.x = MulHigh(As<UShort4>(current.x), As<UShort4>(sourceFactor.x));
1187 current.y = MulHigh(As<UShort4>(current.y), As<UShort4>(sourceFactor.y));
1188 current.z = MulHigh(As<UShort4>(current.z), As<UShort4>(sourceFactor.z));
John Bauman89401822014-05-06 15:04:28 -04001189 }
1190
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001191 if(state.destBlendFactor != BLEND_ONE && state.destBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04001192 {
John Bauman19bac1e2014-05-06 15:23:49 -04001193 pixel.x = MulHigh(As<UShort4>(pixel.x), As<UShort4>(destFactor.x));
1194 pixel.y = MulHigh(As<UShort4>(pixel.y), As<UShort4>(destFactor.y));
1195 pixel.z = MulHigh(As<UShort4>(pixel.z), As<UShort4>(destFactor.z));
John Bauman89401822014-05-06 15:04:28 -04001196 }
1197
1198 switch(state.blendOperation)
1199 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001200 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04001201 current.x = AddSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
1202 current.y = AddSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
1203 current.z = AddSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04001204 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001205 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04001206 current.x = SubSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
1207 current.y = SubSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
1208 current.z = SubSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04001209 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001210 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04001211 current.x = SubSat(As<UShort4>(pixel.x), As<UShort4>(current.x));
1212 current.y = SubSat(As<UShort4>(pixel.y), As<UShort4>(current.y));
1213 current.z = SubSat(As<UShort4>(pixel.z), As<UShort4>(current.z));
John Bauman89401822014-05-06 15:04:28 -04001214 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001215 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04001216 current.x = Min(As<UShort4>(current.x), As<UShort4>(pixel.x));
1217 current.y = Min(As<UShort4>(current.y), As<UShort4>(pixel.y));
1218 current.z = Min(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04001219 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001220 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04001221 current.x = Max(As<UShort4>(current.x), As<UShort4>(pixel.x));
1222 current.y = Max(As<UShort4>(current.y), As<UShort4>(pixel.y));
1223 current.z = Max(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04001224 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001225 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04001226 // No operation
1227 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001228 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001229 current.x = pixel.x;
1230 current.y = pixel.y;
1231 current.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04001232 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001233 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04001234 current.x = Short4(0x0000, 0x0000, 0x0000, 0x0000);
1235 current.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
1236 current.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04001237 break;
1238 default:
1239 ASSERT(false);
1240 }
1241
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001242 blendFactorAlpha(r, sourceFactor, current, pixel, state.sourceBlendFactorAlpha);
1243 blendFactorAlpha(r, destFactor, current, pixel, state.destBlendFactorAlpha);
John Bauman89401822014-05-06 15:04:28 -04001244
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001245 if(state.sourceBlendFactorAlpha != BLEND_ONE && state.sourceBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04001246 {
John Bauman19bac1e2014-05-06 15:23:49 -04001247 current.w = MulHigh(As<UShort4>(current.w), As<UShort4>(sourceFactor.w));
John Bauman89401822014-05-06 15:04:28 -04001248 }
1249
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001250 if(state.destBlendFactorAlpha != BLEND_ONE && state.destBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04001251 {
John Bauman19bac1e2014-05-06 15:23:49 -04001252 pixel.w = MulHigh(As<UShort4>(pixel.w), As<UShort4>(destFactor.w));
John Bauman89401822014-05-06 15:04:28 -04001253 }
1254
1255 switch(state.blendOperationAlpha)
1256 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001257 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04001258 current.w = AddSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001259 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001260 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04001261 current.w = SubSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001262 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001263 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04001264 current.w = SubSat(As<UShort4>(pixel.w), As<UShort4>(current.w));
John Bauman89401822014-05-06 15:04:28 -04001265 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001266 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04001267 current.w = Min(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001268 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001269 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04001270 current.w = Max(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001271 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001272 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04001273 // No operation
1274 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001275 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001276 current.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001277 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001278 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04001279 current.w = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04001280 break;
1281 default:
1282 ASSERT(false);
1283 }
1284 }
1285
Maxime Grégoired9762742015-07-08 16:43:48 -04001286 void PixelRoutine::logicOperation(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4s &current, Int &x)
1287 {
1288 if(state.logicalOperation == LogicalOperation::LOGICALOP_COPY)
1289 {
1290 return;
1291 }
1292
1293 Vector4s pixel;
1294
1295 // Read pixel
1296 readPixel(r, index, cBuffer, current, x, pixel);
1297
1298 switch(state.logicalOperation)
1299 {
1300 case LOGICALOP_CLEAR:
1301 current.x = 0;
1302 current.y = 0;
1303 current.z = 0;
1304 break;
1305 case LOGICALOP_SET:
1306 current.x = 0xFFFF;
1307 current.y = 0xFFFF;
1308 current.z = 0xFFFF;
1309 break;
1310 case LOGICALOP_COPY:
1311 ASSERT(false); // Optimized out
1312 break;
1313 case LOGICALOP_COPY_INVERTED:
1314 current.x = ~current.x;
1315 current.y = ~current.y;
1316 current.z = ~current.z;
1317 break;
1318 case LOGICALOP_NOOP:
1319 current.x = pixel.x;
1320 current.y = pixel.y;
1321 current.z = pixel.z;
1322 break;
1323 case LOGICALOP_INVERT:
1324 current.x = ~pixel.x;
1325 current.y = ~pixel.y;
1326 current.z = ~pixel.z;
1327 break;
1328 case LOGICALOP_AND:
1329 current.x = pixel.x & current.x;
1330 current.y = pixel.y & current.y;
1331 current.z = pixel.z & current.z;
1332 break;
1333 case LOGICALOP_NAND:
1334 current.x = ~(pixel.x & current.x);
1335 current.y = ~(pixel.y & current.y);
1336 current.z = ~(pixel.z & current.z);
1337 break;
1338 case LOGICALOP_OR:
1339 current.x = pixel.x | current.x;
1340 current.y = pixel.y | current.y;
1341 current.z = pixel.z | current.z;
1342 break;
1343 case LOGICALOP_NOR:
1344 current.x = ~(pixel.x | current.x);
1345 current.y = ~(pixel.y | current.y);
1346 current.z = ~(pixel.z | current.z);
1347 break;
1348 case LOGICALOP_XOR:
1349 current.x = pixel.x ^ current.x;
1350 current.y = pixel.y ^ current.y;
1351 current.z = pixel.z ^ current.z;
1352 break;
1353 case LOGICALOP_EQUIV:
1354 current.x = ~(pixel.x ^ current.x);
1355 current.y = ~(pixel.y ^ current.y);
1356 current.z = ~(pixel.z ^ current.z);
1357 break;
1358 case LOGICALOP_AND_REVERSE:
1359 current.x = ~pixel.x & current.x;
1360 current.y = ~pixel.y & current.y;
1361 current.z = ~pixel.z & current.z;
1362 break;
1363 case LOGICALOP_AND_INVERTED:
1364 current.x = pixel.x & ~current.x;
1365 current.y = pixel.y & ~current.y;
1366 current.z = pixel.z & ~current.z;
1367 break;
1368 case LOGICALOP_OR_REVERSE:
1369 current.x = ~pixel.x | current.x;
1370 current.y = ~pixel.y | current.y;
1371 current.z = ~pixel.z | current.z;
1372 break;
1373 case LOGICALOP_OR_INVERTED:
1374 current.x = pixel.x | ~current.x;
1375 current.y = pixel.y | ~current.y;
1376 current.z = pixel.z | ~current.z;
1377 break;
1378 default:
1379 ASSERT(false);
1380 }
1381 }
1382
Alexis Hetu96517182015-04-15 10:30:23 -04001383 void PixelRoutine::writeColor(Registers &r, int index, Pointer<Byte> &cBuffer, Int &x, Vector4s &current, Int &sMask, Int &zMask, Int &cMask)
John Bauman89401822014-05-06 15:04:28 -04001384 {
John Bauman89401822014-05-06 15:04:28 -04001385 if(postBlendSRGB && state.writeSRGB)
1386 {
Nicolas Capense1a50af2015-05-13 16:48:18 -04001387 linearToSRGB16_12_16(r, current);
John Bauman89401822014-05-06 15:04:28 -04001388 }
1389
1390 if(exactColorRounding)
1391 {
1392 switch(state.targetFormat[index])
1393 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001394 case FORMAT_R5G6B5:
1395 // UNIMPLEMENTED(); // FIXME
1396 break;
John Bauman89401822014-05-06 15:04:28 -04001397 case FORMAT_X8G8R8B8Q:
1398 case FORMAT_A8G8R8B8Q:
1399 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001400 case FORMAT_X8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04001401 case FORMAT_A8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001402 case FORMAT_A8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04001403 {
John Bauman19bac1e2014-05-06 15:23:49 -04001404 current.x = current.x - As<Short4>(As<UShort4>(current.x) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
1405 current.y = current.y - As<Short4>(As<UShort4>(current.y) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
1406 current.z = current.z - As<Short4>(As<UShort4>(current.z) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
1407 current.w = current.w - As<Short4>(As<UShort4>(current.w) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
John Bauman89401822014-05-06 15:04:28 -04001408 }
1409 break;
1410 }
1411 }
1412
1413 int rgbaWriteMask = state.colorWriteActive(index);
1414 int bgraWriteMask = rgbaWriteMask & 0x0000000A | (rgbaWriteMask & 0x00000001) << 2 | (rgbaWriteMask & 0x00000004) >> 2;
1415 int brgaWriteMask = rgbaWriteMask & 0x00000008 | (rgbaWriteMask & 0x00000001) << 1 | (rgbaWriteMask & 0x00000002) << 1 | (rgbaWriteMask & 0x00000004) >> 2;
1416
1417 switch(state.targetFormat[index])
1418 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001419 case FORMAT_R5G6B5:
1420 {
1421 current.x = current.x & Short4(0xF800u);
1422 current.y = As<UShort4>(current.y & Short4(0xFC00u)) >> 5;
1423 current.z = As<UShort4>(current.z) >> 11;
1424
1425 current.x = current.x | current.y | current.z;
1426 }
1427 break;
John Bauman89401822014-05-06 15:04:28 -04001428 case FORMAT_X8G8R8B8Q:
1429 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04001430 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1431 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1432 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04001433
John Bauman19bac1e2014-05-06 15:23:49 -04001434 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
1435 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
John Bauman89401822014-05-06 15:04:28 -04001436 break;
1437 case FORMAT_A8G8R8B8Q:
1438 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04001439 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1440 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1441 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
1442 // current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04001443
John Bauman19bac1e2014-05-06 15:23:49 -04001444 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
1445 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04001446 break;
1447 case FORMAT_X8R8G8B8:
1448 case FORMAT_A8R8G8B8:
1449 if(state.targetFormat[index] == FORMAT_X8R8G8B8 || rgbaWriteMask == 0x7)
1450 {
John Bauman19bac1e2014-05-06 15:23:49 -04001451 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1452 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1453 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04001454
John Bauman19bac1e2014-05-06 15:23:49 -04001455 current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
1456 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
John Bauman89401822014-05-06 15:04:28 -04001457
John Bauman19bac1e2014-05-06 15:23:49 -04001458 current.x = current.z;
1459 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
1460 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
1461 current.y = current.z;
1462 current.z = As<Short4>(UnpackLow(current.z, current.x));
1463 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04001464 }
1465 else
1466 {
John Bauman19bac1e2014-05-06 15:23:49 -04001467 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1468 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1469 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
1470 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04001471
John Bauman19bac1e2014-05-06 15:23:49 -04001472 current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
1473 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04001474
John Bauman19bac1e2014-05-06 15:23:49 -04001475 current.x = current.z;
1476 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
1477 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
1478 current.y = current.z;
1479 current.z = As<Short4>(UnpackLow(current.z, current.x));
1480 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04001481 }
1482 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001483 case FORMAT_X8B8G8R8:
1484 case FORMAT_A8B8G8R8:
1485 if(state.targetFormat[index] == FORMAT_X8B8G8R8 || rgbaWriteMask == 0x7)
1486 {
1487 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1488 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1489 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
1490
1491 current.z = As<Short4>(Pack(As<UShort4>(current.x), As<UShort4>(current.z)));
1492 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
1493
1494 current.x = current.z;
1495 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
1496 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
1497 current.y = current.z;
1498 current.z = As<Short4>(UnpackLow(current.z, current.x));
1499 current.y = As<Short4>(UnpackHigh(current.y, current.x));
1500 }
1501 else
1502 {
1503 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1504 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1505 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
1506 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
1507
1508 current.z = As<Short4>(Pack(As<UShort4>(current.x), As<UShort4>(current.z)));
1509 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
1510
1511 current.x = current.z;
1512 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
1513 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
1514 current.y = current.z;
1515 current.z = As<Short4>(UnpackLow(current.z, current.x));
1516 current.y = As<Short4>(UnpackHigh(current.y, current.x));
1517 }
1518 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001519 case FORMAT_A8:
1520 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
1521 current.w = As<Short4>(Pack(As<UShort4>(current.w), As<UShort4>(current.w)));
1522 break;
John Bauman89401822014-05-06 15:04:28 -04001523 case FORMAT_G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04001524 current.z = current.x;
1525 current.x = As<Short4>(UnpackLow(current.x, current.y));
1526 current.z = As<Short4>(UnpackHigh(current.z, current.y));
1527 current.y = current.z;
John Bauman89401822014-05-06 15:04:28 -04001528 break;
1529 case FORMAT_A16B16G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04001530 transpose4x4(current.x, current.y, current.z, current.w);
John Bauman89401822014-05-06 15:04:28 -04001531 break;
John Bauman89401822014-05-06 15:04:28 -04001532 default:
1533 ASSERT(false);
1534 }
1535
John Bauman19bac1e2014-05-06 15:23:49 -04001536 Short4 c01 = current.z;
1537 Short4 c23 = current.y;
John Bauman89401822014-05-06 15:04:28 -04001538
1539 Int xMask; // Combination of all masks
1540
1541 if(state.depthTestActive)
1542 {
1543 xMask = zMask;
1544 }
1545 else
1546 {
1547 xMask = cMask;
1548 }
1549
1550 if(state.stencilActive)
1551 {
1552 xMask &= sMask;
1553 }
1554
John Bauman89401822014-05-06 15:04:28 -04001555 switch(state.targetFormat[index])
1556 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001557 case FORMAT_R5G6B5:
1558 {
1559 Pointer<Byte> buffer = cBuffer + 2 * x;
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001560 Int value = *Pointer<Int>(buffer);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001561
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001562 Int c01 = Extract(As<Int2>(current.x), 0);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001563
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001564 if((bgraWriteMask & 0x00000007) != 0x00000007)
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001565 {
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001566 Int masked = value;
1567 c01 &= *Pointer<Int>(r.constants + OFFSET(Constants,mask565Q[bgraWriteMask & 0x7][0]));
1568 masked &= *Pointer<Int>(r.constants + OFFSET(Constants,invMask565Q[bgraWriteMask & 0x7][0]));
1569 c01 |= masked;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001570 }
1571
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001572 c01 &= *Pointer<Int>(r.constants + OFFSET(Constants,maskW4Q[0][0]) + xMask * 8);
1573 value &= *Pointer<Int>(r.constants + OFFSET(Constants,invMaskW4Q[0][0]) + xMask * 8);
1574 c01 |= value;
1575 *Pointer<Int>(buffer) = c01;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001576
1577 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001578 value = *Pointer<Int>(buffer);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001579
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001580 Int c23 = Extract(As<Int2>(current.x), 1);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001581
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001582 if((bgraWriteMask & 0x00000007) != 0x00000007)
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001583 {
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001584 Int masked = value;
1585 c23 &= *Pointer<Int>(r.constants + OFFSET(Constants,mask565Q[bgraWriteMask & 0x7][0]));
1586 masked &= *Pointer<Int>(r.constants + OFFSET(Constants,invMask565Q[bgraWriteMask & 0x7][0]));
1587 c23 |= masked;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001588 }
1589
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001590 c23 &= *Pointer<Int>(r.constants + OFFSET(Constants,maskW4Q[0][2]) + xMask * 8);
1591 value &= *Pointer<Int>(r.constants + OFFSET(Constants,invMaskW4Q[0][2]) + xMask * 8);
1592 c23 |= value;
1593 *Pointer<Int>(buffer) = c23;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001594 }
1595 break;
John Bauman89401822014-05-06 15:04:28 -04001596 case FORMAT_A8G8R8B8Q:
1597 case FORMAT_X8G8R8B8Q: // FIXME: Don't touch alpha?
1598 UNIMPLEMENTED();
1599 // value = *Pointer<Short4>(cBuffer + 8 * x + 0);
1600
1601 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
1602 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
1603 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
1604 // {
1605 // Short4 masked = value;
1606 // c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
1607 // masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
1608 // c01 |= masked;
1609 // }
1610
1611 // c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
1612 // value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
1613 // c01 |= value;
1614 // *Pointer<Short4>(cBuffer + 8 * x + 0) = c01;
1615
1616 // value = *Pointer<Short4>(cBuffer + 8 * x + 8);
1617
1618 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
1619 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
1620 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
1621 // {
1622 // Short4 masked = value;
1623 // c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
1624 // masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
1625 // c23 |= masked;
1626 // }
1627
1628 // c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
1629 // value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
1630 // c23 |= value;
1631 // *Pointer<Short4>(cBuffer + 8 * x + 8) = c23;
1632 break;
1633 case FORMAT_A8R8G8B8:
1634 case FORMAT_X8R8G8B8: // FIXME: Don't touch alpha?
John Bauman89401822014-05-06 15:04:28 -04001635 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001636 Pointer<Byte> buffer = cBuffer + x * 4;
1637 Short4 value = *Pointer<Short4>(buffer);
1638
1639 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
1640 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
1641 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
1642 {
1643 Short4 masked = value;
1644 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
1645 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
1646 c01 |= masked;
1647 }
1648
1649 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
1650 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
1651 c01 |= value;
1652 *Pointer<Short4>(buffer) = c01;
1653
1654 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
1655 value = *Pointer<Short4>(buffer);
1656
1657 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
1658 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
1659 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
1660 {
1661 Short4 masked = value;
1662 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
1663 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
1664 c23 |= masked;
1665 }
1666
1667 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
1668 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
1669 c23 |= value;
1670 *Pointer<Short4>(buffer) = c23;
John Bauman89401822014-05-06 15:04:28 -04001671 }
John Bauman89401822014-05-06 15:04:28 -04001672 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001673 case FORMAT_A8B8G8R8:
1674 case FORMAT_X8B8G8R8: // FIXME: Don't touch alpha?
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001675 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001676 Pointer<Byte> buffer = cBuffer + x * 4;
1677 Short4 value = *Pointer<Short4>(buffer);
1678
1679 if((state.targetFormat[index] == FORMAT_A8B8G8R8 && rgbaWriteMask != 0x0000000F) ||
1680 ((state.targetFormat[index] == FORMAT_X8B8G8R8 && rgbaWriteMask != 0x00000007) &&
1681 (state.targetFormat[index] == FORMAT_X8B8G8R8 && rgbaWriteMask != 0x0000000F))) // FIXME: Need for masking when XBGR && Fh?
1682 {
1683 Short4 masked = value;
1684 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[rgbaWriteMask][0]));
1685 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[rgbaWriteMask][0]));
1686 c01 |= masked;
1687 }
1688
1689 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
1690 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
1691 c01 |= value;
1692 *Pointer<Short4>(buffer) = c01;
1693
1694 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
1695 value = *Pointer<Short4>(buffer);
1696
1697 if((state.targetFormat[index] == FORMAT_A8B8G8R8 && rgbaWriteMask != 0x0000000F) ||
1698 ((state.targetFormat[index] == FORMAT_X8B8G8R8 && rgbaWriteMask != 0x00000007) &&
1699 (state.targetFormat[index] == FORMAT_X8B8G8R8 && rgbaWriteMask != 0x0000000F))) // FIXME: Need for masking when XBGR && Fh?
1700 {
1701 Short4 masked = value;
1702 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[rgbaWriteMask][0]));
1703 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[rgbaWriteMask][0]));
1704 c23 |= masked;
1705 }
1706
1707 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
1708 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
1709 c23 |= value;
1710 *Pointer<Short4>(buffer) = c23;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001711 }
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001712 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001713 case FORMAT_A8:
1714 if(rgbaWriteMask & 0x00000008)
1715 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001716 Pointer<Byte> buffer = cBuffer + 1 * x;
1717 Short4 value;
John Bauman66b8ab22014-05-06 15:57:45 -04001718 Insert(value, *Pointer<Short>(buffer), 0);
1719 Int pitch = *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
1720 Insert(value, *Pointer<Short>(buffer + pitch), 1);
1721 value = UnpackLow(As<Byte8>(value), As<Byte8>(value));
1722
1723 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q) + 8 * xMask);
1724 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * xMask);
1725 current.w |= value;
1726
1727 *Pointer<Short>(buffer) = Extract(current.w, 0);
1728 *Pointer<Short>(buffer + pitch) = Extract(current.w, 1);
1729 }
1730 break;
John Bauman89401822014-05-06 15:04:28 -04001731 case FORMAT_G16R16:
John Bauman89401822014-05-06 15:04:28 -04001732 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001733 Pointer<Byte> buffer = cBuffer + 4 * x;
John Bauman89401822014-05-06 15:04:28 -04001734
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001735 Short4 value = *Pointer<Short4>(buffer);
John Bauman89401822014-05-06 15:04:28 -04001736
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001737 if((rgbaWriteMask & 0x00000003) != 0x00000003)
John Bauman89401822014-05-06 15:04:28 -04001738 {
1739 Short4 masked = value;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001740 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
1741 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04001742 current.x |= masked;
John Bauman89401822014-05-06 15:04:28 -04001743 }
1744
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001745 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
1746 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04001747 current.x |= value;
1748 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04001749
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001750 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04001751
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001752 value = *Pointer<Short4>(buffer);
1753
1754 if((rgbaWriteMask & 0x00000003) != 0x00000003)
John Bauman89401822014-05-06 15:04:28 -04001755 {
1756 Short4 masked = value;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001757 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
1758 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04001759 current.y |= masked;
John Bauman89401822014-05-06 15:04:28 -04001760 }
1761
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001762 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
1763 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04001764 current.y |= value;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001765 *Pointer<Short4>(buffer) = current.y;
John Bauman89401822014-05-06 15:04:28 -04001766 }
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001767 break;
1768 case FORMAT_A16B16G16R16:
John Bauman89401822014-05-06 15:04:28 -04001769 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001770 Pointer<Byte> buffer = cBuffer + 8 * x;
John Bauman89401822014-05-06 15:04:28 -04001771
John Bauman89401822014-05-06 15:04:28 -04001772 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001773 Short4 value = *Pointer<Short4>(buffer);
1774
1775 if(rgbaWriteMask != 0x0000000F)
1776 {
1777 Short4 masked = value;
1778 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
1779 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
1780 current.x |= masked;
1781 }
1782
1783 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ0Q) + xMask * 8);
1784 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ0Q) + xMask * 8);
1785 current.x |= value;
1786 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04001787 }
1788
John Bauman89401822014-05-06 15:04:28 -04001789 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001790 Short4 value = *Pointer<Short4>(buffer + 8);
1791
1792 if(rgbaWriteMask != 0x0000000F)
1793 {
1794 Short4 masked = value;
1795 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
1796 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
1797 current.y |= masked;
1798 }
1799
1800 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ1Q) + xMask * 8);
1801 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ1Q) + xMask * 8);
1802 current.y |= value;
1803 *Pointer<Short4>(buffer + 8) = current.y;
John Bauman89401822014-05-06 15:04:28 -04001804 }
1805
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001806 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
1807
1808 {
1809 Short4 value = *Pointer<Short4>(buffer);
1810
1811 if(rgbaWriteMask != 0x0000000F)
1812 {
1813 Short4 masked = value;
1814 current.z &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
1815 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
1816 current.z |= masked;
1817 }
1818
1819 current.z &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ2Q) + xMask * 8);
1820 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ2Q) + xMask * 8);
1821 current.z |= value;
1822 *Pointer<Short4>(buffer) = current.z;
1823 }
1824
1825 {
1826 Short4 value = *Pointer<Short4>(buffer + 8);
1827
1828 if(rgbaWriteMask != 0x0000000F)
1829 {
1830 Short4 masked = value;
1831 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
1832 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
1833 current.w |= masked;
1834 }
1835
1836 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ3Q) + xMask * 8);
1837 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ3Q) + xMask * 8);
1838 current.w |= value;
1839 *Pointer<Short4>(buffer + 8) = current.w;
1840 }
John Bauman89401822014-05-06 15:04:28 -04001841 }
1842 break;
1843 default:
1844 ASSERT(false);
1845 }
1846 }
1847
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001848 void PixelRoutine::blendFactor(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -04001849 {
1850 switch(blendFactorActive)
1851 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001852 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04001853 // Optimized
1854 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001855 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04001856 // Optimized
1857 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001858 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04001859 blendFactor.x = oC.x;
1860 blendFactor.y = oC.y;
1861 blendFactor.z = oC.z;
John Bauman89401822014-05-06 15:04:28 -04001862 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001863 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04001864 blendFactor.x = Float4(1.0f) - oC.x;
1865 blendFactor.y = Float4(1.0f) - oC.y;
1866 blendFactor.z = Float4(1.0f) - oC.z;
John Bauman89401822014-05-06 15:04:28 -04001867 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001868 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001869 blendFactor.x = pixel.x;
1870 blendFactor.y = pixel.y;
1871 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04001872 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001873 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001874 blendFactor.x = Float4(1.0f) - pixel.x;
1875 blendFactor.y = Float4(1.0f) - pixel.y;
1876 blendFactor.z = Float4(1.0f) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -04001877 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001878 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001879 blendFactor.x = oC.w;
1880 blendFactor.y = oC.w;
1881 blendFactor.z = oC.w;
John Bauman89401822014-05-06 15:04:28 -04001882 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001883 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001884 blendFactor.x = Float4(1.0f) - oC.w;
1885 blendFactor.y = Float4(1.0f) - oC.w;
1886 blendFactor.z = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04001887 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001888 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001889 blendFactor.x = pixel.w;
1890 blendFactor.y = pixel.w;
1891 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001892 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001893 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001894 blendFactor.x = Float4(1.0f) - pixel.w;
1895 blendFactor.y = Float4(1.0f) - pixel.w;
1896 blendFactor.z = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001897 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001898 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04001899 blendFactor.x = Float4(1.0f) - pixel.w;
1900 blendFactor.x = Min(blendFactor.x, oC.w);
1901 blendFactor.y = blendFactor.x;
1902 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -04001903 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001904 case BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04001905 blendFactor.x = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[0]));
1906 blendFactor.y = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[1]));
1907 blendFactor.z = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04001908 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001909 case BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04001910 blendFactor.x = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[0]));
1911 blendFactor.y = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[1]));
1912 blendFactor.z = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04001913 break;
1914 default:
1915 ASSERT(false);
1916 }
1917 }
1918
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001919 void PixelRoutine::blendFactorAlpha(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -04001920 {
1921 switch(blendFactorAlphaActive)
1922 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001923 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04001924 // Optimized
1925 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001926 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04001927 // Optimized
1928 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001929 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04001930 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04001931 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001932 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04001933 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04001934 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001935 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001936 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001937 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001938 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001939 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001940 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001941 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001942 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04001943 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001944 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001945 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04001946 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001947 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001948 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001949 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001950 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001951 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001952 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001953 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04001954 blendFactor.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04001955 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001956 case BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04001957 blendFactor.w = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04001958 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001959 case BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04001960 blendFactor.w = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04001961 break;
1962 default:
1963 ASSERT(false);
1964 }
1965 }
1966
John Bauman19bac1e2014-05-06 15:23:49 -04001967 void PixelRoutine::alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4f &oC, Int &x)
John Bauman89401822014-05-06 15:04:28 -04001968 {
1969 if(!state.alphaBlendActive)
1970 {
1971 return;
1972 }
1973
1974 Pointer<Byte> buffer;
John Bauman19bac1e2014-05-06 15:23:49 -04001975 Vector4f pixel;
John Bauman89401822014-05-06 15:04:28 -04001976
Alexis Hetu96517182015-04-15 10:30:23 -04001977 Vector4s color;
John Bauman89401822014-05-06 15:04:28 -04001978 Short4 c01;
1979 Short4 c23;
1980
John Bauman89401822014-05-06 15:04:28 -04001981 switch(state.targetFormat[index])
1982 {
John Bauman89401822014-05-06 15:04:28 -04001983 case FORMAT_R32F:
1984 buffer = cBuffer;
1985 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04001986 pixel.x.x = *Pointer<Float>(buffer + 4 * x + 0);
1987 pixel.x.y = *Pointer<Float>(buffer + 4 * x + 4);
John Bauman89401822014-05-06 15:04:28 -04001988 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
1989 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04001990 pixel.x.z = *Pointer<Float>(buffer + 4 * x + 0);
1991 pixel.x.w = *Pointer<Float>(buffer + 4 * x + 4);
1992 pixel.y = Float4(1.0f);
1993 pixel.z = Float4(1.0f);
1994 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04001995 break;
1996 case FORMAT_G32R32F:
1997 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04001998 pixel.x = *Pointer<Float4>(buffer + 8 * x, 16);
John Bauman89401822014-05-06 15:04:28 -04001999 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002000 pixel.y = *Pointer<Float4>(buffer + 8 * x, 16);
2001 pixel.z = pixel.x;
2002 pixel.x = ShuffleLowHigh(pixel.x, pixel.y, 0x88);
2003 pixel.z = ShuffleLowHigh(pixel.z, pixel.y, 0xDD);
2004 pixel.y = pixel.z;
2005 pixel.z = Float4(1.0f);
2006 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04002007 break;
2008 case FORMAT_A32B32G32R32F:
2009 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002010 pixel.x = *Pointer<Float4>(buffer + 16 * x, 16);
2011 pixel.y = *Pointer<Float4>(buffer + 16 * x + 16, 16);
John Bauman89401822014-05-06 15:04:28 -04002012 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002013 pixel.z = *Pointer<Float4>(buffer + 16 * x, 16);
2014 pixel.w = *Pointer<Float4>(buffer + 16 * x + 16, 16);
2015 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04002016 break;
2017 default:
2018 ASSERT(false);
2019 }
2020
2021 if(postBlendSRGB && state.writeSRGB)
2022 {
John Bauman19bac1e2014-05-06 15:23:49 -04002023 sRGBtoLinear(pixel.x);
2024 sRGBtoLinear(pixel.y);
2025 sRGBtoLinear(pixel.z);
John Bauman89401822014-05-06 15:04:28 -04002026 }
2027
2028 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
John Bauman19bac1e2014-05-06 15:23:49 -04002029 Vector4f sourceFactor;
2030 Vector4f destFactor;
John Bauman89401822014-05-06 15:04:28 -04002031
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002032 blendFactor(r, sourceFactor, oC, pixel, state.sourceBlendFactor);
2033 blendFactor(r, destFactor, oC, pixel, state.destBlendFactor);
John Bauman89401822014-05-06 15:04:28 -04002034
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002035 if(state.sourceBlendFactor != BLEND_ONE && state.sourceBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002036 {
John Bauman19bac1e2014-05-06 15:23:49 -04002037 oC.x *= sourceFactor.x;
2038 oC.y *= sourceFactor.y;
2039 oC.z *= sourceFactor.z;
John Bauman89401822014-05-06 15:04:28 -04002040 }
2041
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002042 if(state.destBlendFactor != BLEND_ONE && state.destBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002043 {
John Bauman19bac1e2014-05-06 15:23:49 -04002044 pixel.x *= destFactor.x;
2045 pixel.y *= destFactor.y;
2046 pixel.z *= destFactor.z;
John Bauman89401822014-05-06 15:04:28 -04002047 }
2048
2049 switch(state.blendOperation)
2050 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002051 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002052 oC.x += pixel.x;
2053 oC.y += pixel.y;
2054 oC.z += pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002055 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002056 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002057 oC.x -= pixel.x;
2058 oC.y -= pixel.y;
2059 oC.z -= pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002060 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002061 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002062 oC.x = pixel.x - oC.x;
2063 oC.y = pixel.y - oC.y;
2064 oC.z = pixel.z - oC.z;
John Bauman89401822014-05-06 15:04:28 -04002065 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002066 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002067 oC.x = Min(oC.x, pixel.x);
2068 oC.y = Min(oC.y, pixel.y);
2069 oC.z = Min(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04002070 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002071 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002072 oC.x = Max(oC.x, pixel.x);
2073 oC.y = Max(oC.y, pixel.y);
2074 oC.z = Max(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04002075 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002076 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04002077 // No operation
2078 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002079 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002080 oC.x = pixel.x;
2081 oC.y = pixel.y;
2082 oC.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002083 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002084 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002085 oC.x = Float4(0.0f);
2086 oC.y = Float4(0.0f);
2087 oC.z = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04002088 break;
2089 default:
2090 ASSERT(false);
2091 }
2092
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002093 blendFactorAlpha(r, sourceFactor, oC, pixel, state.sourceBlendFactorAlpha);
2094 blendFactorAlpha(r, destFactor, oC, pixel, state.destBlendFactorAlpha);
John Bauman89401822014-05-06 15:04:28 -04002095
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002096 if(state.sourceBlendFactorAlpha != BLEND_ONE && state.sourceBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002097 {
John Bauman19bac1e2014-05-06 15:23:49 -04002098 oC.w *= sourceFactor.w;
John Bauman89401822014-05-06 15:04:28 -04002099 }
2100
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002101 if(state.destBlendFactorAlpha != BLEND_ONE && state.destBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002102 {
John Bauman19bac1e2014-05-06 15:23:49 -04002103 pixel.w *= destFactor.w;
John Bauman89401822014-05-06 15:04:28 -04002104 }
2105
2106 switch(state.blendOperationAlpha)
2107 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002108 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002109 oC.w += pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002110 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002111 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002112 oC.w -= pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002113 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002114 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002115 pixel.w -= oC.w;
2116 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002117 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002118 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002119 oC.w = Min(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04002120 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002121 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002122 oC.w = Max(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04002123 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002124 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04002125 // No operation
2126 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002127 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002128 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002129 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002130 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002131 oC.w = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04002132 break;
2133 default:
2134 ASSERT(false);
2135 }
2136 }
2137
John Bauman19bac1e2014-05-06 15:23:49 -04002138 void PixelRoutine::writeColor(Registers &r, int index, Pointer<Byte> &cBuffer, Int &x, Vector4f &oC, Int &sMask, Int &zMask, Int &cMask)
John Bauman89401822014-05-06 15:04:28 -04002139 {
John Bauman89401822014-05-06 15:04:28 -04002140 switch(state.targetFormat[index])
2141 {
John Bauman89401822014-05-06 15:04:28 -04002142 case FORMAT_R32F:
2143 break;
2144 case FORMAT_G32R32F:
John Bauman19bac1e2014-05-06 15:23:49 -04002145 oC.z = oC.x;
2146 oC.x = UnpackLow(oC.x, oC.y);
2147 oC.z = UnpackHigh(oC.z, oC.y);
2148 oC.y = oC.z;
John Bauman89401822014-05-06 15:04:28 -04002149 break;
2150 case FORMAT_A32B32G32R32F:
John Bauman19bac1e2014-05-06 15:23:49 -04002151 transpose4x4(oC.x, oC.y, oC.z, oC.w);
John Bauman89401822014-05-06 15:04:28 -04002152 break;
2153 default:
2154 ASSERT(false);
2155 }
2156
2157 int rgbaWriteMask = state.colorWriteActive(index);
2158
2159 Int xMask; // Combination of all masks
2160
2161 if(state.depthTestActive)
2162 {
2163 xMask = zMask;
2164 }
2165 else
2166 {
2167 xMask = cMask;
2168 }
2169
2170 if(state.stencilActive)
2171 {
2172 xMask &= sMask;
2173 }
2174
2175 Pointer<Byte> buffer;
2176 Float4 value;
2177
2178 switch(state.targetFormat[index])
2179 {
2180 case FORMAT_R32F:
2181 if(rgbaWriteMask & 0x00000001)
2182 {
2183 buffer = cBuffer + 4 * x;
2184
2185 // FIXME: movlps
2186 value.x = *Pointer<Float>(buffer + 0);
2187 value.y = *Pointer<Float>(buffer + 4);
2188
2189 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2190
2191 // FIXME: movhps
2192 value.z = *Pointer<Float>(buffer + 0);
2193 value.w = *Pointer<Float>(buffer + 4);
2194
John Bauman19bac1e2014-05-06 15:23:49 -04002195 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X) + xMask * 16, 16));
John Bauman89401822014-05-06 15:04:28 -04002196 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002197 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
John Bauman89401822014-05-06 15:04:28 -04002198
2199 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04002200 *Pointer<Float>(buffer + 0) = oC.x.z;
2201 *Pointer<Float>(buffer + 4) = oC.x.w;
John Bauman89401822014-05-06 15:04:28 -04002202
2203 buffer -= *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2204
2205 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04002206 *Pointer<Float>(buffer + 0) = oC.x.x;
2207 *Pointer<Float>(buffer + 4) = oC.x.y;
John Bauman89401822014-05-06 15:04:28 -04002208 }
2209 break;
2210 case FORMAT_G32R32F:
2211 buffer = cBuffer + 8 * x;
2212
2213 value = *Pointer<Float4>(buffer);
2214
2215 if((rgbaWriteMask & 0x00000003) != 0x00000003)
2216 {
2217 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002218 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD01X[rgbaWriteMask & 0x3][0])));
John Bauman89401822014-05-06 15:04:28 -04002219 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD01X[rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002220 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002221 }
2222
John Bauman19bac1e2014-05-06 15:23:49 -04002223 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskQ01X) + xMask * 16, 16));
John Bauman89401822014-05-06 15:04:28 -04002224 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskQ01X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002225 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
2226 *Pointer<Float4>(buffer) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04002227
2228 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2229
2230 value = *Pointer<Float4>(buffer);
2231
2232 if((rgbaWriteMask & 0x00000003) != 0x00000003)
2233 {
2234 Float4 masked;
2235
2236 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002237 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD01X[rgbaWriteMask & 0x3][0])));
John Bauman89401822014-05-06 15:04:28 -04002238 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD01X[rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002239 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002240 }
2241
John Bauman19bac1e2014-05-06 15:23:49 -04002242 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskQ23X) + xMask * 16, 16));
John Bauman89401822014-05-06 15:04:28 -04002243 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskQ23X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002244 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
2245 *Pointer<Float4>(buffer) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04002246 break;
2247 case FORMAT_A32B32G32R32F:
2248 buffer = cBuffer + 16 * x;
2249
2250 {
2251 value = *Pointer<Float4>(buffer, 16);
2252
2253 if(rgbaWriteMask != 0x0000000F)
2254 {
2255 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002256 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04002257 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002258 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002259 }
2260
John Bauman19bac1e2014-05-06 15:23:49 -04002261 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskX0X) + xMask * 16, 16));
John Bauman89401822014-05-06 15:04:28 -04002262 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX0X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002263 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
2264 *Pointer<Float4>(buffer, 16) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04002265 }
2266
2267 {
2268 value = *Pointer<Float4>(buffer + 16, 16);
2269
2270 if(rgbaWriteMask != 0x0000000F)
2271 {
2272 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002273 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04002274 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002275 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002276 }
2277
John Bauman19bac1e2014-05-06 15:23:49 -04002278 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskX1X) + xMask * 16, 16));
John Bauman89401822014-05-06 15:04:28 -04002279 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX1X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002280 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
2281 *Pointer<Float4>(buffer + 16, 16) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04002282 }
2283
2284 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2285
2286 {
2287 value = *Pointer<Float4>(buffer, 16);
2288
2289 if(rgbaWriteMask != 0x0000000F)
2290 {
2291 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002292 oC.z = As<Float4>(As<Int4>(oC.z) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04002293 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002294 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002295 }
2296
John Bauman19bac1e2014-05-06 15:23:49 -04002297 oC.z = As<Float4>(As<Int4>(oC.z) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskX2X) + xMask * 16, 16));
John Bauman89401822014-05-06 15:04:28 -04002298 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX2X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002299 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(value));
2300 *Pointer<Float4>(buffer, 16) = oC.z;
John Bauman89401822014-05-06 15:04:28 -04002301 }
2302
2303 {
2304 value = *Pointer<Float4>(buffer + 16, 16);
2305
2306 if(rgbaWriteMask != 0x0000000F)
2307 {
2308 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002309 oC.w = As<Float4>(As<Int4>(oC.w) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04002310 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002311 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002312 }
2313
John Bauman19bac1e2014-05-06 15:23:49 -04002314 oC.w = As<Float4>(As<Int4>(oC.w) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskX3X) + xMask * 16, 16));
John Bauman89401822014-05-06 15:04:28 -04002315 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX3X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002316 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(value));
2317 *Pointer<Float4>(buffer + 16, 16) = oC.w;
John Bauman89401822014-05-06 15:04:28 -04002318 }
2319 break;
2320 default:
2321 ASSERT(false);
2322 }
2323 }
2324
John Bauman89401822014-05-06 15:04:28 -04002325 UShort4 PixelRoutine::convertFixed16(Float4 &cf, bool saturate)
2326 {
John Bauman19bac1e2014-05-06 15:23:49 -04002327 return UShort4(cf * Float4(0xFFFF), saturate);
John Bauman89401822014-05-06 15:04:28 -04002328 }
2329
Nicolas Capense1a50af2015-05-13 16:48:18 -04002330 void PixelRoutine::sRGBtoLinear16_12_16(Registers &r, Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04002331 {
John Bauman19bac1e2014-05-06 15:23:49 -04002332 c.x = As<UShort4>(c.x) >> 4;
2333 c.y = As<UShort4>(c.y) >> 4;
2334 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04002335
2336 sRGBtoLinear12_16(r, c);
2337 }
2338
Alexis Hetu96517182015-04-15 10:30:23 -04002339 void PixelRoutine::sRGBtoLinear12_16(Registers &r, Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04002340 {
Nicolas Capense1a50af2015-05-13 16:48:18 -04002341 Pointer<Byte> LUT = r.constants + OFFSET(Constants,sRGBtoLinear12_16);
John Bauman89401822014-05-06 15:04:28 -04002342
John Bauman19bac1e2014-05-06 15:23:49 -04002343 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
2344 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
2345 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
2346 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002347
John Bauman19bac1e2014-05-06 15:23:49 -04002348 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
2349 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
2350 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
2351 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002352
John Bauman19bac1e2014-05-06 15:23:49 -04002353 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
2354 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
2355 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
2356 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002357 }
2358
Nicolas Capense1a50af2015-05-13 16:48:18 -04002359 void PixelRoutine::linearToSRGB16_12_16(Registers &r, Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04002360 {
John Bauman19bac1e2014-05-06 15:23:49 -04002361 c.x = As<UShort4>(c.x) >> 4;
2362 c.y = As<UShort4>(c.y) >> 4;
2363 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04002364
2365 linearToSRGB12_16(r, c);
2366 }
2367
Alexis Hetu96517182015-04-15 10:30:23 -04002368 void PixelRoutine::linearToSRGB12_16(Registers &r, Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04002369 {
Nicolas Capense1a50af2015-05-13 16:48:18 -04002370 Pointer<Byte> LUT = r.constants + OFFSET(Constants,linearToSRGB12_16);
John Bauman89401822014-05-06 15:04:28 -04002371
John Bauman19bac1e2014-05-06 15:23:49 -04002372 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
2373 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
2374 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
2375 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002376
John Bauman19bac1e2014-05-06 15:23:49 -04002377 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
2378 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
2379 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
2380 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002381
John Bauman19bac1e2014-05-06 15:23:49 -04002382 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
2383 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
2384 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
2385 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002386 }
2387
John Bauman89401822014-05-06 15:04:28 -04002388 Float4 PixelRoutine::sRGBtoLinear(const Float4 &x) // Approximates x^2.2
2389 {
2390 Float4 linear = x * x;
2391 linear = linear * Float4(0.73f) + linear * x * Float4(0.27f);
2392
2393 return Min(Max(linear, Float4(0.0f)), Float4(1.0f));
2394 }
2395
John Bauman19bac1e2014-05-06 15:23:49 -04002396 bool PixelRoutine::colorUsed()
2397 {
2398 return state.colorWriteMask || state.alphaTestActive() || state.shaderContainsKill;
2399 }
John Bauman89401822014-05-06 15:04:28 -04002400}