blob: 76356e46da2bb335d619fbfbcf50892aa873054f [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
Alexis Hetuf2a8c372015-07-13 11:08:41 -040017#include "PixelPipeline.hpp"
18#include "PixelProgram.hpp"
John Bauman89401822014-05-06 15:04:28 -040019#include "PixelShader.hpp"
John Bauman89401822014-05-06 15:04:28 -040020#include "Surface.hpp"
21#include "Primitive.hpp"
22#include "Constants.hpp"
23#include "Debug.hpp"
24
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 Hetu1d01aa32015-09-29 11:50:05 -0400447 void PixelProcessor::setSwizzleR(unsigned int sampler, SwizzleType swizzleR)
448 {
449 if(sampler < TEXTURE_IMAGE_UNITS)
450 {
451 context->sampler[sampler].setSwizzleR(swizzleR);
452 }
453 else ASSERT(false);
454 }
455
456 void PixelProcessor::setSwizzleG(unsigned int sampler, SwizzleType swizzleG)
457 {
458 if(sampler < TEXTURE_IMAGE_UNITS)
459 {
460 context->sampler[sampler].setSwizzleG(swizzleG);
461 }
462 else ASSERT(false);
463 }
464
465 void PixelProcessor::setSwizzleB(unsigned int sampler, SwizzleType swizzleB)
466 {
467 if(sampler < TEXTURE_IMAGE_UNITS)
468 {
469 context->sampler[sampler].setSwizzleB(swizzleB);
470 }
471 else ASSERT(false);
472 }
473
474 void PixelProcessor::setSwizzleA(unsigned int sampler, SwizzleType swizzleA)
475 {
476 if(sampler < TEXTURE_IMAGE_UNITS)
477 {
478 context->sampler[sampler].setSwizzleA(swizzleA);
479 }
480 else ASSERT(false);
481 }
482
Alexis Hetu95ac1872016-06-06 13:26:52 -0400483 void PixelProcessor::setBaseLevel(unsigned int sampler, int baseLevel)
484 {
485 if(sampler < TEXTURE_IMAGE_UNITS)
486 {
487 context->sampler[sampler].setBaseLevel(baseLevel);
488 }
489 else ASSERT(false);
490 }
491
492 void PixelProcessor::setMaxLevel(unsigned int sampler, int maxLevel)
493 {
494 if(sampler < TEXTURE_IMAGE_UNITS)
495 {
496 context->sampler[sampler].setMaxLevel(maxLevel);
497 }
498 else ASSERT(false);
499 }
500
Alexis Hetu112d81f2016-06-07 12:36:35 -0400501 void PixelProcessor::setMinLod(unsigned int sampler, float minLod)
502 {
503 if(sampler < TEXTURE_IMAGE_UNITS)
504 {
505 context->sampler[sampler].setMinLod(minLod);
506 }
507 else ASSERT(false);
508 }
509
510 void PixelProcessor::setMaxLod(unsigned int sampler, float maxLod)
511 {
512 if(sampler < TEXTURE_IMAGE_UNITS)
513 {
514 context->sampler[sampler].setMaxLod(maxLod);
515 }
516 else ASSERT(false);
517 }
518
John Bauman89401822014-05-06 15:04:28 -0400519 void PixelProcessor::setWriteSRGB(bool sRGB)
520 {
521 context->setWriteSRGB(sRGB);
522 }
523
Maxime Grégoired9762742015-07-08 16:43:48 -0400524 void PixelProcessor::setColorLogicOpEnabled(bool colorLogicOpEnabled)
525 {
526 context->setColorLogicOpEnabled(colorLogicOpEnabled);
527 }
528
529 void PixelProcessor::setLogicalOperation(LogicalOperation logicalOperation)
530 {
531 context->setLogicalOperation(logicalOperation);
532 }
533
John Bauman89401822014-05-06 15:04:28 -0400534 void PixelProcessor::setDepthBufferEnable(bool depthBufferEnable)
535 {
536 context->setDepthBufferEnable(depthBufferEnable);
537 }
538
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400539 void PixelProcessor::setDepthCompare(DepthCompareMode depthCompareMode)
John Bauman89401822014-05-06 15:04:28 -0400540 {
541 context->depthCompareMode = depthCompareMode;
542 }
543
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400544 void PixelProcessor::setAlphaCompare(AlphaCompareMode alphaCompareMode)
John Bauman89401822014-05-06 15:04:28 -0400545 {
546 context->alphaCompareMode = alphaCompareMode;
547 }
548
549 void PixelProcessor::setDepthWriteEnable(bool depthWriteEnable)
550 {
551 context->depthWriteEnable = depthWriteEnable;
552 }
553
554 void PixelProcessor::setAlphaTestEnable(bool alphaTestEnable)
555 {
556 context->alphaTestEnable = alphaTestEnable;
557 }
558
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400559 void PixelProcessor::setCullMode(CullMode cullMode)
John Bauman89401822014-05-06 15:04:28 -0400560 {
561 context->cullMode = cullMode;
562 }
563
564 void PixelProcessor::setColorWriteMask(int index, int rgbaMask)
565 {
566 context->setColorWriteMask(index, rgbaMask);
567 }
568
569 void PixelProcessor::setStencilEnable(bool stencilEnable)
570 {
571 context->stencilEnable = stencilEnable;
572 }
573
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400574 void PixelProcessor::setStencilCompare(StencilCompareMode stencilCompareMode)
John Bauman89401822014-05-06 15:04:28 -0400575 {
576 context->stencilCompareMode = stencilCompareMode;
577 }
578
579 void PixelProcessor::setStencilReference(int stencilReference)
580 {
581 context->stencilReference = stencilReference;
582 stencil.set(stencilReference, context->stencilMask, context->stencilWriteMask);
583 }
584
585 void PixelProcessor::setStencilReferenceCCW(int stencilReferenceCCW)
586 {
587 context->stencilReferenceCCW = stencilReferenceCCW;
588 stencilCCW.set(stencilReferenceCCW, context->stencilMaskCCW, context->stencilWriteMaskCCW);
589 }
590
591 void PixelProcessor::setStencilMask(int stencilMask)
592 {
593 context->stencilMask = stencilMask;
594 stencil.set(context->stencilReference, stencilMask, context->stencilWriteMask);
595 }
596
597 void PixelProcessor::setStencilMaskCCW(int stencilMaskCCW)
598 {
599 context->stencilMaskCCW = stencilMaskCCW;
600 stencilCCW.set(context->stencilReferenceCCW, stencilMaskCCW, context->stencilWriteMaskCCW);
601 }
602
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400603 void PixelProcessor::setStencilFailOperation(StencilOperation stencilFailOperation)
John Bauman89401822014-05-06 15:04:28 -0400604 {
605 context->stencilFailOperation = stencilFailOperation;
606 }
607
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400608 void PixelProcessor::setStencilPassOperation(StencilOperation stencilPassOperation)
John Bauman89401822014-05-06 15:04:28 -0400609 {
610 context->stencilPassOperation = stencilPassOperation;
611 }
612
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400613 void PixelProcessor::setStencilZFailOperation(StencilOperation stencilZFailOperation)
John Bauman89401822014-05-06 15:04:28 -0400614 {
615 context->stencilZFailOperation = stencilZFailOperation;
616 }
617
618 void PixelProcessor::setStencilWriteMask(int stencilWriteMask)
619 {
620 context->stencilWriteMask = stencilWriteMask;
621 stencil.set(context->stencilReference, context->stencilMask, stencilWriteMask);
622 }
623
624 void PixelProcessor::setStencilWriteMaskCCW(int stencilWriteMaskCCW)
625 {
626 context->stencilWriteMaskCCW = stencilWriteMaskCCW;
627 stencilCCW.set(context->stencilReferenceCCW, context->stencilMaskCCW, stencilWriteMaskCCW);
628 }
629
630 void PixelProcessor::setTwoSidedStencil(bool enable)
631 {
632 context->twoSidedStencil = enable;
633 }
634
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400635 void PixelProcessor::setStencilCompareCCW(StencilCompareMode stencilCompareMode)
John Bauman89401822014-05-06 15:04:28 -0400636 {
637 context->stencilCompareModeCCW = stencilCompareMode;
638 }
639
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400640 void PixelProcessor::setStencilFailOperationCCW(StencilOperation stencilFailOperation)
John Bauman89401822014-05-06 15:04:28 -0400641 {
642 context->stencilFailOperationCCW = stencilFailOperation;
643 }
644
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400645 void PixelProcessor::setStencilPassOperationCCW(StencilOperation stencilPassOperation)
John Bauman89401822014-05-06 15:04:28 -0400646 {
647 context->stencilPassOperationCCW = stencilPassOperation;
648 }
649
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400650 void PixelProcessor::setStencilZFailOperationCCW(StencilOperation stencilZFailOperation)
John Bauman89401822014-05-06 15:04:28 -0400651 {
652 context->stencilZFailOperationCCW = stencilZFailOperation;
653 }
654
655 void PixelProcessor::setTextureFactor(const Color<float> &textureFactor)
656 {
657 // FIXME: Compact into generic function // FIXME: Clamp
658 short textureFactorR = iround(4095 * textureFactor.r);
659 short textureFactorG = iround(4095 * textureFactor.g);
660 short textureFactorB = iround(4095 * textureFactor.b);
661 short textureFactorA = iround(4095 * textureFactor.a);
662
663 factor.textureFactor4[0][0] = textureFactorR;
664 factor.textureFactor4[0][1] = textureFactorR;
665 factor.textureFactor4[0][2] = textureFactorR;
666 factor.textureFactor4[0][3] = textureFactorR;
667
668 factor.textureFactor4[1][0] = textureFactorG;
669 factor.textureFactor4[1][1] = textureFactorG;
670 factor.textureFactor4[1][2] = textureFactorG;
671 factor.textureFactor4[1][3] = textureFactorG;
672
673 factor.textureFactor4[2][0] = textureFactorB;
674 factor.textureFactor4[2][1] = textureFactorB;
675 factor.textureFactor4[2][2] = textureFactorB;
676 factor.textureFactor4[2][3] = textureFactorB;
677
678 factor.textureFactor4[3][0] = textureFactorA;
679 factor.textureFactor4[3][1] = textureFactorA;
680 factor.textureFactor4[3][2] = textureFactorA;
681 factor.textureFactor4[3][3] = textureFactorA;
682 }
683
684 void PixelProcessor::setBlendConstant(const Color<float> &blendConstant)
685 {
686 // FIXME: Compact into generic function // FIXME: Clamp
687 short blendConstantR = iround(65535 * blendConstant.r);
688 short blendConstantG = iround(65535 * blendConstant.g);
689 short blendConstantB = iround(65535 * blendConstant.b);
690 short blendConstantA = iround(65535 * blendConstant.a);
691
692 factor.blendConstant4W[0][0] = blendConstantR;
693 factor.blendConstant4W[0][1] = blendConstantR;
694 factor.blendConstant4W[0][2] = blendConstantR;
695 factor.blendConstant4W[0][3] = blendConstantR;
696
697 factor.blendConstant4W[1][0] = blendConstantG;
698 factor.blendConstant4W[1][1] = blendConstantG;
699 factor.blendConstant4W[1][2] = blendConstantG;
700 factor.blendConstant4W[1][3] = blendConstantG;
701
702 factor.blendConstant4W[2][0] = blendConstantB;
703 factor.blendConstant4W[2][1] = blendConstantB;
704 factor.blendConstant4W[2][2] = blendConstantB;
705 factor.blendConstant4W[2][3] = blendConstantB;
706
707 factor.blendConstant4W[3][0] = blendConstantA;
708 factor.blendConstant4W[3][1] = blendConstantA;
709 factor.blendConstant4W[3][2] = blendConstantA;
710 factor.blendConstant4W[3][3] = blendConstantA;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500711
John Bauman89401822014-05-06 15:04:28 -0400712 // FIXME: Compact into generic function // FIXME: Clamp
713 short invBlendConstantR = iround(65535 * (1 - blendConstant.r));
714 short invBlendConstantG = iround(65535 * (1 - blendConstant.g));
715 short invBlendConstantB = iround(65535 * (1 - blendConstant.b));
716 short invBlendConstantA = iround(65535 * (1 - blendConstant.a));
717
718 factor.invBlendConstant4W[0][0] = invBlendConstantR;
719 factor.invBlendConstant4W[0][1] = invBlendConstantR;
720 factor.invBlendConstant4W[0][2] = invBlendConstantR;
721 factor.invBlendConstant4W[0][3] = invBlendConstantR;
722
723 factor.invBlendConstant4W[1][0] = invBlendConstantG;
724 factor.invBlendConstant4W[1][1] = invBlendConstantG;
725 factor.invBlendConstant4W[1][2] = invBlendConstantG;
726 factor.invBlendConstant4W[1][3] = invBlendConstantG;
727
728 factor.invBlendConstant4W[2][0] = invBlendConstantB;
729 factor.invBlendConstant4W[2][1] = invBlendConstantB;
730 factor.invBlendConstant4W[2][2] = invBlendConstantB;
731 factor.invBlendConstant4W[2][3] = invBlendConstantB;
732
733 factor.invBlendConstant4W[3][0] = invBlendConstantA;
734 factor.invBlendConstant4W[3][1] = invBlendConstantA;
735 factor.invBlendConstant4W[3][2] = invBlendConstantA;
736 factor.invBlendConstant4W[3][3] = invBlendConstantA;
737
738 factor.blendConstant4F[0][0] = blendConstant.r;
739 factor.blendConstant4F[0][1] = blendConstant.r;
740 factor.blendConstant4F[0][2] = blendConstant.r;
741 factor.blendConstant4F[0][3] = blendConstant.r;
742
743 factor.blendConstant4F[1][0] = blendConstant.g;
744 factor.blendConstant4F[1][1] = blendConstant.g;
745 factor.blendConstant4F[1][2] = blendConstant.g;
746 factor.blendConstant4F[1][3] = blendConstant.g;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500747
John Bauman89401822014-05-06 15:04:28 -0400748 factor.blendConstant4F[2][0] = blendConstant.b;
749 factor.blendConstant4F[2][1] = blendConstant.b;
750 factor.blendConstant4F[2][2] = blendConstant.b;
751 factor.blendConstant4F[2][3] = blendConstant.b;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500752
John Bauman89401822014-05-06 15:04:28 -0400753 factor.blendConstant4F[3][0] = blendConstant.a;
754 factor.blendConstant4F[3][1] = blendConstant.a;
755 factor.blendConstant4F[3][2] = blendConstant.a;
756 factor.blendConstant4F[3][3] = blendConstant.a;
757
758 factor.invBlendConstant4F[0][0] = 1 - blendConstant.r;
759 factor.invBlendConstant4F[0][1] = 1 - blendConstant.r;
760 factor.invBlendConstant4F[0][2] = 1 - blendConstant.r;
761 factor.invBlendConstant4F[0][3] = 1 - blendConstant.r;
762
763 factor.invBlendConstant4F[1][0] = 1 - blendConstant.g;
764 factor.invBlendConstant4F[1][1] = 1 - blendConstant.g;
765 factor.invBlendConstant4F[1][2] = 1 - blendConstant.g;
766 factor.invBlendConstant4F[1][3] = 1 - blendConstant.g;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500767
John Bauman89401822014-05-06 15:04:28 -0400768 factor.invBlendConstant4F[2][0] = 1 - blendConstant.b;
769 factor.invBlendConstant4F[2][1] = 1 - blendConstant.b;
770 factor.invBlendConstant4F[2][2] = 1 - blendConstant.b;
771 factor.invBlendConstant4F[2][3] = 1 - blendConstant.b;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500772
John Bauman89401822014-05-06 15:04:28 -0400773 factor.invBlendConstant4F[3][0] = 1 - blendConstant.a;
774 factor.invBlendConstant4F[3][1] = 1 - blendConstant.a;
775 factor.invBlendConstant4F[3][2] = 1 - blendConstant.a;
776 factor.invBlendConstant4F[3][3] = 1 - blendConstant.a;
777 }
778
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400779 void PixelProcessor::setFillMode(FillMode fillMode)
John Bauman89401822014-05-06 15:04:28 -0400780 {
781 context->fillMode = fillMode;
782 }
783
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400784 void PixelProcessor::setShadingMode(ShadingMode shadingMode)
John Bauman89401822014-05-06 15:04:28 -0400785 {
786 context->shadingMode = shadingMode;
787 }
788
789 void PixelProcessor::setAlphaBlendEnable(bool alphaBlendEnable)
790 {
791 context->setAlphaBlendEnable(alphaBlendEnable);
792 }
793
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400794 void PixelProcessor::setSourceBlendFactor(BlendFactor sourceBlendFactor)
John Bauman89401822014-05-06 15:04:28 -0400795 {
796 context->setSourceBlendFactor(sourceBlendFactor);
797 }
798
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400799 void PixelProcessor::setDestBlendFactor(BlendFactor destBlendFactor)
John Bauman89401822014-05-06 15:04:28 -0400800 {
801 context->setDestBlendFactor(destBlendFactor);
802 }
803
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400804 void PixelProcessor::setBlendOperation(BlendOperation blendOperation)
John Bauman89401822014-05-06 15:04:28 -0400805 {
806 context->setBlendOperation(blendOperation);
807 }
808
809 void PixelProcessor::setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable)
810 {
811 context->setSeparateAlphaBlendEnable(separateAlphaBlendEnable);
812 }
813
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400814 void PixelProcessor::setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha)
John Bauman89401822014-05-06 15:04:28 -0400815 {
816 context->setSourceBlendFactorAlpha(sourceBlendFactorAlpha);
817 }
818
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400819 void PixelProcessor::setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha)
John Bauman89401822014-05-06 15:04:28 -0400820 {
821 context->setDestBlendFactorAlpha(destBlendFactorAlpha);
822 }
823
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400824 void PixelProcessor::setBlendOperationAlpha(BlendOperation blendOperationAlpha)
John Bauman89401822014-05-06 15:04:28 -0400825 {
826 context->setBlendOperationAlpha(blendOperationAlpha);
827 }
828
Alexis Hetua818c452015-06-11 13:06:58 -0400829 void PixelProcessor::setAlphaReference(float alphaReference)
John Bauman89401822014-05-06 15:04:28 -0400830 {
831 context->alphaReference = alphaReference;
832
Alexis Hetua818c452015-06-11 13:06:58 -0400833 factor.alphaReference4[0] = (word)iround(alphaReference * 0x1000 / 0xFF);
834 factor.alphaReference4[1] = (word)iround(alphaReference * 0x1000 / 0xFF);
835 factor.alphaReference4[2] = (word)iround(alphaReference * 0x1000 / 0xFF);
836 factor.alphaReference4[3] = (word)iround(alphaReference * 0x1000 / 0xFF);
John Bauman89401822014-05-06 15:04:28 -0400837 }
838
839 void PixelProcessor::setGlobalMipmapBias(float bias)
840 {
841 context->setGlobalMipmapBias(bias);
842 }
843
844 void PixelProcessor::setFogStart(float start)
845 {
846 setFogRanges(start, context->fogEnd);
847 }
848
849 void PixelProcessor::setFogEnd(float end)
850 {
851 setFogRanges(context->fogStart, end);
852 }
853
854 void PixelProcessor::setFogColor(Color<float> fogColor)
855 {
856 // TODO: Compact into generic function
857 word fogR = (unsigned short)(65535 * fogColor.r);
858 word fogG = (unsigned short)(65535 * fogColor.g);
859 word fogB = (unsigned short)(65535 * fogColor.b);
860
861 fog.color4[0][0] = fogR;
862 fog.color4[0][1] = fogR;
863 fog.color4[0][2] = fogR;
864 fog.color4[0][3] = fogR;
865
866 fog.color4[1][0] = fogG;
867 fog.color4[1][1] = fogG;
868 fog.color4[1][2] = fogG;
869 fog.color4[1][3] = fogG;
870
871 fog.color4[2][0] = fogB;
872 fog.color4[2][1] = fogB;
873 fog.color4[2][2] = fogB;
874 fog.color4[2][3] = fogB;
875
876 fog.colorF[0] = replicate(fogColor.r);
877 fog.colorF[1] = replicate(fogColor.g);
878 fog.colorF[2] = replicate(fogColor.b);
879 }
880
881 void PixelProcessor::setFogDensity(float fogDensity)
882 {
883 fog.densityE = replicate(-fogDensity * 1.442695f); // 1/e^x = 2^(-x*1.44)
Nicolas Capensa36f3f92015-08-04 15:34:26 -0400884 fog.density2E = replicate(-fogDensity * fogDensity * 1.442695f);
John Bauman89401822014-05-06 15:04:28 -0400885 }
886
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400887 void PixelProcessor::setPixelFogMode(FogMode fogMode)
John Bauman89401822014-05-06 15:04:28 -0400888 {
889 context->pixelFogMode = fogMode;
890 }
891
892 void PixelProcessor::setPerspectiveCorrection(bool perspectiveEnable)
893 {
894 perspectiveCorrection = perspectiveEnable;
895 }
896
897 void PixelProcessor::setOcclusionEnabled(bool enable)
898 {
899 context->occlusionEnabled = enable;
900 }
901
902 void PixelProcessor::setRoutineCacheSize(int cacheSize)
903 {
904 delete routineCache;
John Bauman66b8ab22014-05-06 15:57:45 -0400905 routineCache = new RoutineCache<State>(clamp(cacheSize, 1, 65536), precachePixel ? "sw-pixel" : 0);
John Bauman89401822014-05-06 15:04:28 -0400906 }
907
908 void PixelProcessor::setFogRanges(float start, float end)
909 {
910 context->fogStart = start;
911 context->fogEnd = end;
912
913 if(start == end)
914 {
915 end += 0.001f; // Hack: ensure there is a small range
916 }
917
918 float fogScale = -1.0f / (end - start);
919 float fogOffset = end * -fogScale;
920
921 fog.scale = replicate(fogScale);
922 fog.offset = replicate(fogOffset);
923 }
924
925 const PixelProcessor::State PixelProcessor::update() const
926 {
927 State state;
928
929 if(context->pixelShader)
930 {
John Bauman19bac1e2014-05-06 15:23:49 -0400931 state.shaderID = context->pixelShader->getSerialID();
John Bauman89401822014-05-06 15:04:28 -0400932 }
933 else
934 {
John Bauman19bac1e2014-05-06 15:23:49 -0400935 state.shaderID = 0;
John Bauman89401822014-05-06 15:04:28 -0400936 }
937
938 state.depthOverride = context->pixelShader && context->pixelShader->depthOverride();
John Bauman19bac1e2014-05-06 15:23:49 -0400939 state.shaderContainsKill = context->pixelShader ? context->pixelShader->containsKill() : false;
Nicolas Capens4f172c72016-01-13 08:34:30 -0500940
John Bauman89401822014-05-06 15:04:28 -0400941 if(context->alphaTestActive())
942 {
943 state.alphaCompareMode = context->alphaCompareMode;
944
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400945 state.transparencyAntialiasing = context->getMultiSampleCount() > 1 ? transparencyAntialiasing : TRANSPARENCY_NONE;
John Bauman89401822014-05-06 15:04:28 -0400946 }
947
948 state.depthWriteEnable = context->depthWriteActive();
949
950 if(context->stencilActive())
951 {
952 state.stencilActive = true;
953 state.stencilCompareMode = context->stencilCompareMode;
954 state.stencilFailOperation = context->stencilFailOperation;
955 state.stencilPassOperation = context->stencilPassOperation;
956 state.stencilZFailOperation = context->stencilZFailOperation;
957 state.noStencilMask = (context->stencilMask == 0xFF);
958 state.noStencilWriteMask = (context->stencilWriteMask == 0xFF);
959 state.stencilWriteMasked = (context->stencilWriteMask == 0x00);
960
961 state.twoSidedStencil = context->twoSidedStencil;
962 state.stencilCompareModeCCW = context->twoSidedStencil ? context->stencilCompareModeCCW : state.stencilCompareMode;
963 state.stencilFailOperationCCW = context->twoSidedStencil ? context->stencilFailOperationCCW : state.stencilFailOperation;
964 state.stencilPassOperationCCW = context->twoSidedStencil ? context->stencilPassOperationCCW : state.stencilPassOperation;
965 state.stencilZFailOperationCCW = context->twoSidedStencil ? context->stencilZFailOperationCCW : state.stencilZFailOperation;
966 state.noStencilMaskCCW = context->twoSidedStencil ? (context->stencilMaskCCW == 0xFF) : state.noStencilMask;
967 state.noStencilWriteMaskCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0xFF) : state.noStencilWriteMask;
968 state.stencilWriteMaskedCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0x00) : state.stencilWriteMasked;
969 }
970
971 if(context->depthBufferActive())
972 {
973 state.depthTestActive = true;
974 state.depthCompareMode = context->depthCompareMode;
Nicolas Capens3751c1e2016-03-21 14:14:14 -0400975 state.quadLayoutDepthBuffer = context->depthBuffer->getInternalFormat() != FORMAT_D32F_LOCKABLE &&
976 context->depthBuffer->getInternalFormat() != FORMAT_D32FS8_TEXTURE &&
977 context->depthBuffer->getInternalFormat() != FORMAT_D32FS8_SHADOW;
John Bauman89401822014-05-06 15:04:28 -0400978 }
979
980 state.occlusionEnabled = context->occlusionEnabled;
981
982 state.fogActive = context->fogActive();
983 state.pixelFogMode = context->pixelFogActive();
Nicolas Capensa0f4be82014-10-22 14:35:30 -0400984 state.wBasedFog = context->wBasedFog && context->pixelFogActive() != FOG_NONE;
John Bauman89401822014-05-06 15:04:28 -0400985 state.perspective = context->perspectiveActive();
986
987 if(context->alphaBlendActive())
988 {
989 state.alphaBlendActive = true;
990 state.sourceBlendFactor = context->sourceBlendFactor();
991 state.destBlendFactor = context->destBlendFactor();
992 state.blendOperation = context->blendOperation();
993 state.sourceBlendFactorAlpha = context->sourceBlendFactorAlpha();
994 state.destBlendFactorAlpha = context->destBlendFactorAlpha();
995 state.blendOperationAlpha = context->blendOperationAlpha();
996 }
Maxime Grégoired9762742015-07-08 16:43:48 -0400997
998 state.logicalOperation = context->colorLogicOp();
999
Alexis Hetu1edcd8b2015-11-05 11:12:41 -05001000 for(int i = 0; i < RENDERTARGETS; i++)
John Bauman89401822014-05-06 15:04:28 -04001001 {
Nicolas Capensc98186a2016-04-18 12:07:22 -04001002 state.colorWriteMask |= context->colorWriteActive(i) << (4 * i);
John Bauman89401822014-05-06 15:04:28 -04001003 state.targetFormat[i] = context->renderTargetInternalFormat(i);
1004 }
1005
John Bauman66b8ab22014-05-06 15:57:45 -04001006 state.writeSRGB = context->writeSRGB && context->renderTarget[0] && Surface::isSRGBwritable(context->renderTarget[0]->getExternalFormat());
1007 state.multiSample = context->getMultiSampleCount();
John Bauman89401822014-05-06 15:04:28 -04001008 state.multiSampleMask = context->multiSampleMask;
1009
1010 if(state.multiSample > 1 && context->pixelShader)
1011 {
1012 state.centroid = context->pixelShader->containsCentroid();
1013 }
1014
1015 if(!context->pixelShader)
1016 {
1017 for(unsigned int i = 0; i < 8; i++)
1018 {
1019 state.textureStage[i] = context->textureStage[i].textureStageState();
1020 }
1021
1022 state.specularAdd = context->specularActive() && context->specularEnable;
1023 }
1024
1025 for(unsigned int i = 0; i < 16; i++)
1026 {
1027 if(context->pixelShader)
1028 {
1029 if(context->pixelShader->usesSampler(i))
1030 {
1031 state.sampler[i] = context->sampler[i].samplerState();
1032 }
1033 }
1034 else
1035 {
1036 if(i < 8 && state.textureStage[i].stageOperation != TextureStage::STAGE_DISABLE)
1037 {
1038 state.sampler[i] = context->sampler[i].samplerState();
1039 }
1040 else break;
1041 }
1042 }
1043
1044 const bool point = context->isDrawPoint(true);
1045 const bool sprite = context->pointSpriteActive();
Nicolas Capensa0f4be82014-10-22 14:35:30 -04001046 const bool flatShading = (context->shadingMode == SHADING_FLAT) || point;
John Bauman89401822014-05-06 15:04:28 -04001047
1048 if(context->pixelShaderVersion() < 0x0300)
1049 {
1050 for(int coordinate = 0; coordinate < 8; coordinate++)
1051 {
1052 for(int component = 0; component < 4; component++)
1053 {
1054 if(context->textureActive(coordinate, component))
1055 {
1056 state.texture[coordinate].component |= 1 << component;
1057
1058 if(point && !sprite)
1059 {
1060 state.texture[coordinate].flat |= 1 << component;
1061 }
1062 }
1063 }
1064
1065 if(context->textureTransformProject[coordinate] && context->pixelShaderVersion() <= 0x0103)
1066 {
1067 if(context->textureTransformCount[coordinate] == 2)
1068 {
1069 state.texture[coordinate].project = 1;
1070 }
1071 else if(context->textureTransformCount[coordinate] == 3)
1072 {
1073 state.texture[coordinate].project = 2;
1074 }
1075 else if(context->textureTransformCount[coordinate] == 4 || context->textureTransformCount[coordinate] == 0)
1076 {
1077 state.texture[coordinate].project = 3;
1078 }
1079 }
1080 }
1081
1082 for(int color = 0; color < 2; color++)
1083 {
1084 for(int component = 0; component < 4; component++)
1085 {
1086 if(context->colorActive(color, component))
1087 {
1088 state.color[color].component |= 1 << component;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001089
John Bauman89401822014-05-06 15:04:28 -04001090 if(point || flatShading)
1091 {
1092 state.color[color].flat |= 1 << component;
1093 }
1094 }
1095 }
1096 }
1097
1098 if(context->fogActive())
1099 {
1100 state.fog.component = true;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001101
John Bauman89401822014-05-06 15:04:28 -04001102 if(point)
1103 {
1104 state.fog.flat = true;
1105 }
1106 }
1107 }
1108 else
1109 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04001110 for(int interpolant = 0; interpolant < MAX_FRAGMENT_INPUTS; interpolant++)
John Bauman89401822014-05-06 15:04:28 -04001111 {
1112 for(int component = 0; component < 4; component++)
1113 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04001114 const Shader::Semantic &semantic = context->pixelShader->getInput(interpolant, component);
Alexis Hetu12b00502016-05-20 13:01:11 -04001115
1116 if(semantic.active())
John Bauman89401822014-05-06 15:04:28 -04001117 {
1118 bool flat = point;
1119
Alexis Hetu12b00502016-05-20 13:01:11 -04001120 switch(semantic.usage)
John Bauman89401822014-05-06 15:04:28 -04001121 {
Alexis Hetu12b00502016-05-20 13:01:11 -04001122 case Shader::USAGE_TEXCOORD: flat = point && !sprite; break;
1123 case Shader::USAGE_COLOR: flat = semantic.flat || flatShading; break;
John Bauman89401822014-05-06 15:04:28 -04001124 }
1125
1126 state.interpolant[interpolant].component |= 1 << component;
1127
1128 if(flat)
1129 {
1130 state.interpolant[interpolant].flat |= 1 << component;
1131 }
1132 }
1133 }
1134 }
1135 }
1136
1137 if(state.centroid)
1138 {
Nicolas Capens3b4c93f2016-05-18 12:51:37 -04001139 for(int interpolant = 0; interpolant < MAX_FRAGMENT_INPUTS; interpolant++)
John Bauman89401822014-05-06 15:04:28 -04001140 {
1141 for(int component = 0; component < 4; component++)
1142 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -04001143 state.interpolant[interpolant].centroid = context->pixelShader->getInput(interpolant, 0).centroid;
John Bauman89401822014-05-06 15:04:28 -04001144 }
1145 }
1146 }
1147
1148 state.hash = state.computeHash();
1149
1150 return state;
1151 }
1152
1153 Routine *PixelProcessor::routine(const State &state)
1154 {
1155 Routine *routine = routineCache->query(state);
1156
1157 if(!routine)
1158 {
Alexis Hetuf2a8c372015-07-13 11:08:41 -04001159 const bool integerPipeline = (context->pixelShaderVersion() <= 0x0104);
Nicolas Capensba53fbf2016-01-14 13:43:42 -05001160 QuadRasterizer *generator = nullptr;
Nicolas Capens4f172c72016-01-13 08:34:30 -05001161
Alexis Hetuf2a8c372015-07-13 11:08:41 -04001162 if(integerPipeline)
1163 {
1164 generator = new PixelPipeline(state, context->pixelShader);
1165 }
1166 else
1167 {
1168 generator = new PixelProgram(state, context->pixelShader);
1169 }
Nicolas Capens4f172c72016-01-13 08:34:30 -05001170
John Bauman89401822014-05-06 15:04:28 -04001171 generator->generate();
Nicolas Capens4f172c72016-01-13 08:34:30 -05001172 routine = (*generator)(L"PixelRoutine_%0.8X", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -04001173 delete generator;
1174
1175 routineCache->add(state, routine);
1176 }
1177
1178 return routine;
1179 }
1180}