OSDN Git Service

Derive Rasterizer from Function<>.
[android-x86/external-swiftshader.git] / src / Renderer / PixelProcessor.cpp
1 // SwiftShader Software Renderer
2 //
3 // Copyright(c) 2005-2013 TransGaming Inc.
4 //
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
14 #include "PixelPipeline.hpp"
15 #include "PixelProgram.hpp"
16 #include "PixelShader.hpp"
17 #include "MetaMacro.hpp"
18 #include "Surface.hpp"
19 #include "Primitive.hpp"
20 #include "Constants.hpp"
21 #include "Debug.hpp"
22
23 #include <string.h>
24
25 namespace sw
26 {
27         extern bool complementaryDepthBuffer;
28         extern TransparencyAntialiasing transparencyAntialiasing;
29         extern bool perspectiveCorrection;
30
31         bool precachePixel = false;
32
33         unsigned int PixelProcessor::States::computeHash()
34         {
35                 unsigned int *state = (unsigned int*)this;
36                 unsigned int hash = 0;
37
38                 for(unsigned 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         {
79                 if(index < FRAGMENT_UNIFORM_VECTORS)
80                 {
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         {
331                 if(sampler < TEXTURE_IMAGE_UNITS)
332                 {
333                         context->sampler[sampler].setTextureFilter(textureFilter);
334                 }
335                 else ASSERT(false);
336         }
337
338         void PixelProcessor::setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter)
339         {
340                 if(sampler < TEXTURE_IMAGE_UNITS)
341                 {
342                         context->sampler[sampler].setMipmapFilter(mipmapFilter);
343                 }
344                 else ASSERT(false);
345         }
346
347         void PixelProcessor::setGatherEnable(unsigned int sampler, bool enable)
348         {
349                 if(sampler < TEXTURE_IMAGE_UNITS)
350                 {
351                         context->sampler[sampler].setGatherEnable(enable);
352                 }
353                 else ASSERT(false);
354         }
355
356         void PixelProcessor::setAddressingModeU(unsigned int sampler, AddressingMode addressMode)
357         {
358                 if(sampler < TEXTURE_IMAGE_UNITS)
359                 {
360                         context->sampler[sampler].setAddressingModeU(addressMode);
361                 }
362                 else ASSERT(false);
363         }
364
365         void PixelProcessor::setAddressingModeV(unsigned int sampler, AddressingMode addressMode)
366         {
367                 if(sampler < TEXTURE_IMAGE_UNITS)
368                 {
369                         context->sampler[sampler].setAddressingModeV(addressMode);
370                 }
371                 else ASSERT(false);
372         }
373
374         void PixelProcessor::setAddressingModeW(unsigned int sampler, AddressingMode addressMode)
375         {
376                 if(sampler < TEXTURE_IMAGE_UNITS)
377                 {
378                         context->sampler[sampler].setAddressingModeW(addressMode);
379                 }
380                 else ASSERT(false);
381         }
382
383         void PixelProcessor::setReadSRGB(unsigned int sampler, bool sRGB)
384         {
385                 if(sampler < TEXTURE_IMAGE_UNITS)
386                 {
387                         context->sampler[sampler].setReadSRGB(sRGB);
388                 }
389                 else ASSERT(false);
390         }
391
392         void PixelProcessor::setMipmapLOD(unsigned int sampler, float bias)
393         {
394                 if(sampler < TEXTURE_IMAGE_UNITS)
395                 {
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         {
403                 if(sampler < TEXTURE_IMAGE_UNITS)
404                 {
405                         context->sampler[sampler].setBorderColor(borderColor);
406                 }
407                 else ASSERT(false);
408         }
409
410         void PixelProcessor::setMaxAnisotropy(unsigned int sampler, float maxAnisotropy)
411         {
412                 if(sampler < TEXTURE_IMAGE_UNITS)
413                 {
414                         context->sampler[sampler].setMaxAnisotropy(maxAnisotropy);
415                 }
416                 else ASSERT(false);
417         }
418
419         void PixelProcessor::setSwizzleR(unsigned int sampler, SwizzleType swizzleR)
420         {
421                 if(sampler < TEXTURE_IMAGE_UNITS)
422                 {
423                         context->sampler[sampler].setSwizzleR(swizzleR);
424                 }
425                 else ASSERT(false);
426         }
427
428         void PixelProcessor::setSwizzleG(unsigned int sampler, SwizzleType swizzleG)
429         {
430                 if(sampler < TEXTURE_IMAGE_UNITS)
431                 {
432                         context->sampler[sampler].setSwizzleG(swizzleG);
433                 }
434                 else ASSERT(false);
435         }
436
437         void PixelProcessor::setSwizzleB(unsigned int sampler, SwizzleType swizzleB)
438         {
439                 if(sampler < TEXTURE_IMAGE_UNITS)
440                 {
441                         context->sampler[sampler].setSwizzleB(swizzleB);
442                 }
443                 else ASSERT(false);
444         }
445
446         void PixelProcessor::setSwizzleA(unsigned int sampler, SwizzleType swizzleA)
447         {
448                 if(sampler < TEXTURE_IMAGE_UNITS)
449                 {
450                         context->sampler[sampler].setSwizzleA(swizzleA);
451                 }
452                 else ASSERT(false);
453         }
454
455         void PixelProcessor::setWriteSRGB(bool sRGB)
456         {
457                 context->setWriteSRGB(sRGB);
458         }
459
460         void PixelProcessor::setColorLogicOpEnabled(bool colorLogicOpEnabled)
461         {
462                 context->setColorLogicOpEnabled(colorLogicOpEnabled);
463         }
464
465         void PixelProcessor::setLogicalOperation(LogicalOperation logicalOperation)
466         {
467                 context->setLogicalOperation(logicalOperation);
468         }
469
470         void PixelProcessor::setDepthBufferEnable(bool depthBufferEnable)
471         {
472                 context->setDepthBufferEnable(depthBufferEnable);
473         }
474
475         void PixelProcessor::setDepthCompare(DepthCompareMode depthCompareMode)
476         {
477                 context->depthCompareMode = depthCompareMode;
478         }
479
480         void PixelProcessor::setAlphaCompare(AlphaCompareMode alphaCompareMode)
481         {
482                 context->alphaCompareMode = alphaCompareMode;
483         }
484
485         void PixelProcessor::setDepthWriteEnable(bool depthWriteEnable)
486         {
487                 context->depthWriteEnable = depthWriteEnable;
488         }
489
490         void PixelProcessor::setAlphaTestEnable(bool alphaTestEnable)
491         {
492                 context->alphaTestEnable = alphaTestEnable;
493         }
494
495         void PixelProcessor::setCullMode(CullMode cullMode)
496         {
497                 context->cullMode = cullMode;
498         }
499
500         void PixelProcessor::setColorWriteMask(int index, int rgbaMask)
501         {
502                 context->setColorWriteMask(index, rgbaMask);
503         }
504
505         void PixelProcessor::setStencilEnable(bool stencilEnable)
506         {
507                 context->stencilEnable = stencilEnable;
508         }
509
510         void PixelProcessor::setStencilCompare(StencilCompareMode stencilCompareMode)
511         {
512                 context->stencilCompareMode = stencilCompareMode;
513         }
514
515         void PixelProcessor::setStencilReference(int stencilReference)
516         {
517                 context->stencilReference = stencilReference;
518                 stencil.set(stencilReference, context->stencilMask, context->stencilWriteMask);
519         }
520
521         void PixelProcessor::setStencilReferenceCCW(int stencilReferenceCCW)
522         {
523                 context->stencilReferenceCCW = stencilReferenceCCW;
524                 stencilCCW.set(stencilReferenceCCW, context->stencilMaskCCW, context->stencilWriteMaskCCW);
525         }
526
527         void PixelProcessor::setStencilMask(int stencilMask)
528         {
529                 context->stencilMask = stencilMask;
530                 stencil.set(context->stencilReference, stencilMask, context->stencilWriteMask);
531         }
532
533         void PixelProcessor::setStencilMaskCCW(int stencilMaskCCW)
534         {
535                 context->stencilMaskCCW = stencilMaskCCW;
536                 stencilCCW.set(context->stencilReferenceCCW, stencilMaskCCW, context->stencilWriteMaskCCW);
537         }
538
539         void PixelProcessor::setStencilFailOperation(StencilOperation stencilFailOperation)
540         {
541                 context->stencilFailOperation = stencilFailOperation;
542         }
543
544         void PixelProcessor::setStencilPassOperation(StencilOperation stencilPassOperation)
545         {
546                 context->stencilPassOperation = stencilPassOperation;
547         }
548
549         void PixelProcessor::setStencilZFailOperation(StencilOperation stencilZFailOperation)
550         {
551                 context->stencilZFailOperation = stencilZFailOperation;
552         }
553
554         void PixelProcessor::setStencilWriteMask(int stencilWriteMask)
555         {
556                 context->stencilWriteMask = stencilWriteMask;
557                 stencil.set(context->stencilReference, context->stencilMask, stencilWriteMask);
558         }
559
560         void PixelProcessor::setStencilWriteMaskCCW(int stencilWriteMaskCCW)
561         {
562                 context->stencilWriteMaskCCW = stencilWriteMaskCCW;
563                 stencilCCW.set(context->stencilReferenceCCW, context->stencilMaskCCW, stencilWriteMaskCCW);
564         }
565
566         void PixelProcessor::setTwoSidedStencil(bool enable)
567         {
568                 context->twoSidedStencil = enable;
569         }
570
571         void PixelProcessor::setStencilCompareCCW(StencilCompareMode stencilCompareMode)
572         {
573                 context->stencilCompareModeCCW = stencilCompareMode;
574         }
575
576         void PixelProcessor::setStencilFailOperationCCW(StencilOperation stencilFailOperation)
577         {
578                 context->stencilFailOperationCCW = stencilFailOperation;
579         }
580
581         void PixelProcessor::setStencilPassOperationCCW(StencilOperation stencilPassOperation)
582         {
583                 context->stencilPassOperationCCW = stencilPassOperation;
584         }
585
586         void PixelProcessor::setStencilZFailOperationCCW(StencilOperation stencilZFailOperation)
587         {
588                 context->stencilZFailOperationCCW = stencilZFailOperation;
589         }
590
591         void PixelProcessor::setTextureFactor(const Color<float> &textureFactor)
592         {
593                 // FIXME: Compact into generic function   // FIXME: Clamp
594                 short textureFactorR = iround(4095 * textureFactor.r);
595                 short textureFactorG = iround(4095 * textureFactor.g);
596                 short textureFactorB = iround(4095 * textureFactor.b);
597                 short textureFactorA = iround(4095 * textureFactor.a);
598
599                 factor.textureFactor4[0][0] = textureFactorR;
600                 factor.textureFactor4[0][1] = textureFactorR;
601                 factor.textureFactor4[0][2] = textureFactorR;
602                 factor.textureFactor4[0][3] = textureFactorR;
603
604                 factor.textureFactor4[1][0] = textureFactorG;
605                 factor.textureFactor4[1][1] = textureFactorG;
606                 factor.textureFactor4[1][2] = textureFactorG;
607                 factor.textureFactor4[1][3] = textureFactorG;
608
609                 factor.textureFactor4[2][0] = textureFactorB;
610                 factor.textureFactor4[2][1] = textureFactorB;
611                 factor.textureFactor4[2][2] = textureFactorB;
612                 factor.textureFactor4[2][3] = textureFactorB;
613
614                 factor.textureFactor4[3][0] = textureFactorA;
615                 factor.textureFactor4[3][1] = textureFactorA;
616                 factor.textureFactor4[3][2] = textureFactorA;
617                 factor.textureFactor4[3][3] = textureFactorA;
618         }
619
620         void PixelProcessor::setBlendConstant(const Color<float> &blendConstant)
621         {
622                 // FIXME: Compact into generic function   // FIXME: Clamp
623                 short blendConstantR = iround(65535 * blendConstant.r);
624                 short blendConstantG = iround(65535 * blendConstant.g);
625                 short blendConstantB = iround(65535 * blendConstant.b);
626                 short blendConstantA = iround(65535 * blendConstant.a);
627
628                 factor.blendConstant4W[0][0] = blendConstantR;
629                 factor.blendConstant4W[0][1] = blendConstantR;
630                 factor.blendConstant4W[0][2] = blendConstantR;
631                 factor.blendConstant4W[0][3] = blendConstantR;
632
633                 factor.blendConstant4W[1][0] = blendConstantG;
634                 factor.blendConstant4W[1][1] = blendConstantG;
635                 factor.blendConstant4W[1][2] = blendConstantG;
636                 factor.blendConstant4W[1][3] = blendConstantG;
637
638                 factor.blendConstant4W[2][0] = blendConstantB;
639                 factor.blendConstant4W[2][1] = blendConstantB;
640                 factor.blendConstant4W[2][2] = blendConstantB;
641                 factor.blendConstant4W[2][3] = blendConstantB;
642
643                 factor.blendConstant4W[3][0] = blendConstantA;
644                 factor.blendConstant4W[3][1] = blendConstantA;
645                 factor.blendConstant4W[3][2] = blendConstantA;
646                 factor.blendConstant4W[3][3] = blendConstantA;
647
648                 // FIXME: Compact into generic function   // FIXME: Clamp
649                 short invBlendConstantR = iround(65535 * (1 - blendConstant.r));
650                 short invBlendConstantG = iround(65535 * (1 - blendConstant.g));
651                 short invBlendConstantB = iround(65535 * (1 - blendConstant.b));
652                 short invBlendConstantA = iround(65535 * (1 - blendConstant.a));
653
654                 factor.invBlendConstant4W[0][0] = invBlendConstantR;
655                 factor.invBlendConstant4W[0][1] = invBlendConstantR;
656                 factor.invBlendConstant4W[0][2] = invBlendConstantR;
657                 factor.invBlendConstant4W[0][3] = invBlendConstantR;
658
659                 factor.invBlendConstant4W[1][0] = invBlendConstantG;
660                 factor.invBlendConstant4W[1][1] = invBlendConstantG;
661                 factor.invBlendConstant4W[1][2] = invBlendConstantG;
662                 factor.invBlendConstant4W[1][3] = invBlendConstantG;
663
664                 factor.invBlendConstant4W[2][0] = invBlendConstantB;
665                 factor.invBlendConstant4W[2][1] = invBlendConstantB;
666                 factor.invBlendConstant4W[2][2] = invBlendConstantB;
667                 factor.invBlendConstant4W[2][3] = invBlendConstantB;
668
669                 factor.invBlendConstant4W[3][0] = invBlendConstantA;
670                 factor.invBlendConstant4W[3][1] = invBlendConstantA;
671                 factor.invBlendConstant4W[3][2] = invBlendConstantA;
672                 factor.invBlendConstant4W[3][3] = invBlendConstantA;
673
674                 factor.blendConstant4F[0][0] = blendConstant.r;
675                 factor.blendConstant4F[0][1] = blendConstant.r;
676                 factor.blendConstant4F[0][2] = blendConstant.r;
677                 factor.blendConstant4F[0][3] = blendConstant.r;
678
679                 factor.blendConstant4F[1][0] = blendConstant.g;
680                 factor.blendConstant4F[1][1] = blendConstant.g;
681                 factor.blendConstant4F[1][2] = blendConstant.g;
682                 factor.blendConstant4F[1][3] = blendConstant.g;
683
684                 factor.blendConstant4F[2][0] = blendConstant.b;
685                 factor.blendConstant4F[2][1] = blendConstant.b;
686                 factor.blendConstant4F[2][2] = blendConstant.b;
687                 factor.blendConstant4F[2][3] = blendConstant.b;
688
689                 factor.blendConstant4F[3][0] = blendConstant.a;
690                 factor.blendConstant4F[3][1] = blendConstant.a;
691                 factor.blendConstant4F[3][2] = blendConstant.a;
692                 factor.blendConstant4F[3][3] = blendConstant.a;
693
694                 factor.invBlendConstant4F[0][0] = 1 - blendConstant.r;
695                 factor.invBlendConstant4F[0][1] = 1 - blendConstant.r;
696                 factor.invBlendConstant4F[0][2] = 1 - blendConstant.r;
697                 factor.invBlendConstant4F[0][3] = 1 - blendConstant.r;
698
699                 factor.invBlendConstant4F[1][0] = 1 - blendConstant.g;
700                 factor.invBlendConstant4F[1][1] = 1 - blendConstant.g;
701                 factor.invBlendConstant4F[1][2] = 1 - blendConstant.g;
702                 factor.invBlendConstant4F[1][3] = 1 - blendConstant.g;
703
704                 factor.invBlendConstant4F[2][0] = 1 - blendConstant.b;
705                 factor.invBlendConstant4F[2][1] = 1 - blendConstant.b;
706                 factor.invBlendConstant4F[2][2] = 1 - blendConstant.b;
707                 factor.invBlendConstant4F[2][3] = 1 - blendConstant.b;
708
709                 factor.invBlendConstant4F[3][0] = 1 - blendConstant.a;
710                 factor.invBlendConstant4F[3][1] = 1 - blendConstant.a;
711                 factor.invBlendConstant4F[3][2] = 1 - blendConstant.a;
712                 factor.invBlendConstant4F[3][3] = 1 - blendConstant.a;
713         }
714
715         void PixelProcessor::setFillMode(FillMode fillMode)
716         {
717                 context->fillMode = fillMode;
718         }
719
720         void PixelProcessor::setShadingMode(ShadingMode shadingMode)
721         {
722                 context->shadingMode = shadingMode;
723         }
724
725         void PixelProcessor::setAlphaBlendEnable(bool alphaBlendEnable)
726         {
727                 context->setAlphaBlendEnable(alphaBlendEnable);
728         }
729
730         void PixelProcessor::setSourceBlendFactor(BlendFactor sourceBlendFactor)
731         {
732                 context->setSourceBlendFactor(sourceBlendFactor);
733         }
734
735         void PixelProcessor::setDestBlendFactor(BlendFactor destBlendFactor)
736         {
737                 context->setDestBlendFactor(destBlendFactor);
738         }
739
740         void PixelProcessor::setBlendOperation(BlendOperation blendOperation)
741         {
742                 context->setBlendOperation(blendOperation);
743         }
744
745         void PixelProcessor::setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable)
746         {
747                 context->setSeparateAlphaBlendEnable(separateAlphaBlendEnable);
748         }
749
750         void PixelProcessor::setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha)
751         {
752                 context->setSourceBlendFactorAlpha(sourceBlendFactorAlpha);
753         }
754
755         void PixelProcessor::setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha)
756         {
757                 context->setDestBlendFactorAlpha(destBlendFactorAlpha);
758         }
759
760         void PixelProcessor::setBlendOperationAlpha(BlendOperation blendOperationAlpha)
761         {
762                 context->setBlendOperationAlpha(blendOperationAlpha);
763         }
764
765         void PixelProcessor::setAlphaReference(float alphaReference)
766         {
767                 context->alphaReference = alphaReference;
768
769                 factor.alphaReference4[0] = (word)iround(alphaReference * 0x1000 / 0xFF);
770                 factor.alphaReference4[1] = (word)iround(alphaReference * 0x1000 / 0xFF);
771                 factor.alphaReference4[2] = (word)iround(alphaReference * 0x1000 / 0xFF);
772                 factor.alphaReference4[3] = (word)iround(alphaReference * 0x1000 / 0xFF);
773         }
774
775         void PixelProcessor::setGlobalMipmapBias(float bias)
776         {
777                 context->setGlobalMipmapBias(bias);
778         }
779
780         void PixelProcessor::setFogStart(float start)
781         {
782                 setFogRanges(start, context->fogEnd);
783         }
784
785         void PixelProcessor::setFogEnd(float end)
786         {
787                 setFogRanges(context->fogStart, end);
788         }
789
790         void PixelProcessor::setFogColor(Color<float> fogColor)
791         {
792                 // TODO: Compact into generic function
793                 word fogR = (unsigned short)(65535 * fogColor.r);
794                 word fogG = (unsigned short)(65535 * fogColor.g);
795                 word fogB = (unsigned short)(65535 * fogColor.b);
796
797                 fog.color4[0][0] = fogR;
798                 fog.color4[0][1] = fogR;
799                 fog.color4[0][2] = fogR;
800                 fog.color4[0][3] = fogR;
801
802                 fog.color4[1][0] = fogG;
803                 fog.color4[1][1] = fogG;
804                 fog.color4[1][2] = fogG;
805                 fog.color4[1][3] = fogG;
806
807                 fog.color4[2][0] = fogB;
808                 fog.color4[2][1] = fogB;
809                 fog.color4[2][2] = fogB;
810                 fog.color4[2][3] = fogB;
811
812                 fog.colorF[0] = replicate(fogColor.r);
813                 fog.colorF[1] = replicate(fogColor.g);
814                 fog.colorF[2] = replicate(fogColor.b);
815         }
816
817         void PixelProcessor::setFogDensity(float fogDensity)
818         {
819                 fog.densityE = replicate(-fogDensity * 1.442695f);   // 1/e^x = 2^(-x*1.44)
820                 fog.density2E = replicate(-fogDensity * fogDensity * 1.442695f);
821         }
822
823         void PixelProcessor::setPixelFogMode(FogMode fogMode)
824         {
825                 context->pixelFogMode = fogMode;
826         }
827
828         void PixelProcessor::setPerspectiveCorrection(bool perspectiveEnable)
829         {
830                 perspectiveCorrection = perspectiveEnable;
831         }
832
833         void PixelProcessor::setOcclusionEnabled(bool enable)
834         {
835                 context->occlusionEnabled = enable;
836         }
837
838         void PixelProcessor::setRoutineCacheSize(int cacheSize)
839         {
840                 delete routineCache;
841                 routineCache = new RoutineCache<State>(clamp(cacheSize, 1, 65536), precachePixel ? "sw-pixel" : 0);
842         }
843
844         void PixelProcessor::setFogRanges(float start, float end)
845         {
846                 context->fogStart = start;
847                 context->fogEnd = end;
848
849                 if(start == end)
850                 {
851                         end += 0.001f;   // Hack: ensure there is a small range
852                 }
853
854                 float fogScale = -1.0f / (end - start);
855                 float fogOffset = end * -fogScale;
856
857                 fog.scale = replicate(fogScale);
858                 fog.offset = replicate(fogOffset);
859         }
860
861         const PixelProcessor::State PixelProcessor::update() const
862         {
863                 State state;
864
865                 if(context->pixelShader)
866                 {
867                         state.shaderID = context->pixelShader->getSerialID();
868                 }
869                 else
870                 {
871                         state.shaderID = 0;
872                 }
873
874                 state.depthOverride = context->pixelShader && context->pixelShader->depthOverride();
875                 state.shaderContainsKill = context->pixelShader ? context->pixelShader->containsKill() : false;
876
877                 if(context->alphaTestActive())
878                 {
879                         state.alphaCompareMode = context->alphaCompareMode;
880
881                         state.transparencyAntialiasing = context->getMultiSampleCount() > 1 ? transparencyAntialiasing : TRANSPARENCY_NONE;
882                 }
883
884                 state.depthWriteEnable = context->depthWriteActive();
885
886                 if(context->stencilActive())
887                 {
888                         state.stencilActive = true;
889                         state.stencilCompareMode = context->stencilCompareMode;
890                         state.stencilFailOperation = context->stencilFailOperation;
891                         state.stencilPassOperation = context->stencilPassOperation;
892                         state.stencilZFailOperation = context->stencilZFailOperation;
893                         state.noStencilMask = (context->stencilMask == 0xFF);
894                         state.noStencilWriteMask = (context->stencilWriteMask == 0xFF);
895                         state.stencilWriteMasked = (context->stencilWriteMask == 0x00);
896
897                         state.twoSidedStencil = context->twoSidedStencil;
898                         state.stencilCompareModeCCW = context->twoSidedStencil ? context->stencilCompareModeCCW : state.stencilCompareMode;
899                         state.stencilFailOperationCCW = context->twoSidedStencil ? context->stencilFailOperationCCW : state.stencilFailOperation;
900                         state.stencilPassOperationCCW = context->twoSidedStencil ? context->stencilPassOperationCCW : state.stencilPassOperation;
901                         state.stencilZFailOperationCCW = context->twoSidedStencil ? context->stencilZFailOperationCCW : state.stencilZFailOperation;
902                         state.noStencilMaskCCW = context->twoSidedStencil ? (context->stencilMaskCCW == 0xFF) : state.noStencilMask;
903                         state.noStencilWriteMaskCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0xFF) : state.noStencilWriteMask;
904                         state.stencilWriteMaskedCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0x00) : state.stencilWriteMasked;
905                 }
906
907                 if(context->depthBufferActive())
908                 {
909                         state.depthTestActive = true;
910                         state.depthCompareMode = context->depthCompareMode;
911                         state.quadLayoutDepthBuffer = context->depthStencil->getInternalFormat() != FORMAT_D32F_LOCKABLE &&
912                                                       context->depthStencil->getInternalFormat() != FORMAT_D32FS8_TEXTURE &&
913                                                       context->depthStencil->getInternalFormat() != FORMAT_D32FS8_SHADOW;
914                 }
915
916                 state.occlusionEnabled = context->occlusionEnabled;
917
918                 state.fogActive = context->fogActive();
919                 state.pixelFogMode = context->pixelFogActive();
920                 state.wBasedFog = context->wBasedFog && context->pixelFogActive() != FOG_NONE;
921                 state.perspective = context->perspectiveActive();
922
923                 if(context->alphaBlendActive())
924                 {
925                         state.alphaBlendActive = true;
926                         state.sourceBlendFactor = context->sourceBlendFactor();
927                         state.destBlendFactor = context->destBlendFactor();
928                         state.blendOperation = context->blendOperation();
929                         state.sourceBlendFactorAlpha = context->sourceBlendFactorAlpha();
930                         state.destBlendFactorAlpha = context->destBlendFactorAlpha();
931                         state.blendOperationAlpha = context->blendOperationAlpha();
932                 }
933
934                 state.logicalOperation = context->colorLogicOp();
935
936                 state.colorWriteMask = (context->colorWriteActive(0) << 0) |
937                                        (context->colorWriteActive(1) << 4) |
938                                        (context->colorWriteActive(2) << 8) |
939                                        (context->colorWriteActive(3) << 12);
940
941                 for(int i = 0; i < RENDERTARGETS; i++)
942                 {
943                         state.targetFormat[i] = context->renderTargetInternalFormat(i);
944                 }
945
946                 state.writeSRGB = context->writeSRGB && context->renderTarget[0] && Surface::isSRGBwritable(context->renderTarget[0]->getExternalFormat());
947                 state.multiSample = context->getMultiSampleCount();
948                 state.multiSampleMask = context->multiSampleMask;
949
950                 if(state.multiSample > 1 && context->pixelShader)
951                 {
952                         state.centroid = context->pixelShader->containsCentroid();
953                 }
954
955                 if(!context->pixelShader)
956                 {
957                         for(unsigned int i = 0; i < 8; i++)
958                         {
959                                 state.textureStage[i] = context->textureStage[i].textureStageState();
960                         }
961
962                         state.specularAdd = context->specularActive() && context->specularEnable;
963                 }
964
965                 for(unsigned int i = 0; i < 16; i++)
966                 {
967                         if(context->pixelShader)
968                         {
969                                 if(context->pixelShader->usesSampler(i))
970                                 {
971                                         state.sampler[i] = context->sampler[i].samplerState();
972                                 }
973                         }
974                         else
975                         {
976                                 if(i < 8 && state.textureStage[i].stageOperation != TextureStage::STAGE_DISABLE)
977                                 {
978                                         state.sampler[i] = context->sampler[i].samplerState();
979                                 }
980                                 else break;
981                         }
982                 }
983
984                 const bool point = context->isDrawPoint(true);
985                 const bool sprite = context->pointSpriteActive();
986                 const bool flatShading = (context->shadingMode == SHADING_FLAT) || point;
987
988                 if(context->pixelShaderVersion() < 0x0300)
989                 {
990                         for(int coordinate = 0; coordinate < 8; coordinate++)
991                         {
992                                 for(int component = 0; component < 4; component++)
993                                 {
994                                         if(context->textureActive(coordinate, component))
995                                         {
996                                                 state.texture[coordinate].component |= 1 << component;
997
998                                                 if(point && !sprite)
999                                                 {
1000                                                         state.texture[coordinate].flat |= 1 << component;
1001                                                 }
1002                                         }
1003                                 }
1004
1005                                 if(context->textureTransformProject[coordinate] && context->pixelShaderVersion() <= 0x0103)
1006                                 {
1007                                         if(context->textureTransformCount[coordinate] == 2)
1008                                         {
1009                                                 state.texture[coordinate].project = 1;
1010                                         }
1011                                         else if(context->textureTransformCount[coordinate] == 3)
1012                                         {
1013                                                 state.texture[coordinate].project = 2;
1014                                         }
1015                                         else if(context->textureTransformCount[coordinate] == 4 || context->textureTransformCount[coordinate] == 0)
1016                                         {
1017                                                 state.texture[coordinate].project = 3;
1018                                         }
1019                                 }
1020                         }
1021
1022                         for(int color = 0; color < 2; color++)
1023                         {
1024                                 for(int component = 0; component < 4; component++)
1025                                 {
1026                                         if(context->colorActive(color, component))
1027                                         {
1028                                                 state.color[color].component |= 1 << component;
1029
1030                                                 if(point || flatShading)
1031                                                 {
1032                                                         state.color[color].flat |= 1 << component;
1033                                                 }
1034                                         }
1035                                 }
1036                         }
1037
1038                         if(context->fogActive())
1039                         {
1040                                 state.fog.component = true;
1041
1042                                 if(point)
1043                                 {
1044                                         state.fog.flat = true;
1045                                 }
1046                         }
1047                 }
1048                 else
1049                 {
1050                         for(int interpolant = 0; interpolant < 10; interpolant++)
1051                         {
1052                                 for(int component = 0; component < 4; component++)
1053                                 {
1054                                         if(context->pixelShader->semantic[interpolant][component].active())
1055                                         {
1056                                                 bool flat = point;
1057
1058                                                 switch(context->pixelShader->semantic[interpolant][component].usage)
1059                                                 {
1060                                                 case Shader::USAGE_TEXCOORD:    flat = point && !sprite;        break;
1061                                                 case Shader::USAGE_COLOR:               flat = flatShading;                     break;
1062                                                 }
1063
1064                                                 state.interpolant[interpolant].component |= 1 << component;
1065
1066                                                 if(flat)
1067                                                 {
1068                                                         state.interpolant[interpolant].flat |= 1 << component;
1069                                                 }
1070                                         }
1071                                 }
1072                         }
1073                 }
1074
1075                 if(state.centroid)
1076                 {
1077                         for(int interpolant = 0; interpolant < 10; interpolant++)
1078                         {
1079                                 for(int component = 0; component < 4; component++)
1080                                 {
1081                                         state.interpolant[interpolant].centroid = context->pixelShader->semantic[interpolant][0].centroid;
1082                                 }
1083                         }
1084                 }
1085
1086                 state.hash = state.computeHash();
1087
1088                 return state;
1089         }
1090
1091         Routine *PixelProcessor::routine(const State &state)
1092         {
1093                 Routine *routine = routineCache->query(state);
1094
1095                 if(!routine)
1096                 {
1097                         const bool integerPipeline = (context->pixelShaderVersion() <= 0x0104);
1098                         Rasterizer *generator = nullptr;
1099
1100                         if(integerPipeline)
1101                         {
1102                                 generator = new PixelPipeline(state, context->pixelShader);
1103                         }
1104                         else
1105                         {
1106                                 generator = new PixelProgram(state, context->pixelShader);
1107                         }
1108
1109                         generator->generate();
1110                         routine = (*generator)(L"PixelRoutine_%0.8X", state.shaderID);
1111                         delete generator;
1112
1113                         routineCache->add(state, routine);
1114                 }
1115
1116                 return routine;
1117         }
1118 }