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