blob: 06eb37f6ac1c8f5d7ea6e8f5589392d4cc955d36 [file] [log] [blame]
John Bauman89401822014-05-06 15:04:28 -04001// SwiftShader Software Renderer
2//
John Bauman66b8ab22014-05-06 15:57:45 -04003// Copyright(c) 2005-2013 TransGaming Inc.
John Bauman89401822014-05-06 15:04:28 -04004//
5// All rights reserved. No part of this software may be copied, distributed, transmitted,
6// transcribed, stored in a retrieval system, translated into any human or computer
7// language by any means, or disclosed to third parties without the explicit written
8// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9// or implied, including but not limited to any patent rights, are granted to you.
10//
11
12#include "PixelRoutine.hpp"
13
14#include "Renderer.hpp"
John Bauman89401822014-05-06 15:04:28 -040015#include "QuadRasterizer.hpp"
16#include "Surface.hpp"
17#include "Primitive.hpp"
18#include "CPUID.hpp"
19#include "SamplerCore.hpp"
20#include "Constants.hpp"
21#include "Debug.hpp"
22
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 Bauman89401822014-05-06 15:04:28 -040092 Float4 (&z)[4] = r.z;
John Bauman19bac1e2014-05-06 15:23:49 -040093 Float4 &w = r.w;
John Bauman89401822014-05-06 15:04:28 -040094 Float4 &rhw = r.rhw;
95 Float4 rhwCentroid;
96
97 Float4 xxxx = Float4(Float(x)) + *Pointer<Float4>(r.primitive + OFFSET(Primitive,xQuad), 16);
John Bauman89401822014-05-06 15:04:28 -040098
John Bauman19bac1e2014-05-06 15:23:49 -040099 if(interpolateZ())
John Bauman89401822014-05-06 15:04:28 -0400100 {
101 for(unsigned int q = 0; q < state.multiSample; q++)
102 {
103 Float4 x = xxxx;
104
105 if(state.multiSample > 1)
106 {
107 x -= *Pointer<Float4>(r.constants + OFFSET(Constants,X) + q * sizeof(float4));
108 }
109
110 z[q] = interpolate(x, r.Dz[q], z[q], r.primitive + OFFSET(Primitive,z), false, false);
111 }
112 }
113
114 Bool depthPass = false;
115
116 if(earlyDepthTest)
117 {
118 for(unsigned int q = 0; q < state.multiSample; q++)
119 {
120 depthPass = depthPass || depthTest(r, zBuffer, q, x, z[q], sMask[q], zMask[q], cMask[q]);
121 }
122 }
123
124 If(depthPass || Bool(!earlyDepthTest))
125 {
126 #if PERF_PROFILE
127 Long interpTime = Ticks();
128 #endif
129
Nicolas Capens66be2452015-01-27 14:58:57 -0500130 Float4 yyyy = Float4(Float(y)) + *Pointer<Float4>(r.primitive + OFFSET(Primitive,yQuad), 16);
Nicolas Capenscbefe532014-10-16 00:16:01 -0400131
John Bauman89401822014-05-06 15:04:28 -0400132 // Centroid locations
133 Float4 XXXX = Float4(0.0f);
134 Float4 YYYY = Float4(0.0f);
135
136 if(state.centroid)
137 {
138 Float4 WWWW(1.0e-9f);
139
140 for(unsigned int q = 0; q < state.multiSample; q++)
141 {
142 XXXX += *Pointer<Float4>(r.constants + OFFSET(Constants,sampleX[q]) + 16 * cMask[q]);
143 YYYY += *Pointer<Float4>(r.constants + OFFSET(Constants,sampleY[q]) + 16 * cMask[q]);
144 WWWW += *Pointer<Float4>(r.constants + OFFSET(Constants,weight) + 16 * cMask[q]);
145 }
146
147 WWWW = Rcp_pp(WWWW);
148 XXXX *= WWWW;
149 YYYY *= WWWW;
150
151 XXXX += xxxx;
152 YYYY += yyyy;
153 }
154
John Bauman19bac1e2014-05-06 15:23:49 -0400155 if(interpolateW())
John Bauman89401822014-05-06 15:04:28 -0400156 {
John Bauman19bac1e2014-05-06 15:23:49 -0400157 w = interpolate(xxxx, r.Dw, rhw, r.primitive + OFFSET(Primitive,w), false, false);
158 rhw = reciprocal(w);
John Bauman89401822014-05-06 15:04:28 -0400159
160 if(state.centroid)
161 {
162 rhwCentroid = reciprocal(interpolateCentroid(XXXX, YYYY, rhwCentroid, r.primitive + OFFSET(Primitive,w), false, false));
163 }
164 }
165
166 for(int interpolant = 0; interpolant < 10; interpolant++)
167 {
168 for(int component = 0; component < 4; component++)
169 {
John Bauman89401822014-05-06 15:04:28 -0400170 if(state.interpolant[interpolant].component & (1 << component))
171 {
172 if(!state.interpolant[interpolant].centroid)
173 {
John Bauman19bac1e2014-05-06 15:23:49 -0400174 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 -0400175 }
176 else
177 {
John Bauman19bac1e2014-05-06 15:23:49 -0400178 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 -0400179 }
180 }
181 }
182
183 Float4 rcp;
184
185 switch(state.interpolant[interpolant].project)
186 {
187 case 0:
188 break;
189 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400190 rcp = reciprocal(r.vf[interpolant].y);
191 r.vf[interpolant].x = r.vf[interpolant].x * rcp;
John Bauman89401822014-05-06 15:04:28 -0400192 break;
193 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400194 rcp = reciprocal(r.vf[interpolant].z);
195 r.vf[interpolant].x = r.vf[interpolant].x * rcp;
196 r.vf[interpolant].y = r.vf[interpolant].y * rcp;
John Bauman89401822014-05-06 15:04:28 -0400197 break;
198 case 3:
John Bauman19bac1e2014-05-06 15:23:49 -0400199 rcp = reciprocal(r.vf[interpolant].w);
200 r.vf[interpolant].x = r.vf[interpolant].x * rcp;
201 r.vf[interpolant].y = r.vf[interpolant].y * rcp;
202 r.vf[interpolant].z = r.vf[interpolant].z * rcp;
John Bauman89401822014-05-06 15:04:28 -0400203 break;
204 }
205 }
206
207 if(state.fog.component)
208 {
209 f = interpolate(xxxx, r.Df, rhw, r.primitive + OFFSET(Primitive,f), state.fog.flat & 0x01, state.perspective);
210 }
211
212 if(integerPipeline)
213 {
Nicolas Capenscbefe532014-10-16 00:16:01 -0400214 if(state.color[0].component & 0x1) r.diffuse.x = convertFixed12(r.vf[0].x); else r.diffuse.x = Short4(0x1000);
215 if(state.color[0].component & 0x2) r.diffuse.y = convertFixed12(r.vf[0].y); else r.diffuse.y = Short4(0x1000);
216 if(state.color[0].component & 0x4) r.diffuse.z = convertFixed12(r.vf[0].z); else r.diffuse.z = Short4(0x1000);
217 if(state.color[0].component & 0x8) r.diffuse.w = convertFixed12(r.vf[0].w); else r.diffuse.w = Short4(0x1000);
John Bauman89401822014-05-06 15:04:28 -0400218
Nicolas Capenscbefe532014-10-16 00:16:01 -0400219 if(state.color[1].component & 0x1) r.specular.x = convertFixed12(r.vf[1].x); else r.specular.x = Short4(0x0000, 0x0000, 0x0000, 0x0000);
220 if(state.color[1].component & 0x2) r.specular.y = convertFixed12(r.vf[1].y); else r.specular.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
221 if(state.color[1].component & 0x4) r.specular.z = convertFixed12(r.vf[1].z); else r.specular.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
222 if(state.color[1].component & 0x8) r.specular.w = convertFixed12(r.vf[1].w); else r.specular.w = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -0400223 }
John Bauman19bac1e2014-05-06 15:23:49 -0400224 else if(shaderVersion() >= 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400225 {
John Bauman19bac1e2014-05-06 15:23:49 -0400226 if(shader->vPosDeclared)
John Bauman89401822014-05-06 15:04:28 -0400227 {
John Bauman19bac1e2014-05-06 15:23:49 -0400228 if(!halfIntegerCoordinates)
229 {
230 r.vPos.x = Float4(Float(x)) + Float4(0, 1, 0, 1);
231 r.vPos.y = Float4(Float(y)) + Float4(0, 0, 1, 1);
232 }
233 else
234 {
235 r.vPos.x = Float4(Float(x)) + Float4(0.5f, 1.5f, 0.5f, 1.5f);
236 r.vPos.y = Float4(Float(y)) + Float4(0.5f, 0.5f, 1.5f, 1.5f);
237 }
238
239 if(fullPixelPositionRegister)
240 {
241 r.vPos.z = z[0]; // FIXME: Centroid?
242 r.vPos.w = w; // FIXME: Centroid?
243 }
John Bauman89401822014-05-06 15:04:28 -0400244 }
245
John Bauman19bac1e2014-05-06 15:23:49 -0400246 if(shader->vFaceDeclared)
John Bauman89401822014-05-06 15:04:28 -0400247 {
248 Float4 area = *Pointer<Float>(r.primitive + OFFSET(Primitive,area));
John Bauman66b8ab22014-05-06 15:57:45 -0400249 Float4 face = booleanFaceRegister ? Float4(As<Float4>(CmpNLT(area, Float4(0.0f)))) : area;
John Bauman19bac1e2014-05-06 15:23:49 -0400250
251 r.vFace.x = face;
252 r.vFace.y = face;
253 r.vFace.z = face;
254 r.vFace.w = face;
John Bauman89401822014-05-06 15:04:28 -0400255 }
256 }
257
258 #if PERF_PROFILE
259 r.cycles[PERF_INTERP] += Ticks() - interpTime;
260 #endif
261
262 Bool alphaPass = true;
263
264 if(colorUsed())
265 {
266 #if PERF_PROFILE
267 Long shaderTime = Ticks();
268 #endif
269
John Bauman19bac1e2014-05-06 15:23:49 -0400270 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400271 {
John Bauman19bac1e2014-05-06 15:23:49 -0400272 // shader->print("PixelShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -0400273
John Bauman19bac1e2014-05-06 15:23:49 -0400274 if(shader->getVersion() <= 0x0104)
John Bauman89401822014-05-06 15:04:28 -0400275 {
276 ps_1_x(r, cMask);
277 }
278 else
279 {
280 ps_2_x(r, cMask);
281 }
282 }
283 else
284 {
Nicolas Capenscbefe532014-10-16 00:16:01 -0400285 r.current = r.diffuse;
John Bauman19bac1e2014-05-06 15:23:49 -0400286 Vector4i temp(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -0400287
288 for(int stage = 0; stage < 8; stage++)
289 {
290 if(state.textureStage[stage].stageOperation == TextureStage::STAGE_DISABLE)
291 {
292 break;
293 }
294
John Bauman19bac1e2014-05-06 15:23:49 -0400295 Vector4i texture;
John Bauman89401822014-05-06 15:04:28 -0400296
297 if(state.textureStage[stage].usesTexture)
298 {
299 sampleTexture(r, texture, stage, stage);
300 }
301
Nicolas Capenscbefe532014-10-16 00:16:01 -0400302 blendTexture(r, temp, texture, stage);
John Bauman89401822014-05-06 15:04:28 -0400303 }
304
Nicolas Capenscbefe532014-10-16 00:16:01 -0400305 specularPixel(r.current, r.specular);
John Bauman89401822014-05-06 15:04:28 -0400306 }
307
308 #if PERF_PROFILE
309 r.cycles[PERF_SHADER] += Ticks() - shaderTime;
310 #endif
311
312 if(integerPipeline)
313 {
Nicolas Capenscbefe532014-10-16 00:16:01 -0400314 r.current.x = Min(r.current.x, Short4(0x0FFF, 0x0FFF, 0x0FFF, 0x0FFF)); r.current.x = Max(r.current.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));
315 r.current.y = Min(r.current.y, Short4(0x0FFF, 0x0FFF, 0x0FFF, 0x0FFF)); r.current.y = Max(r.current.y, Short4(0x0000, 0x0000, 0x0000, 0x0000));
316 r.current.z = Min(r.current.z, Short4(0x0FFF, 0x0FFF, 0x0FFF, 0x0FFF)); r.current.z = Max(r.current.z, Short4(0x0000, 0x0000, 0x0000, 0x0000));
317 r.current.w = Min(r.current.w, Short4(0x0FFF, 0x0FFF, 0x0FFF, 0x0FFF)); r.current.w = Max(r.current.w, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman89401822014-05-06 15:04:28 -0400318
Nicolas Capenscbefe532014-10-16 00:16:01 -0400319 alphaPass = alphaTest(r, cMask, r.current);
John Bauman89401822014-05-06 15:04:28 -0400320 }
321 else
322 {
323 clampColor(r.oC);
324
325 alphaPass = alphaTest(r, cMask, r.oC[0]);
326 }
327
John Bauman19bac1e2014-05-06 15:23:49 -0400328 if((shader && shader->containsKill()) || state.alphaTestActive())
John Bauman89401822014-05-06 15:04:28 -0400329 {
330 for(unsigned int q = 0; q < state.multiSample; q++)
331 {
332 zMask[q] &= cMask[q];
333 sMask[q] &= cMask[q];
334 }
335 }
336 }
337
338 If(alphaPass)
339 {
340 if(!earlyDepthTest)
341 {
342 for(unsigned int q = 0; q < state.multiSample; q++)
343 {
344 depthPass = depthPass || depthTest(r, zBuffer, q, x, z[q], sMask[q], zMask[q], cMask[q]);
345 }
346 }
347
348 #if PERF_PROFILE
349 Long ropTime = Ticks();
350 #endif
351
352 If(depthPass || Bool(earlyDepthTest))
353 {
354 for(unsigned int q = 0; q < state.multiSample; q++)
355 {
356 if(state.multiSampleMask & (1 << q))
357 {
358 writeDepth(r, zBuffer, q, x, z[q], zMask[q]);
359
360 if(state.occlusionEnabled)
361 {
362 r.occlusion += *Pointer<UInt>(r.constants + OFFSET(Constants,occlusionCount) + 4 * (zMask[q] & sMask[q]));
363 }
364 }
365 }
366
367 if(colorUsed())
368 {
369 #if PERF_PROFILE
John Bauman66b8ab22014-05-06 15:57:45 -0400370 AddAtomic(Pointer<Long>(&profiler.ropOperations), 4);
John Bauman89401822014-05-06 15:04:28 -0400371 #endif
372
373 if(integerPipeline)
374 {
Nicolas Capenscbefe532014-10-16 00:16:01 -0400375 rasterOperation(r.current, r, f, cBuffer[0], x, sMask, zMask, cMask);
John Bauman89401822014-05-06 15:04:28 -0400376 }
377 else
378 {
379 rasterOperation(r.oC, r, f, cBuffer, x, sMask, zMask, cMask);
380 }
381 }
382 }
383
384 #if PERF_PROFILE
385 r.cycles[PERF_ROP] += Ticks() - ropTime;
386 #endif
387 }
388 }
389
390 for(unsigned int q = 0; q < state.multiSample; q++)
391 {
392 if(state.multiSampleMask & (1 << q))
393 {
394 writeStencil(r, sBuffer, q, x, sMask[q], zMask[q], cMask[q]);
395 }
396 }
397
398 #if PERF_PROFILE
399 r.cycles[PERF_PIPE] += Ticks() - pipeTime;
400 #endif
401 }
402
403 Float4 PixelRoutine::interpolate(Float4 &x, Float4 &D, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective)
404 {
405 Float4 interpolant = D;
406
407 if(!flat)
408 {
409 interpolant += x * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,A), 16);
410
411 if(perspective)
412 {
413 interpolant *= rhw;
414 }
415 }
416
417 return interpolant;
418 }
419
420 Float4 PixelRoutine::interpolateCentroid(Float4 &x, Float4 &y, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective)
421 {
422 Float4 interpolant = *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,C), 16);
423
424 if(!flat)
425 {
426 interpolant += x * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,A), 16) +
427 y * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,B), 16);
428
429 if(perspective)
430 {
431 interpolant *= rhw;
432 }
433 }
434
435 return interpolant;
436 }
437
438 void PixelRoutine::stencilTest(Registers &r, Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &cMask)
439 {
440 if(!state.stencilActive)
441 {
442 return;
443 }
444
445 // (StencilRef & StencilMask) CompFunc (StencilBufferValue & StencilMask)
446
447 Pointer<Byte> buffer = sBuffer + 2 * x;
448
449 if(q > 0)
450 {
451 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,stencilSliceB));
452 }
453
454 Byte8 value = As<Byte8>(Long1(*Pointer<UInt>(buffer)));
455 Byte8 valueCCW = value;
456
457 if(!state.noStencilMask)
458 {
459 value &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[0].testMaskQ));
460 }
461
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400462 stencilTest(r, value, state.stencilCompareMode, false);
John Bauman89401822014-05-06 15:04:28 -0400463
464 if(state.twoSidedStencil)
465 {
466 if(!state.noStencilMaskCCW)
467 {
468 valueCCW &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[1].testMaskQ));
469 }
470
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400471 stencilTest(r, valueCCW, state.stencilCompareModeCCW, true);
John Bauman89401822014-05-06 15:04:28 -0400472
473 value &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,clockwiseMask));
474 valueCCW &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,invClockwiseMask));
475 value |= valueCCW;
476 }
477
478 sMask = SignMask(value) & cMask;
479 }
480
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400481 void PixelRoutine::stencilTest(Registers &r, Byte8 &value, StencilCompareMode stencilCompareMode, bool CCW)
John Bauman89401822014-05-06 15:04:28 -0400482 {
483 Byte8 equal;
484
485 switch(stencilCompareMode)
486 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400487 case STENCIL_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400488 value = Byte8(0xFFFFFFFFFFFFFFFF);
489 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400490 case STENCIL_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400491 value = Byte8(0x0000000000000000);
492 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400493 case STENCIL_LESS: // a < b ~ b > a
John Bauman89401822014-05-06 15:04:28 -0400494 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
495 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
496 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400497 case STENCIL_EQUAL:
John Bauman89401822014-05-06 15:04:28 -0400498 value = CmpEQ(value, *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
499 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400500 case STENCIL_NOTEQUAL: // a != b ~ !(a == b)
John Bauman89401822014-05-06 15:04:28 -0400501 value = CmpEQ(value, *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
502 value ^= Byte8(0xFFFFFFFFFFFFFFFF);
503 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400504 case STENCIL_LESSEQUAL: // a <= b ~ (b > a) || (a == b)
John Bauman89401822014-05-06 15:04:28 -0400505 equal = value;
506 equal = CmpEQ(equal, *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
507 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
508 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
509 value |= equal;
510 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400511 case STENCIL_GREATER: // a > b
John Bauman89401822014-05-06 15:04:28 -0400512 equal = *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ));
513 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
514 equal = CmpGT(As<SByte8>(equal), As<SByte8>(value));
515 value = equal;
516 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400517 case STENCIL_GREATEREQUAL: // a >= b ~ !(a < b) ~ !(b > a)
John Bauman89401822014-05-06 15:04:28 -0400518 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
519 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
520 value ^= Byte8(0xFFFFFFFFFFFFFFFF);
521 break;
522 default:
523 ASSERT(false);
524 }
525 }
526
527 Bool PixelRoutine::depthTest(Registers &r, Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &sMask, Int &zMask, Int &cMask)
528 {
529 if(!state.depthTestActive)
530 {
531 return true;
532 }
533
534 Float4 Z = z;
535
John Bauman19bac1e2014-05-06 15:23:49 -0400536 if(shader && shader->depthOverride())
John Bauman89401822014-05-06 15:04:28 -0400537 {
538 if(complementaryDepthBuffer)
539 {
John Bauman19bac1e2014-05-06 15:23:49 -0400540 Z = Float4(1.0f) - r.oDepth;
John Bauman89401822014-05-06 15:04:28 -0400541 }
542 else
543 {
544 Z = r.oDepth;
545 }
546 }
547
548 Pointer<Byte> buffer;
549 Int pitch;
550
551 if(!state.quadLayoutDepthBuffer)
552 {
553 buffer = zBuffer + 4 * x;
554 pitch = *Pointer<Int>(r.data + OFFSET(DrawData,depthPitchB));
555 }
556 else
557 {
558 buffer = zBuffer + 8 * x;
559 }
560
561 if(q > 0)
562 {
563 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,depthSliceB));
564 }
565
566 Float4 zValue;
567
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400568 if(state.depthCompareMode != DEPTH_NEVER || (state.depthCompareMode != DEPTH_ALWAYS && !state.depthWriteEnable))
John Bauman89401822014-05-06 15:04:28 -0400569 {
570 if(!state.quadLayoutDepthBuffer)
571 {
572 // FIXME: Properly optimizes?
573 zValue.xy = *Pointer<Float4>(buffer);
574 zValue.zw = *Pointer<Float4>(buffer + pitch - 8);
575 }
576 else
577 {
578 zValue = *Pointer<Float4>(buffer, 16);
579 }
580 }
581
582 Int4 zTest;
583
584 switch(state.depthCompareMode)
585 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400586 case DEPTH_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400587 // Optimized
588 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400589 case DEPTH_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400590 // Optimized
591 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400592 case DEPTH_EQUAL:
John Bauman89401822014-05-06 15:04:28 -0400593 zTest = CmpEQ(zValue, Z);
594 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400595 case DEPTH_NOTEQUAL:
John Bauman89401822014-05-06 15:04:28 -0400596 zTest = CmpNEQ(zValue, Z);
597 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400598 case DEPTH_LESS:
John Bauman89401822014-05-06 15:04:28 -0400599 if(complementaryDepthBuffer)
600 {
601 zTest = CmpLT(zValue, Z);
602 }
603 else
604 {
605 zTest = CmpNLE(zValue, Z);
606 }
607 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400608 case DEPTH_GREATEREQUAL:
John Bauman89401822014-05-06 15:04:28 -0400609 if(complementaryDepthBuffer)
610 {
611 zTest = CmpNLT(zValue, Z);
612 }
613 else
614 {
615 zTest = CmpLE(zValue, Z);
616 }
617 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400618 case DEPTH_LESSEQUAL:
John Bauman89401822014-05-06 15:04:28 -0400619 if(complementaryDepthBuffer)
620 {
621 zTest = CmpLE(zValue, Z);
622 }
623 else
624 {
625 zTest = CmpNLT(zValue, Z);
626 }
627 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400628 case DEPTH_GREATER:
John Bauman89401822014-05-06 15:04:28 -0400629 if(complementaryDepthBuffer)
630 {
631 zTest = CmpNLE(zValue, Z);
632 }
633 else
634 {
635 zTest = CmpLT(zValue, Z);
636 }
637 break;
638 default:
639 ASSERT(false);
640 }
641
642 switch(state.depthCompareMode)
643 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400644 case DEPTH_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400645 zMask = cMask;
646 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400647 case DEPTH_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400648 zMask = 0x0;
649 break;
650 default:
651 zMask = SignMask(zTest) & cMask;
652 break;
653 }
654
655 if(state.stencilActive)
656 {
657 zMask &= sMask;
658 }
659
660 return zMask != 0;
661 }
662
Nicolas Capenscbefe532014-10-16 00:16:01 -0400663 void PixelRoutine::blendTexture(Registers &r, Vector4i &temp, Vector4i &texture, int stage)
John Bauman89401822014-05-06 15:04:28 -0400664 {
John Bauman19bac1e2014-05-06 15:23:49 -0400665 Vector4i *arg1;
666 Vector4i *arg2;
667 Vector4i *arg3;
668 Vector4i res;
John Bauman89401822014-05-06 15:04:28 -0400669
John Bauman19bac1e2014-05-06 15:23:49 -0400670 Vector4i constant;
671 Vector4i tfactor;
John Bauman89401822014-05-06 15:04:28 -0400672
673 const TextureStage::State &textureStage = state.textureStage[stage];
674
675 if(textureStage.firstArgument == TextureStage::SOURCE_CONSTANT ||
676 textureStage.firstArgumentAlpha == TextureStage::SOURCE_CONSTANT ||
677 textureStage.secondArgument == TextureStage::SOURCE_CONSTANT ||
678 textureStage.secondArgumentAlpha == TextureStage::SOURCE_CONSTANT ||
679 textureStage.thirdArgument == TextureStage::SOURCE_CONSTANT ||
680 textureStage.thirdArgumentAlpha == TextureStage::SOURCE_CONSTANT)
681 {
John Bauman19bac1e2014-05-06 15:23:49 -0400682 constant.x = *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].constantColor4[0]));
683 constant.y = *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].constantColor4[1]));
684 constant.z = *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].constantColor4[2]));
685 constant.w = *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].constantColor4[3]));
John Bauman89401822014-05-06 15:04:28 -0400686 }
687
688 if(textureStage.firstArgument == TextureStage::SOURCE_TFACTOR ||
689 textureStage.firstArgumentAlpha == TextureStage::SOURCE_TFACTOR ||
690 textureStage.secondArgument == TextureStage::SOURCE_TFACTOR ||
691 textureStage.secondArgumentAlpha == TextureStage::SOURCE_TFACTOR ||
692 textureStage.thirdArgument == TextureStage::SOURCE_TFACTOR ||
693 textureStage.thirdArgumentAlpha == TextureStage::SOURCE_TFACTOR)
694 {
John Bauman19bac1e2014-05-06 15:23:49 -0400695 tfactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[0]));
696 tfactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[1]));
697 tfactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[2]));
698 tfactor.w = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[3]));
John Bauman89401822014-05-06 15:04:28 -0400699 }
700
701 // Premodulate
702 if(stage > 0 && textureStage.usesTexture)
703 {
704 if(state.textureStage[stage - 1].stageOperation == TextureStage::STAGE_PREMODULATE)
705 {
Nicolas Capenscbefe532014-10-16 00:16:01 -0400706 r.current.x = MulHigh(r.current.x, texture.x) << 4;
707 r.current.y = MulHigh(r.current.y, texture.y) << 4;
708 r.current.z = MulHigh(r.current.z, texture.z) << 4;
John Bauman89401822014-05-06 15:04:28 -0400709 }
710
711 if(state.textureStage[stage - 1].stageOperationAlpha == TextureStage::STAGE_PREMODULATE)
712 {
Nicolas Capenscbefe532014-10-16 00:16:01 -0400713 r.current.w = MulHigh(r.current.w, texture.w) << 4;
John Bauman89401822014-05-06 15:04:28 -0400714 }
715 }
716
717 if(luminance)
718 {
John Bauman19bac1e2014-05-06 15:23:49 -0400719 texture.x = MulHigh(texture.x, r.L) << 4;
720 texture.y = MulHigh(texture.y, r.L) << 4;
721 texture.z = MulHigh(texture.z, r.L) << 4;
John Bauman89401822014-05-06 15:04:28 -0400722
723 luminance = false;
724 }
725
726 switch(textureStage.firstArgument)
727 {
728 case TextureStage::SOURCE_TEXTURE: arg1 = &texture; break;
729 case TextureStage::SOURCE_CONSTANT: arg1 = &constant; break;
Nicolas Capenscbefe532014-10-16 00:16:01 -0400730 case TextureStage::SOURCE_CURRENT: arg1 = &r.current; break;
John Bauman89401822014-05-06 15:04:28 -0400731 case TextureStage::SOURCE_DIFFUSE: arg1 = &r.diffuse; break;
732 case TextureStage::SOURCE_SPECULAR: arg1 = &r.specular; break;
733 case TextureStage::SOURCE_TEMP: arg1 = &temp; break;
734 case TextureStage::SOURCE_TFACTOR: arg1 = &tfactor; break;
735 default:
736 ASSERT(false);
737 }
738
739 switch(textureStage.secondArgument)
740 {
741 case TextureStage::SOURCE_TEXTURE: arg2 = &texture; break;
742 case TextureStage::SOURCE_CONSTANT: arg2 = &constant; break;
Nicolas Capenscbefe532014-10-16 00:16:01 -0400743 case TextureStage::SOURCE_CURRENT: arg2 = &r.current; break;
John Bauman89401822014-05-06 15:04:28 -0400744 case TextureStage::SOURCE_DIFFUSE: arg2 = &r.diffuse; break;
745 case TextureStage::SOURCE_SPECULAR: arg2 = &r.specular; break;
746 case TextureStage::SOURCE_TEMP: arg2 = &temp; break;
747 case TextureStage::SOURCE_TFACTOR: arg2 = &tfactor; break;
748 default:
749 ASSERT(false);
750 }
751
752 switch(textureStage.thirdArgument)
753 {
754 case TextureStage::SOURCE_TEXTURE: arg3 = &texture; break;
755 case TextureStage::SOURCE_CONSTANT: arg3 = &constant; break;
Nicolas Capenscbefe532014-10-16 00:16:01 -0400756 case TextureStage::SOURCE_CURRENT: arg3 = &r.current; break;
John Bauman89401822014-05-06 15:04:28 -0400757 case TextureStage::SOURCE_DIFFUSE: arg3 = &r.diffuse; break;
758 case TextureStage::SOURCE_SPECULAR: arg3 = &r.specular; break;
759 case TextureStage::SOURCE_TEMP: arg3 = &temp; break;
760 case TextureStage::SOURCE_TFACTOR: arg3 = &tfactor; break;
761 default:
762 ASSERT(false);
763 }
764
John Bauman19bac1e2014-05-06 15:23:49 -0400765 Vector4i mod1;
766 Vector4i mod2;
767 Vector4i mod3;
John Bauman89401822014-05-06 15:04:28 -0400768
769 switch(textureStage.firstModifier)
770 {
771 case TextureStage::MODIFIER_COLOR:
772 break;
773 case TextureStage::MODIFIER_INVCOLOR:
774 {
John Bauman19bac1e2014-05-06 15:23:49 -0400775 mod1.x = SubSat(Short4(0x1000), arg1->x);
776 mod1.y = SubSat(Short4(0x1000), arg1->y);
777 mod1.z = SubSat(Short4(0x1000), arg1->z);
778 mod1.w = SubSat(Short4(0x1000), arg1->w);
John Bauman89401822014-05-06 15:04:28 -0400779
780 arg1 = &mod1;
781 }
782 break;
783 case TextureStage::MODIFIER_ALPHA:
784 {
John Bauman19bac1e2014-05-06 15:23:49 -0400785 mod1.x = arg1->w;
786 mod1.y = arg1->w;
787 mod1.z = arg1->w;
788 mod1.w = arg1->w;
John Bauman89401822014-05-06 15:04:28 -0400789
790 arg1 = &mod1;
791 }
792 break;
793 case TextureStage::MODIFIER_INVALPHA:
794 {
John Bauman19bac1e2014-05-06 15:23:49 -0400795 mod1.x = SubSat(Short4(0x1000), arg1->w);
796 mod1.y = SubSat(Short4(0x1000), arg1->w);
797 mod1.z = SubSat(Short4(0x1000), arg1->w);
798 mod1.w = SubSat(Short4(0x1000), arg1->w);
John Bauman89401822014-05-06 15:04:28 -0400799
800 arg1 = &mod1;
801 }
802 break;
803 default:
804 ASSERT(false);
805 }
806
807 switch(textureStage.secondModifier)
808 {
809 case TextureStage::MODIFIER_COLOR:
810 break;
811 case TextureStage::MODIFIER_INVCOLOR:
812 {
John Bauman19bac1e2014-05-06 15:23:49 -0400813 mod2.x = SubSat(Short4(0x1000), arg2->x);
814 mod2.y = SubSat(Short4(0x1000), arg2->y);
815 mod2.z = SubSat(Short4(0x1000), arg2->z);
816 mod2.w = SubSat(Short4(0x1000), arg2->w);
John Bauman89401822014-05-06 15:04:28 -0400817
818 arg2 = &mod2;
819 }
820 break;
821 case TextureStage::MODIFIER_ALPHA:
822 {
John Bauman19bac1e2014-05-06 15:23:49 -0400823 mod2.x = arg2->w;
824 mod2.y = arg2->w;
825 mod2.z = arg2->w;
826 mod2.w = arg2->w;
John Bauman89401822014-05-06 15:04:28 -0400827
828 arg2 = &mod2;
829 }
830 break;
831 case TextureStage::MODIFIER_INVALPHA:
832 {
John Bauman19bac1e2014-05-06 15:23:49 -0400833 mod2.x = SubSat(Short4(0x1000), arg2->w);
834 mod2.y = SubSat(Short4(0x1000), arg2->w);
835 mod2.z = SubSat(Short4(0x1000), arg2->w);
836 mod2.w = SubSat(Short4(0x1000), arg2->w);
John Bauman89401822014-05-06 15:04:28 -0400837
838 arg2 = &mod2;
839 }
840 break;
841 default:
842 ASSERT(false);
843 }
844
845 switch(textureStage.thirdModifier)
846 {
847 case TextureStage::MODIFIER_COLOR:
848 break;
849 case TextureStage::MODIFIER_INVCOLOR:
850 {
John Bauman19bac1e2014-05-06 15:23:49 -0400851 mod3.x = SubSat(Short4(0x1000), arg3->x);
852 mod3.y = SubSat(Short4(0x1000), arg3->y);
853 mod3.z = SubSat(Short4(0x1000), arg3->z);
854 mod3.w = SubSat(Short4(0x1000), arg3->w);
John Bauman89401822014-05-06 15:04:28 -0400855
856 arg3 = &mod3;
857 }
858 break;
859 case TextureStage::MODIFIER_ALPHA:
860 {
John Bauman19bac1e2014-05-06 15:23:49 -0400861 mod3.x = arg3->w;
862 mod3.y = arg3->w;
863 mod3.z = arg3->w;
864 mod3.w = arg3->w;
John Bauman89401822014-05-06 15:04:28 -0400865
866 arg3 = &mod3;
867 }
868 break;
869 case TextureStage::MODIFIER_INVALPHA:
870 {
John Bauman19bac1e2014-05-06 15:23:49 -0400871 mod3.x = SubSat(Short4(0x1000), arg3->w);
872 mod3.y = SubSat(Short4(0x1000), arg3->w);
873 mod3.z = SubSat(Short4(0x1000), arg3->w);
874 mod3.w = SubSat(Short4(0x1000), arg3->w);
John Bauman89401822014-05-06 15:04:28 -0400875
876 arg3 = &mod3;
877 }
878 break;
879 default:
880 ASSERT(false);
881 }
882
883 switch(textureStage.stageOperation)
884 {
885 case TextureStage::STAGE_DISABLE:
886 break;
887 case TextureStage::STAGE_SELECTARG1: // Arg1
888 {
John Bauman19bac1e2014-05-06 15:23:49 -0400889 res.x = arg1->x;
890 res.y = arg1->y;
891 res.z = arg1->z;
John Bauman89401822014-05-06 15:04:28 -0400892 }
893 break;
894 case TextureStage::STAGE_SELECTARG2: // Arg2
895 {
John Bauman19bac1e2014-05-06 15:23:49 -0400896 res.x = arg2->x;
897 res.y = arg2->y;
898 res.z = arg2->z;
John Bauman89401822014-05-06 15:04:28 -0400899 }
900 break;
901 case TextureStage::STAGE_SELECTARG3: // Arg3
902 {
John Bauman19bac1e2014-05-06 15:23:49 -0400903 res.x = arg3->x;
904 res.y = arg3->y;
905 res.z = arg3->z;
John Bauman89401822014-05-06 15:04:28 -0400906 }
907 break;
908 case TextureStage::STAGE_MODULATE: // Arg1 * Arg2
909 {
John Bauman19bac1e2014-05-06 15:23:49 -0400910 res.x = MulHigh(arg1->x, arg2->x) << 4;
911 res.y = MulHigh(arg1->y, arg2->y) << 4;
912 res.z = MulHigh(arg1->z, arg2->z) << 4;
John Bauman89401822014-05-06 15:04:28 -0400913 }
914 break;
915 case TextureStage::STAGE_MODULATE2X: // Arg1 * Arg2 * 2
916 {
John Bauman19bac1e2014-05-06 15:23:49 -0400917 res.x = MulHigh(arg1->x, arg2->x) << 5;
918 res.y = MulHigh(arg1->y, arg2->y) << 5;
919 res.z = MulHigh(arg1->z, arg2->z) << 5;
John Bauman89401822014-05-06 15:04:28 -0400920 }
921 break;
922 case TextureStage::STAGE_MODULATE4X: // Arg1 * Arg2 * 4
923 {
John Bauman19bac1e2014-05-06 15:23:49 -0400924 res.x = MulHigh(arg1->x, arg2->x) << 6;
925 res.y = MulHigh(arg1->y, arg2->y) << 6;
926 res.z = MulHigh(arg1->z, arg2->z) << 6;
John Bauman89401822014-05-06 15:04:28 -0400927 }
928 break;
929 case TextureStage::STAGE_ADD: // Arg1 + Arg2
930 {
John Bauman19bac1e2014-05-06 15:23:49 -0400931 res.x = AddSat(arg1->x, arg2->x);
932 res.y = AddSat(arg1->y, arg2->y);
933 res.z = AddSat(arg1->z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400934 }
935 break;
936 case TextureStage::STAGE_ADDSIGNED: // Arg1 + Arg2 - 0.5
937 {
John Bauman19bac1e2014-05-06 15:23:49 -0400938 res.x = AddSat(arg1->x, arg2->x);
939 res.y = AddSat(arg1->y, arg2->y);
940 res.z = AddSat(arg1->z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400941
John Bauman19bac1e2014-05-06 15:23:49 -0400942 res.x = SubSat(res.x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
943 res.y = SubSat(res.y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
944 res.z = SubSat(res.z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -0400945 }
946 break;
947 case TextureStage::STAGE_ADDSIGNED2X: // (Arg1 + Arg2 - 0.5) << 1
948 {
John Bauman19bac1e2014-05-06 15:23:49 -0400949 res.x = AddSat(arg1->x, arg2->x);
950 res.y = AddSat(arg1->y, arg2->y);
951 res.z = AddSat(arg1->z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400952
John Bauman19bac1e2014-05-06 15:23:49 -0400953 res.x = SubSat(res.x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
954 res.y = SubSat(res.y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
955 res.z = SubSat(res.z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -0400956
John Bauman19bac1e2014-05-06 15:23:49 -0400957 res.x = AddSat(res.x, res.x);
958 res.y = AddSat(res.y, res.y);
959 res.z = AddSat(res.z, res.z);
John Bauman89401822014-05-06 15:04:28 -0400960 }
961 break;
962 case TextureStage::STAGE_SUBTRACT: // Arg1 - Arg2
963 {
John Bauman19bac1e2014-05-06 15:23:49 -0400964 res.x = SubSat(arg1->x, arg2->x);
965 res.y = SubSat(arg1->y, arg2->y);
966 res.z = SubSat(arg1->z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400967 }
968 break;
969 case TextureStage::STAGE_ADDSMOOTH: // Arg1 + Arg2 - Arg1 * Arg2
970 {
971 Short4 tmp;
972
John Bauman19bac1e2014-05-06 15:23:49 -0400973 tmp = MulHigh(arg1->x, arg2->x) << 4; res.x = AddSat(arg1->x, arg2->x); res.x = SubSat(res.x, tmp);
974 tmp = MulHigh(arg1->y, arg2->y) << 4; res.y = AddSat(arg1->y, arg2->y); res.y = SubSat(res.y, tmp);
975 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 -0400976 }
977 break;
978 case TextureStage::STAGE_MULTIPLYADD: // Arg3 + Arg1 * Arg2
979 {
John Bauman19bac1e2014-05-06 15:23:49 -0400980 res.x = MulHigh(arg1->x, arg2->x) << 4; res.x = AddSat(res.x, arg3->x);
981 res.y = MulHigh(arg1->y, arg2->y) << 4; res.y = AddSat(res.y, arg3->y);
982 res.z = MulHigh(arg1->z, arg2->z) << 4; res.z = AddSat(res.z, arg3->z);
John Bauman89401822014-05-06 15:04:28 -0400983 }
984 break;
985 case TextureStage::STAGE_LERP: // Arg3 * (Arg1 - Arg2) + Arg2
986 {
John Bauman19bac1e2014-05-06 15:23:49 -0400987 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, arg3->x) << 4; res.x = AddSat(res.x, arg2->x);
988 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, arg3->y) << 4; res.y = AddSat(res.y, arg2->y);
989 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 -0400990 }
991 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400992 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 -0400993 {
994 Short4 tmp;
995
John Bauman19bac1e2014-05-06 15:23:49 -0400996 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);
997 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);
998 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 -0400999
John Bauman19bac1e2014-05-06 15:23:49 -04001000 res.x = res.x << 6;
1001 res.y = res.y << 6;
1002 res.z = res.z << 6;
John Bauman89401822014-05-06 15:04:28 -04001003
John Bauman19bac1e2014-05-06 15:23:49 -04001004 res.x = AddSat(res.x, res.y);
1005 res.x = AddSat(res.x, res.z);
John Bauman89401822014-05-06 15:04:28 -04001006
1007 // Clamp to [0, 1]
John Bauman19bac1e2014-05-06 15:23:49 -04001008 res.x = Max(res.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));
1009 res.x = Min(res.x, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001010
John Bauman19bac1e2014-05-06 15:23:49 -04001011 res.y = res.x;
1012 res.z = res.x;
1013 res.w = res.x;
John Bauman89401822014-05-06 15:04:28 -04001014 }
1015 break;
1016 case TextureStage::STAGE_BLENDCURRENTALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1017 {
Nicolas Capenscbefe532014-10-16 00:16:01 -04001018 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, r.current.w) << 4; res.x = AddSat(res.x, arg2->x);
1019 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, r.current.w) << 4; res.y = AddSat(res.y, arg2->y);
1020 res.z = SubSat(arg1->z, arg2->z); res.z = MulHigh(res.z, r.current.w) << 4; res.z = AddSat(res.z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -04001021 }
1022 break;
1023 case TextureStage::STAGE_BLENDDIFFUSEALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1024 {
John Bauman19bac1e2014-05-06 15:23:49 -04001025 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, r.diffuse.w) << 4; res.x = AddSat(res.x, arg2->x);
1026 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, r.diffuse.w) << 4; res.y = AddSat(res.y, arg2->y);
1027 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 -04001028 }
1029 break;
1030 case TextureStage::STAGE_BLENDFACTORALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1031 {
John Bauman19bac1e2014-05-06 15:23:49 -04001032 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);
1033 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);
1034 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 -04001035 }
1036 break;
1037 case TextureStage::STAGE_BLENDTEXTUREALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1038 {
John Bauman19bac1e2014-05-06 15:23:49 -04001039 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, texture.w) << 4; res.x = AddSat(res.x, arg2->x);
1040 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, texture.w) << 4; res.y = AddSat(res.y, arg2->y);
1041 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 -04001042 }
1043 break;
1044 case TextureStage::STAGE_BLENDTEXTUREALPHAPM: // Arg1 + Arg2 * (1 - Alpha)
1045 {
John Bauman19bac1e2014-05-06 15:23:49 -04001046 res.x = SubSat(Short4(0x1000), texture.w); res.x = MulHigh(res.x, arg2->x) << 4; res.x = AddSat(res.x, arg1->x);
1047 res.y = SubSat(Short4(0x1000), texture.w); res.y = MulHigh(res.y, arg2->y) << 4; res.y = AddSat(res.y, arg1->y);
1048 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 -04001049 }
1050 break;
1051 case TextureStage::STAGE_PREMODULATE:
1052 {
John Bauman19bac1e2014-05-06 15:23:49 -04001053 res.x = arg1->x;
1054 res.y = arg1->y;
1055 res.z = arg1->z;
John Bauman89401822014-05-06 15:04:28 -04001056 }
1057 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001058 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR: // Arg1 + Arg1.w * Arg2
John Bauman89401822014-05-06 15:04:28 -04001059 {
John Bauman19bac1e2014-05-06 15:23:49 -04001060 res.x = MulHigh(arg1->w, arg2->x) << 4; res.x = AddSat(res.x, arg1->x);
1061 res.y = MulHigh(arg1->w, arg2->y) << 4; res.y = AddSat(res.y, arg1->y);
1062 res.z = MulHigh(arg1->w, arg2->z) << 4; res.z = AddSat(res.z, arg1->z);
John Bauman89401822014-05-06 15:04:28 -04001063 }
1064 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001065 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA: // Arg1 * Arg2 + Arg1.w
John Bauman89401822014-05-06 15:04:28 -04001066 {
John Bauman19bac1e2014-05-06 15:23:49 -04001067 res.x = MulHigh(arg1->x, arg2->x) << 4; res.x = AddSat(res.x, arg1->w);
1068 res.y = MulHigh(arg1->y, arg2->y) << 4; res.y = AddSat(res.y, arg1->w);
1069 res.z = MulHigh(arg1->z, arg2->z) << 4; res.z = AddSat(res.z, arg1->w);
John Bauman89401822014-05-06 15:04:28 -04001070 }
1071 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001072 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR: // (1 - Arg1.w) * Arg2 + Arg1
John Bauman89401822014-05-06 15:04:28 -04001073 {
1074 Short4 tmp;
1075
John Bauman19bac1e2014-05-06 15:23:49 -04001076 res.x = AddSat(arg1->x, arg2->x); tmp = MulHigh(arg1->w, arg2->x) << 4; res.x = SubSat(res.x, tmp);
1077 res.y = AddSat(arg1->y, arg2->y); tmp = MulHigh(arg1->w, arg2->y) << 4; res.y = SubSat(res.y, tmp);
1078 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 -04001079 }
1080 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001081 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA: // (1 - Arg1) * Arg2 + Arg1.w
John Bauman89401822014-05-06 15:04:28 -04001082 {
1083 Short4 tmp;
1084
John Bauman19bac1e2014-05-06 15:23:49 -04001085 res.x = AddSat(arg1->w, arg2->x); tmp = MulHigh(arg1->x, arg2->x) << 4; res.x = SubSat(res.x, tmp);
1086 res.y = AddSat(arg1->w, arg2->y); tmp = MulHigh(arg1->y, arg2->y) << 4; res.y = SubSat(res.y, tmp);
1087 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 -04001088 }
1089 break;
1090 case TextureStage::STAGE_BUMPENVMAP:
1091 {
John Bauman19bac1e2014-05-06 15:23:49 -04001092 r.du = Float4(texture.x) * Float4(1.0f / 0x0FE0);
1093 r.dv = Float4(texture.y) * Float4(1.0f / 0x0FE0);
John Bauman89401822014-05-06 15:04:28 -04001094
1095 Float4 du2;
1096 Float4 dv2;
1097
1098 du2 = r.du;
1099 dv2 = r.dv;
1100 r.du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
1101 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
1102 r.du += dv2;
1103 r.dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
1104 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
1105 r.dv += du2;
1106
1107 perturbate = true;
1108
John Bauman19bac1e2014-05-06 15:23:49 -04001109 res.x = r.current.x;
1110 res.y = r.current.y;
1111 res.z = r.current.z;
1112 res.w = r.current.w;
John Bauman89401822014-05-06 15:04:28 -04001113 }
1114 break;
1115 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1116 {
John Bauman19bac1e2014-05-06 15:23:49 -04001117 r.du = Float4(texture.x) * Float4(1.0f / 0x0FE0);
1118 r.dv = Float4(texture.y) * Float4(1.0f / 0x0FE0);
John Bauman89401822014-05-06 15:04:28 -04001119
1120 Float4 du2;
1121 Float4 dv2;
1122
1123 du2 = r.du;
1124 dv2 = r.dv;
1125
1126 r.du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
1127 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
1128 r.du += dv2;
1129 r.dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
1130 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
1131 r.dv += du2;
1132
1133 perturbate = true;
1134
John Bauman19bac1e2014-05-06 15:23:49 -04001135 r.L = texture.z;
John Bauman89401822014-05-06 15:04:28 -04001136 r.L = MulHigh(r.L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceScale4)));
1137 r.L = r.L << 4;
1138 r.L = AddSat(r.L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceOffset4)));
1139 r.L = Max(r.L, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman19bac1e2014-05-06 15:23:49 -04001140 r.L = Min(r.L, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001141
1142 luminance = true;
1143
John Bauman19bac1e2014-05-06 15:23:49 -04001144 res.x = r.current.x;
1145 res.y = r.current.y;
1146 res.z = r.current.z;
1147 res.w = r.current.w;
John Bauman89401822014-05-06 15:04:28 -04001148 }
1149 break;
1150 default:
1151 ASSERT(false);
1152 }
1153
1154 if(textureStage.stageOperation != TextureStage::STAGE_DOT3)
1155 {
1156 switch(textureStage.firstArgumentAlpha)
1157 {
1158 case TextureStage::SOURCE_TEXTURE: arg1 = &texture; break;
1159 case TextureStage::SOURCE_CONSTANT: arg1 = &constant; break;
Nicolas Capenscbefe532014-10-16 00:16:01 -04001160 case TextureStage::SOURCE_CURRENT: arg1 = &r.current; break;
John Bauman89401822014-05-06 15:04:28 -04001161 case TextureStage::SOURCE_DIFFUSE: arg1 = &r.diffuse; break;
1162 case TextureStage::SOURCE_SPECULAR: arg1 = &r.specular; break;
1163 case TextureStage::SOURCE_TEMP: arg1 = &temp; break;
1164 case TextureStage::SOURCE_TFACTOR: arg1 = &tfactor; break;
1165 default:
1166 ASSERT(false);
1167 }
1168
1169 switch(textureStage.secondArgumentAlpha)
1170 {
1171 case TextureStage::SOURCE_TEXTURE: arg2 = &texture; break;
1172 case TextureStage::SOURCE_CONSTANT: arg2 = &constant; break;
Nicolas Capenscbefe532014-10-16 00:16:01 -04001173 case TextureStage::SOURCE_CURRENT: arg2 = &r.current; break;
John Bauman89401822014-05-06 15:04:28 -04001174 case TextureStage::SOURCE_DIFFUSE: arg2 = &r.diffuse; break;
1175 case TextureStage::SOURCE_SPECULAR: arg2 = &r.specular; break;
1176 case TextureStage::SOURCE_TEMP: arg2 = &temp; break;
1177 case TextureStage::SOURCE_TFACTOR: arg2 = &tfactor; break;
1178 default:
1179 ASSERT(false);
1180 }
1181
1182 switch(textureStage.thirdArgumentAlpha)
1183 {
1184 case TextureStage::SOURCE_TEXTURE: arg3 = &texture; break;
1185 case TextureStage::SOURCE_CONSTANT: arg3 = &constant; break;
Nicolas Capenscbefe532014-10-16 00:16:01 -04001186 case TextureStage::SOURCE_CURRENT: arg3 = &r.current; break;
John Bauman89401822014-05-06 15:04:28 -04001187 case TextureStage::SOURCE_DIFFUSE: arg3 = &r.diffuse; break;
1188 case TextureStage::SOURCE_SPECULAR: arg3 = &r.specular; break;
1189 case TextureStage::SOURCE_TEMP: arg3 = &temp; break;
1190 case TextureStage::SOURCE_TFACTOR: arg3 = &tfactor; break;
1191 default:
1192 ASSERT(false);
1193 }
1194
1195 switch(textureStage.firstModifierAlpha) // FIXME: Check if actually used
1196 {
1197 case TextureStage::MODIFIER_COLOR:
1198 break;
1199 case TextureStage::MODIFIER_INVCOLOR:
1200 {
John Bauman19bac1e2014-05-06 15:23:49 -04001201 mod1.w = SubSat(Short4(0x1000), arg1->w);
John Bauman89401822014-05-06 15:04:28 -04001202
1203 arg1 = &mod1;
1204 }
1205 break;
1206 case TextureStage::MODIFIER_ALPHA:
1207 {
1208 // Redudant
1209 }
1210 break;
1211 case TextureStage::MODIFIER_INVALPHA:
1212 {
John Bauman19bac1e2014-05-06 15:23:49 -04001213 mod1.w = SubSat(Short4(0x1000), arg1->w);
John Bauman89401822014-05-06 15:04:28 -04001214
1215 arg1 = &mod1;
1216 }
1217 break;
1218 default:
1219 ASSERT(false);
1220 }
1221
1222 switch(textureStage.secondModifierAlpha) // FIXME: Check if actually used
1223 {
1224 case TextureStage::MODIFIER_COLOR:
1225 break;
1226 case TextureStage::MODIFIER_INVCOLOR:
1227 {
John Bauman19bac1e2014-05-06 15:23:49 -04001228 mod2.w = SubSat(Short4(0x1000), arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001229
1230 arg2 = &mod2;
1231 }
1232 break;
1233 case TextureStage::MODIFIER_ALPHA:
1234 {
1235 // Redudant
1236 }
1237 break;
1238 case TextureStage::MODIFIER_INVALPHA:
1239 {
John Bauman19bac1e2014-05-06 15:23:49 -04001240 mod2.w = SubSat(Short4(0x1000), arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001241
1242 arg2 = &mod2;
1243 }
1244 break;
1245 default:
1246 ASSERT(false);
1247 }
1248
1249 switch(textureStage.thirdModifierAlpha) // FIXME: Check if actually used
1250 {
1251 case TextureStage::MODIFIER_COLOR:
1252 break;
1253 case TextureStage::MODIFIER_INVCOLOR:
1254 {
John Bauman19bac1e2014-05-06 15:23:49 -04001255 mod3.w = SubSat(Short4(0x1000), arg3->w);
John Bauman89401822014-05-06 15:04:28 -04001256
1257 arg3 = &mod3;
1258 }
1259 break;
1260 case TextureStage::MODIFIER_ALPHA:
1261 {
1262 // Redudant
1263 }
1264 break;
1265 case TextureStage::MODIFIER_INVALPHA:
1266 {
John Bauman19bac1e2014-05-06 15:23:49 -04001267 mod3.w = SubSat(Short4(0x1000), arg3->w);
John Bauman89401822014-05-06 15:04:28 -04001268
1269 arg3 = &mod3;
1270 }
1271 break;
1272 default:
1273 ASSERT(false);
1274 }
1275
1276 switch(textureStage.stageOperationAlpha)
1277 {
1278 case TextureStage::STAGE_DISABLE:
1279 break;
1280 case TextureStage::STAGE_SELECTARG1: // Arg1
1281 {
John Bauman19bac1e2014-05-06 15:23:49 -04001282 res.w = arg1->w;
John Bauman89401822014-05-06 15:04:28 -04001283 }
1284 break;
1285 case TextureStage::STAGE_SELECTARG2: // Arg2
1286 {
John Bauman19bac1e2014-05-06 15:23:49 -04001287 res.w = arg2->w;
John Bauman89401822014-05-06 15:04:28 -04001288 }
1289 break;
1290 case TextureStage::STAGE_SELECTARG3: // Arg3
1291 {
John Bauman19bac1e2014-05-06 15:23:49 -04001292 res.w = arg3->w;
John Bauman89401822014-05-06 15:04:28 -04001293 }
1294 break;
1295 case TextureStage::STAGE_MODULATE: // Arg1 * Arg2
1296 {
John Bauman19bac1e2014-05-06 15:23:49 -04001297 res.w = MulHigh(arg1->w, arg2->w) << 4;
John Bauman89401822014-05-06 15:04:28 -04001298 }
1299 break;
1300 case TextureStage::STAGE_MODULATE2X: // Arg1 * Arg2 * 2
1301 {
John Bauman19bac1e2014-05-06 15:23:49 -04001302 res.w = MulHigh(arg1->w, arg2->w) << 5;
John Bauman89401822014-05-06 15:04:28 -04001303 }
1304 break;
1305 case TextureStage::STAGE_MODULATE4X: // Arg1 * Arg2 * 4
1306 {
John Bauman19bac1e2014-05-06 15:23:49 -04001307 res.w = MulHigh(arg1->w, arg2->w) << 6;
John Bauman89401822014-05-06 15:04:28 -04001308 }
1309 break;
1310 case TextureStage::STAGE_ADD: // Arg1 + Arg2
1311 {
John Bauman19bac1e2014-05-06 15:23:49 -04001312 res.w = AddSat(arg1->w, arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001313 }
1314 break;
1315 case TextureStage::STAGE_ADDSIGNED: // Arg1 + Arg2 - 0.5
1316 {
John Bauman19bac1e2014-05-06 15:23:49 -04001317 res.w = AddSat(arg1->w, arg2->w);
1318 res.w = SubSat(res.w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -04001319 }
1320 break;
1321 case TextureStage::STAGE_ADDSIGNED2X: // (Arg1 + Arg2 - 0.5) << 1
1322 {
John Bauman19bac1e2014-05-06 15:23:49 -04001323 res.w = AddSat(arg1->w, arg2->w);
1324 res.w = SubSat(res.w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
1325 res.w = AddSat(res.w, res.w);
John Bauman89401822014-05-06 15:04:28 -04001326 }
1327 break;
1328 case TextureStage::STAGE_SUBTRACT: // Arg1 - Arg2
1329 {
John Bauman19bac1e2014-05-06 15:23:49 -04001330 res.w = SubSat(arg1->w, arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001331 }
1332 break;
1333 case TextureStage::STAGE_ADDSMOOTH: // Arg1 + Arg2 - Arg1 * Arg2
1334 {
1335 Short4 tmp;
1336
John Bauman19bac1e2014-05-06 15:23:49 -04001337 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 -04001338 }
1339 break;
1340 case TextureStage::STAGE_MULTIPLYADD: // Arg3 + Arg1 * Arg2
1341 {
John Bauman19bac1e2014-05-06 15:23:49 -04001342 res.w = MulHigh(arg1->w, arg2->w) << 4; res.w = AddSat(res.w, arg3->w);
John Bauman89401822014-05-06 15:04:28 -04001343 }
1344 break;
1345 case TextureStage::STAGE_LERP: // Arg3 * (Arg1 - Arg2) + Arg2
1346 {
John Bauman19bac1e2014-05-06 15:23:49 -04001347 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 -04001348 }
1349 break;
1350 case TextureStage::STAGE_DOT3:
1351 break; // Already computed in color channel
1352 case TextureStage::STAGE_BLENDCURRENTALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1353 {
Nicolas Capenscbefe532014-10-16 00:16:01 -04001354 res.w = SubSat(arg1->w, arg2->w); res.w = MulHigh(res.w, r.current.w) << 4; res.w = AddSat(res.w, arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001355 }
1356 break;
1357 case TextureStage::STAGE_BLENDDIFFUSEALPHA: // Arg1 * (Alpha) + Arg2 * (1 - Alpha)
1358 {
John Bauman19bac1e2014-05-06 15:23:49 -04001359 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 -04001360 }
1361 break;
1362 case TextureStage::STAGE_BLENDFACTORALPHA:
1363 {
John Bauman19bac1e2014-05-06 15:23:49 -04001364 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 -04001365 }
1366 break;
1367 case TextureStage::STAGE_BLENDTEXTUREALPHA: // Arg1 * (Alpha) + Arg2 * (1 - Alpha)
1368 {
John Bauman19bac1e2014-05-06 15:23:49 -04001369 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 -04001370 }
1371 break;
1372 case TextureStage::STAGE_BLENDTEXTUREALPHAPM: // Arg1 + Arg2 * (1 - Alpha)
1373 {
John Bauman19bac1e2014-05-06 15:23:49 -04001374 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 -04001375 }
1376 break;
1377 case TextureStage::STAGE_PREMODULATE:
1378 {
John Bauman19bac1e2014-05-06 15:23:49 -04001379 res.w = arg1->w;
John Bauman89401822014-05-06 15:04:28 -04001380 }
1381 break;
1382 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1383 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
1384 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1385 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1386 case TextureStage::STAGE_BUMPENVMAP:
1387 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1388 break; // Invalid alpha operations
1389 default:
1390 ASSERT(false);
1391 }
1392 }
1393
1394 // Clamp result to [0, 1]
1395
1396 switch(textureStage.stageOperation)
1397 {
1398 case TextureStage::STAGE_DISABLE:
1399 case TextureStage::STAGE_SELECTARG1:
1400 case TextureStage::STAGE_SELECTARG2:
1401 case TextureStage::STAGE_SELECTARG3:
1402 case TextureStage::STAGE_MODULATE:
1403 case TextureStage::STAGE_MODULATE2X:
1404 case TextureStage::STAGE_MODULATE4X:
1405 case TextureStage::STAGE_ADD:
1406 case TextureStage::STAGE_MULTIPLYADD:
1407 case TextureStage::STAGE_LERP:
1408 case TextureStage::STAGE_BLENDCURRENTALPHA:
1409 case TextureStage::STAGE_BLENDDIFFUSEALPHA:
1410 case TextureStage::STAGE_BLENDFACTORALPHA:
1411 case TextureStage::STAGE_BLENDTEXTUREALPHA:
1412 case TextureStage::STAGE_BLENDTEXTUREALPHAPM:
1413 case TextureStage::STAGE_DOT3: // Already clamped
1414 case TextureStage::STAGE_PREMODULATE:
1415 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1416 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
1417 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1418 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1419 case TextureStage::STAGE_BUMPENVMAP:
1420 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1421 if(state.textureStage[stage].cantUnderflow)
1422 {
1423 break; // Can't go below zero
1424 }
1425 case TextureStage::STAGE_ADDSIGNED:
1426 case TextureStage::STAGE_ADDSIGNED2X:
1427 case TextureStage::STAGE_SUBTRACT:
1428 case TextureStage::STAGE_ADDSMOOTH:
John Bauman19bac1e2014-05-06 15:23:49 -04001429 res.x = Max(res.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));
1430 res.y = Max(res.y, Short4(0x0000, 0x0000, 0x0000, 0x0000));
1431 res.z = Max(res.z, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman89401822014-05-06 15:04:28 -04001432 break;
1433 default:
1434 ASSERT(false);
1435 }
1436
1437 switch(textureStage.stageOperationAlpha)
1438 {
1439 case TextureStage::STAGE_DISABLE:
1440 case TextureStage::STAGE_SELECTARG1:
1441 case TextureStage::STAGE_SELECTARG2:
1442 case TextureStage::STAGE_SELECTARG3:
1443 case TextureStage::STAGE_MODULATE:
1444 case TextureStage::STAGE_MODULATE2X:
1445 case TextureStage::STAGE_MODULATE4X:
1446 case TextureStage::STAGE_ADD:
1447 case TextureStage::STAGE_MULTIPLYADD:
1448 case TextureStage::STAGE_LERP:
1449 case TextureStage::STAGE_BLENDCURRENTALPHA:
1450 case TextureStage::STAGE_BLENDDIFFUSEALPHA:
1451 case TextureStage::STAGE_BLENDFACTORALPHA:
1452 case TextureStage::STAGE_BLENDTEXTUREALPHA:
1453 case TextureStage::STAGE_BLENDTEXTUREALPHAPM:
1454 case TextureStage::STAGE_DOT3: // Already clamped
1455 case TextureStage::STAGE_PREMODULATE:
1456 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1457 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
1458 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1459 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1460 case TextureStage::STAGE_BUMPENVMAP:
1461 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1462 if(state.textureStage[stage].cantUnderflow)
1463 {
1464 break; // Can't go below zero
1465 }
1466 case TextureStage::STAGE_ADDSIGNED:
1467 case TextureStage::STAGE_ADDSIGNED2X:
1468 case TextureStage::STAGE_SUBTRACT:
1469 case TextureStage::STAGE_ADDSMOOTH:
John Bauman19bac1e2014-05-06 15:23:49 -04001470 res.w = Max(res.w, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman89401822014-05-06 15:04:28 -04001471 break;
1472 default:
1473 ASSERT(false);
1474 }
1475
1476 switch(textureStage.stageOperation)
1477 {
1478 case TextureStage::STAGE_DISABLE:
1479 case TextureStage::STAGE_SELECTARG1:
1480 case TextureStage::STAGE_SELECTARG2:
1481 case TextureStage::STAGE_SELECTARG3:
1482 case TextureStage::STAGE_MODULATE:
1483 case TextureStage::STAGE_SUBTRACT:
1484 case TextureStage::STAGE_ADDSMOOTH:
1485 case TextureStage::STAGE_LERP:
1486 case TextureStage::STAGE_BLENDCURRENTALPHA:
1487 case TextureStage::STAGE_BLENDDIFFUSEALPHA:
1488 case TextureStage::STAGE_BLENDFACTORALPHA:
1489 case TextureStage::STAGE_BLENDTEXTUREALPHA:
1490 case TextureStage::STAGE_DOT3: // Already clamped
1491 case TextureStage::STAGE_PREMODULATE:
1492 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1493 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1494 case TextureStage::STAGE_BUMPENVMAP:
1495 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1496 break; // Can't go above one
1497 case TextureStage::STAGE_MODULATE2X:
1498 case TextureStage::STAGE_MODULATE4X:
1499 case TextureStage::STAGE_ADD:
1500 case TextureStage::STAGE_ADDSIGNED:
1501 case TextureStage::STAGE_ADDSIGNED2X:
1502 case TextureStage::STAGE_MULTIPLYADD:
1503 case TextureStage::STAGE_BLENDTEXTUREALPHAPM:
1504 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1505 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001506 res.x = Min(res.x, Short4(0x1000));
1507 res.y = Min(res.y, Short4(0x1000));
1508 res.z = Min(res.z, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001509 break;
1510 default:
1511 ASSERT(false);
1512 }
1513
1514 switch(textureStage.stageOperationAlpha)
1515 {
1516 case TextureStage::STAGE_DISABLE:
1517 case TextureStage::STAGE_SELECTARG1:
1518 case TextureStage::STAGE_SELECTARG2:
1519 case TextureStage::STAGE_SELECTARG3:
1520 case TextureStage::STAGE_MODULATE:
1521 case TextureStage::STAGE_SUBTRACT:
1522 case TextureStage::STAGE_ADDSMOOTH:
1523 case TextureStage::STAGE_LERP:
1524 case TextureStage::STAGE_BLENDCURRENTALPHA:
1525 case TextureStage::STAGE_BLENDDIFFUSEALPHA:
1526 case TextureStage::STAGE_BLENDFACTORALPHA:
1527 case TextureStage::STAGE_BLENDTEXTUREALPHA:
1528 case TextureStage::STAGE_DOT3: // Already clamped
1529 case TextureStage::STAGE_PREMODULATE:
1530 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1531 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1532 case TextureStage::STAGE_BUMPENVMAP:
1533 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1534 break; // Can't go above one
1535 case TextureStage::STAGE_MODULATE2X:
1536 case TextureStage::STAGE_MODULATE4X:
1537 case TextureStage::STAGE_ADD:
1538 case TextureStage::STAGE_ADDSIGNED:
1539 case TextureStage::STAGE_ADDSIGNED2X:
1540 case TextureStage::STAGE_MULTIPLYADD:
1541 case TextureStage::STAGE_BLENDTEXTUREALPHAPM:
1542 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1543 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001544 res.w = Min(res.w, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001545 break;
1546 default:
1547 ASSERT(false);
1548 }
1549
1550 switch(textureStage.destinationArgument)
1551 {
1552 case TextureStage::DESTINATION_CURRENT:
Nicolas Capenscbefe532014-10-16 00:16:01 -04001553 r.current.x = res.x;
1554 r.current.y = res.y;
1555 r.current.z = res.z;
1556 r.current.w = res.w;
John Bauman89401822014-05-06 15:04:28 -04001557 break;
1558 case TextureStage::DESTINATION_TEMP:
John Bauman19bac1e2014-05-06 15:23:49 -04001559 temp.x = res.x;
1560 temp.y = res.y;
1561 temp.z = res.z;
1562 temp.w = res.w;
John Bauman89401822014-05-06 15:04:28 -04001563 break;
1564 default:
1565 ASSERT(false);
1566 }
1567 }
1568
1569 void PixelRoutine::alphaTest(Registers &r, Int &aMask, Short4 &alpha)
1570 {
1571 Short4 cmp;
1572 Short4 equal;
1573
1574 switch(state.alphaCompareMode)
1575 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001576 case ALPHA_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -04001577 aMask = 0xF;
1578 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001579 case ALPHA_NEVER:
John Bauman89401822014-05-06 15:04:28 -04001580 aMask = 0x0;
1581 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001582 case ALPHA_EQUAL:
John Bauman89401822014-05-06 15:04:28 -04001583 cmp = CmpEQ(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
1584 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1585 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001586 case ALPHA_NOTEQUAL: // a != b ~ !(a == b)
John Bauman89401822014-05-06 15:04:28 -04001587 cmp = CmpEQ(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4))) ^ Short4((short)0xFFFF, (short)0xFFFF, (short)0xFFFF, (short)0xFFFF); // FIXME
1588 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1589 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001590 case ALPHA_LESS: // a < b ~ b > a
John Bauman89401822014-05-06 15:04:28 -04001591 cmp = CmpGT(*Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)), alpha);
1592 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1593 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001594 case ALPHA_GREATEREQUAL: // a >= b ~ (a > b) || (a == b) ~ !(b > a) // TODO: Approximate
John Bauman89401822014-05-06 15:04:28 -04001595 equal = CmpEQ(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
1596 cmp = CmpGT(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
1597 cmp |= equal;
1598 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1599 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001600 case ALPHA_LESSEQUAL: // a <= b ~ !(a > b)
John Bauman89401822014-05-06 15:04:28 -04001601 cmp = CmpGT(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4))) ^ Short4((short)0xFFFF, (short)0xFFFF, (short)0xFFFF, (short)0xFFFF); // FIXME
1602 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1603 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001604 case ALPHA_GREATER: // a > b
John Bauman89401822014-05-06 15:04:28 -04001605 cmp = CmpGT(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
1606 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1607 break;
1608 default:
1609 ASSERT(false);
1610 }
1611 }
1612
1613 void PixelRoutine::alphaToCoverage(Registers &r, Int cMask[4], Float4 &alpha)
1614 {
1615 Int4 coverage0 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c0)));
1616 Int4 coverage1 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c1)));
1617 Int4 coverage2 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c2)));
1618 Int4 coverage3 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c3)));
1619
1620 Int aMask0 = SignMask(coverage0);
1621 Int aMask1 = SignMask(coverage1);
1622 Int aMask2 = SignMask(coverage2);
1623 Int aMask3 = SignMask(coverage3);
1624
1625 cMask[0] &= aMask0;
1626 cMask[1] &= aMask1;
1627 cMask[2] &= aMask2;
1628 cMask[3] &= aMask3;
1629 }
1630
John Bauman19bac1e2014-05-06 15:23:49 -04001631 Bool PixelRoutine::alphaTest(Registers &r, Int cMask[4], Vector4i &current)
John Bauman89401822014-05-06 15:04:28 -04001632 {
1633 if(!state.alphaTestActive())
1634 {
1635 return true;
1636 }
1637
1638 Int aMask;
1639
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001640 if(state.transparencyAntialiasing == TRANSPARENCY_NONE)
John Bauman89401822014-05-06 15:04:28 -04001641 {
John Bauman19bac1e2014-05-06 15:23:49 -04001642 alphaTest(r, aMask, current.w);
John Bauman89401822014-05-06 15:04:28 -04001643
1644 for(unsigned int q = 0; q < state.multiSample; q++)
1645 {
1646 cMask[q] &= aMask;
1647 }
1648 }
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001649 else if(state.transparencyAntialiasing == TRANSPARENCY_ALPHA_TO_COVERAGE)
John Bauman89401822014-05-06 15:04:28 -04001650 {
John Bauman19bac1e2014-05-06 15:23:49 -04001651 Float4 alpha = Float4(current.w) * Float4(1.0f / 0x1000);
John Bauman89401822014-05-06 15:04:28 -04001652
1653 alphaToCoverage(r, cMask, alpha);
1654 }
1655 else ASSERT(false);
1656
1657 Int pass = cMask[0];
1658
1659 for(unsigned int q = 1; q < state.multiSample; q++)
1660 {
1661 pass = pass | cMask[q];
1662 }
1663
1664 return pass != 0x0;
1665 }
1666
John Bauman19bac1e2014-05-06 15:23:49 -04001667 Bool PixelRoutine::alphaTest(Registers &r, Int cMask[4], Vector4f &c0)
John Bauman89401822014-05-06 15:04:28 -04001668 {
1669 if(!state.alphaTestActive())
1670 {
1671 return true;
1672 }
1673
1674 Int aMask;
1675
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001676 if(state.transparencyAntialiasing == TRANSPARENCY_NONE)
John Bauman89401822014-05-06 15:04:28 -04001677 {
John Bauman19bac1e2014-05-06 15:23:49 -04001678 Short4 alpha = RoundShort4(c0.w * Float4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001679
1680 alphaTest(r, aMask, alpha);
1681
1682 for(unsigned int q = 0; q < state.multiSample; q++)
1683 {
1684 cMask[q] &= aMask;
1685 }
1686 }
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001687 else if(state.transparencyAntialiasing == TRANSPARENCY_ALPHA_TO_COVERAGE)
John Bauman89401822014-05-06 15:04:28 -04001688 {
John Bauman19bac1e2014-05-06 15:23:49 -04001689 alphaToCoverage(r, cMask, c0.w);
John Bauman89401822014-05-06 15:04:28 -04001690 }
1691 else ASSERT(false);
1692
1693 Int pass = cMask[0];
1694
1695 for(unsigned int q = 1; q < state.multiSample; q++)
1696 {
1697 pass = pass | cMask[q];
1698 }
1699
1700 return pass != 0x0;
1701 }
1702
John Bauman19bac1e2014-05-06 15:23:49 -04001703 void PixelRoutine::fogBlend(Registers &r, Vector4i &current, Float4 &f, Float4 &z, Float4 &rhw)
John Bauman89401822014-05-06 15:04:28 -04001704 {
1705 if(!state.fogActive)
1706 {
1707 return;
1708 }
1709
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001710 if(state.pixelFogMode != FOG_NONE)
John Bauman89401822014-05-06 15:04:28 -04001711 {
1712 pixelFog(r, f, z, rhw);
1713 }
1714
1715 UShort4 fog = convertFixed16(f, true);
1716
John Bauman19bac1e2014-05-06 15:23:49 -04001717 current.x = As<Short4>(MulHigh(As<UShort4>(current.x), fog));
1718 current.y = As<Short4>(MulHigh(As<UShort4>(current.y), fog));
1719 current.z = As<Short4>(MulHigh(As<UShort4>(current.z), fog));
John Bauman89401822014-05-06 15:04:28 -04001720
John Bauman19bac1e2014-05-06 15:23:49 -04001721 UShort4 invFog = UShort4(0xFFFFu) - fog;
John Bauman89401822014-05-06 15:04:28 -04001722
John Bauman19bac1e2014-05-06 15:23:49 -04001723 current.x += As<Short4>(MulHigh(invFog, *Pointer<UShort4>(r.data + OFFSET(DrawData,fog.color4[0]))));
1724 current.y += As<Short4>(MulHigh(invFog, *Pointer<UShort4>(r.data + OFFSET(DrawData,fog.color4[1]))));
1725 current.z += As<Short4>(MulHigh(invFog, *Pointer<UShort4>(r.data + OFFSET(DrawData,fog.color4[2]))));
John Bauman89401822014-05-06 15:04:28 -04001726 }
1727
John Bauman19bac1e2014-05-06 15:23:49 -04001728 void PixelRoutine::fogBlend(Registers &r, Vector4f &c0, Float4 &fog, Float4 &z, Float4 &rhw)
John Bauman89401822014-05-06 15:04:28 -04001729 {
1730 if(!state.fogActive)
1731 {
1732 return;
1733 }
1734
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001735 if(state.pixelFogMode != FOG_NONE)
John Bauman89401822014-05-06 15:04:28 -04001736 {
1737 pixelFog(r, fog, z, rhw);
1738
John Bauman19bac1e2014-05-06 15:23:49 -04001739 fog = Min(fog, Float4(1.0f));
1740 fog = Max(fog, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -04001741 }
1742
John Bauman19bac1e2014-05-06 15:23:49 -04001743 c0.x -= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[0]));
1744 c0.y -= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[1]));
1745 c0.z -= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[2]));
John Bauman89401822014-05-06 15:04:28 -04001746
John Bauman19bac1e2014-05-06 15:23:49 -04001747 c0.x *= fog;
1748 c0.y *= fog;
1749 c0.z *= fog;
John Bauman89401822014-05-06 15:04:28 -04001750
John Bauman19bac1e2014-05-06 15:23:49 -04001751 c0.x += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[0]));
1752 c0.y += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[1]));
1753 c0.z += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[2]));
John Bauman89401822014-05-06 15:04:28 -04001754 }
1755
1756 void PixelRoutine::pixelFog(Registers &r, Float4 &visibility, Float4 &z, Float4 &rhw)
1757 {
1758 Float4 &zw = visibility;
1759
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001760 if(state.pixelFogMode != FOG_NONE)
John Bauman89401822014-05-06 15:04:28 -04001761 {
1762 if(state.wBasedFog)
1763 {
1764 zw = rhw;
1765 }
1766 else
1767 {
1768 if(complementaryDepthBuffer)
1769 {
John Bauman19bac1e2014-05-06 15:23:49 -04001770 zw = Float4(1.0f) - z;
John Bauman89401822014-05-06 15:04:28 -04001771 }
1772 else
1773 {
1774 zw = z;
1775 }
1776 }
1777 }
1778
1779 switch(state.pixelFogMode)
1780 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001781 case FOG_NONE:
John Bauman89401822014-05-06 15:04:28 -04001782 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001783 case FOG_LINEAR:
John Bauman89401822014-05-06 15:04:28 -04001784 zw *= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.scale));
1785 zw += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.offset));
1786 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001787 case FOG_EXP:
John Bauman89401822014-05-06 15:04:28 -04001788 zw *= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.densityE));
John Bauman19bac1e2014-05-06 15:23:49 -04001789 zw = exponential2(zw, true);
John Bauman89401822014-05-06 15:04:28 -04001790 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001791 case FOG_EXP2:
John Bauman89401822014-05-06 15:04:28 -04001792 zw *= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.densityE2));
1793 zw *= zw;
John Bauman19bac1e2014-05-06 15:23:49 -04001794 zw = exponential2(zw, true);
John Bauman89401822014-05-06 15:04:28 -04001795 zw = Rcp_pp(zw);
1796 break;
1797 default:
1798 ASSERT(false);
1799 }
1800 }
1801
John Bauman19bac1e2014-05-06 15:23:49 -04001802 void PixelRoutine::specularPixel(Vector4i &current, Vector4i &specular)
John Bauman89401822014-05-06 15:04:28 -04001803 {
1804 if(!state.specularAdd)
1805 {
1806 return;
1807 }
1808
John Bauman19bac1e2014-05-06 15:23:49 -04001809 current.x = AddSat(current.x, specular.x);
1810 current.y = AddSat(current.y, specular.y);
1811 current.z = AddSat(current.z, specular.z);
John Bauman89401822014-05-06 15:04:28 -04001812 }
1813
1814 void PixelRoutine::writeDepth(Registers &r, Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &zMask)
1815 {
1816 if(!state.depthWriteEnable)
1817 {
1818 return;
1819 }
1820
1821 Float4 Z = z;
1822
John Bauman19bac1e2014-05-06 15:23:49 -04001823 if(shader && shader->depthOverride())
John Bauman89401822014-05-06 15:04:28 -04001824 {
1825 if(complementaryDepthBuffer)
1826 {
John Bauman19bac1e2014-05-06 15:23:49 -04001827 Z = Float4(1.0f) - r.oDepth;
John Bauman89401822014-05-06 15:04:28 -04001828 }
1829 else
1830 {
1831 Z = r.oDepth;
1832 }
1833 }
1834
1835 Pointer<Byte> buffer;
1836 Int pitch;
1837
1838 if(!state.quadLayoutDepthBuffer)
1839 {
1840 buffer = zBuffer + 4 * x;
1841 pitch = *Pointer<Int>(r.data + OFFSET(DrawData,depthPitchB));
1842 }
1843 else
1844 {
1845 buffer = zBuffer + 8 * x;
1846 }
1847
1848 if(q > 0)
1849 {
1850 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,depthSliceB));
1851 }
1852
1853 Float4 zValue;
1854
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001855 if(state.depthCompareMode != DEPTH_NEVER || (state.depthCompareMode != DEPTH_ALWAYS && !state.depthWriteEnable))
John Bauman89401822014-05-06 15:04:28 -04001856 {
1857 if(!state.quadLayoutDepthBuffer)
1858 {
1859 // FIXME: Properly optimizes?
1860 zValue.xy = *Pointer<Float4>(buffer);
1861 zValue.zw = *Pointer<Float4>(buffer + pitch - 8);
1862 }
1863 else
1864 {
1865 zValue = *Pointer<Float4>(buffer, 16);
1866 }
1867 }
1868
1869 Z = As<Float4>(As<Int4>(Z) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X) + zMask * 16, 16));
1870 zValue = As<Float4>(As<Int4>(zValue) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X) + zMask * 16, 16));
1871 Z = As<Float4>(As<Int4>(Z) | As<Int4>(zValue));
1872
1873 if(!state.quadLayoutDepthBuffer)
1874 {
1875 // FIXME: Properly optimizes?
1876 *Pointer<Float2>(buffer) = Float2(Z.xy);
1877 *Pointer<Float2>(buffer + pitch) = Float2(Z.zw);
1878 }
1879 else
1880 {
1881 *Pointer<Float4>(buffer, 16) = Z;
1882 }
1883 }
1884
1885 void PixelRoutine::writeStencil(Registers &r, Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &zMask, Int &cMask)
1886 {
1887 if(!state.stencilActive)
1888 {
1889 return;
1890 }
1891
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001892 if(state.stencilPassOperation == OPERATION_KEEP && state.stencilZFailOperation == OPERATION_KEEP && state.stencilFailOperation == OPERATION_KEEP)
John Bauman89401822014-05-06 15:04:28 -04001893 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001894 if(!state.twoSidedStencil || (state.stencilPassOperationCCW == OPERATION_KEEP && state.stencilZFailOperationCCW == OPERATION_KEEP && state.stencilFailOperationCCW == OPERATION_KEEP))
John Bauman89401822014-05-06 15:04:28 -04001895 {
1896 return;
1897 }
1898 }
1899
1900 if(state.stencilWriteMasked && (!state.twoSidedStencil || state.stencilWriteMaskedCCW))
1901 {
1902 return;
1903 }
1904
1905 Pointer<Byte> buffer = sBuffer + 2 * x;
1906
1907 if(q > 0)
1908 {
1909 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,stencilSliceB));
1910 }
1911
1912 Byte8 bufferValue = As<Byte8>(Long1(*Pointer<UInt>(buffer)));
1913
1914 Byte8 newValue;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001915 stencilOperation(r, newValue, bufferValue, state.stencilPassOperation, state.stencilZFailOperation, state.stencilFailOperation, false, zMask, sMask);
John Bauman89401822014-05-06 15:04:28 -04001916
1917 if(!state.noStencilWriteMask)
1918 {
1919 Byte8 maskedValue = bufferValue;
1920 newValue &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[0].writeMaskQ));
1921 maskedValue &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[0].invWriteMaskQ));
1922 newValue |= maskedValue;
1923 }
1924
1925 if(state.twoSidedStencil)
1926 {
1927 Byte8 newValueCCW;
1928
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001929 stencilOperation(r, newValueCCW, bufferValue, state.stencilPassOperationCCW, state.stencilZFailOperationCCW, state.stencilFailOperationCCW, true, zMask, sMask);
John Bauman89401822014-05-06 15:04:28 -04001930
1931 if(!state.noStencilWriteMaskCCW)
1932 {
1933 Byte8 maskedValue = bufferValue;
1934 newValueCCW &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[1].writeMaskQ));
1935 maskedValue &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[1].invWriteMaskQ));
1936 newValueCCW |= maskedValue;
1937 }
1938
1939 newValue &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,clockwiseMask));
1940 newValueCCW &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,invClockwiseMask));
1941 newValue |= newValueCCW;
1942 }
1943
1944 newValue &= *Pointer<Byte8>(r.constants + OFFSET(Constants,maskB4Q) + 8 * cMask);
1945 bufferValue &= *Pointer<Byte8>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * cMask);
1946 newValue |= bufferValue;
1947
1948 *Pointer<UInt>(buffer) = UInt(As<Long>(newValue));
1949 }
1950
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001951 void PixelRoutine::stencilOperation(Registers &r, Byte8 &newValue, Byte8 &bufferValue, StencilOperation stencilPassOperation, StencilOperation stencilZFailOperation, StencilOperation stencilFailOperation, bool CCW, Int &zMask, Int &sMask)
John Bauman89401822014-05-06 15:04:28 -04001952 {
1953 Byte8 &pass = newValue;
1954 Byte8 fail;
1955 Byte8 zFail;
1956
1957 stencilOperation(r, pass, bufferValue, stencilPassOperation, CCW);
1958
1959 if(stencilZFailOperation != stencilPassOperation)
1960 {
1961 stencilOperation(r, zFail, bufferValue, stencilZFailOperation, CCW);
1962 }
1963
1964 if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
1965 {
1966 stencilOperation(r, fail, bufferValue, stencilFailOperation, CCW);
1967 }
1968
1969 if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
1970 {
1971 if(state.depthTestActive && stencilZFailOperation != stencilPassOperation) // zMask valid and values not the same
1972 {
1973 pass &= *Pointer<Byte8>(r.constants + OFFSET(Constants,maskB4Q) + 8 * zMask);
1974 zFail &= *Pointer<Byte8>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * zMask);
1975 pass |= zFail;
1976 }
1977
1978 pass &= *Pointer<Byte8>(r.constants + OFFSET(Constants,maskB4Q) + 8 * sMask);
1979 fail &= *Pointer<Byte8>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * sMask);
1980 pass |= fail;
1981 }
1982 }
1983
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001984 void PixelRoutine::stencilOperation(Registers &r, Byte8 &output, Byte8 &bufferValue, StencilOperation operation, bool CCW)
John Bauman89401822014-05-06 15:04:28 -04001985 {
1986 switch(operation)
1987 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001988 case OPERATION_KEEP:
John Bauman89401822014-05-06 15:04:28 -04001989 output = bufferValue;
1990 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001991 case OPERATION_ZERO:
John Bauman89401822014-05-06 15:04:28 -04001992 output = Byte8(0x0000000000000000);
1993 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001994 case OPERATION_REPLACE:
John Bauman89401822014-05-06 15:04:28 -04001995 output = *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceQ));
1996 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001997 case OPERATION_INCRSAT:
John Bauman89401822014-05-06 15:04:28 -04001998 output = AddSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));
1999 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002000 case OPERATION_DECRSAT:
John Bauman89401822014-05-06 15:04:28 -04002001 output = SubSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));
2002 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002003 case OPERATION_INVERT:
John Bauman89401822014-05-06 15:04:28 -04002004 output = bufferValue ^ Byte8(0xFFFFFFFFFFFFFFFF);
2005 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002006 case OPERATION_INCR:
John Bauman89401822014-05-06 15:04:28 -04002007 output = bufferValue + Byte8(1, 1, 1, 1, 1, 1, 1, 1);
2008 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002009 case OPERATION_DECR:
John Bauman89401822014-05-06 15:04:28 -04002010 output = bufferValue - Byte8(1, 1, 1, 1, 1, 1, 1, 1);
2011 break;
2012 default:
2013 ASSERT(false);
2014 }
2015 }
2016
John Bauman19bac1e2014-05-06 15:23:49 -04002017 void PixelRoutine::sampleTexture(Registers &r, Vector4i &c, int coordinates, int stage, bool project)
John Bauman89401822014-05-06 15:04:28 -04002018 {
John Bauman19bac1e2014-05-06 15:23:49 -04002019 Float4 u = r.vf[2 + coordinates].x;
2020 Float4 v = r.vf[2 + coordinates].y;
2021 Float4 w = r.vf[2 + coordinates].z;
2022 Float4 q = r.vf[2 + coordinates].w;
John Bauman89401822014-05-06 15:04:28 -04002023
2024 if(perturbate)
2025 {
2026 u += r.du;
2027 v += r.dv;
2028
2029 perturbate = false;
2030 }
2031
2032 sampleTexture(r, c, stage, u, v, w, q, project);
2033 }
2034
John Bauman19bac1e2014-05-06 15:23:49 -04002035 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 -04002036 {
John Bauman19bac1e2014-05-06 15:23:49 -04002037 Vector4f dsx;
2038 Vector4f dsy;
John Bauman89401822014-05-06 15:04:28 -04002039
2040 sampleTexture(r, c, stage, u, v, w, q, dsx, dsy, project, bias, fixed12, false);
2041 }
2042
John Bauman19bac1e2014-05-06 15:23:49 -04002043 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 -04002044 {
2045 #if PERF_PROFILE
2046 Long texTime = Ticks();
2047 #endif
2048
2049 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap) + stage * sizeof(Texture);
2050
2051 if(!project)
2052 {
2053 sampler[stage]->sampleTexture(texture, c, u, v, w, q, dsx, dsy, bias, fixed12, gradients, lodProvided);
2054 }
2055 else
2056 {
2057 Float4 rq = reciprocal(q);
2058
2059 Float4 u_q = u * rq;
2060 Float4 v_q = v * rq;
2061 Float4 w_q = w * rq;
2062
2063 sampler[stage]->sampleTexture(texture, c, u_q, v_q, w_q, q, dsx, dsy, bias, fixed12, gradients, lodProvided);
2064 }
2065
2066 #if PERF_PROFILE
2067 r.cycles[PERF_TEX] += Ticks() - texTime;
2068 #endif
2069 }
2070
John Bauman19bac1e2014-05-06 15:23:49 -04002071 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)
2072 {
2073 if(sampler.type == Shader::PARAMETER_SAMPLER && sampler.rel.type == Shader::PARAMETER_VOID)
2074 {
2075 sampleTexture(r, c, sampler.index, u, v, w, q, dsx, dsy, project, bias, gradients, lodProvided);
2076 }
2077 else
2078 {
2079 Int index = As<Int>(Float(reg(r, sampler).x.x));
2080
2081 for(int i = 0; i < 16; i++)
2082 {
2083 if(shader->usesSampler(i))
2084 {
2085 If(index == i)
2086 {
2087 sampleTexture(r, c, i, u, v, w, q, dsx, dsy, project, bias, gradients, lodProvided);
2088 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
2089 }
2090 }
2091 }
2092 }
2093 }
2094
2095 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 -04002096 {
2097 #if PERF_PROFILE
2098 Long texTime = Ticks();
2099 #endif
2100
2101 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap) + stage * sizeof(Texture);
2102
2103 if(!project)
2104 {
2105 sampler[stage]->sampleTexture(texture, c, u, v, w, q, dsx, dsy, bias, gradients, lodProvided);
2106 }
2107 else
2108 {
2109 Float4 rq = reciprocal(q);
2110
2111 Float4 u_q = u * rq;
2112 Float4 v_q = v * rq;
2113 Float4 w_q = w * rq;
2114
2115 sampler[stage]->sampleTexture(texture, c, u_q, v_q, w_q, q, dsx, dsy, bias, gradients, lodProvided);
2116 }
2117
2118 #if PERF_PROFILE
2119 r.cycles[PERF_TEX] += Ticks() - texTime;
2120 #endif
2121 }
2122
John Bauman19bac1e2014-05-06 15:23:49 -04002123 void PixelRoutine::clampColor(Vector4f oC[4])
John Bauman89401822014-05-06 15:04:28 -04002124 {
2125 for(int index = 0; index < 4; index++)
2126 {
2127 if(!state.colorWriteActive(index) && !(index == 0 && state.alphaTestActive()))
2128 {
2129 continue;
2130 }
2131
2132 switch(state.targetFormat[index])
2133 {
2134 case FORMAT_NULL:
2135 break;
2136 case FORMAT_A16B16G16R16:
2137 case FORMAT_A8R8G8B8:
2138 case FORMAT_X8R8G8B8:
John Bauman66b8ab22014-05-06 15:57:45 -04002139 case FORMAT_A8:
John Bauman89401822014-05-06 15:04:28 -04002140 case FORMAT_G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04002141 oC[index].x = Max(oC[index].x, Float4(0.0f)); oC[index].x = Min(oC[index].x, Float4(1.0f));
2142 oC[index].y = Max(oC[index].y, Float4(0.0f)); oC[index].y = Min(oC[index].y, Float4(1.0f));
2143 oC[index].z = Max(oC[index].z, Float4(0.0f)); oC[index].z = Min(oC[index].z, Float4(1.0f));
2144 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 -04002145 break;
2146 case FORMAT_R32F:
2147 case FORMAT_G32R32F:
2148 case FORMAT_A32B32G32R32F:
2149 break;
2150 default:
2151 ASSERT(false);
2152 }
2153 }
2154 }
2155
John Bauman19bac1e2014-05-06 15:23:49 -04002156 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 -04002157 {
2158 if(!state.colorWriteActive(0))
2159 {
2160 return;
2161 }
2162
John Bauman19bac1e2014-05-06 15:23:49 -04002163 Vector4f oC;
John Bauman89401822014-05-06 15:04:28 -04002164
2165 switch(state.targetFormat[0])
2166 {
2167 case FORMAT_X8R8G8B8:
2168 case FORMAT_A8R8G8B8:
John Bauman66b8ab22014-05-06 15:57:45 -04002169 case FORMAT_A8:
John Bauman89401822014-05-06 15:04:28 -04002170 case FORMAT_G16R16:
2171 case FORMAT_A16B16G16R16:
2172 if(!postBlendSRGB && state.writeSRGB)
2173 {
2174 linearToSRGB12_16(r, current);
2175 }
2176 else
2177 {
John Bauman19bac1e2014-05-06 15:23:49 -04002178 current.x <<= 4;
2179 current.y <<= 4;
2180 current.z <<= 4;
2181 current.w <<= 4;
John Bauman89401822014-05-06 15:04:28 -04002182 }
2183
2184 fogBlend(r, current, fog, r.z[0], r.rhw);
2185
2186 for(unsigned int q = 0; q < state.multiSample; q++)
2187 {
2188 Pointer<Byte> buffer = cBuffer + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002189 Vector4i color = current;
John Bauman89401822014-05-06 15:04:28 -04002190
2191 if(state.multiSampleMask & (1 << q))
2192 {
2193 alphaBlend(r, 0, buffer, color, x);
2194 writeColor(r, 0, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2195 }
2196 }
2197 break;
2198 case FORMAT_R32F:
2199 case FORMAT_G32R32F:
2200 case FORMAT_A32B32G32R32F:
2201 convertSigned12(oC, current);
2202 fogBlend(r, oC, fog, r.z[0], r.rhw);
2203
2204 for(unsigned int q = 0; q < state.multiSample; q++)
2205 {
2206 Pointer<Byte> buffer = cBuffer + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002207 Vector4f color = oC;
John Bauman89401822014-05-06 15:04:28 -04002208
2209 if(state.multiSampleMask & (1 << q))
2210 {
2211 alphaBlend(r, 0, buffer, color, x);
2212 writeColor(r, 0, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2213 }
2214 }
2215 break;
2216 default:
2217 ASSERT(false);
2218 }
2219 }
2220
John Bauman19bac1e2014-05-06 15:23:49 -04002221 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 -04002222 {
2223 for(int index = 0; index < 4; index++)
2224 {
2225 if(!state.colorWriteActive(index))
2226 {
2227 continue;
2228 }
2229
2230 if(!postBlendSRGB && state.writeSRGB)
2231 {
John Bauman19bac1e2014-05-06 15:23:49 -04002232 oC[index].x = linearToSRGB(oC[index].x);
2233 oC[index].y = linearToSRGB(oC[index].y);
2234 oC[index].z = linearToSRGB(oC[index].z);
John Bauman89401822014-05-06 15:04:28 -04002235 }
2236
2237 if(index == 0)
2238 {
2239 fogBlend(r, oC[index], fog, r.z[0], r.rhw);
2240 }
2241
2242 switch(state.targetFormat[index])
2243 {
2244 case FORMAT_X8R8G8B8:
2245 case FORMAT_A8R8G8B8:
John Bauman66b8ab22014-05-06 15:57:45 -04002246 case FORMAT_A8:
John Bauman89401822014-05-06 15:04:28 -04002247 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
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002287 void PixelRoutine::blendFactor(Registers &r, const Vector4i &blendFactor, const Vector4i &current, const Vector4i &pixel, BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -04002288 {
2289 switch(blendFactorActive)
2290 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002291 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04002292 // Optimized
2293 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002294 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04002295 // Optimized
2296 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002297 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002302 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002307 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002312 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002317 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002322 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002327 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002332 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002337 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002343 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002348 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002353 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002358 case 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
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002368 void PixelRoutine::blendFactorAlpha(Registers &r, const Vector4i &blendFactor, const Vector4i &current, const Vector4i &pixel, BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -04002369 {
2370 switch(blendFactorAlphaActive)
2371 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002372 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04002373 // Optimized
2374 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002375 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04002376 // Optimized
2377 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002378 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002379 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -04002380 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002381 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002384 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002385 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002386 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002387 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002390 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002391 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -04002392 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002393 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002396 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002397 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002398 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002399 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002402 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04002403 blendFactor.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002404 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002405 case BLEND_CONSTANT:
2406 case 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;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002409 case BLEND_INVCONSTANT:
2410 case 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;
John Bauman66b8ab22014-05-06 15:57:45 -04002453 case FORMAT_A8:
2454 buffer = cBuffer + 1 * x;
2455 pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 0);
2456 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2457 pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 1);
2458 pixel.w = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
2459 pixel.x = Short4(0x0000);
2460 pixel.y = Short4(0x0000);
2461 pixel.z = Short4(0x0000);
2462 break;
John Bauman89401822014-05-06 15:04:28 -04002463 case FORMAT_X8R8G8B8:
2464 buffer = cBuffer + 4 * x;
2465 c01 = *Pointer<Short4>(buffer);
2466 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2467 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04002468 pixel.z = c01;
2469 pixel.y = c01;
2470 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
2471 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
2472 pixel.x = pixel.z;
2473 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
2474 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
2475 pixel.y = pixel.z;
2476 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
2477 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
2478 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
2479 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002480 break;
2481 case FORMAT_A8G8R8B8Q:
2482 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002483 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2484 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2485 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
2486 // pixel.w = UnpackHigh(As<Byte8>(pixel.w), *Pointer<Byte8>(cBuffer + 8 * x + 8));
John Bauman89401822014-05-06 15:04:28 -04002487 break;
2488 case FORMAT_X8G8R8B8Q:
2489 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002490 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2491 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2492 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
2493 // pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002494 break;
2495 case FORMAT_A16B16G16R16:
2496 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002497 pixel.x = *Pointer<Short4>(buffer + 8 * x);
2498 pixel.y = *Pointer<Short4>(buffer + 8 * x + 8);
John Bauman89401822014-05-06 15:04:28 -04002499 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002500 pixel.z = *Pointer<Short4>(buffer + 8 * x);
2501 pixel.w = *Pointer<Short4>(buffer + 8 * x + 8);
2502 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04002503 break;
2504 case FORMAT_G16R16:
2505 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002506 pixel.x = *Pointer<Short4>(buffer + 4 * x);
John Bauman89401822014-05-06 15:04:28 -04002507 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002508 pixel.y = *Pointer<Short4>(buffer + 4 * x);
2509 pixel.z = pixel.x;
2510 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.y));
2511 pixel.z = As<Short4>(UnpackHigh(pixel.z, pixel.y));
2512 pixel.y = pixel.z;
2513 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.z));
2514 pixel.y = As<Short4>(UnpackHigh(pixel.y, pixel.z));
2515 pixel.z = Short4(0xFFFFu);
2516 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002517 break;
2518 default:
2519 ASSERT(false);
2520 }
2521
2522 if(postBlendSRGB && state.writeSRGB)
2523 {
2524 sRGBtoLinear16_16(r, pixel);
2525 }
2526
2527 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
John Bauman19bac1e2014-05-06 15:23:49 -04002528 Vector4i sourceFactor;
2529 Vector4i destFactor;
John Bauman89401822014-05-06 15:04:28 -04002530
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002531 blendFactor(r, sourceFactor, current, pixel, state.sourceBlendFactor);
2532 blendFactor(r, destFactor, current, pixel, state.destBlendFactor);
John Bauman89401822014-05-06 15:04:28 -04002533
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002534 if(state.sourceBlendFactor != BLEND_ONE && state.sourceBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002535 {
John Bauman19bac1e2014-05-06 15:23:49 -04002536 current.x = MulHigh(As<UShort4>(current.x), As<UShort4>(sourceFactor.x));
2537 current.y = MulHigh(As<UShort4>(current.y), As<UShort4>(sourceFactor.y));
2538 current.z = MulHigh(As<UShort4>(current.z), As<UShort4>(sourceFactor.z));
John Bauman89401822014-05-06 15:04:28 -04002539 }
2540
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002541 if(state.destBlendFactor != BLEND_ONE && state.destBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002542 {
John Bauman19bac1e2014-05-06 15:23:49 -04002543 pixel.x = MulHigh(As<UShort4>(pixel.x), As<UShort4>(destFactor.x));
2544 pixel.y = MulHigh(As<UShort4>(pixel.y), As<UShort4>(destFactor.y));
2545 pixel.z = MulHigh(As<UShort4>(pixel.z), As<UShort4>(destFactor.z));
John Bauman89401822014-05-06 15:04:28 -04002546 }
2547
2548 switch(state.blendOperation)
2549 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002550 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002551 current.x = AddSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
2552 current.y = AddSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
2553 current.z = AddSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002554 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002555 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002556 current.x = SubSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
2557 current.y = SubSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
2558 current.z = SubSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002559 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002560 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002561 current.x = SubSat(As<UShort4>(pixel.x), As<UShort4>(current.x));
2562 current.y = SubSat(As<UShort4>(pixel.y), As<UShort4>(current.y));
2563 current.z = SubSat(As<UShort4>(pixel.z), As<UShort4>(current.z));
John Bauman89401822014-05-06 15:04:28 -04002564 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002565 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002566 current.x = Min(As<UShort4>(current.x), As<UShort4>(pixel.x));
2567 current.y = Min(As<UShort4>(current.y), As<UShort4>(pixel.y));
2568 current.z = Min(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002569 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002570 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002571 current.x = Max(As<UShort4>(current.x), As<UShort4>(pixel.x));
2572 current.y = Max(As<UShort4>(current.y), As<UShort4>(pixel.y));
2573 current.z = Max(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002574 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002575 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04002576 // No operation
2577 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002578 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002579 current.x = pixel.x;
2580 current.y = pixel.y;
2581 current.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002582 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002583 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002584 current.x = Short4(0x0000, 0x0000, 0x0000, 0x0000);
2585 current.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
2586 current.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04002587 break;
2588 default:
2589 ASSERT(false);
2590 }
2591
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002592 blendFactorAlpha(r, sourceFactor, current, pixel, state.sourceBlendFactorAlpha);
2593 blendFactorAlpha(r, destFactor, current, pixel, state.destBlendFactorAlpha);
John Bauman89401822014-05-06 15:04:28 -04002594
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002595 if(state.sourceBlendFactorAlpha != BLEND_ONE && state.sourceBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002596 {
John Bauman19bac1e2014-05-06 15:23:49 -04002597 current.w = MulHigh(As<UShort4>(current.w), As<UShort4>(sourceFactor.w));
John Bauman89401822014-05-06 15:04:28 -04002598 }
2599
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002600 if(state.destBlendFactorAlpha != BLEND_ONE && state.destBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002601 {
John Bauman19bac1e2014-05-06 15:23:49 -04002602 pixel.w = MulHigh(As<UShort4>(pixel.w), As<UShort4>(destFactor.w));
John Bauman89401822014-05-06 15:04:28 -04002603 }
2604
2605 switch(state.blendOperationAlpha)
2606 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002607 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002608 current.w = AddSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002609 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002610 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002611 current.w = SubSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002612 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002613 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002614 current.w = SubSat(As<UShort4>(pixel.w), As<UShort4>(current.w));
John Bauman89401822014-05-06 15:04:28 -04002615 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002616 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002617 current.w = Min(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002618 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002619 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002620 current.w = Max(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002621 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002622 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04002623 // No operation
2624 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002625 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002626 current.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002627 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002628 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002629 current.w = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04002630 break;
2631 default:
2632 ASSERT(false);
2633 }
2634 }
2635
John Bauman19bac1e2014-05-06 15:23:49 -04002636 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 -04002637 {
2638 if(!state.colorWriteActive(index))
2639 {
2640 return;
2641 }
2642
2643 if(postBlendSRGB && state.writeSRGB)
2644 {
2645 linearToSRGB16_16(r, current);
2646 }
2647
2648 if(exactColorRounding)
2649 {
2650 switch(state.targetFormat[index])
2651 {
2652 case FORMAT_X8G8R8B8Q:
2653 case FORMAT_A8G8R8B8Q:
2654 case FORMAT_X8R8G8B8:
2655 case FORMAT_A8R8G8B8:
2656 {
John Bauman19bac1e2014-05-06 15:23:49 -04002657 current.x = current.x - As<Short4>(As<UShort4>(current.x) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
2658 current.y = current.y - As<Short4>(As<UShort4>(current.y) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
2659 current.z = current.z - As<Short4>(As<UShort4>(current.z) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
2660 current.w = current.w - As<Short4>(As<UShort4>(current.w) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
John Bauman89401822014-05-06 15:04:28 -04002661 }
2662 break;
2663 }
2664 }
2665
2666 int rgbaWriteMask = state.colorWriteActive(index);
2667 int bgraWriteMask = rgbaWriteMask & 0x0000000A | (rgbaWriteMask & 0x00000001) << 2 | (rgbaWriteMask & 0x00000004) >> 2;
2668 int brgaWriteMask = rgbaWriteMask & 0x00000008 | (rgbaWriteMask & 0x00000001) << 1 | (rgbaWriteMask & 0x00000002) << 1 | (rgbaWriteMask & 0x00000004) >> 2;
2669
2670 switch(state.targetFormat[index])
2671 {
2672 case FORMAT_X8G8R8B8Q:
2673 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002674 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2675 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2676 // current.z = As<Short4>(As<UShort4>(current.z) >> 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.y)));
John Bauman89401822014-05-06 15:04:28 -04002680 break;
2681 case FORMAT_A8G8R8B8Q:
2682 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002683 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2684 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2685 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
2686 // current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002687
John Bauman19bac1e2014-05-06 15:23:49 -04002688 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2689 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04002690 break;
2691 case FORMAT_X8R8G8B8:
2692 case FORMAT_A8R8G8B8:
2693 if(state.targetFormat[index] == FORMAT_X8R8G8B8 || rgbaWriteMask == 0x7)
2694 {
John Bauman19bac1e2014-05-06 15:23:49 -04002695 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2696 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2697 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002698
John Bauman19bac1e2014-05-06 15:23:49 -04002699 current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2700 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
John Bauman89401822014-05-06 15:04:28 -04002701
John Bauman19bac1e2014-05-06 15:23:49 -04002702 current.x = current.z;
2703 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
2704 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
2705 current.y = current.z;
2706 current.z = As<Short4>(UnpackLow(current.z, current.x));
2707 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04002708 }
2709 else
2710 {
John Bauman19bac1e2014-05-06 15:23:49 -04002711 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2712 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2713 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
2714 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002715
John Bauman19bac1e2014-05-06 15:23:49 -04002716 current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2717 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04002718
John Bauman19bac1e2014-05-06 15:23:49 -04002719 current.x = current.z;
2720 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
2721 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
2722 current.y = current.z;
2723 current.z = As<Short4>(UnpackLow(current.z, current.x));
2724 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04002725 }
2726 break;
John Bauman66b8ab22014-05-06 15:57:45 -04002727 case FORMAT_A8:
2728 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
2729 current.w = As<Short4>(Pack(As<UShort4>(current.w), As<UShort4>(current.w)));
2730 break;
John Bauman89401822014-05-06 15:04:28 -04002731 case FORMAT_G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04002732 current.z = current.x;
2733 current.x = As<Short4>(UnpackLow(current.x, current.y));
2734 current.z = As<Short4>(UnpackHigh(current.z, current.y));
2735 current.y = current.z;
John Bauman89401822014-05-06 15:04:28 -04002736 break;
2737 case FORMAT_A16B16G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04002738 transpose4x4(current.x, current.y, current.z, current.w);
John Bauman89401822014-05-06 15:04:28 -04002739 break;
2740 case FORMAT_R32F:
2741 case FORMAT_G32R32F:
2742 case FORMAT_A32B32G32R32F:
2743 {
John Bauman19bac1e2014-05-06 15:23:49 -04002744 Vector4f oC;
John Bauman89401822014-05-06 15:04:28 -04002745
John Bauman19bac1e2014-05-06 15:23:49 -04002746 oC.x = convertUnsigned16(UShort4(current.x));
2747 oC.y = convertUnsigned16(UShort4(current.y));
2748 oC.z = convertUnsigned16(UShort4(current.z));
2749 oC.w = convertUnsigned16(UShort4(current.w));
John Bauman89401822014-05-06 15:04:28 -04002750
2751 writeColor(r, index, cBuffer, x, oC, sMask, zMask, cMask);
2752 }
2753 return;
2754 default:
2755 ASSERT(false);
2756 }
2757
John Bauman19bac1e2014-05-06 15:23:49 -04002758 Short4 c01 = current.z;
2759 Short4 c23 = current.y;
John Bauman89401822014-05-06 15:04:28 -04002760
2761 Int xMask; // Combination of all masks
2762
2763 if(state.depthTestActive)
2764 {
2765 xMask = zMask;
2766 }
2767 else
2768 {
2769 xMask = cMask;
2770 }
2771
2772 if(state.stencilActive)
2773 {
2774 xMask &= sMask;
2775 }
2776
2777 Pointer<Byte> buffer;
2778 Short4 value;
2779
2780 switch(state.targetFormat[index])
2781 {
2782 case FORMAT_A8G8R8B8Q:
2783 case FORMAT_X8G8R8B8Q: // FIXME: Don't touch alpha?
2784 UNIMPLEMENTED();
2785 // value = *Pointer<Short4>(cBuffer + 8 * x + 0);
2786
2787 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
2788 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
2789 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2790 // {
2791 // Short4 masked = value;
2792 // c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2793 // masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2794 // c01 |= masked;
2795 // }
2796
2797 // c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
2798 // value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
2799 // c01 |= value;
2800 // *Pointer<Short4>(cBuffer + 8 * x + 0) = c01;
2801
2802 // value = *Pointer<Short4>(cBuffer + 8 * x + 8);
2803
2804 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
2805 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
2806 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2807 // {
2808 // Short4 masked = value;
2809 // c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2810 // masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2811 // c23 |= masked;
2812 // }
2813
2814 // c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
2815 // value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
2816 // c23 |= value;
2817 // *Pointer<Short4>(cBuffer + 8 * x + 8) = c23;
2818 break;
2819 case FORMAT_A8R8G8B8:
2820 case FORMAT_X8R8G8B8: // FIXME: Don't touch alpha?
2821 buffer = cBuffer + x * 4;
2822 value = *Pointer<Short4>(buffer);
2823
2824 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
2825 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
2826 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2827 {
2828 Short4 masked = value;
2829 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2830 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2831 c01 |= masked;
2832 }
2833
2834 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
2835 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
2836 c01 |= value;
2837 *Pointer<Short4>(buffer) = c01;
2838
2839 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2840 value = *Pointer<Short4>(buffer);
2841
2842 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
2843 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
2844 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2845 {
2846 Short4 masked = value;
2847 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2848 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2849 c23 |= masked;
2850 }
2851
2852 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
2853 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
2854 c23 |= value;
2855 *Pointer<Short4>(buffer) = c23;
2856 break;
John Bauman66b8ab22014-05-06 15:57:45 -04002857 case FORMAT_A8:
2858 if(rgbaWriteMask & 0x00000008)
2859 {
2860 buffer = cBuffer + 1 * x;
2861 Insert(value, *Pointer<Short>(buffer), 0);
2862 Int pitch = *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2863 Insert(value, *Pointer<Short>(buffer + pitch), 1);
2864 value = UnpackLow(As<Byte8>(value), As<Byte8>(value));
2865
2866 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q) + 8 * xMask);
2867 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * xMask);
2868 current.w |= value;
2869
2870 *Pointer<Short>(buffer) = Extract(current.w, 0);
2871 *Pointer<Short>(buffer + pitch) = Extract(current.w, 1);
2872 }
2873 break;
John Bauman89401822014-05-06 15:04:28 -04002874 case FORMAT_G16R16:
2875 buffer = cBuffer + 4 * x;
2876
2877 value = *Pointer<Short4>(buffer);
2878
2879 if((rgbaWriteMask & 0x00000003) != 0x00000003)
2880 {
2881 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002882 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman89401822014-05-06 15:04:28 -04002883 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002884 current.x |= masked;
John Bauman89401822014-05-06 15:04:28 -04002885 }
2886
John Bauman19bac1e2014-05-06 15:23:49 -04002887 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04002888 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04002889 current.x |= value;
2890 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04002891
2892 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2893
2894 value = *Pointer<Short4>(buffer);
2895
2896 if((rgbaWriteMask & 0x00000003) != 0x00000003)
2897 {
2898 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002899 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman89401822014-05-06 15:04:28 -04002900 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002901 current.y |= masked;
John Bauman89401822014-05-06 15:04:28 -04002902 }
2903
John Bauman19bac1e2014-05-06 15:23:49 -04002904 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04002905 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04002906 current.y |= value;
2907 *Pointer<Short4>(buffer) = current.y;
John Bauman89401822014-05-06 15:04:28 -04002908 break;
2909 case FORMAT_A16B16G16R16:
2910 buffer = cBuffer + 8 * x;
2911
2912 {
2913 value = *Pointer<Short4>(buffer);
2914
2915 if(rgbaWriteMask != 0x0000000F)
2916 {
2917 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002918 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04002919 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002920 current.x |= masked;
John Bauman89401822014-05-06 15:04:28 -04002921 }
2922
John Bauman19bac1e2014-05-06 15:23:49 -04002923 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ0Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04002924 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ0Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04002925 current.x |= value;
2926 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04002927 }
2928
2929 {
2930 value = *Pointer<Short4>(buffer + 8);
2931
2932 if(rgbaWriteMask != 0x0000000F)
2933 {
2934 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002935 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04002936 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002937 current.y |= masked;
John Bauman89401822014-05-06 15:04:28 -04002938 }
2939
John Bauman19bac1e2014-05-06 15:23:49 -04002940 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ1Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04002941 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ1Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04002942 current.y |= value;
2943 *Pointer<Short4>(buffer + 8) = current.y;
John Bauman89401822014-05-06 15:04:28 -04002944 }
2945
2946 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2947
2948 {
2949 value = *Pointer<Short4>(buffer);
2950
2951 if(rgbaWriteMask != 0x0000000F)
2952 {
2953 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002954 current.z &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04002955 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002956 current.z |= masked;
John Bauman89401822014-05-06 15:04:28 -04002957 }
2958
John Bauman19bac1e2014-05-06 15:23:49 -04002959 current.z &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ2Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04002960 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ2Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04002961 current.z |= value;
2962 *Pointer<Short4>(buffer) = current.z;
John Bauman89401822014-05-06 15:04:28 -04002963 }
2964
2965 {
2966 value = *Pointer<Short4>(buffer + 8);
2967
2968 if(rgbaWriteMask != 0x0000000F)
2969 {
2970 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002971 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04002972 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002973 current.w |= masked;
John Bauman89401822014-05-06 15:04:28 -04002974 }
2975
John Bauman19bac1e2014-05-06 15:23:49 -04002976 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ3Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04002977 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ3Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04002978 current.w |= value;
2979 *Pointer<Short4>(buffer + 8) = current.w;
John Bauman89401822014-05-06 15:04:28 -04002980 }
2981 break;
2982 default:
2983 ASSERT(false);
2984 }
2985 }
2986
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002987 void PixelRoutine::blendFactor(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -04002988 {
2989 switch(blendFactorActive)
2990 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002991 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04002992 // Optimized
2993 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002994 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04002995 // Optimized
2996 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002997 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002998 blendFactor.x = oC.x;
2999 blendFactor.y = oC.y;
3000 blendFactor.z = oC.z;
John Bauman89401822014-05-06 15:04:28 -04003001 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003002 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003003 blendFactor.x = Float4(1.0f) - oC.x;
3004 blendFactor.y = Float4(1.0f) - oC.y;
3005 blendFactor.z = Float4(1.0f) - oC.z;
John Bauman89401822014-05-06 15:04:28 -04003006 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003007 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003008 blendFactor.x = pixel.x;
3009 blendFactor.y = pixel.y;
3010 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003011 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003012 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003013 blendFactor.x = Float4(1.0f) - pixel.x;
3014 blendFactor.y = Float4(1.0f) - pixel.y;
3015 blendFactor.z = Float4(1.0f) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003016 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003017 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003018 blendFactor.x = oC.w;
3019 blendFactor.y = oC.w;
3020 blendFactor.z = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003021 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003022 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003023 blendFactor.x = Float4(1.0f) - oC.w;
3024 blendFactor.y = Float4(1.0f) - oC.w;
3025 blendFactor.z = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04003026 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003027 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003028 blendFactor.x = pixel.w;
3029 blendFactor.y = pixel.w;
3030 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003031 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003032 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003033 blendFactor.x = Float4(1.0f) - pixel.w;
3034 blendFactor.y = Float4(1.0f) - pixel.w;
3035 blendFactor.z = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003036 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003037 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04003038 blendFactor.x = Float4(1.0f) - pixel.w;
3039 blendFactor.x = Min(blendFactor.x, oC.w);
3040 blendFactor.y = blendFactor.x;
3041 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -04003042 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003043 case BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003044 blendFactor.x = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[0]));
3045 blendFactor.y = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[1]));
3046 blendFactor.z = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04003047 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003048 case BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003049 blendFactor.x = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[0]));
3050 blendFactor.y = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[1]));
3051 blendFactor.z = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04003052 break;
3053 default:
3054 ASSERT(false);
3055 }
3056 }
3057
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003058 void PixelRoutine::blendFactorAlpha(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -04003059 {
3060 switch(blendFactorAlphaActive)
3061 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003062 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04003063 // Optimized
3064 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003065 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04003066 // Optimized
3067 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003068 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003069 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003070 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003071 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003072 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04003073 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003074 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003075 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003076 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003077 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003078 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003079 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003080 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003081 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003082 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003083 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003084 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04003085 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003086 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003087 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003088 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003089 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003090 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003091 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003092 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04003093 blendFactor.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003094 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003095 case BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003096 blendFactor.w = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04003097 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003098 case BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003099 blendFactor.w = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04003100 break;
3101 default:
3102 ASSERT(false);
3103 }
3104 }
3105
John Bauman19bac1e2014-05-06 15:23:49 -04003106 void PixelRoutine::alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4f &oC, Int &x)
John Bauman89401822014-05-06 15:04:28 -04003107 {
3108 if(!state.alphaBlendActive)
3109 {
3110 return;
3111 }
3112
3113 Pointer<Byte> buffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003114 Vector4f pixel;
John Bauman89401822014-05-06 15:04:28 -04003115
John Bauman19bac1e2014-05-06 15:23:49 -04003116 Vector4i color;
John Bauman89401822014-05-06 15:04:28 -04003117 Short4 c01;
3118 Short4 c23;
3119
3120 // Read pixel
3121 switch(state.targetFormat[index])
3122 {
3123 case FORMAT_A8R8G8B8:
3124 buffer = cBuffer + 4 * x;
3125 c01 = *Pointer<Short4>(buffer);
3126 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3127 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04003128 color.z = c01;
3129 color.y = c01;
3130 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(c23));
3131 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(c23));
3132 color.x = color.z;
3133 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.y));
3134 color.x = UnpackHigh(As<Byte8>(color.x), As<Byte8>(color.y));
3135 color.y = color.z;
3136 color.w = color.x;
3137 color.x = UnpackLow(As<Byte8>(color.x), As<Byte8>(color.x));
3138 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(color.y));
3139 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.z));
3140 color.w = UnpackHigh(As<Byte8>(color.w), As<Byte8>(color.w));
John Bauman89401822014-05-06 15:04:28 -04003141
John Bauman19bac1e2014-05-06 15:23:49 -04003142 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3143 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3144 pixel.z = convertUnsigned16(As<UShort4>(color.z));
3145 pixel.w = convertUnsigned16(As<UShort4>(color.w));
John Bauman89401822014-05-06 15:04:28 -04003146 break;
3147 case FORMAT_X8R8G8B8:
3148 buffer = cBuffer + 4 * x;
3149 c01 = *Pointer<Short4>(buffer);
3150 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3151 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04003152 color.z = c01;
3153 color.y = c01;
3154 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(c23));
3155 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(c23));
3156 color.x = color.z;
3157 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.y));
3158 color.x = UnpackHigh(As<Byte8>(color.x), As<Byte8>(color.y));
3159 color.y = color.z;
3160 color.x = UnpackLow(As<Byte8>(color.x), As<Byte8>(color.x));
3161 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(color.y));
3162 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.z));
John Bauman89401822014-05-06 15:04:28 -04003163
John Bauman19bac1e2014-05-06 15:23:49 -04003164 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3165 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3166 pixel.z = convertUnsigned16(As<UShort4>(color.z));
3167 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003168 break;
John Bauman66b8ab22014-05-06 15:57:45 -04003169 case FORMAT_A8:
3170 buffer = cBuffer + 1 * x;
3171 c01 = Insert(c01, *Pointer<Short>(buffer), 0);
3172 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3173 c01 = Insert(c01, *Pointer<Short>(buffer), 1);
3174 pixel.w = convertUnsigned16(As<UShort4>(UnpackLow(As<Byte8>(c01), As<Byte8>(c01))));
3175 pixel.x = Float4(0.0f);
3176 pixel.y = Float4(0.0f);
3177 pixel.z = Float4(0.0f);
3178 break;
John Bauman89401822014-05-06 15:04:28 -04003179 case FORMAT_A8G8R8B8Q:
John Bauman66b8ab22014-05-06 15:57:45 -04003180 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04003181 // UnpackLow(pixel.z, qword_ptr [cBuffer+8*x+0]);
3182 // UnpackHigh(pixel.x, qword_ptr [cBuffer+8*x+0]);
3183 // UnpackLow(pixel.y, qword_ptr [cBuffer+8*x+8]);
3184 // UnpackHigh(pixel.w, qword_ptr [cBuffer+8*x+8]);
John Bauman89401822014-05-06 15:04:28 -04003185 break;
3186 case FORMAT_X8G8R8B8Q:
John Bauman66b8ab22014-05-06 15:57:45 -04003187 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04003188 // UnpackLow(pixel.z, qword_ptr [cBuffer+8*x+0]);
3189 // UnpackHigh(pixel.x, qword_ptr [cBuffer+8*x+0]);
3190 // UnpackLow(pixel.y, qword_ptr [cBuffer+8*x+8]);
3191 // pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04003192 break;
3193 case FORMAT_A16B16G16R16:
3194 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003195 color.x = *Pointer<Short4>(buffer + 8 * x);
3196 color.y = *Pointer<Short4>(buffer + 8 * x + 8);
John Bauman89401822014-05-06 15:04:28 -04003197 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003198 color.z = *Pointer<Short4>(buffer + 8 * x);
3199 color.w = *Pointer<Short4>(buffer + 8 * x + 8);
John Bauman89401822014-05-06 15:04:28 -04003200
John Bauman19bac1e2014-05-06 15:23:49 -04003201 transpose4x4(color.x, color.y, color.z, color.w);
John Bauman89401822014-05-06 15:04:28 -04003202
John Bauman19bac1e2014-05-06 15:23:49 -04003203 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3204 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3205 pixel.z = convertUnsigned16(As<UShort4>(color.z));
3206 pixel.w = convertUnsigned16(As<UShort4>(color.w));
John Bauman89401822014-05-06 15:04:28 -04003207 break;
3208 case FORMAT_G16R16:
3209 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003210 color.x = *Pointer<Short4>(buffer + 4 * x);
John Bauman89401822014-05-06 15:04:28 -04003211 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003212 color.y = *Pointer<Short4>(buffer + 4 * x);
3213 color.z = color.x;
3214 color.x = As<Short4>(UnpackLow(color.x, color.y));
3215 color.z = As<Short4>(UnpackHigh(color.z, color.y));
3216 color.y = color.z;
3217 color.x = As<Short4>(UnpackLow(color.x, color.z));
3218 color.y = As<Short4>(UnpackHigh(color.y, color.z));
John Bauman89401822014-05-06 15:04:28 -04003219
John Bauman19bac1e2014-05-06 15:23:49 -04003220 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3221 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3222 pixel.z = Float4(1.0f);
3223 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003224 break;
3225 case FORMAT_R32F:
3226 buffer = cBuffer;
3227 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04003228 pixel.x.x = *Pointer<Float>(buffer + 4 * x + 0);
3229 pixel.x.y = *Pointer<Float>(buffer + 4 * x + 4);
John Bauman89401822014-05-06 15:04:28 -04003230 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3231 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04003232 pixel.x.z = *Pointer<Float>(buffer + 4 * x + 0);
3233 pixel.x.w = *Pointer<Float>(buffer + 4 * x + 4);
3234 pixel.y = Float4(1.0f);
3235 pixel.z = Float4(1.0f);
3236 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003237 break;
3238 case FORMAT_G32R32F:
3239 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003240 pixel.x = *Pointer<Float4>(buffer + 8 * x, 16);
John Bauman89401822014-05-06 15:04:28 -04003241 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003242 pixel.y = *Pointer<Float4>(buffer + 8 * x, 16);
3243 pixel.z = pixel.x;
3244 pixel.x = ShuffleLowHigh(pixel.x, pixel.y, 0x88);
3245 pixel.z = ShuffleLowHigh(pixel.z, pixel.y, 0xDD);
3246 pixel.y = pixel.z;
3247 pixel.z = Float4(1.0f);
3248 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003249 break;
3250 case FORMAT_A32B32G32R32F:
3251 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003252 pixel.x = *Pointer<Float4>(buffer + 16 * x, 16);
3253 pixel.y = *Pointer<Float4>(buffer + 16 * x + 16, 16);
John Bauman89401822014-05-06 15:04:28 -04003254 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003255 pixel.z = *Pointer<Float4>(buffer + 16 * x, 16);
3256 pixel.w = *Pointer<Float4>(buffer + 16 * x + 16, 16);
3257 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04003258 break;
3259 default:
3260 ASSERT(false);
3261 }
3262
3263 if(postBlendSRGB && state.writeSRGB)
3264 {
John Bauman19bac1e2014-05-06 15:23:49 -04003265 sRGBtoLinear(pixel.x);
3266 sRGBtoLinear(pixel.y);
3267 sRGBtoLinear(pixel.z);
John Bauman89401822014-05-06 15:04:28 -04003268 }
3269
3270 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
John Bauman19bac1e2014-05-06 15:23:49 -04003271 Vector4f sourceFactor;
3272 Vector4f destFactor;
John Bauman89401822014-05-06 15:04:28 -04003273
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003274 blendFactor(r, sourceFactor, oC, pixel, state.sourceBlendFactor);
3275 blendFactor(r, destFactor, oC, pixel, state.destBlendFactor);
John Bauman89401822014-05-06 15:04:28 -04003276
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003277 if(state.sourceBlendFactor != BLEND_ONE && state.sourceBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04003278 {
John Bauman19bac1e2014-05-06 15:23:49 -04003279 oC.x *= sourceFactor.x;
3280 oC.y *= sourceFactor.y;
3281 oC.z *= sourceFactor.z;
John Bauman89401822014-05-06 15:04:28 -04003282 }
3283
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003284 if(state.destBlendFactor != BLEND_ONE && state.destBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04003285 {
John Bauman19bac1e2014-05-06 15:23:49 -04003286 pixel.x *= destFactor.x;
3287 pixel.y *= destFactor.y;
3288 pixel.z *= destFactor.z;
John Bauman89401822014-05-06 15:04:28 -04003289 }
3290
3291 switch(state.blendOperation)
3292 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003293 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04003294 oC.x += pixel.x;
3295 oC.y += pixel.y;
3296 oC.z += pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003297 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003298 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003299 oC.x -= pixel.x;
3300 oC.y -= pixel.y;
3301 oC.z -= pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003302 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003303 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003304 oC.x = pixel.x - oC.x;
3305 oC.y = pixel.y - oC.y;
3306 oC.z = pixel.z - oC.z;
John Bauman89401822014-05-06 15:04:28 -04003307 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003308 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04003309 oC.x = Min(oC.x, pixel.x);
3310 oC.y = Min(oC.y, pixel.y);
3311 oC.z = Min(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04003312 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003313 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04003314 oC.x = Max(oC.x, pixel.x);
3315 oC.y = Max(oC.y, pixel.y);
3316 oC.z = Max(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04003317 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003318 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04003319 // No operation
3320 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003321 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003322 oC.x = pixel.x;
3323 oC.y = pixel.y;
3324 oC.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003325 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003326 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04003327 oC.x = Float4(0.0f);
3328 oC.y = Float4(0.0f);
3329 oC.z = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04003330 break;
3331 default:
3332 ASSERT(false);
3333 }
3334
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003335 blendFactorAlpha(r, sourceFactor, oC, pixel, state.sourceBlendFactorAlpha);
3336 blendFactorAlpha(r, destFactor, oC, pixel, state.destBlendFactorAlpha);
John Bauman89401822014-05-06 15:04:28 -04003337
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003338 if(state.sourceBlendFactorAlpha != BLEND_ONE && state.sourceBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04003339 {
John Bauman19bac1e2014-05-06 15:23:49 -04003340 oC.w *= sourceFactor.w;
John Bauman89401822014-05-06 15:04:28 -04003341 }
3342
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003343 if(state.destBlendFactorAlpha != BLEND_ONE && state.destBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04003344 {
John Bauman19bac1e2014-05-06 15:23:49 -04003345 pixel.w *= destFactor.w;
John Bauman89401822014-05-06 15:04:28 -04003346 }
3347
3348 switch(state.blendOperationAlpha)
3349 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003350 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04003351 oC.w += pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003352 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003353 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003354 oC.w -= pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003355 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003356 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003357 pixel.w -= oC.w;
3358 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003359 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003360 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04003361 oC.w = Min(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04003362 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003363 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04003364 oC.w = Max(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04003365 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003366 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04003367 // No operation
3368 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003369 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003370 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003371 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003372 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04003373 oC.w = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04003374 break;
3375 default:
3376 ASSERT(false);
3377 }
3378 }
3379
John Bauman19bac1e2014-05-06 15:23:49 -04003380 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 -04003381 {
3382 if(!state.colorWriteActive(index))
3383 {
3384 return;
3385 }
3386
John Bauman19bac1e2014-05-06 15:23:49 -04003387 Vector4i color;
John Bauman89401822014-05-06 15:04:28 -04003388
3389 switch(state.targetFormat[index])
3390 {
3391 case FORMAT_X8R8G8B8:
3392 case FORMAT_A8R8G8B8:
John Bauman66b8ab22014-05-06 15:57:45 -04003393 case FORMAT_A8:
John Bauman89401822014-05-06 15:04:28 -04003394 case FORMAT_G16R16:
3395 case FORMAT_A16B16G16R16:
3396 convertFixed16(color, oC, true);
3397 writeColor(r, index, cBuffer, x, color, sMask, zMask, cMask);
3398 return;
3399 case FORMAT_R32F:
3400 break;
3401 case FORMAT_G32R32F:
John Bauman19bac1e2014-05-06 15:23:49 -04003402 oC.z = oC.x;
3403 oC.x = UnpackLow(oC.x, oC.y);
3404 oC.z = UnpackHigh(oC.z, oC.y);
3405 oC.y = oC.z;
John Bauman89401822014-05-06 15:04:28 -04003406 break;
3407 case FORMAT_A32B32G32R32F:
John Bauman19bac1e2014-05-06 15:23:49 -04003408 transpose4x4(oC.x, oC.y, oC.z, oC.w);
John Bauman89401822014-05-06 15:04:28 -04003409 break;
3410 default:
3411 ASSERT(false);
3412 }
3413
3414 int rgbaWriteMask = state.colorWriteActive(index);
3415
3416 Int xMask; // Combination of all masks
3417
3418 if(state.depthTestActive)
3419 {
3420 xMask = zMask;
3421 }
3422 else
3423 {
3424 xMask = cMask;
3425 }
3426
3427 if(state.stencilActive)
3428 {
3429 xMask &= sMask;
3430 }
3431
3432 Pointer<Byte> buffer;
3433 Float4 value;
3434
3435 switch(state.targetFormat[index])
3436 {
3437 case FORMAT_R32F:
3438 if(rgbaWriteMask & 0x00000001)
3439 {
3440 buffer = cBuffer + 4 * x;
3441
3442 // FIXME: movlps
3443 value.x = *Pointer<Float>(buffer + 0);
3444 value.y = *Pointer<Float>(buffer + 4);
3445
3446 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3447
3448 // FIXME: movhps
3449 value.z = *Pointer<Float>(buffer + 0);
3450 value.w = *Pointer<Float>(buffer + 4);
3451
John Bauman19bac1e2014-05-06 15:23:49 -04003452 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 -04003453 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003454 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
John Bauman89401822014-05-06 15:04:28 -04003455
3456 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04003457 *Pointer<Float>(buffer + 0) = oC.x.z;
3458 *Pointer<Float>(buffer + 4) = oC.x.w;
John Bauman89401822014-05-06 15:04:28 -04003459
3460 buffer -= *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3461
3462 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04003463 *Pointer<Float>(buffer + 0) = oC.x.x;
3464 *Pointer<Float>(buffer + 4) = oC.x.y;
John Bauman89401822014-05-06 15:04:28 -04003465 }
3466 break;
3467 case FORMAT_G32R32F:
3468 buffer = cBuffer + 8 * x;
3469
3470 value = *Pointer<Float4>(buffer);
3471
3472 if((rgbaWriteMask & 0x00000003) != 0x00000003)
3473 {
3474 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003475 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 -04003476 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD01X[rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003477 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003478 }
3479
John Bauman19bac1e2014-05-06 15:23:49 -04003480 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 -04003481 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskQ01X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003482 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
3483 *Pointer<Float4>(buffer) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04003484
3485 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3486
3487 value = *Pointer<Float4>(buffer);
3488
3489 if((rgbaWriteMask & 0x00000003) != 0x00000003)
3490 {
3491 Float4 masked;
3492
3493 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003494 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 -04003495 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD01X[rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003496 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003497 }
3498
John Bauman19bac1e2014-05-06 15:23:49 -04003499 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 -04003500 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskQ23X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003501 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
3502 *Pointer<Float4>(buffer) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04003503 break;
3504 case FORMAT_A32B32G32R32F:
3505 buffer = cBuffer + 16 * x;
3506
3507 {
3508 value = *Pointer<Float4>(buffer, 16);
3509
3510 if(rgbaWriteMask != 0x0000000F)
3511 {
3512 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003513 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003514 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003515 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003516 }
3517
John Bauman19bac1e2014-05-06 15:23:49 -04003518 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 -04003519 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX0X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003520 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
3521 *Pointer<Float4>(buffer, 16) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04003522 }
3523
3524 {
3525 value = *Pointer<Float4>(buffer + 16, 16);
3526
3527 if(rgbaWriteMask != 0x0000000F)
3528 {
3529 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003530 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003531 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003532 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003533 }
3534
John Bauman19bac1e2014-05-06 15:23:49 -04003535 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 -04003536 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX1X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003537 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
3538 *Pointer<Float4>(buffer + 16, 16) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04003539 }
3540
3541 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3542
3543 {
3544 value = *Pointer<Float4>(buffer, 16);
3545
3546 if(rgbaWriteMask != 0x0000000F)
3547 {
3548 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003549 oC.z = As<Float4>(As<Int4>(oC.z) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003550 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003551 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003552 }
3553
John Bauman19bac1e2014-05-06 15:23:49 -04003554 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 -04003555 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX2X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003556 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(value));
3557 *Pointer<Float4>(buffer, 16) = oC.z;
John Bauman89401822014-05-06 15:04:28 -04003558 }
3559
3560 {
3561 value = *Pointer<Float4>(buffer + 16, 16);
3562
3563 if(rgbaWriteMask != 0x0000000F)
3564 {
3565 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003566 oC.w = As<Float4>(As<Int4>(oC.w) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003567 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003568 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003569 }
3570
John Bauman19bac1e2014-05-06 15:23:49 -04003571 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 -04003572 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX3X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003573 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(value));
3574 *Pointer<Float4>(buffer + 16, 16) = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003575 }
3576 break;
3577 default:
3578 ASSERT(false);
3579 }
3580 }
3581
3582 void PixelRoutine::ps_1_x(Registers &r, Int cMask[4])
3583 {
3584 int pad = 0; // Count number of texm3x3pad instructions
John Bauman19bac1e2014-05-06 15:23:49 -04003585 Vector4i dPairing; // Destination for first pairing instruction
John Bauman89401822014-05-06 15:04:28 -04003586
Alexis Hetu903e0252014-11-25 14:25:32 -05003587 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -04003588 {
John Bauman19bac1e2014-05-06 15:23:49 -04003589 const Shader::Instruction *instruction = shader->getInstruction(i);
3590 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -04003591
3592 // #ifndef NDEBUG // FIXME: Centralize debug output control
John Bauman19bac1e2014-05-06 15:23:49 -04003593 // shader->printInstruction(i, "debug.txt");
John Bauman89401822014-05-06 15:04:28 -04003594 // #endif
3595
John Bauman19bac1e2014-05-06 15:23:49 -04003596 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -04003597 {
3598 continue;
3599 }
3600
John Bauman19bac1e2014-05-06 15:23:49 -04003601 const Dst &dst = instruction->dst;
3602 const Src &src0 = instruction->src[0];
3603 const Src &src1 = instruction->src[1];
3604 const Src &src2 = instruction->src[2];
John Bauman89401822014-05-06 15:04:28 -04003605
John Bauman19bac1e2014-05-06 15:23:49 -04003606 unsigned short version = shader->getVersion();
3607 bool pairing = i + 1 < shader->getLength() && shader->getInstruction(i + 1)->coissue; // First instruction of pair
3608 bool coissue = instruction->coissue; // Second instruction of pair
John Bauman89401822014-05-06 15:04:28 -04003609
John Bauman19bac1e2014-05-06 15:23:49 -04003610 Vector4i d;
3611 Vector4i s0;
3612 Vector4i s1;
3613 Vector4i s2;
John Bauman89401822014-05-06 15:04:28 -04003614
John Bauman19bac1e2014-05-06 15:23:49 -04003615 if(src0.type != Shader::PARAMETER_VOID) s0 = regi(r, src0);
3616 if(src1.type != Shader::PARAMETER_VOID) s1 = regi(r, src1);
3617 if(src2.type != Shader::PARAMETER_VOID) s2 = regi(r, src2);
3618
3619 Float4 u = version < 0x0104 ? r.vf[2 + dst.index].x : r.vf[2 + src0.index].x;
3620 Float4 v = version < 0x0104 ? r.vf[2 + dst.index].y : r.vf[2 + src0.index].y;
3621 Float4 s = version < 0x0104 ? r.vf[2 + dst.index].z : r.vf[2 + src0.index].z;
3622 Float4 t = version < 0x0104 ? r.vf[2 + dst.index].w : r.vf[2 + src0.index].w;
John Bauman89401822014-05-06 15:04:28 -04003623
3624 switch(opcode)
3625 {
John Bauman19bac1e2014-05-06 15:23:49 -04003626 case Shader::OPCODE_PS_1_0: break;
3627 case Shader::OPCODE_PS_1_1: break;
3628 case Shader::OPCODE_PS_1_2: break;
3629 case Shader::OPCODE_PS_1_3: break;
3630 case Shader::OPCODE_PS_1_4: break;
John Bauman89401822014-05-06 15:04:28 -04003631
John Bauman19bac1e2014-05-06 15:23:49 -04003632 case Shader::OPCODE_DEF: break;
John Bauman89401822014-05-06 15:04:28 -04003633
John Bauman19bac1e2014-05-06 15:23:49 -04003634 case Shader::OPCODE_NOP: break;
3635 case Shader::OPCODE_MOV: MOV(d, s0); break;
3636 case Shader::OPCODE_ADD: ADD(d, s0, s1); break;
3637 case Shader::OPCODE_SUB: SUB(d, s0, s1); break;
3638 case Shader::OPCODE_MAD: MAD(d, s0, s1, s2); break;
3639 case Shader::OPCODE_MUL: MUL(d, s0, s1); break;
3640 case Shader::OPCODE_DP3: DP3(d, s0, s1); break;
3641 case Shader::OPCODE_DP4: DP4(d, s0, s1); break;
3642 case Shader::OPCODE_LRP: LRP(d, s0, s1, s2); break;
3643 case Shader::OPCODE_TEXCOORD:
3644 if(version < 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003645 {
John Bauman19bac1e2014-05-06 15:23:49 -04003646 TEXCOORD(d, u, v, s, dst.index);
John Bauman89401822014-05-06 15:04:28 -04003647 }
3648 else
3649 {
3650 if((src0.swizzle & 0x30) == 0x20) // .xyz
3651 {
John Bauman19bac1e2014-05-06 15:23:49 -04003652 TEXCRD(d, u, v, s, src0.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003653 }
3654 else // .xyw
3655 {
John Bauman19bac1e2014-05-06 15:23:49 -04003656 TEXCRD(d, u, v, t, src0.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003657 }
3658 }
3659 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003660 case Shader::OPCODE_TEXKILL:
3661 if(version < 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003662 {
John Bauman19bac1e2014-05-06 15:23:49 -04003663 TEXKILL(cMask, u, v, s);
John Bauman89401822014-05-06 15:04:28 -04003664 }
John Bauman19bac1e2014-05-06 15:23:49 -04003665 else if(version == 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003666 {
John Bauman19bac1e2014-05-06 15:23:49 -04003667 if(dst.type == Shader::PARAMETER_TEXTURE)
John Bauman89401822014-05-06 15:04:28 -04003668 {
John Bauman19bac1e2014-05-06 15:23:49 -04003669 TEXKILL(cMask, u, v, s);
John Bauman89401822014-05-06 15:04:28 -04003670 }
3671 else
3672 {
3673 TEXKILL(cMask, r.ri[dst.index]);
3674 }
3675 }
3676 else ASSERT(false);
3677 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003678 case Shader::OPCODE_TEX:
3679 if(version < 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003680 {
John Bauman19bac1e2014-05-06 15:23:49 -04003681 TEX(r, d, u, v, s, dst.index, false);
John Bauman89401822014-05-06 15:04:28 -04003682 }
John Bauman19bac1e2014-05-06 15:23:49 -04003683 else if(version == 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003684 {
John Bauman19bac1e2014-05-06 15:23:49 -04003685 if(src0.type == Shader::PARAMETER_TEXTURE)
John Bauman89401822014-05-06 15:04:28 -04003686 {
3687 if((src0.swizzle & 0x30) == 0x20) // .xyz
3688 {
John Bauman19bac1e2014-05-06 15:23:49 -04003689 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 -04003690 }
3691 else // .xyw
3692 {
John Bauman19bac1e2014-05-06 15:23:49 -04003693 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 -04003694 }
3695 }
3696 else
3697 {
John Bauman19bac1e2014-05-06 15:23:49 -04003698 TEXLD(r, d, s0, dst.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003699 }
3700 }
3701 else ASSERT(false);
3702 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003703 case Shader::OPCODE_TEXBEM: TEXBEM(r, d, s0, u, v, s, dst.index); break;
3704 case Shader::OPCODE_TEXBEML: TEXBEML(r, d, s0, u, v, s, dst.index); break;
3705 case Shader::OPCODE_TEXREG2AR: TEXREG2AR(r, d, s0, dst.index); break;
3706 case Shader::OPCODE_TEXREG2GB: TEXREG2GB(r, d, s0, dst.index); break;
3707 case Shader::OPCODE_TEXM3X2PAD: TEXM3X2PAD(r, u, v, s, s0, 0, src0.modifier == Shader::MODIFIER_SIGN); break;
3708 case Shader::OPCODE_TEXM3X2TEX: TEXM3X2TEX(r, d, u, v, s, dst.index, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3709 case Shader::OPCODE_TEXM3X3PAD: TEXM3X3PAD(r, u, v, s, s0, pad++ % 2, src0.modifier == Shader::MODIFIER_SIGN); break;
3710 case Shader::OPCODE_TEXM3X3TEX: TEXM3X3TEX(r, d, u, v, s, dst.index, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3711 case Shader::OPCODE_TEXM3X3SPEC: TEXM3X3SPEC(r, d, u, v, s, dst.index, s0, s1); break;
3712 case Shader::OPCODE_TEXM3X3VSPEC: TEXM3X3VSPEC(r, d, u, v, s, dst.index, s0); break;
3713 case Shader::OPCODE_CND: CND(d, s0, s1, s2); break;
3714 case Shader::OPCODE_TEXREG2RGB: TEXREG2RGB(r, d, s0, dst.index); break;
3715 case Shader::OPCODE_TEXDP3TEX: TEXDP3TEX(r, d, u, v, s, dst.index, s0); break;
3716 case Shader::OPCODE_TEXM3X2DEPTH: TEXM3X2DEPTH(r, d, u, v, s, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3717 case Shader::OPCODE_TEXDP3: TEXDP3(r, d, u, v, s, s0); break;
3718 case Shader::OPCODE_TEXM3X3: TEXM3X3(r, d, u, v, s, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3719 case Shader::OPCODE_TEXDEPTH: TEXDEPTH(r); break;
3720 case Shader::OPCODE_CMP0: CMP(d, s0, s1, s2); break;
3721 case Shader::OPCODE_BEM: BEM(r, d, s0, s1, dst.index); break;
3722 case Shader::OPCODE_PHASE: break;
3723 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -04003724 default:
3725 ASSERT(false);
3726 }
3727
John Bauman19bac1e2014-05-06 15:23:49 -04003728 if(dst.type != Shader::PARAMETER_VOID && opcode != Shader::OPCODE_TEXKILL)
John Bauman89401822014-05-06 15:04:28 -04003729 {
3730 if(dst.shift > 0)
3731 {
John Bauman19bac1e2014-05-06 15:23:49 -04003732 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);}
3733 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);}
3734 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);}
3735 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 -04003736 }
3737 else if(dst.shift < 0)
3738 {
John Bauman19bac1e2014-05-06 15:23:49 -04003739 if(dst.mask & 0x1) d.x = d.x >> -dst.shift;
3740 if(dst.mask & 0x2) d.y = d.y >> -dst.shift;
3741 if(dst.mask & 0x4) d.z = d.z >> -dst.shift;
3742 if(dst.mask & 0x8) d.w = d.w >> -dst.shift;
John Bauman89401822014-05-06 15:04:28 -04003743 }
3744
3745 if(dst.saturate)
3746 {
John Bauman19bac1e2014-05-06 15:23:49 -04003747 if(dst.mask & 0x1) {d.x = Min(d.x, Short4(0x1000)); d.x = Max(d.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
3748 if(dst.mask & 0x2) {d.y = Min(d.y, Short4(0x1000)); d.y = Max(d.y, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
3749 if(dst.mask & 0x4) {d.z = Min(d.z, Short4(0x1000)); d.z = Max(d.z, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
3750 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 -04003751 }
3752
3753 if(pairing)
3754 {
John Bauman19bac1e2014-05-06 15:23:49 -04003755 if(dst.mask & 0x1) dPairing.x = d.x;
3756 if(dst.mask & 0x2) dPairing.y = d.y;
3757 if(dst.mask & 0x4) dPairing.z = d.z;
3758 if(dst.mask & 0x8) dPairing.w = d.w;
John Bauman89401822014-05-06 15:04:28 -04003759 }
3760
3761 if(coissue)
3762 {
John Bauman19bac1e2014-05-06 15:23:49 -04003763 const Dst &dst = shader->getInstruction(i - 1)->dst;
John Bauman89401822014-05-06 15:04:28 -04003764
3765 writeDestination(r, dPairing, dst);
3766 }
3767
3768 if(!pairing)
3769 {
3770 writeDestination(r, d, dst);
3771 }
3772 }
3773 }
3774 }
3775
3776 void PixelRoutine::ps_2_x(Registers &r, Int cMask[4])
3777 {
3778 r.enableIndex = 0;
3779 r.stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -04003780
Nicolas Capens4677a5f2014-05-06 16:42:26 -04003781 if(shader->containsLeaveInstruction())
3782 {
3783 r.enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3784 }
3785
John Bauman19bac1e2014-05-06 15:23:49 -04003786 bool out[4][4] = {false};
3787
3788 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -05003789 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -04003790 {
John Bauman19bac1e2014-05-06 15:23:49 -04003791 const Shader::Instruction *instruction = shader->getInstruction(i);
3792 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -04003793
John Bauman19bac1e2014-05-06 15:23:49 -04003794 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
3795 {
3796 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -04003797
John Bauman19bac1e2014-05-06 15:23:49 -04003798 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
3799 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
3800 }
3801 }
3802
Alexis Hetu903e0252014-11-25 14:25:32 -05003803 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -04003804 {
3805 const Shader::Instruction *instruction = shader->getInstruction(i);
3806 Shader::Opcode opcode = instruction->opcode;
3807
3808 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -04003809 {
3810 continue;
3811 }
3812
John Bauman19bac1e2014-05-06 15:23:49 -04003813 const Dst &dst = instruction->dst;
3814 const Src &src0 = instruction->src[0];
3815 const Src &src1 = instruction->src[1];
3816 const Src &src2 = instruction->src[2];
3817 const Src &src3 = instruction->src[3];
John Bauman89401822014-05-06 15:04:28 -04003818
John Bauman19bac1e2014-05-06 15:23:49 -04003819 bool predicate = instruction->predicate;
3820 Control control = instruction->control;
John Bauman89401822014-05-06 15:04:28 -04003821 bool pp = dst.partialPrecision;
John Bauman19bac1e2014-05-06 15:23:49 -04003822 bool project = instruction->project;
3823 bool bias = instruction->bias;
John Bauman89401822014-05-06 15:04:28 -04003824
John Bauman19bac1e2014-05-06 15:23:49 -04003825 Vector4f d;
3826 Vector4f s0;
3827 Vector4f s1;
3828 Vector4f s2;
3829 Vector4f s3;
John Bauman89401822014-05-06 15:04:28 -04003830
John Bauman19bac1e2014-05-06 15:23:49 -04003831 if(opcode == Shader::OPCODE_TEXKILL) // Takes destination as input
John Bauman89401822014-05-06 15:04:28 -04003832 {
John Bauman19bac1e2014-05-06 15:23:49 -04003833 if(dst.type == Shader::PARAMETER_TEXTURE)
John Bauman89401822014-05-06 15:04:28 -04003834 {
John Bauman19bac1e2014-05-06 15:23:49 -04003835 d.x = r.vf[2 + dst.index].x;
3836 d.y = r.vf[2 + dst.index].y;
3837 d.z = r.vf[2 + dst.index].z;
3838 d.w = r.vf[2 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -04003839 }
3840 else
3841 {
3842 d = r.rf[dst.index];
3843 }
3844 }
3845
John Bauman19bac1e2014-05-06 15:23:49 -04003846 if(src0.type != Shader::PARAMETER_VOID) s0 = reg(r, src0);
3847 if(src1.type != Shader::PARAMETER_VOID) s1 = reg(r, src1);
3848 if(src2.type != Shader::PARAMETER_VOID) s2 = reg(r, src2);
3849 if(src3.type != Shader::PARAMETER_VOID) s3 = reg(r, src3);
John Bauman89401822014-05-06 15:04:28 -04003850
3851 switch(opcode)
3852 {
John Bauman19bac1e2014-05-06 15:23:49 -04003853 case Shader::OPCODE_PS_2_0: break;
3854 case Shader::OPCODE_PS_2_x: break;
3855 case Shader::OPCODE_PS_3_0: break;
3856 case Shader::OPCODE_DEF: break;
3857 case Shader::OPCODE_DCL: break;
3858 case Shader::OPCODE_NOP: break;
3859 case Shader::OPCODE_MOV: mov(d, s0); break;
3860 case Shader::OPCODE_F2B: f2b(d, s0); break;
3861 case Shader::OPCODE_B2F: b2f(d, s0); break;
3862 case Shader::OPCODE_ADD: add(d, s0, s1); break;
3863 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
3864 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
3865 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
3866 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
3867 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
3868 case Shader::OPCODE_DP2ADD: dp2add(d, s0, s1, s2); break;
3869 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
3870 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
3871 case Shader::OPCODE_CMP0: cmp0(d, s0, s1, s2); break;
3872 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
3873 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
3874 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
3875 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
3876 case Shader::OPCODE_FRC: frc(d, s0); break;
3877 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
3878 case Shader::OPCODE_FLOOR: floor(d, s0); break;
3879 case Shader::OPCODE_CEIL: ceil(d, s0); break;
3880 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
3881 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
3882 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
3883 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
3884 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
3885 case Shader::OPCODE_LOG: log(d, s0, pp); break;
3886 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
3887 case Shader::OPCODE_DIV: div(d, s0, s1); break;
3888 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
3889 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
3890 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
3891 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
3892 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
3893 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
3894 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
3895 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
3896 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
3897 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
3898 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
3899 case Shader::OPCODE_MIN: min(d, s0, s1); break;
3900 case Shader::OPCODE_MAX: max(d, s0, s1); break;
3901 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
3902 case Shader::OPCODE_STEP: step(d, s0, s1); break;
3903 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
3904 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
3905 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
3906 case Shader::OPCODE_SGN: sgn(d, s0); break;
3907 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
3908 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
3909 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
3910 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
3911 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
3912 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
3913 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
3914 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
3915 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
3916 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
3917 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
3918 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
3919 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
3920 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
3921 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
3922 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
3923 case Shader::OPCODE_ABS: abs(d, s0); break;
3924 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
3925 case Shader::OPCODE_COS: cos(d, s0, pp); break;
3926 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
3927 case Shader::OPCODE_TAN: tan(d, s0, pp); break;
3928 case Shader::OPCODE_ACOS: acos(d, s0, pp); break;
3929 case Shader::OPCODE_ASIN: asin(d, s0, pp); break;
3930 case Shader::OPCODE_ATAN: atan(d, s0, pp); break;
3931 case Shader::OPCODE_ATAN2: atan2(d, s0, s1, pp); break;
3932 case Shader::OPCODE_M4X4: M4X4(r, d, s0, src1); break;
3933 case Shader::OPCODE_M4X3: M4X3(r, d, s0, src1); break;
3934 case Shader::OPCODE_M3X4: M3X4(r, d, s0, src1); break;
3935 case Shader::OPCODE_M3X3: M3X3(r, d, s0, src1); break;
3936 case Shader::OPCODE_M3X2: M3X2(r, d, s0, src1); break;
3937 case Shader::OPCODE_TEX: TEXLD(r, d, s0, src1, project, bias); break;
3938 case Shader::OPCODE_TEXLDD: TEXLDD(r, d, s0, src1, s2, s3, project, bias); break;
3939 case Shader::OPCODE_TEXLDL: TEXLDL(r, d, s0, src1, project, bias); break;
3940 case Shader::OPCODE_TEXKILL: TEXKILL(cMask, d, dst.mask); break;
3941 case Shader::OPCODE_DISCARD: DISCARD(r, cMask, instruction); break;
3942 case Shader::OPCODE_DFDX: DFDX(d, s0); break;
3943 case Shader::OPCODE_DFDY: DFDY(d, s0); break;
3944 case Shader::OPCODE_FWIDTH: FWIDTH(d, s0); break;
3945 case Shader::OPCODE_BREAK: BREAK(r); break;
3946 case Shader::OPCODE_BREAKC: BREAKC(r, s0, s1, control); break;
3947 case Shader::OPCODE_BREAKP: BREAKP(r, src0); break;
3948 case Shader::OPCODE_CONTINUE: CONTINUE(r); break;
3949 case Shader::OPCODE_TEST: TEST(); break;
3950 case Shader::OPCODE_CALL: CALL(r, dst.label, dst.callSite); break;
3951 case Shader::OPCODE_CALLNZ: CALLNZ(r, dst.label, dst.callSite, src0); break;
3952 case Shader::OPCODE_ELSE: ELSE(r); break;
3953 case Shader::OPCODE_ENDIF: ENDIF(r); break;
3954 case Shader::OPCODE_ENDLOOP: ENDLOOP(r); break;
3955 case Shader::OPCODE_ENDREP: ENDREP(r); break;
3956 case Shader::OPCODE_ENDWHILE: ENDWHILE(r); break;
3957 case Shader::OPCODE_IF: IF(r, src0); break;
3958 case Shader::OPCODE_IFC: IFC(r, s0, s1, control); break;
3959 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
3960 case Shader::OPCODE_LOOP: LOOP(r, src1); break;
3961 case Shader::OPCODE_REP: REP(r, src0); break;
3962 case Shader::OPCODE_WHILE: WHILE(r, src0); break;
3963 case Shader::OPCODE_RET: RET(r); break;
3964 case Shader::OPCODE_LEAVE: LEAVE(r); break;
3965 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
3966 case Shader::OPCODE_ALL: all(d.x, s0); break;
3967 case Shader::OPCODE_ANY: any(d.x, s0); break;
3968 case Shader::OPCODE_NOT: not(d, s0); break;
3969 case Shader::OPCODE_OR: or(d.x, s0.x, s1.x); break;
3970 case Shader::OPCODE_XOR: xor(d.x, s0.x, s1.x); break;
3971 case Shader::OPCODE_AND: and(d.x, s0.x, s1.x); break;
3972 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -04003973 default:
3974 ASSERT(false);
3975 }
3976
John Bauman19bac1e2014-05-06 15:23:49 -04003977 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 -04003978 {
John Bauman19bac1e2014-05-06 15:23:49 -04003979 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -04003980 {
John Bauman19bac1e2014-05-06 15:23:49 -04003981 switch(opcode)
3982 {
3983 case Shader::OPCODE_DIV:
3984 if(dst.x) d.x = Trunc(d.x);
3985 if(dst.y) d.y = Trunc(d.y);
3986 if(dst.z) d.z = Trunc(d.z);
3987 if(dst.w) d.w = Trunc(d.w);
3988 break;
3989 default:
3990 break; // No truncation to integer required when arguments are integer
3991 }
John Bauman89401822014-05-06 15:04:28 -04003992 }
3993
John Bauman19bac1e2014-05-06 15:23:49 -04003994 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -04003995 {
John Bauman19bac1e2014-05-06 15:23:49 -04003996 if(dst.x) d.x = Max(d.x, Float4(0.0f));
3997 if(dst.y) d.y = Max(d.y, Float4(0.0f));
3998 if(dst.z) d.z = Max(d.z, Float4(0.0f));
3999 if(dst.w) d.w = Max(d.w, Float4(0.0f));
4000
4001 if(dst.x) d.x = Min(d.x, Float4(1.0f));
4002 if(dst.y) d.y = Min(d.y, Float4(1.0f));
4003 if(dst.z) d.z = Min(d.z, Float4(1.0f));
4004 if(dst.w) d.w = Min(d.w, Float4(1.0f));
4005 }
4006
Nicolas Capensc6e8ab12014-05-06 23:31:07 -04004007 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -04004008 {
4009 Vector4f pDst; // FIXME: Rename
John Bauman89401822014-05-06 15:04:28 -04004010
4011 switch(dst.type)
4012 {
John Bauman19bac1e2014-05-06 15:23:49 -04004013 case Shader::PARAMETER_TEMP:
4014 if(dst.rel.type == Shader::PARAMETER_VOID)
4015 {
4016 if(dst.x) pDst.x = r.rf[dst.index].x;
4017 if(dst.y) pDst.y = r.rf[dst.index].y;
4018 if(dst.z) pDst.z = r.rf[dst.index].z;
4019 if(dst.w) pDst.w = r.rf[dst.index].w;
4020 }
4021 else
4022 {
4023 Int a = relativeAddress(r, dst);
4024
4025 if(dst.x) pDst.x = r.rf[dst.index + a].x;
4026 if(dst.y) pDst.y = r.rf[dst.index + a].y;
4027 if(dst.z) pDst.z = r.rf[dst.index + a].z;
4028 if(dst.w) pDst.w = r.rf[dst.index + a].w;
4029 }
John Bauman89401822014-05-06 15:04:28 -04004030 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004031 case Shader::PARAMETER_COLOROUT:
4032 ASSERT(dst.rel.type == Shader::PARAMETER_VOID);
John Bauman89401822014-05-06 15:04:28 -04004033 if(dst.x) pDst.x = r.oC[dst.index].x;
4034 if(dst.y) pDst.y = r.oC[dst.index].y;
4035 if(dst.z) pDst.z = r.oC[dst.index].z;
4036 if(dst.w) pDst.w = r.oC[dst.index].w;
4037 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004038 case Shader::PARAMETER_PREDICATE:
John Bauman89401822014-05-06 15:04:28 -04004039 if(dst.x) pDst.x = r.p0.x;
4040 if(dst.y) pDst.y = r.p0.y;
4041 if(dst.z) pDst.z = r.p0.z;
4042 if(dst.w) pDst.w = r.p0.w;
4043 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004044 case Shader::PARAMETER_DEPTHOUT:
John Bauman89401822014-05-06 15:04:28 -04004045 pDst.x = r.oDepth;
4046 break;
4047 default:
4048 ASSERT(false);
4049 }
4050
John Bauman19bac1e2014-05-06 15:23:49 -04004051 Int4 enable = enableMask(r, instruction);
John Bauman89401822014-05-06 15:04:28 -04004052
4053 Int4 xEnable = enable;
4054 Int4 yEnable = enable;
4055 Int4 zEnable = enable;
4056 Int4 wEnable = enable;
4057
4058 if(predicate)
4059 {
John Bauman19bac1e2014-05-06 15:23:49 -04004060 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -04004061
4062 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
4063 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
4064 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
4065 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
4066
John Bauman19bac1e2014-05-06 15:23:49 -04004067 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -04004068 {
4069 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
4070 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
4071 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
4072 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
4073 }
4074 else
4075 {
4076 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
4077 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
4078 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
4079 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
4080 }
4081 }
4082
4083 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
4084 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
4085 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
4086 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
4087
4088 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
4089 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
4090 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
4091 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
4092 }
4093
4094 switch(dst.type)
4095 {
John Bauman19bac1e2014-05-06 15:23:49 -04004096 case Shader::PARAMETER_TEMP:
4097 if(dst.rel.type == Shader::PARAMETER_VOID)
4098 {
4099 if(dst.x) r.rf[dst.index].x = d.x;
4100 if(dst.y) r.rf[dst.index].y = d.y;
4101 if(dst.z) r.rf[dst.index].z = d.z;
4102 if(dst.w) r.rf[dst.index].w = d.w;
4103 }
4104 else
4105 {
4106 Int a = relativeAddress(r, dst);
4107
4108 if(dst.x) r.rf[dst.index + a].x = d.x;
4109 if(dst.y) r.rf[dst.index + a].y = d.y;
4110 if(dst.z) r.rf[dst.index + a].z = d.z;
4111 if(dst.w) r.rf[dst.index + a].w = d.w;
4112 }
John Bauman89401822014-05-06 15:04:28 -04004113 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004114 case Shader::PARAMETER_COLOROUT:
4115 ASSERT(dst.rel.type == Shader::PARAMETER_VOID);
4116 if(dst.x) {r.oC[dst.index].x = d.x; out[dst.index][0] = true;}
4117 if(dst.y) {r.oC[dst.index].y = d.y; out[dst.index][1] = true;}
4118 if(dst.z) {r.oC[dst.index].z = d.z; out[dst.index][2] = true;}
4119 if(dst.w) {r.oC[dst.index].w = d.w; out[dst.index][3] = true;}
John Bauman89401822014-05-06 15:04:28 -04004120 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004121 case Shader::PARAMETER_PREDICATE:
John Bauman89401822014-05-06 15:04:28 -04004122 if(dst.x) r.p0.x = d.x;
4123 if(dst.y) r.p0.y = d.y;
4124 if(dst.z) r.p0.z = d.z;
4125 if(dst.w) r.p0.w = d.w;
4126 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004127 case Shader::PARAMETER_DEPTHOUT:
John Bauman89401822014-05-06 15:04:28 -04004128 r.oDepth = d.x;
4129 break;
4130 default:
4131 ASSERT(false);
4132 }
4133 }
4134 }
4135
John Bauman19bac1e2014-05-06 15:23:49 -04004136 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -04004137 {
4138 Nucleus::setInsertBlock(returnBlock);
4139 }
John Bauman19bac1e2014-05-06 15:23:49 -04004140
4141 for(int i = 0; i < 4; i++)
4142 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04004143 if(state.targetFormat[i] != FORMAT_NULL)
John Bauman19bac1e2014-05-06 15:23:49 -04004144 {
4145 if(!out[i][0]) r.oC[i].x = Float4(0.0f);
4146 if(!out[i][1]) r.oC[i].y = Float4(0.0f);
4147 if(!out[i][2]) r.oC[i].z = Float4(0.0f);
4148 if(!out[i][3]) r.oC[i].w = Float4(0.0f);
4149 }
4150 }
John Bauman89401822014-05-06 15:04:28 -04004151 }
4152
John Bauman19bac1e2014-05-06 15:23:49 -04004153 Short4 PixelRoutine::convertFixed12(RValue<Float4> cf)
John Bauman89401822014-05-06 15:04:28 -04004154 {
John Bauman19bac1e2014-05-06 15:23:49 -04004155 return RoundShort4(cf * Float4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04004156 }
4157
John Bauman19bac1e2014-05-06 15:23:49 -04004158 void PixelRoutine::convertFixed12(Vector4i &ci, Vector4f &cf)
John Bauman89401822014-05-06 15:04:28 -04004159 {
John Bauman19bac1e2014-05-06 15:23:49 -04004160 ci.x = convertFixed12(cf.x);
4161 ci.y = convertFixed12(cf.y);
4162 ci.z = convertFixed12(cf.z);
4163 ci.w = convertFixed12(cf.w);
John Bauman89401822014-05-06 15:04:28 -04004164 }
4165
4166 UShort4 PixelRoutine::convertFixed16(Float4 &cf, bool saturate)
4167 {
John Bauman19bac1e2014-05-06 15:23:49 -04004168 return UShort4(cf * Float4(0xFFFF), saturate);
John Bauman89401822014-05-06 15:04:28 -04004169 }
4170
John Bauman19bac1e2014-05-06 15:23:49 -04004171 void PixelRoutine::convertFixed16(Vector4i &ci, Vector4f &cf, bool saturate)
John Bauman89401822014-05-06 15:04:28 -04004172 {
John Bauman19bac1e2014-05-06 15:23:49 -04004173 ci.x = convertFixed16(cf.x, saturate);
4174 ci.y = convertFixed16(cf.y, saturate);
4175 ci.z = convertFixed16(cf.z, saturate);
4176 ci.w = convertFixed16(cf.w, saturate);
John Bauman89401822014-05-06 15:04:28 -04004177 }
4178
4179 Float4 PixelRoutine::convertSigned12(Short4 &ci)
4180 {
4181 return Float4(ci) * Float4(1.0f / 0x0FFE);
4182 }
4183
John Bauman19bac1e2014-05-06 15:23:49 -04004184 void PixelRoutine::convertSigned12(Vector4f &cf, Vector4i &ci)
John Bauman89401822014-05-06 15:04:28 -04004185 {
John Bauman19bac1e2014-05-06 15:23:49 -04004186 cf.x = convertSigned12(ci.x);
4187 cf.y = convertSigned12(ci.y);
4188 cf.z = convertSigned12(ci.z);
4189 cf.w = convertSigned12(ci.w);
John Bauman89401822014-05-06 15:04:28 -04004190 }
4191
4192 Float4 PixelRoutine::convertUnsigned16(UShort4 ci)
4193 {
John Bauman19bac1e2014-05-06 15:23:49 -04004194 return Float4(ci) * Float4(1.0f / 0xFFFF);
John Bauman89401822014-05-06 15:04:28 -04004195 }
4196
John Bauman19bac1e2014-05-06 15:23:49 -04004197 void PixelRoutine::sRGBtoLinear16_16(Registers &r, Vector4i &c)
John Bauman89401822014-05-06 15:04:28 -04004198 {
John Bauman19bac1e2014-05-06 15:23:49 -04004199 c.x = As<UShort4>(c.x) >> 4;
4200 c.y = As<UShort4>(c.y) >> 4;
4201 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04004202
4203 sRGBtoLinear12_16(r, c);
4204 }
4205
John Bauman19bac1e2014-05-06 15:23:49 -04004206 void PixelRoutine::sRGBtoLinear12_16(Registers &r, Vector4i &c)
John Bauman89401822014-05-06 15:04:28 -04004207 {
4208 Pointer<Byte> LUT = r.constants + OFFSET(Constants,sRGBtoLin12_16);
4209
John Bauman19bac1e2014-05-06 15:23:49 -04004210 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
4211 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
4212 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
4213 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004214
John Bauman19bac1e2014-05-06 15:23:49 -04004215 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
4216 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
4217 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
4218 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004219
John Bauman19bac1e2014-05-06 15:23:49 -04004220 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
4221 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
4222 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
4223 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004224 }
4225
John Bauman19bac1e2014-05-06 15:23:49 -04004226 void PixelRoutine::linearToSRGB16_16(Registers &r, Vector4i &c)
John Bauman89401822014-05-06 15:04:28 -04004227 {
John Bauman19bac1e2014-05-06 15:23:49 -04004228 c.x = As<UShort4>(c.x) >> 4;
4229 c.y = As<UShort4>(c.y) >> 4;
4230 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04004231
4232 linearToSRGB12_16(r, c);
4233 }
4234
John Bauman19bac1e2014-05-06 15:23:49 -04004235 void PixelRoutine::linearToSRGB12_16(Registers &r, Vector4i &c)
John Bauman89401822014-05-06 15:04:28 -04004236 {
4237 Pointer<Byte> LUT = r.constants + OFFSET(Constants,linToSRGB12_16);
4238
John Bauman19bac1e2014-05-06 15:23:49 -04004239 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
4240 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
4241 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
4242 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004243
John Bauman19bac1e2014-05-06 15:23:49 -04004244 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
4245 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
4246 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
4247 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004248
John Bauman19bac1e2014-05-06 15:23:49 -04004249 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
4250 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
4251 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
4252 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004253 }
4254
4255 Float4 PixelRoutine::linearToSRGB(const Float4 &x) // Approximates x^(1.0/2.2)
4256 {
4257 Float4 sqrtx = Rcp_pp(RcpSqrt_pp(x));
4258 Float4 sRGB = sqrtx * Float4(1.14f) - x * Float4(0.14f);
4259
4260 return Min(Max(sRGB, Float4(0.0f)), Float4(1.0f));
4261 }
4262
4263 Float4 PixelRoutine::sRGBtoLinear(const Float4 &x) // Approximates x^2.2
4264 {
4265 Float4 linear = x * x;
4266 linear = linear * Float4(0.73f) + linear * x * Float4(0.27f);
4267
4268 return Min(Max(linear, Float4(0.0f)), Float4(1.0f));
4269 }
4270
John Bauman19bac1e2014-05-06 15:23:49 -04004271 void PixelRoutine::MOV(Vector4i &dst, Vector4i &src0)
John Bauman89401822014-05-06 15:04:28 -04004272 {
John Bauman19bac1e2014-05-06 15:23:49 -04004273 dst.x = src0.x;
4274 dst.y = src0.y;
4275 dst.z = src0.z;
4276 dst.w = src0.w;
John Bauman89401822014-05-06 15:04:28 -04004277 }
4278
John Bauman19bac1e2014-05-06 15:23:49 -04004279 void PixelRoutine::ADD(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004280 {
John Bauman19bac1e2014-05-06 15:23:49 -04004281 dst.x = AddSat(src0.x, src1.x);
4282 dst.y = AddSat(src0.y, src1.y);
4283 dst.z = AddSat(src0.z, src1.z);
4284 dst.w = AddSat(src0.w, src1.w);
John Bauman89401822014-05-06 15:04:28 -04004285 }
4286
John Bauman19bac1e2014-05-06 15:23:49 -04004287 void PixelRoutine::SUB(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004288 {
John Bauman19bac1e2014-05-06 15:23:49 -04004289 dst.x = SubSat(src0.x, src1.x);
4290 dst.y = SubSat(src0.y, src1.y);
4291 dst.z = SubSat(src0.z, src1.z);
4292 dst.w = SubSat(src0.w, src1.w);
John Bauman89401822014-05-06 15:04:28 -04004293 }
4294
John Bauman19bac1e2014-05-06 15:23:49 -04004295 void PixelRoutine::MAD(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2)
John Bauman89401822014-05-06 15:04:28 -04004296 {
4297 // FIXME: Long fixed-point multiply fixup
4298 {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);}
4299 {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);}
4300 {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);}
4301 {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);}
4302 }
4303
John Bauman19bac1e2014-05-06 15:23:49 -04004304 void PixelRoutine::MUL(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004305 {
4306 // FIXME: Long fixed-point multiply fixup
4307 {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);}
4308 {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);}
4309 {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);}
4310 {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);}
4311 }
4312
John Bauman19bac1e2014-05-06 15:23:49 -04004313 void PixelRoutine::DP3(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004314 {
4315 Short4 t0;
4316 Short4 t1;
4317
4318 // FIXME: Long fixed-point multiply fixup
4319 t0 = MulHigh(src0.x, src1.x); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0);
4320 t1 = MulHigh(src0.y, src1.y); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4321 t0 = AddSat(t0, t1);
4322 t1 = MulHigh(src0.z, src1.z); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4323 t0 = AddSat(t0, t1);
4324
John Bauman19bac1e2014-05-06 15:23:49 -04004325 dst.x = t0;
4326 dst.y = t0;
4327 dst.z = t0;
4328 dst.w = t0;
John Bauman89401822014-05-06 15:04:28 -04004329 }
4330
John Bauman19bac1e2014-05-06 15:23:49 -04004331 void PixelRoutine::DP4(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004332 {
4333 Short4 t0;
4334 Short4 t1;
4335
4336 // FIXME: Long fixed-point multiply fixup
4337 t0 = MulHigh(src0.x, src1.x); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0);
4338 t1 = MulHigh(src0.y, src1.y); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4339 t0 = AddSat(t0, t1);
4340 t1 = MulHigh(src0.z, src1.z); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4341 t0 = AddSat(t0, t1);
4342 t1 = MulHigh(src0.w, src1.w); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4343 t0 = AddSat(t0, t1);
4344
John Bauman19bac1e2014-05-06 15:23:49 -04004345 dst.x = t0;
4346 dst.y = t0;
4347 dst.z = t0;
4348 dst.w = t0;
John Bauman89401822014-05-06 15:04:28 -04004349 }
4350
John Bauman19bac1e2014-05-06 15:23:49 -04004351 void PixelRoutine::LRP(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2)
John Bauman89401822014-05-06 15:04:28 -04004352 {
4353 // FIXME: Long fixed-point multiply fixup
4354 {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);}
4355 {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);}
4356 {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);}
4357 {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);}
4358 }
4359
John Bauman19bac1e2014-05-06 15:23:49 -04004360 void PixelRoutine::TEXCOORD(Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int coordinate)
John Bauman89401822014-05-06 15:04:28 -04004361 {
4362 Float4 uw;
4363 Float4 vw;
4364 Float4 sw;
4365
4366 if(state.interpolant[2 + coordinate].component & 0x01)
4367 {
John Bauman19bac1e2014-05-06 15:23:49 -04004368 uw = Max(u, Float4(0.0f));
4369 uw = Min(uw, Float4(1.0f));
4370 dst.x = convertFixed12(uw);
John Bauman89401822014-05-06 15:04:28 -04004371 }
4372 else
4373 {
John Bauman19bac1e2014-05-06 15:23:49 -04004374 dst.x = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004375 }
4376
4377 if(state.interpolant[2 + coordinate].component & 0x02)
4378 {
John Bauman19bac1e2014-05-06 15:23:49 -04004379 vw = Max(v, Float4(0.0f));
4380 vw = Min(vw, Float4(1.0f));
4381 dst.y = convertFixed12(vw);
John Bauman89401822014-05-06 15:04:28 -04004382 }
4383 else
4384 {
John Bauman19bac1e2014-05-06 15:23:49 -04004385 dst.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004386 }
4387
4388 if(state.interpolant[2 + coordinate].component & 0x04)
4389 {
John Bauman19bac1e2014-05-06 15:23:49 -04004390 sw = Max(s, Float4(0.0f));
4391 sw = Min(sw, Float4(1.0f));
4392 dst.z = convertFixed12(sw);
John Bauman89401822014-05-06 15:04:28 -04004393 }
4394 else
4395 {
John Bauman19bac1e2014-05-06 15:23:49 -04004396 dst.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004397 }
4398
John Bauman19bac1e2014-05-06 15:23:49 -04004399 dst.w = Short4(0x1000);
John Bauman89401822014-05-06 15:04:28 -04004400 }
4401
John Bauman19bac1e2014-05-06 15:23:49 -04004402 void PixelRoutine::TEXCRD(Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int coordinate, bool project)
John Bauman89401822014-05-06 15:04:28 -04004403 {
4404 Float4 uw = u;
4405 Float4 vw = v;
4406 Float4 sw = s;
4407
4408 if(project)
4409 {
4410 uw *= Rcp_pp(s);
4411 vw *= Rcp_pp(s);
4412 }
4413
4414 if(state.interpolant[2 + coordinate].component & 0x01)
4415 {
John Bauman19bac1e2014-05-06 15:23:49 -04004416 uw *= Float4(0x1000);
4417 uw = Max(uw, Float4(-0x8000));
4418 uw = Min(uw, Float4(0x7FFF));
4419 dst.x = RoundShort4(uw);
John Bauman89401822014-05-06 15:04:28 -04004420 }
4421 else
4422 {
John Bauman19bac1e2014-05-06 15:23:49 -04004423 dst.x = Short4(0x0000);
John Bauman89401822014-05-06 15:04:28 -04004424 }
4425
4426 if(state.interpolant[2 + coordinate].component & 0x02)
4427 {
John Bauman19bac1e2014-05-06 15:23:49 -04004428 vw *= Float4(0x1000);
4429 vw = Max(vw, Float4(-0x8000));
4430 vw = Min(vw, Float4(0x7FFF));
4431 dst.y = RoundShort4(vw);
John Bauman89401822014-05-06 15:04:28 -04004432 }
4433 else
4434 {
John Bauman19bac1e2014-05-06 15:23:49 -04004435 dst.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004436 }
4437
4438 if(state.interpolant[2 + coordinate].component & 0x04)
4439 {
John Bauman19bac1e2014-05-06 15:23:49 -04004440 sw *= Float4(0x1000);
4441 sw = Max(sw, Float4(-0x8000));
4442 sw = Min(sw, Float4(0x7FFF));
4443 dst.z = RoundShort4(sw);
John Bauman89401822014-05-06 15:04:28 -04004444 }
4445 else
4446 {
John Bauman19bac1e2014-05-06 15:23:49 -04004447 dst.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004448 }
4449 }
4450
John Bauman19bac1e2014-05-06 15:23:49 -04004451 void PixelRoutine::TEXDP3(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src)
John Bauman89401822014-05-06 15:04:28 -04004452 {
4453 TEXM3X3PAD(r, u, v, s, src, 0, false);
4454
John Bauman19bac1e2014-05-06 15:23:49 -04004455 Short4 t0 = RoundShort4(r.u_ * Float4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04004456
John Bauman19bac1e2014-05-06 15:23:49 -04004457 dst.x = t0;
4458 dst.y = t0;
4459 dst.z = t0;
4460 dst.w = t0;
John Bauman89401822014-05-06 15:04:28 -04004461 }
4462
John Bauman19bac1e2014-05-06 15:23:49 -04004463 void PixelRoutine::TEXDP3TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0)
John Bauman89401822014-05-06 15:04:28 -04004464 {
4465 TEXM3X3PAD(r, u, v, s, src0, 0, false);
4466
John Bauman19bac1e2014-05-06 15:23:49 -04004467 r.v_ = Float4(0.0f);
4468 r.w_ = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04004469
4470 sampleTexture(r, dst, stage, r.u_, r.v_, r.w_, r.w_);
4471 }
4472
4473 void PixelRoutine::TEXKILL(Int cMask[4], Float4 &u, Float4 &v, Float4 &s)
4474 {
John Bauman19bac1e2014-05-06 15:23:49 -04004475 Int kill = SignMask(CmpNLT(u, Float4(0.0f))) &
4476 SignMask(CmpNLT(v, Float4(0.0f))) &
4477 SignMask(CmpNLT(s, Float4(0.0f)));
John Bauman89401822014-05-06 15:04:28 -04004478
4479 for(unsigned int q = 0; q < state.multiSample; q++)
4480 {
4481 cMask[q] &= kill;
4482 }
4483 }
4484
John Bauman19bac1e2014-05-06 15:23:49 -04004485 void PixelRoutine::TEXKILL(Int cMask[4], Vector4i &src)
John Bauman89401822014-05-06 15:04:28 -04004486 {
John Bauman19bac1e2014-05-06 15:23:49 -04004487 Short4 test = src.x | src.y | src.z;
John Bauman89401822014-05-06 15:04:28 -04004488 Int kill = SignMask(Pack(test, test)) ^ 0x0000000F;
4489
4490 for(unsigned int q = 0; q < state.multiSample; q++)
4491 {
4492 cMask[q] &= kill;
4493 }
4494 }
4495
John Bauman19bac1e2014-05-06 15:23:49 -04004496 void PixelRoutine::TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int sampler, bool project)
John Bauman89401822014-05-06 15:04:28 -04004497 {
4498 sampleTexture(r, dst, sampler, u, v, s, s, project);
4499 }
4500
John Bauman19bac1e2014-05-06 15:23:49 -04004501 void PixelRoutine::TEXLD(Registers &r, Vector4i &dst, Vector4i &src, int sampler, bool project)
John Bauman89401822014-05-06 15:04:28 -04004502 {
John Bauman19bac1e2014-05-06 15:23:49 -04004503 Float4 u = Float4(src.x) * Float4(1.0f / 0x0FFE);
4504 Float4 v = Float4(src.y) * Float4(1.0f / 0x0FFE);
4505 Float4 s = Float4(src.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004506
4507 sampleTexture(r, dst, sampler, u, v, s, s, project);
4508 }
4509
John Bauman19bac1e2014-05-06 15:23:49 -04004510 void PixelRoutine::TEXBEM(Registers &r, Vector4i &dst, Vector4i &src, Float4 &u, Float4 &v, Float4 &s, int stage)
John Bauman89401822014-05-06 15:04:28 -04004511 {
John Bauman19bac1e2014-05-06 15:23:49 -04004512 Float4 du = Float4(src.x) * Float4(1.0f / 0x0FFE);
4513 Float4 dv = Float4(src.y) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004514
4515 Float4 du2 = du;
4516 Float4 dv2 = dv;
4517
4518 du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
4519 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
4520 du += dv2;
4521 dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
4522 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
4523 dv += du2;
4524
4525 Float4 u_ = u + du;
4526 Float4 v_ = v + dv;
4527
4528 sampleTexture(r, dst, stage, u_, v_, s, s);
4529 }
4530
John Bauman19bac1e2014-05-06 15:23:49 -04004531 void PixelRoutine::TEXBEML(Registers &r, Vector4i &dst, Vector4i &src, Float4 &u, Float4 &v, Float4 &s, int stage)
John Bauman89401822014-05-06 15:04:28 -04004532 {
John Bauman19bac1e2014-05-06 15:23:49 -04004533 Float4 du = Float4(src.x) * Float4(1.0f / 0x0FFE);
4534 Float4 dv = Float4(src.y) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004535
4536 Float4 du2 = du;
4537 Float4 dv2 = dv;
4538
4539 du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
4540 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
4541 du += dv2;
4542 dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
4543 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
4544 dv += du2;
4545
4546 Float4 u_ = u + du;
4547 Float4 v_ = v + dv;
4548
4549 sampleTexture(r, dst, stage, u_, v_, s, s);
4550
4551 Short4 L;
4552
John Bauman19bac1e2014-05-06 15:23:49 -04004553 L = src.z;
John Bauman89401822014-05-06 15:04:28 -04004554 L = MulHigh(L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceScale4)));
4555 L = L << 4;
4556 L = AddSat(L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceOffset4)));
4557 L = Max(L, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman19bac1e2014-05-06 15:23:49 -04004558 L = Min(L, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04004559
John Bauman19bac1e2014-05-06 15:23:49 -04004560 dst.x = MulHigh(dst.x, L); dst.x = dst.x << 4;
4561 dst.y = MulHigh(dst.y, L); dst.y = dst.y << 4;
4562 dst.z = MulHigh(dst.z, L); dst.z = dst.z << 4;
John Bauman89401822014-05-06 15:04:28 -04004563 }
4564
John Bauman19bac1e2014-05-06 15:23:49 -04004565 void PixelRoutine::TEXREG2AR(Registers &r, Vector4i &dst, Vector4i &src0, int stage)
John Bauman89401822014-05-06 15:04:28 -04004566 {
John Bauman19bac1e2014-05-06 15:23:49 -04004567 Float4 u = Float4(src0.w) * Float4(1.0f / 0x0FFE);
4568 Float4 v = Float4(src0.x) * Float4(1.0f / 0x0FFE);
4569 Float4 s = Float4(src0.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004570
4571 sampleTexture(r, dst, stage, u, v, s, s);
4572 }
4573
John Bauman19bac1e2014-05-06 15:23:49 -04004574 void PixelRoutine::TEXREG2GB(Registers &r, Vector4i &dst, Vector4i &src0, int stage)
John Bauman89401822014-05-06 15:04:28 -04004575 {
John Bauman19bac1e2014-05-06 15:23:49 -04004576 Float4 u = Float4(src0.y) * Float4(1.0f / 0x0FFE);
4577 Float4 v = Float4(src0.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004578 Float4 s = v;
4579
4580 sampleTexture(r, dst, stage, u, v, s, s);
4581 }
4582
John Bauman19bac1e2014-05-06 15:23:49 -04004583 void PixelRoutine::TEXREG2RGB(Registers &r, Vector4i &dst, Vector4i &src0, int stage)
John Bauman89401822014-05-06 15:04:28 -04004584 {
John Bauman19bac1e2014-05-06 15:23:49 -04004585 Float4 u = Float4(src0.x) * Float4(1.0f / 0x0FFE);
4586 Float4 v = Float4(src0.y) * Float4(1.0f / 0x0FFE);
4587 Float4 s = Float4(src0.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004588
4589 sampleTexture(r, dst, stage, u, v, s, s);
4590 }
4591
John Bauman19bac1e2014-05-06 15:23:49 -04004592 void PixelRoutine::TEXM3X2DEPTH(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004593 {
4594 TEXM3X2PAD(r, u, v, s, src, 1, signedScaling);
4595
4596 // z / w
4597 r.u_ *= Rcp_pp(r.v_); // FIXME: Set result to 1.0 when division by zero
4598
4599 r.oDepth = r.u_;
4600 }
4601
John Bauman19bac1e2014-05-06 15:23:49 -04004602 void PixelRoutine::TEXM3X2PAD(Registers &r, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, int component, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004603 {
4604 TEXM3X3PAD(r, u, v, s, src0, component, signedScaling);
4605 }
4606
John Bauman19bac1e2014-05-06 15:23:49 -04004607 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 -04004608 {
4609 TEXM3X2PAD(r, u, v, s, src0, 1, signedScaling);
4610
John Bauman19bac1e2014-05-06 15:23:49 -04004611 r.w_ = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04004612
4613 sampleTexture(r, dst, stage, r.u_, r.v_, r.w_, r.w_);
4614 }
4615
John Bauman19bac1e2014-05-06 15:23:49 -04004616 void PixelRoutine::TEXM3X3(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004617 {
4618 TEXM3X3PAD(r, u, v, s, src0, 2, signedScaling);
4619
John Bauman19bac1e2014-05-06 15:23:49 -04004620 dst.x = RoundShort4(r.u_ * Float4(0x1000));
4621 dst.y = RoundShort4(r.v_ * Float4(0x1000));
4622 dst.z = RoundShort4(r.w_ * Float4(0x1000));
4623 dst.w = Short4(0x1000);
John Bauman89401822014-05-06 15:04:28 -04004624 }
4625
John Bauman19bac1e2014-05-06 15:23:49 -04004626 void PixelRoutine::TEXM3X3PAD(Registers &r, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, int component, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004627 {
4628 if(component == 0 || previousScaling != signedScaling) // FIXME: Other source modifiers?
4629 {
John Bauman19bac1e2014-05-06 15:23:49 -04004630 r.U = Float4(src0.x);
4631 r.V = Float4(src0.y);
4632 r.W = Float4(src0.z);
John Bauman89401822014-05-06 15:04:28 -04004633
4634 previousScaling = signedScaling;
4635 }
4636
4637 Float4 x = r.U * u + r.V * v + r.W * s;
4638
John Bauman19bac1e2014-05-06 15:23:49 -04004639 x *= Float4(1.0f / 0x1000);
John Bauman89401822014-05-06 15:04:28 -04004640
4641 switch(component)
4642 {
4643 case 0: r.u_ = x; break;
4644 case 1: r.v_ = x; break;
4645 case 2: r.w_ = x; break;
4646 default: ASSERT(false);
4647 }
4648 }
4649
John Bauman19bac1e2014-05-06 15:23:49 -04004650 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 -04004651 {
4652 TEXM3X3PAD(r, u, v, s, src0, 2, false);
4653
4654 Float4 E[3]; // Eye vector
4655
John Bauman19bac1e2014-05-06 15:23:49 -04004656 E[0] = Float4(src1.x) * Float4(1.0f / 0x0FFE);
4657 E[1] = Float4(src1.y) * Float4(1.0f / 0x0FFE);
4658 E[2] = Float4(src1.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004659
4660 // Reflection
4661 Float4 u__;
4662 Float4 v__;
4663 Float4 w__;
4664
4665 // (u'', v'', w'') = 2 * (N . E) * N - E * (N . N)
4666 u__ = r.u_ * E[0];
4667 v__ = r.v_ * E[1];
4668 w__ = r.w_ * E[2];
4669 u__ += v__ + w__;
4670 u__ += u__;
4671 v__ = u__;
4672 w__ = u__;
4673 u__ *= r.u_;
4674 v__ *= r.v_;
4675 w__ *= r.w_;
4676 r.u_ *= r.u_;
4677 r.v_ *= r.v_;
4678 r.w_ *= r.w_;
4679 r.u_ += r.v_ + r.w_;
4680 u__ -= E[0] * r.u_;
4681 v__ -= E[1] * r.u_;
4682 w__ -= E[2] * r.u_;
4683
4684 sampleTexture(r, dst, stage, u__, v__, w__, w__);
4685 }
4686
John Bauman19bac1e2014-05-06 15:23:49 -04004687 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 -04004688 {
4689 TEXM3X3PAD(r, u, v, s, src0, 2, signedScaling);
4690
4691 sampleTexture(r, dst, stage, r.u_, r.v_, r.w_, r.w_);
4692 }
4693
John Bauman19bac1e2014-05-06 15:23:49 -04004694 void PixelRoutine::TEXM3X3VSPEC(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0)
John Bauman89401822014-05-06 15:04:28 -04004695 {
4696 TEXM3X3PAD(r, u, v, s, src0, 2, false);
4697
4698 Float4 E[3]; // Eye vector
4699
John Bauman19bac1e2014-05-06 15:23:49 -04004700 E[0] = r.vf[2 + stage - 2].w;
4701 E[1] = r.vf[2 + stage - 1].w;
4702 E[2] = r.vf[2 + stage - 0].w;
John Bauman89401822014-05-06 15:04:28 -04004703
4704 // Reflection
4705 Float4 u__;
4706 Float4 v__;
4707 Float4 w__;
4708
4709 // (u'', v'', w'') = 2 * (N . E) * N - E * (N . N)
4710 u__ = r.u_ * E[0];
4711 v__ = r.v_ * E[1];
4712 w__ = r.w_ * E[2];
4713 u__ += v__ + w__;
4714 u__ += u__;
4715 v__ = u__;
4716 w__ = u__;
4717 u__ *= r.u_;
4718 v__ *= r.v_;
4719 w__ *= r.w_;
4720 r.u_ *= r.u_;
4721 r.v_ *= r.v_;
4722 r.w_ *= r.w_;
4723 r.u_ += r.v_ + r.w_;
4724 u__ -= E[0] * r.u_;
4725 v__ -= E[1] * r.u_;
4726 w__ -= E[2] * r.u_;
4727
4728 sampleTexture(r, dst, stage, u__, v__, w__, w__);
4729 }
4730
4731 void PixelRoutine::TEXDEPTH(Registers &r)
4732 {
John Bauman19bac1e2014-05-06 15:23:49 -04004733 r.u_ = Float4(r.ri[5].x);
4734 r.v_ = Float4(r.ri[5].y);
John Bauman89401822014-05-06 15:04:28 -04004735
4736 // z / w
4737 r.u_ *= Rcp_pp(r.v_); // FIXME: Set result to 1.0 when division by zero
4738
4739 r.oDepth = r.u_;
4740 }
4741
John Bauman19bac1e2014-05-06 15:23:49 -04004742 void PixelRoutine::CND(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2)
John Bauman89401822014-05-06 15:04:28 -04004743 {
John Bauman19bac1e2014-05-06 15:23:49 -04004744 {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;};
4745 {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;};
4746 {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;};
4747 {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 -04004748 }
4749
John Bauman19bac1e2014-05-06 15:23:49 -04004750 void PixelRoutine::CMP(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2)
John Bauman89401822014-05-06 15:04:28 -04004751 {
John Bauman19bac1e2014-05-06 15:23:49 -04004752 {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;};
4753 {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;};
4754 {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;};
4755 {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 -04004756 }
4757
John Bauman19bac1e2014-05-06 15:23:49 -04004758 void PixelRoutine::BEM(Registers &r, Vector4i &dst, Vector4i &src0, Vector4i &src1, int stage)
John Bauman89401822014-05-06 15:04:28 -04004759 {
4760 Short4 t0;
4761 Short4 t1;
4762
John Bauman19bac1e2014-05-06 15:23:49 -04004763 // dst.x = src0.x + BUMPENVMAT00(stage) * src1.x + BUMPENVMAT10(stage) * src1.y
John Bauman89401822014-05-06 15:04:28 -04004764 t0 = MulHigh(src1.x, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[0][0]))); t0 = t0 << 4; // FIXME: Matrix components range? Overflow hazard.
4765 t1 = MulHigh(src1.y, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[1][0]))); t1 = t1 << 4; // FIXME: Matrix components range? Overflow hazard.
4766 t0 = AddSat(t0, t1);
4767 t0 = AddSat(t0, src0.x);
John Bauman19bac1e2014-05-06 15:23:49 -04004768 dst.x = t0;
John Bauman89401822014-05-06 15:04:28 -04004769
John Bauman19bac1e2014-05-06 15:23:49 -04004770 // dst.y = src0.y + BUMPENVMAT01(stage) * src1.x + BUMPENVMAT11(stage) * src1.y
John Bauman89401822014-05-06 15:04:28 -04004771 t0 = MulHigh(src1.x, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[0][1]))); t0 = t0 << 4; // FIXME: Matrix components range? Overflow hazard.
4772 t1 = MulHigh(src1.y, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[1][1]))); t1 = t1 << 4; // FIXME: Matrix components range? Overflow hazard.
4773 t0 = AddSat(t0, t1);
4774 t0 = AddSat(t0, src0.y);
John Bauman19bac1e2014-05-06 15:23:49 -04004775 dst.y = t0;
John Bauman89401822014-05-06 15:04:28 -04004776 }
4777
John Bauman19bac1e2014-05-06 15:23:49 -04004778 void PixelRoutine::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004779 {
John Bauman19bac1e2014-05-06 15:23:49 -04004780 Vector4f row0 = reg(r, src1, 0);
4781 Vector4f row1 = reg(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -04004782
4783 dst.x = dot3(src0, row0);
4784 dst.y = dot3(src0, row1);
4785 }
4786
John Bauman19bac1e2014-05-06 15:23:49 -04004787 void PixelRoutine::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004788 {
John Bauman19bac1e2014-05-06 15:23:49 -04004789 Vector4f row0 = reg(r, src1, 0);
4790 Vector4f row1 = reg(r, src1, 1);
4791 Vector4f row2 = reg(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -04004792
4793 dst.x = dot3(src0, row0);
4794 dst.y = dot3(src0, row1);
4795 dst.z = dot3(src0, row2);
4796 }
4797
John Bauman19bac1e2014-05-06 15:23:49 -04004798 void PixelRoutine::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004799 {
John Bauman19bac1e2014-05-06 15:23:49 -04004800 Vector4f row0 = reg(r, src1, 0);
4801 Vector4f row1 = reg(r, src1, 1);
4802 Vector4f row2 = reg(r, src1, 2);
4803 Vector4f row3 = reg(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -04004804
4805 dst.x = dot3(src0, row0);
4806 dst.y = dot3(src0, row1);
4807 dst.z = dot3(src0, row2);
4808 dst.w = dot3(src0, row3);
4809 }
4810
John Bauman19bac1e2014-05-06 15:23:49 -04004811 void PixelRoutine::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004812 {
John Bauman19bac1e2014-05-06 15:23:49 -04004813 Vector4f row0 = reg(r, src1, 0);
4814 Vector4f row1 = reg(r, src1, 1);
4815 Vector4f row2 = reg(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -04004816
4817 dst.x = dot4(src0, row0);
4818 dst.y = dot4(src0, row1);
4819 dst.z = dot4(src0, row2);
4820 }
4821
John Bauman19bac1e2014-05-06 15:23:49 -04004822 void PixelRoutine::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004823 {
John Bauman19bac1e2014-05-06 15:23:49 -04004824 Vector4f row0 = reg(r, src1, 0);
4825 Vector4f row1 = reg(r, src1, 1);
4826 Vector4f row2 = reg(r, src1, 2);
4827 Vector4f row3 = reg(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -04004828
4829 dst.x = dot4(src0, row0);
4830 dst.y = dot4(src0, row1);
4831 dst.z = dot4(src0, row2);
4832 dst.w = dot4(src0, row3);
4833 }
4834
John Bauman19bac1e2014-05-06 15:23:49 -04004835 void PixelRoutine::TEXLD(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias)
John Bauman89401822014-05-06 15:04:28 -04004836 {
John Bauman19bac1e2014-05-06 15:23:49 -04004837 Vector4f tmp;
4838 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w, src0, src0, project, bias);
John Bauman89401822014-05-06 15:04:28 -04004839
4840 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
4841 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
4842 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
4843 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
4844 }
4845
John Bauman19bac1e2014-05-06 15:23:49 -04004846 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 -04004847 {
John Bauman19bac1e2014-05-06 15:23:49 -04004848 Vector4f tmp;
4849 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w, src2, src3, project, bias, true);
John Bauman89401822014-05-06 15:04:28 -04004850
4851 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
4852 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
4853 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
4854 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
4855 }
4856
John Bauman19bac1e2014-05-06 15:23:49 -04004857 void PixelRoutine::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias)
John Bauman89401822014-05-06 15:04:28 -04004858 {
John Bauman19bac1e2014-05-06 15:23:49 -04004859 Vector4f tmp;
4860 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 -04004861
4862 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
4863 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
4864 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
4865 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
4866 }
4867
John Bauman19bac1e2014-05-06 15:23:49 -04004868 void PixelRoutine::TEXKILL(Int cMask[4], Vector4f &src, unsigned char mask)
John Bauman89401822014-05-06 15:04:28 -04004869 {
4870 Int kill = -1;
4871
John Bauman19bac1e2014-05-06 15:23:49 -04004872 if(mask & 0x1) kill &= SignMask(CmpNLT(src.x, Float4(0.0f)));
4873 if(mask & 0x2) kill &= SignMask(CmpNLT(src.y, Float4(0.0f)));
4874 if(mask & 0x4) kill &= SignMask(CmpNLT(src.z, Float4(0.0f)));
4875 if(mask & 0x8) kill &= SignMask(CmpNLT(src.w, Float4(0.0f)));
4876
4877 // FIXME: Dynamic branching affects TEXKILL?
4878 // if(shader->containsDynamicBranching())
4879 // {
4880 // kill = ~SignMask(enableMask(r));
4881 // }
John Bauman89401822014-05-06 15:04:28 -04004882
4883 for(unsigned int q = 0; q < state.multiSample; q++)
4884 {
4885 cMask[q] &= kill;
4886 }
John Bauman19bac1e2014-05-06 15:23:49 -04004887
4888 // FIXME: Branch to end of shader if all killed?
John Bauman89401822014-05-06 15:04:28 -04004889 }
4890
John Bauman19bac1e2014-05-06 15:23:49 -04004891 void PixelRoutine::DISCARD(Registers &r, Int cMask[4], const Shader::Instruction *instruction)
John Bauman89401822014-05-06 15:04:28 -04004892 {
John Bauman19bac1e2014-05-06 15:23:49 -04004893 Int kill = 0;
4894
4895 if(shader->containsDynamicBranching())
4896 {
4897 kill = ~SignMask(enableMask(r, instruction));
4898 }
4899
4900 for(unsigned int q = 0; q < state.multiSample; q++)
4901 {
4902 cMask[q] &= kill;
4903 }
4904
4905 // FIXME: Branch to end of shader if all killed?
John Bauman89401822014-05-06 15:04:28 -04004906 }
4907
John Bauman19bac1e2014-05-06 15:23:49 -04004908 void PixelRoutine::DFDX(Vector4f &dst, Vector4f &src)
John Bauman89401822014-05-06 15:04:28 -04004909 {
John Bauman19bac1e2014-05-06 15:23:49 -04004910 dst.x = src.x.yyww - src.x.xxzz;
4911 dst.y = src.y.yyww - src.y.xxzz;
4912 dst.z = src.z.yyww - src.z.xxzz;
4913 dst.w = src.w.yyww - src.w.xxzz;
4914 }
4915
4916 void PixelRoutine::DFDY(Vector4f &dst, Vector4f &src)
4917 {
4918 dst.x = src.x.zwzw - src.x.xyxy;
4919 dst.y = src.y.zwzw - src.y.xyxy;
4920 dst.z = src.z.zwzw - src.z.xyxy;
4921 dst.w = src.w.zwzw - src.w.xyxy;
4922 }
4923
4924 void PixelRoutine::FWIDTH(Vector4f &dst, Vector4f &src)
4925 {
4926 // abs(dFdx(src)) + abs(dFdy(src));
4927 dst.x = Abs(src.x.yyww - src.x.xxzz) + Abs(src.x.zwzw - src.x.xyxy);
4928 dst.y = Abs(src.y.yyww - src.x.xxzz) + Abs(src.y.zwzw - src.y.xyxy);
4929 dst.z = Abs(src.z.yyww - src.x.xxzz) + Abs(src.z.zwzw - src.z.xyxy);
4930 dst.w = Abs(src.w.yyww - src.x.xxzz) + Abs(src.w.zwzw - src.w.xyxy);
John Bauman89401822014-05-06 15:04:28 -04004931 }
4932
4933 void PixelRoutine::BREAK(Registers &r)
4934 {
4935 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
4936 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
4937
4938 if(breakDepth == 0)
4939 {
John Bauman19bac1e2014-05-06 15:23:49 -04004940 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04004941 Nucleus::createBr(endBlock);
4942 }
4943 else
4944 {
4945 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
4946 Bool allBreak = SignMask(r.enableBreak) == 0x0;
4947
John Bauman19bac1e2014-05-06 15:23:49 -04004948 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04004949 branch(allBreak, endBlock, deadBlock);
4950 }
4951
4952 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04004953 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04004954 }
4955
John Bauman19bac1e2014-05-06 15:23:49 -04004956 void PixelRoutine::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04004957 {
4958 Int4 condition;
4959
4960 switch(control)
4961 {
John Bauman19bac1e2014-05-06 15:23:49 -04004962 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
4963 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
4964 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
4965 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
4966 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
4967 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04004968 default:
4969 ASSERT(false);
4970 }
4971
John Bauman19bac1e2014-05-06 15:23:49 -04004972 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -04004973 }
4974
4975 void PixelRoutine::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
4976 {
4977 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
4978
John Bauman19bac1e2014-05-06 15:23:49 -04004979 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04004980 {
4981 condition = ~condition;
4982 }
4983
John Bauman19bac1e2014-05-06 15:23:49 -04004984 BREAK(r, condition);
4985 }
4986
4987 void PixelRoutine::BREAK(Registers &r, Int4 &condition)
4988 {
John Bauman89401822014-05-06 15:04:28 -04004989 condition &= r.enableStack[r.enableIndex];
4990
4991 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
4992 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
4993
4994 r.enableBreak = r.enableBreak & ~condition;
4995 Bool allBreak = SignMask(r.enableBreak) == 0x0;
4996
John Bauman19bac1e2014-05-06 15:23:49 -04004997 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04004998 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04004999
John Bauman89401822014-05-06 15:04:28 -04005000 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04005001 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04005002 }
5003
John Bauman19bac1e2014-05-06 15:23:49 -04005004 void PixelRoutine::CONTINUE(Registers &r)
5005 {
5006 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
5007 }
5008
5009 void PixelRoutine::TEST()
5010 {
5011 whileTest = true;
5012 }
5013
5014 void PixelRoutine::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04005015 {
5016 if(!labelBlock[labelIndex])
5017 {
5018 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5019 }
5020
John Bauman19bac1e2014-05-06 15:23:49 -04005021 if(callRetBlock[labelIndex].size() > 1)
5022 {
5023 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
5024 }
John Bauman89401822014-05-06 15:04:28 -04005025
John Bauman19bac1e2014-05-06 15:23:49 -04005026 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04005027
5028 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04005029 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
5030
5031 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04005032 }
5033
John Bauman19bac1e2014-05-06 15:23:49 -04005034 void PixelRoutine::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04005035 {
John Bauman19bac1e2014-05-06 15:23:49 -04005036 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04005037 {
John Bauman19bac1e2014-05-06 15:23:49 -04005038 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04005039 }
John Bauman19bac1e2014-05-06 15:23:49 -04005040 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04005041 {
John Bauman19bac1e2014-05-06 15:23:49 -04005042 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04005043 }
5044 else ASSERT(false);
5045 }
5046
John Bauman19bac1e2014-05-06 15:23:49 -04005047 void PixelRoutine::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04005048 {
5049 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,ps.b[boolRegister.index])) != Byte(0)); // FIXME
5050
John Bauman19bac1e2014-05-06 15:23:49 -04005051 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005052 {
5053 condition = !condition;
5054 }
5055
5056 if(!labelBlock[labelIndex])
5057 {
5058 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5059 }
5060
John Bauman19bac1e2014-05-06 15:23:49 -04005061 if(callRetBlock[labelIndex].size() > 1)
5062 {
5063 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
5064 }
John Bauman89401822014-05-06 15:04:28 -04005065
John Bauman19bac1e2014-05-06 15:23:49 -04005066 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04005067
John Bauman19bac1e2014-05-06 15:23:49 -04005068 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
5069 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
5070
5071 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04005072 }
5073
John Bauman19bac1e2014-05-06 15:23:49 -04005074 void PixelRoutine::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04005075 {
5076 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
5077
John Bauman19bac1e2014-05-06 15:23:49 -04005078 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005079 {
5080 condition = ~condition;
5081 }
5082
5083 condition &= r.enableStack[r.enableIndex];
5084
5085 if(!labelBlock[labelIndex])
5086 {
5087 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5088 }
5089
John Bauman19bac1e2014-05-06 15:23:49 -04005090 if(callRetBlock[labelIndex].size() > 1)
5091 {
5092 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
5093 }
John Bauman89401822014-05-06 15:04:28 -04005094
5095 r.enableIndex++;
5096 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04005097 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04005098
John Bauman19bac1e2014-05-06 15:23:49 -04005099 Bool notAllFalse = SignMask(condition) != 0;
5100 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
5101 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04005102
5103 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04005104 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04005105 }
5106
5107 void PixelRoutine::ELSE(Registers &r)
5108 {
5109 ifDepth--;
5110
5111 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
5112 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5113
5114 if(isConditionalIf[ifDepth])
5115 {
5116 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04005117 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04005118
5119 branch(notAllFalse, falseBlock, endBlock);
5120
5121 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
5122 }
5123 else
5124 {
5125 Nucleus::createBr(endBlock);
5126 Nucleus::setInsertBlock(falseBlock);
5127 }
5128
5129 ifFalseBlock[ifDepth] = endBlock;
5130
5131 ifDepth++;
5132 }
5133
5134 void PixelRoutine::ENDIF(Registers &r)
5135 {
5136 ifDepth--;
5137
5138 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
5139
5140 Nucleus::createBr(endBlock);
5141 Nucleus::setInsertBlock(endBlock);
5142
5143 if(isConditionalIf[ifDepth])
5144 {
5145 breakDepth--;
5146 r.enableIndex--;
5147 }
5148 }
5149
John Bauman89401822014-05-06 15:04:28 -04005150 void PixelRoutine::ENDLOOP(Registers &r)
5151 {
5152 loopRepDepth--;
5153
5154 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
5155
5156 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
5157 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
5158
5159 Nucleus::createBr(testBlock);
5160 Nucleus::setInsertBlock(endBlock);
5161
5162 r.loopDepth--;
5163 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
5164 }
5165
John Bauman19bac1e2014-05-06 15:23:49 -04005166 void PixelRoutine::ENDREP(Registers &r)
5167 {
5168 loopRepDepth--;
5169
5170 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
5171 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
5172
5173 Nucleus::createBr(testBlock);
5174 Nucleus::setInsertBlock(endBlock);
5175
5176 r.loopDepth--;
5177 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
5178 }
5179
5180 void PixelRoutine::ENDWHILE(Registers &r)
5181 {
5182 loopRepDepth--;
5183
5184 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
5185 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
5186
5187 Nucleus::createBr(testBlock);
5188 Nucleus::setInsertBlock(endBlock);
5189
5190 r.enableIndex--;
5191 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
5192 whileTest = false;
5193 }
5194
John Bauman89401822014-05-06 15:04:28 -04005195 void PixelRoutine::IF(Registers &r, const Src &src)
5196 {
John Bauman19bac1e2014-05-06 15:23:49 -04005197 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04005198 {
5199 IFb(r, src);
5200 }
John Bauman19bac1e2014-05-06 15:23:49 -04005201 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04005202 {
5203 IFp(r, src);
5204 }
John Bauman19bac1e2014-05-06 15:23:49 -04005205 else
5206 {
5207 Int4 condition = As<Int4>(reg(r, src).x);
5208 IF(r, condition);
5209 }
John Bauman89401822014-05-06 15:04:28 -04005210 }
5211
5212 void PixelRoutine::IFb(Registers &r, const Src &boolRegister)
5213 {
John Bauman19bac1e2014-05-06 15:23:49 -04005214 ASSERT(ifDepth < 24 + 4);
5215
John Bauman89401822014-05-06 15:04:28 -04005216 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,ps.b[boolRegister.index])) != Byte(0)); // FIXME
5217
John Bauman19bac1e2014-05-06 15:23:49 -04005218 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005219 {
John Bauman19bac1e2014-05-06 15:23:49 -04005220 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04005221 }
5222
5223 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
5224 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
5225
5226 branch(condition, trueBlock, falseBlock);
5227
5228 isConditionalIf[ifDepth] = false;
5229 ifFalseBlock[ifDepth] = falseBlock;
5230
5231 ifDepth++;
5232 }
5233
John Bauman19bac1e2014-05-06 15:23:49 -04005234 void PixelRoutine::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04005235 {
5236 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
5237
John Bauman19bac1e2014-05-06 15:23:49 -04005238 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005239 {
5240 condition = ~condition;
5241 }
5242
John Bauman19bac1e2014-05-06 15:23:49 -04005243 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04005244 }
5245
John Bauman19bac1e2014-05-06 15:23:49 -04005246 void PixelRoutine::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04005247 {
5248 Int4 condition;
5249
5250 switch(control)
5251 {
John Bauman19bac1e2014-05-06 15:23:49 -04005252 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
5253 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
5254 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
5255 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
5256 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
5257 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04005258 default:
5259 ASSERT(false);
5260 }
5261
John Bauman19bac1e2014-05-06 15:23:49 -04005262 IF(r, condition);
5263 }
5264
5265 void PixelRoutine::IF(Registers &r, Int4 &condition)
5266 {
John Bauman89401822014-05-06 15:04:28 -04005267 condition &= r.enableStack[r.enableIndex];
5268
5269 r.enableIndex++;
5270 r.enableStack[r.enableIndex] = condition;
5271
5272 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
5273 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
5274
John Bauman19bac1e2014-05-06 15:23:49 -04005275 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04005276
5277 branch(notAllFalse, trueBlock, falseBlock);
5278
5279 isConditionalIf[ifDepth] = true;
5280 ifFalseBlock[ifDepth] = falseBlock;
5281
5282 ifDepth++;
5283 breakDepth++;
5284 }
5285
5286 void PixelRoutine::LABEL(int labelIndex)
5287 {
John Bauman19bac1e2014-05-06 15:23:49 -04005288 if(!labelBlock[labelIndex])
5289 {
5290 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5291 }
5292
John Bauman89401822014-05-06 15:04:28 -04005293 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04005294 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04005295 }
5296
5297 void PixelRoutine::LOOP(Registers &r, const Src &integerRegister)
5298 {
5299 r.loopDepth++;
5300
5301 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][0]));
5302 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][1]));
5303 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][2]));
5304
5305 // If(r.increment[r.loopDepth] == 0)
5306 // {
5307 // r.increment[r.loopDepth] = 1;
5308 // }
5309
5310 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
5311 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
5312 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5313
5314 loopRepTestBlock[loopRepDepth] = testBlock;
5315 loopRepEndBlock[loopRepDepth] = endBlock;
5316
5317 // FIXME: jump(testBlock)
5318 Nucleus::createBr(testBlock);
5319 Nucleus::setInsertBlock(testBlock);
5320
5321 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
5322 Nucleus::setInsertBlock(loopBlock);
5323
5324 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
5325
5326 loopRepDepth++;
5327 breakDepth = 0;
5328 }
5329
5330 void PixelRoutine::REP(Registers &r, const Src &integerRegister)
5331 {
5332 r.loopDepth++;
5333
5334 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][0]));
5335 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
5336
5337 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
5338 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
5339 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5340
5341 loopRepTestBlock[loopRepDepth] = testBlock;
5342 loopRepEndBlock[loopRepDepth] = endBlock;
5343
5344 // FIXME: jump(testBlock)
5345 Nucleus::createBr(testBlock);
5346 Nucleus::setInsertBlock(testBlock);
5347
5348 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
5349 Nucleus::setInsertBlock(loopBlock);
5350
5351 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
5352
5353 loopRepDepth++;
5354 breakDepth = 0;
5355 }
5356
John Bauman19bac1e2014-05-06 15:23:49 -04005357 void PixelRoutine::WHILE(Registers &r, const Src &temporaryRegister)
5358 {
5359 r.enableIndex++;
5360
5361 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
5362 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
5363 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5364
5365 loopRepTestBlock[loopRepDepth] = testBlock;
5366 loopRepEndBlock[loopRepDepth] = endBlock;
5367
5368 Int4 restoreBreak = r.enableBreak;
5369 Int4 restoreContinue = r.enableContinue;
5370
5371 // FIXME: jump(testBlock)
5372 Nucleus::createBr(testBlock);
5373 Nucleus::setInsertBlock(testBlock);
5374 r.enableContinue = restoreContinue;
5375
John Bauman66b8ab22014-05-06 15:57:45 -04005376 const Vector4f &src = reg(r, temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04005377 Int4 condition = As<Int4>(src.x);
5378 condition &= r.enableStack[r.enableIndex - 1];
5379 r.enableStack[r.enableIndex] = condition;
5380
5381 Bool notAllFalse = SignMask(condition) != 0;
5382 branch(notAllFalse, loopBlock, endBlock);
5383
5384 Nucleus::setInsertBlock(endBlock);
5385 r.enableBreak = restoreBreak;
5386
5387 Nucleus::setInsertBlock(loopBlock);
5388
5389 loopRepDepth++;
5390 breakDepth = 0;
5391 }
5392
John Bauman89401822014-05-06 15:04:28 -04005393 void PixelRoutine::RET(Registers &r)
5394 {
John Bauman19bac1e2014-05-06 15:23:49 -04005395 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04005396 {
5397 returnBlock = Nucleus::createBasicBlock();
5398 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04005399 }
5400 else
5401 {
John Bauman89401822014-05-06 15:04:28 -04005402 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04005403
John Bauman19bac1e2014-05-06 15:23:49 -04005404 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04005405 {
John Bauman19bac1e2014-05-06 15:23:49 -04005406 // FIXME: Encapsulate
5407 UInt index = r.callStack[--r.stackIndex];
5408
John Bauman66b8ab22014-05-06 15:57:45 -04005409 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04005410 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
5411
5412 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
5413 {
5414 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
5415 }
5416 }
5417 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
5418 {
5419 Nucleus::createBr(callRetBlock[currentLabel][0]);
5420 }
5421 else // Function isn't called
5422 {
5423 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04005424 }
5425
5426 Nucleus::setInsertBlock(unreachableBlock);
5427 Nucleus::createUnreachable();
5428 }
5429 }
5430
John Bauman19bac1e2014-05-06 15:23:49 -04005431 void PixelRoutine::LEAVE(Registers &r)
5432 {
5433 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
5434
5435 // FIXME: Return from function if all instances left
5436 // FIXME: Use enableLeave in other control-flow constructs
5437 }
5438
5439 void PixelRoutine::writeDestination(Registers &r, Vector4i &d, const Dst &dst)
John Bauman89401822014-05-06 15:04:28 -04005440 {
5441 switch(dst.type)
5442 {
John Bauman19bac1e2014-05-06 15:23:49 -04005443 case Shader::PARAMETER_TEMP:
John Bauman89401822014-05-06 15:04:28 -04005444 if(dst.mask & 0x1) r.ri[dst.index].x = d.x;
5445 if(dst.mask & 0x2) r.ri[dst.index].y = d.y;
5446 if(dst.mask & 0x4) r.ri[dst.index].z = d.z;
5447 if(dst.mask & 0x8) r.ri[dst.index].w = d.w;
5448 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005449 case Shader::PARAMETER_INPUT:
John Bauman89401822014-05-06 15:04:28 -04005450 if(dst.mask & 0x1) r.vi[dst.index].x = d.x;
5451 if(dst.mask & 0x2) r.vi[dst.index].y = d.y;
5452 if(dst.mask & 0x4) r.vi[dst.index].z = d.z;
5453 if(dst.mask & 0x8) r.vi[dst.index].w = d.w;
5454 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005455 case Shader::PARAMETER_CONST: ASSERT(false); break;
5456 case Shader::PARAMETER_TEXTURE:
John Bauman89401822014-05-06 15:04:28 -04005457 if(dst.mask & 0x1) r.ti[dst.index].x = d.x;
5458 if(dst.mask & 0x2) r.ti[dst.index].y = d.y;
5459 if(dst.mask & 0x4) r.ti[dst.index].z = d.z;
5460 if(dst.mask & 0x8) r.ti[dst.index].w = d.w;
5461 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005462 case Shader::PARAMETER_COLOROUT:
John Bauman89401822014-05-06 15:04:28 -04005463 if(dst.mask & 0x1) r.vi[dst.index].x = d.x;
5464 if(dst.mask & 0x2) r.vi[dst.index].y = d.y;
5465 if(dst.mask & 0x4) r.vi[dst.index].z = d.z;
5466 if(dst.mask & 0x8) r.vi[dst.index].w = d.w;
5467 break;
5468 default:
5469 ASSERT(false);
5470 }
5471 }
5472
John Bauman19bac1e2014-05-06 15:23:49 -04005473 Vector4i PixelRoutine::regi(Registers &r, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04005474 {
John Bauman19bac1e2014-05-06 15:23:49 -04005475 Vector4i *reg;
John Bauman89401822014-05-06 15:04:28 -04005476 int i = src.index;
5477
John Bauman19bac1e2014-05-06 15:23:49 -04005478 Vector4i c;
John Bauman89401822014-05-06 15:04:28 -04005479
John Bauman19bac1e2014-05-06 15:23:49 -04005480 if(src.type == Shader::PARAMETER_CONST)
John Bauman89401822014-05-06 15:04:28 -04005481 {
John Bauman19bac1e2014-05-06 15:23:49 -04005482 c.x = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][0]));
5483 c.y = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][1]));
5484 c.z = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][2]));
5485 c.w = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][3]));
John Bauman89401822014-05-06 15:04:28 -04005486 }
5487
5488 switch(src.type)
5489 {
John Bauman19bac1e2014-05-06 15:23:49 -04005490 case Shader::PARAMETER_TEMP: reg = &r.ri[i]; break;
5491 case Shader::PARAMETER_INPUT: reg = &r.vi[i]; break;
5492 case Shader::PARAMETER_CONST: reg = &c; break;
5493 case Shader::PARAMETER_TEXTURE: reg = &r.ti[i]; break;
5494 case Shader::PARAMETER_VOID: return r.ri[0]; // Dummy
5495 case Shader::PARAMETER_FLOAT4LITERAL: return r.ri[0]; // Dummy
John Bauman89401822014-05-06 15:04:28 -04005496 default:
5497 ASSERT(false);
5498 }
5499
John Bauman66b8ab22014-05-06 15:57:45 -04005500 const Short4 &x = (*reg)[(src.swizzle >> 0) & 0x3];
5501 const Short4 &y = (*reg)[(src.swizzle >> 2) & 0x3];
5502 const Short4 &z = (*reg)[(src.swizzle >> 4) & 0x3];
5503 const Short4 &w = (*reg)[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -04005504
John Bauman19bac1e2014-05-06 15:23:49 -04005505 Vector4i mod;
John Bauman89401822014-05-06 15:04:28 -04005506
5507 switch(src.modifier)
5508 {
John Bauman19bac1e2014-05-06 15:23:49 -04005509 case Shader::MODIFIER_NONE:
5510 mod.x = x;
5511 mod.y = y;
5512 mod.z = z;
5513 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -04005514 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005515 case Shader::MODIFIER_BIAS:
5516 mod.x = SubSat(x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5517 mod.y = SubSat(y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5518 mod.z = SubSat(z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5519 mod.w = SubSat(w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -04005520 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005521 case Shader::MODIFIER_BIAS_NEGATE:
5522 mod.x = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), x);
5523 mod.y = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), y);
5524 mod.z = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), z);
5525 mod.w = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), w);
John Bauman89401822014-05-06 15:04:28 -04005526 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005527 case Shader::MODIFIER_COMPLEMENT:
5528 mod.x = SubSat(Short4(0x1000), x);
5529 mod.y = SubSat(Short4(0x1000), y);
5530 mod.z = SubSat(Short4(0x1000), z);
5531 mod.w = SubSat(Short4(0x1000), w);
John Bauman89401822014-05-06 15:04:28 -04005532 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005533 case Shader::MODIFIER_NEGATE:
5534 mod.x = -x;
5535 mod.y = -y;
5536 mod.z = -z;
5537 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -04005538 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005539 case Shader::MODIFIER_X2:
5540 mod.x = AddSat(x, x);
5541 mod.y = AddSat(y, y);
5542 mod.z = AddSat(z, z);
5543 mod.w = AddSat(w, w);
John Bauman89401822014-05-06 15:04:28 -04005544 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005545 case Shader::MODIFIER_X2_NEGATE:
5546 mod.x = -AddSat(x, x);
5547 mod.y = -AddSat(y, y);
5548 mod.z = -AddSat(z, z);
5549 mod.w = -AddSat(w, w);
John Bauman89401822014-05-06 15:04:28 -04005550 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005551 case Shader::MODIFIER_SIGN:
5552 mod.x = SubSat(x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5553 mod.y = SubSat(y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5554 mod.z = SubSat(z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5555 mod.w = SubSat(w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5556 mod.x = AddSat(mod.x, mod.x);
5557 mod.y = AddSat(mod.y, mod.y);
5558 mod.z = AddSat(mod.z, mod.z);
5559 mod.w = AddSat(mod.w, mod.w);
John Bauman89401822014-05-06 15:04:28 -04005560 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005561 case Shader::MODIFIER_SIGN_NEGATE:
5562 mod.x = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), x);
5563 mod.y = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), y);
5564 mod.z = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), z);
5565 mod.w = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), w);
5566 mod.x = AddSat(mod.x, mod.x);
5567 mod.y = AddSat(mod.y, mod.y);
5568 mod.z = AddSat(mod.z, mod.z);
5569 mod.w = AddSat(mod.w, mod.w);
John Bauman89401822014-05-06 15:04:28 -04005570 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005571 case Shader::MODIFIER_DZ:
5572 mod.x = x;
5573 mod.y = y;
5574 mod.z = z;
5575 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -04005576 // Projection performed by texture sampler
5577 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005578 case Shader::MODIFIER_DW:
5579 mod.x = x;
5580 mod.y = y;
5581 mod.z = z;
5582 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -04005583 // Projection performed by texture sampler
5584 break;
5585 default:
5586 ASSERT(false);
5587 }
5588
John Bauman19bac1e2014-05-06 15:23:49 -04005589 if(src.type == Shader::PARAMETER_CONST && (src.modifier == Shader::MODIFIER_X2 || src.modifier == Shader::MODIFIER_X2_NEGATE))
John Bauman89401822014-05-06 15:04:28 -04005590 {
John Bauman19bac1e2014-05-06 15:23:49 -04005591 mod.x = Min(mod.x, Short4(0x1000)); mod.x = Max(mod.x, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
5592 mod.y = Min(mod.y, Short4(0x1000)); mod.y = Max(mod.y, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
5593 mod.z = Min(mod.z, Short4(0x1000)); mod.z = Max(mod.z, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
5594 mod.w = Min(mod.w, Short4(0x1000)); mod.w = Max(mod.w, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
John Bauman89401822014-05-06 15:04:28 -04005595 }
5596
5597 return mod;
5598 }
5599
John Bauman19bac1e2014-05-06 15:23:49 -04005600 Vector4f PixelRoutine::reg(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -04005601 {
John Bauman19bac1e2014-05-06 15:23:49 -04005602 Vector4f reg;
John Bauman89401822014-05-06 15:04:28 -04005603 int i = src.index + offset;
5604
5605 switch(src.type)
5606 {
John Bauman19bac1e2014-05-06 15:23:49 -04005607 case Shader::PARAMETER_TEMP:
5608 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman89401822014-05-06 15:04:28 -04005609 {
John Bauman19bac1e2014-05-06 15:23:49 -04005610 reg = r.rf[i];
5611 }
5612 else
5613 {
5614 Int a = relativeAddress(r, src);
5615
5616 reg = r.rf[i + a];
5617 }
5618 break;
5619 case Shader::PARAMETER_INPUT:
5620 {
5621 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -04005622 {
John Bauman19bac1e2014-05-06 15:23:49 -04005623 reg = r.vf[i];
John Bauman89401822014-05-06 15:04:28 -04005624 }
John Bauman19bac1e2014-05-06 15:23:49 -04005625 else if(src.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -04005626 {
5627 Int aL = r.aL[r.loopDepth];
5628
John Bauman19bac1e2014-05-06 15:23:49 -04005629 reg = r.vf[i + aL];
John Bauman89401822014-05-06 15:04:28 -04005630 }
John Bauman19bac1e2014-05-06 15:23:49 -04005631 else
John Bauman89401822014-05-06 15:04:28 -04005632 {
John Bauman19bac1e2014-05-06 15:23:49 -04005633 Int a = relativeAddress(r, src);
5634
5635 reg = r.vf[i + a];
John Bauman89401822014-05-06 15:04:28 -04005636 }
5637 }
5638 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005639 case Shader::PARAMETER_CONST:
5640 reg = readConstant(r, src, offset);
John Bauman89401822014-05-06 15:04:28 -04005641 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005642 case Shader::PARAMETER_TEXTURE:
5643 reg = r.vf[2 + i];
5644 break;
5645 case Shader::PARAMETER_MISCTYPE:
John Bauman89401822014-05-06 15:04:28 -04005646 if(src.index == 0) reg = r.vPos;
5647 if(src.index == 1) reg = r.vFace;
5648 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005649 case Shader::PARAMETER_SAMPLER:
5650 if(src.rel.type == Shader::PARAMETER_VOID)
5651 {
5652 reg.x = As<Float4>(Int4(i));
5653 }
5654 else if(src.rel.type == Shader::PARAMETER_TEMP)
5655 {
5656 reg.x = As<Float4>(Int4(i) + RoundInt(r.rf[src.rel.index].x));
5657 }
5658 return reg;
5659 case Shader::PARAMETER_PREDICATE: return reg; // Dummy
5660 case Shader::PARAMETER_VOID: return reg; // Dummy
5661 case Shader::PARAMETER_FLOAT4LITERAL:
5662 reg.x = Float4(src.value[0]);
5663 reg.y = Float4(src.value[1]);
5664 reg.z = Float4(src.value[2]);
5665 reg.w = Float4(src.value[3]);
5666 break;
5667 case Shader::PARAMETER_CONSTINT: return reg; // Dummy
5668 case Shader::PARAMETER_CONSTBOOL: return reg; // Dummy
5669 case Shader::PARAMETER_LOOP: return reg; // Dummy
5670 case Shader::PARAMETER_COLOROUT:
5671 reg = r.oC[i];
5672 break;
5673 case Shader::PARAMETER_DEPTHOUT:
5674 reg.x = r.oDepth;
5675 break;
John Bauman89401822014-05-06 15:04:28 -04005676 default:
5677 ASSERT(false);
5678 }
5679
John Bauman66b8ab22014-05-06 15:57:45 -04005680 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
5681 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
5682 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
5683 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -04005684
John Bauman19bac1e2014-05-06 15:23:49 -04005685 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -04005686
5687 switch(src.modifier)
5688 {
John Bauman19bac1e2014-05-06 15:23:49 -04005689 case Shader::MODIFIER_NONE:
John Bauman89401822014-05-06 15:04:28 -04005690 mod.x = x;
5691 mod.y = y;
5692 mod.z = z;
5693 mod.w = w;
5694 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005695 case Shader::MODIFIER_NEGATE:
John Bauman89401822014-05-06 15:04:28 -04005696 mod.x = -x;
5697 mod.y = -y;
5698 mod.z = -z;
5699 mod.w = -w;
5700 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005701 case Shader::MODIFIER_ABS:
John Bauman89401822014-05-06 15:04:28 -04005702 mod.x = Abs(x);
5703 mod.y = Abs(y);
5704 mod.z = Abs(z);
5705 mod.w = Abs(w);
5706 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005707 case Shader::MODIFIER_ABS_NEGATE:
John Bauman89401822014-05-06 15:04:28 -04005708 mod.x = -Abs(x);
5709 mod.y = -Abs(y);
5710 mod.z = -Abs(z);
5711 mod.w = -Abs(w);
5712 break;
John Bauman66b8ab22014-05-06 15:57:45 -04005713 case Shader::MODIFIER_NOT:
5714 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
5715 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
5716 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
5717 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
5718 break;
John Bauman89401822014-05-06 15:04:28 -04005719 default:
5720 ASSERT(false);
5721 }
5722
5723 return mod;
5724 }
5725
John Bauman19bac1e2014-05-06 15:23:49 -04005726 Vector4f PixelRoutine::readConstant(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -04005727 {
John Bauman19bac1e2014-05-06 15:23:49 -04005728 Vector4f c;
5729
5730 int i = src.index + offset;
5731
5732 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
5733 {
5734 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[i]));
5735
5736 c.x = c.x.xxxx;
5737 c.y = c.y.yyyy;
5738 c.z = c.z.zzzz;
5739 c.w = c.w.wwww;
5740
5741 if(localShaderConstants) // Constant may be known at compile time
5742 {
Alexis Hetu903e0252014-11-25 14:25:32 -05005743 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -04005744 {
5745 const Shader::Instruction &instruction = *shader->getInstruction(j);
5746
5747 if(instruction.opcode == Shader::OPCODE_DEF)
5748 {
5749 if(instruction.dst.index == i)
5750 {
5751 c.x = Float4(instruction.src[0].value[0]);
5752 c.y = Float4(instruction.src[0].value[1]);
5753 c.z = Float4(instruction.src[0].value[2]);
5754 c.w = Float4(instruction.src[0].value[3]);
5755
5756 break;
5757 }
5758 }
5759 }
5760 }
5761 }
5762 else if(src.rel.type == Shader::PARAMETER_LOOP)
5763 {
5764 Int loopCounter = r.aL[r.loopDepth];
5765
5766 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[i]) + loopCounter * 16);
5767
5768 c.x = c.x.xxxx;
5769 c.y = c.y.yyyy;
5770 c.z = c.z.zzzz;
5771 c.w = c.w.wwww;
5772 }
5773 else
5774 {
5775 Int a = relativeAddress(r, src);
5776
5777 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[i]) + a * 16);
5778
5779 c.x = c.x.xxxx;
5780 c.y = c.y.yyyy;
5781 c.z = c.z.zzzz;
5782 c.w = c.w.wwww;
5783 }
5784
5785 return c;
John Bauman89401822014-05-06 15:04:28 -04005786 }
5787
John Bauman19bac1e2014-05-06 15:23:49 -04005788 Int PixelRoutine::relativeAddress(Registers &r, const Shader::Parameter &var)
John Bauman89401822014-05-06 15:04:28 -04005789 {
John Bauman19bac1e2014-05-06 15:23:49 -04005790 ASSERT(var.rel.deterministic);
5791
5792 if(var.rel.type == Shader::PARAMETER_TEMP)
5793 {
5794 return RoundInt(Extract(r.rf[var.rel.index].x, 0)) * var.rel.scale;
5795 }
5796 else if(var.rel.type == Shader::PARAMETER_INPUT)
5797 {
5798 return RoundInt(Extract(r.vf[var.rel.index].x, 0)) * var.rel.scale;
5799 }
5800 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
5801 {
5802 return RoundInt(Extract(r.oC[var.rel.index].x, 0)) * var.rel.scale;
5803 }
5804 else if(var.rel.type == Shader::PARAMETER_CONST)
5805 {
Nicolas Capensb5e7a2a2014-05-06 16:38:19 -04005806 RValue<Float4> c = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[var.rel.index]));
John Bauman19bac1e2014-05-06 15:23:49 -04005807
5808 return RoundInt(Extract(c, 0)) * var.rel.scale;
5809 }
5810 else ASSERT(false);
5811
5812 return 0;
5813 }
5814
5815 Int4 PixelRoutine::enableMask(Registers &r, const Shader::Instruction *instruction)
5816 {
5817 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
John Baumand4ae8632014-05-06 16:18:33 -04005818
5819 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -04005820 {
John Baumand4ae8632014-05-06 16:18:33 -04005821 if(shader->containsBreakInstruction() && instruction->analysisBreak)
5822 {
5823 enable &= r.enableBreak;
5824 }
John Bauman19bac1e2014-05-06 15:23:49 -04005825
John Baumand4ae8632014-05-06 16:18:33 -04005826 if(shader->containsContinueInstruction() && instruction->analysisContinue)
5827 {
5828 enable &= r.enableContinue;
5829 }
John Bauman19bac1e2014-05-06 15:23:49 -04005830
John Baumand4ae8632014-05-06 16:18:33 -04005831 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
5832 {
5833 enable &= r.enableLeave;
5834 }
John Bauman19bac1e2014-05-06 15:23:49 -04005835 }
5836
5837 return enable;
5838 }
5839
5840 bool PixelRoutine::colorUsed()
5841 {
5842 return state.colorWriteMask || state.alphaTestActive() || state.shaderContainsKill;
5843 }
5844
5845 unsigned short PixelRoutine::shaderVersion() const
5846 {
5847 return shader ? shader->getVersion() : 0x0000;
5848 }
5849
5850 bool PixelRoutine::interpolateZ() const
5851 {
5852 return state.depthTestActive || state.pixelFogActive() || (shader && shader->vPosDeclared && fullPixelPositionRegister);
5853 }
5854
5855 bool PixelRoutine::interpolateW() const
5856 {
5857 return state.perspective || (shader && shader->vPosDeclared && fullPixelPositionRegister);
John Bauman89401822014-05-06 15:04:28 -04005858 }
5859}