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