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