blob: c6341e0d376802f6c0456657c8ef2487472dc3c5 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman89401822014-05-06 15:04:28 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman89401822014-05-06 15:04:28 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
John Bauman89401822014-05-06 15:04:28 -04008//
Nicolas Capens0bac2852016-05-07 06:09:58 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman89401822014-05-06 15:04:28 -040014
15#include "PixelRoutine.hpp"
16
John Bauman89401822014-05-06 15:04:28 -040017#include "SamplerCore.hpp"
18#include "Constants.hpp"
Nicolas Capens708c24b2017-10-26 13:07:10 -040019#include "Renderer/Renderer.hpp"
20#include "Renderer/QuadRasterizer.hpp"
21#include "Renderer/Surface.hpp"
22#include "Renderer/Primitive.hpp"
23#include "Common/Debug.hpp"
John Bauman89401822014-05-06 15:04:28 -040024
John Bauman89401822014-05-06 15:04:28 -040025namespace sw
26{
27 extern bool complementaryDepthBuffer;
28 extern bool postBlendSRGB;
29 extern bool exactColorRounding;
Alexis Hetuf2a8c372015-07-13 11:08:41 -040030 extern bool forceClearRegisters;
John Bauman89401822014-05-06 15:04:28 -040031
Nicolas Capens4f172c72016-01-13 08:34:30 -050032 PixelRoutine::PixelRoutine(const PixelProcessor::State &state, const PixelShader *shader) : QuadRasterizer(state, shader), v(shader && shader->dynamicallyIndexedInput)
John Bauman89401822014-05-06 15:04:28 -040033 {
Alexis Hetu53ad4af2017-12-06 14:49:07 -050034 if(!shader || shader->getShaderModel() < 0x0200 || forceClearRegisters)
John Bauman89401822014-05-06 15:04:28 -040035 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -040036 for(int i = 0; i < MAX_FRAGMENT_INPUTS; i++)
Alexis Hetuf2a8c372015-07-13 11:08:41 -040037 {
Alexis Hetu3e1fd3a2015-08-11 10:15:34 -040038 v[i].x = Float4(0.0f);
39 v[i].y = Float4(0.0f);
40 v[i].z = Float4(0.0f);
41 v[i].w = Float4(0.0f);
Alexis Hetuf2a8c372015-07-13 11:08:41 -040042 }
John Bauman89401822014-05-06 15:04:28 -040043 }
44 }
45
46 PixelRoutine::~PixelRoutine()
47 {
John Bauman89401822014-05-06 15:04:28 -040048 }
49
Nicolas Capens4f172c72016-01-13 08:34:30 -050050 void PixelRoutine::quad(Pointer<Byte> cBuffer[RENDERTARGETS], Pointer<Byte> &zBuffer, Pointer<Byte> &sBuffer, Int cMask[4], Int &x, Int &y)
John Bauman89401822014-05-06 15:04:28 -040051 {
52 #if PERF_PROFILE
53 Long pipeTime = Ticks();
54 #endif
55
John Bauman89401822014-05-06 15:04:28 -040056 const bool earlyDepthTest = !state.depthOverride && !state.alphaTestActive();
John Bauman89401822014-05-06 15:04:28 -040057
58 Int zMask[4]; // Depth mask
59 Int sMask[4]; // Stencil mask
60
61 for(unsigned int q = 0; q < state.multiSample; q++)
62 {
63 zMask[q] = cMask[q];
64 sMask[q] = cMask[q];
65 }
66
67 for(unsigned int q = 0; q < state.multiSample; q++)
68 {
Nicolas Capens4f172c72016-01-13 08:34:30 -050069 stencilTest(sBuffer, q, x, sMask[q], cMask[q]);
John Bauman89401822014-05-06 15:04:28 -040070 }
71
72 Float4 f;
John Bauman89401822014-05-06 15:04:28 -040073 Float4 rhwCentroid;
74
Nicolas Capens4f172c72016-01-13 08:34:30 -050075 Float4 xxxx = Float4(Float(x)) + *Pointer<Float4>(primitive + OFFSET(Primitive,xQuad), 16);
John Bauman89401822014-05-06 15:04:28 -040076
John Bauman19bac1e2014-05-06 15:23:49 -040077 if(interpolateZ())
John Bauman89401822014-05-06 15:04:28 -040078 {
79 for(unsigned int q = 0; q < state.multiSample; q++)
80 {
81 Float4 x = xxxx;
Nicolas Capens4f172c72016-01-13 08:34:30 -050082
John Bauman89401822014-05-06 15:04:28 -040083 if(state.multiSample > 1)
84 {
Nicolas Capens4f172c72016-01-13 08:34:30 -050085 x -= *Pointer<Float4>(constants + OFFSET(Constants,X) + q * sizeof(float4));
John Bauman89401822014-05-06 15:04:28 -040086 }
87
Nicolas Capens5ba372f2017-10-05 16:05:47 -040088 z[q] = interpolate(x, Dz[q], z[q], primitive + OFFSET(Primitive,z), false, false, state.depthClamp);
John Bauman89401822014-05-06 15:04:28 -040089 }
90 }
91
92 Bool depthPass = false;
93
94 if(earlyDepthTest)
95 {
96 for(unsigned int q = 0; q < state.multiSample; q++)
97 {
Nicolas Capens4f172c72016-01-13 08:34:30 -050098 depthPass = depthPass || depthTest(zBuffer, q, x, z[q], sMask[q], zMask[q], cMask[q]);
John Bauman89401822014-05-06 15:04:28 -040099 }
100 }
101
102 If(depthPass || Bool(!earlyDepthTest))
103 {
104 #if PERF_PROFILE
105 Long interpTime = Ticks();
106 #endif
107
Nicolas Capens4f172c72016-01-13 08:34:30 -0500108 Float4 yyyy = Float4(Float(y)) + *Pointer<Float4>(primitive + OFFSET(Primitive,yQuad), 16);
Nicolas Capenscbefe532014-10-16 00:16:01 -0400109
John Bauman89401822014-05-06 15:04:28 -0400110 // Centroid locations
111 Float4 XXXX = Float4(0.0f);
112 Float4 YYYY = Float4(0.0f);
113
114 if(state.centroid)
115 {
116 Float4 WWWW(1.0e-9f);
117
118 for(unsigned int q = 0; q < state.multiSample; q++)
119 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500120 XXXX += *Pointer<Float4>(constants + OFFSET(Constants,sampleX[q]) + 16 * cMask[q]);
121 YYYY += *Pointer<Float4>(constants + OFFSET(Constants,sampleY[q]) + 16 * cMask[q]);
122 WWWW += *Pointer<Float4>(constants + OFFSET(Constants,weight) + 16 * cMask[q]);
John Bauman89401822014-05-06 15:04:28 -0400123 }
124
125 WWWW = Rcp_pp(WWWW);
126 XXXX *= WWWW;
127 YYYY *= WWWW;
128
129 XXXX += xxxx;
130 YYYY += yyyy;
131 }
132
John Bauman19bac1e2014-05-06 15:23:49 -0400133 if(interpolateW())
John Bauman89401822014-05-06 15:04:28 -0400134 {
Nicolas Capens5ba372f2017-10-05 16:05:47 -0400135 w = interpolate(xxxx, Dw, rhw, primitive + OFFSET(Primitive,w), false, false, false);
Nicolas Capens05b3d662016-02-25 23:58:33 -0500136 rhw = reciprocal(w, false, false, true);
John Bauman89401822014-05-06 15:04:28 -0400137
138 if(state.centroid)
139 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500140 rhwCentroid = reciprocal(interpolateCentroid(XXXX, YYYY, rhwCentroid, primitive + OFFSET(Primitive,w), false, false));
John Bauman89401822014-05-06 15:04:28 -0400141 }
142 }
143
Nicolas Capens3b4c93f2016-05-18 12:51:37 -0400144 for(int interpolant = 0; interpolant < MAX_FRAGMENT_INPUTS; interpolant++)
John Bauman89401822014-05-06 15:04:28 -0400145 {
146 for(int component = 0; component < 4; component++)
147 {
John Bauman89401822014-05-06 15:04:28 -0400148 if(state.interpolant[interpolant].component & (1 << component))
149 {
150 if(!state.interpolant[interpolant].centroid)
151 {
Nicolas Capens5ba372f2017-10-05 16:05:47 -0400152 v[interpolant][component] = interpolate(xxxx, Dv[interpolant][component], rhw, primitive + OFFSET(Primitive, V[interpolant][component]), (state.interpolant[interpolant].flat & (1 << component)) != 0, state.perspective, false);
John Bauman89401822014-05-06 15:04:28 -0400153 }
154 else
155 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500156 v[interpolant][component] = interpolateCentroid(XXXX, YYYY, rhwCentroid, primitive + OFFSET(Primitive, V[interpolant][component]), (state.interpolant[interpolant].flat & (1 << component)) != 0, state.perspective);
John Bauman89401822014-05-06 15:04:28 -0400157 }
158 }
159 }
160
161 Float4 rcp;
162
163 switch(state.interpolant[interpolant].project)
164 {
165 case 0:
166 break;
167 case 1:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500168 rcp = reciprocal(v[interpolant].y);
169 v[interpolant].x = v[interpolant].x * rcp;
John Bauman89401822014-05-06 15:04:28 -0400170 break;
171 case 2:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500172 rcp = reciprocal(v[interpolant].z);
173 v[interpolant].x = v[interpolant].x * rcp;
174 v[interpolant].y = v[interpolant].y * rcp;
John Bauman89401822014-05-06 15:04:28 -0400175 break;
176 case 3:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500177 rcp = reciprocal(v[interpolant].w);
178 v[interpolant].x = v[interpolant].x * rcp;
179 v[interpolant].y = v[interpolant].y * rcp;
180 v[interpolant].z = v[interpolant].z * rcp;
John Bauman89401822014-05-06 15:04:28 -0400181 break;
182 }
183 }
184
185 if(state.fog.component)
186 {
Nicolas Capens5ba372f2017-10-05 16:05:47 -0400187 f = interpolate(xxxx, Df, rhw, primitive + OFFSET(Primitive,f), state.fog.flat & 0x01, state.perspective, false);
John Bauman89401822014-05-06 15:04:28 -0400188 }
189
Nicolas Capens4f172c72016-01-13 08:34:30 -0500190 setBuiltins(x, y, z, w);
John Bauman89401822014-05-06 15:04:28 -0400191
192 #if PERF_PROFILE
Nicolas Capens4f172c72016-01-13 08:34:30 -0500193 cycles[PERF_INTERP] += Ticks() - interpTime;
John Bauman89401822014-05-06 15:04:28 -0400194 #endif
195
196 Bool alphaPass = true;
197
198 if(colorUsed())
199 {
200 #if PERF_PROFILE
201 Long shaderTime = Ticks();
202 #endif
203
Nicolas Capens4f172c72016-01-13 08:34:30 -0500204 applyShader(cMask);
John Bauman89401822014-05-06 15:04:28 -0400205
206 #if PERF_PROFILE
Nicolas Capens4f172c72016-01-13 08:34:30 -0500207 cycles[PERF_SHADER] += Ticks() - shaderTime;
John Bauman89401822014-05-06 15:04:28 -0400208 #endif
209
Nicolas Capens4f172c72016-01-13 08:34:30 -0500210 alphaPass = alphaTest(cMask);
John Bauman89401822014-05-06 15:04:28 -0400211
John Bauman19bac1e2014-05-06 15:23:49 -0400212 if((shader && shader->containsKill()) || state.alphaTestActive())
John Bauman89401822014-05-06 15:04:28 -0400213 {
214 for(unsigned int q = 0; q < state.multiSample; q++)
215 {
216 zMask[q] &= cMask[q];
217 sMask[q] &= cMask[q];
218 }
219 }
220 }
221
222 If(alphaPass)
223 {
224 if(!earlyDepthTest)
225 {
226 for(unsigned int q = 0; q < state.multiSample; q++)
227 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500228 depthPass = depthPass || depthTest(zBuffer, q, x, z[q], sMask[q], zMask[q], cMask[q]);
John Bauman89401822014-05-06 15:04:28 -0400229 }
230 }
231
232 #if PERF_PROFILE
233 Long ropTime = Ticks();
234 #endif
235
236 If(depthPass || Bool(earlyDepthTest))
237 {
238 for(unsigned int q = 0; q < state.multiSample; q++)
239 {
240 if(state.multiSampleMask & (1 << q))
241 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500242 writeDepth(zBuffer, q, x, z[q], zMask[q]);
John Bauman89401822014-05-06 15:04:28 -0400243
244 if(state.occlusionEnabled)
245 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500246 occlusion += *Pointer<UInt>(constants + OFFSET(Constants,occlusionCount) + 4 * (zMask[q] & sMask[q]));
John Bauman89401822014-05-06 15:04:28 -0400247 }
248 }
249 }
250
251 if(colorUsed())
252 {
253 #if PERF_PROFILE
John Bauman66b8ab22014-05-06 15:57:45 -0400254 AddAtomic(Pointer<Long>(&profiler.ropOperations), 4);
John Bauman89401822014-05-06 15:04:28 -0400255 #endif
256
Nicolas Capens4f172c72016-01-13 08:34:30 -0500257 rasterOperation(f, cBuffer, x, sMask, zMask, cMask);
John Bauman89401822014-05-06 15:04:28 -0400258 }
259 }
260
261 #if PERF_PROFILE
Nicolas Capens4f172c72016-01-13 08:34:30 -0500262 cycles[PERF_ROP] += Ticks() - ropTime;
John Bauman89401822014-05-06 15:04:28 -0400263 #endif
264 }
265 }
266
267 for(unsigned int q = 0; q < state.multiSample; q++)
268 {
269 if(state.multiSampleMask & (1 << q))
270 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500271 writeStencil(sBuffer, q, x, sMask[q], zMask[q], cMask[q]);
John Bauman89401822014-05-06 15:04:28 -0400272 }
273 }
274
275 #if PERF_PROFILE
Nicolas Capens4f172c72016-01-13 08:34:30 -0500276 cycles[PERF_PIPE] += Ticks() - pipeTime;
John Bauman89401822014-05-06 15:04:28 -0400277 #endif
278 }
279
John Bauman89401822014-05-06 15:04:28 -0400280 Float4 PixelRoutine::interpolateCentroid(Float4 &x, Float4 &y, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective)
281 {
282 Float4 interpolant = *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,C), 16);
283
284 if(!flat)
285 {
286 interpolant += x * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,A), 16) +
287 y * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,B), 16);
288
289 if(perspective)
290 {
291 interpolant *= rhw;
292 }
293 }
294
295 return interpolant;
296 }
297
Nicolas Capens4f172c72016-01-13 08:34:30 -0500298 void PixelRoutine::stencilTest(Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &cMask)
John Bauman89401822014-05-06 15:04:28 -0400299 {
300 if(!state.stencilActive)
301 {
302 return;
303 }
304
305 // (StencilRef & StencilMask) CompFunc (StencilBufferValue & StencilMask)
306
307 Pointer<Byte> buffer = sBuffer + 2 * x;
308
309 if(q > 0)
310 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500311 buffer += q * *Pointer<Int>(data + OFFSET(DrawData,stencilSliceB));
John Bauman89401822014-05-06 15:04:28 -0400312 }
313
Nicolas Capens48ef1252016-11-07 15:30:33 -0500314 Byte8 value = *Pointer<Byte8>(buffer);
John Bauman89401822014-05-06 15:04:28 -0400315 Byte8 valueCCW = value;
316
317 if(!state.noStencilMask)
318 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500319 value &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[0].testMaskQ));
John Bauman89401822014-05-06 15:04:28 -0400320 }
321
Nicolas Capens4f172c72016-01-13 08:34:30 -0500322 stencilTest(value, state.stencilCompareMode, false);
John Bauman89401822014-05-06 15:04:28 -0400323
324 if(state.twoSidedStencil)
325 {
326 if(!state.noStencilMaskCCW)
327 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500328 valueCCW &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[1].testMaskQ));
John Bauman89401822014-05-06 15:04:28 -0400329 }
330
Nicolas Capens4f172c72016-01-13 08:34:30 -0500331 stencilTest(valueCCW, state.stencilCompareModeCCW, true);
John Bauman89401822014-05-06 15:04:28 -0400332
Nicolas Capens4f172c72016-01-13 08:34:30 -0500333 value &= *Pointer<Byte8>(primitive + OFFSET(Primitive,clockwiseMask));
334 valueCCW &= *Pointer<Byte8>(primitive + OFFSET(Primitive,invClockwiseMask));
John Bauman89401822014-05-06 15:04:28 -0400335 value |= valueCCW;
336 }
337
338 sMask = SignMask(value) & cMask;
339 }
340
Nicolas Capens4f172c72016-01-13 08:34:30 -0500341 void PixelRoutine::stencilTest(Byte8 &value, StencilCompareMode stencilCompareMode, bool CCW)
John Bauman89401822014-05-06 15:04:28 -0400342 {
343 Byte8 equal;
344
345 switch(stencilCompareMode)
346 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400347 case STENCIL_ALWAYS:
Nicolas Capens16b5f152016-10-13 13:39:01 -0400348 value = Byte8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
John Bauman89401822014-05-06 15:04:28 -0400349 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400350 case STENCIL_NEVER:
Nicolas Capens16b5f152016-10-13 13:39:01 -0400351 value = Byte8(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
John Bauman89401822014-05-06 15:04:28 -0400352 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400353 case STENCIL_LESS: // a < b ~ b > a
John Bauman89401822014-05-06 15:04:28 -0400354 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
Nicolas Capens4f172c72016-01-13 08:34:30 -0500355 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
John Bauman89401822014-05-06 15:04:28 -0400356 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400357 case STENCIL_EQUAL:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500358 value = CmpEQ(value, *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
John Bauman89401822014-05-06 15:04:28 -0400359 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400360 case STENCIL_NOTEQUAL: // a != b ~ !(a == b)
Nicolas Capens4f172c72016-01-13 08:34:30 -0500361 value = CmpEQ(value, *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
Nicolas Capens16b5f152016-10-13 13:39:01 -0400362 value ^= Byte8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
John Bauman89401822014-05-06 15:04:28 -0400363 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400364 case STENCIL_LESSEQUAL: // a <= b ~ (b > a) || (a == b)
John Bauman89401822014-05-06 15:04:28 -0400365 equal = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500366 equal = CmpEQ(equal, *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
John Bauman89401822014-05-06 15:04:28 -0400367 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
Nicolas Capens4f172c72016-01-13 08:34:30 -0500368 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
John Bauman89401822014-05-06 15:04:28 -0400369 value |= equal;
370 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400371 case STENCIL_GREATER: // a > b
Nicolas Capens4f172c72016-01-13 08:34:30 -0500372 equal = *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ));
John Bauman89401822014-05-06 15:04:28 -0400373 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
374 equal = CmpGT(As<SByte8>(equal), As<SByte8>(value));
375 value = equal;
376 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400377 case STENCIL_GREATEREQUAL: // a >= b ~ !(a < b) ~ !(b > a)
John Bauman89401822014-05-06 15:04:28 -0400378 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
Nicolas Capens4f172c72016-01-13 08:34:30 -0500379 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
Nicolas Capens16b5f152016-10-13 13:39:01 -0400380 value ^= Byte8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
John Bauman89401822014-05-06 15:04:28 -0400381 break;
382 default:
383 ASSERT(false);
384 }
385 }
386
Nicolas Capens4f172c72016-01-13 08:34:30 -0500387 Bool PixelRoutine::depthTest(Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &sMask, Int &zMask, Int &cMask)
John Bauman89401822014-05-06 15:04:28 -0400388 {
389 if(!state.depthTestActive)
390 {
391 return true;
392 }
393
394 Float4 Z = z;
395
John Bauman19bac1e2014-05-06 15:23:49 -0400396 if(shader && shader->depthOverride())
John Bauman89401822014-05-06 15:04:28 -0400397 {
398 if(complementaryDepthBuffer)
399 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500400 Z = Float4(1.0f) - oDepth;
John Bauman89401822014-05-06 15:04:28 -0400401 }
402 else
403 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500404 Z = oDepth;
John Bauman89401822014-05-06 15:04:28 -0400405 }
406 }
407
408 Pointer<Byte> buffer;
409 Int pitch;
410
411 if(!state.quadLayoutDepthBuffer)
412 {
413 buffer = zBuffer + 4 * x;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500414 pitch = *Pointer<Int>(data + OFFSET(DrawData,depthPitchB));
John Bauman89401822014-05-06 15:04:28 -0400415 }
416 else
417 {
418 buffer = zBuffer + 8 * x;
419 }
420
421 if(q > 0)
422 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500423 buffer += q * *Pointer<Int>(data + OFFSET(DrawData,depthSliceB));
John Bauman89401822014-05-06 15:04:28 -0400424 }
425
426 Float4 zValue;
427
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400428 if(state.depthCompareMode != DEPTH_NEVER || (state.depthCompareMode != DEPTH_ALWAYS && !state.depthWriteEnable))
John Bauman89401822014-05-06 15:04:28 -0400429 {
430 if(!state.quadLayoutDepthBuffer)
431 {
432 // FIXME: Properly optimizes?
433 zValue.xy = *Pointer<Float4>(buffer);
434 zValue.zw = *Pointer<Float4>(buffer + pitch - 8);
435 }
436 else
437 {
438 zValue = *Pointer<Float4>(buffer, 16);
439 }
440 }
441
442 Int4 zTest;
443
444 switch(state.depthCompareMode)
445 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400446 case DEPTH_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400447 // Optimized
448 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400449 case DEPTH_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400450 // Optimized
451 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400452 case DEPTH_EQUAL:
John Bauman89401822014-05-06 15:04:28 -0400453 zTest = CmpEQ(zValue, Z);
454 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400455 case DEPTH_NOTEQUAL:
John Bauman89401822014-05-06 15:04:28 -0400456 zTest = CmpNEQ(zValue, Z);
457 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400458 case DEPTH_LESS:
John Bauman89401822014-05-06 15:04:28 -0400459 if(complementaryDepthBuffer)
460 {
461 zTest = CmpLT(zValue, Z);
462 }
463 else
464 {
465 zTest = CmpNLE(zValue, Z);
466 }
467 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400468 case DEPTH_GREATEREQUAL:
John Bauman89401822014-05-06 15:04:28 -0400469 if(complementaryDepthBuffer)
470 {
471 zTest = CmpNLT(zValue, Z);
472 }
473 else
474 {
475 zTest = CmpLE(zValue, Z);
476 }
477 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400478 case DEPTH_LESSEQUAL:
John Bauman89401822014-05-06 15:04:28 -0400479 if(complementaryDepthBuffer)
480 {
481 zTest = CmpLE(zValue, Z);
482 }
483 else
484 {
485 zTest = CmpNLT(zValue, Z);
486 }
487 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400488 case DEPTH_GREATER:
John Bauman89401822014-05-06 15:04:28 -0400489 if(complementaryDepthBuffer)
490 {
491 zTest = CmpNLE(zValue, Z);
492 }
493 else
494 {
495 zTest = CmpLT(zValue, Z);
496 }
497 break;
498 default:
499 ASSERT(false);
500 }
501
502 switch(state.depthCompareMode)
503 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400504 case DEPTH_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400505 zMask = cMask;
506 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400507 case DEPTH_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400508 zMask = 0x0;
509 break;
510 default:
511 zMask = SignMask(zTest) & cMask;
512 break;
513 }
Nicolas Capens05b3d662016-02-25 23:58:33 -0500514
John Bauman89401822014-05-06 15:04:28 -0400515 if(state.stencilActive)
516 {
517 zMask &= sMask;
518 }
519
520 return zMask != 0;
521 }
522
Nicolas Capens4f172c72016-01-13 08:34:30 -0500523 void PixelRoutine::alphaTest(Int &aMask, Short4 &alpha)
John Bauman89401822014-05-06 15:04:28 -0400524 {
525 Short4 cmp;
526 Short4 equal;
527
528 switch(state.alphaCompareMode)
529 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400530 case ALPHA_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400531 aMask = 0xF;
532 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400533 case ALPHA_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400534 aMask = 0x0;
535 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400536 case ALPHA_EQUAL:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500537 cmp = CmpEQ(alpha, *Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4)));
Nicolas Capens33438a62017-09-27 11:47:35 -0400538 aMask = SignMask(PackSigned(cmp, Short4(0x0000)));
John Bauman89401822014-05-06 15:04:28 -0400539 break;
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400540 case ALPHA_NOTEQUAL: // a != b ~ !(a == b)
541 cmp = CmpEQ(alpha, *Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4))) ^ Short4(0xFFFFu); // FIXME
Nicolas Capens33438a62017-09-27 11:47:35 -0400542 aMask = SignMask(PackSigned(cmp, Short4(0x0000)));
John Bauman89401822014-05-06 15:04:28 -0400543 break;
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400544 case ALPHA_LESS: // a < b ~ b > a
Nicolas Capens4f172c72016-01-13 08:34:30 -0500545 cmp = CmpGT(*Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4)), alpha);
Nicolas Capens33438a62017-09-27 11:47:35 -0400546 aMask = SignMask(PackSigned(cmp, Short4(0x0000)));
John Bauman89401822014-05-06 15:04:28 -0400547 break;
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400548 case ALPHA_GREATEREQUAL: // a >= b ~ (a > b) || (a == b) ~ !(b > a) // TODO: Approximate
Nicolas Capens4f172c72016-01-13 08:34:30 -0500549 equal = CmpEQ(alpha, *Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4)));
550 cmp = CmpGT(alpha, *Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4)));
John Bauman89401822014-05-06 15:04:28 -0400551 cmp |= equal;
Nicolas Capens33438a62017-09-27 11:47:35 -0400552 aMask = SignMask(PackSigned(cmp, Short4(0x0000)));
John Bauman89401822014-05-06 15:04:28 -0400553 break;
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400554 case ALPHA_LESSEQUAL: // a <= b ~ !(a > b)
555 cmp = CmpGT(alpha, *Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4))) ^ Short4(0xFFFFu); // FIXME
Nicolas Capens33438a62017-09-27 11:47:35 -0400556 aMask = SignMask(PackSigned(cmp, Short4(0x0000)));
John Bauman89401822014-05-06 15:04:28 -0400557 break;
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400558 case ALPHA_GREATER: // a > b
Nicolas Capens4f172c72016-01-13 08:34:30 -0500559 cmp = CmpGT(alpha, *Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4)));
Nicolas Capens33438a62017-09-27 11:47:35 -0400560 aMask = SignMask(PackSigned(cmp, Short4(0x0000)));
John Bauman89401822014-05-06 15:04:28 -0400561 break;
562 default:
563 ASSERT(false);
564 }
565 }
566
Nicolas Capens4f172c72016-01-13 08:34:30 -0500567 void PixelRoutine::alphaToCoverage(Int cMask[4], Float4 &alpha)
John Bauman89401822014-05-06 15:04:28 -0400568 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500569 Int4 coverage0 = CmpNLT(alpha, *Pointer<Float4>(data + OFFSET(DrawData,a2c0)));
570 Int4 coverage1 = CmpNLT(alpha, *Pointer<Float4>(data + OFFSET(DrawData,a2c1)));
571 Int4 coverage2 = CmpNLT(alpha, *Pointer<Float4>(data + OFFSET(DrawData,a2c2)));
572 Int4 coverage3 = CmpNLT(alpha, *Pointer<Float4>(data + OFFSET(DrawData,a2c3)));
John Bauman89401822014-05-06 15:04:28 -0400573
574 Int aMask0 = SignMask(coverage0);
575 Int aMask1 = SignMask(coverage1);
576 Int aMask2 = SignMask(coverage2);
577 Int aMask3 = SignMask(coverage3);
578
579 cMask[0] &= aMask0;
580 cMask[1] &= aMask1;
581 cMask[2] &= aMask2;
582 cMask[3] &= aMask3;
583 }
584
Nicolas Capens4f172c72016-01-13 08:34:30 -0500585 void PixelRoutine::fogBlend(Vector4f &c0, Float4 &fog)
John Bauman89401822014-05-06 15:04:28 -0400586 {
587 if(!state.fogActive)
588 {
589 return;
590 }
591
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400592 if(state.pixelFogMode != FOG_NONE)
John Bauman89401822014-05-06 15:04:28 -0400593 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500594 pixelFog(fog);
John Bauman89401822014-05-06 15:04:28 -0400595
John Bauman19bac1e2014-05-06 15:23:49 -0400596 fog = Min(fog, Float4(1.0f));
597 fog = Max(fog, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400598 }
599
Nicolas Capens4f172c72016-01-13 08:34:30 -0500600 c0.x -= *Pointer<Float4>(data + OFFSET(DrawData,fog.colorF[0]));
601 c0.y -= *Pointer<Float4>(data + OFFSET(DrawData,fog.colorF[1]));
602 c0.z -= *Pointer<Float4>(data + OFFSET(DrawData,fog.colorF[2]));
John Bauman89401822014-05-06 15:04:28 -0400603
John Bauman19bac1e2014-05-06 15:23:49 -0400604 c0.x *= fog;
605 c0.y *= fog;
606 c0.z *= fog;
John Bauman89401822014-05-06 15:04:28 -0400607
Nicolas Capens4f172c72016-01-13 08:34:30 -0500608 c0.x += *Pointer<Float4>(data + OFFSET(DrawData,fog.colorF[0]));
609 c0.y += *Pointer<Float4>(data + OFFSET(DrawData,fog.colorF[1]));
610 c0.z += *Pointer<Float4>(data + OFFSET(DrawData,fog.colorF[2]));
John Bauman89401822014-05-06 15:04:28 -0400611 }
612
Nicolas Capens4f172c72016-01-13 08:34:30 -0500613 void PixelRoutine::pixelFog(Float4 &visibility)
John Bauman89401822014-05-06 15:04:28 -0400614 {
615 Float4 &zw = visibility;
616
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400617 if(state.pixelFogMode != FOG_NONE)
John Bauman89401822014-05-06 15:04:28 -0400618 {
619 if(state.wBasedFog)
620 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500621 zw = rhw;
John Bauman89401822014-05-06 15:04:28 -0400622 }
623 else
624 {
625 if(complementaryDepthBuffer)
626 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500627 zw = Float4(1.0f) - z[0];
John Bauman89401822014-05-06 15:04:28 -0400628 }
629 else
630 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500631 zw = z[0];
John Bauman89401822014-05-06 15:04:28 -0400632 }
633 }
634 }
635
636 switch(state.pixelFogMode)
637 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400638 case FOG_NONE:
John Bauman89401822014-05-06 15:04:28 -0400639 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400640 case FOG_LINEAR:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500641 zw *= *Pointer<Float4>(data + OFFSET(DrawData,fog.scale));
642 zw += *Pointer<Float4>(data + OFFSET(DrawData,fog.offset));
John Bauman89401822014-05-06 15:04:28 -0400643 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400644 case FOG_EXP:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500645 zw *= *Pointer<Float4>(data + OFFSET(DrawData,fog.densityE));
John Bauman19bac1e2014-05-06 15:23:49 -0400646 zw = exponential2(zw, true);
John Bauman89401822014-05-06 15:04:28 -0400647 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400648 case FOG_EXP2:
John Bauman89401822014-05-06 15:04:28 -0400649 zw *= zw;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500650 zw *= *Pointer<Float4>(data + OFFSET(DrawData,fog.density2E));
John Bauman19bac1e2014-05-06 15:23:49 -0400651 zw = exponential2(zw, true);
John Bauman89401822014-05-06 15:04:28 -0400652 break;
653 default:
654 ASSERT(false);
655 }
656 }
657
Nicolas Capens4f172c72016-01-13 08:34:30 -0500658 void PixelRoutine::writeDepth(Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &zMask)
John Bauman89401822014-05-06 15:04:28 -0400659 {
660 if(!state.depthWriteEnable)
661 {
662 return;
663 }
664
665 Float4 Z = z;
666
John Bauman19bac1e2014-05-06 15:23:49 -0400667 if(shader && shader->depthOverride())
John Bauman89401822014-05-06 15:04:28 -0400668 {
669 if(complementaryDepthBuffer)
670 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500671 Z = Float4(1.0f) - oDepth;
John Bauman89401822014-05-06 15:04:28 -0400672 }
673 else
674 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500675 Z = oDepth;
John Bauman89401822014-05-06 15:04:28 -0400676 }
677 }
678
679 Pointer<Byte> buffer;
680 Int pitch;
681
682 if(!state.quadLayoutDepthBuffer)
Nicolas Capens05b3d662016-02-25 23:58:33 -0500683 {
John Bauman89401822014-05-06 15:04:28 -0400684 buffer = zBuffer + 4 * x;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500685 pitch = *Pointer<Int>(data + OFFSET(DrawData,depthPitchB));
John Bauman89401822014-05-06 15:04:28 -0400686 }
687 else
Nicolas Capens05b3d662016-02-25 23:58:33 -0500688 {
John Bauman89401822014-05-06 15:04:28 -0400689 buffer = zBuffer + 8 * x;
690 }
691
692 if(q > 0)
693 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500694 buffer += q * *Pointer<Int>(data + OFFSET(DrawData,depthSliceB));
John Bauman89401822014-05-06 15:04:28 -0400695 }
696
697 Float4 zValue;
698
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400699 if(state.depthCompareMode != DEPTH_NEVER || (state.depthCompareMode != DEPTH_ALWAYS && !state.depthWriteEnable))
John Bauman89401822014-05-06 15:04:28 -0400700 {
701 if(!state.quadLayoutDepthBuffer)
702 {
703 // FIXME: Properly optimizes?
704 zValue.xy = *Pointer<Float4>(buffer);
705 zValue.zw = *Pointer<Float4>(buffer + pitch - 8);
706 }
707 else
708 {
709 zValue = *Pointer<Float4>(buffer, 16);
710 }
711 }
712
Nicolas Capens4f172c72016-01-13 08:34:30 -0500713 Z = As<Float4>(As<Int4>(Z) & *Pointer<Int4>(constants + OFFSET(Constants,maskD4X) + zMask * 16, 16));
714 zValue = As<Float4>(As<Int4>(zValue) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD4X) + zMask * 16, 16));
John Bauman89401822014-05-06 15:04:28 -0400715 Z = As<Float4>(As<Int4>(Z) | As<Int4>(zValue));
716
717 if(!state.quadLayoutDepthBuffer)
718 {
719 // FIXME: Properly optimizes?
720 *Pointer<Float2>(buffer) = Float2(Z.xy);
721 *Pointer<Float2>(buffer + pitch) = Float2(Z.zw);
722 }
723 else
724 {
725 *Pointer<Float4>(buffer, 16) = Z;
726 }
727 }
728
Nicolas Capens4f172c72016-01-13 08:34:30 -0500729 void PixelRoutine::writeStencil(Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &zMask, Int &cMask)
John Bauman89401822014-05-06 15:04:28 -0400730 {
731 if(!state.stencilActive)
732 {
733 return;
734 }
735
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400736 if(state.stencilPassOperation == OPERATION_KEEP && state.stencilZFailOperation == OPERATION_KEEP && state.stencilFailOperation == OPERATION_KEEP)
John Bauman89401822014-05-06 15:04:28 -0400737 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400738 if(!state.twoSidedStencil || (state.stencilPassOperationCCW == OPERATION_KEEP && state.stencilZFailOperationCCW == OPERATION_KEEP && state.stencilFailOperationCCW == OPERATION_KEEP))
John Bauman89401822014-05-06 15:04:28 -0400739 {
740 return;
741 }
742 }
743
744 if(state.stencilWriteMasked && (!state.twoSidedStencil || state.stencilWriteMaskedCCW))
745 {
746 return;
747 }
748
749 Pointer<Byte> buffer = sBuffer + 2 * x;
750
751 if(q > 0)
752 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500753 buffer += q * *Pointer<Int>(data + OFFSET(DrawData,stencilSliceB));
John Bauman89401822014-05-06 15:04:28 -0400754 }
755
Nicolas Capens48ef1252016-11-07 15:30:33 -0500756 Byte8 bufferValue = *Pointer<Byte8>(buffer);
Nicolas Capens05b3d662016-02-25 23:58:33 -0500757
John Bauman89401822014-05-06 15:04:28 -0400758 Byte8 newValue;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500759 stencilOperation(newValue, bufferValue, state.stencilPassOperation, state.stencilZFailOperation, state.stencilFailOperation, false, zMask, sMask);
John Bauman89401822014-05-06 15:04:28 -0400760
761 if(!state.noStencilWriteMask)
762 {
763 Byte8 maskedValue = bufferValue;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500764 newValue &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[0].writeMaskQ));
765 maskedValue &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[0].invWriteMaskQ));
John Bauman89401822014-05-06 15:04:28 -0400766 newValue |= maskedValue;
767 }
768
769 if(state.twoSidedStencil)
770 {
771 Byte8 newValueCCW;
772
Nicolas Capens4f172c72016-01-13 08:34:30 -0500773 stencilOperation(newValueCCW, bufferValue, state.stencilPassOperationCCW, state.stencilZFailOperationCCW, state.stencilFailOperationCCW, true, zMask, sMask);
John Bauman89401822014-05-06 15:04:28 -0400774
775 if(!state.noStencilWriteMaskCCW)
776 {
777 Byte8 maskedValue = bufferValue;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500778 newValueCCW &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[1].writeMaskQ));
779 maskedValue &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[1].invWriteMaskQ));
John Bauman89401822014-05-06 15:04:28 -0400780 newValueCCW |= maskedValue;
781 }
782
Nicolas Capens4f172c72016-01-13 08:34:30 -0500783 newValue &= *Pointer<Byte8>(primitive + OFFSET(Primitive,clockwiseMask));
784 newValueCCW &= *Pointer<Byte8>(primitive + OFFSET(Primitive,invClockwiseMask));
John Bauman89401822014-05-06 15:04:28 -0400785 newValue |= newValueCCW;
786 }
787
Nicolas Capens4f172c72016-01-13 08:34:30 -0500788 newValue &= *Pointer<Byte8>(constants + OFFSET(Constants,maskB4Q) + 8 * cMask);
789 bufferValue &= *Pointer<Byte8>(constants + OFFSET(Constants,invMaskB4Q) + 8 * cMask);
John Bauman89401822014-05-06 15:04:28 -0400790 newValue |= bufferValue;
791
Nicolas Capens16b5f152016-10-13 13:39:01 -0400792 *Pointer<Byte4>(buffer) = Byte4(newValue);
John Bauman89401822014-05-06 15:04:28 -0400793 }
794
Nicolas Capens4f172c72016-01-13 08:34:30 -0500795 void PixelRoutine::stencilOperation(Byte8 &newValue, Byte8 &bufferValue, StencilOperation stencilPassOperation, StencilOperation stencilZFailOperation, StencilOperation stencilFailOperation, bool CCW, Int &zMask, Int &sMask)
John Bauman89401822014-05-06 15:04:28 -0400796 {
797 Byte8 &pass = newValue;
798 Byte8 fail;
799 Byte8 zFail;
800
Nicolas Capens4f172c72016-01-13 08:34:30 -0500801 stencilOperation(pass, bufferValue, stencilPassOperation, CCW);
John Bauman89401822014-05-06 15:04:28 -0400802
803 if(stencilZFailOperation != stencilPassOperation)
804 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500805 stencilOperation(zFail, bufferValue, stencilZFailOperation, CCW);
John Bauman89401822014-05-06 15:04:28 -0400806 }
807
808 if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
809 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500810 stencilOperation(fail, bufferValue, stencilFailOperation, CCW);
John Bauman89401822014-05-06 15:04:28 -0400811 }
812
813 if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
814 {
815 if(state.depthTestActive && stencilZFailOperation != stencilPassOperation) // zMask valid and values not the same
816 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500817 pass &= *Pointer<Byte8>(constants + OFFSET(Constants,maskB4Q) + 8 * zMask);
818 zFail &= *Pointer<Byte8>(constants + OFFSET(Constants,invMaskB4Q) + 8 * zMask);
John Bauman89401822014-05-06 15:04:28 -0400819 pass |= zFail;
820 }
821
Nicolas Capens4f172c72016-01-13 08:34:30 -0500822 pass &= *Pointer<Byte8>(constants + OFFSET(Constants,maskB4Q) + 8 * sMask);
823 fail &= *Pointer<Byte8>(constants + OFFSET(Constants,invMaskB4Q) + 8 * sMask);
John Bauman89401822014-05-06 15:04:28 -0400824 pass |= fail;
825 }
826 }
827
Nicolas Capens4f172c72016-01-13 08:34:30 -0500828 void PixelRoutine::stencilOperation(Byte8 &output, Byte8 &bufferValue, StencilOperation operation, bool CCW)
John Bauman89401822014-05-06 15:04:28 -0400829 {
830 switch(operation)
831 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400832 case OPERATION_KEEP:
John Bauman89401822014-05-06 15:04:28 -0400833 output = bufferValue;
834 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400835 case OPERATION_ZERO:
Nicolas Capens16b5f152016-10-13 13:39:01 -0400836 output = Byte8(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
John Bauman89401822014-05-06 15:04:28 -0400837 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400838 case OPERATION_REPLACE:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500839 output = *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceQ));
John Bauman89401822014-05-06 15:04:28 -0400840 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400841 case OPERATION_INCRSAT:
John Bauman89401822014-05-06 15:04:28 -0400842 output = AddSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));
843 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400844 case OPERATION_DECRSAT:
John Bauman89401822014-05-06 15:04:28 -0400845 output = SubSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));
846 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400847 case OPERATION_INVERT:
Nicolas Capens16b5f152016-10-13 13:39:01 -0400848 output = bufferValue ^ Byte8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
John Bauman89401822014-05-06 15:04:28 -0400849 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400850 case OPERATION_INCR:
John Bauman89401822014-05-06 15:04:28 -0400851 output = bufferValue + Byte8(1, 1, 1, 1, 1, 1, 1, 1);
852 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400853 case OPERATION_DECR:
John Bauman89401822014-05-06 15:04:28 -0400854 output = bufferValue - Byte8(1, 1, 1, 1, 1, 1, 1, 1);
855 break;
856 default:
857 ASSERT(false);
858 }
859 }
860
Nicolas Capens96d4e092016-11-18 14:22:38 -0500861 void PixelRoutine::blendFactor(Vector4s &blendFactor, const Vector4s &current, const Vector4s &pixel, BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -0400862 {
863 switch(blendFactorActive)
864 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400865 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -0400866 // Optimized
867 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400868 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -0400869 // Optimized
870 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400871 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -0400872 blendFactor.x = current.x;
873 blendFactor.y = current.y;
874 blendFactor.z = current.z;
John Bauman89401822014-05-06 15:04:28 -0400875 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400876 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -0400877 blendFactor.x = Short4(0xFFFFu) - current.x;
878 blendFactor.y = Short4(0xFFFFu) - current.y;
879 blendFactor.z = Short4(0xFFFFu) - current.z;
John Bauman89401822014-05-06 15:04:28 -0400880 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400881 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -0400882 blendFactor.x = pixel.x;
883 blendFactor.y = pixel.y;
884 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -0400885 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400886 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -0400887 blendFactor.x = Short4(0xFFFFu) - pixel.x;
888 blendFactor.y = Short4(0xFFFFu) - pixel.y;
889 blendFactor.z = Short4(0xFFFFu) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -0400890 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400891 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400892 blendFactor.x = current.w;
893 blendFactor.y = current.w;
894 blendFactor.z = current.w;
John Bauman89401822014-05-06 15:04:28 -0400895 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400896 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400897 blendFactor.x = Short4(0xFFFFu) - current.w;
898 blendFactor.y = Short4(0xFFFFu) - current.w;
899 blendFactor.z = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -0400900 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400901 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400902 blendFactor.x = pixel.w;
903 blendFactor.y = pixel.w;
904 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400905 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400906 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400907 blendFactor.x = Short4(0xFFFFu) - pixel.w;
908 blendFactor.y = Short4(0xFFFFu) - pixel.w;
909 blendFactor.z = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400910 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400911 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -0400912 blendFactor.x = Short4(0xFFFFu) - pixel.w;
913 blendFactor.x = Min(As<UShort4>(blendFactor.x), As<UShort4>(current.w));
914 blendFactor.y = blendFactor.x;
915 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -0400916 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400917 case BLEND_CONSTANT:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500918 blendFactor.x = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[0]));
919 blendFactor.y = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[1]));
920 blendFactor.z = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[2]));
John Bauman89401822014-05-06 15:04:28 -0400921 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400922 case BLEND_INVCONSTANT:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500923 blendFactor.x = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[0]));
924 blendFactor.y = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[1]));
925 blendFactor.z = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[2]));
John Bauman89401822014-05-06 15:04:28 -0400926 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400927 case BLEND_CONSTANTALPHA:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500928 blendFactor.x = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[3]));
929 blendFactor.y = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[3]));
930 blendFactor.z = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -0400931 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400932 case BLEND_INVCONSTANTALPHA:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500933 blendFactor.x = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
934 blendFactor.y = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
935 blendFactor.z = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -0400936 break;
937 default:
938 ASSERT(false);
939 }
940 }
Nicolas Capens05b3d662016-02-25 23:58:33 -0500941
Nicolas Capens96d4e092016-11-18 14:22:38 -0500942 void PixelRoutine::blendFactorAlpha(Vector4s &blendFactor, const Vector4s &current, const Vector4s &pixel, BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -0400943 {
944 switch(blendFactorAlphaActive)
945 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400946 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -0400947 // Optimized
948 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400949 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -0400950 // Optimized
951 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400952 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -0400953 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -0400954 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400955 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -0400956 blendFactor.w = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -0400957 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400958 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -0400959 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400960 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400961 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -0400962 blendFactor.w = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400963 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400964 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400965 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -0400966 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400967 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400968 blendFactor.w = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -0400969 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400970 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400971 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400972 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400973 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400974 blendFactor.w = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400975 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400976 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -0400977 blendFactor.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -0400978 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400979 case BLEND_CONSTANT:
980 case BLEND_CONSTANTALPHA:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500981 blendFactor.w = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -0400982 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400983 case BLEND_INVCONSTANT:
984 case BLEND_INVCONSTANTALPHA:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500985 blendFactor.w = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -0400986 break;
987 default:
988 ASSERT(false);
989 }
990 }
991
Alexis Hetu049a1872016-04-25 16:59:58 -0400992 bool PixelRoutine::isSRGB(int index) const
993 {
994 return state.targetFormat[index] == FORMAT_SRGB8_A8 || state.targetFormat[index] == FORMAT_SRGB8_X8;
995 }
996
Nicolas Capens4f172c72016-01-13 08:34:30 -0500997 void PixelRoutine::readPixel(int index, Pointer<Byte> &cBuffer, Int &x, Vector4s &pixel)
John Bauman89401822014-05-06 15:04:28 -0400998 {
John Bauman89401822014-05-06 15:04:28 -0400999 Short4 c01;
1000 Short4 c23;
Maxime Grégoired9762742015-07-08 16:43:48 -04001001 Pointer<Byte> buffer;
Nicolas Capensb40a2562016-01-05 00:08:45 -05001002 Pointer<Byte> buffer2;
John Bauman89401822014-05-06 15:04:28 -04001003
John Bauman89401822014-05-06 15:04:28 -04001004 switch(state.targetFormat[index])
1005 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001006 case FORMAT_R5G6B5:
1007 buffer = cBuffer + 2 * x;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001008 buffer2 = buffer + *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
Nicolas Capensb40a2562016-01-05 00:08:45 -05001009 c01 = As<Short4>(Int2(*Pointer<Int>(buffer), *Pointer<Int>(buffer2)));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001010
1011 pixel.x = c01 & Short4(0xF800u);
1012 pixel.y = (c01 & Short4(0x07E0u)) << 5;
1013 pixel.z = (c01 & Short4(0x001Fu)) << 11;
1014 pixel.w = Short4(0xFFFFu);
1015 break;
John Bauman89401822014-05-06 15:04:28 -04001016 case FORMAT_A8R8G8B8:
1017 buffer = cBuffer + 4 * x;
1018 c01 = *Pointer<Short4>(buffer);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001019 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04001020 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04001021 pixel.z = c01;
1022 pixel.y = c01;
1023 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
1024 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
1025 pixel.x = pixel.z;
1026 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
1027 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
1028 pixel.y = pixel.z;
1029 pixel.w = pixel.x;
1030 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
1031 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
1032 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
1033 pixel.w = UnpackHigh(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001034 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001035 case FORMAT_A8B8G8R8:
Alexis Hetu049a1872016-04-25 16:59:58 -04001036 case FORMAT_SRGB8_A8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001037 buffer = cBuffer + 4 * x;
1038 c01 = *Pointer<Short4>(buffer);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001039 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001040 c23 = *Pointer<Short4>(buffer);
1041 pixel.z = c01;
1042 pixel.y = c01;
1043 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
1044 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
1045 pixel.x = pixel.z;
1046 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
1047 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
1048 pixel.y = pixel.z;
1049 pixel.w = pixel.x;
1050 pixel.x = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
1051 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
1052 pixel.z = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
1053 pixel.w = UnpackHigh(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
1054 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001055 case FORMAT_A8:
1056 buffer = cBuffer + 1 * x;
1057 pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 0);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001058 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
John Bauman66b8ab22014-05-06 15:57:45 -04001059 pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 1);
1060 pixel.w = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
1061 pixel.x = Short4(0x0000);
1062 pixel.y = Short4(0x0000);
1063 pixel.z = Short4(0x0000);
1064 break;
Nicolas Capens7a473b72017-10-25 17:18:55 -04001065 case FORMAT_R8:
1066 buffer = cBuffer + 1 * x;
1067 pixel.x = Insert(pixel.x, *Pointer<Short>(buffer), 0);
1068 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
1069 pixel.x = Insert(pixel.x, *Pointer<Short>(buffer), 1);
1070 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
1071 pixel.y = Short4(0x0000);
1072 pixel.z = Short4(0x0000);
1073 pixel.w = Short4(0xFFFFu);
1074 break;
John Bauman89401822014-05-06 15:04:28 -04001075 case FORMAT_X8R8G8B8:
1076 buffer = cBuffer + 4 * x;
1077 c01 = *Pointer<Short4>(buffer);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001078 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04001079 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04001080 pixel.z = c01;
1081 pixel.y = c01;
1082 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
1083 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
1084 pixel.x = pixel.z;
1085 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
1086 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
1087 pixel.y = pixel.z;
1088 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
1089 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
1090 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
1091 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04001092 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001093 case FORMAT_X8B8G8R8:
Alexis Hetu049a1872016-04-25 16:59:58 -04001094 case FORMAT_SRGB8_X8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001095 buffer = cBuffer + 4 * x;
1096 c01 = *Pointer<Short4>(buffer);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001097 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001098 c23 = *Pointer<Short4>(buffer);
1099 pixel.z = c01;
1100 pixel.y = c01;
1101 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
1102 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
1103 pixel.x = pixel.z;
1104 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
1105 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
1106 pixel.y = pixel.z;
1107 pixel.w = pixel.x;
1108 pixel.x = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
1109 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
1110 pixel.z = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
1111 pixel.w = Short4(0xFFFFu);
1112 break;
John Bauman89401822014-05-06 15:04:28 -04001113 case FORMAT_A8G8R8B8Q:
1114 UNIMPLEMENTED();
Nicolas Capens64750b52015-09-22 10:11:00 -04001115 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
1116 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
1117 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
1118 // pixel.w = UnpackHigh(As<Byte8>(pixel.w), *Pointer<Byte8>(cBuffer + 8 * x + 8));
John Bauman89401822014-05-06 15:04:28 -04001119 break;
1120 case FORMAT_X8G8R8B8Q:
1121 UNIMPLEMENTED();
Nicolas Capens64750b52015-09-22 10:11:00 -04001122 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
1123 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
1124 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
1125 // pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04001126 break;
1127 case FORMAT_A16B16G16R16:
Maxime Grégoired9762742015-07-08 16:43:48 -04001128 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04001129 pixel.x = *Pointer<Short4>(buffer + 8 * x);
1130 pixel.y = *Pointer<Short4>(buffer + 8 * x + 8);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001131 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04001132 pixel.z = *Pointer<Short4>(buffer + 8 * x);
1133 pixel.w = *Pointer<Short4>(buffer + 8 * x + 8);
1134 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04001135 break;
1136 case FORMAT_G16R16:
1137 buffer = cBuffer;
Maxime Grégoired9762742015-07-08 16:43:48 -04001138 pixel.x = *Pointer<Short4>(buffer + 4 * x);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001139 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
Maxime Grégoired9762742015-07-08 16:43:48 -04001140 pixel.y = *Pointer<Short4>(buffer + 4 * x);
John Bauman19bac1e2014-05-06 15:23:49 -04001141 pixel.z = pixel.x;
1142 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.y));
1143 pixel.z = As<Short4>(UnpackHigh(pixel.z, pixel.y));
1144 pixel.y = pixel.z;
1145 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.z));
1146 pixel.y = As<Short4>(UnpackHigh(pixel.y, pixel.z));
1147 pixel.z = Short4(0xFFFFu);
1148 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04001149 break;
1150 default:
1151 ASSERT(false);
1152 }
1153
Alexis Hetu049a1872016-04-25 16:59:58 -04001154 if((postBlendSRGB && state.writeSRGB) || isSRGB(index))
John Bauman89401822014-05-06 15:04:28 -04001155 {
Nicolas Capens4f172c72016-01-13 08:34:30 -05001156 sRGBtoLinear16_12_16(pixel);
John Bauman89401822014-05-06 15:04:28 -04001157 }
Maxime Grégoired9762742015-07-08 16:43:48 -04001158 }
1159
Nicolas Capens4f172c72016-01-13 08:34:30 -05001160 void PixelRoutine::alphaBlend(int index, Pointer<Byte> &cBuffer, Vector4s &current, Int &x)
Maxime Grégoired9762742015-07-08 16:43:48 -04001161 {
1162 if(!state.alphaBlendActive)
1163 {
1164 return;
1165 }
1166
1167 Vector4s pixel;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001168 readPixel(index, cBuffer, x, pixel);
John Bauman89401822014-05-06 15:04:28 -04001169
1170 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
Alexis Hetu96517182015-04-15 10:30:23 -04001171 Vector4s sourceFactor;
1172 Vector4s destFactor;
John Bauman89401822014-05-06 15:04:28 -04001173
Nicolas Capens4f172c72016-01-13 08:34:30 -05001174 blendFactor(sourceFactor, current, pixel, state.sourceBlendFactor);
1175 blendFactor(destFactor, current, pixel, state.destBlendFactor);
John Bauman89401822014-05-06 15:04:28 -04001176
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001177 if(state.sourceBlendFactor != BLEND_ONE && state.sourceBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04001178 {
John Bauman19bac1e2014-05-06 15:23:49 -04001179 current.x = MulHigh(As<UShort4>(current.x), As<UShort4>(sourceFactor.x));
1180 current.y = MulHigh(As<UShort4>(current.y), As<UShort4>(sourceFactor.y));
1181 current.z = MulHigh(As<UShort4>(current.z), As<UShort4>(sourceFactor.z));
John Bauman89401822014-05-06 15:04:28 -04001182 }
Nicolas Capens05b3d662016-02-25 23:58:33 -05001183
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001184 if(state.destBlendFactor != BLEND_ONE && state.destBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04001185 {
John Bauman19bac1e2014-05-06 15:23:49 -04001186 pixel.x = MulHigh(As<UShort4>(pixel.x), As<UShort4>(destFactor.x));
1187 pixel.y = MulHigh(As<UShort4>(pixel.y), As<UShort4>(destFactor.y));
1188 pixel.z = MulHigh(As<UShort4>(pixel.z), As<UShort4>(destFactor.z));
John Bauman89401822014-05-06 15:04:28 -04001189 }
1190
1191 switch(state.blendOperation)
1192 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001193 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04001194 current.x = AddSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
1195 current.y = AddSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
1196 current.z = AddSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04001197 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001198 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04001199 current.x = SubSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
1200 current.y = SubSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
1201 current.z = SubSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04001202 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001203 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04001204 current.x = SubSat(As<UShort4>(pixel.x), As<UShort4>(current.x));
1205 current.y = SubSat(As<UShort4>(pixel.y), As<UShort4>(current.y));
1206 current.z = SubSat(As<UShort4>(pixel.z), As<UShort4>(current.z));
John Bauman89401822014-05-06 15:04:28 -04001207 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001208 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04001209 current.x = Min(As<UShort4>(current.x), As<UShort4>(pixel.x));
1210 current.y = Min(As<UShort4>(current.y), As<UShort4>(pixel.y));
1211 current.z = Min(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04001212 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001213 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04001214 current.x = Max(As<UShort4>(current.x), As<UShort4>(pixel.x));
1215 current.y = Max(As<UShort4>(current.y), As<UShort4>(pixel.y));
1216 current.z = Max(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04001217 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001218 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04001219 // No operation
1220 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001221 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001222 current.x = pixel.x;
1223 current.y = pixel.y;
1224 current.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04001225 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001226 case BLENDOP_NULL:
Alexis Hetu90c7ad62016-06-27 11:50:40 -04001227 current.x = Short4(0x0000);
1228 current.y = Short4(0x0000);
1229 current.z = Short4(0x0000);
John Bauman89401822014-05-06 15:04:28 -04001230 break;
1231 default:
1232 ASSERT(false);
1233 }
1234
Nicolas Capens4f172c72016-01-13 08:34:30 -05001235 blendFactorAlpha(sourceFactor, current, pixel, state.sourceBlendFactorAlpha);
1236 blendFactorAlpha(destFactor, current, pixel, state.destBlendFactorAlpha);
John Bauman89401822014-05-06 15:04:28 -04001237
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001238 if(state.sourceBlendFactorAlpha != BLEND_ONE && state.sourceBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04001239 {
John Bauman19bac1e2014-05-06 15:23:49 -04001240 current.w = MulHigh(As<UShort4>(current.w), As<UShort4>(sourceFactor.w));
John Bauman89401822014-05-06 15:04:28 -04001241 }
Nicolas Capens05b3d662016-02-25 23:58:33 -05001242
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001243 if(state.destBlendFactorAlpha != BLEND_ONE && state.destBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04001244 {
John Bauman19bac1e2014-05-06 15:23:49 -04001245 pixel.w = MulHigh(As<UShort4>(pixel.w), As<UShort4>(destFactor.w));
John Bauman89401822014-05-06 15:04:28 -04001246 }
1247
1248 switch(state.blendOperationAlpha)
1249 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001250 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04001251 current.w = AddSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001252 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001253 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04001254 current.w = SubSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001255 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001256 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04001257 current.w = SubSat(As<UShort4>(pixel.w), As<UShort4>(current.w));
John Bauman89401822014-05-06 15:04:28 -04001258 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001259 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04001260 current.w = Min(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001261 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001262 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04001263 current.w = Max(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001264 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001265 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04001266 // No operation
1267 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001268 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001269 current.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001270 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001271 case BLENDOP_NULL:
Alexis Hetu90c7ad62016-06-27 11:50:40 -04001272 current.w = Short4(0x0000);
John Bauman89401822014-05-06 15:04:28 -04001273 break;
1274 default:
1275 ASSERT(false);
1276 }
1277 }
1278
Nicolas Capens4f172c72016-01-13 08:34:30 -05001279 void PixelRoutine::logicOperation(int index, Pointer<Byte> &cBuffer, Vector4s &current, Int &x)
Maxime Grégoired9762742015-07-08 16:43:48 -04001280 {
Nicolas Capens2afcc802015-08-04 10:34:43 -04001281 if(state.logicalOperation == LOGICALOP_COPY)
Maxime Grégoired9762742015-07-08 16:43:48 -04001282 {
1283 return;
1284 }
1285
1286 Vector4s pixel;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001287 readPixel(index, cBuffer, x, pixel);
Maxime Grégoired9762742015-07-08 16:43:48 -04001288
1289 switch(state.logicalOperation)
1290 {
1291 case LOGICALOP_CLEAR:
Alexis Hetu90c7ad62016-06-27 11:50:40 -04001292 current.x = UShort4(0);
1293 current.y = UShort4(0);
1294 current.z = UShort4(0);
Maxime Grégoired9762742015-07-08 16:43:48 -04001295 break;
1296 case LOGICALOP_SET:
Alexis Hetu90c7ad62016-06-27 11:50:40 -04001297 current.x = UShort4(0xFFFFu);
1298 current.y = UShort4(0xFFFFu);
1299 current.z = UShort4(0xFFFFu);
Maxime Grégoired9762742015-07-08 16:43:48 -04001300 break;
1301 case LOGICALOP_COPY:
1302 ASSERT(false); // Optimized out
1303 break;
1304 case LOGICALOP_COPY_INVERTED:
1305 current.x = ~current.x;
1306 current.y = ~current.y;
1307 current.z = ~current.z;
1308 break;
1309 case LOGICALOP_NOOP:
1310 current.x = pixel.x;
1311 current.y = pixel.y;
1312 current.z = pixel.z;
1313 break;
1314 case LOGICALOP_INVERT:
1315 current.x = ~pixel.x;
1316 current.y = ~pixel.y;
1317 current.z = ~pixel.z;
1318 break;
1319 case LOGICALOP_AND:
1320 current.x = pixel.x & current.x;
1321 current.y = pixel.y & current.y;
1322 current.z = pixel.z & current.z;
1323 break;
1324 case LOGICALOP_NAND:
1325 current.x = ~(pixel.x & current.x);
1326 current.y = ~(pixel.y & current.y);
1327 current.z = ~(pixel.z & current.z);
1328 break;
1329 case LOGICALOP_OR:
1330 current.x = pixel.x | current.x;
1331 current.y = pixel.y | current.y;
1332 current.z = pixel.z | current.z;
1333 break;
1334 case LOGICALOP_NOR:
1335 current.x = ~(pixel.x | current.x);
1336 current.y = ~(pixel.y | current.y);
1337 current.z = ~(pixel.z | current.z);
1338 break;
1339 case LOGICALOP_XOR:
1340 current.x = pixel.x ^ current.x;
1341 current.y = pixel.y ^ current.y;
1342 current.z = pixel.z ^ current.z;
1343 break;
1344 case LOGICALOP_EQUIV:
1345 current.x = ~(pixel.x ^ current.x);
1346 current.y = ~(pixel.y ^ current.y);
1347 current.z = ~(pixel.z ^ current.z);
1348 break;
1349 case LOGICALOP_AND_REVERSE:
1350 current.x = ~pixel.x & current.x;
1351 current.y = ~pixel.y & current.y;
1352 current.z = ~pixel.z & current.z;
1353 break;
1354 case LOGICALOP_AND_INVERTED:
1355 current.x = pixel.x & ~current.x;
1356 current.y = pixel.y & ~current.y;
1357 current.z = pixel.z & ~current.z;
1358 break;
1359 case LOGICALOP_OR_REVERSE:
1360 current.x = ~pixel.x | current.x;
1361 current.y = ~pixel.y | current.y;
1362 current.z = ~pixel.z | current.z;
1363 break;
1364 case LOGICALOP_OR_INVERTED:
1365 current.x = pixel.x | ~current.x;
1366 current.y = pixel.y | ~current.y;
1367 current.z = pixel.z | ~current.z;
1368 break;
1369 default:
1370 ASSERT(false);
1371 }
1372 }
1373
Nicolas Capens4f172c72016-01-13 08:34:30 -05001374 void PixelRoutine::writeColor(int index, Pointer<Byte> &cBuffer, Int &x, Vector4s &current, Int &sMask, Int &zMask, Int &cMask)
John Bauman89401822014-05-06 15:04:28 -04001375 {
Alexis Hetu049a1872016-04-25 16:59:58 -04001376 if((postBlendSRGB && state.writeSRGB) || isSRGB(index))
John Bauman89401822014-05-06 15:04:28 -04001377 {
Nicolas Capens4f172c72016-01-13 08:34:30 -05001378 linearToSRGB16_12_16(current);
John Bauman89401822014-05-06 15:04:28 -04001379 }
1380
1381 if(exactColorRounding)
1382 {
1383 switch(state.targetFormat[index])
1384 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001385 case FORMAT_R5G6B5:
Nicolas Capens26f37222015-09-22 09:53:45 -04001386 current.x = AddSat(As<UShort4>(current.x), UShort4(0x0400));
1387 current.y = AddSat(As<UShort4>(current.y), UShort4(0x0200));
1388 current.z = AddSat(As<UShort4>(current.z), UShort4(0x0400));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001389 break;
John Bauman89401822014-05-06 15:04:28 -04001390 case FORMAT_X8G8R8B8Q:
1391 case FORMAT_A8G8R8B8Q:
1392 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001393 case FORMAT_X8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04001394 case FORMAT_A8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001395 case FORMAT_A8B8G8R8:
Alexis Hetu049a1872016-04-25 16:59:58 -04001396 case FORMAT_SRGB8_X8:
1397 case FORMAT_SRGB8_A8:
Alexis Hetu143dfc72016-09-13 18:41:27 -04001398 case FORMAT_G8R8:
Alexis Hetu0cff3cb2016-05-04 16:23:50 -04001399 case FORMAT_R8:
Alexis Hetu90c7ad62016-06-27 11:50:40 -04001400 current.x = current.x - As<Short4>(As<UShort4>(current.x) >> 8) + Short4(0x0080);
1401 current.y = current.y - As<Short4>(As<UShort4>(current.y) >> 8) + Short4(0x0080);
1402 current.z = current.z - As<Short4>(As<UShort4>(current.z) >> 8) + Short4(0x0080);
1403 current.w = current.w - As<Short4>(As<UShort4>(current.w) >> 8) + Short4(0x0080);
John Bauman89401822014-05-06 15:04:28 -04001404 break;
Nicolas Capensb69aa272016-01-02 00:06:41 -05001405 default:
1406 break;
John Bauman89401822014-05-06 15:04:28 -04001407 }
1408 }
1409
1410 int rgbaWriteMask = state.colorWriteActive(index);
Nicolas Capens3b396462016-01-02 00:23:53 -05001411 int bgraWriteMask = (rgbaWriteMask & 0x0000000A) | (rgbaWriteMask & 0x00000001) << 2 | (rgbaWriteMask & 0x00000004) >> 2;
John Bauman89401822014-05-06 15:04:28 -04001412
1413 switch(state.targetFormat[index])
1414 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001415 case FORMAT_R5G6B5:
1416 {
1417 current.x = current.x & Short4(0xF800u);
1418 current.y = As<UShort4>(current.y & Short4(0xFC00u)) >> 5;
1419 current.z = As<UShort4>(current.z) >> 11;
1420
1421 current.x = current.x | current.y | current.z;
1422 }
1423 break;
John Bauman89401822014-05-06 15:04:28 -04001424 case FORMAT_X8G8R8B8Q:
1425 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04001426 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1427 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1428 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04001429
John Bauman19bac1e2014-05-06 15:23:49 -04001430 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
1431 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
John Bauman89401822014-05-06 15:04:28 -04001432 break;
1433 case FORMAT_A8G8R8B8Q:
1434 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04001435 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1436 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1437 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
1438 // current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04001439
John Bauman19bac1e2014-05-06 15:23:49 -04001440 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
1441 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04001442 break;
1443 case FORMAT_X8R8G8B8:
1444 case FORMAT_A8R8G8B8:
1445 if(state.targetFormat[index] == FORMAT_X8R8G8B8 || rgbaWriteMask == 0x7)
1446 {
John Bauman19bac1e2014-05-06 15:23:49 -04001447 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1448 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1449 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04001450
Nicolas Capens33438a62017-09-27 11:47:35 -04001451 current.z = As<Short4>(PackUnsigned(current.z, current.x));
1452 current.y = As<Short4>(PackUnsigned(current.y, current.y));
John Bauman89401822014-05-06 15:04:28 -04001453
John Bauman19bac1e2014-05-06 15:23:49 -04001454 current.x = current.z;
1455 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
1456 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
1457 current.y = current.z;
1458 current.z = As<Short4>(UnpackLow(current.z, current.x));
1459 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04001460 }
1461 else
1462 {
John Bauman19bac1e2014-05-06 15:23:49 -04001463 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1464 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1465 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
1466 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04001467
Nicolas Capens33438a62017-09-27 11:47:35 -04001468 current.z = As<Short4>(PackUnsigned(current.z, current.x));
1469 current.y = As<Short4>(PackUnsigned(current.y, current.w));
John Bauman89401822014-05-06 15:04:28 -04001470
John Bauman19bac1e2014-05-06 15:23:49 -04001471 current.x = current.z;
1472 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
1473 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
1474 current.y = current.z;
1475 current.z = As<Short4>(UnpackLow(current.z, current.x));
1476 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04001477 }
1478 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001479 case FORMAT_X8B8G8R8:
1480 case FORMAT_A8B8G8R8:
Alexis Hetu049a1872016-04-25 16:59:58 -04001481 case FORMAT_SRGB8_X8:
1482 case FORMAT_SRGB8_A8:
1483 if(state.targetFormat[index] == FORMAT_X8B8G8R8 || state.targetFormat[index] == FORMAT_SRGB8_X8 || rgbaWriteMask == 0x7)
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001484 {
1485 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1486 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1487 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
1488
Nicolas Capens33438a62017-09-27 11:47:35 -04001489 current.z = As<Short4>(PackUnsigned(current.x, current.z));
1490 current.y = As<Short4>(PackUnsigned(current.y, current.y));
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001491
1492 current.x = current.z;
1493 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
1494 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
1495 current.y = current.z;
1496 current.z = As<Short4>(UnpackLow(current.z, current.x));
1497 current.y = As<Short4>(UnpackHigh(current.y, current.x));
1498 }
1499 else
1500 {
1501 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1502 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1503 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
1504 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
1505
Nicolas Capens33438a62017-09-27 11:47:35 -04001506 current.z = As<Short4>(PackUnsigned(current.x, current.z));
1507 current.y = As<Short4>(PackUnsigned(current.y, current.w));
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001508
1509 current.x = current.z;
1510 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
1511 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
1512 current.y = current.z;
1513 current.z = As<Short4>(UnpackLow(current.z, current.x));
1514 current.y = As<Short4>(UnpackHigh(current.y, current.x));
1515 }
1516 break;
Alexis Hetu143dfc72016-09-13 18:41:27 -04001517 case FORMAT_G8R8:
1518 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1519 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
Nicolas Capens33438a62017-09-27 11:47:35 -04001520 current.x = As<Short4>(PackUnsigned(current.x, current.x));
1521 current.y = As<Short4>(PackUnsigned(current.y, current.y));
Alexis Hetu143dfc72016-09-13 18:41:27 -04001522 current.x = UnpackLow(As<Byte8>(current.x), As<Byte8>(current.y));
1523 break;
Alexis Hetu0cff3cb2016-05-04 16:23:50 -04001524 case FORMAT_R8:
1525 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
Nicolas Capens33438a62017-09-27 11:47:35 -04001526 current.x = As<Short4>(PackUnsigned(current.x, current.x));
Alexis Hetu0cff3cb2016-05-04 16:23:50 -04001527 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001528 case FORMAT_A8:
1529 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
Nicolas Capens33438a62017-09-27 11:47:35 -04001530 current.w = As<Short4>(PackUnsigned(current.w, current.w));
John Bauman66b8ab22014-05-06 15:57:45 -04001531 break;
John Bauman89401822014-05-06 15:04:28 -04001532 case FORMAT_G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04001533 current.z = current.x;
1534 current.x = As<Short4>(UnpackLow(current.x, current.y));
1535 current.z = As<Short4>(UnpackHigh(current.z, current.y));
1536 current.y = current.z;
John Bauman89401822014-05-06 15:04:28 -04001537 break;
1538 case FORMAT_A16B16G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04001539 transpose4x4(current.x, current.y, current.z, current.w);
John Bauman89401822014-05-06 15:04:28 -04001540 break;
John Bauman89401822014-05-06 15:04:28 -04001541 default:
1542 ASSERT(false);
1543 }
1544
John Bauman19bac1e2014-05-06 15:23:49 -04001545 Short4 c01 = current.z;
1546 Short4 c23 = current.y;
John Bauman89401822014-05-06 15:04:28 -04001547
1548 Int xMask; // Combination of all masks
1549
1550 if(state.depthTestActive)
1551 {
1552 xMask = zMask;
1553 }
1554 else
1555 {
1556 xMask = cMask;
1557 }
1558
1559 if(state.stencilActive)
1560 {
1561 xMask &= sMask;
1562 }
1563
John Bauman89401822014-05-06 15:04:28 -04001564 switch(state.targetFormat[index])
1565 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001566 case FORMAT_R5G6B5:
1567 {
1568 Pointer<Byte> buffer = cBuffer + 2 * x;
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001569 Int value = *Pointer<Int>(buffer);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001570
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001571 Int c01 = Extract(As<Int2>(current.x), 0);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001572
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001573 if((bgraWriteMask & 0x00000007) != 0x00000007)
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001574 {
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001575 Int masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001576 c01 &= *Pointer<Int>(constants + OFFSET(Constants,mask565Q[bgraWriteMask & 0x7][0]));
Nicolas Capens7d9bdcb2015-05-26 02:09:27 -04001577 masked &= *Pointer<Int>(constants + OFFSET(Constants,mask565Q[~bgraWriteMask & 0x7][0]));
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001578 c01 |= masked;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001579 }
1580
Nicolas Capens4f172c72016-01-13 08:34:30 -05001581 c01 &= *Pointer<Int>(constants + OFFSET(Constants,maskW4Q[0][0]) + xMask * 8);
1582 value &= *Pointer<Int>(constants + OFFSET(Constants,invMaskW4Q[0][0]) + xMask * 8);
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001583 c01 |= value;
1584 *Pointer<Int>(buffer) = c01;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001585
Nicolas Capens4f172c72016-01-13 08:34:30 -05001586 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001587 value = *Pointer<Int>(buffer);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001588
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001589 Int c23 = Extract(As<Int2>(current.x), 1);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001590
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001591 if((bgraWriteMask & 0x00000007) != 0x00000007)
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001592 {
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001593 Int masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001594 c23 &= *Pointer<Int>(constants + OFFSET(Constants,mask565Q[bgraWriteMask & 0x7][0]));
Nicolas Capens7d9bdcb2015-05-26 02:09:27 -04001595 masked &= *Pointer<Int>(constants + OFFSET(Constants,mask565Q[~bgraWriteMask & 0x7][0]));
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001596 c23 |= masked;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001597 }
1598
Nicolas Capens4f172c72016-01-13 08:34:30 -05001599 c23 &= *Pointer<Int>(constants + OFFSET(Constants,maskW4Q[0][2]) + xMask * 8);
1600 value &= *Pointer<Int>(constants + OFFSET(Constants,invMaskW4Q[0][2]) + xMask * 8);
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001601 c23 |= value;
1602 *Pointer<Int>(buffer) = c23;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001603 }
1604 break;
John Bauman89401822014-05-06 15:04:28 -04001605 case FORMAT_A8G8R8B8Q:
1606 case FORMAT_X8G8R8B8Q: // FIXME: Don't touch alpha?
1607 UNIMPLEMENTED();
1608 // value = *Pointer<Short4>(cBuffer + 8 * x + 0);
1609
1610 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
1611 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
1612 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
1613 // {
1614 // Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001615 // c01 &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
1616 // masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04001617 // c01 |= masked;
1618 // }
1619
Nicolas Capens4f172c72016-01-13 08:34:30 -05001620 // c01 &= *Pointer<Short4>(constants + OFFSET(Constants,maskD01Q) + xMask * 8);
1621 // value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04001622 // c01 |= value;
1623 // *Pointer<Short4>(cBuffer + 8 * x + 0) = c01;
1624
1625 // value = *Pointer<Short4>(cBuffer + 8 * x + 8);
1626
1627 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
1628 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
1629 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
1630 // {
1631 // Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001632 // c23 &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
1633 // masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04001634 // c23 |= masked;
1635 // }
1636
Nicolas Capens4f172c72016-01-13 08:34:30 -05001637 // c23 &= *Pointer<Short4>(constants + OFFSET(Constants,maskD23Q) + xMask * 8);
1638 // value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04001639 // c23 |= value;
1640 // *Pointer<Short4>(cBuffer + 8 * x + 8) = c23;
1641 break;
1642 case FORMAT_A8R8G8B8:
1643 case FORMAT_X8R8G8B8: // FIXME: Don't touch alpha?
John Bauman89401822014-05-06 15:04:28 -04001644 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001645 Pointer<Byte> buffer = cBuffer + x * 4;
1646 Short4 value = *Pointer<Short4>(buffer);
1647
1648 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
1649 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
1650 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
1651 {
1652 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001653 c01 &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
1654 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001655 c01 |= masked;
1656 }
1657
Nicolas Capens4f172c72016-01-13 08:34:30 -05001658 c01 &= *Pointer<Short4>(constants + OFFSET(Constants,maskD01Q) + xMask * 8);
1659 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001660 c01 |= value;
1661 *Pointer<Short4>(buffer) = c01;
1662
Nicolas Capens4f172c72016-01-13 08:34:30 -05001663 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001664 value = *Pointer<Short4>(buffer);
1665
1666 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
1667 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
1668 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
1669 {
1670 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001671 c23 &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
1672 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001673 c23 |= masked;
1674 }
1675
Nicolas Capens4f172c72016-01-13 08:34:30 -05001676 c23 &= *Pointer<Short4>(constants + OFFSET(Constants,maskD23Q) + xMask * 8);
1677 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001678 c23 |= value;
1679 *Pointer<Short4>(buffer) = c23;
John Bauman89401822014-05-06 15:04:28 -04001680 }
John Bauman89401822014-05-06 15:04:28 -04001681 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001682 case FORMAT_A8B8G8R8:
1683 case FORMAT_X8B8G8R8: // FIXME: Don't touch alpha?
Alexis Hetu049a1872016-04-25 16:59:58 -04001684 case FORMAT_SRGB8_X8:
1685 case FORMAT_SRGB8_A8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001686 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001687 Pointer<Byte> buffer = cBuffer + x * 4;
1688 Short4 value = *Pointer<Short4>(buffer);
1689
Alexis Hetu049a1872016-04-25 16:59:58 -04001690 bool masked = (((state.targetFormat[index] == FORMAT_A8B8G8R8 || state.targetFormat[index] == FORMAT_SRGB8_A8) && rgbaWriteMask != 0x0000000F) ||
1691 (((state.targetFormat[index] == FORMAT_X8B8G8R8 || state.targetFormat[index] == FORMAT_SRGB8_X8) && rgbaWriteMask != 0x00000007) &&
1692 ((state.targetFormat[index] == FORMAT_X8B8G8R8 || state.targetFormat[index] == FORMAT_SRGB8_X8) && rgbaWriteMask != 0x0000000F))); // FIXME: Need for masking when XBGR && Fh?
1693
1694 if(masked)
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001695 {
1696 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001697 c01 &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q[rgbaWriteMask][0]));
1698 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q[rgbaWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001699 c01 |= masked;
1700 }
1701
Nicolas Capens4f172c72016-01-13 08:34:30 -05001702 c01 &= *Pointer<Short4>(constants + OFFSET(Constants,maskD01Q) + xMask * 8);
1703 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001704 c01 |= value;
1705 *Pointer<Short4>(buffer) = c01;
1706
Nicolas Capens4f172c72016-01-13 08:34:30 -05001707 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001708 value = *Pointer<Short4>(buffer);
1709
Alexis Hetu049a1872016-04-25 16:59:58 -04001710 if(masked)
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001711 {
1712 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001713 c23 &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q[rgbaWriteMask][0]));
1714 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q[rgbaWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001715 c23 |= masked;
1716 }
1717
Nicolas Capens4f172c72016-01-13 08:34:30 -05001718 c23 &= *Pointer<Short4>(constants + OFFSET(Constants,maskD23Q) + xMask * 8);
1719 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001720 c23 |= value;
1721 *Pointer<Short4>(buffer) = c23;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001722 }
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001723 break;
Alexis Hetu143dfc72016-09-13 18:41:27 -04001724 case FORMAT_G8R8:
1725 if((rgbaWriteMask & 0x00000003) != 0x0)
1726 {
1727 Pointer<Byte> buffer = cBuffer + 2 * x;
1728 Int2 value;
1729 value = Insert(value, *Pointer<Int>(buffer), 0);
1730 Int pitch = *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
1731 value = Insert(value, *Pointer<Int>(buffer + pitch), 1);
1732
1733 Int2 packedCol = As<Int2>(current.x);
1734
1735 UInt2 mergedMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskW4Q) + xMask * 8);
1736 if((rgbaWriteMask & 0x3) != 0x3)
1737 {
1738 Int tmpMask = *Pointer<Int>(constants + OFFSET(Constants, maskB4Q[5 * (rgbaWriteMask & 0x3)][0]));
1739 UInt2 rgbaMask = As<UInt2>(Int2(tmpMask, tmpMask));
1740 mergedMask &= rgbaMask;
1741 }
1742
1743 packedCol = As<Int2>((As<UInt2>(packedCol) & mergedMask) | (As<UInt2>(value) & ~mergedMask));
1744
1745 *Pointer<UInt>(buffer) = As<UInt>(Extract(packedCol, 0));
1746 *Pointer<UInt>(buffer + pitch) = As<UInt>(Extract(packedCol, 1));
1747 }
1748 break;
Alexis Hetu0cff3cb2016-05-04 16:23:50 -04001749 case FORMAT_R8:
1750 if(rgbaWriteMask & 0x00000001)
1751 {
1752 Pointer<Byte> buffer = cBuffer + 1 * x;
1753 Short4 value;
Alexis Hetub14ed802016-06-15 13:06:50 -04001754 value = Insert(value, *Pointer<Short>(buffer), 0);
Alexis Hetu0cff3cb2016-05-04 16:23:50 -04001755 Int pitch = *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
Alexis Hetub14ed802016-06-15 13:06:50 -04001756 value = Insert(value, *Pointer<Short>(buffer + pitch), 1);
Alexis Hetu0cff3cb2016-05-04 16:23:50 -04001757
1758 current.x &= *Pointer<Short4>(constants + OFFSET(Constants, maskB4Q) + 8 * xMask);
1759 value &= *Pointer<Short4>(constants + OFFSET(Constants, invMaskB4Q) + 8 * xMask);
1760 current.x |= value;
1761
1762 *Pointer<Short>(buffer) = Extract(current.x, 0);
1763 *Pointer<Short>(buffer + pitch) = Extract(current.x, 1);
1764 }
1765 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001766 case FORMAT_A8:
1767 if(rgbaWriteMask & 0x00000008)
1768 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001769 Pointer<Byte> buffer = cBuffer + 1 * x;
1770 Short4 value;
Alexis Hetub14ed802016-06-15 13:06:50 -04001771 value = Insert(value, *Pointer<Short>(buffer), 0);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001772 Int pitch = *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
Alexis Hetub14ed802016-06-15 13:06:50 -04001773 value = Insert(value, *Pointer<Short>(buffer + pitch), 1);
John Bauman66b8ab22014-05-06 15:57:45 -04001774
Nicolas Capens4f172c72016-01-13 08:34:30 -05001775 current.w &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q) + 8 * xMask);
1776 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q) + 8 * xMask);
John Bauman66b8ab22014-05-06 15:57:45 -04001777 current.w |= value;
1778
1779 *Pointer<Short>(buffer) = Extract(current.w, 0);
1780 *Pointer<Short>(buffer + pitch) = Extract(current.w, 1);
1781 }
1782 break;
John Bauman89401822014-05-06 15:04:28 -04001783 case FORMAT_G16R16:
John Bauman89401822014-05-06 15:04:28 -04001784 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001785 Pointer<Byte> buffer = cBuffer + 4 * x;
John Bauman89401822014-05-06 15:04:28 -04001786
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001787 Short4 value = *Pointer<Short4>(buffer);
John Bauman89401822014-05-06 15:04:28 -04001788
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001789 if((rgbaWriteMask & 0x00000003) != 0x00000003)
John Bauman89401822014-05-06 15:04:28 -04001790 {
1791 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001792 current.x &= *Pointer<Short4>(constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
Nicolas Capens7d9bdcb2015-05-26 02:09:27 -04001793 masked &= *Pointer<Short4>(constants + OFFSET(Constants,maskW01Q[~rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04001794 current.x |= masked;
John Bauman89401822014-05-06 15:04:28 -04001795 }
1796
Nicolas Capens4f172c72016-01-13 08:34:30 -05001797 current.x &= *Pointer<Short4>(constants + OFFSET(Constants,maskD01Q) + xMask * 8);
1798 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04001799 current.x |= value;
1800 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04001801
Nicolas Capens4f172c72016-01-13 08:34:30 -05001802 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04001803
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001804 value = *Pointer<Short4>(buffer);
1805
1806 if((rgbaWriteMask & 0x00000003) != 0x00000003)
John Bauman89401822014-05-06 15:04:28 -04001807 {
1808 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001809 current.y &= *Pointer<Short4>(constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
Nicolas Capens7d9bdcb2015-05-26 02:09:27 -04001810 masked &= *Pointer<Short4>(constants + OFFSET(Constants,maskW01Q[~rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04001811 current.y |= masked;
John Bauman89401822014-05-06 15:04:28 -04001812 }
1813
Nicolas Capens4f172c72016-01-13 08:34:30 -05001814 current.y &= *Pointer<Short4>(constants + OFFSET(Constants,maskD23Q) + xMask * 8);
1815 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04001816 current.y |= value;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001817 *Pointer<Short4>(buffer) = current.y;
John Bauman89401822014-05-06 15:04:28 -04001818 }
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001819 break;
1820 case FORMAT_A16B16G16R16:
John Bauman89401822014-05-06 15:04:28 -04001821 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001822 Pointer<Byte> buffer = cBuffer + 8 * x;
John Bauman89401822014-05-06 15:04:28 -04001823
John Bauman89401822014-05-06 15:04:28 -04001824 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001825 Short4 value = *Pointer<Short4>(buffer);
1826
1827 if(rgbaWriteMask != 0x0000000F)
1828 {
1829 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001830 current.x &= *Pointer<Short4>(constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
1831 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001832 current.x |= masked;
1833 }
1834
Nicolas Capens4f172c72016-01-13 08:34:30 -05001835 current.x &= *Pointer<Short4>(constants + OFFSET(Constants,maskQ0Q) + xMask * 8);
1836 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskQ0Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001837 current.x |= value;
1838 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04001839 }
1840
John Bauman89401822014-05-06 15:04:28 -04001841 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001842 Short4 value = *Pointer<Short4>(buffer + 8);
1843
1844 if(rgbaWriteMask != 0x0000000F)
1845 {
1846 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001847 current.y &= *Pointer<Short4>(constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
1848 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001849 current.y |= masked;
1850 }
1851
Nicolas Capens4f172c72016-01-13 08:34:30 -05001852 current.y &= *Pointer<Short4>(constants + OFFSET(Constants,maskQ1Q) + xMask * 8);
1853 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskQ1Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001854 current.y |= value;
1855 *Pointer<Short4>(buffer + 8) = current.y;
John Bauman89401822014-05-06 15:04:28 -04001856 }
1857
Nicolas Capens4f172c72016-01-13 08:34:30 -05001858 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001859
1860 {
1861 Short4 value = *Pointer<Short4>(buffer);
1862
1863 if(rgbaWriteMask != 0x0000000F)
1864 {
1865 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001866 current.z &= *Pointer<Short4>(constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
1867 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001868 current.z |= masked;
1869 }
1870
Nicolas Capens4f172c72016-01-13 08:34:30 -05001871 current.z &= *Pointer<Short4>(constants + OFFSET(Constants,maskQ2Q) + xMask * 8);
1872 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskQ2Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001873 current.z |= value;
1874 *Pointer<Short4>(buffer) = current.z;
1875 }
1876
1877 {
1878 Short4 value = *Pointer<Short4>(buffer + 8);
1879
1880 if(rgbaWriteMask != 0x0000000F)
1881 {
1882 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001883 current.w &= *Pointer<Short4>(constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
1884 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001885 current.w |= masked;
1886 }
1887
Nicolas Capens4f172c72016-01-13 08:34:30 -05001888 current.w &= *Pointer<Short4>(constants + OFFSET(Constants,maskQ3Q) + xMask * 8);
1889 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskQ3Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001890 current.w |= value;
1891 *Pointer<Short4>(buffer + 8) = current.w;
1892 }
John Bauman89401822014-05-06 15:04:28 -04001893 }
1894 break;
1895 default:
1896 ASSERT(false);
1897 }
1898 }
1899
Nicolas Capens96d4e092016-11-18 14:22:38 -05001900 void PixelRoutine::blendFactor(Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -04001901 {
1902 switch(blendFactorActive)
1903 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001904 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04001905 // Optimized
1906 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001907 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04001908 // Optimized
1909 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001910 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04001911 blendFactor.x = oC.x;
1912 blendFactor.y = oC.y;
1913 blendFactor.z = oC.z;
John Bauman89401822014-05-06 15:04:28 -04001914 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001915 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04001916 blendFactor.x = Float4(1.0f) - oC.x;
1917 blendFactor.y = Float4(1.0f) - oC.y;
1918 blendFactor.z = Float4(1.0f) - oC.z;
John Bauman89401822014-05-06 15:04:28 -04001919 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001920 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001921 blendFactor.x = pixel.x;
1922 blendFactor.y = pixel.y;
1923 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04001924 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001925 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001926 blendFactor.x = Float4(1.0f) - pixel.x;
1927 blendFactor.y = Float4(1.0f) - pixel.y;
1928 blendFactor.z = Float4(1.0f) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -04001929 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001930 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001931 blendFactor.x = oC.w;
1932 blendFactor.y = oC.w;
1933 blendFactor.z = oC.w;
John Bauman89401822014-05-06 15:04:28 -04001934 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001935 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001936 blendFactor.x = Float4(1.0f) - oC.w;
1937 blendFactor.y = Float4(1.0f) - oC.w;
1938 blendFactor.z = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04001939 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001940 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001941 blendFactor.x = pixel.w;
1942 blendFactor.y = pixel.w;
1943 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001944 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001945 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001946 blendFactor.x = Float4(1.0f) - pixel.w;
1947 blendFactor.y = Float4(1.0f) - pixel.w;
1948 blendFactor.z = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001949 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001950 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04001951 blendFactor.x = Float4(1.0f) - pixel.w;
1952 blendFactor.x = Min(blendFactor.x, oC.w);
1953 blendFactor.y = blendFactor.x;
1954 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -04001955 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001956 case BLEND_CONSTANT:
Nicolas Capens4f172c72016-01-13 08:34:30 -05001957 blendFactor.x = *Pointer<Float4>(data + OFFSET(DrawData,factor.blendConstant4F[0]));
1958 blendFactor.y = *Pointer<Float4>(data + OFFSET(DrawData,factor.blendConstant4F[1]));
1959 blendFactor.z = *Pointer<Float4>(data + OFFSET(DrawData,factor.blendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04001960 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001961 case BLEND_INVCONSTANT:
Nicolas Capens4f172c72016-01-13 08:34:30 -05001962 blendFactor.x = *Pointer<Float4>(data + OFFSET(DrawData,factor.invBlendConstant4F[0]));
1963 blendFactor.y = *Pointer<Float4>(data + OFFSET(DrawData,factor.invBlendConstant4F[1]));
1964 blendFactor.z = *Pointer<Float4>(data + OFFSET(DrawData,factor.invBlendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04001965 break;
1966 default:
1967 ASSERT(false);
1968 }
1969 }
1970
Nicolas Capens96d4e092016-11-18 14:22:38 -05001971 void PixelRoutine::blendFactorAlpha(Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -04001972 {
1973 switch(blendFactorAlphaActive)
1974 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001975 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04001976 // Optimized
1977 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001978 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04001979 // Optimized
1980 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001981 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04001982 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04001983 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001984 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04001985 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04001986 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001987 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001988 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001989 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001990 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001991 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001992 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001993 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001994 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04001995 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001996 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001997 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04001998 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001999 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002000 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002001 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002002 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002003 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002004 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002005 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04002006 blendFactor.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04002007 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002008 case BLEND_CONSTANT:
Nicolas Capens4f172c72016-01-13 08:34:30 -05002009 blendFactor.w = *Pointer<Float4>(data + OFFSET(DrawData,factor.blendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04002010 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002011 case BLEND_INVCONSTANT:
Nicolas Capens4f172c72016-01-13 08:34:30 -05002012 blendFactor.w = *Pointer<Float4>(data + OFFSET(DrawData,factor.invBlendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04002013 break;
2014 default:
2015 ASSERT(false);
2016 }
2017 }
2018
Nicolas Capens4f172c72016-01-13 08:34:30 -05002019 void PixelRoutine::alphaBlend(int index, Pointer<Byte> &cBuffer, Vector4f &oC, Int &x)
John Bauman89401822014-05-06 15:04:28 -04002020 {
2021 if(!state.alphaBlendActive)
2022 {
2023 return;
2024 }
2025
2026 Pointer<Byte> buffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002027 Vector4f pixel;
John Bauman89401822014-05-06 15:04:28 -04002028
Alexis Hetu96517182015-04-15 10:30:23 -04002029 Vector4s color;
John Bauman89401822014-05-06 15:04:28 -04002030 Short4 c01;
2031 Short4 c23;
2032
Alexis Hetu1abb6382016-02-08 11:21:16 -05002033 Float4 one;
Alexis Hetu7208e932016-06-02 11:19:24 -04002034 if(Surface::isFloatFormat(state.targetFormat[index]))
John Bauman89401822014-05-06 15:04:28 -04002035 {
Alexis Hetu1abb6382016-02-08 11:21:16 -05002036 one = Float4(1.0f);
Alexis Hetu7208e932016-06-02 11:19:24 -04002037 }
2038 else if(Surface::isNonNormalizedInteger(state.targetFormat[index]))
2039 {
2040 one = As<Float4>(Surface::isUnsignedComponent(state.targetFormat[index], 0) ? Int4(0xFFFFFFFF) : Int4(0x7FFFFFFF));
Alexis Hetu1abb6382016-02-08 11:21:16 -05002041 }
2042
2043 switch(state.targetFormat[index])
2044 {
2045 case FORMAT_R32I:
2046 case FORMAT_R32UI:
John Bauman89401822014-05-06 15:04:28 -04002047 case FORMAT_R32F:
2048 buffer = cBuffer;
2049 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04002050 pixel.x.x = *Pointer<Float>(buffer + 4 * x + 0);
2051 pixel.x.y = *Pointer<Float>(buffer + 4 * x + 4);
Nicolas Capens4f172c72016-01-13 08:34:30 -05002052 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04002053 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04002054 pixel.x.z = *Pointer<Float>(buffer + 4 * x + 0);
2055 pixel.x.w = *Pointer<Float>(buffer + 4 * x + 4);
Alexis Hetu1abb6382016-02-08 11:21:16 -05002056 pixel.y = pixel.z = pixel.w = one;
John Bauman89401822014-05-06 15:04:28 -04002057 break;
Alexis Hetu1abb6382016-02-08 11:21:16 -05002058 case FORMAT_G32R32I:
2059 case FORMAT_G32R32UI:
John Bauman89401822014-05-06 15:04:28 -04002060 case FORMAT_G32R32F:
2061 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002062 pixel.x = *Pointer<Float4>(buffer + 8 * x, 16);
Nicolas Capens4f172c72016-01-13 08:34:30 -05002063 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002064 pixel.y = *Pointer<Float4>(buffer + 8 * x, 16);
2065 pixel.z = pixel.x;
2066 pixel.x = ShuffleLowHigh(pixel.x, pixel.y, 0x88);
2067 pixel.z = ShuffleLowHigh(pixel.z, pixel.y, 0xDD);
2068 pixel.y = pixel.z;
Alexis Hetu1abb6382016-02-08 11:21:16 -05002069 pixel.z = pixel.w = one;
John Bauman89401822014-05-06 15:04:28 -04002070 break;
Alexis Hetudbd1a8e2016-04-13 11:40:30 -04002071 case FORMAT_X32B32G32R32F:
John Bauman89401822014-05-06 15:04:28 -04002072 case FORMAT_A32B32G32R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002073 case FORMAT_A32B32G32R32I:
2074 case FORMAT_A32B32G32R32UI:
John Bauman89401822014-05-06 15:04:28 -04002075 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002076 pixel.x = *Pointer<Float4>(buffer + 16 * x, 16);
2077 pixel.y = *Pointer<Float4>(buffer + 16 * x + 16, 16);
Nicolas Capens4f172c72016-01-13 08:34:30 -05002078 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002079 pixel.z = *Pointer<Float4>(buffer + 16 * x, 16);
2080 pixel.w = *Pointer<Float4>(buffer + 16 * x + 16, 16);
2081 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
Alexis Hetudbd1a8e2016-04-13 11:40:30 -04002082 if(state.targetFormat[index] == FORMAT_X32B32G32R32F)
2083 {
2084 pixel.w = Float4(1.0f);
2085 }
John Bauman89401822014-05-06 15:04:28 -04002086 break;
2087 default:
2088 ASSERT(false);
2089 }
2090
Alexis Hetu049a1872016-04-25 16:59:58 -04002091 if((postBlendSRGB && state.writeSRGB) || isSRGB(index))
John Bauman89401822014-05-06 15:04:28 -04002092 {
John Bauman19bac1e2014-05-06 15:23:49 -04002093 sRGBtoLinear(pixel.x);
2094 sRGBtoLinear(pixel.y);
2095 sRGBtoLinear(pixel.z);
John Bauman89401822014-05-06 15:04:28 -04002096 }
2097
2098 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
John Bauman19bac1e2014-05-06 15:23:49 -04002099 Vector4f sourceFactor;
2100 Vector4f destFactor;
John Bauman89401822014-05-06 15:04:28 -04002101
Nicolas Capens4f172c72016-01-13 08:34:30 -05002102 blendFactor(sourceFactor, oC, pixel, state.sourceBlendFactor);
2103 blendFactor(destFactor, oC, pixel, state.destBlendFactor);
John Bauman89401822014-05-06 15:04:28 -04002104
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002105 if(state.sourceBlendFactor != BLEND_ONE && state.sourceBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002106 {
John Bauman19bac1e2014-05-06 15:23:49 -04002107 oC.x *= sourceFactor.x;
2108 oC.y *= sourceFactor.y;
2109 oC.z *= sourceFactor.z;
John Bauman89401822014-05-06 15:04:28 -04002110 }
Nicolas Capens05b3d662016-02-25 23:58:33 -05002111
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002112 if(state.destBlendFactor != BLEND_ONE && state.destBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002113 {
John Bauman19bac1e2014-05-06 15:23:49 -04002114 pixel.x *= destFactor.x;
2115 pixel.y *= destFactor.y;
2116 pixel.z *= destFactor.z;
John Bauman89401822014-05-06 15:04:28 -04002117 }
2118
2119 switch(state.blendOperation)
2120 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002121 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002122 oC.x += pixel.x;
2123 oC.y += pixel.y;
2124 oC.z += pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002125 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002126 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002127 oC.x -= pixel.x;
2128 oC.y -= pixel.y;
2129 oC.z -= pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002130 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002131 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002132 oC.x = pixel.x - oC.x;
2133 oC.y = pixel.y - oC.y;
2134 oC.z = pixel.z - oC.z;
John Bauman89401822014-05-06 15:04:28 -04002135 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002136 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002137 oC.x = Min(oC.x, pixel.x);
2138 oC.y = Min(oC.y, pixel.y);
2139 oC.z = Min(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04002140 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002141 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002142 oC.x = Max(oC.x, pixel.x);
2143 oC.y = Max(oC.y, pixel.y);
2144 oC.z = Max(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04002145 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002146 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04002147 // No operation
2148 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002149 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002150 oC.x = pixel.x;
2151 oC.y = pixel.y;
2152 oC.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002153 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002154 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002155 oC.x = Float4(0.0f);
2156 oC.y = Float4(0.0f);
2157 oC.z = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04002158 break;
2159 default:
2160 ASSERT(false);
2161 }
2162
Nicolas Capens4f172c72016-01-13 08:34:30 -05002163 blendFactorAlpha(sourceFactor, oC, pixel, state.sourceBlendFactorAlpha);
2164 blendFactorAlpha(destFactor, oC, pixel, state.destBlendFactorAlpha);
John Bauman89401822014-05-06 15:04:28 -04002165
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002166 if(state.sourceBlendFactorAlpha != BLEND_ONE && state.sourceBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002167 {
John Bauman19bac1e2014-05-06 15:23:49 -04002168 oC.w *= sourceFactor.w;
John Bauman89401822014-05-06 15:04:28 -04002169 }
Nicolas Capens05b3d662016-02-25 23:58:33 -05002170
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002171 if(state.destBlendFactorAlpha != BLEND_ONE && state.destBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002172 {
John Bauman19bac1e2014-05-06 15:23:49 -04002173 pixel.w *= destFactor.w;
John Bauman89401822014-05-06 15:04:28 -04002174 }
2175
2176 switch(state.blendOperationAlpha)
2177 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002178 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002179 oC.w += pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002180 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002181 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002182 oC.w -= pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002183 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002184 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002185 pixel.w -= oC.w;
2186 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002187 break;
Nicolas Capens05b3d662016-02-25 23:58:33 -05002188 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002189 oC.w = Min(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04002190 break;
Nicolas Capens05b3d662016-02-25 23:58:33 -05002191 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002192 oC.w = Max(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04002193 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002194 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04002195 // No operation
2196 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002197 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002198 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002199 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002200 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002201 oC.w = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04002202 break;
2203 default:
2204 ASSERT(false);
2205 }
2206 }
2207
Nicolas Capens4f172c72016-01-13 08:34:30 -05002208 void PixelRoutine::writeColor(int index, Pointer<Byte> &cBuffer, Int &x, Vector4f &oC, Int &sMask, Int &zMask, Int &cMask)
John Bauman89401822014-05-06 15:04:28 -04002209 {
John Bauman89401822014-05-06 15:04:28 -04002210 switch(state.targetFormat[index])
2211 {
John Bauman89401822014-05-06 15:04:28 -04002212 case FORMAT_R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002213 case FORMAT_R32I:
2214 case FORMAT_R32UI:
Alexis Hetubd7117d2016-06-02 10:35:59 -04002215 case FORMAT_R16I:
2216 case FORMAT_R16UI:
2217 case FORMAT_R8I:
2218 case FORMAT_R8UI:
John Bauman89401822014-05-06 15:04:28 -04002219 break;
2220 case FORMAT_G32R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002221 case FORMAT_G32R32I:
2222 case FORMAT_G32R32UI:
Alexis Hetubd7117d2016-06-02 10:35:59 -04002223 case FORMAT_G16R16I:
2224 case FORMAT_G16R16UI:
2225 case FORMAT_G8R8I:
2226 case FORMAT_G8R8UI:
John Bauman19bac1e2014-05-06 15:23:49 -04002227 oC.z = oC.x;
2228 oC.x = UnpackLow(oC.x, oC.y);
2229 oC.z = UnpackHigh(oC.z, oC.y);
2230 oC.y = oC.z;
John Bauman89401822014-05-06 15:04:28 -04002231 break;
Alexis Hetudbd1a8e2016-04-13 11:40:30 -04002232 case FORMAT_X32B32G32R32F:
John Bauman89401822014-05-06 15:04:28 -04002233 case FORMAT_A32B32G32R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002234 case FORMAT_A32B32G32R32I:
2235 case FORMAT_A32B32G32R32UI:
Alexis Hetubd7117d2016-06-02 10:35:59 -04002236 case FORMAT_A16B16G16R16I:
2237 case FORMAT_A16B16G16R16UI:
2238 case FORMAT_A8B8G8R8I:
2239 case FORMAT_A8B8G8R8UI:
John Bauman19bac1e2014-05-06 15:23:49 -04002240 transpose4x4(oC.x, oC.y, oC.z, oC.w);
John Bauman89401822014-05-06 15:04:28 -04002241 break;
2242 default:
2243 ASSERT(false);
2244 }
2245
2246 int rgbaWriteMask = state.colorWriteActive(index);
2247
2248 Int xMask; // Combination of all masks
2249
2250 if(state.depthTestActive)
2251 {
2252 xMask = zMask;
2253 }
2254 else
2255 {
2256 xMask = cMask;
2257 }
2258
2259 if(state.stencilActive)
2260 {
2261 xMask &= sMask;
2262 }
2263
2264 Pointer<Byte> buffer;
2265 Float4 value;
2266
2267 switch(state.targetFormat[index])
2268 {
2269 case FORMAT_R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002270 case FORMAT_R32I:
2271 case FORMAT_R32UI:
John Bauman89401822014-05-06 15:04:28 -04002272 if(rgbaWriteMask & 0x00000001)
2273 {
2274 buffer = cBuffer + 4 * x;
2275
2276 // FIXME: movlps
2277 value.x = *Pointer<Float>(buffer + 0);
2278 value.y = *Pointer<Float>(buffer + 4);
2279
Nicolas Capens4f172c72016-01-13 08:34:30 -05002280 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04002281
2282 // FIXME: movhps
2283 value.z = *Pointer<Float>(buffer + 0);
2284 value.w = *Pointer<Float>(buffer + 4);
2285
Nicolas Capens4f172c72016-01-13 08:34:30 -05002286 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(constants + OFFSET(Constants,maskD4X) + xMask * 16, 16));
2287 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD4X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002288 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
John Bauman89401822014-05-06 15:04:28 -04002289
2290 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04002291 *Pointer<Float>(buffer + 0) = oC.x.z;
2292 *Pointer<Float>(buffer + 4) = oC.x.w;
John Bauman89401822014-05-06 15:04:28 -04002293
Nicolas Capens4f172c72016-01-13 08:34:30 -05002294 buffer -= *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04002295
2296 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04002297 *Pointer<Float>(buffer + 0) = oC.x.x;
2298 *Pointer<Float>(buffer + 4) = oC.x.y;
John Bauman89401822014-05-06 15:04:28 -04002299 }
2300 break;
Alexis Hetubd7117d2016-06-02 10:35:59 -04002301 case FORMAT_R16I:
2302 case FORMAT_R16UI:
2303 if(rgbaWriteMask & 0x00000001)
2304 {
2305 buffer = cBuffer + 2 * x;
2306
2307 UShort4 xyzw;
2308 xyzw = As<UShort4>(Insert(As<Int2>(xyzw), *Pointer<Int>(buffer), 0));
2309
2310 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2311
2312 xyzw = As<UShort4>(Insert(As<Int2>(xyzw), *Pointer<Int>(buffer), 1));
2313 value = As<Float4>(Int4(xyzw));
2314
2315 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(constants + OFFSET(Constants, maskD4X) + xMask * 16, 16));
2316 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants, invMaskD4X) + xMask * 16, 16));
2317 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
2318
2319 if(state.targetFormat[index] == FORMAT_R16I)
2320 {
2321 Float component = oC.x.z;
2322 *Pointer<Short>(buffer + 0) = Short(As<Int>(component));
2323 component = oC.x.w;
2324 *Pointer<Short>(buffer + 2) = Short(As<Int>(component));
2325
2326 buffer -= *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2327
2328 component = oC.x.x;
2329 *Pointer<Short>(buffer + 0) = Short(As<Int>(component));
2330 component = oC.x.y;
2331 *Pointer<Short>(buffer + 2) = Short(As<Int>(component));
2332 }
2333 else // FORMAT_R16UI
2334 {
2335 Float component = oC.x.z;
2336 *Pointer<UShort>(buffer + 0) = UShort(As<Int>(component));
2337 component = oC.x.w;
2338 *Pointer<UShort>(buffer + 2) = UShort(As<Int>(component));
2339
2340 buffer -= *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2341
2342 component = oC.x.x;
2343 *Pointer<UShort>(buffer + 0) = UShort(As<Int>(component));
2344 component = oC.x.y;
2345 *Pointer<UShort>(buffer + 2) = UShort(As<Int>(component));
2346 }
2347 }
2348 break;
2349 case FORMAT_R8I:
2350 case FORMAT_R8UI:
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002351 if(rgbaWriteMask & 0x00000001)
2352 {
2353 buffer = cBuffer + x;
2354
2355 UInt xyzw, packedCol;
2356
Alexis Hetu827d07a2016-09-15 17:54:05 -04002357 xyzw = UInt(*Pointer<UShort>(buffer)) & 0xFFFF;
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002358 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
Alexis Hetu827d07a2016-09-15 17:54:05 -04002359 xyzw |= UInt(*Pointer<UShort>(buffer)) << 16;
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002360
2361 Short4 tmpCol = Short4(As<Int4>(oC.x));
2362 if(state.targetFormat[index] == FORMAT_R8I)
2363 {
Nicolas Capens33438a62017-09-27 11:47:35 -04002364 tmpCol = As<Short4>(PackSigned(tmpCol, tmpCol));
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002365 }
2366 else
2367 {
Nicolas Capens33438a62017-09-27 11:47:35 -04002368 tmpCol = As<Short4>(PackUnsigned(tmpCol, tmpCol));
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002369 }
2370 packedCol = Extract(As<Int2>(tmpCol), 0);
2371
2372 packedCol = (packedCol & *Pointer<UInt>(constants + OFFSET(Constants, maskB4Q) + 8 * xMask)) |
2373 (xyzw & *Pointer<UInt>(constants + OFFSET(Constants, invMaskB4Q) + 8 * xMask));
2374
2375 *Pointer<UShort>(buffer) = UShort(packedCol >> 16);
2376 buffer -= *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2377 *Pointer<UShort>(buffer) = UShort(packedCol);
2378 }
Alexis Hetubd7117d2016-06-02 10:35:59 -04002379 break;
John Bauman89401822014-05-06 15:04:28 -04002380 case FORMAT_G32R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002381 case FORMAT_G32R32I:
2382 case FORMAT_G32R32UI:
John Bauman89401822014-05-06 15:04:28 -04002383 buffer = cBuffer + 8 * x;
2384
2385 value = *Pointer<Float4>(buffer);
2386
2387 if((rgbaWriteMask & 0x00000003) != 0x00000003)
2388 {
2389 Float4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05002390 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(constants + OFFSET(Constants,maskD01X[rgbaWriteMask & 0x3][0])));
Nicolas Capens7d9bdcb2015-05-26 02:09:27 -04002391 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(constants + OFFSET(Constants,maskD01X[~rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002392 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002393 }
2394
Nicolas Capens4f172c72016-01-13 08:34:30 -05002395 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(constants + OFFSET(Constants,maskQ01X) + xMask * 16, 16));
2396 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskQ01X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002397 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
2398 *Pointer<Float4>(buffer) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04002399
Nicolas Capens4f172c72016-01-13 08:34:30 -05002400 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04002401
2402 value = *Pointer<Float4>(buffer);
2403
2404 if((rgbaWriteMask & 0x00000003) != 0x00000003)
2405 {
2406 Float4 masked;
2407
2408 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05002409 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(constants + OFFSET(Constants,maskD01X[rgbaWriteMask & 0x3][0])));
Nicolas Capens7d9bdcb2015-05-26 02:09:27 -04002410 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(constants + OFFSET(Constants,maskD01X[~rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002411 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002412 }
2413
Nicolas Capens4f172c72016-01-13 08:34:30 -05002414 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(constants + OFFSET(Constants,maskQ23X) + xMask * 16, 16));
2415 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskQ23X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002416 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
2417 *Pointer<Float4>(buffer) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04002418 break;
Alexis Hetubd7117d2016-06-02 10:35:59 -04002419 case FORMAT_G16R16I:
2420 case FORMAT_G16R16UI:
Meng-Lin Wu1978ff72016-06-20 15:08:42 -04002421 if((rgbaWriteMask & 0x00000003) != 0x0)
2422 {
2423 buffer = cBuffer + 4 * x;
2424
2425 UInt2 rgbaMask;
2426 UShort4 packedCol = UShort4(As<Int4>(oC.x));
2427 UShort4 value = *Pointer<UShort4>(buffer);
2428 UInt2 mergedMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskD01Q) + xMask * 8);
2429 if((rgbaWriteMask & 0x3) != 0x3)
2430 {
2431 Int tmpMask = *Pointer<Int>(constants + OFFSET(Constants, maskW4Q[rgbaWriteMask & 0x3][0]));
2432 rgbaMask = As<UInt2>(Int2(tmpMask, tmpMask));
2433 mergedMask &= rgbaMask;
2434 }
2435 *Pointer<UInt2>(buffer) = (As<UInt2>(packedCol) & mergedMask) | (As<UInt2>(value) & ~mergedMask);
2436
2437 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2438
2439 packedCol = UShort4(As<Int4>(oC.y));
2440 value = *Pointer<UShort4>(buffer);
2441 mergedMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskD23Q) + xMask * 8);
2442 if((rgbaWriteMask & 0x3) != 0x3)
2443 {
2444 mergedMask &= rgbaMask;
2445 }
2446 *Pointer<UInt2>(buffer) = (As<UInt2>(packedCol) & mergedMask) | (As<UInt2>(value) & ~mergedMask);
2447 }
Alexis Hetubd7117d2016-06-02 10:35:59 -04002448 break;
2449 case FORMAT_G8R8I:
2450 case FORMAT_G8R8UI:
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002451 if((rgbaWriteMask & 0x00000003) != 0x0)
2452 {
2453 buffer = cBuffer + 2 * x;
2454
2455 Int2 xyzw, packedCol;
2456
2457 xyzw = Insert(xyzw, *Pointer<Int>(buffer), 0);
2458 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2459 xyzw = Insert(xyzw, *Pointer<Int>(buffer), 1);
2460
2461 if(state.targetFormat[index] == FORMAT_G8R8I)
2462 {
Nicolas Capens33438a62017-09-27 11:47:35 -04002463 packedCol = As<Int2>(PackSigned(Short4(As<Int4>(oC.x)), Short4(As<Int4>(oC.y))));
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002464 }
2465 else
2466 {
Nicolas Capens33438a62017-09-27 11:47:35 -04002467 packedCol = As<Int2>(PackUnsigned(Short4(As<Int4>(oC.x)), Short4(As<Int4>(oC.y))));
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002468 }
2469
2470 UInt2 mergedMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskW4Q) + xMask * 8);
2471 if((rgbaWriteMask & 0x3) != 0x3)
2472 {
2473 Int tmpMask = *Pointer<Int>(constants + OFFSET(Constants, maskB4Q[5 * (rgbaWriteMask & 0x3)][0]));
2474 UInt2 rgbaMask = As<UInt2>(Int2(tmpMask, tmpMask));
2475 mergedMask &= rgbaMask;
2476 }
2477
2478 packedCol = As<Int2>((As<UInt2>(packedCol) & mergedMask) | (As<UInt2>(xyzw) & ~mergedMask));
2479
2480 *Pointer<UInt>(buffer) = As<UInt>(Extract(packedCol, 1));
2481 buffer -= *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2482 *Pointer<UInt>(buffer) = As<UInt>(Extract(packedCol, 0));
2483 }
Alexis Hetubd7117d2016-06-02 10:35:59 -04002484 break;
Alexis Hetudbd1a8e2016-04-13 11:40:30 -04002485 case FORMAT_X32B32G32R32F:
John Bauman89401822014-05-06 15:04:28 -04002486 case FORMAT_A32B32G32R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002487 case FORMAT_A32B32G32R32I:
2488 case FORMAT_A32B32G32R32UI:
John Bauman89401822014-05-06 15:04:28 -04002489 buffer = cBuffer + 16 * x;
2490
2491 {
2492 value = *Pointer<Float4>(buffer, 16);
2493
2494 if(rgbaWriteMask != 0x0000000F)
2495 {
2496 Float4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05002497 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
2498 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002499 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002500 }
Nicolas Capens05b3d662016-02-25 23:58:33 -05002501
Nicolas Capens4f172c72016-01-13 08:34:30 -05002502 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(constants + OFFSET(Constants,maskX0X) + xMask * 16, 16));
2503 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskX0X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002504 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
2505 *Pointer<Float4>(buffer, 16) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04002506 }
2507
2508 {
2509 value = *Pointer<Float4>(buffer + 16, 16);
2510
2511 if(rgbaWriteMask != 0x0000000F)
Nicolas Capens05b3d662016-02-25 23:58:33 -05002512 {
John Bauman89401822014-05-06 15:04:28 -04002513 Float4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05002514 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
2515 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002516 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002517 }
2518
Nicolas Capens4f172c72016-01-13 08:34:30 -05002519 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(constants + OFFSET(Constants,maskX1X) + xMask * 16, 16));
2520 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskX1X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002521 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
2522 *Pointer<Float4>(buffer + 16, 16) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04002523 }
2524
Nicolas Capens4f172c72016-01-13 08:34:30 -05002525 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04002526
2527 {
2528 value = *Pointer<Float4>(buffer, 16);
2529
2530 if(rgbaWriteMask != 0x0000000F)
2531 {
2532 Float4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05002533 oC.z = As<Float4>(As<Int4>(oC.z) & *Pointer<Int4>(constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
2534 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002535 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002536 }
2537
Nicolas Capens4f172c72016-01-13 08:34:30 -05002538 oC.z = As<Float4>(As<Int4>(oC.z) & *Pointer<Int4>(constants + OFFSET(Constants,maskX2X) + xMask * 16, 16));
2539 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskX2X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002540 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(value));
2541 *Pointer<Float4>(buffer, 16) = oC.z;
John Bauman89401822014-05-06 15:04:28 -04002542 }
2543
2544 {
Nicolas Capens400667e2017-03-29 14:40:14 -04002545 value = *Pointer<Float4>(buffer + 16, 16);
John Bauman89401822014-05-06 15:04:28 -04002546
2547 if(rgbaWriteMask != 0x0000000F)
2548 {
2549 Float4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05002550 oC.w = As<Float4>(As<Int4>(oC.w) & *Pointer<Int4>(constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
2551 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002552 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002553 }
2554
Nicolas Capens4f172c72016-01-13 08:34:30 -05002555 oC.w = As<Float4>(As<Int4>(oC.w) & *Pointer<Int4>(constants + OFFSET(Constants,maskX3X) + xMask * 16, 16));
2556 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskX3X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002557 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(value));
2558 *Pointer<Float4>(buffer + 16, 16) = oC.w;
John Bauman89401822014-05-06 15:04:28 -04002559 }
2560 break;
Alexis Hetubd7117d2016-06-02 10:35:59 -04002561 case FORMAT_A16B16G16R16I:
2562 case FORMAT_A16B16G16R16UI:
Meng-Lin Wu1978ff72016-06-20 15:08:42 -04002563 if((rgbaWriteMask & 0x0000000F) != 0x0)
2564 {
2565 buffer = cBuffer + 8 * x;
2566
2567 UInt4 rgbaMask;
2568 UShort8 value = *Pointer<UShort8>(buffer);
2569 UShort8 packedCol = UShort8(UShort4(As<Int4>(oC.x)), UShort4(As<Int4>(oC.y)));
2570 UInt4 mergedMask = *Pointer<UInt4>(constants + OFFSET(Constants, maskQ01X) + xMask * 16);
2571 if((rgbaWriteMask & 0xF) != 0xF)
2572 {
2573 UInt2 tmpMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskW4Q[rgbaWriteMask][0]));
2574 rgbaMask = UInt4(tmpMask, tmpMask);
2575 mergedMask &= rgbaMask;
2576 }
2577 *Pointer<UInt4>(buffer) = (As<UInt4>(packedCol) & mergedMask) | (As<UInt4>(value) & ~mergedMask);
2578
2579 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2580
2581 value = *Pointer<UShort8>(buffer);
2582 packedCol = UShort8(UShort4(As<Int4>(oC.z)), UShort4(As<Int4>(oC.w)));
2583 mergedMask = *Pointer<UInt4>(constants + OFFSET(Constants, maskQ23X) + xMask * 16);
2584 if((rgbaWriteMask & 0xF) != 0xF)
2585 {
2586 mergedMask &= rgbaMask;
2587 }
2588 *Pointer<UInt4>(buffer) = (As<UInt4>(packedCol) & mergedMask) | (As<UInt4>(value) & ~mergedMask);
2589 }
Alexis Hetubd7117d2016-06-02 10:35:59 -04002590 break;
2591 case FORMAT_A8B8G8R8I:
2592 case FORMAT_A8B8G8R8UI:
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002593 if((rgbaWriteMask & 0x0000000F) != 0x0)
2594 {
2595 UInt2 value, packedCol, mergedMask;
2596
2597 buffer = cBuffer + 4 * x;
2598
2599 if(state.targetFormat[index] == FORMAT_A8B8G8R8I)
2600 {
Nicolas Capens33438a62017-09-27 11:47:35 -04002601 packedCol = As<UInt2>(PackSigned(Short4(As<Int4>(oC.x)), Short4(As<Int4>(oC.y))));
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002602 }
2603 else
2604 {
Nicolas Capens33438a62017-09-27 11:47:35 -04002605 packedCol = As<UInt2>(PackUnsigned(Short4(As<Int4>(oC.x)), Short4(As<Int4>(oC.y))));
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002606 }
2607 value = *Pointer<UInt2>(buffer, 16);
2608 mergedMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskD01Q) + xMask * 8);
2609 if(rgbaWriteMask != 0xF)
2610 {
2611 mergedMask &= *Pointer<UInt2>(constants + OFFSET(Constants, maskB4Q[rgbaWriteMask][0]));
2612 }
2613 *Pointer<UInt2>(buffer) = (packedCol & mergedMask) | (value & ~mergedMask);
2614
2615 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2616
2617 if(state.targetFormat[index] == FORMAT_A8B8G8R8I)
2618 {
Nicolas Capens33438a62017-09-27 11:47:35 -04002619 packedCol = As<UInt2>(PackSigned(Short4(As<Int4>(oC.z)), Short4(As<Int4>(oC.w))));
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002620 }
2621 else
2622 {
Nicolas Capens33438a62017-09-27 11:47:35 -04002623 packedCol = As<UInt2>(PackUnsigned(Short4(As<Int4>(oC.z)), Short4(As<Int4>(oC.w))));
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002624 }
2625 value = *Pointer<UInt2>(buffer, 16);
2626 mergedMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskD23Q) + xMask * 8);
2627 if(rgbaWriteMask != 0xF)
2628 {
2629 mergedMask &= *Pointer<UInt2>(constants + OFFSET(Constants, maskB4Q[rgbaWriteMask][0]));
2630 }
2631 *Pointer<UInt2>(buffer) = (packedCol & mergedMask) | (value & ~mergedMask);
2632 }
Alexis Hetubd7117d2016-06-02 10:35:59 -04002633 break;
John Bauman89401822014-05-06 15:04:28 -04002634 default:
2635 ASSERT(false);
2636 }
2637 }
2638
John Bauman89401822014-05-06 15:04:28 -04002639 UShort4 PixelRoutine::convertFixed16(Float4 &cf, bool saturate)
2640 {
John Bauman19bac1e2014-05-06 15:23:49 -04002641 return UShort4(cf * Float4(0xFFFF), saturate);
John Bauman89401822014-05-06 15:04:28 -04002642 }
2643
Nicolas Capens4f172c72016-01-13 08:34:30 -05002644 void PixelRoutine::sRGBtoLinear16_12_16(Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04002645 {
John Bauman19bac1e2014-05-06 15:23:49 -04002646 c.x = As<UShort4>(c.x) >> 4;
2647 c.y = As<UShort4>(c.y) >> 4;
2648 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04002649
Nicolas Capens4f172c72016-01-13 08:34:30 -05002650 sRGBtoLinear12_16(c);
John Bauman89401822014-05-06 15:04:28 -04002651 }
2652
Nicolas Capens4f172c72016-01-13 08:34:30 -05002653 void PixelRoutine::sRGBtoLinear12_16(Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04002654 {
Nicolas Capens4f172c72016-01-13 08:34:30 -05002655 Pointer<Byte> LUT = constants + OFFSET(Constants,sRGBtoLinear12_16);
John Bauman89401822014-05-06 15:04:28 -04002656
John Bauman19bac1e2014-05-06 15:23:49 -04002657 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
2658 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
2659 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
2660 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002661
John Bauman19bac1e2014-05-06 15:23:49 -04002662 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
2663 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
2664 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
2665 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002666
John Bauman19bac1e2014-05-06 15:23:49 -04002667 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
2668 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
2669 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
2670 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002671 }
2672
Nicolas Capens4f172c72016-01-13 08:34:30 -05002673 void PixelRoutine::linearToSRGB16_12_16(Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04002674 {
John Bauman19bac1e2014-05-06 15:23:49 -04002675 c.x = As<UShort4>(c.x) >> 4;
2676 c.y = As<UShort4>(c.y) >> 4;
2677 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04002678
Nicolas Capens4f172c72016-01-13 08:34:30 -05002679 linearToSRGB12_16(c);
John Bauman89401822014-05-06 15:04:28 -04002680 }
2681
Nicolas Capens4f172c72016-01-13 08:34:30 -05002682 void PixelRoutine::linearToSRGB12_16(Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04002683 {
Nicolas Capens4f172c72016-01-13 08:34:30 -05002684 Pointer<Byte> LUT = constants + OFFSET(Constants,linearToSRGB12_16);
John Bauman89401822014-05-06 15:04:28 -04002685
John Bauman19bac1e2014-05-06 15:23:49 -04002686 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
2687 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
2688 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
2689 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002690
John Bauman19bac1e2014-05-06 15:23:49 -04002691 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
2692 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
2693 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
2694 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002695
John Bauman19bac1e2014-05-06 15:23:49 -04002696 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
2697 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
2698 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
2699 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002700 }
2701
John Bauman89401822014-05-06 15:04:28 -04002702 Float4 PixelRoutine::sRGBtoLinear(const Float4 &x) // Approximates x^2.2
2703 {
2704 Float4 linear = x * x;
2705 linear = linear * Float4(0.73f) + linear * x * Float4(0.27f);
2706
2707 return Min(Max(linear, Float4(0.0f)), Float4(1.0f));
2708 }
2709
John Bauman19bac1e2014-05-06 15:23:49 -04002710 bool PixelRoutine::colorUsed()
2711 {
2712 return state.colorWriteMask || state.alphaTestActive() || state.shaderContainsKill;
2713 }
John Bauman89401822014-05-06 15:04:28 -04002714}