apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // Program.cpp: Implements the gl::Program class. Implements GL program objects |
| 8 | // and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28. |
| 9 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 10 | #include "libGLESv2/BinaryStream.h" |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 11 | #include "libGLESv2/ProgramBinary.h" |
| 12 | |
| 13 | #include "common/debug.h" |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 14 | #include "common/version.h" |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 15 | |
| 16 | #include "libGLESv2/main.h" |
| 17 | #include "libGLESv2/Shader.h" |
| 18 | #include "libGLESv2/utilities.h" |
| 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | #if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL) |
| 23 | #define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3 |
| 24 | #endif |
| 25 | |
| 26 | namespace gl |
| 27 | { |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 28 | std::string str(int i) |
| 29 | { |
| 30 | char buffer[20]; |
| 31 | snprintf(buffer, sizeof(buffer), "%d", i); |
| 32 | return buffer; |
| 33 | } |
| 34 | |
| 35 | Uniform::Uniform(GLenum type, const std::string &_name, unsigned int arraySize) |
| 36 | : type(type), _name(_name), name(ProgramBinary::undecorateUniform(_name)), arraySize(arraySize) |
| 37 | { |
| 38 | int bytes = UniformInternalSize(type) * arraySize; |
| 39 | data = new unsigned char[bytes]; |
| 40 | memset(data, 0, bytes); |
| 41 | dirty = true; |
| 42 | } |
| 43 | |
| 44 | Uniform::~Uniform() |
| 45 | { |
| 46 | delete[] data; |
| 47 | } |
| 48 | |
| 49 | bool Uniform::isArray() |
| 50 | { |
daniel@transgaming.com | 22ba0f7 | 2012-09-27 17:46:04 +0000 | [diff] [blame] | 51 | size_t dot = _name.find_last_of('.'); |
| 52 | if (dot == std::string::npos) dot = -1; |
| 53 | |
| 54 | return _name.compare(dot + 1, dot + 4, "ar_") == 0; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index) |
| 58 | : name(ProgramBinary::undecorateUniform(_name)), element(element), index(index) |
| 59 | { |
| 60 | } |
| 61 | |
daniel@transgaming.com | e87ca00 | 2012-07-24 18:30:43 +0000 | [diff] [blame] | 62 | unsigned int ProgramBinary::mCurrentSerial = 1; |
| 63 | |
daniel@transgaming.com | 70062c9 | 2012-11-28 19:32:30 +0000 | [diff] [blame] | 64 | ProgramBinary::ProgramBinary(rx::Renderer *renderer) : RefCountObject(0), mSerial(issueSerial()) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 65 | { |
daniel@transgaming.com | 70062c9 | 2012-11-28 19:32:30 +0000 | [diff] [blame] | 66 | ASSERT(dynamic_cast<rx::Renderer9*>(renderer) != NULL); // D3D9_REPLACE |
| 67 | mRenderer = static_cast<rx::Renderer9*>(renderer); |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 68 | mDevice = mRenderer->getDevice(); // D3D9_REPLACE |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 69 | |
| 70 | mPixelExecutable = NULL; |
| 71 | mVertexExecutable = NULL; |
| 72 | mConstantTablePS = NULL; |
| 73 | mConstantTableVS = NULL; |
| 74 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 75 | mValidated = false; |
| 76 | |
| 77 | for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++) |
| 78 | { |
| 79 | mSemanticIndex[index] = -1; |
| 80 | } |
| 81 | |
| 82 | for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++) |
| 83 | { |
| 84 | mSamplersPS[index].active = false; |
| 85 | } |
| 86 | |
| 87 | for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++) |
| 88 | { |
| 89 | mSamplersVS[index].active = false; |
| 90 | } |
| 91 | |
| 92 | mUsedVertexSamplerRange = 0; |
| 93 | mUsedPixelSamplerRange = 0; |
| 94 | |
| 95 | mDxDepthRangeLocation = -1; |
| 96 | mDxDepthLocation = -1; |
| 97 | mDxCoordLocation = -1; |
| 98 | mDxHalfPixelSizeLocation = -1; |
| 99 | mDxFrontCCWLocation = -1; |
| 100 | mDxPointsOrLinesLocation = -1; |
| 101 | } |
| 102 | |
| 103 | ProgramBinary::~ProgramBinary() |
| 104 | { |
| 105 | if (mPixelExecutable) |
| 106 | { |
| 107 | mPixelExecutable->Release(); |
| 108 | } |
| 109 | |
| 110 | if (mVertexExecutable) |
| 111 | { |
| 112 | mVertexExecutable->Release(); |
| 113 | } |
| 114 | |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 115 | delete mConstantTablePS; |
| 116 | delete mConstantTableVS; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 117 | |
| 118 | while (!mUniforms.empty()) |
| 119 | { |
| 120 | delete mUniforms.back(); |
| 121 | mUniforms.pop_back(); |
| 122 | } |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 123 | } |
| 124 | |
daniel@transgaming.com | e87ca00 | 2012-07-24 18:30:43 +0000 | [diff] [blame] | 125 | unsigned int ProgramBinary::getSerial() const |
| 126 | { |
| 127 | return mSerial; |
| 128 | } |
| 129 | |
| 130 | unsigned int ProgramBinary::issueSerial() |
| 131 | { |
| 132 | return mCurrentSerial++; |
| 133 | } |
| 134 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 135 | IDirect3DPixelShader9 *ProgramBinary::getPixelShader() |
| 136 | { |
| 137 | return mPixelExecutable; |
| 138 | } |
| 139 | |
| 140 | IDirect3DVertexShader9 *ProgramBinary::getVertexShader() |
| 141 | { |
| 142 | return mVertexExecutable; |
| 143 | } |
| 144 | |
| 145 | GLuint ProgramBinary::getAttributeLocation(const char *name) |
| 146 | { |
| 147 | if (name) |
| 148 | { |
| 149 | for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++) |
| 150 | { |
| 151 | if (mLinkedAttribute[index].name == std::string(name)) |
| 152 | { |
| 153 | return index; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | int ProgramBinary::getSemanticIndex(int attributeIndex) |
| 162 | { |
| 163 | ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS); |
| 164 | |
| 165 | return mSemanticIndex[attributeIndex]; |
| 166 | } |
| 167 | |
| 168 | // Returns one more than the highest sampler index used. |
| 169 | GLint ProgramBinary::getUsedSamplerRange(SamplerType type) |
| 170 | { |
| 171 | switch (type) |
| 172 | { |
| 173 | case SAMPLER_PIXEL: |
| 174 | return mUsedPixelSamplerRange; |
| 175 | case SAMPLER_VERTEX: |
| 176 | return mUsedVertexSamplerRange; |
| 177 | default: |
| 178 | UNREACHABLE(); |
| 179 | return 0; |
| 180 | } |
| 181 | } |
| 182 | |
daniel@transgaming.com | 087e578 | 2012-09-17 21:28:47 +0000 | [diff] [blame] | 183 | bool ProgramBinary::usesPointSize() const |
| 184 | { |
| 185 | return mUsesPointSize; |
| 186 | } |
| 187 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 188 | // Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler |
| 189 | // index (0-15 for the pixel shader and 0-3 for the vertex shader). |
| 190 | GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex) |
| 191 | { |
| 192 | GLint logicalTextureUnit = -1; |
| 193 | |
| 194 | switch (type) |
| 195 | { |
| 196 | case SAMPLER_PIXEL: |
| 197 | ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0])); |
| 198 | |
| 199 | if (mSamplersPS[samplerIndex].active) |
| 200 | { |
| 201 | logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit; |
| 202 | } |
| 203 | break; |
| 204 | case SAMPLER_VERTEX: |
| 205 | ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0])); |
| 206 | |
| 207 | if (mSamplersVS[samplerIndex].active) |
| 208 | { |
| 209 | logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit; |
| 210 | } |
| 211 | break; |
| 212 | default: UNREACHABLE(); |
| 213 | } |
| 214 | |
| 215 | if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits()) |
| 216 | { |
| 217 | return logicalTextureUnit; |
| 218 | } |
| 219 | |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | // Returns the texture type for a given Direct3D 9 sampler type and |
| 224 | // index (0-15 for the pixel shader and 0-3 for the vertex shader). |
| 225 | TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex) |
| 226 | { |
| 227 | switch (type) |
| 228 | { |
| 229 | case SAMPLER_PIXEL: |
| 230 | ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0])); |
| 231 | ASSERT(mSamplersPS[samplerIndex].active); |
| 232 | return mSamplersPS[samplerIndex].textureType; |
| 233 | case SAMPLER_VERTEX: |
| 234 | ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0])); |
| 235 | ASSERT(mSamplersVS[samplerIndex].active); |
| 236 | return mSamplersVS[samplerIndex].textureType; |
| 237 | default: UNREACHABLE(); |
| 238 | } |
| 239 | |
| 240 | return TEXTURE_2D; |
| 241 | } |
| 242 | |
| 243 | GLint ProgramBinary::getUniformLocation(std::string name) |
| 244 | { |
| 245 | unsigned int subscript = 0; |
| 246 | |
| 247 | // Strip any trailing array operator and retrieve the subscript |
| 248 | size_t open = name.find_last_of('['); |
| 249 | size_t close = name.find_last_of(']'); |
| 250 | if (open != std::string::npos && close == name.length() - 1) |
| 251 | { |
| 252 | subscript = atoi(name.substr(open + 1).c_str()); |
| 253 | name.erase(open); |
| 254 | } |
| 255 | |
| 256 | unsigned int numUniforms = mUniformIndex.size(); |
| 257 | for (unsigned int location = 0; location < numUniforms; location++) |
| 258 | { |
| 259 | if (mUniformIndex[location].name == name && |
| 260 | mUniformIndex[location].element == subscript) |
| 261 | { |
| 262 | return location; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v) |
| 270 | { |
| 271 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 272 | { |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 277 | targetUniform->dirty = true; |
| 278 | |
| 279 | if (targetUniform->type == GL_FLOAT) |
| 280 | { |
| 281 | int arraySize = targetUniform->arraySize; |
| 282 | |
| 283 | if (arraySize == 1 && count > 1) |
| 284 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 285 | |
| 286 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 287 | |
| 288 | GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; |
| 289 | |
| 290 | for (int i = 0; i < count; i++) |
| 291 | { |
| 292 | target[0] = v[0]; |
| 293 | target[1] = 0; |
| 294 | target[2] = 0; |
| 295 | target[3] = 0; |
| 296 | target += 4; |
| 297 | v += 1; |
| 298 | } |
| 299 | } |
| 300 | else if (targetUniform->type == GL_BOOL) |
| 301 | { |
| 302 | int arraySize = targetUniform->arraySize; |
| 303 | |
| 304 | if (arraySize == 1 && count > 1) |
| 305 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 306 | |
| 307 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 308 | GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element; |
| 309 | |
| 310 | for (int i = 0; i < count; ++i) |
| 311 | { |
| 312 | if (v[i] == 0.0f) |
| 313 | { |
| 314 | boolParams[i] = GL_FALSE; |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | boolParams[i] = GL_TRUE; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 331 | { |
| 332 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 333 | { |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 338 | targetUniform->dirty = true; |
| 339 | |
| 340 | if (targetUniform->type == GL_FLOAT_VEC2) |
| 341 | { |
| 342 | int arraySize = targetUniform->arraySize; |
| 343 | |
| 344 | if (arraySize == 1 && count > 1) |
| 345 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 346 | |
| 347 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 348 | |
| 349 | GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; |
| 350 | |
| 351 | for (int i = 0; i < count; i++) |
| 352 | { |
| 353 | target[0] = v[0]; |
| 354 | target[1] = v[1]; |
| 355 | target[2] = 0; |
| 356 | target[3] = 0; |
| 357 | target += 4; |
| 358 | v += 2; |
| 359 | } |
| 360 | } |
| 361 | else if (targetUniform->type == GL_BOOL_VEC2) |
| 362 | { |
| 363 | int arraySize = targetUniform->arraySize; |
| 364 | |
| 365 | if (arraySize == 1 && count > 1) |
| 366 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 367 | |
| 368 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 369 | |
| 370 | GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2; |
| 371 | |
| 372 | for (int i = 0; i < count * 2; ++i) |
| 373 | { |
| 374 | if (v[i] == 0.0f) |
| 375 | { |
| 376 | boolParams[i] = GL_FALSE; |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | boolParams[i] = GL_TRUE; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 393 | { |
| 394 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 395 | { |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 400 | targetUniform->dirty = true; |
| 401 | |
| 402 | if (targetUniform->type == GL_FLOAT_VEC3) |
| 403 | { |
| 404 | int arraySize = targetUniform->arraySize; |
| 405 | |
| 406 | if (arraySize == 1 && count > 1) |
| 407 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 408 | |
| 409 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 410 | |
| 411 | GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; |
| 412 | |
| 413 | for (int i = 0; i < count; i++) |
| 414 | { |
| 415 | target[0] = v[0]; |
| 416 | target[1] = v[1]; |
| 417 | target[2] = v[2]; |
| 418 | target[3] = 0; |
| 419 | target += 4; |
| 420 | v += 3; |
| 421 | } |
| 422 | } |
| 423 | else if (targetUniform->type == GL_BOOL_VEC3) |
| 424 | { |
| 425 | int arraySize = targetUniform->arraySize; |
| 426 | |
| 427 | if (arraySize == 1 && count > 1) |
| 428 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 429 | |
| 430 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 431 | GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3; |
| 432 | |
| 433 | for (int i = 0; i < count * 3; ++i) |
| 434 | { |
| 435 | if (v[i] == 0.0f) |
| 436 | { |
| 437 | boolParams[i] = GL_FALSE; |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | boolParams[i] = GL_TRUE; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | else |
| 446 | { |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 454 | { |
| 455 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 456 | { |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 461 | targetUniform->dirty = true; |
| 462 | |
| 463 | if (targetUniform->type == GL_FLOAT_VEC4) |
| 464 | { |
| 465 | int arraySize = targetUniform->arraySize; |
| 466 | |
| 467 | if (arraySize == 1 && count > 1) |
| 468 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 469 | |
| 470 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 471 | |
| 472 | memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4, |
| 473 | v, 4 * sizeof(GLfloat) * count); |
| 474 | } |
| 475 | else if (targetUniform->type == GL_BOOL_VEC4) |
| 476 | { |
| 477 | int arraySize = targetUniform->arraySize; |
| 478 | |
| 479 | if (arraySize == 1 && count > 1) |
| 480 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 481 | |
| 482 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 483 | GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4; |
| 484 | |
| 485 | for (int i = 0; i < count * 4; ++i) |
| 486 | { |
| 487 | if (v[i] == 0.0f) |
| 488 | { |
| 489 | boolParams[i] = GL_FALSE; |
| 490 | } |
| 491 | else |
| 492 | { |
| 493 | boolParams[i] = GL_TRUE; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | return true; |
| 503 | } |
| 504 | |
| 505 | template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight> |
| 506 | void transposeMatrix(T *target, const GLfloat *value) |
| 507 | { |
| 508 | int copyWidth = std::min(targetWidth, srcWidth); |
| 509 | int copyHeight = std::min(targetHeight, srcHeight); |
| 510 | |
| 511 | for (int x = 0; x < copyWidth; x++) |
| 512 | { |
| 513 | for (int y = 0; y < copyHeight; y++) |
| 514 | { |
| 515 | target[x * targetWidth + y] = (T)value[y * srcWidth + x]; |
| 516 | } |
| 517 | } |
| 518 | // clear unfilled right side |
| 519 | for (int y = 0; y < copyHeight; y++) |
| 520 | { |
| 521 | for (int x = srcWidth; x < targetWidth; x++) |
| 522 | { |
| 523 | target[y * targetWidth + x] = (T)0; |
| 524 | } |
| 525 | } |
| 526 | // clear unfilled bottom. |
| 527 | for (int y = srcHeight; y < targetHeight; y++) |
| 528 | { |
| 529 | for (int x = 0; x < targetWidth; x++) |
| 530 | { |
| 531 | target[y * targetWidth + x] = (T)0; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value) |
| 537 | { |
| 538 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 539 | { |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 544 | targetUniform->dirty = true; |
| 545 | |
| 546 | if (targetUniform->type != GL_FLOAT_MAT2) |
| 547 | { |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | int arraySize = targetUniform->arraySize; |
| 552 | |
| 553 | if (arraySize == 1 && count > 1) |
| 554 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 555 | |
| 556 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 557 | |
| 558 | GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8; |
| 559 | for (int i = 0; i < count; i++) |
| 560 | { |
| 561 | transposeMatrix<GLfloat,4,2,2,2>(target, value); |
| 562 | target += 8; |
| 563 | value += 4; |
| 564 | } |
| 565 | |
| 566 | return true; |
| 567 | } |
| 568 | |
| 569 | bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value) |
| 570 | { |
| 571 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 572 | { |
| 573 | return false; |
| 574 | } |
| 575 | |
| 576 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 577 | targetUniform->dirty = true; |
| 578 | |
| 579 | if (targetUniform->type != GL_FLOAT_MAT3) |
| 580 | { |
| 581 | return false; |
| 582 | } |
| 583 | |
| 584 | int arraySize = targetUniform->arraySize; |
| 585 | |
| 586 | if (arraySize == 1 && count > 1) |
| 587 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 588 | |
| 589 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 590 | |
| 591 | GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12; |
| 592 | for (int i = 0; i < count; i++) |
| 593 | { |
| 594 | transposeMatrix<GLfloat,4,3,3,3>(target, value); |
| 595 | target += 12; |
| 596 | value += 9; |
| 597 | } |
| 598 | |
| 599 | return true; |
| 600 | } |
| 601 | |
| 602 | |
| 603 | bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value) |
| 604 | { |
| 605 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 606 | { |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 611 | targetUniform->dirty = true; |
| 612 | |
| 613 | if (targetUniform->type != GL_FLOAT_MAT4) |
| 614 | { |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | int arraySize = targetUniform->arraySize; |
| 619 | |
| 620 | if (arraySize == 1 && count > 1) |
| 621 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 622 | |
| 623 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 624 | |
| 625 | GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16); |
| 626 | for (int i = 0; i < count; i++) |
| 627 | { |
| 628 | transposeMatrix<GLfloat,4,4,4,4>(target, value); |
| 629 | target += 16; |
| 630 | value += 16; |
| 631 | } |
| 632 | |
| 633 | return true; |
| 634 | } |
| 635 | |
| 636 | bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
| 637 | { |
| 638 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 639 | { |
| 640 | return false; |
| 641 | } |
| 642 | |
| 643 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 644 | targetUniform->dirty = true; |
| 645 | |
| 646 | if (targetUniform->type == GL_INT || |
| 647 | targetUniform->type == GL_SAMPLER_2D || |
| 648 | targetUniform->type == GL_SAMPLER_CUBE) |
| 649 | { |
| 650 | int arraySize = targetUniform->arraySize; |
| 651 | |
| 652 | if (arraySize == 1 && count > 1) |
| 653 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 654 | |
| 655 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 656 | |
| 657 | memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint), |
| 658 | v, sizeof(GLint) * count); |
| 659 | } |
| 660 | else if (targetUniform->type == GL_BOOL) |
| 661 | { |
| 662 | int arraySize = targetUniform->arraySize; |
| 663 | |
| 664 | if (arraySize == 1 && count > 1) |
| 665 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 666 | |
| 667 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 668 | GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element; |
| 669 | |
| 670 | for (int i = 0; i < count; ++i) |
| 671 | { |
| 672 | if (v[i] == 0) |
| 673 | { |
| 674 | boolParams[i] = GL_FALSE; |
| 675 | } |
| 676 | else |
| 677 | { |
| 678 | boolParams[i] = GL_TRUE; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | else |
| 683 | { |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v) |
| 691 | { |
| 692 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 693 | { |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 698 | targetUniform->dirty = true; |
| 699 | |
| 700 | if (targetUniform->type == GL_INT_VEC2) |
| 701 | { |
| 702 | int arraySize = targetUniform->arraySize; |
| 703 | |
| 704 | if (arraySize == 1 && count > 1) |
| 705 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 706 | |
| 707 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 708 | |
| 709 | memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2, |
| 710 | v, 2 * sizeof(GLint) * count); |
| 711 | } |
| 712 | else if (targetUniform->type == GL_BOOL_VEC2) |
| 713 | { |
| 714 | int arraySize = targetUniform->arraySize; |
| 715 | |
| 716 | if (arraySize == 1 && count > 1) |
| 717 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 718 | |
| 719 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 720 | GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2; |
| 721 | |
| 722 | for (int i = 0; i < count * 2; ++i) |
| 723 | { |
| 724 | if (v[i] == 0) |
| 725 | { |
| 726 | boolParams[i] = GL_FALSE; |
| 727 | } |
| 728 | else |
| 729 | { |
| 730 | boolParams[i] = GL_TRUE; |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | else |
| 735 | { |
| 736 | return false; |
| 737 | } |
| 738 | |
| 739 | return true; |
| 740 | } |
| 741 | |
| 742 | bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 743 | { |
| 744 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 745 | { |
| 746 | return false; |
| 747 | } |
| 748 | |
| 749 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 750 | targetUniform->dirty = true; |
| 751 | |
| 752 | if (targetUniform->type == GL_INT_VEC3) |
| 753 | { |
| 754 | int arraySize = targetUniform->arraySize; |
| 755 | |
| 756 | if (arraySize == 1 && count > 1) |
| 757 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 758 | |
| 759 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 760 | |
| 761 | memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3, |
| 762 | v, 3 * sizeof(GLint) * count); |
| 763 | } |
| 764 | else if (targetUniform->type == GL_BOOL_VEC3) |
| 765 | { |
| 766 | int arraySize = targetUniform->arraySize; |
| 767 | |
| 768 | if (arraySize == 1 && count > 1) |
| 769 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 770 | |
| 771 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 772 | GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3; |
| 773 | |
| 774 | for (int i = 0; i < count * 3; ++i) |
| 775 | { |
| 776 | if (v[i] == 0) |
| 777 | { |
| 778 | boolParams[i] = GL_FALSE; |
| 779 | } |
| 780 | else |
| 781 | { |
| 782 | boolParams[i] = GL_TRUE; |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | return false; |
| 789 | } |
| 790 | |
| 791 | return true; |
| 792 | } |
| 793 | |
| 794 | bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 795 | { |
| 796 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 797 | { |
| 798 | return false; |
| 799 | } |
| 800 | |
| 801 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 802 | targetUniform->dirty = true; |
| 803 | |
| 804 | if (targetUniform->type == GL_INT_VEC4) |
| 805 | { |
| 806 | int arraySize = targetUniform->arraySize; |
| 807 | |
| 808 | if (arraySize == 1 && count > 1) |
| 809 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 810 | |
| 811 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 812 | |
| 813 | memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4, |
| 814 | v, 4 * sizeof(GLint) * count); |
| 815 | } |
| 816 | else if (targetUniform->type == GL_BOOL_VEC4) |
| 817 | { |
| 818 | int arraySize = targetUniform->arraySize; |
| 819 | |
| 820 | if (arraySize == 1 && count > 1) |
| 821 | return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
| 822 | |
| 823 | count = std::min(arraySize - (int)mUniformIndex[location].element, count); |
| 824 | GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4; |
| 825 | |
| 826 | for (int i = 0; i < count * 4; ++i) |
| 827 | { |
| 828 | if (v[i] == 0) |
| 829 | { |
| 830 | boolParams[i] = GL_FALSE; |
| 831 | } |
| 832 | else |
| 833 | { |
| 834 | boolParams[i] = GL_TRUE; |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | else |
| 839 | { |
| 840 | return false; |
| 841 | } |
| 842 | |
| 843 | return true; |
| 844 | } |
| 845 | |
| 846 | bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params) |
| 847 | { |
| 848 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 849 | { |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 854 | |
| 855 | // sized queries -- ensure the provided buffer is large enough |
| 856 | if (bufSize) |
| 857 | { |
| 858 | int requiredBytes = UniformExternalSize(targetUniform->type); |
| 859 | if (*bufSize < requiredBytes) |
| 860 | { |
| 861 | return false; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | switch (targetUniform->type) |
| 866 | { |
| 867 | case GL_FLOAT_MAT2: |
| 868 | transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8); |
| 869 | break; |
| 870 | case GL_FLOAT_MAT3: |
| 871 | transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12); |
| 872 | break; |
| 873 | case GL_FLOAT_MAT4: |
| 874 | transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16); |
| 875 | break; |
| 876 | default: |
| 877 | { |
| 878 | unsigned int count = UniformExternalComponentCount(targetUniform->type); |
| 879 | unsigned int internalCount = UniformInternalComponentCount(targetUniform->type); |
| 880 | |
| 881 | switch (UniformComponentType(targetUniform->type)) |
| 882 | { |
| 883 | case GL_BOOL: |
| 884 | { |
| 885 | GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount; |
| 886 | |
| 887 | for (unsigned int i = 0; i < count; ++i) |
| 888 | { |
| 889 | params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f; |
| 890 | } |
| 891 | } |
| 892 | break; |
| 893 | case GL_FLOAT: |
| 894 | memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat), |
| 895 | count * sizeof(GLfloat)); |
| 896 | break; |
| 897 | case GL_INT: |
| 898 | { |
| 899 | GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount; |
| 900 | |
| 901 | for (unsigned int i = 0; i < count; ++i) |
| 902 | { |
| 903 | params[i] = (float)intParams[i]; |
| 904 | } |
| 905 | } |
| 906 | break; |
| 907 | default: UNREACHABLE(); |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | return true; |
| 913 | } |
| 914 | |
| 915 | bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params) |
| 916 | { |
| 917 | if (location < 0 || location >= (int)mUniformIndex.size()) |
| 918 | { |
| 919 | return false; |
| 920 | } |
| 921 | |
| 922 | Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 923 | |
| 924 | // sized queries -- ensure the provided buffer is large enough |
| 925 | if (bufSize) |
| 926 | { |
| 927 | int requiredBytes = UniformExternalSize(targetUniform->type); |
| 928 | if (*bufSize < requiredBytes) |
| 929 | { |
| 930 | return false; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | switch (targetUniform->type) |
| 935 | { |
| 936 | case GL_FLOAT_MAT2: |
| 937 | { |
| 938 | transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8); |
| 939 | } |
| 940 | break; |
| 941 | case GL_FLOAT_MAT3: |
| 942 | { |
| 943 | transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12); |
| 944 | } |
| 945 | break; |
| 946 | case GL_FLOAT_MAT4: |
| 947 | { |
| 948 | transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16); |
| 949 | } |
| 950 | break; |
| 951 | default: |
| 952 | { |
| 953 | unsigned int count = UniformExternalComponentCount(targetUniform->type); |
| 954 | unsigned int internalCount = UniformInternalComponentCount(targetUniform->type); |
| 955 | |
| 956 | switch (UniformComponentType(targetUniform->type)) |
| 957 | { |
| 958 | case GL_BOOL: |
| 959 | { |
| 960 | GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount; |
| 961 | |
| 962 | for (unsigned int i = 0; i < count; ++i) |
| 963 | { |
| 964 | params[i] = (GLint)boolParams[i]; |
| 965 | } |
| 966 | } |
| 967 | break; |
| 968 | case GL_FLOAT: |
| 969 | { |
| 970 | GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount; |
| 971 | |
| 972 | for (unsigned int i = 0; i < count; ++i) |
| 973 | { |
| 974 | params[i] = (GLint)floatParams[i]; |
| 975 | } |
| 976 | } |
| 977 | break; |
| 978 | case GL_INT: |
| 979 | memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint), |
| 980 | count * sizeof(GLint)); |
| 981 | break; |
| 982 | default: UNREACHABLE(); |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | return true; |
| 988 | } |
| 989 | |
| 990 | void ProgramBinary::dirtyAllUniforms() |
| 991 | { |
| 992 | unsigned int numUniforms = mUniforms.size(); |
| 993 | for (unsigned int index = 0; index < numUniforms; index++) |
| 994 | { |
| 995 | mUniforms[index]->dirty = true; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | // Applies all the uniforms set for this program object to the Direct3D 9 device |
| 1000 | void ProgramBinary::applyUniforms() |
| 1001 | { |
| 1002 | for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub) { |
| 1003 | Uniform *targetUniform = *ub; |
| 1004 | |
| 1005 | if (targetUniform->dirty) |
| 1006 | { |
| 1007 | int arraySize = targetUniform->arraySize; |
| 1008 | GLfloat *f = (GLfloat*)targetUniform->data; |
| 1009 | GLint *i = (GLint*)targetUniform->data; |
| 1010 | GLboolean *b = (GLboolean*)targetUniform->data; |
| 1011 | |
| 1012 | switch (targetUniform->type) |
| 1013 | { |
| 1014 | case GL_BOOL: applyUniformnbv(targetUniform, arraySize, 1, b); break; |
| 1015 | case GL_BOOL_VEC2: applyUniformnbv(targetUniform, arraySize, 2, b); break; |
| 1016 | case GL_BOOL_VEC3: applyUniformnbv(targetUniform, arraySize, 3, b); break; |
| 1017 | case GL_BOOL_VEC4: applyUniformnbv(targetUniform, arraySize, 4, b); break; |
| 1018 | case GL_FLOAT: |
| 1019 | case GL_FLOAT_VEC2: |
| 1020 | case GL_FLOAT_VEC3: |
| 1021 | case GL_FLOAT_VEC4: |
| 1022 | case GL_FLOAT_MAT2: |
| 1023 | case GL_FLOAT_MAT3: |
| 1024 | case GL_FLOAT_MAT4: applyUniformnfv(targetUniform, f); break; |
| 1025 | case GL_SAMPLER_2D: |
| 1026 | case GL_SAMPLER_CUBE: |
| 1027 | case GL_INT: applyUniform1iv(targetUniform, arraySize, i); break; |
| 1028 | case GL_INT_VEC2: applyUniform2iv(targetUniform, arraySize, i); break; |
| 1029 | case GL_INT_VEC3: applyUniform3iv(targetUniform, arraySize, i); break; |
| 1030 | case GL_INT_VEC4: applyUniform4iv(targetUniform, arraySize, i); break; |
| 1031 | default: |
| 1032 | UNREACHABLE(); |
| 1033 | } |
| 1034 | |
| 1035 | targetUniform->dirty = false; |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | // Compiles the HLSL code of the attached shaders into executable binaries |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 1041 | ID3D10Blob *ProgramBinary::compileToBinary(InfoLog &infoLog, const char *hlsl, const char *profile, D3DConstantTable **constantTable) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1042 | { |
| 1043 | if (!hlsl) |
| 1044 | { |
| 1045 | return NULL; |
| 1046 | } |
| 1047 | |
daniel@transgaming.com | e3e826d | 2012-11-28 19:42:35 +0000 | [diff] [blame] | 1048 | HRESULT result = S_OK; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1049 | UINT flags = 0; |
| 1050 | std::string sourceText; |
| 1051 | if (perfActive()) |
| 1052 | { |
| 1053 | flags |= D3DCOMPILE_DEBUG; |
| 1054 | #ifdef NDEBUG |
| 1055 | flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL; |
| 1056 | #else |
| 1057 | flags |= D3DCOMPILE_SKIP_OPTIMIZATION; |
| 1058 | #endif |
| 1059 | |
| 1060 | std::string sourcePath = getTempPath(); |
| 1061 | sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl); |
| 1062 | writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size()); |
| 1063 | } |
| 1064 | else |
| 1065 | { |
| 1066 | flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL; |
| 1067 | sourceText = hlsl; |
| 1068 | } |
| 1069 | |
apatrick@chromium.org | 637ca47 | 2012-10-04 18:12:56 +0000 | [diff] [blame] | 1070 | // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options. |
| 1071 | // Try the default flags first and if compilation fails, try some alternatives. |
| 1072 | const static UINT extraFlags[] = |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1073 | { |
apatrick@chromium.org | 637ca47 | 2012-10-04 18:12:56 +0000 | [diff] [blame] | 1074 | 0, |
| 1075 | D3DCOMPILE_AVOID_FLOW_CONTROL, |
| 1076 | D3DCOMPILE_PREFER_FLOW_CONTROL |
| 1077 | }; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1078 | |
apatrick@chromium.org | 637ca47 | 2012-10-04 18:12:56 +0000 | [diff] [blame] | 1079 | const static char * const extraFlagNames[] = |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1080 | { |
apatrick@chromium.org | 637ca47 | 2012-10-04 18:12:56 +0000 | [diff] [blame] | 1081 | "default", |
| 1082 | "avoid flow control", |
| 1083 | "prefer flow control" |
| 1084 | }; |
| 1085 | |
| 1086 | for (int i = 0; i < sizeof(extraFlags) / sizeof(UINT); ++i) |
| 1087 | { |
| 1088 | ID3D10Blob *errorMessage = NULL; |
| 1089 | ID3D10Blob *binary = NULL; |
daniel@transgaming.com | 7d738a2 | 2012-11-28 19:43:08 +0000 | [diff] [blame] | 1090 | result = mRenderer->compileShaderSource(hlsl, g_fakepath, profile, flags | extraFlags[i], &binary, &errorMessage); |
apatrick@chromium.org | 637ca47 | 2012-10-04 18:12:56 +0000 | [diff] [blame] | 1091 | |
| 1092 | if (errorMessage) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1093 | { |
apatrick@chromium.org | 637ca47 | 2012-10-04 18:12:56 +0000 | [diff] [blame] | 1094 | const char *message = (const char*)errorMessage->GetBufferPointer(); |
| 1095 | |
| 1096 | infoLog.appendSanitized(message); |
| 1097 | TRACE("\n%s", hlsl); |
| 1098 | TRACE("\n%s", message); |
| 1099 | |
| 1100 | errorMessage->Release(); |
| 1101 | errorMessage = NULL; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
apatrick@chromium.org | 637ca47 | 2012-10-04 18:12:56 +0000 | [diff] [blame] | 1104 | if (SUCCEEDED(result)) |
| 1105 | { |
| 1106 | D3DConstantTable *table = new D3DConstantTable(binary->GetBufferPointer(), binary->GetBufferSize()); |
| 1107 | if (table->error()) |
| 1108 | { |
| 1109 | delete table; |
| 1110 | binary->Release(); |
| 1111 | return NULL; |
| 1112 | } |
| 1113 | |
| 1114 | *constantTable = table; |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 1115 | |
apatrick@chromium.org | 637ca47 | 2012-10-04 18:12:56 +0000 | [diff] [blame] | 1116 | return binary; |
| 1117 | } |
| 1118 | else |
| 1119 | { |
| 1120 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 1121 | { |
| 1122 | return error(GL_OUT_OF_MEMORY, (ID3D10Blob*) NULL); |
| 1123 | } |
| 1124 | |
| 1125 | infoLog.append("Warning: D3D shader compilation failed with "); |
| 1126 | infoLog.append(extraFlagNames[i]); |
| 1127 | infoLog.append(" flags."); |
| 1128 | if (i + 1 < sizeof(extraFlagNames) / sizeof(char*)) |
| 1129 | { |
| 1130 | infoLog.append(" Retrying with "); |
| 1131 | infoLog.append(extraFlagNames[i + 1]); |
| 1132 | infoLog.append(".\n"); |
| 1133 | } |
| 1134 | } |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
apatrick@chromium.org | 637ca47 | 2012-10-04 18:12:56 +0000 | [diff] [blame] | 1137 | return NULL; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | // Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111 |
| 1141 | // Returns the number of used varying registers, or -1 if unsuccesful |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1142 | int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1143 | { |
| 1144 | Context *context = getContext(); |
| 1145 | const int maxVaryingVectors = context->getMaximumVaryingVectors(); |
| 1146 | |
| 1147 | for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++) |
| 1148 | { |
| 1149 | int n = VariableRowCount(varying->type) * varying->size; |
| 1150 | int m = VariableColumnCount(varying->type); |
| 1151 | bool success = false; |
| 1152 | |
| 1153 | if (m == 2 || m == 3 || m == 4) |
| 1154 | { |
| 1155 | for (int r = 0; r <= maxVaryingVectors - n && !success; r++) |
| 1156 | { |
| 1157 | bool available = true; |
| 1158 | |
| 1159 | for (int y = 0; y < n && available; y++) |
| 1160 | { |
| 1161 | for (int x = 0; x < m && available; x++) |
| 1162 | { |
| 1163 | if (packing[r + y][x]) |
| 1164 | { |
| 1165 | available = false; |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | if (available) |
| 1171 | { |
| 1172 | varying->reg = r; |
| 1173 | varying->col = 0; |
| 1174 | |
| 1175 | for (int y = 0; y < n; y++) |
| 1176 | { |
| 1177 | for (int x = 0; x < m; x++) |
| 1178 | { |
| 1179 | packing[r + y][x] = &*varying; |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | success = true; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | if (!success && m == 2) |
| 1188 | { |
| 1189 | for (int r = maxVaryingVectors - n; r >= 0 && !success; r--) |
| 1190 | { |
| 1191 | bool available = true; |
| 1192 | |
| 1193 | for (int y = 0; y < n && available; y++) |
| 1194 | { |
| 1195 | for (int x = 2; x < 4 && available; x++) |
| 1196 | { |
| 1197 | if (packing[r + y][x]) |
| 1198 | { |
| 1199 | available = false; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | if (available) |
| 1205 | { |
| 1206 | varying->reg = r; |
| 1207 | varying->col = 2; |
| 1208 | |
| 1209 | for (int y = 0; y < n; y++) |
| 1210 | { |
| 1211 | for (int x = 2; x < 4; x++) |
| 1212 | { |
| 1213 | packing[r + y][x] = &*varying; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | success = true; |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | else if (m == 1) |
| 1223 | { |
| 1224 | int space[4] = {0}; |
| 1225 | |
| 1226 | for (int y = 0; y < maxVaryingVectors; y++) |
| 1227 | { |
| 1228 | for (int x = 0; x < 4; x++) |
| 1229 | { |
| 1230 | space[x] += packing[y][x] ? 0 : 1; |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | int column = 0; |
| 1235 | |
| 1236 | for (int x = 0; x < 4; x++) |
| 1237 | { |
| 1238 | if (space[x] >= n && space[x] < space[column]) |
| 1239 | { |
| 1240 | column = x; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | if (space[column] >= n) |
| 1245 | { |
| 1246 | for (int r = 0; r < maxVaryingVectors; r++) |
| 1247 | { |
| 1248 | if (!packing[r][column]) |
| 1249 | { |
| 1250 | varying->reg = r; |
| 1251 | |
| 1252 | for (int y = r; y < r + n; y++) |
| 1253 | { |
| 1254 | packing[y][column] = &*varying; |
| 1255 | } |
| 1256 | |
| 1257 | break; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | varying->col = column; |
| 1262 | |
| 1263 | success = true; |
| 1264 | } |
| 1265 | } |
| 1266 | else UNREACHABLE(); |
| 1267 | |
| 1268 | if (!success) |
| 1269 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1270 | infoLog.append("Could not pack varying %s", varying->name.c_str()); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1271 | |
| 1272 | return -1; |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | // Return the number of used registers |
| 1277 | int registers = 0; |
| 1278 | |
| 1279 | for (int r = 0; r < maxVaryingVectors; r++) |
| 1280 | { |
| 1281 | if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3]) |
| 1282 | { |
| 1283 | registers++; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | return registers; |
| 1288 | } |
| 1289 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1290 | bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1291 | { |
| 1292 | if (pixelHLSL.empty() || vertexHLSL.empty()) |
| 1293 | { |
| 1294 | return false; |
| 1295 | } |
| 1296 | |
| 1297 | // Reset the varying register assignments |
| 1298 | for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++) |
| 1299 | { |
| 1300 | fragVar->reg = -1; |
| 1301 | fragVar->col = -1; |
| 1302 | } |
| 1303 | |
| 1304 | for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++) |
| 1305 | { |
| 1306 | vtxVar->reg = -1; |
| 1307 | vtxVar->col = -1; |
| 1308 | } |
| 1309 | |
| 1310 | // Map the varyings to the register file |
| 1311 | const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL}; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1312 | int registers = packVaryings(infoLog, packing, fragmentShader); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1313 | |
| 1314 | if (registers < 0) |
| 1315 | { |
| 1316 | return false; |
| 1317 | } |
| 1318 | |
| 1319 | // Write the HLSL input/output declarations |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 1320 | const bool sm3 = mRenderer->getMajorShaderModel() >= 3; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1321 | Context *context = getContext(); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1322 | const int maxVaryingVectors = context->getMaximumVaryingVectors(); |
| 1323 | |
| 1324 | if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord) |
| 1325 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1326 | infoLog.append("No varying registers left to support gl_FragCoord"); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1327 | |
| 1328 | return false; |
| 1329 | } |
| 1330 | |
| 1331 | for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++) |
| 1332 | { |
| 1333 | bool matched = false; |
| 1334 | |
| 1335 | for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++) |
| 1336 | { |
| 1337 | if (output->name == input->name) |
| 1338 | { |
| 1339 | if (output->type != input->type || output->size != input->size) |
| 1340 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1341 | infoLog.append("Type of vertex varying %s does not match that of the fragment varying", output->name.c_str()); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1342 | |
| 1343 | return false; |
| 1344 | } |
| 1345 | |
| 1346 | output->reg = input->reg; |
| 1347 | output->col = input->col; |
| 1348 | |
| 1349 | matched = true; |
| 1350 | break; |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | if (!matched) |
| 1355 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1356 | infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str()); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1357 | |
| 1358 | return false; |
| 1359 | } |
| 1360 | } |
| 1361 | |
daniel@transgaming.com | 087e578 | 2012-09-17 21:28:47 +0000 | [diff] [blame] | 1362 | mUsesPointSize = vertexShader->mUsesPointSize; |
| 1363 | std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD"; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1364 | |
| 1365 | vertexHLSL += "struct VS_INPUT\n" |
| 1366 | "{\n"; |
| 1367 | |
| 1368 | int semanticIndex = 0; |
| 1369 | for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++) |
| 1370 | { |
| 1371 | switch (attribute->type) |
| 1372 | { |
| 1373 | case GL_FLOAT: vertexHLSL += " float "; break; |
| 1374 | case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break; |
| 1375 | case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break; |
| 1376 | case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break; |
| 1377 | case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break; |
| 1378 | case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break; |
| 1379 | case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break; |
| 1380 | default: UNREACHABLE(); |
| 1381 | } |
| 1382 | |
| 1383 | vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n"; |
| 1384 | |
| 1385 | semanticIndex += VariableRowCount(attribute->type); |
| 1386 | } |
| 1387 | |
| 1388 | vertexHLSL += "};\n" |
| 1389 | "\n" |
| 1390 | "struct VS_OUTPUT\n" |
| 1391 | "{\n" |
| 1392 | " float4 gl_Position : POSITION;\n"; |
| 1393 | |
| 1394 | for (int r = 0; r < registers; r++) |
| 1395 | { |
| 1396 | int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1)); |
| 1397 | |
| 1398 | vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n"; |
| 1399 | } |
| 1400 | |
| 1401 | if (fragmentShader->mUsesFragCoord) |
| 1402 | { |
| 1403 | vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n"; |
| 1404 | } |
| 1405 | |
| 1406 | if (vertexShader->mUsesPointSize && sm3) |
| 1407 | { |
| 1408 | vertexHLSL += " float gl_PointSize : PSIZE;\n"; |
| 1409 | } |
| 1410 | |
| 1411 | vertexHLSL += "};\n" |
| 1412 | "\n" |
| 1413 | "VS_OUTPUT main(VS_INPUT input)\n" |
| 1414 | "{\n"; |
| 1415 | |
| 1416 | for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++) |
| 1417 | { |
| 1418 | vertexHLSL += " " + decorateAttribute(attribute->name) + " = "; |
| 1419 | |
| 1420 | if (VariableRowCount(attribute->type) > 1) // Matrix |
| 1421 | { |
| 1422 | vertexHLSL += "transpose"; |
| 1423 | } |
| 1424 | |
| 1425 | vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n"; |
| 1426 | } |
| 1427 | |
| 1428 | vertexHLSL += "\n" |
| 1429 | " gl_main();\n" |
| 1430 | "\n" |
| 1431 | " VS_OUTPUT output;\n" |
| 1432 | " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n" |
apatrick@chromium.org | 9616e58 | 2012-06-22 18:27:01 +0000 | [diff] [blame] | 1433 | " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n" |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1434 | " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n" |
| 1435 | " output.gl_Position.w = gl_Position.w;\n"; |
| 1436 | |
| 1437 | if (vertexShader->mUsesPointSize && sm3) |
| 1438 | { |
daniel@transgaming.com | 13be3e4 | 2012-07-04 19:16:24 +0000 | [diff] [blame] | 1439 | vertexHLSL += " output.gl_PointSize = gl_PointSize;\n"; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | if (fragmentShader->mUsesFragCoord) |
| 1443 | { |
| 1444 | vertexHLSL += " output.gl_FragCoord = gl_Position;\n"; |
| 1445 | } |
| 1446 | |
| 1447 | for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++) |
| 1448 | { |
| 1449 | if (varying->reg >= 0) |
| 1450 | { |
| 1451 | for (int i = 0; i < varying->size; i++) |
| 1452 | { |
| 1453 | int rows = VariableRowCount(varying->type); |
| 1454 | |
| 1455 | for (int j = 0; j < rows; j++) |
| 1456 | { |
| 1457 | int r = varying->reg + i * rows + j; |
| 1458 | vertexHLSL += " output.v" + str(r); |
| 1459 | |
| 1460 | bool sharedRegister = false; // Register used by multiple varyings |
| 1461 | |
| 1462 | for (int x = 0; x < 4; x++) |
| 1463 | { |
| 1464 | if (packing[r][x] && packing[r][x] != packing[r][0]) |
| 1465 | { |
| 1466 | sharedRegister = true; |
| 1467 | break; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | if(sharedRegister) |
| 1472 | { |
| 1473 | vertexHLSL += "."; |
| 1474 | |
| 1475 | for (int x = 0; x < 4; x++) |
| 1476 | { |
| 1477 | if (packing[r][x] == &*varying) |
| 1478 | { |
| 1479 | switch(x) |
| 1480 | { |
| 1481 | case 0: vertexHLSL += "x"; break; |
| 1482 | case 1: vertexHLSL += "y"; break; |
| 1483 | case 2: vertexHLSL += "z"; break; |
| 1484 | case 3: vertexHLSL += "w"; break; |
| 1485 | } |
| 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | vertexHLSL += " = " + varying->name; |
| 1491 | |
| 1492 | if (varying->array) |
| 1493 | { |
| 1494 | vertexHLSL += "[" + str(i) + "]"; |
| 1495 | } |
| 1496 | |
| 1497 | if (rows > 1) |
| 1498 | { |
| 1499 | vertexHLSL += "[" + str(j) + "]"; |
| 1500 | } |
| 1501 | |
| 1502 | vertexHLSL += ";\n"; |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | vertexHLSL += "\n" |
| 1509 | " return output;\n" |
| 1510 | "}\n"; |
| 1511 | |
| 1512 | pixelHLSL += "struct PS_INPUT\n" |
| 1513 | "{\n"; |
| 1514 | |
| 1515 | for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++) |
| 1516 | { |
| 1517 | if (varying->reg >= 0) |
| 1518 | { |
| 1519 | for (int i = 0; i < varying->size; i++) |
| 1520 | { |
| 1521 | int rows = VariableRowCount(varying->type); |
| 1522 | for (int j = 0; j < rows; j++) |
| 1523 | { |
| 1524 | std::string n = str(varying->reg + i * rows + j); |
| 1525 | pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n"; |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | else UNREACHABLE(); |
| 1530 | } |
| 1531 | |
| 1532 | if (fragmentShader->mUsesFragCoord) |
| 1533 | { |
| 1534 | pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n"; |
| 1535 | if (sm3) { |
| 1536 | pixelHLSL += " float2 dx_VPos : VPOS;\n"; |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | if (fragmentShader->mUsesPointCoord && sm3) |
| 1541 | { |
| 1542 | pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n"; |
| 1543 | } |
| 1544 | |
| 1545 | if (fragmentShader->mUsesFrontFacing) |
| 1546 | { |
| 1547 | pixelHLSL += " float vFace : VFACE;\n"; |
| 1548 | } |
| 1549 | |
| 1550 | pixelHLSL += "};\n" |
| 1551 | "\n" |
| 1552 | "struct PS_OUTPUT\n" |
| 1553 | "{\n" |
| 1554 | " float4 gl_Color[1] : COLOR;\n" |
| 1555 | "};\n" |
| 1556 | "\n" |
| 1557 | "PS_OUTPUT main(PS_INPUT input)\n" |
| 1558 | "{\n"; |
| 1559 | |
| 1560 | if (fragmentShader->mUsesFragCoord) |
| 1561 | { |
| 1562 | pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n"; |
| 1563 | |
| 1564 | if (sm3) |
| 1565 | { |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1566 | pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n" |
apatrick@chromium.org | 9616e58 | 2012-06-22 18:27:01 +0000 | [diff] [blame] | 1567 | " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n"; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1568 | } |
| 1569 | else |
| 1570 | { |
| 1571 | // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget() |
| 1572 | pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n" |
apatrick@chromium.org | 9616e58 | 2012-06-22 18:27:01 +0000 | [diff] [blame] | 1573 | " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n"; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_Depth.x + dx_Depth.y;\n" |
| 1577 | " gl_FragCoord.w = rhw;\n"; |
| 1578 | } |
| 1579 | |
| 1580 | if (fragmentShader->mUsesPointCoord && sm3) |
| 1581 | { |
apatrick@chromium.org | 9616e58 | 2012-06-22 18:27:01 +0000 | [diff] [blame] | 1582 | pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n"; |
| 1583 | pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n"; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | if (fragmentShader->mUsesFrontFacing) |
| 1587 | { |
| 1588 | pixelHLSL += " gl_FrontFacing = dx_PointsOrLines || (dx_FrontCCW ? (input.vFace >= 0.0) : (input.vFace <= 0.0));\n"; |
| 1589 | } |
| 1590 | |
| 1591 | for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++) |
| 1592 | { |
| 1593 | if (varying->reg >= 0) |
| 1594 | { |
| 1595 | for (int i = 0; i < varying->size; i++) |
| 1596 | { |
| 1597 | int rows = VariableRowCount(varying->type); |
| 1598 | for (int j = 0; j < rows; j++) |
| 1599 | { |
| 1600 | std::string n = str(varying->reg + i * rows + j); |
| 1601 | pixelHLSL += " " + varying->name; |
| 1602 | |
| 1603 | if (varying->array) |
| 1604 | { |
| 1605 | pixelHLSL += "[" + str(i) + "]"; |
| 1606 | } |
| 1607 | |
| 1608 | if (rows > 1) |
| 1609 | { |
| 1610 | pixelHLSL += "[" + str(j) + "]"; |
| 1611 | } |
| 1612 | |
| 1613 | pixelHLSL += " = input.v" + n + ";\n"; |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | else UNREACHABLE(); |
| 1618 | } |
| 1619 | |
| 1620 | pixelHLSL += "\n" |
| 1621 | " gl_main();\n" |
| 1622 | "\n" |
| 1623 | " PS_OUTPUT output;\n" |
| 1624 | " output.gl_Color[0] = gl_Color[0];\n" |
| 1625 | "\n" |
| 1626 | " return output;\n" |
| 1627 | "}\n"; |
| 1628 | |
| 1629 | return true; |
| 1630 | } |
| 1631 | |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1632 | bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length) |
| 1633 | { |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1634 | BinaryInputStream stream(binary, length); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1635 | |
| 1636 | int format = 0; |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1637 | stream.read(&format); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1638 | if (format != GL_PROGRAM_BINARY_ANGLE) |
| 1639 | { |
| 1640 | infoLog.append("Invalid program binary format."); |
| 1641 | return false; |
| 1642 | } |
| 1643 | |
| 1644 | int version = 0; |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1645 | stream.read(&version); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1646 | if (version != BUILD_REVISION) |
| 1647 | { |
| 1648 | infoLog.append("Invalid program binary version."); |
| 1649 | return false; |
| 1650 | } |
| 1651 | |
| 1652 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i) |
| 1653 | { |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1654 | stream.read(&mLinkedAttribute[i].type); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1655 | std::string name; |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1656 | stream.read(&name); |
| 1657 | mLinkedAttribute[i].name = name; |
| 1658 | stream.read(&mSemanticIndex[i]); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i) |
| 1662 | { |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1663 | stream.read(&mSamplersPS[i].active); |
| 1664 | stream.read(&mSamplersPS[i].logicalTextureUnit); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1665 | |
| 1666 | int textureType; |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1667 | stream.read(&textureType); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1668 | mSamplersPS[i].textureType = (TextureType) textureType; |
| 1669 | } |
| 1670 | |
| 1671 | for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i) |
| 1672 | { |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1673 | stream.read(&mSamplersVS[i].active); |
| 1674 | stream.read(&mSamplersVS[i].logicalTextureUnit); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1675 | |
| 1676 | int textureType; |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1677 | stream.read(&textureType); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1678 | mSamplersVS[i].textureType = (TextureType) textureType; |
| 1679 | } |
| 1680 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1681 | stream.read(&mUsedVertexSamplerRange); |
| 1682 | stream.read(&mUsedPixelSamplerRange); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1683 | |
| 1684 | unsigned int size; |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1685 | stream.read(&size); |
| 1686 | if (stream.error()) |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1687 | { |
| 1688 | infoLog.append("Invalid program binary."); |
| 1689 | return false; |
| 1690 | } |
| 1691 | |
| 1692 | mUniforms.resize(size); |
| 1693 | for (unsigned int i = 0; i < size; ++i) |
| 1694 | { |
| 1695 | GLenum type; |
| 1696 | std::string _name; |
| 1697 | unsigned int arraySize; |
| 1698 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1699 | stream.read(&type); |
| 1700 | stream.read(&_name); |
| 1701 | stream.read(&arraySize); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1702 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1703 | mUniforms[i] = new Uniform(type, _name, arraySize); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1704 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1705 | stream.read(&mUniforms[i]->ps.float4Index); |
| 1706 | stream.read(&mUniforms[i]->ps.samplerIndex); |
| 1707 | stream.read(&mUniforms[i]->ps.boolIndex); |
| 1708 | stream.read(&mUniforms[i]->ps.registerCount); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1709 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1710 | stream.read(&mUniforms[i]->vs.float4Index); |
| 1711 | stream.read(&mUniforms[i]->vs.samplerIndex); |
| 1712 | stream.read(&mUniforms[i]->vs.boolIndex); |
| 1713 | stream.read(&mUniforms[i]->vs.registerCount); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1716 | stream.read(&size); |
| 1717 | if (stream.error()) |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1718 | { |
| 1719 | infoLog.append("Invalid program binary."); |
| 1720 | return false; |
| 1721 | } |
| 1722 | |
| 1723 | mUniformIndex.resize(size); |
| 1724 | for (unsigned int i = 0; i < size; ++i) |
| 1725 | { |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1726 | stream.read(&mUniformIndex[i].name); |
| 1727 | stream.read(&mUniformIndex[i].element); |
| 1728 | stream.read(&mUniformIndex[i].index); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1731 | stream.read(&mDxDepthRangeLocation); |
| 1732 | stream.read(&mDxDepthLocation); |
| 1733 | stream.read(&mDxCoordLocation); |
| 1734 | stream.read(&mDxHalfPixelSizeLocation); |
| 1735 | stream.read(&mDxFrontCCWLocation); |
| 1736 | stream.read(&mDxPointsOrLinesLocation); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1737 | |
| 1738 | unsigned int pixelShaderSize; |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1739 | stream.read(&pixelShaderSize); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1740 | |
| 1741 | unsigned int vertexShaderSize; |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1742 | stream.read(&vertexShaderSize); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1743 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1744 | const char *ptr = (const char*) binary + stream.offset(); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1745 | |
| 1746 | const D3DCAPS9 *binaryIdentifier = (const D3DCAPS9*) ptr; |
| 1747 | ptr += sizeof(GUID); |
| 1748 | |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1749 | GUID identifier = mRenderer->getAdapterIdentifier(); |
| 1750 | if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0) |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1751 | { |
| 1752 | infoLog.append("Invalid program binary."); |
| 1753 | return false; |
| 1754 | } |
| 1755 | |
| 1756 | const char *pixelShaderFunction = ptr; |
| 1757 | ptr += pixelShaderSize; |
| 1758 | |
| 1759 | const char *vertexShaderFunction = ptr; |
| 1760 | ptr += vertexShaderSize; |
| 1761 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 1762 | mPixelExecutable = mRenderer->createPixelShader(reinterpret_cast<const DWORD*>(pixelShaderFunction), pixelShaderSize); |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 1763 | if (!mPixelExecutable) |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1764 | { |
| 1765 | infoLog.append("Could not create pixel shader."); |
| 1766 | return false; |
| 1767 | } |
| 1768 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 1769 | mVertexExecutable = mRenderer->createVertexShader(reinterpret_cast<const DWORD*>(vertexShaderFunction), vertexShaderSize); |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 1770 | if (!mVertexExecutable) |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1771 | { |
| 1772 | infoLog.append("Could not create vertex shader."); |
| 1773 | mPixelExecutable->Release(); |
| 1774 | mPixelExecutable = NULL; |
| 1775 | return false; |
| 1776 | } |
| 1777 | |
| 1778 | return true; |
| 1779 | } |
| 1780 | |
| 1781 | bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length) |
| 1782 | { |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1783 | BinaryOutputStream stream; |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1784 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1785 | stream.write(GL_PROGRAM_BINARY_ANGLE); |
| 1786 | stream.write(BUILD_REVISION); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1787 | |
| 1788 | for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i) |
| 1789 | { |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1790 | stream.write(mLinkedAttribute[i].type); |
| 1791 | stream.write(mLinkedAttribute[i].name); |
| 1792 | stream.write(mSemanticIndex[i]); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i) |
| 1796 | { |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1797 | stream.write(mSamplersPS[i].active); |
| 1798 | stream.write(mSamplersPS[i].logicalTextureUnit); |
| 1799 | stream.write((int) mSamplersPS[i].textureType); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i) |
| 1803 | { |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1804 | stream.write(mSamplersVS[i].active); |
| 1805 | stream.write(mSamplersVS[i].logicalTextureUnit); |
| 1806 | stream.write((int) mSamplersVS[i].textureType); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1809 | stream.write(mUsedVertexSamplerRange); |
| 1810 | stream.write(mUsedPixelSamplerRange); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1811 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1812 | stream.write(mUniforms.size()); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1813 | for (unsigned int i = 0; i < mUniforms.size(); ++i) |
| 1814 | { |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1815 | stream.write(mUniforms[i]->type); |
| 1816 | stream.write(mUniforms[i]->_name); |
| 1817 | stream.write(mUniforms[i]->arraySize); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1818 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1819 | stream.write(mUniforms[i]->ps.float4Index); |
| 1820 | stream.write(mUniforms[i]->ps.samplerIndex); |
| 1821 | stream.write(mUniforms[i]->ps.boolIndex); |
| 1822 | stream.write(mUniforms[i]->ps.registerCount); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1823 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1824 | stream.write(mUniforms[i]->vs.float4Index); |
| 1825 | stream.write(mUniforms[i]->vs.samplerIndex); |
| 1826 | stream.write(mUniforms[i]->vs.boolIndex); |
| 1827 | stream.write(mUniforms[i]->vs.registerCount); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1830 | stream.write(mUniformIndex.size()); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1831 | for (unsigned int i = 0; i < mUniformIndex.size(); ++i) |
| 1832 | { |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1833 | stream.write(mUniformIndex[i].name); |
| 1834 | stream.write(mUniformIndex[i].element); |
| 1835 | stream.write(mUniformIndex[i].index); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1838 | stream.write(mDxDepthRangeLocation); |
| 1839 | stream.write(mDxDepthLocation); |
| 1840 | stream.write(mDxCoordLocation); |
| 1841 | stream.write(mDxHalfPixelSizeLocation); |
| 1842 | stream.write(mDxFrontCCWLocation); |
| 1843 | stream.write(mDxPointsOrLinesLocation); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1844 | |
| 1845 | UINT pixelShaderSize; |
| 1846 | HRESULT result = mPixelExecutable->GetFunction(NULL, &pixelShaderSize); |
| 1847 | ASSERT(SUCCEEDED(result)); |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1848 | stream.write(pixelShaderSize); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1849 | |
| 1850 | UINT vertexShaderSize; |
| 1851 | result = mVertexExecutable->GetFunction(NULL, &vertexShaderSize); |
| 1852 | ASSERT(SUCCEEDED(result)); |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1853 | stream.write(vertexShaderSize); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1854 | |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1855 | GUID identifier = mRenderer->getAdapterIdentifier(); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1856 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1857 | GLsizei streamLength = stream.length(); |
| 1858 | const void *streamData = stream.data(); |
| 1859 | |
| 1860 | GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize; |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1861 | if (totalLength > bufSize) |
| 1862 | { |
| 1863 | if (length) |
| 1864 | { |
| 1865 | *length = 0; |
| 1866 | } |
| 1867 | |
| 1868 | return false; |
| 1869 | } |
| 1870 | |
| 1871 | if (binary) |
| 1872 | { |
| 1873 | char *ptr = (char*) binary; |
| 1874 | |
apatrick@chromium.org | 6f1796f | 2012-07-12 01:40:11 +0000 | [diff] [blame] | 1875 | memcpy(ptr, streamData, streamLength); |
| 1876 | ptr += streamLength; |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1877 | |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1878 | memcpy(ptr, &identifier, sizeof(GUID)); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1879 | ptr += sizeof(GUID); |
| 1880 | |
| 1881 | result = mPixelExecutable->GetFunction(ptr, &pixelShaderSize); |
| 1882 | ASSERT(SUCCEEDED(result)); |
| 1883 | ptr += pixelShaderSize; |
| 1884 | |
| 1885 | result = mVertexExecutable->GetFunction(ptr, &vertexShaderSize); |
| 1886 | ASSERT(SUCCEEDED(result)); |
| 1887 | ptr += vertexShaderSize; |
| 1888 | |
| 1889 | ASSERT(ptr - totalLength == binary); |
| 1890 | } |
| 1891 | |
| 1892 | if (length) |
| 1893 | { |
| 1894 | *length = totalLength; |
| 1895 | } |
| 1896 | |
| 1897 | return true; |
| 1898 | } |
| 1899 | |
| 1900 | GLint ProgramBinary::getLength() |
| 1901 | { |
| 1902 | GLint length; |
| 1903 | if (save(NULL, INT_MAX, &length)) |
| 1904 | { |
| 1905 | return length; |
| 1906 | } |
| 1907 | else |
| 1908 | { |
| 1909 | return 0; |
| 1910 | } |
| 1911 | } |
| 1912 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1913 | bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1914 | { |
| 1915 | if (!fragmentShader || !fragmentShader->isCompiled()) |
| 1916 | { |
| 1917 | return false; |
| 1918 | } |
| 1919 | |
| 1920 | if (!vertexShader || !vertexShader->isCompiled()) |
| 1921 | { |
| 1922 | return false; |
| 1923 | } |
| 1924 | |
| 1925 | std::string pixelHLSL = fragmentShader->getHLSL(); |
| 1926 | std::string vertexHLSL = vertexShader->getHLSL(); |
| 1927 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1928 | if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader)) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1929 | { |
| 1930 | return false; |
| 1931 | } |
| 1932 | |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 1933 | const char *vertexProfile = mRenderer->getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0"; |
| 1934 | const char *pixelProfile = mRenderer->getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0"; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1935 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1936 | ID3D10Blob *vertexBinary = compileToBinary(infoLog, vertexHLSL.c_str(), vertexProfile, &mConstantTableVS); |
| 1937 | ID3D10Blob *pixelBinary = compileToBinary(infoLog, pixelHLSL.c_str(), pixelProfile, &mConstantTablePS); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1938 | |
| 1939 | if (vertexBinary && pixelBinary) |
| 1940 | { |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 1941 | mVertexExecutable = mRenderer->createVertexShader((DWORD*)vertexBinary->GetBufferPointer(), vertexBinary->GetBufferSize()); |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 1942 | if (!mVertexExecutable) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1943 | { |
| 1944 | return error(GL_OUT_OF_MEMORY, false); |
| 1945 | } |
| 1946 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 1947 | mPixelExecutable = mRenderer->createPixelShader((DWORD*)pixelBinary->GetBufferPointer(), pixelBinary->GetBufferSize()); |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 1948 | if (!mPixelExecutable) |
| 1949 | { |
| 1950 | mVertexExecutable->Release(); |
| 1951 | mVertexExecutable = NULL; |
| 1952 | return error(GL_OUT_OF_MEMORY, false); |
| 1953 | } |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1954 | |
| 1955 | vertexBinary->Release(); |
| 1956 | pixelBinary->Release(); |
| 1957 | vertexBinary = NULL; |
| 1958 | pixelBinary = NULL; |
| 1959 | |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 1960 | if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader)) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1961 | { |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 1962 | return false; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1963 | } |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 1964 | |
| 1965 | if (!linkUniforms(infoLog, GL_FRAGMENT_SHADER, mConstantTablePS)) |
| 1966 | { |
| 1967 | return false; |
| 1968 | } |
| 1969 | |
| 1970 | if (!linkUniforms(infoLog, GL_VERTEX_SHADER, mConstantTableVS)) |
| 1971 | { |
| 1972 | return false; |
| 1973 | } |
| 1974 | |
| 1975 | // these uniforms are searched as already-decorated because gl_ and dx_ |
| 1976 | // are reserved prefixes, and do not receive additional decoration |
| 1977 | mDxDepthRangeLocation = getUniformLocation("dx_DepthRange"); |
| 1978 | mDxDepthLocation = getUniformLocation("dx_Depth"); |
| 1979 | mDxCoordLocation = getUniformLocation("dx_Coord"); |
| 1980 | mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize"); |
| 1981 | mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW"); |
| 1982 | mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines"); |
| 1983 | |
daniel@transgaming.com | ea32d48 | 2012-11-28 19:33:18 +0000 | [diff] [blame] | 1984 | Context *context = getContext(); |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 1985 | context->markDxUniformsDirty(); |
| 1986 | |
| 1987 | return true; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | return false; |
| 1991 | } |
| 1992 | |
| 1993 | // Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1994 | bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1995 | { |
| 1996 | unsigned int usedLocations = 0; |
| 1997 | |
| 1998 | // Link attributes that have a binding location |
| 1999 | for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++) |
| 2000 | { |
| 2001 | int location = attributeBindings.getAttributeBinding(attribute->name); |
| 2002 | |
| 2003 | if (location != -1) // Set by glBindAttribLocation |
| 2004 | { |
| 2005 | if (!mLinkedAttribute[location].name.empty()) |
| 2006 | { |
| 2007 | // Multiple active attributes bound to the same location; not an error |
| 2008 | } |
| 2009 | |
| 2010 | mLinkedAttribute[location] = *attribute; |
| 2011 | |
| 2012 | int rows = VariableRowCount(attribute->type); |
| 2013 | |
| 2014 | if (rows + location > MAX_VERTEX_ATTRIBS) |
| 2015 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2016 | infoLog.append("Active attribute (%s) at location %d is too big to fit", attribute->name.c_str(), location); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2017 | |
| 2018 | return false; |
| 2019 | } |
| 2020 | |
| 2021 | for (int i = 0; i < rows; i++) |
| 2022 | { |
| 2023 | usedLocations |= 1 << (location + i); |
| 2024 | } |
| 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | // Link attributes that don't have a binding location |
| 2029 | for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++) |
| 2030 | { |
| 2031 | int location = attributeBindings.getAttributeBinding(attribute->name); |
| 2032 | |
| 2033 | if (location == -1) // Not set by glBindAttribLocation |
| 2034 | { |
| 2035 | int rows = VariableRowCount(attribute->type); |
| 2036 | int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS); |
| 2037 | |
| 2038 | if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS) |
| 2039 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2040 | infoLog.append("Too many active attributes (%s)", attribute->name.c_str()); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2041 | |
| 2042 | return false; // Fail to link |
| 2043 | } |
| 2044 | |
| 2045 | mLinkedAttribute[availableIndex] = *attribute; |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; ) |
| 2050 | { |
| 2051 | int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name); |
| 2052 | int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1); |
| 2053 | |
| 2054 | for (int r = 0; r < rows; r++) |
| 2055 | { |
| 2056 | mSemanticIndex[attributeIndex++] = index++; |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | return true; |
| 2061 | } |
| 2062 | |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2063 | bool ProgramBinary::linkUniforms(InfoLog &infoLog, GLenum shader, D3DConstantTable *constantTable) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2064 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2065 | for (unsigned int constantIndex = 0; constantIndex < constantTable->constants(); constantIndex++) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2066 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2067 | const D3DConstant *constant = constantTable->getConstant(constantIndex); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2068 | |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2069 | if (!defineUniform(infoLog, shader, constant)) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2070 | { |
| 2071 | return false; |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | return true; |
| 2076 | } |
| 2077 | |
| 2078 | // Adds the description of a constant found in the binary shader to the list of uniforms |
| 2079 | // Returns true if succesful (uniform not already defined) |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2080 | bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const D3DConstant *constant, std::string name) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2081 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2082 | if (constant->registerSet == D3DConstant::RS_SAMPLER) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2083 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2084 | for (unsigned int i = 0; i < constant->registerCount; i++) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2085 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2086 | const D3DConstant *psConstant = mConstantTablePS->getConstantByName(constant->name.c_str()); |
| 2087 | const D3DConstant *vsConstant = mConstantTableVS->getConstantByName(constant->name.c_str()); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2088 | |
| 2089 | if (psConstant) |
| 2090 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2091 | unsigned int samplerIndex = psConstant->registerIndex + i; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2092 | |
| 2093 | if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS) |
| 2094 | { |
| 2095 | mSamplersPS[samplerIndex].active = true; |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2096 | mSamplersPS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2097 | mSamplersPS[samplerIndex].logicalTextureUnit = 0; |
| 2098 | mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange); |
| 2099 | } |
| 2100 | else |
| 2101 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2102 | infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2103 | return false; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | if (vsConstant) |
| 2108 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2109 | unsigned int samplerIndex = vsConstant->registerIndex + i; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2110 | |
| 2111 | if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits()) |
| 2112 | { |
| 2113 | mSamplersVS[samplerIndex].active = true; |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2114 | mSamplersVS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2115 | mSamplersVS[samplerIndex].logicalTextureUnit = 0; |
| 2116 | mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange); |
| 2117 | } |
| 2118 | else |
| 2119 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2120 | infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits()); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2121 | return false; |
| 2122 | } |
| 2123 | } |
| 2124 | } |
| 2125 | } |
| 2126 | |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2127 | switch(constant->typeClass) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2128 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2129 | case D3DConstant::CLASS_STRUCT: |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2130 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2131 | for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2132 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2133 | for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2134 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2135 | const D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field]; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2136 | |
apatrick@chromium.org | 85fee29 | 2012-09-06 00:43:06 +0000 | [diff] [blame] | 2137 | std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : ""; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2138 | |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2139 | if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".")) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2140 | { |
| 2141 | return false; |
| 2142 | } |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | return true; |
| 2147 | } |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2148 | case D3DConstant::CLASS_SCALAR: |
| 2149 | case D3DConstant::CLASS_VECTOR: |
| 2150 | case D3DConstant::CLASS_MATRIX_COLUMNS: |
| 2151 | case D3DConstant::CLASS_OBJECT: |
| 2152 | return defineUniform(shader, constant, name + constant->name); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2153 | default: |
| 2154 | UNREACHABLE(); |
| 2155 | return false; |
| 2156 | } |
| 2157 | } |
| 2158 | |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2159 | bool ProgramBinary::defineUniform(GLenum shader, const D3DConstant *constant, const std::string &_name) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2160 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2161 | Uniform *uniform = createUniform(constant, _name); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2162 | |
| 2163 | if(!uniform) |
| 2164 | { |
| 2165 | return false; |
| 2166 | } |
| 2167 | |
| 2168 | // Check if already defined |
| 2169 | GLint location = getUniformLocation(uniform->name); |
| 2170 | GLenum type = uniform->type; |
| 2171 | |
| 2172 | if (location >= 0) |
| 2173 | { |
| 2174 | delete uniform; |
| 2175 | uniform = mUniforms[mUniformIndex[location].index]; |
| 2176 | } |
| 2177 | |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2178 | if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant); |
| 2179 | if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2180 | |
| 2181 | if (location >= 0) |
| 2182 | { |
| 2183 | return uniform->type == type; |
| 2184 | } |
| 2185 | |
| 2186 | mUniforms.push_back(uniform); |
| 2187 | unsigned int uniformIndex = mUniforms.size() - 1; |
| 2188 | |
| 2189 | for (unsigned int i = 0; i < uniform->arraySize; ++i) |
| 2190 | { |
| 2191 | mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex)); |
| 2192 | } |
| 2193 | |
| 2194 | return true; |
| 2195 | } |
| 2196 | |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2197 | Uniform *ProgramBinary::createUniform(const D3DConstant *constant, const std::string &_name) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2198 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2199 | if (constant->rows == 1) // Vectors and scalars |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2200 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2201 | switch (constant->type) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2202 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2203 | case D3DConstant::PT_SAMPLER2D: |
| 2204 | switch (constant->columns) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2205 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2206 | case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2207 | default: UNREACHABLE(); |
| 2208 | } |
| 2209 | break; |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2210 | case D3DConstant::PT_SAMPLERCUBE: |
| 2211 | switch (constant->columns) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2212 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2213 | case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2214 | default: UNREACHABLE(); |
| 2215 | } |
| 2216 | break; |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2217 | case D3DConstant::PT_BOOL: |
| 2218 | switch (constant->columns) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2219 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2220 | case 1: return new Uniform(GL_BOOL, _name, constant->elements); |
| 2221 | case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements); |
| 2222 | case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements); |
| 2223 | case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2224 | default: UNREACHABLE(); |
| 2225 | } |
| 2226 | break; |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2227 | case D3DConstant::PT_INT: |
| 2228 | switch (constant->columns) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2229 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2230 | case 1: return new Uniform(GL_INT, _name, constant->elements); |
| 2231 | case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements); |
| 2232 | case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements); |
| 2233 | case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2234 | default: UNREACHABLE(); |
| 2235 | } |
| 2236 | break; |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2237 | case D3DConstant::PT_FLOAT: |
| 2238 | switch (constant->columns) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2239 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2240 | case 1: return new Uniform(GL_FLOAT, _name, constant->elements); |
| 2241 | case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements); |
| 2242 | case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements); |
| 2243 | case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2244 | default: UNREACHABLE(); |
| 2245 | } |
| 2246 | break; |
| 2247 | default: |
| 2248 | UNREACHABLE(); |
| 2249 | } |
| 2250 | } |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2251 | else if (constant->rows == constant->columns) // Square matrices |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2252 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2253 | switch (constant->type) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2254 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2255 | case D3DConstant::PT_FLOAT: |
| 2256 | switch (constant->rows) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2257 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2258 | case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements); |
| 2259 | case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements); |
| 2260 | case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2261 | default: UNREACHABLE(); |
| 2262 | } |
| 2263 | break; |
| 2264 | default: UNREACHABLE(); |
| 2265 | } |
| 2266 | } |
| 2267 | else UNREACHABLE(); |
| 2268 | |
| 2269 | return 0; |
| 2270 | } |
| 2271 | |
| 2272 | // This method needs to match OutputHLSL::decorate |
| 2273 | std::string ProgramBinary::decorateAttribute(const std::string &name) |
| 2274 | { |
| 2275 | if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0) |
| 2276 | { |
| 2277 | return "_" + name; |
| 2278 | } |
| 2279 | |
| 2280 | return name; |
| 2281 | } |
| 2282 | |
| 2283 | std::string ProgramBinary::undecorateUniform(const std::string &_name) |
| 2284 | { |
| 2285 | std::string name = _name; |
| 2286 | |
| 2287 | // Remove any structure field decoration |
| 2288 | size_t pos = 0; |
| 2289 | while ((pos = name.find("._", pos)) != std::string::npos) |
| 2290 | { |
| 2291 | name.replace(pos, 2, "."); |
| 2292 | } |
| 2293 | |
| 2294 | // Remove the leading decoration |
| 2295 | if (name[0] == '_') |
| 2296 | { |
| 2297 | return name.substr(1); |
| 2298 | } |
| 2299 | else if (name.compare(0, 3, "ar_") == 0) |
| 2300 | { |
| 2301 | return name.substr(3); |
| 2302 | } |
| 2303 | |
| 2304 | return name; |
| 2305 | } |
| 2306 | |
| 2307 | void ProgramBinary::applyUniformnbv(Uniform *targetUniform, GLsizei count, int width, const GLboolean *v) |
| 2308 | { |
| 2309 | float vector[D3D9_MAX_FLOAT_CONSTANTS * 4]; |
| 2310 | BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS]; |
| 2311 | |
| 2312 | if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0) |
| 2313 | { |
| 2314 | ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS); |
| 2315 | for (int i = 0; i < count; i++) |
| 2316 | { |
| 2317 | for (int j = 0; j < 4; j++) |
| 2318 | { |
| 2319 | if (j < width) |
| 2320 | { |
| 2321 | vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f; |
| 2322 | } |
| 2323 | else |
| 2324 | { |
| 2325 | vector[i * 4 + j] = 0.0f; |
| 2326 | } |
| 2327 | } |
| 2328 | } |
| 2329 | } |
| 2330 | |
| 2331 | if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0) |
| 2332 | { |
| 2333 | int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0; |
| 2334 | int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0; |
| 2335 | int copyCount = std::min(count * width, std::max(psCount, vsCount)); |
| 2336 | ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS); |
| 2337 | for (int i = 0; i < copyCount; i++) |
| 2338 | { |
| 2339 | boolVector[i] = v[i] != GL_FALSE; |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | if (targetUniform->ps.float4Index >= 0) |
| 2344 | { |
| 2345 | mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount); |
| 2346 | } |
| 2347 | |
| 2348 | if (targetUniform->ps.boolIndex >= 0) |
| 2349 | { |
| 2350 | mDevice->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount); |
| 2351 | } |
| 2352 | |
| 2353 | if (targetUniform->vs.float4Index >= 0) |
| 2354 | { |
| 2355 | mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount); |
| 2356 | } |
| 2357 | |
| 2358 | if (targetUniform->vs.boolIndex >= 0) |
| 2359 | { |
| 2360 | mDevice->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount); |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | bool ProgramBinary::applyUniformnfv(Uniform *targetUniform, const GLfloat *v) |
| 2365 | { |
| 2366 | if (targetUniform->ps.registerCount) |
| 2367 | { |
| 2368 | mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount); |
| 2369 | } |
| 2370 | |
| 2371 | if (targetUniform->vs.registerCount) |
| 2372 | { |
| 2373 | mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount); |
| 2374 | } |
| 2375 | |
| 2376 | return true; |
| 2377 | } |
| 2378 | |
| 2379 | bool ProgramBinary::applyUniform1iv(Uniform *targetUniform, GLsizei count, const GLint *v) |
| 2380 | { |
| 2381 | ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS); |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2382 | Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS]; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2383 | |
| 2384 | for (int i = 0; i < count; i++) |
| 2385 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2386 | vector[i] = Vector4((float)v[i], 0, 0, 0); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | if (targetUniform->ps.registerCount) |
| 2390 | { |
| 2391 | if (targetUniform->ps.samplerIndex >= 0) |
| 2392 | { |
| 2393 | unsigned int firstIndex = targetUniform->ps.samplerIndex; |
| 2394 | |
| 2395 | for (int i = 0; i < count; i++) |
| 2396 | { |
| 2397 | unsigned int samplerIndex = firstIndex + i; |
| 2398 | |
| 2399 | if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS) |
| 2400 | { |
| 2401 | ASSERT(mSamplersPS[samplerIndex].active); |
| 2402 | mSamplersPS[samplerIndex].logicalTextureUnit = v[i]; |
| 2403 | } |
| 2404 | } |
| 2405 | } |
| 2406 | else |
| 2407 | { |
| 2408 | ASSERT(targetUniform->ps.float4Index >= 0); |
| 2409 | mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount); |
| 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | if (targetUniform->vs.registerCount) |
| 2414 | { |
| 2415 | if (targetUniform->vs.samplerIndex >= 0) |
| 2416 | { |
| 2417 | unsigned int firstIndex = targetUniform->vs.samplerIndex; |
| 2418 | |
| 2419 | for (int i = 0; i < count; i++) |
| 2420 | { |
| 2421 | unsigned int samplerIndex = firstIndex + i; |
| 2422 | |
| 2423 | if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF) |
| 2424 | { |
| 2425 | ASSERT(mSamplersVS[samplerIndex].active); |
| 2426 | mSamplersVS[samplerIndex].logicalTextureUnit = v[i]; |
| 2427 | } |
| 2428 | } |
| 2429 | } |
| 2430 | else |
| 2431 | { |
| 2432 | ASSERT(targetUniform->vs.float4Index >= 0); |
| 2433 | mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount); |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | return true; |
| 2438 | } |
| 2439 | |
| 2440 | bool ProgramBinary::applyUniform2iv(Uniform *targetUniform, GLsizei count, const GLint *v) |
| 2441 | { |
| 2442 | ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS); |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2443 | Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS]; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2444 | |
| 2445 | for (int i = 0; i < count; i++) |
| 2446 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2447 | vector[i] = Vector4((float)v[0], (float)v[1], 0, 0); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2448 | |
| 2449 | v += 2; |
| 2450 | } |
| 2451 | |
| 2452 | applyUniformniv(targetUniform, count, vector); |
| 2453 | |
| 2454 | return true; |
| 2455 | } |
| 2456 | |
| 2457 | bool ProgramBinary::applyUniform3iv(Uniform *targetUniform, GLsizei count, const GLint *v) |
| 2458 | { |
| 2459 | ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS); |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2460 | Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS]; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2461 | |
| 2462 | for (int i = 0; i < count; i++) |
| 2463 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2464 | vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2465 | |
| 2466 | v += 3; |
| 2467 | } |
| 2468 | |
| 2469 | applyUniformniv(targetUniform, count, vector); |
| 2470 | |
| 2471 | return true; |
| 2472 | } |
| 2473 | |
| 2474 | bool ProgramBinary::applyUniform4iv(Uniform *targetUniform, GLsizei count, const GLint *v) |
| 2475 | { |
| 2476 | ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS); |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2477 | Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS]; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2478 | |
| 2479 | for (int i = 0; i < count; i++) |
| 2480 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2481 | vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2482 | |
| 2483 | v += 4; |
| 2484 | } |
| 2485 | |
| 2486 | applyUniformniv(targetUniform, count, vector); |
| 2487 | |
| 2488 | return true; |
| 2489 | } |
| 2490 | |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 2491 | void ProgramBinary::applyUniformniv(Uniform *targetUniform, GLsizei count, const Vector4 *vector) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2492 | { |
| 2493 | if (targetUniform->ps.registerCount) |
| 2494 | { |
| 2495 | ASSERT(targetUniform->ps.float4Index >= 0); |
| 2496 | mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount); |
| 2497 | } |
| 2498 | |
| 2499 | if (targetUniform->vs.registerCount) |
| 2500 | { |
| 2501 | ASSERT(targetUniform->vs.float4Index >= 0); |
| 2502 | mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount); |
| 2503 | } |
| 2504 | } |
| 2505 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2506 | bool ProgramBinary::isValidated() const |
| 2507 | { |
| 2508 | return mValidated; |
| 2509 | } |
| 2510 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2511 | void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) |
| 2512 | { |
| 2513 | // Skip over inactive attributes |
| 2514 | unsigned int activeAttribute = 0; |
| 2515 | unsigned int attribute; |
| 2516 | for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++) |
| 2517 | { |
| 2518 | if (mLinkedAttribute[attribute].name.empty()) |
| 2519 | { |
| 2520 | continue; |
| 2521 | } |
| 2522 | |
| 2523 | if (activeAttribute == index) |
| 2524 | { |
| 2525 | break; |
| 2526 | } |
| 2527 | |
| 2528 | activeAttribute++; |
| 2529 | } |
| 2530 | |
| 2531 | if (bufsize > 0) |
| 2532 | { |
| 2533 | const char *string = mLinkedAttribute[attribute].name.c_str(); |
| 2534 | |
| 2535 | strncpy(name, string, bufsize); |
| 2536 | name[bufsize - 1] = '\0'; |
| 2537 | |
| 2538 | if (length) |
| 2539 | { |
| 2540 | *length = strlen(name); |
| 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | *size = 1; // Always a single 'type' instance |
| 2545 | |
| 2546 | *type = mLinkedAttribute[attribute].type; |
| 2547 | } |
| 2548 | |
| 2549 | GLint ProgramBinary::getActiveAttributeCount() |
| 2550 | { |
| 2551 | int count = 0; |
| 2552 | |
| 2553 | for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
| 2554 | { |
| 2555 | if (!mLinkedAttribute[attributeIndex].name.empty()) |
| 2556 | { |
| 2557 | count++; |
| 2558 | } |
| 2559 | } |
| 2560 | |
| 2561 | return count; |
| 2562 | } |
| 2563 | |
| 2564 | GLint ProgramBinary::getActiveAttributeMaxLength() |
| 2565 | { |
| 2566 | int maxLength = 0; |
| 2567 | |
| 2568 | for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
| 2569 | { |
| 2570 | if (!mLinkedAttribute[attributeIndex].name.empty()) |
| 2571 | { |
| 2572 | maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength); |
| 2573 | } |
| 2574 | } |
| 2575 | |
| 2576 | return maxLength; |
| 2577 | } |
| 2578 | |
| 2579 | void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) |
| 2580 | { |
| 2581 | // Skip over internal uniforms |
| 2582 | unsigned int activeUniform = 0; |
| 2583 | unsigned int uniform; |
| 2584 | for (uniform = 0; uniform < mUniforms.size(); uniform++) |
| 2585 | { |
| 2586 | if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0) |
| 2587 | { |
| 2588 | continue; |
| 2589 | } |
| 2590 | |
| 2591 | if (activeUniform == index) |
| 2592 | { |
| 2593 | break; |
| 2594 | } |
| 2595 | |
| 2596 | activeUniform++; |
| 2597 | } |
| 2598 | |
| 2599 | ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount() |
| 2600 | |
| 2601 | if (bufsize > 0) |
| 2602 | { |
| 2603 | std::string string = mUniforms[uniform]->name; |
| 2604 | |
| 2605 | if (mUniforms[uniform]->isArray()) |
| 2606 | { |
| 2607 | string += "[0]"; |
| 2608 | } |
| 2609 | |
| 2610 | strncpy(name, string.c_str(), bufsize); |
| 2611 | name[bufsize - 1] = '\0'; |
| 2612 | |
| 2613 | if (length) |
| 2614 | { |
| 2615 | *length = strlen(name); |
| 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | *size = mUniforms[uniform]->arraySize; |
| 2620 | |
| 2621 | *type = mUniforms[uniform]->type; |
| 2622 | } |
| 2623 | |
| 2624 | GLint ProgramBinary::getActiveUniformCount() |
| 2625 | { |
| 2626 | int count = 0; |
| 2627 | |
| 2628 | unsigned int numUniforms = mUniforms.size(); |
| 2629 | for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++) |
| 2630 | { |
| 2631 | if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0) |
| 2632 | { |
| 2633 | count++; |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | return count; |
| 2638 | } |
| 2639 | |
| 2640 | GLint ProgramBinary::getActiveUniformMaxLength() |
| 2641 | { |
| 2642 | int maxLength = 0; |
| 2643 | |
| 2644 | unsigned int numUniforms = mUniforms.size(); |
| 2645 | for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++) |
| 2646 | { |
| 2647 | if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0) |
| 2648 | { |
| 2649 | int length = (int)(mUniforms[uniformIndex]->name.length() + 1); |
| 2650 | if (mUniforms[uniformIndex]->isArray()) |
| 2651 | { |
| 2652 | length += 3; // Counting in "[0]". |
| 2653 | } |
| 2654 | maxLength = std::max(length, maxLength); |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | return maxLength; |
| 2659 | } |
| 2660 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2661 | void ProgramBinary::validate(InfoLog &infoLog) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2662 | { |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2663 | applyUniforms(); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2664 | if (!validateSamplers(&infoLog)) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2665 | { |
| 2666 | mValidated = false; |
| 2667 | } |
| 2668 | else |
| 2669 | { |
| 2670 | mValidated = true; |
| 2671 | } |
| 2672 | } |
| 2673 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2674 | bool ProgramBinary::validateSamplers(InfoLog *infoLog) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2675 | { |
| 2676 | // if any two active samplers in a program are of different types, but refer to the same |
| 2677 | // texture image unit, and this is the current program, then ValidateProgram will fail, and |
| 2678 | // DrawArrays and DrawElements will issue the INVALID_OPERATION error. |
| 2679 | |
| 2680 | const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits(); |
| 2681 | TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF]; |
| 2682 | |
| 2683 | for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i) |
| 2684 | { |
| 2685 | textureUnitType[i] = TEXTURE_UNKNOWN; |
| 2686 | } |
| 2687 | |
| 2688 | for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i) |
| 2689 | { |
| 2690 | if (mSamplersPS[i].active) |
| 2691 | { |
| 2692 | unsigned int unit = mSamplersPS[i].logicalTextureUnit; |
| 2693 | |
| 2694 | if (unit >= maxCombinedTextureImageUnits) |
| 2695 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2696 | if (infoLog) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2697 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2698 | infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | return false; |
| 2702 | } |
| 2703 | |
| 2704 | if (textureUnitType[unit] != TEXTURE_UNKNOWN) |
| 2705 | { |
| 2706 | if (mSamplersPS[i].textureType != textureUnitType[unit]) |
| 2707 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2708 | if (infoLog) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2709 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2710 | infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | return false; |
| 2714 | } |
| 2715 | } |
| 2716 | else |
| 2717 | { |
| 2718 | textureUnitType[unit] = mSamplersPS[i].textureType; |
| 2719 | } |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i) |
| 2724 | { |
| 2725 | if (mSamplersVS[i].active) |
| 2726 | { |
| 2727 | unsigned int unit = mSamplersVS[i].logicalTextureUnit; |
| 2728 | |
| 2729 | if (unit >= maxCombinedTextureImageUnits) |
| 2730 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2731 | if (infoLog) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2732 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2733 | infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | return false; |
| 2737 | } |
| 2738 | |
| 2739 | if (textureUnitType[unit] != TEXTURE_UNKNOWN) |
| 2740 | { |
| 2741 | if (mSamplersVS[i].textureType != textureUnitType[unit]) |
| 2742 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2743 | if (infoLog) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2744 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 2745 | infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | return false; |
| 2749 | } |
| 2750 | } |
| 2751 | else |
| 2752 | { |
| 2753 | textureUnitType[unit] = mSamplersVS[i].textureType; |
| 2754 | } |
| 2755 | } |
| 2756 | } |
| 2757 | |
| 2758 | return true; |
| 2759 | } |
| 2760 | |
| 2761 | GLint ProgramBinary::getDxDepthRangeLocation() const |
| 2762 | { |
| 2763 | return mDxDepthRangeLocation; |
| 2764 | } |
| 2765 | |
| 2766 | GLint ProgramBinary::getDxDepthLocation() const |
| 2767 | { |
| 2768 | return mDxDepthLocation; |
| 2769 | } |
| 2770 | |
| 2771 | GLint ProgramBinary::getDxCoordLocation() const |
| 2772 | { |
| 2773 | return mDxCoordLocation; |
| 2774 | } |
| 2775 | |
| 2776 | GLint ProgramBinary::getDxHalfPixelSizeLocation() const |
| 2777 | { |
| 2778 | return mDxHalfPixelSizeLocation; |
| 2779 | } |
| 2780 | |
| 2781 | GLint ProgramBinary::getDxFrontCCWLocation() const |
| 2782 | { |
| 2783 | return mDxFrontCCWLocation; |
| 2784 | } |
| 2785 | |
| 2786 | GLint ProgramBinary::getDxPointsOrLinesLocation() const |
| 2787 | { |
| 2788 | return mDxPointsOrLinesLocation; |
| 2789 | } |
| 2790 | |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 2791 | ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D) |
| 2792 | { |
| 2793 | } |
| 2794 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 2795 | } |