blob: e1b735d2caef88f985089870c6d510bb7839dfcd [file] [log] [blame]
John Bauman89401822014-05-06 15:04:28 -04001// SwiftShader Software Renderer
2//
John Bauman19bac1e2014-05-06 15:23:49 -04003// Copyright(c) 2005-2012 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
23#include <assert.h>
24
25extern bool localShaderConstants;
26
27namespace sw
28{
29 extern bool complementaryDepthBuffer;
30 extern bool postBlendSRGB;
31 extern bool exactColorRounding;
John Bauman19bac1e2014-05-06 15:23:49 -040032 extern bool booleanFaceRegister;
33 extern bool halfIntegerCoordinates; // Pixel centers are not at integer coordinates
34 extern bool fullPixelPositionRegister;
John Bauman89401822014-05-06 15:04:28 -040035
John Bauman19bac1e2014-05-06 15:23:49 -040036 PixelRoutine::PixelRoutine(const PixelProcessor::State &state, const PixelShader *shader) : Rasterizer(state), shader(shader)
John Bauman89401822014-05-06 15:04:28 -040037 {
38 perturbate = false;
39 luminance = false;
40 previousScaling = false;
41
John Bauman89401822014-05-06 15:04:28 -040042 ifDepth = 0;
43 loopRepDepth = 0;
44 breakDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040045 currentLabel = -1;
46 whileTest = false;
John Bauman89401822014-05-06 15:04:28 -040047
48 for(int i = 0; i < 2048; i++)
49 {
50 labelBlock[i] = 0;
51 }
52 }
53
54 PixelRoutine::~PixelRoutine()
55 {
56 for(int i = 0; i < 16; i++)
57 {
58 delete sampler[i];
59 }
60 }
61
62 void PixelRoutine::quad(Registers &r, Pointer<Byte> cBuffer[4], Pointer<Byte> &zBuffer, Pointer<Byte> &sBuffer, Int cMask[4], Int &x, Int &y)
63 {
64 #if PERF_PROFILE
65 Long pipeTime = Ticks();
66 #endif
67
68 for(int i = 0; i < 16; i++)
69 {
70 sampler[i] = new SamplerCore(r.constants, state.sampler[i]);
71 }
72
73 const bool earlyDepthTest = !state.depthOverride && !state.alphaTestActive();
John Bauman19bac1e2014-05-06 15:23:49 -040074 const bool integerPipeline = shaderVersion() <= 0x0104;
John Bauman89401822014-05-06 15:04:28 -040075
76 Int zMask[4]; // Depth mask
77 Int sMask[4]; // Stencil mask
78
79 for(unsigned int q = 0; q < state.multiSample; q++)
80 {
81 zMask[q] = cMask[q];
82 sMask[q] = cMask[q];
83 }
84
85 for(unsigned int q = 0; q < state.multiSample; q++)
86 {
87 stencilTest(r, sBuffer, q, x, sMask[q], cMask[q]);
88 }
89
90 Float4 f;
91
John Bauman19bac1e2014-05-06 15:23:49 -040092 Vector4i &current = r.ri[0];
93 Vector4i &diffuse = r.vi[0];
94 Vector4i &specular = r.vi[1];
John Bauman89401822014-05-06 15:04:28 -040095
96 Float4 (&z)[4] = r.z;
John Bauman19bac1e2014-05-06 15:23:49 -040097 Float4 &w = r.w;
John Bauman89401822014-05-06 15:04:28 -040098 Float4 &rhw = r.rhw;
99 Float4 rhwCentroid;
100
101 Float4 xxxx = Float4(Float(x)) + *Pointer<Float4>(r.primitive + OFFSET(Primitive,xQuad), 16);
102 Float4 yyyy = Float4(Float(y)) + *Pointer<Float4>(r.primitive + OFFSET(Primitive,yQuad), 16);
103
John Bauman19bac1e2014-05-06 15:23:49 -0400104 if(interpolateZ())
John Bauman89401822014-05-06 15:04:28 -0400105 {
106 for(unsigned int q = 0; q < state.multiSample; q++)
107 {
108 Float4 x = xxxx;
109
110 if(state.multiSample > 1)
111 {
112 x -= *Pointer<Float4>(r.constants + OFFSET(Constants,X) + q * sizeof(float4));
113 }
114
115 z[q] = interpolate(x, r.Dz[q], z[q], r.primitive + OFFSET(Primitive,z), false, false);
116 }
117 }
118
119 Bool depthPass = false;
120
121 if(earlyDepthTest)
122 {
123 for(unsigned int q = 0; q < state.multiSample; q++)
124 {
125 depthPass = depthPass || depthTest(r, zBuffer, q, x, z[q], sMask[q], zMask[q], cMask[q]);
126 }
127 }
128
129 If(depthPass || Bool(!earlyDepthTest))
130 {
131 #if PERF_PROFILE
132 Long interpTime = Ticks();
133 #endif
134
135 // Centroid locations
136 Float4 XXXX = Float4(0.0f);
137 Float4 YYYY = Float4(0.0f);
138
139 if(state.centroid)
140 {
141 Float4 WWWW(1.0e-9f);
142
143 for(unsigned int q = 0; q < state.multiSample; q++)
144 {
145 XXXX += *Pointer<Float4>(r.constants + OFFSET(Constants,sampleX[q]) + 16 * cMask[q]);
146 YYYY += *Pointer<Float4>(r.constants + OFFSET(Constants,sampleY[q]) + 16 * cMask[q]);
147 WWWW += *Pointer<Float4>(r.constants + OFFSET(Constants,weight) + 16 * cMask[q]);
148 }
149
150 WWWW = Rcp_pp(WWWW);
151 XXXX *= WWWW;
152 YYYY *= WWWW;
153
154 XXXX += xxxx;
155 YYYY += yyyy;
156 }
157
John Bauman19bac1e2014-05-06 15:23:49 -0400158 if(interpolateW())
John Bauman89401822014-05-06 15:04:28 -0400159 {
John Bauman19bac1e2014-05-06 15:23:49 -0400160 w = interpolate(xxxx, r.Dw, rhw, r.primitive + OFFSET(Primitive,w), false, false);
161 rhw = reciprocal(w);
John Bauman89401822014-05-06 15:04:28 -0400162
163 if(state.centroid)
164 {
165 rhwCentroid = reciprocal(interpolateCentroid(XXXX, YYYY, rhwCentroid, r.primitive + OFFSET(Primitive,w), false, false));
166 }
167 }
168
169 for(int interpolant = 0; interpolant < 10; interpolant++)
170 {
171 for(int component = 0; component < 4; component++)
172 {
John Bauman89401822014-05-06 15:04:28 -0400173 if(state.interpolant[interpolant].component & (1 << component))
174 {
175 if(!state.interpolant[interpolant].centroid)
176 {
John Bauman19bac1e2014-05-06 15:23:49 -0400177 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 -0400178 }
179 else
180 {
John Bauman19bac1e2014-05-06 15:23:49 -0400181 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 -0400182 }
183 }
184 }
185
186 Float4 rcp;
187
188 switch(state.interpolant[interpolant].project)
189 {
190 case 0:
191 break;
192 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400193 rcp = reciprocal(r.vf[interpolant].y);
194 r.vf[interpolant].x = r.vf[interpolant].x * rcp;
John Bauman89401822014-05-06 15:04:28 -0400195 break;
196 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400197 rcp = reciprocal(r.vf[interpolant].z);
198 r.vf[interpolant].x = r.vf[interpolant].x * rcp;
199 r.vf[interpolant].y = r.vf[interpolant].y * rcp;
John Bauman89401822014-05-06 15:04:28 -0400200 break;
201 case 3:
John Bauman19bac1e2014-05-06 15:23:49 -0400202 rcp = reciprocal(r.vf[interpolant].w);
203 r.vf[interpolant].x = r.vf[interpolant].x * rcp;
204 r.vf[interpolant].y = r.vf[interpolant].y * rcp;
205 r.vf[interpolant].z = r.vf[interpolant].z * rcp;
John Bauman89401822014-05-06 15:04:28 -0400206 break;
207 }
208 }
209
210 if(state.fog.component)
211 {
212 f = interpolate(xxxx, r.Df, rhw, r.primitive + OFFSET(Primitive,f), state.fog.flat & 0x01, state.perspective);
213 }
214
215 if(integerPipeline)
216 {
John Bauman19bac1e2014-05-06 15:23:49 -0400217 if(state.color[0].component & 0x1) diffuse.x = convertFixed12(r.vf[0].x); else diffuse.x = Short4(0x1000);
218 if(state.color[0].component & 0x2) diffuse.y = convertFixed12(r.vf[0].y); else diffuse.y = Short4(0x1000);
219 if(state.color[0].component & 0x4) diffuse.z = convertFixed12(r.vf[0].z); else diffuse.z = Short4(0x1000);
220 if(state.color[0].component & 0x8) diffuse.w = convertFixed12(r.vf[0].w); else diffuse.w = Short4(0x1000);
John Bauman89401822014-05-06 15:04:28 -0400221
John Bauman19bac1e2014-05-06 15:23:49 -0400222 if(state.color[1].component & 0x1) specular.x = convertFixed12(r.vf[1].x); else specular.x = Short4(0x0000, 0x0000, 0x0000, 0x0000);
223 if(state.color[1].component & 0x2) specular.y = convertFixed12(r.vf[1].y); else specular.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
224 if(state.color[1].component & 0x4) specular.z = convertFixed12(r.vf[1].z); else specular.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
225 if(state.color[1].component & 0x8) specular.w = convertFixed12(r.vf[1].w); else specular.w = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -0400226 }
John Bauman19bac1e2014-05-06 15:23:49 -0400227 else if(shaderVersion() >= 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400228 {
John Bauman19bac1e2014-05-06 15:23:49 -0400229 if(shader->vPosDeclared)
John Bauman89401822014-05-06 15:04:28 -0400230 {
John Bauman19bac1e2014-05-06 15:23:49 -0400231 if(!halfIntegerCoordinates)
232 {
233 r.vPos.x = Float4(Float(x)) + Float4(0, 1, 0, 1);
234 r.vPos.y = Float4(Float(y)) + Float4(0, 0, 1, 1);
235 }
236 else
237 {
238 r.vPos.x = Float4(Float(x)) + Float4(0.5f, 1.5f, 0.5f, 1.5f);
239 r.vPos.y = Float4(Float(y)) + Float4(0.5f, 0.5f, 1.5f, 1.5f);
240 }
241
242 if(fullPixelPositionRegister)
243 {
244 r.vPos.z = z[0]; // FIXME: Centroid?
245 r.vPos.w = w; // FIXME: Centroid?
246 }
John Bauman89401822014-05-06 15:04:28 -0400247 }
248
John Bauman19bac1e2014-05-06 15:23:49 -0400249 if(shader->vFaceDeclared)
John Bauman89401822014-05-06 15:04:28 -0400250 {
251 Float4 area = *Pointer<Float>(r.primitive + OFFSET(Primitive,area));
John Bauman19bac1e2014-05-06 15:23:49 -0400252 Float4 face = booleanFaceRegister ? As<Float4>(CmpNLT(area, Float4(0.0f))) : area;
253
254 r.vFace.x = face;
255 r.vFace.y = face;
256 r.vFace.z = face;
257 r.vFace.w = face;
John Bauman89401822014-05-06 15:04:28 -0400258 }
259 }
260
261 #if PERF_PROFILE
262 r.cycles[PERF_INTERP] += Ticks() - interpTime;
263 #endif
264
265 Bool alphaPass = true;
266
267 if(colorUsed())
268 {
269 #if PERF_PROFILE
270 Long shaderTime = Ticks();
271 #endif
272
John Bauman19bac1e2014-05-06 15:23:49 -0400273 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400274 {
John Bauman19bac1e2014-05-06 15:23:49 -0400275 // shader->print("PixelShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -0400276
John Bauman19bac1e2014-05-06 15:23:49 -0400277 if(shader->getVersion() <= 0x0104)
John Bauman89401822014-05-06 15:04:28 -0400278 {
279 ps_1_x(r, cMask);
280 }
281 else
282 {
283 ps_2_x(r, cMask);
284 }
285 }
286 else
287 {
288 current = diffuse;
John Bauman19bac1e2014-05-06 15:23:49 -0400289 Vector4i temp(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -0400290
291 for(int stage = 0; stage < 8; stage++)
292 {
293 if(state.textureStage[stage].stageOperation == TextureStage::STAGE_DISABLE)
294 {
295 break;
296 }
297
John Bauman19bac1e2014-05-06 15:23:49 -0400298 Vector4i texture;
John Bauman89401822014-05-06 15:04:28 -0400299
300 if(state.textureStage[stage].usesTexture)
301 {
302 sampleTexture(r, texture, stage, stage);
303 }
304
305 blendTexture(r, current, temp, texture, stage);
306 }
307
308 specularPixel(current, specular);
309 }
310
311 #if PERF_PROFILE
312 r.cycles[PERF_SHADER] += Ticks() - shaderTime;
313 #endif
314
315 if(integerPipeline)
316 {
John Bauman19bac1e2014-05-06 15:23:49 -0400317 current.x = Min(current.x, Short4(0x0FFF, 0x0FFF, 0x0FFF, 0x0FFF)); current.x = Max(current.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));
318 current.y = Min(current.y, Short4(0x0FFF, 0x0FFF, 0x0FFF, 0x0FFF)); current.y = Max(current.y, Short4(0x0000, 0x0000, 0x0000, 0x0000));
319 current.z = Min(current.z, Short4(0x0FFF, 0x0FFF, 0x0FFF, 0x0FFF)); current.z = Max(current.z, Short4(0x0000, 0x0000, 0x0000, 0x0000));
320 current.w = Min(current.w, Short4(0x0FFF, 0x0FFF, 0x0FFF, 0x0FFF)); current.w = Max(current.w, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman89401822014-05-06 15:04:28 -0400321
322 alphaPass = alphaTest(r, cMask, current);
323 }
324 else
325 {
326 clampColor(r.oC);
327
328 alphaPass = alphaTest(r, cMask, r.oC[0]);
329 }
330
John Bauman19bac1e2014-05-06 15:23:49 -0400331 if((shader && shader->containsKill()) || state.alphaTestActive())
John Bauman89401822014-05-06 15:04:28 -0400332 {
333 for(unsigned int q = 0; q < state.multiSample; q++)
334 {
335 zMask[q] &= cMask[q];
336 sMask[q] &= cMask[q];
337 }
338 }
339 }
340
341 If(alphaPass)
342 {
343 if(!earlyDepthTest)
344 {
345 for(unsigned int q = 0; q < state.multiSample; q++)
346 {
347 depthPass = depthPass || depthTest(r, zBuffer, q, x, z[q], sMask[q], zMask[q], cMask[q]);
348 }
349 }
350
351 #if PERF_PROFILE
352 Long ropTime = Ticks();
353 #endif
354
355 If(depthPass || Bool(earlyDepthTest))
356 {
357 for(unsigned int q = 0; q < state.multiSample; q++)
358 {
359 if(state.multiSampleMask & (1 << q))
360 {
361 writeDepth(r, zBuffer, q, x, z[q], zMask[q]);
362
363 if(state.occlusionEnabled)
364 {
365 r.occlusion += *Pointer<UInt>(r.constants + OFFSET(Constants,occlusionCount) + 4 * (zMask[q] & sMask[q]));
366 }
367 }
368 }
369
370 if(colorUsed())
371 {
372 #if PERF_PROFILE
373 AddAtomic(Pointer<Long>(&profiler.ropOperations), Long(4));
374 #endif
375
376 if(integerPipeline)
377 {
378 rasterOperation(current, r, f, cBuffer[0], x, sMask, zMask, cMask);
379 }
380 else
381 {
382 rasterOperation(r.oC, r, f, cBuffer, x, sMask, zMask, cMask);
383 }
384 }
385 }
386
387 #if PERF_PROFILE
388 r.cycles[PERF_ROP] += Ticks() - ropTime;
389 #endif
390 }
391 }
392
393 for(unsigned int q = 0; q < state.multiSample; q++)
394 {
395 if(state.multiSampleMask & (1 << q))
396 {
397 writeStencil(r, sBuffer, q, x, sMask[q], zMask[q], cMask[q]);
398 }
399 }
400
401 #if PERF_PROFILE
402 r.cycles[PERF_PIPE] += Ticks() - pipeTime;
403 #endif
404 }
405
406 Float4 PixelRoutine::interpolate(Float4 &x, Float4 &D, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective)
407 {
408 Float4 interpolant = D;
409
410 if(!flat)
411 {
412 interpolant += x * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,A), 16);
413
414 if(perspective)
415 {
416 interpolant *= rhw;
417 }
418 }
419
420 return interpolant;
421 }
422
423 Float4 PixelRoutine::interpolateCentroid(Float4 &x, Float4 &y, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective)
424 {
425 Float4 interpolant = *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,C), 16);
426
427 if(!flat)
428 {
429 interpolant += x * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,A), 16) +
430 y * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,B), 16);
431
432 if(perspective)
433 {
434 interpolant *= rhw;
435 }
436 }
437
438 return interpolant;
439 }
440
441 void PixelRoutine::stencilTest(Registers &r, Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &cMask)
442 {
443 if(!state.stencilActive)
444 {
445 return;
446 }
447
448 // (StencilRef & StencilMask) CompFunc (StencilBufferValue & StencilMask)
449
450 Pointer<Byte> buffer = sBuffer + 2 * x;
451
452 if(q > 0)
453 {
454 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,stencilSliceB));
455 }
456
457 Byte8 value = As<Byte8>(Long1(*Pointer<UInt>(buffer)));
458 Byte8 valueCCW = value;
459
460 if(!state.noStencilMask)
461 {
462 value &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[0].testMaskQ));
463 }
464
465 stencilTest(r, value, (Context::StencilCompareMode)state.stencilCompareMode, false);
466
467 if(state.twoSidedStencil)
468 {
469 if(!state.noStencilMaskCCW)
470 {
471 valueCCW &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[1].testMaskQ));
472 }
473
474 stencilTest(r, valueCCW, (Context::StencilCompareMode)state.stencilCompareModeCCW, true);
475
476 value &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,clockwiseMask));
477 valueCCW &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,invClockwiseMask));
478 value |= valueCCW;
479 }
480
481 sMask = SignMask(value) & cMask;
482 }
483
484 void PixelRoutine::stencilTest(Registers &r, Byte8 &value, Context::StencilCompareMode stencilCompareMode, bool CCW)
485 {
486 Byte8 equal;
487
488 switch(stencilCompareMode)
489 {
490 case Context::STENCIL_ALWAYS:
491 value = Byte8(0xFFFFFFFFFFFFFFFF);
492 break;
493 case Context::STENCIL_NEVER:
494 value = Byte8(0x0000000000000000);
495 break;
496 case Context::STENCIL_LESS: // a < b ~ b > a
497 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
498 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
499 break;
500 case Context::STENCIL_EQUAL:
501 value = CmpEQ(value, *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
502 break;
503 case Context::STENCIL_NOTEQUAL: // a != b ~ !(a == b)
504 value = CmpEQ(value, *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
505 value ^= Byte8(0xFFFFFFFFFFFFFFFF);
506 break;
507 case Context::STENCIL_LESSEQUAL: // a <= b ~ (b > a) || (a == b)
508 equal = value;
509 equal = CmpEQ(equal, *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
510 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
511 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
512 value |= equal;
513 break;
514 case Context::STENCIL_GREATER: // a > b
515 equal = *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ));
516 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
517 equal = CmpGT(As<SByte8>(equal), As<SByte8>(value));
518 value = equal;
519 break;
520 case Context::STENCIL_GREATEREQUAL: // a >= b ~ !(a < b) ~ !(b > a)
521 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
522 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
523 value ^= Byte8(0xFFFFFFFFFFFFFFFF);
524 break;
525 default:
526 ASSERT(false);
527 }
528 }
529
530 Bool PixelRoutine::depthTest(Registers &r, Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &sMask, Int &zMask, Int &cMask)
531 {
532 if(!state.depthTestActive)
533 {
534 return true;
535 }
536
537 Float4 Z = z;
538
John Bauman19bac1e2014-05-06 15:23:49 -0400539 if(shader && shader->depthOverride())
John Bauman89401822014-05-06 15:04:28 -0400540 {
541 if(complementaryDepthBuffer)
542 {
John Bauman19bac1e2014-05-06 15:23:49 -0400543 Z = Float4(1.0f) - r.oDepth;
John Bauman89401822014-05-06 15:04:28 -0400544 }
545 else
546 {
547 Z = r.oDepth;
548 }
549 }
550
551 Pointer<Byte> buffer;
552 Int pitch;
553
554 if(!state.quadLayoutDepthBuffer)
555 {
556 buffer = zBuffer + 4 * x;
557 pitch = *Pointer<Int>(r.data + OFFSET(DrawData,depthPitchB));
558 }
559 else
560 {
561 buffer = zBuffer + 8 * x;
562 }
563
564 if(q > 0)
565 {
566 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,depthSliceB));
567 }
568
569 Float4 zValue;
570
571 if(state.depthCompareMode != Context::DEPTH_NEVER || (state.depthCompareMode != Context::DEPTH_ALWAYS && !state.depthWriteEnable))
572 {
573 if(!state.quadLayoutDepthBuffer)
574 {
575 // FIXME: Properly optimizes?
576 zValue.xy = *Pointer<Float4>(buffer);
577 zValue.zw = *Pointer<Float4>(buffer + pitch - 8);
578 }
579 else
580 {
581 zValue = *Pointer<Float4>(buffer, 16);
582 }
583 }
584
585 Int4 zTest;
586
587 switch(state.depthCompareMode)
588 {
589 case Context::DEPTH_ALWAYS:
590 // Optimized
591 break;
592 case Context::DEPTH_NEVER:
593 // Optimized
594 break;
595 case Context::DEPTH_EQUAL:
596 zTest = CmpEQ(zValue, Z);
597 break;
598 case Context::DEPTH_NOTEQUAL:
599 zTest = CmpNEQ(zValue, Z);
600 break;
601 case Context::DEPTH_LESS:
602 if(complementaryDepthBuffer)
603 {
604 zTest = CmpLT(zValue, Z);
605 }
606 else
607 {
608 zTest = CmpNLE(zValue, Z);
609 }
610 break;
611 case Context::DEPTH_GREATEREQUAL:
612 if(complementaryDepthBuffer)
613 {
614 zTest = CmpNLT(zValue, Z);
615 }
616 else
617 {
618 zTest = CmpLE(zValue, Z);
619 }
620 break;
621 case Context::DEPTH_LESSEQUAL:
622 if(complementaryDepthBuffer)
623 {
624 zTest = CmpLE(zValue, Z);
625 }
626 else
627 {
628 zTest = CmpNLT(zValue, Z);
629 }
630 break;
631 case Context::DEPTH_GREATER:
632 if(complementaryDepthBuffer)
633 {
634 zTest = CmpNLE(zValue, Z);
635 }
636 else
637 {
638 zTest = CmpLT(zValue, Z);
639 }
640 break;
641 default:
642 ASSERT(false);
643 }
644
645 switch(state.depthCompareMode)
646 {
647 case Context::DEPTH_ALWAYS:
648 zMask = cMask;
649 break;
650 case Context::DEPTH_NEVER:
651 zMask = 0x0;
652 break;
653 default:
654 zMask = SignMask(zTest) & cMask;
655 break;
656 }
657
658 if(state.stencilActive)
659 {
660 zMask &= sMask;
661 }
662
663 return zMask != 0;
664 }
665
John Bauman19bac1e2014-05-06 15:23:49 -0400666 void PixelRoutine::blendTexture(Registers &r, Vector4i &current, Vector4i &temp, Vector4i &texture, int stage)
John Bauman89401822014-05-06 15:04:28 -0400667 {
John Bauman19bac1e2014-05-06 15:23:49 -0400668 Vector4i *arg1;
669 Vector4i *arg2;
670 Vector4i *arg3;
671 Vector4i res;
John Bauman89401822014-05-06 15:04:28 -0400672
John Bauman19bac1e2014-05-06 15:23:49 -0400673 Vector4i constant;
674 Vector4i tfactor;
John Bauman89401822014-05-06 15:04:28 -0400675
676 const TextureStage::State &textureStage = state.textureStage[stage];
677
678 if(textureStage.firstArgument == TextureStage::SOURCE_CONSTANT ||
679 textureStage.firstArgumentAlpha == TextureStage::SOURCE_CONSTANT ||
680 textureStage.secondArgument == TextureStage::SOURCE_CONSTANT ||
681 textureStage.secondArgumentAlpha == TextureStage::SOURCE_CONSTANT ||
682 textureStage.thirdArgument == TextureStage::SOURCE_CONSTANT ||
683 textureStage.thirdArgumentAlpha == TextureStage::SOURCE_CONSTANT)
684 {
John Bauman19bac1e2014-05-06 15:23:49 -0400685 constant.x = *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].constantColor4[0]));
686 constant.y = *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].constantColor4[1]));
687 constant.z = *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].constantColor4[2]));
688 constant.w = *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].constantColor4[3]));
John Bauman89401822014-05-06 15:04:28 -0400689 }
690
691 if(textureStage.firstArgument == TextureStage::SOURCE_TFACTOR ||
692 textureStage.firstArgumentAlpha == TextureStage::SOURCE_TFACTOR ||
693 textureStage.secondArgument == TextureStage::SOURCE_TFACTOR ||
694 textureStage.secondArgumentAlpha == TextureStage::SOURCE_TFACTOR ||
695 textureStage.thirdArgument == TextureStage::SOURCE_TFACTOR ||
696 textureStage.thirdArgumentAlpha == TextureStage::SOURCE_TFACTOR)
697 {
John Bauman19bac1e2014-05-06 15:23:49 -0400698 tfactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[0]));
699 tfactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[1]));
700 tfactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[2]));
701 tfactor.w = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[3]));
John Bauman89401822014-05-06 15:04:28 -0400702 }
703
704 // Premodulate
705 if(stage > 0 && textureStage.usesTexture)
706 {
707 if(state.textureStage[stage - 1].stageOperation == TextureStage::STAGE_PREMODULATE)
708 {
John Bauman19bac1e2014-05-06 15:23:49 -0400709 current.x = MulHigh(current.x, texture.x) << 4;
710 current.y = MulHigh(current.y, texture.y) << 4;
711 current.z = MulHigh(current.z, texture.z) << 4;
John Bauman89401822014-05-06 15:04:28 -0400712 }
713
714 if(state.textureStage[stage - 1].stageOperationAlpha == TextureStage::STAGE_PREMODULATE)
715 {
John Bauman19bac1e2014-05-06 15:23:49 -0400716 current.w = MulHigh(current.w, texture.w) << 4;
John Bauman89401822014-05-06 15:04:28 -0400717 }
718 }
719
720 if(luminance)
721 {
John Bauman19bac1e2014-05-06 15:23:49 -0400722 texture.x = MulHigh(texture.x, r.L) << 4;
723 texture.y = MulHigh(texture.y, r.L) << 4;
724 texture.z = MulHigh(texture.z, r.L) << 4;
John Bauman89401822014-05-06 15:04:28 -0400725
726 luminance = false;
727 }
728
729 switch(textureStage.firstArgument)
730 {
731 case TextureStage::SOURCE_TEXTURE: arg1 = &texture; break;
732 case TextureStage::SOURCE_CONSTANT: arg1 = &constant; break;
733 case TextureStage::SOURCE_CURRENT: arg1 = &current; break;
734 case TextureStage::SOURCE_DIFFUSE: arg1 = &r.diffuse; break;
735 case TextureStage::SOURCE_SPECULAR: arg1 = &r.specular; break;
736 case TextureStage::SOURCE_TEMP: arg1 = &temp; break;
737 case TextureStage::SOURCE_TFACTOR: arg1 = &tfactor; break;
738 default:
739 ASSERT(false);
740 }
741
742 switch(textureStage.secondArgument)
743 {
744 case TextureStage::SOURCE_TEXTURE: arg2 = &texture; break;
745 case TextureStage::SOURCE_CONSTANT: arg2 = &constant; break;
746 case TextureStage::SOURCE_CURRENT: arg2 = &current; break;
747 case TextureStage::SOURCE_DIFFUSE: arg2 = &r.diffuse; break;
748 case TextureStage::SOURCE_SPECULAR: arg2 = &r.specular; break;
749 case TextureStage::SOURCE_TEMP: arg2 = &temp; break;
750 case TextureStage::SOURCE_TFACTOR: arg2 = &tfactor; break;
751 default:
752 ASSERT(false);
753 }
754
755 switch(textureStage.thirdArgument)
756 {
757 case TextureStage::SOURCE_TEXTURE: arg3 = &texture; break;
758 case TextureStage::SOURCE_CONSTANT: arg3 = &constant; break;
759 case TextureStage::SOURCE_CURRENT: arg3 = &current; break;
760 case TextureStage::SOURCE_DIFFUSE: arg3 = &r.diffuse; break;
761 case TextureStage::SOURCE_SPECULAR: arg3 = &r.specular; break;
762 case TextureStage::SOURCE_TEMP: arg3 = &temp; break;
763 case TextureStage::SOURCE_TFACTOR: arg3 = &tfactor; break;
764 default:
765 ASSERT(false);
766 }
767
John Bauman19bac1e2014-05-06 15:23:49 -0400768 Vector4i mod1;
769 Vector4i mod2;
770 Vector4i mod3;
John Bauman89401822014-05-06 15:04:28 -0400771
772 switch(textureStage.firstModifier)
773 {
774 case TextureStage::MODIFIER_COLOR:
775 break;
776 case TextureStage::MODIFIER_INVCOLOR:
777 {
John Bauman19bac1e2014-05-06 15:23:49 -0400778 mod1.x = SubSat(Short4(0x1000), arg1->x);
779 mod1.y = SubSat(Short4(0x1000), arg1->y);
780 mod1.z = SubSat(Short4(0x1000), arg1->z);
781 mod1.w = SubSat(Short4(0x1000), arg1->w);
John Bauman89401822014-05-06 15:04:28 -0400782
783 arg1 = &mod1;
784 }
785 break;
786 case TextureStage::MODIFIER_ALPHA:
787 {
John Bauman19bac1e2014-05-06 15:23:49 -0400788 mod1.x = arg1->w;
789 mod1.y = arg1->w;
790 mod1.z = arg1->w;
791 mod1.w = arg1->w;
John Bauman89401822014-05-06 15:04:28 -0400792
793 arg1 = &mod1;
794 }
795 break;
796 case TextureStage::MODIFIER_INVALPHA:
797 {
John Bauman19bac1e2014-05-06 15:23:49 -0400798 mod1.x = SubSat(Short4(0x1000), arg1->w);
799 mod1.y = SubSat(Short4(0x1000), arg1->w);
800 mod1.z = SubSat(Short4(0x1000), arg1->w);
801 mod1.w = SubSat(Short4(0x1000), arg1->w);
John Bauman89401822014-05-06 15:04:28 -0400802
803 arg1 = &mod1;
804 }
805 break;
806 default:
807 ASSERT(false);
808 }
809
810 switch(textureStage.secondModifier)
811 {
812 case TextureStage::MODIFIER_COLOR:
813 break;
814 case TextureStage::MODIFIER_INVCOLOR:
815 {
John Bauman19bac1e2014-05-06 15:23:49 -0400816 mod2.x = SubSat(Short4(0x1000), arg2->x);
817 mod2.y = SubSat(Short4(0x1000), arg2->y);
818 mod2.z = SubSat(Short4(0x1000), arg2->z);
819 mod2.w = SubSat(Short4(0x1000), arg2->w);
John Bauman89401822014-05-06 15:04:28 -0400820
821 arg2 = &mod2;
822 }
823 break;
824 case TextureStage::MODIFIER_ALPHA:
825 {
John Bauman19bac1e2014-05-06 15:23:49 -0400826 mod2.x = arg2->w;
827 mod2.y = arg2->w;
828 mod2.z = arg2->w;
829 mod2.w = arg2->w;
John Bauman89401822014-05-06 15:04:28 -0400830
831 arg2 = &mod2;
832 }
833 break;
834 case TextureStage::MODIFIER_INVALPHA:
835 {
John Bauman19bac1e2014-05-06 15:23:49 -0400836 mod2.x = SubSat(Short4(0x1000), arg2->w);
837 mod2.y = SubSat(Short4(0x1000), arg2->w);
838 mod2.z = SubSat(Short4(0x1000), arg2->w);
839 mod2.w = SubSat(Short4(0x1000), arg2->w);
John Bauman89401822014-05-06 15:04:28 -0400840
841 arg2 = &mod2;
842 }
843 break;
844 default:
845 ASSERT(false);
846 }
847
848 switch(textureStage.thirdModifier)
849 {
850 case TextureStage::MODIFIER_COLOR:
851 break;
852 case TextureStage::MODIFIER_INVCOLOR:
853 {
John Bauman19bac1e2014-05-06 15:23:49 -0400854 mod3.x = SubSat(Short4(0x1000), arg3->x);
855 mod3.y = SubSat(Short4(0x1000), arg3->y);
856 mod3.z = SubSat(Short4(0x1000), arg3->z);
857 mod3.w = SubSat(Short4(0x1000), arg3->w);
John Bauman89401822014-05-06 15:04:28 -0400858
859 arg3 = &mod3;
860 }
861 break;
862 case TextureStage::MODIFIER_ALPHA:
863 {
John Bauman19bac1e2014-05-06 15:23:49 -0400864 mod3.x = arg3->w;
865 mod3.y = arg3->w;
866 mod3.z = arg3->w;
867 mod3.w = arg3->w;
John Bauman89401822014-05-06 15:04:28 -0400868
869 arg3 = &mod3;
870 }
871 break;
872 case TextureStage::MODIFIER_INVALPHA:
873 {
John Bauman19bac1e2014-05-06 15:23:49 -0400874 mod3.x = SubSat(Short4(0x1000), arg3->w);
875 mod3.y = SubSat(Short4(0x1000), arg3->w);
876 mod3.z = SubSat(Short4(0x1000), arg3->w);
877 mod3.w = SubSat(Short4(0x1000), arg3->w);
John Bauman89401822014-05-06 15:04:28 -0400878
879 arg3 = &mod3;
880 }
881 break;
882 default:
883 ASSERT(false);
884 }
885
886 switch(textureStage.stageOperation)
887 {
888 case TextureStage::STAGE_DISABLE:
889 break;
890 case TextureStage::STAGE_SELECTARG1: // Arg1
891 {
John Bauman19bac1e2014-05-06 15:23:49 -0400892 res.x = arg1->x;
893 res.y = arg1->y;
894 res.z = arg1->z;
John Bauman89401822014-05-06 15:04:28 -0400895 }
896 break;
897 case TextureStage::STAGE_SELECTARG2: // Arg2
898 {
John Bauman19bac1e2014-05-06 15:23:49 -0400899 res.x = arg2->x;
900 res.y = arg2->y;
901 res.z = arg2->z;
John Bauman89401822014-05-06 15:04:28 -0400902 }
903 break;
904 case TextureStage::STAGE_SELECTARG3: // Arg3
905 {
John Bauman19bac1e2014-05-06 15:23:49 -0400906 res.x = arg3->x;
907 res.y = arg3->y;
908 res.z = arg3->z;
John Bauman89401822014-05-06 15:04:28 -0400909 }
910 break;
911 case TextureStage::STAGE_MODULATE: // Arg1 * Arg2
912 {
John Bauman19bac1e2014-05-06 15:23:49 -0400913 res.x = MulHigh(arg1->x, arg2->x) << 4;
914 res.y = MulHigh(arg1->y, arg2->y) << 4;
915 res.z = MulHigh(arg1->z, arg2->z) << 4;
John Bauman89401822014-05-06 15:04:28 -0400916 }
917 break;
918 case TextureStage::STAGE_MODULATE2X: // Arg1 * Arg2 * 2
919 {
John Bauman19bac1e2014-05-06 15:23:49 -0400920 res.x = MulHigh(arg1->x, arg2->x) << 5;
921 res.y = MulHigh(arg1->y, arg2->y) << 5;
922 res.z = MulHigh(arg1->z, arg2->z) << 5;
John Bauman89401822014-05-06 15:04:28 -0400923 }
924 break;
925 case TextureStage::STAGE_MODULATE4X: // Arg1 * Arg2 * 4
926 {
John Bauman19bac1e2014-05-06 15:23:49 -0400927 res.x = MulHigh(arg1->x, arg2->x) << 6;
928 res.y = MulHigh(arg1->y, arg2->y) << 6;
929 res.z = MulHigh(arg1->z, arg2->z) << 6;
John Bauman89401822014-05-06 15:04:28 -0400930 }
931 break;
932 case TextureStage::STAGE_ADD: // Arg1 + Arg2
933 {
John Bauman19bac1e2014-05-06 15:23:49 -0400934 res.x = AddSat(arg1->x, arg2->x);
935 res.y = AddSat(arg1->y, arg2->y);
936 res.z = AddSat(arg1->z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400937 }
938 break;
939 case TextureStage::STAGE_ADDSIGNED: // Arg1 + Arg2 - 0.5
940 {
John Bauman19bac1e2014-05-06 15:23:49 -0400941 res.x = AddSat(arg1->x, arg2->x);
942 res.y = AddSat(arg1->y, arg2->y);
943 res.z = AddSat(arg1->z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400944
John Bauman19bac1e2014-05-06 15:23:49 -0400945 res.x = SubSat(res.x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
946 res.y = SubSat(res.y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
947 res.z = SubSat(res.z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -0400948 }
949 break;
950 case TextureStage::STAGE_ADDSIGNED2X: // (Arg1 + Arg2 - 0.5) << 1
951 {
John Bauman19bac1e2014-05-06 15:23:49 -0400952 res.x = AddSat(arg1->x, arg2->x);
953 res.y = AddSat(arg1->y, arg2->y);
954 res.z = AddSat(arg1->z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400955
John Bauman19bac1e2014-05-06 15:23:49 -0400956 res.x = SubSat(res.x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
957 res.y = SubSat(res.y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
958 res.z = SubSat(res.z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -0400959
John Bauman19bac1e2014-05-06 15:23:49 -0400960 res.x = AddSat(res.x, res.x);
961 res.y = AddSat(res.y, res.y);
962 res.z = AddSat(res.z, res.z);
John Bauman89401822014-05-06 15:04:28 -0400963 }
964 break;
965 case TextureStage::STAGE_SUBTRACT: // Arg1 - Arg2
966 {
John Bauman19bac1e2014-05-06 15:23:49 -0400967 res.x = SubSat(arg1->x, arg2->x);
968 res.y = SubSat(arg1->y, arg2->y);
969 res.z = SubSat(arg1->z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400970 }
971 break;
972 case TextureStage::STAGE_ADDSMOOTH: // Arg1 + Arg2 - Arg1 * Arg2
973 {
974 Short4 tmp;
975
John Bauman19bac1e2014-05-06 15:23:49 -0400976 tmp = MulHigh(arg1->x, arg2->x) << 4; res.x = AddSat(arg1->x, arg2->x); res.x = SubSat(res.x, tmp);
977 tmp = MulHigh(arg1->y, arg2->y) << 4; res.y = AddSat(arg1->y, arg2->y); res.y = SubSat(res.y, tmp);
978 tmp = MulHigh(arg1->z, arg2->z) << 4; res.z = AddSat(arg1->z, arg2->z); res.z = SubSat(res.z, tmp);
John Bauman89401822014-05-06 15:04:28 -0400979 }
980 break;
981 case TextureStage::STAGE_MULTIPLYADD: // Arg3 + Arg1 * Arg2
982 {
John Bauman19bac1e2014-05-06 15:23:49 -0400983 res.x = MulHigh(arg1->x, arg2->x) << 4; res.x = AddSat(res.x, arg3->x);
984 res.y = MulHigh(arg1->y, arg2->y) << 4; res.y = AddSat(res.y, arg3->y);
985 res.z = MulHigh(arg1->z, arg2->z) << 4; res.z = AddSat(res.z, arg3->z);
John Bauman89401822014-05-06 15:04:28 -0400986 }
987 break;
988 case TextureStage::STAGE_LERP: // Arg3 * (Arg1 - Arg2) + Arg2
989 {
John Bauman19bac1e2014-05-06 15:23:49 -0400990 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, arg3->x) << 4; res.x = AddSat(res.x, arg2->x);
991 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, arg3->y) << 4; res.y = AddSat(res.y, arg2->y);
992 res.z = SubSat(arg1->z, arg2->z); res.z = MulHigh(res.z, arg3->z) << 4; res.z = AddSat(res.z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400993 }
994 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400995 case TextureStage::STAGE_DOT3: // 2 * (Arg1.x - 0.5) * 2 * (Arg2.x - 0.5) + 2 * (Arg1.y - 0.5) * 2 * (Arg2.y - 0.5) + 2 * (Arg1.z - 0.5) * 2 * (Arg2.z - 0.5)
John Bauman89401822014-05-06 15:04:28 -0400996 {
997 Short4 tmp;
998
John Bauman19bac1e2014-05-06 15:23:49 -0400999 res.x = SubSat(arg1->x, Short4(0x0800, 0x0800, 0x0800, 0x0800)); tmp = SubSat(arg2->x, Short4(0x0800, 0x0800, 0x0800, 0x0800)); res.x = MulHigh(res.x, tmp);
1000 res.y = SubSat(arg1->y, Short4(0x0800, 0x0800, 0x0800, 0x0800)); tmp = SubSat(arg2->y, Short4(0x0800, 0x0800, 0x0800, 0x0800)); res.y = MulHigh(res.y, tmp);
1001 res.z = SubSat(arg1->z, Short4(0x0800, 0x0800, 0x0800, 0x0800)); tmp = SubSat(arg2->z, Short4(0x0800, 0x0800, 0x0800, 0x0800)); res.z = MulHigh(res.z, tmp);
John Bauman89401822014-05-06 15:04:28 -04001002
John Bauman19bac1e2014-05-06 15:23:49 -04001003 res.x = res.x << 6;
1004 res.y = res.y << 6;
1005 res.z = res.z << 6;
John Bauman89401822014-05-06 15:04:28 -04001006
John Bauman19bac1e2014-05-06 15:23:49 -04001007 res.x = AddSat(res.x, res.y);
1008 res.x = AddSat(res.x, res.z);
John Bauman89401822014-05-06 15:04:28 -04001009
1010 // Clamp to [0, 1]
John Bauman19bac1e2014-05-06 15:23:49 -04001011 res.x = Max(res.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));
1012 res.x = Min(res.x, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001013
John Bauman19bac1e2014-05-06 15:23:49 -04001014 res.y = res.x;
1015 res.z = res.x;
1016 res.w = res.x;
John Bauman89401822014-05-06 15:04:28 -04001017 }
1018 break;
1019 case TextureStage::STAGE_BLENDCURRENTALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1020 {
John Bauman19bac1e2014-05-06 15:23:49 -04001021 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, current.w) << 4; res.x = AddSat(res.x, arg2->x);
1022 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, current.w) << 4; res.y = AddSat(res.y, arg2->y);
1023 res.z = SubSat(arg1->z, arg2->z); res.z = MulHigh(res.z, current.w) << 4; res.z = AddSat(res.z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -04001024 }
1025 break;
1026 case TextureStage::STAGE_BLENDDIFFUSEALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1027 {
John Bauman19bac1e2014-05-06 15:23:49 -04001028 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, r.diffuse.w) << 4; res.x = AddSat(res.x, arg2->x);
1029 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, r.diffuse.w) << 4; res.y = AddSat(res.y, arg2->y);
1030 res.z = SubSat(arg1->z, arg2->z); res.z = MulHigh(res.z, r.diffuse.w) << 4; res.z = AddSat(res.z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -04001031 }
1032 break;
1033 case TextureStage::STAGE_BLENDFACTORALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1034 {
John Bauman19bac1e2014-05-06 15:23:49 -04001035 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[3]))) << 4; res.x = AddSat(res.x, arg2->x);
1036 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[3]))) << 4; res.y = AddSat(res.y, arg2->y);
1037 res.z = SubSat(arg1->z, arg2->z); res.z = MulHigh(res.z, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[3]))) << 4; res.z = AddSat(res.z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -04001038 }
1039 break;
1040 case TextureStage::STAGE_BLENDTEXTUREALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1041 {
John Bauman19bac1e2014-05-06 15:23:49 -04001042 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, texture.w) << 4; res.x = AddSat(res.x, arg2->x);
1043 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, texture.w) << 4; res.y = AddSat(res.y, arg2->y);
1044 res.z = SubSat(arg1->z, arg2->z); res.z = MulHigh(res.z, texture.w) << 4; res.z = AddSat(res.z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -04001045 }
1046 break;
1047 case TextureStage::STAGE_BLENDTEXTUREALPHAPM: // Arg1 + Arg2 * (1 - Alpha)
1048 {
John Bauman19bac1e2014-05-06 15:23:49 -04001049 res.x = SubSat(Short4(0x1000), texture.w); res.x = MulHigh(res.x, arg2->x) << 4; res.x = AddSat(res.x, arg1->x);
1050 res.y = SubSat(Short4(0x1000), texture.w); res.y = MulHigh(res.y, arg2->y) << 4; res.y = AddSat(res.y, arg1->y);
1051 res.z = SubSat(Short4(0x1000), texture.w); res.z = MulHigh(res.z, arg2->z) << 4; res.z = AddSat(res.z, arg1->z);
John Bauman89401822014-05-06 15:04:28 -04001052 }
1053 break;
1054 case TextureStage::STAGE_PREMODULATE:
1055 {
John Bauman19bac1e2014-05-06 15:23:49 -04001056 res.x = arg1->x;
1057 res.y = arg1->y;
1058 res.z = arg1->z;
John Bauman89401822014-05-06 15:04:28 -04001059 }
1060 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001061 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR: // Arg1 + Arg1.w * Arg2
John Bauman89401822014-05-06 15:04:28 -04001062 {
John Bauman19bac1e2014-05-06 15:23:49 -04001063 res.x = MulHigh(arg1->w, arg2->x) << 4; res.x = AddSat(res.x, arg1->x);
1064 res.y = MulHigh(arg1->w, arg2->y) << 4; res.y = AddSat(res.y, arg1->y);
1065 res.z = MulHigh(arg1->w, arg2->z) << 4; res.z = AddSat(res.z, arg1->z);
John Bauman89401822014-05-06 15:04:28 -04001066 }
1067 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001068 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA: // Arg1 * Arg2 + Arg1.w
John Bauman89401822014-05-06 15:04:28 -04001069 {
John Bauman19bac1e2014-05-06 15:23:49 -04001070 res.x = MulHigh(arg1->x, arg2->x) << 4; res.x = AddSat(res.x, arg1->w);
1071 res.y = MulHigh(arg1->y, arg2->y) << 4; res.y = AddSat(res.y, arg1->w);
1072 res.z = MulHigh(arg1->z, arg2->z) << 4; res.z = AddSat(res.z, arg1->w);
John Bauman89401822014-05-06 15:04:28 -04001073 }
1074 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001075 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR: // (1 - Arg1.w) * Arg2 + Arg1
John Bauman89401822014-05-06 15:04:28 -04001076 {
1077 Short4 tmp;
1078
John Bauman19bac1e2014-05-06 15:23:49 -04001079 res.x = AddSat(arg1->x, arg2->x); tmp = MulHigh(arg1->w, arg2->x) << 4; res.x = SubSat(res.x, tmp);
1080 res.y = AddSat(arg1->y, arg2->y); tmp = MulHigh(arg1->w, arg2->y) << 4; res.y = SubSat(res.y, tmp);
1081 res.z = AddSat(arg1->z, arg2->z); tmp = MulHigh(arg1->w, arg2->z) << 4; res.z = SubSat(res.z, tmp);
John Bauman89401822014-05-06 15:04:28 -04001082 }
1083 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001084 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA: // (1 - Arg1) * Arg2 + Arg1.w
John Bauman89401822014-05-06 15:04:28 -04001085 {
1086 Short4 tmp;
1087
John Bauman19bac1e2014-05-06 15:23:49 -04001088 res.x = AddSat(arg1->w, arg2->x); tmp = MulHigh(arg1->x, arg2->x) << 4; res.x = SubSat(res.x, tmp);
1089 res.y = AddSat(arg1->w, arg2->y); tmp = MulHigh(arg1->y, arg2->y) << 4; res.y = SubSat(res.y, tmp);
1090 res.z = AddSat(arg1->w, arg2->z); tmp = MulHigh(arg1->z, arg2->z) << 4; res.z = SubSat(res.z, tmp);
John Bauman89401822014-05-06 15:04:28 -04001091 }
1092 break;
1093 case TextureStage::STAGE_BUMPENVMAP:
1094 {
John Bauman19bac1e2014-05-06 15:23:49 -04001095 r.du = Float4(texture.x) * Float4(1.0f / 0x0FE0);
1096 r.dv = Float4(texture.y) * Float4(1.0f / 0x0FE0);
John Bauman89401822014-05-06 15:04:28 -04001097
1098 Float4 du2;
1099 Float4 dv2;
1100
1101 du2 = r.du;
1102 dv2 = r.dv;
1103 r.du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
1104 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
1105 r.du += dv2;
1106 r.dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
1107 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
1108 r.dv += du2;
1109
1110 perturbate = true;
1111
John Bauman19bac1e2014-05-06 15:23:49 -04001112 res.x = r.current.x;
1113 res.y = r.current.y;
1114 res.z = r.current.z;
1115 res.w = r.current.w;
John Bauman89401822014-05-06 15:04:28 -04001116 }
1117 break;
1118 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1119 {
John Bauman19bac1e2014-05-06 15:23:49 -04001120 r.du = Float4(texture.x) * Float4(1.0f / 0x0FE0);
1121 r.dv = Float4(texture.y) * Float4(1.0f / 0x0FE0);
John Bauman89401822014-05-06 15:04:28 -04001122
1123 Float4 du2;
1124 Float4 dv2;
1125
1126 du2 = r.du;
1127 dv2 = r.dv;
1128
1129 r.du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
1130 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
1131 r.du += dv2;
1132 r.dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
1133 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
1134 r.dv += du2;
1135
1136 perturbate = true;
1137
John Bauman19bac1e2014-05-06 15:23:49 -04001138 r.L = texture.z;
John Bauman89401822014-05-06 15:04:28 -04001139 r.L = MulHigh(r.L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceScale4)));
1140 r.L = r.L << 4;
1141 r.L = AddSat(r.L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceOffset4)));
1142 r.L = Max(r.L, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman19bac1e2014-05-06 15:23:49 -04001143 r.L = Min(r.L, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001144
1145 luminance = true;
1146
John Bauman19bac1e2014-05-06 15:23:49 -04001147 res.x = r.current.x;
1148 res.y = r.current.y;
1149 res.z = r.current.z;
1150 res.w = r.current.w;
John Bauman89401822014-05-06 15:04:28 -04001151 }
1152 break;
1153 default:
1154 ASSERT(false);
1155 }
1156
1157 if(textureStage.stageOperation != TextureStage::STAGE_DOT3)
1158 {
1159 switch(textureStage.firstArgumentAlpha)
1160 {
1161 case TextureStage::SOURCE_TEXTURE: arg1 = &texture; break;
1162 case TextureStage::SOURCE_CONSTANT: arg1 = &constant; break;
1163 case TextureStage::SOURCE_CURRENT: arg1 = &current; break;
1164 case TextureStage::SOURCE_DIFFUSE: arg1 = &r.diffuse; break;
1165 case TextureStage::SOURCE_SPECULAR: arg1 = &r.specular; break;
1166 case TextureStage::SOURCE_TEMP: arg1 = &temp; break;
1167 case TextureStage::SOURCE_TFACTOR: arg1 = &tfactor; break;
1168 default:
1169 ASSERT(false);
1170 }
1171
1172 switch(textureStage.secondArgumentAlpha)
1173 {
1174 case TextureStage::SOURCE_TEXTURE: arg2 = &texture; break;
1175 case TextureStage::SOURCE_CONSTANT: arg2 = &constant; break;
1176 case TextureStage::SOURCE_CURRENT: arg2 = &current; break;
1177 case TextureStage::SOURCE_DIFFUSE: arg2 = &r.diffuse; break;
1178 case TextureStage::SOURCE_SPECULAR: arg2 = &r.specular; break;
1179 case TextureStage::SOURCE_TEMP: arg2 = &temp; break;
1180 case TextureStage::SOURCE_TFACTOR: arg2 = &tfactor; break;
1181 default:
1182 ASSERT(false);
1183 }
1184
1185 switch(textureStage.thirdArgumentAlpha)
1186 {
1187 case TextureStage::SOURCE_TEXTURE: arg3 = &texture; break;
1188 case TextureStage::SOURCE_CONSTANT: arg3 = &constant; break;
1189 case TextureStage::SOURCE_CURRENT: arg3 = &current; break;
1190 case TextureStage::SOURCE_DIFFUSE: arg3 = &r.diffuse; break;
1191 case TextureStage::SOURCE_SPECULAR: arg3 = &r.specular; break;
1192 case TextureStage::SOURCE_TEMP: arg3 = &temp; break;
1193 case TextureStage::SOURCE_TFACTOR: arg3 = &tfactor; break;
1194 default:
1195 ASSERT(false);
1196 }
1197
1198 switch(textureStage.firstModifierAlpha) // FIXME: Check if actually used
1199 {
1200 case TextureStage::MODIFIER_COLOR:
1201 break;
1202 case TextureStage::MODIFIER_INVCOLOR:
1203 {
John Bauman19bac1e2014-05-06 15:23:49 -04001204 mod1.w = SubSat(Short4(0x1000), arg1->w);
John Bauman89401822014-05-06 15:04:28 -04001205
1206 arg1 = &mod1;
1207 }
1208 break;
1209 case TextureStage::MODIFIER_ALPHA:
1210 {
1211 // Redudant
1212 }
1213 break;
1214 case TextureStage::MODIFIER_INVALPHA:
1215 {
John Bauman19bac1e2014-05-06 15:23:49 -04001216 mod1.w = SubSat(Short4(0x1000), arg1->w);
John Bauman89401822014-05-06 15:04:28 -04001217
1218 arg1 = &mod1;
1219 }
1220 break;
1221 default:
1222 ASSERT(false);
1223 }
1224
1225 switch(textureStage.secondModifierAlpha) // FIXME: Check if actually used
1226 {
1227 case TextureStage::MODIFIER_COLOR:
1228 break;
1229 case TextureStage::MODIFIER_INVCOLOR:
1230 {
John Bauman19bac1e2014-05-06 15:23:49 -04001231 mod2.w = SubSat(Short4(0x1000), arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001232
1233 arg2 = &mod2;
1234 }
1235 break;
1236 case TextureStage::MODIFIER_ALPHA:
1237 {
1238 // Redudant
1239 }
1240 break;
1241 case TextureStage::MODIFIER_INVALPHA:
1242 {
John Bauman19bac1e2014-05-06 15:23:49 -04001243 mod2.w = SubSat(Short4(0x1000), arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001244
1245 arg2 = &mod2;
1246 }
1247 break;
1248 default:
1249 ASSERT(false);
1250 }
1251
1252 switch(textureStage.thirdModifierAlpha) // FIXME: Check if actually used
1253 {
1254 case TextureStage::MODIFIER_COLOR:
1255 break;
1256 case TextureStage::MODIFIER_INVCOLOR:
1257 {
John Bauman19bac1e2014-05-06 15:23:49 -04001258 mod3.w = SubSat(Short4(0x1000), arg3->w);
John Bauman89401822014-05-06 15:04:28 -04001259
1260 arg3 = &mod3;
1261 }
1262 break;
1263 case TextureStage::MODIFIER_ALPHA:
1264 {
1265 // Redudant
1266 }
1267 break;
1268 case TextureStage::MODIFIER_INVALPHA:
1269 {
John Bauman19bac1e2014-05-06 15:23:49 -04001270 mod3.w = SubSat(Short4(0x1000), arg3->w);
John Bauman89401822014-05-06 15:04:28 -04001271
1272 arg3 = &mod3;
1273 }
1274 break;
1275 default:
1276 ASSERT(false);
1277 }
1278
1279 switch(textureStage.stageOperationAlpha)
1280 {
1281 case TextureStage::STAGE_DISABLE:
1282 break;
1283 case TextureStage::STAGE_SELECTARG1: // Arg1
1284 {
John Bauman19bac1e2014-05-06 15:23:49 -04001285 res.w = arg1->w;
John Bauman89401822014-05-06 15:04:28 -04001286 }
1287 break;
1288 case TextureStage::STAGE_SELECTARG2: // Arg2
1289 {
John Bauman19bac1e2014-05-06 15:23:49 -04001290 res.w = arg2->w;
John Bauman89401822014-05-06 15:04:28 -04001291 }
1292 break;
1293 case TextureStage::STAGE_SELECTARG3: // Arg3
1294 {
John Bauman19bac1e2014-05-06 15:23:49 -04001295 res.w = arg3->w;
John Bauman89401822014-05-06 15:04:28 -04001296 }
1297 break;
1298 case TextureStage::STAGE_MODULATE: // Arg1 * Arg2
1299 {
John Bauman19bac1e2014-05-06 15:23:49 -04001300 res.w = MulHigh(arg1->w, arg2->w) << 4;
John Bauman89401822014-05-06 15:04:28 -04001301 }
1302 break;
1303 case TextureStage::STAGE_MODULATE2X: // Arg1 * Arg2 * 2
1304 {
John Bauman19bac1e2014-05-06 15:23:49 -04001305 res.w = MulHigh(arg1->w, arg2->w) << 5;
John Bauman89401822014-05-06 15:04:28 -04001306 }
1307 break;
1308 case TextureStage::STAGE_MODULATE4X: // Arg1 * Arg2 * 4
1309 {
John Bauman19bac1e2014-05-06 15:23:49 -04001310 res.w = MulHigh(arg1->w, arg2->w) << 6;
John Bauman89401822014-05-06 15:04:28 -04001311 }
1312 break;
1313 case TextureStage::STAGE_ADD: // Arg1 + Arg2
1314 {
John Bauman19bac1e2014-05-06 15:23:49 -04001315 res.w = AddSat(arg1->w, arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001316 }
1317 break;
1318 case TextureStage::STAGE_ADDSIGNED: // Arg1 + Arg2 - 0.5
1319 {
John Bauman19bac1e2014-05-06 15:23:49 -04001320 res.w = AddSat(arg1->w, arg2->w);
1321 res.w = SubSat(res.w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -04001322 }
1323 break;
1324 case TextureStage::STAGE_ADDSIGNED2X: // (Arg1 + Arg2 - 0.5) << 1
1325 {
John Bauman19bac1e2014-05-06 15:23:49 -04001326 res.w = AddSat(arg1->w, arg2->w);
1327 res.w = SubSat(res.w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
1328 res.w = AddSat(res.w, res.w);
John Bauman89401822014-05-06 15:04:28 -04001329 }
1330 break;
1331 case TextureStage::STAGE_SUBTRACT: // Arg1 - Arg2
1332 {
John Bauman19bac1e2014-05-06 15:23:49 -04001333 res.w = SubSat(arg1->w, arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001334 }
1335 break;
1336 case TextureStage::STAGE_ADDSMOOTH: // Arg1 + Arg2 - Arg1 * Arg2
1337 {
1338 Short4 tmp;
1339
John Bauman19bac1e2014-05-06 15:23:49 -04001340 tmp = MulHigh(arg1->w, arg2->w) << 4; res.w = AddSat(arg1->w, arg2->w); res.w = SubSat(res.w, tmp);
John Bauman89401822014-05-06 15:04:28 -04001341 }
1342 break;
1343 case TextureStage::STAGE_MULTIPLYADD: // Arg3 + Arg1 * Arg2
1344 {
John Bauman19bac1e2014-05-06 15:23:49 -04001345 res.w = MulHigh(arg1->w, arg2->w) << 4; res.w = AddSat(res.w, arg3->w);
John Bauman89401822014-05-06 15:04:28 -04001346 }
1347 break;
1348 case TextureStage::STAGE_LERP: // Arg3 * (Arg1 - Arg2) + Arg2
1349 {
John Bauman19bac1e2014-05-06 15:23:49 -04001350 res.w = SubSat(arg1->w, arg2->w); res.w = MulHigh(res.w, arg3->w) << 4; res.w = AddSat(res.w, arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001351 }
1352 break;
1353 case TextureStage::STAGE_DOT3:
1354 break; // Already computed in color channel
1355 case TextureStage::STAGE_BLENDCURRENTALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1356 {
John Bauman19bac1e2014-05-06 15:23:49 -04001357 res.w = SubSat(arg1->w, arg2->w); res.w = MulHigh(res.w, current.w) << 4; res.w = AddSat(res.w, arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001358 }
1359 break;
1360 case TextureStage::STAGE_BLENDDIFFUSEALPHA: // Arg1 * (Alpha) + Arg2 * (1 - Alpha)
1361 {
John Bauman19bac1e2014-05-06 15:23:49 -04001362 res.w = SubSat(arg1->w, arg2->w); res.w = MulHigh(res.w, r.diffuse.w) << 4; res.w = AddSat(res.w, arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001363 }
1364 break;
1365 case TextureStage::STAGE_BLENDFACTORALPHA:
1366 {
John Bauman19bac1e2014-05-06 15:23:49 -04001367 res.w = SubSat(arg1->w, arg2->w); res.w = MulHigh(res.w, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[3]))) << 4; res.w = AddSat(res.w, arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001368 }
1369 break;
1370 case TextureStage::STAGE_BLENDTEXTUREALPHA: // Arg1 * (Alpha) + Arg2 * (1 - Alpha)
1371 {
John Bauman19bac1e2014-05-06 15:23:49 -04001372 res.w = SubSat(arg1->w, arg2->w); res.w = MulHigh(res.w, texture.w) << 4; res.w = AddSat(res.w, arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001373 }
1374 break;
1375 case TextureStage::STAGE_BLENDTEXTUREALPHAPM: // Arg1 + Arg2 * (1 - Alpha)
1376 {
John Bauman19bac1e2014-05-06 15:23:49 -04001377 res.w = SubSat(Short4(0x1000), texture.w); res.w = MulHigh(res.w, arg2->w) << 4; res.w = AddSat(res.w, arg1->w);
John Bauman89401822014-05-06 15:04:28 -04001378 }
1379 break;
1380 case TextureStage::STAGE_PREMODULATE:
1381 {
John Bauman19bac1e2014-05-06 15:23:49 -04001382 res.w = arg1->w;
John Bauman89401822014-05-06 15:04:28 -04001383 }
1384 break;
1385 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1386 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
1387 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1388 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1389 case TextureStage::STAGE_BUMPENVMAP:
1390 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1391 break; // Invalid alpha operations
1392 default:
1393 ASSERT(false);
1394 }
1395 }
1396
1397 // Clamp result to [0, 1]
1398
1399 switch(textureStage.stageOperation)
1400 {
1401 case TextureStage::STAGE_DISABLE:
1402 case TextureStage::STAGE_SELECTARG1:
1403 case TextureStage::STAGE_SELECTARG2:
1404 case TextureStage::STAGE_SELECTARG3:
1405 case TextureStage::STAGE_MODULATE:
1406 case TextureStage::STAGE_MODULATE2X:
1407 case TextureStage::STAGE_MODULATE4X:
1408 case TextureStage::STAGE_ADD:
1409 case TextureStage::STAGE_MULTIPLYADD:
1410 case TextureStage::STAGE_LERP:
1411 case TextureStage::STAGE_BLENDCURRENTALPHA:
1412 case TextureStage::STAGE_BLENDDIFFUSEALPHA:
1413 case TextureStage::STAGE_BLENDFACTORALPHA:
1414 case TextureStage::STAGE_BLENDTEXTUREALPHA:
1415 case TextureStage::STAGE_BLENDTEXTUREALPHAPM:
1416 case TextureStage::STAGE_DOT3: // Already clamped
1417 case TextureStage::STAGE_PREMODULATE:
1418 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1419 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
1420 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1421 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1422 case TextureStage::STAGE_BUMPENVMAP:
1423 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1424 if(state.textureStage[stage].cantUnderflow)
1425 {
1426 break; // Can't go below zero
1427 }
1428 case TextureStage::STAGE_ADDSIGNED:
1429 case TextureStage::STAGE_ADDSIGNED2X:
1430 case TextureStage::STAGE_SUBTRACT:
1431 case TextureStage::STAGE_ADDSMOOTH:
John Bauman19bac1e2014-05-06 15:23:49 -04001432 res.x = Max(res.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));
1433 res.y = Max(res.y, Short4(0x0000, 0x0000, 0x0000, 0x0000));
1434 res.z = Max(res.z, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman89401822014-05-06 15:04:28 -04001435 break;
1436 default:
1437 ASSERT(false);
1438 }
1439
1440 switch(textureStage.stageOperationAlpha)
1441 {
1442 case TextureStage::STAGE_DISABLE:
1443 case TextureStage::STAGE_SELECTARG1:
1444 case TextureStage::STAGE_SELECTARG2:
1445 case TextureStage::STAGE_SELECTARG3:
1446 case TextureStage::STAGE_MODULATE:
1447 case TextureStage::STAGE_MODULATE2X:
1448 case TextureStage::STAGE_MODULATE4X:
1449 case TextureStage::STAGE_ADD:
1450 case TextureStage::STAGE_MULTIPLYADD:
1451 case TextureStage::STAGE_LERP:
1452 case TextureStage::STAGE_BLENDCURRENTALPHA:
1453 case TextureStage::STAGE_BLENDDIFFUSEALPHA:
1454 case TextureStage::STAGE_BLENDFACTORALPHA:
1455 case TextureStage::STAGE_BLENDTEXTUREALPHA:
1456 case TextureStage::STAGE_BLENDTEXTUREALPHAPM:
1457 case TextureStage::STAGE_DOT3: // Already clamped
1458 case TextureStage::STAGE_PREMODULATE:
1459 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1460 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
1461 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1462 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1463 case TextureStage::STAGE_BUMPENVMAP:
1464 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1465 if(state.textureStage[stage].cantUnderflow)
1466 {
1467 break; // Can't go below zero
1468 }
1469 case TextureStage::STAGE_ADDSIGNED:
1470 case TextureStage::STAGE_ADDSIGNED2X:
1471 case TextureStage::STAGE_SUBTRACT:
1472 case TextureStage::STAGE_ADDSMOOTH:
John Bauman19bac1e2014-05-06 15:23:49 -04001473 res.w = Max(res.w, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman89401822014-05-06 15:04:28 -04001474 break;
1475 default:
1476 ASSERT(false);
1477 }
1478
1479 switch(textureStage.stageOperation)
1480 {
1481 case TextureStage::STAGE_DISABLE:
1482 case TextureStage::STAGE_SELECTARG1:
1483 case TextureStage::STAGE_SELECTARG2:
1484 case TextureStage::STAGE_SELECTARG3:
1485 case TextureStage::STAGE_MODULATE:
1486 case TextureStage::STAGE_SUBTRACT:
1487 case TextureStage::STAGE_ADDSMOOTH:
1488 case TextureStage::STAGE_LERP:
1489 case TextureStage::STAGE_BLENDCURRENTALPHA:
1490 case TextureStage::STAGE_BLENDDIFFUSEALPHA:
1491 case TextureStage::STAGE_BLENDFACTORALPHA:
1492 case TextureStage::STAGE_BLENDTEXTUREALPHA:
1493 case TextureStage::STAGE_DOT3: // Already clamped
1494 case TextureStage::STAGE_PREMODULATE:
1495 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1496 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1497 case TextureStage::STAGE_BUMPENVMAP:
1498 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1499 break; // Can't go above one
1500 case TextureStage::STAGE_MODULATE2X:
1501 case TextureStage::STAGE_MODULATE4X:
1502 case TextureStage::STAGE_ADD:
1503 case TextureStage::STAGE_ADDSIGNED:
1504 case TextureStage::STAGE_ADDSIGNED2X:
1505 case TextureStage::STAGE_MULTIPLYADD:
1506 case TextureStage::STAGE_BLENDTEXTUREALPHAPM:
1507 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1508 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001509 res.x = Min(res.x, Short4(0x1000));
1510 res.y = Min(res.y, Short4(0x1000));
1511 res.z = Min(res.z, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001512 break;
1513 default:
1514 ASSERT(false);
1515 }
1516
1517 switch(textureStage.stageOperationAlpha)
1518 {
1519 case TextureStage::STAGE_DISABLE:
1520 case TextureStage::STAGE_SELECTARG1:
1521 case TextureStage::STAGE_SELECTARG2:
1522 case TextureStage::STAGE_SELECTARG3:
1523 case TextureStage::STAGE_MODULATE:
1524 case TextureStage::STAGE_SUBTRACT:
1525 case TextureStage::STAGE_ADDSMOOTH:
1526 case TextureStage::STAGE_LERP:
1527 case TextureStage::STAGE_BLENDCURRENTALPHA:
1528 case TextureStage::STAGE_BLENDDIFFUSEALPHA:
1529 case TextureStage::STAGE_BLENDFACTORALPHA:
1530 case TextureStage::STAGE_BLENDTEXTUREALPHA:
1531 case TextureStage::STAGE_DOT3: // Already clamped
1532 case TextureStage::STAGE_PREMODULATE:
1533 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1534 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1535 case TextureStage::STAGE_BUMPENVMAP:
1536 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1537 break; // Can't go above one
1538 case TextureStage::STAGE_MODULATE2X:
1539 case TextureStage::STAGE_MODULATE4X:
1540 case TextureStage::STAGE_ADD:
1541 case TextureStage::STAGE_ADDSIGNED:
1542 case TextureStage::STAGE_ADDSIGNED2X:
1543 case TextureStage::STAGE_MULTIPLYADD:
1544 case TextureStage::STAGE_BLENDTEXTUREALPHAPM:
1545 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1546 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001547 res.w = Min(res.w, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001548 break;
1549 default:
1550 ASSERT(false);
1551 }
1552
1553 switch(textureStage.destinationArgument)
1554 {
1555 case TextureStage::DESTINATION_CURRENT:
John Bauman19bac1e2014-05-06 15:23:49 -04001556 current.x = res.x;
1557 current.y = res.y;
1558 current.z = res.z;
1559 current.w = res.w;
John Bauman89401822014-05-06 15:04:28 -04001560 break;
1561 case TextureStage::DESTINATION_TEMP:
John Bauman19bac1e2014-05-06 15:23:49 -04001562 temp.x = res.x;
1563 temp.y = res.y;
1564 temp.z = res.z;
1565 temp.w = res.w;
John Bauman89401822014-05-06 15:04:28 -04001566 break;
1567 default:
1568 ASSERT(false);
1569 }
1570 }
1571
1572 void PixelRoutine::alphaTest(Registers &r, Int &aMask, Short4 &alpha)
1573 {
1574 Short4 cmp;
1575 Short4 equal;
1576
1577 switch(state.alphaCompareMode)
1578 {
1579 case Context::ALPHA_ALWAYS:
1580 aMask = 0xF;
1581 break;
1582 case Context::ALPHA_NEVER:
1583 aMask = 0x0;
1584 break;
1585 case Context::ALPHA_EQUAL:
1586 cmp = CmpEQ(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
1587 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1588 break;
1589 case Context::ALPHA_NOTEQUAL: // a != b ~ !(a == b)
1590 cmp = CmpEQ(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4))) ^ Short4((short)0xFFFF, (short)0xFFFF, (short)0xFFFF, (short)0xFFFF); // FIXME
1591 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1592 break;
1593 case Context::ALPHA_LESS: // a < b ~ b > a
1594 cmp = CmpGT(*Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)), alpha);
1595 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1596 break;
1597 case Context::ALPHA_GREATEREQUAL: // a >= b ~ (a > b) || (a == b) ~ !(b > a) // TODO: Approximate
1598 equal = CmpEQ(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
1599 cmp = CmpGT(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
1600 cmp |= equal;
1601 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1602 break;
1603 case Context::ALPHA_LESSEQUAL: // a <= b ~ !(a > b)
1604 cmp = CmpGT(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4))) ^ Short4((short)0xFFFF, (short)0xFFFF, (short)0xFFFF, (short)0xFFFF); // FIXME
1605 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1606 break;
1607 case Context::ALPHA_GREATER: // a > b
1608 cmp = CmpGT(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
1609 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1610 break;
1611 default:
1612 ASSERT(false);
1613 }
1614 }
1615
1616 void PixelRoutine::alphaToCoverage(Registers &r, Int cMask[4], Float4 &alpha)
1617 {
1618 Int4 coverage0 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c0)));
1619 Int4 coverage1 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c1)));
1620 Int4 coverage2 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c2)));
1621 Int4 coverage3 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c3)));
1622
1623 Int aMask0 = SignMask(coverage0);
1624 Int aMask1 = SignMask(coverage1);
1625 Int aMask2 = SignMask(coverage2);
1626 Int aMask3 = SignMask(coverage3);
1627
1628 cMask[0] &= aMask0;
1629 cMask[1] &= aMask1;
1630 cMask[2] &= aMask2;
1631 cMask[3] &= aMask3;
1632 }
1633
John Bauman19bac1e2014-05-06 15:23:49 -04001634 Bool PixelRoutine::alphaTest(Registers &r, Int cMask[4], Vector4i &current)
John Bauman89401822014-05-06 15:04:28 -04001635 {
1636 if(!state.alphaTestActive())
1637 {
1638 return true;
1639 }
1640
1641 Int aMask;
1642
1643 if(state.transparencyAntialiasing == Context::TRANSPARENCY_NONE)
1644 {
John Bauman19bac1e2014-05-06 15:23:49 -04001645 alphaTest(r, aMask, current.w);
John Bauman89401822014-05-06 15:04:28 -04001646
1647 for(unsigned int q = 0; q < state.multiSample; q++)
1648 {
1649 cMask[q] &= aMask;
1650 }
1651 }
1652 else if(state.transparencyAntialiasing == Context::TRANSPARENCY_ALPHA_TO_COVERAGE)
1653 {
John Bauman19bac1e2014-05-06 15:23:49 -04001654 Float4 alpha = Float4(current.w) * Float4(1.0f / 0x1000);
John Bauman89401822014-05-06 15:04:28 -04001655
1656 alphaToCoverage(r, cMask, alpha);
1657 }
1658 else ASSERT(false);
1659
1660 Int pass = cMask[0];
1661
1662 for(unsigned int q = 1; q < state.multiSample; q++)
1663 {
1664 pass = pass | cMask[q];
1665 }
1666
1667 return pass != 0x0;
1668 }
1669
John Bauman19bac1e2014-05-06 15:23:49 -04001670 Bool PixelRoutine::alphaTest(Registers &r, Int cMask[4], Vector4f &c0)
John Bauman89401822014-05-06 15:04:28 -04001671 {
1672 if(!state.alphaTestActive())
1673 {
1674 return true;
1675 }
1676
1677 Int aMask;
1678
1679 if(state.transparencyAntialiasing == Context::TRANSPARENCY_NONE)
1680 {
John Bauman19bac1e2014-05-06 15:23:49 -04001681 Short4 alpha = RoundShort4(c0.w * Float4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001682
1683 alphaTest(r, aMask, alpha);
1684
1685 for(unsigned int q = 0; q < state.multiSample; q++)
1686 {
1687 cMask[q] &= aMask;
1688 }
1689 }
1690 else if(state.transparencyAntialiasing == Context::TRANSPARENCY_ALPHA_TO_COVERAGE)
1691 {
John Bauman19bac1e2014-05-06 15:23:49 -04001692 alphaToCoverage(r, cMask, c0.w);
John Bauman89401822014-05-06 15:04:28 -04001693 }
1694 else ASSERT(false);
1695
1696 Int pass = cMask[0];
1697
1698 for(unsigned int q = 1; q < state.multiSample; q++)
1699 {
1700 pass = pass | cMask[q];
1701 }
1702
1703 return pass != 0x0;
1704 }
1705
John Bauman19bac1e2014-05-06 15:23:49 -04001706 void PixelRoutine::fogBlend(Registers &r, Vector4i &current, Float4 &f, Float4 &z, Float4 &rhw)
John Bauman89401822014-05-06 15:04:28 -04001707 {
1708 if(!state.fogActive)
1709 {
1710 return;
1711 }
1712
1713 if(state.pixelFogMode != Context::FOG_NONE)
1714 {
1715 pixelFog(r, f, z, rhw);
1716 }
1717
1718 UShort4 fog = convertFixed16(f, true);
1719
John Bauman19bac1e2014-05-06 15:23:49 -04001720 current.x = As<Short4>(MulHigh(As<UShort4>(current.x), fog));
1721 current.y = As<Short4>(MulHigh(As<UShort4>(current.y), fog));
1722 current.z = As<Short4>(MulHigh(As<UShort4>(current.z), fog));
John Bauman89401822014-05-06 15:04:28 -04001723
John Bauman19bac1e2014-05-06 15:23:49 -04001724 UShort4 invFog = UShort4(0xFFFFu) - fog;
John Bauman89401822014-05-06 15:04:28 -04001725
John Bauman19bac1e2014-05-06 15:23:49 -04001726 current.x += As<Short4>(MulHigh(invFog, *Pointer<UShort4>(r.data + OFFSET(DrawData,fog.color4[0]))));
1727 current.y += As<Short4>(MulHigh(invFog, *Pointer<UShort4>(r.data + OFFSET(DrawData,fog.color4[1]))));
1728 current.z += As<Short4>(MulHigh(invFog, *Pointer<UShort4>(r.data + OFFSET(DrawData,fog.color4[2]))));
John Bauman89401822014-05-06 15:04:28 -04001729 }
1730
John Bauman19bac1e2014-05-06 15:23:49 -04001731 void PixelRoutine::fogBlend(Registers &r, Vector4f &c0, Float4 &fog, Float4 &z, Float4 &rhw)
John Bauman89401822014-05-06 15:04:28 -04001732 {
1733 if(!state.fogActive)
1734 {
1735 return;
1736 }
1737
1738 if(state.pixelFogMode != Context::FOG_NONE)
1739 {
1740 pixelFog(r, fog, z, rhw);
1741
John Bauman19bac1e2014-05-06 15:23:49 -04001742 fog = Min(fog, Float4(1.0f));
1743 fog = Max(fog, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -04001744 }
1745
John Bauman19bac1e2014-05-06 15:23:49 -04001746 c0.x -= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[0]));
1747 c0.y -= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[1]));
1748 c0.z -= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[2]));
John Bauman89401822014-05-06 15:04:28 -04001749
John Bauman19bac1e2014-05-06 15:23:49 -04001750 c0.x *= fog;
1751 c0.y *= fog;
1752 c0.z *= fog;
John Bauman89401822014-05-06 15:04:28 -04001753
John Bauman19bac1e2014-05-06 15:23:49 -04001754 c0.x += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[0]));
1755 c0.y += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[1]));
1756 c0.z += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[2]));
John Bauman89401822014-05-06 15:04:28 -04001757 }
1758
1759 void PixelRoutine::pixelFog(Registers &r, Float4 &visibility, Float4 &z, Float4 &rhw)
1760 {
1761 Float4 &zw = visibility;
1762
1763 if(state.pixelFogMode != Context::FOG_NONE)
1764 {
1765 if(state.wBasedFog)
1766 {
1767 zw = rhw;
1768 }
1769 else
1770 {
1771 if(complementaryDepthBuffer)
1772 {
John Bauman19bac1e2014-05-06 15:23:49 -04001773 zw = Float4(1.0f) - z;
John Bauman89401822014-05-06 15:04:28 -04001774 }
1775 else
1776 {
1777 zw = z;
1778 }
1779 }
1780 }
1781
1782 switch(state.pixelFogMode)
1783 {
1784 case Context::FOG_NONE:
1785 break;
1786 case Context::FOG_LINEAR:
1787 zw *= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.scale));
1788 zw += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.offset));
1789 break;
1790 case Context::FOG_EXP:
1791 zw *= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.densityE));
John Bauman19bac1e2014-05-06 15:23:49 -04001792 zw = exponential2(zw, true);
John Bauman89401822014-05-06 15:04:28 -04001793 break;
1794 case Context::FOG_EXP2:
1795 zw *= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.densityE2));
1796 zw *= zw;
John Bauman19bac1e2014-05-06 15:23:49 -04001797 zw = exponential2(zw, true);
John Bauman89401822014-05-06 15:04:28 -04001798 zw = Rcp_pp(zw);
1799 break;
1800 default:
1801 ASSERT(false);
1802 }
1803 }
1804
John Bauman19bac1e2014-05-06 15:23:49 -04001805 void PixelRoutine::specularPixel(Vector4i &current, Vector4i &specular)
John Bauman89401822014-05-06 15:04:28 -04001806 {
1807 if(!state.specularAdd)
1808 {
1809 return;
1810 }
1811
John Bauman19bac1e2014-05-06 15:23:49 -04001812 current.x = AddSat(current.x, specular.x);
1813 current.y = AddSat(current.y, specular.y);
1814 current.z = AddSat(current.z, specular.z);
John Bauman89401822014-05-06 15:04:28 -04001815 }
1816
1817 void PixelRoutine::writeDepth(Registers &r, Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &zMask)
1818 {
1819 if(!state.depthWriteEnable)
1820 {
1821 return;
1822 }
1823
1824 Float4 Z = z;
1825
John Bauman19bac1e2014-05-06 15:23:49 -04001826 if(shader && shader->depthOverride())
John Bauman89401822014-05-06 15:04:28 -04001827 {
1828 if(complementaryDepthBuffer)
1829 {
John Bauman19bac1e2014-05-06 15:23:49 -04001830 Z = Float4(1.0f) - r.oDepth;
John Bauman89401822014-05-06 15:04:28 -04001831 }
1832 else
1833 {
1834 Z = r.oDepth;
1835 }
1836 }
1837
1838 Pointer<Byte> buffer;
1839 Int pitch;
1840
1841 if(!state.quadLayoutDepthBuffer)
1842 {
1843 buffer = zBuffer + 4 * x;
1844 pitch = *Pointer<Int>(r.data + OFFSET(DrawData,depthPitchB));
1845 }
1846 else
1847 {
1848 buffer = zBuffer + 8 * x;
1849 }
1850
1851 if(q > 0)
1852 {
1853 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,depthSliceB));
1854 }
1855
1856 Float4 zValue;
1857
1858 if(state.depthCompareMode != Context::DEPTH_NEVER || (state.depthCompareMode != Context::DEPTH_ALWAYS && !state.depthWriteEnable))
1859 {
1860 if(!state.quadLayoutDepthBuffer)
1861 {
1862 // FIXME: Properly optimizes?
1863 zValue.xy = *Pointer<Float4>(buffer);
1864 zValue.zw = *Pointer<Float4>(buffer + pitch - 8);
1865 }
1866 else
1867 {
1868 zValue = *Pointer<Float4>(buffer, 16);
1869 }
1870 }
1871
1872 Z = As<Float4>(As<Int4>(Z) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X) + zMask * 16, 16));
1873 zValue = As<Float4>(As<Int4>(zValue) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X) + zMask * 16, 16));
1874 Z = As<Float4>(As<Int4>(Z) | As<Int4>(zValue));
1875
1876 if(!state.quadLayoutDepthBuffer)
1877 {
1878 // FIXME: Properly optimizes?
1879 *Pointer<Float2>(buffer) = Float2(Z.xy);
1880 *Pointer<Float2>(buffer + pitch) = Float2(Z.zw);
1881 }
1882 else
1883 {
1884 *Pointer<Float4>(buffer, 16) = Z;
1885 }
1886 }
1887
1888 void PixelRoutine::writeStencil(Registers &r, Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &zMask, Int &cMask)
1889 {
1890 if(!state.stencilActive)
1891 {
1892 return;
1893 }
1894
1895 if(state.stencilPassOperation == Context::OPERATION_KEEP && state.stencilZFailOperation == Context::OPERATION_KEEP && state.stencilFailOperation == Context::OPERATION_KEEP)
1896 {
1897 if(!state.twoSidedStencil || (state.stencilPassOperationCCW == Context::OPERATION_KEEP && state.stencilZFailOperationCCW == Context::OPERATION_KEEP && state.stencilFailOperationCCW == Context::OPERATION_KEEP))
1898 {
1899 return;
1900 }
1901 }
1902
1903 if(state.stencilWriteMasked && (!state.twoSidedStencil || state.stencilWriteMaskedCCW))
1904 {
1905 return;
1906 }
1907
1908 Pointer<Byte> buffer = sBuffer + 2 * x;
1909
1910 if(q > 0)
1911 {
1912 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,stencilSliceB));
1913 }
1914
1915 Byte8 bufferValue = As<Byte8>(Long1(*Pointer<UInt>(buffer)));
1916
1917 Byte8 newValue;
1918 stencilOperation(r, newValue, bufferValue, (Context::StencilOperation)state.stencilPassOperation, (Context::StencilOperation)state.stencilZFailOperation, (Context::StencilOperation)state.stencilFailOperation, false, zMask, sMask);
1919
1920 if(!state.noStencilWriteMask)
1921 {
1922 Byte8 maskedValue = bufferValue;
1923 newValue &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[0].writeMaskQ));
1924 maskedValue &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[0].invWriteMaskQ));
1925 newValue |= maskedValue;
1926 }
1927
1928 if(state.twoSidedStencil)
1929 {
1930 Byte8 newValueCCW;
1931
1932 stencilOperation(r, newValueCCW, bufferValue, (Context::StencilOperation)state.stencilPassOperationCCW, (Context::StencilOperation)state.stencilZFailOperationCCW, (Context::StencilOperation)state.stencilFailOperationCCW, true, zMask, sMask);
1933
1934 if(!state.noStencilWriteMaskCCW)
1935 {
1936 Byte8 maskedValue = bufferValue;
1937 newValueCCW &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[1].writeMaskQ));
1938 maskedValue &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[1].invWriteMaskQ));
1939 newValueCCW |= maskedValue;
1940 }
1941
1942 newValue &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,clockwiseMask));
1943 newValueCCW &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,invClockwiseMask));
1944 newValue |= newValueCCW;
1945 }
1946
1947 newValue &= *Pointer<Byte8>(r.constants + OFFSET(Constants,maskB4Q) + 8 * cMask);
1948 bufferValue &= *Pointer<Byte8>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * cMask);
1949 newValue |= bufferValue;
1950
1951 *Pointer<UInt>(buffer) = UInt(As<Long>(newValue));
1952 }
1953
1954 void PixelRoutine::stencilOperation(Registers &r, Byte8 &newValue, Byte8 &bufferValue, Context::StencilOperation stencilPassOperation, Context::StencilOperation stencilZFailOperation, Context::StencilOperation stencilFailOperation, bool CCW, Int &zMask, Int &sMask)
1955 {
1956 Byte8 &pass = newValue;
1957 Byte8 fail;
1958 Byte8 zFail;
1959
1960 stencilOperation(r, pass, bufferValue, stencilPassOperation, CCW);
1961
1962 if(stencilZFailOperation != stencilPassOperation)
1963 {
1964 stencilOperation(r, zFail, bufferValue, stencilZFailOperation, CCW);
1965 }
1966
1967 if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
1968 {
1969 stencilOperation(r, fail, bufferValue, stencilFailOperation, CCW);
1970 }
1971
1972 if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
1973 {
1974 if(state.depthTestActive && stencilZFailOperation != stencilPassOperation) // zMask valid and values not the same
1975 {
1976 pass &= *Pointer<Byte8>(r.constants + OFFSET(Constants,maskB4Q) + 8 * zMask);
1977 zFail &= *Pointer<Byte8>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * zMask);
1978 pass |= zFail;
1979 }
1980
1981 pass &= *Pointer<Byte8>(r.constants + OFFSET(Constants,maskB4Q) + 8 * sMask);
1982 fail &= *Pointer<Byte8>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * sMask);
1983 pass |= fail;
1984 }
1985 }
1986
1987 void PixelRoutine::stencilOperation(Registers &r, Byte8 &output, Byte8 &bufferValue, Context::StencilOperation operation, bool CCW)
1988 {
1989 switch(operation)
1990 {
1991 case Context::OPERATION_KEEP:
1992 output = bufferValue;
1993 break;
1994 case Context::OPERATION_ZERO:
1995 output = Byte8(0x0000000000000000);
1996 break;
1997 case Context::OPERATION_REPLACE:
1998 output = *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceQ));
1999 break;
2000 case Context::OPERATION_INCRSAT:
2001 output = AddSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));
2002 break;
2003 case Context::OPERATION_DECRSAT:
2004 output = SubSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));
2005 break;
2006 case Context::OPERATION_INVERT:
2007 output = bufferValue ^ Byte8(0xFFFFFFFFFFFFFFFF);
2008 break;
2009 case Context::OPERATION_INCR:
2010 output = bufferValue + Byte8(1, 1, 1, 1, 1, 1, 1, 1);
2011 break;
2012 case Context::OPERATION_DECR:
2013 output = bufferValue - Byte8(1, 1, 1, 1, 1, 1, 1, 1);
2014 break;
2015 default:
2016 ASSERT(false);
2017 }
2018 }
2019
John Bauman19bac1e2014-05-06 15:23:49 -04002020 void PixelRoutine::sampleTexture(Registers &r, Vector4i &c, int coordinates, int stage, bool project)
John Bauman89401822014-05-06 15:04:28 -04002021 {
John Bauman19bac1e2014-05-06 15:23:49 -04002022 Float4 u = r.vf[2 + coordinates].x;
2023 Float4 v = r.vf[2 + coordinates].y;
2024 Float4 w = r.vf[2 + coordinates].z;
2025 Float4 q = r.vf[2 + coordinates].w;
John Bauman89401822014-05-06 15:04:28 -04002026
2027 if(perturbate)
2028 {
2029 u += r.du;
2030 v += r.dv;
2031
2032 perturbate = false;
2033 }
2034
2035 sampleTexture(r, c, stage, u, v, w, q, project);
2036 }
2037
John Bauman19bac1e2014-05-06 15:23:49 -04002038 void PixelRoutine::sampleTexture(Registers &r, Vector4i &c, int stage, Float4 &u, Float4 &v, Float4 &w, Float4 &q, bool project, bool bias, bool fixed12)
John Bauman89401822014-05-06 15:04:28 -04002039 {
John Bauman19bac1e2014-05-06 15:23:49 -04002040 Vector4f dsx;
2041 Vector4f dsy;
John Bauman89401822014-05-06 15:04:28 -04002042
2043 sampleTexture(r, c, stage, u, v, w, q, dsx, dsy, project, bias, fixed12, false);
2044 }
2045
John Bauman19bac1e2014-05-06 15:23:49 -04002046 void PixelRoutine::sampleTexture(Registers &r, Vector4i &c, int stage, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, bool project, bool bias, bool fixed12, bool gradients, bool lodProvided)
John Bauman89401822014-05-06 15:04:28 -04002047 {
2048 #if PERF_PROFILE
2049 Long texTime = Ticks();
2050 #endif
2051
2052 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap) + stage * sizeof(Texture);
2053
2054 if(!project)
2055 {
2056 sampler[stage]->sampleTexture(texture, c, u, v, w, q, dsx, dsy, bias, fixed12, gradients, lodProvided);
2057 }
2058 else
2059 {
2060 Float4 rq = reciprocal(q);
2061
2062 Float4 u_q = u * rq;
2063 Float4 v_q = v * rq;
2064 Float4 w_q = w * rq;
2065
2066 sampler[stage]->sampleTexture(texture, c, u_q, v_q, w_q, q, dsx, dsy, bias, fixed12, gradients, lodProvided);
2067 }
2068
2069 #if PERF_PROFILE
2070 r.cycles[PERF_TEX] += Ticks() - texTime;
2071 #endif
2072 }
2073
John Bauman19bac1e2014-05-06 15:23:49 -04002074 void PixelRoutine::sampleTexture(Registers &r, Vector4f &c, const Src &sampler, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, bool project, bool bias, bool gradients, bool lodProvided)
2075 {
2076 if(sampler.type == Shader::PARAMETER_SAMPLER && sampler.rel.type == Shader::PARAMETER_VOID)
2077 {
2078 sampleTexture(r, c, sampler.index, u, v, w, q, dsx, dsy, project, bias, gradients, lodProvided);
2079 }
2080 else
2081 {
2082 Int index = As<Int>(Float(reg(r, sampler).x.x));
2083
2084 for(int i = 0; i < 16; i++)
2085 {
2086 if(shader->usesSampler(i))
2087 {
2088 If(index == i)
2089 {
2090 sampleTexture(r, c, i, u, v, w, q, dsx, dsy, project, bias, gradients, lodProvided);
2091 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
2092 }
2093 }
2094 }
2095 }
2096 }
2097
2098 void PixelRoutine::sampleTexture(Registers &r, Vector4f &c, int stage, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, bool project, bool bias, bool gradients, bool lodProvided)
John Bauman89401822014-05-06 15:04:28 -04002099 {
2100 #if PERF_PROFILE
2101 Long texTime = Ticks();
2102 #endif
2103
2104 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap) + stage * sizeof(Texture);
2105
2106 if(!project)
2107 {
2108 sampler[stage]->sampleTexture(texture, c, u, v, w, q, dsx, dsy, bias, gradients, lodProvided);
2109 }
2110 else
2111 {
2112 Float4 rq = reciprocal(q);
2113
2114 Float4 u_q = u * rq;
2115 Float4 v_q = v * rq;
2116 Float4 w_q = w * rq;
2117
2118 sampler[stage]->sampleTexture(texture, c, u_q, v_q, w_q, q, dsx, dsy, bias, gradients, lodProvided);
2119 }
2120
2121 #if PERF_PROFILE
2122 r.cycles[PERF_TEX] += Ticks() - texTime;
2123 #endif
2124 }
2125
John Bauman19bac1e2014-05-06 15:23:49 -04002126 void PixelRoutine::clampColor(Vector4f oC[4])
John Bauman89401822014-05-06 15:04:28 -04002127 {
2128 for(int index = 0; index < 4; index++)
2129 {
2130 if(!state.colorWriteActive(index) && !(index == 0 && state.alphaTestActive()))
2131 {
2132 continue;
2133 }
2134
2135 switch(state.targetFormat[index])
2136 {
2137 case FORMAT_NULL:
2138 break;
2139 case FORMAT_A16B16G16R16:
2140 case FORMAT_A8R8G8B8:
2141 case FORMAT_X8R8G8B8:
2142 case FORMAT_G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04002143 oC[index].x = Max(oC[index].x, Float4(0.0f)); oC[index].x = Min(oC[index].x, Float4(1.0f));
2144 oC[index].y = Max(oC[index].y, Float4(0.0f)); oC[index].y = Min(oC[index].y, Float4(1.0f));
2145 oC[index].z = Max(oC[index].z, Float4(0.0f)); oC[index].z = Min(oC[index].z, Float4(1.0f));
2146 oC[index].w = Max(oC[index].w, Float4(0.0f)); oC[index].w = Min(oC[index].w, Float4(1.0f));
John Bauman89401822014-05-06 15:04:28 -04002147 break;
2148 case FORMAT_R32F:
2149 case FORMAT_G32R32F:
2150 case FORMAT_A32B32G32R32F:
2151 break;
2152 default:
2153 ASSERT(false);
2154 }
2155 }
2156 }
2157
John Bauman19bac1e2014-05-06 15:23:49 -04002158 void PixelRoutine::rasterOperation(Vector4i &current, Registers &r, Float4 &fog, Pointer<Byte> &cBuffer, Int &x, Int sMask[4], Int zMask[4], Int cMask[4])
John Bauman89401822014-05-06 15:04:28 -04002159 {
2160 if(!state.colorWriteActive(0))
2161 {
2162 return;
2163 }
2164
John Bauman19bac1e2014-05-06 15:23:49 -04002165 Vector4f oC;
John Bauman89401822014-05-06 15:04:28 -04002166
2167 switch(state.targetFormat[0])
2168 {
2169 case FORMAT_X8R8G8B8:
2170 case FORMAT_A8R8G8B8:
2171 case FORMAT_G16R16:
2172 case FORMAT_A16B16G16R16:
2173 if(!postBlendSRGB && state.writeSRGB)
2174 {
2175 linearToSRGB12_16(r, current);
2176 }
2177 else
2178 {
John Bauman19bac1e2014-05-06 15:23:49 -04002179 current.x <<= 4;
2180 current.y <<= 4;
2181 current.z <<= 4;
2182 current.w <<= 4;
John Bauman89401822014-05-06 15:04:28 -04002183 }
2184
2185 fogBlend(r, current, fog, r.z[0], r.rhw);
2186
2187 for(unsigned int q = 0; q < state.multiSample; q++)
2188 {
2189 Pointer<Byte> buffer = cBuffer + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002190 Vector4i color = current;
John Bauman89401822014-05-06 15:04:28 -04002191
2192 if(state.multiSampleMask & (1 << q))
2193 {
2194 alphaBlend(r, 0, buffer, color, x);
2195 writeColor(r, 0, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2196 }
2197 }
2198 break;
2199 case FORMAT_R32F:
2200 case FORMAT_G32R32F:
2201 case FORMAT_A32B32G32R32F:
2202 convertSigned12(oC, current);
2203 fogBlend(r, oC, fog, r.z[0], r.rhw);
2204
2205 for(unsigned int q = 0; q < state.multiSample; q++)
2206 {
2207 Pointer<Byte> buffer = cBuffer + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002208 Vector4f color = oC;
John Bauman89401822014-05-06 15:04:28 -04002209
2210 if(state.multiSampleMask & (1 << q))
2211 {
2212 alphaBlend(r, 0, buffer, color, x);
2213 writeColor(r, 0, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2214 }
2215 }
2216 break;
2217 default:
2218 ASSERT(false);
2219 }
2220 }
2221
John Bauman19bac1e2014-05-06 15:23:49 -04002222 void PixelRoutine::rasterOperation(Vector4f oC[4], Registers &r, Float4 &fog, Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4])
John Bauman89401822014-05-06 15:04:28 -04002223 {
2224 for(int index = 0; index < 4; index++)
2225 {
2226 if(!state.colorWriteActive(index))
2227 {
2228 continue;
2229 }
2230
2231 if(!postBlendSRGB && state.writeSRGB)
2232 {
John Bauman19bac1e2014-05-06 15:23:49 -04002233 oC[index].x = linearToSRGB(oC[index].x);
2234 oC[index].y = linearToSRGB(oC[index].y);
2235 oC[index].z = linearToSRGB(oC[index].z);
John Bauman89401822014-05-06 15:04:28 -04002236 }
2237
2238 if(index == 0)
2239 {
2240 fogBlend(r, oC[index], fog, r.z[0], r.rhw);
2241 }
2242
2243 switch(state.targetFormat[index])
2244 {
2245 case FORMAT_X8R8G8B8:
2246 case FORMAT_A8R8G8B8:
2247 case FORMAT_G16R16:
2248 case FORMAT_A16B16G16R16:
2249 for(unsigned int q = 0; q < state.multiSample; q++)
2250 {
2251 Pointer<Byte> buffer = cBuffer[index] + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002252 Vector4i color;
John Bauman89401822014-05-06 15:04:28 -04002253
John Bauman19bac1e2014-05-06 15:23:49 -04002254 color.x = convertFixed16(oC[index].x, false);
2255 color.y = convertFixed16(oC[index].y, false);
2256 color.z = convertFixed16(oC[index].z, false);
2257 color.w = convertFixed16(oC[index].w, false);
John Bauman89401822014-05-06 15:04:28 -04002258
2259 if(state.multiSampleMask & (1 << q))
2260 {
2261 alphaBlend(r, index, buffer, color, x);
2262 writeColor(r, index, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2263 }
2264 }
2265 break;
2266 case FORMAT_R32F:
2267 case FORMAT_G32R32F:
2268 case FORMAT_A32B32G32R32F:
2269 for(unsigned int q = 0; q < state.multiSample; q++)
2270 {
2271 Pointer<Byte> buffer = cBuffer[index] + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002272 Vector4f color = oC[index];
John Bauman89401822014-05-06 15:04:28 -04002273
2274 if(state.multiSampleMask & (1 << q))
2275 {
2276 alphaBlend(r, index, buffer, color, x);
2277 writeColor(r, index, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2278 }
2279 }
2280 break;
2281 default:
2282 ASSERT(false);
2283 }
2284 }
2285 }
2286
John Bauman19bac1e2014-05-06 15:23:49 -04002287 void PixelRoutine::blendFactor(Registers &r, const Vector4i &blendFactor, const Vector4i &current, const Vector4i &pixel, Context::BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -04002288 {
2289 switch(blendFactorActive)
2290 {
2291 case Context::BLEND_ZERO:
2292 // Optimized
2293 break;
2294 case Context::BLEND_ONE:
2295 // Optimized
2296 break;
2297 case Context::BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002298 blendFactor.x = current.x;
2299 blendFactor.y = current.y;
2300 blendFactor.z = current.z;
John Bauman89401822014-05-06 15:04:28 -04002301 break;
2302 case Context::BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002303 blendFactor.x = Short4(0xFFFFu) - current.x;
2304 blendFactor.y = Short4(0xFFFFu) - current.y;
2305 blendFactor.z = Short4(0xFFFFu) - current.z;
John Bauman89401822014-05-06 15:04:28 -04002306 break;
2307 case Context::BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002308 blendFactor.x = pixel.x;
2309 blendFactor.y = pixel.y;
2310 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002311 break;
2312 case Context::BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002313 blendFactor.x = Short4(0xFFFFu) - pixel.x;
2314 blendFactor.y = Short4(0xFFFFu) - pixel.y;
2315 blendFactor.z = Short4(0xFFFFu) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002316 break;
2317 case Context::BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002318 blendFactor.x = current.w;
2319 blendFactor.y = current.w;
2320 blendFactor.z = current.w;
John Bauman89401822014-05-06 15:04:28 -04002321 break;
2322 case Context::BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002323 blendFactor.x = Short4(0xFFFFu) - current.w;
2324 blendFactor.y = Short4(0xFFFFu) - current.w;
2325 blendFactor.z = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -04002326 break;
2327 case Context::BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002328 blendFactor.x = pixel.w;
2329 blendFactor.y = pixel.w;
2330 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002331 break;
2332 case Context::BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002333 blendFactor.x = Short4(0xFFFFu) - pixel.w;
2334 blendFactor.y = Short4(0xFFFFu) - pixel.w;
2335 blendFactor.z = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002336 break;
2337 case Context::BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04002338 blendFactor.x = Short4(0xFFFFu) - pixel.w;
2339 blendFactor.x = Min(As<UShort4>(blendFactor.x), As<UShort4>(current.w));
2340 blendFactor.y = blendFactor.x;
2341 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -04002342 break;
2343 case Context::BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04002344 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[0]));
2345 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[1]));
2346 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[2]));
John Bauman89401822014-05-06 15:04:28 -04002347 break;
2348 case Context::BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04002349 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[0]));
2350 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[1]));
2351 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[2]));
John Bauman89401822014-05-06 15:04:28 -04002352 break;
2353 case Context::BLEND_CONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002354 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
2355 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
2356 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04002357 break;
2358 case Context::BLEND_INVCONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002359 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
2360 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
2361 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04002362 break;
2363 default:
2364 ASSERT(false);
2365 }
2366 }
2367
John Bauman19bac1e2014-05-06 15:23:49 -04002368 void PixelRoutine::blendFactorAlpha(Registers &r, const Vector4i &blendFactor, const Vector4i &current, const Vector4i &pixel, Context::BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -04002369 {
2370 switch(blendFactorAlphaActive)
2371 {
2372 case Context::BLEND_ZERO:
2373 // Optimized
2374 break;
2375 case Context::BLEND_ONE:
2376 // Optimized
2377 break;
2378 case Context::BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002379 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -04002380 break;
2381 case Context::BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002382 blendFactor.w = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -04002383 break;
2384 case Context::BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002385 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002386 break;
2387 case Context::BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002388 blendFactor.w = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002389 break;
2390 case Context::BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002391 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -04002392 break;
2393 case Context::BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002394 blendFactor.w = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -04002395 break;
2396 case Context::BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002397 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002398 break;
2399 case Context::BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002400 blendFactor.w = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002401 break;
2402 case Context::BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04002403 blendFactor.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002404 break;
2405 case Context::BLEND_CONSTANT:
2406 case Context::BLEND_CONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002407 blendFactor.w = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04002408 break;
2409 case Context::BLEND_INVCONSTANT:
2410 case Context::BLEND_INVCONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002411 blendFactor.w = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04002412 break;
2413 default:
2414 ASSERT(false);
2415 }
2416 }
2417
John Bauman19bac1e2014-05-06 15:23:49 -04002418 void PixelRoutine::alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4i &current, Int &x)
John Bauman89401822014-05-06 15:04:28 -04002419 {
2420 if(!state.alphaBlendActive)
2421 {
2422 return;
2423 }
2424
2425 Pointer<Byte> buffer;
2426
John Bauman19bac1e2014-05-06 15:23:49 -04002427 Vector4i pixel;
John Bauman89401822014-05-06 15:04:28 -04002428 Short4 c01;
2429 Short4 c23;
2430
2431 // Read pixel
2432 switch(state.targetFormat[index])
2433 {
2434 case FORMAT_A8R8G8B8:
2435 buffer = cBuffer + 4 * x;
2436 c01 = *Pointer<Short4>(buffer);
2437 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2438 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04002439 pixel.z = c01;
2440 pixel.y = c01;
2441 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
2442 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
2443 pixel.x = pixel.z;
2444 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
2445 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
2446 pixel.y = pixel.z;
2447 pixel.w = pixel.x;
2448 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
2449 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
2450 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
2451 pixel.w = UnpackHigh(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002452 break;
2453 case FORMAT_X8R8G8B8:
2454 buffer = cBuffer + 4 * x;
2455 c01 = *Pointer<Short4>(buffer);
2456 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2457 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04002458 pixel.z = c01;
2459 pixel.y = c01;
2460 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
2461 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
2462 pixel.x = pixel.z;
2463 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
2464 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
2465 pixel.y = pixel.z;
2466 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
2467 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
2468 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
2469 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002470 break;
2471 case FORMAT_A8G8R8B8Q:
2472 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002473 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2474 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2475 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
2476 // pixel.w = UnpackHigh(As<Byte8>(pixel.w), *Pointer<Byte8>(cBuffer + 8 * x + 8));
John Bauman89401822014-05-06 15:04:28 -04002477 break;
2478 case FORMAT_X8G8R8B8Q:
2479 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002480 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2481 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2482 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
2483 // pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002484 break;
2485 case FORMAT_A16B16G16R16:
2486 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002487 pixel.x = *Pointer<Short4>(buffer + 8 * x);
2488 pixel.y = *Pointer<Short4>(buffer + 8 * x + 8);
John Bauman89401822014-05-06 15:04:28 -04002489 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002490 pixel.z = *Pointer<Short4>(buffer + 8 * x);
2491 pixel.w = *Pointer<Short4>(buffer + 8 * x + 8);
2492 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04002493 break;
2494 case FORMAT_G16R16:
2495 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002496 pixel.x = *Pointer<Short4>(buffer + 4 * x);
John Bauman89401822014-05-06 15:04:28 -04002497 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002498 pixel.y = *Pointer<Short4>(buffer + 4 * x);
2499 pixel.z = pixel.x;
2500 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.y));
2501 pixel.z = As<Short4>(UnpackHigh(pixel.z, pixel.y));
2502 pixel.y = pixel.z;
2503 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.z));
2504 pixel.y = As<Short4>(UnpackHigh(pixel.y, pixel.z));
2505 pixel.z = Short4(0xFFFFu);
2506 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002507 break;
2508 default:
2509 ASSERT(false);
2510 }
2511
2512 if(postBlendSRGB && state.writeSRGB)
2513 {
2514 sRGBtoLinear16_16(r, pixel);
2515 }
2516
2517 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
John Bauman19bac1e2014-05-06 15:23:49 -04002518 Vector4i sourceFactor;
2519 Vector4i destFactor;
John Bauman89401822014-05-06 15:04:28 -04002520
2521 blendFactor(r, sourceFactor, current, pixel, (Context::BlendFactor)state.sourceBlendFactor);
2522 blendFactor(r, destFactor, current, pixel, (Context::BlendFactor)state.destBlendFactor);
2523
2524 if(state.sourceBlendFactor != Context::BLEND_ONE && state.sourceBlendFactor != Context::BLEND_ZERO)
2525 {
John Bauman19bac1e2014-05-06 15:23:49 -04002526 current.x = MulHigh(As<UShort4>(current.x), As<UShort4>(sourceFactor.x));
2527 current.y = MulHigh(As<UShort4>(current.y), As<UShort4>(sourceFactor.y));
2528 current.z = MulHigh(As<UShort4>(current.z), As<UShort4>(sourceFactor.z));
John Bauman89401822014-05-06 15:04:28 -04002529 }
2530
2531 if(state.destBlendFactor != Context::BLEND_ONE && state.destBlendFactor != Context::BLEND_ZERO)
2532 {
John Bauman19bac1e2014-05-06 15:23:49 -04002533 pixel.x = MulHigh(As<UShort4>(pixel.x), As<UShort4>(destFactor.x));
2534 pixel.y = MulHigh(As<UShort4>(pixel.y), As<UShort4>(destFactor.y));
2535 pixel.z = MulHigh(As<UShort4>(pixel.z), As<UShort4>(destFactor.z));
John Bauman89401822014-05-06 15:04:28 -04002536 }
2537
2538 switch(state.blendOperation)
2539 {
2540 case Context::BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002541 current.x = AddSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
2542 current.y = AddSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
2543 current.z = AddSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002544 break;
2545 case Context::BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002546 current.x = SubSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
2547 current.y = SubSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
2548 current.z = SubSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002549 break;
2550 case Context::BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002551 current.x = SubSat(As<UShort4>(pixel.x), As<UShort4>(current.x));
2552 current.y = SubSat(As<UShort4>(pixel.y), As<UShort4>(current.y));
2553 current.z = SubSat(As<UShort4>(pixel.z), As<UShort4>(current.z));
John Bauman89401822014-05-06 15:04:28 -04002554 break;
2555 case Context::BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002556 current.x = Min(As<UShort4>(current.x), As<UShort4>(pixel.x));
2557 current.y = Min(As<UShort4>(current.y), As<UShort4>(pixel.y));
2558 current.z = Min(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002559 break;
2560 case Context::BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002561 current.x = Max(As<UShort4>(current.x), As<UShort4>(pixel.x));
2562 current.y = Max(As<UShort4>(current.y), As<UShort4>(pixel.y));
2563 current.z = Max(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002564 break;
2565 case Context::BLENDOP_SOURCE:
2566 // No operation
2567 break;
2568 case Context::BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002569 current.x = pixel.x;
2570 current.y = pixel.y;
2571 current.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002572 break;
2573 case Context::BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002574 current.x = Short4(0x0000, 0x0000, 0x0000, 0x0000);
2575 current.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
2576 current.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04002577 break;
2578 default:
2579 ASSERT(false);
2580 }
2581
2582 blendFactorAlpha(r, sourceFactor, current, pixel, (Context::BlendFactor)state.sourceBlendFactorAlpha);
2583 blendFactorAlpha(r, destFactor, current, pixel, (Context::BlendFactor)state.destBlendFactorAlpha);
2584
2585 if(state.sourceBlendFactorAlpha != Context::BLEND_ONE && state.sourceBlendFactorAlpha != Context::BLEND_ZERO)
2586 {
John Bauman19bac1e2014-05-06 15:23:49 -04002587 current.w = MulHigh(As<UShort4>(current.w), As<UShort4>(sourceFactor.w));
John Bauman89401822014-05-06 15:04:28 -04002588 }
2589
2590 if(state.destBlendFactorAlpha != Context::BLEND_ONE && state.destBlendFactorAlpha != Context::BLEND_ZERO)
2591 {
John Bauman19bac1e2014-05-06 15:23:49 -04002592 pixel.w = MulHigh(As<UShort4>(pixel.w), As<UShort4>(destFactor.w));
John Bauman89401822014-05-06 15:04:28 -04002593 }
2594
2595 switch(state.blendOperationAlpha)
2596 {
2597 case Context::BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002598 current.w = AddSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002599 break;
2600 case Context::BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002601 current.w = SubSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002602 break;
2603 case Context::BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002604 current.w = SubSat(As<UShort4>(pixel.w), As<UShort4>(current.w));
John Bauman89401822014-05-06 15:04:28 -04002605 break;
2606 case Context::BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002607 current.w = Min(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002608 break;
2609 case Context::BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002610 current.w = Max(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002611 break;
2612 case Context::BLENDOP_SOURCE:
2613 // No operation
2614 break;
2615 case Context::BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002616 current.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002617 break;
2618 case Context::BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002619 current.w = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04002620 break;
2621 default:
2622 ASSERT(false);
2623 }
2624 }
2625
John Bauman19bac1e2014-05-06 15:23:49 -04002626 void PixelRoutine::writeColor(Registers &r, int index, Pointer<Byte> &cBuffer, Int &x, Vector4i &current, Int &sMask, Int &zMask, Int &cMask)
John Bauman89401822014-05-06 15:04:28 -04002627 {
2628 if(!state.colorWriteActive(index))
2629 {
2630 return;
2631 }
2632
2633 if(postBlendSRGB && state.writeSRGB)
2634 {
2635 linearToSRGB16_16(r, current);
2636 }
2637
2638 if(exactColorRounding)
2639 {
2640 switch(state.targetFormat[index])
2641 {
2642 case FORMAT_X8G8R8B8Q:
2643 case FORMAT_A8G8R8B8Q:
2644 case FORMAT_X8R8G8B8:
2645 case FORMAT_A8R8G8B8:
2646 {
John Bauman19bac1e2014-05-06 15:23:49 -04002647 current.x = current.x - As<Short4>(As<UShort4>(current.x) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
2648 current.y = current.y - As<Short4>(As<UShort4>(current.y) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
2649 current.z = current.z - As<Short4>(As<UShort4>(current.z) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
2650 current.w = current.w - As<Short4>(As<UShort4>(current.w) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
John Bauman89401822014-05-06 15:04:28 -04002651 }
2652 break;
2653 }
2654 }
2655
2656 int rgbaWriteMask = state.colorWriteActive(index);
2657 int bgraWriteMask = rgbaWriteMask & 0x0000000A | (rgbaWriteMask & 0x00000001) << 2 | (rgbaWriteMask & 0x00000004) >> 2;
2658 int brgaWriteMask = rgbaWriteMask & 0x00000008 | (rgbaWriteMask & 0x00000001) << 1 | (rgbaWriteMask & 0x00000002) << 1 | (rgbaWriteMask & 0x00000004) >> 2;
2659
2660 switch(state.targetFormat[index])
2661 {
2662 case FORMAT_X8G8R8B8Q:
2663 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002664 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2665 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2666 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002667
John Bauman19bac1e2014-05-06 15:23:49 -04002668 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2669 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
John Bauman89401822014-05-06 15:04:28 -04002670 break;
2671 case FORMAT_A8G8R8B8Q:
2672 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002673 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2674 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2675 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
2676 // current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002677
John Bauman19bac1e2014-05-06 15:23:49 -04002678 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2679 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04002680 break;
2681 case FORMAT_X8R8G8B8:
2682 case FORMAT_A8R8G8B8:
2683 if(state.targetFormat[index] == FORMAT_X8R8G8B8 || rgbaWriteMask == 0x7)
2684 {
John Bauman19bac1e2014-05-06 15:23:49 -04002685 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2686 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2687 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002688
John Bauman19bac1e2014-05-06 15:23:49 -04002689 current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2690 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
John Bauman89401822014-05-06 15:04:28 -04002691
John Bauman19bac1e2014-05-06 15:23:49 -04002692 current.x = current.z;
2693 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
2694 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
2695 current.y = current.z;
2696 current.z = As<Short4>(UnpackLow(current.z, current.x));
2697 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04002698 }
2699 else
2700 {
John Bauman19bac1e2014-05-06 15:23:49 -04002701 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2702 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2703 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
2704 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002705
John Bauman19bac1e2014-05-06 15:23:49 -04002706 current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2707 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04002708
John Bauman19bac1e2014-05-06 15:23:49 -04002709 current.x = current.z;
2710 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
2711 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
2712 current.y = current.z;
2713 current.z = As<Short4>(UnpackLow(current.z, current.x));
2714 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04002715 }
2716 break;
2717 case FORMAT_G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04002718 current.z = current.x;
2719 current.x = As<Short4>(UnpackLow(current.x, current.y));
2720 current.z = As<Short4>(UnpackHigh(current.z, current.y));
2721 current.y = current.z;
John Bauman89401822014-05-06 15:04:28 -04002722 break;
2723 case FORMAT_A16B16G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04002724 transpose4x4(current.x, current.y, current.z, current.w);
John Bauman89401822014-05-06 15:04:28 -04002725 break;
2726 case FORMAT_R32F:
2727 case FORMAT_G32R32F:
2728 case FORMAT_A32B32G32R32F:
2729 {
John Bauman19bac1e2014-05-06 15:23:49 -04002730 Vector4f oC;
John Bauman89401822014-05-06 15:04:28 -04002731
John Bauman19bac1e2014-05-06 15:23:49 -04002732 oC.x = convertUnsigned16(UShort4(current.x));
2733 oC.y = convertUnsigned16(UShort4(current.y));
2734 oC.z = convertUnsigned16(UShort4(current.z));
2735 oC.w = convertUnsigned16(UShort4(current.w));
John Bauman89401822014-05-06 15:04:28 -04002736
2737 writeColor(r, index, cBuffer, x, oC, sMask, zMask, cMask);
2738 }
2739 return;
2740 default:
2741 ASSERT(false);
2742 }
2743
John Bauman19bac1e2014-05-06 15:23:49 -04002744 Short4 c01 = current.z;
2745 Short4 c23 = current.y;
John Bauman89401822014-05-06 15:04:28 -04002746
2747 Int xMask; // Combination of all masks
2748
2749 if(state.depthTestActive)
2750 {
2751 xMask = zMask;
2752 }
2753 else
2754 {
2755 xMask = cMask;
2756 }
2757
2758 if(state.stencilActive)
2759 {
2760 xMask &= sMask;
2761 }
2762
2763 Pointer<Byte> buffer;
2764 Short4 value;
2765
2766 switch(state.targetFormat[index])
2767 {
2768 case FORMAT_A8G8R8B8Q:
2769 case FORMAT_X8G8R8B8Q: // FIXME: Don't touch alpha?
2770 UNIMPLEMENTED();
2771 // value = *Pointer<Short4>(cBuffer + 8 * x + 0);
2772
2773 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
2774 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
2775 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2776 // {
2777 // Short4 masked = value;
2778 // c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2779 // masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2780 // c01 |= masked;
2781 // }
2782
2783 // c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
2784 // value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
2785 // c01 |= value;
2786 // *Pointer<Short4>(cBuffer + 8 * x + 0) = c01;
2787
2788 // value = *Pointer<Short4>(cBuffer + 8 * x + 8);
2789
2790 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
2791 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
2792 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2793 // {
2794 // Short4 masked = value;
2795 // c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2796 // masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2797 // c23 |= masked;
2798 // }
2799
2800 // c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
2801 // value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
2802 // c23 |= value;
2803 // *Pointer<Short4>(cBuffer + 8 * x + 8) = c23;
2804 break;
2805 case FORMAT_A8R8G8B8:
2806 case FORMAT_X8R8G8B8: // FIXME: Don't touch alpha?
2807 buffer = cBuffer + x * 4;
2808 value = *Pointer<Short4>(buffer);
2809
2810 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
2811 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
2812 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2813 {
2814 Short4 masked = value;
2815 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2816 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2817 c01 |= masked;
2818 }
2819
2820 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
2821 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
2822 c01 |= value;
2823 *Pointer<Short4>(buffer) = c01;
2824
2825 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2826 value = *Pointer<Short4>(buffer);
2827
2828 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
2829 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
2830 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2831 {
2832 Short4 masked = value;
2833 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2834 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2835 c23 |= masked;
2836 }
2837
2838 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
2839 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
2840 c23 |= value;
2841 *Pointer<Short4>(buffer) = c23;
2842 break;
2843 case FORMAT_G16R16:
2844 buffer = cBuffer + 4 * x;
2845
2846 value = *Pointer<Short4>(buffer);
2847
2848 if((rgbaWriteMask & 0x00000003) != 0x00000003)
2849 {
2850 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002851 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman89401822014-05-06 15:04:28 -04002852 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002853 current.x |= masked;
John Bauman89401822014-05-06 15:04:28 -04002854 }
2855
John Bauman19bac1e2014-05-06 15:23:49 -04002856 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04002857 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04002858 current.x |= value;
2859 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04002860
2861 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2862
2863 value = *Pointer<Short4>(buffer);
2864
2865 if((rgbaWriteMask & 0x00000003) != 0x00000003)
2866 {
2867 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002868 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman89401822014-05-06 15:04:28 -04002869 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002870 current.y |= masked;
John Bauman89401822014-05-06 15:04:28 -04002871 }
2872
John Bauman19bac1e2014-05-06 15:23:49 -04002873 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04002874 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04002875 current.y |= value;
2876 *Pointer<Short4>(buffer) = current.y;
John Bauman89401822014-05-06 15:04:28 -04002877 break;
2878 case FORMAT_A16B16G16R16:
2879 buffer = cBuffer + 8 * x;
2880
2881 {
2882 value = *Pointer<Short4>(buffer);
2883
2884 if(rgbaWriteMask != 0x0000000F)
2885 {
2886 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002887 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04002888 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002889 current.x |= masked;
John Bauman89401822014-05-06 15:04:28 -04002890 }
2891
John Bauman19bac1e2014-05-06 15:23:49 -04002892 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ0Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04002893 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ0Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04002894 current.x |= value;
2895 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04002896 }
2897
2898 {
2899 value = *Pointer<Short4>(buffer + 8);
2900
2901 if(rgbaWriteMask != 0x0000000F)
2902 {
2903 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002904 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04002905 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002906 current.y |= masked;
John Bauman89401822014-05-06 15:04:28 -04002907 }
2908
John Bauman19bac1e2014-05-06 15:23:49 -04002909 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ1Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04002910 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ1Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04002911 current.y |= value;
2912 *Pointer<Short4>(buffer + 8) = current.y;
John Bauman89401822014-05-06 15:04:28 -04002913 }
2914
2915 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2916
2917 {
2918 value = *Pointer<Short4>(buffer);
2919
2920 if(rgbaWriteMask != 0x0000000F)
2921 {
2922 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002923 current.z &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04002924 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002925 current.z |= masked;
John Bauman89401822014-05-06 15:04:28 -04002926 }
2927
John Bauman19bac1e2014-05-06 15:23:49 -04002928 current.z &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ2Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04002929 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ2Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04002930 current.z |= value;
2931 *Pointer<Short4>(buffer) = current.z;
John Bauman89401822014-05-06 15:04:28 -04002932 }
2933
2934 {
2935 value = *Pointer<Short4>(buffer + 8);
2936
2937 if(rgbaWriteMask != 0x0000000F)
2938 {
2939 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002940 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04002941 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002942 current.w |= masked;
John Bauman89401822014-05-06 15:04:28 -04002943 }
2944
John Bauman19bac1e2014-05-06 15:23:49 -04002945 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ3Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04002946 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ3Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04002947 current.w |= value;
2948 *Pointer<Short4>(buffer + 8) = current.w;
John Bauman89401822014-05-06 15:04:28 -04002949 }
2950 break;
2951 default:
2952 ASSERT(false);
2953 }
2954 }
2955
John Bauman19bac1e2014-05-06 15:23:49 -04002956 void PixelRoutine::blendFactor(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, Context::BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -04002957 {
2958 switch(blendFactorActive)
2959 {
2960 case Context::BLEND_ZERO:
2961 // Optimized
2962 break;
2963 case Context::BLEND_ONE:
2964 // Optimized
2965 break;
2966 case Context::BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002967 blendFactor.x = oC.x;
2968 blendFactor.y = oC.y;
2969 blendFactor.z = oC.z;
John Bauman89401822014-05-06 15:04:28 -04002970 break;
2971 case Context::BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002972 blendFactor.x = Float4(1.0f) - oC.x;
2973 blendFactor.y = Float4(1.0f) - oC.y;
2974 blendFactor.z = Float4(1.0f) - oC.z;
John Bauman89401822014-05-06 15:04:28 -04002975 break;
2976 case Context::BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002977 blendFactor.x = pixel.x;
2978 blendFactor.y = pixel.y;
2979 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002980 break;
2981 case Context::BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002982 blendFactor.x = Float4(1.0f) - pixel.x;
2983 blendFactor.y = Float4(1.0f) - pixel.y;
2984 blendFactor.z = Float4(1.0f) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002985 break;
2986 case Context::BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002987 blendFactor.x = oC.w;
2988 blendFactor.y = oC.w;
2989 blendFactor.z = oC.w;
John Bauman89401822014-05-06 15:04:28 -04002990 break;
2991 case Context::BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002992 blendFactor.x = Float4(1.0f) - oC.w;
2993 blendFactor.y = Float4(1.0f) - oC.w;
2994 blendFactor.z = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04002995 break;
2996 case Context::BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002997 blendFactor.x = pixel.w;
2998 blendFactor.y = pixel.w;
2999 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003000 break;
3001 case Context::BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003002 blendFactor.x = Float4(1.0f) - pixel.w;
3003 blendFactor.y = Float4(1.0f) - pixel.w;
3004 blendFactor.z = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003005 break;
3006 case Context::BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04003007 blendFactor.x = Float4(1.0f) - pixel.w;
3008 blendFactor.x = Min(blendFactor.x, oC.w);
3009 blendFactor.y = blendFactor.x;
3010 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -04003011 break;
3012 case Context::BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003013 blendFactor.x = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[0]));
3014 blendFactor.y = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[1]));
3015 blendFactor.z = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04003016 break;
3017 case Context::BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003018 blendFactor.x = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[0]));
3019 blendFactor.y = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[1]));
3020 blendFactor.z = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04003021 break;
3022 default:
3023 ASSERT(false);
3024 }
3025 }
3026
John Bauman19bac1e2014-05-06 15:23:49 -04003027 void PixelRoutine::blendFactorAlpha(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, Context::BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -04003028 {
3029 switch(blendFactorAlphaActive)
3030 {
3031 case Context::BLEND_ZERO:
3032 // Optimized
3033 break;
3034 case Context::BLEND_ONE:
3035 // Optimized
3036 break;
3037 case Context::BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003038 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003039 break;
3040 case Context::BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003041 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04003042 break;
3043 case Context::BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003044 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003045 break;
3046 case Context::BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003047 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003048 break;
3049 case Context::BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003050 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003051 break;
3052 case Context::BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003053 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04003054 break;
3055 case Context::BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003056 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003057 break;
3058 case Context::BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003059 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003060 break;
3061 case Context::BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04003062 blendFactor.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003063 break;
3064 case Context::BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003065 blendFactor.w = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04003066 break;
3067 case Context::BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003068 blendFactor.w = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04003069 break;
3070 default:
3071 ASSERT(false);
3072 }
3073 }
3074
John Bauman19bac1e2014-05-06 15:23:49 -04003075 void PixelRoutine::alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4f &oC, Int &x)
John Bauman89401822014-05-06 15:04:28 -04003076 {
3077 if(!state.alphaBlendActive)
3078 {
3079 return;
3080 }
3081
3082 Pointer<Byte> buffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003083 Vector4f pixel;
John Bauman89401822014-05-06 15:04:28 -04003084
John Bauman19bac1e2014-05-06 15:23:49 -04003085 Vector4i color;
John Bauman89401822014-05-06 15:04:28 -04003086 Short4 c01;
3087 Short4 c23;
3088
3089 // Read pixel
3090 switch(state.targetFormat[index])
3091 {
3092 case FORMAT_A8R8G8B8:
3093 buffer = cBuffer + 4 * x;
3094 c01 = *Pointer<Short4>(buffer);
3095 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3096 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04003097 color.z = c01;
3098 color.y = c01;
3099 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(c23));
3100 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(c23));
3101 color.x = color.z;
3102 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.y));
3103 color.x = UnpackHigh(As<Byte8>(color.x), As<Byte8>(color.y));
3104 color.y = color.z;
3105 color.w = color.x;
3106 color.x = UnpackLow(As<Byte8>(color.x), As<Byte8>(color.x));
3107 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(color.y));
3108 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.z));
3109 color.w = UnpackHigh(As<Byte8>(color.w), As<Byte8>(color.w));
John Bauman89401822014-05-06 15:04:28 -04003110
John Bauman19bac1e2014-05-06 15:23:49 -04003111 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3112 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3113 pixel.z = convertUnsigned16(As<UShort4>(color.z));
3114 pixel.w = convertUnsigned16(As<UShort4>(color.w));
John Bauman89401822014-05-06 15:04:28 -04003115 break;
3116 case FORMAT_X8R8G8B8:
3117 buffer = cBuffer + 4 * x;
3118 c01 = *Pointer<Short4>(buffer);
3119 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3120 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04003121 color.z = c01;
3122 color.y = c01;
3123 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(c23));
3124 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(c23));
3125 color.x = color.z;
3126 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.y));
3127 color.x = UnpackHigh(As<Byte8>(color.x), As<Byte8>(color.y));
3128 color.y = color.z;
3129 color.x = UnpackLow(As<Byte8>(color.x), As<Byte8>(color.x));
3130 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(color.y));
3131 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.z));
John Bauman89401822014-05-06 15:04:28 -04003132
John Bauman19bac1e2014-05-06 15:23:49 -04003133 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3134 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3135 pixel.z = convertUnsigned16(As<UShort4>(color.z));
3136 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003137 break;
3138 case FORMAT_A8G8R8B8Q:
3139UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04003140 // UnpackLow(pixel.z, qword_ptr [cBuffer+8*x+0]);
3141 // UnpackHigh(pixel.x, qword_ptr [cBuffer+8*x+0]);
3142 // UnpackLow(pixel.y, qword_ptr [cBuffer+8*x+8]);
3143 // UnpackHigh(pixel.w, qword_ptr [cBuffer+8*x+8]);
John Bauman89401822014-05-06 15:04:28 -04003144 break;
3145 case FORMAT_X8G8R8B8Q:
3146UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04003147 // UnpackLow(pixel.z, qword_ptr [cBuffer+8*x+0]);
3148 // UnpackHigh(pixel.x, qword_ptr [cBuffer+8*x+0]);
3149 // UnpackLow(pixel.y, qword_ptr [cBuffer+8*x+8]);
3150 // pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04003151 break;
3152 case FORMAT_A16B16G16R16:
3153 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003154 color.x = *Pointer<Short4>(buffer + 8 * x);
3155 color.y = *Pointer<Short4>(buffer + 8 * x + 8);
John Bauman89401822014-05-06 15:04:28 -04003156 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003157 color.z = *Pointer<Short4>(buffer + 8 * x);
3158 color.w = *Pointer<Short4>(buffer + 8 * x + 8);
John Bauman89401822014-05-06 15:04:28 -04003159
John Bauman19bac1e2014-05-06 15:23:49 -04003160 transpose4x4(color.x, color.y, color.z, color.w);
John Bauman89401822014-05-06 15:04:28 -04003161
John Bauman19bac1e2014-05-06 15:23:49 -04003162 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3163 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3164 pixel.z = convertUnsigned16(As<UShort4>(color.z));
3165 pixel.w = convertUnsigned16(As<UShort4>(color.w));
John Bauman89401822014-05-06 15:04:28 -04003166 break;
3167 case FORMAT_G16R16:
3168 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003169 color.x = *Pointer<Short4>(buffer + 4 * x);
John Bauman89401822014-05-06 15:04:28 -04003170 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003171 color.y = *Pointer<Short4>(buffer + 4 * x);
3172 color.z = color.x;
3173 color.x = As<Short4>(UnpackLow(color.x, color.y));
3174 color.z = As<Short4>(UnpackHigh(color.z, color.y));
3175 color.y = color.z;
3176 color.x = As<Short4>(UnpackLow(color.x, color.z));
3177 color.y = As<Short4>(UnpackHigh(color.y, color.z));
John Bauman89401822014-05-06 15:04:28 -04003178
John Bauman19bac1e2014-05-06 15:23:49 -04003179 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3180 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3181 pixel.z = Float4(1.0f);
3182 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003183 break;
3184 case FORMAT_R32F:
3185 buffer = cBuffer;
3186 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04003187 pixel.x.x = *Pointer<Float>(buffer + 4 * x + 0);
3188 pixel.x.y = *Pointer<Float>(buffer + 4 * x + 4);
John Bauman89401822014-05-06 15:04:28 -04003189 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3190 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04003191 pixel.x.z = *Pointer<Float>(buffer + 4 * x + 0);
3192 pixel.x.w = *Pointer<Float>(buffer + 4 * x + 4);
3193 pixel.y = Float4(1.0f);
3194 pixel.z = Float4(1.0f);
3195 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003196 break;
3197 case FORMAT_G32R32F:
3198 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003199 pixel.x = *Pointer<Float4>(buffer + 8 * x, 16);
John Bauman89401822014-05-06 15:04:28 -04003200 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003201 pixel.y = *Pointer<Float4>(buffer + 8 * x, 16);
3202 pixel.z = pixel.x;
3203 pixel.x = ShuffleLowHigh(pixel.x, pixel.y, 0x88);
3204 pixel.z = ShuffleLowHigh(pixel.z, pixel.y, 0xDD);
3205 pixel.y = pixel.z;
3206 pixel.z = Float4(1.0f);
3207 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003208 break;
3209 case FORMAT_A32B32G32R32F:
3210 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003211 pixel.x = *Pointer<Float4>(buffer + 16 * x, 16);
3212 pixel.y = *Pointer<Float4>(buffer + 16 * x + 16, 16);
John Bauman89401822014-05-06 15:04:28 -04003213 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003214 pixel.z = *Pointer<Float4>(buffer + 16 * x, 16);
3215 pixel.w = *Pointer<Float4>(buffer + 16 * x + 16, 16);
3216 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04003217 break;
3218 default:
3219 ASSERT(false);
3220 }
3221
3222 if(postBlendSRGB && state.writeSRGB)
3223 {
John Bauman19bac1e2014-05-06 15:23:49 -04003224 sRGBtoLinear(pixel.x);
3225 sRGBtoLinear(pixel.y);
3226 sRGBtoLinear(pixel.z);
John Bauman89401822014-05-06 15:04:28 -04003227 }
3228
3229 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
John Bauman19bac1e2014-05-06 15:23:49 -04003230 Vector4f sourceFactor;
3231 Vector4f destFactor;
John Bauman89401822014-05-06 15:04:28 -04003232
3233 blendFactor(r, sourceFactor, oC, pixel, (Context::BlendFactor)state.sourceBlendFactor);
3234 blendFactor(r, destFactor, oC, pixel, (Context::BlendFactor)state.destBlendFactor);
3235
3236 if(state.sourceBlendFactor != Context::BLEND_ONE && state.sourceBlendFactor != Context::BLEND_ZERO)
3237 {
John Bauman19bac1e2014-05-06 15:23:49 -04003238 oC.x *= sourceFactor.x;
3239 oC.y *= sourceFactor.y;
3240 oC.z *= sourceFactor.z;
John Bauman89401822014-05-06 15:04:28 -04003241 }
3242
3243 if(state.destBlendFactor != Context::BLEND_ONE && state.destBlendFactor != Context::BLEND_ZERO)
3244 {
John Bauman19bac1e2014-05-06 15:23:49 -04003245 pixel.x *= destFactor.x;
3246 pixel.y *= destFactor.y;
3247 pixel.z *= destFactor.z;
John Bauman89401822014-05-06 15:04:28 -04003248 }
3249
3250 switch(state.blendOperation)
3251 {
3252 case Context::BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04003253 oC.x += pixel.x;
3254 oC.y += pixel.y;
3255 oC.z += pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003256 break;
3257 case Context::BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003258 oC.x -= pixel.x;
3259 oC.y -= pixel.y;
3260 oC.z -= pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003261 break;
3262 case Context::BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003263 oC.x = pixel.x - oC.x;
3264 oC.y = pixel.y - oC.y;
3265 oC.z = pixel.z - oC.z;
John Bauman89401822014-05-06 15:04:28 -04003266 break;
3267 case Context::BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04003268 oC.x = Min(oC.x, pixel.x);
3269 oC.y = Min(oC.y, pixel.y);
3270 oC.z = Min(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04003271 break;
3272 case Context::BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04003273 oC.x = Max(oC.x, pixel.x);
3274 oC.y = Max(oC.y, pixel.y);
3275 oC.z = Max(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04003276 break;
3277 case Context::BLENDOP_SOURCE:
3278 // No operation
3279 break;
3280 case Context::BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003281 oC.x = pixel.x;
3282 oC.y = pixel.y;
3283 oC.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003284 break;
3285 case Context::BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04003286 oC.x = Float4(0.0f);
3287 oC.y = Float4(0.0f);
3288 oC.z = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04003289 break;
3290 default:
3291 ASSERT(false);
3292 }
3293
3294 blendFactorAlpha(r, sourceFactor, oC, pixel, (Context::BlendFactor)state.sourceBlendFactorAlpha);
3295 blendFactorAlpha(r, destFactor, oC, pixel, (Context::BlendFactor)state.destBlendFactorAlpha);
3296
3297 if(state.sourceBlendFactorAlpha != Context::BLEND_ONE && state.sourceBlendFactorAlpha != Context::BLEND_ZERO)
3298 {
John Bauman19bac1e2014-05-06 15:23:49 -04003299 oC.w *= sourceFactor.w;
John Bauman89401822014-05-06 15:04:28 -04003300 }
3301
3302 if(state.destBlendFactorAlpha != Context::BLEND_ONE && state.destBlendFactorAlpha != Context::BLEND_ZERO)
3303 {
John Bauman19bac1e2014-05-06 15:23:49 -04003304 pixel.w *= destFactor.w;
John Bauman89401822014-05-06 15:04:28 -04003305 }
3306
3307 switch(state.blendOperationAlpha)
3308 {
3309 case Context::BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04003310 oC.w += pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003311 break;
3312 case Context::BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003313 oC.w -= pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003314 break;
3315 case Context::BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003316 pixel.w -= oC.w;
3317 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003318 break;
3319 case Context::BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04003320 oC.w = Min(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04003321 break;
3322 case Context::BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04003323 oC.w = Max(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04003324 break;
3325 case Context::BLENDOP_SOURCE:
3326 // No operation
3327 break;
3328 case Context::BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003329 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003330 break;
3331 case Context::BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04003332 oC.w = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04003333 break;
3334 default:
3335 ASSERT(false);
3336 }
3337 }
3338
John Bauman19bac1e2014-05-06 15:23:49 -04003339 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 -04003340 {
3341 if(!state.colorWriteActive(index))
3342 {
3343 return;
3344 }
3345
John Bauman19bac1e2014-05-06 15:23:49 -04003346 Vector4i color;
John Bauman89401822014-05-06 15:04:28 -04003347
3348 switch(state.targetFormat[index])
3349 {
3350 case FORMAT_X8R8G8B8:
3351 case FORMAT_A8R8G8B8:
3352 case FORMAT_G16R16:
3353 case FORMAT_A16B16G16R16:
3354 convertFixed16(color, oC, true);
3355 writeColor(r, index, cBuffer, x, color, sMask, zMask, cMask);
3356 return;
3357 case FORMAT_R32F:
3358 break;
3359 case FORMAT_G32R32F:
John Bauman19bac1e2014-05-06 15:23:49 -04003360 oC.z = oC.x;
3361 oC.x = UnpackLow(oC.x, oC.y);
3362 oC.z = UnpackHigh(oC.z, oC.y);
3363 oC.y = oC.z;
John Bauman89401822014-05-06 15:04:28 -04003364 break;
3365 case FORMAT_A32B32G32R32F:
John Bauman19bac1e2014-05-06 15:23:49 -04003366 transpose4x4(oC.x, oC.y, oC.z, oC.w);
John Bauman89401822014-05-06 15:04:28 -04003367 break;
3368 default:
3369 ASSERT(false);
3370 }
3371
3372 int rgbaWriteMask = state.colorWriteActive(index);
3373
3374 Int xMask; // Combination of all masks
3375
3376 if(state.depthTestActive)
3377 {
3378 xMask = zMask;
3379 }
3380 else
3381 {
3382 xMask = cMask;
3383 }
3384
3385 if(state.stencilActive)
3386 {
3387 xMask &= sMask;
3388 }
3389
3390 Pointer<Byte> buffer;
3391 Float4 value;
3392
3393 switch(state.targetFormat[index])
3394 {
3395 case FORMAT_R32F:
3396 if(rgbaWriteMask & 0x00000001)
3397 {
3398 buffer = cBuffer + 4 * x;
3399
3400 // FIXME: movlps
3401 value.x = *Pointer<Float>(buffer + 0);
3402 value.y = *Pointer<Float>(buffer + 4);
3403
3404 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3405
3406 // FIXME: movhps
3407 value.z = *Pointer<Float>(buffer + 0);
3408 value.w = *Pointer<Float>(buffer + 4);
3409
John Bauman19bac1e2014-05-06 15:23:49 -04003410 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 -04003411 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003412 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
John Bauman89401822014-05-06 15:04:28 -04003413
3414 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04003415 *Pointer<Float>(buffer + 0) = oC.x.z;
3416 *Pointer<Float>(buffer + 4) = oC.x.w;
John Bauman89401822014-05-06 15:04:28 -04003417
3418 buffer -= *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3419
3420 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04003421 *Pointer<Float>(buffer + 0) = oC.x.x;
3422 *Pointer<Float>(buffer + 4) = oC.x.y;
John Bauman89401822014-05-06 15:04:28 -04003423 }
3424 break;
3425 case FORMAT_G32R32F:
3426 buffer = cBuffer + 8 * x;
3427
3428 value = *Pointer<Float4>(buffer);
3429
3430 if((rgbaWriteMask & 0x00000003) != 0x00000003)
3431 {
3432 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003433 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 -04003434 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD01X[rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003435 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003436 }
3437
John Bauman19bac1e2014-05-06 15:23:49 -04003438 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 -04003439 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskQ01X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003440 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
3441 *Pointer<Float4>(buffer) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04003442
3443 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3444
3445 value = *Pointer<Float4>(buffer);
3446
3447 if((rgbaWriteMask & 0x00000003) != 0x00000003)
3448 {
3449 Float4 masked;
3450
3451 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003452 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 -04003453 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD01X[rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003454 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003455 }
3456
John Bauman19bac1e2014-05-06 15:23:49 -04003457 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 -04003458 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskQ23X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003459 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
3460 *Pointer<Float4>(buffer) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04003461 break;
3462 case FORMAT_A32B32G32R32F:
3463 buffer = cBuffer + 16 * x;
3464
3465 {
3466 value = *Pointer<Float4>(buffer, 16);
3467
3468 if(rgbaWriteMask != 0x0000000F)
3469 {
3470 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003471 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003472 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003473 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003474 }
3475
John Bauman19bac1e2014-05-06 15:23:49 -04003476 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 -04003477 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX0X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003478 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
3479 *Pointer<Float4>(buffer, 16) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04003480 }
3481
3482 {
3483 value = *Pointer<Float4>(buffer + 16, 16);
3484
3485 if(rgbaWriteMask != 0x0000000F)
3486 {
3487 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003488 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003489 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003490 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003491 }
3492
John Bauman19bac1e2014-05-06 15:23:49 -04003493 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 -04003494 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX1X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003495 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
3496 *Pointer<Float4>(buffer + 16, 16) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04003497 }
3498
3499 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3500
3501 {
3502 value = *Pointer<Float4>(buffer, 16);
3503
3504 if(rgbaWriteMask != 0x0000000F)
3505 {
3506 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003507 oC.z = As<Float4>(As<Int4>(oC.z) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003508 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003509 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003510 }
3511
John Bauman19bac1e2014-05-06 15:23:49 -04003512 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 -04003513 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX2X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003514 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(value));
3515 *Pointer<Float4>(buffer, 16) = oC.z;
John Bauman89401822014-05-06 15:04:28 -04003516 }
3517
3518 {
3519 value = *Pointer<Float4>(buffer + 16, 16);
3520
3521 if(rgbaWriteMask != 0x0000000F)
3522 {
3523 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003524 oC.w = As<Float4>(As<Int4>(oC.w) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003525 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003526 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003527 }
3528
John Bauman19bac1e2014-05-06 15:23:49 -04003529 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 -04003530 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX3X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003531 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(value));
3532 *Pointer<Float4>(buffer + 16, 16) = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003533 }
3534 break;
3535 default:
3536 ASSERT(false);
3537 }
3538 }
3539
3540 void PixelRoutine::ps_1_x(Registers &r, Int cMask[4])
3541 {
3542 int pad = 0; // Count number of texm3x3pad instructions
John Bauman19bac1e2014-05-06 15:23:49 -04003543 Vector4i dPairing; // Destination for first pairing instruction
John Bauman89401822014-05-06 15:04:28 -04003544
John Bauman19bac1e2014-05-06 15:23:49 -04003545 for(int i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -04003546 {
John Bauman19bac1e2014-05-06 15:23:49 -04003547 const Shader::Instruction *instruction = shader->getInstruction(i);
3548 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -04003549
3550 // #ifndef NDEBUG // FIXME: Centralize debug output control
John Bauman19bac1e2014-05-06 15:23:49 -04003551 // shader->printInstruction(i, "debug.txt");
John Bauman89401822014-05-06 15:04:28 -04003552 // #endif
3553
John Bauman19bac1e2014-05-06 15:23:49 -04003554 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -04003555 {
3556 continue;
3557 }
3558
John Bauman19bac1e2014-05-06 15:23:49 -04003559 const Dst &dst = instruction->dst;
3560 const Src &src0 = instruction->src[0];
3561 const Src &src1 = instruction->src[1];
3562 const Src &src2 = instruction->src[2];
John Bauman89401822014-05-06 15:04:28 -04003563
John Bauman19bac1e2014-05-06 15:23:49 -04003564 unsigned short version = shader->getVersion();
3565 bool pairing = i + 1 < shader->getLength() && shader->getInstruction(i + 1)->coissue; // First instruction of pair
3566 bool coissue = instruction->coissue; // Second instruction of pair
John Bauman89401822014-05-06 15:04:28 -04003567
John Bauman19bac1e2014-05-06 15:23:49 -04003568 Vector4i d;
3569 Vector4i s0;
3570 Vector4i s1;
3571 Vector4i s2;
John Bauman89401822014-05-06 15:04:28 -04003572
John Bauman19bac1e2014-05-06 15:23:49 -04003573 if(src0.type != Shader::PARAMETER_VOID) s0 = regi(r, src0);
3574 if(src1.type != Shader::PARAMETER_VOID) s1 = regi(r, src1);
3575 if(src2.type != Shader::PARAMETER_VOID) s2 = regi(r, src2);
3576
3577 Float4 u = version < 0x0104 ? r.vf[2 + dst.index].x : r.vf[2 + src0.index].x;
3578 Float4 v = version < 0x0104 ? r.vf[2 + dst.index].y : r.vf[2 + src0.index].y;
3579 Float4 s = version < 0x0104 ? r.vf[2 + dst.index].z : r.vf[2 + src0.index].z;
3580 Float4 t = version < 0x0104 ? r.vf[2 + dst.index].w : r.vf[2 + src0.index].w;
John Bauman89401822014-05-06 15:04:28 -04003581
3582 switch(opcode)
3583 {
John Bauman19bac1e2014-05-06 15:23:49 -04003584 case Shader::OPCODE_PS_1_0: break;
3585 case Shader::OPCODE_PS_1_1: break;
3586 case Shader::OPCODE_PS_1_2: break;
3587 case Shader::OPCODE_PS_1_3: break;
3588 case Shader::OPCODE_PS_1_4: break;
John Bauman89401822014-05-06 15:04:28 -04003589
John Bauman19bac1e2014-05-06 15:23:49 -04003590 case Shader::OPCODE_DEF: break;
John Bauman89401822014-05-06 15:04:28 -04003591
John Bauman19bac1e2014-05-06 15:23:49 -04003592 case Shader::OPCODE_NOP: break;
3593 case Shader::OPCODE_MOV: MOV(d, s0); break;
3594 case Shader::OPCODE_ADD: ADD(d, s0, s1); break;
3595 case Shader::OPCODE_SUB: SUB(d, s0, s1); break;
3596 case Shader::OPCODE_MAD: MAD(d, s0, s1, s2); break;
3597 case Shader::OPCODE_MUL: MUL(d, s0, s1); break;
3598 case Shader::OPCODE_DP3: DP3(d, s0, s1); break;
3599 case Shader::OPCODE_DP4: DP4(d, s0, s1); break;
3600 case Shader::OPCODE_LRP: LRP(d, s0, s1, s2); break;
3601 case Shader::OPCODE_TEXCOORD:
3602 if(version < 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003603 {
John Bauman19bac1e2014-05-06 15:23:49 -04003604 TEXCOORD(d, u, v, s, dst.index);
John Bauman89401822014-05-06 15:04:28 -04003605 }
3606 else
3607 {
3608 if((src0.swizzle & 0x30) == 0x20) // .xyz
3609 {
John Bauman19bac1e2014-05-06 15:23:49 -04003610 TEXCRD(d, u, v, s, src0.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003611 }
3612 else // .xyw
3613 {
John Bauman19bac1e2014-05-06 15:23:49 -04003614 TEXCRD(d, u, v, t, src0.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003615 }
3616 }
3617 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003618 case Shader::OPCODE_TEXKILL:
3619 if(version < 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003620 {
John Bauman19bac1e2014-05-06 15:23:49 -04003621 TEXKILL(cMask, u, v, s);
John Bauman89401822014-05-06 15:04:28 -04003622 }
John Bauman19bac1e2014-05-06 15:23:49 -04003623 else if(version == 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003624 {
John Bauman19bac1e2014-05-06 15:23:49 -04003625 if(dst.type == Shader::PARAMETER_TEXTURE)
John Bauman89401822014-05-06 15:04:28 -04003626 {
John Bauman19bac1e2014-05-06 15:23:49 -04003627 TEXKILL(cMask, u, v, s);
John Bauman89401822014-05-06 15:04:28 -04003628 }
3629 else
3630 {
3631 TEXKILL(cMask, r.ri[dst.index]);
3632 }
3633 }
3634 else ASSERT(false);
3635 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003636 case Shader::OPCODE_TEX:
3637 if(version < 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003638 {
John Bauman19bac1e2014-05-06 15:23:49 -04003639 TEX(r, d, u, v, s, dst.index, false);
John Bauman89401822014-05-06 15:04:28 -04003640 }
John Bauman19bac1e2014-05-06 15:23:49 -04003641 else if(version == 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003642 {
John Bauman19bac1e2014-05-06 15:23:49 -04003643 if(src0.type == Shader::PARAMETER_TEXTURE)
John Bauman89401822014-05-06 15:04:28 -04003644 {
3645 if((src0.swizzle & 0x30) == 0x20) // .xyz
3646 {
John Bauman19bac1e2014-05-06 15:23:49 -04003647 TEX(r, d, u, v, s, dst.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003648 }
3649 else // .xyw
3650 {
John Bauman19bac1e2014-05-06 15:23:49 -04003651 TEX(r, d, u, v, t, dst.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003652 }
3653 }
3654 else
3655 {
John Bauman19bac1e2014-05-06 15:23:49 -04003656 TEXLD(r, d, s0, dst.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003657 }
3658 }
3659 else ASSERT(false);
3660 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003661 case Shader::OPCODE_TEXBEM: TEXBEM(r, d, s0, u, v, s, dst.index); break;
3662 case Shader::OPCODE_TEXBEML: TEXBEML(r, d, s0, u, v, s, dst.index); break;
3663 case Shader::OPCODE_TEXREG2AR: TEXREG2AR(r, d, s0, dst.index); break;
3664 case Shader::OPCODE_TEXREG2GB: TEXREG2GB(r, d, s0, dst.index); break;
3665 case Shader::OPCODE_TEXM3X2PAD: TEXM3X2PAD(r, u, v, s, s0, 0, src0.modifier == Shader::MODIFIER_SIGN); break;
3666 case Shader::OPCODE_TEXM3X2TEX: TEXM3X2TEX(r, d, u, v, s, dst.index, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3667 case Shader::OPCODE_TEXM3X3PAD: TEXM3X3PAD(r, u, v, s, s0, pad++ % 2, src0.modifier == Shader::MODIFIER_SIGN); break;
3668 case Shader::OPCODE_TEXM3X3TEX: TEXM3X3TEX(r, d, u, v, s, dst.index, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3669 case Shader::OPCODE_TEXM3X3SPEC: TEXM3X3SPEC(r, d, u, v, s, dst.index, s0, s1); break;
3670 case Shader::OPCODE_TEXM3X3VSPEC: TEXM3X3VSPEC(r, d, u, v, s, dst.index, s0); break;
3671 case Shader::OPCODE_CND: CND(d, s0, s1, s2); break;
3672 case Shader::OPCODE_TEXREG2RGB: TEXREG2RGB(r, d, s0, dst.index); break;
3673 case Shader::OPCODE_TEXDP3TEX: TEXDP3TEX(r, d, u, v, s, dst.index, s0); break;
3674 case Shader::OPCODE_TEXM3X2DEPTH: TEXM3X2DEPTH(r, d, u, v, s, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3675 case Shader::OPCODE_TEXDP3: TEXDP3(r, d, u, v, s, s0); break;
3676 case Shader::OPCODE_TEXM3X3: TEXM3X3(r, d, u, v, s, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3677 case Shader::OPCODE_TEXDEPTH: TEXDEPTH(r); break;
3678 case Shader::OPCODE_CMP0: CMP(d, s0, s1, s2); break;
3679 case Shader::OPCODE_BEM: BEM(r, d, s0, s1, dst.index); break;
3680 case Shader::OPCODE_PHASE: break;
3681 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -04003682 default:
3683 ASSERT(false);
3684 }
3685
John Bauman19bac1e2014-05-06 15:23:49 -04003686 if(dst.type != Shader::PARAMETER_VOID && opcode != Shader::OPCODE_TEXKILL)
John Bauman89401822014-05-06 15:04:28 -04003687 {
3688 if(dst.shift > 0)
3689 {
John Bauman19bac1e2014-05-06 15:23:49 -04003690 if(dst.mask & 0x1) {d.x = AddSat(d.x, d.x); if(dst.shift > 1) d.x = AddSat(d.x, d.x); if(dst.shift > 2) d.x = AddSat(d.x, d.x);}
3691 if(dst.mask & 0x2) {d.y = AddSat(d.y, d.y); if(dst.shift > 1) d.y = AddSat(d.y, d.y); if(dst.shift > 2) d.y = AddSat(d.y, d.y);}
3692 if(dst.mask & 0x4) {d.z = AddSat(d.z, d.z); if(dst.shift > 1) d.z = AddSat(d.z, d.z); if(dst.shift > 2) d.z = AddSat(d.z, d.z);}
3693 if(dst.mask & 0x8) {d.w = AddSat(d.w, d.w); if(dst.shift > 1) d.w = AddSat(d.w, d.w); if(dst.shift > 2) d.w = AddSat(d.w, d.w);}
John Bauman89401822014-05-06 15:04:28 -04003694 }
3695 else if(dst.shift < 0)
3696 {
John Bauman19bac1e2014-05-06 15:23:49 -04003697 if(dst.mask & 0x1) d.x = d.x >> -dst.shift;
3698 if(dst.mask & 0x2) d.y = d.y >> -dst.shift;
3699 if(dst.mask & 0x4) d.z = d.z >> -dst.shift;
3700 if(dst.mask & 0x8) d.w = d.w >> -dst.shift;
John Bauman89401822014-05-06 15:04:28 -04003701 }
3702
3703 if(dst.saturate)
3704 {
John Bauman19bac1e2014-05-06 15:23:49 -04003705 if(dst.mask & 0x1) {d.x = Min(d.x, Short4(0x1000)); d.x = Max(d.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
3706 if(dst.mask & 0x2) {d.y = Min(d.y, Short4(0x1000)); d.y = Max(d.y, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
3707 if(dst.mask & 0x4) {d.z = Min(d.z, Short4(0x1000)); d.z = Max(d.z, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
3708 if(dst.mask & 0x8) {d.w = Min(d.w, Short4(0x1000)); d.w = Max(d.w, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
John Bauman89401822014-05-06 15:04:28 -04003709 }
3710
3711 if(pairing)
3712 {
John Bauman19bac1e2014-05-06 15:23:49 -04003713 if(dst.mask & 0x1) dPairing.x = d.x;
3714 if(dst.mask & 0x2) dPairing.y = d.y;
3715 if(dst.mask & 0x4) dPairing.z = d.z;
3716 if(dst.mask & 0x8) dPairing.w = d.w;
John Bauman89401822014-05-06 15:04:28 -04003717 }
3718
3719 if(coissue)
3720 {
John Bauman19bac1e2014-05-06 15:23:49 -04003721 const Dst &dst = shader->getInstruction(i - 1)->dst;
John Bauman89401822014-05-06 15:04:28 -04003722
3723 writeDestination(r, dPairing, dst);
3724 }
3725
3726 if(!pairing)
3727 {
3728 writeDestination(r, d, dst);
3729 }
3730 }
3731 }
3732 }
3733
3734 void PixelRoutine::ps_2_x(Registers &r, Int cMask[4])
3735 {
3736 r.enableIndex = 0;
3737 r.stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -04003738
3739 bool out[4][4] = {false};
3740
3741 // Create all call site return blocks up front
3742 for(int i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -04003743 {
John Bauman19bac1e2014-05-06 15:23:49 -04003744 const Shader::Instruction *instruction = shader->getInstruction(i);
3745 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -04003746
John Bauman19bac1e2014-05-06 15:23:49 -04003747 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
3748 {
3749 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -04003750
John Bauman19bac1e2014-05-06 15:23:49 -04003751 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
3752 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
3753 }
3754 }
3755
3756 for(int i = 0; i < shader->getLength(); i++)
3757 {
3758 const Shader::Instruction *instruction = shader->getInstruction(i);
3759 Shader::Opcode opcode = instruction->opcode;
3760
3761 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -04003762 {
3763 continue;
3764 }
3765
John Bauman19bac1e2014-05-06 15:23:49 -04003766 const Dst &dst = instruction->dst;
3767 const Src &src0 = instruction->src[0];
3768 const Src &src1 = instruction->src[1];
3769 const Src &src2 = instruction->src[2];
3770 const Src &src3 = instruction->src[3];
John Bauman89401822014-05-06 15:04:28 -04003771
John Bauman19bac1e2014-05-06 15:23:49 -04003772 bool predicate = instruction->predicate;
3773 Control control = instruction->control;
John Bauman89401822014-05-06 15:04:28 -04003774 bool pp = dst.partialPrecision;
John Bauman19bac1e2014-05-06 15:23:49 -04003775 bool project = instruction->project;
3776 bool bias = instruction->bias;
John Bauman89401822014-05-06 15:04:28 -04003777
John Bauman19bac1e2014-05-06 15:23:49 -04003778 Vector4f d;
3779 Vector4f s0;
3780 Vector4f s1;
3781 Vector4f s2;
3782 Vector4f s3;
John Bauman89401822014-05-06 15:04:28 -04003783
John Bauman19bac1e2014-05-06 15:23:49 -04003784 if(opcode == Shader::OPCODE_TEXKILL) // Takes destination as input
John Bauman89401822014-05-06 15:04:28 -04003785 {
John Bauman19bac1e2014-05-06 15:23:49 -04003786 if(dst.type == Shader::PARAMETER_TEXTURE)
John Bauman89401822014-05-06 15:04:28 -04003787 {
John Bauman19bac1e2014-05-06 15:23:49 -04003788 d.x = r.vf[2 + dst.index].x;
3789 d.y = r.vf[2 + dst.index].y;
3790 d.z = r.vf[2 + dst.index].z;
3791 d.w = r.vf[2 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -04003792 }
3793 else
3794 {
3795 d = r.rf[dst.index];
3796 }
3797 }
3798
John Bauman19bac1e2014-05-06 15:23:49 -04003799 if(src0.type != Shader::PARAMETER_VOID) s0 = reg(r, src0);
3800 if(src1.type != Shader::PARAMETER_VOID) s1 = reg(r, src1);
3801 if(src2.type != Shader::PARAMETER_VOID) s2 = reg(r, src2);
3802 if(src3.type != Shader::PARAMETER_VOID) s3 = reg(r, src3);
John Bauman89401822014-05-06 15:04:28 -04003803
3804 switch(opcode)
3805 {
John Bauman19bac1e2014-05-06 15:23:49 -04003806 case Shader::OPCODE_PS_2_0: break;
3807 case Shader::OPCODE_PS_2_x: break;
3808 case Shader::OPCODE_PS_3_0: break;
3809 case Shader::OPCODE_DEF: break;
3810 case Shader::OPCODE_DCL: break;
3811 case Shader::OPCODE_NOP: break;
3812 case Shader::OPCODE_MOV: mov(d, s0); break;
3813 case Shader::OPCODE_F2B: f2b(d, s0); break;
3814 case Shader::OPCODE_B2F: b2f(d, s0); break;
3815 case Shader::OPCODE_ADD: add(d, s0, s1); break;
3816 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
3817 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
3818 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
3819 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
3820 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
3821 case Shader::OPCODE_DP2ADD: dp2add(d, s0, s1, s2); break;
3822 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
3823 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
3824 case Shader::OPCODE_CMP0: cmp0(d, s0, s1, s2); break;
3825 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
3826 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
3827 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
3828 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
3829 case Shader::OPCODE_FRC: frc(d, s0); break;
3830 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
3831 case Shader::OPCODE_FLOOR: floor(d, s0); break;
3832 case Shader::OPCODE_CEIL: ceil(d, s0); break;
3833 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
3834 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
3835 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
3836 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
3837 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
3838 case Shader::OPCODE_LOG: log(d, s0, pp); break;
3839 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
3840 case Shader::OPCODE_DIV: div(d, s0, s1); break;
3841 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
3842 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
3843 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
3844 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
3845 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
3846 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
3847 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
3848 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
3849 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
3850 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
3851 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
3852 case Shader::OPCODE_MIN: min(d, s0, s1); break;
3853 case Shader::OPCODE_MAX: max(d, s0, s1); break;
3854 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
3855 case Shader::OPCODE_STEP: step(d, s0, s1); break;
3856 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
3857 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
3858 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
3859 case Shader::OPCODE_SGN: sgn(d, s0); break;
3860 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
3861 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
3862 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
3863 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
3864 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
3865 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
3866 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
3867 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
3868 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
3869 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
3870 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
3871 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
3872 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
3873 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
3874 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
3875 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
3876 case Shader::OPCODE_ABS: abs(d, s0); break;
3877 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
3878 case Shader::OPCODE_COS: cos(d, s0, pp); break;
3879 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
3880 case Shader::OPCODE_TAN: tan(d, s0, pp); break;
3881 case Shader::OPCODE_ACOS: acos(d, s0, pp); break;
3882 case Shader::OPCODE_ASIN: asin(d, s0, pp); break;
3883 case Shader::OPCODE_ATAN: atan(d, s0, pp); break;
3884 case Shader::OPCODE_ATAN2: atan2(d, s0, s1, pp); break;
3885 case Shader::OPCODE_M4X4: M4X4(r, d, s0, src1); break;
3886 case Shader::OPCODE_M4X3: M4X3(r, d, s0, src1); break;
3887 case Shader::OPCODE_M3X4: M3X4(r, d, s0, src1); break;
3888 case Shader::OPCODE_M3X3: M3X3(r, d, s0, src1); break;
3889 case Shader::OPCODE_M3X2: M3X2(r, d, s0, src1); break;
3890 case Shader::OPCODE_TEX: TEXLD(r, d, s0, src1, project, bias); break;
3891 case Shader::OPCODE_TEXLDD: TEXLDD(r, d, s0, src1, s2, s3, project, bias); break;
3892 case Shader::OPCODE_TEXLDL: TEXLDL(r, d, s0, src1, project, bias); break;
3893 case Shader::OPCODE_TEXKILL: TEXKILL(cMask, d, dst.mask); break;
3894 case Shader::OPCODE_DISCARD: DISCARD(r, cMask, instruction); break;
3895 case Shader::OPCODE_DFDX: DFDX(d, s0); break;
3896 case Shader::OPCODE_DFDY: DFDY(d, s0); break;
3897 case Shader::OPCODE_FWIDTH: FWIDTH(d, s0); break;
3898 case Shader::OPCODE_BREAK: BREAK(r); break;
3899 case Shader::OPCODE_BREAKC: BREAKC(r, s0, s1, control); break;
3900 case Shader::OPCODE_BREAKP: BREAKP(r, src0); break;
3901 case Shader::OPCODE_CONTINUE: CONTINUE(r); break;
3902 case Shader::OPCODE_TEST: TEST(); break;
3903 case Shader::OPCODE_CALL: CALL(r, dst.label, dst.callSite); break;
3904 case Shader::OPCODE_CALLNZ: CALLNZ(r, dst.label, dst.callSite, src0); break;
3905 case Shader::OPCODE_ELSE: ELSE(r); break;
3906 case Shader::OPCODE_ENDIF: ENDIF(r); break;
3907 case Shader::OPCODE_ENDLOOP: ENDLOOP(r); break;
3908 case Shader::OPCODE_ENDREP: ENDREP(r); break;
3909 case Shader::OPCODE_ENDWHILE: ENDWHILE(r); break;
3910 case Shader::OPCODE_IF: IF(r, src0); break;
3911 case Shader::OPCODE_IFC: IFC(r, s0, s1, control); break;
3912 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
3913 case Shader::OPCODE_LOOP: LOOP(r, src1); break;
3914 case Shader::OPCODE_REP: REP(r, src0); break;
3915 case Shader::OPCODE_WHILE: WHILE(r, src0); break;
3916 case Shader::OPCODE_RET: RET(r); break;
3917 case Shader::OPCODE_LEAVE: LEAVE(r); break;
3918 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
3919 case Shader::OPCODE_ALL: all(d.x, s0); break;
3920 case Shader::OPCODE_ANY: any(d.x, s0); break;
3921 case Shader::OPCODE_NOT: not(d, s0); break;
3922 case Shader::OPCODE_OR: or(d.x, s0.x, s1.x); break;
3923 case Shader::OPCODE_XOR: xor(d.x, s0.x, s1.x); break;
3924 case Shader::OPCODE_AND: and(d.x, s0.x, s1.x); break;
3925 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -04003926 default:
3927 ASSERT(false);
3928 }
3929
John Bauman19bac1e2014-05-06 15:23:49 -04003930 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_TEXKILL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -04003931 {
John Bauman19bac1e2014-05-06 15:23:49 -04003932 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -04003933 {
John Bauman19bac1e2014-05-06 15:23:49 -04003934 switch(opcode)
3935 {
3936 case Shader::OPCODE_DIV:
3937 if(dst.x) d.x = Trunc(d.x);
3938 if(dst.y) d.y = Trunc(d.y);
3939 if(dst.z) d.z = Trunc(d.z);
3940 if(dst.w) d.w = Trunc(d.w);
3941 break;
3942 default:
3943 break; // No truncation to integer required when arguments are integer
3944 }
John Bauman89401822014-05-06 15:04:28 -04003945 }
3946
John Bauman19bac1e2014-05-06 15:23:49 -04003947 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -04003948 {
John Bauman19bac1e2014-05-06 15:23:49 -04003949 if(dst.x) d.x = Max(d.x, Float4(0.0f));
3950 if(dst.y) d.y = Max(d.y, Float4(0.0f));
3951 if(dst.z) d.z = Max(d.z, Float4(0.0f));
3952 if(dst.w) d.w = Max(d.w, Float4(0.0f));
3953
3954 if(dst.x) d.x = Min(d.x, Float4(1.0f));
3955 if(dst.y) d.y = Min(d.y, Float4(1.0f));
3956 if(dst.z) d.z = Min(d.z, Float4(1.0f));
3957 if(dst.w) d.w = Min(d.w, Float4(1.0f));
3958 }
3959
3960 if(shader->containsDynamicBranching())
3961 {
3962 Vector4f pDst; // FIXME: Rename
John Bauman89401822014-05-06 15:04:28 -04003963
3964 switch(dst.type)
3965 {
John Bauman19bac1e2014-05-06 15:23:49 -04003966 case Shader::PARAMETER_TEMP:
3967 if(dst.rel.type == Shader::PARAMETER_VOID)
3968 {
3969 if(dst.x) pDst.x = r.rf[dst.index].x;
3970 if(dst.y) pDst.y = r.rf[dst.index].y;
3971 if(dst.z) pDst.z = r.rf[dst.index].z;
3972 if(dst.w) pDst.w = r.rf[dst.index].w;
3973 }
3974 else
3975 {
3976 Int a = relativeAddress(r, dst);
3977
3978 if(dst.x) pDst.x = r.rf[dst.index + a].x;
3979 if(dst.y) pDst.y = r.rf[dst.index + a].y;
3980 if(dst.z) pDst.z = r.rf[dst.index + a].z;
3981 if(dst.w) pDst.w = r.rf[dst.index + a].w;
3982 }
John Bauman89401822014-05-06 15:04:28 -04003983 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003984 case Shader::PARAMETER_COLOROUT:
3985 ASSERT(dst.rel.type == Shader::PARAMETER_VOID);
John Bauman89401822014-05-06 15:04:28 -04003986 if(dst.x) pDst.x = r.oC[dst.index].x;
3987 if(dst.y) pDst.y = r.oC[dst.index].y;
3988 if(dst.z) pDst.z = r.oC[dst.index].z;
3989 if(dst.w) pDst.w = r.oC[dst.index].w;
3990 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003991 case Shader::PARAMETER_PREDICATE:
John Bauman89401822014-05-06 15:04:28 -04003992 if(dst.x) pDst.x = r.p0.x;
3993 if(dst.y) pDst.y = r.p0.y;
3994 if(dst.z) pDst.z = r.p0.z;
3995 if(dst.w) pDst.w = r.p0.w;
3996 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003997 case Shader::PARAMETER_DEPTHOUT:
John Bauman89401822014-05-06 15:04:28 -04003998 pDst.x = r.oDepth;
3999 break;
4000 default:
4001 ASSERT(false);
4002 }
4003
John Bauman19bac1e2014-05-06 15:23:49 -04004004 Int4 enable = enableMask(r, instruction);
John Bauman89401822014-05-06 15:04:28 -04004005
4006 Int4 xEnable = enable;
4007 Int4 yEnable = enable;
4008 Int4 zEnable = enable;
4009 Int4 wEnable = enable;
4010
4011 if(predicate)
4012 {
John Bauman19bac1e2014-05-06 15:23:49 -04004013 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -04004014
4015 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
4016 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
4017 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
4018 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
4019
John Bauman19bac1e2014-05-06 15:23:49 -04004020 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -04004021 {
4022 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
4023 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
4024 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
4025 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
4026 }
4027 else
4028 {
4029 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
4030 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
4031 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
4032 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
4033 }
4034 }
4035
4036 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
4037 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
4038 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
4039 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
4040
4041 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
4042 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
4043 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
4044 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
4045 }
4046
4047 switch(dst.type)
4048 {
John Bauman19bac1e2014-05-06 15:23:49 -04004049 case Shader::PARAMETER_TEMP:
4050 if(dst.rel.type == Shader::PARAMETER_VOID)
4051 {
4052 if(dst.x) r.rf[dst.index].x = d.x;
4053 if(dst.y) r.rf[dst.index].y = d.y;
4054 if(dst.z) r.rf[dst.index].z = d.z;
4055 if(dst.w) r.rf[dst.index].w = d.w;
4056 }
4057 else
4058 {
4059 Int a = relativeAddress(r, dst);
4060
4061 if(dst.x) r.rf[dst.index + a].x = d.x;
4062 if(dst.y) r.rf[dst.index + a].y = d.y;
4063 if(dst.z) r.rf[dst.index + a].z = d.z;
4064 if(dst.w) r.rf[dst.index + a].w = d.w;
4065 }
John Bauman89401822014-05-06 15:04:28 -04004066 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004067 case Shader::PARAMETER_COLOROUT:
4068 ASSERT(dst.rel.type == Shader::PARAMETER_VOID);
4069 if(dst.x) {r.oC[dst.index].x = d.x; out[dst.index][0] = true;}
4070 if(dst.y) {r.oC[dst.index].y = d.y; out[dst.index][1] = true;}
4071 if(dst.z) {r.oC[dst.index].z = d.z; out[dst.index][2] = true;}
4072 if(dst.w) {r.oC[dst.index].w = d.w; out[dst.index][3] = true;}
John Bauman89401822014-05-06 15:04:28 -04004073 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004074 case Shader::PARAMETER_PREDICATE:
John Bauman89401822014-05-06 15:04:28 -04004075 if(dst.x) r.p0.x = d.x;
4076 if(dst.y) r.p0.y = d.y;
4077 if(dst.z) r.p0.z = d.z;
4078 if(dst.w) r.p0.w = d.w;
4079 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004080 case Shader::PARAMETER_DEPTHOUT:
John Bauman89401822014-05-06 15:04:28 -04004081 r.oDepth = d.x;
4082 break;
4083 default:
4084 ASSERT(false);
4085 }
4086 }
4087 }
4088
John Bauman19bac1e2014-05-06 15:23:49 -04004089 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -04004090 {
4091 Nucleus::setInsertBlock(returnBlock);
4092 }
John Bauman19bac1e2014-05-06 15:23:49 -04004093
4094 for(int i = 0; i < 4; i++)
4095 {
4096 if((Format)state.targetFormat[i] != FORMAT_NULL)
4097 {
4098 if(!out[i][0]) r.oC[i].x = Float4(0.0f);
4099 if(!out[i][1]) r.oC[i].y = Float4(0.0f);
4100 if(!out[i][2]) r.oC[i].z = Float4(0.0f);
4101 if(!out[i][3]) r.oC[i].w = Float4(0.0f);
4102 }
4103 }
John Bauman89401822014-05-06 15:04:28 -04004104 }
4105
John Bauman19bac1e2014-05-06 15:23:49 -04004106 Short4 PixelRoutine::convertFixed12(RValue<Float4> cf)
John Bauman89401822014-05-06 15:04:28 -04004107 {
John Bauman19bac1e2014-05-06 15:23:49 -04004108 return RoundShort4(cf * Float4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04004109 }
4110
John Bauman19bac1e2014-05-06 15:23:49 -04004111 void PixelRoutine::convertFixed12(Vector4i &ci, Vector4f &cf)
John Bauman89401822014-05-06 15:04:28 -04004112 {
John Bauman19bac1e2014-05-06 15:23:49 -04004113 ci.x = convertFixed12(cf.x);
4114 ci.y = convertFixed12(cf.y);
4115 ci.z = convertFixed12(cf.z);
4116 ci.w = convertFixed12(cf.w);
John Bauman89401822014-05-06 15:04:28 -04004117 }
4118
4119 UShort4 PixelRoutine::convertFixed16(Float4 &cf, bool saturate)
4120 {
John Bauman19bac1e2014-05-06 15:23:49 -04004121 return UShort4(cf * Float4(0xFFFF), saturate);
John Bauman89401822014-05-06 15:04:28 -04004122 }
4123
John Bauman19bac1e2014-05-06 15:23:49 -04004124 void PixelRoutine::convertFixed16(Vector4i &ci, Vector4f &cf, bool saturate)
John Bauman89401822014-05-06 15:04:28 -04004125 {
John Bauman19bac1e2014-05-06 15:23:49 -04004126 ci.x = convertFixed16(cf.x, saturate);
4127 ci.y = convertFixed16(cf.y, saturate);
4128 ci.z = convertFixed16(cf.z, saturate);
4129 ci.w = convertFixed16(cf.w, saturate);
John Bauman89401822014-05-06 15:04:28 -04004130 }
4131
4132 Float4 PixelRoutine::convertSigned12(Short4 &ci)
4133 {
4134 return Float4(ci) * Float4(1.0f / 0x0FFE);
4135 }
4136
John Bauman19bac1e2014-05-06 15:23:49 -04004137 void PixelRoutine::convertSigned12(Vector4f &cf, Vector4i &ci)
John Bauman89401822014-05-06 15:04:28 -04004138 {
John Bauman19bac1e2014-05-06 15:23:49 -04004139 cf.x = convertSigned12(ci.x);
4140 cf.y = convertSigned12(ci.y);
4141 cf.z = convertSigned12(ci.z);
4142 cf.w = convertSigned12(ci.w);
John Bauman89401822014-05-06 15:04:28 -04004143 }
4144
4145 Float4 PixelRoutine::convertUnsigned16(UShort4 ci)
4146 {
John Bauman19bac1e2014-05-06 15:23:49 -04004147 return Float4(ci) * Float4(1.0f / 0xFFFF);
John Bauman89401822014-05-06 15:04:28 -04004148 }
4149
John Bauman19bac1e2014-05-06 15:23:49 -04004150 void PixelRoutine::sRGBtoLinear16_16(Registers &r, Vector4i &c)
John Bauman89401822014-05-06 15:04:28 -04004151 {
John Bauman19bac1e2014-05-06 15:23:49 -04004152 c.x = As<UShort4>(c.x) >> 4;
4153 c.y = As<UShort4>(c.y) >> 4;
4154 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04004155
4156 sRGBtoLinear12_16(r, c);
4157 }
4158
John Bauman19bac1e2014-05-06 15:23:49 -04004159 void PixelRoutine::sRGBtoLinear12_16(Registers &r, Vector4i &c)
John Bauman89401822014-05-06 15:04:28 -04004160 {
4161 Pointer<Byte> LUT = r.constants + OFFSET(Constants,sRGBtoLin12_16);
4162
John Bauman19bac1e2014-05-06 15:23:49 -04004163 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
4164 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
4165 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
4166 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004167
John Bauman19bac1e2014-05-06 15:23:49 -04004168 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
4169 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
4170 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
4171 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004172
John Bauman19bac1e2014-05-06 15:23:49 -04004173 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
4174 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
4175 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
4176 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004177 }
4178
John Bauman19bac1e2014-05-06 15:23:49 -04004179 void PixelRoutine::linearToSRGB16_16(Registers &r, Vector4i &c)
John Bauman89401822014-05-06 15:04:28 -04004180 {
John Bauman19bac1e2014-05-06 15:23:49 -04004181 c.x = As<UShort4>(c.x) >> 4;
4182 c.y = As<UShort4>(c.y) >> 4;
4183 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04004184
4185 linearToSRGB12_16(r, c);
4186 }
4187
John Bauman19bac1e2014-05-06 15:23:49 -04004188 void PixelRoutine::linearToSRGB12_16(Registers &r, Vector4i &c)
John Bauman89401822014-05-06 15:04:28 -04004189 {
4190 Pointer<Byte> LUT = r.constants + OFFSET(Constants,linToSRGB12_16);
4191
John Bauman19bac1e2014-05-06 15:23:49 -04004192 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
4193 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
4194 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
4195 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004196
John Bauman19bac1e2014-05-06 15:23:49 -04004197 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
4198 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
4199 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
4200 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004201
John Bauman19bac1e2014-05-06 15:23:49 -04004202 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
4203 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
4204 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
4205 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004206 }
4207
4208 Float4 PixelRoutine::linearToSRGB(const Float4 &x) // Approximates x^(1.0/2.2)
4209 {
4210 Float4 sqrtx = Rcp_pp(RcpSqrt_pp(x));
4211 Float4 sRGB = sqrtx * Float4(1.14f) - x * Float4(0.14f);
4212
4213 return Min(Max(sRGB, Float4(0.0f)), Float4(1.0f));
4214 }
4215
4216 Float4 PixelRoutine::sRGBtoLinear(const Float4 &x) // Approximates x^2.2
4217 {
4218 Float4 linear = x * x;
4219 linear = linear * Float4(0.73f) + linear * x * Float4(0.27f);
4220
4221 return Min(Max(linear, Float4(0.0f)), Float4(1.0f));
4222 }
4223
John Bauman19bac1e2014-05-06 15:23:49 -04004224 void PixelRoutine::MOV(Vector4i &dst, Vector4i &src0)
John Bauman89401822014-05-06 15:04:28 -04004225 {
John Bauman19bac1e2014-05-06 15:23:49 -04004226 dst.x = src0.x;
4227 dst.y = src0.y;
4228 dst.z = src0.z;
4229 dst.w = src0.w;
John Bauman89401822014-05-06 15:04:28 -04004230 }
4231
John Bauman19bac1e2014-05-06 15:23:49 -04004232 void PixelRoutine::ADD(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004233 {
John Bauman19bac1e2014-05-06 15:23:49 -04004234 dst.x = AddSat(src0.x, src1.x);
4235 dst.y = AddSat(src0.y, src1.y);
4236 dst.z = AddSat(src0.z, src1.z);
4237 dst.w = AddSat(src0.w, src1.w);
John Bauman89401822014-05-06 15:04:28 -04004238 }
4239
John Bauman19bac1e2014-05-06 15:23:49 -04004240 void PixelRoutine::SUB(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004241 {
John Bauman19bac1e2014-05-06 15:23:49 -04004242 dst.x = SubSat(src0.x, src1.x);
4243 dst.y = SubSat(src0.y, src1.y);
4244 dst.z = SubSat(src0.z, src1.z);
4245 dst.w = SubSat(src0.w, src1.w);
John Bauman89401822014-05-06 15:04:28 -04004246 }
4247
John Bauman19bac1e2014-05-06 15:23:49 -04004248 void PixelRoutine::MAD(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2)
John Bauman89401822014-05-06 15:04:28 -04004249 {
4250 // FIXME: Long fixed-point multiply fixup
4251 {dst.x = MulHigh(src0.x, src1.x); dst.x = AddSat(dst.x, dst.x); dst.x = AddSat(dst.x, dst.x); dst.x = AddSat(dst.x, dst.x); dst.x = AddSat(dst.x, dst.x); dst.x = AddSat(dst.x, src2.x);}
4252 {dst.y = MulHigh(src0.y, src1.y); dst.y = AddSat(dst.y, dst.y); dst.y = AddSat(dst.y, dst.y); dst.y = AddSat(dst.y, dst.y); dst.y = AddSat(dst.y, dst.y); dst.y = AddSat(dst.y, src2.y);}
4253 {dst.z = MulHigh(src0.z, src1.z); dst.z = AddSat(dst.z, dst.z); dst.z = AddSat(dst.z, dst.z); dst.z = AddSat(dst.z, dst.z); dst.z = AddSat(dst.z, dst.z); dst.z = AddSat(dst.z, src2.z);}
4254 {dst.w = MulHigh(src0.w, src1.w); dst.w = AddSat(dst.w, dst.w); dst.w = AddSat(dst.w, dst.w); dst.w = AddSat(dst.w, dst.w); dst.w = AddSat(dst.w, dst.w); dst.w = AddSat(dst.w, src2.w);}
4255 }
4256
John Bauman19bac1e2014-05-06 15:23:49 -04004257 void PixelRoutine::MUL(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004258 {
4259 // FIXME: Long fixed-point multiply fixup
4260 {dst.x = MulHigh(src0.x, src1.x); dst.x = AddSat(dst.x, dst.x); dst.x = AddSat(dst.x, dst.x); dst.x = AddSat(dst.x, dst.x); dst.x = AddSat(dst.x, dst.x);}
4261 {dst.y = MulHigh(src0.y, src1.y); dst.y = AddSat(dst.y, dst.y); dst.y = AddSat(dst.y, dst.y); dst.y = AddSat(dst.y, dst.y); dst.y = AddSat(dst.y, dst.y);}
4262 {dst.z = MulHigh(src0.z, src1.z); dst.z = AddSat(dst.z, dst.z); dst.z = AddSat(dst.z, dst.z); dst.z = AddSat(dst.z, dst.z); dst.z = AddSat(dst.z, dst.z);}
4263 {dst.w = MulHigh(src0.w, src1.w); dst.w = AddSat(dst.w, dst.w); dst.w = AddSat(dst.w, dst.w); dst.w = AddSat(dst.w, dst.w); dst.w = AddSat(dst.w, dst.w);}
4264 }
4265
John Bauman19bac1e2014-05-06 15:23:49 -04004266 void PixelRoutine::DP3(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004267 {
4268 Short4 t0;
4269 Short4 t1;
4270
4271 // FIXME: Long fixed-point multiply fixup
4272 t0 = MulHigh(src0.x, src1.x); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0);
4273 t1 = MulHigh(src0.y, src1.y); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4274 t0 = AddSat(t0, t1);
4275 t1 = MulHigh(src0.z, src1.z); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4276 t0 = AddSat(t0, t1);
4277
John Bauman19bac1e2014-05-06 15:23:49 -04004278 dst.x = t0;
4279 dst.y = t0;
4280 dst.z = t0;
4281 dst.w = t0;
John Bauman89401822014-05-06 15:04:28 -04004282 }
4283
John Bauman19bac1e2014-05-06 15:23:49 -04004284 void PixelRoutine::DP4(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004285 {
4286 Short4 t0;
4287 Short4 t1;
4288
4289 // FIXME: Long fixed-point multiply fixup
4290 t0 = MulHigh(src0.x, src1.x); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0);
4291 t1 = MulHigh(src0.y, src1.y); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4292 t0 = AddSat(t0, t1);
4293 t1 = MulHigh(src0.z, src1.z); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4294 t0 = AddSat(t0, t1);
4295 t1 = MulHigh(src0.w, src1.w); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4296 t0 = AddSat(t0, t1);
4297
John Bauman19bac1e2014-05-06 15:23:49 -04004298 dst.x = t0;
4299 dst.y = t0;
4300 dst.z = t0;
4301 dst.w = t0;
John Bauman89401822014-05-06 15:04:28 -04004302 }
4303
John Bauman19bac1e2014-05-06 15:23:49 -04004304 void PixelRoutine::LRP(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2)
John Bauman89401822014-05-06 15:04:28 -04004305 {
4306 // FIXME: Long fixed-point multiply fixup
4307 {dst.x = SubSat(src1.x, src2.x); dst.x = MulHigh(dst.x, src0.x); dst.x = AddSat(dst.x, dst.x); dst.x = AddSat(dst.x, dst.x); dst.x = AddSat(dst.x, dst.x); dst.x = AddSat(dst.x, dst.x); dst.x = AddSat(dst.x, src2.x);}
4308 {dst.y = SubSat(src1.y, src2.y); dst.y = MulHigh(dst.y, src0.y); dst.y = AddSat(dst.y, dst.y); dst.y = AddSat(dst.y, dst.y); dst.y = AddSat(dst.y, dst.y); dst.y = AddSat(dst.y, dst.y); dst.y = AddSat(dst.y, src2.y);}
4309 {dst.z = SubSat(src1.z, src2.z); dst.z = MulHigh(dst.z, src0.z); dst.z = AddSat(dst.z, dst.z); dst.z = AddSat(dst.z, dst.z); dst.z = AddSat(dst.z, dst.z); dst.z = AddSat(dst.z, dst.z); dst.z = AddSat(dst.z, src2.z);}
4310 {dst.w = SubSat(src1.w, src2.w); dst.w = MulHigh(dst.w, src0.w); dst.w = AddSat(dst.w, dst.w); dst.w = AddSat(dst.w, dst.w); dst.w = AddSat(dst.w, dst.w); dst.w = AddSat(dst.w, dst.w); dst.w = AddSat(dst.w, src2.w);}
4311 }
4312
John Bauman19bac1e2014-05-06 15:23:49 -04004313 void PixelRoutine::TEXCOORD(Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int coordinate)
John Bauman89401822014-05-06 15:04:28 -04004314 {
4315 Float4 uw;
4316 Float4 vw;
4317 Float4 sw;
4318
4319 if(state.interpolant[2 + coordinate].component & 0x01)
4320 {
John Bauman19bac1e2014-05-06 15:23:49 -04004321 uw = Max(u, Float4(0.0f));
4322 uw = Min(uw, Float4(1.0f));
4323 dst.x = convertFixed12(uw);
John Bauman89401822014-05-06 15:04:28 -04004324 }
4325 else
4326 {
John Bauman19bac1e2014-05-06 15:23:49 -04004327 dst.x = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004328 }
4329
4330 if(state.interpolant[2 + coordinate].component & 0x02)
4331 {
John Bauman19bac1e2014-05-06 15:23:49 -04004332 vw = Max(v, Float4(0.0f));
4333 vw = Min(vw, Float4(1.0f));
4334 dst.y = convertFixed12(vw);
John Bauman89401822014-05-06 15:04:28 -04004335 }
4336 else
4337 {
John Bauman19bac1e2014-05-06 15:23:49 -04004338 dst.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004339 }
4340
4341 if(state.interpolant[2 + coordinate].component & 0x04)
4342 {
John Bauman19bac1e2014-05-06 15:23:49 -04004343 sw = Max(s, Float4(0.0f));
4344 sw = Min(sw, Float4(1.0f));
4345 dst.z = convertFixed12(sw);
John Bauman89401822014-05-06 15:04:28 -04004346 }
4347 else
4348 {
John Bauman19bac1e2014-05-06 15:23:49 -04004349 dst.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004350 }
4351
John Bauman19bac1e2014-05-06 15:23:49 -04004352 dst.w = Short4(0x1000);
John Bauman89401822014-05-06 15:04:28 -04004353 }
4354
John Bauman19bac1e2014-05-06 15:23:49 -04004355 void PixelRoutine::TEXCRD(Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int coordinate, bool project)
John Bauman89401822014-05-06 15:04:28 -04004356 {
4357 Float4 uw = u;
4358 Float4 vw = v;
4359 Float4 sw = s;
4360
4361 if(project)
4362 {
4363 uw *= Rcp_pp(s);
4364 vw *= Rcp_pp(s);
4365 }
4366
4367 if(state.interpolant[2 + coordinate].component & 0x01)
4368 {
John Bauman19bac1e2014-05-06 15:23:49 -04004369 uw *= Float4(0x1000);
4370 uw = Max(uw, Float4(-0x8000));
4371 uw = Min(uw, Float4(0x7FFF));
4372 dst.x = RoundShort4(uw);
John Bauman89401822014-05-06 15:04:28 -04004373 }
4374 else
4375 {
John Bauman19bac1e2014-05-06 15:23:49 -04004376 dst.x = Short4(0x0000);
John Bauman89401822014-05-06 15:04:28 -04004377 }
4378
4379 if(state.interpolant[2 + coordinate].component & 0x02)
4380 {
John Bauman19bac1e2014-05-06 15:23:49 -04004381 vw *= Float4(0x1000);
4382 vw = Max(vw, Float4(-0x8000));
4383 vw = Min(vw, Float4(0x7FFF));
4384 dst.y = RoundShort4(vw);
John Bauman89401822014-05-06 15:04:28 -04004385 }
4386 else
4387 {
John Bauman19bac1e2014-05-06 15:23:49 -04004388 dst.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004389 }
4390
4391 if(state.interpolant[2 + coordinate].component & 0x04)
4392 {
John Bauman19bac1e2014-05-06 15:23:49 -04004393 sw *= Float4(0x1000);
4394 sw = Max(sw, Float4(-0x8000));
4395 sw = Min(sw, Float4(0x7FFF));
4396 dst.z = RoundShort4(sw);
John Bauman89401822014-05-06 15:04:28 -04004397 }
4398 else
4399 {
John Bauman19bac1e2014-05-06 15:23:49 -04004400 dst.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004401 }
4402 }
4403
John Bauman19bac1e2014-05-06 15:23:49 -04004404 void PixelRoutine::TEXDP3(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src)
John Bauman89401822014-05-06 15:04:28 -04004405 {
4406 TEXM3X3PAD(r, u, v, s, src, 0, false);
4407
John Bauman19bac1e2014-05-06 15:23:49 -04004408 Short4 t0 = RoundShort4(r.u_ * Float4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04004409
John Bauman19bac1e2014-05-06 15:23:49 -04004410 dst.x = t0;
4411 dst.y = t0;
4412 dst.z = t0;
4413 dst.w = t0;
John Bauman89401822014-05-06 15:04:28 -04004414 }
4415
John Bauman19bac1e2014-05-06 15:23:49 -04004416 void PixelRoutine::TEXDP3TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0)
John Bauman89401822014-05-06 15:04:28 -04004417 {
4418 TEXM3X3PAD(r, u, v, s, src0, 0, false);
4419
John Bauman19bac1e2014-05-06 15:23:49 -04004420 r.v_ = Float4(0.0f);
4421 r.w_ = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04004422
4423 sampleTexture(r, dst, stage, r.u_, r.v_, r.w_, r.w_);
4424 }
4425
4426 void PixelRoutine::TEXKILL(Int cMask[4], Float4 &u, Float4 &v, Float4 &s)
4427 {
John Bauman19bac1e2014-05-06 15:23:49 -04004428 Int kill = SignMask(CmpNLT(u, Float4(0.0f))) &
4429 SignMask(CmpNLT(v, Float4(0.0f))) &
4430 SignMask(CmpNLT(s, Float4(0.0f)));
John Bauman89401822014-05-06 15:04:28 -04004431
4432 for(unsigned int q = 0; q < state.multiSample; q++)
4433 {
4434 cMask[q] &= kill;
4435 }
4436 }
4437
John Bauman19bac1e2014-05-06 15:23:49 -04004438 void PixelRoutine::TEXKILL(Int cMask[4], Vector4i &src)
John Bauman89401822014-05-06 15:04:28 -04004439 {
John Bauman19bac1e2014-05-06 15:23:49 -04004440 Short4 test = src.x | src.y | src.z;
John Bauman89401822014-05-06 15:04:28 -04004441 Int kill = SignMask(Pack(test, test)) ^ 0x0000000F;
4442
4443 for(unsigned int q = 0; q < state.multiSample; q++)
4444 {
4445 cMask[q] &= kill;
4446 }
4447 }
4448
John Bauman19bac1e2014-05-06 15:23:49 -04004449 void PixelRoutine::TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int sampler, bool project)
John Bauman89401822014-05-06 15:04:28 -04004450 {
4451 sampleTexture(r, dst, sampler, u, v, s, s, project);
4452 }
4453
John Bauman19bac1e2014-05-06 15:23:49 -04004454 void PixelRoutine::TEXLD(Registers &r, Vector4i &dst, Vector4i &src, int sampler, bool project)
John Bauman89401822014-05-06 15:04:28 -04004455 {
John Bauman19bac1e2014-05-06 15:23:49 -04004456 Float4 u = Float4(src.x) * Float4(1.0f / 0x0FFE);
4457 Float4 v = Float4(src.y) * Float4(1.0f / 0x0FFE);
4458 Float4 s = Float4(src.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004459
4460 sampleTexture(r, dst, sampler, u, v, s, s, project);
4461 }
4462
John Bauman19bac1e2014-05-06 15:23:49 -04004463 void PixelRoutine::TEXBEM(Registers &r, Vector4i &dst, Vector4i &src, Float4 &u, Float4 &v, Float4 &s, int stage)
John Bauman89401822014-05-06 15:04:28 -04004464 {
John Bauman19bac1e2014-05-06 15:23:49 -04004465 Float4 du = Float4(src.x) * Float4(1.0f / 0x0FFE);
4466 Float4 dv = Float4(src.y) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004467
4468 Float4 du2 = du;
4469 Float4 dv2 = dv;
4470
4471 du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
4472 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
4473 du += dv2;
4474 dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
4475 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
4476 dv += du2;
4477
4478 Float4 u_ = u + du;
4479 Float4 v_ = v + dv;
4480
4481 sampleTexture(r, dst, stage, u_, v_, s, s);
4482 }
4483
John Bauman19bac1e2014-05-06 15:23:49 -04004484 void PixelRoutine::TEXBEML(Registers &r, Vector4i &dst, Vector4i &src, Float4 &u, Float4 &v, Float4 &s, int stage)
John Bauman89401822014-05-06 15:04:28 -04004485 {
John Bauman19bac1e2014-05-06 15:23:49 -04004486 Float4 du = Float4(src.x) * Float4(1.0f / 0x0FFE);
4487 Float4 dv = Float4(src.y) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004488
4489 Float4 du2 = du;
4490 Float4 dv2 = dv;
4491
4492 du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
4493 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
4494 du += dv2;
4495 dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
4496 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
4497 dv += du2;
4498
4499 Float4 u_ = u + du;
4500 Float4 v_ = v + dv;
4501
4502 sampleTexture(r, dst, stage, u_, v_, s, s);
4503
4504 Short4 L;
4505
John Bauman19bac1e2014-05-06 15:23:49 -04004506 L = src.z;
John Bauman89401822014-05-06 15:04:28 -04004507 L = MulHigh(L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceScale4)));
4508 L = L << 4;
4509 L = AddSat(L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceOffset4)));
4510 L = Max(L, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman19bac1e2014-05-06 15:23:49 -04004511 L = Min(L, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04004512
John Bauman19bac1e2014-05-06 15:23:49 -04004513 dst.x = MulHigh(dst.x, L); dst.x = dst.x << 4;
4514 dst.y = MulHigh(dst.y, L); dst.y = dst.y << 4;
4515 dst.z = MulHigh(dst.z, L); dst.z = dst.z << 4;
John Bauman89401822014-05-06 15:04:28 -04004516 }
4517
John Bauman19bac1e2014-05-06 15:23:49 -04004518 void PixelRoutine::TEXREG2AR(Registers &r, Vector4i &dst, Vector4i &src0, int stage)
John Bauman89401822014-05-06 15:04:28 -04004519 {
John Bauman19bac1e2014-05-06 15:23:49 -04004520 Float4 u = Float4(src0.w) * Float4(1.0f / 0x0FFE);
4521 Float4 v = Float4(src0.x) * Float4(1.0f / 0x0FFE);
4522 Float4 s = Float4(src0.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004523
4524 sampleTexture(r, dst, stage, u, v, s, s);
4525 }
4526
John Bauman19bac1e2014-05-06 15:23:49 -04004527 void PixelRoutine::TEXREG2GB(Registers &r, Vector4i &dst, Vector4i &src0, int stage)
John Bauman89401822014-05-06 15:04:28 -04004528 {
John Bauman19bac1e2014-05-06 15:23:49 -04004529 Float4 u = Float4(src0.y) * Float4(1.0f / 0x0FFE);
4530 Float4 v = Float4(src0.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004531 Float4 s = v;
4532
4533 sampleTexture(r, dst, stage, u, v, s, s);
4534 }
4535
John Bauman19bac1e2014-05-06 15:23:49 -04004536 void PixelRoutine::TEXREG2RGB(Registers &r, Vector4i &dst, Vector4i &src0, int stage)
John Bauman89401822014-05-06 15:04:28 -04004537 {
John Bauman19bac1e2014-05-06 15:23:49 -04004538 Float4 u = Float4(src0.x) * Float4(1.0f / 0x0FFE);
4539 Float4 v = Float4(src0.y) * Float4(1.0f / 0x0FFE);
4540 Float4 s = Float4(src0.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004541
4542 sampleTexture(r, dst, stage, u, v, s, s);
4543 }
4544
John Bauman19bac1e2014-05-06 15:23:49 -04004545 void PixelRoutine::TEXM3X2DEPTH(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004546 {
4547 TEXM3X2PAD(r, u, v, s, src, 1, signedScaling);
4548
4549 // z / w
4550 r.u_ *= Rcp_pp(r.v_); // FIXME: Set result to 1.0 when division by zero
4551
4552 r.oDepth = r.u_;
4553 }
4554
John Bauman19bac1e2014-05-06 15:23:49 -04004555 void PixelRoutine::TEXM3X2PAD(Registers &r, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, int component, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004556 {
4557 TEXM3X3PAD(r, u, v, s, src0, component, signedScaling);
4558 }
4559
John Bauman19bac1e2014-05-06 15:23:49 -04004560 void PixelRoutine::TEXM3X2TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004561 {
4562 TEXM3X2PAD(r, u, v, s, src0, 1, signedScaling);
4563
John Bauman19bac1e2014-05-06 15:23:49 -04004564 r.w_ = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04004565
4566 sampleTexture(r, dst, stage, r.u_, r.v_, r.w_, r.w_);
4567 }
4568
John Bauman19bac1e2014-05-06 15:23:49 -04004569 void PixelRoutine::TEXM3X3(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004570 {
4571 TEXM3X3PAD(r, u, v, s, src0, 2, signedScaling);
4572
John Bauman19bac1e2014-05-06 15:23:49 -04004573 dst.x = RoundShort4(r.u_ * Float4(0x1000));
4574 dst.y = RoundShort4(r.v_ * Float4(0x1000));
4575 dst.z = RoundShort4(r.w_ * Float4(0x1000));
4576 dst.w = Short4(0x1000);
John Bauman89401822014-05-06 15:04:28 -04004577 }
4578
John Bauman19bac1e2014-05-06 15:23:49 -04004579 void PixelRoutine::TEXM3X3PAD(Registers &r, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, int component, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004580 {
4581 if(component == 0 || previousScaling != signedScaling) // FIXME: Other source modifiers?
4582 {
John Bauman19bac1e2014-05-06 15:23:49 -04004583 r.U = Float4(src0.x);
4584 r.V = Float4(src0.y);
4585 r.W = Float4(src0.z);
John Bauman89401822014-05-06 15:04:28 -04004586
4587 previousScaling = signedScaling;
4588 }
4589
4590 Float4 x = r.U * u + r.V * v + r.W * s;
4591
John Bauman19bac1e2014-05-06 15:23:49 -04004592 x *= Float4(1.0f / 0x1000);
John Bauman89401822014-05-06 15:04:28 -04004593
4594 switch(component)
4595 {
4596 case 0: r.u_ = x; break;
4597 case 1: r.v_ = x; break;
4598 case 2: r.w_ = x; break;
4599 default: ASSERT(false);
4600 }
4601 }
4602
John Bauman19bac1e2014-05-06 15:23:49 -04004603 void PixelRoutine::TEXM3X3SPEC(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004604 {
4605 TEXM3X3PAD(r, u, v, s, src0, 2, false);
4606
4607 Float4 E[3]; // Eye vector
4608
John Bauman19bac1e2014-05-06 15:23:49 -04004609 E[0] = Float4(src1.x) * Float4(1.0f / 0x0FFE);
4610 E[1] = Float4(src1.y) * Float4(1.0f / 0x0FFE);
4611 E[2] = Float4(src1.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004612
4613 // Reflection
4614 Float4 u__;
4615 Float4 v__;
4616 Float4 w__;
4617
4618 // (u'', v'', w'') = 2 * (N . E) * N - E * (N . N)
4619 u__ = r.u_ * E[0];
4620 v__ = r.v_ * E[1];
4621 w__ = r.w_ * E[2];
4622 u__ += v__ + w__;
4623 u__ += u__;
4624 v__ = u__;
4625 w__ = u__;
4626 u__ *= r.u_;
4627 v__ *= r.v_;
4628 w__ *= r.w_;
4629 r.u_ *= r.u_;
4630 r.v_ *= r.v_;
4631 r.w_ *= r.w_;
4632 r.u_ += r.v_ + r.w_;
4633 u__ -= E[0] * r.u_;
4634 v__ -= E[1] * r.u_;
4635 w__ -= E[2] * r.u_;
4636
4637 sampleTexture(r, dst, stage, u__, v__, w__, w__);
4638 }
4639
John Bauman19bac1e2014-05-06 15:23:49 -04004640 void PixelRoutine::TEXM3X3TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004641 {
4642 TEXM3X3PAD(r, u, v, s, src0, 2, signedScaling);
4643
4644 sampleTexture(r, dst, stage, r.u_, r.v_, r.w_, r.w_);
4645 }
4646
John Bauman19bac1e2014-05-06 15:23:49 -04004647 void PixelRoutine::TEXM3X3VSPEC(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0)
John Bauman89401822014-05-06 15:04:28 -04004648 {
4649 TEXM3X3PAD(r, u, v, s, src0, 2, false);
4650
4651 Float4 E[3]; // Eye vector
4652
John Bauman19bac1e2014-05-06 15:23:49 -04004653 E[0] = r.vf[2 + stage - 2].w;
4654 E[1] = r.vf[2 + stage - 1].w;
4655 E[2] = r.vf[2 + stage - 0].w;
John Bauman89401822014-05-06 15:04:28 -04004656
4657 // Reflection
4658 Float4 u__;
4659 Float4 v__;
4660 Float4 w__;
4661
4662 // (u'', v'', w'') = 2 * (N . E) * N - E * (N . N)
4663 u__ = r.u_ * E[0];
4664 v__ = r.v_ * E[1];
4665 w__ = r.w_ * E[2];
4666 u__ += v__ + w__;
4667 u__ += u__;
4668 v__ = u__;
4669 w__ = u__;
4670 u__ *= r.u_;
4671 v__ *= r.v_;
4672 w__ *= r.w_;
4673 r.u_ *= r.u_;
4674 r.v_ *= r.v_;
4675 r.w_ *= r.w_;
4676 r.u_ += r.v_ + r.w_;
4677 u__ -= E[0] * r.u_;
4678 v__ -= E[1] * r.u_;
4679 w__ -= E[2] * r.u_;
4680
4681 sampleTexture(r, dst, stage, u__, v__, w__, w__);
4682 }
4683
4684 void PixelRoutine::TEXDEPTH(Registers &r)
4685 {
John Bauman19bac1e2014-05-06 15:23:49 -04004686 r.u_ = Float4(r.ri[5].x);
4687 r.v_ = Float4(r.ri[5].y);
John Bauman89401822014-05-06 15:04:28 -04004688
4689 // z / w
4690 r.u_ *= Rcp_pp(r.v_); // FIXME: Set result to 1.0 when division by zero
4691
4692 r.oDepth = r.u_;
4693 }
4694
John Bauman19bac1e2014-05-06 15:23:49 -04004695 void PixelRoutine::CND(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2)
John Bauman89401822014-05-06 15:04:28 -04004696 {
John Bauman19bac1e2014-05-06 15:23:49 -04004697 {Short4 t0; t0 = src0.x; t0 = CmpGT(t0, Short4(0x0800, 0x0800, 0x0800, 0x0800)); Short4 t1; t1 = src1.x; t1 = t1 & t0; t0 = ~t0 & src2.x; t0 = t0 | t1; dst.x = t0;};
4698 {Short4 t0; t0 = src0.y; t0 = CmpGT(t0, Short4(0x0800, 0x0800, 0x0800, 0x0800)); Short4 t1; t1 = src1.y; t1 = t1 & t0; t0 = ~t0 & src2.y; t0 = t0 | t1; dst.y = t0;};
4699 {Short4 t0; t0 = src0.z; t0 = CmpGT(t0, Short4(0x0800, 0x0800, 0x0800, 0x0800)); Short4 t1; t1 = src1.z; t1 = t1 & t0; t0 = ~t0 & src2.z; t0 = t0 | t1; dst.z = t0;};
4700 {Short4 t0; t0 = src0.w; t0 = CmpGT(t0, Short4(0x0800, 0x0800, 0x0800, 0x0800)); Short4 t1; t1 = src1.w; t1 = t1 & t0; t0 = ~t0 & src2.w; t0 = t0 | t1; dst.w = t0;};
John Bauman89401822014-05-06 15:04:28 -04004701 }
4702
John Bauman19bac1e2014-05-06 15:23:49 -04004703 void PixelRoutine::CMP(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2)
John Bauman89401822014-05-06 15:04:28 -04004704 {
John Bauman19bac1e2014-05-06 15:23:49 -04004705 {Short4 t0 = CmpGT(Short4(0x0000, 0x0000, 0x0000, 0x0000), src0.x); Short4 t1; t1 = src2.x; t1 &= t0; t0 = ~t0 & src1.x; t0 |= t1; dst.x = t0;};
4706 {Short4 t0 = CmpGT(Short4(0x0000, 0x0000, 0x0000, 0x0000), src0.y); Short4 t1; t1 = src2.y; t1 &= t0; t0 = ~t0 & src1.y; t0 |= t1; dst.y = t0;};
4707 {Short4 t0 = CmpGT(Short4(0x0000, 0x0000, 0x0000, 0x0000), src0.z); Short4 t1; t1 = src2.z; t1 &= t0; t0 = ~t0 & src1.z; t0 |= t1; dst.z = t0;};
4708 {Short4 t0 = CmpGT(Short4(0x0000, 0x0000, 0x0000, 0x0000), src0.w); Short4 t1; t1 = src2.w; t1 &= t0; t0 = ~t0 & src1.w; t0 |= t1; dst.w = t0;};
John Bauman89401822014-05-06 15:04:28 -04004709 }
4710
John Bauman19bac1e2014-05-06 15:23:49 -04004711 void PixelRoutine::BEM(Registers &r, Vector4i &dst, Vector4i &src0, Vector4i &src1, int stage)
John Bauman89401822014-05-06 15:04:28 -04004712 {
4713 Short4 t0;
4714 Short4 t1;
4715
John Bauman19bac1e2014-05-06 15:23:49 -04004716 // dst.x = src0.x + BUMPENVMAT00(stage) * src1.x + BUMPENVMAT10(stage) * src1.y
John Bauman89401822014-05-06 15:04:28 -04004717 t0 = MulHigh(src1.x, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[0][0]))); t0 = t0 << 4; // FIXME: Matrix components range? Overflow hazard.
4718 t1 = MulHigh(src1.y, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[1][0]))); t1 = t1 << 4; // FIXME: Matrix components range? Overflow hazard.
4719 t0 = AddSat(t0, t1);
4720 t0 = AddSat(t0, src0.x);
John Bauman19bac1e2014-05-06 15:23:49 -04004721 dst.x = t0;
John Bauman89401822014-05-06 15:04:28 -04004722
John Bauman19bac1e2014-05-06 15:23:49 -04004723 // dst.y = src0.y + BUMPENVMAT01(stage) * src1.x + BUMPENVMAT11(stage) * src1.y
John Bauman89401822014-05-06 15:04:28 -04004724 t0 = MulHigh(src1.x, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[0][1]))); t0 = t0 << 4; // FIXME: Matrix components range? Overflow hazard.
4725 t1 = MulHigh(src1.y, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[1][1]))); t1 = t1 << 4; // FIXME: Matrix components range? Overflow hazard.
4726 t0 = AddSat(t0, t1);
4727 t0 = AddSat(t0, src0.y);
John Bauman19bac1e2014-05-06 15:23:49 -04004728 dst.y = t0;
John Bauman89401822014-05-06 15:04:28 -04004729 }
4730
John Bauman19bac1e2014-05-06 15:23:49 -04004731 void PixelRoutine::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004732 {
John Bauman19bac1e2014-05-06 15:23:49 -04004733 Vector4f row0 = reg(r, src1, 0);
4734 Vector4f row1 = reg(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -04004735
4736 dst.x = dot3(src0, row0);
4737 dst.y = dot3(src0, row1);
4738 }
4739
John Bauman19bac1e2014-05-06 15:23:49 -04004740 void PixelRoutine::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004741 {
John Bauman19bac1e2014-05-06 15:23:49 -04004742 Vector4f row0 = reg(r, src1, 0);
4743 Vector4f row1 = reg(r, src1, 1);
4744 Vector4f row2 = reg(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -04004745
4746 dst.x = dot3(src0, row0);
4747 dst.y = dot3(src0, row1);
4748 dst.z = dot3(src0, row2);
4749 }
4750
John Bauman19bac1e2014-05-06 15:23:49 -04004751 void PixelRoutine::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004752 {
John Bauman19bac1e2014-05-06 15:23:49 -04004753 Vector4f row0 = reg(r, src1, 0);
4754 Vector4f row1 = reg(r, src1, 1);
4755 Vector4f row2 = reg(r, src1, 2);
4756 Vector4f row3 = reg(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -04004757
4758 dst.x = dot3(src0, row0);
4759 dst.y = dot3(src0, row1);
4760 dst.z = dot3(src0, row2);
4761 dst.w = dot3(src0, row3);
4762 }
4763
John Bauman19bac1e2014-05-06 15:23:49 -04004764 void PixelRoutine::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004765 {
John Bauman19bac1e2014-05-06 15:23:49 -04004766 Vector4f row0 = reg(r, src1, 0);
4767 Vector4f row1 = reg(r, src1, 1);
4768 Vector4f row2 = reg(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -04004769
4770 dst.x = dot4(src0, row0);
4771 dst.y = dot4(src0, row1);
4772 dst.z = dot4(src0, row2);
4773 }
4774
John Bauman19bac1e2014-05-06 15:23:49 -04004775 void PixelRoutine::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004776 {
John Bauman19bac1e2014-05-06 15:23:49 -04004777 Vector4f row0 = reg(r, src1, 0);
4778 Vector4f row1 = reg(r, src1, 1);
4779 Vector4f row2 = reg(r, src1, 2);
4780 Vector4f row3 = reg(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -04004781
4782 dst.x = dot4(src0, row0);
4783 dst.y = dot4(src0, row1);
4784 dst.z = dot4(src0, row2);
4785 dst.w = dot4(src0, row3);
4786 }
4787
John Bauman19bac1e2014-05-06 15:23:49 -04004788 void PixelRoutine::TEXLD(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias)
John Bauman89401822014-05-06 15:04:28 -04004789 {
John Bauman19bac1e2014-05-06 15:23:49 -04004790 Vector4f tmp;
4791 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w, src0, src0, project, bias);
John Bauman89401822014-05-06 15:04:28 -04004792
4793 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
4794 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
4795 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
4796 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
4797 }
4798
John Bauman19bac1e2014-05-06 15:23:49 -04004799 void PixelRoutine::TEXLDD(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &src2, Vector4f &src3, bool project, bool bias)
John Bauman89401822014-05-06 15:04:28 -04004800 {
John Bauman19bac1e2014-05-06 15:23:49 -04004801 Vector4f tmp;
4802 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w, src2, src3, project, bias, true);
John Bauman89401822014-05-06 15:04:28 -04004803
4804 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
4805 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
4806 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
4807 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
4808 }
4809
John Bauman19bac1e2014-05-06 15:23:49 -04004810 void PixelRoutine::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias)
John Bauman89401822014-05-06 15:04:28 -04004811 {
John Bauman19bac1e2014-05-06 15:23:49 -04004812 Vector4f tmp;
4813 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w, src0, src0, project, bias, false, true);
John Bauman89401822014-05-06 15:04:28 -04004814
4815 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
4816 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
4817 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
4818 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
4819 }
4820
John Bauman19bac1e2014-05-06 15:23:49 -04004821 void PixelRoutine::TEXKILL(Int cMask[4], Vector4f &src, unsigned char mask)
John Bauman89401822014-05-06 15:04:28 -04004822 {
4823 Int kill = -1;
4824
John Bauman19bac1e2014-05-06 15:23:49 -04004825 if(mask & 0x1) kill &= SignMask(CmpNLT(src.x, Float4(0.0f)));
4826 if(mask & 0x2) kill &= SignMask(CmpNLT(src.y, Float4(0.0f)));
4827 if(mask & 0x4) kill &= SignMask(CmpNLT(src.z, Float4(0.0f)));
4828 if(mask & 0x8) kill &= SignMask(CmpNLT(src.w, Float4(0.0f)));
4829
4830 // FIXME: Dynamic branching affects TEXKILL?
4831 // if(shader->containsDynamicBranching())
4832 // {
4833 // kill = ~SignMask(enableMask(r));
4834 // }
John Bauman89401822014-05-06 15:04:28 -04004835
4836 for(unsigned int q = 0; q < state.multiSample; q++)
4837 {
4838 cMask[q] &= kill;
4839 }
John Bauman19bac1e2014-05-06 15:23:49 -04004840
4841 // FIXME: Branch to end of shader if all killed?
John Bauman89401822014-05-06 15:04:28 -04004842 }
4843
John Bauman19bac1e2014-05-06 15:23:49 -04004844 void PixelRoutine::DISCARD(Registers &r, Int cMask[4], const Shader::Instruction *instruction)
John Bauman89401822014-05-06 15:04:28 -04004845 {
John Bauman19bac1e2014-05-06 15:23:49 -04004846 Int kill = 0;
4847
4848 if(shader->containsDynamicBranching())
4849 {
4850 kill = ~SignMask(enableMask(r, instruction));
4851 }
4852
4853 for(unsigned int q = 0; q < state.multiSample; q++)
4854 {
4855 cMask[q] &= kill;
4856 }
4857
4858 // FIXME: Branch to end of shader if all killed?
John Bauman89401822014-05-06 15:04:28 -04004859 }
4860
John Bauman19bac1e2014-05-06 15:23:49 -04004861 void PixelRoutine::DFDX(Vector4f &dst, Vector4f &src)
John Bauman89401822014-05-06 15:04:28 -04004862 {
John Bauman19bac1e2014-05-06 15:23:49 -04004863 dst.x = src.x.yyww - src.x.xxzz;
4864 dst.y = src.y.yyww - src.y.xxzz;
4865 dst.z = src.z.yyww - src.z.xxzz;
4866 dst.w = src.w.yyww - src.w.xxzz;
4867 }
4868
4869 void PixelRoutine::DFDY(Vector4f &dst, Vector4f &src)
4870 {
4871 dst.x = src.x.zwzw - src.x.xyxy;
4872 dst.y = src.y.zwzw - src.y.xyxy;
4873 dst.z = src.z.zwzw - src.z.xyxy;
4874 dst.w = src.w.zwzw - src.w.xyxy;
4875 }
4876
4877 void PixelRoutine::FWIDTH(Vector4f &dst, Vector4f &src)
4878 {
4879 // abs(dFdx(src)) + abs(dFdy(src));
4880 dst.x = Abs(src.x.yyww - src.x.xxzz) + Abs(src.x.zwzw - src.x.xyxy);
4881 dst.y = Abs(src.y.yyww - src.x.xxzz) + Abs(src.y.zwzw - src.y.xyxy);
4882 dst.z = Abs(src.z.yyww - src.x.xxzz) + Abs(src.z.zwzw - src.z.xyxy);
4883 dst.w = Abs(src.w.yyww - src.x.xxzz) + Abs(src.w.zwzw - src.w.xyxy);
John Bauman89401822014-05-06 15:04:28 -04004884 }
4885
4886 void PixelRoutine::BREAK(Registers &r)
4887 {
4888 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
4889 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
4890
4891 if(breakDepth == 0)
4892 {
John Bauman19bac1e2014-05-06 15:23:49 -04004893 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04004894 Nucleus::createBr(endBlock);
4895 }
4896 else
4897 {
4898 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
4899 Bool allBreak = SignMask(r.enableBreak) == 0x0;
4900
John Bauman19bac1e2014-05-06 15:23:49 -04004901 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04004902 branch(allBreak, endBlock, deadBlock);
4903 }
4904
4905 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04004906 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04004907 }
4908
John Bauman19bac1e2014-05-06 15:23:49 -04004909 void PixelRoutine::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04004910 {
4911 Int4 condition;
4912
4913 switch(control)
4914 {
John Bauman19bac1e2014-05-06 15:23:49 -04004915 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
4916 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
4917 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
4918 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
4919 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
4920 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04004921 default:
4922 ASSERT(false);
4923 }
4924
John Bauman19bac1e2014-05-06 15:23:49 -04004925 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -04004926 }
4927
4928 void PixelRoutine::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
4929 {
4930 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
4931
John Bauman19bac1e2014-05-06 15:23:49 -04004932 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04004933 {
4934 condition = ~condition;
4935 }
4936
John Bauman19bac1e2014-05-06 15:23:49 -04004937 BREAK(r, condition);
4938 }
4939
4940 void PixelRoutine::BREAK(Registers &r, Int4 &condition)
4941 {
John Bauman89401822014-05-06 15:04:28 -04004942 condition &= r.enableStack[r.enableIndex];
4943
4944 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
4945 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
4946
4947 r.enableBreak = r.enableBreak & ~condition;
4948 Bool allBreak = SignMask(r.enableBreak) == 0x0;
4949
John Bauman19bac1e2014-05-06 15:23:49 -04004950 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04004951 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04004952
John Bauman89401822014-05-06 15:04:28 -04004953 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04004954 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04004955 }
4956
John Bauman19bac1e2014-05-06 15:23:49 -04004957 void PixelRoutine::CONTINUE(Registers &r)
4958 {
4959 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
4960 }
4961
4962 void PixelRoutine::TEST()
4963 {
4964 whileTest = true;
4965 }
4966
4967 void PixelRoutine::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04004968 {
4969 if(!labelBlock[labelIndex])
4970 {
4971 labelBlock[labelIndex] = Nucleus::createBasicBlock();
4972 }
4973
John Bauman19bac1e2014-05-06 15:23:49 -04004974 if(callRetBlock[labelIndex].size() > 1)
4975 {
4976 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
4977 }
John Bauman89401822014-05-06 15:04:28 -04004978
John Bauman19bac1e2014-05-06 15:23:49 -04004979 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04004980
4981 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04004982 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
4983
4984 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04004985 }
4986
John Bauman19bac1e2014-05-06 15:23:49 -04004987 void PixelRoutine::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04004988 {
John Bauman19bac1e2014-05-06 15:23:49 -04004989 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04004990 {
John Bauman19bac1e2014-05-06 15:23:49 -04004991 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04004992 }
John Bauman19bac1e2014-05-06 15:23:49 -04004993 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04004994 {
John Bauman19bac1e2014-05-06 15:23:49 -04004995 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04004996 }
4997 else ASSERT(false);
4998 }
4999
John Bauman19bac1e2014-05-06 15:23:49 -04005000 void PixelRoutine::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04005001 {
5002 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,ps.b[boolRegister.index])) != Byte(0)); // FIXME
5003
John Bauman19bac1e2014-05-06 15:23:49 -04005004 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005005 {
5006 condition = !condition;
5007 }
5008
5009 if(!labelBlock[labelIndex])
5010 {
5011 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5012 }
5013
John Bauman19bac1e2014-05-06 15:23:49 -04005014 if(callRetBlock[labelIndex].size() > 1)
5015 {
5016 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
5017 }
John Bauman89401822014-05-06 15:04:28 -04005018
John Bauman19bac1e2014-05-06 15:23:49 -04005019 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04005020
John Bauman19bac1e2014-05-06 15:23:49 -04005021 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
5022 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
5023
5024 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04005025 }
5026
John Bauman19bac1e2014-05-06 15:23:49 -04005027 void PixelRoutine::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04005028 {
5029 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
5030
John Bauman19bac1e2014-05-06 15:23:49 -04005031 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005032 {
5033 condition = ~condition;
5034 }
5035
5036 condition &= r.enableStack[r.enableIndex];
5037
5038 if(!labelBlock[labelIndex])
5039 {
5040 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5041 }
5042
John Bauman19bac1e2014-05-06 15:23:49 -04005043 if(callRetBlock[labelIndex].size() > 1)
5044 {
5045 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
5046 }
John Bauman89401822014-05-06 15:04:28 -04005047
5048 r.enableIndex++;
5049 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04005050 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04005051
John Bauman19bac1e2014-05-06 15:23:49 -04005052 Bool notAllFalse = SignMask(condition) != 0;
5053 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
5054 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04005055
5056 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04005057 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04005058 }
5059
5060 void PixelRoutine::ELSE(Registers &r)
5061 {
5062 ifDepth--;
5063
5064 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
5065 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5066
5067 if(isConditionalIf[ifDepth])
5068 {
5069 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04005070 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04005071
5072 branch(notAllFalse, falseBlock, endBlock);
5073
5074 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
5075 }
5076 else
5077 {
5078 Nucleus::createBr(endBlock);
5079 Nucleus::setInsertBlock(falseBlock);
5080 }
5081
5082 ifFalseBlock[ifDepth] = endBlock;
5083
5084 ifDepth++;
5085 }
5086
5087 void PixelRoutine::ENDIF(Registers &r)
5088 {
5089 ifDepth--;
5090
5091 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
5092
5093 Nucleus::createBr(endBlock);
5094 Nucleus::setInsertBlock(endBlock);
5095
5096 if(isConditionalIf[ifDepth])
5097 {
5098 breakDepth--;
5099 r.enableIndex--;
5100 }
5101 }
5102
John Bauman89401822014-05-06 15:04:28 -04005103 void PixelRoutine::ENDLOOP(Registers &r)
5104 {
5105 loopRepDepth--;
5106
5107 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
5108
5109 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
5110 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
5111
5112 Nucleus::createBr(testBlock);
5113 Nucleus::setInsertBlock(endBlock);
5114
5115 r.loopDepth--;
5116 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
5117 }
5118
John Bauman19bac1e2014-05-06 15:23:49 -04005119 void PixelRoutine::ENDREP(Registers &r)
5120 {
5121 loopRepDepth--;
5122
5123 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
5124 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
5125
5126 Nucleus::createBr(testBlock);
5127 Nucleus::setInsertBlock(endBlock);
5128
5129 r.loopDepth--;
5130 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
5131 }
5132
5133 void PixelRoutine::ENDWHILE(Registers &r)
5134 {
5135 loopRepDepth--;
5136
5137 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
5138 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
5139
5140 Nucleus::createBr(testBlock);
5141 Nucleus::setInsertBlock(endBlock);
5142
5143 r.enableIndex--;
5144 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
5145 whileTest = false;
5146 }
5147
John Bauman89401822014-05-06 15:04:28 -04005148 void PixelRoutine::IF(Registers &r, const Src &src)
5149 {
John Bauman19bac1e2014-05-06 15:23:49 -04005150 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04005151 {
5152 IFb(r, src);
5153 }
John Bauman19bac1e2014-05-06 15:23:49 -04005154 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04005155 {
5156 IFp(r, src);
5157 }
John Bauman19bac1e2014-05-06 15:23:49 -04005158 else
5159 {
5160 Int4 condition = As<Int4>(reg(r, src).x);
5161 IF(r, condition);
5162 }
John Bauman89401822014-05-06 15:04:28 -04005163 }
5164
5165 void PixelRoutine::IFb(Registers &r, const Src &boolRegister)
5166 {
John Bauman19bac1e2014-05-06 15:23:49 -04005167 ASSERT(ifDepth < 24 + 4);
5168
John Bauman89401822014-05-06 15:04:28 -04005169 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,ps.b[boolRegister.index])) != Byte(0)); // FIXME
5170
John Bauman19bac1e2014-05-06 15:23:49 -04005171 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005172 {
John Bauman19bac1e2014-05-06 15:23:49 -04005173 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04005174 }
5175
5176 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
5177 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
5178
5179 branch(condition, trueBlock, falseBlock);
5180
5181 isConditionalIf[ifDepth] = false;
5182 ifFalseBlock[ifDepth] = falseBlock;
5183
5184 ifDepth++;
5185 }
5186
John Bauman19bac1e2014-05-06 15:23:49 -04005187 void PixelRoutine::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04005188 {
5189 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
5190
John Bauman19bac1e2014-05-06 15:23:49 -04005191 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005192 {
5193 condition = ~condition;
5194 }
5195
John Bauman19bac1e2014-05-06 15:23:49 -04005196 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04005197 }
5198
John Bauman19bac1e2014-05-06 15:23:49 -04005199 void PixelRoutine::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04005200 {
5201 Int4 condition;
5202
5203 switch(control)
5204 {
John Bauman19bac1e2014-05-06 15:23:49 -04005205 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
5206 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
5207 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
5208 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
5209 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
5210 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04005211 default:
5212 ASSERT(false);
5213 }
5214
John Bauman19bac1e2014-05-06 15:23:49 -04005215 IF(r, condition);
5216 }
5217
5218 void PixelRoutine::IF(Registers &r, Int4 &condition)
5219 {
John Bauman89401822014-05-06 15:04:28 -04005220 condition &= r.enableStack[r.enableIndex];
5221
5222 r.enableIndex++;
5223 r.enableStack[r.enableIndex] = condition;
5224
5225 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
5226 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
5227
John Bauman19bac1e2014-05-06 15:23:49 -04005228 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04005229
5230 branch(notAllFalse, trueBlock, falseBlock);
5231
5232 isConditionalIf[ifDepth] = true;
5233 ifFalseBlock[ifDepth] = falseBlock;
5234
5235 ifDepth++;
5236 breakDepth++;
5237 }
5238
5239 void PixelRoutine::LABEL(int labelIndex)
5240 {
John Bauman19bac1e2014-05-06 15:23:49 -04005241 if(!labelBlock[labelIndex])
5242 {
5243 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5244 }
5245
John Bauman89401822014-05-06 15:04:28 -04005246 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04005247 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04005248 }
5249
5250 void PixelRoutine::LOOP(Registers &r, const Src &integerRegister)
5251 {
5252 r.loopDepth++;
5253
5254 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][0]));
5255 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][1]));
5256 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][2]));
5257
5258 // If(r.increment[r.loopDepth] == 0)
5259 // {
5260 // r.increment[r.loopDepth] = 1;
5261 // }
5262
5263 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
5264 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
5265 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5266
5267 loopRepTestBlock[loopRepDepth] = testBlock;
5268 loopRepEndBlock[loopRepDepth] = endBlock;
5269
5270 // FIXME: jump(testBlock)
5271 Nucleus::createBr(testBlock);
5272 Nucleus::setInsertBlock(testBlock);
5273
5274 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
5275 Nucleus::setInsertBlock(loopBlock);
5276
5277 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
5278
5279 loopRepDepth++;
5280 breakDepth = 0;
5281 }
5282
5283 void PixelRoutine::REP(Registers &r, const Src &integerRegister)
5284 {
5285 r.loopDepth++;
5286
5287 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][0]));
5288 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
5289
5290 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
5291 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
5292 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5293
5294 loopRepTestBlock[loopRepDepth] = testBlock;
5295 loopRepEndBlock[loopRepDepth] = endBlock;
5296
5297 // FIXME: jump(testBlock)
5298 Nucleus::createBr(testBlock);
5299 Nucleus::setInsertBlock(testBlock);
5300
5301 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
5302 Nucleus::setInsertBlock(loopBlock);
5303
5304 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
5305
5306 loopRepDepth++;
5307 breakDepth = 0;
5308 }
5309
John Bauman19bac1e2014-05-06 15:23:49 -04005310 void PixelRoutine::WHILE(Registers &r, const Src &temporaryRegister)
5311 {
5312 r.enableIndex++;
5313
5314 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
5315 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
5316 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5317
5318 loopRepTestBlock[loopRepDepth] = testBlock;
5319 loopRepEndBlock[loopRepDepth] = endBlock;
5320
5321 Int4 restoreBreak = r.enableBreak;
5322 Int4 restoreContinue = r.enableContinue;
5323
5324 // FIXME: jump(testBlock)
5325 Nucleus::createBr(testBlock);
5326 Nucleus::setInsertBlock(testBlock);
5327 r.enableContinue = restoreContinue;
5328
5329 Vector4f &src = reg(r, temporaryRegister);
5330 Int4 condition = As<Int4>(src.x);
5331 condition &= r.enableStack[r.enableIndex - 1];
5332 r.enableStack[r.enableIndex] = condition;
5333
5334 Bool notAllFalse = SignMask(condition) != 0;
5335 branch(notAllFalse, loopBlock, endBlock);
5336
5337 Nucleus::setInsertBlock(endBlock);
5338 r.enableBreak = restoreBreak;
5339
5340 Nucleus::setInsertBlock(loopBlock);
5341
5342 loopRepDepth++;
5343 breakDepth = 0;
5344 }
5345
John Bauman89401822014-05-06 15:04:28 -04005346 void PixelRoutine::RET(Registers &r)
5347 {
John Bauman19bac1e2014-05-06 15:23:49 -04005348 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04005349 {
5350 returnBlock = Nucleus::createBasicBlock();
5351 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04005352 }
5353 else
5354 {
John Bauman89401822014-05-06 15:04:28 -04005355 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04005356
John Bauman19bac1e2014-05-06 15:23:49 -04005357 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04005358 {
John Bauman19bac1e2014-05-06 15:23:49 -04005359 // FIXME: Encapsulate
5360 UInt index = r.callStack[--r.stackIndex];
5361
5362 llvm::Value *value = Nucleus::createLoad(index.address);
5363 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
5364
5365 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
5366 {
5367 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
5368 }
5369 }
5370 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
5371 {
5372 Nucleus::createBr(callRetBlock[currentLabel][0]);
5373 }
5374 else // Function isn't called
5375 {
5376 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04005377 }
5378
5379 Nucleus::setInsertBlock(unreachableBlock);
5380 Nucleus::createUnreachable();
5381 }
5382 }
5383
John Bauman19bac1e2014-05-06 15:23:49 -04005384 void PixelRoutine::LEAVE(Registers &r)
5385 {
5386 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
5387
5388 // FIXME: Return from function if all instances left
5389 // FIXME: Use enableLeave in other control-flow constructs
5390 }
5391
5392 void PixelRoutine::writeDestination(Registers &r, Vector4i &d, const Dst &dst)
John Bauman89401822014-05-06 15:04:28 -04005393 {
5394 switch(dst.type)
5395 {
John Bauman19bac1e2014-05-06 15:23:49 -04005396 case Shader::PARAMETER_TEMP:
John Bauman89401822014-05-06 15:04:28 -04005397 if(dst.mask & 0x1) r.ri[dst.index].x = d.x;
5398 if(dst.mask & 0x2) r.ri[dst.index].y = d.y;
5399 if(dst.mask & 0x4) r.ri[dst.index].z = d.z;
5400 if(dst.mask & 0x8) r.ri[dst.index].w = d.w;
5401 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005402 case Shader::PARAMETER_INPUT:
John Bauman89401822014-05-06 15:04:28 -04005403 if(dst.mask & 0x1) r.vi[dst.index].x = d.x;
5404 if(dst.mask & 0x2) r.vi[dst.index].y = d.y;
5405 if(dst.mask & 0x4) r.vi[dst.index].z = d.z;
5406 if(dst.mask & 0x8) r.vi[dst.index].w = d.w;
5407 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005408 case Shader::PARAMETER_CONST: ASSERT(false); break;
5409 case Shader::PARAMETER_TEXTURE:
John Bauman89401822014-05-06 15:04:28 -04005410 if(dst.mask & 0x1) r.ti[dst.index].x = d.x;
5411 if(dst.mask & 0x2) r.ti[dst.index].y = d.y;
5412 if(dst.mask & 0x4) r.ti[dst.index].z = d.z;
5413 if(dst.mask & 0x8) r.ti[dst.index].w = d.w;
5414 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005415 case Shader::PARAMETER_COLOROUT:
John Bauman89401822014-05-06 15:04:28 -04005416 if(dst.mask & 0x1) r.vi[dst.index].x = d.x;
5417 if(dst.mask & 0x2) r.vi[dst.index].y = d.y;
5418 if(dst.mask & 0x4) r.vi[dst.index].z = d.z;
5419 if(dst.mask & 0x8) r.vi[dst.index].w = d.w;
5420 break;
5421 default:
5422 ASSERT(false);
5423 }
5424 }
5425
John Bauman19bac1e2014-05-06 15:23:49 -04005426 Vector4i PixelRoutine::regi(Registers &r, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04005427 {
John Bauman19bac1e2014-05-06 15:23:49 -04005428 Vector4i *reg;
John Bauman89401822014-05-06 15:04:28 -04005429 int i = src.index;
5430
John Bauman19bac1e2014-05-06 15:23:49 -04005431 Vector4i c;
John Bauman89401822014-05-06 15:04:28 -04005432
John Bauman19bac1e2014-05-06 15:23:49 -04005433 if(src.type == Shader::PARAMETER_CONST)
John Bauman89401822014-05-06 15:04:28 -04005434 {
John Bauman19bac1e2014-05-06 15:23:49 -04005435 c.x = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][0]));
5436 c.y = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][1]));
5437 c.z = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][2]));
5438 c.w = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][3]));
John Bauman89401822014-05-06 15:04:28 -04005439 }
5440
5441 switch(src.type)
5442 {
John Bauman19bac1e2014-05-06 15:23:49 -04005443 case Shader::PARAMETER_TEMP: reg = &r.ri[i]; break;
5444 case Shader::PARAMETER_INPUT: reg = &r.vi[i]; break;
5445 case Shader::PARAMETER_CONST: reg = &c; break;
5446 case Shader::PARAMETER_TEXTURE: reg = &r.ti[i]; break;
5447 case Shader::PARAMETER_VOID: return r.ri[0]; // Dummy
5448 case Shader::PARAMETER_FLOAT4LITERAL: return r.ri[0]; // Dummy
John Bauman89401822014-05-06 15:04:28 -04005449 default:
5450 ASSERT(false);
5451 }
5452
5453 Short4 &x = (*reg)[(src.swizzle >> 0) & 0x3];
5454 Short4 &y = (*reg)[(src.swizzle >> 2) & 0x3];
5455 Short4 &z = (*reg)[(src.swizzle >> 4) & 0x3];
5456 Short4 &w = (*reg)[(src.swizzle >> 6) & 0x3];
5457
John Bauman19bac1e2014-05-06 15:23:49 -04005458 Vector4i mod;
John Bauman89401822014-05-06 15:04:28 -04005459
5460 switch(src.modifier)
5461 {
John Bauman19bac1e2014-05-06 15:23:49 -04005462 case Shader::MODIFIER_NONE:
5463 mod.x = x;
5464 mod.y = y;
5465 mod.z = z;
5466 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -04005467 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005468 case Shader::MODIFIER_BIAS:
5469 mod.x = SubSat(x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5470 mod.y = SubSat(y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5471 mod.z = SubSat(z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5472 mod.w = SubSat(w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -04005473 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005474 case Shader::MODIFIER_BIAS_NEGATE:
5475 mod.x = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), x);
5476 mod.y = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), y);
5477 mod.z = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), z);
5478 mod.w = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), w);
John Bauman89401822014-05-06 15:04:28 -04005479 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005480 case Shader::MODIFIER_COMPLEMENT:
5481 mod.x = SubSat(Short4(0x1000), x);
5482 mod.y = SubSat(Short4(0x1000), y);
5483 mod.z = SubSat(Short4(0x1000), z);
5484 mod.w = SubSat(Short4(0x1000), w);
John Bauman89401822014-05-06 15:04:28 -04005485 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005486 case Shader::MODIFIER_NEGATE:
5487 mod.x = -x;
5488 mod.y = -y;
5489 mod.z = -z;
5490 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -04005491 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005492 case Shader::MODIFIER_X2:
5493 mod.x = AddSat(x, x);
5494 mod.y = AddSat(y, y);
5495 mod.z = AddSat(z, z);
5496 mod.w = AddSat(w, w);
John Bauman89401822014-05-06 15:04:28 -04005497 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005498 case Shader::MODIFIER_X2_NEGATE:
5499 mod.x = -AddSat(x, x);
5500 mod.y = -AddSat(y, y);
5501 mod.z = -AddSat(z, z);
5502 mod.w = -AddSat(w, w);
John Bauman89401822014-05-06 15:04:28 -04005503 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005504 case Shader::MODIFIER_SIGN:
5505 mod.x = SubSat(x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5506 mod.y = SubSat(y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5507 mod.z = SubSat(z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5508 mod.w = SubSat(w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5509 mod.x = AddSat(mod.x, mod.x);
5510 mod.y = AddSat(mod.y, mod.y);
5511 mod.z = AddSat(mod.z, mod.z);
5512 mod.w = AddSat(mod.w, mod.w);
John Bauman89401822014-05-06 15:04:28 -04005513 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005514 case Shader::MODIFIER_SIGN_NEGATE:
5515 mod.x = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), x);
5516 mod.y = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), y);
5517 mod.z = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), z);
5518 mod.w = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), w);
5519 mod.x = AddSat(mod.x, mod.x);
5520 mod.y = AddSat(mod.y, mod.y);
5521 mod.z = AddSat(mod.z, mod.z);
5522 mod.w = AddSat(mod.w, mod.w);
John Bauman89401822014-05-06 15:04:28 -04005523 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005524 case Shader::MODIFIER_DZ:
5525 mod.x = x;
5526 mod.y = y;
5527 mod.z = z;
5528 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -04005529 // Projection performed by texture sampler
5530 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005531 case Shader::MODIFIER_DW:
5532 mod.x = x;
5533 mod.y = y;
5534 mod.z = z;
5535 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -04005536 // Projection performed by texture sampler
5537 break;
5538 default:
5539 ASSERT(false);
5540 }
5541
John Bauman19bac1e2014-05-06 15:23:49 -04005542 if(src.type == Shader::PARAMETER_CONST && (src.modifier == Shader::MODIFIER_X2 || src.modifier == Shader::MODIFIER_X2_NEGATE))
John Bauman89401822014-05-06 15:04:28 -04005543 {
John Bauman19bac1e2014-05-06 15:23:49 -04005544 mod.x = Min(mod.x, Short4(0x1000)); mod.x = Max(mod.x, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
5545 mod.y = Min(mod.y, Short4(0x1000)); mod.y = Max(mod.y, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
5546 mod.z = Min(mod.z, Short4(0x1000)); mod.z = Max(mod.z, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
5547 mod.w = Min(mod.w, Short4(0x1000)); mod.w = Max(mod.w, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
John Bauman89401822014-05-06 15:04:28 -04005548 }
5549
5550 return mod;
5551 }
5552
John Bauman19bac1e2014-05-06 15:23:49 -04005553 Vector4f PixelRoutine::reg(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -04005554 {
John Bauman19bac1e2014-05-06 15:23:49 -04005555 Vector4f reg;
John Bauman89401822014-05-06 15:04:28 -04005556 int i = src.index + offset;
5557
5558 switch(src.type)
5559 {
John Bauman19bac1e2014-05-06 15:23:49 -04005560 case Shader::PARAMETER_TEMP:
5561 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman89401822014-05-06 15:04:28 -04005562 {
John Bauman19bac1e2014-05-06 15:23:49 -04005563 reg = r.rf[i];
5564 }
5565 else
5566 {
5567 Int a = relativeAddress(r, src);
5568
5569 reg = r.rf[i + a];
5570 }
5571 break;
5572 case Shader::PARAMETER_INPUT:
5573 {
5574 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -04005575 {
John Bauman19bac1e2014-05-06 15:23:49 -04005576 reg = r.vf[i];
John Bauman89401822014-05-06 15:04:28 -04005577 }
John Bauman19bac1e2014-05-06 15:23:49 -04005578 else if(src.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -04005579 {
5580 Int aL = r.aL[r.loopDepth];
5581
John Bauman19bac1e2014-05-06 15:23:49 -04005582 reg = r.vf[i + aL];
John Bauman89401822014-05-06 15:04:28 -04005583 }
John Bauman19bac1e2014-05-06 15:23:49 -04005584 else
John Bauman89401822014-05-06 15:04:28 -04005585 {
John Bauman19bac1e2014-05-06 15:23:49 -04005586 Int a = relativeAddress(r, src);
5587
5588 reg = r.vf[i + a];
John Bauman89401822014-05-06 15:04:28 -04005589 }
5590 }
5591 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005592 case Shader::PARAMETER_CONST:
5593 reg = readConstant(r, src, offset);
John Bauman89401822014-05-06 15:04:28 -04005594 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005595 case Shader::PARAMETER_TEXTURE:
5596 reg = r.vf[2 + i];
5597 break;
5598 case Shader::PARAMETER_MISCTYPE:
John Bauman89401822014-05-06 15:04:28 -04005599 if(src.index == 0) reg = r.vPos;
5600 if(src.index == 1) reg = r.vFace;
5601 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005602 case Shader::PARAMETER_SAMPLER:
5603 if(src.rel.type == Shader::PARAMETER_VOID)
5604 {
5605 reg.x = As<Float4>(Int4(i));
5606 }
5607 else if(src.rel.type == Shader::PARAMETER_TEMP)
5608 {
5609 reg.x = As<Float4>(Int4(i) + RoundInt(r.rf[src.rel.index].x));
5610 }
5611 return reg;
5612 case Shader::PARAMETER_PREDICATE: return reg; // Dummy
5613 case Shader::PARAMETER_VOID: return reg; // Dummy
5614 case Shader::PARAMETER_FLOAT4LITERAL:
5615 reg.x = Float4(src.value[0]);
5616 reg.y = Float4(src.value[1]);
5617 reg.z = Float4(src.value[2]);
5618 reg.w = Float4(src.value[3]);
5619 break;
5620 case Shader::PARAMETER_CONSTINT: return reg; // Dummy
5621 case Shader::PARAMETER_CONSTBOOL: return reg; // Dummy
5622 case Shader::PARAMETER_LOOP: return reg; // Dummy
5623 case Shader::PARAMETER_COLOROUT:
5624 reg = r.oC[i];
5625 break;
5626 case Shader::PARAMETER_DEPTHOUT:
5627 reg.x = r.oDepth;
5628 break;
John Bauman89401822014-05-06 15:04:28 -04005629 default:
5630 ASSERT(false);
5631 }
5632
5633 Float4 &x = reg[(src.swizzle >> 0) & 0x3];
5634 Float4 &y = reg[(src.swizzle >> 2) & 0x3];
5635 Float4 &z = reg[(src.swizzle >> 4) & 0x3];
5636 Float4 &w = reg[(src.swizzle >> 6) & 0x3];
5637
John Bauman19bac1e2014-05-06 15:23:49 -04005638 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -04005639
5640 switch(src.modifier)
5641 {
John Bauman19bac1e2014-05-06 15:23:49 -04005642 case Shader::MODIFIER_NONE:
John Bauman89401822014-05-06 15:04:28 -04005643 mod.x = x;
5644 mod.y = y;
5645 mod.z = z;
5646 mod.w = w;
5647 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005648 case Shader::MODIFIER_NEGATE:
John Bauman89401822014-05-06 15:04:28 -04005649 mod.x = -x;
5650 mod.y = -y;
5651 mod.z = -z;
5652 mod.w = -w;
5653 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005654 case Shader::MODIFIER_ABS:
John Bauman89401822014-05-06 15:04:28 -04005655 mod.x = Abs(x);
5656 mod.y = Abs(y);
5657 mod.z = Abs(z);
5658 mod.w = Abs(w);
5659 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005660 case Shader::MODIFIER_ABS_NEGATE:
John Bauman89401822014-05-06 15:04:28 -04005661 mod.x = -Abs(x);
5662 mod.y = -Abs(y);
5663 mod.z = -Abs(z);
5664 mod.w = -Abs(w);
5665 break;
5666 default:
5667 ASSERT(false);
5668 }
5669
5670 return mod;
5671 }
5672
John Bauman19bac1e2014-05-06 15:23:49 -04005673 Vector4f PixelRoutine::readConstant(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -04005674 {
John Bauman19bac1e2014-05-06 15:23:49 -04005675 Vector4f c;
5676
5677 int i = src.index + offset;
5678
5679 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
5680 {
5681 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[i]));
5682
5683 c.x = c.x.xxxx;
5684 c.y = c.y.yyyy;
5685 c.z = c.z.zzzz;
5686 c.w = c.w.wwww;
5687
5688 if(localShaderConstants) // Constant may be known at compile time
5689 {
5690 for(int j = 0; j < shader->getLength(); j++)
5691 {
5692 const Shader::Instruction &instruction = *shader->getInstruction(j);
5693
5694 if(instruction.opcode == Shader::OPCODE_DEF)
5695 {
5696 if(instruction.dst.index == i)
5697 {
5698 c.x = Float4(instruction.src[0].value[0]);
5699 c.y = Float4(instruction.src[0].value[1]);
5700 c.z = Float4(instruction.src[0].value[2]);
5701 c.w = Float4(instruction.src[0].value[3]);
5702
5703 break;
5704 }
5705 }
5706 }
5707 }
5708 }
5709 else if(src.rel.type == Shader::PARAMETER_LOOP)
5710 {
5711 Int loopCounter = r.aL[r.loopDepth];
5712
5713 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[i]) + loopCounter * 16);
5714
5715 c.x = c.x.xxxx;
5716 c.y = c.y.yyyy;
5717 c.z = c.z.zzzz;
5718 c.w = c.w.wwww;
5719 }
5720 else
5721 {
5722 Int a = relativeAddress(r, src);
5723
5724 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[i]) + a * 16);
5725
5726 c.x = c.x.xxxx;
5727 c.y = c.y.yyyy;
5728 c.z = c.z.zzzz;
5729 c.w = c.w.wwww;
5730 }
5731
5732 return c;
John Bauman89401822014-05-06 15:04:28 -04005733 }
5734
John Bauman19bac1e2014-05-06 15:23:49 -04005735 Int PixelRoutine::relativeAddress(Registers &r, const Shader::Parameter &var)
John Bauman89401822014-05-06 15:04:28 -04005736 {
John Bauman19bac1e2014-05-06 15:23:49 -04005737 ASSERT(var.rel.deterministic);
5738
5739 if(var.rel.type == Shader::PARAMETER_TEMP)
5740 {
5741 return RoundInt(Extract(r.rf[var.rel.index].x, 0)) * var.rel.scale;
5742 }
5743 else if(var.rel.type == Shader::PARAMETER_INPUT)
5744 {
5745 return RoundInt(Extract(r.vf[var.rel.index].x, 0)) * var.rel.scale;
5746 }
5747 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
5748 {
5749 return RoundInt(Extract(r.oC[var.rel.index].x, 0)) * var.rel.scale;
5750 }
5751 else if(var.rel.type == Shader::PARAMETER_CONST)
5752 {
5753 RValue<Float4> c = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[var.rel.index]));
5754
5755 return RoundInt(Extract(c, 0)) * var.rel.scale;
5756 }
5757 else ASSERT(false);
5758
5759 return 0;
5760 }
5761
5762 Int4 PixelRoutine::enableMask(Registers &r, const Shader::Instruction *instruction)
5763 {
5764 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
5765
5766 if(shader->containsBreakInstruction() && !whileTest && instruction->analysisBreak)
5767 {
5768 enable &= r.enableBreak;
5769 }
5770
5771 if(shader->containsContinueInstruction() && !whileTest && instruction->analysisContinue)
5772 {
5773 enable &= r.enableContinue;
5774 }
5775
5776 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
5777 {
5778 enable &= r.enableLeave;
5779 }
5780
5781 return enable;
5782 }
5783
5784 bool PixelRoutine::colorUsed()
5785 {
5786 return state.colorWriteMask || state.alphaTestActive() || state.shaderContainsKill;
5787 }
5788
5789 unsigned short PixelRoutine::shaderVersion() const
5790 {
5791 return shader ? shader->getVersion() : 0x0000;
5792 }
5793
5794 bool PixelRoutine::interpolateZ() const
5795 {
5796 return state.depthTestActive || state.pixelFogActive() || (shader && shader->vPosDeclared && fullPixelPositionRegister);
5797 }
5798
5799 bool PixelRoutine::interpolateW() const
5800 {
5801 return state.perspective || (shader && shader->vPosDeclared && fullPixelPositionRegister);
John Bauman89401822014-05-06 15:04:28 -04005802 }
5803}