daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 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 | |
| 10 | #include "Program.h" |
| 11 | |
| 12 | #include "main.h" |
| 13 | #include "Shader.h" |
alokp@chromium.org | ea0e1af | 2010-03-22 19:33:14 +0000 | [diff] [blame] | 14 | #include "common/debug.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 15 | |
| 16 | namespace gl |
| 17 | { |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 18 | Uniform::Uniform(GLenum type, const std::string &name, unsigned int bytes) : type(type), name(name), bytes(bytes) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 19 | { |
| 20 | this->data = new unsigned char[bytes]; |
| 21 | memset(this->data, 0, bytes); |
| 22 | } |
| 23 | |
| 24 | Uniform::~Uniform() |
| 25 | { |
| 26 | delete[] data; |
| 27 | } |
| 28 | |
| 29 | Program::Program() |
| 30 | { |
| 31 | mFragmentShader = NULL; |
| 32 | mVertexShader = NULL; |
| 33 | |
| 34 | mPixelExecutable = NULL; |
| 35 | mVertexExecutable = NULL; |
| 36 | mConstantTablePS = NULL; |
| 37 | mConstantTableVS = NULL; |
| 38 | |
| 39 | for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++) |
| 40 | { |
| 41 | mAttributeName[index] = NULL; |
| 42 | } |
| 43 | |
daniel@transgaming.com | 0e3358a | 2010-04-05 20:32:42 +0000 | [diff] [blame^] | 44 | mPixelHLSL = NULL; |
| 45 | mVertexHLSL = NULL; |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 46 | mInfoLog = NULL; |
| 47 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 48 | unlink(); |
| 49 | |
| 50 | mDeleteStatus = false; |
| 51 | } |
| 52 | |
| 53 | Program::~Program() |
| 54 | { |
| 55 | unlink(true); |
| 56 | } |
| 57 | |
| 58 | bool Program::attachShader(Shader *shader) |
| 59 | { |
| 60 | if (shader->getType() == GL_VERTEX_SHADER) |
| 61 | { |
| 62 | if (mVertexShader) |
| 63 | { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | mVertexShader = (VertexShader*)shader; |
| 68 | mVertexShader->attach(); |
| 69 | } |
| 70 | else if (shader->getType() == GL_FRAGMENT_SHADER) |
| 71 | { |
| 72 | if (mFragmentShader) |
| 73 | { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | mFragmentShader = (FragmentShader*)shader; |
| 78 | mFragmentShader->attach(); |
| 79 | } |
| 80 | else UNREACHABLE(); |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | bool Program::detachShader(Shader *shader) |
| 86 | { |
| 87 | if (shader->getType() == GL_VERTEX_SHADER) |
| 88 | { |
| 89 | if (mVertexShader != shader) |
| 90 | { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | mVertexShader->detach(); |
| 95 | mVertexShader = NULL; |
| 96 | } |
| 97 | else if (shader->getType() == GL_FRAGMENT_SHADER) |
| 98 | { |
| 99 | if (mFragmentShader != shader) |
| 100 | { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | mFragmentShader->detach(); |
| 105 | mFragmentShader = NULL; |
| 106 | } |
| 107 | else UNREACHABLE(); |
| 108 | |
| 109 | unlink(); |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 114 | int Program::getAttachedShadersCount() const |
| 115 | { |
| 116 | return (mVertexShader ? 1 : 0) + (mFragmentShader ? 1 : 0); |
| 117 | } |
| 118 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 119 | IDirect3DPixelShader9 *Program::getPixelShader() |
| 120 | { |
| 121 | return mPixelExecutable; |
| 122 | } |
| 123 | |
| 124 | IDirect3DVertexShader9 *Program::getVertexShader() |
| 125 | { |
| 126 | return mVertexExecutable; |
| 127 | } |
| 128 | |
| 129 | void Program::bindAttributeLocation(GLuint index, const char *name) |
| 130 | { |
| 131 | if (index < MAX_VERTEX_ATTRIBS) |
| 132 | { |
| 133 | delete[] mAttributeName[index]; |
| 134 | mAttributeName[index] = new char[strlen(name) + 1]; |
| 135 | strcpy(mAttributeName[index], name); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | GLuint Program::getAttributeLocation(const char *name) |
| 140 | { |
| 141 | for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++) |
| 142 | { |
| 143 | if (mAttributeName[index] && strcmp(mAttributeName[index], name) == 0) |
| 144 | { |
| 145 | return index; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | bool Program::isActiveAttribute(int attributeIndex) |
| 153 | { |
| 154 | if (attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS) |
| 155 | { |
| 156 | return mInputMapping[attributeIndex] != -1; |
| 157 | } |
| 158 | |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | int Program::getInputMapping(int attributeIndex) |
| 163 | { |
| 164 | if (attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS) |
| 165 | { |
| 166 | return mInputMapping[attributeIndex]; |
| 167 | } |
| 168 | |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | // Returns the index of the texture unit corresponding to a Direct3D 9 sampler |
| 173 | // index referenced in the compiled HLSL shader |
| 174 | GLint Program::getSamplerMapping(unsigned int samplerIndex) |
| 175 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 176 | assert(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0])); |
| 177 | |
| 178 | if (mSamplers[samplerIndex].active) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 179 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 180 | return mSamplers[samplerIndex].logicalTextureUnit; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 181 | } |
| 182 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | SamplerType Program::getSamplerType(unsigned int samplerIndex) |
| 187 | { |
| 188 | assert(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0])); |
| 189 | assert(mSamplers[samplerIndex].active); |
| 190 | |
| 191 | return mSamplers[samplerIndex].type; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | GLint Program::getUniformLocation(const char *name) |
| 195 | { |
| 196 | for (unsigned int location = 0; location < mUniforms.size(); location++) |
| 197 | { |
| 198 | if (mUniforms[location]->name == name) |
| 199 | { |
| 200 | return location; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | bool Program::setUniform1fv(GLint location, GLsizei count, const GLfloat* v) |
| 208 | { |
| 209 | if (location < 0 || location >= (int)mUniforms.size()) |
| 210 | { |
| 211 | return false; |
| 212 | } |
| 213 | |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 214 | if (mUniforms[location]->type != GL_FLOAT || mUniforms[location]->bytes < sizeof(GLfloat) * count) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 215 | { |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | memcpy(mUniforms[location]->data, v, sizeof(GLfloat) * count); |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | bool Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 225 | { |
| 226 | if (location < 0 || location >= (int)mUniforms.size()) |
| 227 | { |
| 228 | return false; |
| 229 | } |
| 230 | |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 231 | if (mUniforms[location]->type != GL_FLOAT_VEC2 || mUniforms[location]->bytes < 2 * sizeof(GLfloat) * count) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 232 | { |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | memcpy(mUniforms[location]->data, v, 2 * sizeof(GLfloat) * count); |
| 237 | |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | bool Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 242 | { |
| 243 | if (location < 0 || location >= (int)mUniforms.size()) |
| 244 | { |
| 245 | return false; |
| 246 | } |
| 247 | |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 248 | if (mUniforms[location]->type != GL_FLOAT_VEC3 || mUniforms[location]->bytes < 3 * sizeof(GLfloat) * count) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 249 | { |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | memcpy(mUniforms[location]->data, v, 3 * sizeof(GLfloat) * count); |
| 254 | |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | bool Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 259 | { |
| 260 | if (location < 0 || location >= (int)mUniforms.size()) |
| 261 | { |
| 262 | return false; |
| 263 | } |
| 264 | |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 265 | if (mUniforms[location]->type != GL_FLOAT_VEC4 || mUniforms[location]->bytes < 4 * sizeof(GLfloat) * count) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 266 | { |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | memcpy(mUniforms[location]->data, v, 4 * sizeof(GLfloat) * count); |
| 271 | |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | bool Program::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value) |
| 276 | { |
| 277 | if (location < 0 || location >= (int)mUniforms.size()) |
| 278 | { |
| 279 | return false; |
| 280 | } |
| 281 | |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 282 | if (mUniforms[location]->type != GL_FLOAT_MAT2 || mUniforms[location]->bytes < 4 * sizeof(GLfloat) * count) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 283 | { |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | memcpy(mUniforms[location]->data, value, 4 * sizeof(GLfloat) * count); |
| 288 | |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | bool Program::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value) |
| 293 | { |
| 294 | if (location < 0 || location >= (int)mUniforms.size()) |
| 295 | { |
| 296 | return false; |
| 297 | } |
| 298 | |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 299 | if (mUniforms[location]->type != GL_FLOAT_MAT3 || mUniforms[location]->bytes < 9 * sizeof(GLfloat) * count) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 300 | { |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | memcpy(mUniforms[location]->data, value, 9 * sizeof(GLfloat) * count); |
| 305 | |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | bool Program::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value) |
| 310 | { |
| 311 | if (location < 0 || location >= (int)mUniforms.size()) |
| 312 | { |
| 313 | return false; |
| 314 | } |
| 315 | |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 316 | if (mUniforms[location]->type != GL_FLOAT_MAT4 || mUniforms[location]->bytes < 16 * sizeof(GLfloat) * count) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 317 | { |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | memcpy(mUniforms[location]->data, value, 16 * sizeof(GLfloat) * count); |
| 322 | |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | bool Program::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
| 327 | { |
| 328 | if (location < 0 || location >= (int)mUniforms.size()) |
| 329 | { |
| 330 | return false; |
| 331 | } |
| 332 | |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 333 | if (mUniforms[location]->type != GL_INT || mUniforms[location]->bytes < sizeof(GLint) * count) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 334 | { |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | memcpy(mUniforms[location]->data, v, sizeof(GLint) * count); |
| 339 | |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | // Applies all the uniforms set for this program object to the Direct3D 9 device |
| 344 | void Program::applyUniforms() |
| 345 | { |
| 346 | for (unsigned int location = 0; location < mUniforms.size(); location++) |
| 347 | { |
| 348 | int bytes = mUniforms[location]->bytes; |
| 349 | GLfloat *f = (GLfloat*)mUniforms[location]->data; |
| 350 | GLint *i = (GLint*)mUniforms[location]->data; |
| 351 | |
| 352 | switch (mUniforms[location]->type) |
| 353 | { |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 354 | case GL_FLOAT: applyUniform1fv(location, bytes / sizeof(GLfloat), f); break; |
| 355 | case GL_FLOAT_VEC2: applyUniform2fv(location, bytes / 2 / sizeof(GLfloat), f); break; |
| 356 | case GL_FLOAT_VEC3: applyUniform3fv(location, bytes / 3 / sizeof(GLfloat), f); break; |
| 357 | case GL_FLOAT_VEC4: applyUniform4fv(location, bytes / 4 / sizeof(GLfloat), f); break; |
| 358 | case GL_FLOAT_MAT2: applyUniformMatrix2fv(location, bytes / 4 / sizeof(GLfloat), f); break; |
| 359 | case GL_FLOAT_MAT3: applyUniformMatrix3fv(location, bytes / 9 / sizeof(GLfloat), f); break; |
| 360 | case GL_FLOAT_MAT4: applyUniformMatrix4fv(location, bytes / 16 / sizeof(GLfloat), f); break; |
| 361 | case GL_INT: applyUniform1iv(location, bytes / sizeof(GLint), i); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 362 | default: |
| 363 | UNIMPLEMENTED(); // FIXME |
| 364 | UNREACHABLE(); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // Compiles the HLSL code of the attached shaders into executable binaries |
| 370 | ID3DXBuffer *Program::compileToBinary(const char *hlsl, const char *profile, ID3DXConstantTable **constantTable) |
| 371 | { |
| 372 | if (!hlsl) |
| 373 | { |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | ID3DXBuffer *binary = NULL; |
| 378 | ID3DXBuffer *errorMessage = NULL; |
| 379 | |
daniel@transgaming.com | cbbca00 | 2010-04-01 13:39:32 +0000 | [diff] [blame] | 380 | HRESULT result = D3DXCompileShader(hlsl, (UINT)strlen(hlsl), NULL, 0, "main", profile, 0, &binary, &errorMessage, constantTable); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 381 | |
| 382 | if (SUCCEEDED(result)) |
| 383 | { |
| 384 | return binary; |
| 385 | } |
| 386 | |
| 387 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 388 | { |
| 389 | return error(GL_OUT_OF_MEMORY, (ID3DXBuffer*)NULL); |
| 390 | } |
| 391 | |
| 392 | if (errorMessage) |
| 393 | { |
| 394 | const char *message = (const char*)errorMessage->GetBufferPointer(); |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 395 | |
daniel@transgaming.com | 0599dc6 | 2010-03-21 04:31:36 +0000 | [diff] [blame] | 396 | TRACE("\n%s", hlsl); |
| 397 | TRACE("\n%s", message); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | return NULL; |
| 401 | } |
| 402 | |
daniel@transgaming.com | 0e3358a | 2010-04-05 20:32:42 +0000 | [diff] [blame^] | 403 | void Program::parseVaryings(const char *structure, char *hlsl, VaryingArray &varyings) |
| 404 | { |
| 405 | char *input = strstr(hlsl, structure); |
| 406 | input += strlen(structure); |
| 407 | |
| 408 | while (input && *input != '}') |
| 409 | { |
| 410 | char varyingType[256]; |
| 411 | char varyingName[256]; |
| 412 | unsigned int semanticIndex; |
| 413 | int matches = sscanf(input, " %s %s : TEXCOORD%d;", varyingType, varyingName, &semanticIndex); |
| 414 | |
| 415 | if (matches == 3) |
| 416 | { |
| 417 | ASSERT(semanticIndex <= 9); // Single character |
| 418 | |
| 419 | varyings.push_back(Varying(varyingName, input)); |
| 420 | } |
| 421 | |
| 422 | input = strstr(input, ";"); |
| 423 | input += 2; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | bool Program::linkVaryings() |
| 428 | { |
| 429 | if (!mPixelHLSL || !mVertexHLSL) |
| 430 | { |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | VaryingArray vertexVaryings; |
| 435 | VaryingArray pixelVaryings; |
| 436 | |
| 437 | parseVaryings("struct VS_OUTPUT\n{\n", mVertexHLSL, vertexVaryings); |
| 438 | parseVaryings("struct PS_INPUT\n{\n", mPixelHLSL, pixelVaryings); |
| 439 | |
| 440 | for (unsigned int out = 0; out < vertexVaryings.size(); out++) |
| 441 | { |
| 442 | unsigned int in; |
| 443 | for (in = 0; in < pixelVaryings.size(); in++) |
| 444 | { |
| 445 | if (vertexVaryings[out].name == pixelVaryings[in].name) |
| 446 | { |
| 447 | pixelVaryings[in].link = out; |
| 448 | vertexVaryings[out].link = in; |
| 449 | |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | if (in != pixelVaryings.size()) |
| 455 | { |
| 456 | // FIXME: Verify matching type and qualifiers |
| 457 | |
| 458 | char *outputSemantic = strstr(vertexVaryings[out].declaration, " : TEXCOORD"); |
| 459 | char *inputSemantic = strstr(pixelVaryings[in].declaration, " : TEXCOORD"); |
| 460 | outputSemantic[11] = inputSemantic[11]; |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | // Comment out the declaration and output assignment |
| 465 | vertexVaryings[out].declaration[0] = '/'; |
| 466 | vertexVaryings[out].declaration[1] = '/'; |
| 467 | |
| 468 | char outputString[256]; |
| 469 | sprintf(outputString, " output.%s = ", vertexVaryings[out].name.c_str()); |
| 470 | char *varyingOutput = strstr(mVertexHLSL, outputString); |
| 471 | |
| 472 | varyingOutput[0] = '/'; |
| 473 | varyingOutput[1] = '/'; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // Verify that each pixel varying has been linked to a vertex varying |
| 478 | for (unsigned int in = 0; in < pixelVaryings.size(); in++) |
| 479 | { |
| 480 | if (pixelVaryings[in].link < 0) |
| 481 | { |
| 482 | return false; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | return true; |
| 487 | } |
| 488 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 489 | // Links the HLSL code of the vertex and pixel shader by matching up their varyings, |
| 490 | // compiling them into binaries, determining the attribute mappings, and collecting |
| 491 | // a list of uniforms |
| 492 | void Program::link() |
| 493 | { |
| 494 | if (mLinked) |
| 495 | { |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | unlink(); |
| 500 | |
| 501 | if (!mFragmentShader || !mFragmentShader->isCompiled()) |
| 502 | { |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | if (!mVertexShader || !mVertexShader->isCompiled()) |
| 507 | { |
| 508 | return; |
| 509 | } |
| 510 | |
daniel@transgaming.com | 296ca9c | 2010-04-03 20:56:07 +0000 | [diff] [blame] | 511 | Context *context = getContext(); |
| 512 | const char *vertexProfile = context->getVertexShaderProfile(); |
| 513 | const char *pixelProfile = context->getPixelShaderProfile(); |
daniel@transgaming.com | debe259 | 2010-03-24 09:44:08 +0000 | [diff] [blame] | 514 | |
daniel@transgaming.com | 0e3358a | 2010-04-05 20:32:42 +0000 | [diff] [blame^] | 515 | const char *ps = mFragmentShader->getHLSL(); |
| 516 | const char *vs = mVertexShader->getHLSL(); |
| 517 | |
| 518 | mPixelHLSL = new char[strlen(ps) + 1]; |
| 519 | strcpy(mPixelHLSL, ps); |
| 520 | mVertexHLSL = new char[strlen(vs) + 1]; |
| 521 | strcpy(mVertexHLSL, vs); |
| 522 | |
| 523 | if (!linkVaryings()) |
| 524 | { |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | ID3DXBuffer *vertexBinary = compileToBinary(mVertexHLSL, vertexProfile, &mConstantTableVS); |
| 529 | ID3DXBuffer *pixelBinary = compileToBinary(mPixelHLSL, pixelProfile, &mConstantTablePS); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 530 | |
| 531 | if (vertexBinary && pixelBinary) |
| 532 | { |
daniel@transgaming.com | 296ca9c | 2010-04-03 20:56:07 +0000 | [diff] [blame] | 533 | IDirect3DDevice9 *device = getDevice(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 534 | HRESULT vertexResult = device->CreateVertexShader((DWORD*)vertexBinary->GetBufferPointer(), &mVertexExecutable); |
| 535 | HRESULT pixelResult = device->CreatePixelShader((DWORD*)pixelBinary->GetBufferPointer(), &mPixelExecutable); |
| 536 | |
| 537 | if (vertexResult == D3DERR_OUTOFVIDEOMEMORY || vertexResult == E_OUTOFMEMORY || pixelResult == D3DERR_OUTOFVIDEOMEMORY || pixelResult == E_OUTOFMEMORY) |
| 538 | { |
| 539 | return error(GL_OUT_OF_MEMORY); |
| 540 | } |
| 541 | |
| 542 | ASSERT(SUCCEEDED(vertexResult) && SUCCEEDED(pixelResult)); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 543 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 544 | vertexBinary->Release(); |
| 545 | pixelBinary->Release(); |
| 546 | vertexBinary = NULL; |
| 547 | pixelBinary = NULL; |
| 548 | |
| 549 | if (mVertexExecutable && mPixelExecutable) |
| 550 | { |
| 551 | if (!linkAttributes()) |
| 552 | { |
| 553 | return; |
| 554 | } |
| 555 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 556 | for (int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) |
| 557 | { |
| 558 | mSamplers[i].active = false; |
| 559 | } |
| 560 | |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 561 | if (!linkUniforms(mConstantTablePS)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 562 | { |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 563 | return; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 564 | } |
| 565 | |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 566 | if (!linkUniforms(mConstantTableVS)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 567 | { |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 568 | return; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 569 | } |
| 570 | |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 571 | mLinked = true; // Success |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | // Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices |
| 577 | bool Program::linkAttributes() |
| 578 | { |
| 579 | for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
| 580 | { |
| 581 | const char *name = mVertexShader->getAttributeName(attributeIndex); |
| 582 | |
| 583 | if (name) |
| 584 | { |
| 585 | GLuint location = getAttributeLocation(name); |
| 586 | |
| 587 | if (location == -1) // Not set by glBindAttribLocation |
| 588 | { |
| 589 | int availableIndex = 0; |
| 590 | |
| 591 | while (availableIndex < MAX_VERTEX_ATTRIBS && mAttributeName[availableIndex] && mVertexShader->isActiveAttribute(mAttributeName[availableIndex])) |
| 592 | { |
| 593 | availableIndex++; |
| 594 | } |
| 595 | |
| 596 | if (availableIndex == MAX_VERTEX_ATTRIBS) |
| 597 | { |
| 598 | return false; // Fail to link |
| 599 | } |
| 600 | |
| 601 | delete[] mAttributeName[availableIndex]; |
| 602 | mAttributeName[availableIndex] = new char[strlen(name) + 1]; // FIXME: Check allocation |
| 603 | strcpy(mAttributeName[availableIndex], name); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
| 609 | { |
| 610 | mInputMapping[attributeIndex] = mVertexShader->getInputMapping(mAttributeName[attributeIndex]); |
| 611 | } |
| 612 | |
| 613 | return true; |
| 614 | } |
| 615 | |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 616 | bool Program::linkUniforms(ID3DXConstantTable *constantTable) |
| 617 | { |
| 618 | D3DXCONSTANTTABLE_DESC constantTableDescription; |
| 619 | D3DXCONSTANT_DESC constantDescription; |
| 620 | UINT descriptionCount = 1; |
| 621 | |
| 622 | constantTable->GetDesc(&constantTableDescription); |
| 623 | |
| 624 | for (unsigned int constantIndex = 0; constantIndex < constantTableDescription.Constants; constantIndex++) |
| 625 | { |
| 626 | D3DXHANDLE constantHandle = constantTable->GetConstant(0, constantIndex); |
| 627 | constantTable->GetConstantDesc(constantHandle, &constantDescription, &descriptionCount); |
| 628 | |
| 629 | if (!defineUniform(constantHandle, constantDescription)) |
| 630 | { |
| 631 | return false; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | return true; |
| 636 | } |
| 637 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 638 | // Adds the description of a constant found in the binary shader to the list of uniforms |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 639 | // Returns true if succesful (uniform not already defined) |
| 640 | bool Program::defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT_DESC &constantDescription, std::string name) |
| 641 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 642 | if (constantDescription.RegisterSet == D3DXRS_SAMPLER) |
| 643 | { |
| 644 | unsigned int samplerIndex = constantDescription.RegisterIndex; |
| 645 | |
| 646 | assert(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0])); |
| 647 | |
| 648 | mSamplers[samplerIndex].active = true; |
| 649 | mSamplers[samplerIndex].type = (constantDescription.Type == D3DXPT_SAMPLERCUBE) ? SAMPLER_CUBE : SAMPLER_2D; |
| 650 | mSamplers[samplerIndex].logicalTextureUnit = 0; |
| 651 | } |
| 652 | |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 653 | switch(constantDescription.Class) |
| 654 | { |
| 655 | case D3DXPC_STRUCT: |
| 656 | { |
| 657 | for (unsigned int field = 0; field < constantDescription.StructMembers; field++) |
| 658 | { |
| 659 | D3DXHANDLE fieldHandle = mConstantTablePS->GetConstant(constantHandle, field); |
| 660 | |
| 661 | D3DXCONSTANT_DESC fieldDescription; |
| 662 | UINT descriptionCount = 1; |
| 663 | |
| 664 | mConstantTablePS->GetConstantDesc(fieldHandle, &fieldDescription, &descriptionCount); |
| 665 | |
| 666 | if (!defineUniform(fieldHandle, fieldDescription, name + constantDescription.Name + ".")) |
| 667 | { |
| 668 | return false; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | return true; |
| 673 | } |
| 674 | case D3DXPC_SCALAR: |
| 675 | case D3DXPC_VECTOR: |
| 676 | case D3DXPC_MATRIX_COLUMNS: |
| 677 | case D3DXPC_OBJECT: |
| 678 | return defineUniform(constantDescription, name + constantDescription.Name); |
| 679 | default: |
| 680 | UNREACHABLE(); |
| 681 | return false; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name) |
| 686 | { |
| 687 | Uniform *uniform = createUniform(constantDescription, name); |
| 688 | |
| 689 | if(!uniform) |
| 690 | { |
| 691 | return false; |
| 692 | } |
| 693 | |
| 694 | // Check if already defined |
| 695 | GLint location = getUniformLocation(name.c_str()); |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 696 | GLenum type = uniform->type; |
daniel@transgaming.com | c7d8a93 | 2010-03-16 06:16:45 +0000 | [diff] [blame] | 697 | |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 698 | if (location >= 0) |
| 699 | { |
| 700 | delete uniform; |
| 701 | |
| 702 | if (mUniforms[location]->type != type) |
| 703 | { |
| 704 | return false; |
| 705 | } |
| 706 | else |
| 707 | { |
| 708 | return true; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | mUniforms.push_back(uniform); |
| 713 | |
| 714 | return true; |
| 715 | } |
| 716 | |
| 717 | Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 718 | { |
| 719 | if (constantDescription.Rows == 1) // Vectors and scalars |
| 720 | { |
| 721 | switch (constantDescription.Type) |
| 722 | { |
| 723 | case D3DXPT_SAMPLER2D: |
| 724 | case D3DXPT_SAMPLERCUBE: |
| 725 | case D3DXPT_BOOL: |
| 726 | switch (constantDescription.Columns) |
| 727 | { |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 728 | case 1: return new Uniform(GL_INT, name, 1 * sizeof(GLint) * constantDescription.Elements); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 729 | default: |
| 730 | UNIMPLEMENTED(); // FIXME |
| 731 | UNREACHABLE(); |
| 732 | } |
| 733 | break; |
| 734 | case D3DXPT_FLOAT: |
| 735 | switch (constantDescription.Columns) |
| 736 | { |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 737 | case 1: return new Uniform(GL_FLOAT, name, 1 * sizeof(GLfloat) * constantDescription.Elements); |
| 738 | case 2: return new Uniform(GL_FLOAT_VEC2, name, 2 * sizeof(GLfloat) * constantDescription.Elements); |
| 739 | case 3: return new Uniform(GL_FLOAT_VEC3, name, 3 * sizeof(GLfloat) * constantDescription.Elements); |
| 740 | case 4: return new Uniform(GL_FLOAT_VEC4, name, 4 * sizeof(GLfloat) * constantDescription.Elements); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 741 | default: UNREACHABLE(); |
| 742 | } |
| 743 | break; |
| 744 | default: |
| 745 | UNIMPLEMENTED(); // FIXME |
| 746 | UNREACHABLE(); |
| 747 | } |
| 748 | } |
| 749 | else if (constantDescription.Rows == constantDescription.Columns) // Square matrices |
| 750 | { |
| 751 | switch (constantDescription.Type) |
| 752 | { |
| 753 | case D3DXPT_FLOAT: |
| 754 | switch (constantDescription.Rows) |
| 755 | { |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 756 | case 2: return new Uniform(GL_FLOAT_MAT2, name, 2 * 2 * sizeof(GLfloat) * constantDescription.Elements); |
| 757 | case 3: return new Uniform(GL_FLOAT_MAT3, name, 3 * 3 * sizeof(GLfloat) * constantDescription.Elements); |
| 758 | case 4: return new Uniform(GL_FLOAT_MAT4, name, 4 * 4 * sizeof(GLfloat) * constantDescription.Elements); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 759 | default: UNREACHABLE(); |
| 760 | } |
| 761 | break; |
| 762 | default: UNREACHABLE(); |
| 763 | } |
| 764 | } |
| 765 | else UNREACHABLE(); |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 766 | |
| 767 | return 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | bool Program::applyUniform1fv(GLint location, GLsizei count, const GLfloat *v) |
| 771 | { |
| 772 | D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 773 | D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 774 | IDirect3DDevice9 *device = getDevice(); |
| 775 | |
| 776 | if (constantPS) |
| 777 | { |
| 778 | mConstantTablePS->SetFloatArray(device, constantPS, v, count); |
| 779 | } |
| 780 | |
| 781 | if (constantVS) |
| 782 | { |
| 783 | mConstantTableVS->SetFloatArray(device, constantVS, v, count); |
| 784 | } |
| 785 | |
| 786 | return true; |
| 787 | } |
| 788 | |
| 789 | bool Program::applyUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 790 | { |
| 791 | D3DXVECTOR4 *vector = new D3DXVECTOR4[count]; |
| 792 | |
| 793 | for (int i = 0; i < count; i++) |
| 794 | { |
| 795 | vector[i] = D3DXVECTOR4(v[0], v[1], 0, 0); |
| 796 | |
| 797 | v += 2; |
| 798 | } |
| 799 | |
| 800 | D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 801 | D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 802 | IDirect3DDevice9 *device = getDevice(); |
| 803 | |
| 804 | if (constantPS) |
| 805 | { |
| 806 | mConstantTablePS->SetVectorArray(device, constantPS, vector, count); |
| 807 | } |
| 808 | |
| 809 | if (constantVS) |
| 810 | { |
| 811 | mConstantTableVS->SetVectorArray(device, constantVS, vector, count); |
| 812 | } |
| 813 | |
| 814 | delete[] vector; |
| 815 | |
| 816 | return true; |
| 817 | } |
| 818 | |
| 819 | bool Program::applyUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 820 | { |
| 821 | D3DXVECTOR4 *vector = new D3DXVECTOR4[count]; |
| 822 | |
| 823 | for (int i = 0; i < count; i++) |
| 824 | { |
| 825 | vector[i] = D3DXVECTOR4(v[0], v[1], v[2], 0); |
| 826 | |
| 827 | v += 3; |
| 828 | } |
| 829 | |
| 830 | D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 831 | D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 832 | IDirect3DDevice9 *device = getDevice(); |
| 833 | |
| 834 | if (constantPS) |
| 835 | { |
| 836 | mConstantTablePS->SetVectorArray(device, constantPS, vector, count); |
| 837 | } |
| 838 | |
| 839 | if (constantVS) |
| 840 | { |
| 841 | mConstantTableVS->SetVectorArray(device, constantVS, vector, count); |
| 842 | } |
| 843 | |
| 844 | delete[] vector; |
| 845 | |
| 846 | return true; |
| 847 | } |
| 848 | |
| 849 | bool Program::applyUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 850 | { |
| 851 | D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 852 | D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 853 | IDirect3DDevice9 *device = getDevice(); |
| 854 | |
| 855 | if (constantPS) |
| 856 | { |
| 857 | mConstantTablePS->SetVectorArray(device, constantPS, (D3DXVECTOR4*)v, count); |
| 858 | } |
| 859 | |
| 860 | if (constantVS) |
| 861 | { |
| 862 | mConstantTableVS->SetVectorArray(device, constantVS, (D3DXVECTOR4*)v, count); |
| 863 | } |
| 864 | |
| 865 | return true; |
| 866 | } |
| 867 | |
| 868 | bool Program::applyUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value) |
| 869 | { |
| 870 | D3DXMATRIX *matrix = new D3DXMATRIX[count]; |
| 871 | |
| 872 | for (int i = 0; i < count; i++) |
| 873 | { |
| 874 | matrix[i] = D3DXMATRIX(value[0], value[2], 0, 0, |
| 875 | value[1], value[3], 0, 0, |
| 876 | 0, 0, 1, 0, |
| 877 | 0, 0, 0, 1); |
| 878 | |
| 879 | value += 4; |
| 880 | } |
| 881 | |
| 882 | D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 883 | D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 884 | IDirect3DDevice9 *device = getDevice(); |
| 885 | |
| 886 | if (constantPS) |
| 887 | { |
daniel@transgaming.com | 279e38a | 2010-04-03 20:56:13 +0000 | [diff] [blame] | 888 | mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | if (constantVS) |
| 892 | { |
daniel@transgaming.com | 279e38a | 2010-04-03 20:56:13 +0000 | [diff] [blame] | 893 | mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | delete[] matrix; |
| 897 | |
| 898 | return true; |
| 899 | } |
| 900 | |
| 901 | bool Program::applyUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value) |
| 902 | { |
| 903 | D3DXMATRIX *matrix = new D3DXMATRIX[count]; |
| 904 | |
| 905 | for (int i = 0; i < count; i++) |
| 906 | { |
| 907 | matrix[i] = D3DXMATRIX(value[0], value[3], value[6], 0, |
| 908 | value[1], value[4], value[7], 0, |
| 909 | value[2], value[5], value[8], 0, |
| 910 | 0, 0, 0, 1); |
| 911 | |
| 912 | value += 9; |
| 913 | } |
| 914 | |
| 915 | D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 916 | D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 917 | IDirect3DDevice9 *device = getDevice(); |
| 918 | |
| 919 | if (constantPS) |
| 920 | { |
daniel@transgaming.com | 279e38a | 2010-04-03 20:56:13 +0000 | [diff] [blame] | 921 | mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | if (constantVS) |
| 925 | { |
daniel@transgaming.com | 279e38a | 2010-04-03 20:56:13 +0000 | [diff] [blame] | 926 | mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | delete[] matrix; |
| 930 | |
| 931 | return true; |
| 932 | } |
| 933 | |
| 934 | bool Program::applyUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value) |
| 935 | { |
| 936 | D3DXMATRIX *matrix = new D3DXMATRIX[count]; |
| 937 | |
| 938 | for (int i = 0; i < count; i++) |
| 939 | { |
| 940 | matrix[i] = D3DXMATRIX(value[0], value[4], value[8], value[12], |
| 941 | value[1], value[5], value[9], value[13], |
| 942 | value[2], value[6], value[10], value[14], |
| 943 | value[3], value[7], value[11], value[15]); |
| 944 | |
| 945 | value += 16; |
| 946 | } |
| 947 | |
| 948 | D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 949 | D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 950 | IDirect3DDevice9 *device = getDevice(); |
| 951 | |
| 952 | if (constantPS) |
| 953 | { |
daniel@transgaming.com | 279e38a | 2010-04-03 20:56:13 +0000 | [diff] [blame] | 954 | mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | if (constantVS) |
| 958 | { |
daniel@transgaming.com | 279e38a | 2010-04-03 20:56:13 +0000 | [diff] [blame] | 959 | mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | delete[] matrix; |
| 963 | |
| 964 | return true; |
| 965 | } |
| 966 | |
| 967 | bool Program::applyUniform1iv(GLint location, GLsizei count, const GLint *v) |
| 968 | { |
| 969 | D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 970 | D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str()); |
| 971 | IDirect3DDevice9 *device = getDevice(); |
| 972 | |
| 973 | if (constantPS) |
| 974 | { |
| 975 | D3DXCONSTANT_DESC constantDescription; |
| 976 | UINT descriptionCount = 1; |
| 977 | HRESULT result = mConstantTablePS->GetConstantDesc(constantPS, &constantDescription, &descriptionCount); |
| 978 | |
daniel@transgaming.com | 2884b78 | 2010-03-08 21:30:48 +0000 | [diff] [blame] | 979 | if (FAILED(result)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 980 | { |
| 981 | return false; |
| 982 | } |
| 983 | |
| 984 | if (constantDescription.RegisterSet == D3DXRS_SAMPLER) |
| 985 | { |
| 986 | unsigned int firstIndex = mConstantTablePS->GetSamplerIndex(constantPS); |
| 987 | |
| 988 | for (unsigned int samplerIndex = firstIndex; samplerIndex < firstIndex + count; samplerIndex++) |
| 989 | { |
| 990 | GLint mappedSampler = v[0]; |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 991 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 992 | if (mappedSampler >= 0 && mappedSampler < MAX_TEXTURE_IMAGE_UNITS) |
| 993 | { |
| 994 | if (samplerIndex >= 0 && samplerIndex < MAX_TEXTURE_IMAGE_UNITS) |
| 995 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 996 | ASSERT(mSamplers[samplerIndex].active); |
| 997 | mSamplers[samplerIndex].logicalTextureUnit = mappedSampler; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 1001 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1002 | return true; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | if (constantPS) |
| 1007 | { |
| 1008 | mConstantTablePS->SetIntArray(device, constantPS, v, count); |
| 1009 | } |
| 1010 | |
| 1011 | if (constantVS) |
| 1012 | { |
| 1013 | mConstantTableVS->SetIntArray(device, constantVS, v, count); |
| 1014 | } |
| 1015 | |
| 1016 | return true; |
| 1017 | } |
| 1018 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 1019 | void Program::appendToInfoLog(const char *info) |
| 1020 | { |
| 1021 | if (!info) |
| 1022 | { |
| 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | size_t infoLength = strlen(info); |
| 1027 | |
| 1028 | if (!mInfoLog) |
| 1029 | { |
| 1030 | mInfoLog = new char[infoLength + 1]; |
| 1031 | strcpy(mInfoLog, info); |
| 1032 | } |
| 1033 | else |
| 1034 | { |
| 1035 | size_t logLength = strlen(mInfoLog); |
| 1036 | char *newLog = new char[logLength + infoLength + 1]; |
| 1037 | strcpy(newLog, mInfoLog); |
| 1038 | strcpy(newLog + logLength, info); |
| 1039 | |
| 1040 | delete[] mInfoLog; |
| 1041 | mInfoLog = newLog; |
| 1042 | } |
| 1043 | } |
| 1044 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1045 | // Returns the program object to an unlinked state, after detaching a shader, before re-linking, or at destruction |
| 1046 | void Program::unlink(bool destroy) |
| 1047 | { |
| 1048 | if (destroy) // Object being destructed |
| 1049 | { |
| 1050 | if (mFragmentShader) |
| 1051 | { |
| 1052 | mFragmentShader->detach(); |
| 1053 | mFragmentShader = NULL; |
| 1054 | } |
| 1055 | |
| 1056 | if (mVertexShader) |
| 1057 | { |
| 1058 | mVertexShader->detach(); |
| 1059 | mVertexShader = NULL; |
| 1060 | } |
| 1061 | |
| 1062 | for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++) |
| 1063 | { |
| 1064 | delete[] mAttributeName[index]; |
| 1065 | mAttributeName[index] = NULL; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | if (mPixelExecutable) |
| 1070 | { |
| 1071 | mPixelExecutable->Release(); |
| 1072 | mPixelExecutable = NULL; |
| 1073 | } |
| 1074 | |
| 1075 | if (mVertexExecutable) |
| 1076 | { |
| 1077 | mVertexExecutable->Release(); |
| 1078 | mVertexExecutable = NULL; |
| 1079 | } |
| 1080 | |
| 1081 | if (mConstantTablePS) |
| 1082 | { |
| 1083 | mConstantTablePS->Release(); |
| 1084 | mConstantTablePS = NULL; |
| 1085 | } |
| 1086 | |
| 1087 | if (mConstantTableVS) |
| 1088 | { |
| 1089 | mConstantTableVS->Release(); |
| 1090 | mConstantTableVS = NULL; |
| 1091 | } |
| 1092 | |
| 1093 | for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++) |
| 1094 | { |
| 1095 | mInputMapping[index] = 0; |
| 1096 | } |
| 1097 | |
| 1098 | for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++) |
| 1099 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 1100 | mSamplers[index].active = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | while (!mUniforms.empty()) |
| 1104 | { |
| 1105 | delete mUniforms.back(); |
| 1106 | mUniforms.pop_back(); |
| 1107 | } |
| 1108 | |
daniel@transgaming.com | 0e3358a | 2010-04-05 20:32:42 +0000 | [diff] [blame^] | 1109 | delete[] mPixelHLSL; |
| 1110 | mPixelHLSL = NULL; |
| 1111 | |
| 1112 | delete[] mVertexHLSL; |
| 1113 | mVertexHLSL = NULL; |
| 1114 | |
| 1115 | delete[] mInfoLog; |
| 1116 | mInfoLog = NULL; |
| 1117 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1118 | mLinked = false; |
| 1119 | } |
| 1120 | |
| 1121 | bool Program::isLinked() |
| 1122 | { |
| 1123 | return mLinked; |
| 1124 | } |
| 1125 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 1126 | int Program::getInfoLogLength() const |
| 1127 | { |
| 1128 | if (!mInfoLog) |
| 1129 | { |
| 1130 | return 0; |
| 1131 | } |
| 1132 | else |
| 1133 | { |
| 1134 | return strlen(mInfoLog) + 1; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) |
| 1139 | { |
| 1140 | int index = 0; |
| 1141 | |
| 1142 | if (mInfoLog) |
| 1143 | { |
| 1144 | while (index < bufSize - 1 && index < (int)strlen(mInfoLog)) |
| 1145 | { |
| 1146 | infoLog[index] = mInfoLog[index]; |
| 1147 | index++; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | if (bufSize) |
| 1152 | { |
| 1153 | infoLog[index] = '\0'; |
| 1154 | } |
| 1155 | |
| 1156 | if (length) |
| 1157 | { |
| 1158 | *length = index; |
| 1159 | } |
| 1160 | } |
| 1161 | |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 1162 | void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) |
| 1163 | { |
| 1164 | int total = 0; |
| 1165 | |
| 1166 | if (mVertexShader) |
| 1167 | { |
| 1168 | if (total < maxCount) |
| 1169 | { |
| 1170 | shaders[total] = mVertexShader->getHandle(); |
| 1171 | } |
| 1172 | |
| 1173 | total++; |
| 1174 | } |
| 1175 | |
| 1176 | if (mFragmentShader) |
| 1177 | { |
| 1178 | if (total < maxCount) |
| 1179 | { |
| 1180 | shaders[total] = mFragmentShader->getHandle(); |
| 1181 | } |
| 1182 | |
| 1183 | total++; |
| 1184 | } |
| 1185 | |
| 1186 | if (count) |
| 1187 | { |
| 1188 | *count = total; |
| 1189 | } |
| 1190 | } |
| 1191 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1192 | void Program::flagForDeletion() |
| 1193 | { |
| 1194 | mDeleteStatus = true; |
| 1195 | } |
| 1196 | |
| 1197 | bool Program::isFlaggedForDeletion() const |
| 1198 | { |
| 1199 | return mDeleteStatus; |
| 1200 | } |
| 1201 | } |