OSDN Git Service

Removed SwiftShader's custom Blending enums
[android-x86/external-swiftshader.git] / src / Device / PixelProcessor.cpp
1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // 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
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // 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.
14
15 #include "PixelProcessor.hpp"
16
17 #include "Surface.hpp"
18 #include "Primitive.hpp"
19 #include "Pipeline/PixelProgram.hpp"
20 #include "Pipeline/PixelShader.hpp"
21 #include "Pipeline/Constants.hpp"
22 #include "System/Debug.hpp"
23
24 #include <string.h>
25
26 namespace sw
27 {
28         extern bool complementaryDepthBuffer;
29         extern TransparencyAntialiasing transparencyAntialiasing;
30         extern bool perspectiveCorrection;
31
32         bool precachePixel = false;
33
34         unsigned int PixelProcessor::States::computeHash()
35         {
36                 unsigned int *state = (unsigned int*)this;
37                 unsigned int hash = 0;
38
39                 for(unsigned int i = 0; i < sizeof(States) / 4; i++)
40                 {
41                         hash ^= state[i];
42                 }
43
44                 return hash;
45         }
46
47         PixelProcessor::State::State()
48         {
49                 memset(this, 0, sizeof(State));
50         }
51
52         bool PixelProcessor::State::operator==(const State &state) const
53         {
54                 if(hash != state.hash)
55                 {
56                         return false;
57                 }
58
59                 return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0;
60         }
61
62         PixelProcessor::UniformBufferInfo::UniformBufferInfo()
63         {
64                 buffer = nullptr;
65                 offset = 0;
66         }
67
68         PixelProcessor::PixelProcessor(Context *context) : context(context)
69         {
70                 routineCache = nullptr;
71                 setRoutineCacheSize(1024);
72         }
73
74         PixelProcessor::~PixelProcessor()
75         {
76                 delete routineCache;
77                 routineCache = nullptr;
78         }
79
80         void PixelProcessor::setFloatConstant(unsigned int index, const float value[4])
81         {
82                 if(index < FRAGMENT_UNIFORM_VECTORS)
83                 {
84                         c[index][0] = value[0];
85                         c[index][1] = value[1];
86                         c[index][2] = value[2];
87                         c[index][3] = value[3];
88                 }
89                 else ASSERT(false);
90         }
91
92         void PixelProcessor::setIntegerConstant(unsigned int index, const int value[4])
93         {
94                 if(index < 16)
95                 {
96                         i[index][0] = value[0];
97                         i[index][1] = value[1];
98                         i[index][2] = value[2];
99                         i[index][3] = value[3];
100                 }
101                 else ASSERT(false);
102         }
103
104         void PixelProcessor::setBooleanConstant(unsigned int index, int boolean)
105         {
106                 if(index < 16)
107                 {
108                         b[index] = boolean != 0;
109                 }
110                 else ASSERT(false);
111         }
112
113         void PixelProcessor::setUniformBuffer(int index, sw::Resource* buffer, int offset)
114         {
115                 uniformBufferInfo[index].buffer = buffer;
116                 uniformBufferInfo[index].offset = offset;
117         }
118
119         void PixelProcessor::lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[])
120         {
121                 for(int i = 0; i < MAX_UNIFORM_BUFFER_BINDINGS; ++i)
122                 {
123                         u[i] = uniformBufferInfo[i].buffer ? static_cast<byte*>(uniformBufferInfo[i].buffer->lock(PUBLIC, PRIVATE)) + uniformBufferInfo[i].offset : nullptr;
124                         uniformBuffers[i] = uniformBufferInfo[i].buffer;
125                 }
126         }
127
128         void PixelProcessor::setRenderTarget(int index, Surface *renderTarget, unsigned int layer)
129         {
130                 context->renderTarget[index] = renderTarget;
131                 context->renderTargetLayer[index] = layer;
132         }
133
134         void PixelProcessor::setDepthBuffer(Surface *depthBuffer, unsigned int layer)
135         {
136                 context->depthBuffer = depthBuffer;
137                 context->depthBufferLayer = layer;
138         }
139
140         void PixelProcessor::setStencilBuffer(Surface *stencilBuffer, unsigned int layer)
141         {
142                 context->stencilBuffer = stencilBuffer;
143                 context->stencilBufferLayer = layer;
144         }
145
146         void PixelProcessor::setTextureFilter(unsigned int sampler, FilterType textureFilter)
147         {
148                 if(sampler < TEXTURE_IMAGE_UNITS)
149                 {
150                         context->sampler[sampler].setTextureFilter(textureFilter);
151                 }
152                 else ASSERT(false);
153         }
154
155         void PixelProcessor::setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter)
156         {
157                 if(sampler < TEXTURE_IMAGE_UNITS)
158                 {
159                         context->sampler[sampler].setMipmapFilter(mipmapFilter);
160                 }
161                 else ASSERT(false);
162         }
163
164         void PixelProcessor::setGatherEnable(unsigned int sampler, bool enable)
165         {
166                 if(sampler < TEXTURE_IMAGE_UNITS)
167                 {
168                         context->sampler[sampler].setGatherEnable(enable);
169                 }
170                 else ASSERT(false);
171         }
172
173         void PixelProcessor::setAddressingModeU(unsigned int sampler, AddressingMode addressMode)
174         {
175                 if(sampler < TEXTURE_IMAGE_UNITS)
176                 {
177                         context->sampler[sampler].setAddressingModeU(addressMode);
178                 }
179                 else ASSERT(false);
180         }
181
182         void PixelProcessor::setAddressingModeV(unsigned int sampler, AddressingMode addressMode)
183         {
184                 if(sampler < TEXTURE_IMAGE_UNITS)
185                 {
186                         context->sampler[sampler].setAddressingModeV(addressMode);
187                 }
188                 else ASSERT(false);
189         }
190
191         void PixelProcessor::setAddressingModeW(unsigned int sampler, AddressingMode addressMode)
192         {
193                 if(sampler < TEXTURE_IMAGE_UNITS)
194                 {
195                         context->sampler[sampler].setAddressingModeW(addressMode);
196                 }
197                 else ASSERT(false);
198         }
199
200         void PixelProcessor::setReadSRGB(unsigned int sampler, bool sRGB)
201         {
202                 if(sampler < TEXTURE_IMAGE_UNITS)
203                 {
204                         context->sampler[sampler].setReadSRGB(sRGB);
205                 }
206                 else ASSERT(false);
207         }
208
209         void PixelProcessor::setMipmapLOD(unsigned int sampler, float bias)
210         {
211                 if(sampler < TEXTURE_IMAGE_UNITS)
212                 {
213                         context->sampler[sampler].setMipmapLOD(bias);
214                 }
215                 else ASSERT(false);
216         }
217
218         void PixelProcessor::setBorderColor(unsigned int sampler, const Color<float> &borderColor)
219         {
220                 if(sampler < TEXTURE_IMAGE_UNITS)
221                 {
222                         context->sampler[sampler].setBorderColor(borderColor);
223                 }
224                 else ASSERT(false);
225         }
226
227         void PixelProcessor::setMaxAnisotropy(unsigned int sampler, float maxAnisotropy)
228         {
229                 if(sampler < TEXTURE_IMAGE_UNITS)
230                 {
231                         context->sampler[sampler].setMaxAnisotropy(maxAnisotropy);
232                 }
233                 else ASSERT(false);
234         }
235
236         void PixelProcessor::setHighPrecisionFiltering(unsigned int sampler, bool highPrecisionFiltering)
237         {
238                 if(sampler < TEXTURE_IMAGE_UNITS)
239                 {
240                         context->sampler[sampler].setHighPrecisionFiltering(highPrecisionFiltering);
241                 }
242                 else ASSERT(false);
243         }
244
245         void PixelProcessor::setSwizzleR(unsigned int sampler, SwizzleType swizzleR)
246         {
247                 if(sampler < TEXTURE_IMAGE_UNITS)
248                 {
249                         context->sampler[sampler].setSwizzleR(swizzleR);
250                 }
251                 else ASSERT(false);
252         }
253
254         void PixelProcessor::setSwizzleG(unsigned int sampler, SwizzleType swizzleG)
255         {
256                 if(sampler < TEXTURE_IMAGE_UNITS)
257                 {
258                         context->sampler[sampler].setSwizzleG(swizzleG);
259                 }
260                 else ASSERT(false);
261         }
262
263         void PixelProcessor::setSwizzleB(unsigned int sampler, SwizzleType swizzleB)
264         {
265                 if(sampler < TEXTURE_IMAGE_UNITS)
266                 {
267                         context->sampler[sampler].setSwizzleB(swizzleB);
268                 }
269                 else ASSERT(false);
270         }
271
272         void PixelProcessor::setSwizzleA(unsigned int sampler, SwizzleType swizzleA)
273         {
274                 if(sampler < TEXTURE_IMAGE_UNITS)
275                 {
276                         context->sampler[sampler].setSwizzleA(swizzleA);
277                 }
278                 else ASSERT(false);
279         }
280
281         void PixelProcessor::setCompareFunc(unsigned int sampler, CompareFunc compFunc)
282         {
283                 if(sampler < TEXTURE_IMAGE_UNITS)
284                 {
285                         context->sampler[sampler].setCompareFunc(compFunc);
286                 }
287                 else ASSERT(false);
288         }
289
290         void PixelProcessor::setBaseLevel(unsigned int sampler, int baseLevel)
291         {
292                 if(sampler < TEXTURE_IMAGE_UNITS)
293                 {
294                         context->sampler[sampler].setBaseLevel(baseLevel);
295                 }
296                 else ASSERT(false);
297         }
298
299         void PixelProcessor::setMaxLevel(unsigned int sampler, int maxLevel)
300         {
301                 if(sampler < TEXTURE_IMAGE_UNITS)
302                 {
303                         context->sampler[sampler].setMaxLevel(maxLevel);
304                 }
305                 else ASSERT(false);
306         }
307
308         void PixelProcessor::setMinLod(unsigned int sampler, float minLod)
309         {
310                 if(sampler < TEXTURE_IMAGE_UNITS)
311                 {
312                         context->sampler[sampler].setMinLod(minLod);
313                 }
314                 else ASSERT(false);
315         }
316
317         void PixelProcessor::setMaxLod(unsigned int sampler, float maxLod)
318         {
319                 if(sampler < TEXTURE_IMAGE_UNITS)
320                 {
321                         context->sampler[sampler].setMaxLod(maxLod);
322                 }
323                 else ASSERT(false);
324         }
325
326         void PixelProcessor::setWriteSRGB(bool sRGB)
327         {
328                 context->setWriteSRGB(sRGB);
329         }
330
331         void PixelProcessor::setColorLogicOpEnabled(bool colorLogicOpEnabled)
332         {
333                 context->setColorLogicOpEnabled(colorLogicOpEnabled);
334         }
335
336         void PixelProcessor::setLogicalOperation(VkLogicOp logicalOperation)
337         {
338                 context->setLogicalOperation(logicalOperation);
339         }
340
341         void PixelProcessor::setDepthBufferEnable(bool depthBufferEnable)
342         {
343                 context->setDepthBufferEnable(depthBufferEnable);
344         }
345
346         void PixelProcessor::setDepthCompare(VkCompareOp depthCompareMode)
347         {
348                 context->depthCompareMode = depthCompareMode;
349         }
350
351         void PixelProcessor::setAlphaCompare(VkCompareOp alphaCompareMode)
352         {
353                 context->alphaCompareMode = alphaCompareMode;
354         }
355
356         void PixelProcessor::setDepthWriteEnable(bool depthWriteEnable)
357         {
358                 context->depthWriteEnable = depthWriteEnable;
359         }
360
361         void PixelProcessor::setAlphaTestEnable(bool alphaTestEnable)
362         {
363                 context->alphaTestEnable = alphaTestEnable;
364         }
365
366         void PixelProcessor::setCullMode(CullMode cullMode, bool frontFacingCCW)
367         {
368                 context->cullMode = cullMode;
369                 context->frontFacingCCW = frontFacingCCW;
370         }
371
372         void PixelProcessor::setColorWriteMask(int index, int rgbaMask)
373         {
374                 context->setColorWriteMask(index, rgbaMask);
375         }
376
377         void PixelProcessor::setStencilEnable(bool stencilEnable)
378         {
379                 context->stencilEnable = stencilEnable;
380         }
381
382         void PixelProcessor::setStencilCompare(VkCompareOp stencilCompareMode)
383         {
384                 context->stencilCompareMode = stencilCompareMode;
385         }
386
387         void PixelProcessor::setStencilReference(int stencilReference)
388         {
389                 context->stencilReference = stencilReference;
390                 stencil.set(stencilReference, context->stencilMask, context->stencilWriteMask);
391         }
392
393         void PixelProcessor::setStencilReferenceCCW(int stencilReferenceCCW)
394         {
395                 context->stencilReferenceCCW = stencilReferenceCCW;
396                 stencilCCW.set(stencilReferenceCCW, context->stencilMaskCCW, context->stencilWriteMaskCCW);
397         }
398
399         void PixelProcessor::setStencilMask(int stencilMask)
400         {
401                 context->stencilMask = stencilMask;
402                 stencil.set(context->stencilReference, stencilMask, context->stencilWriteMask);
403         }
404
405         void PixelProcessor::setStencilMaskCCW(int stencilMaskCCW)
406         {
407                 context->stencilMaskCCW = stencilMaskCCW;
408                 stencilCCW.set(context->stencilReferenceCCW, stencilMaskCCW, context->stencilWriteMaskCCW);
409         }
410
411         void PixelProcessor::setStencilFailOperation(VkStencilOp stencilFailOperation)
412         {
413                 context->stencilFailOperation = stencilFailOperation;
414         }
415
416         void PixelProcessor::setStencilPassOperation(VkStencilOp stencilPassOperation)
417         {
418                 context->stencilPassOperation = stencilPassOperation;
419         }
420
421         void PixelProcessor::setStencilZFailOperation(VkStencilOp stencilZFailOperation)
422         {
423                 context->stencilZFailOperation = stencilZFailOperation;
424         }
425
426         void PixelProcessor::setStencilWriteMask(int stencilWriteMask)
427         {
428                 context->stencilWriteMask = stencilWriteMask;
429                 stencil.set(context->stencilReference, context->stencilMask, stencilWriteMask);
430         }
431
432         void PixelProcessor::setStencilWriteMaskCCW(int stencilWriteMaskCCW)
433         {
434                 context->stencilWriteMaskCCW = stencilWriteMaskCCW;
435                 stencilCCW.set(context->stencilReferenceCCW, context->stencilMaskCCW, stencilWriteMaskCCW);
436         }
437
438         void PixelProcessor::setTwoSidedStencil(bool enable)
439         {
440                 context->twoSidedStencil = enable;
441         }
442
443         void PixelProcessor::setStencilCompareCCW(VkCompareOp stencilCompareMode)
444         {
445                 context->stencilCompareModeCCW = stencilCompareMode;
446         }
447
448         void PixelProcessor::setStencilFailOperationCCW(VkStencilOp stencilFailOperation)
449         {
450                 context->stencilFailOperationCCW = stencilFailOperation;
451         }
452
453         void PixelProcessor::setStencilPassOperationCCW(VkStencilOp stencilPassOperation)
454         {
455                 context->stencilPassOperationCCW = stencilPassOperation;
456         }
457
458         void PixelProcessor::setStencilZFailOperationCCW(VkStencilOp stencilZFailOperation)
459         {
460                 context->stencilZFailOperationCCW = stencilZFailOperation;
461         }
462
463         void PixelProcessor::setBlendConstant(const Color<float> &blendConstant)
464         {
465                 // FIXME: Compact into generic function   // FIXME: Clamp
466                 short blendConstantR = iround(65535 * blendConstant.r);
467                 short blendConstantG = iround(65535 * blendConstant.g);
468                 short blendConstantB = iround(65535 * blendConstant.b);
469                 short blendConstantA = iround(65535 * blendConstant.a);
470
471                 factor.blendConstant4W[0][0] = blendConstantR;
472                 factor.blendConstant4W[0][1] = blendConstantR;
473                 factor.blendConstant4W[0][2] = blendConstantR;
474                 factor.blendConstant4W[0][3] = blendConstantR;
475
476                 factor.blendConstant4W[1][0] = blendConstantG;
477                 factor.blendConstant4W[1][1] = blendConstantG;
478                 factor.blendConstant4W[1][2] = blendConstantG;
479                 factor.blendConstant4W[1][3] = blendConstantG;
480
481                 factor.blendConstant4W[2][0] = blendConstantB;
482                 factor.blendConstant4W[2][1] = blendConstantB;
483                 factor.blendConstant4W[2][2] = blendConstantB;
484                 factor.blendConstant4W[2][3] = blendConstantB;
485
486                 factor.blendConstant4W[3][0] = blendConstantA;
487                 factor.blendConstant4W[3][1] = blendConstantA;
488                 factor.blendConstant4W[3][2] = blendConstantA;
489                 factor.blendConstant4W[3][3] = blendConstantA;
490
491                 // FIXME: Compact into generic function   // FIXME: Clamp
492                 short invBlendConstantR = iround(65535 * (1 - blendConstant.r));
493                 short invBlendConstantG = iround(65535 * (1 - blendConstant.g));
494                 short invBlendConstantB = iround(65535 * (1 - blendConstant.b));
495                 short invBlendConstantA = iround(65535 * (1 - blendConstant.a));
496
497                 factor.invBlendConstant4W[0][0] = invBlendConstantR;
498                 factor.invBlendConstant4W[0][1] = invBlendConstantR;
499                 factor.invBlendConstant4W[0][2] = invBlendConstantR;
500                 factor.invBlendConstant4W[0][3] = invBlendConstantR;
501
502                 factor.invBlendConstant4W[1][0] = invBlendConstantG;
503                 factor.invBlendConstant4W[1][1] = invBlendConstantG;
504                 factor.invBlendConstant4W[1][2] = invBlendConstantG;
505                 factor.invBlendConstant4W[1][3] = invBlendConstantG;
506
507                 factor.invBlendConstant4W[2][0] = invBlendConstantB;
508                 factor.invBlendConstant4W[2][1] = invBlendConstantB;
509                 factor.invBlendConstant4W[2][2] = invBlendConstantB;
510                 factor.invBlendConstant4W[2][3] = invBlendConstantB;
511
512                 factor.invBlendConstant4W[3][0] = invBlendConstantA;
513                 factor.invBlendConstant4W[3][1] = invBlendConstantA;
514                 factor.invBlendConstant4W[3][2] = invBlendConstantA;
515                 factor.invBlendConstant4W[3][3] = invBlendConstantA;
516
517                 factor.blendConstant4F[0][0] = blendConstant.r;
518                 factor.blendConstant4F[0][1] = blendConstant.r;
519                 factor.blendConstant4F[0][2] = blendConstant.r;
520                 factor.blendConstant4F[0][3] = blendConstant.r;
521
522                 factor.blendConstant4F[1][0] = blendConstant.g;
523                 factor.blendConstant4F[1][1] = blendConstant.g;
524                 factor.blendConstant4F[1][2] = blendConstant.g;
525                 factor.blendConstant4F[1][3] = blendConstant.g;
526
527                 factor.blendConstant4F[2][0] = blendConstant.b;
528                 factor.blendConstant4F[2][1] = blendConstant.b;
529                 factor.blendConstant4F[2][2] = blendConstant.b;
530                 factor.blendConstant4F[2][3] = blendConstant.b;
531
532                 factor.blendConstant4F[3][0] = blendConstant.a;
533                 factor.blendConstant4F[3][1] = blendConstant.a;
534                 factor.blendConstant4F[3][2] = blendConstant.a;
535                 factor.blendConstant4F[3][3] = blendConstant.a;
536
537                 factor.invBlendConstant4F[0][0] = 1 - blendConstant.r;
538                 factor.invBlendConstant4F[0][1] = 1 - blendConstant.r;
539                 factor.invBlendConstant4F[0][2] = 1 - blendConstant.r;
540                 factor.invBlendConstant4F[0][3] = 1 - blendConstant.r;
541
542                 factor.invBlendConstant4F[1][0] = 1 - blendConstant.g;
543                 factor.invBlendConstant4F[1][1] = 1 - blendConstant.g;
544                 factor.invBlendConstant4F[1][2] = 1 - blendConstant.g;
545                 factor.invBlendConstant4F[1][3] = 1 - blendConstant.g;
546
547                 factor.invBlendConstant4F[2][0] = 1 - blendConstant.b;
548                 factor.invBlendConstant4F[2][1] = 1 - blendConstant.b;
549                 factor.invBlendConstant4F[2][2] = 1 - blendConstant.b;
550                 factor.invBlendConstant4F[2][3] = 1 - blendConstant.b;
551
552                 factor.invBlendConstant4F[3][0] = 1 - blendConstant.a;
553                 factor.invBlendConstant4F[3][1] = 1 - blendConstant.a;
554                 factor.invBlendConstant4F[3][2] = 1 - blendConstant.a;
555                 factor.invBlendConstant4F[3][3] = 1 - blendConstant.a;
556         }
557
558         void PixelProcessor::setAlphaBlendEnable(bool alphaBlendEnable)
559         {
560                 context->setAlphaBlendEnable(alphaBlendEnable);
561         }
562
563         void PixelProcessor::setSourceBlendFactor(VkBlendFactor sourceBlendFactor)
564         {
565                 context->setSourceBlendFactor(sourceBlendFactor);
566         }
567
568         void PixelProcessor::setDestBlendFactor(VkBlendFactor destBlendFactor)
569         {
570                 context->setDestBlendFactor(destBlendFactor);
571         }
572
573         void PixelProcessor::setBlendOperation(VkBlendOp blendOperation)
574         {
575                 context->setBlendOperation(blendOperation);
576         }
577
578         void PixelProcessor::setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable)
579         {
580                 context->setSeparateAlphaBlendEnable(separateAlphaBlendEnable);
581         }
582
583         void PixelProcessor::setSourceBlendFactorAlpha(VkBlendFactor sourceBlendFactorAlpha)
584         {
585                 context->setSourceBlendFactorAlpha(sourceBlendFactorAlpha);
586         }
587
588         void PixelProcessor::setDestBlendFactorAlpha(VkBlendFactor destBlendFactorAlpha)
589         {
590                 context->setDestBlendFactorAlpha(destBlendFactorAlpha);
591         }
592
593         void PixelProcessor::setBlendOperationAlpha(VkBlendOp blendOperationAlpha)
594         {
595                 context->setBlendOperationAlpha(blendOperationAlpha);
596         }
597
598         void PixelProcessor::setAlphaReference(float alphaReference)
599         {
600                 context->alphaReference = alphaReference;
601
602                 factor.alphaReference4[0] = (word)iround(alphaReference * 0x1000 / 0xFF);
603                 factor.alphaReference4[1] = (word)iround(alphaReference * 0x1000 / 0xFF);
604                 factor.alphaReference4[2] = (word)iround(alphaReference * 0x1000 / 0xFF);
605                 factor.alphaReference4[3] = (word)iround(alphaReference * 0x1000 / 0xFF);
606         }
607
608         void PixelProcessor::setPerspectiveCorrection(bool perspectiveEnable)
609         {
610                 perspectiveCorrection = perspectiveEnable;
611         }
612
613         void PixelProcessor::setOcclusionEnabled(bool enable)
614         {
615                 context->occlusionEnabled = enable;
616         }
617
618         void PixelProcessor::setRoutineCacheSize(int cacheSize)
619         {
620                 delete routineCache;
621                 routineCache = new RoutineCache<State>(clamp(cacheSize, 1, 65536), precachePixel ? "sw-pixel" : 0);
622         }
623
624         const PixelProcessor::State PixelProcessor::update() const
625         {
626                 State state;
627
628                 if(context->pixelShader)
629                 {
630                         state.shaderID = context->pixelShader->getSerialID();
631                 }
632                 else
633                 {
634                         state.shaderID = 0;
635                 }
636
637                 state.depthOverride = context->pixelShader && context->pixelShader->depthOverride();
638                 state.shaderContainsKill = context->pixelShader ? context->pixelShader->containsKill() : false;
639
640                 if(context->alphaTestActive())
641                 {
642                         state.alphaCompareMode = context->alphaCompareMode;
643
644                         state.transparencyAntialiasing = context->getMultiSampleCount() > 1 ? transparencyAntialiasing : TRANSPARENCY_NONE;
645                 }
646
647                 state.depthWriteEnable = context->depthWriteActive();
648
649                 if(context->stencilActive())
650                 {
651                         state.stencilActive = true;
652                         state.stencilCompareMode = context->stencilCompareMode;
653                         state.stencilFailOperation = context->stencilFailOperation;
654                         state.stencilPassOperation = context->stencilPassOperation;
655                         state.stencilZFailOperation = context->stencilZFailOperation;
656                         state.noStencilMask = (context->stencilMask == 0xFF);
657                         state.noStencilWriteMask = (context->stencilWriteMask == 0xFF);
658                         state.stencilWriteMasked = (context->stencilWriteMask == 0x00);
659
660                         state.twoSidedStencil = context->twoSidedStencil;
661                         state.stencilCompareModeCCW = context->twoSidedStencil ? context->stencilCompareModeCCW : state.stencilCompareMode;
662                         state.stencilFailOperationCCW = context->twoSidedStencil ? context->stencilFailOperationCCW : state.stencilFailOperation;
663                         state.stencilPassOperationCCW = context->twoSidedStencil ? context->stencilPassOperationCCW : state.stencilPassOperation;
664                         state.stencilZFailOperationCCW = context->twoSidedStencil ? context->stencilZFailOperationCCW : state.stencilZFailOperation;
665                         state.noStencilMaskCCW = context->twoSidedStencil ? (context->stencilMaskCCW == 0xFF) : state.noStencilMask;
666                         state.noStencilWriteMaskCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0xFF) : state.noStencilWriteMask;
667                         state.stencilWriteMaskedCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0x00) : state.stencilWriteMasked;
668                 }
669
670                 if(context->depthBufferActive())
671                 {
672                         state.depthTestActive = true;
673                         state.depthCompareMode = context->depthCompareMode;
674                         state.quadLayoutDepthBuffer = Surface::hasQuadLayout(context->depthBuffer->getInternalFormat());
675                 }
676
677                 state.occlusionEnabled = context->occlusionEnabled;
678
679                 state.perspective = context->perspectiveActive();
680                 state.depthClamp = (context->depthBias != 0.0f) || (context->slopeDepthBias != 0.0f);
681
682                 if(context->alphaBlendActive())
683                 {
684                         state.alphaBlendActive = true;
685                         state.sourceBlendFactor = context->sourceBlendFactor();
686                         state.destBlendFactor = context->destBlendFactor();
687                         state.blendOperation = context->blendOperation();
688                         state.sourceBlendFactorAlpha = context->sourceBlendFactorAlpha();
689                         state.destBlendFactorAlpha = context->destBlendFactorAlpha();
690                         state.blendOperationAlpha = context->blendOperationAlpha();
691                 }
692
693                 state.logicalOperation = context->colorLogicOp();
694
695                 for(int i = 0; i < RENDERTARGETS; i++)
696                 {
697                         state.colorWriteMask |= context->colorWriteActive(i) << (4 * i);
698                         state.targetFormat[i] = context->renderTargetInternalFormat(i);
699                 }
700
701                 state.writeSRGB = context->writeSRGB && context->renderTarget[0] && Surface::isSRGBwritable(context->renderTarget[0]->getExternalFormat());
702                 state.multiSample = context->getMultiSampleCount();
703                 state.multiSampleMask = context->multiSampleMask;
704
705                 if(state.multiSample > 1 && context->pixelShader)
706                 {
707                         state.centroid = context->pixelShader->containsCentroid();
708                 }
709
710                 state.frontFaceCCW = context->frontFacingCCW;
711
712
713                 for(unsigned int i = 0; i < 16; i++)
714                 {
715                         if(context->pixelShader)
716                         {
717                                 if(context->pixelShader->usesSampler(i))
718                                 {
719                                         state.sampler[i] = context->sampler[i].samplerState();
720                                 }
721                         }
722                 }
723
724                 const bool point = context->isDrawPoint();
725
726                 for(int interpolant = 0; interpolant < MAX_FRAGMENT_INPUTS; interpolant++)
727                 {
728                         for(int component = 0; component < 4; component++)
729                         {
730                                 const Shader::Semantic &semantic = context->pixelShader->getInput(interpolant, component);
731
732                                 if(semantic.active())
733                                 {
734                                         bool flat = point;
735
736                                         switch(semantic.usage)
737                                         {
738                                         case Shader::USAGE_TEXCOORD: flat = false;                  break;
739                                         case Shader::USAGE_COLOR:    flat = semantic.flat || point; break;
740                                         }
741
742                                         state.interpolant[interpolant].component |= 1 << component;
743
744                                         if(flat)
745                                         {
746                                                 state.interpolant[interpolant].flat |= 1 << component;
747                                         }
748                                 }
749                         }
750                 }
751
752                 if(state.centroid)
753                 {
754                         for(int interpolant = 0; interpolant < MAX_FRAGMENT_INPUTS; interpolant++)
755                         {
756                                 for(int component = 0; component < 4; component++)
757                                 {
758                                         state.interpolant[interpolant].centroid = context->pixelShader->getInput(interpolant, 0).centroid;
759                                 }
760                         }
761                 }
762
763                 state.hash = state.computeHash();
764
765                 return state;
766         }
767
768         Routine *PixelProcessor::routine(const State &state)
769         {
770                 Routine *routine = routineCache->query(state);
771
772                 if(!routine)
773                 {
774                         const bool integerPipeline = (context->pixelShaderModel() <= 0x0104);
775                         QuadRasterizer *generator = new PixelProgram(state, context->pixelShader);
776                         generator->generate();
777                         routine = (*generator)(L"PixelRoutine_%0.8X", state.shaderID);
778                         delete generator;
779
780                         routineCache->add(state, routine);
781                 }
782
783                 return routine;
784         }
785 }