blob: d0650b0ec15470a2424f24f64f395e29eb527d1a [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
17#include "Renderer.hpp"
John Bauman89401822014-05-06 15:04:28 -040018#include "QuadRasterizer.hpp"
19#include "Surface.hpp"
20#include "Primitive.hpp"
John Bauman89401822014-05-06 15:04:28 -040021#include "SamplerCore.hpp"
22#include "Constants.hpp"
23#include "Debug.hpp"
24
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 Hetuf2a8c372015-07-13 11:08:41 -040034 if(!shader || shader->getVersion() < 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 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040048 for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040049 {
50 delete sampler[i];
51 }
52 }
53
Nicolas Capens4f172c72016-01-13 08:34:30 -050054 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 -040055 {
56 #if PERF_PROFILE
57 Long pipeTime = Ticks();
58 #endif
59
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040060 for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040061 {
Nicolas Capens4f172c72016-01-13 08:34:30 -050062 sampler[i] = new SamplerCore(constants, state.sampler[i]);
John Bauman89401822014-05-06 15:04:28 -040063 }
64
65 const bool earlyDepthTest = !state.depthOverride && !state.alphaTestActive();
John Bauman89401822014-05-06 15:04:28 -040066
67 Int zMask[4]; // Depth mask
68 Int sMask[4]; // Stencil mask
69
70 for(unsigned int q = 0; q < state.multiSample; q++)
71 {
72 zMask[q] = cMask[q];
73 sMask[q] = cMask[q];
74 }
75
76 for(unsigned int q = 0; q < state.multiSample; q++)
77 {
Nicolas Capens4f172c72016-01-13 08:34:30 -050078 stencilTest(sBuffer, q, x, sMask[q], cMask[q]);
John Bauman89401822014-05-06 15:04:28 -040079 }
80
81 Float4 f;
John Bauman89401822014-05-06 15:04:28 -040082 Float4 rhwCentroid;
83
Nicolas Capens4f172c72016-01-13 08:34:30 -050084 Float4 xxxx = Float4(Float(x)) + *Pointer<Float4>(primitive + OFFSET(Primitive,xQuad), 16);
John Bauman89401822014-05-06 15:04:28 -040085
John Bauman19bac1e2014-05-06 15:23:49 -040086 if(interpolateZ())
John Bauman89401822014-05-06 15:04:28 -040087 {
88 for(unsigned int q = 0; q < state.multiSample; q++)
89 {
90 Float4 x = xxxx;
Nicolas Capens4f172c72016-01-13 08:34:30 -050091
John Bauman89401822014-05-06 15:04:28 -040092 if(state.multiSample > 1)
93 {
Nicolas Capens4f172c72016-01-13 08:34:30 -050094 x -= *Pointer<Float4>(constants + OFFSET(Constants,X) + q * sizeof(float4));
John Bauman89401822014-05-06 15:04:28 -040095 }
96
Nicolas Capens4f172c72016-01-13 08:34:30 -050097 z[q] = interpolate(x, Dz[q], z[q], primitive + OFFSET(Primitive,z), false, false);
John Bauman89401822014-05-06 15:04:28 -040098 }
99 }
100
101 Bool depthPass = false;
102
103 if(earlyDepthTest)
104 {
105 for(unsigned int q = 0; q < state.multiSample; q++)
106 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500107 depthPass = depthPass || depthTest(zBuffer, q, x, z[q], sMask[q], zMask[q], cMask[q]);
John Bauman89401822014-05-06 15:04:28 -0400108 }
109 }
110
111 If(depthPass || Bool(!earlyDepthTest))
112 {
113 #if PERF_PROFILE
114 Long interpTime = Ticks();
115 #endif
116
Nicolas Capens4f172c72016-01-13 08:34:30 -0500117 Float4 yyyy = Float4(Float(y)) + *Pointer<Float4>(primitive + OFFSET(Primitive,yQuad), 16);
Nicolas Capenscbefe532014-10-16 00:16:01 -0400118
John Bauman89401822014-05-06 15:04:28 -0400119 // Centroid locations
120 Float4 XXXX = Float4(0.0f);
121 Float4 YYYY = Float4(0.0f);
122
123 if(state.centroid)
124 {
125 Float4 WWWW(1.0e-9f);
126
127 for(unsigned int q = 0; q < state.multiSample; q++)
128 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500129 XXXX += *Pointer<Float4>(constants + OFFSET(Constants,sampleX[q]) + 16 * cMask[q]);
130 YYYY += *Pointer<Float4>(constants + OFFSET(Constants,sampleY[q]) + 16 * cMask[q]);
131 WWWW += *Pointer<Float4>(constants + OFFSET(Constants,weight) + 16 * cMask[q]);
John Bauman89401822014-05-06 15:04:28 -0400132 }
133
134 WWWW = Rcp_pp(WWWW);
135 XXXX *= WWWW;
136 YYYY *= WWWW;
137
138 XXXX += xxxx;
139 YYYY += yyyy;
140 }
141
John Bauman19bac1e2014-05-06 15:23:49 -0400142 if(interpolateW())
John Bauman89401822014-05-06 15:04:28 -0400143 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500144 w = interpolate(xxxx, Dw, rhw, primitive + OFFSET(Primitive,w), false, false);
Nicolas Capens05b3d662016-02-25 23:58:33 -0500145 rhw = reciprocal(w, false, false, true);
John Bauman89401822014-05-06 15:04:28 -0400146
147 if(state.centroid)
148 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500149 rhwCentroid = reciprocal(interpolateCentroid(XXXX, YYYY, rhwCentroid, primitive + OFFSET(Primitive,w), false, false));
John Bauman89401822014-05-06 15:04:28 -0400150 }
151 }
152
Nicolas Capens3b4c93f2016-05-18 12:51:37 -0400153 for(int interpolant = 0; interpolant < MAX_FRAGMENT_INPUTS; interpolant++)
John Bauman89401822014-05-06 15:04:28 -0400154 {
155 for(int component = 0; component < 4; component++)
156 {
John Bauman89401822014-05-06 15:04:28 -0400157 if(state.interpolant[interpolant].component & (1 << component))
158 {
159 if(!state.interpolant[interpolant].centroid)
160 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500161 v[interpolant][component] = interpolate(xxxx, Dv[interpolant][component], rhw, primitive + OFFSET(Primitive, V[interpolant][component]), (state.interpolant[interpolant].flat & (1 << component)) != 0, state.perspective);
John Bauman89401822014-05-06 15:04:28 -0400162 }
163 else
164 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500165 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 -0400166 }
167 }
168 }
169
170 Float4 rcp;
171
172 switch(state.interpolant[interpolant].project)
173 {
174 case 0:
175 break;
176 case 1:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500177 rcp = reciprocal(v[interpolant].y);
178 v[interpolant].x = v[interpolant].x * rcp;
John Bauman89401822014-05-06 15:04:28 -0400179 break;
180 case 2:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500181 rcp = reciprocal(v[interpolant].z);
182 v[interpolant].x = v[interpolant].x * rcp;
183 v[interpolant].y = v[interpolant].y * rcp;
John Bauman89401822014-05-06 15:04:28 -0400184 break;
185 case 3:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500186 rcp = reciprocal(v[interpolant].w);
187 v[interpolant].x = v[interpolant].x * rcp;
188 v[interpolant].y = v[interpolant].y * rcp;
189 v[interpolant].z = v[interpolant].z * rcp;
John Bauman89401822014-05-06 15:04:28 -0400190 break;
191 }
192 }
193
194 if(state.fog.component)
195 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500196 f = interpolate(xxxx, Df, rhw, primitive + OFFSET(Primitive,f), state.fog.flat & 0x01, state.perspective);
John Bauman89401822014-05-06 15:04:28 -0400197 }
198
Nicolas Capens4f172c72016-01-13 08:34:30 -0500199 setBuiltins(x, y, z, w);
John Bauman89401822014-05-06 15:04:28 -0400200
201 #if PERF_PROFILE
Nicolas Capens4f172c72016-01-13 08:34:30 -0500202 cycles[PERF_INTERP] += Ticks() - interpTime;
John Bauman89401822014-05-06 15:04:28 -0400203 #endif
204
205 Bool alphaPass = true;
206
207 if(colorUsed())
208 {
209 #if PERF_PROFILE
210 Long shaderTime = Ticks();
211 #endif
212
Nicolas Capens4f172c72016-01-13 08:34:30 -0500213 applyShader(cMask);
John Bauman89401822014-05-06 15:04:28 -0400214
215 #if PERF_PROFILE
Nicolas Capens4f172c72016-01-13 08:34:30 -0500216 cycles[PERF_SHADER] += Ticks() - shaderTime;
John Bauman89401822014-05-06 15:04:28 -0400217 #endif
218
Nicolas Capens4f172c72016-01-13 08:34:30 -0500219 alphaPass = alphaTest(cMask);
John Bauman89401822014-05-06 15:04:28 -0400220
John Bauman19bac1e2014-05-06 15:23:49 -0400221 if((shader && shader->containsKill()) || state.alphaTestActive())
John Bauman89401822014-05-06 15:04:28 -0400222 {
223 for(unsigned int q = 0; q < state.multiSample; q++)
224 {
225 zMask[q] &= cMask[q];
226 sMask[q] &= cMask[q];
227 }
228 }
229 }
230
231 If(alphaPass)
232 {
233 if(!earlyDepthTest)
234 {
235 for(unsigned int q = 0; q < state.multiSample; q++)
236 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500237 depthPass = depthPass || depthTest(zBuffer, q, x, z[q], sMask[q], zMask[q], cMask[q]);
John Bauman89401822014-05-06 15:04:28 -0400238 }
239 }
240
241 #if PERF_PROFILE
242 Long ropTime = Ticks();
243 #endif
244
245 If(depthPass || Bool(earlyDepthTest))
246 {
247 for(unsigned int q = 0; q < state.multiSample; q++)
248 {
249 if(state.multiSampleMask & (1 << q))
250 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500251 writeDepth(zBuffer, q, x, z[q], zMask[q]);
John Bauman89401822014-05-06 15:04:28 -0400252
253 if(state.occlusionEnabled)
254 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500255 occlusion += *Pointer<UInt>(constants + OFFSET(Constants,occlusionCount) + 4 * (zMask[q] & sMask[q]));
John Bauman89401822014-05-06 15:04:28 -0400256 }
257 }
258 }
259
260 if(colorUsed())
261 {
262 #if PERF_PROFILE
John Bauman66b8ab22014-05-06 15:57:45 -0400263 AddAtomic(Pointer<Long>(&profiler.ropOperations), 4);
John Bauman89401822014-05-06 15:04:28 -0400264 #endif
265
Nicolas Capens4f172c72016-01-13 08:34:30 -0500266 rasterOperation(f, cBuffer, x, sMask, zMask, cMask);
John Bauman89401822014-05-06 15:04:28 -0400267 }
268 }
269
270 #if PERF_PROFILE
Nicolas Capens4f172c72016-01-13 08:34:30 -0500271 cycles[PERF_ROP] += Ticks() - ropTime;
John Bauman89401822014-05-06 15:04:28 -0400272 #endif
273 }
274 }
275
276 for(unsigned int q = 0; q < state.multiSample; q++)
277 {
278 if(state.multiSampleMask & (1 << q))
279 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500280 writeStencil(sBuffer, q, x, sMask[q], zMask[q], cMask[q]);
John Bauman89401822014-05-06 15:04:28 -0400281 }
282 }
283
284 #if PERF_PROFILE
Nicolas Capens4f172c72016-01-13 08:34:30 -0500285 cycles[PERF_PIPE] += Ticks() - pipeTime;
John Bauman89401822014-05-06 15:04:28 -0400286 #endif
287 }
288
John Bauman89401822014-05-06 15:04:28 -0400289 Float4 PixelRoutine::interpolateCentroid(Float4 &x, Float4 &y, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective)
290 {
291 Float4 interpolant = *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,C), 16);
292
293 if(!flat)
294 {
295 interpolant += x * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,A), 16) +
296 y * *Pointer<Float4>(planeEquation + OFFSET(PlaneEquation,B), 16);
297
298 if(perspective)
299 {
300 interpolant *= rhw;
301 }
302 }
303
304 return interpolant;
305 }
306
Nicolas Capens4f172c72016-01-13 08:34:30 -0500307 void PixelRoutine::stencilTest(Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &cMask)
John Bauman89401822014-05-06 15:04:28 -0400308 {
309 if(!state.stencilActive)
310 {
311 return;
312 }
313
314 // (StencilRef & StencilMask) CompFunc (StencilBufferValue & StencilMask)
315
316 Pointer<Byte> buffer = sBuffer + 2 * x;
317
318 if(q > 0)
319 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500320 buffer += q * *Pointer<Int>(data + OFFSET(DrawData,stencilSliceB));
John Bauman89401822014-05-06 15:04:28 -0400321 }
322
Nicolas Capens48ef1252016-11-07 15:30:33 -0500323 Byte8 value = *Pointer<Byte8>(buffer);
John Bauman89401822014-05-06 15:04:28 -0400324 Byte8 valueCCW = value;
325
326 if(!state.noStencilMask)
327 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500328 value &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[0].testMaskQ));
John Bauman89401822014-05-06 15:04:28 -0400329 }
330
Nicolas Capens4f172c72016-01-13 08:34:30 -0500331 stencilTest(value, state.stencilCompareMode, false);
John Bauman89401822014-05-06 15:04:28 -0400332
333 if(state.twoSidedStencil)
334 {
335 if(!state.noStencilMaskCCW)
336 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500337 valueCCW &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[1].testMaskQ));
John Bauman89401822014-05-06 15:04:28 -0400338 }
339
Nicolas Capens4f172c72016-01-13 08:34:30 -0500340 stencilTest(valueCCW, state.stencilCompareModeCCW, true);
John Bauman89401822014-05-06 15:04:28 -0400341
Nicolas Capens4f172c72016-01-13 08:34:30 -0500342 value &= *Pointer<Byte8>(primitive + OFFSET(Primitive,clockwiseMask));
343 valueCCW &= *Pointer<Byte8>(primitive + OFFSET(Primitive,invClockwiseMask));
John Bauman89401822014-05-06 15:04:28 -0400344 value |= valueCCW;
345 }
346
347 sMask = SignMask(value) & cMask;
348 }
349
Nicolas Capens4f172c72016-01-13 08:34:30 -0500350 void PixelRoutine::stencilTest(Byte8 &value, StencilCompareMode stencilCompareMode, bool CCW)
John Bauman89401822014-05-06 15:04:28 -0400351 {
352 Byte8 equal;
353
354 switch(stencilCompareMode)
355 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400356 case STENCIL_ALWAYS:
Nicolas Capens16b5f152016-10-13 13:39:01 -0400357 value = Byte8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
John Bauman89401822014-05-06 15:04:28 -0400358 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400359 case STENCIL_NEVER:
Nicolas Capens16b5f152016-10-13 13:39:01 -0400360 value = Byte8(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
John Bauman89401822014-05-06 15:04:28 -0400361 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400362 case STENCIL_LESS: // a < b ~ b > a
John Bauman89401822014-05-06 15:04:28 -0400363 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
Nicolas Capens4f172c72016-01-13 08:34:30 -0500364 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
John Bauman89401822014-05-06 15:04:28 -0400365 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400366 case STENCIL_EQUAL:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500367 value = CmpEQ(value, *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
John Bauman89401822014-05-06 15:04:28 -0400368 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400369 case STENCIL_NOTEQUAL: // a != b ~ !(a == b)
Nicolas Capens4f172c72016-01-13 08:34:30 -0500370 value = CmpEQ(value, *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
Nicolas Capens16b5f152016-10-13 13:39:01 -0400371 value ^= Byte8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
John Bauman89401822014-05-06 15:04:28 -0400372 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400373 case STENCIL_LESSEQUAL: // a <= b ~ (b > a) || (a == b)
John Bauman89401822014-05-06 15:04:28 -0400374 equal = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500375 equal = CmpEQ(equal, *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
John Bauman89401822014-05-06 15:04:28 -0400376 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
Nicolas Capens4f172c72016-01-13 08:34:30 -0500377 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
John Bauman89401822014-05-06 15:04:28 -0400378 value |= equal;
379 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400380 case STENCIL_GREATER: // a > b
Nicolas Capens4f172c72016-01-13 08:34:30 -0500381 equal = *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ));
John Bauman89401822014-05-06 15:04:28 -0400382 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
383 equal = CmpGT(As<SByte8>(equal), As<SByte8>(value));
384 value = equal;
385 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400386 case STENCIL_GREATEREQUAL: // a >= b ~ !(a < b) ~ !(b > a)
John Bauman89401822014-05-06 15:04:28 -0400387 value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
Nicolas Capens4f172c72016-01-13 08:34:30 -0500388 value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
Nicolas Capens16b5f152016-10-13 13:39:01 -0400389 value ^= Byte8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
John Bauman89401822014-05-06 15:04:28 -0400390 break;
391 default:
392 ASSERT(false);
393 }
394 }
395
Nicolas Capens4f172c72016-01-13 08:34:30 -0500396 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 -0400397 {
398 if(!state.depthTestActive)
399 {
400 return true;
401 }
402
403 Float4 Z = z;
404
John Bauman19bac1e2014-05-06 15:23:49 -0400405 if(shader && shader->depthOverride())
John Bauman89401822014-05-06 15:04:28 -0400406 {
407 if(complementaryDepthBuffer)
408 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500409 Z = Float4(1.0f) - oDepth;
John Bauman89401822014-05-06 15:04:28 -0400410 }
411 else
412 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500413 Z = oDepth;
John Bauman89401822014-05-06 15:04:28 -0400414 }
415 }
416
417 Pointer<Byte> buffer;
418 Int pitch;
419
420 if(!state.quadLayoutDepthBuffer)
421 {
422 buffer = zBuffer + 4 * x;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500423 pitch = *Pointer<Int>(data + OFFSET(DrawData,depthPitchB));
John Bauman89401822014-05-06 15:04:28 -0400424 }
425 else
426 {
427 buffer = zBuffer + 8 * x;
428 }
429
430 if(q > 0)
431 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500432 buffer += q * *Pointer<Int>(data + OFFSET(DrawData,depthSliceB));
John Bauman89401822014-05-06 15:04:28 -0400433 }
434
435 Float4 zValue;
436
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400437 if(state.depthCompareMode != DEPTH_NEVER || (state.depthCompareMode != DEPTH_ALWAYS && !state.depthWriteEnable))
John Bauman89401822014-05-06 15:04:28 -0400438 {
439 if(!state.quadLayoutDepthBuffer)
440 {
441 // FIXME: Properly optimizes?
442 zValue.xy = *Pointer<Float4>(buffer);
443 zValue.zw = *Pointer<Float4>(buffer + pitch - 8);
444 }
445 else
446 {
447 zValue = *Pointer<Float4>(buffer, 16);
448 }
449 }
450
451 Int4 zTest;
452
453 switch(state.depthCompareMode)
454 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400455 case DEPTH_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400456 // Optimized
457 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400458 case DEPTH_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400459 // Optimized
460 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400461 case DEPTH_EQUAL:
John Bauman89401822014-05-06 15:04:28 -0400462 zTest = CmpEQ(zValue, Z);
463 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400464 case DEPTH_NOTEQUAL:
John Bauman89401822014-05-06 15:04:28 -0400465 zTest = CmpNEQ(zValue, Z);
466 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400467 case DEPTH_LESS:
John Bauman89401822014-05-06 15:04:28 -0400468 if(complementaryDepthBuffer)
469 {
470 zTest = CmpLT(zValue, Z);
471 }
472 else
473 {
474 zTest = CmpNLE(zValue, Z);
475 }
476 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400477 case DEPTH_GREATEREQUAL:
John Bauman89401822014-05-06 15:04:28 -0400478 if(complementaryDepthBuffer)
479 {
480 zTest = CmpNLT(zValue, Z);
481 }
482 else
483 {
484 zTest = CmpLE(zValue, Z);
485 }
486 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400487 case DEPTH_LESSEQUAL:
John Bauman89401822014-05-06 15:04:28 -0400488 if(complementaryDepthBuffer)
489 {
490 zTest = CmpLE(zValue, Z);
491 }
492 else
493 {
494 zTest = CmpNLT(zValue, Z);
495 }
496 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400497 case DEPTH_GREATER:
John Bauman89401822014-05-06 15:04:28 -0400498 if(complementaryDepthBuffer)
499 {
500 zTest = CmpNLE(zValue, Z);
501 }
502 else
503 {
504 zTest = CmpLT(zValue, Z);
505 }
506 break;
507 default:
508 ASSERT(false);
509 }
510
511 switch(state.depthCompareMode)
512 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400513 case DEPTH_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400514 zMask = cMask;
515 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400516 case DEPTH_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400517 zMask = 0x0;
518 break;
519 default:
520 zMask = SignMask(zTest) & cMask;
521 break;
522 }
Nicolas Capens05b3d662016-02-25 23:58:33 -0500523
John Bauman89401822014-05-06 15:04:28 -0400524 if(state.stencilActive)
525 {
526 zMask &= sMask;
527 }
528
529 return zMask != 0;
530 }
531
Nicolas Capens4f172c72016-01-13 08:34:30 -0500532 void PixelRoutine::alphaTest(Int &aMask, Short4 &alpha)
John Bauman89401822014-05-06 15:04:28 -0400533 {
534 Short4 cmp;
535 Short4 equal;
536
537 switch(state.alphaCompareMode)
538 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400539 case ALPHA_ALWAYS:
John Bauman89401822014-05-06 15:04:28 -0400540 aMask = 0xF;
541 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400542 case ALPHA_NEVER:
John Bauman89401822014-05-06 15:04:28 -0400543 aMask = 0x0;
544 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400545 case ALPHA_EQUAL:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500546 cmp = CmpEQ(alpha, *Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4)));
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400547 aMask = SignMask(Pack(cmp, Short4(0x0000)));
John Bauman89401822014-05-06 15:04:28 -0400548 break;
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400549 case ALPHA_NOTEQUAL: // a != b ~ !(a == b)
550 cmp = CmpEQ(alpha, *Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4))) ^ Short4(0xFFFFu); // FIXME
551 aMask = SignMask(Pack(cmp, Short4(0x0000)));
John Bauman89401822014-05-06 15:04:28 -0400552 break;
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400553 case ALPHA_LESS: // a < b ~ b > a
Nicolas Capens4f172c72016-01-13 08:34:30 -0500554 cmp = CmpGT(*Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4)), alpha);
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400555 aMask = SignMask(Pack(cmp, Short4(0x0000)));
John Bauman89401822014-05-06 15:04:28 -0400556 break;
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400557 case ALPHA_GREATEREQUAL: // a >= b ~ (a > b) || (a == b) ~ !(b > a) // TODO: Approximate
Nicolas Capens4f172c72016-01-13 08:34:30 -0500558 equal = CmpEQ(alpha, *Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4)));
559 cmp = CmpGT(alpha, *Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4)));
John Bauman89401822014-05-06 15:04:28 -0400560 cmp |= equal;
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400561 aMask = SignMask(Pack(cmp, Short4(0x0000)));
John Bauman89401822014-05-06 15:04:28 -0400562 break;
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400563 case ALPHA_LESSEQUAL: // a <= b ~ !(a > b)
564 cmp = CmpGT(alpha, *Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4))) ^ Short4(0xFFFFu); // FIXME
565 aMask = SignMask(Pack(cmp, Short4(0x0000)));
John Bauman89401822014-05-06 15:04:28 -0400566 break;
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400567 case ALPHA_GREATER: // a > b
Nicolas Capens4f172c72016-01-13 08:34:30 -0500568 cmp = CmpGT(alpha, *Pointer<Short4>(data + OFFSET(DrawData,factor.alphaReference4)));
Alexis Hetu90c7ad62016-06-27 11:50:40 -0400569 aMask = SignMask(Pack(cmp, Short4(0x0000)));
John Bauman89401822014-05-06 15:04:28 -0400570 break;
571 default:
572 ASSERT(false);
573 }
574 }
575
Nicolas Capens4f172c72016-01-13 08:34:30 -0500576 void PixelRoutine::alphaToCoverage(Int cMask[4], Float4 &alpha)
John Bauman89401822014-05-06 15:04:28 -0400577 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500578 Int4 coverage0 = CmpNLT(alpha, *Pointer<Float4>(data + OFFSET(DrawData,a2c0)));
579 Int4 coverage1 = CmpNLT(alpha, *Pointer<Float4>(data + OFFSET(DrawData,a2c1)));
580 Int4 coverage2 = CmpNLT(alpha, *Pointer<Float4>(data + OFFSET(DrawData,a2c2)));
581 Int4 coverage3 = CmpNLT(alpha, *Pointer<Float4>(data + OFFSET(DrawData,a2c3)));
John Bauman89401822014-05-06 15:04:28 -0400582
583 Int aMask0 = SignMask(coverage0);
584 Int aMask1 = SignMask(coverage1);
585 Int aMask2 = SignMask(coverage2);
586 Int aMask3 = SignMask(coverage3);
587
588 cMask[0] &= aMask0;
589 cMask[1] &= aMask1;
590 cMask[2] &= aMask2;
591 cMask[3] &= aMask3;
592 }
593
Nicolas Capens4f172c72016-01-13 08:34:30 -0500594 void PixelRoutine::fogBlend(Vector4f &c0, Float4 &fog)
John Bauman89401822014-05-06 15:04:28 -0400595 {
596 if(!state.fogActive)
597 {
598 return;
599 }
600
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400601 if(state.pixelFogMode != FOG_NONE)
John Bauman89401822014-05-06 15:04:28 -0400602 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500603 pixelFog(fog);
John Bauman89401822014-05-06 15:04:28 -0400604
John Bauman19bac1e2014-05-06 15:23:49 -0400605 fog = Min(fog, Float4(1.0f));
606 fog = Max(fog, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400607 }
608
Nicolas Capens4f172c72016-01-13 08:34:30 -0500609 c0.x -= *Pointer<Float4>(data + OFFSET(DrawData,fog.colorF[0]));
610 c0.y -= *Pointer<Float4>(data + OFFSET(DrawData,fog.colorF[1]));
611 c0.z -= *Pointer<Float4>(data + OFFSET(DrawData,fog.colorF[2]));
John Bauman89401822014-05-06 15:04:28 -0400612
John Bauman19bac1e2014-05-06 15:23:49 -0400613 c0.x *= fog;
614 c0.y *= fog;
615 c0.z *= fog;
John Bauman89401822014-05-06 15:04:28 -0400616
Nicolas Capens4f172c72016-01-13 08:34:30 -0500617 c0.x += *Pointer<Float4>(data + OFFSET(DrawData,fog.colorF[0]));
618 c0.y += *Pointer<Float4>(data + OFFSET(DrawData,fog.colorF[1]));
619 c0.z += *Pointer<Float4>(data + OFFSET(DrawData,fog.colorF[2]));
John Bauman89401822014-05-06 15:04:28 -0400620 }
621
Nicolas Capens4f172c72016-01-13 08:34:30 -0500622 void PixelRoutine::pixelFog(Float4 &visibility)
John Bauman89401822014-05-06 15:04:28 -0400623 {
624 Float4 &zw = visibility;
625
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400626 if(state.pixelFogMode != FOG_NONE)
John Bauman89401822014-05-06 15:04:28 -0400627 {
628 if(state.wBasedFog)
629 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500630 zw = rhw;
John Bauman89401822014-05-06 15:04:28 -0400631 }
632 else
633 {
634 if(complementaryDepthBuffer)
635 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500636 zw = Float4(1.0f) - z[0];
John Bauman89401822014-05-06 15:04:28 -0400637 }
638 else
639 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500640 zw = z[0];
John Bauman89401822014-05-06 15:04:28 -0400641 }
642 }
643 }
644
645 switch(state.pixelFogMode)
646 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400647 case FOG_NONE:
John Bauman89401822014-05-06 15:04:28 -0400648 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400649 case FOG_LINEAR:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500650 zw *= *Pointer<Float4>(data + OFFSET(DrawData,fog.scale));
651 zw += *Pointer<Float4>(data + OFFSET(DrawData,fog.offset));
John Bauman89401822014-05-06 15:04:28 -0400652 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400653 case FOG_EXP:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500654 zw *= *Pointer<Float4>(data + OFFSET(DrawData,fog.densityE));
John Bauman19bac1e2014-05-06 15:23:49 -0400655 zw = exponential2(zw, true);
John Bauman89401822014-05-06 15:04:28 -0400656 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400657 case FOG_EXP2:
John Bauman89401822014-05-06 15:04:28 -0400658 zw *= zw;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500659 zw *= *Pointer<Float4>(data + OFFSET(DrawData,fog.density2E));
John Bauman19bac1e2014-05-06 15:23:49 -0400660 zw = exponential2(zw, true);
John Bauman89401822014-05-06 15:04:28 -0400661 break;
662 default:
663 ASSERT(false);
664 }
665 }
666
Nicolas Capens4f172c72016-01-13 08:34:30 -0500667 void PixelRoutine::writeDepth(Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &zMask)
John Bauman89401822014-05-06 15:04:28 -0400668 {
669 if(!state.depthWriteEnable)
670 {
671 return;
672 }
673
674 Float4 Z = z;
675
John Bauman19bac1e2014-05-06 15:23:49 -0400676 if(shader && shader->depthOverride())
John Bauman89401822014-05-06 15:04:28 -0400677 {
678 if(complementaryDepthBuffer)
679 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500680 Z = Float4(1.0f) - oDepth;
John Bauman89401822014-05-06 15:04:28 -0400681 }
682 else
683 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500684 Z = oDepth;
John Bauman89401822014-05-06 15:04:28 -0400685 }
686 }
687
688 Pointer<Byte> buffer;
689 Int pitch;
690
691 if(!state.quadLayoutDepthBuffer)
Nicolas Capens05b3d662016-02-25 23:58:33 -0500692 {
John Bauman89401822014-05-06 15:04:28 -0400693 buffer = zBuffer + 4 * x;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500694 pitch = *Pointer<Int>(data + OFFSET(DrawData,depthPitchB));
John Bauman89401822014-05-06 15:04:28 -0400695 }
696 else
Nicolas Capens05b3d662016-02-25 23:58:33 -0500697 {
John Bauman89401822014-05-06 15:04:28 -0400698 buffer = zBuffer + 8 * x;
699 }
700
701 if(q > 0)
702 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500703 buffer += q * *Pointer<Int>(data + OFFSET(DrawData,depthSliceB));
John Bauman89401822014-05-06 15:04:28 -0400704 }
705
706 Float4 zValue;
707
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400708 if(state.depthCompareMode != DEPTH_NEVER || (state.depthCompareMode != DEPTH_ALWAYS && !state.depthWriteEnable))
John Bauman89401822014-05-06 15:04:28 -0400709 {
710 if(!state.quadLayoutDepthBuffer)
711 {
712 // FIXME: Properly optimizes?
713 zValue.xy = *Pointer<Float4>(buffer);
714 zValue.zw = *Pointer<Float4>(buffer + pitch - 8);
715 }
716 else
717 {
718 zValue = *Pointer<Float4>(buffer, 16);
719 }
720 }
721
Nicolas Capens4f172c72016-01-13 08:34:30 -0500722 Z = As<Float4>(As<Int4>(Z) & *Pointer<Int4>(constants + OFFSET(Constants,maskD4X) + zMask * 16, 16));
723 zValue = As<Float4>(As<Int4>(zValue) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD4X) + zMask * 16, 16));
John Bauman89401822014-05-06 15:04:28 -0400724 Z = As<Float4>(As<Int4>(Z) | As<Int4>(zValue));
725
726 if(!state.quadLayoutDepthBuffer)
727 {
728 // FIXME: Properly optimizes?
729 *Pointer<Float2>(buffer) = Float2(Z.xy);
730 *Pointer<Float2>(buffer + pitch) = Float2(Z.zw);
731 }
732 else
733 {
734 *Pointer<Float4>(buffer, 16) = Z;
735 }
736 }
737
Nicolas Capens4f172c72016-01-13 08:34:30 -0500738 void PixelRoutine::writeStencil(Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &zMask, Int &cMask)
John Bauman89401822014-05-06 15:04:28 -0400739 {
740 if(!state.stencilActive)
741 {
742 return;
743 }
744
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400745 if(state.stencilPassOperation == OPERATION_KEEP && state.stencilZFailOperation == OPERATION_KEEP && state.stencilFailOperation == OPERATION_KEEP)
John Bauman89401822014-05-06 15:04:28 -0400746 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400747 if(!state.twoSidedStencil || (state.stencilPassOperationCCW == OPERATION_KEEP && state.stencilZFailOperationCCW == OPERATION_KEEP && state.stencilFailOperationCCW == OPERATION_KEEP))
John Bauman89401822014-05-06 15:04:28 -0400748 {
749 return;
750 }
751 }
752
753 if(state.stencilWriteMasked && (!state.twoSidedStencil || state.stencilWriteMaskedCCW))
754 {
755 return;
756 }
757
758 Pointer<Byte> buffer = sBuffer + 2 * x;
759
760 if(q > 0)
761 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500762 buffer += q * *Pointer<Int>(data + OFFSET(DrawData,stencilSliceB));
John Bauman89401822014-05-06 15:04:28 -0400763 }
764
Nicolas Capens48ef1252016-11-07 15:30:33 -0500765 Byte8 bufferValue = *Pointer<Byte8>(buffer);
Nicolas Capens05b3d662016-02-25 23:58:33 -0500766
John Bauman89401822014-05-06 15:04:28 -0400767 Byte8 newValue;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500768 stencilOperation(newValue, bufferValue, state.stencilPassOperation, state.stencilZFailOperation, state.stencilFailOperation, false, zMask, sMask);
John Bauman89401822014-05-06 15:04:28 -0400769
770 if(!state.noStencilWriteMask)
771 {
772 Byte8 maskedValue = bufferValue;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500773 newValue &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[0].writeMaskQ));
774 maskedValue &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[0].invWriteMaskQ));
John Bauman89401822014-05-06 15:04:28 -0400775 newValue |= maskedValue;
776 }
777
778 if(state.twoSidedStencil)
779 {
780 Byte8 newValueCCW;
781
Nicolas Capens4f172c72016-01-13 08:34:30 -0500782 stencilOperation(newValueCCW, bufferValue, state.stencilPassOperationCCW, state.stencilZFailOperationCCW, state.stencilFailOperationCCW, true, zMask, sMask);
John Bauman89401822014-05-06 15:04:28 -0400783
784 if(!state.noStencilWriteMaskCCW)
785 {
786 Byte8 maskedValue = bufferValue;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500787 newValueCCW &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[1].writeMaskQ));
788 maskedValue &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[1].invWriteMaskQ));
John Bauman89401822014-05-06 15:04:28 -0400789 newValueCCW |= maskedValue;
790 }
791
Nicolas Capens4f172c72016-01-13 08:34:30 -0500792 newValue &= *Pointer<Byte8>(primitive + OFFSET(Primitive,clockwiseMask));
793 newValueCCW &= *Pointer<Byte8>(primitive + OFFSET(Primitive,invClockwiseMask));
John Bauman89401822014-05-06 15:04:28 -0400794 newValue |= newValueCCW;
795 }
796
Nicolas Capens4f172c72016-01-13 08:34:30 -0500797 newValue &= *Pointer<Byte8>(constants + OFFSET(Constants,maskB4Q) + 8 * cMask);
798 bufferValue &= *Pointer<Byte8>(constants + OFFSET(Constants,invMaskB4Q) + 8 * cMask);
John Bauman89401822014-05-06 15:04:28 -0400799 newValue |= bufferValue;
800
Nicolas Capens16b5f152016-10-13 13:39:01 -0400801 *Pointer<Byte4>(buffer) = Byte4(newValue);
John Bauman89401822014-05-06 15:04:28 -0400802 }
803
Nicolas Capens4f172c72016-01-13 08:34:30 -0500804 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 -0400805 {
806 Byte8 &pass = newValue;
807 Byte8 fail;
808 Byte8 zFail;
809
Nicolas Capens4f172c72016-01-13 08:34:30 -0500810 stencilOperation(pass, bufferValue, stencilPassOperation, CCW);
John Bauman89401822014-05-06 15:04:28 -0400811
812 if(stencilZFailOperation != stencilPassOperation)
813 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500814 stencilOperation(zFail, bufferValue, stencilZFailOperation, CCW);
John Bauman89401822014-05-06 15:04:28 -0400815 }
816
817 if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
818 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500819 stencilOperation(fail, bufferValue, stencilFailOperation, CCW);
John Bauman89401822014-05-06 15:04:28 -0400820 }
821
822 if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
823 {
824 if(state.depthTestActive && stencilZFailOperation != stencilPassOperation) // zMask valid and values not the same
825 {
Nicolas Capens4f172c72016-01-13 08:34:30 -0500826 pass &= *Pointer<Byte8>(constants + OFFSET(Constants,maskB4Q) + 8 * zMask);
827 zFail &= *Pointer<Byte8>(constants + OFFSET(Constants,invMaskB4Q) + 8 * zMask);
John Bauman89401822014-05-06 15:04:28 -0400828 pass |= zFail;
829 }
830
Nicolas Capens4f172c72016-01-13 08:34:30 -0500831 pass &= *Pointer<Byte8>(constants + OFFSET(Constants,maskB4Q) + 8 * sMask);
832 fail &= *Pointer<Byte8>(constants + OFFSET(Constants,invMaskB4Q) + 8 * sMask);
John Bauman89401822014-05-06 15:04:28 -0400833 pass |= fail;
834 }
835 }
836
Nicolas Capens4f172c72016-01-13 08:34:30 -0500837 void PixelRoutine::stencilOperation(Byte8 &output, Byte8 &bufferValue, StencilOperation operation, bool CCW)
John Bauman89401822014-05-06 15:04:28 -0400838 {
839 switch(operation)
840 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400841 case OPERATION_KEEP:
John Bauman89401822014-05-06 15:04:28 -0400842 output = bufferValue;
843 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400844 case OPERATION_ZERO:
Nicolas Capens16b5f152016-10-13 13:39:01 -0400845 output = Byte8(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
John Bauman89401822014-05-06 15:04:28 -0400846 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400847 case OPERATION_REPLACE:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500848 output = *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceQ));
John Bauman89401822014-05-06 15:04:28 -0400849 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400850 case OPERATION_INCRSAT:
John Bauman89401822014-05-06 15:04:28 -0400851 output = AddSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));
852 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400853 case OPERATION_DECRSAT:
John Bauman89401822014-05-06 15:04:28 -0400854 output = SubSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));
855 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400856 case OPERATION_INVERT:
Nicolas Capens16b5f152016-10-13 13:39:01 -0400857 output = bufferValue ^ Byte8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
John Bauman89401822014-05-06 15:04:28 -0400858 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400859 case OPERATION_INCR:
John Bauman89401822014-05-06 15:04:28 -0400860 output = bufferValue + Byte8(1, 1, 1, 1, 1, 1, 1, 1);
861 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400862 case OPERATION_DECR:
John Bauman89401822014-05-06 15:04:28 -0400863 output = bufferValue - Byte8(1, 1, 1, 1, 1, 1, 1, 1);
864 break;
865 default:
866 ASSERT(false);
867 }
868 }
869
Nicolas Capens96d4e092016-11-18 14:22:38 -0500870 void PixelRoutine::blendFactor(Vector4s &blendFactor, const Vector4s &current, const Vector4s &pixel, BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -0400871 {
872 switch(blendFactorActive)
873 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400874 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -0400875 // Optimized
876 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400877 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -0400878 // Optimized
879 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400880 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -0400881 blendFactor.x = current.x;
882 blendFactor.y = current.y;
883 blendFactor.z = current.z;
John Bauman89401822014-05-06 15:04:28 -0400884 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400885 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -0400886 blendFactor.x = Short4(0xFFFFu) - current.x;
887 blendFactor.y = Short4(0xFFFFu) - current.y;
888 blendFactor.z = Short4(0xFFFFu) - current.z;
John Bauman89401822014-05-06 15:04:28 -0400889 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400890 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -0400891 blendFactor.x = pixel.x;
892 blendFactor.y = pixel.y;
893 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -0400894 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400895 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -0400896 blendFactor.x = Short4(0xFFFFu) - pixel.x;
897 blendFactor.y = Short4(0xFFFFu) - pixel.y;
898 blendFactor.z = Short4(0xFFFFu) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -0400899 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400900 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400901 blendFactor.x = current.w;
902 blendFactor.y = current.w;
903 blendFactor.z = current.w;
John Bauman89401822014-05-06 15:04:28 -0400904 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400905 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400906 blendFactor.x = Short4(0xFFFFu) - current.w;
907 blendFactor.y = Short4(0xFFFFu) - current.w;
908 blendFactor.z = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -0400909 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400910 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400911 blendFactor.x = pixel.w;
912 blendFactor.y = pixel.w;
913 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400914 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400915 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400916 blendFactor.x = Short4(0xFFFFu) - pixel.w;
917 blendFactor.y = Short4(0xFFFFu) - pixel.w;
918 blendFactor.z = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400919 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400920 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -0400921 blendFactor.x = Short4(0xFFFFu) - pixel.w;
922 blendFactor.x = Min(As<UShort4>(blendFactor.x), As<UShort4>(current.w));
923 blendFactor.y = blendFactor.x;
924 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -0400925 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400926 case BLEND_CONSTANT:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500927 blendFactor.x = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[0]));
928 blendFactor.y = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[1]));
929 blendFactor.z = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[2]));
John Bauman89401822014-05-06 15:04:28 -0400930 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400931 case BLEND_INVCONSTANT:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500932 blendFactor.x = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[0]));
933 blendFactor.y = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[1]));
934 blendFactor.z = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[2]));
John Bauman89401822014-05-06 15:04:28 -0400935 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400936 case BLEND_CONSTANTALPHA:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500937 blendFactor.x = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[3]));
938 blendFactor.y = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[3]));
939 blendFactor.z = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -0400940 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400941 case BLEND_INVCONSTANTALPHA:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500942 blendFactor.x = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
943 blendFactor.y = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
944 blendFactor.z = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -0400945 break;
946 default:
947 ASSERT(false);
948 }
949 }
Nicolas Capens05b3d662016-02-25 23:58:33 -0500950
Nicolas Capens96d4e092016-11-18 14:22:38 -0500951 void PixelRoutine::blendFactorAlpha(Vector4s &blendFactor, const Vector4s &current, const Vector4s &pixel, BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -0400952 {
953 switch(blendFactorAlphaActive)
954 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400955 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -0400956 // Optimized
957 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400958 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -0400959 // Optimized
960 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400961 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -0400962 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -0400963 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400964 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -0400965 blendFactor.w = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -0400966 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400967 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -0400968 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400969 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400970 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -0400971 blendFactor.w = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400972 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400973 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400974 blendFactor.w = current.w;
John Bauman89401822014-05-06 15:04:28 -0400975 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400976 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400977 blendFactor.w = Short4(0xFFFFu) - current.w;
John Bauman89401822014-05-06 15:04:28 -0400978 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400979 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400980 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400981 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400982 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -0400983 blendFactor.w = Short4(0xFFFFu) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -0400984 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400985 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -0400986 blendFactor.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -0400987 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400988 case BLEND_CONSTANT:
989 case BLEND_CONSTANTALPHA:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500990 blendFactor.w = *Pointer<Short4>(data + OFFSET(DrawData,factor.blendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -0400991 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400992 case BLEND_INVCONSTANT:
993 case BLEND_INVCONSTANTALPHA:
Nicolas Capens4f172c72016-01-13 08:34:30 -0500994 blendFactor.w = *Pointer<Short4>(data + OFFSET(DrawData,factor.invBlendConstant4W[3]));
John Bauman89401822014-05-06 15:04:28 -0400995 break;
996 default:
997 ASSERT(false);
998 }
999 }
1000
Alexis Hetu049a1872016-04-25 16:59:58 -04001001 bool PixelRoutine::isSRGB(int index) const
1002 {
1003 return state.targetFormat[index] == FORMAT_SRGB8_A8 || state.targetFormat[index] == FORMAT_SRGB8_X8;
1004 }
1005
Nicolas Capens4f172c72016-01-13 08:34:30 -05001006 void PixelRoutine::readPixel(int index, Pointer<Byte> &cBuffer, Int &x, Vector4s &pixel)
John Bauman89401822014-05-06 15:04:28 -04001007 {
John Bauman89401822014-05-06 15:04:28 -04001008 Short4 c01;
1009 Short4 c23;
Maxime Grégoired9762742015-07-08 16:43:48 -04001010 Pointer<Byte> buffer;
Nicolas Capensb40a2562016-01-05 00:08:45 -05001011 Pointer<Byte> buffer2;
John Bauman89401822014-05-06 15:04:28 -04001012
John Bauman89401822014-05-06 15:04:28 -04001013 switch(state.targetFormat[index])
1014 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001015 case FORMAT_R5G6B5:
1016 buffer = cBuffer + 2 * x;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001017 buffer2 = buffer + *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
Nicolas Capensb40a2562016-01-05 00:08:45 -05001018 c01 = As<Short4>(Int2(*Pointer<Int>(buffer), *Pointer<Int>(buffer2)));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001019
1020 pixel.x = c01 & Short4(0xF800u);
1021 pixel.y = (c01 & Short4(0x07E0u)) << 5;
1022 pixel.z = (c01 & Short4(0x001Fu)) << 11;
1023 pixel.w = Short4(0xFFFFu);
1024 break;
John Bauman89401822014-05-06 15:04:28 -04001025 case FORMAT_A8R8G8B8:
1026 buffer = cBuffer + 4 * x;
1027 c01 = *Pointer<Short4>(buffer);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001028 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04001029 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04001030 pixel.z = c01;
1031 pixel.y = c01;
1032 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
1033 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
1034 pixel.x = pixel.z;
1035 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
1036 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
1037 pixel.y = pixel.z;
1038 pixel.w = pixel.x;
1039 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
1040 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
1041 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
1042 pixel.w = UnpackHigh(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001043 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001044 case FORMAT_A8B8G8R8:
Alexis Hetu049a1872016-04-25 16:59:58 -04001045 case FORMAT_SRGB8_A8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001046 buffer = cBuffer + 4 * x;
1047 c01 = *Pointer<Short4>(buffer);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001048 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001049 c23 = *Pointer<Short4>(buffer);
1050 pixel.z = c01;
1051 pixel.y = c01;
1052 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
1053 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
1054 pixel.x = pixel.z;
1055 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
1056 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
1057 pixel.y = pixel.z;
1058 pixel.w = pixel.x;
1059 pixel.x = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
1060 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
1061 pixel.z = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
1062 pixel.w = UnpackHigh(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
1063 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001064 case FORMAT_A8:
1065 buffer = cBuffer + 1 * x;
1066 pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 0);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001067 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
John Bauman66b8ab22014-05-06 15:57:45 -04001068 pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 1);
1069 pixel.w = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
1070 pixel.x = Short4(0x0000);
1071 pixel.y = Short4(0x0000);
1072 pixel.z = Short4(0x0000);
1073 break;
John Bauman89401822014-05-06 15:04:28 -04001074 case FORMAT_X8R8G8B8:
1075 buffer = cBuffer + 4 * x;
1076 c01 = *Pointer<Short4>(buffer);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001077 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04001078 c23 = *Pointer<Short4>(buffer);
John Bauman19bac1e2014-05-06 15:23:49 -04001079 pixel.z = c01;
1080 pixel.y = c01;
1081 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
1082 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
1083 pixel.x = pixel.z;
1084 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
1085 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
1086 pixel.y = pixel.z;
1087 pixel.x = UnpackLow(As<Byte8>(pixel.x), As<Byte8>(pixel.x));
1088 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
1089 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
1090 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04001091 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001092 case FORMAT_X8B8G8R8:
Alexis Hetu049a1872016-04-25 16:59:58 -04001093 case FORMAT_SRGB8_X8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001094 buffer = cBuffer + 4 * x;
1095 c01 = *Pointer<Short4>(buffer);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001096 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001097 c23 = *Pointer<Short4>(buffer);
1098 pixel.z = c01;
1099 pixel.y = c01;
1100 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(c23));
1101 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(c23));
1102 pixel.x = pixel.z;
1103 pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.y));
1104 pixel.x = UnpackHigh(As<Byte8>(pixel.x), As<Byte8>(pixel.y));
1105 pixel.y = pixel.z;
1106 pixel.w = pixel.x;
1107 pixel.x = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
1108 pixel.y = UnpackHigh(As<Byte8>(pixel.y), As<Byte8>(pixel.y));
1109 pixel.z = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
1110 pixel.w = Short4(0xFFFFu);
1111 break;
John Bauman89401822014-05-06 15:04:28 -04001112 case FORMAT_A8G8R8B8Q:
1113 UNIMPLEMENTED();
Nicolas Capens64750b52015-09-22 10:11:00 -04001114 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
1115 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
1116 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
1117 // pixel.w = UnpackHigh(As<Byte8>(pixel.w), *Pointer<Byte8>(cBuffer + 8 * x + 8));
John Bauman89401822014-05-06 15:04:28 -04001118 break;
1119 case FORMAT_X8G8R8B8Q:
1120 UNIMPLEMENTED();
Nicolas Capens64750b52015-09-22 10:11:00 -04001121 // pixel.z = UnpackLow(As<Byte8>(pixel.z), *Pointer<Byte8>(cBuffer + 8 * x + 0));
1122 // pixel.x = UnpackHigh(As<Byte8>(pixel.x), *Pointer<Byte8>(cBuffer + 8 * x + 0));
1123 // pixel.y = UnpackLow(As<Byte8>(pixel.y), *Pointer<Byte8>(cBuffer + 8 * x + 8));
1124 // pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04001125 break;
1126 case FORMAT_A16B16G16R16:
Maxime Grégoired9762742015-07-08 16:43:48 -04001127 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04001128 pixel.x = *Pointer<Short4>(buffer + 8 * x);
1129 pixel.y = *Pointer<Short4>(buffer + 8 * x + 8);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001130 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04001131 pixel.z = *Pointer<Short4>(buffer + 8 * x);
1132 pixel.w = *Pointer<Short4>(buffer + 8 * x + 8);
1133 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04001134 break;
1135 case FORMAT_G16R16:
1136 buffer = cBuffer;
Maxime Grégoired9762742015-07-08 16:43:48 -04001137 pixel.x = *Pointer<Short4>(buffer + 4 * x);
Nicolas Capens4f172c72016-01-13 08:34:30 -05001138 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
Maxime Grégoired9762742015-07-08 16:43:48 -04001139 pixel.y = *Pointer<Short4>(buffer + 4 * x);
John Bauman19bac1e2014-05-06 15:23:49 -04001140 pixel.z = pixel.x;
1141 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.y));
1142 pixel.z = As<Short4>(UnpackHigh(pixel.z, pixel.y));
1143 pixel.y = pixel.z;
1144 pixel.x = As<Short4>(UnpackLow(pixel.x, pixel.z));
1145 pixel.y = As<Short4>(UnpackHigh(pixel.y, pixel.z));
1146 pixel.z = Short4(0xFFFFu);
1147 pixel.w = Short4(0xFFFFu);
John Bauman89401822014-05-06 15:04:28 -04001148 break;
1149 default:
1150 ASSERT(false);
1151 }
1152
Alexis Hetu049a1872016-04-25 16:59:58 -04001153 if((postBlendSRGB && state.writeSRGB) || isSRGB(index))
John Bauman89401822014-05-06 15:04:28 -04001154 {
Nicolas Capens4f172c72016-01-13 08:34:30 -05001155 sRGBtoLinear16_12_16(pixel);
John Bauman89401822014-05-06 15:04:28 -04001156 }
Maxime Grégoired9762742015-07-08 16:43:48 -04001157 }
1158
Nicolas Capens4f172c72016-01-13 08:34:30 -05001159 void PixelRoutine::alphaBlend(int index, Pointer<Byte> &cBuffer, Vector4s &current, Int &x)
Maxime Grégoired9762742015-07-08 16:43:48 -04001160 {
1161 if(!state.alphaBlendActive)
1162 {
1163 return;
1164 }
1165
1166 Vector4s pixel;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001167 readPixel(index, cBuffer, x, pixel);
John Bauman89401822014-05-06 15:04:28 -04001168
1169 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
Alexis Hetu96517182015-04-15 10:30:23 -04001170 Vector4s sourceFactor;
1171 Vector4s destFactor;
John Bauman89401822014-05-06 15:04:28 -04001172
Nicolas Capens4f172c72016-01-13 08:34:30 -05001173 blendFactor(sourceFactor, current, pixel, state.sourceBlendFactor);
1174 blendFactor(destFactor, current, pixel, state.destBlendFactor);
John Bauman89401822014-05-06 15:04:28 -04001175
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001176 if(state.sourceBlendFactor != BLEND_ONE && state.sourceBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04001177 {
John Bauman19bac1e2014-05-06 15:23:49 -04001178 current.x = MulHigh(As<UShort4>(current.x), As<UShort4>(sourceFactor.x));
1179 current.y = MulHigh(As<UShort4>(current.y), As<UShort4>(sourceFactor.y));
1180 current.z = MulHigh(As<UShort4>(current.z), As<UShort4>(sourceFactor.z));
John Bauman89401822014-05-06 15:04:28 -04001181 }
Nicolas Capens05b3d662016-02-25 23:58:33 -05001182
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001183 if(state.destBlendFactor != BLEND_ONE && state.destBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04001184 {
John Bauman19bac1e2014-05-06 15:23:49 -04001185 pixel.x = MulHigh(As<UShort4>(pixel.x), As<UShort4>(destFactor.x));
1186 pixel.y = MulHigh(As<UShort4>(pixel.y), As<UShort4>(destFactor.y));
1187 pixel.z = MulHigh(As<UShort4>(pixel.z), As<UShort4>(destFactor.z));
John Bauman89401822014-05-06 15:04:28 -04001188 }
1189
1190 switch(state.blendOperation)
1191 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001192 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04001193 current.x = AddSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
1194 current.y = AddSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
1195 current.z = AddSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04001196 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001197 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04001198 current.x = SubSat(As<UShort4>(current.x), As<UShort4>(pixel.x));
1199 current.y = SubSat(As<UShort4>(current.y), As<UShort4>(pixel.y));
1200 current.z = SubSat(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04001201 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001202 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04001203 current.x = SubSat(As<UShort4>(pixel.x), As<UShort4>(current.x));
1204 current.y = SubSat(As<UShort4>(pixel.y), As<UShort4>(current.y));
1205 current.z = SubSat(As<UShort4>(pixel.z), As<UShort4>(current.z));
John Bauman89401822014-05-06 15:04:28 -04001206 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001207 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04001208 current.x = Min(As<UShort4>(current.x), As<UShort4>(pixel.x));
1209 current.y = Min(As<UShort4>(current.y), As<UShort4>(pixel.y));
1210 current.z = Min(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04001211 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001212 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04001213 current.x = Max(As<UShort4>(current.x), As<UShort4>(pixel.x));
1214 current.y = Max(As<UShort4>(current.y), As<UShort4>(pixel.y));
1215 current.z = Max(As<UShort4>(current.z), As<UShort4>(pixel.z));
John Bauman89401822014-05-06 15:04:28 -04001216 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001217 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04001218 // No operation
1219 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001220 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001221 current.x = pixel.x;
1222 current.y = pixel.y;
1223 current.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04001224 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001225 case BLENDOP_NULL:
Alexis Hetu90c7ad62016-06-27 11:50:40 -04001226 current.x = Short4(0x0000);
1227 current.y = Short4(0x0000);
1228 current.z = Short4(0x0000);
John Bauman89401822014-05-06 15:04:28 -04001229 break;
1230 default:
1231 ASSERT(false);
1232 }
1233
Nicolas Capens4f172c72016-01-13 08:34:30 -05001234 blendFactorAlpha(sourceFactor, current, pixel, state.sourceBlendFactorAlpha);
1235 blendFactorAlpha(destFactor, current, pixel, state.destBlendFactorAlpha);
John Bauman89401822014-05-06 15:04:28 -04001236
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001237 if(state.sourceBlendFactorAlpha != BLEND_ONE && state.sourceBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04001238 {
John Bauman19bac1e2014-05-06 15:23:49 -04001239 current.w = MulHigh(As<UShort4>(current.w), As<UShort4>(sourceFactor.w));
John Bauman89401822014-05-06 15:04:28 -04001240 }
Nicolas Capens05b3d662016-02-25 23:58:33 -05001241
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001242 if(state.destBlendFactorAlpha != BLEND_ONE && state.destBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04001243 {
John Bauman19bac1e2014-05-06 15:23:49 -04001244 pixel.w = MulHigh(As<UShort4>(pixel.w), As<UShort4>(destFactor.w));
John Bauman89401822014-05-06 15:04:28 -04001245 }
1246
1247 switch(state.blendOperationAlpha)
1248 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001249 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04001250 current.w = AddSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001251 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001252 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04001253 current.w = SubSat(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001254 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001255 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04001256 current.w = SubSat(As<UShort4>(pixel.w), As<UShort4>(current.w));
John Bauman89401822014-05-06 15:04:28 -04001257 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001258 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04001259 current.w = Min(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001260 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001261 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04001262 current.w = Max(As<UShort4>(current.w), As<UShort4>(pixel.w));
John Bauman89401822014-05-06 15:04:28 -04001263 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001264 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04001265 // No operation
1266 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001267 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001268 current.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001269 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001270 case BLENDOP_NULL:
Alexis Hetu90c7ad62016-06-27 11:50:40 -04001271 current.w = Short4(0x0000);
John Bauman89401822014-05-06 15:04:28 -04001272 break;
1273 default:
1274 ASSERT(false);
1275 }
1276 }
1277
Nicolas Capens4f172c72016-01-13 08:34:30 -05001278 void PixelRoutine::logicOperation(int index, Pointer<Byte> &cBuffer, Vector4s &current, Int &x)
Maxime Grégoired9762742015-07-08 16:43:48 -04001279 {
Nicolas Capens2afcc802015-08-04 10:34:43 -04001280 if(state.logicalOperation == LOGICALOP_COPY)
Maxime Grégoired9762742015-07-08 16:43:48 -04001281 {
1282 return;
1283 }
1284
1285 Vector4s pixel;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001286 readPixel(index, cBuffer, x, pixel);
Maxime Grégoired9762742015-07-08 16:43:48 -04001287
1288 switch(state.logicalOperation)
1289 {
1290 case LOGICALOP_CLEAR:
Alexis Hetu90c7ad62016-06-27 11:50:40 -04001291 current.x = UShort4(0);
1292 current.y = UShort4(0);
1293 current.z = UShort4(0);
Maxime Grégoired9762742015-07-08 16:43:48 -04001294 break;
1295 case LOGICALOP_SET:
Alexis Hetu90c7ad62016-06-27 11:50:40 -04001296 current.x = UShort4(0xFFFFu);
1297 current.y = UShort4(0xFFFFu);
1298 current.z = UShort4(0xFFFFu);
Maxime Grégoired9762742015-07-08 16:43:48 -04001299 break;
1300 case LOGICALOP_COPY:
1301 ASSERT(false); // Optimized out
1302 break;
1303 case LOGICALOP_COPY_INVERTED:
1304 current.x = ~current.x;
1305 current.y = ~current.y;
1306 current.z = ~current.z;
1307 break;
1308 case LOGICALOP_NOOP:
1309 current.x = pixel.x;
1310 current.y = pixel.y;
1311 current.z = pixel.z;
1312 break;
1313 case LOGICALOP_INVERT:
1314 current.x = ~pixel.x;
1315 current.y = ~pixel.y;
1316 current.z = ~pixel.z;
1317 break;
1318 case LOGICALOP_AND:
1319 current.x = pixel.x & current.x;
1320 current.y = pixel.y & current.y;
1321 current.z = pixel.z & current.z;
1322 break;
1323 case LOGICALOP_NAND:
1324 current.x = ~(pixel.x & current.x);
1325 current.y = ~(pixel.y & current.y);
1326 current.z = ~(pixel.z & current.z);
1327 break;
1328 case LOGICALOP_OR:
1329 current.x = pixel.x | current.x;
1330 current.y = pixel.y | current.y;
1331 current.z = pixel.z | current.z;
1332 break;
1333 case LOGICALOP_NOR:
1334 current.x = ~(pixel.x | current.x);
1335 current.y = ~(pixel.y | current.y);
1336 current.z = ~(pixel.z | current.z);
1337 break;
1338 case LOGICALOP_XOR:
1339 current.x = pixel.x ^ current.x;
1340 current.y = pixel.y ^ current.y;
1341 current.z = pixel.z ^ current.z;
1342 break;
1343 case LOGICALOP_EQUIV:
1344 current.x = ~(pixel.x ^ current.x);
1345 current.y = ~(pixel.y ^ current.y);
1346 current.z = ~(pixel.z ^ current.z);
1347 break;
1348 case LOGICALOP_AND_REVERSE:
1349 current.x = ~pixel.x & current.x;
1350 current.y = ~pixel.y & current.y;
1351 current.z = ~pixel.z & current.z;
1352 break;
1353 case LOGICALOP_AND_INVERTED:
1354 current.x = pixel.x & ~current.x;
1355 current.y = pixel.y & ~current.y;
1356 current.z = pixel.z & ~current.z;
1357 break;
1358 case LOGICALOP_OR_REVERSE:
1359 current.x = ~pixel.x | current.x;
1360 current.y = ~pixel.y | current.y;
1361 current.z = ~pixel.z | current.z;
1362 break;
1363 case LOGICALOP_OR_INVERTED:
1364 current.x = pixel.x | ~current.x;
1365 current.y = pixel.y | ~current.y;
1366 current.z = pixel.z | ~current.z;
1367 break;
1368 default:
1369 ASSERT(false);
1370 }
1371 }
1372
Nicolas Capens4f172c72016-01-13 08:34:30 -05001373 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 -04001374 {
Alexis Hetu049a1872016-04-25 16:59:58 -04001375 if((postBlendSRGB && state.writeSRGB) || isSRGB(index))
John Bauman89401822014-05-06 15:04:28 -04001376 {
Nicolas Capens4f172c72016-01-13 08:34:30 -05001377 linearToSRGB16_12_16(current);
John Bauman89401822014-05-06 15:04:28 -04001378 }
1379
1380 if(exactColorRounding)
1381 {
1382 switch(state.targetFormat[index])
1383 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001384 case FORMAT_R5G6B5:
Nicolas Capens26f37222015-09-22 09:53:45 -04001385 current.x = AddSat(As<UShort4>(current.x), UShort4(0x0400));
1386 current.y = AddSat(As<UShort4>(current.y), UShort4(0x0200));
1387 current.z = AddSat(As<UShort4>(current.z), UShort4(0x0400));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001388 break;
John Bauman89401822014-05-06 15:04:28 -04001389 case FORMAT_X8G8R8B8Q:
1390 case FORMAT_A8G8R8B8Q:
1391 case FORMAT_X8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001392 case FORMAT_X8B8G8R8:
John Bauman89401822014-05-06 15:04:28 -04001393 case FORMAT_A8R8G8B8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001394 case FORMAT_A8B8G8R8:
Alexis Hetu049a1872016-04-25 16:59:58 -04001395 case FORMAT_SRGB8_X8:
1396 case FORMAT_SRGB8_A8:
Alexis Hetu143dfc72016-09-13 18:41:27 -04001397 case FORMAT_G8R8:
Alexis Hetu0cff3cb2016-05-04 16:23:50 -04001398 case FORMAT_R8:
Alexis Hetu90c7ad62016-06-27 11:50:40 -04001399 current.x = current.x - As<Short4>(As<UShort4>(current.x) >> 8) + Short4(0x0080);
1400 current.y = current.y - As<Short4>(As<UShort4>(current.y) >> 8) + Short4(0x0080);
1401 current.z = current.z - As<Short4>(As<UShort4>(current.z) >> 8) + Short4(0x0080);
1402 current.w = current.w - As<Short4>(As<UShort4>(current.w) >> 8) + Short4(0x0080);
John Bauman89401822014-05-06 15:04:28 -04001403 break;
Nicolas Capensb69aa272016-01-02 00:06:41 -05001404 default:
1405 break;
John Bauman89401822014-05-06 15:04:28 -04001406 }
1407 }
1408
1409 int rgbaWriteMask = state.colorWriteActive(index);
Nicolas Capens3b396462016-01-02 00:23:53 -05001410 int bgraWriteMask = (rgbaWriteMask & 0x0000000A) | (rgbaWriteMask & 0x00000001) << 2 | (rgbaWriteMask & 0x00000004) >> 2;
John Bauman89401822014-05-06 15:04:28 -04001411
1412 switch(state.targetFormat[index])
1413 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001414 case FORMAT_R5G6B5:
1415 {
1416 current.x = current.x & Short4(0xF800u);
1417 current.y = As<UShort4>(current.y & Short4(0xFC00u)) >> 5;
1418 current.z = As<UShort4>(current.z) >> 11;
1419
1420 current.x = current.x | current.y | current.z;
1421 }
1422 break;
John Bauman89401822014-05-06 15:04:28 -04001423 case FORMAT_X8G8R8B8Q:
1424 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04001425 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1426 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1427 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04001428
John Bauman19bac1e2014-05-06 15:23:49 -04001429 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
1430 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
John Bauman89401822014-05-06 15:04:28 -04001431 break;
1432 case FORMAT_A8G8R8B8Q:
1433 UNIMPLEMENTED();
John Bauman19bac1e2014-05-06 15:23:49 -04001434 // current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1435 // current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1436 // current.z = As<Short4>(As<UShort4>(current.z) >> 8);
1437 // current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04001438
John Bauman19bac1e2014-05-06 15:23:49 -04001439 // current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
1440 // current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04001441 break;
1442 case FORMAT_X8R8G8B8:
1443 case FORMAT_A8R8G8B8:
1444 if(state.targetFormat[index] == FORMAT_X8R8G8B8 || rgbaWriteMask == 0x7)
1445 {
John Bauman19bac1e2014-05-06 15:23:49 -04001446 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1447 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1448 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
John Bauman89401822014-05-06 15:04:28 -04001449
John Bauman19bac1e2014-05-06 15:23:49 -04001450 current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
1451 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
John Bauman89401822014-05-06 15:04:28 -04001452
John Bauman19bac1e2014-05-06 15:23:49 -04001453 current.x = current.z;
1454 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
1455 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
1456 current.y = current.z;
1457 current.z = As<Short4>(UnpackLow(current.z, current.x));
1458 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04001459 }
1460 else
1461 {
John Bauman19bac1e2014-05-06 15:23:49 -04001462 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1463 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1464 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
1465 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
John Bauman89401822014-05-06 15:04:28 -04001466
John Bauman19bac1e2014-05-06 15:23:49 -04001467 current.z = As<Short4>(Pack(As<UShort4>(current.z), As<UShort4>(current.x)));
1468 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
John Bauman89401822014-05-06 15:04:28 -04001469
John Bauman19bac1e2014-05-06 15:23:49 -04001470 current.x = current.z;
1471 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
1472 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
1473 current.y = current.z;
1474 current.z = As<Short4>(UnpackLow(current.z, current.x));
1475 current.y = As<Short4>(UnpackHigh(current.y, current.x));
John Bauman89401822014-05-06 15:04:28 -04001476 }
1477 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001478 case FORMAT_X8B8G8R8:
1479 case FORMAT_A8B8G8R8:
Alexis Hetu049a1872016-04-25 16:59:58 -04001480 case FORMAT_SRGB8_X8:
1481 case FORMAT_SRGB8_A8:
1482 if(state.targetFormat[index] == FORMAT_X8B8G8R8 || state.targetFormat[index] == FORMAT_SRGB8_X8 || rgbaWriteMask == 0x7)
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001483 {
1484 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1485 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1486 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
1487
1488 current.z = As<Short4>(Pack(As<UShort4>(current.x), As<UShort4>(current.z)));
1489 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
1490
1491 current.x = current.z;
1492 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
1493 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
1494 current.y = current.z;
1495 current.z = As<Short4>(UnpackLow(current.z, current.x));
1496 current.y = As<Short4>(UnpackHigh(current.y, current.x));
1497 }
1498 else
1499 {
1500 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1501 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1502 current.z = As<Short4>(As<UShort4>(current.z) >> 8);
1503 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
1504
1505 current.z = As<Short4>(Pack(As<UShort4>(current.x), As<UShort4>(current.z)));
1506 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.w)));
1507
1508 current.x = current.z;
1509 current.z = UnpackLow(As<Byte8>(current.z), As<Byte8>(current.y));
1510 current.x = UnpackHigh(As<Byte8>(current.x), As<Byte8>(current.y));
1511 current.y = current.z;
1512 current.z = As<Short4>(UnpackLow(current.z, current.x));
1513 current.y = As<Short4>(UnpackHigh(current.y, current.x));
1514 }
1515 break;
Alexis Hetu143dfc72016-09-13 18:41:27 -04001516 case FORMAT_G8R8:
1517 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1518 current.y = As<Short4>(As<UShort4>(current.y) >> 8);
1519 current.x = As<Short4>(Pack(As<UShort4>(current.x), As<UShort4>(current.x)));
1520 current.y = As<Short4>(Pack(As<UShort4>(current.y), As<UShort4>(current.y)));
1521 current.x = UnpackLow(As<Byte8>(current.x), As<Byte8>(current.y));
1522 break;
Alexis Hetu0cff3cb2016-05-04 16:23:50 -04001523 case FORMAT_R8:
1524 current.x = As<Short4>(As<UShort4>(current.x) >> 8);
1525 current.x = As<Short4>(Pack(As<UShort4>(current.x), As<UShort4>(current.x)));
1526 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001527 case FORMAT_A8:
1528 current.w = As<Short4>(As<UShort4>(current.w) >> 8);
1529 current.w = As<Short4>(Pack(As<UShort4>(current.w), As<UShort4>(current.w)));
1530 break;
John Bauman89401822014-05-06 15:04:28 -04001531 case FORMAT_G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04001532 current.z = current.x;
1533 current.x = As<Short4>(UnpackLow(current.x, current.y));
1534 current.z = As<Short4>(UnpackHigh(current.z, current.y));
1535 current.y = current.z;
John Bauman89401822014-05-06 15:04:28 -04001536 break;
1537 case FORMAT_A16B16G16R16:
John Bauman19bac1e2014-05-06 15:23:49 -04001538 transpose4x4(current.x, current.y, current.z, current.w);
John Bauman89401822014-05-06 15:04:28 -04001539 break;
John Bauman89401822014-05-06 15:04:28 -04001540 default:
1541 ASSERT(false);
1542 }
1543
John Bauman19bac1e2014-05-06 15:23:49 -04001544 Short4 c01 = current.z;
1545 Short4 c23 = current.y;
John Bauman89401822014-05-06 15:04:28 -04001546
1547 Int xMask; // Combination of all masks
1548
1549 if(state.depthTestActive)
1550 {
1551 xMask = zMask;
1552 }
1553 else
1554 {
1555 xMask = cMask;
1556 }
1557
1558 if(state.stencilActive)
1559 {
1560 xMask &= sMask;
1561 }
1562
John Bauman89401822014-05-06 15:04:28 -04001563 switch(state.targetFormat[index])
1564 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001565 case FORMAT_R5G6B5:
1566 {
1567 Pointer<Byte> buffer = cBuffer + 2 * x;
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001568 Int value = *Pointer<Int>(buffer);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001569
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001570 Int c01 = Extract(As<Int2>(current.x), 0);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001571
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001572 if((bgraWriteMask & 0x00000007) != 0x00000007)
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001573 {
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001574 Int masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001575 c01 &= *Pointer<Int>(constants + OFFSET(Constants,mask565Q[bgraWriteMask & 0x7][0]));
1576 masked &= *Pointer<Int>(constants + OFFSET(Constants,invMask565Q[bgraWriteMask & 0x7][0]));
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001577 c01 |= masked;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001578 }
1579
Nicolas Capens4f172c72016-01-13 08:34:30 -05001580 c01 &= *Pointer<Int>(constants + OFFSET(Constants,maskW4Q[0][0]) + xMask * 8);
1581 value &= *Pointer<Int>(constants + OFFSET(Constants,invMaskW4Q[0][0]) + xMask * 8);
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001582 c01 |= value;
1583 *Pointer<Int>(buffer) = c01;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001584
Nicolas Capens4f172c72016-01-13 08:34:30 -05001585 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001586 value = *Pointer<Int>(buffer);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001587
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001588 Int c23 = Extract(As<Int2>(current.x), 1);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001589
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001590 if((bgraWriteMask & 0x00000007) != 0x00000007)
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001591 {
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001592 Int masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001593 c23 &= *Pointer<Int>(constants + OFFSET(Constants,mask565Q[bgraWriteMask & 0x7][0]));
1594 masked &= *Pointer<Int>(constants + OFFSET(Constants,invMask565Q[bgraWriteMask & 0x7][0]));
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001595 c23 |= masked;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001596 }
1597
Nicolas Capens4f172c72016-01-13 08:34:30 -05001598 c23 &= *Pointer<Int>(constants + OFFSET(Constants,maskW4Q[0][2]) + xMask * 8);
1599 value &= *Pointer<Int>(constants + OFFSET(Constants,invMaskW4Q[0][2]) + xMask * 8);
Nicolas Capens9919b6c2015-05-26 01:11:26 -04001600 c23 |= value;
1601 *Pointer<Int>(buffer) = c23;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001602 }
1603 break;
John Bauman89401822014-05-06 15:04:28 -04001604 case FORMAT_A8G8R8B8Q:
1605 case FORMAT_X8G8R8B8Q: // FIXME: Don't touch alpha?
1606 UNIMPLEMENTED();
1607 // value = *Pointer<Short4>(cBuffer + 8 * x + 0);
1608
1609 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
1610 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
1611 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
1612 // {
1613 // Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001614 // c01 &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
1615 // masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04001616 // c01 |= masked;
1617 // }
1618
Nicolas Capens4f172c72016-01-13 08:34:30 -05001619 // c01 &= *Pointer<Short4>(constants + OFFSET(Constants,maskD01Q) + xMask * 8);
1620 // value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04001621 // c01 |= value;
1622 // *Pointer<Short4>(cBuffer + 8 * x + 0) = c01;
1623
1624 // value = *Pointer<Short4>(cBuffer + 8 * x + 8);
1625
1626 // if((state.targetFormat[index] == FORMAT_A8G8R8B8Q && bgraWriteMask != 0x0000000F) ||
1627 // ((state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x00000007) &&
1628 // (state.targetFormat[index] == FORMAT_X8G8R8B8Q && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
1629 // {
1630 // Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001631 // c23 &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
1632 // masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
John Bauman89401822014-05-06 15:04:28 -04001633 // c23 |= masked;
1634 // }
1635
Nicolas Capens4f172c72016-01-13 08:34:30 -05001636 // c23 &= *Pointer<Short4>(constants + OFFSET(Constants,maskD23Q) + xMask * 8);
1637 // value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
John Bauman89401822014-05-06 15:04:28 -04001638 // c23 |= value;
1639 // *Pointer<Short4>(cBuffer + 8 * x + 8) = c23;
1640 break;
1641 case FORMAT_A8R8G8B8:
1642 case FORMAT_X8R8G8B8: // FIXME: Don't touch alpha?
John Bauman89401822014-05-06 15:04:28 -04001643 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001644 Pointer<Byte> buffer = cBuffer + x * 4;
1645 Short4 value = *Pointer<Short4>(buffer);
1646
1647 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
1648 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
1649 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
1650 {
1651 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001652 c01 &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
1653 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001654 c01 |= masked;
1655 }
1656
Nicolas Capens4f172c72016-01-13 08:34:30 -05001657 c01 &= *Pointer<Short4>(constants + OFFSET(Constants,maskD01Q) + xMask * 8);
1658 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001659 c01 |= value;
1660 *Pointer<Short4>(buffer) = c01;
1661
Nicolas Capens4f172c72016-01-13 08:34:30 -05001662 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001663 value = *Pointer<Short4>(buffer);
1664
1665 if((state.targetFormat[index] == FORMAT_A8R8G8B8 && bgraWriteMask != 0x0000000F) ||
1666 ((state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x00000007) &&
1667 (state.targetFormat[index] == FORMAT_X8R8G8B8 && bgraWriteMask != 0x0000000F))) // FIXME: Need for masking when XRGB && Fh?
1668 {
1669 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001670 c23 &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q[bgraWriteMask][0]));
1671 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q[bgraWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001672 c23 |= masked;
1673 }
1674
Nicolas Capens4f172c72016-01-13 08:34:30 -05001675 c23 &= *Pointer<Short4>(constants + OFFSET(Constants,maskD23Q) + xMask * 8);
1676 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001677 c23 |= value;
1678 *Pointer<Short4>(buffer) = c23;
John Bauman89401822014-05-06 15:04:28 -04001679 }
John Bauman89401822014-05-06 15:04:28 -04001680 break;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001681 case FORMAT_A8B8G8R8:
1682 case FORMAT_X8B8G8R8: // FIXME: Don't touch alpha?
Alexis Hetu049a1872016-04-25 16:59:58 -04001683 case FORMAT_SRGB8_X8:
1684 case FORMAT_SRGB8_A8:
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001685 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001686 Pointer<Byte> buffer = cBuffer + x * 4;
1687 Short4 value = *Pointer<Short4>(buffer);
1688
Alexis Hetu049a1872016-04-25 16:59:58 -04001689 bool masked = (((state.targetFormat[index] == FORMAT_A8B8G8R8 || state.targetFormat[index] == FORMAT_SRGB8_A8) && rgbaWriteMask != 0x0000000F) ||
1690 (((state.targetFormat[index] == FORMAT_X8B8G8R8 || state.targetFormat[index] == FORMAT_SRGB8_X8) && rgbaWriteMask != 0x00000007) &&
1691 ((state.targetFormat[index] == FORMAT_X8B8G8R8 || state.targetFormat[index] == FORMAT_SRGB8_X8) && rgbaWriteMask != 0x0000000F))); // FIXME: Need for masking when XBGR && Fh?
1692
1693 if(masked)
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001694 {
1695 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001696 c01 &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q[rgbaWriteMask][0]));
1697 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q[rgbaWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001698 c01 |= masked;
1699 }
1700
Nicolas Capens4f172c72016-01-13 08:34:30 -05001701 c01 &= *Pointer<Short4>(constants + OFFSET(Constants,maskD01Q) + xMask * 8);
1702 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001703 c01 |= value;
1704 *Pointer<Short4>(buffer) = c01;
1705
Nicolas Capens4f172c72016-01-13 08:34:30 -05001706 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001707 value = *Pointer<Short4>(buffer);
1708
Alexis Hetu049a1872016-04-25 16:59:58 -04001709 if(masked)
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001710 {
1711 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001712 c23 &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q[rgbaWriteMask][0]));
1713 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q[rgbaWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001714 c23 |= masked;
1715 }
1716
Nicolas Capens4f172c72016-01-13 08:34:30 -05001717 c23 &= *Pointer<Short4>(constants + OFFSET(Constants,maskD23Q) + xMask * 8);
1718 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001719 c23 |= value;
1720 *Pointer<Short4>(buffer) = c23;
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001721 }
Nicolas Capens0c42ee12015-03-28 18:54:07 -04001722 break;
Alexis Hetu143dfc72016-09-13 18:41:27 -04001723 case FORMAT_G8R8:
1724 if((rgbaWriteMask & 0x00000003) != 0x0)
1725 {
1726 Pointer<Byte> buffer = cBuffer + 2 * x;
1727 Int2 value;
1728 value = Insert(value, *Pointer<Int>(buffer), 0);
1729 Int pitch = *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
1730 value = Insert(value, *Pointer<Int>(buffer + pitch), 1);
1731
1732 Int2 packedCol = As<Int2>(current.x);
1733
1734 UInt2 mergedMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskW4Q) + xMask * 8);
1735 if((rgbaWriteMask & 0x3) != 0x3)
1736 {
1737 Int tmpMask = *Pointer<Int>(constants + OFFSET(Constants, maskB4Q[5 * (rgbaWriteMask & 0x3)][0]));
1738 UInt2 rgbaMask = As<UInt2>(Int2(tmpMask, tmpMask));
1739 mergedMask &= rgbaMask;
1740 }
1741
1742 packedCol = As<Int2>((As<UInt2>(packedCol) & mergedMask) | (As<UInt2>(value) & ~mergedMask));
1743
1744 *Pointer<UInt>(buffer) = As<UInt>(Extract(packedCol, 0));
1745 *Pointer<UInt>(buffer + pitch) = As<UInt>(Extract(packedCol, 1));
1746 }
1747 break;
Alexis Hetu0cff3cb2016-05-04 16:23:50 -04001748 case FORMAT_R8:
1749 if(rgbaWriteMask & 0x00000001)
1750 {
1751 Pointer<Byte> buffer = cBuffer + 1 * x;
1752 Short4 value;
Alexis Hetub14ed802016-06-15 13:06:50 -04001753 value = Insert(value, *Pointer<Short>(buffer), 0);
Alexis Hetu0cff3cb2016-05-04 16:23:50 -04001754 Int pitch = *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
Alexis Hetub14ed802016-06-15 13:06:50 -04001755 value = Insert(value, *Pointer<Short>(buffer + pitch), 1);
Alexis Hetu0cff3cb2016-05-04 16:23:50 -04001756 value = UnpackLow(As<Byte8>(value), As<Byte8>(value));
1757
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 value = UnpackLow(As<Byte8>(value), As<Byte8>(value));
1775
Nicolas Capens4f172c72016-01-13 08:34:30 -05001776 current.w &= *Pointer<Short4>(constants + OFFSET(Constants,maskB4Q) + 8 * xMask);
1777 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskB4Q) + 8 * xMask);
John Bauman66b8ab22014-05-06 15:57:45 -04001778 current.w |= value;
1779
1780 *Pointer<Short>(buffer) = Extract(current.w, 0);
1781 *Pointer<Short>(buffer + pitch) = Extract(current.w, 1);
1782 }
1783 break;
John Bauman89401822014-05-06 15:04:28 -04001784 case FORMAT_G16R16:
John Bauman89401822014-05-06 15:04:28 -04001785 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001786 Pointer<Byte> buffer = cBuffer + 4 * x;
John Bauman89401822014-05-06 15:04:28 -04001787
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001788 Short4 value = *Pointer<Short4>(buffer);
John Bauman89401822014-05-06 15:04:28 -04001789
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001790 if((rgbaWriteMask & 0x00000003) != 0x00000003)
John Bauman89401822014-05-06 15:04:28 -04001791 {
1792 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001793 current.x &= *Pointer<Short4>(constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
1794 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04001795 current.x |= masked;
John Bauman89401822014-05-06 15:04:28 -04001796 }
1797
Nicolas Capens4f172c72016-01-13 08:34:30 -05001798 current.x &= *Pointer<Short4>(constants + OFFSET(Constants,maskD01Q) + xMask * 8);
1799 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD01Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04001800 current.x |= value;
1801 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04001802
Nicolas Capens4f172c72016-01-13 08:34:30 -05001803 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04001804
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001805 value = *Pointer<Short4>(buffer);
1806
1807 if((rgbaWriteMask & 0x00000003) != 0x00000003)
John Bauman89401822014-05-06 15:04:28 -04001808 {
1809 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001810 current.y &= *Pointer<Short4>(constants + OFFSET(Constants,maskW01Q[rgbaWriteMask & 0x3][0]));
1811 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskW01Q[rgbaWriteMask & 0x3][0]));
John Bauman19bac1e2014-05-06 15:23:49 -04001812 current.y |= masked;
John Bauman89401822014-05-06 15:04:28 -04001813 }
1814
Nicolas Capens4f172c72016-01-13 08:34:30 -05001815 current.y &= *Pointer<Short4>(constants + OFFSET(Constants,maskD23Q) + xMask * 8);
1816 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskD23Q) + xMask * 8);
John Bauman19bac1e2014-05-06 15:23:49 -04001817 current.y |= value;
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001818 *Pointer<Short4>(buffer) = current.y;
John Bauman89401822014-05-06 15:04:28 -04001819 }
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001820 break;
1821 case FORMAT_A16B16G16R16:
John Bauman89401822014-05-06 15:04:28 -04001822 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001823 Pointer<Byte> buffer = cBuffer + 8 * x;
John Bauman89401822014-05-06 15:04:28 -04001824
John Bauman89401822014-05-06 15:04:28 -04001825 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001826 Short4 value = *Pointer<Short4>(buffer);
1827
1828 if(rgbaWriteMask != 0x0000000F)
1829 {
1830 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001831 current.x &= *Pointer<Short4>(constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
1832 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001833 current.x |= masked;
1834 }
1835
Nicolas Capens4f172c72016-01-13 08:34:30 -05001836 current.x &= *Pointer<Short4>(constants + OFFSET(Constants,maskQ0Q) + xMask * 8);
1837 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskQ0Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001838 current.x |= value;
1839 *Pointer<Short4>(buffer) = current.x;
John Bauman89401822014-05-06 15:04:28 -04001840 }
1841
John Bauman89401822014-05-06 15:04:28 -04001842 {
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001843 Short4 value = *Pointer<Short4>(buffer + 8);
1844
1845 if(rgbaWriteMask != 0x0000000F)
1846 {
1847 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001848 current.y &= *Pointer<Short4>(constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
1849 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001850 current.y |= masked;
1851 }
1852
Nicolas Capens4f172c72016-01-13 08:34:30 -05001853 current.y &= *Pointer<Short4>(constants + OFFSET(Constants,maskQ1Q) + xMask * 8);
1854 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskQ1Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001855 current.y |= value;
1856 *Pointer<Short4>(buffer + 8) = current.y;
John Bauman89401822014-05-06 15:04:28 -04001857 }
1858
Nicolas Capens4f172c72016-01-13 08:34:30 -05001859 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001860
1861 {
1862 Short4 value = *Pointer<Short4>(buffer);
1863
1864 if(rgbaWriteMask != 0x0000000F)
1865 {
1866 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001867 current.z &= *Pointer<Short4>(constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
1868 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001869 current.z |= masked;
1870 }
1871
Nicolas Capens4f172c72016-01-13 08:34:30 -05001872 current.z &= *Pointer<Short4>(constants + OFFSET(Constants,maskQ2Q) + xMask * 8);
1873 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskQ2Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001874 current.z |= value;
1875 *Pointer<Short4>(buffer) = current.z;
1876 }
1877
1878 {
1879 Short4 value = *Pointer<Short4>(buffer + 8);
1880
1881 if(rgbaWriteMask != 0x0000000F)
1882 {
1883 Short4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001884 current.w &= *Pointer<Short4>(constants + OFFSET(Constants,maskW4Q[rgbaWriteMask][0]));
1885 masked &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskW4Q[rgbaWriteMask][0]));
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001886 current.w |= masked;
1887 }
1888
Nicolas Capens4f172c72016-01-13 08:34:30 -05001889 current.w &= *Pointer<Short4>(constants + OFFSET(Constants,maskQ3Q) + xMask * 8);
1890 value &= *Pointer<Short4>(constants + OFFSET(Constants,invMaskQ3Q) + xMask * 8);
Nicolas Capensd5f0a6c2015-05-26 00:18:01 -04001891 current.w |= value;
1892 *Pointer<Short4>(buffer + 8) = current.w;
1893 }
John Bauman89401822014-05-06 15:04:28 -04001894 }
1895 break;
1896 default:
1897 ASSERT(false);
1898 }
1899 }
1900
Nicolas Capens96d4e092016-11-18 14:22:38 -05001901 void PixelRoutine::blendFactor(Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, BlendFactor blendFactorActive)
John Bauman89401822014-05-06 15:04:28 -04001902 {
1903 switch(blendFactorActive)
1904 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001905 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04001906 // Optimized
1907 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001908 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04001909 // Optimized
1910 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001911 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04001912 blendFactor.x = oC.x;
1913 blendFactor.y = oC.y;
1914 blendFactor.z = oC.z;
John Bauman89401822014-05-06 15:04:28 -04001915 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001916 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04001917 blendFactor.x = Float4(1.0f) - oC.x;
1918 blendFactor.y = Float4(1.0f) - oC.y;
1919 blendFactor.z = Float4(1.0f) - oC.z;
John Bauman89401822014-05-06 15:04:28 -04001920 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001921 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001922 blendFactor.x = pixel.x;
1923 blendFactor.y = pixel.y;
1924 blendFactor.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04001925 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001926 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001927 blendFactor.x = Float4(1.0f) - pixel.x;
1928 blendFactor.y = Float4(1.0f) - pixel.y;
1929 blendFactor.z = Float4(1.0f) - pixel.z;
John Bauman89401822014-05-06 15:04:28 -04001930 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001931 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001932 blendFactor.x = oC.w;
1933 blendFactor.y = oC.w;
1934 blendFactor.z = oC.w;
John Bauman89401822014-05-06 15:04:28 -04001935 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001936 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001937 blendFactor.x = Float4(1.0f) - oC.w;
1938 blendFactor.y = Float4(1.0f) - oC.w;
1939 blendFactor.z = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04001940 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001941 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001942 blendFactor.x = pixel.w;
1943 blendFactor.y = pixel.w;
1944 blendFactor.z = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001945 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001946 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001947 blendFactor.x = Float4(1.0f) - pixel.w;
1948 blendFactor.y = Float4(1.0f) - pixel.w;
1949 blendFactor.z = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001950 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001951 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04001952 blendFactor.x = Float4(1.0f) - pixel.w;
1953 blendFactor.x = Min(blendFactor.x, oC.w);
1954 blendFactor.y = blendFactor.x;
1955 blendFactor.z = blendFactor.x;
John Bauman89401822014-05-06 15:04:28 -04001956 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001957 case BLEND_CONSTANT:
Nicolas Capens4f172c72016-01-13 08:34:30 -05001958 blendFactor.x = *Pointer<Float4>(data + OFFSET(DrawData,factor.blendConstant4F[0]));
1959 blendFactor.y = *Pointer<Float4>(data + OFFSET(DrawData,factor.blendConstant4F[1]));
1960 blendFactor.z = *Pointer<Float4>(data + OFFSET(DrawData,factor.blendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04001961 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001962 case BLEND_INVCONSTANT:
Nicolas Capens4f172c72016-01-13 08:34:30 -05001963 blendFactor.x = *Pointer<Float4>(data + OFFSET(DrawData,factor.invBlendConstant4F[0]));
1964 blendFactor.y = *Pointer<Float4>(data + OFFSET(DrawData,factor.invBlendConstant4F[1]));
1965 blendFactor.z = *Pointer<Float4>(data + OFFSET(DrawData,factor.invBlendConstant4F[2]));
John Bauman89401822014-05-06 15:04:28 -04001966 break;
1967 default:
1968 ASSERT(false);
1969 }
1970 }
1971
Nicolas Capens96d4e092016-11-18 14:22:38 -05001972 void PixelRoutine::blendFactorAlpha(Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, BlendFactor blendFactorAlphaActive)
John Bauman89401822014-05-06 15:04:28 -04001973 {
1974 switch(blendFactorAlphaActive)
1975 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001976 case BLEND_ZERO:
John Bauman89401822014-05-06 15:04:28 -04001977 // Optimized
1978 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001979 case BLEND_ONE:
John Bauman89401822014-05-06 15:04:28 -04001980 // Optimized
1981 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001982 case BLEND_SOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04001983 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04001984 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001985 case BLEND_INVSOURCE:
John Bauman19bac1e2014-05-06 15:23:49 -04001986 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04001987 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001988 case BLEND_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001989 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001990 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001991 case BLEND_INVDEST:
John Bauman19bac1e2014-05-06 15:23:49 -04001992 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04001993 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001994 case BLEND_SOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001995 blendFactor.w = oC.w;
John Bauman89401822014-05-06 15:04:28 -04001996 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001997 case BLEND_INVSOURCEALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04001998 blendFactor.w = Float4(1.0f) - oC.w;
John Bauman89401822014-05-06 15:04:28 -04001999 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002000 case BLEND_DESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002001 blendFactor.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002002 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002003 case BLEND_INVDESTALPHA:
John Bauman19bac1e2014-05-06 15:23:49 -04002004 blendFactor.w = Float4(1.0f) - pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002005 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002006 case BLEND_SRCALPHASAT:
John Bauman19bac1e2014-05-06 15:23:49 -04002007 blendFactor.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04002008 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002009 case BLEND_CONSTANT:
Nicolas Capens4f172c72016-01-13 08:34:30 -05002010 blendFactor.w = *Pointer<Float4>(data + OFFSET(DrawData,factor.blendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04002011 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002012 case BLEND_INVCONSTANT:
Nicolas Capens4f172c72016-01-13 08:34:30 -05002013 blendFactor.w = *Pointer<Float4>(data + OFFSET(DrawData,factor.invBlendConstant4F[3]));
John Bauman89401822014-05-06 15:04:28 -04002014 break;
2015 default:
2016 ASSERT(false);
2017 }
2018 }
2019
Nicolas Capens4f172c72016-01-13 08:34:30 -05002020 void PixelRoutine::alphaBlend(int index, Pointer<Byte> &cBuffer, Vector4f &oC, Int &x)
John Bauman89401822014-05-06 15:04:28 -04002021 {
2022 if(!state.alphaBlendActive)
2023 {
2024 return;
2025 }
2026
2027 Pointer<Byte> buffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002028 Vector4f pixel;
John Bauman89401822014-05-06 15:04:28 -04002029
Alexis Hetu96517182015-04-15 10:30:23 -04002030 Vector4s color;
John Bauman89401822014-05-06 15:04:28 -04002031 Short4 c01;
2032 Short4 c23;
2033
Alexis Hetu1abb6382016-02-08 11:21:16 -05002034 Float4 one;
Alexis Hetu7208e932016-06-02 11:19:24 -04002035 if(Surface::isFloatFormat(state.targetFormat[index]))
John Bauman89401822014-05-06 15:04:28 -04002036 {
Alexis Hetu1abb6382016-02-08 11:21:16 -05002037 one = Float4(1.0f);
Alexis Hetu7208e932016-06-02 11:19:24 -04002038 }
2039 else if(Surface::isNonNormalizedInteger(state.targetFormat[index]))
2040 {
2041 one = As<Float4>(Surface::isUnsignedComponent(state.targetFormat[index], 0) ? Int4(0xFFFFFFFF) : Int4(0x7FFFFFFF));
Alexis Hetu1abb6382016-02-08 11:21:16 -05002042 }
2043
2044 switch(state.targetFormat[index])
2045 {
2046 case FORMAT_R32I:
2047 case FORMAT_R32UI:
John Bauman89401822014-05-06 15:04:28 -04002048 case FORMAT_R32F:
2049 buffer = cBuffer;
2050 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04002051 pixel.x.x = *Pointer<Float>(buffer + 4 * x + 0);
2052 pixel.x.y = *Pointer<Float>(buffer + 4 * x + 4);
Nicolas Capens4f172c72016-01-13 08:34:30 -05002053 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04002054 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04002055 pixel.x.z = *Pointer<Float>(buffer + 4 * x + 0);
2056 pixel.x.w = *Pointer<Float>(buffer + 4 * x + 4);
Alexis Hetu1abb6382016-02-08 11:21:16 -05002057 pixel.y = pixel.z = pixel.w = one;
John Bauman89401822014-05-06 15:04:28 -04002058 break;
Alexis Hetu1abb6382016-02-08 11:21:16 -05002059 case FORMAT_G32R32I:
2060 case FORMAT_G32R32UI:
John Bauman89401822014-05-06 15:04:28 -04002061 case FORMAT_G32R32F:
2062 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002063 pixel.x = *Pointer<Float4>(buffer + 8 * x, 16);
Nicolas Capens4f172c72016-01-13 08:34:30 -05002064 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002065 pixel.y = *Pointer<Float4>(buffer + 8 * x, 16);
2066 pixel.z = pixel.x;
2067 pixel.x = ShuffleLowHigh(pixel.x, pixel.y, 0x88);
2068 pixel.z = ShuffleLowHigh(pixel.z, pixel.y, 0xDD);
2069 pixel.y = pixel.z;
Alexis Hetu1abb6382016-02-08 11:21:16 -05002070 pixel.z = pixel.w = one;
John Bauman89401822014-05-06 15:04:28 -04002071 break;
Alexis Hetudbd1a8e2016-04-13 11:40:30 -04002072 case FORMAT_X32B32G32R32F:
John Bauman89401822014-05-06 15:04:28 -04002073 case FORMAT_A32B32G32R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002074 case FORMAT_A32B32G32R32I:
2075 case FORMAT_A32B32G32R32UI:
John Bauman89401822014-05-06 15:04:28 -04002076 buffer = cBuffer;
John Bauman19bac1e2014-05-06 15:23:49 -04002077 pixel.x = *Pointer<Float4>(buffer + 16 * x, 16);
2078 pixel.y = *Pointer<Float4>(buffer + 16 * x + 16, 16);
Nicolas Capens4f172c72016-01-13 08:34:30 -05002079 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman19bac1e2014-05-06 15:23:49 -04002080 pixel.z = *Pointer<Float4>(buffer + 16 * x, 16);
2081 pixel.w = *Pointer<Float4>(buffer + 16 * x + 16, 16);
2082 transpose4x4(pixel.x, pixel.y, pixel.z, pixel.w);
Alexis Hetudbd1a8e2016-04-13 11:40:30 -04002083 if(state.targetFormat[index] == FORMAT_X32B32G32R32F)
2084 {
2085 pixel.w = Float4(1.0f);
2086 }
John Bauman89401822014-05-06 15:04:28 -04002087 break;
2088 default:
2089 ASSERT(false);
2090 }
2091
Alexis Hetu049a1872016-04-25 16:59:58 -04002092 if((postBlendSRGB && state.writeSRGB) || isSRGB(index))
John Bauman89401822014-05-06 15:04:28 -04002093 {
John Bauman19bac1e2014-05-06 15:23:49 -04002094 sRGBtoLinear(pixel.x);
2095 sRGBtoLinear(pixel.y);
2096 sRGBtoLinear(pixel.z);
John Bauman89401822014-05-06 15:04:28 -04002097 }
2098
2099 // Final Color = ObjectColor * SourceBlendFactor + PixelColor * DestinationBlendFactor
John Bauman19bac1e2014-05-06 15:23:49 -04002100 Vector4f sourceFactor;
2101 Vector4f destFactor;
John Bauman89401822014-05-06 15:04:28 -04002102
Nicolas Capens4f172c72016-01-13 08:34:30 -05002103 blendFactor(sourceFactor, oC, pixel, state.sourceBlendFactor);
2104 blendFactor(destFactor, oC, pixel, state.destBlendFactor);
John Bauman89401822014-05-06 15:04:28 -04002105
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002106 if(state.sourceBlendFactor != BLEND_ONE && state.sourceBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002107 {
John Bauman19bac1e2014-05-06 15:23:49 -04002108 oC.x *= sourceFactor.x;
2109 oC.y *= sourceFactor.y;
2110 oC.z *= sourceFactor.z;
John Bauman89401822014-05-06 15:04:28 -04002111 }
Nicolas Capens05b3d662016-02-25 23:58:33 -05002112
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002113 if(state.destBlendFactor != BLEND_ONE && state.destBlendFactor != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002114 {
John Bauman19bac1e2014-05-06 15:23:49 -04002115 pixel.x *= destFactor.x;
2116 pixel.y *= destFactor.y;
2117 pixel.z *= destFactor.z;
John Bauman89401822014-05-06 15:04:28 -04002118 }
2119
2120 switch(state.blendOperation)
2121 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002122 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002123 oC.x += pixel.x;
2124 oC.y += pixel.y;
2125 oC.z += pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002126 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002127 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002128 oC.x -= pixel.x;
2129 oC.y -= pixel.y;
2130 oC.z -= pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002131 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002132 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002133 oC.x = pixel.x - oC.x;
2134 oC.y = pixel.y - oC.y;
2135 oC.z = pixel.z - oC.z;
John Bauman89401822014-05-06 15:04:28 -04002136 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002137 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002138 oC.x = Min(oC.x, pixel.x);
2139 oC.y = Min(oC.y, pixel.y);
2140 oC.z = Min(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04002141 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002142 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002143 oC.x = Max(oC.x, pixel.x);
2144 oC.y = Max(oC.y, pixel.y);
2145 oC.z = Max(oC.z, pixel.z);
John Bauman89401822014-05-06 15:04:28 -04002146 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002147 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04002148 // No operation
2149 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002150 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002151 oC.x = pixel.x;
2152 oC.y = pixel.y;
2153 oC.z = pixel.z;
John Bauman89401822014-05-06 15:04:28 -04002154 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002155 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002156 oC.x = Float4(0.0f);
2157 oC.y = Float4(0.0f);
2158 oC.z = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04002159 break;
2160 default:
2161 ASSERT(false);
2162 }
2163
Nicolas Capens4f172c72016-01-13 08:34:30 -05002164 blendFactorAlpha(sourceFactor, oC, pixel, state.sourceBlendFactorAlpha);
2165 blendFactorAlpha(destFactor, oC, pixel, state.destBlendFactorAlpha);
John Bauman89401822014-05-06 15:04:28 -04002166
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002167 if(state.sourceBlendFactorAlpha != BLEND_ONE && state.sourceBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002168 {
John Bauman19bac1e2014-05-06 15:23:49 -04002169 oC.w *= sourceFactor.w;
John Bauman89401822014-05-06 15:04:28 -04002170 }
Nicolas Capens05b3d662016-02-25 23:58:33 -05002171
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002172 if(state.destBlendFactorAlpha != BLEND_ONE && state.destBlendFactorAlpha != BLEND_ZERO)
John Bauman89401822014-05-06 15:04:28 -04002173 {
John Bauman19bac1e2014-05-06 15:23:49 -04002174 pixel.w *= destFactor.w;
John Bauman89401822014-05-06 15:04:28 -04002175 }
2176
2177 switch(state.blendOperationAlpha)
2178 {
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002179 case BLENDOP_ADD:
John Bauman19bac1e2014-05-06 15:23:49 -04002180 oC.w += pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002181 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002182 case BLENDOP_SUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002183 oC.w -= pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002184 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002185 case BLENDOP_INVSUB:
John Bauman19bac1e2014-05-06 15:23:49 -04002186 pixel.w -= oC.w;
2187 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002188 break;
Nicolas Capens05b3d662016-02-25 23:58:33 -05002189 case BLENDOP_MIN:
John Bauman19bac1e2014-05-06 15:23:49 -04002190 oC.w = Min(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04002191 break;
Nicolas Capens05b3d662016-02-25 23:58:33 -05002192 case BLENDOP_MAX:
John Bauman19bac1e2014-05-06 15:23:49 -04002193 oC.w = Max(oC.w, pixel.w);
John Bauman89401822014-05-06 15:04:28 -04002194 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002195 case BLENDOP_SOURCE:
John Bauman89401822014-05-06 15:04:28 -04002196 // No operation
2197 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002198 case BLENDOP_DEST:
John Bauman19bac1e2014-05-06 15:23:49 -04002199 oC.w = pixel.w;
John Bauman89401822014-05-06 15:04:28 -04002200 break;
Nicolas Capensa0f4be82014-10-22 14:35:30 -04002201 case BLENDOP_NULL:
John Bauman19bac1e2014-05-06 15:23:49 -04002202 oC.w = Float4(0.0f);
John Bauman89401822014-05-06 15:04:28 -04002203 break;
2204 default:
2205 ASSERT(false);
2206 }
2207 }
2208
Nicolas Capens4f172c72016-01-13 08:34:30 -05002209 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 -04002210 {
John Bauman89401822014-05-06 15:04:28 -04002211 switch(state.targetFormat[index])
2212 {
John Bauman89401822014-05-06 15:04:28 -04002213 case FORMAT_R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002214 case FORMAT_R32I:
2215 case FORMAT_R32UI:
Alexis Hetubd7117d2016-06-02 10:35:59 -04002216 case FORMAT_R16I:
2217 case FORMAT_R16UI:
2218 case FORMAT_R8I:
2219 case FORMAT_R8UI:
John Bauman89401822014-05-06 15:04:28 -04002220 break;
2221 case FORMAT_G32R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002222 case FORMAT_G32R32I:
2223 case FORMAT_G32R32UI:
Alexis Hetubd7117d2016-06-02 10:35:59 -04002224 case FORMAT_G16R16I:
2225 case FORMAT_G16R16UI:
2226 case FORMAT_G8R8I:
2227 case FORMAT_G8R8UI:
John Bauman19bac1e2014-05-06 15:23:49 -04002228 oC.z = oC.x;
2229 oC.x = UnpackLow(oC.x, oC.y);
2230 oC.z = UnpackHigh(oC.z, oC.y);
2231 oC.y = oC.z;
John Bauman89401822014-05-06 15:04:28 -04002232 break;
Alexis Hetudbd1a8e2016-04-13 11:40:30 -04002233 case FORMAT_X32B32G32R32F:
John Bauman89401822014-05-06 15:04:28 -04002234 case FORMAT_A32B32G32R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002235 case FORMAT_A32B32G32R32I:
2236 case FORMAT_A32B32G32R32UI:
Alexis Hetubd7117d2016-06-02 10:35:59 -04002237 case FORMAT_A16B16G16R16I:
2238 case FORMAT_A16B16G16R16UI:
2239 case FORMAT_A8B8G8R8I:
2240 case FORMAT_A8B8G8R8UI:
John Bauman19bac1e2014-05-06 15:23:49 -04002241 transpose4x4(oC.x, oC.y, oC.z, oC.w);
John Bauman89401822014-05-06 15:04:28 -04002242 break;
2243 default:
2244 ASSERT(false);
2245 }
2246
2247 int rgbaWriteMask = state.colorWriteActive(index);
2248
2249 Int xMask; // Combination of all masks
2250
2251 if(state.depthTestActive)
2252 {
2253 xMask = zMask;
2254 }
2255 else
2256 {
2257 xMask = cMask;
2258 }
2259
2260 if(state.stencilActive)
2261 {
2262 xMask &= sMask;
2263 }
2264
2265 Pointer<Byte> buffer;
2266 Float4 value;
2267
2268 switch(state.targetFormat[index])
2269 {
2270 case FORMAT_R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002271 case FORMAT_R32I:
2272 case FORMAT_R32UI:
John Bauman89401822014-05-06 15:04:28 -04002273 if(rgbaWriteMask & 0x00000001)
2274 {
2275 buffer = cBuffer + 4 * x;
2276
2277 // FIXME: movlps
2278 value.x = *Pointer<Float>(buffer + 0);
2279 value.y = *Pointer<Float>(buffer + 4);
2280
Nicolas Capens4f172c72016-01-13 08:34:30 -05002281 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04002282
2283 // FIXME: movhps
2284 value.z = *Pointer<Float>(buffer + 0);
2285 value.w = *Pointer<Float>(buffer + 4);
2286
Nicolas Capens4f172c72016-01-13 08:34:30 -05002287 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(constants + OFFSET(Constants,maskD4X) + xMask * 16, 16));
2288 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD4X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002289 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
John Bauman89401822014-05-06 15:04:28 -04002290
2291 // FIXME: movhps
John Bauman19bac1e2014-05-06 15:23:49 -04002292 *Pointer<Float>(buffer + 0) = oC.x.z;
2293 *Pointer<Float>(buffer + 4) = oC.x.w;
John Bauman89401822014-05-06 15:04:28 -04002294
Nicolas Capens4f172c72016-01-13 08:34:30 -05002295 buffer -= *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04002296
2297 // FIXME: movlps
John Bauman19bac1e2014-05-06 15:23:49 -04002298 *Pointer<Float>(buffer + 0) = oC.x.x;
2299 *Pointer<Float>(buffer + 4) = oC.x.y;
John Bauman89401822014-05-06 15:04:28 -04002300 }
2301 break;
Alexis Hetubd7117d2016-06-02 10:35:59 -04002302 case FORMAT_R16I:
2303 case FORMAT_R16UI:
2304 if(rgbaWriteMask & 0x00000001)
2305 {
2306 buffer = cBuffer + 2 * x;
2307
2308 UShort4 xyzw;
2309 xyzw = As<UShort4>(Insert(As<Int2>(xyzw), *Pointer<Int>(buffer), 0));
2310
2311 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2312
2313 xyzw = As<UShort4>(Insert(As<Int2>(xyzw), *Pointer<Int>(buffer), 1));
2314 value = As<Float4>(Int4(xyzw));
2315
2316 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(constants + OFFSET(Constants, maskD4X) + xMask * 16, 16));
2317 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants, invMaskD4X) + xMask * 16, 16));
2318 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
2319
2320 if(state.targetFormat[index] == FORMAT_R16I)
2321 {
2322 Float component = oC.x.z;
2323 *Pointer<Short>(buffer + 0) = Short(As<Int>(component));
2324 component = oC.x.w;
2325 *Pointer<Short>(buffer + 2) = Short(As<Int>(component));
2326
2327 buffer -= *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2328
2329 component = oC.x.x;
2330 *Pointer<Short>(buffer + 0) = Short(As<Int>(component));
2331 component = oC.x.y;
2332 *Pointer<Short>(buffer + 2) = Short(As<Int>(component));
2333 }
2334 else // FORMAT_R16UI
2335 {
2336 Float component = oC.x.z;
2337 *Pointer<UShort>(buffer + 0) = UShort(As<Int>(component));
2338 component = oC.x.w;
2339 *Pointer<UShort>(buffer + 2) = UShort(As<Int>(component));
2340
2341 buffer -= *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2342
2343 component = oC.x.x;
2344 *Pointer<UShort>(buffer + 0) = UShort(As<Int>(component));
2345 component = oC.x.y;
2346 *Pointer<UShort>(buffer + 2) = UShort(As<Int>(component));
2347 }
2348 }
2349 break;
2350 case FORMAT_R8I:
2351 case FORMAT_R8UI:
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002352 if(rgbaWriteMask & 0x00000001)
2353 {
2354 buffer = cBuffer + x;
2355
2356 UInt xyzw, packedCol;
2357
Alexis Hetu827d07a2016-09-15 17:54:05 -04002358 xyzw = UInt(*Pointer<UShort>(buffer)) & 0xFFFF;
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002359 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
Alexis Hetu827d07a2016-09-15 17:54:05 -04002360 xyzw |= UInt(*Pointer<UShort>(buffer)) << 16;
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002361
2362 Short4 tmpCol = Short4(As<Int4>(oC.x));
2363 if(state.targetFormat[index] == FORMAT_R8I)
2364 {
2365 tmpCol = As<Short4>(Pack(tmpCol, tmpCol));
2366 }
2367 else
2368 {
2369 tmpCol = As<Short4>(Pack(As<UShort4>(tmpCol), As<UShort4>(tmpCol)));
2370 }
2371 packedCol = Extract(As<Int2>(tmpCol), 0);
2372
2373 packedCol = (packedCol & *Pointer<UInt>(constants + OFFSET(Constants, maskB4Q) + 8 * xMask)) |
2374 (xyzw & *Pointer<UInt>(constants + OFFSET(Constants, invMaskB4Q) + 8 * xMask));
2375
2376 *Pointer<UShort>(buffer) = UShort(packedCol >> 16);
2377 buffer -= *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2378 *Pointer<UShort>(buffer) = UShort(packedCol);
2379 }
Alexis Hetubd7117d2016-06-02 10:35:59 -04002380 break;
John Bauman89401822014-05-06 15:04:28 -04002381 case FORMAT_G32R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002382 case FORMAT_G32R32I:
2383 case FORMAT_G32R32UI:
John Bauman89401822014-05-06 15:04:28 -04002384 buffer = cBuffer + 8 * x;
2385
2386 value = *Pointer<Float4>(buffer);
2387
2388 if((rgbaWriteMask & 0x00000003) != 0x00000003)
2389 {
2390 Float4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05002391 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(constants + OFFSET(Constants,maskD01X[rgbaWriteMask & 0x3][0])));
2392 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD01X[rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002393 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002394 }
2395
Nicolas Capens4f172c72016-01-13 08:34:30 -05002396 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(constants + OFFSET(Constants,maskQ01X) + xMask * 16, 16));
2397 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskQ01X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002398 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
2399 *Pointer<Float4>(buffer) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04002400
Nicolas Capens4f172c72016-01-13 08:34:30 -05002401 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04002402
2403 value = *Pointer<Float4>(buffer);
2404
2405 if((rgbaWriteMask & 0x00000003) != 0x00000003)
2406 {
2407 Float4 masked;
2408
2409 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05002410 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(constants + OFFSET(Constants,maskD01X[rgbaWriteMask & 0x3][0])));
2411 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD01X[rgbaWriteMask & 0x3][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002412 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002413 }
2414
Nicolas Capens4f172c72016-01-13 08:34:30 -05002415 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(constants + OFFSET(Constants,maskQ23X) + xMask * 16, 16));
2416 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskQ23X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002417 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
2418 *Pointer<Float4>(buffer) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04002419 break;
Alexis Hetubd7117d2016-06-02 10:35:59 -04002420 case FORMAT_G16R16I:
2421 case FORMAT_G16R16UI:
Meng-Lin Wu1978ff72016-06-20 15:08:42 -04002422 if((rgbaWriteMask & 0x00000003) != 0x0)
2423 {
2424 buffer = cBuffer + 4 * x;
2425
2426 UInt2 rgbaMask;
2427 UShort4 packedCol = UShort4(As<Int4>(oC.x));
2428 UShort4 value = *Pointer<UShort4>(buffer);
2429 UInt2 mergedMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskD01Q) + xMask * 8);
2430 if((rgbaWriteMask & 0x3) != 0x3)
2431 {
2432 Int tmpMask = *Pointer<Int>(constants + OFFSET(Constants, maskW4Q[rgbaWriteMask & 0x3][0]));
2433 rgbaMask = As<UInt2>(Int2(tmpMask, tmpMask));
2434 mergedMask &= rgbaMask;
2435 }
2436 *Pointer<UInt2>(buffer) = (As<UInt2>(packedCol) & mergedMask) | (As<UInt2>(value) & ~mergedMask);
2437
2438 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2439
2440 packedCol = UShort4(As<Int4>(oC.y));
2441 value = *Pointer<UShort4>(buffer);
2442 mergedMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskD23Q) + xMask * 8);
2443 if((rgbaWriteMask & 0x3) != 0x3)
2444 {
2445 mergedMask &= rgbaMask;
2446 }
2447 *Pointer<UInt2>(buffer) = (As<UInt2>(packedCol) & mergedMask) | (As<UInt2>(value) & ~mergedMask);
2448 }
Alexis Hetubd7117d2016-06-02 10:35:59 -04002449 break;
2450 case FORMAT_G8R8I:
2451 case FORMAT_G8R8UI:
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002452 if((rgbaWriteMask & 0x00000003) != 0x0)
2453 {
2454 buffer = cBuffer + 2 * x;
2455
2456 Int2 xyzw, packedCol;
2457
2458 xyzw = Insert(xyzw, *Pointer<Int>(buffer), 0);
2459 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2460 xyzw = Insert(xyzw, *Pointer<Int>(buffer), 1);
2461
2462 if(state.targetFormat[index] == FORMAT_G8R8I)
2463 {
2464 packedCol = As<Int2>(Pack(Short4(As<Int4>(oC.x)), Short4(As<Int4>(oC.y))));
2465 }
2466 else
2467 {
2468 packedCol = As<Int2>(Pack(UShort4(As<Int4>(oC.x)), UShort4(As<Int4>(oC.y))));
2469 }
2470
2471 UInt2 mergedMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskW4Q) + xMask * 8);
2472 if((rgbaWriteMask & 0x3) != 0x3)
2473 {
2474 Int tmpMask = *Pointer<Int>(constants + OFFSET(Constants, maskB4Q[5 * (rgbaWriteMask & 0x3)][0]));
2475 UInt2 rgbaMask = As<UInt2>(Int2(tmpMask, tmpMask));
2476 mergedMask &= rgbaMask;
2477 }
2478
2479 packedCol = As<Int2>((As<UInt2>(packedCol) & mergedMask) | (As<UInt2>(xyzw) & ~mergedMask));
2480
2481 *Pointer<UInt>(buffer) = As<UInt>(Extract(packedCol, 1));
2482 buffer -= *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2483 *Pointer<UInt>(buffer) = As<UInt>(Extract(packedCol, 0));
2484 }
Alexis Hetubd7117d2016-06-02 10:35:59 -04002485 break;
Alexis Hetudbd1a8e2016-04-13 11:40:30 -04002486 case FORMAT_X32B32G32R32F:
John Bauman89401822014-05-06 15:04:28 -04002487 case FORMAT_A32B32G32R32F:
Alexis Hetu1abb6382016-02-08 11:21:16 -05002488 case FORMAT_A32B32G32R32I:
2489 case FORMAT_A32B32G32R32UI:
John Bauman89401822014-05-06 15:04:28 -04002490 buffer = cBuffer + 16 * x;
2491
2492 {
2493 value = *Pointer<Float4>(buffer, 16);
2494
2495 if(rgbaWriteMask != 0x0000000F)
2496 {
2497 Float4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05002498 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
2499 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002500 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002501 }
Nicolas Capens05b3d662016-02-25 23:58:33 -05002502
Nicolas Capens4f172c72016-01-13 08:34:30 -05002503 oC.x = As<Float4>(As<Int4>(oC.x) & *Pointer<Int4>(constants + OFFSET(Constants,maskX0X) + xMask * 16, 16));
2504 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskX0X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002505 oC.x = As<Float4>(As<Int4>(oC.x) | As<Int4>(value));
2506 *Pointer<Float4>(buffer, 16) = oC.x;
John Bauman89401822014-05-06 15:04:28 -04002507 }
2508
2509 {
2510 value = *Pointer<Float4>(buffer + 16, 16);
2511
2512 if(rgbaWriteMask != 0x0000000F)
Nicolas Capens05b3d662016-02-25 23:58:33 -05002513 {
John Bauman89401822014-05-06 15:04:28 -04002514 Float4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05002515 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
2516 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002517 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002518 }
2519
Nicolas Capens4f172c72016-01-13 08:34:30 -05002520 oC.y = As<Float4>(As<Int4>(oC.y) & *Pointer<Int4>(constants + OFFSET(Constants,maskX1X) + xMask * 16, 16));
2521 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskX1X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002522 oC.y = As<Float4>(As<Int4>(oC.y) | As<Int4>(value));
2523 *Pointer<Float4>(buffer + 16, 16) = oC.y;
John Bauman89401822014-05-06 15:04:28 -04002524 }
2525
Nicolas Capens4f172c72016-01-13 08:34:30 -05002526 buffer += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
John Bauman89401822014-05-06 15:04:28 -04002527
2528 {
2529 value = *Pointer<Float4>(buffer, 16);
2530
2531 if(rgbaWriteMask != 0x0000000F)
2532 {
2533 Float4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05002534 oC.z = As<Float4>(As<Int4>(oC.z) & *Pointer<Int4>(constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
2535 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002536 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002537 }
2538
Nicolas Capens4f172c72016-01-13 08:34:30 -05002539 oC.z = As<Float4>(As<Int4>(oC.z) & *Pointer<Int4>(constants + OFFSET(Constants,maskX2X) + xMask * 16, 16));
2540 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskX2X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002541 oC.z = As<Float4>(As<Int4>(oC.z) | As<Int4>(value));
2542 *Pointer<Float4>(buffer, 16) = oC.z;
John Bauman89401822014-05-06 15:04:28 -04002543 }
2544
2545 {
Alexis Hetudbd1a8e2016-04-13 11:40:30 -04002546 value = (state.targetFormat[index] == FORMAT_X32B32G32R32F) ? Float4(1.0f) : *Pointer<Float4>(buffer + 16, 16);
John Bauman89401822014-05-06 15:04:28 -04002547
2548 if(rgbaWriteMask != 0x0000000F)
2549 {
2550 Float4 masked = value;
Nicolas Capens4f172c72016-01-13 08:34:30 -05002551 oC.w = As<Float4>(As<Int4>(oC.w) & *Pointer<Int4>(constants + OFFSET(Constants,maskD4X[rgbaWriteMask][0])));
2552 masked = As<Float4>(As<Int4>(masked) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskD4X[rgbaWriteMask][0])));
John Bauman19bac1e2014-05-06 15:23:49 -04002553 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(masked));
John Bauman89401822014-05-06 15:04:28 -04002554 }
2555
Nicolas Capens4f172c72016-01-13 08:34:30 -05002556 oC.w = As<Float4>(As<Int4>(oC.w) & *Pointer<Int4>(constants + OFFSET(Constants,maskX3X) + xMask * 16, 16));
2557 value = As<Float4>(As<Int4>(value) & *Pointer<Int4>(constants + OFFSET(Constants,invMaskX3X) + xMask * 16, 16));
John Bauman19bac1e2014-05-06 15:23:49 -04002558 oC.w = As<Float4>(As<Int4>(oC.w) | As<Int4>(value));
2559 *Pointer<Float4>(buffer + 16, 16) = oC.w;
John Bauman89401822014-05-06 15:04:28 -04002560 }
2561 break;
Alexis Hetubd7117d2016-06-02 10:35:59 -04002562 case FORMAT_A16B16G16R16I:
2563 case FORMAT_A16B16G16R16UI:
Meng-Lin Wu1978ff72016-06-20 15:08:42 -04002564 if((rgbaWriteMask & 0x0000000F) != 0x0)
2565 {
2566 buffer = cBuffer + 8 * x;
2567
2568 UInt4 rgbaMask;
2569 UShort8 value = *Pointer<UShort8>(buffer);
2570 UShort8 packedCol = UShort8(UShort4(As<Int4>(oC.x)), UShort4(As<Int4>(oC.y)));
2571 UInt4 mergedMask = *Pointer<UInt4>(constants + OFFSET(Constants, maskQ01X) + xMask * 16);
2572 if((rgbaWriteMask & 0xF) != 0xF)
2573 {
2574 UInt2 tmpMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskW4Q[rgbaWriteMask][0]));
2575 rgbaMask = UInt4(tmpMask, tmpMask);
2576 mergedMask &= rgbaMask;
2577 }
2578 *Pointer<UInt4>(buffer) = (As<UInt4>(packedCol) & mergedMask) | (As<UInt4>(value) & ~mergedMask);
2579
2580 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2581
2582 value = *Pointer<UShort8>(buffer);
2583 packedCol = UShort8(UShort4(As<Int4>(oC.z)), UShort4(As<Int4>(oC.w)));
2584 mergedMask = *Pointer<UInt4>(constants + OFFSET(Constants, maskQ23X) + xMask * 16);
2585 if((rgbaWriteMask & 0xF) != 0xF)
2586 {
2587 mergedMask &= rgbaMask;
2588 }
2589 *Pointer<UInt4>(buffer) = (As<UInt4>(packedCol) & mergedMask) | (As<UInt4>(value) & ~mergedMask);
2590 }
Alexis Hetubd7117d2016-06-02 10:35:59 -04002591 break;
2592 case FORMAT_A8B8G8R8I:
2593 case FORMAT_A8B8G8R8UI:
Meng-Lin Wu92a95e92016-06-16 16:56:15 -04002594 if((rgbaWriteMask & 0x0000000F) != 0x0)
2595 {
2596 UInt2 value, packedCol, mergedMask;
2597
2598 buffer = cBuffer + 4 * x;
2599
2600 if(state.targetFormat[index] == FORMAT_A8B8G8R8I)
2601 {
2602 packedCol = As<UInt2>(Pack(Short4(As<Int4>(oC.x)), Short4(As<Int4>(oC.y))));
2603 }
2604 else
2605 {
2606 packedCol = As<UInt2>(Pack(UShort4(As<Int4>(oC.x)), UShort4(As<Int4>(oC.y))));
2607 }
2608 value = *Pointer<UInt2>(buffer, 16);
2609 mergedMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskD01Q) + xMask * 8);
2610 if(rgbaWriteMask != 0xF)
2611 {
2612 mergedMask &= *Pointer<UInt2>(constants + OFFSET(Constants, maskB4Q[rgbaWriteMask][0]));
2613 }
2614 *Pointer<UInt2>(buffer) = (packedCol & mergedMask) | (value & ~mergedMask);
2615
2616 buffer += *Pointer<Int>(data + OFFSET(DrawData, colorPitchB[index]));
2617
2618 if(state.targetFormat[index] == FORMAT_A8B8G8R8I)
2619 {
2620 packedCol = As<UInt2>(Pack(Short4(As<Int4>(oC.z)), Short4(As<Int4>(oC.w))));
2621 }
2622 else
2623 {
2624 packedCol = As<UInt2>(Pack(UShort4(As<Int4>(oC.z)), UShort4(As<Int4>(oC.w))));
2625 }
2626 value = *Pointer<UInt2>(buffer, 16);
2627 mergedMask = *Pointer<UInt2>(constants + OFFSET(Constants, maskD23Q) + xMask * 8);
2628 if(rgbaWriteMask != 0xF)
2629 {
2630 mergedMask &= *Pointer<UInt2>(constants + OFFSET(Constants, maskB4Q[rgbaWriteMask][0]));
2631 }
2632 *Pointer<UInt2>(buffer) = (packedCol & mergedMask) | (value & ~mergedMask);
2633 }
Alexis Hetubd7117d2016-06-02 10:35:59 -04002634 break;
John Bauman89401822014-05-06 15:04:28 -04002635 default:
2636 ASSERT(false);
2637 }
2638 }
2639
John Bauman89401822014-05-06 15:04:28 -04002640 UShort4 PixelRoutine::convertFixed16(Float4 &cf, bool saturate)
2641 {
John Bauman19bac1e2014-05-06 15:23:49 -04002642 return UShort4(cf * Float4(0xFFFF), saturate);
John Bauman89401822014-05-06 15:04:28 -04002643 }
2644
Nicolas Capens4f172c72016-01-13 08:34:30 -05002645 void PixelRoutine::sRGBtoLinear16_12_16(Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04002646 {
John Bauman19bac1e2014-05-06 15:23:49 -04002647 c.x = As<UShort4>(c.x) >> 4;
2648 c.y = As<UShort4>(c.y) >> 4;
2649 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04002650
Nicolas Capens4f172c72016-01-13 08:34:30 -05002651 sRGBtoLinear12_16(c);
John Bauman89401822014-05-06 15:04:28 -04002652 }
2653
Nicolas Capens4f172c72016-01-13 08:34:30 -05002654 void PixelRoutine::sRGBtoLinear12_16(Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04002655 {
Nicolas Capens4f172c72016-01-13 08:34:30 -05002656 Pointer<Byte> LUT = constants + OFFSET(Constants,sRGBtoLinear12_16);
John Bauman89401822014-05-06 15:04:28 -04002657
John Bauman19bac1e2014-05-06 15:23:49 -04002658 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
2659 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
2660 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
2661 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002662
John Bauman19bac1e2014-05-06 15:23:49 -04002663 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
2664 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
2665 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
2666 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002667
John Bauman19bac1e2014-05-06 15:23:49 -04002668 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
2669 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
2670 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
2671 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002672 }
2673
Nicolas Capens4f172c72016-01-13 08:34:30 -05002674 void PixelRoutine::linearToSRGB16_12_16(Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04002675 {
John Bauman19bac1e2014-05-06 15:23:49 -04002676 c.x = As<UShort4>(c.x) >> 4;
2677 c.y = As<UShort4>(c.y) >> 4;
2678 c.z = As<UShort4>(c.z) >> 4;
John Bauman89401822014-05-06 15:04:28 -04002679
Nicolas Capens4f172c72016-01-13 08:34:30 -05002680 linearToSRGB12_16(c);
John Bauman89401822014-05-06 15:04:28 -04002681 }
2682
Nicolas Capens4f172c72016-01-13 08:34:30 -05002683 void PixelRoutine::linearToSRGB12_16(Vector4s &c)
John Bauman89401822014-05-06 15:04:28 -04002684 {
Nicolas Capens4f172c72016-01-13 08:34:30 -05002685 Pointer<Byte> LUT = constants + OFFSET(Constants,linearToSRGB12_16);
John Bauman89401822014-05-06 15:04:28 -04002686
John Bauman19bac1e2014-05-06 15:23:49 -04002687 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 0))), 0);
2688 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 1))), 1);
2689 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 2))), 2);
2690 c.x = Insert(c.x, *Pointer<Short>(LUT + 2 * Int(Extract(c.x, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002691
John Bauman19bac1e2014-05-06 15:23:49 -04002692 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 0))), 0);
2693 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 1))), 1);
2694 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 2))), 2);
2695 c.y = Insert(c.y, *Pointer<Short>(LUT + 2 * Int(Extract(c.y, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002696
John Bauman19bac1e2014-05-06 15:23:49 -04002697 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 0))), 0);
2698 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 1))), 1);
2699 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 2))), 2);
2700 c.z = Insert(c.z, *Pointer<Short>(LUT + 2 * Int(Extract(c.z, 3))), 3);
John Bauman89401822014-05-06 15:04:28 -04002701 }
2702
John Bauman89401822014-05-06 15:04:28 -04002703 Float4 PixelRoutine::sRGBtoLinear(const Float4 &x) // Approximates x^2.2
2704 {
2705 Float4 linear = x * x;
2706 linear = linear * Float4(0.73f) + linear * x * Float4(0.27f);
2707
2708 return Min(Max(linear, Float4(0.0f)), Float4(1.0f));
2709 }
2710
John Bauman19bac1e2014-05-06 15:23:49 -04002711 bool PixelRoutine::colorUsed()
2712 {
2713 return state.colorWriteMask || state.alphaTestActive() || state.shaderContainsKill;
2714 }
John Bauman89401822014-05-06 15:04:28 -04002715}