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