blob: 003cfb3f9189fd51e6eb155e243c409003761687 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman89401822014-05-06 15:04:28 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman89401822014-05-06 15:04:28 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
John Bauman89401822014-05-06 15:04:28 -04008//
Nicolas Capens0bac2852016-05-07 06:09:58 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman89401822014-05-06 15:04:28 -040014
15#include "PixelProcessor.hpp"
16
John Bauman89401822014-05-06 15:04:28 -040017#include "Surface.hpp"
18#include "Primitive.hpp"
Nicolas Capens708c24b2017-10-26 13:07:10 -040019#include "Shader/PixelPipeline.hpp"
20#include "Shader/PixelProgram.hpp"
21#include "Shader/PixelShader.hpp"
22#include "Shader/Constants.hpp"
23#include "Common/Debug.hpp"
John Bauman89401822014-05-06 15:04:28 -040024
John Bauman66b8ab22014-05-06 15:57:45 -040025#include <string.h>
26
John Bauman89401822014-05-06 15:04:28 -040027namespace sw
28{
29 extern bool complementaryDepthBuffer;
Nicolas Capensa0f4be82014-10-22 14:35:30 -040030 extern TransparencyAntialiasing transparencyAntialiasing;
John Bauman89401822014-05-06 15:04:28 -040031 extern bool perspectiveCorrection;
32
John Bauman66b8ab22014-05-06 15:57:45 -040033 bool precachePixel = false;
34
John Bauman89401822014-05-06 15:04:28 -040035 unsigned int PixelProcessor::States::computeHash()
36 {
37 unsigned int *state = (unsigned int*)this;
38 unsigned int hash = 0;
39
Nicolas Capens5d961882016-01-01 23:18:14 -050040 for(unsigned int i = 0; i < sizeof(States) / 4; i++)
John Bauman89401822014-05-06 15:04:28 -040041 {
42 hash ^= state[i];
43 }
44
45 return hash;
46 }
47
48 PixelProcessor::State::State()
49 {
50 memset(this, 0, sizeof(State));
51 }
52
53 bool PixelProcessor::State::operator==(const State &state) const
54 {
55 if(hash != state.hash)
56 {
57 return false;
58 }
59
60 return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0;
61 }
62
Alexis Hetuc6a57cb2016-04-07 10:48:31 -040063 PixelProcessor::UniformBufferInfo::UniformBufferInfo()
64 {
65 buffer = nullptr;
66 offset = 0;
67 }
68
John Bauman89401822014-05-06 15:04:28 -040069 PixelProcessor::PixelProcessor(Context *context) : context(context)
70 {
71 setGlobalMipmapBias(0.0f); // Round to highest LOD [0.5, 1.0]: -0.5
72 // Round to nearest LOD [0.7, 1.4]: 0.0
73 // Round to lowest LOD [1.0, 2.0]: 0.5
74
75 routineCache = 0;
76 setRoutineCacheSize(1024);
77 }
78
79 PixelProcessor::~PixelProcessor()
80 {
81 delete routineCache;
82 routineCache = 0;
83 }
84
85 void PixelProcessor::setFloatConstant(unsigned int index, const float value[4])
86 {
Alexis Hetu04c967a2015-07-08 15:56:17 -040087 if(index < FRAGMENT_UNIFORM_VECTORS)
John Bauman89401822014-05-06 15:04:28 -040088 {
89 c[index][0] = value[0];
90 c[index][1] = value[1];
91 c[index][2] = value[2];
92 c[index][3] = value[3];
93 }
94 else ASSERT(false);
95
96 if(index < 8) // ps_1_x constants
97 {
98 // FIXME: Compact into generic function
99 short x = iround(4095 * clamp(value[0], -1.0f, 1.0f));
100 short y = iround(4095 * clamp(value[1], -1.0f, 1.0f));
101 short z = iround(4095 * clamp(value[2], -1.0f, 1.0f));
102 short w = iround(4095 * clamp(value[3], -1.0f, 1.0f));
103
104 cW[index][0][0] = x;
105 cW[index][0][1] = x;
106 cW[index][0][2] = x;
107 cW[index][0][3] = x;
108
109 cW[index][1][0] = y;
110 cW[index][1][1] = y;
111 cW[index][1][2] = y;
112 cW[index][1][3] = y;
113
114 cW[index][2][0] = z;
115 cW[index][2][1] = z;
116 cW[index][2][2] = z;
117 cW[index][2][3] = z;
118
119 cW[index][3][0] = w;
120 cW[index][3][1] = w;
121 cW[index][3][2] = w;
122 cW[index][3][3] = w;
123 }
124 }
125
126 void PixelProcessor::setIntegerConstant(unsigned int index, const int value[4])
127 {
128 if(index < 16)
129 {
130 i[index][0] = value[0];
131 i[index][1] = value[1];
132 i[index][2] = value[2];
133 i[index][3] = value[3];
134 }
135 else ASSERT(false);
136 }
137
138 void PixelProcessor::setBooleanConstant(unsigned int index, int boolean)
139 {
140 if(index < 16)
141 {
142 b[index] = boolean != 0;
143 }
144 else ASSERT(false);
145 }
146
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400147 void PixelProcessor::setUniformBuffer(int index, sw::Resource* buffer, int offset)
148 {
Alexis Hetuc6a57cb2016-04-07 10:48:31 -0400149 uniformBufferInfo[index].buffer = buffer;
150 uniformBufferInfo[index].offset = offset;
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400151 }
152
Alexis Hetuc6a57cb2016-04-07 10:48:31 -0400153 void PixelProcessor::lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[])
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400154 {
155 for(int i = 0; i < MAX_UNIFORM_BUFFER_BINDINGS; ++i)
156 {
Alexis Hetuc6a57cb2016-04-07 10:48:31 -0400157 u[i] = uniformBufferInfo[i].buffer ? static_cast<byte*>(uniformBufferInfo[i].buffer->lock(PUBLIC, PRIVATE)) + uniformBufferInfo[i].offset : nullptr;
158 uniformBuffers[i] = uniformBufferInfo[i].buffer;
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400159 }
160 }
161
John Bauman89401822014-05-06 15:04:28 -0400162 void PixelProcessor::setRenderTarget(int index, Surface *renderTarget)
163 {
164 context->renderTarget[index] = renderTarget;
165 }
166
Nicolas Capens3751c1e2016-03-21 14:14:14 -0400167 void PixelProcessor::setDepthBuffer(Surface *depthBuffer)
John Bauman89401822014-05-06 15:04:28 -0400168 {
Nicolas Capens3751c1e2016-03-21 14:14:14 -0400169 context->depthBuffer = depthBuffer;
170 }
171
172 void PixelProcessor::setStencilBuffer(Surface *stencilBuffer)
173 {
174 context->stencilBuffer = stencilBuffer;
John Bauman89401822014-05-06 15:04:28 -0400175 }
176
177 void PixelProcessor::setTexCoordIndex(unsigned int stage, int texCoordIndex)
178 {
179 if(stage < 8)
180 {
181 context->textureStage[stage].setTexCoordIndex(texCoordIndex);
182 }
183 else ASSERT(false);
184 }
185
186 void PixelProcessor::setStageOperation(unsigned int stage, TextureStage::StageOperation stageOperation)
187 {
188 if(stage < 8)
189 {
190 context->textureStage[stage].setStageOperation(stageOperation);
191 }
192 else ASSERT(false);
193 }
194
195 void PixelProcessor::setFirstArgument(unsigned int stage, TextureStage::SourceArgument firstArgument)
196 {
197 if(stage < 8)
198 {
199 context->textureStage[stage].setFirstArgument(firstArgument);
200 }
201 else ASSERT(false);
202 }
203
204 void PixelProcessor::setSecondArgument(unsigned int stage, TextureStage::SourceArgument secondArgument)
205 {
206 if(stage < 8)
207 {
208 context->textureStage[stage].setSecondArgument(secondArgument);
209 }
210 else ASSERT(false);
211 }
212
213 void PixelProcessor::setThirdArgument(unsigned int stage, TextureStage::SourceArgument thirdArgument)
214 {
215 if(stage < 8)
216 {
217 context->textureStage[stage].setThirdArgument(thirdArgument);
218 }
219 else ASSERT(false);
220 }
221
222 void PixelProcessor::setStageOperationAlpha(unsigned int stage, TextureStage::StageOperation stageOperationAlpha)
223 {
224 if(stage < 8)
225 {
226 context->textureStage[stage].setStageOperationAlpha(stageOperationAlpha);
227 }
228 else ASSERT(false);
229 }
230
231 void PixelProcessor::setFirstArgumentAlpha(unsigned int stage, TextureStage::SourceArgument firstArgumentAlpha)
232 {
233 if(stage < 8)
234 {
235 context->textureStage[stage].setFirstArgumentAlpha(firstArgumentAlpha);
236 }
237 else ASSERT(false);
238 }
239
240 void PixelProcessor::setSecondArgumentAlpha(unsigned int stage, TextureStage::SourceArgument secondArgumentAlpha)
241 {
242 if(stage < 8)
243 {
244 context->textureStage[stage].setSecondArgumentAlpha(secondArgumentAlpha);
245 }
246 else ASSERT(false);
247 }
248
249 void PixelProcessor::setThirdArgumentAlpha(unsigned int stage, TextureStage::SourceArgument thirdArgumentAlpha)
250 {
251 if(stage < 8)
252 {
253 context->textureStage[stage].setThirdArgumentAlpha(thirdArgumentAlpha);
254 }
255 else ASSERT(false);
256 }
257
258 void PixelProcessor::setFirstModifier(unsigned int stage, TextureStage::ArgumentModifier firstModifier)
259 {
260 if(stage < 8)
261 {
262 context->textureStage[stage].setFirstModifier(firstModifier);
263 }
264 else ASSERT(false);
265 }
266
267 void PixelProcessor::setSecondModifier(unsigned int stage, TextureStage::ArgumentModifier secondModifier)
268 {
269 if(stage < 8)
270 {
271 context->textureStage[stage].setSecondModifier(secondModifier);
272 }
273 else ASSERT(false);
274 }
275
276 void PixelProcessor::setThirdModifier(unsigned int stage, TextureStage::ArgumentModifier thirdModifier)
277 {
278 if(stage < 8)
279 {
280 context->textureStage[stage].setThirdModifier(thirdModifier);
281 }
282 else ASSERT(false);
283 }
284
285 void PixelProcessor::setFirstModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier firstModifierAlpha)
286 {
287 if(stage < 8)
288 {
289 context->textureStage[stage].setFirstModifierAlpha(firstModifierAlpha);
290 }
291 else ASSERT(false);
292 }
293
294 void PixelProcessor::setSecondModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier secondModifierAlpha)
295 {
296 if(stage < 8)
297 {
298 context->textureStage[stage].setSecondModifierAlpha(secondModifierAlpha);
299 }
300 else ASSERT(false);
301 }
302
303 void PixelProcessor::setThirdModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier thirdModifierAlpha)
304 {
305 if(stage < 8)
306 {
307 context->textureStage[stage].setThirdModifierAlpha(thirdModifierAlpha);
308 }
309 else ASSERT(false);
310 }
311
312 void PixelProcessor::setDestinationArgument(unsigned int stage, TextureStage::DestinationArgument destinationArgument)
313 {
314 if(stage < 8)
315 {
316 context->textureStage[stage].setDestinationArgument(destinationArgument);
317 }
318 else ASSERT(false);
319 }
320
321 void PixelProcessor::setConstantColor(unsigned int stage, const Color<float> &constantColor)
322 {
323 if(stage < 8)
324 {
325 context->textureStage[stage].setConstantColor(constantColor);
326 }
327 else ASSERT(false);
328 }
329
330 void PixelProcessor::setBumpmapMatrix(unsigned int stage, int element, float value)
331 {
332 if(stage < 8)
333 {
334 context->textureStage[stage].setBumpmapMatrix(element, value);
335 }
336 else ASSERT(false);
337 }
338
339 void PixelProcessor::setLuminanceScale(unsigned int stage, float value)
340 {
341 if(stage < 8)
342 {
343 context->textureStage[stage].setLuminanceScale(value);
344 }
345 else ASSERT(false);
346 }
347
348 void PixelProcessor::setLuminanceOffset(unsigned int stage, float value)
349 {
350 if(stage < 8)
351 {
352 context->textureStage[stage].setLuminanceOffset(value);
353 }
354 else ASSERT(false);
355 }
356
357 void PixelProcessor::setTextureFilter(unsigned int sampler, FilterType textureFilter)
358 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400359 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400360 {
361 context->sampler[sampler].setTextureFilter(textureFilter);
362 }
363 else ASSERT(false);
364 }
365
366 void PixelProcessor::setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter)
367 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400368 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400369 {
370 context->sampler[sampler].setMipmapFilter(mipmapFilter);
371 }
372 else ASSERT(false);
373 }
374
375 void PixelProcessor::setGatherEnable(unsigned int sampler, bool enable)
376 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400377 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400378 {
379 context->sampler[sampler].setGatherEnable(enable);
380 }
381 else ASSERT(false);
382 }
383
384 void PixelProcessor::setAddressingModeU(unsigned int sampler, AddressingMode addressMode)
385 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400386 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400387 {
388 context->sampler[sampler].setAddressingModeU(addressMode);
389 }
390 else ASSERT(false);
391 }
392
393 void PixelProcessor::setAddressingModeV(unsigned int sampler, AddressingMode addressMode)
394 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400395 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400396 {
397 context->sampler[sampler].setAddressingModeV(addressMode);
398 }
399 else ASSERT(false);
400 }
401
402 void PixelProcessor::setAddressingModeW(unsigned int sampler, AddressingMode addressMode)
403 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400404 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400405 {
406 context->sampler[sampler].setAddressingModeW(addressMode);
407 }
408 else ASSERT(false);
409 }
410
411 void PixelProcessor::setReadSRGB(unsigned int sampler, bool sRGB)
412 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400413 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400414 {
415 context->sampler[sampler].setReadSRGB(sRGB);
416 }
417 else ASSERT(false);
418 }
419
420 void PixelProcessor::setMipmapLOD(unsigned int sampler, float bias)
421 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400422 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400423 {
424 context->sampler[sampler].setMipmapLOD(bias);
425 }
426 else ASSERT(false);
427 }
428
429 void PixelProcessor::setBorderColor(unsigned int sampler, const Color<float> &borderColor)
430 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400431 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400432 {
433 context->sampler[sampler].setBorderColor(borderColor);
434 }
435 else ASSERT(false);
436 }
437
Alexis Hetu617a5d52014-11-13 10:56:20 -0500438 void PixelProcessor::setMaxAnisotropy(unsigned int sampler, float maxAnisotropy)
John Bauman89401822014-05-06 15:04:28 -0400439 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -0400440 if(sampler < TEXTURE_IMAGE_UNITS)
John Bauman89401822014-05-06 15:04:28 -0400441 {
442 context->sampler[sampler].setMaxAnisotropy(maxAnisotropy);
443 }
444 else ASSERT(false);
445 }
446
Alexis Hetu010a4642017-07-18 14:33:04 -0400447 void PixelProcessor::setHighPrecisionFiltering(unsigned int sampler, bool highPrecisionFiltering)
448 {
449 if(sampler < TEXTURE_IMAGE_UNITS)
450 {
451 context->sampler[sampler].setHighPrecisionFiltering(highPrecisionFiltering);
452 }
453 else ASSERT(false);
454 }
455
Alexis Hetu1d01aa32015-09-29 11:50:05 -0400456 void PixelProcessor::setSwizzleR(unsigned int sampler, SwizzleType swizzleR)
457 {
458 if(sampler < TEXTURE_IMAGE_UNITS)
459 {
460 context->sampler[sampler].setSwizzleR(swizzleR);
461 }
462 else ASSERT(false);
463 }
464
465 void PixelProcessor::setSwizzleG(unsigned int sampler, SwizzleType swizzleG)
466 {
467 if(sampler < TEXTURE_IMAGE_UNITS)
468 {
469 context->sampler[sampler].setSwizzleG(swizzleG);
470 }
471 else ASSERT(false);
472 }
473
474 void PixelProcessor::setSwizzleB(unsigned int sampler, SwizzleType swizzleB)
475 {
476 if(sampler < TEXTURE_IMAGE_UNITS)
477 {
478 context->sampler[sampler].setSwizzleB(swizzleB);
479 }
480 else ASSERT(false);
481 }
482
483 void PixelProcessor::setSwizzleA(unsigned int sampler, SwizzleType swizzleA)
484 {
485 if(sampler < TEXTURE_IMAGE_UNITS)
486 {
487 context->sampler[sampler].setSwizzleA(swizzleA);
488 }
489 else ASSERT(false);
490 }
491
Nicolas Capensf878d502017-11-06 15:29:46 -0500492 void PixelProcessor::setCompareFunc(unsigned int sampler, CompareFunc compFunc)
493 {
494 if(sampler < TEXTURE_IMAGE_UNITS)
495 {
496 context->sampler[sampler].setCompareFunc(compFunc);
497 }
498 else ASSERT(false);
499 }
500
Alexis Hetu95ac1872016-06-06 13:26:52 -0400501 void PixelProcessor::setBaseLevel(unsigned int sampler, int baseLevel)
502 {
503 if(sampler < TEXTURE_IMAGE_UNITS)
504 {
505 context->sampler[sampler].setBaseLevel(baseLevel);
506 }
507 else ASSERT(false);
508 }
509
510 void PixelProcessor::setMaxLevel(unsigned int sampler, int maxLevel)
511 {
512 if(sampler < TEXTURE_IMAGE_UNITS)
513 {
514 context->sampler[sampler].setMaxLevel(maxLevel);
515 }
516 else ASSERT(false);
517 }
518
Alexis Hetu112d81f2016-06-07 12:36:35 -0400519 void PixelProcessor::setMinLod(unsigned int sampler, float minLod)
520 {
521 if(sampler < TEXTURE_IMAGE_UNITS)
522 {
523 context->sampler[sampler].setMinLod(minLod);
524 }
525 else ASSERT(false);
526 }
527
528 void PixelProcessor::setMaxLod(unsigned int sampler, float maxLod)
529 {
530 if(sampler < TEXTURE_IMAGE_UNITS)
531 {
532 context->sampler[sampler].setMaxLod(maxLod);
533 }
534 else ASSERT(false);
535 }
536
John Bauman89401822014-05-06 15:04:28 -0400537 void PixelProcessor::setWriteSRGB(bool sRGB)
538 {
539 context->setWriteSRGB(sRGB);
540 }
541
Maxime Grégoired9762742015-07-08 16:43:48 -0400542 void PixelProcessor::setColorLogicOpEnabled(bool colorLogicOpEnabled)
543 {
544 context->setColorLogicOpEnabled(colorLogicOpEnabled);
545 }
546
547 void PixelProcessor::setLogicalOperation(LogicalOperation logicalOperation)
548 {
549 context->setLogicalOperation(logicalOperation);
550 }
551
John Bauman89401822014-05-06 15:04:28 -0400552 void PixelProcessor::setDepthBufferEnable(bool depthBufferEnable)
553 {
554 context->setDepthBufferEnable(depthBufferEnable);
555 }
556
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400557 void PixelProcessor::setDepthCompare(DepthCompareMode depthCompareMode)
John Bauman89401822014-05-06 15:04:28 -0400558 {
559 context->depthCompareMode = depthCompareMode;
560 }
561
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400562 void PixelProcessor::setAlphaCompare(AlphaCompareMode alphaCompareMode)
John Bauman89401822014-05-06 15:04:28 -0400563 {
564 context->alphaCompareMode = alphaCompareMode;
565 }
566
567 void PixelProcessor::setDepthWriteEnable(bool depthWriteEnable)
568 {
569 context->depthWriteEnable = depthWriteEnable;
570 }
571
572 void PixelProcessor::setAlphaTestEnable(bool alphaTestEnable)
573 {
574 context->alphaTestEnable = alphaTestEnable;
575 }
576
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400577 void PixelProcessor::setCullMode(CullMode cullMode)
John Bauman89401822014-05-06 15:04:28 -0400578 {
579 context->cullMode = cullMode;
580 }
581
582 void PixelProcessor::setColorWriteMask(int index, int rgbaMask)
583 {
584 context->setColorWriteMask(index, rgbaMask);
585 }
586
587 void PixelProcessor::setStencilEnable(bool stencilEnable)
588 {
589 context->stencilEnable = stencilEnable;
590 }
591
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400592 void PixelProcessor::setStencilCompare(StencilCompareMode stencilCompareMode)
John Bauman89401822014-05-06 15:04:28 -0400593 {
594 context->stencilCompareMode = stencilCompareMode;
595 }
596
597 void PixelProcessor::setStencilReference(int stencilReference)
598 {
599 context->stencilReference = stencilReference;
600 stencil.set(stencilReference, context->stencilMask, context->stencilWriteMask);
601 }
602
603 void PixelProcessor::setStencilReferenceCCW(int stencilReferenceCCW)
604 {
605 context->stencilReferenceCCW = stencilReferenceCCW;
606 stencilCCW.set(stencilReferenceCCW, context->stencilMaskCCW, context->stencilWriteMaskCCW);
607 }
608
609 void PixelProcessor::setStencilMask(int stencilMask)
610 {
611 context->stencilMask = stencilMask;
612 stencil.set(context->stencilReference, stencilMask, context->stencilWriteMask);
613 }
614
615 void PixelProcessor::setStencilMaskCCW(int stencilMaskCCW)
616 {
617 context->stencilMaskCCW = stencilMaskCCW;
618 stencilCCW.set(context->stencilReferenceCCW, stencilMaskCCW, context->stencilWriteMaskCCW);
619 }
620
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400621 void PixelProcessor::setStencilFailOperation(StencilOperation stencilFailOperation)
John Bauman89401822014-05-06 15:04:28 -0400622 {
623 context->stencilFailOperation = stencilFailOperation;
624 }
625
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400626 void PixelProcessor::setStencilPassOperation(StencilOperation stencilPassOperation)
John Bauman89401822014-05-06 15:04:28 -0400627 {
628 context->stencilPassOperation = stencilPassOperation;
629 }
630
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400631 void PixelProcessor::setStencilZFailOperation(StencilOperation stencilZFailOperation)
John Bauman89401822014-05-06 15:04:28 -0400632 {
633 context->stencilZFailOperation = stencilZFailOperation;
634 }
635
636 void PixelProcessor::setStencilWriteMask(int stencilWriteMask)
637 {
638 context->stencilWriteMask = stencilWriteMask;
639 stencil.set(context->stencilReference, context->stencilMask, stencilWriteMask);
640 }
641
642 void PixelProcessor::setStencilWriteMaskCCW(int stencilWriteMaskCCW)
643 {
644 context->stencilWriteMaskCCW = stencilWriteMaskCCW;
645 stencilCCW.set(context->stencilReferenceCCW, context->stencilMaskCCW, stencilWriteMaskCCW);
646 }
647
648 void PixelProcessor::setTwoSidedStencil(bool enable)
649 {
650 context->twoSidedStencil = enable;
651 }
652
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400653 void PixelProcessor::setStencilCompareCCW(StencilCompareMode stencilCompareMode)
John Bauman89401822014-05-06 15:04:28 -0400654 {
655 context->stencilCompareModeCCW = stencilCompareMode;
656 }
657
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400658 void PixelProcessor::setStencilFailOperationCCW(StencilOperation stencilFailOperation)
John Bauman89401822014-05-06 15:04:28 -0400659 {
660 context->stencilFailOperationCCW = stencilFailOperation;
661 }
662
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400663 void PixelProcessor::setStencilPassOperationCCW(StencilOperation stencilPassOperation)
John Bauman89401822014-05-06 15:04:28 -0400664 {
665 context->stencilPassOperationCCW = stencilPassOperation;
666 }
667
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400668 void PixelProcessor::setStencilZFailOperationCCW(StencilOperation stencilZFailOperation)
John Bauman89401822014-05-06 15:04:28 -0400669 {
670 context->stencilZFailOperationCCW = stencilZFailOperation;
671 }
672
673 void PixelProcessor::setTextureFactor(const Color<float> &textureFactor)
674 {
675 // FIXME: Compact into generic function // FIXME: Clamp
676 short textureFactorR = iround(4095 * textureFactor.r);
677 short textureFactorG = iround(4095 * textureFactor.g);
678 short textureFactorB = iround(4095 * textureFactor.b);
679 short textureFactorA = iround(4095 * textureFactor.a);
680
681 factor.textureFactor4[0][0] = textureFactorR;
682 factor.textureFactor4[0][1] = textureFactorR;
683 factor.textureFactor4[0][2] = textureFactorR;
684 factor.textureFactor4[0][3] = textureFactorR;
685
686 factor.textureFactor4[1][0] = textureFactorG;
687 factor.textureFactor4[1][1] = textureFactorG;
688 factor.textureFactor4[1][2] = textureFactorG;
689 factor.textureFactor4[1][3] = textureFactorG;
690
691 factor.textureFactor4[2][0] = textureFactorB;
692 factor.textureFactor4[2][1] = textureFactorB;
693 factor.textureFactor4[2][2] = textureFactorB;
694 factor.textureFactor4[2][3] = textureFactorB;
695
696 factor.textureFactor4[3][0] = textureFactorA;
697 factor.textureFactor4[3][1] = textureFactorA;
698 factor.textureFactor4[3][2] = textureFactorA;
699 factor.textureFactor4[3][3] = textureFactorA;
700 }
701
702 void PixelProcessor::setBlendConstant(const Color<float> &blendConstant)
703 {
704 // FIXME: Compact into generic function // FIXME: Clamp
705 short blendConstantR = iround(65535 * blendConstant.r);
706 short blendConstantG = iround(65535 * blendConstant.g);
707 short blendConstantB = iround(65535 * blendConstant.b);
708 short blendConstantA = iround(65535 * blendConstant.a);
709
710 factor.blendConstant4W[0][0] = blendConstantR;
711 factor.blendConstant4W[0][1] = blendConstantR;
712 factor.blendConstant4W[0][2] = blendConstantR;
713 factor.blendConstant4W[0][3] = blendConstantR;
714
715 factor.blendConstant4W[1][0] = blendConstantG;
716 factor.blendConstant4W[1][1] = blendConstantG;
717 factor.blendConstant4W[1][2] = blendConstantG;
718 factor.blendConstant4W[1][3] = blendConstantG;
719
720 factor.blendConstant4W[2][0] = blendConstantB;
721 factor.blendConstant4W[2][1] = blendConstantB;
722 factor.blendConstant4W[2][2] = blendConstantB;
723 factor.blendConstant4W[2][3] = blendConstantB;
724
725 factor.blendConstant4W[3][0] = blendConstantA;
726 factor.blendConstant4W[3][1] = blendConstantA;
727 factor.blendConstant4W[3][2] = blendConstantA;
728 factor.blendConstant4W[3][3] = blendConstantA;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500729
John Bauman89401822014-05-06 15:04:28 -0400730 // FIXME: Compact into generic function // FIXME: Clamp
731 short invBlendConstantR = iround(65535 * (1 - blendConstant.r));
732 short invBlendConstantG = iround(65535 * (1 - blendConstant.g));
733 short invBlendConstantB = iround(65535 * (1 - blendConstant.b));
734 short invBlendConstantA = iround(65535 * (1 - blendConstant.a));
735
736 factor.invBlendConstant4W[0][0] = invBlendConstantR;
737 factor.invBlendConstant4W[0][1] = invBlendConstantR;
738 factor.invBlendConstant4W[0][2] = invBlendConstantR;
739 factor.invBlendConstant4W[0][3] = invBlendConstantR;
740
741 factor.invBlendConstant4W[1][0] = invBlendConstantG;
742 factor.invBlendConstant4W[1][1] = invBlendConstantG;
743 factor.invBlendConstant4W[1][2] = invBlendConstantG;
744 factor.invBlendConstant4W[1][3] = invBlendConstantG;
745
746 factor.invBlendConstant4W[2][0] = invBlendConstantB;
747 factor.invBlendConstant4W[2][1] = invBlendConstantB;
748 factor.invBlendConstant4W[2][2] = invBlendConstantB;
749 factor.invBlendConstant4W[2][3] = invBlendConstantB;
750
751 factor.invBlendConstant4W[3][0] = invBlendConstantA;
752 factor.invBlendConstant4W[3][1] = invBlendConstantA;
753 factor.invBlendConstant4W[3][2] = invBlendConstantA;
754 factor.invBlendConstant4W[3][3] = invBlendConstantA;
755
756 factor.blendConstant4F[0][0] = blendConstant.r;
757 factor.blendConstant4F[0][1] = blendConstant.r;
758 factor.blendConstant4F[0][2] = blendConstant.r;
759 factor.blendConstant4F[0][3] = blendConstant.r;
760
761 factor.blendConstant4F[1][0] = blendConstant.g;
762 factor.blendConstant4F[1][1] = blendConstant.g;
763 factor.blendConstant4F[1][2] = blendConstant.g;
764 factor.blendConstant4F[1][3] = blendConstant.g;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500765
John Bauman89401822014-05-06 15:04:28 -0400766 factor.blendConstant4F[2][0] = blendConstant.b;
767 factor.blendConstant4F[2][1] = blendConstant.b;
768 factor.blendConstant4F[2][2] = blendConstant.b;
769 factor.blendConstant4F[2][3] = blendConstant.b;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500770
John Bauman89401822014-05-06 15:04:28 -0400771 factor.blendConstant4F[3][0] = blendConstant.a;
772 factor.blendConstant4F[3][1] = blendConstant.a;
773 factor.blendConstant4F[3][2] = blendConstant.a;
774 factor.blendConstant4F[3][3] = blendConstant.a;
775
776 factor.invBlendConstant4F[0][0] = 1 - blendConstant.r;
777 factor.invBlendConstant4F[0][1] = 1 - blendConstant.r;
778 factor.invBlendConstant4F[0][2] = 1 - blendConstant.r;
779 factor.invBlendConstant4F[0][3] = 1 - blendConstant.r;
780
781 factor.invBlendConstant4F[1][0] = 1 - blendConstant.g;
782 factor.invBlendConstant4F[1][1] = 1 - blendConstant.g;
783 factor.invBlendConstant4F[1][2] = 1 - blendConstant.g;
784 factor.invBlendConstant4F[1][3] = 1 - blendConstant.g;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500785
John Bauman89401822014-05-06 15:04:28 -0400786 factor.invBlendConstant4F[2][0] = 1 - blendConstant.b;
787 factor.invBlendConstant4F[2][1] = 1 - blendConstant.b;
788 factor.invBlendConstant4F[2][2] = 1 - blendConstant.b;
789 factor.invBlendConstant4F[2][3] = 1 - blendConstant.b;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500790
John Bauman89401822014-05-06 15:04:28 -0400791 factor.invBlendConstant4F[3][0] = 1 - blendConstant.a;
792 factor.invBlendConstant4F[3][1] = 1 - blendConstant.a;
793 factor.invBlendConstant4F[3][2] = 1 - blendConstant.a;
794 factor.invBlendConstant4F[3][3] = 1 - blendConstant.a;
795 }
796
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400797 void PixelProcessor::setFillMode(FillMode fillMode)
John Bauman89401822014-05-06 15:04:28 -0400798 {
799 context->fillMode = fillMode;
800 }
801
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400802 void PixelProcessor::setShadingMode(ShadingMode shadingMode)
John Bauman89401822014-05-06 15:04:28 -0400803 {
804 context->shadingMode = shadingMode;
805 }
806
807 void PixelProcessor::setAlphaBlendEnable(bool alphaBlendEnable)
808 {
809 context->setAlphaBlendEnable(alphaBlendEnable);
810 }
811
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400812 void PixelProcessor::setSourceBlendFactor(BlendFactor sourceBlendFactor)
John Bauman89401822014-05-06 15:04:28 -0400813 {
814 context->setSourceBlendFactor(sourceBlendFactor);
815 }
816
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400817 void PixelProcessor::setDestBlendFactor(BlendFactor destBlendFactor)
John Bauman89401822014-05-06 15:04:28 -0400818 {
819 context->setDestBlendFactor(destBlendFactor);
820 }
821
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400822 void PixelProcessor::setBlendOperation(BlendOperation blendOperation)
John Bauman89401822014-05-06 15:04:28 -0400823 {
824 context->setBlendOperation(blendOperation);
825 }
826
827 void PixelProcessor::setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable)
828 {
829 context->setSeparateAlphaBlendEnable(separateAlphaBlendEnable);
830 }
831
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400832 void PixelProcessor::setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha)
John Bauman89401822014-05-06 15:04:28 -0400833 {
834 context->setSourceBlendFactorAlpha(sourceBlendFactorAlpha);
835 }
836
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400837 void PixelProcessor::setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha)
John Bauman89401822014-05-06 15:04:28 -0400838 {
839 context->setDestBlendFactorAlpha(destBlendFactorAlpha);
840 }
841
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400842 void PixelProcessor::setBlendOperationAlpha(BlendOperation blendOperationAlpha)
John Bauman89401822014-05-06 15:04:28 -0400843 {
844 context->setBlendOperationAlpha(blendOperationAlpha);
845 }
846
Alexis Hetua818c452015-06-11 13:06:58 -0400847 void PixelProcessor::setAlphaReference(float alphaReference)
John Bauman89401822014-05-06 15:04:28 -0400848 {
849 context->alphaReference = alphaReference;
850
Alexis Hetua818c452015-06-11 13:06:58 -0400851 factor.alphaReference4[0] = (word)iround(alphaReference * 0x1000 / 0xFF);
852 factor.alphaReference4[1] = (word)iround(alphaReference * 0x1000 / 0xFF);
853 factor.alphaReference4[2] = (word)iround(alphaReference * 0x1000 / 0xFF);
854 factor.alphaReference4[3] = (word)iround(alphaReference * 0x1000 / 0xFF);
John Bauman89401822014-05-06 15:04:28 -0400855 }
856
857 void PixelProcessor::setGlobalMipmapBias(float bias)
858 {
859 context->setGlobalMipmapBias(bias);
860 }
861
862 void PixelProcessor::setFogStart(float start)
863 {
864 setFogRanges(start, context->fogEnd);
865 }
866
867 void PixelProcessor::setFogEnd(float end)
868 {
869 setFogRanges(context->fogStart, end);
870 }
871
872 void PixelProcessor::setFogColor(Color<float> fogColor)
873 {
874 // TODO: Compact into generic function
875 word fogR = (unsigned short)(65535 * fogColor.r);
876 word fogG = (unsigned short)(65535 * fogColor.g);
877 word fogB = (unsigned short)(65535 * fogColor.b);
878
879 fog.color4[0][0] = fogR;
880 fog.color4[0][1] = fogR;
881 fog.color4[0][2] = fogR;
882 fog.color4[0][3] = fogR;
883
884 fog.color4[1][0] = fogG;
885 fog.color4[1][1] = fogG;
886 fog.color4[1][2] = fogG;
887 fog.color4[1][3] = fogG;
888
889 fog.color4[2][0] = fogB;
890 fog.color4[2][1] = fogB;
891 fog.color4[2][2] = fogB;
892 fog.color4[2][3] = fogB;
893
894 fog.colorF[0] = replicate(fogColor.r);
895 fog.colorF[1] = replicate(fogColor.g);
896 fog.colorF[2] = replicate(fogColor.b);
897 }
898
899 void PixelProcessor::setFogDensity(float fogDensity)
900 {
901 fog.densityE = replicate(-fogDensity * 1.442695f); // 1/e^x = 2^(-x*1.44)
Nicolas Capensa36f3f92015-08-04 15:34:26 -0400902 fog.density2E = replicate(-fogDensity * fogDensity * 1.442695f);
John Bauman89401822014-05-06 15:04:28 -0400903 }
904
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400905 void PixelProcessor::setPixelFogMode(FogMode fogMode)
John Bauman89401822014-05-06 15:04:28 -0400906 {
907 context->pixelFogMode = fogMode;
908 }
909
910 void PixelProcessor::setPerspectiveCorrection(bool perspectiveEnable)
911 {
912 perspectiveCorrection = perspectiveEnable;
913 }
914
915 void PixelProcessor::setOcclusionEnabled(bool enable)
916 {
917 context->occlusionEnabled = enable;
918 }
919
920 void PixelProcessor::setRoutineCacheSize(int cacheSize)
921 {
922 delete routineCache;
John Bauman66b8ab22014-05-06 15:57:45 -0400923 routineCache = new RoutineCache<State>(clamp(cacheSize, 1, 65536), precachePixel ? "sw-pixel" : 0);
John Bauman89401822014-05-06 15:04:28 -0400924 }
925
926 void PixelProcessor::setFogRanges(float start, float end)
927 {
928 context->fogStart = start;
929 context->fogEnd = end;
930
931 if(start == end)
932 {
933 end += 0.001f; // Hack: ensure there is a small range
934 }
935
936 float fogScale = -1.0f / (end - start);
937 float fogOffset = end * -fogScale;
938
939 fog.scale = replicate(fogScale);
940 fog.offset = replicate(fogOffset);
941 }
942
943 const PixelProcessor::State PixelProcessor::update() const
944 {
945 State state;
946
947 if(context->pixelShader)
948 {
John Bauman19bac1e2014-05-06 15:23:49 -0400949 state.shaderID = context->pixelShader->getSerialID();
John Bauman89401822014-05-06 15:04:28 -0400950 }
951 else
952 {
John Bauman19bac1e2014-05-06 15:23:49 -0400953 state.shaderID = 0;
John Bauman89401822014-05-06 15:04:28 -0400954 }
955
956 state.depthOverride = context->pixelShader && context->pixelShader->depthOverride();
John Bauman19bac1e2014-05-06 15:23:49 -0400957 state.shaderContainsKill = context->pixelShader ? context->pixelShader->containsKill() : false;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500958
John Bauman89401822014-05-06 15:04:28 -0400959 if(context->alphaTestActive())
960 {
961 state.alphaCompareMode = context->alphaCompareMode;
962
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400963 state.transparencyAntialiasing = context->getMultiSampleCount() > 1 ? transparencyAntialiasing : TRANSPARENCY_NONE;
John Bauman89401822014-05-06 15:04:28 -0400964 }
965
966 state.depthWriteEnable = context->depthWriteActive();
967
968 if(context->stencilActive())
969 {
970 state.stencilActive = true;
971 state.stencilCompareMode = context->stencilCompareMode;
972 state.stencilFailOperation = context->stencilFailOperation;
973 state.stencilPassOperation = context->stencilPassOperation;
974 state.stencilZFailOperation = context->stencilZFailOperation;
975 state.noStencilMask = (context->stencilMask == 0xFF);
976 state.noStencilWriteMask = (context->stencilWriteMask == 0xFF);
977 state.stencilWriteMasked = (context->stencilWriteMask == 0x00);
978
979 state.twoSidedStencil = context->twoSidedStencil;
980 state.stencilCompareModeCCW = context->twoSidedStencil ? context->stencilCompareModeCCW : state.stencilCompareMode;
981 state.stencilFailOperationCCW = context->twoSidedStencil ? context->stencilFailOperationCCW : state.stencilFailOperation;
982 state.stencilPassOperationCCW = context->twoSidedStencil ? context->stencilPassOperationCCW : state.stencilPassOperation;
983 state.stencilZFailOperationCCW = context->twoSidedStencil ? context->stencilZFailOperationCCW : state.stencilZFailOperation;
984 state.noStencilMaskCCW = context->twoSidedStencil ? (context->stencilMaskCCW == 0xFF) : state.noStencilMask;
985 state.noStencilWriteMaskCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0xFF) : state.noStencilWriteMask;
986 state.stencilWriteMaskedCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0x00) : state.stencilWriteMasked;
987 }
988
989 if(context->depthBufferActive())
990 {
991 state.depthTestActive = true;
992 state.depthCompareMode = context->depthCompareMode;
Alexis Hetub9dda642016-10-06 11:25:32 -0400993 state.quadLayoutDepthBuffer = Surface::hasQuadLayout(context->depthBuffer->getInternalFormat());
John Bauman89401822014-05-06 15:04:28 -0400994 }
995
996 state.occlusionEnabled = context->occlusionEnabled;
997
998 state.fogActive = context->fogActive();
999 state.pixelFogMode = context->pixelFogActive();
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001000 state.wBasedFog = context->wBasedFog && context->pixelFogActive() != FOG_NONE;
John Bauman89401822014-05-06 15:04:28 -04001001 state.perspective = context->perspectiveActive();
Nicolas Capens3cbeac52017-09-15 11:49:31 -04001002 state.depthClamp = (context->depthBias != 0.0f) || (context->slopeDepthBias != 0.0f);
John Bauman89401822014-05-06 15:04:28 -04001003
1004 if(context->alphaBlendActive())
1005 {
1006 state.alphaBlendActive = true;
1007 state.sourceBlendFactor = context->sourceBlendFactor();
1008 state.destBlendFactor = context->destBlendFactor();
1009 state.blendOperation = context->blendOperation();
1010 state.sourceBlendFactorAlpha = context->sourceBlendFactorAlpha();
1011 state.destBlendFactorAlpha = context->destBlendFactorAlpha();
1012 state.blendOperationAlpha = context->blendOperationAlpha();
1013 }
Maxime Grégoired9762742015-07-08 16:43:48 -04001014
1015 state.logicalOperation = context->colorLogicOp();
1016
Alexis Hetu1edcd8b2015-11-05 11:12:41 -05001017 for(int i = 0; i < RENDERTARGETS; i++)
John Bauman89401822014-05-06 15:04:28 -04001018 {
Nicolas Capensc98186a2016-04-18 12:07:22 -04001019 state.colorWriteMask |= context->colorWriteActive(i) << (4 * i);
John Bauman89401822014-05-06 15:04:28 -04001020 state.targetFormat[i] = context->renderTargetInternalFormat(i);
1021 }
1022
John Bauman66b8ab22014-05-06 15:57:45 -04001023 state.writeSRGB = context->writeSRGB && context->renderTarget[0] && Surface::isSRGBwritable(context->renderTarget[0]->getExternalFormat());
1024 state.multiSample = context->getMultiSampleCount();
John Bauman89401822014-05-06 15:04:28 -04001025 state.multiSampleMask = context->multiSampleMask;
1026
1027 if(state.multiSample > 1 && context->pixelShader)
1028 {
1029 state.centroid = context->pixelShader->containsCentroid();
1030 }
1031
1032 if(!context->pixelShader)
1033 {
1034 for(unsigned int i = 0; i < 8; i++)
1035 {
1036 state.textureStage[i] = context->textureStage[i].textureStageState();
1037 }
1038
1039 state.specularAdd = context->specularActive() && context->specularEnable;
1040 }
1041
1042 for(unsigned int i = 0; i < 16; i++)
1043 {
1044 if(context->pixelShader)
1045 {
1046 if(context->pixelShader->usesSampler(i))
1047 {
1048 state.sampler[i] = context->sampler[i].samplerState();
1049 }
1050 }
1051 else
1052 {
1053 if(i < 8 && state.textureStage[i].stageOperation != TextureStage::STAGE_DISABLE)
1054 {
1055 state.sampler[i] = context->sampler[i].samplerState();
1056 }
1057 else break;
1058 }
1059 }
1060
1061 const bool point = context->isDrawPoint(true);
1062 const bool sprite = context->pointSpriteActive();
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001063 const bool flatShading = (context->shadingMode == SHADING_FLAT) || point;
John Bauman89401822014-05-06 15:04:28 -04001064
1065 if(context->pixelShaderVersion() < 0x0300)
1066 {
1067 for(int coordinate = 0; coordinate < 8; coordinate++)
1068 {
1069 for(int component = 0; component < 4; component++)
1070 {
1071 if(context->textureActive(coordinate, component))
1072 {
1073 state.texture[coordinate].component |= 1 << component;
1074
1075 if(point && !sprite)
1076 {
1077 state.texture[coordinate].flat |= 1 << component;
1078 }
1079 }
1080 }
1081
1082 if(context->textureTransformProject[coordinate] && context->pixelShaderVersion() <= 0x0103)
1083 {
1084 if(context->textureTransformCount[coordinate] == 2)
1085 {
1086 state.texture[coordinate].project = 1;
1087 }
1088 else if(context->textureTransformCount[coordinate] == 3)
1089 {
1090 state.texture[coordinate].project = 2;
1091 }
1092 else if(context->textureTransformCount[coordinate] == 4 || context->textureTransformCount[coordinate] == 0)
1093 {
1094 state.texture[coordinate].project = 3;
1095 }
1096 }
1097 }
1098
1099 for(int color = 0; color < 2; color++)
1100 {
1101 for(int component = 0; component < 4; component++)
1102 {
1103 if(context->colorActive(color, component))
1104 {
1105 state.color[color].component |= 1 << component;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001106
John Bauman89401822014-05-06 15:04:28 -04001107 if(point || flatShading)
1108 {
1109 state.color[color].flat |= 1 << component;
1110 }
1111 }
1112 }
1113 }
1114
1115 if(context->fogActive())
1116 {
1117 state.fog.component = true;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001118
John Bauman89401822014-05-06 15:04:28 -04001119 if(point)
1120 {
1121 state.fog.flat = true;
1122 }
1123 }
1124 }
1125 else
1126 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04001127 for(int interpolant = 0; interpolant < MAX_FRAGMENT_INPUTS; interpolant++)
John Bauman89401822014-05-06 15:04:28 -04001128 {
1129 for(int component = 0; component < 4; component++)
1130 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04001131 const Shader::Semantic &semantic = context->pixelShader->getInput(interpolant, component);
Alexis Hetu12b00502016-05-20 13:01:11 -04001132
1133 if(semantic.active())
John Bauman89401822014-05-06 15:04:28 -04001134 {
1135 bool flat = point;
1136
Alexis Hetu12b00502016-05-20 13:01:11 -04001137 switch(semantic.usage)
John Bauman89401822014-05-06 15:04:28 -04001138 {
Alexis Hetu12b00502016-05-20 13:01:11 -04001139 case Shader::USAGE_TEXCOORD: flat = point && !sprite; break;
1140 case Shader::USAGE_COLOR: flat = semantic.flat || flatShading; break;
John Bauman89401822014-05-06 15:04:28 -04001141 }
1142
1143 state.interpolant[interpolant].component |= 1 << component;
1144
1145 if(flat)
1146 {
1147 state.interpolant[interpolant].flat |= 1 << component;
1148 }
1149 }
1150 }
1151 }
1152 }
1153
1154 if(state.centroid)
1155 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04001156 for(int interpolant = 0; interpolant < MAX_FRAGMENT_INPUTS; interpolant++)
John Bauman89401822014-05-06 15:04:28 -04001157 {
1158 for(int component = 0; component < 4; component++)
1159 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04001160 state.interpolant[interpolant].centroid = context->pixelShader->getInput(interpolant, 0).centroid;
John Bauman89401822014-05-06 15:04:28 -04001161 }
1162 }
1163 }
1164
1165 state.hash = state.computeHash();
1166
1167 return state;
1168 }
1169
1170 Routine *PixelProcessor::routine(const State &state)
1171 {
1172 Routine *routine = routineCache->query(state);
1173
1174 if(!routine)
1175 {
Alexis Hetuf2a8c372015-07-13 11:08:41 -04001176 const bool integerPipeline = (context->pixelShaderVersion() <= 0x0104);
Nicolas Capensba53fbf2016-01-14 13:43:42 -05001177 QuadRasterizer *generator = nullptr;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001178
Alexis Hetuf2a8c372015-07-13 11:08:41 -04001179 if(integerPipeline)
1180 {
1181 generator = new PixelPipeline(state, context->pixelShader);
1182 }
1183 else
1184 {
1185 generator = new PixelProgram(state, context->pixelShader);
1186 }
Nicolas Capens4f172c72016-01-13 08:34:30 -05001187
John Bauman89401822014-05-06 15:04:28 -04001188 generator->generate();
Nicolas Capens4f172c72016-01-13 08:34:30 -05001189 routine = (*generator)(L"PixelRoutine_%0.8X", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -04001190 delete generator;
1191
1192 routineCache->add(state, routine);
1193 }
1194
1195 return routine;
1196 }
1197}