blob: d9cab28b857c89adef513ec0cc396e1dafd0f38a [file] [log] [blame]
John Bauman89401822014-05-06 15:04:28 -04001// SwiftShader Software Renderer
2//
John Bauman66b8ab22014-05-06 15:57:45 -04003// Copyright(c) 2005-2013 TransGaming Inc.
John Bauman89401822014-05-06 15:04:28 -04004//
5// All rights reserved. No part of this software may be copied, distributed, transmitted,
6// transcribed, stored in a retrieval system, translated into any human or computer
7// language by any means, or disclosed to third parties without the explicit written
8// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9// or implied, including but not limited to any patent rights, are granted to you.
10//
11
12#include "PixelRoutine.hpp"
13
14#include "Renderer.hpp"
John Bauman89401822014-05-06 15:04:28 -040015#include "QuadRasterizer.hpp"
16#include "Surface.hpp"
17#include "Primitive.hpp"
18#include "CPUID.hpp"
19#include "SamplerCore.hpp"
20#include "Constants.hpp"
21#include "Debug.hpp"
22
John Bauman89401822014-05-06 15:04:28 -040023namespace sw
24{
25 extern bool complementaryDepthBuffer;
26 extern bool postBlendSRGB;
27 extern bool exactColorRounding;
John Bauman19bac1e2014-05-06 15:23:49 -040028 extern bool booleanFaceRegister;
29 extern bool halfIntegerCoordinates; // Pixel centers are not at integer coordinates
30 extern bool fullPixelPositionRegister;
John Bauman89401822014-05-06 15:04:28 -040031
John Bauman19bac1e2014-05-06 15:23:49 -040032 PixelRoutine::PixelRoutine(const PixelProcessor::State &state, const PixelShader *shader) : Rasterizer(state), shader(shader)
John Bauman89401822014-05-06 15:04:28 -040033 {
34 perturbate = false;
35 luminance = false;
36 previousScaling = false;
37
John Bauman89401822014-05-06 15:04:28 -040038 ifDepth = 0;
39 loopRepDepth = 0;
40 breakDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040041 currentLabel = -1;
42 whileTest = false;
John Bauman89401822014-05-06 15:04:28 -040043
44 for(int i = 0; i < 2048; i++)
45 {
46 labelBlock[i] = 0;
47 }
48 }
49
50 PixelRoutine::~PixelRoutine()
51 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040052 for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040053 {
54 delete sampler[i];
55 }
56 }
57
58 void PixelRoutine::quad(Registers &r, Pointer<Byte> cBuffer[4], Pointer<Byte> &zBuffer, Pointer<Byte> &sBuffer, Int cMask[4], Int &x, Int &y)
59 {
60 #if PERF_PROFILE
61 Long pipeTime = Ticks();
62 #endif
63
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040064 for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040065 {
66 sampler[i] = new SamplerCore(r.constants, state.sampler[i]);
67 }
68
69 const bool earlyDepthTest = !state.depthOverride && !state.alphaTestActive();
John Bauman19bac1e2014-05-06 15:23:49 -040070 const bool integerPipeline = shaderVersion() <= 0x0104;
John Bauman89401822014-05-06 15:04:28 -040071
72 Int zMask[4]; // Depth mask
73 Int sMask[4]; // Stencil mask
74
75 for(unsigned int q = 0; q < state.multiSample; q++)
76 {
77 zMask[q] = cMask[q];
78 sMask[q] = cMask[q];
79 }
80
81 for(unsigned int q = 0; q < state.multiSample; q++)
82 {
83 stencilTest(r, sBuffer, q, x, sMask[q], cMask[q]);
84 }
85
86 Float4 f;
87
John Bauman89401822014-05-06 15:04:28 -040088 Float4 (&z)[4] = r.z;
John Bauman19bac1e2014-05-06 15:23:49 -040089 Float4 &w = r.w;
John Bauman89401822014-05-06 15:04:28 -040090 Float4 &rhw = r.rhw;
91 Float4 rhwCentroid;
92
93 Float4 xxxx = Float4(Float(x)) + *Pointer<Float4>(r.primitive + OFFSET(Primitive,xQuad), 16);
John Bauman89401822014-05-06 15:04:28 -040094
John Bauman19bac1e2014-05-06 15:23:49 -040095 if(interpolateZ())
John Bauman89401822014-05-06 15:04:28 -040096 {
97 for(unsigned int q = 0; q < state.multiSample; q++)
98 {
99 Float4 x = xxxx;
100
101 if(state.multiSample > 1)
102 {
103 x -= *Pointer<Float4>(r.constants + OFFSET(Constants,X) + q * sizeof(float4));
104 }
105
106 z[q] = interpolate(x, r.Dz[q], z[q], r.primitive + OFFSET(Primitive,z), false, false);
107 }
108 }
109
110 Bool depthPass = false;
111
112 if(earlyDepthTest)
113 {
114 for(unsigned int q = 0; q < state.multiSample; q++)
115 {
116 depthPass = depthPass || depthTest(r, zBuffer, q, x, z[q], sMask[q], zMask[q], cMask[q]);
117 }
118 }
119
120 If(depthPass || Bool(!earlyDepthTest))
121 {
122 #if PERF_PROFILE
123 Long interpTime = Ticks();
124 #endif
125
Nicolas Capens66be2452015-01-27 14:58:57 -0500126 Float4 yyyy = Float4(Float(y)) + *Pointer<Float4>(r.primitive + OFFSET(Primitive,yQuad), 16);
Nicolas Capenscbefe532014-10-16 00:16:01 -0400127
John Bauman89401822014-05-06 15:04:28 -0400128 // Centroid locations
129 Float4 XXXX = Float4(0.0f);
130 Float4 YYYY = Float4(0.0f);
131
132 if(state.centroid)
133 {
134 Float4 WWWW(1.0e-9f);
135
136 for(unsigned int q = 0; q < state.multiSample; q++)
137 {
138 XXXX += *Pointer<Float4>(r.constants + OFFSET(Constants,sampleX[q]) + 16 * cMask[q]);
139 YYYY += *Pointer<Float4>(r.constants + OFFSET(Constants,sampleY[q]) + 16 * cMask[q]);
140 WWWW += *Pointer<Float4>(r.constants + OFFSET(Constants,weight) + 16 * cMask[q]);
141 }
142
143 WWWW = Rcp_pp(WWWW);
144 XXXX *= WWWW;
145 YYYY *= WWWW;
146
147 XXXX += xxxx;
148 YYYY += yyyy;
149 }
150
John Bauman19bac1e2014-05-06 15:23:49 -0400151 if(interpolateW())
John Bauman89401822014-05-06 15:04:28 -0400152 {
John Bauman19bac1e2014-05-06 15:23:49 -0400153 w = interpolate(xxxx, r.Dw, rhw, r.primitive + OFFSET(Primitive,w), false, false);
154 rhw = reciprocal(w);
John Bauman89401822014-05-06 15:04:28 -0400155
156 if(state.centroid)
157 {
158 rhwCentroid = reciprocal(interpolateCentroid(XXXX, YYYY, rhwCentroid, r.primitive + OFFSET(Primitive,w), false, false));
159 }
160 }
161
162 for(int interpolant = 0; interpolant < 10; interpolant++)
163 {
164 for(int component = 0; component < 4; component++)
165 {
John Bauman89401822014-05-06 15:04:28 -0400166 if(state.interpolant[interpolant].component & (1 << component))
167 {
168 if(!state.interpolant[interpolant].centroid)
169 {
John Bauman19bac1e2014-05-06 15:23:49 -0400170 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 -0400171 }
172 else
173 {
John Bauman19bac1e2014-05-06 15:23:49 -0400174 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 -0400175 }
176 }
177 }
178
179 Float4 rcp;
180
181 switch(state.interpolant[interpolant].project)
182 {
183 case 0:
184 break;
185 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400186 rcp = reciprocal(r.vf[interpolant].y);
187 r.vf[interpolant].x = r.vf[interpolant].x * rcp;
John Bauman89401822014-05-06 15:04:28 -0400188 break;
189 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400190 rcp = reciprocal(r.vf[interpolant].z);
191 r.vf[interpolant].x = r.vf[interpolant].x * rcp;
192 r.vf[interpolant].y = r.vf[interpolant].y * rcp;
John Bauman89401822014-05-06 15:04:28 -0400193 break;
194 case 3:
John Bauman19bac1e2014-05-06 15:23:49 -0400195 rcp = reciprocal(r.vf[interpolant].w);
196 r.vf[interpolant].x = r.vf[interpolant].x * rcp;
197 r.vf[interpolant].y = r.vf[interpolant].y * rcp;
198 r.vf[interpolant].z = r.vf[interpolant].z * rcp;
John Bauman89401822014-05-06 15:04:28 -0400199 break;
200 }
201 }
202
203 if(state.fog.component)
204 {
205 f = interpolate(xxxx, r.Df, rhw, r.primitive + OFFSET(Primitive,f), state.fog.flat & 0x01, state.perspective);
206 }
207
208 if(integerPipeline)
209 {
Nicolas Capenscbefe532014-10-16 00:16:01 -0400210 if(state.color[0].component & 0x1) r.diffuse.x = convertFixed12(r.vf[0].x); else r.diffuse.x = Short4(0x1000);
211 if(state.color[0].component & 0x2) r.diffuse.y = convertFixed12(r.vf[0].y); else r.diffuse.y = Short4(0x1000);
212 if(state.color[0].component & 0x4) r.diffuse.z = convertFixed12(r.vf[0].z); else r.diffuse.z = Short4(0x1000);
213 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 -0400214
Nicolas Capenscbefe532014-10-16 00:16:01 -0400215 if(state.color[1].component & 0x1) r.specular.x = convertFixed12(r.vf[1].x); else r.specular.x = Short4(0x0000, 0x0000, 0x0000, 0x0000);
216 if(state.color[1].component & 0x2) r.specular.y = convertFixed12(r.vf[1].y); else r.specular.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
217 if(state.color[1].component & 0x4) r.specular.z = convertFixed12(r.vf[1].z); else r.specular.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
218 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 -0400219 }
John Bauman19bac1e2014-05-06 15:23:49 -0400220 else if(shaderVersion() >= 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400221 {
John Bauman19bac1e2014-05-06 15:23:49 -0400222 if(shader->vPosDeclared)
John Bauman89401822014-05-06 15:04:28 -0400223 {
John Bauman19bac1e2014-05-06 15:23:49 -0400224 if(!halfIntegerCoordinates)
225 {
226 r.vPos.x = Float4(Float(x)) + Float4(0, 1, 0, 1);
227 r.vPos.y = Float4(Float(y)) + Float4(0, 0, 1, 1);
228 }
229 else
230 {
231 r.vPos.x = Float4(Float(x)) + Float4(0.5f, 1.5f, 0.5f, 1.5f);
232 r.vPos.y = Float4(Float(y)) + Float4(0.5f, 0.5f, 1.5f, 1.5f);
233 }
234
235 if(fullPixelPositionRegister)
236 {
237 r.vPos.z = z[0]; // FIXME: Centroid?
238 r.vPos.w = w; // FIXME: Centroid?
239 }
John Bauman89401822014-05-06 15:04:28 -0400240 }
241
John Bauman19bac1e2014-05-06 15:23:49 -0400242 if(shader->vFaceDeclared)
John Bauman89401822014-05-06 15:04:28 -0400243 {
244 Float4 area = *Pointer<Float>(r.primitive + OFFSET(Primitive,area));
John Bauman66b8ab22014-05-06 15:57:45 -0400245 Float4 face = booleanFaceRegister ? Float4(As<Float4>(CmpNLT(area, Float4(0.0f)))) : area;
John Bauman19bac1e2014-05-06 15:23:49 -0400246
247 r.vFace.x = face;
248 r.vFace.y = face;
249 r.vFace.z = face;
250 r.vFace.w = face;
John Bauman89401822014-05-06 15:04:28 -0400251 }
252 }
253
254 #if PERF_PROFILE
255 r.cycles[PERF_INTERP] += Ticks() - interpTime;
256 #endif
257
258 Bool alphaPass = true;
259
260 if(colorUsed())
261 {
262 #if PERF_PROFILE
263 Long shaderTime = Ticks();
264 #endif
265
John Bauman19bac1e2014-05-06 15:23:49 -0400266 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400267 {
John Bauman19bac1e2014-05-06 15:23:49 -0400268 // shader->print("PixelShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -0400269
John Bauman19bac1e2014-05-06 15:23:49 -0400270 if(shader->getVersion() <= 0x0104)
John Bauman89401822014-05-06 15:04:28 -0400271 {
272 ps_1_x(r, cMask);
273 }
274 else
275 {
276 ps_2_x(r, cMask);
277 }
278 }
279 else
280 {
Nicolas Capenscbefe532014-10-16 00:16:01 -0400281 r.current = r.diffuse;
Alexis Hetu96517182015-04-15 10:30:23 -0400282 Vector4s temp(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -0400283
284 for(int stage = 0; stage < 8; stage++)
285 {
286 if(state.textureStage[stage].stageOperation == TextureStage::STAGE_DISABLE)
287 {
288 break;
289 }
290
Alexis Hetu96517182015-04-15 10:30:23 -0400291 Vector4s texture;
John Bauman89401822014-05-06 15:04:28 -0400292
293 if(state.textureStage[stage].usesTexture)
294 {
295 sampleTexture(r, texture, stage, stage);
296 }
297
Nicolas Capenscbefe532014-10-16 00:16:01 -0400298 blendTexture(r, temp, texture, stage);
John Bauman89401822014-05-06 15:04:28 -0400299 }
300
Nicolas Capenscbefe532014-10-16 00:16:01 -0400301 specularPixel(r.current, r.specular);
John Bauman89401822014-05-06 15:04:28 -0400302 }
303
304 #if PERF_PROFILE
305 r.cycles[PERF_SHADER] += Ticks() - shaderTime;
306 #endif
307
308 if(integerPipeline)
309 {
Nicolas Capenscbefe532014-10-16 00:16:01 -0400310 r.current.x = Min(r.current.x, Short4(0x0FFF, 0x0FFF, 0x0FFF, 0x0FFF)); r.current.x = Max(r.current.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));
311 r.current.y = Min(r.current.y, Short4(0x0FFF, 0x0FFF, 0x0FFF, 0x0FFF)); r.current.y = Max(r.current.y, Short4(0x0000, 0x0000, 0x0000, 0x0000));
312 r.current.z = Min(r.current.z, Short4(0x0FFF, 0x0FFF, 0x0FFF, 0x0FFF)); r.current.z = Max(r.current.z, Short4(0x0000, 0x0000, 0x0000, 0x0000));
313 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 -0400314
Nicolas Capenscbefe532014-10-16 00:16:01 -0400315 alphaPass = alphaTest(r, cMask, r.current);
John Bauman89401822014-05-06 15:04:28 -0400316 }
317 else
318 {
319 clampColor(r.oC);
320
321 alphaPass = alphaTest(r, cMask, r.oC[0]);
322 }
323
John Bauman19bac1e2014-05-06 15:23:49 -0400324 if((shader && shader->containsKill()) || state.alphaTestActive())
John Bauman89401822014-05-06 15:04:28 -0400325 {
326 for(unsigned int q = 0; q < state.multiSample; q++)
327 {
328 zMask[q] &= cMask[q];
329 sMask[q] &= cMask[q];
330 }
331 }
332 }
333
334 If(alphaPass)
335 {
336 if(!earlyDepthTest)
337 {
338 for(unsigned int q = 0; q < state.multiSample; q++)
339 {
340 depthPass = depthPass || depthTest(r, zBuffer, q, x, z[q], sMask[q], zMask[q], cMask[q]);
341 }
342 }
343
344 #if PERF_PROFILE
345 Long ropTime = Ticks();
346 #endif
347
348 If(depthPass || Bool(earlyDepthTest))
349 {
350 for(unsigned int q = 0; q < state.multiSample; q++)
351 {
352 if(state.multiSampleMask & (1 << q))
353 {
354 writeDepth(r, zBuffer, q, x, z[q], zMask[q]);
355
356 if(state.occlusionEnabled)
357 {
358 r.occlusion += *Pointer<UInt>(r.constants + OFFSET(Constants,occlusionCount) + 4 * (zMask[q] & sMask[q]));
359 }
360 }
361 }
362
363 if(colorUsed())
364 {
365 #if PERF_PROFILE
John Bauman66b8ab22014-05-06 15:57:45 -0400366 AddAtomic(Pointer<Long>(&profiler.ropOperations), 4);
John Bauman89401822014-05-06 15:04:28 -0400367 #endif
368
369 if(integerPipeline)
370 {
Nicolas Capenscbefe532014-10-16 00:16:01 -0400371 rasterOperation(r.current, r, f, cBuffer[0], x, sMask, zMask, cMask);
John Bauman89401822014-05-06 15:04:28 -0400372 }
373 else
374 {
375 rasterOperation(r.oC, r, f, cBuffer, x, sMask, zMask, cMask);
376 }
377 }
378 }
379
380 #if PERF_PROFILE
381 r.cycles[PERF_ROP] += Ticks() - ropTime;
382 #endif
383 }
384 }
385
386 for(unsigned int q = 0; q < state.multiSample; q++)
387 {
388 if(state.multiSampleMask & (1 << q))
389 {
390 writeStencil(r, sBuffer, q, x, sMask[q], zMask[q], cMask[q]);
391 }
392 }
393
394 #if PERF_PROFILE
395 r.cycles[PERF_PIPE] += Ticks() - pipeTime;
396 #endif
397 }
398
399 Float4 PixelRoutine::interpolate(Float4 &x, Float4 &D, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective)
400 {
401 Float4 interpolant = D;
402
403 if(!flat)
404 {
405 interpolant += x * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,A), 16);
406
407 if(perspective)
408 {
409 interpolant *= rhw;
410 }
411 }
412
413 return interpolant;
414 }
415
416 Float4 PixelRoutine::interpolateCentroid(Float4 &x, Float4 &y, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective)
417 {
418 Float4 interpolant = *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,C), 16);
419
420 if(!flat)
421 {
422 interpolant += x * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,A), 16) +
423 y * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,B), 16);
424
425 if(perspective)
426 {
427 interpolant *= rhw;
428 }
429 }
430
431 return interpolant;
432 }
433
434 void PixelRoutine::stencilTest(Registers &r, Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &cMask)
435 {
436 if(!state.stencilActive)
437 {
438 return;
439 }
440
441 // (StencilRef & StencilMask) CompFunc (StencilBufferValue & StencilMask)
442
443 Pointer<Byte> buffer = sBuffer + 2 * x;
444
445 if(q > 0)
446 {
447 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,stencilSliceB));
448 }
449
450 Byte8 value = As<Byte8>(Long1(*Pointer<UInt>(buffer)));
451 Byte8 valueCCW = value;
452
453 if(!state.noStencilMask)
454 {
455 value &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[0].testMaskQ));
456 }
457
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400458 stencilTest(r, value, state.stencilCompareMode, false);
John Bauman89401822014-05-06 15:04:28 -0400459
460 if(state.twoSidedStencil)
461 {
462 if(!state.noStencilMaskCCW)
463 {
464 valueCCW &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[1].testMaskQ));
465 }
466
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400467 stencilTest(r, valueCCW, state.stencilCompareModeCCW, true);
John Bauman89401822014-05-06 15:04:28 -0400468
469 value &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,clockwiseMask));
470 valueCCW &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,invClockwiseMask));
471 value |= valueCCW;
472 }
473
474 sMask = SignMask(value) & cMask;
475 }
476
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400477 void PixelRoutine::stencilTest(Registers &r, Byte8 &value, StencilCompareMode stencilCompareMode, bool CCW)
John Bauman89401822014-05-06 15:04:28 -0400478 {
479 Byte8 equal;
480
481 switch(stencilCompareMode)
482 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400483 case STENCIL_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400484 value = Byte8(0xFFFFFFFFFFFFFFFF);
485 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400486 case STENCIL_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400487 value = Byte8(0x0000000000000000);
488 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400489 case STENCIL_LESS: // a < b ~ b > a
John Bauman89401822014-05-06 15:04:28 -0400490 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
491 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
492 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400493 case STENCIL_EQUAL:
John Bauman89401822014-05-06 15:04:28 -0400494 value = CmpEQ(value, *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
495 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400496 case STENCIL_NOTEQUAL: // a != b ~ !(a == b)
John Bauman89401822014-05-06 15:04:28 -0400497 value = CmpEQ(value, *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
498 value ^= Byte8(0xFFFFFFFFFFFFFFFF);
499 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400500 case STENCIL_LESSEQUAL: // a <= b ~ (b > a) || (a == b)
John Bauman89401822014-05-06 15:04:28 -0400501 equal = value;
502 equal = CmpEQ(equal, *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
503 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
504 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
505 value |= equal;
506 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400507 case STENCIL_GREATER: // a > b
John Bauman89401822014-05-06 15:04:28 -0400508 equal = *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ));
509 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
510 equal = CmpGT(As<SByte8>(equal), As<SByte8>(value));
511 value = equal;
512 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400513 case STENCIL_GREATEREQUAL: // a >= b ~ !(a < b) ~ !(b > a)
John Bauman89401822014-05-06 15:04:28 -0400514 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
515 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
516 value ^= Byte8(0xFFFFFFFFFFFFFFFF);
517 break;
518 default:
519 ASSERT(false);
520 }
521 }
522
523 Bool PixelRoutine::depthTest(Registers &r, Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &sMask, Int &zMask, Int &cMask)
524 {
525 if(!state.depthTestActive)
526 {
527 return true;
528 }
529
530 Float4 Z = z;
531
John Bauman19bac1e2014-05-06 15:23:49 -0400532 if(shader && shader->depthOverride())
John Bauman89401822014-05-06 15:04:28 -0400533 {
534 if(complementaryDepthBuffer)
535 {
John Bauman19bac1e2014-05-06 15:23:49 -0400536 Z = Float4(1.0f) - r.oDepth;
John Bauman89401822014-05-06 15:04:28 -0400537 }
538 else
539 {
540 Z = r.oDepth;
541 }
542 }
543
544 Pointer<Byte> buffer;
545 Int pitch;
546
547 if(!state.quadLayoutDepthBuffer)
548 {
549 buffer = zBuffer + 4 * x;
550 pitch = *Pointer<Int>(r.data + OFFSET(DrawData,depthPitchB));
551 }
552 else
553 {
554 buffer = zBuffer + 8 * x;
555 }
556
557 if(q > 0)
558 {
559 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,depthSliceB));
560 }
561
562 Float4 zValue;
563
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400564 if(state.depthCompareMode != DEPTH_NEVER || (state.depthCompareMode != DEPTH_ALWAYS && !state.depthWriteEnable))
John Bauman89401822014-05-06 15:04:28 -0400565 {
566 if(!state.quadLayoutDepthBuffer)
567 {
568 // FIXME: Properly optimizes?
569 zValue.xy = *Pointer<Float4>(buffer);
570 zValue.zw = *Pointer<Float4>(buffer + pitch - 8);
571 }
572 else
573 {
574 zValue = *Pointer<Float4>(buffer, 16);
575 }
576 }
577
578 Int4 zTest;
579
580 switch(state.depthCompareMode)
581 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400582 case DEPTH_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400583 // Optimized
584 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400585 case DEPTH_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400586 // Optimized
587 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400588 case DEPTH_EQUAL:
John Bauman89401822014-05-06 15:04:28 -0400589 zTest = CmpEQ(zValue, Z);
590 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400591 case DEPTH_NOTEQUAL:
John Bauman89401822014-05-06 15:04:28 -0400592 zTest = CmpNEQ(zValue, Z);
593 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400594 case DEPTH_LESS:
John Bauman89401822014-05-06 15:04:28 -0400595 if(complementaryDepthBuffer)
596 {
597 zTest = CmpLT(zValue, Z);
598 }
599 else
600 {
601 zTest = CmpNLE(zValue, Z);
602 }
603 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400604 case DEPTH_GREATEREQUAL:
John Bauman89401822014-05-06 15:04:28 -0400605 if(complementaryDepthBuffer)
606 {
607 zTest = CmpNLT(zValue, Z);
608 }
609 else
610 {
611 zTest = CmpLE(zValue, Z);
612 }
613 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400614 case DEPTH_LESSEQUAL:
John Bauman89401822014-05-06 15:04:28 -0400615 if(complementaryDepthBuffer)
616 {
617 zTest = CmpLE(zValue, Z);
618 }
619 else
620 {
621 zTest = CmpNLT(zValue, Z);
622 }
623 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400624 case DEPTH_GREATER:
John Bauman89401822014-05-06 15:04:28 -0400625 if(complementaryDepthBuffer)
626 {
627 zTest = CmpNLE(zValue, Z);
628 }
629 else
630 {
631 zTest = CmpLT(zValue, Z);
632 }
633 break;
634 default:
635 ASSERT(false);
636 }
637
638 switch(state.depthCompareMode)
639 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400640 case DEPTH_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400641 zMask = cMask;
642 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400643 case DEPTH_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400644 zMask = 0x0;
645 break;
646 default:
647 zMask = SignMask(zTest) & cMask;
648 break;
649 }
650
651 if(state.stencilActive)
652 {
653 zMask &= sMask;
654 }
655
656 return zMask != 0;
657 }
658
Alexis Hetu96517182015-04-15 10:30:23 -0400659 void PixelRoutine::blendTexture(Registers &r, Vector4s &temp, Vector4s &texture, int stage)
John Bauman89401822014-05-06 15:04:28 -0400660 {
Alexis Hetu96517182015-04-15 10:30:23 -0400661 Vector4s *arg1;
662 Vector4s *arg2;
663 Vector4s *arg3;
664 Vector4s res;
John Bauman89401822014-05-06 15:04:28 -0400665
Alexis Hetu96517182015-04-15 10:30:23 -0400666 Vector4s constant;
667 Vector4s tfactor;
John Bauman89401822014-05-06 15:04:28 -0400668
669 const TextureStage::State &textureStage = state.textureStage[stage];
670
671 if(textureStage.firstArgument == TextureStage::SOURCE_CONSTANT ||
672 textureStage.firstArgumentAlpha == TextureStage::SOURCE_CONSTANT ||
673 textureStage.secondArgument == TextureStage::SOURCE_CONSTANT ||
674 textureStage.secondArgumentAlpha == TextureStage::SOURCE_CONSTANT ||
675 textureStage.thirdArgument == TextureStage::SOURCE_CONSTANT ||
676 textureStage.thirdArgumentAlpha == TextureStage::SOURCE_CONSTANT)
677 {
John Bauman19bac1e2014-05-06 15:23:49 -0400678 constant.x = *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].constantColor4[0]));
679 constant.y = *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].constantColor4[1]));
680 constant.z = *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].constantColor4[2]));
681 constant.w = *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].constantColor4[3]));
John Bauman89401822014-05-06 15:04:28 -0400682 }
683
684 if(textureStage.firstArgument == TextureStage::SOURCE_TFACTOR ||
685 textureStage.firstArgumentAlpha == TextureStage::SOURCE_TFACTOR ||
686 textureStage.secondArgument == TextureStage::SOURCE_TFACTOR ||
687 textureStage.secondArgumentAlpha == TextureStage::SOURCE_TFACTOR ||
688 textureStage.thirdArgument == TextureStage::SOURCE_TFACTOR ||
689 textureStage.thirdArgumentAlpha == TextureStage::SOURCE_TFACTOR)
690 {
John Bauman19bac1e2014-05-06 15:23:49 -0400691 tfactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[0]));
692 tfactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[1]));
693 tfactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[2]));
694 tfactor.w = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[3]));
John Bauman89401822014-05-06 15:04:28 -0400695 }
696
697 // Premodulate
698 if(stage > 0 && textureStage.usesTexture)
699 {
700 if(state.textureStage[stage - 1].stageOperation == TextureStage::STAGE_PREMODULATE)
701 {
Nicolas Capenscbefe532014-10-16 00:16:01 -0400702 r.current.x = MulHigh(r.current.x, texture.x) << 4;
703 r.current.y = MulHigh(r.current.y, texture.y) << 4;
704 r.current.z = MulHigh(r.current.z, texture.z) << 4;
John Bauman89401822014-05-06 15:04:28 -0400705 }
706
707 if(state.textureStage[stage - 1].stageOperationAlpha == TextureStage::STAGE_PREMODULATE)
708 {
Nicolas Capenscbefe532014-10-16 00:16:01 -0400709 r.current.w = MulHigh(r.current.w, texture.w) << 4;
John Bauman89401822014-05-06 15:04:28 -0400710 }
711 }
712
713 if(luminance)
714 {
John Bauman19bac1e2014-05-06 15:23:49 -0400715 texture.x = MulHigh(texture.x, r.L) << 4;
716 texture.y = MulHigh(texture.y, r.L) << 4;
717 texture.z = MulHigh(texture.z, r.L) << 4;
John Bauman89401822014-05-06 15:04:28 -0400718
719 luminance = false;
720 }
721
722 switch(textureStage.firstArgument)
723 {
724 case TextureStage::SOURCE_TEXTURE: arg1 = &texture; break;
725 case TextureStage::SOURCE_CONSTANT: arg1 = &constant; break;
Nicolas Capenscbefe532014-10-16 00:16:01 -0400726 case TextureStage::SOURCE_CURRENT: arg1 = &r.current; break;
John Bauman89401822014-05-06 15:04:28 -0400727 case TextureStage::SOURCE_DIFFUSE: arg1 = &r.diffuse; break;
728 case TextureStage::SOURCE_SPECULAR: arg1 = &r.specular; break;
729 case TextureStage::SOURCE_TEMP: arg1 = &temp; break;
730 case TextureStage::SOURCE_TFACTOR: arg1 = &tfactor; break;
731 default:
732 ASSERT(false);
733 }
734
735 switch(textureStage.secondArgument)
736 {
737 case TextureStage::SOURCE_TEXTURE: arg2 = &texture; break;
738 case TextureStage::SOURCE_CONSTANT: arg2 = &constant; break;
Nicolas Capenscbefe532014-10-16 00:16:01 -0400739 case TextureStage::SOURCE_CURRENT: arg2 = &r.current; break;
John Bauman89401822014-05-06 15:04:28 -0400740 case TextureStage::SOURCE_DIFFUSE: arg2 = &r.diffuse; break;
741 case TextureStage::SOURCE_SPECULAR: arg2 = &r.specular; break;
742 case TextureStage::SOURCE_TEMP: arg2 = &temp; break;
743 case TextureStage::SOURCE_TFACTOR: arg2 = &tfactor; break;
744 default:
745 ASSERT(false);
746 }
747
748 switch(textureStage.thirdArgument)
749 {
750 case TextureStage::SOURCE_TEXTURE: arg3 = &texture; break;
751 case TextureStage::SOURCE_CONSTANT: arg3 = &constant; break;
Nicolas Capenscbefe532014-10-16 00:16:01 -0400752 case TextureStage::SOURCE_CURRENT: arg3 = &r.current; break;
John Bauman89401822014-05-06 15:04:28 -0400753 case TextureStage::SOURCE_DIFFUSE: arg3 = &r.diffuse; break;
754 case TextureStage::SOURCE_SPECULAR: arg3 = &r.specular; break;
755 case TextureStage::SOURCE_TEMP: arg3 = &temp; break;
756 case TextureStage::SOURCE_TFACTOR: arg3 = &tfactor; break;
757 default:
758 ASSERT(false);
759 }
760
Alexis Hetu96517182015-04-15 10:30:23 -0400761 Vector4s mod1;
762 Vector4s mod2;
763 Vector4s mod3;
John Bauman89401822014-05-06 15:04:28 -0400764
765 switch(textureStage.firstModifier)
766 {
767 case TextureStage::MODIFIER_COLOR:
768 break;
769 case TextureStage::MODIFIER_INVCOLOR:
770 {
John Bauman19bac1e2014-05-06 15:23:49 -0400771 mod1.x = SubSat(Short4(0x1000), arg1->x);
772 mod1.y = SubSat(Short4(0x1000), arg1->y);
773 mod1.z = SubSat(Short4(0x1000), arg1->z);
774 mod1.w = SubSat(Short4(0x1000), arg1->w);
John Bauman89401822014-05-06 15:04:28 -0400775
776 arg1 = &mod1;
777 }
778 break;
779 case TextureStage::MODIFIER_ALPHA:
780 {
John Bauman19bac1e2014-05-06 15:23:49 -0400781 mod1.x = arg1->w;
782 mod1.y = arg1->w;
783 mod1.z = arg1->w;
784 mod1.w = arg1->w;
John Bauman89401822014-05-06 15:04:28 -0400785
786 arg1 = &mod1;
787 }
788 break;
789 case TextureStage::MODIFIER_INVALPHA:
790 {
John Bauman19bac1e2014-05-06 15:23:49 -0400791 mod1.x = SubSat(Short4(0x1000), arg1->w);
792 mod1.y = SubSat(Short4(0x1000), arg1->w);
793 mod1.z = SubSat(Short4(0x1000), arg1->w);
794 mod1.w = SubSat(Short4(0x1000), arg1->w);
John Bauman89401822014-05-06 15:04:28 -0400795
796 arg1 = &mod1;
797 }
798 break;
799 default:
800 ASSERT(false);
801 }
802
803 switch(textureStage.secondModifier)
804 {
805 case TextureStage::MODIFIER_COLOR:
806 break;
807 case TextureStage::MODIFIER_INVCOLOR:
808 {
John Bauman19bac1e2014-05-06 15:23:49 -0400809 mod2.x = SubSat(Short4(0x1000), arg2->x);
810 mod2.y = SubSat(Short4(0x1000), arg2->y);
811 mod2.z = SubSat(Short4(0x1000), arg2->z);
812 mod2.w = SubSat(Short4(0x1000), arg2->w);
John Bauman89401822014-05-06 15:04:28 -0400813
814 arg2 = &mod2;
815 }
816 break;
817 case TextureStage::MODIFIER_ALPHA:
818 {
John Bauman19bac1e2014-05-06 15:23:49 -0400819 mod2.x = arg2->w;
820 mod2.y = arg2->w;
821 mod2.z = arg2->w;
822 mod2.w = arg2->w;
John Bauman89401822014-05-06 15:04:28 -0400823
824 arg2 = &mod2;
825 }
826 break;
827 case TextureStage::MODIFIER_INVALPHA:
828 {
John Bauman19bac1e2014-05-06 15:23:49 -0400829 mod2.x = SubSat(Short4(0x1000), arg2->w);
830 mod2.y = SubSat(Short4(0x1000), arg2->w);
831 mod2.z = SubSat(Short4(0x1000), arg2->w);
832 mod2.w = SubSat(Short4(0x1000), arg2->w);
John Bauman89401822014-05-06 15:04:28 -0400833
834 arg2 = &mod2;
835 }
836 break;
837 default:
838 ASSERT(false);
839 }
840
841 switch(textureStage.thirdModifier)
842 {
843 case TextureStage::MODIFIER_COLOR:
844 break;
845 case TextureStage::MODIFIER_INVCOLOR:
846 {
John Bauman19bac1e2014-05-06 15:23:49 -0400847 mod3.x = SubSat(Short4(0x1000), arg3->x);
848 mod3.y = SubSat(Short4(0x1000), arg3->y);
849 mod3.z = SubSat(Short4(0x1000), arg3->z);
850 mod3.w = SubSat(Short4(0x1000), arg3->w);
John Bauman89401822014-05-06 15:04:28 -0400851
852 arg3 = &mod3;
853 }
854 break;
855 case TextureStage::MODIFIER_ALPHA:
856 {
John Bauman19bac1e2014-05-06 15:23:49 -0400857 mod3.x = arg3->w;
858 mod3.y = arg3->w;
859 mod3.z = arg3->w;
860 mod3.w = arg3->w;
John Bauman89401822014-05-06 15:04:28 -0400861
862 arg3 = &mod3;
863 }
864 break;
865 case TextureStage::MODIFIER_INVALPHA:
866 {
John Bauman19bac1e2014-05-06 15:23:49 -0400867 mod3.x = SubSat(Short4(0x1000), arg3->w);
868 mod3.y = SubSat(Short4(0x1000), arg3->w);
869 mod3.z = SubSat(Short4(0x1000), arg3->w);
870 mod3.w = SubSat(Short4(0x1000), arg3->w);
John Bauman89401822014-05-06 15:04:28 -0400871
872 arg3 = &mod3;
873 }
874 break;
875 default:
876 ASSERT(false);
877 }
878
879 switch(textureStage.stageOperation)
880 {
881 case TextureStage::STAGE_DISABLE:
882 break;
883 case TextureStage::STAGE_SELECTARG1: // Arg1
884 {
John Bauman19bac1e2014-05-06 15:23:49 -0400885 res.x = arg1->x;
886 res.y = arg1->y;
887 res.z = arg1->z;
John Bauman89401822014-05-06 15:04:28 -0400888 }
889 break;
890 case TextureStage::STAGE_SELECTARG2: // Arg2
891 {
John Bauman19bac1e2014-05-06 15:23:49 -0400892 res.x = arg2->x;
893 res.y = arg2->y;
894 res.z = arg2->z;
John Bauman89401822014-05-06 15:04:28 -0400895 }
896 break;
897 case TextureStage::STAGE_SELECTARG3: // Arg3
898 {
John Bauman19bac1e2014-05-06 15:23:49 -0400899 res.x = arg3->x;
900 res.y = arg3->y;
901 res.z = arg3->z;
John Bauman89401822014-05-06 15:04:28 -0400902 }
903 break;
904 case TextureStage::STAGE_MODULATE: // Arg1 * Arg2
905 {
John Bauman19bac1e2014-05-06 15:23:49 -0400906 res.x = MulHigh(arg1->x, arg2->x) << 4;
907 res.y = MulHigh(arg1->y, arg2->y) << 4;
908 res.z = MulHigh(arg1->z, arg2->z) << 4;
John Bauman89401822014-05-06 15:04:28 -0400909 }
910 break;
911 case TextureStage::STAGE_MODULATE2X: // Arg1 * Arg2 * 2
912 {
John Bauman19bac1e2014-05-06 15:23:49 -0400913 res.x = MulHigh(arg1->x, arg2->x) << 5;
914 res.y = MulHigh(arg1->y, arg2->y) << 5;
915 res.z = MulHigh(arg1->z, arg2->z) << 5;
John Bauman89401822014-05-06 15:04:28 -0400916 }
917 break;
918 case TextureStage::STAGE_MODULATE4X: // Arg1 * Arg2 * 4
919 {
John Bauman19bac1e2014-05-06 15:23:49 -0400920 res.x = MulHigh(arg1->x, arg2->x) << 6;
921 res.y = MulHigh(arg1->y, arg2->y) << 6;
922 res.z = MulHigh(arg1->z, arg2->z) << 6;
John Bauman89401822014-05-06 15:04:28 -0400923 }
924 break;
925 case TextureStage::STAGE_ADD: // Arg1 + Arg2
926 {
John Bauman19bac1e2014-05-06 15:23:49 -0400927 res.x = AddSat(arg1->x, arg2->x);
928 res.y = AddSat(arg1->y, arg2->y);
929 res.z = AddSat(arg1->z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400930 }
931 break;
932 case TextureStage::STAGE_ADDSIGNED: // Arg1 + Arg2 - 0.5
933 {
John Bauman19bac1e2014-05-06 15:23:49 -0400934 res.x = AddSat(arg1->x, arg2->x);
935 res.y = AddSat(arg1->y, arg2->y);
936 res.z = AddSat(arg1->z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400937
John Bauman19bac1e2014-05-06 15:23:49 -0400938 res.x = SubSat(res.x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
939 res.y = SubSat(res.y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
940 res.z = SubSat(res.z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -0400941 }
942 break;
943 case TextureStage::STAGE_ADDSIGNED2X: // (Arg1 + Arg2 - 0.5) << 1
944 {
John Bauman19bac1e2014-05-06 15:23:49 -0400945 res.x = AddSat(arg1->x, arg2->x);
946 res.y = AddSat(arg1->y, arg2->y);
947 res.z = AddSat(arg1->z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400948
John Bauman19bac1e2014-05-06 15:23:49 -0400949 res.x = SubSat(res.x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
950 res.y = SubSat(res.y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
951 res.z = SubSat(res.z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -0400952
John Bauman19bac1e2014-05-06 15:23:49 -0400953 res.x = AddSat(res.x, res.x);
954 res.y = AddSat(res.y, res.y);
955 res.z = AddSat(res.z, res.z);
John Bauman89401822014-05-06 15:04:28 -0400956 }
957 break;
958 case TextureStage::STAGE_SUBTRACT: // Arg1 - Arg2
959 {
John Bauman19bac1e2014-05-06 15:23:49 -0400960 res.x = SubSat(arg1->x, arg2->x);
961 res.y = SubSat(arg1->y, arg2->y);
962 res.z = SubSat(arg1->z, arg2->z);
John Bauman89401822014-05-06 15:04:28 -0400963 }
964 break;
965 case TextureStage::STAGE_ADDSMOOTH: // Arg1 + Arg2 - Arg1 * Arg2
966 {
967 Short4 tmp;
968
John Bauman19bac1e2014-05-06 15:23:49 -0400969 tmp = MulHigh(arg1->x, arg2->x) << 4; res.x = AddSat(arg1->x, arg2->x); res.x = SubSat(res.x, tmp);
970 tmp = MulHigh(arg1->y, arg2->y) << 4; res.y = AddSat(arg1->y, arg2->y); res.y = SubSat(res.y, tmp);
971 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 -0400972 }
973 break;
974 case TextureStage::STAGE_MULTIPLYADD: // Arg3 + Arg1 * Arg2
975 {
John Bauman19bac1e2014-05-06 15:23:49 -0400976 res.x = MulHigh(arg1->x, arg2->x) << 4; res.x = AddSat(res.x, arg3->x);
977 res.y = MulHigh(arg1->y, arg2->y) << 4; res.y = AddSat(res.y, arg3->y);
978 res.z = MulHigh(arg1->z, arg2->z) << 4; res.z = AddSat(res.z, arg3->z);
John Bauman89401822014-05-06 15:04:28 -0400979 }
980 break;
981 case TextureStage::STAGE_LERP: // Arg3 * (Arg1 - Arg2) + Arg2
982 {
John Bauman19bac1e2014-05-06 15:23:49 -0400983 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, arg3->x) << 4; res.x = AddSat(res.x, arg2->x);
984 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, arg3->y) << 4; res.y = AddSat(res.y, arg2->y);
985 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 -0400986 }
987 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400988 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 -0400989 {
990 Short4 tmp;
991
John Bauman19bac1e2014-05-06 15:23:49 -0400992 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);
993 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);
994 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 -0400995
John Bauman19bac1e2014-05-06 15:23:49 -0400996 res.x = res.x << 6;
997 res.y = res.y << 6;
998 res.z = res.z << 6;
John Bauman89401822014-05-06 15:04:28 -0400999
John Bauman19bac1e2014-05-06 15:23:49 -04001000 res.x = AddSat(res.x, res.y);
1001 res.x = AddSat(res.x, res.z);
John Bauman89401822014-05-06 15:04:28 -04001002
1003 // Clamp to [0, 1]
John Bauman19bac1e2014-05-06 15:23:49 -04001004 res.x = Max(res.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));
1005 res.x = Min(res.x, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001006
John Bauman19bac1e2014-05-06 15:23:49 -04001007 res.y = res.x;
1008 res.z = res.x;
1009 res.w = res.x;
John Bauman89401822014-05-06 15:04:28 -04001010 }
1011 break;
1012 case TextureStage::STAGE_BLENDCURRENTALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1013 {
Nicolas Capenscbefe532014-10-16 00:16:01 -04001014 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, r.current.w) << 4; res.x = AddSat(res.x, arg2->x);
1015 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, r.current.w) << 4; res.y = AddSat(res.y, arg2->y);
1016 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 -04001017 }
1018 break;
1019 case TextureStage::STAGE_BLENDDIFFUSEALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1020 {
John Bauman19bac1e2014-05-06 15:23:49 -04001021 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, r.diffuse.w) << 4; res.x = AddSat(res.x, arg2->x);
1022 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, r.diffuse.w) << 4; res.y = AddSat(res.y, arg2->y);
1023 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 -04001024 }
1025 break;
1026 case TextureStage::STAGE_BLENDFACTORALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1027 {
John Bauman19bac1e2014-05-06 15:23:49 -04001028 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.textureFactor4[3]))) << 4; res.x = AddSat(res.x, arg2->x);
1029 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);
1030 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 -04001031 }
1032 break;
1033 case TextureStage::STAGE_BLENDTEXTUREALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1034 {
John Bauman19bac1e2014-05-06 15:23:49 -04001035 res.x = SubSat(arg1->x, arg2->x); res.x = MulHigh(res.x, texture.w) << 4; res.x = AddSat(res.x, arg2->x);
1036 res.y = SubSat(arg1->y, arg2->y); res.y = MulHigh(res.y, texture.w) << 4; res.y = AddSat(res.y, arg2->y);
1037 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 -04001038 }
1039 break;
1040 case TextureStage::STAGE_BLENDTEXTUREALPHAPM: // Arg1 + Arg2 * (1 - Alpha)
1041 {
John Bauman19bac1e2014-05-06 15:23:49 -04001042 res.x = SubSat(Short4(0x1000), texture.w); res.x = MulHigh(res.x, arg2->x) << 4; res.x = AddSat(res.x, arg1->x);
1043 res.y = SubSat(Short4(0x1000), texture.w); res.y = MulHigh(res.y, arg2->y) << 4; res.y = AddSat(res.y, arg1->y);
1044 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 -04001045 }
1046 break;
1047 case TextureStage::STAGE_PREMODULATE:
1048 {
John Bauman19bac1e2014-05-06 15:23:49 -04001049 res.x = arg1->x;
1050 res.y = arg1->y;
1051 res.z = arg1->z;
John Bauman89401822014-05-06 15:04:28 -04001052 }
1053 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001054 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR: // Arg1 + Arg1.w * Arg2
John Bauman89401822014-05-06 15:04:28 -04001055 {
John Bauman19bac1e2014-05-06 15:23:49 -04001056 res.x = MulHigh(arg1->w, arg2->x) << 4; res.x = AddSat(res.x, arg1->x);
1057 res.y = MulHigh(arg1->w, arg2->y) << 4; res.y = AddSat(res.y, arg1->y);
1058 res.z = MulHigh(arg1->w, arg2->z) << 4; res.z = AddSat(res.z, arg1->z);
John Bauman89401822014-05-06 15:04:28 -04001059 }
1060 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001061 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA: // Arg1 * Arg2 + Arg1.w
John Bauman89401822014-05-06 15:04:28 -04001062 {
John Bauman19bac1e2014-05-06 15:23:49 -04001063 res.x = MulHigh(arg1->x, arg2->x) << 4; res.x = AddSat(res.x, arg1->w);
1064 res.y = MulHigh(arg1->y, arg2->y) << 4; res.y = AddSat(res.y, arg1->w);
1065 res.z = MulHigh(arg1->z, arg2->z) << 4; res.z = AddSat(res.z, arg1->w);
John Bauman89401822014-05-06 15:04:28 -04001066 }
1067 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001068 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR: // (1 - Arg1.w) * Arg2 + Arg1
John Bauman89401822014-05-06 15:04:28 -04001069 {
1070 Short4 tmp;
1071
John Bauman19bac1e2014-05-06 15:23:49 -04001072 res.x = AddSat(arg1->x, arg2->x); tmp = MulHigh(arg1->w, arg2->x) << 4; res.x = SubSat(res.x, tmp);
1073 res.y = AddSat(arg1->y, arg2->y); tmp = MulHigh(arg1->w, arg2->y) << 4; res.y = SubSat(res.y, tmp);
1074 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 -04001075 }
1076 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001077 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA: // (1 - Arg1) * Arg2 + Arg1.w
John Bauman89401822014-05-06 15:04:28 -04001078 {
1079 Short4 tmp;
1080
John Bauman19bac1e2014-05-06 15:23:49 -04001081 res.x = AddSat(arg1->w, arg2->x); tmp = MulHigh(arg1->x, arg2->x) << 4; res.x = SubSat(res.x, tmp);
1082 res.y = AddSat(arg1->w, arg2->y); tmp = MulHigh(arg1->y, arg2->y) << 4; res.y = SubSat(res.y, tmp);
1083 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 -04001084 }
1085 break;
1086 case TextureStage::STAGE_BUMPENVMAP:
1087 {
John Bauman19bac1e2014-05-06 15:23:49 -04001088 r.du = Float4(texture.x) * Float4(1.0f / 0x0FE0);
1089 r.dv = Float4(texture.y) * Float4(1.0f / 0x0FE0);
John Bauman89401822014-05-06 15:04:28 -04001090
1091 Float4 du2;
1092 Float4 dv2;
1093
1094 du2 = r.du;
1095 dv2 = r.dv;
1096 r.du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
1097 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
1098 r.du += dv2;
1099 r.dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
1100 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
1101 r.dv += du2;
1102
1103 perturbate = true;
1104
John Bauman19bac1e2014-05-06 15:23:49 -04001105 res.x = r.current.x;
1106 res.y = r.current.y;
1107 res.z = r.current.z;
1108 res.w = r.current.w;
John Bauman89401822014-05-06 15:04:28 -04001109 }
1110 break;
1111 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1112 {
John Bauman19bac1e2014-05-06 15:23:49 -04001113 r.du = Float4(texture.x) * Float4(1.0f / 0x0FE0);
1114 r.dv = Float4(texture.y) * Float4(1.0f / 0x0FE0);
John Bauman89401822014-05-06 15:04:28 -04001115
1116 Float4 du2;
1117 Float4 dv2;
1118
1119 du2 = r.du;
1120 dv2 = r.dv;
1121
1122 r.du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
1123 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
1124 r.du += dv2;
1125 r.dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
1126 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
1127 r.dv += du2;
1128
1129 perturbate = true;
1130
John Bauman19bac1e2014-05-06 15:23:49 -04001131 r.L = texture.z;
John Bauman89401822014-05-06 15:04:28 -04001132 r.L = MulHigh(r.L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceScale4)));
1133 r.L = r.L << 4;
1134 r.L = AddSat(r.L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceOffset4)));
1135 r.L = Max(r.L, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman19bac1e2014-05-06 15:23:49 -04001136 r.L = Min(r.L, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001137
1138 luminance = true;
1139
John Bauman19bac1e2014-05-06 15:23:49 -04001140 res.x = r.current.x;
1141 res.y = r.current.y;
1142 res.z = r.current.z;
1143 res.w = r.current.w;
John Bauman89401822014-05-06 15:04:28 -04001144 }
1145 break;
1146 default:
1147 ASSERT(false);
1148 }
1149
1150 if(textureStage.stageOperation != TextureStage::STAGE_DOT3)
1151 {
1152 switch(textureStage.firstArgumentAlpha)
1153 {
1154 case TextureStage::SOURCE_TEXTURE: arg1 = &texture; break;
1155 case TextureStage::SOURCE_CONSTANT: arg1 = &constant; break;
Nicolas Capenscbefe532014-10-16 00:16:01 -04001156 case TextureStage::SOURCE_CURRENT: arg1 = &r.current; break;
John Bauman89401822014-05-06 15:04:28 -04001157 case TextureStage::SOURCE_DIFFUSE: arg1 = &r.diffuse; break;
1158 case TextureStage::SOURCE_SPECULAR: arg1 = &r.specular; break;
1159 case TextureStage::SOURCE_TEMP: arg1 = &temp; break;
1160 case TextureStage::SOURCE_TFACTOR: arg1 = &tfactor; break;
1161 default:
1162 ASSERT(false);
1163 }
1164
1165 switch(textureStage.secondArgumentAlpha)
1166 {
1167 case TextureStage::SOURCE_TEXTURE: arg2 = &texture; break;
1168 case TextureStage::SOURCE_CONSTANT: arg2 = &constant; break;
Nicolas Capenscbefe532014-10-16 00:16:01 -04001169 case TextureStage::SOURCE_CURRENT: arg2 = &r.current; break;
John Bauman89401822014-05-06 15:04:28 -04001170 case TextureStage::SOURCE_DIFFUSE: arg2 = &r.diffuse; break;
1171 case TextureStage::SOURCE_SPECULAR: arg2 = &r.specular; break;
1172 case TextureStage::SOURCE_TEMP: arg2 = &temp; break;
1173 case TextureStage::SOURCE_TFACTOR: arg2 = &tfactor; break;
1174 default:
1175 ASSERT(false);
1176 }
1177
1178 switch(textureStage.thirdArgumentAlpha)
1179 {
1180 case TextureStage::SOURCE_TEXTURE: arg3 = &texture; break;
1181 case TextureStage::SOURCE_CONSTANT: arg3 = &constant; break;
Nicolas Capenscbefe532014-10-16 00:16:01 -04001182 case TextureStage::SOURCE_CURRENT: arg3 = &r.current; break;
John Bauman89401822014-05-06 15:04:28 -04001183 case TextureStage::SOURCE_DIFFUSE: arg3 = &r.diffuse; break;
1184 case TextureStage::SOURCE_SPECULAR: arg3 = &r.specular; break;
1185 case TextureStage::SOURCE_TEMP: arg3 = &temp; break;
1186 case TextureStage::SOURCE_TFACTOR: arg3 = &tfactor; break;
1187 default:
1188 ASSERT(false);
1189 }
1190
1191 switch(textureStage.firstModifierAlpha) // FIXME: Check if actually used
1192 {
1193 case TextureStage::MODIFIER_COLOR:
1194 break;
1195 case TextureStage::MODIFIER_INVCOLOR:
1196 {
John Bauman19bac1e2014-05-06 15:23:49 -04001197 mod1.w = SubSat(Short4(0x1000), arg1->w);
John Bauman89401822014-05-06 15:04:28 -04001198
1199 arg1 = &mod1;
1200 }
1201 break;
1202 case TextureStage::MODIFIER_ALPHA:
1203 {
1204 // Redudant
1205 }
1206 break;
1207 case TextureStage::MODIFIER_INVALPHA:
1208 {
John Bauman19bac1e2014-05-06 15:23:49 -04001209 mod1.w = SubSat(Short4(0x1000), arg1->w);
John Bauman89401822014-05-06 15:04:28 -04001210
1211 arg1 = &mod1;
1212 }
1213 break;
1214 default:
1215 ASSERT(false);
1216 }
1217
1218 switch(textureStage.secondModifierAlpha) // FIXME: Check if actually used
1219 {
1220 case TextureStage::MODIFIER_COLOR:
1221 break;
1222 case TextureStage::MODIFIER_INVCOLOR:
1223 {
John Bauman19bac1e2014-05-06 15:23:49 -04001224 mod2.w = SubSat(Short4(0x1000), arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001225
1226 arg2 = &mod2;
1227 }
1228 break;
1229 case TextureStage::MODIFIER_ALPHA:
1230 {
1231 // Redudant
1232 }
1233 break;
1234 case TextureStage::MODIFIER_INVALPHA:
1235 {
John Bauman19bac1e2014-05-06 15:23:49 -04001236 mod2.w = SubSat(Short4(0x1000), arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001237
1238 arg2 = &mod2;
1239 }
1240 break;
1241 default:
1242 ASSERT(false);
1243 }
1244
1245 switch(textureStage.thirdModifierAlpha) // FIXME: Check if actually used
1246 {
1247 case TextureStage::MODIFIER_COLOR:
1248 break;
1249 case TextureStage::MODIFIER_INVCOLOR:
1250 {
John Bauman19bac1e2014-05-06 15:23:49 -04001251 mod3.w = SubSat(Short4(0x1000), arg3->w);
John Bauman89401822014-05-06 15:04:28 -04001252
1253 arg3 = &mod3;
1254 }
1255 break;
1256 case TextureStage::MODIFIER_ALPHA:
1257 {
1258 // Redudant
1259 }
1260 break;
1261 case TextureStage::MODIFIER_INVALPHA:
1262 {
John Bauman19bac1e2014-05-06 15:23:49 -04001263 mod3.w = SubSat(Short4(0x1000), arg3->w);
John Bauman89401822014-05-06 15:04:28 -04001264
1265 arg3 = &mod3;
1266 }
1267 break;
1268 default:
1269 ASSERT(false);
1270 }
1271
1272 switch(textureStage.stageOperationAlpha)
1273 {
1274 case TextureStage::STAGE_DISABLE:
1275 break;
1276 case TextureStage::STAGE_SELECTARG1: // Arg1
1277 {
John Bauman19bac1e2014-05-06 15:23:49 -04001278 res.w = arg1->w;
John Bauman89401822014-05-06 15:04:28 -04001279 }
1280 break;
1281 case TextureStage::STAGE_SELECTARG2: // Arg2
1282 {
John Bauman19bac1e2014-05-06 15:23:49 -04001283 res.w = arg2->w;
John Bauman89401822014-05-06 15:04:28 -04001284 }
1285 break;
1286 case TextureStage::STAGE_SELECTARG3: // Arg3
1287 {
John Bauman19bac1e2014-05-06 15:23:49 -04001288 res.w = arg3->w;
John Bauman89401822014-05-06 15:04:28 -04001289 }
1290 break;
1291 case TextureStage::STAGE_MODULATE: // Arg1 * Arg2
1292 {
John Bauman19bac1e2014-05-06 15:23:49 -04001293 res.w = MulHigh(arg1->w, arg2->w) << 4;
John Bauman89401822014-05-06 15:04:28 -04001294 }
1295 break;
1296 case TextureStage::STAGE_MODULATE2X: // Arg1 * Arg2 * 2
1297 {
John Bauman19bac1e2014-05-06 15:23:49 -04001298 res.w = MulHigh(arg1->w, arg2->w) << 5;
John Bauman89401822014-05-06 15:04:28 -04001299 }
1300 break;
1301 case TextureStage::STAGE_MODULATE4X: // Arg1 * Arg2 * 4
1302 {
John Bauman19bac1e2014-05-06 15:23:49 -04001303 res.w = MulHigh(arg1->w, arg2->w) << 6;
John Bauman89401822014-05-06 15:04:28 -04001304 }
1305 break;
1306 case TextureStage::STAGE_ADD: // Arg1 + Arg2
1307 {
John Bauman19bac1e2014-05-06 15:23:49 -04001308 res.w = AddSat(arg1->w, arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001309 }
1310 break;
1311 case TextureStage::STAGE_ADDSIGNED: // Arg1 + Arg2 - 0.5
1312 {
John Bauman19bac1e2014-05-06 15:23:49 -04001313 res.w = AddSat(arg1->w, arg2->w);
1314 res.w = SubSat(res.w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -04001315 }
1316 break;
1317 case TextureStage::STAGE_ADDSIGNED2X: // (Arg1 + Arg2 - 0.5) << 1
1318 {
John Bauman19bac1e2014-05-06 15:23:49 -04001319 res.w = AddSat(arg1->w, arg2->w);
1320 res.w = SubSat(res.w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
1321 res.w = AddSat(res.w, res.w);
John Bauman89401822014-05-06 15:04:28 -04001322 }
1323 break;
1324 case TextureStage::STAGE_SUBTRACT: // Arg1 - Arg2
1325 {
John Bauman19bac1e2014-05-06 15:23:49 -04001326 res.w = SubSat(arg1->w, arg2->w);
John Bauman89401822014-05-06 15:04:28 -04001327 }
1328 break;
1329 case TextureStage::STAGE_ADDSMOOTH: // Arg1 + Arg2 - Arg1 * Arg2
1330 {
1331 Short4 tmp;
1332
John Bauman19bac1e2014-05-06 15:23:49 -04001333 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 -04001334 }
1335 break;
1336 case TextureStage::STAGE_MULTIPLYADD: // Arg3 + Arg1 * Arg2
1337 {
John Bauman19bac1e2014-05-06 15:23:49 -04001338 res.w = MulHigh(arg1->w, arg2->w) << 4; res.w = AddSat(res.w, arg3->w);
John Bauman89401822014-05-06 15:04:28 -04001339 }
1340 break;
1341 case TextureStage::STAGE_LERP: // Arg3 * (Arg1 - Arg2) + Arg2
1342 {
John Bauman19bac1e2014-05-06 15:23:49 -04001343 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 -04001344 }
1345 break;
1346 case TextureStage::STAGE_DOT3:
1347 break; // Already computed in color channel
1348 case TextureStage::STAGE_BLENDCURRENTALPHA: // Alpha * (Arg1 - Arg2) + Arg2
1349 {
Nicolas Capenscbefe532014-10-16 00:16:01 -04001350 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 -04001351 }
1352 break;
1353 case TextureStage::STAGE_BLENDDIFFUSEALPHA: // Arg1 * (Alpha) + Arg2 * (1 - Alpha)
1354 {
John Bauman19bac1e2014-05-06 15:23:49 -04001355 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 -04001356 }
1357 break;
1358 case TextureStage::STAGE_BLENDFACTORALPHA:
1359 {
John Bauman19bac1e2014-05-06 15:23:49 -04001360 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 -04001361 }
1362 break;
1363 case TextureStage::STAGE_BLENDTEXTUREALPHA: // Arg1 * (Alpha) + Arg2 * (1 - Alpha)
1364 {
John Bauman19bac1e2014-05-06 15:23:49 -04001365 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 -04001366 }
1367 break;
1368 case TextureStage::STAGE_BLENDTEXTUREALPHAPM: // Arg1 + Arg2 * (1 - Alpha)
1369 {
John Bauman19bac1e2014-05-06 15:23:49 -04001370 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 -04001371 }
1372 break;
1373 case TextureStage::STAGE_PREMODULATE:
1374 {
John Bauman19bac1e2014-05-06 15:23:49 -04001375 res.w = arg1->w;
John Bauman89401822014-05-06 15:04:28 -04001376 }
1377 break;
1378 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1379 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
1380 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1381 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1382 case TextureStage::STAGE_BUMPENVMAP:
1383 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1384 break; // Invalid alpha operations
1385 default:
1386 ASSERT(false);
1387 }
1388 }
1389
1390 // Clamp result to [0, 1]
1391
1392 switch(textureStage.stageOperation)
1393 {
1394 case TextureStage::STAGE_DISABLE:
1395 case TextureStage::STAGE_SELECTARG1:
1396 case TextureStage::STAGE_SELECTARG2:
1397 case TextureStage::STAGE_SELECTARG3:
1398 case TextureStage::STAGE_MODULATE:
1399 case TextureStage::STAGE_MODULATE2X:
1400 case TextureStage::STAGE_MODULATE4X:
1401 case TextureStage::STAGE_ADD:
1402 case TextureStage::STAGE_MULTIPLYADD:
1403 case TextureStage::STAGE_LERP:
1404 case TextureStage::STAGE_BLENDCURRENTALPHA:
1405 case TextureStage::STAGE_BLENDDIFFUSEALPHA:
1406 case TextureStage::STAGE_BLENDFACTORALPHA:
1407 case TextureStage::STAGE_BLENDTEXTUREALPHA:
1408 case TextureStage::STAGE_BLENDTEXTUREALPHAPM:
1409 case TextureStage::STAGE_DOT3: // Already clamped
1410 case TextureStage::STAGE_PREMODULATE:
1411 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1412 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
1413 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1414 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1415 case TextureStage::STAGE_BUMPENVMAP:
1416 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1417 if(state.textureStage[stage].cantUnderflow)
1418 {
1419 break; // Can't go below zero
1420 }
1421 case TextureStage::STAGE_ADDSIGNED:
1422 case TextureStage::STAGE_ADDSIGNED2X:
1423 case TextureStage::STAGE_SUBTRACT:
1424 case TextureStage::STAGE_ADDSMOOTH:
John Bauman19bac1e2014-05-06 15:23:49 -04001425 res.x = Max(res.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));
1426 res.y = Max(res.y, Short4(0x0000, 0x0000, 0x0000, 0x0000));
1427 res.z = Max(res.z, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman89401822014-05-06 15:04:28 -04001428 break;
1429 default:
1430 ASSERT(false);
1431 }
1432
1433 switch(textureStage.stageOperationAlpha)
1434 {
1435 case TextureStage::STAGE_DISABLE:
1436 case TextureStage::STAGE_SELECTARG1:
1437 case TextureStage::STAGE_SELECTARG2:
1438 case TextureStage::STAGE_SELECTARG3:
1439 case TextureStage::STAGE_MODULATE:
1440 case TextureStage::STAGE_MODULATE2X:
1441 case TextureStage::STAGE_MODULATE4X:
1442 case TextureStage::STAGE_ADD:
1443 case TextureStage::STAGE_MULTIPLYADD:
1444 case TextureStage::STAGE_LERP:
1445 case TextureStage::STAGE_BLENDCURRENTALPHA:
1446 case TextureStage::STAGE_BLENDDIFFUSEALPHA:
1447 case TextureStage::STAGE_BLENDFACTORALPHA:
1448 case TextureStage::STAGE_BLENDTEXTUREALPHA:
1449 case TextureStage::STAGE_BLENDTEXTUREALPHAPM:
1450 case TextureStage::STAGE_DOT3: // Already clamped
1451 case TextureStage::STAGE_PREMODULATE:
1452 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1453 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
1454 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1455 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1456 case TextureStage::STAGE_BUMPENVMAP:
1457 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1458 if(state.textureStage[stage].cantUnderflow)
1459 {
1460 break; // Can't go below zero
1461 }
1462 case TextureStage::STAGE_ADDSIGNED:
1463 case TextureStage::STAGE_ADDSIGNED2X:
1464 case TextureStage::STAGE_SUBTRACT:
1465 case TextureStage::STAGE_ADDSMOOTH:
John Bauman19bac1e2014-05-06 15:23:49 -04001466 res.w = Max(res.w, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman89401822014-05-06 15:04:28 -04001467 break;
1468 default:
1469 ASSERT(false);
1470 }
1471
1472 switch(textureStage.stageOperation)
1473 {
1474 case TextureStage::STAGE_DISABLE:
1475 case TextureStage::STAGE_SELECTARG1:
1476 case TextureStage::STAGE_SELECTARG2:
1477 case TextureStage::STAGE_SELECTARG3:
1478 case TextureStage::STAGE_MODULATE:
1479 case TextureStage::STAGE_SUBTRACT:
1480 case TextureStage::STAGE_ADDSMOOTH:
1481 case TextureStage::STAGE_LERP:
1482 case TextureStage::STAGE_BLENDCURRENTALPHA:
1483 case TextureStage::STAGE_BLENDDIFFUSEALPHA:
1484 case TextureStage::STAGE_BLENDFACTORALPHA:
1485 case TextureStage::STAGE_BLENDTEXTUREALPHA:
1486 case TextureStage::STAGE_DOT3: // Already clamped
1487 case TextureStage::STAGE_PREMODULATE:
1488 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1489 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1490 case TextureStage::STAGE_BUMPENVMAP:
1491 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1492 break; // Can't go above one
1493 case TextureStage::STAGE_MODULATE2X:
1494 case TextureStage::STAGE_MODULATE4X:
1495 case TextureStage::STAGE_ADD:
1496 case TextureStage::STAGE_ADDSIGNED:
1497 case TextureStage::STAGE_ADDSIGNED2X:
1498 case TextureStage::STAGE_MULTIPLYADD:
1499 case TextureStage::STAGE_BLENDTEXTUREALPHAPM:
1500 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1501 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001502 res.x = Min(res.x, Short4(0x1000));
1503 res.y = Min(res.y, Short4(0x1000));
1504 res.z = Min(res.z, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001505 break;
1506 default:
1507 ASSERT(false);
1508 }
1509
1510 switch(textureStage.stageOperationAlpha)
1511 {
1512 case TextureStage::STAGE_DISABLE:
1513 case TextureStage::STAGE_SELECTARG1:
1514 case TextureStage::STAGE_SELECTARG2:
1515 case TextureStage::STAGE_SELECTARG3:
1516 case TextureStage::STAGE_MODULATE:
1517 case TextureStage::STAGE_SUBTRACT:
1518 case TextureStage::STAGE_ADDSMOOTH:
1519 case TextureStage::STAGE_LERP:
1520 case TextureStage::STAGE_BLENDCURRENTALPHA:
1521 case TextureStage::STAGE_BLENDDIFFUSEALPHA:
1522 case TextureStage::STAGE_BLENDFACTORALPHA:
1523 case TextureStage::STAGE_BLENDTEXTUREALPHA:
1524 case TextureStage::STAGE_DOT3: // Already clamped
1525 case TextureStage::STAGE_PREMODULATE:
1526 case TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR:
1527 case TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA:
1528 case TextureStage::STAGE_BUMPENVMAP:
1529 case TextureStage::STAGE_BUMPENVMAPLUMINANCE:
1530 break; // Can't go above one
1531 case TextureStage::STAGE_MODULATE2X:
1532 case TextureStage::STAGE_MODULATE4X:
1533 case TextureStage::STAGE_ADD:
1534 case TextureStage::STAGE_ADDSIGNED:
1535 case TextureStage::STAGE_ADDSIGNED2X:
1536 case TextureStage::STAGE_MULTIPLYADD:
1537 case TextureStage::STAGE_BLENDTEXTUREALPHAPM:
1538 case TextureStage::STAGE_MODULATEALPHA_ADDCOLOR:
1539 case TextureStage::STAGE_MODULATECOLOR_ADDALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001540 res.w = Min(res.w, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001541 break;
1542 default:
1543 ASSERT(false);
1544 }
1545
1546 switch(textureStage.destinationArgument)
1547 {
1548 case TextureStage::DESTINATION_CURRENT:
Nicolas Capenscbefe532014-10-16 00:16:01 -04001549 r.current.x = res.x;
1550 r.current.y = res.y;
1551 r.current.z = res.z;
1552 r.current.w = res.w;
John Bauman89401822014-05-06 15:04:28 -04001553 break;
1554 case TextureStage::DESTINATION_TEMP:
John Bauman19bac1e2014-05-06 15:23:49 -04001555 temp.x = res.x;
1556 temp.y = res.y;
1557 temp.z = res.z;
1558 temp.w = res.w;
John Bauman89401822014-05-06 15:04:28 -04001559 break;
1560 default:
1561 ASSERT(false);
1562 }
1563 }
1564
1565 void PixelRoutine::alphaTest(Registers &r, Int &aMask, Short4 &alpha)
1566 {
1567 Short4 cmp;
1568 Short4 equal;
1569
1570 switch(state.alphaCompareMode)
1571 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001572 case ALPHA_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -04001573 aMask = 0xF;
1574 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001575 case ALPHA_NEVER:
John Bauman89401822014-05-06 15:04:28 -04001576 aMask = 0x0;
1577 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001578 case ALPHA_EQUAL:
John Bauman89401822014-05-06 15:04:28 -04001579 cmp = CmpEQ(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
1580 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1581 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001582 case ALPHA_NOTEQUAL: // a != b ~ !(a == b)
John Bauman89401822014-05-06 15:04:28 -04001583 cmp = CmpEQ(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4))) ^ Short4((short)0xFFFF, (short)0xFFFF, (short)0xFFFF, (short)0xFFFF); // FIXME
1584 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1585 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001586 case ALPHA_LESS: // a < b ~ b > a
John Bauman89401822014-05-06 15:04:28 -04001587 cmp = CmpGT(*Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)), alpha);
1588 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1589 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001590 case ALPHA_GREATEREQUAL: // a >= b ~ (a > b) || (a == b) ~ !(b > a) // TODO: Approximate
John Bauman89401822014-05-06 15:04:28 -04001591 equal = CmpEQ(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
1592 cmp = CmpGT(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
1593 cmp |= equal;
1594 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1595 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001596 case ALPHA_LESSEQUAL: // a <= b ~ !(a > b)
John Bauman89401822014-05-06 15:04:28 -04001597 cmp = CmpGT(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4))) ^ Short4((short)0xFFFF, (short)0xFFFF, (short)0xFFFF, (short)0xFFFF); // FIXME
1598 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1599 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001600 case ALPHA_GREATER: // a > b
John Bauman89401822014-05-06 15:04:28 -04001601 cmp = CmpGT(alpha, *Pointer<Short4>(r.data + OFFSET(DrawData,factor.alphaReference4)));
1602 aMask = SignMask(Pack(cmp, Short4(0x0000, 0x0000, 0x0000, 0x0000)));
1603 break;
1604 default:
1605 ASSERT(false);
1606 }
1607 }
1608
1609 void PixelRoutine::alphaToCoverage(Registers &r, Int cMask[4], Float4 &alpha)
1610 {
1611 Int4 coverage0 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c0)));
1612 Int4 coverage1 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c1)));
1613 Int4 coverage2 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c2)));
1614 Int4 coverage3 = CmpNLT(alpha, *Pointer<Float4>(r.data + OFFSET(DrawData,a2c3)));
1615
1616 Int aMask0 = SignMask(coverage0);
1617 Int aMask1 = SignMask(coverage1);
1618 Int aMask2 = SignMask(coverage2);
1619 Int aMask3 = SignMask(coverage3);
1620
1621 cMask[0] &= aMask0;
1622 cMask[1] &= aMask1;
1623 cMask[2] &= aMask2;
1624 cMask[3] &= aMask3;
1625 }
1626
Alexis Hetu96517182015-04-15 10:30:23 -04001627 Bool PixelRoutine::alphaTest(Registers &r, Int cMask[4], Vector4s &current)
John Bauman89401822014-05-06 15:04:28 -04001628 {
1629 if(!state.alphaTestActive())
1630 {
1631 return true;
1632 }
1633
1634 Int aMask;
1635
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001636 if(state.transparencyAntialiasing == TRANSPARENCY_NONE)
John Bauman89401822014-05-06 15:04:28 -04001637 {
John Bauman19bac1e2014-05-06 15:23:49 -04001638 alphaTest(r, aMask, current.w);
John Bauman89401822014-05-06 15:04:28 -04001639
1640 for(unsigned int q = 0; q < state.multiSample; q++)
1641 {
1642 cMask[q] &= aMask;
1643 }
1644 }
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001645 else if(state.transparencyAntialiasing == TRANSPARENCY_ALPHA_TO_COVERAGE)
John Bauman89401822014-05-06 15:04:28 -04001646 {
John Bauman19bac1e2014-05-06 15:23:49 -04001647 Float4 alpha = Float4(current.w) * Float4(1.0f / 0x1000);
John Bauman89401822014-05-06 15:04:28 -04001648
1649 alphaToCoverage(r, cMask, alpha);
1650 }
1651 else ASSERT(false);
1652
1653 Int pass = cMask[0];
1654
1655 for(unsigned int q = 1; q < state.multiSample; q++)
1656 {
1657 pass = pass | cMask[q];
1658 }
1659
1660 return pass != 0x0;
1661 }
1662
John Bauman19bac1e2014-05-06 15:23:49 -04001663 Bool PixelRoutine::alphaTest(Registers &r, Int cMask[4], Vector4f &c0)
John Bauman89401822014-05-06 15:04:28 -04001664 {
1665 if(!state.alphaTestActive())
1666 {
1667 return true;
1668 }
1669
1670 Int aMask;
1671
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001672 if(state.transparencyAntialiasing == TRANSPARENCY_NONE)
John Bauman89401822014-05-06 15:04:28 -04001673 {
John Bauman19bac1e2014-05-06 15:23:49 -04001674 Short4 alpha = RoundShort4(c0.w * Float4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04001675
1676 alphaTest(r, aMask, alpha);
1677
1678 for(unsigned int q = 0; q < state.multiSample; q++)
1679 {
1680 cMask[q] &= aMask;
1681 }
1682 }
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001683 else if(state.transparencyAntialiasing == TRANSPARENCY_ALPHA_TO_COVERAGE)
John Bauman89401822014-05-06 15:04:28 -04001684 {
John Bauman19bac1e2014-05-06 15:23:49 -04001685 alphaToCoverage(r, cMask, c0.w);
John Bauman89401822014-05-06 15:04:28 -04001686 }
1687 else ASSERT(false);
1688
1689 Int pass = cMask[0];
1690
1691 for(unsigned int q = 1; q < state.multiSample; q++)
1692 {
1693 pass = pass | cMask[q];
1694 }
1695
1696 return pass != 0x0;
1697 }
1698
Alexis Hetu96517182015-04-15 10:30:23 -04001699 void PixelRoutine::fogBlend(Registers &r, Vector4s &current, Float4 &f, Float4 &z, Float4 &rhw)
John Bauman89401822014-05-06 15:04:28 -04001700 {
1701 if(!state.fogActive)
1702 {
1703 return;
1704 }
1705
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001706 if(state.pixelFogMode != FOG_NONE)
John Bauman89401822014-05-06 15:04:28 -04001707 {
1708 pixelFog(r, f, z, rhw);
1709 }
1710
1711 UShort4 fog = convertFixed16(f, true);
1712
John Bauman19bac1e2014-05-06 15:23:49 -04001713 current.x = As<Short4>(MulHigh(As<UShort4>(current.x), fog));
1714 current.y = As<Short4>(MulHigh(As<UShort4>(current.y), fog));
1715 current.z = As<Short4>(MulHigh(As<UShort4>(current.z), fog));
John Bauman89401822014-05-06 15:04:28 -04001716
John Bauman19bac1e2014-05-06 15:23:49 -04001717 UShort4 invFog = UShort4(0xFFFFu) - fog;
John Bauman89401822014-05-06 15:04:28 -04001718
John Bauman19bac1e2014-05-06 15:23:49 -04001719 current.x += As<Short4>(MulHigh(invFog, *Pointer<UShort4>(r.data + OFFSET(DrawData,fog.color4[0]))));
1720 current.y += As<Short4>(MulHigh(invFog, *Pointer<UShort4>(r.data + OFFSET(DrawData,fog.color4[1]))));
1721 current.z += As<Short4>(MulHigh(invFog, *Pointer<UShort4>(r.data + OFFSET(DrawData,fog.color4[2]))));
John Bauman89401822014-05-06 15:04:28 -04001722 }
1723
John Bauman19bac1e2014-05-06 15:23:49 -04001724 void PixelRoutine::fogBlend(Registers &r, Vector4f &c0, Float4 &fog, Float4 &z, Float4 &rhw)
John Bauman89401822014-05-06 15:04:28 -04001725 {
1726 if(!state.fogActive)
1727 {
1728 return;
1729 }
1730
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001731 if(state.pixelFogMode != FOG_NONE)
John Bauman89401822014-05-06 15:04:28 -04001732 {
1733 pixelFog(r, fog, z, rhw);
1734
John Bauman19bac1e2014-05-06 15:23:49 -04001735 fog = Min(fog, Float4(1.0f));
1736 fog = Max(fog, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -04001737 }
1738
John Bauman19bac1e2014-05-06 15:23:49 -04001739 c0.x -= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[0]));
1740 c0.y -= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[1]));
1741 c0.z -= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[2]));
John Bauman89401822014-05-06 15:04:28 -04001742
John Bauman19bac1e2014-05-06 15:23:49 -04001743 c0.x *= fog;
1744 c0.y *= fog;
1745 c0.z *= fog;
John Bauman89401822014-05-06 15:04:28 -04001746
John Bauman19bac1e2014-05-06 15:23:49 -04001747 c0.x += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[0]));
1748 c0.y += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[1]));
1749 c0.z += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.colorF[2]));
John Bauman89401822014-05-06 15:04:28 -04001750 }
1751
1752 void PixelRoutine::pixelFog(Registers &r, Float4 &visibility, Float4 &z, Float4 &rhw)
1753 {
1754 Float4 &zw = visibility;
1755
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001756 if(state.pixelFogMode != FOG_NONE)
John Bauman89401822014-05-06 15:04:28 -04001757 {
1758 if(state.wBasedFog)
1759 {
1760 zw = rhw;
1761 }
1762 else
1763 {
1764 if(complementaryDepthBuffer)
1765 {
John Bauman19bac1e2014-05-06 15:23:49 -04001766 zw = Float4(1.0f) - z;
John Bauman89401822014-05-06 15:04:28 -04001767 }
1768 else
1769 {
1770 zw = z;
1771 }
1772 }
1773 }
1774
1775 switch(state.pixelFogMode)
1776 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001777 case FOG_NONE:
John Bauman89401822014-05-06 15:04:28 -04001778 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001779 case FOG_LINEAR:
John Bauman89401822014-05-06 15:04:28 -04001780 zw *= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.scale));
1781 zw += *Pointer<Float4>(r.data + OFFSET(DrawData,fog.offset));
1782 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001783 case FOG_EXP:
John Bauman89401822014-05-06 15:04:28 -04001784 zw *= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.densityE));
John Bauman19bac1e2014-05-06 15:23:49 -04001785 zw = exponential2(zw, true);
John Bauman89401822014-05-06 15:04:28 -04001786 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001787 case FOG_EXP2:
John Bauman89401822014-05-06 15:04:28 -04001788 zw *= *Pointer<Float4>(r.data + OFFSET(DrawData,fog.densityE2));
1789 zw *= zw;
John Bauman19bac1e2014-05-06 15:23:49 -04001790 zw = exponential2(zw, true);
John Bauman89401822014-05-06 15:04:28 -04001791 zw = Rcp_pp(zw);
1792 break;
1793 default:
1794 ASSERT(false);
1795 }
1796 }
1797
Alexis Hetu96517182015-04-15 10:30:23 -04001798 void PixelRoutine::specularPixel(Vector4s &current, Vector4s &specular)
John Bauman89401822014-05-06 15:04:28 -04001799 {
1800 if(!state.specularAdd)
1801 {
1802 return;
1803 }
1804
John Bauman19bac1e2014-05-06 15:23:49 -04001805 current.x = AddSat(current.x, specular.x);
1806 current.y = AddSat(current.y, specular.y);
1807 current.z = AddSat(current.z, specular.z);
John Bauman89401822014-05-06 15:04:28 -04001808 }
1809
1810 void PixelRoutine::writeDepth(Registers &r, Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &zMask)
1811 {
1812 if(!state.depthWriteEnable)
1813 {
1814 return;
1815 }
1816
1817 Float4 Z = z;
1818
John Bauman19bac1e2014-05-06 15:23:49 -04001819 if(shader && shader->depthOverride())
John Bauman89401822014-05-06 15:04:28 -04001820 {
1821 if(complementaryDepthBuffer)
1822 {
John Bauman19bac1e2014-05-06 15:23:49 -04001823 Z = Float4(1.0f) - r.oDepth;
John Bauman89401822014-05-06 15:04:28 -04001824 }
1825 else
1826 {
1827 Z = r.oDepth;
1828 }
1829 }
1830
1831 Pointer<Byte> buffer;
1832 Int pitch;
1833
1834 if(!state.quadLayoutDepthBuffer)
1835 {
1836 buffer = zBuffer + 4 * x;
1837 pitch = *Pointer<Int>(r.data + OFFSET(DrawData,depthPitchB));
1838 }
1839 else
1840 {
1841 buffer = zBuffer + 8 * x;
1842 }
1843
1844 if(q > 0)
1845 {
1846 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,depthSliceB));
1847 }
1848
1849 Float4 zValue;
1850
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001851 if(state.depthCompareMode != DEPTH_NEVER || (state.depthCompareMode != DEPTH_ALWAYS && !state.depthWriteEnable))
John Bauman89401822014-05-06 15:04:28 -04001852 {
1853 if(!state.quadLayoutDepthBuffer)
1854 {
1855 // FIXME: Properly optimizes?
1856 zValue.xy = *Pointer<Float4>(buffer);
1857 zValue.zw = *Pointer<Float4>(buffer + pitch - 8);
1858 }
1859 else
1860 {
1861 zValue = *Pointer<Float4>(buffer, 16);
1862 }
1863 }
1864
1865 Z = As<Float4>(As<Int4>(Z) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X) + zMask * 16, 16));
1866 zValue = As<Float4>(As<Int4>(zValue) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X) + zMask * 16, 16));
1867 Z = As<Float4>(As<Int4>(Z) | As<Int4>(zValue));
1868
1869 if(!state.quadLayoutDepthBuffer)
1870 {
1871 // FIXME: Properly optimizes?
1872 *Pointer<Float2>(buffer) = Float2(Z.xy);
1873 *Pointer<Float2>(buffer + pitch) = Float2(Z.zw);
1874 }
1875 else
1876 {
1877 *Pointer<Float4>(buffer, 16) = Z;
1878 }
1879 }
1880
1881 void PixelRoutine::writeStencil(Registers &r, Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &zMask, Int &cMask)
1882 {
1883 if(!state.stencilActive)
1884 {
1885 return;
1886 }
1887
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001888 if(state.stencilPassOperation == OPERATION_KEEP && state.stencilZFailOperation == OPERATION_KEEP && state.stencilFailOperation == OPERATION_KEEP)
John Bauman89401822014-05-06 15:04:28 -04001889 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001890 if(!state.twoSidedStencil || (state.stencilPassOperationCCW == OPERATION_KEEP && state.stencilZFailOperationCCW == OPERATION_KEEP && state.stencilFailOperationCCW == OPERATION_KEEP))
John Bauman89401822014-05-06 15:04:28 -04001891 {
1892 return;
1893 }
1894 }
1895
1896 if(state.stencilWriteMasked && (!state.twoSidedStencil || state.stencilWriteMaskedCCW))
1897 {
1898 return;
1899 }
1900
1901 Pointer<Byte> buffer = sBuffer + 2 * x;
1902
1903 if(q > 0)
1904 {
1905 buffer += q * *Pointer<Int>(r.data + OFFSET(DrawData,stencilSliceB));
1906 }
1907
1908 Byte8 bufferValue = As<Byte8>(Long1(*Pointer<UInt>(buffer)));
1909
1910 Byte8 newValue;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001911 stencilOperation(r, newValue, bufferValue, state.stencilPassOperation, state.stencilZFailOperation, state.stencilFailOperation, false, zMask, sMask);
John Bauman89401822014-05-06 15:04:28 -04001912
1913 if(!state.noStencilWriteMask)
1914 {
1915 Byte8 maskedValue = bufferValue;
1916 newValue &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[0].writeMaskQ));
1917 maskedValue &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[0].invWriteMaskQ));
1918 newValue |= maskedValue;
1919 }
1920
1921 if(state.twoSidedStencil)
1922 {
1923 Byte8 newValueCCW;
1924
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001925 stencilOperation(r, newValueCCW, bufferValue, state.stencilPassOperationCCW, state.stencilZFailOperationCCW, state.stencilFailOperationCCW, true, zMask, sMask);
John Bauman89401822014-05-06 15:04:28 -04001926
1927 if(!state.noStencilWriteMaskCCW)
1928 {
1929 Byte8 maskedValue = bufferValue;
1930 newValueCCW &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[1].writeMaskQ));
1931 maskedValue &= *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[1].invWriteMaskQ));
1932 newValueCCW |= maskedValue;
1933 }
1934
1935 newValue &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,clockwiseMask));
1936 newValueCCW &= *Pointer<Byte8>(r.primitive + OFFSET(Primitive,invClockwiseMask));
1937 newValue |= newValueCCW;
1938 }
1939
1940 newValue &= *Pointer<Byte8>(r.constants + OFFSET(Constants,maskB4Q) + 8 * cMask);
1941 bufferValue &= *Pointer<Byte8>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * cMask);
1942 newValue |= bufferValue;
1943
1944 *Pointer<UInt>(buffer) = UInt(As<Long>(newValue));
1945 }
1946
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001947 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 -04001948 {
1949 Byte8 &pass = newValue;
1950 Byte8 fail;
1951 Byte8 zFail;
1952
1953 stencilOperation(r, pass, bufferValue, stencilPassOperation, CCW);
1954
1955 if(stencilZFailOperation != stencilPassOperation)
1956 {
1957 stencilOperation(r, zFail, bufferValue, stencilZFailOperation, CCW);
1958 }
1959
1960 if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
1961 {
1962 stencilOperation(r, fail, bufferValue, stencilFailOperation, CCW);
1963 }
1964
1965 if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
1966 {
1967 if(state.depthTestActive && stencilZFailOperation != stencilPassOperation) // zMask valid and values not the same
1968 {
1969 pass &= *Pointer<Byte8>(r.constants + OFFSET(Constants,maskB4Q) + 8 * zMask);
1970 zFail &= *Pointer<Byte8>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * zMask);
1971 pass |= zFail;
1972 }
1973
1974 pass &= *Pointer<Byte8>(r.constants + OFFSET(Constants,maskB4Q) + 8 * sMask);
1975 fail &= *Pointer<Byte8>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * sMask);
1976 pass |= fail;
1977 }
1978 }
1979
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001980 void PixelRoutine::stencilOperation(Registers &r, Byte8 &output, Byte8 &bufferValue, StencilOperation operation, bool CCW)
John Bauman89401822014-05-06 15:04:28 -04001981 {
1982 switch(operation)
1983 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001984 case OPERATION_KEEP:
John Bauman89401822014-05-06 15:04:28 -04001985 output = bufferValue;
1986 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001987 case OPERATION_ZERO:
John Bauman89401822014-05-06 15:04:28 -04001988 output = Byte8(0x0000000000000000);
1989 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001990 case OPERATION_REPLACE:
John Bauman89401822014-05-06 15:04:28 -04001991 output = *Pointer<Byte8>(r.data + OFFSET(DrawData,stencil[CCW].referenceQ));
1992 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001993 case OPERATION_INCRSAT:
John Bauman89401822014-05-06 15:04:28 -04001994 output = AddSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));
1995 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001996 case OPERATION_DECRSAT:
John Bauman89401822014-05-06 15:04:28 -04001997 output = SubSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));
1998 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001999 case OPERATION_INVERT:
John Bauman89401822014-05-06 15:04:28 -04002000 output = bufferValue ^ Byte8(0xFFFFFFFFFFFFFFFF);
2001 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002002 case OPERATION_INCR:
John Bauman89401822014-05-06 15:04:28 -04002003 output = bufferValue + Byte8(1, 1, 1, 1, 1, 1, 1, 1);
2004 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002005 case OPERATION_DECR:
John Bauman89401822014-05-06 15:04:28 -04002006 output = bufferValue - Byte8(1, 1, 1, 1, 1, 1, 1, 1);
2007 break;
2008 default:
2009 ASSERT(false);
2010 }
2011 }
2012
Alexis Hetu96517182015-04-15 10:30:23 -04002013 void PixelRoutine::sampleTexture(Registers &r, Vector4s &c, int coordinates, int stage, bool project)
John Bauman89401822014-05-06 15:04:28 -04002014 {
John Bauman19bac1e2014-05-06 15:23:49 -04002015 Float4 u = r.vf[2 + coordinates].x;
2016 Float4 v = r.vf[2 + coordinates].y;
2017 Float4 w = r.vf[2 + coordinates].z;
2018 Float4 q = r.vf[2 + coordinates].w;
John Bauman89401822014-05-06 15:04:28 -04002019
2020 if(perturbate)
2021 {
2022 u += r.du;
2023 v += r.dv;
2024
2025 perturbate = false;
2026 }
2027
2028 sampleTexture(r, c, stage, u, v, w, q, project);
2029 }
2030
Alexis Hetu96517182015-04-15 10:30:23 -04002031 void PixelRoutine::sampleTexture(Registers &r, Vector4s &c, int stage, Float4 &u, Float4 &v, Float4 &w, Float4 &q, bool project, bool bias, bool fixed12)
John Bauman89401822014-05-06 15:04:28 -04002032 {
John Bauman19bac1e2014-05-06 15:23:49 -04002033 Vector4f dsx;
2034 Vector4f dsy;
John Bauman89401822014-05-06 15:04:28 -04002035
2036 sampleTexture(r, c, stage, u, v, w, q, dsx, dsy, project, bias, fixed12, false);
2037 }
2038
Alexis Hetu96517182015-04-15 10:30:23 -04002039 void PixelRoutine::sampleTexture(Registers &r, Vector4s &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 -04002040 {
2041 #if PERF_PROFILE
2042 Long texTime = Ticks();
2043 #endif
2044
2045 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap) + stage * sizeof(Texture);
2046
2047 if(!project)
2048 {
2049 sampler[stage]->sampleTexture(texture, c, u, v, w, q, dsx, dsy, bias, fixed12, gradients, lodProvided);
2050 }
2051 else
2052 {
2053 Float4 rq = reciprocal(q);
2054
2055 Float4 u_q = u * rq;
2056 Float4 v_q = v * rq;
2057 Float4 w_q = w * rq;
2058
2059 sampler[stage]->sampleTexture(texture, c, u_q, v_q, w_q, q, dsx, dsy, bias, fixed12, gradients, lodProvided);
2060 }
2061
2062 #if PERF_PROFILE
2063 r.cycles[PERF_TEX] += Ticks() - texTime;
2064 #endif
2065 }
2066
John Bauman19bac1e2014-05-06 15:23:49 -04002067 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)
2068 {
2069 if(sampler.type == Shader::PARAMETER_SAMPLER && sampler.rel.type == Shader::PARAMETER_VOID)
2070 {
2071 sampleTexture(r, c, sampler.index, u, v, w, q, dsx, dsy, project, bias, gradients, lodProvided);
2072 }
2073 else
2074 {
Alexis Hetu96517182015-04-15 10:30:23 -04002075 Int index = As<Int>(Float(fetchRegisterF(r, sampler).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04002076
Alexis Hetu0b65c5e2015-03-31 11:48:57 -04002077 for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04002078 {
2079 if(shader->usesSampler(i))
2080 {
2081 If(index == i)
2082 {
2083 sampleTexture(r, c, i, u, v, w, q, dsx, dsy, project, bias, gradients, lodProvided);
2084 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
2085 }
2086 }
2087 }
2088 }
2089 }
2090
2091 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 -04002092 {
2093 #if PERF_PROFILE
2094 Long texTime = Ticks();
2095 #endif
2096
2097 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap) + stage * sizeof(Texture);
2098
2099 if(!project)
2100 {
2101 sampler[stage]->sampleTexture(texture, c, u, v, w, q, dsx, dsy, bias, gradients, lodProvided);
2102 }
2103 else
2104 {
2105 Float4 rq = reciprocal(q);
2106
2107 Float4 u_q = u * rq;
2108 Float4 v_q = v * rq;
2109 Float4 w_q = w * rq;
2110
2111 sampler[stage]->sampleTexture(texture, c, u_q, v_q, w_q, q, dsx, dsy, bias, gradients, lodProvided);
2112 }
2113
2114 #if PERF_PROFILE
2115 r.cycles[PERF_TEX] += Ticks() - texTime;
2116 #endif
2117 }
2118
John Bauman19bac1e2014-05-06 15:23:49 -04002119 void PixelRoutine::clampColor(Vector4f oC[4])
John Bauman89401822014-05-06 15:04:28 -04002120 {
2121 for(int index = 0; index < 4; index++)
2122 {
2123 if(!state.colorWriteActive(index) && !(index == 0 && state.alphaTestActive()))
2124 {
2125 continue;
2126 }
2127
2128 switch(state.targetFormat[index])
2129 {
2130 case FORMAT_NULL:
2131 break;
2132 case FORMAT_A16B16G16R16:
2133 case FORMAT_A8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002134 case FORMAT_A8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04002135 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002136 case FORMAT_X8B8G8R8:
John Bauman66b8ab22014-05-06 15:57:45 -04002137 case FORMAT_A8:
John Bauman89401822014-05-06 15:04:28 -04002138 case FORMAT_G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04002139 oC[index].x = Max(oC[index].x, Float4(0.0f)); oC[index].x = Min(oC[index].x, Float4(1.0f));
2140 oC[index].y = Max(oC[index].y, Float4(0.0f)); oC[index].y = Min(oC[index].y, Float4(1.0f));
2141 oC[index].z = Max(oC[index].z, Float4(0.0f)); oC[index].z = Min(oC[index].z, Float4(1.0f));
2142 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 -04002143 break;
2144 case FORMAT_R32F:
2145 case FORMAT_G32R32F:
2146 case FORMAT_A32B32G32R32F:
2147 break;
2148 default:
2149 ASSERT(false);
2150 }
2151 }
2152 }
2153
Alexis Hetu96517182015-04-15 10:30:23 -04002154 void PixelRoutine::rasterOperation(Vector4s &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 -04002155 {
2156 if(!state.colorWriteActive(0))
2157 {
2158 return;
2159 }
2160
John Bauman19bac1e2014-05-06 15:23:49 -04002161 Vector4f oC;
John Bauman89401822014-05-06 15:04:28 -04002162
2163 switch(state.targetFormat[0])
2164 {
2165 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002166 case FORMAT_X8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04002167 case FORMAT_A8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002168 case FORMAT_A8B8G8R8:
John Bauman66b8ab22014-05-06 15:57:45 -04002169 case FORMAT_A8:
John Bauman89401822014-05-06 15:04:28 -04002170 case FORMAT_G16R16:
2171 case FORMAT_A16B16G16R16:
2172 if(!postBlendSRGB && state.writeSRGB)
2173 {
2174 linearToSRGB12_16(r, current);
2175 }
2176 else
2177 {
John Bauman19bac1e2014-05-06 15:23:49 -04002178 current.x <<= 4;
2179 current.y <<= 4;
2180 current.z <<= 4;
2181 current.w <<= 4;
John Bauman89401822014-05-06 15:04:28 -04002182 }
2183
2184 fogBlend(r, current, fog, r.z[0], r.rhw);
2185
2186 for(unsigned int q = 0; q < state.multiSample; q++)
2187 {
2188 Pointer<Byte> buffer = cBuffer + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[0]));
Alexis Hetu96517182015-04-15 10:30:23 -04002189 Vector4s color = current;
John Bauman89401822014-05-06 15:04:28 -04002190
2191 if(state.multiSampleMask & (1 << q))
2192 {
2193 alphaBlend(r, 0, buffer, color, x);
2194 writeColor(r, 0, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2195 }
2196 }
2197 break;
2198 case FORMAT_R32F:
2199 case FORMAT_G32R32F:
2200 case FORMAT_A32B32G32R32F:
2201 convertSigned12(oC, current);
2202 fogBlend(r, oC, fog, r.z[0], r.rhw);
2203
2204 for(unsigned int q = 0; q < state.multiSample; q++)
2205 {
2206 Pointer<Byte> buffer = cBuffer + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[0]));
John Bauman19bac1e2014-05-06 15:23:49 -04002207 Vector4f color = oC;
John Bauman89401822014-05-06 15:04:28 -04002208
2209 if(state.multiSampleMask & (1 << q))
2210 {
2211 alphaBlend(r, 0, buffer, color, x);
2212 writeColor(r, 0, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2213 }
2214 }
2215 break;
2216 default:
2217 ASSERT(false);
2218 }
2219 }
2220
John Bauman19bac1e2014-05-06 15:23:49 -04002221 void PixelRoutine::rasterOperation(Vector4f oC[4], Registers &r, Float4 &fog, Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4])
John Bauman89401822014-05-06 15:04:28 -04002222 {
2223 for(int index = 0; index < 4; index++)
2224 {
2225 if(!state.colorWriteActive(index))
2226 {
2227 continue;
2228 }
2229
2230 if(!postBlendSRGB && state.writeSRGB)
2231 {
John Bauman19bac1e2014-05-06 15:23:49 -04002232 oC[index].x = linearToSRGB(oC[index].x);
2233 oC[index].y = linearToSRGB(oC[index].y);
2234 oC[index].z = linearToSRGB(oC[index].z);
John Bauman89401822014-05-06 15:04:28 -04002235 }
2236
2237 if(index == 0)
2238 {
2239 fogBlend(r, oC[index], fog, r.z[0], r.rhw);
2240 }
2241
2242 switch(state.targetFormat[index])
2243 {
2244 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002245 case FORMAT_X8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04002246 case FORMAT_A8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002247 case FORMAT_A8B8G8R8:
John Bauman66b8ab22014-05-06 15:57:45 -04002248 case FORMAT_A8:
John Bauman89401822014-05-06 15:04:28 -04002249 case FORMAT_G16R16:
2250 case FORMAT_A16B16G16R16:
2251 for(unsigned int q = 0; q < state.multiSample; q++)
2252 {
2253 Pointer<Byte> buffer = cBuffer[index] + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[index]));
Alexis Hetu96517182015-04-15 10:30:23 -04002254 Vector4s color;
John Bauman89401822014-05-06 15:04:28 -04002255
John Bauman19bac1e2014-05-06 15:23:49 -04002256 color.x = convertFixed16(oC[index].x, false);
2257 color.y = convertFixed16(oC[index].y, false);
2258 color.z = convertFixed16(oC[index].z, false);
2259 color.w = convertFixed16(oC[index].w, false);
John Bauman89401822014-05-06 15:04:28 -04002260
2261 if(state.multiSampleMask & (1 << q))
2262 {
2263 alphaBlend(r, index, buffer, color, x);
2264 writeColor(r, index, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2265 }
2266 }
2267 break;
2268 case FORMAT_R32F:
2269 case FORMAT_G32R32F:
2270 case FORMAT_A32B32G32R32F:
2271 for(unsigned int q = 0; q < state.multiSample; q++)
2272 {
2273 Pointer<Byte> buffer = cBuffer[index] + q * *Pointer<Int>(r.data + OFFSET(DrawData,colorSliceB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002274 Vector4f color = oC[index];
John Bauman89401822014-05-06 15:04:28 -04002275
2276 if(state.multiSampleMask & (1 << q))
2277 {
2278 alphaBlend(r, index, buffer, color, x);
2279 writeColor(r, index, buffer, x, color, sMask[q], zMask[q], cMask[q]);
2280 }
2281 }
2282 break;
2283 default:
2284 ASSERT(false);
2285 }
2286 }
2287 }
2288
Alexis Hetu96517182015-04-15 10:30:23 -04002289 void PixelRoutine::blendFactor(Registers &r, const Vector4s &blendFactor, const Vector4s &current, const Vector4s &pixel, BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -04002290 {
2291 switch(blendFactorActive)
2292 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002293 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04002294 // Optimized
2295 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002296 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04002297 // Optimized
2298 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002299 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002300 blendFactor.x = current.x;
2301 blendFactor.y = current.y;
2302 blendFactor.z = current.z;
John Bauman89401822014-05-06 15:04:28 -04002303 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002304 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002305 blendFactor.x = Short4(0xFFFFu) - current.x;
2306 blendFactor.y = Short4(0xFFFFu) - current.y;
2307 blendFactor.z = Short4(0xFFFFu) - current.z;
John Bauman89401822014-05-06 15:04:28 -04002308 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002309 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002310 blendFactor.x = pixel.x;
2311 blendFactor.y = pixel.y;
2312 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002313 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002314 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002315 blendFactor.x = Short4(0xFFFFu) - pixel.x;
2316 blendFactor.y = Short4(0xFFFFu) - pixel.y;
2317 blendFactor.z = Short4(0xFFFFu) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002318 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002319 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002320 blendFactor.x = current.w;
2321 blendFactor.y = current.w;
2322 blendFactor.z = current.w;
John Bauman89401822014-05-06 15:04:28 -04002323 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002324 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002325 blendFactor.x = Short4(0xFFFFu) - current.w;
2326 blendFactor.y = Short4(0xFFFFu) - current.w;
2327 blendFactor.z = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -04002328 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002329 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002330 blendFactor.x = pixel.w;
2331 blendFactor.y = pixel.w;
2332 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002333 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002334 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002335 blendFactor.x = Short4(0xFFFFu) - pixel.w;
2336 blendFactor.y = Short4(0xFFFFu) - pixel.w;
2337 blendFactor.z = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002338 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002339 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04002340 blendFactor.x = Short4(0xFFFFu) - pixel.w;
2341 blendFactor.x = Min(As<UShort4>(blendFactor.x), As<UShort4>(current.w));
2342 blendFactor.y = blendFactor.x;
2343 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -04002344 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002345 case BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04002346 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[0]));
2347 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[1]));
2348 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[2]));
John Bauman89401822014-05-06 15:04:28 -04002349 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002350 case BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04002351 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[0]));
2352 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[1]));
2353 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[2]));
John Bauman89401822014-05-06 15:04:28 -04002354 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002355 case BLEND_CONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002356 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
2357 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
2358 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04002359 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002360 case BLEND_INVCONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002361 blendFactor.x = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
2362 blendFactor.y = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
2363 blendFactor.z = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04002364 break;
2365 default:
2366 ASSERT(false);
2367 }
2368 }
2369
Alexis Hetu96517182015-04-15 10:30:23 -04002370 void PixelRoutine::blendFactorAlpha(Registers &r, const Vector4s &blendFactor, const Vector4s &current, const Vector4s &pixel, BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -04002371 {
2372 switch(blendFactorAlphaActive)
2373 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002374 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04002375 // Optimized
2376 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002377 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04002378 // Optimized
2379 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002380 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002381 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -04002382 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002383 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04002384 blendFactor.w = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -04002385 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002386 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002387 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002388 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002389 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002390 blendFactor.w = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002391 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002392 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002393 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -04002394 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002395 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002396 blendFactor.w = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -04002397 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002398 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002399 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002400 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002401 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002402 blendFactor.w = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002403 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002404 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04002405 blendFactor.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002406 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002407 case BLEND_CONSTANT:
2408 case BLEND_CONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002409 blendFactor.w = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.blendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04002410 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002411 case BLEND_INVCONSTANT:
2412 case BLEND_INVCONSTANTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002413 blendFactor.w = *Pointer<Short4>(r.data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -04002414 break;
2415 default:
2416 ASSERT(false);
2417 }
2418 }
2419
Alexis Hetu96517182015-04-15 10:30:23 -04002420 void PixelRoutine::alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4s &current, Int &x)
John Bauman89401822014-05-06 15:04:28 -04002421 {
2422 if(!state.alphaBlendActive)
2423 {
2424 return;
2425 }
2426
2427 Pointer<Byte> buffer;
2428
Alexis Hetu96517182015-04-15 10:30:23 -04002429 Vector4s pixel;
John Bauman89401822014-05-06 15:04:28 -04002430 Short4 c01;
2431 Short4 c23;
2432
2433 // Read pixel
2434 switch(state.targetFormat[index])
2435 {
2436 case FORMAT_A8R8G8B8:
2437 buffer = cBuffer + 4 * x;
2438 c01 = *Pointer<Short4>(buffer);
2439 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2440 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04002441 pixel.z = c01;
2442 pixel.y = c01;
2443 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
2444 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
2445 pixel.x = pixel.z;
2446 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
2447 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
2448 pixel.y = pixel.z;
2449 pixel.w = pixel.x;
2450 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
2451 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
2452 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
2453 pixel.w = UnpackHigh(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002454 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002455 case FORMAT_A8B8G8R8:
2456 buffer = cBuffer + 4 * x;
2457 c01 = *Pointer<Short4>(buffer);
2458 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2459 c23 = *Pointer<Short4>(buffer);
2460 pixel.z = c01;
2461 pixel.y = c01;
2462 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
2463 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
2464 pixel.x = pixel.z;
2465 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
2466 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
2467 pixel.y = pixel.z;
2468 pixel.w = pixel.x;
2469 pixel.x = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
2470 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
2471 pixel.z = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
2472 pixel.w = UnpackHigh(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
2473 break;
John Bauman66b8ab22014-05-06 15:57:45 -04002474 case FORMAT_A8:
2475 buffer = cBuffer + 1 * x;
2476 pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 0);
2477 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2478 pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 1);
2479 pixel.w = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
2480 pixel.x = Short4(0x0000);
2481 pixel.y = Short4(0x0000);
2482 pixel.z = Short4(0x0000);
2483 break;
John Bauman89401822014-05-06 15:04:28 -04002484 case FORMAT_X8R8G8B8:
2485 buffer = cBuffer + 4 * x;
2486 c01 = *Pointer<Short4>(buffer);
2487 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2488 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04002489 pixel.z = c01;
2490 pixel.y = c01;
2491 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
2492 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
2493 pixel.x = pixel.z;
2494 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
2495 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
2496 pixel.y = pixel.z;
2497 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
2498 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
2499 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
2500 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002501 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002502 case FORMAT_X8B8G8R8:
2503 buffer = cBuffer + 4 * x;
2504 c01 = *Pointer<Short4>(buffer);
2505 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2506 c23 = *Pointer<Short4>(buffer);
2507 pixel.z = c01;
2508 pixel.y = c01;
2509 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
2510 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
2511 pixel.x = pixel.z;
2512 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
2513 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
2514 pixel.y = pixel.z;
2515 pixel.w = pixel.x;
2516 pixel.x = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
2517 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
2518 pixel.z = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
2519 pixel.w = Short4(0xFFFFu);
2520 break;
John Bauman89401822014-05-06 15:04:28 -04002521 case FORMAT_A8G8R8B8Q:
2522 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002523 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2524 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2525 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
2526 // pixel.w = UnpackHigh(As<Byte8>(pixel.w), *Pointer<Byte8>(cBuffer + 8 * x + 8));
John Bauman89401822014-05-06 15:04:28 -04002527 break;
2528 case FORMAT_X8G8R8B8Q:
2529 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002530 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2531 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
2532 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
2533 // pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002534 break;
2535 case FORMAT_A16B16G16R16:
2536 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002537 pixel.x = *Pointer<Short4>(buffer + 8 * x);
2538 pixel.y = *Pointer<Short4>(buffer + 8 * x + 8);
John Bauman89401822014-05-06 15:04:28 -04002539 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002540 pixel.z = *Pointer<Short4>(buffer + 8 * x);
2541 pixel.w = *Pointer<Short4>(buffer + 8 * x + 8);
2542 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04002543 break;
2544 case FORMAT_G16R16:
2545 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002546 pixel.x = *Pointer<Short4>(buffer + 4 * x);
John Bauman89401822014-05-06 15:04:28 -04002547 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002548 pixel.y = *Pointer<Short4>(buffer + 4 * x);
2549 pixel.z = pixel.x;
2550 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.y));
2551 pixel.z = As<Short4>(UnpackHigh(pixel.z, pixel.y));
2552 pixel.y = pixel.z;
2553 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.z));
2554 pixel.y = As<Short4>(UnpackHigh(pixel.y, pixel.z));
2555 pixel.z = Short4(0xFFFFu);
2556 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04002557 break;
2558 default:
2559 ASSERT(false);
2560 }
2561
2562 if(postBlendSRGB && state.writeSRGB)
2563 {
2564 sRGBtoLinear16_16(r, pixel);
2565 }
2566
2567 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
Alexis Hetu96517182015-04-15 10:30:23 -04002568 Vector4s sourceFactor;
2569 Vector4s destFactor;
John Bauman89401822014-05-06 15:04:28 -04002570
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002571 blendFactor(r, sourceFactor, current, pixel, state.sourceBlendFactor);
2572 blendFactor(r, destFactor, current, pixel, state.destBlendFactor);
John Bauman89401822014-05-06 15:04:28 -04002573
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002574 if(state.sourceBlendFactor != BLEND_ONE && state.sourceBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002575 {
John Bauman19bac1e2014-05-06 15:23:49 -04002576 current.x = MulHigh(As<UShort4>(current.x), As<UShort4>(sourceFactor.x));
2577 current.y = MulHigh(As<UShort4>(current.y), As<UShort4>(sourceFactor.y));
2578 current.z = MulHigh(As<UShort4>(current.z), As<UShort4>(sourceFactor.z));
John Bauman89401822014-05-06 15:04:28 -04002579 }
2580
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002581 if(state.destBlendFactor != BLEND_ONE && state.destBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002582 {
John Bauman19bac1e2014-05-06 15:23:49 -04002583 pixel.x = MulHigh(As<UShort4>(pixel.x), As<UShort4>(destFactor.x));
2584 pixel.y = MulHigh(As<UShort4>(pixel.y), As<UShort4>(destFactor.y));
2585 pixel.z = MulHigh(As<UShort4>(pixel.z), As<UShort4>(destFactor.z));
John Bauman89401822014-05-06 15:04:28 -04002586 }
2587
2588 switch(state.blendOperation)
2589 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002590 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002591 current.x = AddSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
2592 current.y = AddSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
2593 current.z = AddSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002594 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002595 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002596 current.x = SubSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
2597 current.y = SubSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
2598 current.z = SubSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002599 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002600 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002601 current.x = SubSat(As<UShort4>(pixel.x), As<UShort4>(current.x));
2602 current.y = SubSat(As<UShort4>(pixel.y), As<UShort4>(current.y));
2603 current.z = SubSat(As<UShort4>(pixel.z), As<UShort4>(current.z));
John Bauman89401822014-05-06 15:04:28 -04002604 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002605 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002606 current.x = Min(As<UShort4>(current.x), As<UShort4>(pixel.x));
2607 current.y = Min(As<UShort4>(current.y), As<UShort4>(pixel.y));
2608 current.z = Min(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002609 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002610 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002611 current.x = Max(As<UShort4>(current.x), As<UShort4>(pixel.x));
2612 current.y = Max(As<UShort4>(current.y), As<UShort4>(pixel.y));
2613 current.z = Max(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04002614 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002615 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04002616 // No operation
2617 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002618 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002619 current.x = pixel.x;
2620 current.y = pixel.y;
2621 current.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002622 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002623 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002624 current.x = Short4(0x0000, 0x0000, 0x0000, 0x0000);
2625 current.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
2626 current.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04002627 break;
2628 default:
2629 ASSERT(false);
2630 }
2631
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002632 blendFactorAlpha(r, sourceFactor, current, pixel, state.sourceBlendFactorAlpha);
2633 blendFactorAlpha(r, destFactor, current, pixel, state.destBlendFactorAlpha);
John Bauman89401822014-05-06 15:04:28 -04002634
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002635 if(state.sourceBlendFactorAlpha != BLEND_ONE && state.sourceBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002636 {
John Bauman19bac1e2014-05-06 15:23:49 -04002637 current.w = MulHigh(As<UShort4>(current.w), As<UShort4>(sourceFactor.w));
John Bauman89401822014-05-06 15:04:28 -04002638 }
2639
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002640 if(state.destBlendFactorAlpha != BLEND_ONE && state.destBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002641 {
John Bauman19bac1e2014-05-06 15:23:49 -04002642 pixel.w = MulHigh(As<UShort4>(pixel.w), As<UShort4>(destFactor.w));
John Bauman89401822014-05-06 15:04:28 -04002643 }
2644
2645 switch(state.blendOperationAlpha)
2646 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002647 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002648 current.w = AddSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002649 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002650 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002651 current.w = SubSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002652 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002653 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002654 current.w = SubSat(As<UShort4>(pixel.w), As<UShort4>(current.w));
John Bauman89401822014-05-06 15:04:28 -04002655 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002656 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002657 current.w = Min(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002658 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002659 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002660 current.w = Max(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04002661 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002662 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04002663 // No operation
2664 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002665 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002666 current.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002667 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002668 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002669 current.w = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04002670 break;
2671 default:
2672 ASSERT(false);
2673 }
2674 }
2675
Alexis Hetu96517182015-04-15 10:30:23 -04002676 void PixelRoutine::writeColor(Registers &r, int index, Pointer<Byte> &cBuffer, Int &x, Vector4s &current, Int &sMask, Int &zMask, Int &cMask)
John Bauman89401822014-05-06 15:04:28 -04002677 {
2678 if(!state.colorWriteActive(index))
2679 {
2680 return;
2681 }
2682
2683 if(postBlendSRGB && state.writeSRGB)
2684 {
2685 linearToSRGB16_16(r, current);
2686 }
2687
2688 if(exactColorRounding)
2689 {
2690 switch(state.targetFormat[index])
2691 {
2692 case FORMAT_X8G8R8B8Q:
2693 case FORMAT_A8G8R8B8Q:
2694 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002695 case FORMAT_X8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04002696 case FORMAT_A8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002697 case FORMAT_A8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04002698 {
John Bauman19bac1e2014-05-06 15:23:49 -04002699 current.x = current.x - As<Short4>(As<UShort4>(current.x) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
2700 current.y = current.y - As<Short4>(As<UShort4>(current.y) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
2701 current.z = current.z - As<Short4>(As<UShort4>(current.z) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
2702 current.w = current.w - As<Short4>(As<UShort4>(current.w) >> 8) + Short4(0x0080, 0x0080, 0x0080, 0x0080);
John Bauman89401822014-05-06 15:04:28 -04002703 }
2704 break;
2705 }
2706 }
2707
2708 int rgbaWriteMask = state.colorWriteActive(index);
2709 int bgraWriteMask = rgbaWriteMask & 0x0000000A | (rgbaWriteMask & 0x00000001) << 2 | (rgbaWriteMask & 0x00000004) >> 2;
2710 int brgaWriteMask = rgbaWriteMask & 0x00000008 | (rgbaWriteMask & 0x00000001) << 1 | (rgbaWriteMask & 0x00000002) << 1 | (rgbaWriteMask & 0x00000004) >> 2;
2711
2712 switch(state.targetFormat[index])
2713 {
2714 case FORMAT_X8G8R8B8Q:
2715 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002716 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2717 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2718 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002719
John Bauman19bac1e2014-05-06 15:23:49 -04002720 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2721 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
John Bauman89401822014-05-06 15:04:28 -04002722 break;
2723 case FORMAT_A8G8R8B8Q:
2724 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04002725 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2726 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2727 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
2728 // current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002729
John Bauman19bac1e2014-05-06 15:23:49 -04002730 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2731 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04002732 break;
2733 case FORMAT_X8R8G8B8:
2734 case FORMAT_A8R8G8B8:
2735 if(state.targetFormat[index] == FORMAT_X8R8G8B8 || rgbaWriteMask == 0x7)
2736 {
John Bauman19bac1e2014-05-06 15:23:49 -04002737 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2738 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2739 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002740
John Bauman19bac1e2014-05-06 15:23:49 -04002741 current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2742 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
John Bauman89401822014-05-06 15:04:28 -04002743
John Bauman19bac1e2014-05-06 15:23:49 -04002744 current.x = current.z;
2745 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
2746 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
2747 current.y = current.z;
2748 current.z = As<Short4>(UnpackLow(current.z, current.x));
2749 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04002750 }
2751 else
2752 {
John Bauman19bac1e2014-05-06 15:23:49 -04002753 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2754 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2755 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
2756 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04002757
John Bauman19bac1e2014-05-06 15:23:49 -04002758 current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
2759 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04002760
John Bauman19bac1e2014-05-06 15:23:49 -04002761 current.x = current.z;
2762 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
2763 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
2764 current.y = current.z;
2765 current.z = As<Short4>(UnpackLow(current.z, current.x));
2766 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04002767 }
2768 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002769 case FORMAT_X8B8G8R8:
2770 case FORMAT_A8B8G8R8:
2771 if(state.targetFormat[index] == FORMAT_X8B8G8R8 || rgbaWriteMask == 0x7)
2772 {
2773 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2774 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2775 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
2776
2777 current.z = As<Short4>(Pack(As<UShort4>(current.x), As<UShort4>(current.z)));
2778 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
2779
2780 current.x = current.z;
2781 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
2782 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
2783 current.y = current.z;
2784 current.z = As<Short4>(UnpackLow(current.z, current.x));
2785 current.y = As<Short4>(UnpackHigh(current.y, current.x));
2786 }
2787 else
2788 {
2789 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
2790 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
2791 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
2792 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
2793
2794 current.z = As<Short4>(Pack(As<UShort4>(current.x), As<UShort4>(current.z)));
2795 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
2796
2797 current.x = current.z;
2798 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
2799 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
2800 current.y = current.z;
2801 current.z = As<Short4>(UnpackLow(current.z, current.x));
2802 current.y = As<Short4>(UnpackHigh(current.y, current.x));
2803 }
2804 break;
John Bauman66b8ab22014-05-06 15:57:45 -04002805 case FORMAT_A8:
2806 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
2807 current.w = As<Short4>(Pack(As<UShort4>(current.w), As<UShort4>(current.w)));
2808 break;
John Bauman89401822014-05-06 15:04:28 -04002809 case FORMAT_G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04002810 current.z = current.x;
2811 current.x = As<Short4>(UnpackLow(current.x, current.y));
2812 current.z = As<Short4>(UnpackHigh(current.z, current.y));
2813 current.y = current.z;
John Bauman89401822014-05-06 15:04:28 -04002814 break;
2815 case FORMAT_A16B16G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04002816 transpose4x4(current.x, current.y, current.z, current.w);
John Bauman89401822014-05-06 15:04:28 -04002817 break;
2818 case FORMAT_R32F:
2819 case FORMAT_G32R32F:
2820 case FORMAT_A32B32G32R32F:
2821 {
John Bauman19bac1e2014-05-06 15:23:49 -04002822 Vector4f oC;
John Bauman89401822014-05-06 15:04:28 -04002823
John Bauman19bac1e2014-05-06 15:23:49 -04002824 oC.x = convertUnsigned16(UShort4(current.x));
2825 oC.y = convertUnsigned16(UShort4(current.y));
2826 oC.z = convertUnsigned16(UShort4(current.z));
2827 oC.w = convertUnsigned16(UShort4(current.w));
John Bauman89401822014-05-06 15:04:28 -04002828
2829 writeColor(r, index, cBuffer, x, oC, sMask, zMask, cMask);
2830 }
2831 return;
2832 default:
2833 ASSERT(false);
2834 }
2835
John Bauman19bac1e2014-05-06 15:23:49 -04002836 Short4 c01 = current.z;
2837 Short4 c23 = current.y;
John Bauman89401822014-05-06 15:04:28 -04002838
2839 Int xMask; // Combination of all masks
2840
2841 if(state.depthTestActive)
2842 {
2843 xMask = zMask;
2844 }
2845 else
2846 {
2847 xMask = cMask;
2848 }
2849
2850 if(state.stencilActive)
2851 {
2852 xMask &= sMask;
2853 }
2854
2855 Pointer<Byte> buffer;
2856 Short4 value;
2857
2858 switch(state.targetFormat[index])
2859 {
2860 case FORMAT_A8G8R8B8Q:
2861 case FORMAT_X8G8R8B8Q: // FIXME: Don't touch alpha?
2862 UNIMPLEMENTED();
2863 // value = *Pointer<Short4>(cBuffer + 8 * x + 0);
2864
2865 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
2866 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
2867 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2868 // {
2869 // Short4 masked = value;
2870 // c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2871 // masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2872 // c01 |= masked;
2873 // }
2874
2875 // c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
2876 // value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
2877 // c01 |= value;
2878 // *Pointer<Short4>(cBuffer + 8 * x + 0) = c01;
2879
2880 // value = *Pointer<Short4>(cBuffer + 8 * x + 8);
2881
2882 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
2883 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
2884 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2885 // {
2886 // Short4 masked = value;
2887 // c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2888 // masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2889 // c23 |= masked;
2890 // }
2891
2892 // c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
2893 // value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
2894 // c23 |= value;
2895 // *Pointer<Short4>(cBuffer + 8 * x + 8) = c23;
2896 break;
2897 case FORMAT_A8R8G8B8:
2898 case FORMAT_X8R8G8B8: // FIXME: Don't touch alpha?
2899 buffer = cBuffer + x * 4;
2900 value = *Pointer<Short4>(buffer);
2901
2902 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
2903 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
2904 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2905 {
2906 Short4 masked = value;
2907 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2908 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2909 c01 |= masked;
2910 }
2911
2912 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
2913 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
2914 c01 |= value;
2915 *Pointer<Short4>(buffer) = c01;
2916
2917 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2918 value = *Pointer<Short4>(buffer);
2919
2920 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
2921 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
2922 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
2923 {
2924 Short4 masked = value;
2925 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
2926 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
2927 c23 |= masked;
2928 }
2929
2930 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
2931 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
2932 c23 |= value;
2933 *Pointer<Short4>(buffer) = c23;
2934 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04002935 case FORMAT_A8B8G8R8:
2936 case FORMAT_X8B8G8R8: // FIXME: Don't touch alpha?
2937 buffer = cBuffer + x * 4;
2938 value = *Pointer<Short4>(buffer);
2939
2940 if((state.targetFormat[index] == FORMAT_A8B8G8R8 && rgbaWriteMask != 0x0000000F) ||
2941 ((state.targetFormat[index] == FORMAT_X8B8G8R8 && rgbaWriteMask != 0x00000007) &&
2942 (state.targetFormat[index] == FORMAT_X8B8G8R8 && rgbaWriteMask != 0x0000000F))) // FIXME: Need for masking when XBGR && Fh?
2943 {
2944 Short4 masked = value;
2945 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[rgbaWriteMask][0]));
2946 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[rgbaWriteMask][0]));
2947 c01 |= masked;
2948 }
2949
2950 c01 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
2951 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
2952 c01 |= value;
2953 *Pointer<Short4>(buffer) = c01;
2954
2955 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2956 value = *Pointer<Short4>(buffer);
2957
2958 if((state.targetFormat[index] == FORMAT_A8B8G8R8 && rgbaWriteMask != 0x0000000F) ||
2959 ((state.targetFormat[index] == FORMAT_X8B8G8R8 && rgbaWriteMask != 0x00000007) &&
2960 (state.targetFormat[index] == FORMAT_X8B8G8R8 && rgbaWriteMask != 0x0000000F))) // FIXME: Need for masking when XBGR && Fh?
2961 {
2962 Short4 masked = value;
2963 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q[rgbaWriteMask][0]));
2964 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q[rgbaWriteMask][0]));
2965 c23 |= masked;
2966 }
2967
2968 c23 &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
2969 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
2970 c23 |= value;
2971 *Pointer<Short4>(buffer) = c23;
2972 break;
John Bauman66b8ab22014-05-06 15:57:45 -04002973 case FORMAT_A8:
2974 if(rgbaWriteMask & 0x00000008)
2975 {
2976 buffer = cBuffer + 1 * x;
2977 Insert(value, *Pointer<Short>(buffer), 0);
2978 Int pitch = *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
2979 Insert(value, *Pointer<Short>(buffer + pitch), 1);
2980 value = UnpackLow(As<Byte8>(value), As<Byte8>(value));
2981
2982 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q) + 8 * xMask);
2983 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * xMask);
2984 current.w |= value;
2985
2986 *Pointer<Short>(buffer) = Extract(current.w, 0);
2987 *Pointer<Short>(buffer + pitch) = Extract(current.w, 1);
2988 }
2989 break;
John Bauman89401822014-05-06 15:04:28 -04002990 case FORMAT_G16R16:
2991 buffer = cBuffer + 4 * x;
2992
2993 value = *Pointer<Short4>(buffer);
2994
2995 if((rgbaWriteMask & 0x00000003) != 0x00000003)
2996 {
2997 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04002998 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman89401822014-05-06 15:04:28 -04002999 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04003000 current.x |= masked;
John Bauman89401822014-05-06 15:04:28 -04003001 }
3002
John Bauman19bac1e2014-05-06 15:23:49 -04003003 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD01Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04003004 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04003005 current.x |= value;
3006 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04003007
3008 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3009
3010 value = *Pointer<Short4>(buffer);
3011
3012 if((rgbaWriteMask & 0x00000003) != 0x00000003)
3013 {
3014 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003015 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman89401822014-05-06 15:04:28 -04003016 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04003017 current.y |= masked;
John Bauman89401822014-05-06 15:04:28 -04003018 }
3019
John Bauman19bac1e2014-05-06 15:23:49 -04003020 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskD23Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04003021 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04003022 current.y |= value;
3023 *Pointer<Short4>(buffer) = current.y;
John Bauman89401822014-05-06 15:04:28 -04003024 break;
3025 case FORMAT_A16B16G16R16:
3026 buffer = cBuffer + 8 * x;
3027
3028 {
3029 value = *Pointer<Short4>(buffer);
3030
3031 if(rgbaWriteMask != 0x0000000F)
3032 {
3033 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003034 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04003035 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04003036 current.x |= masked;
John Bauman89401822014-05-06 15:04:28 -04003037 }
3038
John Bauman19bac1e2014-05-06 15:23:49 -04003039 current.x &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ0Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04003040 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ0Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04003041 current.x |= value;
3042 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04003043 }
3044
3045 {
3046 value = *Pointer<Short4>(buffer + 8);
3047
3048 if(rgbaWriteMask != 0x0000000F)
3049 {
3050 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003051 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04003052 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04003053 current.y |= masked;
John Bauman89401822014-05-06 15:04:28 -04003054 }
3055
John Bauman19bac1e2014-05-06 15:23:49 -04003056 current.y &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ1Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04003057 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ1Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04003058 current.y |= value;
3059 *Pointer<Short4>(buffer + 8) = current.y;
John Bauman89401822014-05-06 15:04:28 -04003060 }
3061
3062 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3063
3064 {
3065 value = *Pointer<Short4>(buffer);
3066
3067 if(rgbaWriteMask != 0x0000000F)
3068 {
3069 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003070 current.z &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04003071 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04003072 current.z |= masked;
John Bauman89401822014-05-06 15:04:28 -04003073 }
3074
John Bauman19bac1e2014-05-06 15:23:49 -04003075 current.z &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ2Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04003076 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ2Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04003077 current.z |= value;
3078 *Pointer<Short4>(buffer) = current.z;
John Bauman89401822014-05-06 15:04:28 -04003079 }
3080
3081 {
3082 value = *Pointer<Short4>(buffer + 8);
3083
3084 if(rgbaWriteMask != 0x0000000F)
3085 {
3086 Short4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003087 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04003088 masked &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04003089 current.w |= masked;
John Bauman89401822014-05-06 15:04:28 -04003090 }
3091
John Bauman19bac1e2014-05-06 15:23:49 -04003092 current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskQ3Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04003093 value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskQ3Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04003094 current.w |= value;
3095 *Pointer<Short4>(buffer + 8) = current.w;
John Bauman89401822014-05-06 15:04:28 -04003096 }
3097 break;
3098 default:
3099 ASSERT(false);
3100 }
3101 }
3102
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003103 void PixelRoutine::blendFactor(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -04003104 {
3105 switch(blendFactorActive)
3106 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003107 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04003108 // Optimized
3109 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003110 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04003111 // Optimized
3112 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003113 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003114 blendFactor.x = oC.x;
3115 blendFactor.y = oC.y;
3116 blendFactor.z = oC.z;
John Bauman89401822014-05-06 15:04:28 -04003117 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003118 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003119 blendFactor.x = Float4(1.0f) - oC.x;
3120 blendFactor.y = Float4(1.0f) - oC.y;
3121 blendFactor.z = Float4(1.0f) - oC.z;
John Bauman89401822014-05-06 15:04:28 -04003122 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003123 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003124 blendFactor.x = pixel.x;
3125 blendFactor.y = pixel.y;
3126 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003127 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003128 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003129 blendFactor.x = Float4(1.0f) - pixel.x;
3130 blendFactor.y = Float4(1.0f) - pixel.y;
3131 blendFactor.z = Float4(1.0f) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003132 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003133 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003134 blendFactor.x = oC.w;
3135 blendFactor.y = oC.w;
3136 blendFactor.z = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003137 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003138 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003139 blendFactor.x = Float4(1.0f) - oC.w;
3140 blendFactor.y = Float4(1.0f) - oC.w;
3141 blendFactor.z = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04003142 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003143 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003144 blendFactor.x = pixel.w;
3145 blendFactor.y = pixel.w;
3146 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003147 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003148 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003149 blendFactor.x = Float4(1.0f) - pixel.w;
3150 blendFactor.y = Float4(1.0f) - pixel.w;
3151 blendFactor.z = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003152 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003153 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04003154 blendFactor.x = Float4(1.0f) - pixel.w;
3155 blendFactor.x = Min(blendFactor.x, oC.w);
3156 blendFactor.y = blendFactor.x;
3157 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -04003158 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003159 case BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003160 blendFactor.x = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[0]));
3161 blendFactor.y = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[1]));
3162 blendFactor.z = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04003163 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003164 case BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003165 blendFactor.x = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[0]));
3166 blendFactor.y = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[1]));
3167 blendFactor.z = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04003168 break;
3169 default:
3170 ASSERT(false);
3171 }
3172 }
3173
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003174 void PixelRoutine::blendFactorAlpha(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -04003175 {
3176 switch(blendFactorAlphaActive)
3177 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003178 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04003179 // Optimized
3180 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003181 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04003182 // Optimized
3183 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003184 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003185 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003186 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003187 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04003188 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04003189 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003190 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003191 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003192 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003193 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003194 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003195 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003196 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003197 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003198 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003199 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003200 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04003201 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003202 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003203 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003204 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003205 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04003206 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003207 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003208 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04003209 blendFactor.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003210 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003211 case BLEND_CONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003212 blendFactor.w = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.blendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04003213 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003214 case BLEND_INVCONSTANT:
John Bauman19bac1e2014-05-06 15:23:49 -04003215 blendFactor.w = *Pointer<Float4>(r.data + OFFSET(DrawData,factor.invBlendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04003216 break;
3217 default:
3218 ASSERT(false);
3219 }
3220 }
3221
John Bauman19bac1e2014-05-06 15:23:49 -04003222 void PixelRoutine::alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4f &oC, Int &x)
John Bauman89401822014-05-06 15:04:28 -04003223 {
3224 if(!state.alphaBlendActive)
3225 {
3226 return;
3227 }
3228
3229 Pointer<Byte> buffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003230 Vector4f pixel;
John Bauman89401822014-05-06 15:04:28 -04003231
Alexis Hetu96517182015-04-15 10:30:23 -04003232 Vector4s color;
John Bauman89401822014-05-06 15:04:28 -04003233 Short4 c01;
3234 Short4 c23;
3235
3236 // Read pixel
3237 switch(state.targetFormat[index])
3238 {
3239 case FORMAT_A8R8G8B8:
3240 buffer = cBuffer + 4 * x;
3241 c01 = *Pointer<Short4>(buffer);
3242 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3243 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04003244 color.z = c01;
3245 color.y = c01;
3246 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(c23));
3247 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(c23));
3248 color.x = color.z;
3249 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.y));
3250 color.x = UnpackHigh(As<Byte8>(color.x), As<Byte8>(color.y));
3251 color.y = color.z;
3252 color.w = color.x;
3253 color.x = UnpackLow(As<Byte8>(color.x), As<Byte8>(color.x));
3254 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(color.y));
3255 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.z));
3256 color.w = UnpackHigh(As<Byte8>(color.w), As<Byte8>(color.w));
John Bauman89401822014-05-06 15:04:28 -04003257
John Bauman19bac1e2014-05-06 15:23:49 -04003258 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3259 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3260 pixel.z = convertUnsigned16(As<UShort4>(color.z));
3261 pixel.w = convertUnsigned16(As<UShort4>(color.w));
John Bauman89401822014-05-06 15:04:28 -04003262 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04003263 case FORMAT_A8B8G8R8:
3264 buffer = cBuffer + 4 * x;
3265 c01 = *Pointer<Short4>(buffer);
3266 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3267 c23 = *Pointer<Short4>(buffer);
3268 color.z = c01;
3269 color.y = c01;
3270 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(c23));
3271 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(c23));
3272 color.x = color.z;
3273 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.y));
3274 color.x = UnpackHigh(As<Byte8>(color.x), As<Byte8>(color.y));
3275 color.y = color.z;
3276 color.w = color.x;
3277 color.x = UnpackLow(As<Byte8>(color.x), As<Byte8>(color.x));
3278 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(color.y));
3279 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.z));
3280 color.w = UnpackHigh(As<Byte8>(color.w), As<Byte8>(color.w));
3281
3282 pixel.x = convertUnsigned16(As<UShort4>(color.z));
3283 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3284 pixel.z = convertUnsigned16(As<UShort4>(color.x));
3285 pixel.w = convertUnsigned16(As<UShort4>(color.w));
3286 break;
John Bauman89401822014-05-06 15:04:28 -04003287 case FORMAT_X8R8G8B8:
3288 buffer = cBuffer + 4 * x;
3289 c01 = *Pointer<Short4>(buffer);
3290 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3291 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04003292 color.z = c01;
3293 color.y = c01;
3294 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(c23));
3295 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(c23));
3296 color.x = color.z;
3297 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.y));
3298 color.x = UnpackHigh(As<Byte8>(color.x), As<Byte8>(color.y));
3299 color.y = color.z;
3300 color.x = UnpackLow(As<Byte8>(color.x), As<Byte8>(color.x));
3301 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(color.y));
3302 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.z));
John Bauman89401822014-05-06 15:04:28 -04003303
John Bauman19bac1e2014-05-06 15:23:49 -04003304 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3305 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3306 pixel.z = convertUnsigned16(As<UShort4>(color.z));
3307 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003308 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04003309 case FORMAT_X8B8G8R8:
3310 buffer = cBuffer + 4 * x;
3311 c01 = *Pointer<Short4>(buffer);
3312 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3313 c23 = *Pointer<Short4>(buffer);
3314 color.z = c01;
3315 color.y = c01;
3316 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(c23));
3317 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(c23));
3318 color.x = color.z;
3319 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.y));
3320 color.x = UnpackHigh(As<Byte8>(color.x), As<Byte8>(color.y));
3321 color.y = color.z;
3322 color.x = UnpackLow(As<Byte8>(color.x), As<Byte8>(color.x));
3323 color.y = UnpackHigh(As<Byte8>(color.y), As<Byte8>(color.y));
3324 color.z = UnpackLow(As<Byte8>(color.z), As<Byte8>(color.z));
3325
3326 pixel.x = convertUnsigned16(As<UShort4>(color.z));
3327 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3328 pixel.z = convertUnsigned16(As<UShort4>(color.x));
3329 pixel.w = Float4(1.0f);
3330 break;
John Bauman66b8ab22014-05-06 15:57:45 -04003331 case FORMAT_A8:
3332 buffer = cBuffer + 1 * x;
3333 c01 = Insert(c01, *Pointer<Short>(buffer), 0);
3334 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3335 c01 = Insert(c01, *Pointer<Short>(buffer), 1);
3336 pixel.w = convertUnsigned16(As<UShort4>(UnpackLow(As<Byte8>(c01), As<Byte8>(c01))));
3337 pixel.x = Float4(0.0f);
3338 pixel.y = Float4(0.0f);
3339 pixel.z = Float4(0.0f);
3340 break;
John Bauman89401822014-05-06 15:04:28 -04003341 case FORMAT_A8G8R8B8Q:
John Bauman66b8ab22014-05-06 15:57:45 -04003342 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04003343 // UnpackLow(pixel.z, qword_ptr [cBuffer+8*x+0]);
3344 // UnpackHigh(pixel.x, qword_ptr [cBuffer+8*x+0]);
3345 // UnpackLow(pixel.y, qword_ptr [cBuffer+8*x+8]);
3346 // UnpackHigh(pixel.w, qword_ptr [cBuffer+8*x+8]);
John Bauman89401822014-05-06 15:04:28 -04003347 break;
3348 case FORMAT_X8G8R8B8Q:
John Bauman66b8ab22014-05-06 15:57:45 -04003349 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04003350 // UnpackLow(pixel.z, qword_ptr [cBuffer+8*x+0]);
3351 // UnpackHigh(pixel.x, qword_ptr [cBuffer+8*x+0]);
3352 // UnpackLow(pixel.y, qword_ptr [cBuffer+8*x+8]);
3353 // pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04003354 break;
3355 case FORMAT_A16B16G16R16:
3356 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003357 color.x = *Pointer<Short4>(buffer + 8 * x);
3358 color.y = *Pointer<Short4>(buffer + 8 * x + 8);
John Bauman89401822014-05-06 15:04:28 -04003359 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003360 color.z = *Pointer<Short4>(buffer + 8 * x);
3361 color.w = *Pointer<Short4>(buffer + 8 * x + 8);
John Bauman89401822014-05-06 15:04:28 -04003362
John Bauman19bac1e2014-05-06 15:23:49 -04003363 transpose4x4(color.x, color.y, color.z, color.w);
John Bauman89401822014-05-06 15:04:28 -04003364
John Bauman19bac1e2014-05-06 15:23:49 -04003365 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3366 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3367 pixel.z = convertUnsigned16(As<UShort4>(color.z));
3368 pixel.w = convertUnsigned16(As<UShort4>(color.w));
John Bauman89401822014-05-06 15:04:28 -04003369 break;
3370 case FORMAT_G16R16:
3371 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003372 color.x = *Pointer<Short4>(buffer + 4 * x);
John Bauman89401822014-05-06 15:04:28 -04003373 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003374 color.y = *Pointer<Short4>(buffer + 4 * x);
3375 color.z = color.x;
3376 color.x = As<Short4>(UnpackLow(color.x, color.y));
3377 color.z = As<Short4>(UnpackHigh(color.z, color.y));
3378 color.y = color.z;
3379 color.x = As<Short4>(UnpackLow(color.x, color.z));
3380 color.y = As<Short4>(UnpackHigh(color.y, color.z));
John Bauman89401822014-05-06 15:04:28 -04003381
John Bauman19bac1e2014-05-06 15:23:49 -04003382 pixel.x = convertUnsigned16(As<UShort4>(color.x));
3383 pixel.y = convertUnsigned16(As<UShort4>(color.y));
3384 pixel.z = Float4(1.0f);
3385 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003386 break;
3387 case FORMAT_R32F:
3388 buffer = cBuffer;
3389 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04003390 pixel.x.x = *Pointer<Float>(buffer + 4 * x + 0);
3391 pixel.x.y = *Pointer<Float>(buffer + 4 * x + 4);
John Bauman89401822014-05-06 15:04:28 -04003392 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3393 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04003394 pixel.x.z = *Pointer<Float>(buffer + 4 * x + 0);
3395 pixel.x.w = *Pointer<Float>(buffer + 4 * x + 4);
3396 pixel.y = Float4(1.0f);
3397 pixel.z = Float4(1.0f);
3398 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003399 break;
3400 case FORMAT_G32R32F:
3401 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003402 pixel.x = *Pointer<Float4>(buffer + 8 * x, 16);
John Bauman89401822014-05-06 15:04:28 -04003403 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003404 pixel.y = *Pointer<Float4>(buffer + 8 * x, 16);
3405 pixel.z = pixel.x;
3406 pixel.x = ShuffleLowHigh(pixel.x, pixel.y, 0x88);
3407 pixel.z = ShuffleLowHigh(pixel.z, pixel.y, 0xDD);
3408 pixel.y = pixel.z;
3409 pixel.z = Float4(1.0f);
3410 pixel.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04003411 break;
3412 case FORMAT_A32B32G32R32F:
3413 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04003414 pixel.x = *Pointer<Float4>(buffer + 16 * x, 16);
3415 pixel.y = *Pointer<Float4>(buffer + 16 * x + 16, 16);
John Bauman89401822014-05-06 15:04:28 -04003416 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04003417 pixel.z = *Pointer<Float4>(buffer + 16 * x, 16);
3418 pixel.w = *Pointer<Float4>(buffer + 16 * x + 16, 16);
3419 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04003420 break;
3421 default:
3422 ASSERT(false);
3423 }
3424
3425 if(postBlendSRGB && state.writeSRGB)
3426 {
John Bauman19bac1e2014-05-06 15:23:49 -04003427 sRGBtoLinear(pixel.x);
3428 sRGBtoLinear(pixel.y);
3429 sRGBtoLinear(pixel.z);
John Bauman89401822014-05-06 15:04:28 -04003430 }
3431
3432 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
John Bauman19bac1e2014-05-06 15:23:49 -04003433 Vector4f sourceFactor;
3434 Vector4f destFactor;
John Bauman89401822014-05-06 15:04:28 -04003435
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003436 blendFactor(r, sourceFactor, oC, pixel, state.sourceBlendFactor);
3437 blendFactor(r, destFactor, oC, pixel, state.destBlendFactor);
John Bauman89401822014-05-06 15:04:28 -04003438
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003439 if(state.sourceBlendFactor != BLEND_ONE && state.sourceBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04003440 {
John Bauman19bac1e2014-05-06 15:23:49 -04003441 oC.x *= sourceFactor.x;
3442 oC.y *= sourceFactor.y;
3443 oC.z *= sourceFactor.z;
John Bauman89401822014-05-06 15:04:28 -04003444 }
3445
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003446 if(state.destBlendFactor != BLEND_ONE && state.destBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04003447 {
John Bauman19bac1e2014-05-06 15:23:49 -04003448 pixel.x *= destFactor.x;
3449 pixel.y *= destFactor.y;
3450 pixel.z *= destFactor.z;
John Bauman89401822014-05-06 15:04:28 -04003451 }
3452
3453 switch(state.blendOperation)
3454 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003455 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04003456 oC.x += pixel.x;
3457 oC.y += pixel.y;
3458 oC.z += pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003459 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003460 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003461 oC.x -= pixel.x;
3462 oC.y -= pixel.y;
3463 oC.z -= pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003464 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003465 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003466 oC.x = pixel.x - oC.x;
3467 oC.y = pixel.y - oC.y;
3468 oC.z = pixel.z - oC.z;
John Bauman89401822014-05-06 15:04:28 -04003469 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003470 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04003471 oC.x = Min(oC.x, pixel.x);
3472 oC.y = Min(oC.y, pixel.y);
3473 oC.z = Min(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04003474 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003475 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04003476 oC.x = Max(oC.x, pixel.x);
3477 oC.y = Max(oC.y, pixel.y);
3478 oC.z = Max(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04003479 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003480 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04003481 // No operation
3482 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003483 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003484 oC.x = pixel.x;
3485 oC.y = pixel.y;
3486 oC.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04003487 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003488 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04003489 oC.x = Float4(0.0f);
3490 oC.y = Float4(0.0f);
3491 oC.z = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04003492 break;
3493 default:
3494 ASSERT(false);
3495 }
3496
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003497 blendFactorAlpha(r, sourceFactor, oC, pixel, state.sourceBlendFactorAlpha);
3498 blendFactorAlpha(r, destFactor, oC, pixel, state.destBlendFactorAlpha);
John Bauman89401822014-05-06 15:04:28 -04003499
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003500 if(state.sourceBlendFactorAlpha != BLEND_ONE && state.sourceBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04003501 {
John Bauman19bac1e2014-05-06 15:23:49 -04003502 oC.w *= sourceFactor.w;
John Bauman89401822014-05-06 15:04:28 -04003503 }
3504
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003505 if(state.destBlendFactorAlpha != BLEND_ONE && state.destBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04003506 {
John Bauman19bac1e2014-05-06 15:23:49 -04003507 pixel.w *= destFactor.w;
John Bauman89401822014-05-06 15:04:28 -04003508 }
3509
3510 switch(state.blendOperationAlpha)
3511 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003512 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04003513 oC.w += pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003514 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003515 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003516 oC.w -= pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003517 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003518 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04003519 pixel.w -= oC.w;
3520 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003521 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003522 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04003523 oC.w = Min(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04003524 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003525 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04003526 oC.w = Max(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04003527 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003528 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04003529 // No operation
3530 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003531 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04003532 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04003533 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04003534 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04003535 oC.w = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04003536 break;
3537 default:
3538 ASSERT(false);
3539 }
3540 }
3541
John Bauman19bac1e2014-05-06 15:23:49 -04003542 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 -04003543 {
3544 if(!state.colorWriteActive(index))
3545 {
3546 return;
3547 }
3548
Alexis Hetu96517182015-04-15 10:30:23 -04003549 Vector4s color;
John Bauman89401822014-05-06 15:04:28 -04003550
3551 switch(state.targetFormat[index])
3552 {
3553 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04003554 case FORMAT_X8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04003555 case FORMAT_A8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04003556 case FORMAT_A8B8G8R8:
John Bauman66b8ab22014-05-06 15:57:45 -04003557 case FORMAT_A8:
John Bauman89401822014-05-06 15:04:28 -04003558 case FORMAT_G16R16:
3559 case FORMAT_A16B16G16R16:
3560 convertFixed16(color, oC, true);
3561 writeColor(r, index, cBuffer, x, color, sMask, zMask, cMask);
3562 return;
3563 case FORMAT_R32F:
3564 break;
3565 case FORMAT_G32R32F:
John Bauman19bac1e2014-05-06 15:23:49 -04003566 oC.z = oC.x;
3567 oC.x = UnpackLow(oC.x, oC.y);
3568 oC.z = UnpackHigh(oC.z, oC.y);
3569 oC.y = oC.z;
John Bauman89401822014-05-06 15:04:28 -04003570 break;
3571 case FORMAT_A32B32G32R32F:
John Bauman19bac1e2014-05-06 15:23:49 -04003572 transpose4x4(oC.x, oC.y, oC.z, oC.w);
John Bauman89401822014-05-06 15:04:28 -04003573 break;
3574 default:
3575 ASSERT(false);
3576 }
3577
3578 int rgbaWriteMask = state.colorWriteActive(index);
3579
3580 Int xMask; // Combination of all masks
3581
3582 if(state.depthTestActive)
3583 {
3584 xMask = zMask;
3585 }
3586 else
3587 {
3588 xMask = cMask;
3589 }
3590
3591 if(state.stencilActive)
3592 {
3593 xMask &= sMask;
3594 }
3595
3596 Pointer<Byte> buffer;
3597 Float4 value;
3598
3599 switch(state.targetFormat[index])
3600 {
3601 case FORMAT_R32F:
3602 if(rgbaWriteMask & 0x00000001)
3603 {
3604 buffer = cBuffer + 4 * x;
3605
3606 // FIXME: movlps
3607 value.x = *Pointer<Float>(buffer + 0);
3608 value.y = *Pointer<Float>(buffer + 4);
3609
3610 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3611
3612 // FIXME: movhps
3613 value.z = *Pointer<Float>(buffer + 0);
3614 value.w = *Pointer<Float>(buffer + 4);
3615
John Bauman19bac1e2014-05-06 15:23:49 -04003616 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 -04003617 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003618 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
John Bauman89401822014-05-06 15:04:28 -04003619
3620 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04003621 *Pointer<Float>(buffer + 0) = oC.x.z;
3622 *Pointer<Float>(buffer + 4) = oC.x.w;
John Bauman89401822014-05-06 15:04:28 -04003623
3624 buffer -= *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3625
3626 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04003627 *Pointer<Float>(buffer + 0) = oC.x.x;
3628 *Pointer<Float>(buffer + 4) = oC.x.y;
John Bauman89401822014-05-06 15:04:28 -04003629 }
3630 break;
3631 case FORMAT_G32R32F:
3632 buffer = cBuffer + 8 * x;
3633
3634 value = *Pointer<Float4>(buffer);
3635
3636 if((rgbaWriteMask & 0x00000003) != 0x00000003)
3637 {
3638 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003639 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 -04003640 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD01X[rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003641 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003642 }
3643
John Bauman19bac1e2014-05-06 15:23:49 -04003644 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 -04003645 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskQ01X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003646 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
3647 *Pointer<Float4>(buffer) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04003648
3649 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3650
3651 value = *Pointer<Float4>(buffer);
3652
3653 if((rgbaWriteMask & 0x00000003) != 0x00000003)
3654 {
3655 Float4 masked;
3656
3657 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003658 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 -04003659 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD01X[rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003660 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003661 }
3662
John Bauman19bac1e2014-05-06 15:23:49 -04003663 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 -04003664 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskQ23X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003665 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
3666 *Pointer<Float4>(buffer) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04003667 break;
3668 case FORMAT_A32B32G32R32F:
3669 buffer = cBuffer + 16 * x;
3670
3671 {
3672 value = *Pointer<Float4>(buffer, 16);
3673
3674 if(rgbaWriteMask != 0x0000000F)
3675 {
3676 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003677 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003678 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003679 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003680 }
3681
John Bauman19bac1e2014-05-06 15:23:49 -04003682 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 -04003683 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX0X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003684 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
3685 *Pointer<Float4>(buffer, 16) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04003686 }
3687
3688 {
3689 value = *Pointer<Float4>(buffer + 16, 16);
3690
3691 if(rgbaWriteMask != 0x0000000F)
3692 {
3693 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003694 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003695 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003696 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003697 }
3698
John Bauman19bac1e2014-05-06 15:23:49 -04003699 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 -04003700 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX1X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003701 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
3702 *Pointer<Float4>(buffer + 16, 16) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04003703 }
3704
3705 buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
3706
3707 {
3708 value = *Pointer<Float4>(buffer, 16);
3709
3710 if(rgbaWriteMask != 0x0000000F)
3711 {
3712 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003713 oC.z = As<Float4>(As<Int4>(oC.z) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003714 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003715 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003716 }
3717
John Bauman19bac1e2014-05-06 15:23:49 -04003718 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 -04003719 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX2X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003720 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(value));
3721 *Pointer<Float4>(buffer, 16) = oC.z;
John Bauman89401822014-05-06 15:04:28 -04003722 }
3723
3724 {
3725 value = *Pointer<Float4>(buffer + 16, 16);
3726
3727 if(rgbaWriteMask != 0x0000000F)
3728 {
3729 Float4 masked = value;
John Bauman19bac1e2014-05-06 15:23:49 -04003730 oC.w = As<Float4>(As<Int4>(oC.w) & *Pointer<Int4>(r.constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
John Bauman89401822014-05-06 15:04:28 -04003731 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04003732 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04003733 }
3734
John Bauman19bac1e2014-05-06 15:23:49 -04003735 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 -04003736 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(r.constants + OFFSET(Constants,invMaskX3X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04003737 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(value));
3738 *Pointer<Float4>(buffer + 16, 16) = oC.w;
John Bauman89401822014-05-06 15:04:28 -04003739 }
3740 break;
3741 default:
3742 ASSERT(false);
3743 }
3744 }
3745
3746 void PixelRoutine::ps_1_x(Registers &r, Int cMask[4])
3747 {
3748 int pad = 0; // Count number of texm3x3pad instructions
Alexis Hetu96517182015-04-15 10:30:23 -04003749 Vector4s dPairing; // Destination for first pairing instruction
John Bauman89401822014-05-06 15:04:28 -04003750
Alexis Hetu903e0252014-11-25 14:25:32 -05003751 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -04003752 {
John Bauman19bac1e2014-05-06 15:23:49 -04003753 const Shader::Instruction *instruction = shader->getInstruction(i);
3754 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -04003755
3756 // #ifndef NDEBUG // FIXME: Centralize debug output control
John Bauman19bac1e2014-05-06 15:23:49 -04003757 // shader->printInstruction(i, "debug.txt");
John Bauman89401822014-05-06 15:04:28 -04003758 // #endif
3759
John Bauman19bac1e2014-05-06 15:23:49 -04003760 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -04003761 {
3762 continue;
3763 }
3764
John Bauman19bac1e2014-05-06 15:23:49 -04003765 const Dst &dst = instruction->dst;
3766 const Src &src0 = instruction->src[0];
3767 const Src &src1 = instruction->src[1];
3768 const Src &src2 = instruction->src[2];
John Bauman89401822014-05-06 15:04:28 -04003769
John Bauman19bac1e2014-05-06 15:23:49 -04003770 unsigned short version = shader->getVersion();
3771 bool pairing = i + 1 < shader->getLength() && shader->getInstruction(i + 1)->coissue; // First instruction of pair
3772 bool coissue = instruction->coissue; // Second instruction of pair
John Bauman89401822014-05-06 15:04:28 -04003773
Alexis Hetu96517182015-04-15 10:30:23 -04003774 Vector4s d;
3775 Vector4s s0;
3776 Vector4s s1;
3777 Vector4s s2;
John Bauman89401822014-05-06 15:04:28 -04003778
Alexis Hetu96517182015-04-15 10:30:23 -04003779 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegisterS(r, src0);
3780 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegisterS(r, src1);
3781 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegisterS(r, src2);
John Bauman19bac1e2014-05-06 15:23:49 -04003782
3783 Float4 u = version < 0x0104 ? r.vf[2 + dst.index].x : r.vf[2 + src0.index].x;
3784 Float4 v = version < 0x0104 ? r.vf[2 + dst.index].y : r.vf[2 + src0.index].y;
3785 Float4 s = version < 0x0104 ? r.vf[2 + dst.index].z : r.vf[2 + src0.index].z;
3786 Float4 t = version < 0x0104 ? r.vf[2 + dst.index].w : r.vf[2 + src0.index].w;
John Bauman89401822014-05-06 15:04:28 -04003787
3788 switch(opcode)
3789 {
John Bauman19bac1e2014-05-06 15:23:49 -04003790 case Shader::OPCODE_PS_1_0: break;
3791 case Shader::OPCODE_PS_1_1: break;
3792 case Shader::OPCODE_PS_1_2: break;
3793 case Shader::OPCODE_PS_1_3: break;
3794 case Shader::OPCODE_PS_1_4: break;
John Bauman89401822014-05-06 15:04:28 -04003795
John Bauman19bac1e2014-05-06 15:23:49 -04003796 case Shader::OPCODE_DEF: break;
John Bauman89401822014-05-06 15:04:28 -04003797
John Bauman19bac1e2014-05-06 15:23:49 -04003798 case Shader::OPCODE_NOP: break;
3799 case Shader::OPCODE_MOV: MOV(d, s0); break;
3800 case Shader::OPCODE_ADD: ADD(d, s0, s1); break;
3801 case Shader::OPCODE_SUB: SUB(d, s0, s1); break;
3802 case Shader::OPCODE_MAD: MAD(d, s0, s1, s2); break;
3803 case Shader::OPCODE_MUL: MUL(d, s0, s1); break;
3804 case Shader::OPCODE_DP3: DP3(d, s0, s1); break;
3805 case Shader::OPCODE_DP4: DP4(d, s0, s1); break;
3806 case Shader::OPCODE_LRP: LRP(d, s0, s1, s2); break;
3807 case Shader::OPCODE_TEXCOORD:
3808 if(version < 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003809 {
John Bauman19bac1e2014-05-06 15:23:49 -04003810 TEXCOORD(d, u, v, s, dst.index);
John Bauman89401822014-05-06 15:04:28 -04003811 }
3812 else
3813 {
3814 if((src0.swizzle & 0x30) == 0x20) // .xyz
3815 {
John Bauman19bac1e2014-05-06 15:23:49 -04003816 TEXCRD(d, u, v, s, src0.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003817 }
3818 else // .xyw
3819 {
John Bauman19bac1e2014-05-06 15:23:49 -04003820 TEXCRD(d, u, v, t, src0.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003821 }
3822 }
3823 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003824 case Shader::OPCODE_TEXKILL:
3825 if(version < 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003826 {
John Bauman19bac1e2014-05-06 15:23:49 -04003827 TEXKILL(cMask, u, v, s);
John Bauman89401822014-05-06 15:04:28 -04003828 }
John Bauman19bac1e2014-05-06 15:23:49 -04003829 else if(version == 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003830 {
John Bauman19bac1e2014-05-06 15:23:49 -04003831 if(dst.type == Shader::PARAMETER_TEXTURE)
John Bauman89401822014-05-06 15:04:28 -04003832 {
John Bauman19bac1e2014-05-06 15:23:49 -04003833 TEXKILL(cMask, u, v, s);
John Bauman89401822014-05-06 15:04:28 -04003834 }
3835 else
3836 {
Alexis Hetu96517182015-04-15 10:30:23 -04003837 TEXKILL(cMask, r.rs[dst.index]);
John Bauman89401822014-05-06 15:04:28 -04003838 }
3839 }
3840 else ASSERT(false);
3841 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003842 case Shader::OPCODE_TEX:
3843 if(version < 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003844 {
John Bauman19bac1e2014-05-06 15:23:49 -04003845 TEX(r, d, u, v, s, dst.index, false);
John Bauman89401822014-05-06 15:04:28 -04003846 }
John Bauman19bac1e2014-05-06 15:23:49 -04003847 else if(version == 0x0104)
John Bauman89401822014-05-06 15:04:28 -04003848 {
John Bauman19bac1e2014-05-06 15:23:49 -04003849 if(src0.type == Shader::PARAMETER_TEXTURE)
John Bauman89401822014-05-06 15:04:28 -04003850 {
3851 if((src0.swizzle & 0x30) == 0x20) // .xyz
3852 {
John Bauman19bac1e2014-05-06 15:23:49 -04003853 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 -04003854 }
3855 else // .xyw
3856 {
John Bauman19bac1e2014-05-06 15:23:49 -04003857 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 -04003858 }
3859 }
3860 else
3861 {
John Bauman19bac1e2014-05-06 15:23:49 -04003862 TEXLD(r, d, s0, dst.index, src0.modifier == Shader::MODIFIER_DZ || src0.modifier == Shader::MODIFIER_DW);
John Bauman89401822014-05-06 15:04:28 -04003863 }
3864 }
3865 else ASSERT(false);
3866 break;
John Bauman19bac1e2014-05-06 15:23:49 -04003867 case Shader::OPCODE_TEXBEM: TEXBEM(r, d, s0, u, v, s, dst.index); break;
3868 case Shader::OPCODE_TEXBEML: TEXBEML(r, d, s0, u, v, s, dst.index); break;
3869 case Shader::OPCODE_TEXREG2AR: TEXREG2AR(r, d, s0, dst.index); break;
3870 case Shader::OPCODE_TEXREG2GB: TEXREG2GB(r, d, s0, dst.index); break;
3871 case Shader::OPCODE_TEXM3X2PAD: TEXM3X2PAD(r, u, v, s, s0, 0, src0.modifier == Shader::MODIFIER_SIGN); break;
3872 case Shader::OPCODE_TEXM3X2TEX: TEXM3X2TEX(r, d, u, v, s, dst.index, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3873 case Shader::OPCODE_TEXM3X3PAD: TEXM3X3PAD(r, u, v, s, s0, pad++ % 2, src0.modifier == Shader::MODIFIER_SIGN); break;
3874 case Shader::OPCODE_TEXM3X3TEX: TEXM3X3TEX(r, d, u, v, s, dst.index, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3875 case Shader::OPCODE_TEXM3X3SPEC: TEXM3X3SPEC(r, d, u, v, s, dst.index, s0, s1); break;
3876 case Shader::OPCODE_TEXM3X3VSPEC: TEXM3X3VSPEC(r, d, u, v, s, dst.index, s0); break;
3877 case Shader::OPCODE_CND: CND(d, s0, s1, s2); break;
3878 case Shader::OPCODE_TEXREG2RGB: TEXREG2RGB(r, d, s0, dst.index); break;
3879 case Shader::OPCODE_TEXDP3TEX: TEXDP3TEX(r, d, u, v, s, dst.index, s0); break;
3880 case Shader::OPCODE_TEXM3X2DEPTH: TEXM3X2DEPTH(r, d, u, v, s, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3881 case Shader::OPCODE_TEXDP3: TEXDP3(r, d, u, v, s, s0); break;
3882 case Shader::OPCODE_TEXM3X3: TEXM3X3(r, d, u, v, s, s0, src0.modifier == Shader::MODIFIER_SIGN); break;
3883 case Shader::OPCODE_TEXDEPTH: TEXDEPTH(r); break;
3884 case Shader::OPCODE_CMP0: CMP(d, s0, s1, s2); break;
3885 case Shader::OPCODE_BEM: BEM(r, d, s0, s1, dst.index); break;
3886 case Shader::OPCODE_PHASE: break;
3887 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -04003888 default:
3889 ASSERT(false);
3890 }
3891
John Bauman19bac1e2014-05-06 15:23:49 -04003892 if(dst.type != Shader::PARAMETER_VOID && opcode != Shader::OPCODE_TEXKILL)
John Bauman89401822014-05-06 15:04:28 -04003893 {
3894 if(dst.shift > 0)
3895 {
John Bauman19bac1e2014-05-06 15:23:49 -04003896 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);}
3897 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);}
3898 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);}
3899 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 -04003900 }
3901 else if(dst.shift < 0)
3902 {
John Bauman19bac1e2014-05-06 15:23:49 -04003903 if(dst.mask & 0x1) d.x = d.x >> -dst.shift;
3904 if(dst.mask & 0x2) d.y = d.y >> -dst.shift;
3905 if(dst.mask & 0x4) d.z = d.z >> -dst.shift;
3906 if(dst.mask & 0x8) d.w = d.w >> -dst.shift;
John Bauman89401822014-05-06 15:04:28 -04003907 }
3908
3909 if(dst.saturate)
3910 {
John Bauman19bac1e2014-05-06 15:23:49 -04003911 if(dst.mask & 0x1) {d.x = Min(d.x, Short4(0x1000)); d.x = Max(d.x, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
3912 if(dst.mask & 0x2) {d.y = Min(d.y, Short4(0x1000)); d.y = Max(d.y, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
3913 if(dst.mask & 0x4) {d.z = Min(d.z, Short4(0x1000)); d.z = Max(d.z, Short4(0x0000, 0x0000, 0x0000, 0x0000));}
3914 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 -04003915 }
3916
3917 if(pairing)
3918 {
John Bauman19bac1e2014-05-06 15:23:49 -04003919 if(dst.mask & 0x1) dPairing.x = d.x;
3920 if(dst.mask & 0x2) dPairing.y = d.y;
3921 if(dst.mask & 0x4) dPairing.z = d.z;
3922 if(dst.mask & 0x8) dPairing.w = d.w;
John Bauman89401822014-05-06 15:04:28 -04003923 }
3924
3925 if(coissue)
3926 {
John Bauman19bac1e2014-05-06 15:23:49 -04003927 const Dst &dst = shader->getInstruction(i - 1)->dst;
John Bauman89401822014-05-06 15:04:28 -04003928
3929 writeDestination(r, dPairing, dst);
3930 }
3931
3932 if(!pairing)
3933 {
3934 writeDestination(r, d, dst);
3935 }
3936 }
3937 }
3938 }
3939
3940 void PixelRoutine::ps_2_x(Registers &r, Int cMask[4])
3941 {
3942 r.enableIndex = 0;
3943 r.stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -04003944
Nicolas Capens4677a5f2014-05-06 16:42:26 -04003945 if(shader->containsLeaveInstruction())
3946 {
3947 r.enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3948 }
3949
John Bauman19bac1e2014-05-06 15:23:49 -04003950 bool out[4][4] = {false};
3951
3952 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -05003953 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -04003954 {
John Bauman19bac1e2014-05-06 15:23:49 -04003955 const Shader::Instruction *instruction = shader->getInstruction(i);
3956 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -04003957
John Bauman19bac1e2014-05-06 15:23:49 -04003958 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
3959 {
3960 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -04003961
John Bauman19bac1e2014-05-06 15:23:49 -04003962 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
3963 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
3964 }
3965 }
3966
Alexis Hetu903e0252014-11-25 14:25:32 -05003967 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -04003968 {
3969 const Shader::Instruction *instruction = shader->getInstruction(i);
3970 Shader::Opcode opcode = instruction->opcode;
3971
3972 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -04003973 {
3974 continue;
3975 }
3976
John Bauman19bac1e2014-05-06 15:23:49 -04003977 const Dst &dst = instruction->dst;
3978 const Src &src0 = instruction->src[0];
3979 const Src &src1 = instruction->src[1];
3980 const Src &src2 = instruction->src[2];
3981 const Src &src3 = instruction->src[3];
John Bauman89401822014-05-06 15:04:28 -04003982
John Bauman19bac1e2014-05-06 15:23:49 -04003983 bool predicate = instruction->predicate;
3984 Control control = instruction->control;
John Bauman89401822014-05-06 15:04:28 -04003985 bool pp = dst.partialPrecision;
John Bauman19bac1e2014-05-06 15:23:49 -04003986 bool project = instruction->project;
3987 bool bias = instruction->bias;
John Bauman89401822014-05-06 15:04:28 -04003988
John Bauman19bac1e2014-05-06 15:23:49 -04003989 Vector4f d;
3990 Vector4f s0;
3991 Vector4f s1;
3992 Vector4f s2;
3993 Vector4f s3;
John Bauman89401822014-05-06 15:04:28 -04003994
John Bauman19bac1e2014-05-06 15:23:49 -04003995 if(opcode == Shader::OPCODE_TEXKILL) // Takes destination as input
John Bauman89401822014-05-06 15:04:28 -04003996 {
John Bauman19bac1e2014-05-06 15:23:49 -04003997 if(dst.type == Shader::PARAMETER_TEXTURE)
John Bauman89401822014-05-06 15:04:28 -04003998 {
John Bauman19bac1e2014-05-06 15:23:49 -04003999 d.x = r.vf[2 + dst.index].x;
4000 d.y = r.vf[2 + dst.index].y;
4001 d.z = r.vf[2 + dst.index].z;
4002 d.w = r.vf[2 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -04004003 }
4004 else
4005 {
4006 d = r.rf[dst.index];
4007 }
4008 }
4009
Alexis Hetu96517182015-04-15 10:30:23 -04004010 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegisterF(r, src0);
4011 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegisterF(r, src1);
4012 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegisterF(r, src2);
4013 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegisterF(r, src3);
John Bauman89401822014-05-06 15:04:28 -04004014
4015 switch(opcode)
4016 {
John Bauman19bac1e2014-05-06 15:23:49 -04004017 case Shader::OPCODE_PS_2_0: break;
4018 case Shader::OPCODE_PS_2_x: break;
4019 case Shader::OPCODE_PS_3_0: break;
4020 case Shader::OPCODE_DEF: break;
4021 case Shader::OPCODE_DCL: break;
4022 case Shader::OPCODE_NOP: break;
4023 case Shader::OPCODE_MOV: mov(d, s0); break;
4024 case Shader::OPCODE_F2B: f2b(d, s0); break;
4025 case Shader::OPCODE_B2F: b2f(d, s0); break;
4026 case Shader::OPCODE_ADD: add(d, s0, s1); break;
4027 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
4028 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
4029 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
4030 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
4031 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
4032 case Shader::OPCODE_DP2ADD: dp2add(d, s0, s1, s2); break;
4033 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
4034 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
4035 case Shader::OPCODE_CMP0: cmp0(d, s0, s1, s2); break;
4036 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
4037 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
4038 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
4039 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
4040 case Shader::OPCODE_FRC: frc(d, s0); break;
4041 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
4042 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -04004043 case Shader::OPCODE_ROUND: round(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -04004044 case Shader::OPCODE_CEIL: ceil(d, s0); break;
4045 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
4046 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
4047 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
4048 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
4049 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
4050 case Shader::OPCODE_LOG: log(d, s0, pp); break;
4051 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
4052 case Shader::OPCODE_DIV: div(d, s0, s1); break;
4053 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
4054 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
4055 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
4056 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
4057 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
4058 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
4059 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
4060 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
4061 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
4062 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
4063 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
4064 case Shader::OPCODE_MIN: min(d, s0, s1); break;
4065 case Shader::OPCODE_MAX: max(d, s0, s1); break;
4066 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
4067 case Shader::OPCODE_STEP: step(d, s0, s1); break;
4068 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
4069 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
4070 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
4071 case Shader::OPCODE_SGN: sgn(d, s0); break;
4072 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
4073 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
4074 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
4075 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
4076 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
4077 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
4078 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
4079 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
4080 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
4081 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
4082 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
4083 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
4084 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
4085 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
4086 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
4087 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
4088 case Shader::OPCODE_ABS: abs(d, s0); break;
4089 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
4090 case Shader::OPCODE_COS: cos(d, s0, pp); break;
4091 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
4092 case Shader::OPCODE_TAN: tan(d, s0, pp); break;
4093 case Shader::OPCODE_ACOS: acos(d, s0, pp); break;
4094 case Shader::OPCODE_ASIN: asin(d, s0, pp); break;
4095 case Shader::OPCODE_ATAN: atan(d, s0, pp); break;
4096 case Shader::OPCODE_ATAN2: atan2(d, s0, s1, pp); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -04004097 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
4098 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
4099 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
4100 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
4101 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
4102 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
John Bauman19bac1e2014-05-06 15:23:49 -04004103 case Shader::OPCODE_M4X4: M4X4(r, d, s0, src1); break;
4104 case Shader::OPCODE_M4X3: M4X3(r, d, s0, src1); break;
4105 case Shader::OPCODE_M3X4: M3X4(r, d, s0, src1); break;
4106 case Shader::OPCODE_M3X3: M3X3(r, d, s0, src1); break;
4107 case Shader::OPCODE_M3X2: M3X2(r, d, s0, src1); break;
4108 case Shader::OPCODE_TEX: TEXLD(r, d, s0, src1, project, bias); break;
4109 case Shader::OPCODE_TEXLDD: TEXLDD(r, d, s0, src1, s2, s3, project, bias); break;
4110 case Shader::OPCODE_TEXLDL: TEXLDL(r, d, s0, src1, project, bias); break;
4111 case Shader::OPCODE_TEXKILL: TEXKILL(cMask, d, dst.mask); break;
4112 case Shader::OPCODE_DISCARD: DISCARD(r, cMask, instruction); break;
4113 case Shader::OPCODE_DFDX: DFDX(d, s0); break;
4114 case Shader::OPCODE_DFDY: DFDY(d, s0); break;
4115 case Shader::OPCODE_FWIDTH: FWIDTH(d, s0); break;
4116 case Shader::OPCODE_BREAK: BREAK(r); break;
4117 case Shader::OPCODE_BREAKC: BREAKC(r, s0, s1, control); break;
4118 case Shader::OPCODE_BREAKP: BREAKP(r, src0); break;
4119 case Shader::OPCODE_CONTINUE: CONTINUE(r); break;
4120 case Shader::OPCODE_TEST: TEST(); break;
4121 case Shader::OPCODE_CALL: CALL(r, dst.label, dst.callSite); break;
4122 case Shader::OPCODE_CALLNZ: CALLNZ(r, dst.label, dst.callSite, src0); break;
4123 case Shader::OPCODE_ELSE: ELSE(r); break;
4124 case Shader::OPCODE_ENDIF: ENDIF(r); break;
4125 case Shader::OPCODE_ENDLOOP: ENDLOOP(r); break;
4126 case Shader::OPCODE_ENDREP: ENDREP(r); break;
4127 case Shader::OPCODE_ENDWHILE: ENDWHILE(r); break;
4128 case Shader::OPCODE_IF: IF(r, src0); break;
4129 case Shader::OPCODE_IFC: IFC(r, s0, s1, control); break;
4130 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
4131 case Shader::OPCODE_LOOP: LOOP(r, src1); break;
4132 case Shader::OPCODE_REP: REP(r, src0); break;
4133 case Shader::OPCODE_WHILE: WHILE(r, src0); break;
4134 case Shader::OPCODE_RET: RET(r); break;
4135 case Shader::OPCODE_LEAVE: LEAVE(r); break;
4136 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
4137 case Shader::OPCODE_ALL: all(d.x, s0); break;
4138 case Shader::OPCODE_ANY: any(d.x, s0); break;
4139 case Shader::OPCODE_NOT: not(d, s0); break;
4140 case Shader::OPCODE_OR: or(d.x, s0.x, s1.x); break;
4141 case Shader::OPCODE_XOR: xor(d.x, s0.x, s1.x); break;
4142 case Shader::OPCODE_AND: and(d.x, s0.x, s1.x); break;
4143 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -04004144 default:
4145 ASSERT(false);
4146 }
4147
John Bauman19bac1e2014-05-06 15:23:49 -04004148 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 -04004149 {
John Bauman19bac1e2014-05-06 15:23:49 -04004150 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -04004151 {
John Bauman19bac1e2014-05-06 15:23:49 -04004152 switch(opcode)
4153 {
4154 case Shader::OPCODE_DIV:
4155 if(dst.x) d.x = Trunc(d.x);
4156 if(dst.y) d.y = Trunc(d.y);
4157 if(dst.z) d.z = Trunc(d.z);
4158 if(dst.w) d.w = Trunc(d.w);
4159 break;
4160 default:
4161 break; // No truncation to integer required when arguments are integer
4162 }
John Bauman89401822014-05-06 15:04:28 -04004163 }
4164
John Bauman19bac1e2014-05-06 15:23:49 -04004165 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -04004166 {
John Bauman19bac1e2014-05-06 15:23:49 -04004167 if(dst.x) d.x = Max(d.x, Float4(0.0f));
4168 if(dst.y) d.y = Max(d.y, Float4(0.0f));
4169 if(dst.z) d.z = Max(d.z, Float4(0.0f));
4170 if(dst.w) d.w = Max(d.w, Float4(0.0f));
4171
4172 if(dst.x) d.x = Min(d.x, Float4(1.0f));
4173 if(dst.y) d.y = Min(d.y, Float4(1.0f));
4174 if(dst.z) d.z = Min(d.z, Float4(1.0f));
4175 if(dst.w) d.w = Min(d.w, Float4(1.0f));
4176 }
4177
Nicolas Capensc6e8ab12014-05-06 23:31:07 -04004178 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -04004179 {
4180 Vector4f pDst; // FIXME: Rename
John Bauman89401822014-05-06 15:04:28 -04004181
4182 switch(dst.type)
4183 {
John Bauman19bac1e2014-05-06 15:23:49 -04004184 case Shader::PARAMETER_TEMP:
4185 if(dst.rel.type == Shader::PARAMETER_VOID)
4186 {
4187 if(dst.x) pDst.x = r.rf[dst.index].x;
4188 if(dst.y) pDst.y = r.rf[dst.index].y;
4189 if(dst.z) pDst.z = r.rf[dst.index].z;
4190 if(dst.w) pDst.w = r.rf[dst.index].w;
4191 }
4192 else
4193 {
4194 Int a = relativeAddress(r, dst);
4195
4196 if(dst.x) pDst.x = r.rf[dst.index + a].x;
4197 if(dst.y) pDst.y = r.rf[dst.index + a].y;
4198 if(dst.z) pDst.z = r.rf[dst.index + a].z;
4199 if(dst.w) pDst.w = r.rf[dst.index + a].w;
4200 }
John Bauman89401822014-05-06 15:04:28 -04004201 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004202 case Shader::PARAMETER_COLOROUT:
4203 ASSERT(dst.rel.type == Shader::PARAMETER_VOID);
John Bauman89401822014-05-06 15:04:28 -04004204 if(dst.x) pDst.x = r.oC[dst.index].x;
4205 if(dst.y) pDst.y = r.oC[dst.index].y;
4206 if(dst.z) pDst.z = r.oC[dst.index].z;
4207 if(dst.w) pDst.w = r.oC[dst.index].w;
4208 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004209 case Shader::PARAMETER_PREDICATE:
John Bauman89401822014-05-06 15:04:28 -04004210 if(dst.x) pDst.x = r.p0.x;
4211 if(dst.y) pDst.y = r.p0.y;
4212 if(dst.z) pDst.z = r.p0.z;
4213 if(dst.w) pDst.w = r.p0.w;
4214 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004215 case Shader::PARAMETER_DEPTHOUT:
John Bauman89401822014-05-06 15:04:28 -04004216 pDst.x = r.oDepth;
4217 break;
4218 default:
4219 ASSERT(false);
4220 }
4221
John Bauman19bac1e2014-05-06 15:23:49 -04004222 Int4 enable = enableMask(r, instruction);
John Bauman89401822014-05-06 15:04:28 -04004223
4224 Int4 xEnable = enable;
4225 Int4 yEnable = enable;
4226 Int4 zEnable = enable;
4227 Int4 wEnable = enable;
4228
4229 if(predicate)
4230 {
John Bauman19bac1e2014-05-06 15:23:49 -04004231 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -04004232
4233 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
4234 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
4235 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
4236 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
4237
John Bauman19bac1e2014-05-06 15:23:49 -04004238 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -04004239 {
4240 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
4241 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
4242 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
4243 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
4244 }
4245 else
4246 {
4247 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
4248 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
4249 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
4250 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
4251 }
4252 }
4253
4254 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
4255 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
4256 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
4257 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
4258
4259 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
4260 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
4261 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
4262 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
4263 }
4264
4265 switch(dst.type)
4266 {
John Bauman19bac1e2014-05-06 15:23:49 -04004267 case Shader::PARAMETER_TEMP:
4268 if(dst.rel.type == Shader::PARAMETER_VOID)
4269 {
4270 if(dst.x) r.rf[dst.index].x = d.x;
4271 if(dst.y) r.rf[dst.index].y = d.y;
4272 if(dst.z) r.rf[dst.index].z = d.z;
4273 if(dst.w) r.rf[dst.index].w = d.w;
4274 }
4275 else
4276 {
4277 Int a = relativeAddress(r, dst);
4278
4279 if(dst.x) r.rf[dst.index + a].x = d.x;
4280 if(dst.y) r.rf[dst.index + a].y = d.y;
4281 if(dst.z) r.rf[dst.index + a].z = d.z;
4282 if(dst.w) r.rf[dst.index + a].w = d.w;
4283 }
John Bauman89401822014-05-06 15:04:28 -04004284 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004285 case Shader::PARAMETER_COLOROUT:
4286 ASSERT(dst.rel.type == Shader::PARAMETER_VOID);
4287 if(dst.x) {r.oC[dst.index].x = d.x; out[dst.index][0] = true;}
4288 if(dst.y) {r.oC[dst.index].y = d.y; out[dst.index][1] = true;}
4289 if(dst.z) {r.oC[dst.index].z = d.z; out[dst.index][2] = true;}
4290 if(dst.w) {r.oC[dst.index].w = d.w; out[dst.index][3] = true;}
John Bauman89401822014-05-06 15:04:28 -04004291 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004292 case Shader::PARAMETER_PREDICATE:
John Bauman89401822014-05-06 15:04:28 -04004293 if(dst.x) r.p0.x = d.x;
4294 if(dst.y) r.p0.y = d.y;
4295 if(dst.z) r.p0.z = d.z;
4296 if(dst.w) r.p0.w = d.w;
4297 break;
John Bauman19bac1e2014-05-06 15:23:49 -04004298 case Shader::PARAMETER_DEPTHOUT:
John Bauman89401822014-05-06 15:04:28 -04004299 r.oDepth = d.x;
4300 break;
4301 default:
4302 ASSERT(false);
4303 }
4304 }
4305 }
4306
John Bauman19bac1e2014-05-06 15:23:49 -04004307 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -04004308 {
4309 Nucleus::setInsertBlock(returnBlock);
4310 }
John Bauman19bac1e2014-05-06 15:23:49 -04004311
4312 for(int i = 0; i < 4; i++)
4313 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04004314 if(state.targetFormat[i] != FORMAT_NULL)
John Bauman19bac1e2014-05-06 15:23:49 -04004315 {
4316 if(!out[i][0]) r.oC[i].x = Float4(0.0f);
4317 if(!out[i][1]) r.oC[i].y = Float4(0.0f);
4318 if(!out[i][2]) r.oC[i].z = Float4(0.0f);
4319 if(!out[i][3]) r.oC[i].w = Float4(0.0f);
4320 }
4321 }
John Bauman89401822014-05-06 15:04:28 -04004322 }
4323
John Bauman19bac1e2014-05-06 15:23:49 -04004324 Short4 PixelRoutine::convertFixed12(RValue<Float4> cf)
John Bauman89401822014-05-06 15:04:28 -04004325 {
John Bauman19bac1e2014-05-06 15:23:49 -04004326 return RoundShort4(cf * Float4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04004327 }
4328
Alexis Hetu96517182015-04-15 10:30:23 -04004329 void PixelRoutine::convertFixed12(Vector4s &cs, Vector4f &cf)
John Bauman89401822014-05-06 15:04:28 -04004330 {
Alexis Hetu96517182015-04-15 10:30:23 -04004331 cs.x = convertFixed12(cf.x);
4332 cs.y = convertFixed12(cf.y);
4333 cs.z = convertFixed12(cf.z);
4334 cs.w = convertFixed12(cf.w);
John Bauman89401822014-05-06 15:04:28 -04004335 }
4336
4337 UShort4 PixelRoutine::convertFixed16(Float4 &cf, bool saturate)
4338 {
John Bauman19bac1e2014-05-06 15:23:49 -04004339 return UShort4(cf * Float4(0xFFFF), saturate);
John Bauman89401822014-05-06 15:04:28 -04004340 }
4341
Alexis Hetu96517182015-04-15 10:30:23 -04004342 void PixelRoutine::convertFixed16(Vector4s &cs, Vector4f &cf, bool saturate)
John Bauman89401822014-05-06 15:04:28 -04004343 {
Alexis Hetu96517182015-04-15 10:30:23 -04004344 cs.x = convertFixed16(cf.x, saturate);
4345 cs.y = convertFixed16(cf.y, saturate);
4346 cs.z = convertFixed16(cf.z, saturate);
4347 cs.w = convertFixed16(cf.w, saturate);
John Bauman89401822014-05-06 15:04:28 -04004348 }
4349
Alexis Hetu96517182015-04-15 10:30:23 -04004350 Float4 PixelRoutine::convertSigned12(Short4 &cs)
John Bauman89401822014-05-06 15:04:28 -04004351 {
Alexis Hetu96517182015-04-15 10:30:23 -04004352 return Float4(cs) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004353 }
4354
Alexis Hetu96517182015-04-15 10:30:23 -04004355 void PixelRoutine::convertSigned12(Vector4f &cf, Vector4s &cs)
John Bauman89401822014-05-06 15:04:28 -04004356 {
Alexis Hetu96517182015-04-15 10:30:23 -04004357 cf.x = convertSigned12(cs.x);
4358 cf.y = convertSigned12(cs.y);
4359 cf.z = convertSigned12(cs.z);
4360 cf.w = convertSigned12(cs.w);
John Bauman89401822014-05-06 15:04:28 -04004361 }
4362
Alexis Hetu96517182015-04-15 10:30:23 -04004363 Float4 PixelRoutine::convertUnsigned16(UShort4 cs)
John Bauman89401822014-05-06 15:04:28 -04004364 {
Alexis Hetu96517182015-04-15 10:30:23 -04004365 return Float4(cs) * Float4(1.0f / 0xFFFF);
John Bauman89401822014-05-06 15:04:28 -04004366 }
4367
Alexis Hetu96517182015-04-15 10:30:23 -04004368 void PixelRoutine::sRGBtoLinear16_16(Registers &r, Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04004369 {
John Bauman19bac1e2014-05-06 15:23:49 -04004370 c.x = As<UShort4>(c.x) >> 4;
4371 c.y = As<UShort4>(c.y) >> 4;
4372 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04004373
4374 sRGBtoLinear12_16(r, c);
4375 }
4376
Alexis Hetu96517182015-04-15 10:30:23 -04004377 void PixelRoutine::sRGBtoLinear12_16(Registers &r, Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04004378 {
4379 Pointer<Byte> LUT = r.constants + OFFSET(Constants,sRGBtoLin12_16);
4380
John Bauman19bac1e2014-05-06 15:23:49 -04004381 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
4382 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
4383 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
4384 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004385
John Bauman19bac1e2014-05-06 15:23:49 -04004386 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
4387 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
4388 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
4389 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004390
John Bauman19bac1e2014-05-06 15:23:49 -04004391 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
4392 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
4393 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
4394 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004395 }
4396
Alexis Hetu96517182015-04-15 10:30:23 -04004397 void PixelRoutine::linearToSRGB16_16(Registers &r, Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04004398 {
John Bauman19bac1e2014-05-06 15:23:49 -04004399 c.x = As<UShort4>(c.x) >> 4;
4400 c.y = As<UShort4>(c.y) >> 4;
4401 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04004402
4403 linearToSRGB12_16(r, c);
4404 }
4405
Alexis Hetu96517182015-04-15 10:30:23 -04004406 void PixelRoutine::linearToSRGB12_16(Registers &r, Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04004407 {
4408 Pointer<Byte> LUT = r.constants + OFFSET(Constants,linToSRGB12_16);
4409
John Bauman19bac1e2014-05-06 15:23:49 -04004410 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
4411 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
4412 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
4413 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004414
John Bauman19bac1e2014-05-06 15:23:49 -04004415 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
4416 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
4417 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
4418 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004419
John Bauman19bac1e2014-05-06 15:23:49 -04004420 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
4421 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
4422 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
4423 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04004424 }
4425
4426 Float4 PixelRoutine::linearToSRGB(const Float4 &x) // Approximates x^(1.0/2.2)
4427 {
4428 Float4 sqrtx = Rcp_pp(RcpSqrt_pp(x));
4429 Float4 sRGB = sqrtx * Float4(1.14f) - x * Float4(0.14f);
4430
4431 return Min(Max(sRGB, Float4(0.0f)), Float4(1.0f));
4432 }
4433
4434 Float4 PixelRoutine::sRGBtoLinear(const Float4 &x) // Approximates x^2.2
4435 {
4436 Float4 linear = x * x;
4437 linear = linear * Float4(0.73f) + linear * x * Float4(0.27f);
4438
4439 return Min(Max(linear, Float4(0.0f)), Float4(1.0f));
4440 }
4441
Alexis Hetu96517182015-04-15 10:30:23 -04004442 void PixelRoutine::MOV(Vector4s &dst, Vector4s &src0)
John Bauman89401822014-05-06 15:04:28 -04004443 {
John Bauman19bac1e2014-05-06 15:23:49 -04004444 dst.x = src0.x;
4445 dst.y = src0.y;
4446 dst.z = src0.z;
4447 dst.w = src0.w;
John Bauman89401822014-05-06 15:04:28 -04004448 }
4449
Alexis Hetu96517182015-04-15 10:30:23 -04004450 void PixelRoutine::ADD(Vector4s &dst, Vector4s &src0, Vector4s &src1)
John Bauman89401822014-05-06 15:04:28 -04004451 {
John Bauman19bac1e2014-05-06 15:23:49 -04004452 dst.x = AddSat(src0.x, src1.x);
4453 dst.y = AddSat(src0.y, src1.y);
4454 dst.z = AddSat(src0.z, src1.z);
4455 dst.w = AddSat(src0.w, src1.w);
John Bauman89401822014-05-06 15:04:28 -04004456 }
4457
Alexis Hetu96517182015-04-15 10:30:23 -04004458 void PixelRoutine::SUB(Vector4s &dst, Vector4s &src0, Vector4s &src1)
John Bauman89401822014-05-06 15:04:28 -04004459 {
John Bauman19bac1e2014-05-06 15:23:49 -04004460 dst.x = SubSat(src0.x, src1.x);
4461 dst.y = SubSat(src0.y, src1.y);
4462 dst.z = SubSat(src0.z, src1.z);
4463 dst.w = SubSat(src0.w, src1.w);
John Bauman89401822014-05-06 15:04:28 -04004464 }
4465
Alexis Hetu96517182015-04-15 10:30:23 -04004466 void PixelRoutine::MAD(Vector4s &dst, Vector4s &src0, Vector4s &src1, Vector4s &src2)
John Bauman89401822014-05-06 15:04:28 -04004467 {
4468 // FIXME: Long fixed-point multiply fixup
4469 {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);}
4470 {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);}
4471 {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);}
4472 {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);}
4473 }
4474
Alexis Hetu96517182015-04-15 10:30:23 -04004475 void PixelRoutine::MUL(Vector4s &dst, Vector4s &src0, Vector4s &src1)
John Bauman89401822014-05-06 15:04:28 -04004476 {
4477 // FIXME: Long fixed-point multiply fixup
4478 {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);}
4479 {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);}
4480 {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);}
4481 {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);}
4482 }
4483
Alexis Hetu96517182015-04-15 10:30:23 -04004484 void PixelRoutine::DP3(Vector4s &dst, Vector4s &src0, Vector4s &src1)
John Bauman89401822014-05-06 15:04:28 -04004485 {
4486 Short4 t0;
4487 Short4 t1;
4488
4489 // FIXME: Long fixed-point multiply fixup
4490 t0 = MulHigh(src0.x, src1.x); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0);
4491 t1 = MulHigh(src0.y, src1.y); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4492 t0 = AddSat(t0, t1);
4493 t1 = MulHigh(src0.z, src1.z); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4494 t0 = AddSat(t0, t1);
4495
John Bauman19bac1e2014-05-06 15:23:49 -04004496 dst.x = t0;
4497 dst.y = t0;
4498 dst.z = t0;
4499 dst.w = t0;
John Bauman89401822014-05-06 15:04:28 -04004500 }
4501
Alexis Hetu96517182015-04-15 10:30:23 -04004502 void PixelRoutine::DP4(Vector4s &dst, Vector4s &src0, Vector4s &src1)
John Bauman89401822014-05-06 15:04:28 -04004503 {
4504 Short4 t0;
4505 Short4 t1;
4506
4507 // FIXME: Long fixed-point multiply fixup
4508 t0 = MulHigh(src0.x, src1.x); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0); t0 = AddSat(t0, t0);
4509 t1 = MulHigh(src0.y, src1.y); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4510 t0 = AddSat(t0, t1);
4511 t1 = MulHigh(src0.z, src1.z); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4512 t0 = AddSat(t0, t1);
4513 t1 = MulHigh(src0.w, src1.w); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1); t1 = AddSat(t1, t1);
4514 t0 = AddSat(t0, t1);
4515
John Bauman19bac1e2014-05-06 15:23:49 -04004516 dst.x = t0;
4517 dst.y = t0;
4518 dst.z = t0;
4519 dst.w = t0;
John Bauman89401822014-05-06 15:04:28 -04004520 }
4521
Alexis Hetu96517182015-04-15 10:30:23 -04004522 void PixelRoutine::LRP(Vector4s &dst, Vector4s &src0, Vector4s &src1, Vector4s &src2)
John Bauman89401822014-05-06 15:04:28 -04004523 {
4524 // FIXME: Long fixed-point multiply fixup
4525 {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);}
4526 {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);}
4527 {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);}
4528 {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);}
4529 }
4530
Alexis Hetu96517182015-04-15 10:30:23 -04004531 void PixelRoutine::TEXCOORD(Vector4s &dst, Float4 &u, Float4 &v, Float4 &s, int coordinate)
John Bauman89401822014-05-06 15:04:28 -04004532 {
4533 Float4 uw;
4534 Float4 vw;
4535 Float4 sw;
4536
4537 if(state.interpolant[2 + coordinate].component & 0x01)
4538 {
John Bauman19bac1e2014-05-06 15:23:49 -04004539 uw = Max(u, Float4(0.0f));
4540 uw = Min(uw, Float4(1.0f));
4541 dst.x = convertFixed12(uw);
John Bauman89401822014-05-06 15:04:28 -04004542 }
4543 else
4544 {
John Bauman19bac1e2014-05-06 15:23:49 -04004545 dst.x = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004546 }
4547
4548 if(state.interpolant[2 + coordinate].component & 0x02)
4549 {
John Bauman19bac1e2014-05-06 15:23:49 -04004550 vw = Max(v, Float4(0.0f));
4551 vw = Min(vw, Float4(1.0f));
4552 dst.y = convertFixed12(vw);
John Bauman89401822014-05-06 15:04:28 -04004553 }
4554 else
4555 {
John Bauman19bac1e2014-05-06 15:23:49 -04004556 dst.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004557 }
4558
4559 if(state.interpolant[2 + coordinate].component & 0x04)
4560 {
John Bauman19bac1e2014-05-06 15:23:49 -04004561 sw = Max(s, Float4(0.0f));
4562 sw = Min(sw, Float4(1.0f));
4563 dst.z = convertFixed12(sw);
John Bauman89401822014-05-06 15:04:28 -04004564 }
4565 else
4566 {
John Bauman19bac1e2014-05-06 15:23:49 -04004567 dst.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004568 }
4569
John Bauman19bac1e2014-05-06 15:23:49 -04004570 dst.w = Short4(0x1000);
John Bauman89401822014-05-06 15:04:28 -04004571 }
4572
Alexis Hetu96517182015-04-15 10:30:23 -04004573 void PixelRoutine::TEXCRD(Vector4s &dst, Float4 &u, Float4 &v, Float4 &s, int coordinate, bool project)
John Bauman89401822014-05-06 15:04:28 -04004574 {
4575 Float4 uw = u;
4576 Float4 vw = v;
4577 Float4 sw = s;
4578
4579 if(project)
4580 {
4581 uw *= Rcp_pp(s);
4582 vw *= Rcp_pp(s);
4583 }
4584
4585 if(state.interpolant[2 + coordinate].component & 0x01)
4586 {
John Bauman19bac1e2014-05-06 15:23:49 -04004587 uw *= Float4(0x1000);
4588 uw = Max(uw, Float4(-0x8000));
4589 uw = Min(uw, Float4(0x7FFF));
4590 dst.x = RoundShort4(uw);
John Bauman89401822014-05-06 15:04:28 -04004591 }
4592 else
4593 {
John Bauman19bac1e2014-05-06 15:23:49 -04004594 dst.x = Short4(0x0000);
John Bauman89401822014-05-06 15:04:28 -04004595 }
4596
4597 if(state.interpolant[2 + coordinate].component & 0x02)
4598 {
John Bauman19bac1e2014-05-06 15:23:49 -04004599 vw *= Float4(0x1000);
4600 vw = Max(vw, Float4(-0x8000));
4601 vw = Min(vw, Float4(0x7FFF));
4602 dst.y = RoundShort4(vw);
John Bauman89401822014-05-06 15:04:28 -04004603 }
4604 else
4605 {
John Bauman19bac1e2014-05-06 15:23:49 -04004606 dst.y = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004607 }
4608
4609 if(state.interpolant[2 + coordinate].component & 0x04)
4610 {
John Bauman19bac1e2014-05-06 15:23:49 -04004611 sw *= Float4(0x1000);
4612 sw = Max(sw, Float4(-0x8000));
4613 sw = Min(sw, Float4(0x7FFF));
4614 dst.z = RoundShort4(sw);
John Bauman89401822014-05-06 15:04:28 -04004615 }
4616 else
4617 {
John Bauman19bac1e2014-05-06 15:23:49 -04004618 dst.z = Short4(0x0000, 0x0000, 0x0000, 0x0000);
John Bauman89401822014-05-06 15:04:28 -04004619 }
4620 }
4621
Alexis Hetu96517182015-04-15 10:30:23 -04004622 void PixelRoutine::TEXDP3(Registers &r, Vector4s &dst, Float4 &u, Float4 &v, Float4 &s, Vector4s &src)
John Bauman89401822014-05-06 15:04:28 -04004623 {
4624 TEXM3X3PAD(r, u, v, s, src, 0, false);
4625
John Bauman19bac1e2014-05-06 15:23:49 -04004626 Short4 t0 = RoundShort4(r.u_ * Float4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04004627
John Bauman19bac1e2014-05-06 15:23:49 -04004628 dst.x = t0;
4629 dst.y = t0;
4630 dst.z = t0;
4631 dst.w = t0;
John Bauman89401822014-05-06 15:04:28 -04004632 }
4633
Alexis Hetu96517182015-04-15 10:30:23 -04004634 void PixelRoutine::TEXDP3TEX(Registers &r, Vector4s &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4s &src0)
John Bauman89401822014-05-06 15:04:28 -04004635 {
4636 TEXM3X3PAD(r, u, v, s, src0, 0, false);
4637
John Bauman19bac1e2014-05-06 15:23:49 -04004638 r.v_ = Float4(0.0f);
4639 r.w_ = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04004640
4641 sampleTexture(r, dst, stage, r.u_, r.v_, r.w_, r.w_);
4642 }
4643
4644 void PixelRoutine::TEXKILL(Int cMask[4], Float4 &u, Float4 &v, Float4 &s)
4645 {
John Bauman19bac1e2014-05-06 15:23:49 -04004646 Int kill = SignMask(CmpNLT(u, Float4(0.0f))) &
4647 SignMask(CmpNLT(v, Float4(0.0f))) &
4648 SignMask(CmpNLT(s, Float4(0.0f)));
John Bauman89401822014-05-06 15:04:28 -04004649
4650 for(unsigned int q = 0; q < state.multiSample; q++)
4651 {
4652 cMask[q] &= kill;
4653 }
4654 }
4655
Alexis Hetu96517182015-04-15 10:30:23 -04004656 void PixelRoutine::TEXKILL(Int cMask[4], Vector4s &src)
John Bauman89401822014-05-06 15:04:28 -04004657 {
John Bauman19bac1e2014-05-06 15:23:49 -04004658 Short4 test = src.x | src.y | src.z;
John Bauman89401822014-05-06 15:04:28 -04004659 Int kill = SignMask(Pack(test, test)) ^ 0x0000000F;
4660
4661 for(unsigned int q = 0; q < state.multiSample; q++)
4662 {
4663 cMask[q] &= kill;
4664 }
4665 }
4666
Alexis Hetu96517182015-04-15 10:30:23 -04004667 void PixelRoutine::TEX(Registers &r, Vector4s &dst, Float4 &u, Float4 &v, Float4 &s, int sampler, bool project)
John Bauman89401822014-05-06 15:04:28 -04004668 {
4669 sampleTexture(r, dst, sampler, u, v, s, s, project);
4670 }
4671
Alexis Hetu96517182015-04-15 10:30:23 -04004672 void PixelRoutine::TEXLD(Registers &r, Vector4s &dst, Vector4s &src, int sampler, bool project)
John Bauman89401822014-05-06 15:04:28 -04004673 {
John Bauman19bac1e2014-05-06 15:23:49 -04004674 Float4 u = Float4(src.x) * Float4(1.0f / 0x0FFE);
4675 Float4 v = Float4(src.y) * Float4(1.0f / 0x0FFE);
4676 Float4 s = Float4(src.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004677
4678 sampleTexture(r, dst, sampler, u, v, s, s, project);
4679 }
4680
Alexis Hetu96517182015-04-15 10:30:23 -04004681 void PixelRoutine::TEXBEM(Registers &r, Vector4s &dst, Vector4s &src, Float4 &u, Float4 &v, Float4 &s, int stage)
John Bauman89401822014-05-06 15:04:28 -04004682 {
John Bauman19bac1e2014-05-06 15:23:49 -04004683 Float4 du = Float4(src.x) * Float4(1.0f / 0x0FFE);
4684 Float4 dv = Float4(src.y) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004685
4686 Float4 du2 = du;
4687 Float4 dv2 = dv;
4688
4689 du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
4690 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
4691 du += dv2;
4692 dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
4693 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
4694 dv += du2;
4695
4696 Float4 u_ = u + du;
4697 Float4 v_ = v + dv;
4698
4699 sampleTexture(r, dst, stage, u_, v_, s, s);
4700 }
4701
Alexis Hetu96517182015-04-15 10:30:23 -04004702 void PixelRoutine::TEXBEML(Registers &r, Vector4s &dst, Vector4s &src, Float4 &u, Float4 &v, Float4 &s, int stage)
John Bauman89401822014-05-06 15:04:28 -04004703 {
John Bauman19bac1e2014-05-06 15:23:49 -04004704 Float4 du = Float4(src.x) * Float4(1.0f / 0x0FFE);
4705 Float4 dv = Float4(src.y) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004706
4707 Float4 du2 = du;
4708 Float4 dv2 = dv;
4709
4710 du *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][0]));
4711 dv2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][0]));
4712 du += dv2;
4713 dv *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[1][1]));
4714 du2 *= *Pointer<Float4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4F[0][1]));
4715 dv += du2;
4716
4717 Float4 u_ = u + du;
4718 Float4 v_ = v + dv;
4719
4720 sampleTexture(r, dst, stage, u_, v_, s, s);
4721
4722 Short4 L;
4723
John Bauman19bac1e2014-05-06 15:23:49 -04004724 L = src.z;
John Bauman89401822014-05-06 15:04:28 -04004725 L = MulHigh(L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceScale4)));
4726 L = L << 4;
4727 L = AddSat(L, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].luminanceOffset4)));
4728 L = Max(L, Short4(0x0000, 0x0000, 0x0000, 0x0000));
John Bauman19bac1e2014-05-06 15:23:49 -04004729 L = Min(L, Short4(0x1000));
John Bauman89401822014-05-06 15:04:28 -04004730
John Bauman19bac1e2014-05-06 15:23:49 -04004731 dst.x = MulHigh(dst.x, L); dst.x = dst.x << 4;
4732 dst.y = MulHigh(dst.y, L); dst.y = dst.y << 4;
4733 dst.z = MulHigh(dst.z, L); dst.z = dst.z << 4;
John Bauman89401822014-05-06 15:04:28 -04004734 }
4735
Alexis Hetu96517182015-04-15 10:30:23 -04004736 void PixelRoutine::TEXREG2AR(Registers &r, Vector4s &dst, Vector4s &src0, int stage)
John Bauman89401822014-05-06 15:04:28 -04004737 {
John Bauman19bac1e2014-05-06 15:23:49 -04004738 Float4 u = Float4(src0.w) * Float4(1.0f / 0x0FFE);
4739 Float4 v = Float4(src0.x) * Float4(1.0f / 0x0FFE);
4740 Float4 s = Float4(src0.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004741
4742 sampleTexture(r, dst, stage, u, v, s, s);
4743 }
4744
Alexis Hetu96517182015-04-15 10:30:23 -04004745 void PixelRoutine::TEXREG2GB(Registers &r, Vector4s &dst, Vector4s &src0, int stage)
John Bauman89401822014-05-06 15:04:28 -04004746 {
John Bauman19bac1e2014-05-06 15:23:49 -04004747 Float4 u = Float4(src0.y) * Float4(1.0f / 0x0FFE);
4748 Float4 v = Float4(src0.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004749 Float4 s = v;
4750
4751 sampleTexture(r, dst, stage, u, v, s, s);
4752 }
4753
Alexis Hetu96517182015-04-15 10:30:23 -04004754 void PixelRoutine::TEXREG2RGB(Registers &r, Vector4s &dst, Vector4s &src0, int stage)
John Bauman89401822014-05-06 15:04:28 -04004755 {
John Bauman19bac1e2014-05-06 15:23:49 -04004756 Float4 u = Float4(src0.x) * Float4(1.0f / 0x0FFE);
4757 Float4 v = Float4(src0.y) * Float4(1.0f / 0x0FFE);
4758 Float4 s = Float4(src0.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004759
4760 sampleTexture(r, dst, stage, u, v, s, s);
4761 }
4762
Alexis Hetu96517182015-04-15 10:30:23 -04004763 void PixelRoutine::TEXM3X2DEPTH(Registers &r, Vector4s &dst, Float4 &u, Float4 &v, Float4 &s, Vector4s &src, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004764 {
4765 TEXM3X2PAD(r, u, v, s, src, 1, signedScaling);
4766
4767 // z / w
4768 r.u_ *= Rcp_pp(r.v_); // FIXME: Set result to 1.0 when division by zero
4769
4770 r.oDepth = r.u_;
4771 }
4772
Alexis Hetu96517182015-04-15 10:30:23 -04004773 void PixelRoutine::TEXM3X2PAD(Registers &r, Float4 &u, Float4 &v, Float4 &s, Vector4s &src0, int component, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004774 {
4775 TEXM3X3PAD(r, u, v, s, src0, component, signedScaling);
4776 }
4777
Alexis Hetu96517182015-04-15 10:30:23 -04004778 void PixelRoutine::TEXM3X2TEX(Registers &r, Vector4s &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4s &src0, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004779 {
4780 TEXM3X2PAD(r, u, v, s, src0, 1, signedScaling);
4781
John Bauman19bac1e2014-05-06 15:23:49 -04004782 r.w_ = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04004783
4784 sampleTexture(r, dst, stage, r.u_, r.v_, r.w_, r.w_);
4785 }
4786
Alexis Hetu96517182015-04-15 10:30:23 -04004787 void PixelRoutine::TEXM3X3(Registers &r, Vector4s &dst, Float4 &u, Float4 &v, Float4 &s, Vector4s &src0, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004788 {
4789 TEXM3X3PAD(r, u, v, s, src0, 2, signedScaling);
4790
John Bauman19bac1e2014-05-06 15:23:49 -04004791 dst.x = RoundShort4(r.u_ * Float4(0x1000));
4792 dst.y = RoundShort4(r.v_ * Float4(0x1000));
4793 dst.z = RoundShort4(r.w_ * Float4(0x1000));
4794 dst.w = Short4(0x1000);
John Bauman89401822014-05-06 15:04:28 -04004795 }
4796
Alexis Hetu96517182015-04-15 10:30:23 -04004797 void PixelRoutine::TEXM3X3PAD(Registers &r, Float4 &u, Float4 &v, Float4 &s, Vector4s &src0, int component, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004798 {
4799 if(component == 0 || previousScaling != signedScaling) // FIXME: Other source modifiers?
4800 {
John Bauman19bac1e2014-05-06 15:23:49 -04004801 r.U = Float4(src0.x);
4802 r.V = Float4(src0.y);
4803 r.W = Float4(src0.z);
John Bauman89401822014-05-06 15:04:28 -04004804
4805 previousScaling = signedScaling;
4806 }
4807
4808 Float4 x = r.U * u + r.V * v + r.W * s;
4809
John Bauman19bac1e2014-05-06 15:23:49 -04004810 x *= Float4(1.0f / 0x1000);
John Bauman89401822014-05-06 15:04:28 -04004811
4812 switch(component)
4813 {
4814 case 0: r.u_ = x; break;
4815 case 1: r.v_ = x; break;
4816 case 2: r.w_ = x; break;
4817 default: ASSERT(false);
4818 }
4819 }
4820
Alexis Hetu96517182015-04-15 10:30:23 -04004821 void PixelRoutine::TEXM3X3SPEC(Registers &r, Vector4s &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4s &src0, Vector4s &src1)
John Bauman89401822014-05-06 15:04:28 -04004822 {
4823 TEXM3X3PAD(r, u, v, s, src0, 2, false);
4824
4825 Float4 E[3]; // Eye vector
4826
John Bauman19bac1e2014-05-06 15:23:49 -04004827 E[0] = Float4(src1.x) * Float4(1.0f / 0x0FFE);
4828 E[1] = Float4(src1.y) * Float4(1.0f / 0x0FFE);
4829 E[2] = Float4(src1.z) * Float4(1.0f / 0x0FFE);
John Bauman89401822014-05-06 15:04:28 -04004830
4831 // Reflection
4832 Float4 u__;
4833 Float4 v__;
4834 Float4 w__;
4835
4836 // (u'', v'', w'') = 2 * (N . E) * N - E * (N . N)
4837 u__ = r.u_ * E[0];
4838 v__ = r.v_ * E[1];
4839 w__ = r.w_ * E[2];
4840 u__ += v__ + w__;
4841 u__ += u__;
4842 v__ = u__;
4843 w__ = u__;
4844 u__ *= r.u_;
4845 v__ *= r.v_;
4846 w__ *= r.w_;
4847 r.u_ *= r.u_;
4848 r.v_ *= r.v_;
4849 r.w_ *= r.w_;
4850 r.u_ += r.v_ + r.w_;
4851 u__ -= E[0] * r.u_;
4852 v__ -= E[1] * r.u_;
4853 w__ -= E[2] * r.u_;
4854
4855 sampleTexture(r, dst, stage, u__, v__, w__, w__);
4856 }
4857
Alexis Hetu96517182015-04-15 10:30:23 -04004858 void PixelRoutine::TEXM3X3TEX(Registers &r, Vector4s &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4s &src0, bool signedScaling)
John Bauman89401822014-05-06 15:04:28 -04004859 {
4860 TEXM3X3PAD(r, u, v, s, src0, 2, signedScaling);
4861
4862 sampleTexture(r, dst, stage, r.u_, r.v_, r.w_, r.w_);
4863 }
4864
Alexis Hetu96517182015-04-15 10:30:23 -04004865 void PixelRoutine::TEXM3X3VSPEC(Registers &r, Vector4s &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4s &src0)
John Bauman89401822014-05-06 15:04:28 -04004866 {
4867 TEXM3X3PAD(r, u, v, s, src0, 2, false);
4868
4869 Float4 E[3]; // Eye vector
4870
John Bauman19bac1e2014-05-06 15:23:49 -04004871 E[0] = r.vf[2 + stage - 2].w;
4872 E[1] = r.vf[2 + stage - 1].w;
4873 E[2] = r.vf[2 + stage - 0].w;
John Bauman89401822014-05-06 15:04:28 -04004874
4875 // Reflection
4876 Float4 u__;
4877 Float4 v__;
4878 Float4 w__;
4879
4880 // (u'', v'', w'') = 2 * (N . E) * N - E * (N . N)
4881 u__ = r.u_ * E[0];
4882 v__ = r.v_ * E[1];
4883 w__ = r.w_ * E[2];
4884 u__ += v__ + w__;
4885 u__ += u__;
4886 v__ = u__;
4887 w__ = u__;
4888 u__ *= r.u_;
4889 v__ *= r.v_;
4890 w__ *= r.w_;
4891 r.u_ *= r.u_;
4892 r.v_ *= r.v_;
4893 r.w_ *= r.w_;
4894 r.u_ += r.v_ + r.w_;
4895 u__ -= E[0] * r.u_;
4896 v__ -= E[1] * r.u_;
4897 w__ -= E[2] * r.u_;
4898
4899 sampleTexture(r, dst, stage, u__, v__, w__, w__);
4900 }
4901
4902 void PixelRoutine::TEXDEPTH(Registers &r)
4903 {
Alexis Hetu96517182015-04-15 10:30:23 -04004904 r.u_ = Float4(r.rs[5].x);
4905 r.v_ = Float4(r.rs[5].y);
John Bauman89401822014-05-06 15:04:28 -04004906
4907 // z / w
4908 r.u_ *= Rcp_pp(r.v_); // FIXME: Set result to 1.0 when division by zero
4909
4910 r.oDepth = r.u_;
4911 }
4912
Alexis Hetu96517182015-04-15 10:30:23 -04004913 void PixelRoutine::CND(Vector4s &dst, Vector4s &src0, Vector4s &src1, Vector4s &src2)
John Bauman89401822014-05-06 15:04:28 -04004914 {
John Bauman19bac1e2014-05-06 15:23:49 -04004915 {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;};
4916 {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;};
4917 {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;};
4918 {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 -04004919 }
4920
Alexis Hetu96517182015-04-15 10:30:23 -04004921 void PixelRoutine::CMP(Vector4s &dst, Vector4s &src0, Vector4s &src1, Vector4s &src2)
John Bauman89401822014-05-06 15:04:28 -04004922 {
John Bauman19bac1e2014-05-06 15:23:49 -04004923 {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;};
4924 {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;};
4925 {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;};
4926 {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 -04004927 }
4928
Alexis Hetu96517182015-04-15 10:30:23 -04004929 void PixelRoutine::BEM(Registers &r, Vector4s &dst, Vector4s &src0, Vector4s &src1, int stage)
John Bauman89401822014-05-06 15:04:28 -04004930 {
4931 Short4 t0;
4932 Short4 t1;
4933
John Bauman19bac1e2014-05-06 15:23:49 -04004934 // dst.x = src0.x + BUMPENVMAT00(stage) * src1.x + BUMPENVMAT10(stage) * src1.y
John Bauman89401822014-05-06 15:04:28 -04004935 t0 = MulHigh(src1.x, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[0][0]))); t0 = t0 << 4; // FIXME: Matrix components range? Overflow hazard.
4936 t1 = MulHigh(src1.y, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[1][0]))); t1 = t1 << 4; // FIXME: Matrix components range? Overflow hazard.
4937 t0 = AddSat(t0, t1);
4938 t0 = AddSat(t0, src0.x);
John Bauman19bac1e2014-05-06 15:23:49 -04004939 dst.x = t0;
John Bauman89401822014-05-06 15:04:28 -04004940
John Bauman19bac1e2014-05-06 15:23:49 -04004941 // dst.y = src0.y + BUMPENVMAT01(stage) * src1.x + BUMPENVMAT11(stage) * src1.y
John Bauman89401822014-05-06 15:04:28 -04004942 t0 = MulHigh(src1.x, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[0][1]))); t0 = t0 << 4; // FIXME: Matrix components range? Overflow hazard.
4943 t1 = MulHigh(src1.y, *Pointer<Short4>(r.data + OFFSET(DrawData,textureStage[stage].bumpmapMatrix4W[1][1]))); t1 = t1 << 4; // FIXME: Matrix components range? Overflow hazard.
4944 t0 = AddSat(t0, t1);
4945 t0 = AddSat(t0, src0.y);
John Bauman19bac1e2014-05-06 15:23:49 -04004946 dst.y = t0;
John Bauman89401822014-05-06 15:04:28 -04004947 }
4948
John Bauman19bac1e2014-05-06 15:23:49 -04004949 void PixelRoutine::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004950 {
Alexis Hetu96517182015-04-15 10:30:23 -04004951 Vector4f row0 = fetchRegisterF(r, src1, 0);
4952 Vector4f row1 = fetchRegisterF(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -04004953
4954 dst.x = dot3(src0, row0);
4955 dst.y = dot3(src0, row1);
4956 }
4957
John Bauman19bac1e2014-05-06 15:23:49 -04004958 void PixelRoutine::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004959 {
Alexis Hetu96517182015-04-15 10:30:23 -04004960 Vector4f row0 = fetchRegisterF(r, src1, 0);
4961 Vector4f row1 = fetchRegisterF(r, src1, 1);
4962 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -04004963
4964 dst.x = dot3(src0, row0);
4965 dst.y = dot3(src0, row1);
4966 dst.z = dot3(src0, row2);
4967 }
4968
John Bauman19bac1e2014-05-06 15:23:49 -04004969 void PixelRoutine::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004970 {
Alexis Hetu96517182015-04-15 10:30:23 -04004971 Vector4f row0 = fetchRegisterF(r, src1, 0);
4972 Vector4f row1 = fetchRegisterF(r, src1, 1);
4973 Vector4f row2 = fetchRegisterF(r, src1, 2);
4974 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -04004975
4976 dst.x = dot3(src0, row0);
4977 dst.y = dot3(src0, row1);
4978 dst.z = dot3(src0, row2);
4979 dst.w = dot3(src0, row3);
4980 }
4981
John Bauman19bac1e2014-05-06 15:23:49 -04004982 void PixelRoutine::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004983 {
Alexis Hetu96517182015-04-15 10:30:23 -04004984 Vector4f row0 = fetchRegisterF(r, src1, 0);
4985 Vector4f row1 = fetchRegisterF(r, src1, 1);
4986 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -04004987
4988 dst.x = dot4(src0, row0);
4989 dst.y = dot4(src0, row1);
4990 dst.z = dot4(src0, row2);
4991 }
4992
John Bauman19bac1e2014-05-06 15:23:49 -04004993 void PixelRoutine::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman89401822014-05-06 15:04:28 -04004994 {
Alexis Hetu96517182015-04-15 10:30:23 -04004995 Vector4f row0 = fetchRegisterF(r, src1, 0);
4996 Vector4f row1 = fetchRegisterF(r, src1, 1);
4997 Vector4f row2 = fetchRegisterF(r, src1, 2);
4998 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -04004999
5000 dst.x = dot4(src0, row0);
5001 dst.y = dot4(src0, row1);
5002 dst.z = dot4(src0, row2);
5003 dst.w = dot4(src0, row3);
5004 }
5005
John Bauman19bac1e2014-05-06 15:23:49 -04005006 void PixelRoutine::TEXLD(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias)
John Bauman89401822014-05-06 15:04:28 -04005007 {
John Bauman19bac1e2014-05-06 15:23:49 -04005008 Vector4f tmp;
5009 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w, src0, src0, project, bias);
John Bauman89401822014-05-06 15:04:28 -04005010
5011 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
5012 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
5013 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
5014 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
5015 }
5016
John Bauman19bac1e2014-05-06 15:23:49 -04005017 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 -04005018 {
John Bauman19bac1e2014-05-06 15:23:49 -04005019 Vector4f tmp;
5020 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w, src2, src3, project, bias, true);
John Bauman89401822014-05-06 15:04:28 -04005021
5022 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
5023 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
5024 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
5025 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
5026 }
5027
John Bauman19bac1e2014-05-06 15:23:49 -04005028 void PixelRoutine::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias)
John Bauman89401822014-05-06 15:04:28 -04005029 {
John Bauman19bac1e2014-05-06 15:23:49 -04005030 Vector4f tmp;
5031 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 -04005032
5033 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
5034 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
5035 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
5036 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
5037 }
5038
John Bauman19bac1e2014-05-06 15:23:49 -04005039 void PixelRoutine::TEXKILL(Int cMask[4], Vector4f &src, unsigned char mask)
John Bauman89401822014-05-06 15:04:28 -04005040 {
5041 Int kill = -1;
5042
John Bauman19bac1e2014-05-06 15:23:49 -04005043 if(mask & 0x1) kill &= SignMask(CmpNLT(src.x, Float4(0.0f)));
5044 if(mask & 0x2) kill &= SignMask(CmpNLT(src.y, Float4(0.0f)));
5045 if(mask & 0x4) kill &= SignMask(CmpNLT(src.z, Float4(0.0f)));
5046 if(mask & 0x8) kill &= SignMask(CmpNLT(src.w, Float4(0.0f)));
5047
5048 // FIXME: Dynamic branching affects TEXKILL?
5049 // if(shader->containsDynamicBranching())
5050 // {
5051 // kill = ~SignMask(enableMask(r));
5052 // }
John Bauman89401822014-05-06 15:04:28 -04005053
5054 for(unsigned int q = 0; q < state.multiSample; q++)
5055 {
5056 cMask[q] &= kill;
5057 }
John Bauman19bac1e2014-05-06 15:23:49 -04005058
5059 // FIXME: Branch to end of shader if all killed?
John Bauman89401822014-05-06 15:04:28 -04005060 }
5061
John Bauman19bac1e2014-05-06 15:23:49 -04005062 void PixelRoutine::DISCARD(Registers &r, Int cMask[4], const Shader::Instruction *instruction)
John Bauman89401822014-05-06 15:04:28 -04005063 {
John Bauman19bac1e2014-05-06 15:23:49 -04005064 Int kill = 0;
5065
5066 if(shader->containsDynamicBranching())
5067 {
5068 kill = ~SignMask(enableMask(r, instruction));
5069 }
5070
5071 for(unsigned int q = 0; q < state.multiSample; q++)
5072 {
5073 cMask[q] &= kill;
5074 }
5075
5076 // FIXME: Branch to end of shader if all killed?
John Bauman89401822014-05-06 15:04:28 -04005077 }
5078
John Bauman19bac1e2014-05-06 15:23:49 -04005079 void PixelRoutine::DFDX(Vector4f &dst, Vector4f &src)
John Bauman89401822014-05-06 15:04:28 -04005080 {
John Bauman19bac1e2014-05-06 15:23:49 -04005081 dst.x = src.x.yyww - src.x.xxzz;
5082 dst.y = src.y.yyww - src.y.xxzz;
5083 dst.z = src.z.yyww - src.z.xxzz;
5084 dst.w = src.w.yyww - src.w.xxzz;
5085 }
5086
5087 void PixelRoutine::DFDY(Vector4f &dst, Vector4f &src)
5088 {
5089 dst.x = src.x.zwzw - src.x.xyxy;
5090 dst.y = src.y.zwzw - src.y.xyxy;
5091 dst.z = src.z.zwzw - src.z.xyxy;
5092 dst.w = src.w.zwzw - src.w.xyxy;
5093 }
5094
5095 void PixelRoutine::FWIDTH(Vector4f &dst, Vector4f &src)
5096 {
5097 // abs(dFdx(src)) + abs(dFdy(src));
5098 dst.x = Abs(src.x.yyww - src.x.xxzz) + Abs(src.x.zwzw - src.x.xyxy);
5099 dst.y = Abs(src.y.yyww - src.x.xxzz) + Abs(src.y.zwzw - src.y.xyxy);
5100 dst.z = Abs(src.z.yyww - src.x.xxzz) + Abs(src.z.zwzw - src.z.xyxy);
5101 dst.w = Abs(src.w.yyww - src.x.xxzz) + Abs(src.w.zwzw - src.w.xyxy);
John Bauman89401822014-05-06 15:04:28 -04005102 }
5103
5104 void PixelRoutine::BREAK(Registers &r)
5105 {
5106 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
5107 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
5108
5109 if(breakDepth == 0)
5110 {
John Bauman19bac1e2014-05-06 15:23:49 -04005111 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04005112 Nucleus::createBr(endBlock);
5113 }
5114 else
5115 {
5116 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
5117 Bool allBreak = SignMask(r.enableBreak) == 0x0;
5118
John Bauman19bac1e2014-05-06 15:23:49 -04005119 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04005120 branch(allBreak, endBlock, deadBlock);
5121 }
5122
5123 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04005124 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04005125 }
5126
John Bauman19bac1e2014-05-06 15:23:49 -04005127 void PixelRoutine::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04005128 {
5129 Int4 condition;
5130
5131 switch(control)
5132 {
John Bauman19bac1e2014-05-06 15:23:49 -04005133 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
5134 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
5135 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
5136 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
5137 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
5138 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04005139 default:
5140 ASSERT(false);
5141 }
5142
John Bauman19bac1e2014-05-06 15:23:49 -04005143 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -04005144 }
5145
5146 void PixelRoutine::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
5147 {
5148 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
5149
John Bauman19bac1e2014-05-06 15:23:49 -04005150 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005151 {
5152 condition = ~condition;
5153 }
5154
John Bauman19bac1e2014-05-06 15:23:49 -04005155 BREAK(r, condition);
5156 }
5157
5158 void PixelRoutine::BREAK(Registers &r, Int4 &condition)
5159 {
John Bauman89401822014-05-06 15:04:28 -04005160 condition &= r.enableStack[r.enableIndex];
5161
5162 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
5163 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
5164
5165 r.enableBreak = r.enableBreak & ~condition;
5166 Bool allBreak = SignMask(r.enableBreak) == 0x0;
5167
John Bauman19bac1e2014-05-06 15:23:49 -04005168 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04005169 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04005170
John Bauman89401822014-05-06 15:04:28 -04005171 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04005172 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04005173 }
5174
John Bauman19bac1e2014-05-06 15:23:49 -04005175 void PixelRoutine::CONTINUE(Registers &r)
5176 {
5177 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
5178 }
5179
5180 void PixelRoutine::TEST()
5181 {
5182 whileTest = true;
5183 }
5184
5185 void PixelRoutine::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04005186 {
5187 if(!labelBlock[labelIndex])
5188 {
5189 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5190 }
5191
John Bauman19bac1e2014-05-06 15:23:49 -04005192 if(callRetBlock[labelIndex].size() > 1)
5193 {
5194 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
5195 }
John Bauman89401822014-05-06 15:04:28 -04005196
John Bauman19bac1e2014-05-06 15:23:49 -04005197 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04005198
5199 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04005200 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
5201
5202 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04005203 }
5204
John Bauman19bac1e2014-05-06 15:23:49 -04005205 void PixelRoutine::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04005206 {
John Bauman19bac1e2014-05-06 15:23:49 -04005207 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04005208 {
John Bauman19bac1e2014-05-06 15:23:49 -04005209 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04005210 }
John Bauman19bac1e2014-05-06 15:23:49 -04005211 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04005212 {
John Bauman19bac1e2014-05-06 15:23:49 -04005213 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04005214 }
5215 else ASSERT(false);
5216 }
5217
John Bauman19bac1e2014-05-06 15:23:49 -04005218 void PixelRoutine::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04005219 {
5220 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,ps.b[boolRegister.index])) != Byte(0)); // FIXME
5221
John Bauman19bac1e2014-05-06 15:23:49 -04005222 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005223 {
5224 condition = !condition;
5225 }
5226
5227 if(!labelBlock[labelIndex])
5228 {
5229 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5230 }
5231
John Bauman19bac1e2014-05-06 15:23:49 -04005232 if(callRetBlock[labelIndex].size() > 1)
5233 {
5234 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
5235 }
John Bauman89401822014-05-06 15:04:28 -04005236
John Bauman19bac1e2014-05-06 15:23:49 -04005237 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04005238
John Bauman19bac1e2014-05-06 15:23:49 -04005239 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
5240 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
5241
5242 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04005243 }
5244
John Bauman19bac1e2014-05-06 15:23:49 -04005245 void PixelRoutine::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04005246 {
5247 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
5248
John Bauman19bac1e2014-05-06 15:23:49 -04005249 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005250 {
5251 condition = ~condition;
5252 }
5253
5254 condition &= r.enableStack[r.enableIndex];
5255
5256 if(!labelBlock[labelIndex])
5257 {
5258 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5259 }
5260
John Bauman19bac1e2014-05-06 15:23:49 -04005261 if(callRetBlock[labelIndex].size() > 1)
5262 {
5263 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
5264 }
John Bauman89401822014-05-06 15:04:28 -04005265
5266 r.enableIndex++;
5267 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04005268 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04005269
John Bauman19bac1e2014-05-06 15:23:49 -04005270 Bool notAllFalse = SignMask(condition) != 0;
5271 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
5272 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04005273
5274 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04005275 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04005276 }
5277
5278 void PixelRoutine::ELSE(Registers &r)
5279 {
5280 ifDepth--;
5281
5282 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
5283 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5284
5285 if(isConditionalIf[ifDepth])
5286 {
5287 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04005288 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04005289
5290 branch(notAllFalse, falseBlock, endBlock);
5291
5292 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
5293 }
5294 else
5295 {
5296 Nucleus::createBr(endBlock);
5297 Nucleus::setInsertBlock(falseBlock);
5298 }
5299
5300 ifFalseBlock[ifDepth] = endBlock;
5301
5302 ifDepth++;
5303 }
5304
5305 void PixelRoutine::ENDIF(Registers &r)
5306 {
5307 ifDepth--;
5308
5309 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
5310
5311 Nucleus::createBr(endBlock);
5312 Nucleus::setInsertBlock(endBlock);
5313
5314 if(isConditionalIf[ifDepth])
5315 {
5316 breakDepth--;
5317 r.enableIndex--;
5318 }
5319 }
5320
John Bauman89401822014-05-06 15:04:28 -04005321 void PixelRoutine::ENDLOOP(Registers &r)
5322 {
5323 loopRepDepth--;
5324
5325 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
5326
5327 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
5328 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
5329
5330 Nucleus::createBr(testBlock);
5331 Nucleus::setInsertBlock(endBlock);
5332
5333 r.loopDepth--;
5334 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
5335 }
5336
John Bauman19bac1e2014-05-06 15:23:49 -04005337 void PixelRoutine::ENDREP(Registers &r)
5338 {
5339 loopRepDepth--;
5340
5341 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
5342 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
5343
5344 Nucleus::createBr(testBlock);
5345 Nucleus::setInsertBlock(endBlock);
5346
5347 r.loopDepth--;
5348 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
5349 }
5350
5351 void PixelRoutine::ENDWHILE(Registers &r)
5352 {
5353 loopRepDepth--;
5354
5355 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
5356 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
5357
5358 Nucleus::createBr(testBlock);
5359 Nucleus::setInsertBlock(endBlock);
5360
5361 r.enableIndex--;
5362 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
5363 whileTest = false;
5364 }
5365
John Bauman89401822014-05-06 15:04:28 -04005366 void PixelRoutine::IF(Registers &r, const Src &src)
5367 {
John Bauman19bac1e2014-05-06 15:23:49 -04005368 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04005369 {
5370 IFb(r, src);
5371 }
John Bauman19bac1e2014-05-06 15:23:49 -04005372 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04005373 {
5374 IFp(r, src);
5375 }
John Bauman19bac1e2014-05-06 15:23:49 -04005376 else
5377 {
Alexis Hetu96517182015-04-15 10:30:23 -04005378 Int4 condition = As<Int4>(fetchRegisterF(r, src).x);
John Bauman19bac1e2014-05-06 15:23:49 -04005379 IF(r, condition);
5380 }
John Bauman89401822014-05-06 15:04:28 -04005381 }
5382
5383 void PixelRoutine::IFb(Registers &r, const Src &boolRegister)
5384 {
John Bauman19bac1e2014-05-06 15:23:49 -04005385 ASSERT(ifDepth < 24 + 4);
5386
John Bauman89401822014-05-06 15:04:28 -04005387 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,ps.b[boolRegister.index])) != Byte(0)); // FIXME
5388
John Bauman19bac1e2014-05-06 15:23:49 -04005389 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005390 {
John Bauman19bac1e2014-05-06 15:23:49 -04005391 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04005392 }
5393
5394 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
5395 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
5396
5397 branch(condition, trueBlock, falseBlock);
5398
5399 isConditionalIf[ifDepth] = false;
5400 ifFalseBlock[ifDepth] = falseBlock;
5401
5402 ifDepth++;
5403 }
5404
John Bauman19bac1e2014-05-06 15:23:49 -04005405 void PixelRoutine::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04005406 {
5407 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
5408
John Bauman19bac1e2014-05-06 15:23:49 -04005409 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04005410 {
5411 condition = ~condition;
5412 }
5413
John Bauman19bac1e2014-05-06 15:23:49 -04005414 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04005415 }
5416
John Bauman19bac1e2014-05-06 15:23:49 -04005417 void PixelRoutine::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04005418 {
5419 Int4 condition;
5420
5421 switch(control)
5422 {
John Bauman19bac1e2014-05-06 15:23:49 -04005423 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
5424 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
5425 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
5426 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
5427 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
5428 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04005429 default:
5430 ASSERT(false);
5431 }
5432
John Bauman19bac1e2014-05-06 15:23:49 -04005433 IF(r, condition);
5434 }
5435
5436 void PixelRoutine::IF(Registers &r, Int4 &condition)
5437 {
John Bauman89401822014-05-06 15:04:28 -04005438 condition &= r.enableStack[r.enableIndex];
5439
5440 r.enableIndex++;
5441 r.enableStack[r.enableIndex] = condition;
5442
5443 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
5444 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
5445
John Bauman19bac1e2014-05-06 15:23:49 -04005446 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04005447
5448 branch(notAllFalse, trueBlock, falseBlock);
5449
5450 isConditionalIf[ifDepth] = true;
5451 ifFalseBlock[ifDepth] = falseBlock;
5452
5453 ifDepth++;
5454 breakDepth++;
5455 }
5456
5457 void PixelRoutine::LABEL(int labelIndex)
5458 {
John Bauman19bac1e2014-05-06 15:23:49 -04005459 if(!labelBlock[labelIndex])
5460 {
5461 labelBlock[labelIndex] = Nucleus::createBasicBlock();
5462 }
5463
John Bauman89401822014-05-06 15:04:28 -04005464 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04005465 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04005466 }
5467
5468 void PixelRoutine::LOOP(Registers &r, const Src &integerRegister)
5469 {
5470 r.loopDepth++;
5471
5472 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][0]));
5473 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][1]));
5474 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][2]));
5475
5476 // If(r.increment[r.loopDepth] == 0)
5477 // {
5478 // r.increment[r.loopDepth] = 1;
5479 // }
5480
5481 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
5482 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
5483 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5484
5485 loopRepTestBlock[loopRepDepth] = testBlock;
5486 loopRepEndBlock[loopRepDepth] = endBlock;
5487
5488 // FIXME: jump(testBlock)
5489 Nucleus::createBr(testBlock);
5490 Nucleus::setInsertBlock(testBlock);
5491
5492 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
5493 Nucleus::setInsertBlock(loopBlock);
5494
5495 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
5496
5497 loopRepDepth++;
5498 breakDepth = 0;
5499 }
5500
5501 void PixelRoutine::REP(Registers &r, const Src &integerRegister)
5502 {
5503 r.loopDepth++;
5504
5505 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,ps.i[integerRegister.index][0]));
5506 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
5507
5508 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
5509 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
5510 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5511
5512 loopRepTestBlock[loopRepDepth] = testBlock;
5513 loopRepEndBlock[loopRepDepth] = endBlock;
5514
5515 // FIXME: jump(testBlock)
5516 Nucleus::createBr(testBlock);
5517 Nucleus::setInsertBlock(testBlock);
5518
5519 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
5520 Nucleus::setInsertBlock(loopBlock);
5521
5522 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
5523
5524 loopRepDepth++;
5525 breakDepth = 0;
5526 }
5527
John Bauman19bac1e2014-05-06 15:23:49 -04005528 void PixelRoutine::WHILE(Registers &r, const Src &temporaryRegister)
5529 {
5530 r.enableIndex++;
5531
5532 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
5533 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
5534 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
5535
5536 loopRepTestBlock[loopRepDepth] = testBlock;
5537 loopRepEndBlock[loopRepDepth] = endBlock;
5538
5539 Int4 restoreBreak = r.enableBreak;
5540 Int4 restoreContinue = r.enableContinue;
5541
5542 // FIXME: jump(testBlock)
5543 Nucleus::createBr(testBlock);
5544 Nucleus::setInsertBlock(testBlock);
5545 r.enableContinue = restoreContinue;
5546
Alexis Hetu96517182015-04-15 10:30:23 -04005547 const Vector4f &src = fetchRegisterF(r, temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04005548 Int4 condition = As<Int4>(src.x);
5549 condition &= r.enableStack[r.enableIndex - 1];
5550 r.enableStack[r.enableIndex] = condition;
5551
5552 Bool notAllFalse = SignMask(condition) != 0;
5553 branch(notAllFalse, loopBlock, endBlock);
5554
5555 Nucleus::setInsertBlock(endBlock);
5556 r.enableBreak = restoreBreak;
5557
5558 Nucleus::setInsertBlock(loopBlock);
5559
5560 loopRepDepth++;
5561 breakDepth = 0;
5562 }
5563
John Bauman89401822014-05-06 15:04:28 -04005564 void PixelRoutine::RET(Registers &r)
5565 {
John Bauman19bac1e2014-05-06 15:23:49 -04005566 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04005567 {
5568 returnBlock = Nucleus::createBasicBlock();
5569 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04005570 }
5571 else
5572 {
John Bauman89401822014-05-06 15:04:28 -04005573 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04005574
John Bauman19bac1e2014-05-06 15:23:49 -04005575 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04005576 {
John Bauman19bac1e2014-05-06 15:23:49 -04005577 // FIXME: Encapsulate
5578 UInt index = r.callStack[--r.stackIndex];
5579
John Bauman66b8ab22014-05-06 15:57:45 -04005580 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04005581 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
5582
5583 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
5584 {
5585 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
5586 }
5587 }
5588 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
5589 {
5590 Nucleus::createBr(callRetBlock[currentLabel][0]);
5591 }
5592 else // Function isn't called
5593 {
5594 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04005595 }
5596
5597 Nucleus::setInsertBlock(unreachableBlock);
5598 Nucleus::createUnreachable();
5599 }
5600 }
5601
John Bauman19bac1e2014-05-06 15:23:49 -04005602 void PixelRoutine::LEAVE(Registers &r)
5603 {
5604 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
5605
5606 // FIXME: Return from function if all instances left
5607 // FIXME: Use enableLeave in other control-flow constructs
5608 }
5609
Alexis Hetu96517182015-04-15 10:30:23 -04005610 void PixelRoutine::writeDestination(Registers &r, Vector4s &d, const Dst &dst)
John Bauman89401822014-05-06 15:04:28 -04005611 {
5612 switch(dst.type)
5613 {
John Bauman19bac1e2014-05-06 15:23:49 -04005614 case Shader::PARAMETER_TEMP:
Alexis Hetu96517182015-04-15 10:30:23 -04005615 if(dst.mask & 0x1) r.rs[dst.index].x = d.x;
5616 if(dst.mask & 0x2) r.rs[dst.index].y = d.y;
5617 if(dst.mask & 0x4) r.rs[dst.index].z = d.z;
5618 if(dst.mask & 0x8) r.rs[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -04005619 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005620 case Shader::PARAMETER_INPUT:
Alexis Hetu96517182015-04-15 10:30:23 -04005621 if(dst.mask & 0x1) r.vs[dst.index].x = d.x;
5622 if(dst.mask & 0x2) r.vs[dst.index].y = d.y;
5623 if(dst.mask & 0x4) r.vs[dst.index].z = d.z;
5624 if(dst.mask & 0x8) r.vs[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -04005625 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005626 case Shader::PARAMETER_CONST: ASSERT(false); break;
5627 case Shader::PARAMETER_TEXTURE:
Alexis Hetu96517182015-04-15 10:30:23 -04005628 if(dst.mask & 0x1) r.ts[dst.index].x = d.x;
5629 if(dst.mask & 0x2) r.ts[dst.index].y = d.y;
5630 if(dst.mask & 0x4) r.ts[dst.index].z = d.z;
5631 if(dst.mask & 0x8) r.ts[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -04005632 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005633 case Shader::PARAMETER_COLOROUT:
Alexis Hetu96517182015-04-15 10:30:23 -04005634 if(dst.mask & 0x1) r.vs[dst.index].x = d.x;
5635 if(dst.mask & 0x2) r.vs[dst.index].y = d.y;
5636 if(dst.mask & 0x4) r.vs[dst.index].z = d.z;
5637 if(dst.mask & 0x8) r.vs[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -04005638 break;
5639 default:
5640 ASSERT(false);
5641 }
5642 }
5643
Alexis Hetu96517182015-04-15 10:30:23 -04005644 Vector4s PixelRoutine::fetchRegisterS(Registers &r, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04005645 {
Alexis Hetu96517182015-04-15 10:30:23 -04005646 Vector4s *reg;
John Bauman89401822014-05-06 15:04:28 -04005647 int i = src.index;
5648
Alexis Hetu96517182015-04-15 10:30:23 -04005649 Vector4s c;
John Bauman89401822014-05-06 15:04:28 -04005650
John Bauman19bac1e2014-05-06 15:23:49 -04005651 if(src.type == Shader::PARAMETER_CONST)
John Bauman89401822014-05-06 15:04:28 -04005652 {
John Bauman19bac1e2014-05-06 15:23:49 -04005653 c.x = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][0]));
5654 c.y = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][1]));
5655 c.z = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][2]));
5656 c.w = *Pointer<Short4>(r.data + OFFSET(DrawData,ps.cW[i][3]));
John Bauman89401822014-05-06 15:04:28 -04005657 }
5658
5659 switch(src.type)
5660 {
Alexis Hetu96517182015-04-15 10:30:23 -04005661 case Shader::PARAMETER_TEMP: reg = &r.rs[i]; break;
5662 case Shader::PARAMETER_INPUT: reg = &r.vs[i]; break;
John Bauman19bac1e2014-05-06 15:23:49 -04005663 case Shader::PARAMETER_CONST: reg = &c; break;
Alexis Hetu96517182015-04-15 10:30:23 -04005664 case Shader::PARAMETER_TEXTURE: reg = &r.ts[i]; break;
5665 case Shader::PARAMETER_VOID: return r.rs[0]; // Dummy
5666 case Shader::PARAMETER_FLOAT4LITERAL: return r.rs[0]; // Dummy
John Bauman89401822014-05-06 15:04:28 -04005667 default:
5668 ASSERT(false);
5669 }
5670
John Bauman66b8ab22014-05-06 15:57:45 -04005671 const Short4 &x = (*reg)[(src.swizzle >> 0) & 0x3];
5672 const Short4 &y = (*reg)[(src.swizzle >> 2) & 0x3];
5673 const Short4 &z = (*reg)[(src.swizzle >> 4) & 0x3];
5674 const Short4 &w = (*reg)[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -04005675
Alexis Hetu96517182015-04-15 10:30:23 -04005676 Vector4s mod;
John Bauman89401822014-05-06 15:04:28 -04005677
5678 switch(src.modifier)
5679 {
John Bauman19bac1e2014-05-06 15:23:49 -04005680 case Shader::MODIFIER_NONE:
5681 mod.x = x;
5682 mod.y = y;
5683 mod.z = z;
5684 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -04005685 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005686 case Shader::MODIFIER_BIAS:
5687 mod.x = SubSat(x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5688 mod.y = SubSat(y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5689 mod.z = SubSat(z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5690 mod.w = SubSat(w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
John Bauman89401822014-05-06 15:04:28 -04005691 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005692 case Shader::MODIFIER_BIAS_NEGATE:
5693 mod.x = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), x);
5694 mod.y = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), y);
5695 mod.z = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), z);
5696 mod.w = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), w);
John Bauman89401822014-05-06 15:04:28 -04005697 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005698 case Shader::MODIFIER_COMPLEMENT:
5699 mod.x = SubSat(Short4(0x1000), x);
5700 mod.y = SubSat(Short4(0x1000), y);
5701 mod.z = SubSat(Short4(0x1000), z);
5702 mod.w = SubSat(Short4(0x1000), w);
John Bauman89401822014-05-06 15:04:28 -04005703 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005704 case Shader::MODIFIER_NEGATE:
5705 mod.x = -x;
5706 mod.y = -y;
5707 mod.z = -z;
5708 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -04005709 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005710 case Shader::MODIFIER_X2:
5711 mod.x = AddSat(x, x);
5712 mod.y = AddSat(y, y);
5713 mod.z = AddSat(z, z);
5714 mod.w = AddSat(w, w);
John Bauman89401822014-05-06 15:04:28 -04005715 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005716 case Shader::MODIFIER_X2_NEGATE:
5717 mod.x = -AddSat(x, x);
5718 mod.y = -AddSat(y, y);
5719 mod.z = -AddSat(z, z);
5720 mod.w = -AddSat(w, w);
John Bauman89401822014-05-06 15:04:28 -04005721 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005722 case Shader::MODIFIER_SIGN:
5723 mod.x = SubSat(x, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5724 mod.y = SubSat(y, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5725 mod.z = SubSat(z, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5726 mod.w = SubSat(w, Short4(0x0800, 0x0800, 0x0800, 0x0800));
5727 mod.x = AddSat(mod.x, mod.x);
5728 mod.y = AddSat(mod.y, mod.y);
5729 mod.z = AddSat(mod.z, mod.z);
5730 mod.w = AddSat(mod.w, mod.w);
John Bauman89401822014-05-06 15:04:28 -04005731 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005732 case Shader::MODIFIER_SIGN_NEGATE:
5733 mod.x = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), x);
5734 mod.y = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), y);
5735 mod.z = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), z);
5736 mod.w = SubSat(Short4(0x0800, 0x0800, 0x0800, 0x0800), w);
5737 mod.x = AddSat(mod.x, mod.x);
5738 mod.y = AddSat(mod.y, mod.y);
5739 mod.z = AddSat(mod.z, mod.z);
5740 mod.w = AddSat(mod.w, mod.w);
John Bauman89401822014-05-06 15:04:28 -04005741 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005742 case Shader::MODIFIER_DZ:
5743 mod.x = x;
5744 mod.y = y;
5745 mod.z = z;
5746 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -04005747 // Projection performed by texture sampler
5748 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005749 case Shader::MODIFIER_DW:
5750 mod.x = x;
5751 mod.y = y;
5752 mod.z = z;
5753 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -04005754 // Projection performed by texture sampler
5755 break;
5756 default:
5757 ASSERT(false);
5758 }
5759
John Bauman19bac1e2014-05-06 15:23:49 -04005760 if(src.type == Shader::PARAMETER_CONST && (src.modifier == Shader::MODIFIER_X2 || src.modifier == Shader::MODIFIER_X2_NEGATE))
John Bauman89401822014-05-06 15:04:28 -04005761 {
John Bauman19bac1e2014-05-06 15:23:49 -04005762 mod.x = Min(mod.x, Short4(0x1000)); mod.x = Max(mod.x, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
5763 mod.y = Min(mod.y, Short4(0x1000)); mod.y = Max(mod.y, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
5764 mod.z = Min(mod.z, Short4(0x1000)); mod.z = Max(mod.z, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
5765 mod.w = Min(mod.w, Short4(0x1000)); mod.w = Max(mod.w, Short4(-0x1000, -0x1000, -0x1000, -0x1000));
John Bauman89401822014-05-06 15:04:28 -04005766 }
5767
5768 return mod;
5769 }
5770
Alexis Hetu96517182015-04-15 10:30:23 -04005771 Vector4f PixelRoutine::fetchRegisterF(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -04005772 {
John Bauman19bac1e2014-05-06 15:23:49 -04005773 Vector4f reg;
John Bauman89401822014-05-06 15:04:28 -04005774 int i = src.index + offset;
5775
5776 switch(src.type)
5777 {
John Bauman19bac1e2014-05-06 15:23:49 -04005778 case Shader::PARAMETER_TEMP:
5779 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman89401822014-05-06 15:04:28 -04005780 {
John Bauman19bac1e2014-05-06 15:23:49 -04005781 reg = r.rf[i];
5782 }
5783 else
5784 {
5785 Int a = relativeAddress(r, src);
5786
5787 reg = r.rf[i + a];
5788 }
5789 break;
5790 case Shader::PARAMETER_INPUT:
5791 {
5792 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -04005793 {
John Bauman19bac1e2014-05-06 15:23:49 -04005794 reg = r.vf[i];
John Bauman89401822014-05-06 15:04:28 -04005795 }
John Bauman19bac1e2014-05-06 15:23:49 -04005796 else if(src.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -04005797 {
5798 Int aL = r.aL[r.loopDepth];
5799
John Bauman19bac1e2014-05-06 15:23:49 -04005800 reg = r.vf[i + aL];
John Bauman89401822014-05-06 15:04:28 -04005801 }
John Bauman19bac1e2014-05-06 15:23:49 -04005802 else
John Bauman89401822014-05-06 15:04:28 -04005803 {
John Bauman19bac1e2014-05-06 15:23:49 -04005804 Int a = relativeAddress(r, src);
5805
5806 reg = r.vf[i + a];
John Bauman89401822014-05-06 15:04:28 -04005807 }
5808 }
5809 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005810 case Shader::PARAMETER_CONST:
5811 reg = readConstant(r, src, offset);
John Bauman89401822014-05-06 15:04:28 -04005812 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005813 case Shader::PARAMETER_TEXTURE:
5814 reg = r.vf[2 + i];
5815 break;
5816 case Shader::PARAMETER_MISCTYPE:
John Bauman89401822014-05-06 15:04:28 -04005817 if(src.index == 0) reg = r.vPos;
5818 if(src.index == 1) reg = r.vFace;
5819 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005820 case Shader::PARAMETER_SAMPLER:
5821 if(src.rel.type == Shader::PARAMETER_VOID)
5822 {
5823 reg.x = As<Float4>(Int4(i));
5824 }
5825 else if(src.rel.type == Shader::PARAMETER_TEMP)
5826 {
5827 reg.x = As<Float4>(Int4(i) + RoundInt(r.rf[src.rel.index].x));
5828 }
5829 return reg;
5830 case Shader::PARAMETER_PREDICATE: return reg; // Dummy
5831 case Shader::PARAMETER_VOID: return reg; // Dummy
5832 case Shader::PARAMETER_FLOAT4LITERAL:
5833 reg.x = Float4(src.value[0]);
5834 reg.y = Float4(src.value[1]);
5835 reg.z = Float4(src.value[2]);
5836 reg.w = Float4(src.value[3]);
5837 break;
5838 case Shader::PARAMETER_CONSTINT: return reg; // Dummy
5839 case Shader::PARAMETER_CONSTBOOL: return reg; // Dummy
5840 case Shader::PARAMETER_LOOP: return reg; // Dummy
5841 case Shader::PARAMETER_COLOROUT:
5842 reg = r.oC[i];
5843 break;
5844 case Shader::PARAMETER_DEPTHOUT:
5845 reg.x = r.oDepth;
5846 break;
John Bauman89401822014-05-06 15:04:28 -04005847 default:
5848 ASSERT(false);
5849 }
5850
John Bauman66b8ab22014-05-06 15:57:45 -04005851 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
5852 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
5853 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
5854 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -04005855
John Bauman19bac1e2014-05-06 15:23:49 -04005856 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -04005857
5858 switch(src.modifier)
5859 {
John Bauman19bac1e2014-05-06 15:23:49 -04005860 case Shader::MODIFIER_NONE:
John Bauman89401822014-05-06 15:04:28 -04005861 mod.x = x;
5862 mod.y = y;
5863 mod.z = z;
5864 mod.w = w;
5865 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005866 case Shader::MODIFIER_NEGATE:
John Bauman89401822014-05-06 15:04:28 -04005867 mod.x = -x;
5868 mod.y = -y;
5869 mod.z = -z;
5870 mod.w = -w;
5871 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005872 case Shader::MODIFIER_ABS:
John Bauman89401822014-05-06 15:04:28 -04005873 mod.x = Abs(x);
5874 mod.y = Abs(y);
5875 mod.z = Abs(z);
5876 mod.w = Abs(w);
5877 break;
John Bauman19bac1e2014-05-06 15:23:49 -04005878 case Shader::MODIFIER_ABS_NEGATE:
John Bauman89401822014-05-06 15:04:28 -04005879 mod.x = -Abs(x);
5880 mod.y = -Abs(y);
5881 mod.z = -Abs(z);
5882 mod.w = -Abs(w);
5883 break;
John Bauman66b8ab22014-05-06 15:57:45 -04005884 case Shader::MODIFIER_NOT:
5885 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
5886 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
5887 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
5888 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
5889 break;
John Bauman89401822014-05-06 15:04:28 -04005890 default:
5891 ASSERT(false);
5892 }
5893
5894 return mod;
5895 }
5896
John Bauman19bac1e2014-05-06 15:23:49 -04005897 Vector4f PixelRoutine::readConstant(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -04005898 {
John Bauman19bac1e2014-05-06 15:23:49 -04005899 Vector4f c;
5900
5901 int i = src.index + offset;
5902
5903 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
5904 {
5905 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[i]));
5906
5907 c.x = c.x.xxxx;
5908 c.y = c.y.yyyy;
5909 c.z = c.z.zzzz;
5910 c.w = c.w.wwww;
5911
Nicolas Capenseafdb222015-05-15 15:24:08 -04005912 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -04005913 {
Alexis Hetu903e0252014-11-25 14:25:32 -05005914 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -04005915 {
5916 const Shader::Instruction &instruction = *shader->getInstruction(j);
5917
5918 if(instruction.opcode == Shader::OPCODE_DEF)
5919 {
5920 if(instruction.dst.index == i)
5921 {
5922 c.x = Float4(instruction.src[0].value[0]);
5923 c.y = Float4(instruction.src[0].value[1]);
5924 c.z = Float4(instruction.src[0].value[2]);
5925 c.w = Float4(instruction.src[0].value[3]);
5926
5927 break;
5928 }
5929 }
5930 }
5931 }
5932 }
5933 else if(src.rel.type == Shader::PARAMETER_LOOP)
5934 {
5935 Int loopCounter = r.aL[r.loopDepth];
5936
5937 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[i]) + loopCounter * 16);
5938
5939 c.x = c.x.xxxx;
5940 c.y = c.y.yyyy;
5941 c.z = c.z.zzzz;
5942 c.w = c.w.wwww;
5943 }
5944 else
5945 {
5946 Int a = relativeAddress(r, src);
5947
5948 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[i]) + a * 16);
5949
5950 c.x = c.x.xxxx;
5951 c.y = c.y.yyyy;
5952 c.z = c.z.zzzz;
5953 c.w = c.w.wwww;
5954 }
5955
5956 return c;
John Bauman89401822014-05-06 15:04:28 -04005957 }
5958
John Bauman19bac1e2014-05-06 15:23:49 -04005959 Int PixelRoutine::relativeAddress(Registers &r, const Shader::Parameter &var)
John Bauman89401822014-05-06 15:04:28 -04005960 {
John Bauman19bac1e2014-05-06 15:23:49 -04005961 ASSERT(var.rel.deterministic);
5962
5963 if(var.rel.type == Shader::PARAMETER_TEMP)
5964 {
5965 return RoundInt(Extract(r.rf[var.rel.index].x, 0)) * var.rel.scale;
5966 }
5967 else if(var.rel.type == Shader::PARAMETER_INPUT)
5968 {
5969 return RoundInt(Extract(r.vf[var.rel.index].x, 0)) * var.rel.scale;
5970 }
5971 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
5972 {
5973 return RoundInt(Extract(r.oC[var.rel.index].x, 0)) * var.rel.scale;
5974 }
5975 else if(var.rel.type == Shader::PARAMETER_CONST)
5976 {
Nicolas Capensb5e7a2a2014-05-06 16:38:19 -04005977 RValue<Float4> c = *Pointer<Float4>(r.data + OFFSET(DrawData,ps.c[var.rel.index]));
John Bauman19bac1e2014-05-06 15:23:49 -04005978
5979 return RoundInt(Extract(c, 0)) * var.rel.scale;
5980 }
5981 else ASSERT(false);
5982
5983 return 0;
5984 }
5985
5986 Int4 PixelRoutine::enableMask(Registers &r, const Shader::Instruction *instruction)
5987 {
5988 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
John Baumand4ae8632014-05-06 16:18:33 -04005989
5990 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -04005991 {
John Baumand4ae8632014-05-06 16:18:33 -04005992 if(shader->containsBreakInstruction() && instruction->analysisBreak)
5993 {
5994 enable &= r.enableBreak;
5995 }
John Bauman19bac1e2014-05-06 15:23:49 -04005996
John Baumand4ae8632014-05-06 16:18:33 -04005997 if(shader->containsContinueInstruction() && instruction->analysisContinue)
5998 {
5999 enable &= r.enableContinue;
6000 }
John Bauman19bac1e2014-05-06 15:23:49 -04006001
John Baumand4ae8632014-05-06 16:18:33 -04006002 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
6003 {
6004 enable &= r.enableLeave;
6005 }
John Bauman19bac1e2014-05-06 15:23:49 -04006006 }
6007
6008 return enable;
6009 }
6010
6011 bool PixelRoutine::colorUsed()
6012 {
6013 return state.colorWriteMask || state.alphaTestActive() || state.shaderContainsKill;
6014 }
6015
6016 unsigned short PixelRoutine::shaderVersion() const
6017 {
6018 return shader ? shader->getVersion() : 0x0000;
6019 }
6020
6021 bool PixelRoutine::interpolateZ() const
6022 {
6023 return state.depthTestActive || state.pixelFogActive() || (shader && shader->vPosDeclared && fullPixelPositionRegister);
6024 }
6025
6026 bool PixelRoutine::interpolateW() const
6027 {
6028 return state.perspective || (shader && shader->vPosDeclared && fullPixelPositionRegister);
John Bauman89401822014-05-06 15:04:28 -04006029 }
6030}