blob: 7c0f931d18b721fa72b6e9de37a75301c3f92138 [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 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040056 for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040057 {
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
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040068 for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040069 {
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
Alexis Hetu0b65c5e2015-03-31 11:48:57 -04002081 for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04002082 {
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:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002138 case FORMAT_A8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04002139 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002140 case FORMAT_X8B8G8R8:
John Bauman66b8ab22014-05-06 15:57:45 -04002141 case FORMAT_A8:
John Bauman89401822014-05-06 15:04:28 -04002142 case FORMAT_G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04002143 oC[index].x = Max(oC[index].x, Float4(0.0f)); oC[index].x = Min(oC[index].x, Float4(1.0f));
2144 oC[index].y = Max(oC[index].y, Float4(0.0f)); oC[index].y = Min(oC[index].y, Float4(1.0f));
2145 oC[index].z = Max(oC[index].z, Float4(0.0f)); oC[index].z = Min(oC[index].z, Float4(1.0f));
2146 oC[index].w = Max(oC[index].w, Float4(0.0f)); oC[index].w = Min(oC[index].w, Float4(1.0f));
John Bauman89401822014-05-06 15:04:28 -04002147 break;
2148 case FORMAT_R32F:
2149 case FORMAT_G32R32F:
2150 case FORMAT_A32B32G32R32F:
2151 break;
2152 default:
2153 ASSERT(false);
2154 }
2155 }
2156 }
2157
John Bauman19bac1e2014-05-06 15:23:49 -04002158 void PixelRoutine::rasterOperation(Vector4i &current, Registers &r, Float4 &fog, Pointer<Byte> &cBuffer, Int &x, Int sMask[4], Int zMask[4], Int cMask[4])
John Bauman89401822014-05-06 15:04:28 -04002159 {
2160 if(!state.colorWriteActive(0))
2161 {
2162 return;
2163 }
2164
John Bauman19bac1e2014-05-06 15:23:49 -04002165 Vector4f oC;
John Bauman89401822014-05-06 15:04:28 -04002166
2167 switch(state.targetFormat[0])
2168 {
2169 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002170 case FORMAT_X8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04002171 case FORMAT_A8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002172 case FORMAT_A8B8G8R8:
John Bauman66b8ab22014-05-06 15:57:45 -04002173 case FORMAT_A8:
John Bauman89401822014-05-06 15:04:28 -04002174 case FORMAT_G16R16:
2175 case FORMAT_A16B16G16R16:
2176 if(!postBlendSRGB && state.writeSRGB)
2177 {
2178 linearToSRGB12_16(r, current);
2179 }
2180 else
2181 {
John Bauman19bac1e2014-05-06 15:23:49 -04002182 current.x <<= 4;
2183 current.y <<= 4;
2184 current.z <<= 4;
2185 current.w <<= 4;
John Bauman89401822014-05-06 15:04:28 -04002186 }
2187
2188 fogBlend(r, current, fog, r.z[0], r.rhw);
2189
2190 for(unsigned int q = 0; q < state.multiSample; q++)
2191 {
2192 Pointer<Byte> buffer = cBuffer + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002193 Vector4i color = current;
John Bauman89401822014-05-06 15:04:28 -04002194
2195 if(state.multiSampleMask & (1 << q))
2196 {
2197 alphaBlend(r, 0, buffer, color, x);
2198 writeColor(r, 0, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2199 }
2200 }
2201 break;
2202 case FORMAT_R32F:
2203 case FORMAT_G32R32F:
2204 case FORMAT_A32B32G32R32F:
2205 convertSigned12(oC, current);
2206 fogBlend(r, oC, fog, r.z[0], r.rhw);
2207
2208 for(unsigned int q = 0; q < state.multiSample; q++)
2209 {
2210 Pointer<Byte> buffer = cBuffer + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002211 Vector4f color = oC;
John Bauman89401822014-05-06 15:04:28 -04002212
2213 if(state.multiSampleMask & (1 << q))
2214 {
2215 alphaBlend(r, 0, buffer, color, x);
2216 writeColor(r, 0, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2217 }
2218 }
2219 break;
2220 default:
2221 ASSERT(false);
2222 }
2223 }
2224
John Bauman19bac1e2014-05-06 15:23:49 -04002225 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 -04002226 {
2227 for(int index = 0; index < 4; index++)
2228 {
2229 if(!state.colorWriteActive(index))
2230 {
2231 continue;
2232 }
2233
2234 if(!postBlendSRGB && state.writeSRGB)
2235 {
John Bauman19bac1e2014-05-06 15:23:49 -04002236 oC[index].x = linearToSRGB(oC[index].x);
2237 oC[index].y = linearToSRGB(oC[index].y);
2238 oC[index].z = linearToSRGB(oC[index].z);
John Bauman89401822014-05-06 15:04:28 -04002239 }
2240
2241 if(index == 0)
2242 {
2243 fogBlend(r, oC[index], fog, r.z[0], r.rhw);
2244 }
2245
2246 switch(state.targetFormat[index])
2247 {
2248 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002249 case FORMAT_X8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04002250 case FORMAT_A8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002251 case FORMAT_A8B8G8R8:
John Bauman66b8ab22014-05-06 15:57:45 -04002252 case FORMAT_A8:
John Bauman89401822014-05-06 15:04:28 -04002253 case FORMAT_G16R16:
2254 case FORMAT_A16B16G16R16:
2255 for(unsigned int q = 0; q < state.multiSample; q++)
2256 {
2257 Pointer<Byte> buffer = cBuffer[index] + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002258 Vector4i color;
John Bauman89401822014-05-06 15:04:28 -04002259
John Bauman19bac1e2014-05-06 15:23:49 -04002260 color.x = convertFixed16(oC[index].x, false);
2261 color.y = convertFixed16(oC[index].y, false);
2262 color.z = convertFixed16(oC[index].z, false);
2263 color.w = convertFixed16(oC[index].w, false);
John Bauman89401822014-05-06 15:04:28 -04002264
2265 if(state.multiSampleMask & (1 << q))
2266 {
2267 alphaBlend(r, index, buffer, color, x);
2268 writeColor(r, index, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2269 }
2270 }
2271 break;
2272 case FORMAT_R32F:
2273 case FORMAT_G32R32F:
2274 case FORMAT_A32B32G32R32F:
2275 for(unsigned int q = 0; q < state.multiSample; q++)
2276 {
2277 Pointer<Byte> buffer = cBuffer[index] + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002278 Vector4f color = oC[index];
John Bauman89401822014-05-06 15:04:28 -04002279
2280 if(state.multiSampleMask & (1 << q))
2281 {
2282 alphaBlend(r, index, buffer, color, x);
2283 writeColor(r, index, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2284 }
2285 }
2286 break;
2287 default:
2288 ASSERT(false);
2289 }
2290 }
2291 }
2292
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002293 void PixelRoutine::blendFactor(Registers &r, const Vector4i &blendFactor, const Vector4i &current, const Vector4i &pixel, BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -04002294 {
2295 switch(blendFactorActive)
2296 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002297 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04002298 // Optimized
2299 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002300 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04002301 // Optimized
2302 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002303 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002304 blendFactor.x = current.x;
2305 blendFactor.y = current.y;
2306 blendFactor.z = current.z;
John Bauman89401822014-05-06 15:04:28 -04002307 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002308 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002309 blendFactor.x = Short4(0xFFFFu) - current.x;
2310 blendFactor.y = Short4(0xFFFFu) - current.y;
2311 blendFactor.z = Short4(0xFFFFu) - current.z;
John Bauman89401822014-05-06 15:04:28 -04002312 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002313 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002314 blendFactor.x = pixel.x;
2315 blendFactor.y = pixel.y;
2316 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002317 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002318 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002319 blendFactor.x = Short4(0xFFFFu) - pixel.x;
2320 blendFactor.y = Short4(0xFFFFu) - pixel.y;
2321 blendFactor.z = Short4(0xFFFFu) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002322 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002323 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002324 blendFactor.x = current.w;
2325 blendFactor.y = current.w;
2326 blendFactor.z = current.w;
John Bauman89401822014-05-06 15:04:28 -04002327 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002328 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002329 blendFactor.x = Short4(0xFFFFu) - current.w;
2330 blendFactor.y = Short4(0xFFFFu) - current.w;
2331 blendFactor.z = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -04002332 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002333 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002334 blendFactor.x = pixel.w;
2335 blendFactor.y = pixel.w;
2336 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002337 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002338 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002339 blendFactor.x = Short4(0xFFFFu) - pixel.w;
2340 blendFactor.y = Short4(0xFFFFu) - pixel.w;
2341 blendFactor.z = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002342 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002343 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04002344 blendFactor.x = Short4(0xFFFFu) - pixel.w;
2345 blendFactor.x = Min(As<UShort4>(blendFactor.x), As<UShort4>(current.w));
2346 blendFactor.y = blendFactor.x;
2347 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -04002348 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002349 case BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04002350 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[0]));
2351 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[1]));
2352 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[2]));
John Bauman89401822014-05-06 15:04:28 -04002353 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002354 case BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04002355 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[0]));
2356 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[1]));
2357 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[2]));
John Bauman89401822014-05-06 15:04:28 -04002358 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002359 case BLEND_CONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002360 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
2361 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
2362 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04002363 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002364 case BLEND_INVCONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002365 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
2366 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
2367 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04002368 break;
2369 default:
2370 ASSERT(false);
2371 }
2372 }
2373
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002374 void PixelRoutine::blendFactorAlpha(Registers &r, const Vector4i &blendFactor, const Vector4i &current, const Vector4i &pixel, BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -04002375 {
2376 switch(blendFactorAlphaActive)
2377 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002378 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04002379 // Optimized
2380 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002381 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04002382 // Optimized
2383 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002384 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002385 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -04002386 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002387 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002388 blendFactor.w = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -04002389 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002390 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002391 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002392 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002393 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002394 blendFactor.w = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002395 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002396 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002397 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -04002398 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002399 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002400 blendFactor.w = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -04002401 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002402 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002403 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002404 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002405 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002406 blendFactor.w = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002407 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002408 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04002409 blendFactor.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002410 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002411 case BLEND_CONSTANT:
2412 case BLEND_CONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002413 blendFactor.w = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04002414 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002415 case BLEND_INVCONSTANT:
2416 case BLEND_INVCONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002417 blendFactor.w = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04002418 break;
2419 default:
2420 ASSERT(false);
2421 }
2422 }
2423
John Bauman19bac1e2014-05-06 15:23:49 -04002424 void PixelRoutine::alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4i &current, Int &x)
John Bauman89401822014-05-06 15:04:28 -04002425 {
2426 if(!state.alphaBlendActive)
2427 {
2428 return;
2429 }
2430
2431 Pointer<Byte> buffer;
2432
John Bauman19bac1e2014-05-06 15:23:49 -04002433 Vector4i pixel;
John Bauman89401822014-05-06 15:04:28 -04002434 Short4 c01;
2435 Short4 c23;
2436
2437 // Read pixel
2438 switch(state.targetFormat[index])
2439 {
2440 case FORMAT_A8R8G8B8:
2441 buffer = cBuffer + 4 * x;
2442 c01 = *Pointer<Short4>(buffer);
2443 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2444 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04002445 pixel.z = c01;
2446 pixel.y = c01;
2447 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
2448 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
2449 pixel.x = pixel.z;
2450 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
2451 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
2452 pixel.y = pixel.z;
2453 pixel.w = pixel.x;
2454 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
2455 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
2456 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
2457 pixel.w = UnpackHigh(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002458 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002459 case FORMAT_A8B8G8R8:
2460 buffer = cBuffer + 4 * x;
2461 c01 = *Pointer<Short4>(buffer);
2462 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2463 c23 = *Pointer<Short4>(buffer);
2464 pixel.z = c01;
2465 pixel.y = c01;
2466 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
2467 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
2468 pixel.x = pixel.z;
2469 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
2470 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
2471 pixel.y = pixel.z;
2472 pixel.w = pixel.x;
2473 pixel.x = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
2474 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
2475 pixel.z = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
2476 pixel.w = UnpackHigh(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
2477 break;
John Bauman66b8ab22014-05-06 15:57:45 -04002478 case FORMAT_A8:
2479 buffer = cBuffer + 1 * x;
2480 pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 0);
2481 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2482 pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 1);
2483 pixel.w = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
2484 pixel.x = Short4(0x0000);
2485 pixel.y = Short4(0x0000);
2486 pixel.z = Short4(0x0000);
2487 break;
John Bauman89401822014-05-06 15:04:28 -04002488 case FORMAT_X8R8G8B8:
2489 buffer = cBuffer + 4 * x;
2490 c01 = *Pointer<Short4>(buffer);
2491 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2492 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04002493 pixel.z = c01;
2494 pixel.y = c01;
2495 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
2496 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
2497 pixel.x = pixel.z;
2498 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
2499 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
2500 pixel.y = pixel.z;
2501 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
2502 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
2503 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
2504 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002505 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002506 case FORMAT_X8B8G8R8:
2507 buffer = cBuffer + 4 * x;
2508 c01 = *Pointer<Short4>(buffer);
2509 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2510 c23 = *Pointer<Short4>(buffer);
2511 pixel.z = c01;
2512 pixel.y = c01;
2513 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
2514 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
2515 pixel.x = pixel.z;
2516 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
2517 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
2518 pixel.y = pixel.z;
2519 pixel.w = pixel.x;
2520 pixel.x = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
2521 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
2522 pixel.z = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
2523 pixel.w = Short4(0xFFFFu);
2524 break;
John Bauman89401822014-05-06 15:04:28 -04002525 case FORMAT_A8G8R8B8Q:
2526 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002527 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2528 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2529 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
2530 // pixel.w = UnpackHigh(As<Byte8>(pixel.w), *Pointer<Byte8>(cBuffer + 8 * x + 8));
John Bauman89401822014-05-06 15:04:28 -04002531 break;
2532 case FORMAT_X8G8R8B8Q:
2533 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002534 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2535 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2536 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
2537 // pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002538 break;
2539 case FORMAT_A16B16G16R16:
2540 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002541 pixel.x = *Pointer<Short4>(buffer + 8 * x);
2542 pixel.y = *Pointer<Short4>(buffer + 8 * x + 8);
John Bauman89401822014-05-06 15:04:28 -04002543 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002544 pixel.z = *Pointer<Short4>(buffer + 8 * x);
2545 pixel.w = *Pointer<Short4>(buffer + 8 * x + 8);
2546 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04002547 break;
2548 case FORMAT_G16R16:
2549 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002550 pixel.x = *Pointer<Short4>(buffer + 4 * x);
John Bauman89401822014-05-06 15:04:28 -04002551 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002552 pixel.y = *Pointer<Short4>(buffer + 4 * x);
2553 pixel.z = pixel.x;
2554 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.y));
2555 pixel.z = As<Short4>(UnpackHigh(pixel.z, pixel.y));
2556 pixel.y = pixel.z;
2557 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.z));
2558 pixel.y = As<Short4>(UnpackHigh(pixel.y, pixel.z));
2559 pixel.z = Short4(0xFFFFu);
2560 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002561 break;
2562 default:
2563 ASSERT(false);
2564 }
2565
2566 if(postBlendSRGB && state.writeSRGB)
2567 {
2568 sRGBtoLinear16_16(r, pixel);
2569 }
2570
2571 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
John Bauman19bac1e2014-05-06 15:23:49 -04002572 Vector4i sourceFactor;
2573 Vector4i destFactor;
John Bauman89401822014-05-06 15:04:28 -04002574
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002575 blendFactor(r, sourceFactor, current, pixel, state.sourceBlendFactor);
2576 blendFactor(r, destFactor, current, pixel, state.destBlendFactor);
John Bauman89401822014-05-06 15:04:28 -04002577
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002578 if(state.sourceBlendFactor != BLEND_ONE && state.sourceBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002579 {
John Bauman19bac1e2014-05-06 15:23:49 -04002580 current.x = MulHigh(As<UShort4>(current.x), As<UShort4>(sourceFactor.x));
2581 current.y = MulHigh(As<UShort4>(current.y), As<UShort4>(sourceFactor.y));
2582 current.z = MulHigh(As<UShort4>(current.z), As<UShort4>(sourceFactor.z));
John Bauman89401822014-05-06 15:04:28 -04002583 }
2584
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002585 if(state.destBlendFactor != BLEND_ONE && state.destBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002586 {
John Bauman19bac1e2014-05-06 15:23:49 -04002587 pixel.x = MulHigh(As<UShort4>(pixel.x), As<UShort4>(destFactor.x));
2588 pixel.y = MulHigh(As<UShort4>(pixel.y), As<UShort4>(destFactor.y));
2589 pixel.z = MulHigh(As<UShort4>(pixel.z), As<UShort4>(destFactor.z));
John Bauman89401822014-05-06 15:04:28 -04002590 }
2591
2592 switch(state.blendOperation)
2593 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002594 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002595 current.x = AddSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
2596 current.y = AddSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
2597 current.z = AddSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002598 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002599 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002600 current.x = SubSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
2601 current.y = SubSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
2602 current.z = SubSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002603 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002604 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002605 current.x = SubSat(As<UShort4>(pixel.x), As<UShort4>(current.x));
2606 current.y = SubSat(As<UShort4>(pixel.y), As<UShort4>(current.y));
2607 current.z = SubSat(As<UShort4>(pixel.z), As<UShort4>(current.z));
John Bauman89401822014-05-06 15:04:28 -04002608 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002609 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002610 current.x = Min(As<UShort4>(current.x), As<UShort4>(pixel.x));
2611 current.y = Min(As<UShort4>(current.y), As<UShort4>(pixel.y));
2612 current.z = Min(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002613 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002614 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002615 current.x = Max(As<UShort4>(current.x), As<UShort4>(pixel.x));
2616 current.y = Max(As<UShort4>(current.y), As<UShort4>(pixel.y));
2617 current.z = Max(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002618 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002619 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04002620 // No operation
2621 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002622 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002623 current.x = pixel.x;
2624 current.y = pixel.y;
2625 current.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002626 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002627 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002628 current.x = Short4(0x0000, 0x0000, 0x0000, 0x0000);
2629 current.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
2630 current.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04002631 break;
2632 default:
2633 ASSERT(false);
2634 }
2635
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002636 blendFactorAlpha(r, sourceFactor, current, pixel, state.sourceBlendFactorAlpha);
2637 blendFactorAlpha(r, destFactor, current, pixel, state.destBlendFactorAlpha);
John Bauman89401822014-05-06 15:04:28 -04002638
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002639 if(state.sourceBlendFactorAlpha != BLEND_ONE && state.sourceBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002640 {
John Bauman19bac1e2014-05-06 15:23:49 -04002641 current.w = MulHigh(As<UShort4>(current.w), As<UShort4>(sourceFactor.w));
John Bauman89401822014-05-06 15:04:28 -04002642 }
2643
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002644 if(state.destBlendFactorAlpha != BLEND_ONE && state.destBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002645 {
John Bauman19bac1e2014-05-06 15:23:49 -04002646 pixel.w = MulHigh(As<UShort4>(pixel.w), As<UShort4>(destFactor.w));
John Bauman89401822014-05-06 15:04:28 -04002647 }
2648
2649 switch(state.blendOperationAlpha)
2650 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002651 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002652 current.w = AddSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002653 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002654 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002655 current.w = SubSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002656 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002657 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002658 current.w = SubSat(As<UShort4>(pixel.w), As<UShort4>(current.w));
John Bauman89401822014-05-06 15:04:28 -04002659 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002660 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002661 current.w = Min(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002662 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002663 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002664 current.w = Max(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002665 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002666 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04002667 // No operation
2668 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002669 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002670 current.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002671 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002672 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002673 current.w = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04002674 break;
2675 default:
2676 ASSERT(false);
2677 }
2678 }
2679
John Bauman19bac1e2014-05-06 15:23:49 -04002680 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 -04002681 {
2682 if(!state.colorWriteActive(index))
2683 {
2684 return;
2685 }
2686
2687 if(postBlendSRGB && state.writeSRGB)
2688 {
2689 linearToSRGB16_16(r, current);
2690 }
2691
2692 if(exactColorRounding)
2693 {
2694 switch(state.targetFormat[index])
2695 {
2696 case FORMAT_X8G8R8B8Q:
2697 case FORMAT_A8G8R8B8Q:
2698 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002699 case FORMAT_X8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04002700 case FORMAT_A8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002701 case FORMAT_A8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04002702 {
John Bauman19bac1e2014-05-06 15:23:49 -04002703 current.x = current.x - As<Short4>(As<UShort4>(current.x) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
2704 current.y = current.y - As<Short4>(As<UShort4>(current.y) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
2705 current.z = current.z - As<Short4>(As<UShort4>(current.z) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
2706 current.w = current.w - As<Short4>(As<UShort4>(current.w) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
John Bauman89401822014-05-06 15:04:28 -04002707 }
2708 break;
2709 }
2710 }
2711
2712 int rgbaWriteMask = state.colorWriteActive(index);
2713 int bgraWriteMask = rgbaWriteMask & 0x0000000A | (rgbaWriteMask & 0x00000001) << 2 | (rgbaWriteMask & 0x00000004) >> 2;
2714 int brgaWriteMask = rgbaWriteMask & 0x00000008 | (rgbaWriteMask & 0x00000001) << 1 | (rgbaWriteMask & 0x00000002) << 1 | (rgbaWriteMask & 0x00000004) >> 2;
2715
2716 switch(state.targetFormat[index])
2717 {
2718 case FORMAT_X8G8R8B8Q:
2719 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002720 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2721 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2722 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002723
John Bauman19bac1e2014-05-06 15:23:49 -04002724 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2725 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
John Bauman89401822014-05-06 15:04:28 -04002726 break;
2727 case FORMAT_A8G8R8B8Q:
2728 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002729 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2730 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2731 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
2732 // current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002733
John Bauman19bac1e2014-05-06 15:23:49 -04002734 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2735 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04002736 break;
2737 case FORMAT_X8R8G8B8:
2738 case FORMAT_A8R8G8B8:
2739 if(state.targetFormat[index] == FORMAT_X8R8G8B8 || rgbaWriteMask == 0x7)
2740 {
John Bauman19bac1e2014-05-06 15:23:49 -04002741 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2742 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2743 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002744
John Bauman19bac1e2014-05-06 15:23:49 -04002745 current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2746 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
John Bauman89401822014-05-06 15:04:28 -04002747
John Bauman19bac1e2014-05-06 15:23:49 -04002748 current.x = current.z;
2749 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
2750 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
2751 current.y = current.z;
2752 current.z = As<Short4>(UnpackLow(current.z, current.x));
2753 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04002754 }
2755 else
2756 {
John Bauman19bac1e2014-05-06 15:23:49 -04002757 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2758 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2759 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
2760 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002761
John Bauman19bac1e2014-05-06 15:23:49 -04002762 current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2763 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04002764
John Bauman19bac1e2014-05-06 15:23:49 -04002765 current.x = current.z;
2766 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
2767 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
2768 current.y = current.z;
2769 current.z = As<Short4>(UnpackLow(current.z, current.x));
2770 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04002771 }
2772 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002773 case FORMAT_X8B8G8R8:
2774 case FORMAT_A8B8G8R8:
2775 if(state.targetFormat[index] == FORMAT_X8B8G8R8 || rgbaWriteMask == 0x7)
2776 {
2777 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2778 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2779 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
2780
2781 current.z = As<Short4>(Pack(As<UShort4>(current.x), As<UShort4>(current.z)));
2782 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
2783
2784 current.x = current.z;
2785 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
2786 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
2787 current.y = current.z;
2788 current.z = As<Short4>(UnpackLow(current.z, current.x));
2789 current.y = As<Short4>(UnpackHigh(current.y, current.x));
2790 }
2791 else
2792 {
2793 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2794 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2795 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
2796 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
2797
2798 current.z = As<Short4>(Pack(As<UShort4>(current.x), As<UShort4>(current.z)));
2799 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
2800
2801 current.x = current.z;
2802 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
2803 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
2804 current.y = current.z;
2805 current.z = As<Short4>(UnpackLow(current.z, current.x));
2806 current.y = As<Short4>(UnpackHigh(current.y, current.x));
2807 }
2808 break;
John Bauman66b8ab22014-05-06 15:57:45 -04002809 case FORMAT_A8:
2810 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
2811 current.w = As<Short4>(Pack(As<UShort4>(current.w), As<UShort4>(current.w)));
2812 break;
John Bauman89401822014-05-06 15:04:28 -04002813 case FORMAT_G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04002814 current.z = current.x;
2815 current.x = As<Short4>(UnpackLow(current.x, current.y));
2816 current.z = As<Short4>(UnpackHigh(current.z, current.y));
2817 current.y = current.z;
John Bauman89401822014-05-06 15:04:28 -04002818 break;
2819 case FORMAT_A16B16G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04002820 transpose4x4(current.x, current.y, current.z, current.w);
John Bauman89401822014-05-06 15:04:28 -04002821 break;
2822 case FORMAT_R32F:
2823 case FORMAT_G32R32F:
2824 case FORMAT_A32B32G32R32F:
2825 {
John Bauman19bac1e2014-05-06 15:23:49 -04002826 Vector4f oC;
John Bauman89401822014-05-06 15:04:28 -04002827
John Bauman19bac1e2014-05-06 15:23:49 -04002828 oC.x = convertUnsigned16(UShort4(current.x));
2829 oC.y = convertUnsigned16(UShort4(current.y));
2830 oC.z = convertUnsigned16(UShort4(current.z));
2831 oC.w = convertUnsigned16(UShort4(current.w));
John Bauman89401822014-05-06 15:04:28 -04002832
2833 writeColor(r, index, cBuffer, x, oC, sMask, zMask, cMask);
2834 }
2835 return;
2836 default:
2837 ASSERT(false);
2838 }
2839
John Bauman19bac1e2014-05-06 15:23:49 -04002840 Short4 c01 = current.z;
2841 Short4 c23 = current.y;
John Bauman89401822014-05-06 15:04:28 -04002842
2843 Int xMask; // Combination of all masks
2844
2845 if(state.depthTestActive)
2846 {
2847 xMask = zMask;
2848 }
2849 else
2850 {
2851 xMask = cMask;
2852 }
2853
2854 if(state.stencilActive)
2855 {
2856 xMask &= sMask;
2857 }
2858
2859 Pointer<Byte> buffer;
2860 Short4 value;
2861
2862 switch(state.targetFormat[index])
2863 {
2864 case FORMAT_A8G8R8B8Q:
2865 case FORMAT_X8G8R8B8Q: // FIXME: Don't touch alpha?
2866 UNIMPLEMENTED();
2867 // value = *Pointer<Short4>(cBuffer + 8 * x + 0);
2868
2869 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
2870 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
2871 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2872 // {
2873 // Short4 masked = value;
2874 // c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2875 // masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2876 // c01 |= masked;
2877 // }
2878
2879 // c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
2880 // value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
2881 // c01 |= value;
2882 // *Pointer<Short4>(cBuffer + 8 * x + 0) = c01;
2883
2884 // value = *Pointer<Short4>(cBuffer + 8 * x + 8);
2885
2886 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
2887 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
2888 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2889 // {
2890 // Short4 masked = value;
2891 // c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2892 // masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2893 // c23 |= masked;
2894 // }
2895
2896 // c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
2897 // value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
2898 // c23 |= value;
2899 // *Pointer<Short4>(cBuffer + 8 * x + 8) = c23;
2900 break;
2901 case FORMAT_A8R8G8B8:
2902 case FORMAT_X8R8G8B8: // FIXME: Don't touch alpha?
2903 buffer = cBuffer + x * 4;
2904 value = *Pointer<Short4>(buffer);
2905
2906 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
2907 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
2908 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2909 {
2910 Short4 masked = value;
2911 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2912 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2913 c01 |= masked;
2914 }
2915
2916 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
2917 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
2918 c01 |= value;
2919 *Pointer<Short4>(buffer) = c01;
2920
2921 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2922 value = *Pointer<Short4>(buffer);
2923
2924 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
2925 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
2926 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2927 {
2928 Short4 masked = value;
2929 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2930 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2931 c23 |= masked;
2932 }
2933
2934 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
2935 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
2936 c23 |= value;
2937 *Pointer<Short4>(buffer) = c23;
2938 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002939 case FORMAT_A8B8G8R8:
2940 case FORMAT_X8B8G8R8: // FIXME: Don't touch alpha?
2941 buffer = cBuffer + x * 4;
2942 value = *Pointer<Short4>(buffer);
2943
2944 if((state.targetFormat[index] == FORMAT_A8B8G8R8 && rgbaWriteMask != 0x0000000F) ||
2945 ((state.targetFormat[index] == FORMAT_X8B8G8R8 && rgbaWriteMask != 0x00000007) &&
2946 (state.targetFormat[index] == FORMAT_X8B8G8R8 && rgbaWriteMask != 0x0000000F))) // FIXME: Need for masking when XBGR && Fh?
2947 {
2948 Short4 masked = value;
2949 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[rgbaWriteMask][0]));
2950 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[rgbaWriteMask][0]));
2951 c01 |= masked;
2952 }
2953
2954 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
2955 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
2956 c01 |= value;
2957 *Pointer<Short4>(buffer) = c01;
2958
2959 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2960 value = *Pointer<Short4>(buffer);
2961
2962 if((state.targetFormat[index] == FORMAT_A8B8G8R8 && rgbaWriteMask != 0x0000000F) ||
2963 ((state.targetFormat[index] == FORMAT_X8B8G8R8 && rgbaWriteMask != 0x00000007) &&
2964 (state.targetFormat[index] == FORMAT_X8B8G8R8 && rgbaWriteMask != 0x0000000F))) // FIXME: Need for masking when XBGR && Fh?
2965 {
2966 Short4 masked = value;
2967 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[rgbaWriteMask][0]));
2968 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[rgbaWriteMask][0]));
2969 c23 |= masked;
2970 }
2971
2972 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
2973 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
2974 c23 |= value;
2975 *Pointer<Short4>(buffer) = c23;
2976 break;
John Bauman66b8ab22014-05-06 15:57:45 -04002977 case FORMAT_A8:
2978 if(rgbaWriteMask & 0x00000008)
2979 {
2980 buffer = cBuffer + 1 * x;
2981 Insert(value, *Pointer<Short>(buffer), 0);
2982 Int pitch = *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2983 Insert(value, *Pointer<Short>(buffer + pitch), 1);
2984 value = UnpackLow(As<Byte8>(value), As<Byte8>(value));
2985
2986 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q) + 8 * xMask);
2987 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * xMask);
2988 current.w |= value;
2989
2990 *Pointer<Short>(buffer) = Extract(current.w, 0);
2991 *Pointer<Short>(buffer + pitch) = Extract(current.w, 1);
2992 }
2993 break;
John Bauman89401822014-05-06 15:04:28 -04002994 case FORMAT_G16R16:
2995 buffer = cBuffer + 4 * x;
2996
2997 value = *Pointer<Short4>(buffer);
2998
2999 if((rgbaWriteMask & 0x00000003) != 0x00000003)
3000 {
3001 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003002 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman89401822014-05-06 15:04:28 -04003003 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04003004 current.x |= masked;
John Bauman89401822014-05-06 15:04:28 -04003005 }
3006
John Bauman19bac1e2014-05-06 15:23:49 -04003007 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04003008 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04003009 current.x |= value;
3010 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04003011
3012 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3013
3014 value = *Pointer<Short4>(buffer);
3015
3016 if((rgbaWriteMask & 0x00000003) != 0x00000003)
3017 {
3018 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003019 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman89401822014-05-06 15:04:28 -04003020 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04003021 current.y |= masked;
John Bauman89401822014-05-06 15:04:28 -04003022 }
3023
John Bauman19bac1e2014-05-06 15:23:49 -04003024 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04003025 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04003026 current.y |= value;
3027 *Pointer<Short4>(buffer) = current.y;
John Bauman89401822014-05-06 15:04:28 -04003028 break;
3029 case FORMAT_A16B16G16R16:
3030 buffer = cBuffer + 8 * x;
3031
3032 {
3033 value = *Pointer<Short4>(buffer);
3034
3035 if(rgbaWriteMask != 0x0000000F)
3036 {
3037 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003038 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04003039 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04003040 current.x |= masked;
John Bauman89401822014-05-06 15:04:28 -04003041 }
3042
John Bauman19bac1e2014-05-06 15:23:49 -04003043 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ0Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04003044 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ0Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04003045 current.x |= value;
3046 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04003047 }
3048
3049 {
3050 value = *Pointer<Short4>(buffer + 8);
3051
3052 if(rgbaWriteMask != 0x0000000F)
3053 {
3054 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003055 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04003056 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04003057 current.y |= masked;
John Bauman89401822014-05-06 15:04:28 -04003058 }
3059
John Bauman19bac1e2014-05-06 15:23:49 -04003060 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ1Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04003061 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ1Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04003062 current.y |= value;
3063 *Pointer<Short4>(buffer + 8) = current.y;
John Bauman89401822014-05-06 15:04:28 -04003064 }
3065
3066 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3067
3068 {
3069 value = *Pointer<Short4>(buffer);
3070
3071 if(rgbaWriteMask != 0x0000000F)
3072 {
3073 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003074 current.z &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04003075 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04003076 current.z |= masked;
John Bauman89401822014-05-06 15:04:28 -04003077 }
3078
John Bauman19bac1e2014-05-06 15:23:49 -04003079 current.z &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ2Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04003080 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ2Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04003081 current.z |= value;
3082 *Pointer<Short4>(buffer) = current.z;
John Bauman89401822014-05-06 15:04:28 -04003083 }
3084
3085 {
3086 value = *Pointer<Short4>(buffer + 8);
3087
3088 if(rgbaWriteMask != 0x0000000F)
3089 {
3090 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003091 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04003092 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04003093 current.w |= masked;
John Bauman89401822014-05-06 15:04:28 -04003094 }
3095
John Bauman19bac1e2014-05-06 15:23:49 -04003096 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ3Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04003097 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ3Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04003098 current.w |= value;
3099 *Pointer<Short4>(buffer + 8) = current.w;
John Bauman89401822014-05-06 15:04:28 -04003100 }
3101 break;
3102 default:
3103 ASSERT(false);
3104 }
3105 }
3106
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003107 void PixelRoutine::blendFactor(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -04003108 {
3109 switch(blendFactorActive)
3110 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003111 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04003112 // Optimized
3113 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003114 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04003115 // Optimized
3116 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003117 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003118 blendFactor.x = oC.x;
3119 blendFactor.y = oC.y;
3120 blendFactor.z = oC.z;
John Bauman89401822014-05-06 15:04:28 -04003121 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003122 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003123 blendFactor.x = Float4(1.0f) - oC.x;
3124 blendFactor.y = Float4(1.0f) - oC.y;
3125 blendFactor.z = Float4(1.0f) - oC.z;
John Bauman89401822014-05-06 15:04:28 -04003126 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003127 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003128 blendFactor.x = pixel.x;
3129 blendFactor.y = pixel.y;
3130 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003131 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003132 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003133 blendFactor.x = Float4(1.0f) - pixel.x;
3134 blendFactor.y = Float4(1.0f) - pixel.y;
3135 blendFactor.z = Float4(1.0f) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003136 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003137 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003138 blendFactor.x = oC.w;
3139 blendFactor.y = oC.w;
3140 blendFactor.z = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003141 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003142 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003143 blendFactor.x = Float4(1.0f) - oC.w;
3144 blendFactor.y = Float4(1.0f) - oC.w;
3145 blendFactor.z = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04003146 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003147 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003148 blendFactor.x = pixel.w;
3149 blendFactor.y = pixel.w;
3150 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003151 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003152 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003153 blendFactor.x = Float4(1.0f) - pixel.w;
3154 blendFactor.y = Float4(1.0f) - pixel.w;
3155 blendFactor.z = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003156 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003157 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04003158 blendFactor.x = Float4(1.0f) - pixel.w;
3159 blendFactor.x = Min(blendFactor.x, oC.w);
3160 blendFactor.y = blendFactor.x;
3161 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -04003162 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003163 case BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003164 blendFactor.x = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[0]));
3165 blendFactor.y = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[1]));
3166 blendFactor.z = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04003167 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003168 case BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003169 blendFactor.x = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[0]));
3170 blendFactor.y = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[1]));
3171 blendFactor.z = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04003172 break;
3173 default:
3174 ASSERT(false);
3175 }
3176 }
3177
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003178 void PixelRoutine::blendFactorAlpha(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -04003179 {
3180 switch(blendFactorAlphaActive)
3181 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003182 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04003183 // Optimized
3184 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003185 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04003186 // Optimized
3187 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003188 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003189 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003190 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003191 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003192 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04003193 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003194 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003195 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003196 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003197 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003198 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003199 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003200 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003201 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003202 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003203 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003204 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04003205 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003206 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003207 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003208 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003209 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003210 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003211 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003212 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04003213 blendFactor.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003214 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003215 case BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003216 blendFactor.w = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04003217 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003218 case BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003219 blendFactor.w = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04003220 break;
3221 default:
3222 ASSERT(false);
3223 }
3224 }
3225
John Bauman19bac1e2014-05-06 15:23:49 -04003226 void PixelRoutine::alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4f &oC, Int &x)
John Bauman89401822014-05-06 15:04:28 -04003227 {
3228 if(!state.alphaBlendActive)
3229 {
3230 return;
3231 }
3232
3233 Pointer<Byte> buffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003234 Vector4f pixel;
John Bauman89401822014-05-06 15:04:28 -04003235
John Bauman19bac1e2014-05-06 15:23:49 -04003236 Vector4i color;
John Bauman89401822014-05-06 15:04:28 -04003237 Short4 c01;
3238 Short4 c23;
3239
3240 // Read pixel
3241 switch(state.targetFormat[index])
3242 {
3243 case FORMAT_A8R8G8B8:
3244 buffer = cBuffer + 4 * x;
3245 c01 = *Pointer<Short4>(buffer);
3246 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3247 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04003248 color.z = c01;
3249 color.y = c01;
3250 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(c23));
3251 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(c23));
3252 color.x = color.z;
3253 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.y));
3254 color.x = UnpackHigh(As<Byte8>(color.x), As<Byte8>(color.y));
3255 color.y = color.z;
3256 color.w = color.x;
3257 color.x = UnpackLow(As<Byte8>(color.x), As<Byte8>(color.x));
3258 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(color.y));
3259 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.z));
3260 color.w = UnpackHigh(As<Byte8>(color.w), As<Byte8>(color.w));
John Bauman89401822014-05-06 15:04:28 -04003261
John Bauman19bac1e2014-05-06 15:23:49 -04003262 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3263 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3264 pixel.z = convertUnsigned16(As<UShort4>(color.z));
3265 pixel.w = convertUnsigned16(As<UShort4>(color.w));
John Bauman89401822014-05-06 15:04:28 -04003266 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04003267 case FORMAT_A8B8G8R8:
3268 buffer = cBuffer + 4 * x;
3269 c01 = *Pointer<Short4>(buffer);
3270 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3271 c23 = *Pointer<Short4>(buffer);
3272 color.z = c01;
3273 color.y = c01;
3274 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(c23));
3275 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(c23));
3276 color.x = color.z;
3277 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.y));
3278 color.x = UnpackHigh(As<Byte8>(color.x), As<Byte8>(color.y));
3279 color.y = color.z;
3280 color.w = color.x;
3281 color.x = UnpackLow(As<Byte8>(color.x), As<Byte8>(color.x));
3282 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(color.y));
3283 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.z));
3284 color.w = UnpackHigh(As<Byte8>(color.w), As<Byte8>(color.w));
3285
3286 pixel.x = convertUnsigned16(As<UShort4>(color.z));
3287 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3288 pixel.z = convertUnsigned16(As<UShort4>(color.x));
3289 pixel.w = convertUnsigned16(As<UShort4>(color.w));
3290 break;
John Bauman89401822014-05-06 15:04:28 -04003291 case FORMAT_X8R8G8B8:
3292 buffer = cBuffer + 4 * x;
3293 c01 = *Pointer<Short4>(buffer);
3294 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3295 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04003296 color.z = c01;
3297 color.y = c01;
3298 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(c23));
3299 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(c23));
3300 color.x = color.z;
3301 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.y));
3302 color.x = UnpackHigh(As<Byte8>(color.x), As<Byte8>(color.y));
3303 color.y = color.z;
3304 color.x = UnpackLow(As<Byte8>(color.x), As<Byte8>(color.x));
3305 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(color.y));
3306 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.z));
John Bauman89401822014-05-06 15:04:28 -04003307
John Bauman19bac1e2014-05-06 15:23:49 -04003308 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3309 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3310 pixel.z = convertUnsigned16(As<UShort4>(color.z));
3311 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003312 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04003313 case FORMAT_X8B8G8R8:
3314 buffer = cBuffer + 4 * x;
3315 c01 = *Pointer<Short4>(buffer);
3316 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3317 c23 = *Pointer<Short4>(buffer);
3318 color.z = c01;
3319 color.y = c01;
3320 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(c23));
3321 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(c23));
3322 color.x = color.z;
3323 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.y));
3324 color.x = UnpackHigh(As<Byte8>(color.x), As<Byte8>(color.y));
3325 color.y = color.z;
3326 color.x = UnpackLow(As<Byte8>(color.x), As<Byte8>(color.x));
3327 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(color.y));
3328 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.z));
3329
3330 pixel.x = convertUnsigned16(As<UShort4>(color.z));
3331 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3332 pixel.z = convertUnsigned16(As<UShort4>(color.x));
3333 pixel.w = Float4(1.0f);
3334 break;
John Bauman66b8ab22014-05-06 15:57:45 -04003335 case FORMAT_A8:
3336 buffer = cBuffer + 1 * x;
3337 c01 = Insert(c01, *Pointer<Short>(buffer), 0);
3338 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3339 c01 = Insert(c01, *Pointer<Short>(buffer), 1);
3340 pixel.w = convertUnsigned16(As<UShort4>(UnpackLow(As<Byte8>(c01), As<Byte8>(c01))));
3341 pixel.x = Float4(0.0f);
3342 pixel.y = Float4(0.0f);
3343 pixel.z = Float4(0.0f);
3344 break;
John Bauman89401822014-05-06 15:04:28 -04003345 case FORMAT_A8G8R8B8Q:
John Bauman66b8ab22014-05-06 15:57:45 -04003346 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04003347 // UnpackLow(pixel.z, qword_ptr [cBuffer+8*x+0]);
3348 // UnpackHigh(pixel.x, qword_ptr [cBuffer+8*x+0]);
3349 // UnpackLow(pixel.y, qword_ptr [cBuffer+8*x+8]);
3350 // UnpackHigh(pixel.w, qword_ptr [cBuffer+8*x+8]);
John Bauman89401822014-05-06 15:04:28 -04003351 break;
3352 case FORMAT_X8G8R8B8Q:
John Bauman66b8ab22014-05-06 15:57:45 -04003353 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04003354 // UnpackLow(pixel.z, qword_ptr [cBuffer+8*x+0]);
3355 // UnpackHigh(pixel.x, qword_ptr [cBuffer+8*x+0]);
3356 // UnpackLow(pixel.y, qword_ptr [cBuffer+8*x+8]);
3357 // pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04003358 break;
3359 case FORMAT_A16B16G16R16:
3360 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003361 color.x = *Pointer<Short4>(buffer + 8 * x);
3362 color.y = *Pointer<Short4>(buffer + 8 * x + 8);
John Bauman89401822014-05-06 15:04:28 -04003363 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003364 color.z = *Pointer<Short4>(buffer + 8 * x);
3365 color.w = *Pointer<Short4>(buffer + 8 * x + 8);
John Bauman89401822014-05-06 15:04:28 -04003366
John Bauman19bac1e2014-05-06 15:23:49 -04003367 transpose4x4(color.x, color.y, color.z, color.w);
John Bauman89401822014-05-06 15:04:28 -04003368
John Bauman19bac1e2014-05-06 15:23:49 -04003369 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3370 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3371 pixel.z = convertUnsigned16(As<UShort4>(color.z));
3372 pixel.w = convertUnsigned16(As<UShort4>(color.w));
John Bauman89401822014-05-06 15:04:28 -04003373 break;
3374 case FORMAT_G16R16:
3375 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003376 color.x = *Pointer<Short4>(buffer + 4 * x);
John Bauman89401822014-05-06 15:04:28 -04003377 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003378 color.y = *Pointer<Short4>(buffer + 4 * x);
3379 color.z = color.x;
3380 color.x = As<Short4>(UnpackLow(color.x, color.y));
3381 color.z = As<Short4>(UnpackHigh(color.z, color.y));
3382 color.y = color.z;
3383 color.x = As<Short4>(UnpackLow(color.x, color.z));
3384 color.y = As<Short4>(UnpackHigh(color.y, color.z));
John Bauman89401822014-05-06 15:04:28 -04003385
John Bauman19bac1e2014-05-06 15:23:49 -04003386 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3387 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3388 pixel.z = Float4(1.0f);
3389 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003390 break;
3391 case FORMAT_R32F:
3392 buffer = cBuffer;
3393 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04003394 pixel.x.x = *Pointer<Float>(buffer + 4 * x + 0);
3395 pixel.x.y = *Pointer<Float>(buffer + 4 * x + 4);
John Bauman89401822014-05-06 15:04:28 -04003396 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3397 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04003398 pixel.x.z = *Pointer<Float>(buffer + 4 * x + 0);
3399 pixel.x.w = *Pointer<Float>(buffer + 4 * x + 4);
3400 pixel.y = Float4(1.0f);
3401 pixel.z = Float4(1.0f);
3402 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003403 break;
3404 case FORMAT_G32R32F:
3405 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003406 pixel.x = *Pointer<Float4>(buffer + 8 * x, 16);
John Bauman89401822014-05-06 15:04:28 -04003407 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003408 pixel.y = *Pointer<Float4>(buffer + 8 * x, 16);
3409 pixel.z = pixel.x;
3410 pixel.x = ShuffleLowHigh(pixel.x, pixel.y, 0x88);
3411 pixel.z = ShuffleLowHigh(pixel.z, pixel.y, 0xDD);
3412 pixel.y = pixel.z;
3413 pixel.z = Float4(1.0f);
3414 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003415 break;
3416 case FORMAT_A32B32G32R32F:
3417 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003418 pixel.x = *Pointer<Float4>(buffer + 16 * x, 16);
3419 pixel.y = *Pointer<Float4>(buffer + 16 * x + 16, 16);
John Bauman89401822014-05-06 15:04:28 -04003420 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003421 pixel.z = *Pointer<Float4>(buffer + 16 * x, 16);
3422 pixel.w = *Pointer<Float4>(buffer + 16 * x + 16, 16);
3423 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04003424 break;
3425 default:
3426 ASSERT(false);
3427 }
3428
3429 if(postBlendSRGB && state.writeSRGB)
3430 {
John Bauman19bac1e2014-05-06 15:23:49 -04003431 sRGBtoLinear(pixel.x);
3432 sRGBtoLinear(pixel.y);
3433 sRGBtoLinear(pixel.z);
John Bauman89401822014-05-06 15:04:28 -04003434 }
3435
3436 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
John Bauman19bac1e2014-05-06 15:23:49 -04003437 Vector4f sourceFactor;
3438 Vector4f destFactor;
John Bauman89401822014-05-06 15:04:28 -04003439
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003440 blendFactor(r, sourceFactor, oC, pixel, state.sourceBlendFactor);
3441 blendFactor(r, destFactor, oC, pixel, state.destBlendFactor);
John Bauman89401822014-05-06 15:04:28 -04003442
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003443 if(state.sourceBlendFactor != BLEND_ONE && state.sourceBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04003444 {
John Bauman19bac1e2014-05-06 15:23:49 -04003445 oC.x *= sourceFactor.x;
3446 oC.y *= sourceFactor.y;
3447 oC.z *= sourceFactor.z;
John Bauman89401822014-05-06 15:04:28 -04003448 }
3449
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003450 if(state.destBlendFactor != BLEND_ONE && state.destBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04003451 {
John Bauman19bac1e2014-05-06 15:23:49 -04003452 pixel.x *= destFactor.x;
3453 pixel.y *= destFactor.y;
3454 pixel.z *= destFactor.z;
John Bauman89401822014-05-06 15:04:28 -04003455 }
3456
3457 switch(state.blendOperation)
3458 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003459 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04003460 oC.x += pixel.x;
3461 oC.y += pixel.y;
3462 oC.z += pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003463 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003464 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003465 oC.x -= pixel.x;
3466 oC.y -= pixel.y;
3467 oC.z -= pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003468 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003469 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003470 oC.x = pixel.x - oC.x;
3471 oC.y = pixel.y - oC.y;
3472 oC.z = pixel.z - oC.z;
John Bauman89401822014-05-06 15:04:28 -04003473 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003474 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04003475 oC.x = Min(oC.x, pixel.x);
3476 oC.y = Min(oC.y, pixel.y);
3477 oC.z = Min(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04003478 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003479 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04003480 oC.x = Max(oC.x, pixel.x);
3481 oC.y = Max(oC.y, pixel.y);
3482 oC.z = Max(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04003483 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003484 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04003485 // No operation
3486 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003487 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003488 oC.x = pixel.x;
3489 oC.y = pixel.y;
3490 oC.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003491 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003492 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04003493 oC.x = Float4(0.0f);
3494 oC.y = Float4(0.0f);
3495 oC.z = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04003496 break;
3497 default:
3498 ASSERT(false);
3499 }
3500
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003501 blendFactorAlpha(r, sourceFactor, oC, pixel, state.sourceBlendFactorAlpha);
3502 blendFactorAlpha(r, destFactor, oC, pixel, state.destBlendFactorAlpha);
John Bauman89401822014-05-06 15:04:28 -04003503
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003504 if(state.sourceBlendFactorAlpha != BLEND_ONE && state.sourceBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04003505 {
John Bauman19bac1e2014-05-06 15:23:49 -04003506 oC.w *= sourceFactor.w;
John Bauman89401822014-05-06 15:04:28 -04003507 }
3508
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003509 if(state.destBlendFactorAlpha != BLEND_ONE && state.destBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04003510 {
John Bauman19bac1e2014-05-06 15:23:49 -04003511 pixel.w *= destFactor.w;
John Bauman89401822014-05-06 15:04:28 -04003512 }
3513
3514 switch(state.blendOperationAlpha)
3515 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003516 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04003517 oC.w += pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003518 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003519 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003520 oC.w -= pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003521 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003522 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003523 pixel.w -= oC.w;
3524 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003525 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003526 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04003527 oC.w = Min(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04003528 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003529 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04003530 oC.w = Max(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04003531 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003532 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04003533 // No operation
3534 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003535 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003536 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003537 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003538 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04003539 oC.w = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04003540 break;
3541 default:
3542 ASSERT(false);
3543 }
3544 }
3545
John Bauman19bac1e2014-05-06 15:23:49 -04003546 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 -04003547 {
3548 if(!state.colorWriteActive(index))
3549 {
3550 return;
3551 }
3552
John Bauman19bac1e2014-05-06 15:23:49 -04003553 Vector4i color;
John Bauman89401822014-05-06 15:04:28 -04003554
3555 switch(state.targetFormat[index])
3556 {
3557 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04003558 case FORMAT_X8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04003559 case FORMAT_A8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04003560 case FORMAT_A8B8G8R8:
John Bauman66b8ab22014-05-06 15:57:45 -04003561 case FORMAT_A8:
John Bauman89401822014-05-06 15:04:28 -04003562 case FORMAT_G16R16:
3563 case FORMAT_A16B16G16R16:
3564 convertFixed16(color, oC, true);
3565 writeColor(r, index, cBuffer, x, color, sMask, zMask, cMask);
3566 return;
3567 case FORMAT_R32F:
3568 break;
3569 case FORMAT_G32R32F:
John Bauman19bac1e2014-05-06 15:23:49 -04003570 oC.z = oC.x;
3571 oC.x = UnpackLow(oC.x, oC.y);
3572 oC.z = UnpackHigh(oC.z, oC.y);
3573 oC.y = oC.z;
John Bauman89401822014-05-06 15:04:28 -04003574 break;
3575 case FORMAT_A32B32G32R32F:
John Bauman19bac1e2014-05-06 15:23:49 -04003576 transpose4x4(oC.x, oC.y, oC.z, oC.w);
John Bauman89401822014-05-06 15:04:28 -04003577 break;
3578 default:
3579 ASSERT(false);
3580 }
3581
3582 int rgbaWriteMask = state.colorWriteActive(index);
3583
3584 Int xMask; // Combination of all masks
3585
3586 if(state.depthTestActive)
3587 {
3588 xMask = zMask;
3589 }
3590 else
3591 {
3592 xMask = cMask;
3593 }
3594
3595 if(state.stencilActive)
3596 {
3597 xMask &= sMask;
3598 }
3599
3600 Pointer<Byte> buffer;
3601 Float4 value;
3602
3603 switch(state.targetFormat[index])
3604 {
3605 case FORMAT_R32F:
3606 if(rgbaWriteMask & 0x00000001)
3607 {
3608 buffer = cBuffer + 4 * x;
3609
3610 // FIXME: movlps
3611 value.x = *Pointer<Float>(buffer + 0);
3612 value.y = *Pointer<Float>(buffer + 4);
3613
3614 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3615
3616 // FIXME: movhps
3617 value.z = *Pointer<Float>(buffer + 0);
3618 value.w = *Pointer<Float>(buffer + 4);
3619
John Bauman19bac1e2014-05-06 15:23:49 -04003620 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 -04003621 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003622 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
John Bauman89401822014-05-06 15:04:28 -04003623
3624 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04003625 *Pointer<Float>(buffer + 0) = oC.x.z;
3626 *Pointer<Float>(buffer + 4) = oC.x.w;
John Bauman89401822014-05-06 15:04:28 -04003627
3628 buffer -= *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3629
3630 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04003631 *Pointer<Float>(buffer + 0) = oC.x.x;
3632 *Pointer<Float>(buffer + 4) = oC.x.y;
John Bauman89401822014-05-06 15:04:28 -04003633 }
3634 break;
3635 case FORMAT_G32R32F:
3636 buffer = cBuffer + 8 * x;
3637
3638 value = *Pointer<Float4>(buffer);
3639
3640 if((rgbaWriteMask & 0x00000003) != 0x00000003)
3641 {
3642 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003643 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 -04003644 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD01X[rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003645 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003646 }
3647
John Bauman19bac1e2014-05-06 15:23:49 -04003648 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 -04003649 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskQ01X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003650 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
3651 *Pointer<Float4>(buffer) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04003652
3653 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3654
3655 value = *Pointer<Float4>(buffer);
3656
3657 if((rgbaWriteMask & 0x00000003) != 0x00000003)
3658 {
3659 Float4 masked;
3660
3661 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003662 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 -04003663 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD01X[rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003664 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003665 }
3666
John Bauman19bac1e2014-05-06 15:23:49 -04003667 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 -04003668 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskQ23X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003669 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
3670 *Pointer<Float4>(buffer) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04003671 break;
3672 case FORMAT_A32B32G32R32F:
3673 buffer = cBuffer + 16 * x;
3674
3675 {
3676 value = *Pointer<Float4>(buffer, 16);
3677
3678 if(rgbaWriteMask != 0x0000000F)
3679 {
3680 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003681 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003682 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003683 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003684 }
3685
John Bauman19bac1e2014-05-06 15:23:49 -04003686 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 -04003687 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX0X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003688 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
3689 *Pointer<Float4>(buffer, 16) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04003690 }
3691
3692 {
3693 value = *Pointer<Float4>(buffer + 16, 16);
3694
3695 if(rgbaWriteMask != 0x0000000F)
3696 {
3697 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003698 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003699 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003700 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003701 }
3702
John Bauman19bac1e2014-05-06 15:23:49 -04003703 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 -04003704 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX1X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003705 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
3706 *Pointer<Float4>(buffer + 16, 16) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04003707 }
3708
3709 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3710
3711 {
3712 value = *Pointer<Float4>(buffer, 16);
3713
3714 if(rgbaWriteMask != 0x0000000F)
3715 {
3716 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003717 oC.z = As<Float4>(As<Int4>(oC.z) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003718 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003719 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003720 }
3721
John Bauman19bac1e2014-05-06 15:23:49 -04003722 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 -04003723 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX2X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003724 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(value));
3725 *Pointer<Float4>(buffer, 16) = oC.z;
John Bauman89401822014-05-06 15:04:28 -04003726 }
3727
3728 {
3729 value = *Pointer<Float4>(buffer + 16, 16);
3730
3731 if(rgbaWriteMask != 0x0000000F)
3732 {
3733 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003734 oC.w = As<Float4>(As<Int4>(oC.w) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003735 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003736 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003737 }
3738
John Bauman19bac1e2014-05-06 15:23:49 -04003739 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 -04003740 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX3X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003741 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(value));
3742 *Pointer<Float4>(buffer + 16, 16) = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003743 }
3744 break;
3745 default:
3746 ASSERT(false);
3747 }
3748 }
3749
3750 void PixelRoutine::ps_1_x(Registers &r, Int cMask[4])
3751 {
3752 int pad = 0; // Count number of texm3x3pad instructions
John Bauman19bac1e2014-05-06 15:23:49 -04003753 Vector4i dPairing; // Destination for first pairing instruction
John Bauman89401822014-05-06 15:04:28 -04003754
Alexis Hetu903e0252014-11-25 14:25:32 -05003755 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -04003756 {
John Bauman19bac1e2014-05-06 15:23:49 -04003757 const Shader::Instruction *instruction = shader->getInstruction(i);
3758 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -04003759
3760 // #ifndef NDEBUG // FIXME: Centralize debug output control
John Bauman19bac1e2014-05-06 15:23:49 -04003761 // shader->printInstruction(i, "debug.txt");
John Bauman89401822014-05-06 15:04:28 -04003762 // #endif
3763
John Bauman19bac1e2014-05-06 15:23:49 -04003764 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -04003765 {
3766 continue;
3767 }
3768
John Bauman19bac1e2014-05-06 15:23:49 -04003769 const Dst &dst = instruction->dst;
3770 const Src &src0 = instruction->src[0];
3771 const Src &src1 = instruction->src[1];
3772 const Src &src2 = instruction->src[2];
John Bauman89401822014-05-06 15:04:28 -04003773
John Bauman19bac1e2014-05-06 15:23:49 -04003774 unsigned short version = shader->getVersion();
3775 bool pairing = i + 1 < shader->getLength() && shader->getInstruction(i + 1)->coissue; // First instruction of pair
3776 bool coissue = instruction->coissue; // Second instruction of pair
John Bauman89401822014-05-06 15:04:28 -04003777
John Bauman19bac1e2014-05-06 15:23:49 -04003778 Vector4i d;
3779 Vector4i s0;
3780 Vector4i s1;
3781 Vector4i s2;
John Bauman89401822014-05-06 15:04:28 -04003782
John Bauman19bac1e2014-05-06 15:23:49 -04003783 if(src0.type != Shader::PARAMETER_VOID) s0 = regi(r, src0);
3784 if(src1.type != Shader::PARAMETER_VOID) s1 = regi(r, src1);
3785 if(src2.type != Shader::PARAMETER_VOID) s2 = regi(r, src2);
3786
3787 Float4 u = version < 0x0104 ? r.vf[2 + dst.index].x : r.vf[2 + src0.index].x;
3788 Float4 v = version < 0x0104 ? r.vf[2 + dst.index].y : r.vf[2 + src0.index].y;
3789 Float4 s = version < 0x0104 ? r.vf[2 + dst.index].z : r.vf[2 + src0.index].z;
3790 Float4 t = version < 0x0104 ? r.vf[2 + dst.index].w : r.vf[2 + src0.index].w;
John Bauman89401822014-05-06 15:04:28 -04003791
3792 switch(opcode)
3793 {
John Bauman19bac1e2014-05-06 15:23:49 -04003794 case Shader::OPCODE_PS_1_0: break;
3795 case Shader::OPCODE_PS_1_1: break;
3796 case Shader::OPCODE_PS_1_2: break;
3797 case Shader::OPCODE_PS_1_3: break;
3798 case Shader::OPCODE_PS_1_4: break;
John Bauman89401822014-05-06 15:04:28 -04003799
John Bauman19bac1e2014-05-06 15:23:49 -04003800 case Shader::OPCODE_DEF: break;
John Bauman89401822014-05-06 15:04:28 -04003801
John Bauman19bac1e2014-05-06 15:23:49 -04003802 case Shader::OPCODE_NOP: break;
3803 case Shader::OPCODE_MOV: MOV(d, s0); break;
3804 case Shader::OPCODE_ADD: ADD(d, s0, s1); break;
3805 case Shader::OPCODE_SUB: SUB(d, s0, s1); break;
3806 case Shader::OPCODE_MAD: MAD(d, s0, s1, s2); break;
3807 case Shader::OPCODE_MUL: MUL(d, s0, s1); break;
3808 case Shader::OPCODE_DP3: DP3(d, s0, s1); break;
3809 case Shader::OPCODE_DP4: DP4(d, s0, s1); break;
3810 case Shader::OPCODE_LRP: LRP(d, s0, s1, s2); break;
3811 case Shader::OPCODE_TEXCOORD:
3812 if(version < 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003813 {
John Bauman19bac1e2014-05-06 15:23:49 -04003814 TEXCOORD(d, u, v, s, dst.index);
John Bauman89401822014-05-06 15:04:28 -04003815 }
3816 else
3817 {
3818 if((src0.swizzle & 0x30) == 0x20) // .xyz
3819 {
John Bauman19bac1e2014-05-06 15:23:49 -04003820 TEXCRD(d, u, v, s, src0.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003821 }
3822 else // .xyw
3823 {
John Bauman19bac1e2014-05-06 15:23:49 -04003824 TEXCRD(d, u, v, t, src0.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003825 }
3826 }
3827 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003828 case Shader::OPCODE_TEXKILL:
3829 if(version < 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003830 {
John Bauman19bac1e2014-05-06 15:23:49 -04003831 TEXKILL(cMask, u, v, s);
John Bauman89401822014-05-06 15:04:28 -04003832 }
John Bauman19bac1e2014-05-06 15:23:49 -04003833 else if(version == 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003834 {
John Bauman19bac1e2014-05-06 15:23:49 -04003835 if(dst.type == Shader::PARAMETER_TEXTURE)
John Bauman89401822014-05-06 15:04:28 -04003836 {
John Bauman19bac1e2014-05-06 15:23:49 -04003837 TEXKILL(cMask, u, v, s);
John Bauman89401822014-05-06 15:04:28 -04003838 }
3839 else
3840 {
3841 TEXKILL(cMask, r.ri[dst.index]);
3842 }
3843 }
3844 else ASSERT(false);
3845 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003846 case Shader::OPCODE_TEX:
3847 if(version < 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003848 {
John Bauman19bac1e2014-05-06 15:23:49 -04003849 TEX(r, d, u, v, s, dst.index, false);
John Bauman89401822014-05-06 15:04:28 -04003850 }
John Bauman19bac1e2014-05-06 15:23:49 -04003851 else if(version == 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003852 {
John Bauman19bac1e2014-05-06 15:23:49 -04003853 if(src0.type == Shader::PARAMETER_TEXTURE)
John Bauman89401822014-05-06 15:04:28 -04003854 {
3855 if((src0.swizzle & 0x30) == 0x20) // .xyz
3856 {
John Bauman19bac1e2014-05-06 15:23:49 -04003857 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 -04003858 }
3859 else // .xyw
3860 {
John Bauman19bac1e2014-05-06 15:23:49 -04003861 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 -04003862 }
3863 }
3864 else
3865 {
John Bauman19bac1e2014-05-06 15:23:49 -04003866 TEXLD(r, d, s0, dst.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003867 }
3868 }
3869 else ASSERT(false);
3870 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003871 case Shader::OPCODE_TEXBEM: TEXBEM(r, d, s0, u, v, s, dst.index); break;
3872 case Shader::OPCODE_TEXBEML: TEXBEML(r, d, s0, u, v, s, dst.index); break;
3873 case Shader::OPCODE_TEXREG2AR: TEXREG2AR(r, d, s0, dst.index); break;
3874 case Shader::OPCODE_TEXREG2GB: TEXREG2GB(r, d, s0, dst.index); break;
3875 case Shader::OPCODE_TEXM3X2PAD: TEXM3X2PAD(r, u, v, s, s0, 0, src0.modifier == Shader::MODIFIER_SIGN); break;
3876 case Shader::OPCODE_TEXM3X2TEX: TEXM3X2TEX(r, d, u, v, s, dst.index, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3877 case Shader::OPCODE_TEXM3X3PAD: TEXM3X3PAD(r, u, v, s, s0, pad++ % 2, src0.modifier == Shader::MODIFIER_SIGN); break;
3878 case Shader::OPCODE_TEXM3X3TEX: TEXM3X3TEX(r, d, u, v, s, dst.index, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3879 case Shader::OPCODE_TEXM3X3SPEC: TEXM3X3SPEC(r, d, u, v, s, dst.index, s0, s1); break;
3880 case Shader::OPCODE_TEXM3X3VSPEC: TEXM3X3VSPEC(r, d, u, v, s, dst.index, s0); break;
3881 case Shader::OPCODE_CND: CND(d, s0, s1, s2); break;
3882 case Shader::OPCODE_TEXREG2RGB: TEXREG2RGB(r, d, s0, dst.index); break;
3883 case Shader::OPCODE_TEXDP3TEX: TEXDP3TEX(r, d, u, v, s, dst.index, s0); break;
3884 case Shader::OPCODE_TEXM3X2DEPTH: TEXM3X2DEPTH(r, d, u, v, s, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3885 case Shader::OPCODE_TEXDP3: TEXDP3(r, d, u, v, s, s0); break;
3886 case Shader::OPCODE_TEXM3X3: TEXM3X3(r, d, u, v, s, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3887 case Shader::OPCODE_TEXDEPTH: TEXDEPTH(r); break;
3888 case Shader::OPCODE_CMP0: CMP(d, s0, s1, s2); break;
3889 case Shader::OPCODE_BEM: BEM(r, d, s0, s1, dst.index); break;
3890 case Shader::OPCODE_PHASE: break;
3891 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -04003892 default:
3893 ASSERT(false);
3894 }
3895
John Bauman19bac1e2014-05-06 15:23:49 -04003896 if(dst.type != Shader::PARAMETER_VOID && opcode != Shader::OPCODE_TEXKILL)
John Bauman89401822014-05-06 15:04:28 -04003897 {
3898 if(dst.shift > 0)
3899 {
John Bauman19bac1e2014-05-06 15:23:49 -04003900 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);}
3901 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);}
3902 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);}
3903 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 -04003904 }
3905 else if(dst.shift < 0)
3906 {
John Bauman19bac1e2014-05-06 15:23:49 -04003907 if(dst.mask & 0x1) d.x = d.x >> -dst.shift;
3908 if(dst.mask & 0x2) d.y = d.y >> -dst.shift;
3909 if(dst.mask & 0x4) d.z = d.z >> -dst.shift;
3910 if(dst.mask & 0x8) d.w = d.w >> -dst.shift;
John Bauman89401822014-05-06 15:04:28 -04003911 }
3912
3913 if(dst.saturate)
3914 {
John Bauman19bac1e2014-05-06 15:23:49 -04003915 if(dst.mask & 0x1) {d.x = Min(d.x, Short4(0x1000)); d.x = Max(d.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
3916 if(dst.mask & 0x2) {d.y = Min(d.y, Short4(0x1000)); d.y = Max(d.y, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
3917 if(dst.mask & 0x4) {d.z = Min(d.z, Short4(0x1000)); d.z = Max(d.z, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
3918 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 -04003919 }
3920
3921 if(pairing)
3922 {
John Bauman19bac1e2014-05-06 15:23:49 -04003923 if(dst.mask & 0x1) dPairing.x = d.x;
3924 if(dst.mask & 0x2) dPairing.y = d.y;
3925 if(dst.mask & 0x4) dPairing.z = d.z;
3926 if(dst.mask & 0x8) dPairing.w = d.w;
John Bauman89401822014-05-06 15:04:28 -04003927 }
3928
3929 if(coissue)
3930 {
John Bauman19bac1e2014-05-06 15:23:49 -04003931 const Dst &dst = shader->getInstruction(i - 1)->dst;
John Bauman89401822014-05-06 15:04:28 -04003932
3933 writeDestination(r, dPairing, dst);
3934 }
3935
3936 if(!pairing)
3937 {
3938 writeDestination(r, d, dst);
3939 }
3940 }
3941 }
3942 }
3943
3944 void PixelRoutine::ps_2_x(Registers &r, Int cMask[4])
3945 {
3946 r.enableIndex = 0;
3947 r.stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -04003948
Nicolas Capens4677a5f2014-05-06 16:42:26 -04003949 if(shader->containsLeaveInstruction())
3950 {
3951 r.enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3952 }
3953
John Bauman19bac1e2014-05-06 15:23:49 -04003954 bool out[4][4] = {false};
3955
3956 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -05003957 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -04003958 {
John Bauman19bac1e2014-05-06 15:23:49 -04003959 const Shader::Instruction *instruction = shader->getInstruction(i);
3960 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -04003961
John Bauman19bac1e2014-05-06 15:23:49 -04003962 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
3963 {
3964 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -04003965
John Bauman19bac1e2014-05-06 15:23:49 -04003966 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
3967 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
3968 }
3969 }
3970
Alexis Hetu903e0252014-11-25 14:25:32 -05003971 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -04003972 {
3973 const Shader::Instruction *instruction = shader->getInstruction(i);
3974 Shader::Opcode opcode = instruction->opcode;
3975
3976 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -04003977 {
3978 continue;
3979 }
3980
John Bauman19bac1e2014-05-06 15:23:49 -04003981 const Dst &dst = instruction->dst;
3982 const Src &src0 = instruction->src[0];
3983 const Src &src1 = instruction->src[1];
3984 const Src &src2 = instruction->src[2];
3985 const Src &src3 = instruction->src[3];
John Bauman89401822014-05-06 15:04:28 -04003986
John Bauman19bac1e2014-05-06 15:23:49 -04003987 bool predicate = instruction->predicate;
3988 Control control = instruction->control;
John Bauman89401822014-05-06 15:04:28 -04003989 bool pp = dst.partialPrecision;
John Bauman19bac1e2014-05-06 15:23:49 -04003990 bool project = instruction->project;
3991 bool bias = instruction->bias;
John Bauman89401822014-05-06 15:04:28 -04003992
John Bauman19bac1e2014-05-06 15:23:49 -04003993 Vector4f d;
3994 Vector4f s0;
3995 Vector4f s1;
3996 Vector4f s2;
3997 Vector4f s3;
John Bauman89401822014-05-06 15:04:28 -04003998
John Bauman19bac1e2014-05-06 15:23:49 -04003999 if(opcode == Shader::OPCODE_TEXKILL) // Takes destination as input
John Bauman89401822014-05-06 15:04:28 -04004000 {
John Bauman19bac1e2014-05-06 15:23:49 -04004001 if(dst.type == Shader::PARAMETER_TEXTURE)
John Bauman89401822014-05-06 15:04:28 -04004002 {
John Bauman19bac1e2014-05-06 15:23:49 -04004003 d.x = r.vf[2 + dst.index].x;
4004 d.y = r.vf[2 + dst.index].y;
4005 d.z = r.vf[2 + dst.index].z;
4006 d.w = r.vf[2 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -04004007 }
4008 else
4009 {
4010 d = r.rf[dst.index];
4011 }
4012 }
4013
John Bauman19bac1e2014-05-06 15:23:49 -04004014 if(src0.type != Shader::PARAMETER_VOID) s0 = reg(r, src0);
4015 if(src1.type != Shader::PARAMETER_VOID) s1 = reg(r, src1);
4016 if(src2.type != Shader::PARAMETER_VOID) s2 = reg(r, src2);
4017 if(src3.type != Shader::PARAMETER_VOID) s3 = reg(r, src3);
John Bauman89401822014-05-06 15:04:28 -04004018
4019 switch(opcode)
4020 {
John Bauman19bac1e2014-05-06 15:23:49 -04004021 case Shader::OPCODE_PS_2_0: break;
4022 case Shader::OPCODE_PS_2_x: break;
4023 case Shader::OPCODE_PS_3_0: break;
4024 case Shader::OPCODE_DEF: break;
4025 case Shader::OPCODE_DCL: break;
4026 case Shader::OPCODE_NOP: break;
4027 case Shader::OPCODE_MOV: mov(d, s0); break;
4028 case Shader::OPCODE_F2B: f2b(d, s0); break;
4029 case Shader::OPCODE_B2F: b2f(d, s0); break;
4030 case Shader::OPCODE_ADD: add(d, s0, s1); break;
4031 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
4032 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
4033 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
4034 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
4035 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
4036 case Shader::OPCODE_DP2ADD: dp2add(d, s0, s1, s2); break;
4037 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
4038 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
4039 case Shader::OPCODE_CMP0: cmp0(d, s0, s1, s2); break;
4040 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
4041 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
4042 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
4043 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
4044 case Shader::OPCODE_FRC: frc(d, s0); break;
4045 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
4046 case Shader::OPCODE_FLOOR: floor(d, s0); break;
4047 case Shader::OPCODE_CEIL: ceil(d, s0); break;
4048 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
4049 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
4050 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
4051 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
4052 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
4053 case Shader::OPCODE_LOG: log(d, s0, pp); break;
4054 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
4055 case Shader::OPCODE_DIV: div(d, s0, s1); break;
4056 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
4057 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
4058 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
4059 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
4060 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
4061 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
4062 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
4063 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
4064 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
4065 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
4066 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
4067 case Shader::OPCODE_MIN: min(d, s0, s1); break;
4068 case Shader::OPCODE_MAX: max(d, s0, s1); break;
4069 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
4070 case Shader::OPCODE_STEP: step(d, s0, s1); break;
4071 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
4072 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
4073 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
4074 case Shader::OPCODE_SGN: sgn(d, s0); break;
4075 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
4076 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
4077 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
4078 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
4079 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
4080 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
4081 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
4082 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
4083 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
4084 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
4085 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
4086 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
4087 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
4088 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
4089 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
4090 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
4091 case Shader::OPCODE_ABS: abs(d, s0); break;
4092 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
4093 case Shader::OPCODE_COS: cos(d, s0, pp); break;
4094 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
4095 case Shader::OPCODE_TAN: tan(d, s0, pp); break;
4096 case Shader::OPCODE_ACOS: acos(d, s0, pp); break;
4097 case Shader::OPCODE_ASIN: asin(d, s0, pp); break;
4098 case Shader::OPCODE_ATAN: atan(d, s0, pp); break;
4099 case Shader::OPCODE_ATAN2: atan2(d, s0, s1, pp); break;
4100 case Shader::OPCODE_M4X4: M4X4(r, d, s0, src1); break;
4101 case Shader::OPCODE_M4X3: M4X3(r, d, s0, src1); break;
4102 case Shader::OPCODE_M3X4: M3X4(r, d, s0, src1); break;
4103 case Shader::OPCODE_M3X3: M3X3(r, d, s0, src1); break;
4104 case Shader::OPCODE_M3X2: M3X2(r, d, s0, src1); break;
4105 case Shader::OPCODE_TEX: TEXLD(r, d, s0, src1, project, bias); break;
4106 case Shader::OPCODE_TEXLDD: TEXLDD(r, d, s0, src1, s2, s3, project, bias); break;
4107 case Shader::OPCODE_TEXLDL: TEXLDL(r, d, s0, src1, project, bias); break;
4108 case Shader::OPCODE_TEXKILL: TEXKILL(cMask, d, dst.mask); break;
4109 case Shader::OPCODE_DISCARD: DISCARD(r, cMask, instruction); break;
4110 case Shader::OPCODE_DFDX: DFDX(d, s0); break;
4111 case Shader::OPCODE_DFDY: DFDY(d, s0); break;
4112 case Shader::OPCODE_FWIDTH: FWIDTH(d, s0); break;
4113 case Shader::OPCODE_BREAK: BREAK(r); break;
4114 case Shader::OPCODE_BREAKC: BREAKC(r, s0, s1, control); break;
4115 case Shader::OPCODE_BREAKP: BREAKP(r, src0); break;
4116 case Shader::OPCODE_CONTINUE: CONTINUE(r); break;
4117 case Shader::OPCODE_TEST: TEST(); break;
4118 case Shader::OPCODE_CALL: CALL(r, dst.label, dst.callSite); break;
4119 case Shader::OPCODE_CALLNZ: CALLNZ(r, dst.label, dst.callSite, src0); break;
4120 case Shader::OPCODE_ELSE: ELSE(r); break;
4121 case Shader::OPCODE_ENDIF: ENDIF(r); break;
4122 case Shader::OPCODE_ENDLOOP: ENDLOOP(r); break;
4123 case Shader::OPCODE_ENDREP: ENDREP(r); break;
4124 case Shader::OPCODE_ENDWHILE: ENDWHILE(r); break;
4125 case Shader::OPCODE_IF: IF(r, src0); break;
4126 case Shader::OPCODE_IFC: IFC(r, s0, s1, control); break;
4127 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
4128 case Shader::OPCODE_LOOP: LOOP(r, src1); break;
4129 case Shader::OPCODE_REP: REP(r, src0); break;
4130 case Shader::OPCODE_WHILE: WHILE(r, src0); break;
4131 case Shader::OPCODE_RET: RET(r); break;
4132 case Shader::OPCODE_LEAVE: LEAVE(r); break;
4133 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
4134 case Shader::OPCODE_ALL: all(d.x, s0); break;
4135 case Shader::OPCODE_ANY: any(d.x, s0); break;
4136 case Shader::OPCODE_NOT: not(d, s0); break;
4137 case Shader::OPCODE_OR: or(d.x, s0.x, s1.x); break;
4138 case Shader::OPCODE_XOR: xor(d.x, s0.x, s1.x); break;
4139 case Shader::OPCODE_AND: and(d.x, s0.x, s1.x); break;
4140 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -04004141 default:
4142 ASSERT(false);
4143 }
4144
John Bauman19bac1e2014-05-06 15:23:49 -04004145 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 -04004146 {
John Bauman19bac1e2014-05-06 15:23:49 -04004147 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -04004148 {
John Bauman19bac1e2014-05-06 15:23:49 -04004149 switch(opcode)
4150 {
4151 case Shader::OPCODE_DIV:
4152 if(dst.x) d.x = Trunc(d.x);
4153 if(dst.y) d.y = Trunc(d.y);
4154 if(dst.z) d.z = Trunc(d.z);
4155 if(dst.w) d.w = Trunc(d.w);
4156 break;
4157 default:
4158 break; // No truncation to integer required when arguments are integer
4159 }
John Bauman89401822014-05-06 15:04:28 -04004160 }
4161
John Bauman19bac1e2014-05-06 15:23:49 -04004162 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -04004163 {
John Bauman19bac1e2014-05-06 15:23:49 -04004164 if(dst.x) d.x = Max(d.x, Float4(0.0f));
4165 if(dst.y) d.y = Max(d.y, Float4(0.0f));
4166 if(dst.z) d.z = Max(d.z, Float4(0.0f));
4167 if(dst.w) d.w = Max(d.w, Float4(0.0f));
4168
4169 if(dst.x) d.x = Min(d.x, Float4(1.0f));
4170 if(dst.y) d.y = Min(d.y, Float4(1.0f));
4171 if(dst.z) d.z = Min(d.z, Float4(1.0f));
4172 if(dst.w) d.w = Min(d.w, Float4(1.0f));
4173 }
4174
Nicolas Capensc6e8ab12014-05-06 23:31:07 -04004175 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -04004176 {
4177 Vector4f pDst; // FIXME: Rename
John Bauman89401822014-05-06 15:04:28 -04004178
4179 switch(dst.type)
4180 {
John Bauman19bac1e2014-05-06 15:23:49 -04004181 case Shader::PARAMETER_TEMP:
4182 if(dst.rel.type == Shader::PARAMETER_VOID)
4183 {
4184 if(dst.x) pDst.x = r.rf[dst.index].x;
4185 if(dst.y) pDst.y = r.rf[dst.index].y;
4186 if(dst.z) pDst.z = r.rf[dst.index].z;
4187 if(dst.w) pDst.w = r.rf[dst.index].w;
4188 }
4189 else
4190 {
4191 Int a = relativeAddress(r, dst);
4192
4193 if(dst.x) pDst.x = r.rf[dst.index + a].x;
4194 if(dst.y) pDst.y = r.rf[dst.index + a].y;
4195 if(dst.z) pDst.z = r.rf[dst.index + a].z;
4196 if(dst.w) pDst.w = r.rf[dst.index + a].w;
4197 }
John Bauman89401822014-05-06 15:04:28 -04004198 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004199 case Shader::PARAMETER_COLOROUT:
4200 ASSERT(dst.rel.type == Shader::PARAMETER_VOID);
John Bauman89401822014-05-06 15:04:28 -04004201 if(dst.x) pDst.x = r.oC[dst.index].x;
4202 if(dst.y) pDst.y = r.oC[dst.index].y;
4203 if(dst.z) pDst.z = r.oC[dst.index].z;
4204 if(dst.w) pDst.w = r.oC[dst.index].w;
4205 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004206 case Shader::PARAMETER_PREDICATE:
John Bauman89401822014-05-06 15:04:28 -04004207 if(dst.x) pDst.x = r.p0.x;
4208 if(dst.y) pDst.y = r.p0.y;
4209 if(dst.z) pDst.z = r.p0.z;
4210 if(dst.w) pDst.w = r.p0.w;
4211 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004212 case Shader::PARAMETER_DEPTHOUT:
John Bauman89401822014-05-06 15:04:28 -04004213 pDst.x = r.oDepth;
4214 break;
4215 default:
4216 ASSERT(false);
4217 }
4218
John Bauman19bac1e2014-05-06 15:23:49 -04004219 Int4 enable = enableMask(r, instruction);
John Bauman89401822014-05-06 15:04:28 -04004220
4221 Int4 xEnable = enable;
4222 Int4 yEnable = enable;
4223 Int4 zEnable = enable;
4224 Int4 wEnable = enable;
4225
4226 if(predicate)
4227 {
John Bauman19bac1e2014-05-06 15:23:49 -04004228 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -04004229
4230 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
4231 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
4232 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
4233 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
4234
John Bauman19bac1e2014-05-06 15:23:49 -04004235 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -04004236 {
4237 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
4238 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
4239 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
4240 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
4241 }
4242 else
4243 {
4244 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
4245 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
4246 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
4247 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
4248 }
4249 }
4250
4251 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
4252 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
4253 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
4254 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
4255
4256 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
4257 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
4258 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
4259 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
4260 }
4261
4262 switch(dst.type)
4263 {
John Bauman19bac1e2014-05-06 15:23:49 -04004264 case Shader::PARAMETER_TEMP:
4265 if(dst.rel.type == Shader::PARAMETER_VOID)
4266 {
4267 if(dst.x) r.rf[dst.index].x = d.x;
4268 if(dst.y) r.rf[dst.index].y = d.y;
4269 if(dst.z) r.rf[dst.index].z = d.z;
4270 if(dst.w) r.rf[dst.index].w = d.w;
4271 }
4272 else
4273 {
4274 Int a = relativeAddress(r, dst);
4275
4276 if(dst.x) r.rf[dst.index + a].x = d.x;
4277 if(dst.y) r.rf[dst.index + a].y = d.y;
4278 if(dst.z) r.rf[dst.index + a].z = d.z;
4279 if(dst.w) r.rf[dst.index + a].w = d.w;
4280 }
John Bauman89401822014-05-06 15:04:28 -04004281 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004282 case Shader::PARAMETER_COLOROUT:
4283 ASSERT(dst.rel.type == Shader::PARAMETER_VOID);
4284 if(dst.x) {r.oC[dst.index].x = d.x; out[dst.index][0] = true;}
4285 if(dst.y) {r.oC[dst.index].y = d.y; out[dst.index][1] = true;}
4286 if(dst.z) {r.oC[dst.index].z = d.z; out[dst.index][2] = true;}
4287 if(dst.w) {r.oC[dst.index].w = d.w; out[dst.index][3] = true;}
John Bauman89401822014-05-06 15:04:28 -04004288 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004289 case Shader::PARAMETER_PREDICATE:
John Bauman89401822014-05-06 15:04:28 -04004290 if(dst.x) r.p0.x = d.x;
4291 if(dst.y) r.p0.y = d.y;
4292 if(dst.z) r.p0.z = d.z;
4293 if(dst.w) r.p0.w = d.w;
4294 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004295 case Shader::PARAMETER_DEPTHOUT:
John Bauman89401822014-05-06 15:04:28 -04004296 r.oDepth = d.x;
4297 break;
4298 default:
4299 ASSERT(false);
4300 }
4301 }
4302 }
4303
John Bauman19bac1e2014-05-06 15:23:49 -04004304 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -04004305 {
4306 Nucleus::setInsertBlock(returnBlock);
4307 }
John Bauman19bac1e2014-05-06 15:23:49 -04004308
4309 for(int i = 0; i < 4; i++)
4310 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04004311 if(state.targetFormat[i] != FORMAT_NULL)
John Bauman19bac1e2014-05-06 15:23:49 -04004312 {
4313 if(!out[i][0]) r.oC[i].x = Float4(0.0f);
4314 if(!out[i][1]) r.oC[i].y = Float4(0.0f);
4315 if(!out[i][2]) r.oC[i].z = Float4(0.0f);
4316 if(!out[i][3]) r.oC[i].w = Float4(0.0f);
4317 }
4318 }
John Bauman89401822014-05-06 15:04:28 -04004319 }
4320
John Bauman19bac1e2014-05-06 15:23:49 -04004321 Short4 PixelRoutine::convertFixed12(RValue<Float4> cf)
John Bauman89401822014-05-06 15:04:28 -04004322 {
John Bauman19bac1e2014-05-06 15:23:49 -04004323 return RoundShort4(cf * Float4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04004324 }
4325
John Bauman19bac1e2014-05-06 15:23:49 -04004326 void PixelRoutine::convertFixed12(Vector4i &ci, Vector4f &cf)
John Bauman89401822014-05-06 15:04:28 -04004327 {
John Bauman19bac1e2014-05-06 15:23:49 -04004328 ci.x = convertFixed12(cf.x);
4329 ci.y = convertFixed12(cf.y);
4330 ci.z = convertFixed12(cf.z);
4331 ci.w = convertFixed12(cf.w);
John Bauman89401822014-05-06 15:04:28 -04004332 }
4333
4334 UShort4 PixelRoutine::convertFixed16(Float4 &cf, bool saturate)
4335 {
John Bauman19bac1e2014-05-06 15:23:49 -04004336 return UShort4(cf * Float4(0xFFFF), saturate);
John Bauman89401822014-05-06 15:04:28 -04004337 }
4338
John Bauman19bac1e2014-05-06 15:23:49 -04004339 void PixelRoutine::convertFixed16(Vector4i &ci, Vector4f &cf, bool saturate)
John Bauman89401822014-05-06 15:04:28 -04004340 {
John Bauman19bac1e2014-05-06 15:23:49 -04004341 ci.x = convertFixed16(cf.x, saturate);
4342 ci.y = convertFixed16(cf.y, saturate);
4343 ci.z = convertFixed16(cf.z, saturate);
4344 ci.w = convertFixed16(cf.w, saturate);
John Bauman89401822014-05-06 15:04:28 -04004345 }
4346
4347 Float4 PixelRoutine::convertSigned12(Short4 &ci)
4348 {
4349 return Float4(ci) * Float4(1.0f / 0x0FFE);
4350 }
4351
John Bauman19bac1e2014-05-06 15:23:49 -04004352 void PixelRoutine::convertSigned12(Vector4f &cf, Vector4i &ci)
John Bauman89401822014-05-06 15:04:28 -04004353 {
John Bauman19bac1e2014-05-06 15:23:49 -04004354 cf.x = convertSigned12(ci.x);
4355 cf.y = convertSigned12(ci.y);
4356 cf.z = convertSigned12(ci.z);
4357 cf.w = convertSigned12(ci.w);
John Bauman89401822014-05-06 15:04:28 -04004358 }
4359
4360 Float4 PixelRoutine::convertUnsigned16(UShort4 ci)
4361 {
John Bauman19bac1e2014-05-06 15:23:49 -04004362 return Float4(ci) * Float4(1.0f / 0xFFFF);
John Bauman89401822014-05-06 15:04:28 -04004363 }
4364
John Bauman19bac1e2014-05-06 15:23:49 -04004365 void PixelRoutine::sRGBtoLinear16_16(Registers &r, Vector4i &c)
John Bauman89401822014-05-06 15:04:28 -04004366 {
John Bauman19bac1e2014-05-06 15:23:49 -04004367 c.x = As<UShort4>(c.x) >> 4;
4368 c.y = As<UShort4>(c.y) >> 4;
4369 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04004370
4371 sRGBtoLinear12_16(r, c);
4372 }
4373
John Bauman19bac1e2014-05-06 15:23:49 -04004374 void PixelRoutine::sRGBtoLinear12_16(Registers &r, Vector4i &c)
John Bauman89401822014-05-06 15:04:28 -04004375 {
4376 Pointer<Byte> LUT = r.constants + OFFSET(Constants,sRGBtoLin12_16);
4377
John Bauman19bac1e2014-05-06 15:23:49 -04004378 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
4379 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
4380 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
4381 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004382
John Bauman19bac1e2014-05-06 15:23:49 -04004383 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
4384 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
4385 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
4386 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004387
John Bauman19bac1e2014-05-06 15:23:49 -04004388 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
4389 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
4390 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
4391 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004392 }
4393
John Bauman19bac1e2014-05-06 15:23:49 -04004394 void PixelRoutine::linearToSRGB16_16(Registers &r, Vector4i &c)
John Bauman89401822014-05-06 15:04:28 -04004395 {
John Bauman19bac1e2014-05-06 15:23:49 -04004396 c.x = As<UShort4>(c.x) >> 4;
4397 c.y = As<UShort4>(c.y) >> 4;
4398 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04004399
4400 linearToSRGB12_16(r, c);
4401 }
4402
John Bauman19bac1e2014-05-06 15:23:49 -04004403 void PixelRoutine::linearToSRGB12_16(Registers &r, Vector4i &c)
John Bauman89401822014-05-06 15:04:28 -04004404 {
4405 Pointer<Byte> LUT = r.constants + OFFSET(Constants,linToSRGB12_16);
4406
John Bauman19bac1e2014-05-06 15:23:49 -04004407 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
4408 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
4409 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
4410 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004411
John Bauman19bac1e2014-05-06 15:23:49 -04004412 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
4413 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
4414 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
4415 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004416
John Bauman19bac1e2014-05-06 15:23:49 -04004417 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
4418 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
4419 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
4420 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004421 }
4422
4423 Float4 PixelRoutine::linearToSRGB(const Float4 &x) // Approximates x^(1.0/2.2)
4424 {
4425 Float4 sqrtx = Rcp_pp(RcpSqrt_pp(x));
4426 Float4 sRGB = sqrtx * Float4(1.14f) - x * Float4(0.14f);
4427
4428 return Min(Max(sRGB, Float4(0.0f)), Float4(1.0f));
4429 }
4430
4431 Float4 PixelRoutine::sRGBtoLinear(const Float4 &x) // Approximates x^2.2
4432 {
4433 Float4 linear = x * x;
4434 linear = linear * Float4(0.73f) + linear * x * Float4(0.27f);
4435
4436 return Min(Max(linear, Float4(0.0f)), Float4(1.0f));
4437 }
4438
John Bauman19bac1e2014-05-06 15:23:49 -04004439 void PixelRoutine::MOV(Vector4i &dst, Vector4i &src0)
John Bauman89401822014-05-06 15:04:28 -04004440 {
John Bauman19bac1e2014-05-06 15:23:49 -04004441 dst.x = src0.x;
4442 dst.y = src0.y;
4443 dst.z = src0.z;
4444 dst.w = src0.w;
John Bauman89401822014-05-06 15:04:28 -04004445 }
4446
John Bauman19bac1e2014-05-06 15:23:49 -04004447 void PixelRoutine::ADD(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004448 {
John Bauman19bac1e2014-05-06 15:23:49 -04004449 dst.x = AddSat(src0.x, src1.x);
4450 dst.y = AddSat(src0.y, src1.y);
4451 dst.z = AddSat(src0.z, src1.z);
4452 dst.w = AddSat(src0.w, src1.w);
John Bauman89401822014-05-06 15:04:28 -04004453 }
4454
John Bauman19bac1e2014-05-06 15:23:49 -04004455 void PixelRoutine::SUB(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004456 {
John Bauman19bac1e2014-05-06 15:23:49 -04004457 dst.x = SubSat(src0.x, src1.x);
4458 dst.y = SubSat(src0.y, src1.y);
4459 dst.z = SubSat(src0.z, src1.z);
4460 dst.w = SubSat(src0.w, src1.w);
John Bauman89401822014-05-06 15:04:28 -04004461 }
4462
John Bauman19bac1e2014-05-06 15:23:49 -04004463 void PixelRoutine::MAD(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2)
John Bauman89401822014-05-06 15:04:28 -04004464 {
4465 // FIXME: Long fixed-point multiply fixup
4466 {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);}
4467 {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);}
4468 {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);}
4469 {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);}
4470 }
4471
John Bauman19bac1e2014-05-06 15:23:49 -04004472 void PixelRoutine::MUL(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004473 {
4474 // FIXME: Long fixed-point multiply fixup
4475 {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);}
4476 {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);}
4477 {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);}
4478 {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);}
4479 }
4480
John Bauman19bac1e2014-05-06 15:23:49 -04004481 void PixelRoutine::DP3(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004482 {
4483 Short4 t0;
4484 Short4 t1;
4485
4486 // FIXME: Long fixed-point multiply fixup
4487 t0 = MulHigh(src0.x, src1.x); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0);
4488 t1 = MulHigh(src0.y, src1.y); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4489 t0 = AddSat(t0, t1);
4490 t1 = MulHigh(src0.z, src1.z); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4491 t0 = AddSat(t0, t1);
4492
John Bauman19bac1e2014-05-06 15:23:49 -04004493 dst.x = t0;
4494 dst.y = t0;
4495 dst.z = t0;
4496 dst.w = t0;
John Bauman89401822014-05-06 15:04:28 -04004497 }
4498
John Bauman19bac1e2014-05-06 15:23:49 -04004499 void PixelRoutine::DP4(Vector4i &dst, Vector4i &src0, Vector4i &src1)
John Bauman89401822014-05-06 15:04:28 -04004500 {
4501 Short4 t0;
4502 Short4 t1;
4503
4504 // FIXME: Long fixed-point multiply fixup
4505 t0 = MulHigh(src0.x, src1.x); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0);
4506 t1 = MulHigh(src0.y, src1.y); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4507 t0 = AddSat(t0, t1);
4508 t1 = MulHigh(src0.z, src1.z); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4509 t0 = AddSat(t0, t1);
4510 t1 = MulHigh(src0.w, src1.w); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4511 t0 = AddSat(t0, t1);
4512
John Bauman19bac1e2014-05-06 15:23:49 -04004513 dst.x = t0;
4514 dst.y = t0;
4515 dst.z = t0;
4516 dst.w = t0;
John Bauman89401822014-05-06 15:04:28 -04004517 }
4518
John Bauman19bac1e2014-05-06 15:23:49 -04004519 void PixelRoutine::LRP(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2)
John Bauman89401822014-05-06 15:04:28 -04004520 {
4521 // FIXME: Long fixed-point multiply fixup
4522 {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);}
4523 {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);}
4524 {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);}
4525 {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);}
4526 }
4527
John Bauman19bac1e2014-05-06 15:23:49 -04004528 void PixelRoutine::TEXCOORD(Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int coordinate)
John Bauman89401822014-05-06 15:04:28 -04004529 {
4530 Float4 uw;
4531 Float4 vw;
4532 Float4 sw;
4533
4534 if(state.interpolant[2 + coordinate].component & 0x01)
4535 {
John Bauman19bac1e2014-05-06 15:23:49 -04004536 uw = Max(u, Float4(0.0f));
4537 uw = Min(uw, Float4(1.0f));
4538 dst.x = convertFixed12(uw);
John Bauman89401822014-05-06 15:04:28 -04004539 }
4540 else
4541 {
John Bauman19bac1e2014-05-06 15:23:49 -04004542 dst.x = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004543 }
4544
4545 if(state.interpolant[2 + coordinate].component & 0x02)
4546 {
John Bauman19bac1e2014-05-06 15:23:49 -04004547 vw = Max(v, Float4(0.0f));
4548 vw = Min(vw, Float4(1.0f));
4549 dst.y = convertFixed12(vw);
John Bauman89401822014-05-06 15:04:28 -04004550 }
4551 else
4552 {
John Bauman19bac1e2014-05-06 15:23:49 -04004553 dst.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004554 }
4555
4556 if(state.interpolant[2 + coordinate].component & 0x04)
4557 {
John Bauman19bac1e2014-05-06 15:23:49 -04004558 sw = Max(s, Float4(0.0f));
4559 sw = Min(sw, Float4(1.0f));
4560 dst.z = convertFixed12(sw);
John Bauman89401822014-05-06 15:04:28 -04004561 }
4562 else
4563 {
John Bauman19bac1e2014-05-06 15:23:49 -04004564 dst.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004565 }
4566
John Bauman19bac1e2014-05-06 15:23:49 -04004567 dst.w = Short4(0x1000);
John Bauman89401822014-05-06 15:04:28 -04004568 }
4569
John Bauman19bac1e2014-05-06 15:23:49 -04004570 void PixelRoutine::TEXCRD(Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int coordinate, bool project)
John Bauman89401822014-05-06 15:04:28 -04004571 {
4572 Float4 uw = u;
4573 Float4 vw = v;
4574 Float4 sw = s;
4575
4576 if(project)
4577 {
4578 uw *= Rcp_pp(s);
4579 vw *= Rcp_pp(s);
4580 }
4581
4582 if(state.interpolant[2 + coordinate].component & 0x01)
4583 {
John Bauman19bac1e2014-05-06 15:23:49 -04004584 uw *= Float4(0x1000);
4585 uw = Max(uw, Float4(-0x8000));
4586 uw = Min(uw, Float4(0x7FFF));
4587 dst.x = RoundShort4(uw);
John Bauman89401822014-05-06 15:04:28 -04004588 }
4589 else
4590 {
John Bauman19bac1e2014-05-06 15:23:49 -04004591 dst.x = Short4(0x0000);
John Bauman89401822014-05-06 15:04:28 -04004592 }
4593
4594 if(state.interpolant[2 + coordinate].component & 0x02)
4595 {
John Bauman19bac1e2014-05-06 15:23:49 -04004596 vw *= Float4(0x1000);
4597 vw = Max(vw, Float4(-0x8000));
4598 vw = Min(vw, Float4(0x7FFF));
4599 dst.y = RoundShort4(vw);
John Bauman89401822014-05-06 15:04:28 -04004600 }
4601 else
4602 {
John Bauman19bac1e2014-05-06 15:23:49 -04004603 dst.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004604 }
4605
4606 if(state.interpolant[2 + coordinate].component & 0x04)
4607 {
John Bauman19bac1e2014-05-06 15:23:49 -04004608 sw *= Float4(0x1000);
4609 sw = Max(sw, Float4(-0x8000));
4610 sw = Min(sw, Float4(0x7FFF));
4611 dst.z = RoundShort4(sw);
John Bauman89401822014-05-06 15:04:28 -04004612 }
4613 else
4614 {
John Bauman19bac1e2014-05-06 15:23:49 -04004615 dst.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004616 }
4617 }
4618
John Bauman19bac1e2014-05-06 15:23:49 -04004619 void PixelRoutine::TEXDP3(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src)
John Bauman89401822014-05-06 15:04:28 -04004620 {
4621 TEXM3X3PAD(r, u, v, s, src, 0, false);
4622
John Bauman19bac1e2014-05-06 15:23:49 -04004623 Short4 t0 = RoundShort4(r.u_ * Float4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04004624
John Bauman19bac1e2014-05-06 15:23:49 -04004625 dst.x = t0;
4626 dst.y = t0;
4627 dst.z = t0;
4628 dst.w = t0;
John Bauman89401822014-05-06 15:04:28 -04004629 }
4630
John Bauman19bac1e2014-05-06 15:23:49 -04004631 void PixelRoutine::TEXDP3TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0)
John Bauman89401822014-05-06 15:04:28 -04004632 {
4633 TEXM3X3PAD(r, u, v, s, src0, 0, false);
4634
John Bauman19bac1e2014-05-06 15:23:49 -04004635 r.v_ = Float4(0.0f);
4636 r.w_ = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04004637
4638 sampleTexture(r, dst, stage, r.u_, r.v_, r.w_, r.w_);
4639 }
4640
4641 void PixelRoutine::TEXKILL(Int cMask[4], Float4 &u, Float4 &v, Float4 &s)
4642 {
John Bauman19bac1e2014-05-06 15:23:49 -04004643 Int kill = SignMask(CmpNLT(u, Float4(0.0f))) &
4644 SignMask(CmpNLT(v, Float4(0.0f))) &
4645 SignMask(CmpNLT(s, Float4(0.0f)));
John Bauman89401822014-05-06 15:04:28 -04004646
4647 for(unsigned int q = 0; q < state.multiSample; q++)
4648 {
4649 cMask[q] &= kill;
4650 }
4651 }
4652
John Bauman19bac1e2014-05-06 15:23:49 -04004653 void PixelRoutine::TEXKILL(Int cMask[4], Vector4i &src)
John Bauman89401822014-05-06 15:04:28 -04004654 {
John Bauman19bac1e2014-05-06 15:23:49 -04004655 Short4 test = src.x | src.y | src.z;
John Bauman89401822014-05-06 15:04:28 -04004656 Int kill = SignMask(Pack(test, test)) ^ 0x0000000F;
4657
4658 for(unsigned int q = 0; q < state.multiSample; q++)
4659 {
4660 cMask[q] &= kill;
4661 }
4662 }
4663
John Bauman19bac1e2014-05-06 15:23:49 -04004664 void PixelRoutine::TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int sampler, bool project)
John Bauman89401822014-05-06 15:04:28 -04004665 {
4666 sampleTexture(r, dst, sampler, u, v, s, s, project);
4667 }
4668
John Bauman19bac1e2014-05-06 15:23:49 -04004669 void PixelRoutine::TEXLD(Registers &r, Vector4i &dst, Vector4i &src, int sampler, bool project)
John Bauman89401822014-05-06 15:04:28 -04004670 {
John Bauman19bac1e2014-05-06 15:23:49 -04004671 Float4 u = Float4(src.x) * Float4(1.0f / 0x0FFE);
4672 Float4 v = Float4(src.y) * Float4(1.0f / 0x0FFE);
4673 Float4 s = Float4(src.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004674
4675 sampleTexture(r, dst, sampler, u, v, s, s, project);
4676 }
4677
John Bauman19bac1e2014-05-06 15:23:49 -04004678 void PixelRoutine::TEXBEM(Registers &r, Vector4i &dst, Vector4i &src, Float4 &u, Float4 &v, Float4 &s, int stage)
John Bauman89401822014-05-06 15:04:28 -04004679 {
John Bauman19bac1e2014-05-06 15:23:49 -04004680 Float4 du = Float4(src.x) * Float4(1.0f / 0x0FFE);
4681 Float4 dv = Float4(src.y) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004682
4683 Float4 du2 = du;
4684 Float4 dv2 = dv;
4685
4686 du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
4687 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
4688 du += dv2;
4689 dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
4690 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
4691 dv += du2;
4692
4693 Float4 u_ = u + du;
4694 Float4 v_ = v + dv;
4695
4696 sampleTexture(r, dst, stage, u_, v_, s, s);
4697 }
4698
John Bauman19bac1e2014-05-06 15:23:49 -04004699 void PixelRoutine::TEXBEML(Registers &r, Vector4i &dst, Vector4i &src, Float4 &u, Float4 &v, Float4 &s, int stage)
John Bauman89401822014-05-06 15:04:28 -04004700 {
John Bauman19bac1e2014-05-06 15:23:49 -04004701 Float4 du = Float4(src.x) * Float4(1.0f / 0x0FFE);
4702 Float4 dv = Float4(src.y) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004703
4704 Float4 du2 = du;
4705 Float4 dv2 = dv;
4706
4707 du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
4708 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
4709 du += dv2;
4710 dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
4711 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
4712 dv += du2;
4713
4714 Float4 u_ = u + du;
4715 Float4 v_ = v + dv;
4716
4717 sampleTexture(r, dst, stage, u_, v_, s, s);
4718
4719 Short4 L;
4720
John Bauman19bac1e2014-05-06 15:23:49 -04004721 L = src.z;
John Bauman89401822014-05-06 15:04:28 -04004722 L = MulHigh(L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceScale4)));
4723 L = L << 4;
4724 L = AddSat(L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceOffset4)));
4725 L = Max(L, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman19bac1e2014-05-06 15:23:49 -04004726 L = Min(L, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04004727
John Bauman19bac1e2014-05-06 15:23:49 -04004728 dst.x = MulHigh(dst.x, L); dst.x = dst.x << 4;
4729 dst.y = MulHigh(dst.y, L); dst.y = dst.y << 4;
4730 dst.z = MulHigh(dst.z, L); dst.z = dst.z << 4;
John Bauman89401822014-05-06 15:04:28 -04004731 }
4732
John Bauman19bac1e2014-05-06 15:23:49 -04004733 void PixelRoutine::TEXREG2AR(Registers &r, Vector4i &dst, Vector4i &src0, int stage)
John Bauman89401822014-05-06 15:04:28 -04004734 {
John Bauman19bac1e2014-05-06 15:23:49 -04004735 Float4 u = Float4(src0.w) * Float4(1.0f / 0x0FFE);
4736 Float4 v = Float4(src0.x) * Float4(1.0f / 0x0FFE);
4737 Float4 s = Float4(src0.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004738
4739 sampleTexture(r, dst, stage, u, v, s, s);
4740 }
4741
John Bauman19bac1e2014-05-06 15:23:49 -04004742 void PixelRoutine::TEXREG2GB(Registers &r, Vector4i &dst, Vector4i &src0, int stage)
John Bauman89401822014-05-06 15:04:28 -04004743 {
John Bauman19bac1e2014-05-06 15:23:49 -04004744 Float4 u = Float4(src0.y) * Float4(1.0f / 0x0FFE);
4745 Float4 v = Float4(src0.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004746 Float4 s = v;
4747
4748 sampleTexture(r, dst, stage, u, v, s, s);
4749 }
4750
John Bauman19bac1e2014-05-06 15:23:49 -04004751 void PixelRoutine::TEXREG2RGB(Registers &r, Vector4i &dst, Vector4i &src0, int stage)
John Bauman89401822014-05-06 15:04:28 -04004752 {
John Bauman19bac1e2014-05-06 15:23:49 -04004753 Float4 u = Float4(src0.x) * Float4(1.0f / 0x0FFE);
4754 Float4 v = Float4(src0.y) * Float4(1.0f / 0x0FFE);
4755 Float4 s = Float4(src0.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004756
4757 sampleTexture(r, dst, stage, u, v, s, s);
4758 }
4759
John Bauman19bac1e2014-05-06 15:23:49 -04004760 void PixelRoutine::TEXM3X2DEPTH(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004761 {
4762 TEXM3X2PAD(r, u, v, s, src, 1, signedScaling);
4763
4764 // z / w
4765 r.u_ *= Rcp_pp(r.v_); // FIXME: Set result to 1.0 when division by zero
4766
4767 r.oDepth = r.u_;
4768 }
4769
John Bauman19bac1e2014-05-06 15:23:49 -04004770 void PixelRoutine::TEXM3X2PAD(Registers &r, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, int component, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004771 {
4772 TEXM3X3PAD(r, u, v, s, src0, component, signedScaling);
4773 }
4774
John Bauman19bac1e2014-05-06 15:23:49 -04004775 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 -04004776 {
4777 TEXM3X2PAD(r, u, v, s, src0, 1, signedScaling);
4778
John Bauman19bac1e2014-05-06 15:23:49 -04004779 r.w_ = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04004780
4781 sampleTexture(r, dst, stage, r.u_, r.v_, r.w_, r.w_);
4782 }
4783
John Bauman19bac1e2014-05-06 15:23:49 -04004784 void PixelRoutine::TEXM3X3(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004785 {
4786 TEXM3X3PAD(r, u, v, s, src0, 2, signedScaling);
4787
John Bauman19bac1e2014-05-06 15:23:49 -04004788 dst.x = RoundShort4(r.u_ * Float4(0x1000));
4789 dst.y = RoundShort4(r.v_ * Float4(0x1000));
4790 dst.z = RoundShort4(r.w_ * Float4(0x1000));
4791 dst.w = Short4(0x1000);
John Bauman89401822014-05-06 15:04:28 -04004792 }
4793
John Bauman19bac1e2014-05-06 15:23:49 -04004794 void PixelRoutine::TEXM3X3PAD(Registers &r, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, int component, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004795 {
4796 if(component == 0 || previousScaling != signedScaling) // FIXME: Other source modifiers?
4797 {
John Bauman19bac1e2014-05-06 15:23:49 -04004798 r.U = Float4(src0.x);
4799 r.V = Float4(src0.y);
4800 r.W = Float4(src0.z);
John Bauman89401822014-05-06 15:04:28 -04004801
4802 previousScaling = signedScaling;
4803 }
4804
4805 Float4 x = r.U * u + r.V * v + r.W * s;
4806
John Bauman19bac1e2014-05-06 15:23:49 -04004807 x *= Float4(1.0f / 0x1000);
John Bauman89401822014-05-06 15:04:28 -04004808
4809 switch(component)
4810 {
4811 case 0: r.u_ = x; break;
4812 case 1: r.v_ = x; break;
4813 case 2: r.w_ = x; break;
4814 default: ASSERT(false);
4815 }
4816 }
4817
John Bauman19bac1e2014-05-06 15:23:49 -04004818 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 -04004819 {
4820 TEXM3X3PAD(r, u, v, s, src0, 2, false);
4821
4822 Float4 E[3]; // Eye vector
4823
John Bauman19bac1e2014-05-06 15:23:49 -04004824 E[0] = Float4(src1.x) * Float4(1.0f / 0x0FFE);
4825 E[1] = Float4(src1.y) * Float4(1.0f / 0x0FFE);
4826 E[2] = Float4(src1.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004827
4828 // Reflection
4829 Float4 u__;
4830 Float4 v__;
4831 Float4 w__;
4832
4833 // (u'', v'', w'') = 2 * (N . E) * N - E * (N . N)
4834 u__ = r.u_ * E[0];
4835 v__ = r.v_ * E[1];
4836 w__ = r.w_ * E[2];
4837 u__ += v__ + w__;
4838 u__ += u__;
4839 v__ = u__;
4840 w__ = u__;
4841 u__ *= r.u_;
4842 v__ *= r.v_;
4843 w__ *= r.w_;
4844 r.u_ *= r.u_;
4845 r.v_ *= r.v_;
4846 r.w_ *= r.w_;
4847 r.u_ += r.v_ + r.w_;
4848 u__ -= E[0] * r.u_;
4849 v__ -= E[1] * r.u_;
4850 w__ -= E[2] * r.u_;
4851
4852 sampleTexture(r, dst, stage, u__, v__, w__, w__);
4853 }
4854
John Bauman19bac1e2014-05-06 15:23:49 -04004855 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 -04004856 {
4857 TEXM3X3PAD(r, u, v, s, src0, 2, signedScaling);
4858
4859 sampleTexture(r, dst, stage, r.u_, r.v_, r.w_, r.w_);
4860 }
4861
John Bauman19bac1e2014-05-06 15:23:49 -04004862 void PixelRoutine::TEXM3X3VSPEC(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0)
John Bauman89401822014-05-06 15:04:28 -04004863 {
4864 TEXM3X3PAD(r, u, v, s, src0, 2, false);
4865
4866 Float4 E[3]; // Eye vector
4867
John Bauman19bac1e2014-05-06 15:23:49 -04004868 E[0] = r.vf[2 + stage - 2].w;
4869 E[1] = r.vf[2 + stage - 1].w;
4870 E[2] = r.vf[2 + stage - 0].w;
John Bauman89401822014-05-06 15:04:28 -04004871
4872 // Reflection
4873 Float4 u__;
4874 Float4 v__;
4875 Float4 w__;
4876
4877 // (u'', v'', w'') = 2 * (N . E) * N - E * (N . N)
4878 u__ = r.u_ * E[0];
4879 v__ = r.v_ * E[1];
4880 w__ = r.w_ * E[2];
4881 u__ += v__ + w__;
4882 u__ += u__;
4883 v__ = u__;
4884 w__ = u__;
4885 u__ *= r.u_;
4886 v__ *= r.v_;
4887 w__ *= r.w_;
4888 r.u_ *= r.u_;
4889 r.v_ *= r.v_;
4890 r.w_ *= r.w_;
4891 r.u_ += r.v_ + r.w_;
4892 u__ -= E[0] * r.u_;
4893 v__ -= E[1] * r.u_;
4894 w__ -= E[2] * r.u_;
4895
4896 sampleTexture(r, dst, stage, u__, v__, w__, w__);
4897 }
4898
4899 void PixelRoutine::TEXDEPTH(Registers &r)
4900 {
John Bauman19bac1e2014-05-06 15:23:49 -04004901 r.u_ = Float4(r.ri[5].x);
4902 r.v_ = Float4(r.ri[5].y);
John Bauman89401822014-05-06 15:04:28 -04004903
4904 // z / w
4905 r.u_ *= Rcp_pp(r.v_); // FIXME: Set result to 1.0 when division by zero
4906
4907 r.oDepth = r.u_;
4908 }
4909
John Bauman19bac1e2014-05-06 15:23:49 -04004910 void PixelRoutine::CND(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2)
John Bauman89401822014-05-06 15:04:28 -04004911 {
John Bauman19bac1e2014-05-06 15:23:49 -04004912 {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;};
4913 {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;};
4914 {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;};
4915 {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 -04004916 }
4917
John Bauman19bac1e2014-05-06 15:23:49 -04004918 void PixelRoutine::CMP(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2)
John Bauman89401822014-05-06 15:04:28 -04004919 {
John Bauman19bac1e2014-05-06 15:23:49 -04004920 {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;};
4921 {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;};
4922 {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;};
4923 {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 -04004924 }
4925
John Bauman19bac1e2014-05-06 15:23:49 -04004926 void PixelRoutine::BEM(Registers &r, Vector4i &dst, Vector4i &src0, Vector4i &src1, int stage)
John Bauman89401822014-05-06 15:04:28 -04004927 {
4928 Short4 t0;
4929 Short4 t1;
4930
John Bauman19bac1e2014-05-06 15:23:49 -04004931 // dst.x = src0.x + BUMPENVMAT00(stage) * src1.x + BUMPENVMAT10(stage) * src1.y
John Bauman89401822014-05-06 15:04:28 -04004932 t0 = MulHigh(src1.x, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[0][0]))); t0 = t0 << 4; // FIXME: Matrix components range? Overflow hazard.
4933 t1 = MulHigh(src1.y, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[1][0]))); t1 = t1 << 4; // FIXME: Matrix components range? Overflow hazard.
4934 t0 = AddSat(t0, t1);
4935 t0 = AddSat(t0, src0.x);
John Bauman19bac1e2014-05-06 15:23:49 -04004936 dst.x = t0;
John Bauman89401822014-05-06 15:04:28 -04004937
John Bauman19bac1e2014-05-06 15:23:49 -04004938 // dst.y = src0.y + BUMPENVMAT01(stage) * src1.x + BUMPENVMAT11(stage) * src1.y
John Bauman89401822014-05-06 15:04:28 -04004939 t0 = MulHigh(src1.x, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[0][1]))); t0 = t0 << 4; // FIXME: Matrix components range? Overflow hazard.
4940 t1 = MulHigh(src1.y, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[1][1]))); t1 = t1 << 4; // FIXME: Matrix components range? Overflow hazard.
4941 t0 = AddSat(t0, t1);
4942 t0 = AddSat(t0, src0.y);
John Bauman19bac1e2014-05-06 15:23:49 -04004943 dst.y = t0;
John Bauman89401822014-05-06 15:04:28 -04004944 }
4945
John Bauman19bac1e2014-05-06 15:23:49 -04004946 void PixelRoutine::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004947 {
John Bauman19bac1e2014-05-06 15:23:49 -04004948 Vector4f row0 = reg(r, src1, 0);
4949 Vector4f row1 = reg(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -04004950
4951 dst.x = dot3(src0, row0);
4952 dst.y = dot3(src0, row1);
4953 }
4954
John Bauman19bac1e2014-05-06 15:23:49 -04004955 void PixelRoutine::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004956 {
John Bauman19bac1e2014-05-06 15:23:49 -04004957 Vector4f row0 = reg(r, src1, 0);
4958 Vector4f row1 = reg(r, src1, 1);
4959 Vector4f row2 = reg(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -04004960
4961 dst.x = dot3(src0, row0);
4962 dst.y = dot3(src0, row1);
4963 dst.z = dot3(src0, row2);
4964 }
4965
John Bauman19bac1e2014-05-06 15:23:49 -04004966 void PixelRoutine::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004967 {
John Bauman19bac1e2014-05-06 15:23:49 -04004968 Vector4f row0 = reg(r, src1, 0);
4969 Vector4f row1 = reg(r, src1, 1);
4970 Vector4f row2 = reg(r, src1, 2);
4971 Vector4f row3 = reg(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -04004972
4973 dst.x = dot3(src0, row0);
4974 dst.y = dot3(src0, row1);
4975 dst.z = dot3(src0, row2);
4976 dst.w = dot3(src0, row3);
4977 }
4978
John Bauman19bac1e2014-05-06 15:23:49 -04004979 void PixelRoutine::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004980 {
John Bauman19bac1e2014-05-06 15:23:49 -04004981 Vector4f row0 = reg(r, src1, 0);
4982 Vector4f row1 = reg(r, src1, 1);
4983 Vector4f row2 = reg(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -04004984
4985 dst.x = dot4(src0, row0);
4986 dst.y = dot4(src0, row1);
4987 dst.z = dot4(src0, row2);
4988 }
4989
John Bauman19bac1e2014-05-06 15:23:49 -04004990 void PixelRoutine::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004991 {
John Bauman19bac1e2014-05-06 15:23:49 -04004992 Vector4f row0 = reg(r, src1, 0);
4993 Vector4f row1 = reg(r, src1, 1);
4994 Vector4f row2 = reg(r, src1, 2);
4995 Vector4f row3 = reg(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -04004996
4997 dst.x = dot4(src0, row0);
4998 dst.y = dot4(src0, row1);
4999 dst.z = dot4(src0, row2);
5000 dst.w = dot4(src0, row3);
5001 }
5002
John Bauman19bac1e2014-05-06 15:23:49 -04005003 void PixelRoutine::TEXLD(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias)
John Bauman89401822014-05-06 15:04:28 -04005004 {
John Bauman19bac1e2014-05-06 15:23:49 -04005005 Vector4f tmp;
5006 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w, src0, src0, project, bias);
John Bauman89401822014-05-06 15:04:28 -04005007
5008 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
5009 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
5010 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
5011 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
5012 }
5013
John Bauman19bac1e2014-05-06 15:23:49 -04005014 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 -04005015 {
John Bauman19bac1e2014-05-06 15:23:49 -04005016 Vector4f tmp;
5017 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w, src2, src3, project, bias, true);
John Bauman89401822014-05-06 15:04:28 -04005018
5019 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
5020 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
5021 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
5022 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
5023 }
5024
John Bauman19bac1e2014-05-06 15:23:49 -04005025 void PixelRoutine::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias)
John Bauman89401822014-05-06 15:04:28 -04005026 {
John Bauman19bac1e2014-05-06 15:23:49 -04005027 Vector4f tmp;
5028 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 -04005029
5030 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
5031 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
5032 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
5033 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
5034 }
5035
John Bauman19bac1e2014-05-06 15:23:49 -04005036 void PixelRoutine::TEXKILL(Int cMask[4], Vector4f &src, unsigned char mask)
John Bauman89401822014-05-06 15:04:28 -04005037 {
5038 Int kill = -1;
5039
John Bauman19bac1e2014-05-06 15:23:49 -04005040 if(mask & 0x1) kill &= SignMask(CmpNLT(src.x, Float4(0.0f)));
5041 if(mask & 0x2) kill &= SignMask(CmpNLT(src.y, Float4(0.0f)));
5042 if(mask & 0x4) kill &= SignMask(CmpNLT(src.z, Float4(0.0f)));
5043 if(mask & 0x8) kill &= SignMask(CmpNLT(src.w, Float4(0.0f)));
5044
5045 // FIXME: Dynamic branching affects TEXKILL?
5046 // if(shader->containsDynamicBranching())
5047 // {
5048 // kill = ~SignMask(enableMask(r));
5049 // }
John Bauman89401822014-05-06 15:04:28 -04005050
5051 for(unsigned int q = 0; q < state.multiSample; q++)
5052 {
5053 cMask[q] &= kill;
5054 }
John Bauman19bac1e2014-05-06 15:23:49 -04005055
5056 // FIXME: Branch to end of shader if all killed?
John Bauman89401822014-05-06 15:04:28 -04005057 }
5058
John Bauman19bac1e2014-05-06 15:23:49 -04005059 void PixelRoutine::DISCARD(Registers &r, Int cMask[4], const Shader::Instruction *instruction)
John Bauman89401822014-05-06 15:04:28 -04005060 {
John Bauman19bac1e2014-05-06 15:23:49 -04005061 Int kill = 0;
5062
5063 if(shader->containsDynamicBranching())
5064 {
5065 kill = ~SignMask(enableMask(r, instruction));
5066 }
5067
5068 for(unsigned int q = 0; q < state.multiSample; q++)
5069 {
5070 cMask[q] &= kill;
5071 }
5072
5073 // FIXME: Branch to end of shader if all killed?
John Bauman89401822014-05-06 15:04:28 -04005074 }
5075
John Bauman19bac1e2014-05-06 15:23:49 -04005076 void PixelRoutine::DFDX(Vector4f &dst, Vector4f &src)
John Bauman89401822014-05-06 15:04:28 -04005077 {
John Bauman19bac1e2014-05-06 15:23:49 -04005078 dst.x = src.x.yyww - src.x.xxzz;
5079 dst.y = src.y.yyww - src.y.xxzz;
5080 dst.z = src.z.yyww - src.z.xxzz;
5081 dst.w = src.w.yyww - src.w.xxzz;
5082 }
5083
5084 void PixelRoutine::DFDY(Vector4f &dst, Vector4f &src)
5085 {
5086 dst.x = src.x.zwzw - src.x.xyxy;
5087 dst.y = src.y.zwzw - src.y.xyxy;
5088 dst.z = src.z.zwzw - src.z.xyxy;
5089 dst.w = src.w.zwzw - src.w.xyxy;
5090 }
5091
5092 void PixelRoutine::FWIDTH(Vector4f &dst, Vector4f &src)
5093 {
5094 // abs(dFdx(src)) + abs(dFdy(src));
5095 dst.x = Abs(src.x.yyww - src.x.xxzz) + Abs(src.x.zwzw - src.x.xyxy);
5096 dst.y = Abs(src.y.yyww - src.x.xxzz) + Abs(src.y.zwzw - src.y.xyxy);
5097 dst.z = Abs(src.z.yyww - src.x.xxzz) + Abs(src.z.zwzw - src.z.xyxy);
5098 dst.w = Abs(src.w.yyww - src.x.xxzz) + Abs(src.w.zwzw - src.w.xyxy);
John Bauman89401822014-05-06 15:04:28 -04005099 }
5100
5101 void PixelRoutine::BREAK(Registers &r)
5102 {
5103 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
5104 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
5105
5106 if(breakDepth == 0)
5107 {
John Bauman19bac1e2014-05-06 15:23:49 -04005108 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04005109 Nucleus::createBr(endBlock);
5110 }
5111 else
5112 {
5113 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
5114 Bool allBreak = SignMask(r.enableBreak) == 0x0;
5115
John Bauman19bac1e2014-05-06 15:23:49 -04005116 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04005117 branch(allBreak, endBlock, deadBlock);
5118 }
5119
5120 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04005121 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04005122 }
5123
John Bauman19bac1e2014-05-06 15:23:49 -04005124 void PixelRoutine::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04005125 {
5126 Int4 condition;
5127
5128 switch(control)
5129 {
John Bauman19bac1e2014-05-06 15:23:49 -04005130 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
5131 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
5132 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
5133 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
5134 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
5135 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04005136 default:
5137 ASSERT(false);
5138 }
5139
John Bauman19bac1e2014-05-06 15:23:49 -04005140 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -04005141 }
5142
5143 void PixelRoutine::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
5144 {
5145 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
5146
John Bauman19bac1e2014-05-06 15:23:49 -04005147 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005148 {
5149 condition = ~condition;
5150 }
5151
John Bauman19bac1e2014-05-06 15:23:49 -04005152 BREAK(r, condition);
5153 }
5154
5155 void PixelRoutine::BREAK(Registers &r, Int4 &condition)
5156 {
John Bauman89401822014-05-06 15:04:28 -04005157 condition &= r.enableStack[r.enableIndex];
5158
5159 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
5160 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
5161
5162 r.enableBreak = r.enableBreak & ~condition;
5163 Bool allBreak = SignMask(r.enableBreak) == 0x0;
5164
John Bauman19bac1e2014-05-06 15:23:49 -04005165 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04005166 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04005167
John Bauman89401822014-05-06 15:04:28 -04005168 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04005169 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04005170 }
5171
John Bauman19bac1e2014-05-06 15:23:49 -04005172 void PixelRoutine::CONTINUE(Registers &r)
5173 {
5174 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
5175 }
5176
5177 void PixelRoutine::TEST()
5178 {
5179 whileTest = true;
5180 }
5181
5182 void PixelRoutine::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04005183 {
5184 if(!labelBlock[labelIndex])
5185 {
5186 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5187 }
5188
John Bauman19bac1e2014-05-06 15:23:49 -04005189 if(callRetBlock[labelIndex].size() > 1)
5190 {
5191 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
5192 }
John Bauman89401822014-05-06 15:04:28 -04005193
John Bauman19bac1e2014-05-06 15:23:49 -04005194 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04005195
5196 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04005197 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
5198
5199 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04005200 }
5201
John Bauman19bac1e2014-05-06 15:23:49 -04005202 void PixelRoutine::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04005203 {
John Bauman19bac1e2014-05-06 15:23:49 -04005204 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04005205 {
John Bauman19bac1e2014-05-06 15:23:49 -04005206 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04005207 }
John Bauman19bac1e2014-05-06 15:23:49 -04005208 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04005209 {
John Bauman19bac1e2014-05-06 15:23:49 -04005210 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04005211 }
5212 else ASSERT(false);
5213 }
5214
John Bauman19bac1e2014-05-06 15:23:49 -04005215 void PixelRoutine::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04005216 {
5217 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,ps.b[boolRegister.index])) != Byte(0)); // FIXME
5218
John Bauman19bac1e2014-05-06 15:23:49 -04005219 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005220 {
5221 condition = !condition;
5222 }
5223
5224 if(!labelBlock[labelIndex])
5225 {
5226 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5227 }
5228
John Bauman19bac1e2014-05-06 15:23:49 -04005229 if(callRetBlock[labelIndex].size() > 1)
5230 {
5231 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
5232 }
John Bauman89401822014-05-06 15:04:28 -04005233
John Bauman19bac1e2014-05-06 15:23:49 -04005234 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04005235
John Bauman19bac1e2014-05-06 15:23:49 -04005236 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
5237 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
5238
5239 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04005240 }
5241
John Bauman19bac1e2014-05-06 15:23:49 -04005242 void PixelRoutine::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04005243 {
5244 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
5245
John Bauman19bac1e2014-05-06 15:23:49 -04005246 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005247 {
5248 condition = ~condition;
5249 }
5250
5251 condition &= r.enableStack[r.enableIndex];
5252
5253 if(!labelBlock[labelIndex])
5254 {
5255 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5256 }
5257
John Bauman19bac1e2014-05-06 15:23:49 -04005258 if(callRetBlock[labelIndex].size() > 1)
5259 {
5260 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
5261 }
John Bauman89401822014-05-06 15:04:28 -04005262
5263 r.enableIndex++;
5264 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04005265 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04005266
John Bauman19bac1e2014-05-06 15:23:49 -04005267 Bool notAllFalse = SignMask(condition) != 0;
5268 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
5269 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04005270
5271 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04005272 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04005273 }
5274
5275 void PixelRoutine::ELSE(Registers &r)
5276 {
5277 ifDepth--;
5278
5279 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
5280 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5281
5282 if(isConditionalIf[ifDepth])
5283 {
5284 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04005285 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04005286
5287 branch(notAllFalse, falseBlock, endBlock);
5288
5289 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
5290 }
5291 else
5292 {
5293 Nucleus::createBr(endBlock);
5294 Nucleus::setInsertBlock(falseBlock);
5295 }
5296
5297 ifFalseBlock[ifDepth] = endBlock;
5298
5299 ifDepth++;
5300 }
5301
5302 void PixelRoutine::ENDIF(Registers &r)
5303 {
5304 ifDepth--;
5305
5306 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
5307
5308 Nucleus::createBr(endBlock);
5309 Nucleus::setInsertBlock(endBlock);
5310
5311 if(isConditionalIf[ifDepth])
5312 {
5313 breakDepth--;
5314 r.enableIndex--;
5315 }
5316 }
5317
John Bauman89401822014-05-06 15:04:28 -04005318 void PixelRoutine::ENDLOOP(Registers &r)
5319 {
5320 loopRepDepth--;
5321
5322 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
5323
5324 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
5325 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
5326
5327 Nucleus::createBr(testBlock);
5328 Nucleus::setInsertBlock(endBlock);
5329
5330 r.loopDepth--;
5331 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
5332 }
5333
John Bauman19bac1e2014-05-06 15:23:49 -04005334 void PixelRoutine::ENDREP(Registers &r)
5335 {
5336 loopRepDepth--;
5337
5338 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
5339 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
5340
5341 Nucleus::createBr(testBlock);
5342 Nucleus::setInsertBlock(endBlock);
5343
5344 r.loopDepth--;
5345 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
5346 }
5347
5348 void PixelRoutine::ENDWHILE(Registers &r)
5349 {
5350 loopRepDepth--;
5351
5352 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
5353 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
5354
5355 Nucleus::createBr(testBlock);
5356 Nucleus::setInsertBlock(endBlock);
5357
5358 r.enableIndex--;
5359 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
5360 whileTest = false;
5361 }
5362
John Bauman89401822014-05-06 15:04:28 -04005363 void PixelRoutine::IF(Registers &r, const Src &src)
5364 {
John Bauman19bac1e2014-05-06 15:23:49 -04005365 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04005366 {
5367 IFb(r, src);
5368 }
John Bauman19bac1e2014-05-06 15:23:49 -04005369 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04005370 {
5371 IFp(r, src);
5372 }
John Bauman19bac1e2014-05-06 15:23:49 -04005373 else
5374 {
5375 Int4 condition = As<Int4>(reg(r, src).x);
5376 IF(r, condition);
5377 }
John Bauman89401822014-05-06 15:04:28 -04005378 }
5379
5380 void PixelRoutine::IFb(Registers &r, const Src &boolRegister)
5381 {
John Bauman19bac1e2014-05-06 15:23:49 -04005382 ASSERT(ifDepth < 24 + 4);
5383
John Bauman89401822014-05-06 15:04:28 -04005384 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,ps.b[boolRegister.index])) != Byte(0)); // FIXME
5385
John Bauman19bac1e2014-05-06 15:23:49 -04005386 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005387 {
John Bauman19bac1e2014-05-06 15:23:49 -04005388 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04005389 }
5390
5391 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
5392 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
5393
5394 branch(condition, trueBlock, falseBlock);
5395
5396 isConditionalIf[ifDepth] = false;
5397 ifFalseBlock[ifDepth] = falseBlock;
5398
5399 ifDepth++;
5400 }
5401
John Bauman19bac1e2014-05-06 15:23:49 -04005402 void PixelRoutine::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04005403 {
5404 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
5405
John Bauman19bac1e2014-05-06 15:23:49 -04005406 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005407 {
5408 condition = ~condition;
5409 }
5410
John Bauman19bac1e2014-05-06 15:23:49 -04005411 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04005412 }
5413
John Bauman19bac1e2014-05-06 15:23:49 -04005414 void PixelRoutine::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04005415 {
5416 Int4 condition;
5417
5418 switch(control)
5419 {
John Bauman19bac1e2014-05-06 15:23:49 -04005420 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
5421 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
5422 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
5423 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
5424 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
5425 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04005426 default:
5427 ASSERT(false);
5428 }
5429
John Bauman19bac1e2014-05-06 15:23:49 -04005430 IF(r, condition);
5431 }
5432
5433 void PixelRoutine::IF(Registers &r, Int4 &condition)
5434 {
John Bauman89401822014-05-06 15:04:28 -04005435 condition &= r.enableStack[r.enableIndex];
5436
5437 r.enableIndex++;
5438 r.enableStack[r.enableIndex] = condition;
5439
5440 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
5441 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
5442
John Bauman19bac1e2014-05-06 15:23:49 -04005443 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04005444
5445 branch(notAllFalse, trueBlock, falseBlock);
5446
5447 isConditionalIf[ifDepth] = true;
5448 ifFalseBlock[ifDepth] = falseBlock;
5449
5450 ifDepth++;
5451 breakDepth++;
5452 }
5453
5454 void PixelRoutine::LABEL(int labelIndex)
5455 {
John Bauman19bac1e2014-05-06 15:23:49 -04005456 if(!labelBlock[labelIndex])
5457 {
5458 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5459 }
5460
John Bauman89401822014-05-06 15:04:28 -04005461 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04005462 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04005463 }
5464
5465 void PixelRoutine::LOOP(Registers &r, const Src &integerRegister)
5466 {
5467 r.loopDepth++;
5468
5469 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][0]));
5470 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][1]));
5471 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][2]));
5472
5473 // If(r.increment[r.loopDepth] == 0)
5474 // {
5475 // r.increment[r.loopDepth] = 1;
5476 // }
5477
5478 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
5479 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
5480 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5481
5482 loopRepTestBlock[loopRepDepth] = testBlock;
5483 loopRepEndBlock[loopRepDepth] = endBlock;
5484
5485 // FIXME: jump(testBlock)
5486 Nucleus::createBr(testBlock);
5487 Nucleus::setInsertBlock(testBlock);
5488
5489 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
5490 Nucleus::setInsertBlock(loopBlock);
5491
5492 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
5493
5494 loopRepDepth++;
5495 breakDepth = 0;
5496 }
5497
5498 void PixelRoutine::REP(Registers &r, const Src &integerRegister)
5499 {
5500 r.loopDepth++;
5501
5502 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][0]));
5503 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
5504
5505 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
5506 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
5507 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5508
5509 loopRepTestBlock[loopRepDepth] = testBlock;
5510 loopRepEndBlock[loopRepDepth] = endBlock;
5511
5512 // FIXME: jump(testBlock)
5513 Nucleus::createBr(testBlock);
5514 Nucleus::setInsertBlock(testBlock);
5515
5516 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
5517 Nucleus::setInsertBlock(loopBlock);
5518
5519 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
5520
5521 loopRepDepth++;
5522 breakDepth = 0;
5523 }
5524
John Bauman19bac1e2014-05-06 15:23:49 -04005525 void PixelRoutine::WHILE(Registers &r, const Src &temporaryRegister)
5526 {
5527 r.enableIndex++;
5528
5529 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
5530 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
5531 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5532
5533 loopRepTestBlock[loopRepDepth] = testBlock;
5534 loopRepEndBlock[loopRepDepth] = endBlock;
5535
5536 Int4 restoreBreak = r.enableBreak;
5537 Int4 restoreContinue = r.enableContinue;
5538
5539 // FIXME: jump(testBlock)
5540 Nucleus::createBr(testBlock);
5541 Nucleus::setInsertBlock(testBlock);
5542 r.enableContinue = restoreContinue;
5543
John Bauman66b8ab22014-05-06 15:57:45 -04005544 const Vector4f &src = reg(r, temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04005545 Int4 condition = As<Int4>(src.x);
5546 condition &= r.enableStack[r.enableIndex - 1];
5547 r.enableStack[r.enableIndex] = condition;
5548
5549 Bool notAllFalse = SignMask(condition) != 0;
5550 branch(notAllFalse, loopBlock, endBlock);
5551
5552 Nucleus::setInsertBlock(endBlock);
5553 r.enableBreak = restoreBreak;
5554
5555 Nucleus::setInsertBlock(loopBlock);
5556
5557 loopRepDepth++;
5558 breakDepth = 0;
5559 }
5560
John Bauman89401822014-05-06 15:04:28 -04005561 void PixelRoutine::RET(Registers &r)
5562 {
John Bauman19bac1e2014-05-06 15:23:49 -04005563 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04005564 {
5565 returnBlock = Nucleus::createBasicBlock();
5566 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04005567 }
5568 else
5569 {
John Bauman89401822014-05-06 15:04:28 -04005570 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04005571
John Bauman19bac1e2014-05-06 15:23:49 -04005572 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04005573 {
John Bauman19bac1e2014-05-06 15:23:49 -04005574 // FIXME: Encapsulate
5575 UInt index = r.callStack[--r.stackIndex];
5576
John Bauman66b8ab22014-05-06 15:57:45 -04005577 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04005578 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
5579
5580 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
5581 {
5582 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
5583 }
5584 }
5585 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
5586 {
5587 Nucleus::createBr(callRetBlock[currentLabel][0]);
5588 }
5589 else // Function isn't called
5590 {
5591 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04005592 }
5593
5594 Nucleus::setInsertBlock(unreachableBlock);
5595 Nucleus::createUnreachable();
5596 }
5597 }
5598
John Bauman19bac1e2014-05-06 15:23:49 -04005599 void PixelRoutine::LEAVE(Registers &r)
5600 {
5601 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
5602
5603 // FIXME: Return from function if all instances left
5604 // FIXME: Use enableLeave in other control-flow constructs
5605 }
5606
5607 void PixelRoutine::writeDestination(Registers &r, Vector4i &d, const Dst &dst)
John Bauman89401822014-05-06 15:04:28 -04005608 {
5609 switch(dst.type)
5610 {
John Bauman19bac1e2014-05-06 15:23:49 -04005611 case Shader::PARAMETER_TEMP:
John Bauman89401822014-05-06 15:04:28 -04005612 if(dst.mask & 0x1) r.ri[dst.index].x = d.x;
5613 if(dst.mask & 0x2) r.ri[dst.index].y = d.y;
5614 if(dst.mask & 0x4) r.ri[dst.index].z = d.z;
5615 if(dst.mask & 0x8) r.ri[dst.index].w = d.w;
5616 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005617 case Shader::PARAMETER_INPUT:
John Bauman89401822014-05-06 15:04:28 -04005618 if(dst.mask & 0x1) r.vi[dst.index].x = d.x;
5619 if(dst.mask & 0x2) r.vi[dst.index].y = d.y;
5620 if(dst.mask & 0x4) r.vi[dst.index].z = d.z;
5621 if(dst.mask & 0x8) r.vi[dst.index].w = d.w;
5622 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005623 case Shader::PARAMETER_CONST: ASSERT(false); break;
5624 case Shader::PARAMETER_TEXTURE:
John Bauman89401822014-05-06 15:04:28 -04005625 if(dst.mask & 0x1) r.ti[dst.index].x = d.x;
5626 if(dst.mask & 0x2) r.ti[dst.index].y = d.y;
5627 if(dst.mask & 0x4) r.ti[dst.index].z = d.z;
5628 if(dst.mask & 0x8) r.ti[dst.index].w = d.w;
5629 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005630 case Shader::PARAMETER_COLOROUT:
John Bauman89401822014-05-06 15:04:28 -04005631 if(dst.mask & 0x1) r.vi[dst.index].x = d.x;
5632 if(dst.mask & 0x2) r.vi[dst.index].y = d.y;
5633 if(dst.mask & 0x4) r.vi[dst.index].z = d.z;
5634 if(dst.mask & 0x8) r.vi[dst.index].w = d.w;
5635 break;
5636 default:
5637 ASSERT(false);
5638 }
5639 }
5640
John Bauman19bac1e2014-05-06 15:23:49 -04005641 Vector4i PixelRoutine::regi(Registers &r, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04005642 {
John Bauman19bac1e2014-05-06 15:23:49 -04005643 Vector4i *reg;
John Bauman89401822014-05-06 15:04:28 -04005644 int i = src.index;
5645
John Bauman19bac1e2014-05-06 15:23:49 -04005646 Vector4i c;
John Bauman89401822014-05-06 15:04:28 -04005647
John Bauman19bac1e2014-05-06 15:23:49 -04005648 if(src.type == Shader::PARAMETER_CONST)
John Bauman89401822014-05-06 15:04:28 -04005649 {
John Bauman19bac1e2014-05-06 15:23:49 -04005650 c.x = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][0]));
5651 c.y = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][1]));
5652 c.z = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][2]));
5653 c.w = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][3]));
John Bauman89401822014-05-06 15:04:28 -04005654 }
5655
5656 switch(src.type)
5657 {
John Bauman19bac1e2014-05-06 15:23:49 -04005658 case Shader::PARAMETER_TEMP: reg = &r.ri[i]; break;
5659 case Shader::PARAMETER_INPUT: reg = &r.vi[i]; break;
5660 case Shader::PARAMETER_CONST: reg = &c; break;
5661 case Shader::PARAMETER_TEXTURE: reg = &r.ti[i]; break;
5662 case Shader::PARAMETER_VOID: return r.ri[0]; // Dummy
5663 case Shader::PARAMETER_FLOAT4LITERAL: return r.ri[0]; // Dummy
John Bauman89401822014-05-06 15:04:28 -04005664 default:
5665 ASSERT(false);
5666 }
5667
John Bauman66b8ab22014-05-06 15:57:45 -04005668 const Short4 &x = (*reg)[(src.swizzle >> 0) & 0x3];
5669 const Short4 &y = (*reg)[(src.swizzle >> 2) & 0x3];
5670 const Short4 &z = (*reg)[(src.swizzle >> 4) & 0x3];
5671 const Short4 &w = (*reg)[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -04005672
John Bauman19bac1e2014-05-06 15:23:49 -04005673 Vector4i mod;
John Bauman89401822014-05-06 15:04:28 -04005674
5675 switch(src.modifier)
5676 {
John Bauman19bac1e2014-05-06 15:23:49 -04005677 case Shader::MODIFIER_NONE:
5678 mod.x = x;
5679 mod.y = y;
5680 mod.z = z;
5681 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -04005682 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005683 case Shader::MODIFIER_BIAS:
5684 mod.x = SubSat(x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5685 mod.y = SubSat(y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5686 mod.z = SubSat(z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5687 mod.w = SubSat(w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -04005688 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005689 case Shader::MODIFIER_BIAS_NEGATE:
5690 mod.x = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), x);
5691 mod.y = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), y);
5692 mod.z = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), z);
5693 mod.w = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), w);
John Bauman89401822014-05-06 15:04:28 -04005694 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005695 case Shader::MODIFIER_COMPLEMENT:
5696 mod.x = SubSat(Short4(0x1000), x);
5697 mod.y = SubSat(Short4(0x1000), y);
5698 mod.z = SubSat(Short4(0x1000), z);
5699 mod.w = SubSat(Short4(0x1000), w);
John Bauman89401822014-05-06 15:04:28 -04005700 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005701 case Shader::MODIFIER_NEGATE:
5702 mod.x = -x;
5703 mod.y = -y;
5704 mod.z = -z;
5705 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -04005706 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005707 case Shader::MODIFIER_X2:
5708 mod.x = AddSat(x, x);
5709 mod.y = AddSat(y, y);
5710 mod.z = AddSat(z, z);
5711 mod.w = AddSat(w, w);
John Bauman89401822014-05-06 15:04:28 -04005712 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005713 case Shader::MODIFIER_X2_NEGATE:
5714 mod.x = -AddSat(x, x);
5715 mod.y = -AddSat(y, y);
5716 mod.z = -AddSat(z, z);
5717 mod.w = -AddSat(w, w);
John Bauman89401822014-05-06 15:04:28 -04005718 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005719 case Shader::MODIFIER_SIGN:
5720 mod.x = SubSat(x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5721 mod.y = SubSat(y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5722 mod.z = SubSat(z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5723 mod.w = SubSat(w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5724 mod.x = AddSat(mod.x, mod.x);
5725 mod.y = AddSat(mod.y, mod.y);
5726 mod.z = AddSat(mod.z, mod.z);
5727 mod.w = AddSat(mod.w, mod.w);
John Bauman89401822014-05-06 15:04:28 -04005728 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005729 case Shader::MODIFIER_SIGN_NEGATE:
5730 mod.x = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), x);
5731 mod.y = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), y);
5732 mod.z = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), z);
5733 mod.w = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), w);
5734 mod.x = AddSat(mod.x, mod.x);
5735 mod.y = AddSat(mod.y, mod.y);
5736 mod.z = AddSat(mod.z, mod.z);
5737 mod.w = AddSat(mod.w, mod.w);
John Bauman89401822014-05-06 15:04:28 -04005738 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005739 case Shader::MODIFIER_DZ:
5740 mod.x = x;
5741 mod.y = y;
5742 mod.z = z;
5743 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -04005744 // Projection performed by texture sampler
5745 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005746 case Shader::MODIFIER_DW:
5747 mod.x = x;
5748 mod.y = y;
5749 mod.z = z;
5750 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -04005751 // Projection performed by texture sampler
5752 break;
5753 default:
5754 ASSERT(false);
5755 }
5756
John Bauman19bac1e2014-05-06 15:23:49 -04005757 if(src.type == Shader::PARAMETER_CONST && (src.modifier == Shader::MODIFIER_X2 || src.modifier == Shader::MODIFIER_X2_NEGATE))
John Bauman89401822014-05-06 15:04:28 -04005758 {
John Bauman19bac1e2014-05-06 15:23:49 -04005759 mod.x = Min(mod.x, Short4(0x1000)); mod.x = Max(mod.x, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
5760 mod.y = Min(mod.y, Short4(0x1000)); mod.y = Max(mod.y, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
5761 mod.z = Min(mod.z, Short4(0x1000)); mod.z = Max(mod.z, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
5762 mod.w = Min(mod.w, Short4(0x1000)); mod.w = Max(mod.w, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
John Bauman89401822014-05-06 15:04:28 -04005763 }
5764
5765 return mod;
5766 }
5767
John Bauman19bac1e2014-05-06 15:23:49 -04005768 Vector4f PixelRoutine::reg(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -04005769 {
John Bauman19bac1e2014-05-06 15:23:49 -04005770 Vector4f reg;
John Bauman89401822014-05-06 15:04:28 -04005771 int i = src.index + offset;
5772
5773 switch(src.type)
5774 {
John Bauman19bac1e2014-05-06 15:23:49 -04005775 case Shader::PARAMETER_TEMP:
5776 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman89401822014-05-06 15:04:28 -04005777 {
John Bauman19bac1e2014-05-06 15:23:49 -04005778 reg = r.rf[i];
5779 }
5780 else
5781 {
5782 Int a = relativeAddress(r, src);
5783
5784 reg = r.rf[i + a];
5785 }
5786 break;
5787 case Shader::PARAMETER_INPUT:
5788 {
5789 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -04005790 {
John Bauman19bac1e2014-05-06 15:23:49 -04005791 reg = r.vf[i];
John Bauman89401822014-05-06 15:04:28 -04005792 }
John Bauman19bac1e2014-05-06 15:23:49 -04005793 else if(src.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -04005794 {
5795 Int aL = r.aL[r.loopDepth];
5796
John Bauman19bac1e2014-05-06 15:23:49 -04005797 reg = r.vf[i + aL];
John Bauman89401822014-05-06 15:04:28 -04005798 }
John Bauman19bac1e2014-05-06 15:23:49 -04005799 else
John Bauman89401822014-05-06 15:04:28 -04005800 {
John Bauman19bac1e2014-05-06 15:23:49 -04005801 Int a = relativeAddress(r, src);
5802
5803 reg = r.vf[i + a];
John Bauman89401822014-05-06 15:04:28 -04005804 }
5805 }
5806 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005807 case Shader::PARAMETER_CONST:
5808 reg = readConstant(r, src, offset);
John Bauman89401822014-05-06 15:04:28 -04005809 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005810 case Shader::PARAMETER_TEXTURE:
5811 reg = r.vf[2 + i];
5812 break;
5813 case Shader::PARAMETER_MISCTYPE:
John Bauman89401822014-05-06 15:04:28 -04005814 if(src.index == 0) reg = r.vPos;
5815 if(src.index == 1) reg = r.vFace;
5816 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005817 case Shader::PARAMETER_SAMPLER:
5818 if(src.rel.type == Shader::PARAMETER_VOID)
5819 {
5820 reg.x = As<Float4>(Int4(i));
5821 }
5822 else if(src.rel.type == Shader::PARAMETER_TEMP)
5823 {
5824 reg.x = As<Float4>(Int4(i) + RoundInt(r.rf[src.rel.index].x));
5825 }
5826 return reg;
5827 case Shader::PARAMETER_PREDICATE: return reg; // Dummy
5828 case Shader::PARAMETER_VOID: return reg; // Dummy
5829 case Shader::PARAMETER_FLOAT4LITERAL:
5830 reg.x = Float4(src.value[0]);
5831 reg.y = Float4(src.value[1]);
5832 reg.z = Float4(src.value[2]);
5833 reg.w = Float4(src.value[3]);
5834 break;
5835 case Shader::PARAMETER_CONSTINT: return reg; // Dummy
5836 case Shader::PARAMETER_CONSTBOOL: return reg; // Dummy
5837 case Shader::PARAMETER_LOOP: return reg; // Dummy
5838 case Shader::PARAMETER_COLOROUT:
5839 reg = r.oC[i];
5840 break;
5841 case Shader::PARAMETER_DEPTHOUT:
5842 reg.x = r.oDepth;
5843 break;
John Bauman89401822014-05-06 15:04:28 -04005844 default:
5845 ASSERT(false);
5846 }
5847
John Bauman66b8ab22014-05-06 15:57:45 -04005848 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
5849 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
5850 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
5851 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -04005852
John Bauman19bac1e2014-05-06 15:23:49 -04005853 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -04005854
5855 switch(src.modifier)
5856 {
John Bauman19bac1e2014-05-06 15:23:49 -04005857 case Shader::MODIFIER_NONE:
John Bauman89401822014-05-06 15:04:28 -04005858 mod.x = x;
5859 mod.y = y;
5860 mod.z = z;
5861 mod.w = w;
5862 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005863 case Shader::MODIFIER_NEGATE:
John Bauman89401822014-05-06 15:04:28 -04005864 mod.x = -x;
5865 mod.y = -y;
5866 mod.z = -z;
5867 mod.w = -w;
5868 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005869 case Shader::MODIFIER_ABS:
John Bauman89401822014-05-06 15:04:28 -04005870 mod.x = Abs(x);
5871 mod.y = Abs(y);
5872 mod.z = Abs(z);
5873 mod.w = Abs(w);
5874 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005875 case Shader::MODIFIER_ABS_NEGATE:
John Bauman89401822014-05-06 15:04:28 -04005876 mod.x = -Abs(x);
5877 mod.y = -Abs(y);
5878 mod.z = -Abs(z);
5879 mod.w = -Abs(w);
5880 break;
John Bauman66b8ab22014-05-06 15:57:45 -04005881 case Shader::MODIFIER_NOT:
5882 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
5883 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
5884 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
5885 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
5886 break;
John Bauman89401822014-05-06 15:04:28 -04005887 default:
5888 ASSERT(false);
5889 }
5890
5891 return mod;
5892 }
5893
John Bauman19bac1e2014-05-06 15:23:49 -04005894 Vector4f PixelRoutine::readConstant(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -04005895 {
John Bauman19bac1e2014-05-06 15:23:49 -04005896 Vector4f c;
5897
5898 int i = src.index + offset;
5899
5900 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
5901 {
5902 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[i]));
5903
5904 c.x = c.x.xxxx;
5905 c.y = c.y.yyyy;
5906 c.z = c.z.zzzz;
5907 c.w = c.w.wwww;
5908
5909 if(localShaderConstants) // Constant may be known at compile time
5910 {
Alexis Hetu903e0252014-11-25 14:25:32 -05005911 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -04005912 {
5913 const Shader::Instruction &instruction = *shader->getInstruction(j);
5914
5915 if(instruction.opcode == Shader::OPCODE_DEF)
5916 {
5917 if(instruction.dst.index == i)
5918 {
5919 c.x = Float4(instruction.src[0].value[0]);
5920 c.y = Float4(instruction.src[0].value[1]);
5921 c.z = Float4(instruction.src[0].value[2]);
5922 c.w = Float4(instruction.src[0].value[3]);
5923
5924 break;
5925 }
5926 }
5927 }
5928 }
5929 }
5930 else if(src.rel.type == Shader::PARAMETER_LOOP)
5931 {
5932 Int loopCounter = r.aL[r.loopDepth];
5933
5934 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[i]) + loopCounter * 16);
5935
5936 c.x = c.x.xxxx;
5937 c.y = c.y.yyyy;
5938 c.z = c.z.zzzz;
5939 c.w = c.w.wwww;
5940 }
5941 else
5942 {
5943 Int a = relativeAddress(r, src);
5944
5945 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[i]) + a * 16);
5946
5947 c.x = c.x.xxxx;
5948 c.y = c.y.yyyy;
5949 c.z = c.z.zzzz;
5950 c.w = c.w.wwww;
5951 }
5952
5953 return c;
John Bauman89401822014-05-06 15:04:28 -04005954 }
5955
John Bauman19bac1e2014-05-06 15:23:49 -04005956 Int PixelRoutine::relativeAddress(Registers &r, const Shader::Parameter &var)
John Bauman89401822014-05-06 15:04:28 -04005957 {
John Bauman19bac1e2014-05-06 15:23:49 -04005958 ASSERT(var.rel.deterministic);
5959
5960 if(var.rel.type == Shader::PARAMETER_TEMP)
5961 {
5962 return RoundInt(Extract(r.rf[var.rel.index].x, 0)) * var.rel.scale;
5963 }
5964 else if(var.rel.type == Shader::PARAMETER_INPUT)
5965 {
5966 return RoundInt(Extract(r.vf[var.rel.index].x, 0)) * var.rel.scale;
5967 }
5968 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
5969 {
5970 return RoundInt(Extract(r.oC[var.rel.index].x, 0)) * var.rel.scale;
5971 }
5972 else if(var.rel.type == Shader::PARAMETER_CONST)
5973 {
Nicolas Capensb5e7a2a2014-05-06 16:38:19 -04005974 RValue<Float4> c = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[var.rel.index]));
John Bauman19bac1e2014-05-06 15:23:49 -04005975
5976 return RoundInt(Extract(c, 0)) * var.rel.scale;
5977 }
5978 else ASSERT(false);
5979
5980 return 0;
5981 }
5982
5983 Int4 PixelRoutine::enableMask(Registers &r, const Shader::Instruction *instruction)
5984 {
5985 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
John Baumand4ae8632014-05-06 16:18:33 -04005986
5987 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -04005988 {
John Baumand4ae8632014-05-06 16:18:33 -04005989 if(shader->containsBreakInstruction() && instruction->analysisBreak)
5990 {
5991 enable &= r.enableBreak;
5992 }
John Bauman19bac1e2014-05-06 15:23:49 -04005993
John Baumand4ae8632014-05-06 16:18:33 -04005994 if(shader->containsContinueInstruction() && instruction->analysisContinue)
5995 {
5996 enable &= r.enableContinue;
5997 }
John Bauman19bac1e2014-05-06 15:23:49 -04005998
John Baumand4ae8632014-05-06 16:18:33 -04005999 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
6000 {
6001 enable &= r.enableLeave;
6002 }
John Bauman19bac1e2014-05-06 15:23:49 -04006003 }
6004
6005 return enable;
6006 }
6007
6008 bool PixelRoutine::colorUsed()
6009 {
6010 return state.colorWriteMask || state.alphaTestActive() || state.shaderContainsKill;
6011 }
6012
6013 unsigned short PixelRoutine::shaderVersion() const
6014 {
6015 return shader ? shader->getVersion() : 0x0000;
6016 }
6017
6018 bool PixelRoutine::interpolateZ() const
6019 {
6020 return state.depthTestActive || state.pixelFogActive() || (shader && shader->vPosDeclared && fullPixelPositionRegister);
6021 }
6022
6023 bool PixelRoutine::interpolateW() const
6024 {
6025 return state.perspective || (shader && shader->vPosDeclared && fullPixelPositionRegister);
John Bauman89401822014-05-06 15:04:28 -04006026 }
6027}