blob: 858772ad2b95d791ff453c562468f9bfa865ad81 [file] [log] [blame]
John Bauman89401822014-05-06 15:04:28 -04001// SwiftShader Software Renderer
2//
John Bauman66b8ab22014-05-06 15:57:45 -04003// Copyright(c) 2005-2013 TransGaming Inc.
John Bauman89401822014-05-06 15:04:28 -04004//
5// All rights reserved. No part of this software may be copied, distributed, transmitted,
6// transcribed, stored in a retrieval system, translated into any human or computer
7// language by any means, or disclosed to third parties without the explicit written
8// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9// or implied, including but not limited to any patent rights, are granted to you.
10//
11
12#include "PixelProcessor.hpp"
13
Alexis Hetuf2a8c372015-07-13 11:08:41 -040014#include "PixelPipeline.hpp"
15#include "PixelProgram.hpp"
John Bauman89401822014-05-06 15:04:28 -040016#include "PixelShader.hpp"
17#include "MetaMacro.hpp"
18#include "Surface.hpp"
19#include "Primitive.hpp"
20#include "Constants.hpp"
21#include "Debug.hpp"
22
John Bauman66b8ab22014-05-06 15:57:45 -040023#include <string.h>
24
John Bauman89401822014-05-06 15:04:28 -040025namespace sw
26{
27 extern bool complementaryDepthBuffer;
Nicolas Capensa0f4be82014-10-22 14:35:30 -040028 extern TransparencyAntialiasing transparencyAntialiasing;
John Bauman89401822014-05-06 15:04:28 -040029 extern bool perspectiveCorrection;
30
John Bauman66b8ab22014-05-06 15:57:45 -040031 bool precachePixel = false;
32
John Bauman89401822014-05-06 15:04:28 -040033 unsigned int PixelProcessor::States::computeHash()
34 {
35 unsigned int *state = (unsigned int*)this;
36 unsigned int hash = 0;
37
38 for(int i = 0; i < sizeof(States) / 4; i++)
39 {
40 hash ^= state[i];
41 }
42
43 return hash;
44 }
45
46 PixelProcessor::State::State()
47 {
48 memset(this, 0, sizeof(State));
49 }
50
51 bool PixelProcessor::State::operator==(const State &state) const
52 {
53 if(hash != state.hash)
54 {
55 return false;
56 }
57
58 return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0;
59 }
60
61 PixelProcessor::PixelProcessor(Context *context) : context(context)
62 {
63 setGlobalMipmapBias(0.0f); // Round to highest LOD [0.5, 1.0]: -0.5
64 // Round to nearest LOD [0.7, 1.4]: 0.0
65 // Round to lowest LOD [1.0, 2.0]: 0.5
66
67 routineCache = 0;
68 setRoutineCacheSize(1024);
69 }
70
71 PixelProcessor::~PixelProcessor()
72 {
73 delete routineCache;
74 routineCache = 0;
75 }
76
77 void PixelProcessor::setFloatConstant(unsigned int index, const float value[4])
78 {
Alexis Hetu04c967a2015-07-08 15:56:17 -040079 if(index < FRAGMENT_UNIFORM_VECTORS)
John Bauman89401822014-05-06 15:04:28 -040080 {
81 c[index][0] = value[0];
82 c[index][1] = value[1];
83 c[index][2] = value[2];
84 c[index][3] = value[3];
85 }
86 else ASSERT(false);
87
88 if(index < 8) // ps_1_x constants
89 {
90 // FIXME: Compact into generic function
91 short x = iround(4095 * clamp(value[0], -1.0f, 1.0f));
92 short y = iround(4095 * clamp(value[1], -1.0f, 1.0f));
93 short z = iround(4095 * clamp(value[2], -1.0f, 1.0f));
94 short w = iround(4095 * clamp(value[3], -1.0f, 1.0f));
95
96 cW[index][0][0] = x;
97 cW[index][0][1] = x;
98 cW[index][0][2] = x;
99 cW[index][0][3] = x;
100
101 cW[index][1][0] = y;
102 cW[index][1][1] = y;
103 cW[index][1][2] = y;
104 cW[index][1][3] = y;
105
106 cW[index][2][0] = z;
107 cW[index][2][1] = z;
108 cW[index][2][2] = z;
109 cW[index][2][3] = z;
110
111 cW[index][3][0] = w;
112 cW[index][3][1] = w;
113 cW[index][3][2] = w;
114 cW[index][3][3] = w;
115 }
116 }
117
118 void PixelProcessor::setIntegerConstant(unsigned int index, const int value[4])
119 {
120 if(index < 16)
121 {
122 i[index][0] = value[0];
123 i[index][1] = value[1];
124 i[index][2] = value[2];
125 i[index][3] = value[3];
126 }
127 else ASSERT(false);
128 }
129
130 void PixelProcessor::setBooleanConstant(unsigned int index, int boolean)
131 {
132 if(index < 16)
133 {
134 b[index] = boolean != 0;
135 }
136 else ASSERT(false);
137 }
138
139 void PixelProcessor::setRenderTarget(int index, Surface *renderTarget)
140 {
141 context->renderTarget[index] = renderTarget;
142 }
143
144 void PixelProcessor::setDepthStencil(Surface *depthStencil)
145 {
146 context->depthStencil = depthStencil;
147 }
148
149 void PixelProcessor::setTexCoordIndex(unsigned int stage, int texCoordIndex)
150 {
151 if(stage < 8)
152 {
153 context->textureStage[stage].setTexCoordIndex(texCoordIndex);
154 }
155 else ASSERT(false);
156 }
157
158 void PixelProcessor::setStageOperation(unsigned int stage, TextureStage::StageOperation stageOperation)
159 {
160 if(stage < 8)
161 {
162 context->textureStage[stage].setStageOperation(stageOperation);
163 }
164 else ASSERT(false);
165 }
166
167 void PixelProcessor::setFirstArgument(unsigned int stage, TextureStage::SourceArgument firstArgument)
168 {
169 if(stage < 8)
170 {
171 context->textureStage[stage].setFirstArgument(firstArgument);
172 }
173 else ASSERT(false);
174 }
175
176 void PixelProcessor::setSecondArgument(unsigned int stage, TextureStage::SourceArgument secondArgument)
177 {
178 if(stage < 8)
179 {
180 context->textureStage[stage].setSecondArgument(secondArgument);
181 }
182 else ASSERT(false);
183 }
184
185 void PixelProcessor::setThirdArgument(unsigned int stage, TextureStage::SourceArgument thirdArgument)
186 {
187 if(stage < 8)
188 {
189 context->textureStage[stage].setThirdArgument(thirdArgument);
190 }
191 else ASSERT(false);
192 }
193
194 void PixelProcessor::setStageOperationAlpha(unsigned int stage, TextureStage::StageOperation stageOperationAlpha)
195 {
196 if(stage < 8)
197 {
198 context->textureStage[stage].setStageOperationAlpha(stageOperationAlpha);
199 }
200 else ASSERT(false);
201 }
202
203 void PixelProcessor::setFirstArgumentAlpha(unsigned int stage, TextureStage::SourceArgument firstArgumentAlpha)
204 {
205 if(stage < 8)
206 {
207 context->textureStage[stage].setFirstArgumentAlpha(firstArgumentAlpha);
208 }
209 else ASSERT(false);
210 }
211
212 void PixelProcessor::setSecondArgumentAlpha(unsigned int stage, TextureStage::SourceArgument secondArgumentAlpha)
213 {
214 if(stage < 8)
215 {
216 context->textureStage[stage].setSecondArgumentAlpha(secondArgumentAlpha);
217 }
218 else ASSERT(false);
219 }
220
221 void PixelProcessor::setThirdArgumentAlpha(unsigned int stage, TextureStage::SourceArgument thirdArgumentAlpha)
222 {
223 if(stage < 8)
224 {
225 context->textureStage[stage].setThirdArgumentAlpha(thirdArgumentAlpha);
226 }
227 else ASSERT(false);
228 }
229
230 void PixelProcessor::setFirstModifier(unsigned int stage, TextureStage::ArgumentModifier firstModifier)
231 {
232 if(stage < 8)
233 {
234 context->textureStage[stage].setFirstModifier(firstModifier);
235 }
236 else ASSERT(false);
237 }
238
239 void PixelProcessor::setSecondModifier(unsigned int stage, TextureStage::ArgumentModifier secondModifier)
240 {
241 if(stage < 8)
242 {
243 context->textureStage[stage].setSecondModifier(secondModifier);
244 }
245 else ASSERT(false);
246 }
247
248 void PixelProcessor::setThirdModifier(unsigned int stage, TextureStage::ArgumentModifier thirdModifier)
249 {
250 if(stage < 8)
251 {
252 context->textureStage[stage].setThirdModifier(thirdModifier);
253 }
254 else ASSERT(false);
255 }
256
257 void PixelProcessor::setFirstModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier firstModifierAlpha)
258 {
259 if(stage < 8)
260 {
261 context->textureStage[stage].setFirstModifierAlpha(firstModifierAlpha);
262 }
263 else ASSERT(false);
264 }
265
266 void PixelProcessor::setSecondModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier secondModifierAlpha)
267 {
268 if(stage < 8)
269 {
270 context->textureStage[stage].setSecondModifierAlpha(secondModifierAlpha);
271 }
272 else ASSERT(false);
273 }
274
275 void PixelProcessor::setThirdModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier thirdModifierAlpha)
276 {
277 if(stage < 8)
278 {
279 context->textureStage[stage].setThirdModifierAlpha(thirdModifierAlpha);
280 }
281 else ASSERT(false);
282 }
283
284 void PixelProcessor::setDestinationArgument(unsigned int stage, TextureStage::DestinationArgument destinationArgument)
285 {
286 if(stage < 8)
287 {
288 context->textureStage[stage].setDestinationArgument(destinationArgument);
289 }
290 else ASSERT(false);
291 }
292
293 void PixelProcessor::setConstantColor(unsigned int stage, const Color<float> &constantColor)
294 {
295 if(stage < 8)
296 {
297 context->textureStage[stage].setConstantColor(constantColor);
298 }
299 else ASSERT(false);
300 }
301
302 void PixelProcessor::setBumpmapMatrix(unsigned int stage, int element, float value)
303 {
304 if(stage < 8)
305 {
306 context->textureStage[stage].setBumpmapMatrix(element, value);
307 }
308 else ASSERT(false);
309 }
310
311 void PixelProcessor::setLuminanceScale(unsigned int stage, float value)
312 {
313 if(stage < 8)
314 {
315 context->textureStage[stage].setLuminanceScale(value);
316 }
317 else ASSERT(false);
318 }
319
320 void PixelProcessor::setLuminanceOffset(unsigned int stage, float value)
321 {
322 if(stage < 8)
323 {
324 context->textureStage[stage].setLuminanceOffset(value);
325 }
326 else ASSERT(false);
327 }
328
329 void PixelProcessor::setTextureFilter(unsigned int sampler, FilterType textureFilter)
330 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400331 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400332 {
333 context->sampler[sampler].setTextureFilter(textureFilter);
334 }
335 else ASSERT(false);
336 }
337
338 void PixelProcessor::setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter)
339 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400340 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400341 {
342 context->sampler[sampler].setMipmapFilter(mipmapFilter);
343 }
344 else ASSERT(false);
345 }
346
347 void PixelProcessor::setGatherEnable(unsigned int sampler, bool enable)
348 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400349 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400350 {
351 context->sampler[sampler].setGatherEnable(enable);
352 }
353 else ASSERT(false);
354 }
355
356 void PixelProcessor::setAddressingModeU(unsigned int sampler, AddressingMode addressMode)
357 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400358 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400359 {
360 context->sampler[sampler].setAddressingModeU(addressMode);
361 }
362 else ASSERT(false);
363 }
364
365 void PixelProcessor::setAddressingModeV(unsigned int sampler, AddressingMode addressMode)
366 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400367 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400368 {
369 context->sampler[sampler].setAddressingModeV(addressMode);
370 }
371 else ASSERT(false);
372 }
373
374 void PixelProcessor::setAddressingModeW(unsigned int sampler, AddressingMode addressMode)
375 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400376 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400377 {
378 context->sampler[sampler].setAddressingModeW(addressMode);
379 }
380 else ASSERT(false);
381 }
382
383 void PixelProcessor::setReadSRGB(unsigned int sampler, bool sRGB)
384 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400385 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400386 {
387 context->sampler[sampler].setReadSRGB(sRGB);
388 }
389 else ASSERT(false);
390 }
391
392 void PixelProcessor::setMipmapLOD(unsigned int sampler, float bias)
393 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400394 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400395 {
396 context->sampler[sampler].setMipmapLOD(bias);
397 }
398 else ASSERT(false);
399 }
400
401 void PixelProcessor::setBorderColor(unsigned int sampler, const Color<float> &borderColor)
402 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400403 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400404 {
405 context->sampler[sampler].setBorderColor(borderColor);
406 }
407 else ASSERT(false);
408 }
409
Alexis Hetu617a5d52014-11-13 10:56:20 -0500410 void PixelProcessor::setMaxAnisotropy(unsigned int sampler, float maxAnisotropy)
John Bauman89401822014-05-06 15:04:28 -0400411 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400412 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400413 {
414 context->sampler[sampler].setMaxAnisotropy(maxAnisotropy);
415 }
416 else ASSERT(false);
417 }
418
419 void PixelProcessor::setWriteSRGB(bool sRGB)
420 {
421 context->setWriteSRGB(sRGB);
422 }
423
Maxime Grégoired9762742015-07-08 16:43:48 -0400424 void PixelProcessor::setColorLogicOpEnabled(bool colorLogicOpEnabled)
425 {
426 context->setColorLogicOpEnabled(colorLogicOpEnabled);
427 }
428
429 void PixelProcessor::setLogicalOperation(LogicalOperation logicalOperation)
430 {
431 context->setLogicalOperation(logicalOperation);
432 }
433
John Bauman89401822014-05-06 15:04:28 -0400434 void PixelProcessor::setDepthBufferEnable(bool depthBufferEnable)
435 {
436 context->setDepthBufferEnable(depthBufferEnable);
437 }
438
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400439 void PixelProcessor::setDepthCompare(DepthCompareMode depthCompareMode)
John Bauman89401822014-05-06 15:04:28 -0400440 {
441 context->depthCompareMode = depthCompareMode;
442 }
443
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400444 void PixelProcessor::setAlphaCompare(AlphaCompareMode alphaCompareMode)
John Bauman89401822014-05-06 15:04:28 -0400445 {
446 context->alphaCompareMode = alphaCompareMode;
447 }
448
449 void PixelProcessor::setDepthWriteEnable(bool depthWriteEnable)
450 {
451 context->depthWriteEnable = depthWriteEnable;
452 }
453
454 void PixelProcessor::setAlphaTestEnable(bool alphaTestEnable)
455 {
456 context->alphaTestEnable = alphaTestEnable;
457 }
458
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400459 void PixelProcessor::setCullMode(CullMode cullMode)
John Bauman89401822014-05-06 15:04:28 -0400460 {
461 context->cullMode = cullMode;
462 }
463
464 void PixelProcessor::setColorWriteMask(int index, int rgbaMask)
465 {
466 context->setColorWriteMask(index, rgbaMask);
467 }
468
469 void PixelProcessor::setStencilEnable(bool stencilEnable)
470 {
471 context->stencilEnable = stencilEnable;
472 }
473
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400474 void PixelProcessor::setStencilCompare(StencilCompareMode stencilCompareMode)
John Bauman89401822014-05-06 15:04:28 -0400475 {
476 context->stencilCompareMode = stencilCompareMode;
477 }
478
479 void PixelProcessor::setStencilReference(int stencilReference)
480 {
481 context->stencilReference = stencilReference;
482 stencil.set(stencilReference, context->stencilMask, context->stencilWriteMask);
483 }
484
485 void PixelProcessor::setStencilReferenceCCW(int stencilReferenceCCW)
486 {
487 context->stencilReferenceCCW = stencilReferenceCCW;
488 stencilCCW.set(stencilReferenceCCW, context->stencilMaskCCW, context->stencilWriteMaskCCW);
489 }
490
491 void PixelProcessor::setStencilMask(int stencilMask)
492 {
493 context->stencilMask = stencilMask;
494 stencil.set(context->stencilReference, stencilMask, context->stencilWriteMask);
495 }
496
497 void PixelProcessor::setStencilMaskCCW(int stencilMaskCCW)
498 {
499 context->stencilMaskCCW = stencilMaskCCW;
500 stencilCCW.set(context->stencilReferenceCCW, stencilMaskCCW, context->stencilWriteMaskCCW);
501 }
502
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400503 void PixelProcessor::setStencilFailOperation(StencilOperation stencilFailOperation)
John Bauman89401822014-05-06 15:04:28 -0400504 {
505 context->stencilFailOperation = stencilFailOperation;
506 }
507
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400508 void PixelProcessor::setStencilPassOperation(StencilOperation stencilPassOperation)
John Bauman89401822014-05-06 15:04:28 -0400509 {
510 context->stencilPassOperation = stencilPassOperation;
511 }
512
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400513 void PixelProcessor::setStencilZFailOperation(StencilOperation stencilZFailOperation)
John Bauman89401822014-05-06 15:04:28 -0400514 {
515 context->stencilZFailOperation = stencilZFailOperation;
516 }
517
518 void PixelProcessor::setStencilWriteMask(int stencilWriteMask)
519 {
520 context->stencilWriteMask = stencilWriteMask;
521 stencil.set(context->stencilReference, context->stencilMask, stencilWriteMask);
522 }
523
524 void PixelProcessor::setStencilWriteMaskCCW(int stencilWriteMaskCCW)
525 {
526 context->stencilWriteMaskCCW = stencilWriteMaskCCW;
527 stencilCCW.set(context->stencilReferenceCCW, context->stencilMaskCCW, stencilWriteMaskCCW);
528 }
529
530 void PixelProcessor::setTwoSidedStencil(bool enable)
531 {
532 context->twoSidedStencil = enable;
533 }
534
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400535 void PixelProcessor::setStencilCompareCCW(StencilCompareMode stencilCompareMode)
John Bauman89401822014-05-06 15:04:28 -0400536 {
537 context->stencilCompareModeCCW = stencilCompareMode;
538 }
539
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400540 void PixelProcessor::setStencilFailOperationCCW(StencilOperation stencilFailOperation)
John Bauman89401822014-05-06 15:04:28 -0400541 {
542 context->stencilFailOperationCCW = stencilFailOperation;
543 }
544
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400545 void PixelProcessor::setStencilPassOperationCCW(StencilOperation stencilPassOperation)
John Bauman89401822014-05-06 15:04:28 -0400546 {
547 context->stencilPassOperationCCW = stencilPassOperation;
548 }
549
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400550 void PixelProcessor::setStencilZFailOperationCCW(StencilOperation stencilZFailOperation)
John Bauman89401822014-05-06 15:04:28 -0400551 {
552 context->stencilZFailOperationCCW = stencilZFailOperation;
553 }
554
555 void PixelProcessor::setTextureFactor(const Color<float> &textureFactor)
556 {
557 // FIXME: Compact into generic function // FIXME: Clamp
558 short textureFactorR = iround(4095 * textureFactor.r);
559 short textureFactorG = iround(4095 * textureFactor.g);
560 short textureFactorB = iround(4095 * textureFactor.b);
561 short textureFactorA = iround(4095 * textureFactor.a);
562
563 factor.textureFactor4[0][0] = textureFactorR;
564 factor.textureFactor4[0][1] = textureFactorR;
565 factor.textureFactor4[0][2] = textureFactorR;
566 factor.textureFactor4[0][3] = textureFactorR;
567
568 factor.textureFactor4[1][0] = textureFactorG;
569 factor.textureFactor4[1][1] = textureFactorG;
570 factor.textureFactor4[1][2] = textureFactorG;
571 factor.textureFactor4[1][3] = textureFactorG;
572
573 factor.textureFactor4[2][0] = textureFactorB;
574 factor.textureFactor4[2][1] = textureFactorB;
575 factor.textureFactor4[2][2] = textureFactorB;
576 factor.textureFactor4[2][3] = textureFactorB;
577
578 factor.textureFactor4[3][0] = textureFactorA;
579 factor.textureFactor4[3][1] = textureFactorA;
580 factor.textureFactor4[3][2] = textureFactorA;
581 factor.textureFactor4[3][3] = textureFactorA;
582 }
583
584 void PixelProcessor::setBlendConstant(const Color<float> &blendConstant)
585 {
586 // FIXME: Compact into generic function // FIXME: Clamp
587 short blendConstantR = iround(65535 * blendConstant.r);
588 short blendConstantG = iround(65535 * blendConstant.g);
589 short blendConstantB = iround(65535 * blendConstant.b);
590 short blendConstantA = iround(65535 * blendConstant.a);
591
592 factor.blendConstant4W[0][0] = blendConstantR;
593 factor.blendConstant4W[0][1] = blendConstantR;
594 factor.blendConstant4W[0][2] = blendConstantR;
595 factor.blendConstant4W[0][3] = blendConstantR;
596
597 factor.blendConstant4W[1][0] = blendConstantG;
598 factor.blendConstant4W[1][1] = blendConstantG;
599 factor.blendConstant4W[1][2] = blendConstantG;
600 factor.blendConstant4W[1][3] = blendConstantG;
601
602 factor.blendConstant4W[2][0] = blendConstantB;
603 factor.blendConstant4W[2][1] = blendConstantB;
604 factor.blendConstant4W[2][2] = blendConstantB;
605 factor.blendConstant4W[2][3] = blendConstantB;
606
607 factor.blendConstant4W[3][0] = blendConstantA;
608 factor.blendConstant4W[3][1] = blendConstantA;
609 factor.blendConstant4W[3][2] = blendConstantA;
610 factor.blendConstant4W[3][3] = blendConstantA;
611
612 // FIXME: Compact into generic function // FIXME: Clamp
613 short invBlendConstantR = iround(65535 * (1 - blendConstant.r));
614 short invBlendConstantG = iround(65535 * (1 - blendConstant.g));
615 short invBlendConstantB = iround(65535 * (1 - blendConstant.b));
616 short invBlendConstantA = iround(65535 * (1 - blendConstant.a));
617
618 factor.invBlendConstant4W[0][0] = invBlendConstantR;
619 factor.invBlendConstant4W[0][1] = invBlendConstantR;
620 factor.invBlendConstant4W[0][2] = invBlendConstantR;
621 factor.invBlendConstant4W[0][3] = invBlendConstantR;
622
623 factor.invBlendConstant4W[1][0] = invBlendConstantG;
624 factor.invBlendConstant4W[1][1] = invBlendConstantG;
625 factor.invBlendConstant4W[1][2] = invBlendConstantG;
626 factor.invBlendConstant4W[1][3] = invBlendConstantG;
627
628 factor.invBlendConstant4W[2][0] = invBlendConstantB;
629 factor.invBlendConstant4W[2][1] = invBlendConstantB;
630 factor.invBlendConstant4W[2][2] = invBlendConstantB;
631 factor.invBlendConstant4W[2][3] = invBlendConstantB;
632
633 factor.invBlendConstant4W[3][0] = invBlendConstantA;
634 factor.invBlendConstant4W[3][1] = invBlendConstantA;
635 factor.invBlendConstant4W[3][2] = invBlendConstantA;
636 factor.invBlendConstant4W[3][3] = invBlendConstantA;
637
638 factor.blendConstant4F[0][0] = blendConstant.r;
639 factor.blendConstant4F[0][1] = blendConstant.r;
640 factor.blendConstant4F[0][2] = blendConstant.r;
641 factor.blendConstant4F[0][3] = blendConstant.r;
642
643 factor.blendConstant4F[1][0] = blendConstant.g;
644 factor.blendConstant4F[1][1] = blendConstant.g;
645 factor.blendConstant4F[1][2] = blendConstant.g;
646 factor.blendConstant4F[1][3] = blendConstant.g;
647
648 factor.blendConstant4F[2][0] = blendConstant.b;
649 factor.blendConstant4F[2][1] = blendConstant.b;
650 factor.blendConstant4F[2][2] = blendConstant.b;
651 factor.blendConstant4F[2][3] = blendConstant.b;
652
653 factor.blendConstant4F[3][0] = blendConstant.a;
654 factor.blendConstant4F[3][1] = blendConstant.a;
655 factor.blendConstant4F[3][2] = blendConstant.a;
656 factor.blendConstant4F[3][3] = blendConstant.a;
657
658 factor.invBlendConstant4F[0][0] = 1 - blendConstant.r;
659 factor.invBlendConstant4F[0][1] = 1 - blendConstant.r;
660 factor.invBlendConstant4F[0][2] = 1 - blendConstant.r;
661 factor.invBlendConstant4F[0][3] = 1 - blendConstant.r;
662
663 factor.invBlendConstant4F[1][0] = 1 - blendConstant.g;
664 factor.invBlendConstant4F[1][1] = 1 - blendConstant.g;
665 factor.invBlendConstant4F[1][2] = 1 - blendConstant.g;
666 factor.invBlendConstant4F[1][3] = 1 - blendConstant.g;
667
668 factor.invBlendConstant4F[2][0] = 1 - blendConstant.b;
669 factor.invBlendConstant4F[2][1] = 1 - blendConstant.b;
670 factor.invBlendConstant4F[2][2] = 1 - blendConstant.b;
671 factor.invBlendConstant4F[2][3] = 1 - blendConstant.b;
672
673 factor.invBlendConstant4F[3][0] = 1 - blendConstant.a;
674 factor.invBlendConstant4F[3][1] = 1 - blendConstant.a;
675 factor.invBlendConstant4F[3][2] = 1 - blendConstant.a;
676 factor.invBlendConstant4F[3][3] = 1 - blendConstant.a;
677 }
678
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400679 void PixelProcessor::setFillMode(FillMode fillMode)
John Bauman89401822014-05-06 15:04:28 -0400680 {
681 context->fillMode = fillMode;
682 }
683
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400684 void PixelProcessor::setShadingMode(ShadingMode shadingMode)
John Bauman89401822014-05-06 15:04:28 -0400685 {
686 context->shadingMode = shadingMode;
687 }
688
689 void PixelProcessor::setAlphaBlendEnable(bool alphaBlendEnable)
690 {
691 context->setAlphaBlendEnable(alphaBlendEnable);
692 }
693
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400694 void PixelProcessor::setSourceBlendFactor(BlendFactor sourceBlendFactor)
John Bauman89401822014-05-06 15:04:28 -0400695 {
696 context->setSourceBlendFactor(sourceBlendFactor);
697 }
698
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400699 void PixelProcessor::setDestBlendFactor(BlendFactor destBlendFactor)
John Bauman89401822014-05-06 15:04:28 -0400700 {
701 context->setDestBlendFactor(destBlendFactor);
702 }
703
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400704 void PixelProcessor::setBlendOperation(BlendOperation blendOperation)
John Bauman89401822014-05-06 15:04:28 -0400705 {
706 context->setBlendOperation(blendOperation);
707 }
708
709 void PixelProcessor::setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable)
710 {
711 context->setSeparateAlphaBlendEnable(separateAlphaBlendEnable);
712 }
713
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400714 void PixelProcessor::setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha)
John Bauman89401822014-05-06 15:04:28 -0400715 {
716 context->setSourceBlendFactorAlpha(sourceBlendFactorAlpha);
717 }
718
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400719 void PixelProcessor::setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha)
John Bauman89401822014-05-06 15:04:28 -0400720 {
721 context->setDestBlendFactorAlpha(destBlendFactorAlpha);
722 }
723
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400724 void PixelProcessor::setBlendOperationAlpha(BlendOperation blendOperationAlpha)
John Bauman89401822014-05-06 15:04:28 -0400725 {
726 context->setBlendOperationAlpha(blendOperationAlpha);
727 }
728
Alexis Hetua818c452015-06-11 13:06:58 -0400729 void PixelProcessor::setAlphaReference(float alphaReference)
John Bauman89401822014-05-06 15:04:28 -0400730 {
731 context->alphaReference = alphaReference;
732
Alexis Hetua818c452015-06-11 13:06:58 -0400733 factor.alphaReference4[0] = (word)iround(alphaReference * 0x1000 / 0xFF);
734 factor.alphaReference4[1] = (word)iround(alphaReference * 0x1000 / 0xFF);
735 factor.alphaReference4[2] = (word)iround(alphaReference * 0x1000 / 0xFF);
736 factor.alphaReference4[3] = (word)iround(alphaReference * 0x1000 / 0xFF);
John Bauman89401822014-05-06 15:04:28 -0400737 }
738
739 void PixelProcessor::setGlobalMipmapBias(float bias)
740 {
741 context->setGlobalMipmapBias(bias);
742 }
743
744 void PixelProcessor::setFogStart(float start)
745 {
746 setFogRanges(start, context->fogEnd);
747 }
748
749 void PixelProcessor::setFogEnd(float end)
750 {
751 setFogRanges(context->fogStart, end);
752 }
753
754 void PixelProcessor::setFogColor(Color<float> fogColor)
755 {
756 // TODO: Compact into generic function
757 word fogR = (unsigned short)(65535 * fogColor.r);
758 word fogG = (unsigned short)(65535 * fogColor.g);
759 word fogB = (unsigned short)(65535 * fogColor.b);
760
761 fog.color4[0][0] = fogR;
762 fog.color4[0][1] = fogR;
763 fog.color4[0][2] = fogR;
764 fog.color4[0][3] = fogR;
765
766 fog.color4[1][0] = fogG;
767 fog.color4[1][1] = fogG;
768 fog.color4[1][2] = fogG;
769 fog.color4[1][3] = fogG;
770
771 fog.color4[2][0] = fogB;
772 fog.color4[2][1] = fogB;
773 fog.color4[2][2] = fogB;
774 fog.color4[2][3] = fogB;
775
776 fog.colorF[0] = replicate(fogColor.r);
777 fog.colorF[1] = replicate(fogColor.g);
778 fog.colorF[2] = replicate(fogColor.b);
779 }
780
781 void PixelProcessor::setFogDensity(float fogDensity)
782 {
783 fog.densityE = replicate(-fogDensity * 1.442695f); // 1/e^x = 2^(-x*1.44)
Nicolas Capensa36f3f92015-08-04 15:34:26 -0400784 fog.density2E = replicate(-fogDensity * fogDensity * 1.442695f);
John Bauman89401822014-05-06 15:04:28 -0400785 }
786
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400787 void PixelProcessor::setPixelFogMode(FogMode fogMode)
John Bauman89401822014-05-06 15:04:28 -0400788 {
789 context->pixelFogMode = fogMode;
790 }
791
792 void PixelProcessor::setPerspectiveCorrection(bool perspectiveEnable)
793 {
794 perspectiveCorrection = perspectiveEnable;
795 }
796
797 void PixelProcessor::setOcclusionEnabled(bool enable)
798 {
799 context->occlusionEnabled = enable;
800 }
801
802 void PixelProcessor::setRoutineCacheSize(int cacheSize)
803 {
804 delete routineCache;
John Bauman66b8ab22014-05-06 15:57:45 -0400805 routineCache = new RoutineCache<State>(clamp(cacheSize, 1, 65536), precachePixel ? "sw-pixel" : 0);
John Bauman89401822014-05-06 15:04:28 -0400806 }
807
808 void PixelProcessor::setFogRanges(float start, float end)
809 {
810 context->fogStart = start;
811 context->fogEnd = end;
812
813 if(start == end)
814 {
815 end += 0.001f; // Hack: ensure there is a small range
816 }
817
818 float fogScale = -1.0f / (end - start);
819 float fogOffset = end * -fogScale;
820
821 fog.scale = replicate(fogScale);
822 fog.offset = replicate(fogOffset);
823 }
824
825 const PixelProcessor::State PixelProcessor::update() const
826 {
827 State state;
828
829 if(context->pixelShader)
830 {
John Bauman19bac1e2014-05-06 15:23:49 -0400831 state.shaderID = context->pixelShader->getSerialID();
John Bauman89401822014-05-06 15:04:28 -0400832 }
833 else
834 {
John Bauman19bac1e2014-05-06 15:23:49 -0400835 state.shaderID = 0;
John Bauman89401822014-05-06 15:04:28 -0400836 }
837
838 state.depthOverride = context->pixelShader && context->pixelShader->depthOverride();
John Bauman19bac1e2014-05-06 15:23:49 -0400839 state.shaderContainsKill = context->pixelShader ? context->pixelShader->containsKill() : false;
John Bauman89401822014-05-06 15:04:28 -0400840
841 if(context->alphaTestActive())
842 {
843 state.alphaCompareMode = context->alphaCompareMode;
844
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400845 state.transparencyAntialiasing = context->getMultiSampleCount() > 1 ? transparencyAntialiasing : TRANSPARENCY_NONE;
John Bauman89401822014-05-06 15:04:28 -0400846 }
847
848 state.depthWriteEnable = context->depthWriteActive();
849
850 if(context->stencilActive())
851 {
852 state.stencilActive = true;
853 state.stencilCompareMode = context->stencilCompareMode;
854 state.stencilFailOperation = context->stencilFailOperation;
855 state.stencilPassOperation = context->stencilPassOperation;
856 state.stencilZFailOperation = context->stencilZFailOperation;
857 state.noStencilMask = (context->stencilMask == 0xFF);
858 state.noStencilWriteMask = (context->stencilWriteMask == 0xFF);
859 state.stencilWriteMasked = (context->stencilWriteMask == 0x00);
860
861 state.twoSidedStencil = context->twoSidedStencil;
862 state.stencilCompareModeCCW = context->twoSidedStencil ? context->stencilCompareModeCCW : state.stencilCompareMode;
863 state.stencilFailOperationCCW = context->twoSidedStencil ? context->stencilFailOperationCCW : state.stencilFailOperation;
864 state.stencilPassOperationCCW = context->twoSidedStencil ? context->stencilPassOperationCCW : state.stencilPassOperation;
865 state.stencilZFailOperationCCW = context->twoSidedStencil ? context->stencilZFailOperationCCW : state.stencilZFailOperation;
866 state.noStencilMaskCCW = context->twoSidedStencil ? (context->stencilMaskCCW == 0xFF) : state.noStencilMask;
867 state.noStencilWriteMaskCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0xFF) : state.noStencilWriteMask;
868 state.stencilWriteMaskedCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0x00) : state.stencilWriteMasked;
869 }
870
871 if(context->depthBufferActive())
872 {
873 state.depthTestActive = true;
874 state.depthCompareMode = context->depthCompareMode;
875 state.quadLayoutDepthBuffer = context->depthStencil->getInternalFormat() != FORMAT_D32F_LOCKABLE &&
John Bauman66b8ab22014-05-06 15:57:45 -0400876 context->depthStencil->getInternalFormat() != FORMAT_D32FS8_TEXTURE &&
877 context->depthStencil->getInternalFormat() != FORMAT_D32FS8_SHADOW;
John Bauman89401822014-05-06 15:04:28 -0400878 }
879
880 state.occlusionEnabled = context->occlusionEnabled;
881
882 state.fogActive = context->fogActive();
883 state.pixelFogMode = context->pixelFogActive();
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400884 state.wBasedFog = context->wBasedFog && context->pixelFogActive() != FOG_NONE;
John Bauman89401822014-05-06 15:04:28 -0400885 state.perspective = context->perspectiveActive();
886
887 if(context->alphaBlendActive())
888 {
889 state.alphaBlendActive = true;
890 state.sourceBlendFactor = context->sourceBlendFactor();
891 state.destBlendFactor = context->destBlendFactor();
892 state.blendOperation = context->blendOperation();
893 state.sourceBlendFactorAlpha = context->sourceBlendFactorAlpha();
894 state.destBlendFactorAlpha = context->destBlendFactorAlpha();
895 state.blendOperationAlpha = context->blendOperationAlpha();
896 }
Maxime Grégoired9762742015-07-08 16:43:48 -0400897
898 state.logicalOperation = context->colorLogicOp();
899
John Bauman89401822014-05-06 15:04:28 -0400900 state.colorWriteMask = (context->colorWriteActive(0) << 0) |
901 (context->colorWriteActive(1) << 4) |
902 (context->colorWriteActive(2) << 8) |
903 (context->colorWriteActive(3) << 12);
904
905 for(int i = 0; i < 4; i++)
906 {
907 state.targetFormat[i] = context->renderTargetInternalFormat(i);
908 }
909
John Bauman66b8ab22014-05-06 15:57:45 -0400910 state.writeSRGB = context->writeSRGB && context->renderTarget[0] && Surface::isSRGBwritable(context->renderTarget[0]->getExternalFormat());
911 state.multiSample = context->getMultiSampleCount();
John Bauman89401822014-05-06 15:04:28 -0400912 state.multiSampleMask = context->multiSampleMask;
913
914 if(state.multiSample > 1 && context->pixelShader)
915 {
916 state.centroid = context->pixelShader->containsCentroid();
917 }
918
919 if(!context->pixelShader)
920 {
921 for(unsigned int i = 0; i < 8; i++)
922 {
923 state.textureStage[i] = context->textureStage[i].textureStageState();
924 }
925
926 state.specularAdd = context->specularActive() && context->specularEnable;
927 }
928
929 for(unsigned int i = 0; i < 16; i++)
930 {
931 if(context->pixelShader)
932 {
933 if(context->pixelShader->usesSampler(i))
934 {
935 state.sampler[i] = context->sampler[i].samplerState();
936 }
937 }
938 else
939 {
940 if(i < 8 && state.textureStage[i].stageOperation != TextureStage::STAGE_DISABLE)
941 {
942 state.sampler[i] = context->sampler[i].samplerState();
943 }
944 else break;
945 }
946 }
947
948 const bool point = context->isDrawPoint(true);
949 const bool sprite = context->pointSpriteActive();
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400950 const bool flatShading = (context->shadingMode == SHADING_FLAT) || point;
John Bauman89401822014-05-06 15:04:28 -0400951
952 if(context->pixelShaderVersion() < 0x0300)
953 {
954 for(int coordinate = 0; coordinate < 8; coordinate++)
955 {
956 for(int component = 0; component < 4; component++)
957 {
958 if(context->textureActive(coordinate, component))
959 {
960 state.texture[coordinate].component |= 1 << component;
961
962 if(point && !sprite)
963 {
964 state.texture[coordinate].flat |= 1 << component;
965 }
966 }
967 }
968
969 if(context->textureTransformProject[coordinate] && context->pixelShaderVersion() <= 0x0103)
970 {
971 if(context->textureTransformCount[coordinate] == 2)
972 {
973 state.texture[coordinate].project = 1;
974 }
975 else if(context->textureTransformCount[coordinate] == 3)
976 {
977 state.texture[coordinate].project = 2;
978 }
979 else if(context->textureTransformCount[coordinate] == 4 || context->textureTransformCount[coordinate] == 0)
980 {
981 state.texture[coordinate].project = 3;
982 }
983 }
984 }
985
986 for(int color = 0; color < 2; color++)
987 {
988 for(int component = 0; component < 4; component++)
989 {
990 if(context->colorActive(color, component))
991 {
992 state.color[color].component |= 1 << component;
993
994 if(point || flatShading)
995 {
996 state.color[color].flat |= 1 << component;
997 }
998 }
999 }
1000 }
1001
1002 if(context->fogActive())
1003 {
1004 state.fog.component = true;
1005
1006 if(point)
1007 {
1008 state.fog.flat = true;
1009 }
1010 }
1011 }
1012 else
1013 {
1014 for(int interpolant = 0; interpolant < 10; interpolant++)
1015 {
1016 for(int component = 0; component < 4; component++)
1017 {
1018 if(context->pixelShader->semantic[interpolant][component].active())
1019 {
1020 bool flat = point;
1021
1022 switch(context->pixelShader->semantic[interpolant][component].usage)
1023 {
John Bauman19bac1e2014-05-06 15:23:49 -04001024 case Shader::USAGE_TEXCOORD: flat = point && !sprite; break;
1025 case Shader::USAGE_COLOR: flat = flatShading; break;
John Bauman89401822014-05-06 15:04:28 -04001026 }
1027
1028 state.interpolant[interpolant].component |= 1 << component;
1029
1030 if(flat)
1031 {
1032 state.interpolant[interpolant].flat |= 1 << component;
1033 }
1034 }
1035 }
1036 }
1037 }
1038
1039 if(state.centroid)
1040 {
1041 for(int interpolant = 0; interpolant < 10; interpolant++)
1042 {
1043 for(int component = 0; component < 4; component++)
1044 {
1045 state.interpolant[interpolant].centroid = context->pixelShader->semantic[interpolant][0].centroid;
1046 }
1047 }
1048 }
1049
1050 state.hash = state.computeHash();
1051
1052 return state;
1053 }
1054
1055 Routine *PixelProcessor::routine(const State &state)
1056 {
1057 Routine *routine = routineCache->query(state);
1058
1059 if(!routine)
1060 {
Alexis Hetuf2a8c372015-07-13 11:08:41 -04001061 const bool integerPipeline = (context->pixelShaderVersion() <= 0x0104);
1062 Rasterizer *generator = nullptr;
1063 if(integerPipeline)
1064 {
1065 generator = new PixelPipeline(state, context->pixelShader);
1066 }
1067 else
1068 {
1069 generator = new PixelProgram(state, context->pixelShader);
1070 }
John Bauman89401822014-05-06 15:04:28 -04001071 generator->generate();
1072 routine = generator->getRoutine();
1073 delete generator;
1074
1075 routineCache->add(state, routine);
1076 }
1077
1078 return routine;
1079 }
1080}