blob: fb4bd089e4b70155a166d2e87cef1dd2326ab330 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
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.orgea0e1af2010-03-22 19:33:14 +000014#include "common/debug.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000015
16namespace gl
17{
daniel@transgaming.com0361b922010-03-28 19:36:15 +000018Uniform::Uniform(GLenum type, const std::string &name, unsigned int bytes) : type(type), name(name), bytes(bytes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019{
20 this->data = new unsigned char[bytes];
21 memset(this->data, 0, bytes);
22}
23
24Uniform::~Uniform()
25{
26 delete[] data;
27}
28
29Program::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.com0e3358a2010-04-05 20:32:42 +000044 mPixelHLSL = NULL;
45 mVertexHLSL = NULL;
daniel@transgaming.comcba50572010-03-28 19:36:09 +000046 mInfoLog = NULL;
47
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000048 unlink();
49
50 mDeleteStatus = false;
51}
52
53Program::~Program()
54{
55 unlink(true);
56}
57
58bool 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
85bool 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.comcba50572010-03-28 19:36:09 +0000114int Program::getAttachedShadersCount() const
115{
116 return (mVertexShader ? 1 : 0) + (mFragmentShader ? 1 : 0);
117}
118
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119IDirect3DPixelShader9 *Program::getPixelShader()
120{
121 return mPixelExecutable;
122}
123
124IDirect3DVertexShader9 *Program::getVertexShader()
125{
126 return mVertexExecutable;
127}
128
129void 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
139GLuint 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
152bool 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
162int 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
174GLint Program::getSamplerMapping(unsigned int samplerIndex)
175{
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000176 assert(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0]));
177
178 if (mSamplers[samplerIndex].active)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000179 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000180 return mSamplers[samplerIndex].logicalTextureUnit;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181 }
182
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000183 return -1;
184}
185
186SamplerType 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.com4f39fd92010-03-08 20:26:45 +0000192}
193
194GLint 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
207bool 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.comf4a0c8e2010-04-13 03:26:01 +0000214 if (mUniforms[location]->type == GL_FLOAT)
215 {
216 int arraySize = mUniforms[location]->bytes / sizeof(GLfloat);
217
218 if (arraySize == 1 && count > 1)
219 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
220
221 count = std::min(arraySize, count);
222
223 memcpy(mUniforms[location]->data, v, sizeof(GLfloat) * count);
224 }
225 else if (mUniforms[location]->type == GL_BOOL)
226 {
227 int arraySize = mUniforms[location]->bytes / sizeof(GLboolean);
228
229 if (arraySize == 1 && count > 1)
230 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
231
232 count = std::min(arraySize, count);
233 GLboolean *boolParams = new GLboolean[count];
234
235 for (int i = 0; i < count; ++i)
236 {
237 if (v[i] == 0.0f)
238 {
239 boolParams[i] = GL_FALSE;
240 }
241 else
242 {
243 boolParams[i] = GL_TRUE;
244 }
245 }
246
247 memcpy(mUniforms[location]->data, boolParams, sizeof(GLboolean) * count);
248
249 delete [] boolParams;
250 }
251 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252 {
253 return false;
254 }
255
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256 return true;
257}
258
259bool Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
260{
261 if (location < 0 || location >= (int)mUniforms.size())
262 {
263 return false;
264 }
265
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +0000266 if (mUniforms[location]->type == GL_FLOAT_VEC2)
267 {
268 int arraySize = mUniforms[location]->bytes / sizeof(GLfloat) / 2;
269
270 if (arraySize == 1 && count > 1)
271 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
272
273 count = std::min(arraySize, count);
274
275 memcpy(mUniforms[location]->data, v, 2 * sizeof(GLfloat) * count);
276 }
277 else if (mUniforms[location]->type == GL_BOOL_VEC2)
278 {
279 int arraySize = mUniforms[location]->bytes / sizeof(GLboolean) / 2;
280
281 if (arraySize == 1 && count > 1)
282 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
283
284 count = std::min(arraySize, count);
285 GLboolean *boolParams = new GLboolean[count * 2];
286
287 for (int i = 0; i < count * 2; ++i)
288 {
289 if (v[i] == 0.0f)
290 {
291 boolParams[i] = GL_FALSE;
292 }
293 else
294 {
295 boolParams[i] = GL_TRUE;
296 }
297 }
298
299 memcpy(mUniforms[location]->data, boolParams, 2 * sizeof(GLboolean) * count);
300
301 delete [] boolParams;
302 }
303 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304 {
305 return false;
306 }
307
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308 return true;
309}
310
311bool Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
312{
313 if (location < 0 || location >= (int)mUniforms.size())
314 {
315 return false;
316 }
317
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +0000318 if (mUniforms[location]->type == GL_FLOAT_VEC3)
319 {
320 int arraySize = mUniforms[location]->bytes / sizeof(GLfloat) / 3;
321
322 if (arraySize == 1 && count > 1)
323 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
324
325 count = std::min(arraySize, count);
326
327 memcpy(mUniforms[location]->data, v, 3 * sizeof(GLfloat) * count);
328 }
329 else if (mUniforms[location]->type == GL_BOOL_VEC3)
330 {
331 int arraySize = mUniforms[location]->bytes / sizeof(GLboolean) / 3;
332
333 if (arraySize == 1 && count > 1)
334 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
335
336 count = std::min(arraySize, count);
337 GLboolean *boolParams = new GLboolean[count * 3];
338
339 for (int i = 0; i < count * 3; ++i)
340 {
341 if (v[i] == 0.0f)
342 {
343 boolParams[i] = GL_FALSE;
344 }
345 else
346 {
347 boolParams[i] = GL_TRUE;
348 }
349 }
350
351 memcpy(mUniforms[location]->data, boolParams, 3 * sizeof(GLboolean) * count);
352
353 delete [] boolParams;
354 }
355 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356 {
357 return false;
358 }
359
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360 return true;
361}
362
363bool Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
364{
365 if (location < 0 || location >= (int)mUniforms.size())
366 {
367 return false;
368 }
369
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +0000370 if (mUniforms[location]->type == GL_FLOAT_VEC4)
371 {
372 int arraySize = mUniforms[location]->bytes / sizeof(GLfloat) / 4;
373
374 if (arraySize == 1 && count > 1)
375 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
376
377 count = std::min(arraySize, count);
378
379 memcpy(mUniforms[location]->data, v, 4 * sizeof(GLfloat) * count);
380 }
381 else if (mUniforms[location]->type == GL_BOOL_VEC4)
382 {
383 int arraySize = mUniforms[location]->bytes / sizeof(GLboolean) / 4;
384
385 if (arraySize == 1 && count > 1)
386 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
387
388 count = std::min(arraySize, count);
389 GLboolean *boolParams = new GLboolean[count * 4];
390
391 for (int i = 0; i < count * 4; ++i)
392 {
393 if (v[i] == 0.0f)
394 {
395 boolParams[i] = GL_FALSE;
396 }
397 else
398 {
399 boolParams[i] = GL_TRUE;
400 }
401 }
402
403 memcpy(mUniforms[location]->data, boolParams, 4 * sizeof(GLboolean) * count);
404
405 delete [] boolParams;
406 }
407 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408 {
409 return false;
410 }
411
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412 return true;
413}
414
415bool Program::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
416{
417 if (location < 0 || location >= (int)mUniforms.size())
418 {
419 return false;
420 }
421
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000422 if (mUniforms[location]->type != GL_FLOAT_MAT2 || mUniforms[location]->bytes < 4 * sizeof(GLfloat) * count)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423 {
424 return false;
425 }
426
427 memcpy(mUniforms[location]->data, value, 4 * sizeof(GLfloat) * count);
428
429 return true;
430}
431
432bool Program::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
433{
434 if (location < 0 || location >= (int)mUniforms.size())
435 {
436 return false;
437 }
438
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000439 if (mUniforms[location]->type != GL_FLOAT_MAT3 || mUniforms[location]->bytes < 9 * sizeof(GLfloat) * count)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440 {
441 return false;
442 }
443
444 memcpy(mUniforms[location]->data, value, 9 * sizeof(GLfloat) * count);
445
446 return true;
447}
448
449bool Program::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
450{
451 if (location < 0 || location >= (int)mUniforms.size())
452 {
453 return false;
454 }
455
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000456 if (mUniforms[location]->type != GL_FLOAT_MAT4 || mUniforms[location]->bytes < 16 * sizeof(GLfloat) * count)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457 {
458 return false;
459 }
460
461 memcpy(mUniforms[location]->data, value, 16 * sizeof(GLfloat) * count);
462
463 return true;
464}
465
466bool Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
467{
468 if (location < 0 || location >= (int)mUniforms.size())
469 {
470 return false;
471 }
472
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000473 if (mUniforms[location]->type != GL_INT || mUniforms[location]->bytes < sizeof(GLint) * count)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474 {
475 return false;
476 }
477
478 memcpy(mUniforms[location]->data, v, sizeof(GLint) * count);
479
480 return true;
481}
482
483// Applies all the uniforms set for this program object to the Direct3D 9 device
484void Program::applyUniforms()
485{
486 for (unsigned int location = 0; location < mUniforms.size(); location++)
487 {
488 int bytes = mUniforms[location]->bytes;
489 GLfloat *f = (GLfloat*)mUniforms[location]->data;
490 GLint *i = (GLint*)mUniforms[location]->data;
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +0000491 GLboolean *b = (GLboolean*)mUniforms[location]->data;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000492
493 switch (mUniforms[location]->type)
494 {
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +0000495 case GL_BOOL: applyUniform1bv(location, bytes / sizeof(GLboolean), b); break;
496 case GL_BOOL_VEC2: applyUniform2bv(location, bytes / 2 / sizeof(GLboolean), b); break;
497 case GL_BOOL_VEC3: applyUniform3bv(location, bytes / 3 / sizeof(GLboolean), b); break;
498 case GL_BOOL_VEC4: applyUniform4bv(location, bytes / 4 / sizeof(GLboolean), b); break;
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000499 case GL_FLOAT: applyUniform1fv(location, bytes / sizeof(GLfloat), f); break;
500 case GL_FLOAT_VEC2: applyUniform2fv(location, bytes / 2 / sizeof(GLfloat), f); break;
501 case GL_FLOAT_VEC3: applyUniform3fv(location, bytes / 3 / sizeof(GLfloat), f); break;
502 case GL_FLOAT_VEC4: applyUniform4fv(location, bytes / 4 / sizeof(GLfloat), f); break;
503 case GL_FLOAT_MAT2: applyUniformMatrix2fv(location, bytes / 4 / sizeof(GLfloat), f); break;
504 case GL_FLOAT_MAT3: applyUniformMatrix3fv(location, bytes / 9 / sizeof(GLfloat), f); break;
505 case GL_FLOAT_MAT4: applyUniformMatrix4fv(location, bytes / 16 / sizeof(GLfloat), f); break;
506 case GL_INT: applyUniform1iv(location, bytes / sizeof(GLint), i); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507 default:
508 UNIMPLEMENTED(); // FIXME
509 UNREACHABLE();
510 }
511 }
512}
513
514// Compiles the HLSL code of the attached shaders into executable binaries
515ID3DXBuffer *Program::compileToBinary(const char *hlsl, const char *profile, ID3DXConstantTable **constantTable)
516{
517 if (!hlsl)
518 {
519 return NULL;
520 }
521
522 ID3DXBuffer *binary = NULL;
523 ID3DXBuffer *errorMessage = NULL;
524
daniel@transgaming.comcbbca002010-04-01 13:39:32 +0000525 HRESULT result = D3DXCompileShader(hlsl, (UINT)strlen(hlsl), NULL, 0, "main", profile, 0, &binary, &errorMessage, constantTable);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526
527 if (SUCCEEDED(result))
528 {
529 return binary;
530 }
531
532 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
533 {
534 return error(GL_OUT_OF_MEMORY, (ID3DXBuffer*)NULL);
535 }
536
537 if (errorMessage)
538 {
539 const char *message = (const char*)errorMessage->GetBufferPointer();
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000540
daniel@transgaming.com0599dc62010-03-21 04:31:36 +0000541 TRACE("\n%s", hlsl);
542 TRACE("\n%s", message);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000543 }
544
545 return NULL;
546}
547
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000548void Program::parseVaryings(const char *structure, char *hlsl, VaryingArray &varyings)
549{
550 char *input = strstr(hlsl, structure);
551 input += strlen(structure);
552
553 while (input && *input != '}')
554 {
555 char varyingType[256];
556 char varyingName[256];
557 unsigned int semanticIndex;
558 int matches = sscanf(input, " %s %s : TEXCOORD%d;", varyingType, varyingName, &semanticIndex);
559
560 if (matches == 3)
561 {
562 ASSERT(semanticIndex <= 9); // Single character
563
564 varyings.push_back(Varying(varyingName, input));
565 }
566
567 input = strstr(input, ";");
568 input += 2;
569 }
570}
571
572bool Program::linkVaryings()
573{
574 if (!mPixelHLSL || !mVertexHLSL)
575 {
576 return false;
577 }
578
579 VaryingArray vertexVaryings;
580 VaryingArray pixelVaryings;
581
582 parseVaryings("struct VS_OUTPUT\n{\n", mVertexHLSL, vertexVaryings);
583 parseVaryings("struct PS_INPUT\n{\n", mPixelHLSL, pixelVaryings);
584
585 for (unsigned int out = 0; out < vertexVaryings.size(); out++)
586 {
587 unsigned int in;
588 for (in = 0; in < pixelVaryings.size(); in++)
589 {
590 if (vertexVaryings[out].name == pixelVaryings[in].name)
591 {
592 pixelVaryings[in].link = out;
593 vertexVaryings[out].link = in;
594
595 break;
596 }
597 }
598
599 if (in != pixelVaryings.size())
600 {
601 // FIXME: Verify matching type and qualifiers
602
603 char *outputSemantic = strstr(vertexVaryings[out].declaration, " : TEXCOORD");
604 char *inputSemantic = strstr(pixelVaryings[in].declaration, " : TEXCOORD");
605 outputSemantic[11] = inputSemantic[11];
606 }
607 else
608 {
609 // Comment out the declaration and output assignment
610 vertexVaryings[out].declaration[0] = '/';
611 vertexVaryings[out].declaration[1] = '/';
612
613 char outputString[256];
614 sprintf(outputString, " output.%s = ", vertexVaryings[out].name.c_str());
615 char *varyingOutput = strstr(mVertexHLSL, outputString);
616
617 varyingOutput[0] = '/';
618 varyingOutput[1] = '/';
619 }
620 }
621
622 // Verify that each pixel varying has been linked to a vertex varying
623 for (unsigned int in = 0; in < pixelVaryings.size(); in++)
624 {
625 if (pixelVaryings[in].link < 0)
626 {
627 return false;
628 }
629 }
630
631 return true;
632}
633
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634// Links the HLSL code of the vertex and pixel shader by matching up their varyings,
635// compiling them into binaries, determining the attribute mappings, and collecting
636// a list of uniforms
637void Program::link()
638{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639 unlink();
640
641 if (!mFragmentShader || !mFragmentShader->isCompiled())
642 {
643 return;
644 }
645
646 if (!mVertexShader || !mVertexShader->isCompiled())
647 {
648 return;
649 }
650
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000651 Context *context = getContext();
652 const char *vertexProfile = context->getVertexShaderProfile();
653 const char *pixelProfile = context->getPixelShaderProfile();
daniel@transgaming.comdebe2592010-03-24 09:44:08 +0000654
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000655 const char *ps = mFragmentShader->getHLSL();
656 const char *vs = mVertexShader->getHLSL();
657
658 mPixelHLSL = new char[strlen(ps) + 1];
659 strcpy(mPixelHLSL, ps);
660 mVertexHLSL = new char[strlen(vs) + 1];
661 strcpy(mVertexHLSL, vs);
662
663 if (!linkVaryings())
664 {
665 return;
666 }
667
668 ID3DXBuffer *vertexBinary = compileToBinary(mVertexHLSL, vertexProfile, &mConstantTableVS);
669 ID3DXBuffer *pixelBinary = compileToBinary(mPixelHLSL, pixelProfile, &mConstantTablePS);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670
671 if (vertexBinary && pixelBinary)
672 {
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000673 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674 HRESULT vertexResult = device->CreateVertexShader((DWORD*)vertexBinary->GetBufferPointer(), &mVertexExecutable);
675 HRESULT pixelResult = device->CreatePixelShader((DWORD*)pixelBinary->GetBufferPointer(), &mPixelExecutable);
676
677 if (vertexResult == D3DERR_OUTOFVIDEOMEMORY || vertexResult == E_OUTOFMEMORY || pixelResult == D3DERR_OUTOFVIDEOMEMORY || pixelResult == E_OUTOFMEMORY)
678 {
679 return error(GL_OUT_OF_MEMORY);
680 }
681
682 ASSERT(SUCCEEDED(vertexResult) && SUCCEEDED(pixelResult));
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000683
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684 vertexBinary->Release();
685 pixelBinary->Release();
686 vertexBinary = NULL;
687 pixelBinary = NULL;
688
689 if (mVertexExecutable && mPixelExecutable)
690 {
691 if (!linkAttributes())
692 {
693 return;
694 }
695
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000696 for (int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
697 {
698 mSamplers[i].active = false;
699 }
700
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000701 if (!linkUniforms(mConstantTablePS))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000702 {
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000703 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704 }
705
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000706 if (!linkUniforms(mConstantTableVS))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000707 {
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000708 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000709 }
710
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000711 mLinked = true; // Success
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000712 }
713 }
714}
715
716// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
717bool Program::linkAttributes()
718{
719 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
720 {
721 const char *name = mVertexShader->getAttributeName(attributeIndex);
722
723 if (name)
724 {
725 GLuint location = getAttributeLocation(name);
726
727 if (location == -1) // Not set by glBindAttribLocation
728 {
729 int availableIndex = 0;
730
731 while (availableIndex < MAX_VERTEX_ATTRIBS && mAttributeName[availableIndex] && mVertexShader->isActiveAttribute(mAttributeName[availableIndex]))
732 {
733 availableIndex++;
734 }
735
736 if (availableIndex == MAX_VERTEX_ATTRIBS)
737 {
738 return false; // Fail to link
739 }
740
741 delete[] mAttributeName[availableIndex];
742 mAttributeName[availableIndex] = new char[strlen(name) + 1]; // FIXME: Check allocation
743 strcpy(mAttributeName[availableIndex], name);
744 }
745 }
746 }
747
748 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
749 {
750 mInputMapping[attributeIndex] = mVertexShader->getInputMapping(mAttributeName[attributeIndex]);
751 }
752
753 return true;
754}
755
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000756bool Program::linkUniforms(ID3DXConstantTable *constantTable)
757{
758 D3DXCONSTANTTABLE_DESC constantTableDescription;
759 D3DXCONSTANT_DESC constantDescription;
760 UINT descriptionCount = 1;
761
762 constantTable->GetDesc(&constantTableDescription);
763
764 for (unsigned int constantIndex = 0; constantIndex < constantTableDescription.Constants; constantIndex++)
765 {
766 D3DXHANDLE constantHandle = constantTable->GetConstant(0, constantIndex);
767 constantTable->GetConstantDesc(constantHandle, &constantDescription, &descriptionCount);
768
769 if (!defineUniform(constantHandle, constantDescription))
770 {
771 return false;
772 }
773 }
774
775 return true;
776}
777
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778// Adds the description of a constant found in the binary shader to the list of uniforms
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000779// Returns true if succesful (uniform not already defined)
780bool Program::defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT_DESC &constantDescription, std::string name)
781{
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000782 if (constantDescription.RegisterSet == D3DXRS_SAMPLER)
783 {
784 unsigned int samplerIndex = constantDescription.RegisterIndex;
785
786 assert(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0]));
787
788 mSamplers[samplerIndex].active = true;
789 mSamplers[samplerIndex].type = (constantDescription.Type == D3DXPT_SAMPLERCUBE) ? SAMPLER_CUBE : SAMPLER_2D;
790 mSamplers[samplerIndex].logicalTextureUnit = 0;
791 }
792
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000793 switch(constantDescription.Class)
794 {
795 case D3DXPC_STRUCT:
796 {
797 for (unsigned int field = 0; field < constantDescription.StructMembers; field++)
798 {
799 D3DXHANDLE fieldHandle = mConstantTablePS->GetConstant(constantHandle, field);
800
801 D3DXCONSTANT_DESC fieldDescription;
802 UINT descriptionCount = 1;
803
804 mConstantTablePS->GetConstantDesc(fieldHandle, &fieldDescription, &descriptionCount);
805
806 if (!defineUniform(fieldHandle, fieldDescription, name + constantDescription.Name + "."))
807 {
808 return false;
809 }
810 }
811
812 return true;
813 }
814 case D3DXPC_SCALAR:
815 case D3DXPC_VECTOR:
816 case D3DXPC_MATRIX_COLUMNS:
817 case D3DXPC_OBJECT:
818 return defineUniform(constantDescription, name + constantDescription.Name);
819 default:
820 UNREACHABLE();
821 return false;
822 }
823}
824
825bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name)
826{
827 Uniform *uniform = createUniform(constantDescription, name);
828
829 if(!uniform)
830 {
831 return false;
832 }
833
834 // Check if already defined
835 GLint location = getUniformLocation(name.c_str());
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000836 GLenum type = uniform->type;
daniel@transgaming.comc7d8a932010-03-16 06:16:45 +0000837
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000838 if (location >= 0)
839 {
840 delete uniform;
841
842 if (mUniforms[location]->type != type)
843 {
844 return false;
845 }
846 else
847 {
848 return true;
849 }
850 }
851
852 mUniforms.push_back(uniform);
853
854 return true;
855}
856
857Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858{
859 if (constantDescription.Rows == 1) // Vectors and scalars
860 {
861 switch (constantDescription.Type)
862 {
863 case D3DXPT_SAMPLER2D:
864 case D3DXPT_SAMPLERCUBE:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865 switch (constantDescription.Columns)
866 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000867 case 1: return new Uniform(GL_INT, name, 1 * sizeof(GLint) * constantDescription.Elements);
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +0000868 default: UNREACHABLE();
869 }
870 break;
871 case D3DXPT_BOOL:
872 switch (constantDescription.Columns)
873 {
874 case 1: return new Uniform(GL_BOOL, name, 1 * sizeof(GLboolean) * constantDescription.Elements);
875 case 2: return new Uniform(GL_BOOL_VEC2, name, 2 * sizeof(GLboolean) * constantDescription.Elements);
876 case 3: return new Uniform(GL_BOOL_VEC3, name, 3 * sizeof(GLboolean) * constantDescription.Elements);
877 case 4: return new Uniform(GL_BOOL_VEC4, name, 4 * sizeof(GLboolean) * constantDescription.Elements);
878 default: UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879 }
880 break;
881 case D3DXPT_FLOAT:
882 switch (constantDescription.Columns)
883 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000884 case 1: return new Uniform(GL_FLOAT, name, 1 * sizeof(GLfloat) * constantDescription.Elements);
885 case 2: return new Uniform(GL_FLOAT_VEC2, name, 2 * sizeof(GLfloat) * constantDescription.Elements);
886 case 3: return new Uniform(GL_FLOAT_VEC3, name, 3 * sizeof(GLfloat) * constantDescription.Elements);
887 case 4: return new Uniform(GL_FLOAT_VEC4, name, 4 * sizeof(GLfloat) * constantDescription.Elements);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888 default: UNREACHABLE();
889 }
890 break;
891 default:
892 UNIMPLEMENTED(); // FIXME
893 UNREACHABLE();
894 }
895 }
896 else if (constantDescription.Rows == constantDescription.Columns) // Square matrices
897 {
898 switch (constantDescription.Type)
899 {
900 case D3DXPT_FLOAT:
901 switch (constantDescription.Rows)
902 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000903 case 2: return new Uniform(GL_FLOAT_MAT2, name, 2 * 2 * sizeof(GLfloat) * constantDescription.Elements);
904 case 3: return new Uniform(GL_FLOAT_MAT3, name, 3 * 3 * sizeof(GLfloat) * constantDescription.Elements);
905 case 4: return new Uniform(GL_FLOAT_MAT4, name, 4 * 4 * sizeof(GLfloat) * constantDescription.Elements);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906 default: UNREACHABLE();
907 }
908 break;
909 default: UNREACHABLE();
910 }
911 }
912 else UNREACHABLE();
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000913
914 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915}
916
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +0000917bool Program::applyUniform1bv(GLint location, GLsizei count, const GLboolean *v)
918{
919 BOOL *vector = new BOOL[count];
920 for (int i = 0; i < count; i++)
921 {
922 if (v[i] == GL_FALSE)
923 vector[i] = 0;
924 else
925 vector[i] = 1;
926 }
927
928 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
929 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
930 IDirect3DDevice9 *device = getDevice();
931
932 if (constantPS)
933 {
934 mConstantTablePS->SetBoolArray(device, constantPS, vector, count);
935 }
936
937 if (constantVS)
938 {
939 mConstantTableVS->SetBoolArray(device, constantVS, vector, count);
940 }
941
942 delete [] vector;
943
944 return true;
945}
946
947bool Program::applyUniform2bv(GLint location, GLsizei count, const GLboolean *v)
948{
949 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
950
951 for (int i = 0; i < count; i++)
952 {
953 vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f),
954 (v[1] == GL_FALSE ? 0.0f : 1.0f), 0, 0);
955
956 v += 2;
957 }
958
959 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
960 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
961 IDirect3DDevice9 *device = getDevice();
962
963 if (constantPS)
964 {
965 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
966 }
967
968 if (constantVS)
969 {
970 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
971 }
972
973 delete[] vector;
974
975 return true;
976}
977
978bool Program::applyUniform3bv(GLint location, GLsizei count, const GLboolean *v)
979{
980 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
981
982 for (int i = 0; i < count; i++)
983 {
984 vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f),
985 (v[1] == GL_FALSE ? 0.0f : 1.0f),
986 (v[2] == GL_FALSE ? 0.0f : 1.0f), 0);
987
988 v += 3;
989 }
990
991 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
992 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
993 IDirect3DDevice9 *device = getDevice();
994
995 if (constantPS)
996 {
997 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
998 }
999
1000 if (constantVS)
1001 {
1002 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1003 }
1004
1005 delete[] vector;
1006
1007 return true;
1008}
1009
1010bool Program::applyUniform4bv(GLint location, GLsizei count, const GLboolean *v)
1011{
1012 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1013
1014 for (int i = 0; i < count; i++)
1015 {
1016 vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f),
1017 (v[1] == GL_FALSE ? 0.0f : 1.0f),
1018 (v[2] == GL_FALSE ? 0.0f : 1.0f),
1019 (v[3] == GL_FALSE ? 0.0f : 1.0f));
1020
1021 v += 3;
1022 }
1023
1024 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1025 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1026 IDirect3DDevice9 *device = getDevice();
1027
1028 if (constantPS)
1029 {
1030 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1031 }
1032
1033 if (constantVS)
1034 {
1035 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1036 }
1037
1038 delete [] vector;
1039
1040 return true;
1041}
1042
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001043bool Program::applyUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1044{
1045 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1046 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1047 IDirect3DDevice9 *device = getDevice();
1048
1049 if (constantPS)
1050 {
1051 mConstantTablePS->SetFloatArray(device, constantPS, v, count);
1052 }
1053
1054 if (constantVS)
1055 {
1056 mConstantTableVS->SetFloatArray(device, constantVS, v, count);
1057 }
1058
1059 return true;
1060}
1061
1062bool Program::applyUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1063{
1064 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1065
1066 for (int i = 0; i < count; i++)
1067 {
1068 vector[i] = D3DXVECTOR4(v[0], v[1], 0, 0);
1069
1070 v += 2;
1071 }
1072
1073 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1074 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1075 IDirect3DDevice9 *device = getDevice();
1076
1077 if (constantPS)
1078 {
1079 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1080 }
1081
1082 if (constantVS)
1083 {
1084 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1085 }
1086
1087 delete[] vector;
1088
1089 return true;
1090}
1091
1092bool Program::applyUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1093{
1094 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1095
1096 for (int i = 0; i < count; i++)
1097 {
1098 vector[i] = D3DXVECTOR4(v[0], v[1], v[2], 0);
1099
1100 v += 3;
1101 }
1102
1103 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1104 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1105 IDirect3DDevice9 *device = getDevice();
1106
1107 if (constantPS)
1108 {
1109 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1110 }
1111
1112 if (constantVS)
1113 {
1114 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1115 }
1116
1117 delete[] vector;
1118
1119 return true;
1120}
1121
1122bool Program::applyUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1123{
1124 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1125 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1126 IDirect3DDevice9 *device = getDevice();
1127
1128 if (constantPS)
1129 {
1130 mConstantTablePS->SetVectorArray(device, constantPS, (D3DXVECTOR4*)v, count);
1131 }
1132
1133 if (constantVS)
1134 {
1135 mConstantTableVS->SetVectorArray(device, constantVS, (D3DXVECTOR4*)v, count);
1136 }
1137
1138 return true;
1139}
1140
1141bool Program::applyUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
1142{
1143 D3DXMATRIX *matrix = new D3DXMATRIX[count];
1144
1145 for (int i = 0; i < count; i++)
1146 {
1147 matrix[i] = D3DXMATRIX(value[0], value[2], 0, 0,
1148 value[1], value[3], 0, 0,
1149 0, 0, 1, 0,
1150 0, 0, 0, 1);
1151
1152 value += 4;
1153 }
1154
1155 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1156 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1157 IDirect3DDevice9 *device = getDevice();
1158
1159 if (constantPS)
1160 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001161 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001162 }
1163
1164 if (constantVS)
1165 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001166 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001167 }
1168
1169 delete[] matrix;
1170
1171 return true;
1172}
1173
1174bool Program::applyUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
1175{
1176 D3DXMATRIX *matrix = new D3DXMATRIX[count];
1177
1178 for (int i = 0; i < count; i++)
1179 {
1180 matrix[i] = D3DXMATRIX(value[0], value[3], value[6], 0,
1181 value[1], value[4], value[7], 0,
1182 value[2], value[5], value[8], 0,
1183 0, 0, 0, 1);
1184
1185 value += 9;
1186 }
1187
1188 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1189 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1190 IDirect3DDevice9 *device = getDevice();
1191
1192 if (constantPS)
1193 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001194 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001195 }
1196
1197 if (constantVS)
1198 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001199 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001200 }
1201
1202 delete[] matrix;
1203
1204 return true;
1205}
1206
1207bool Program::applyUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
1208{
1209 D3DXMATRIX *matrix = new D3DXMATRIX[count];
1210
1211 for (int i = 0; i < count; i++)
1212 {
1213 matrix[i] = D3DXMATRIX(value[0], value[4], value[8], value[12],
1214 value[1], value[5], value[9], value[13],
1215 value[2], value[6], value[10], value[14],
1216 value[3], value[7], value[11], value[15]);
1217
1218 value += 16;
1219 }
1220
1221 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1222 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1223 IDirect3DDevice9 *device = getDevice();
1224
1225 if (constantPS)
1226 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001227 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001228 }
1229
1230 if (constantVS)
1231 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001232 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001233 }
1234
1235 delete[] matrix;
1236
1237 return true;
1238}
1239
1240bool Program::applyUniform1iv(GLint location, GLsizei count, const GLint *v)
1241{
1242 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1243 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1244 IDirect3DDevice9 *device = getDevice();
1245
1246 if (constantPS)
1247 {
1248 D3DXCONSTANT_DESC constantDescription;
1249 UINT descriptionCount = 1;
1250 HRESULT result = mConstantTablePS->GetConstantDesc(constantPS, &constantDescription, &descriptionCount);
1251
daniel@transgaming.com2884b782010-03-08 21:30:48 +00001252 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001253 {
1254 return false;
1255 }
1256
1257 if (constantDescription.RegisterSet == D3DXRS_SAMPLER)
1258 {
1259 unsigned int firstIndex = mConstantTablePS->GetSamplerIndex(constantPS);
1260
1261 for (unsigned int samplerIndex = firstIndex; samplerIndex < firstIndex + count; samplerIndex++)
1262 {
1263 GLint mappedSampler = v[0];
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001264
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001265 if (mappedSampler >= 0 && mappedSampler < MAX_TEXTURE_IMAGE_UNITS)
1266 {
1267 if (samplerIndex >= 0 && samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1268 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001269 ASSERT(mSamplers[samplerIndex].active);
1270 mSamplers[samplerIndex].logicalTextureUnit = mappedSampler;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001271 }
1272 }
1273 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001274
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001275 return true;
1276 }
1277 }
1278
1279 if (constantPS)
1280 {
1281 mConstantTablePS->SetIntArray(device, constantPS, v, count);
1282 }
1283
1284 if (constantVS)
1285 {
1286 mConstantTableVS->SetIntArray(device, constantVS, v, count);
1287 }
1288
1289 return true;
1290}
1291
daniel@transgaming.comcba50572010-03-28 19:36:09 +00001292void Program::appendToInfoLog(const char *info)
1293{
1294 if (!info)
1295 {
1296 return;
1297 }
1298
1299 size_t infoLength = strlen(info);
1300
1301 if (!mInfoLog)
1302 {
1303 mInfoLog = new char[infoLength + 1];
1304 strcpy(mInfoLog, info);
1305 }
1306 else
1307 {
1308 size_t logLength = strlen(mInfoLog);
1309 char *newLog = new char[logLength + infoLength + 1];
1310 strcpy(newLog, mInfoLog);
1311 strcpy(newLog + logLength, info);
1312
1313 delete[] mInfoLog;
1314 mInfoLog = newLog;
1315 }
1316}
1317
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001318// Returns the program object to an unlinked state, after detaching a shader, before re-linking, or at destruction
1319void Program::unlink(bool destroy)
1320{
1321 if (destroy) // Object being destructed
1322 {
1323 if (mFragmentShader)
1324 {
1325 mFragmentShader->detach();
1326 mFragmentShader = NULL;
1327 }
1328
1329 if (mVertexShader)
1330 {
1331 mVertexShader->detach();
1332 mVertexShader = NULL;
1333 }
1334
1335 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
1336 {
1337 delete[] mAttributeName[index];
1338 mAttributeName[index] = NULL;
1339 }
1340 }
1341
1342 if (mPixelExecutable)
1343 {
1344 mPixelExecutable->Release();
1345 mPixelExecutable = NULL;
1346 }
1347
1348 if (mVertexExecutable)
1349 {
1350 mVertexExecutable->Release();
1351 mVertexExecutable = NULL;
1352 }
1353
1354 if (mConstantTablePS)
1355 {
1356 mConstantTablePS->Release();
1357 mConstantTablePS = NULL;
1358 }
1359
1360 if (mConstantTableVS)
1361 {
1362 mConstantTableVS->Release();
1363 mConstantTableVS = NULL;
1364 }
1365
1366 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
1367 {
1368 mInputMapping[index] = 0;
1369 }
1370
1371 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
1372 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001373 mSamplers[index].active = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001374 }
1375
1376 while (!mUniforms.empty())
1377 {
1378 delete mUniforms.back();
1379 mUniforms.pop_back();
1380 }
1381
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +00001382 delete[] mPixelHLSL;
1383 mPixelHLSL = NULL;
1384
1385 delete[] mVertexHLSL;
1386 mVertexHLSL = NULL;
1387
1388 delete[] mInfoLog;
1389 mInfoLog = NULL;
1390
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001391 mLinked = false;
1392}
1393
1394bool Program::isLinked()
1395{
1396 return mLinked;
1397}
1398
daniel@transgaming.comcba50572010-03-28 19:36:09 +00001399int Program::getInfoLogLength() const
1400{
1401 if (!mInfoLog)
1402 {
1403 return 0;
1404 }
1405 else
1406 {
1407 return strlen(mInfoLog) + 1;
1408 }
1409}
1410
1411void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog)
1412{
1413 int index = 0;
1414
1415 if (mInfoLog)
1416 {
1417 while (index < bufSize - 1 && index < (int)strlen(mInfoLog))
1418 {
1419 infoLog[index] = mInfoLog[index];
1420 index++;
1421 }
1422 }
1423
1424 if (bufSize)
1425 {
1426 infoLog[index] = '\0';
1427 }
1428
1429 if (length)
1430 {
1431 *length = index;
1432 }
1433}
1434
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001435void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders)
1436{
1437 int total = 0;
1438
1439 if (mVertexShader)
1440 {
1441 if (total < maxCount)
1442 {
1443 shaders[total] = mVertexShader->getHandle();
1444 }
1445
1446 total++;
1447 }
1448
1449 if (mFragmentShader)
1450 {
1451 if (total < maxCount)
1452 {
1453 shaders[total] = mFragmentShader->getHandle();
1454 }
1455
1456 total++;
1457 }
1458
1459 if (count)
1460 {
1461 *count = total;
1462 }
1463}
1464
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001465void Program::flagForDeletion()
1466{
1467 mDeleteStatus = true;
1468}
1469
1470bool Program::isFlaggedForDeletion() const
1471{
1472 return mDeleteStatus;
1473}
1474}