blob: e886a86f35a74d4a5db54e63d8dc8a609ea58d87 [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.com9a95e2b2010-04-13 03:26:03 +0000422 if (mUniforms[location]->type != GL_FLOAT_MAT2)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423 {
424 return false;
425 }
426
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +0000427 int arraySize = mUniforms[location]->bytes / sizeof(GLfloat) / 4;
428
429 if (arraySize == 1 && count > 1)
430 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
431
432 count = std::min(arraySize, count);
433
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434 memcpy(mUniforms[location]->data, value, 4 * sizeof(GLfloat) * count);
435
436 return true;
437}
438
439bool Program::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
440{
441 if (location < 0 || location >= (int)mUniforms.size())
442 {
443 return false;
444 }
445
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +0000446 if (mUniforms[location]->type != GL_FLOAT_MAT3)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000447 {
448 return false;
449 }
450
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +0000451 int arraySize = mUniforms[location]->bytes / sizeof(GLfloat) / 9;
452
453 if (arraySize == 1 && count > 1)
454 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
455
456 count = std::min(arraySize, count);
457
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458 memcpy(mUniforms[location]->data, value, 9 * sizeof(GLfloat) * count);
459
460 return true;
461}
462
463bool Program::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
464{
465 if (location < 0 || location >= (int)mUniforms.size())
466 {
467 return false;
468 }
469
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +0000470 if (mUniforms[location]->type != GL_FLOAT_MAT4)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471 {
472 return false;
473 }
474
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +0000475 int arraySize = mUniforms[location]->bytes / sizeof(GLfloat) / 16;
476
477 if (arraySize == 1 && count > 1)
478 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
479
480 count = std::min(arraySize, count);
481
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482 memcpy(mUniforms[location]->data, value, 16 * sizeof(GLfloat) * count);
483
484 return true;
485}
486
487bool Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
488{
489 if (location < 0 || location >= (int)mUniforms.size())
490 {
491 return false;
492 }
493
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +0000494 if (mUniforms[location]->type == GL_INT)
495 {
496 int arraySize = mUniforms[location]->bytes / sizeof(GLint);
497
498 if (arraySize == 1 && count > 1)
499 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
500
501 count = std::min(arraySize, count);
502
503 memcpy(mUniforms[location]->data, v, sizeof(GLint) * count);
504 }
505 else if (mUniforms[location]->type == GL_BOOL)
506 {
507 int arraySize = mUniforms[location]->bytes / sizeof(GLboolean);
508
509 if (arraySize == 1 && count > 1)
510 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
511
512 count = std::min(arraySize, count);
513 GLboolean *boolParams = new GLboolean[count];
514
515 for (int i = 0; i < count; ++i)
516 {
517 if (v[i] == 0)
518 {
519 boolParams[i] = GL_FALSE;
520 }
521 else
522 {
523 boolParams[i] = GL_TRUE;
524 }
525 }
526
527 memcpy(mUniforms[location]->data, boolParams, sizeof(GLboolean) * count);
528
529 delete [] boolParams;
530 }
531 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532 {
533 return false;
534 }
535
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +0000536 return true;
537}
538
539bool Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
540{
541 if (location < 0 || location >= (int)mUniforms.size())
542 {
543 return false;
544 }
545
546 if (mUniforms[location]->type == GL_INT_VEC2)
547 {
548 int arraySize = mUniforms[location]->bytes / sizeof(GLint) / 2;
549
550 if (arraySize == 1 && count > 1)
551 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
552
553 count = std::min(arraySize, count);
554
555 memcpy(mUniforms[location]->data, v, 2 * sizeof(GLint) * count);
556 }
557 else if (mUniforms[location]->type == GL_BOOL_VEC2)
558 {
559 int arraySize = mUniforms[location]->bytes / sizeof(GLboolean) / 2;
560
561 if (arraySize == 1 && count > 1)
562 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
563
564 count = std::min(arraySize, count);
565 GLboolean *boolParams = new GLboolean[count * 2];
566
567 for (int i = 0; i < count * 2; ++i)
568 {
569 if (v[i] == 0)
570 {
571 boolParams[i] = GL_FALSE;
572 }
573 else
574 {
575 boolParams[i] = GL_TRUE;
576 }
577 }
578
579 memcpy(mUniforms[location]->data, boolParams, 2 * sizeof(GLboolean) * count);
580
581 delete [] boolParams;
582 }
583 else
584 {
585 return false;
586 }
587
588 return true;
589}
590
591bool Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
592{
593 if (location < 0 || location >= (int)mUniforms.size())
594 {
595 return false;
596 }
597
598 if (mUniforms[location]->type == GL_INT_VEC3)
599 {
600 int arraySize = mUniforms[location]->bytes / sizeof(GLint) / 3;
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, count);
606
607 memcpy(mUniforms[location]->data, v, 3 * sizeof(GLint) * count);
608 }
609 else if (mUniforms[location]->type == GL_BOOL_VEC3)
610 {
611 int arraySize = mUniforms[location]->bytes / sizeof(GLboolean) / 3;
612
613 if (arraySize == 1 && count > 1)
614 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
615
616 count = std::min(arraySize, count);
617 GLboolean *boolParams = new GLboolean[count * 3];
618
619 for (int i = 0; i < count * 3; ++i)
620 {
621 if (v[i] == 0)
622 {
623 boolParams[i] = GL_FALSE;
624 }
625 else
626 {
627 boolParams[i] = GL_TRUE;
628 }
629 }
630
631 memcpy(mUniforms[location]->data, boolParams, 3 * sizeof(GLboolean) * count);
632
633 delete [] boolParams;
634 }
635 else
636 {
637 return false;
638 }
639
640 return true;
641}
642
643bool Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
644{
645 if (location < 0 || location >= (int)mUniforms.size())
646 {
647 return false;
648 }
649
650 if (mUniforms[location]->type == GL_INT_VEC4)
651 {
652 int arraySize = mUniforms[location]->bytes / sizeof(GLint) / 4;
653
654 if (arraySize == 1 && count > 1)
655 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
656
657 count = std::min(arraySize, count);
658
659 memcpy(mUniforms[location]->data, v, 4 * sizeof(GLint) * count);
660 }
661 else if (mUniforms[location]->type == GL_BOOL_VEC4)
662 {
663 int arraySize = mUniforms[location]->bytes / sizeof(GLboolean) / 4;
664
665 if (arraySize == 1 && count > 1)
666 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
667
668 count = std::min(arraySize, count);
669 GLboolean *boolParams = new GLboolean[count * 4];
670
671 for (int i = 0; i < count * 4; ++i)
672 {
673 if (v[i] == 0)
674 {
675 boolParams[i] = GL_FALSE;
676 }
677 else
678 {
679 boolParams[i] = GL_TRUE;
680 }
681 }
682
683 memcpy(mUniforms[location]->data, boolParams, 4 * sizeof(GLboolean) * count);
684
685 delete [] boolParams;
686 }
687 else
688 {
689 return false;
690 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000691
692 return true;
693}
694
daniel@transgaming.combb3d9d02010-04-13 03:26:06 +0000695bool Program::getUniformfv(GLint location, GLfloat *params)
696{
697 if (location < 0 || location >= (int)mUniforms.size())
698 {
699 return false;
700 }
701
702 unsigned int count = 0;
703
704 switch (mUniforms[location]->type)
705 {
706 case GL_FLOAT:
707 case GL_BOOL:
708 count = 1;
709 break;
710 case GL_FLOAT_VEC2:
711 case GL_BOOL_VEC2:
712 count = 2;
713 break;
714 case GL_FLOAT_VEC3:
715 case GL_BOOL_VEC3:
716 count = 3;
717 break;
718 case GL_FLOAT_VEC4:
719 case GL_BOOL_VEC4:
720 case GL_FLOAT_MAT2:
721 count = 4;
722 break;
723 case GL_FLOAT_MAT3:
724 count = 9;
725 break;
726 case GL_FLOAT_MAT4:
727 count = 16;
728 break;
729 default:
730 return false;
731 }
732
733 if (mUniforms[location]->type == GL_BOOL || mUniforms[location]->type == GL_BOOL_VEC2 ||
734 mUniforms[location]->type == GL_BOOL_VEC3 || mUniforms[location]->type == GL_BOOL_VEC4)
735 {
736 GLboolean *boolParams = mUniforms[location]->data;
737
738 for (unsigned int i = 0; i < count; ++i)
739 {
740 if (boolParams[i] == GL_FALSE)
741 params[i] = 0.0f;
742 else
743 params[i] = 1.0f;
744 }
745 }
746 else
747 {
748 memcpy(params, mUniforms[location]->data, count * sizeof(GLfloat));
749 }
750
751 return true;
752}
753
754bool Program::getUniformiv(GLint location, GLint *params)
755{
756 if (location < 0 || location >= (int)mUniforms.size())
757 {
758 return false;
759 }
760
761 unsigned int count = 0;
762
763 switch (mUniforms[location]->type)
764 {
765 case GL_INT:
766 case GL_BOOL:
767 count = 1;
768 break;
769 case GL_INT_VEC2:
770 case GL_BOOL_VEC2:
771 count = 2;
772 break;
773 case GL_INT_VEC3:
774 case GL_BOOL_VEC3:
775 count = 3;
776 break;
777 case GL_INT_VEC4:
778 case GL_BOOL_VEC4:
779 count = 4;
780 break;
781 default:
782 return false;
783 }
784
785 if (mUniforms[location]->type == GL_BOOL || mUniforms[location]->type == GL_BOOL_VEC2 ||
786 mUniforms[location]->type == GL_BOOL_VEC3 || mUniforms[location]->type == GL_BOOL_VEC4)
787 {
788 GLboolean *boolParams = mUniforms[location]->data;
789
790 for (unsigned int i = 0; i < count; ++i)
791 {
792 if (boolParams[i] == GL_FALSE)
793 params[i] = 0;
794 else
795 params[i] = 1;
796 }
797 }
798 else
799 {
800 memcpy(params, mUniforms[location]->data, count * sizeof(GLint));
801 }
802
803 return true;
804}
805
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806// Applies all the uniforms set for this program object to the Direct3D 9 device
807void Program::applyUniforms()
808{
809 for (unsigned int location = 0; location < mUniforms.size(); location++)
810 {
811 int bytes = mUniforms[location]->bytes;
812 GLfloat *f = (GLfloat*)mUniforms[location]->data;
813 GLint *i = (GLint*)mUniforms[location]->data;
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +0000814 GLboolean *b = (GLboolean*)mUniforms[location]->data;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815
816 switch (mUniforms[location]->type)
817 {
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +0000818 case GL_BOOL: applyUniform1bv(location, bytes / sizeof(GLboolean), b); break;
819 case GL_BOOL_VEC2: applyUniform2bv(location, bytes / 2 / sizeof(GLboolean), b); break;
820 case GL_BOOL_VEC3: applyUniform3bv(location, bytes / 3 / sizeof(GLboolean), b); break;
821 case GL_BOOL_VEC4: applyUniform4bv(location, bytes / 4 / sizeof(GLboolean), b); break;
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000822 case GL_FLOAT: applyUniform1fv(location, bytes / sizeof(GLfloat), f); break;
823 case GL_FLOAT_VEC2: applyUniform2fv(location, bytes / 2 / sizeof(GLfloat), f); break;
824 case GL_FLOAT_VEC3: applyUniform3fv(location, bytes / 3 / sizeof(GLfloat), f); break;
825 case GL_FLOAT_VEC4: applyUniform4fv(location, bytes / 4 / sizeof(GLfloat), f); break;
826 case GL_FLOAT_MAT2: applyUniformMatrix2fv(location, bytes / 4 / sizeof(GLfloat), f); break;
827 case GL_FLOAT_MAT3: applyUniformMatrix3fv(location, bytes / 9 / sizeof(GLfloat), f); break;
828 case GL_FLOAT_MAT4: applyUniformMatrix4fv(location, bytes / 16 / sizeof(GLfloat), f); break;
829 case GL_INT: applyUniform1iv(location, bytes / sizeof(GLint), i); break;
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +0000830 case GL_INT_VEC2: applyUniform2iv(location, bytes / 2 / sizeof(GLint), i); break;
831 case GL_INT_VEC3: applyUniform3iv(location, bytes / 3 / sizeof(GLint), i); break;
832 case GL_INT_VEC4: applyUniform4iv(location, bytes / 4 / sizeof(GLint), i); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833 default:
834 UNIMPLEMENTED(); // FIXME
835 UNREACHABLE();
836 }
837 }
838}
839
840// Compiles the HLSL code of the attached shaders into executable binaries
841ID3DXBuffer *Program::compileToBinary(const char *hlsl, const char *profile, ID3DXConstantTable **constantTable)
842{
843 if (!hlsl)
844 {
845 return NULL;
846 }
847
848 ID3DXBuffer *binary = NULL;
849 ID3DXBuffer *errorMessage = NULL;
850
daniel@transgaming.comcbbca002010-04-01 13:39:32 +0000851 HRESULT result = D3DXCompileShader(hlsl, (UINT)strlen(hlsl), NULL, 0, "main", profile, 0, &binary, &errorMessage, constantTable);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852
853 if (SUCCEEDED(result))
854 {
855 return binary;
856 }
857
858 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
859 {
860 return error(GL_OUT_OF_MEMORY, (ID3DXBuffer*)NULL);
861 }
862
863 if (errorMessage)
864 {
865 const char *message = (const char*)errorMessage->GetBufferPointer();
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000866
daniel@transgaming.com0599dc62010-03-21 04:31:36 +0000867 TRACE("\n%s", hlsl);
868 TRACE("\n%s", message);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869 }
870
871 return NULL;
872}
873
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000874void Program::parseVaryings(const char *structure, char *hlsl, VaryingArray &varyings)
875{
876 char *input = strstr(hlsl, structure);
877 input += strlen(structure);
878
879 while (input && *input != '}')
880 {
881 char varyingType[256];
882 char varyingName[256];
883 unsigned int semanticIndex;
884 int matches = sscanf(input, " %s %s : TEXCOORD%d;", varyingType, varyingName, &semanticIndex);
885
886 if (matches == 3)
887 {
888 ASSERT(semanticIndex <= 9); // Single character
889
890 varyings.push_back(Varying(varyingName, input));
891 }
892
893 input = strstr(input, ";");
894 input += 2;
895 }
896}
897
898bool Program::linkVaryings()
899{
900 if (!mPixelHLSL || !mVertexHLSL)
901 {
902 return false;
903 }
904
905 VaryingArray vertexVaryings;
906 VaryingArray pixelVaryings;
907
908 parseVaryings("struct VS_OUTPUT\n{\n", mVertexHLSL, vertexVaryings);
909 parseVaryings("struct PS_INPUT\n{\n", mPixelHLSL, pixelVaryings);
910
911 for (unsigned int out = 0; out < vertexVaryings.size(); out++)
912 {
913 unsigned int in;
914 for (in = 0; in < pixelVaryings.size(); in++)
915 {
916 if (vertexVaryings[out].name == pixelVaryings[in].name)
917 {
918 pixelVaryings[in].link = out;
919 vertexVaryings[out].link = in;
920
921 break;
922 }
923 }
924
925 if (in != pixelVaryings.size())
926 {
927 // FIXME: Verify matching type and qualifiers
928
929 char *outputSemantic = strstr(vertexVaryings[out].declaration, " : TEXCOORD");
930 char *inputSemantic = strstr(pixelVaryings[in].declaration, " : TEXCOORD");
931 outputSemantic[11] = inputSemantic[11];
932 }
933 else
934 {
935 // Comment out the declaration and output assignment
936 vertexVaryings[out].declaration[0] = '/';
937 vertexVaryings[out].declaration[1] = '/';
938
939 char outputString[256];
940 sprintf(outputString, " output.%s = ", vertexVaryings[out].name.c_str());
941 char *varyingOutput = strstr(mVertexHLSL, outputString);
942
943 varyingOutput[0] = '/';
944 varyingOutput[1] = '/';
945 }
946 }
947
948 // Verify that each pixel varying has been linked to a vertex varying
949 for (unsigned int in = 0; in < pixelVaryings.size(); in++)
950 {
951 if (pixelVaryings[in].link < 0)
952 {
953 return false;
954 }
955 }
956
957 return true;
958}
959
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960// Links the HLSL code of the vertex and pixel shader by matching up their varyings,
961// compiling them into binaries, determining the attribute mappings, and collecting
962// a list of uniforms
963void Program::link()
964{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965 unlink();
966
967 if (!mFragmentShader || !mFragmentShader->isCompiled())
968 {
969 return;
970 }
971
972 if (!mVertexShader || !mVertexShader->isCompiled())
973 {
974 return;
975 }
976
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000977 Context *context = getContext();
978 const char *vertexProfile = context->getVertexShaderProfile();
979 const char *pixelProfile = context->getPixelShaderProfile();
daniel@transgaming.comdebe2592010-03-24 09:44:08 +0000980
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000981 const char *ps = mFragmentShader->getHLSL();
982 const char *vs = mVertexShader->getHLSL();
983
984 mPixelHLSL = new char[strlen(ps) + 1];
985 strcpy(mPixelHLSL, ps);
986 mVertexHLSL = new char[strlen(vs) + 1];
987 strcpy(mVertexHLSL, vs);
988
989 if (!linkVaryings())
990 {
991 return;
992 }
993
994 ID3DXBuffer *vertexBinary = compileToBinary(mVertexHLSL, vertexProfile, &mConstantTableVS);
995 ID3DXBuffer *pixelBinary = compileToBinary(mPixelHLSL, pixelProfile, &mConstantTablePS);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996
997 if (vertexBinary && pixelBinary)
998 {
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000999 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000 HRESULT vertexResult = device->CreateVertexShader((DWORD*)vertexBinary->GetBufferPointer(), &mVertexExecutable);
1001 HRESULT pixelResult = device->CreatePixelShader((DWORD*)pixelBinary->GetBufferPointer(), &mPixelExecutable);
1002
1003 if (vertexResult == D3DERR_OUTOFVIDEOMEMORY || vertexResult == E_OUTOFMEMORY || pixelResult == D3DERR_OUTOFVIDEOMEMORY || pixelResult == E_OUTOFMEMORY)
1004 {
1005 return error(GL_OUT_OF_MEMORY);
1006 }
1007
1008 ASSERT(SUCCEEDED(vertexResult) && SUCCEEDED(pixelResult));
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001009
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010 vertexBinary->Release();
1011 pixelBinary->Release();
1012 vertexBinary = NULL;
1013 pixelBinary = NULL;
1014
1015 if (mVertexExecutable && mPixelExecutable)
1016 {
1017 if (!linkAttributes())
1018 {
1019 return;
1020 }
1021
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001022 for (int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
1023 {
1024 mSamplers[i].active = false;
1025 }
1026
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001027 if (!linkUniforms(mConstantTablePS))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028 {
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001029 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030 }
1031
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001032 if (!linkUniforms(mConstantTableVS))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033 {
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001034 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035 }
1036
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001037 mLinked = true; // Success
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038 }
1039 }
1040}
1041
1042// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
1043bool Program::linkAttributes()
1044{
1045 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
1046 {
1047 const char *name = mVertexShader->getAttributeName(attributeIndex);
1048
1049 if (name)
1050 {
1051 GLuint location = getAttributeLocation(name);
1052
1053 if (location == -1) // Not set by glBindAttribLocation
1054 {
1055 int availableIndex = 0;
1056
1057 while (availableIndex < MAX_VERTEX_ATTRIBS && mAttributeName[availableIndex] && mVertexShader->isActiveAttribute(mAttributeName[availableIndex]))
1058 {
1059 availableIndex++;
1060 }
1061
1062 if (availableIndex == MAX_VERTEX_ATTRIBS)
1063 {
1064 return false; // Fail to link
1065 }
1066
1067 delete[] mAttributeName[availableIndex];
1068 mAttributeName[availableIndex] = new char[strlen(name) + 1]; // FIXME: Check allocation
1069 strcpy(mAttributeName[availableIndex], name);
1070 }
1071 }
1072 }
1073
1074 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
1075 {
1076 mInputMapping[attributeIndex] = mVertexShader->getInputMapping(mAttributeName[attributeIndex]);
1077 }
1078
1079 return true;
1080}
1081
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001082bool Program::linkUniforms(ID3DXConstantTable *constantTable)
1083{
1084 D3DXCONSTANTTABLE_DESC constantTableDescription;
1085 D3DXCONSTANT_DESC constantDescription;
1086 UINT descriptionCount = 1;
1087
1088 constantTable->GetDesc(&constantTableDescription);
1089
1090 for (unsigned int constantIndex = 0; constantIndex < constantTableDescription.Constants; constantIndex++)
1091 {
1092 D3DXHANDLE constantHandle = constantTable->GetConstant(0, constantIndex);
1093 constantTable->GetConstantDesc(constantHandle, &constantDescription, &descriptionCount);
1094
1095 if (!defineUniform(constantHandle, constantDescription))
1096 {
1097 return false;
1098 }
1099 }
1100
1101 return true;
1102}
1103
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104// Adds the description of a constant found in the binary shader to the list of uniforms
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001105// Returns true if succesful (uniform not already defined)
1106bool Program::defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT_DESC &constantDescription, std::string name)
1107{
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001108 if (constantDescription.RegisterSet == D3DXRS_SAMPLER)
1109 {
1110 unsigned int samplerIndex = constantDescription.RegisterIndex;
1111
1112 assert(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0]));
1113
1114 mSamplers[samplerIndex].active = true;
1115 mSamplers[samplerIndex].type = (constantDescription.Type == D3DXPT_SAMPLERCUBE) ? SAMPLER_CUBE : SAMPLER_2D;
1116 mSamplers[samplerIndex].logicalTextureUnit = 0;
1117 }
1118
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001119 switch(constantDescription.Class)
1120 {
1121 case D3DXPC_STRUCT:
1122 {
1123 for (unsigned int field = 0; field < constantDescription.StructMembers; field++)
1124 {
1125 D3DXHANDLE fieldHandle = mConstantTablePS->GetConstant(constantHandle, field);
1126
1127 D3DXCONSTANT_DESC fieldDescription;
1128 UINT descriptionCount = 1;
1129
1130 mConstantTablePS->GetConstantDesc(fieldHandle, &fieldDescription, &descriptionCount);
1131
1132 if (!defineUniform(fieldHandle, fieldDescription, name + constantDescription.Name + "."))
1133 {
1134 return false;
1135 }
1136 }
1137
1138 return true;
1139 }
1140 case D3DXPC_SCALAR:
1141 case D3DXPC_VECTOR:
1142 case D3DXPC_MATRIX_COLUMNS:
1143 case D3DXPC_OBJECT:
1144 return defineUniform(constantDescription, name + constantDescription.Name);
1145 default:
1146 UNREACHABLE();
1147 return false;
1148 }
1149}
1150
1151bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name)
1152{
1153 Uniform *uniform = createUniform(constantDescription, name);
1154
1155 if(!uniform)
1156 {
1157 return false;
1158 }
1159
1160 // Check if already defined
1161 GLint location = getUniformLocation(name.c_str());
daniel@transgaming.com0361b922010-03-28 19:36:15 +00001162 GLenum type = uniform->type;
daniel@transgaming.comc7d8a932010-03-16 06:16:45 +00001163
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001164 if (location >= 0)
1165 {
1166 delete uniform;
1167
1168 if (mUniforms[location]->type != type)
1169 {
1170 return false;
1171 }
1172 else
1173 {
1174 return true;
1175 }
1176 }
1177
1178 mUniforms.push_back(uniform);
1179
1180 return true;
1181}
1182
1183Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184{
1185 if (constantDescription.Rows == 1) // Vectors and scalars
1186 {
1187 switch (constantDescription.Type)
1188 {
1189 case D3DXPT_SAMPLER2D:
1190 case D3DXPT_SAMPLERCUBE:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001191 switch (constantDescription.Columns)
1192 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +00001193 case 1: return new Uniform(GL_INT, name, 1 * sizeof(GLint) * constantDescription.Elements);
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +00001194 default: UNREACHABLE();
1195 }
1196 break;
1197 case D3DXPT_BOOL:
1198 switch (constantDescription.Columns)
1199 {
1200 case 1: return new Uniform(GL_BOOL, name, 1 * sizeof(GLboolean) * constantDescription.Elements);
1201 case 2: return new Uniform(GL_BOOL_VEC2, name, 2 * sizeof(GLboolean) * constantDescription.Elements);
1202 case 3: return new Uniform(GL_BOOL_VEC3, name, 3 * sizeof(GLboolean) * constantDescription.Elements);
1203 case 4: return new Uniform(GL_BOOL_VEC4, name, 4 * sizeof(GLboolean) * constantDescription.Elements);
1204 default: UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001205 }
1206 break;
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +00001207 case D3DXPT_INT:
1208 switch (constantDescription.Columns)
1209 {
1210 case 1: return new Uniform(GL_INT, name, 1 * sizeof(GLint) * constantDescription.Elements);
1211 case 2: return new Uniform(GL_INT_VEC2, name, 2 * sizeof(GLint) * constantDescription.Elements);
1212 case 3: return new Uniform(GL_INT_VEC3, name, 3 * sizeof(GLint) * constantDescription.Elements);
1213 case 4: return new Uniform(GL_INT_VEC4, name, 4 * sizeof(GLint) * constantDescription.Elements);
1214 default: UNREACHABLE();
1215 }
1216 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001217 case D3DXPT_FLOAT:
1218 switch (constantDescription.Columns)
1219 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +00001220 case 1: return new Uniform(GL_FLOAT, name, 1 * sizeof(GLfloat) * constantDescription.Elements);
1221 case 2: return new Uniform(GL_FLOAT_VEC2, name, 2 * sizeof(GLfloat) * constantDescription.Elements);
1222 case 3: return new Uniform(GL_FLOAT_VEC3, name, 3 * sizeof(GLfloat) * constantDescription.Elements);
1223 case 4: return new Uniform(GL_FLOAT_VEC4, name, 4 * sizeof(GLfloat) * constantDescription.Elements);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001224 default: UNREACHABLE();
1225 }
1226 break;
1227 default:
1228 UNIMPLEMENTED(); // FIXME
1229 UNREACHABLE();
1230 }
1231 }
1232 else if (constantDescription.Rows == constantDescription.Columns) // Square matrices
1233 {
1234 switch (constantDescription.Type)
1235 {
1236 case D3DXPT_FLOAT:
1237 switch (constantDescription.Rows)
1238 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +00001239 case 2: return new Uniform(GL_FLOAT_MAT2, name, 2 * 2 * sizeof(GLfloat) * constantDescription.Elements);
1240 case 3: return new Uniform(GL_FLOAT_MAT3, name, 3 * 3 * sizeof(GLfloat) * constantDescription.Elements);
1241 case 4: return new Uniform(GL_FLOAT_MAT4, name, 4 * 4 * sizeof(GLfloat) * constantDescription.Elements);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001242 default: UNREACHABLE();
1243 }
1244 break;
1245 default: UNREACHABLE();
1246 }
1247 }
1248 else UNREACHABLE();
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001249
1250 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001251}
1252
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +00001253bool Program::applyUniform1bv(GLint location, GLsizei count, const GLboolean *v)
1254{
1255 BOOL *vector = new BOOL[count];
1256 for (int i = 0; i < count; i++)
1257 {
1258 if (v[i] == GL_FALSE)
1259 vector[i] = 0;
1260 else
1261 vector[i] = 1;
1262 }
1263
1264 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1265 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1266 IDirect3DDevice9 *device = getDevice();
1267
1268 if (constantPS)
1269 {
1270 mConstantTablePS->SetBoolArray(device, constantPS, vector, count);
1271 }
1272
1273 if (constantVS)
1274 {
1275 mConstantTableVS->SetBoolArray(device, constantVS, vector, count);
1276 }
1277
1278 delete [] vector;
1279
1280 return true;
1281}
1282
1283bool Program::applyUniform2bv(GLint location, GLsizei count, const GLboolean *v)
1284{
1285 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1286
1287 for (int i = 0; i < count; i++)
1288 {
1289 vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f),
1290 (v[1] == GL_FALSE ? 0.0f : 1.0f), 0, 0);
1291
1292 v += 2;
1293 }
1294
1295 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1296 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1297 IDirect3DDevice9 *device = getDevice();
1298
1299 if (constantPS)
1300 {
1301 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1302 }
1303
1304 if (constantVS)
1305 {
1306 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1307 }
1308
1309 delete[] vector;
1310
1311 return true;
1312}
1313
1314bool Program::applyUniform3bv(GLint location, GLsizei count, const GLboolean *v)
1315{
1316 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1317
1318 for (int i = 0; i < count; i++)
1319 {
1320 vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f),
1321 (v[1] == GL_FALSE ? 0.0f : 1.0f),
1322 (v[2] == GL_FALSE ? 0.0f : 1.0f), 0);
1323
1324 v += 3;
1325 }
1326
1327 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1328 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1329 IDirect3DDevice9 *device = getDevice();
1330
1331 if (constantPS)
1332 {
1333 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1334 }
1335
1336 if (constantVS)
1337 {
1338 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1339 }
1340
1341 delete[] vector;
1342
1343 return true;
1344}
1345
1346bool Program::applyUniform4bv(GLint location, GLsizei count, const GLboolean *v)
1347{
1348 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1349
1350 for (int i = 0; i < count; i++)
1351 {
1352 vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f),
1353 (v[1] == GL_FALSE ? 0.0f : 1.0f),
1354 (v[2] == GL_FALSE ? 0.0f : 1.0f),
1355 (v[3] == GL_FALSE ? 0.0f : 1.0f));
1356
1357 v += 3;
1358 }
1359
1360 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1361 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1362 IDirect3DDevice9 *device = getDevice();
1363
1364 if (constantPS)
1365 {
1366 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1367 }
1368
1369 if (constantVS)
1370 {
1371 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1372 }
1373
1374 delete [] vector;
1375
1376 return true;
1377}
1378
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001379bool Program::applyUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1380{
1381 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1382 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1383 IDirect3DDevice9 *device = getDevice();
1384
1385 if (constantPS)
1386 {
1387 mConstantTablePS->SetFloatArray(device, constantPS, v, count);
1388 }
1389
1390 if (constantVS)
1391 {
1392 mConstantTableVS->SetFloatArray(device, constantVS, v, count);
1393 }
1394
1395 return true;
1396}
1397
1398bool Program::applyUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1399{
1400 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1401
1402 for (int i = 0; i < count; i++)
1403 {
1404 vector[i] = D3DXVECTOR4(v[0], v[1], 0, 0);
1405
1406 v += 2;
1407 }
1408
1409 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1410 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1411 IDirect3DDevice9 *device = getDevice();
1412
1413 if (constantPS)
1414 {
1415 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1416 }
1417
1418 if (constantVS)
1419 {
1420 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1421 }
1422
1423 delete[] vector;
1424
1425 return true;
1426}
1427
1428bool Program::applyUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1429{
1430 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1431
1432 for (int i = 0; i < count; i++)
1433 {
1434 vector[i] = D3DXVECTOR4(v[0], v[1], v[2], 0);
1435
1436 v += 3;
1437 }
1438
1439 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1440 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1441 IDirect3DDevice9 *device = getDevice();
1442
1443 if (constantPS)
1444 {
1445 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1446 }
1447
1448 if (constantVS)
1449 {
1450 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1451 }
1452
1453 delete[] vector;
1454
1455 return true;
1456}
1457
1458bool Program::applyUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1459{
1460 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1461 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1462 IDirect3DDevice9 *device = getDevice();
1463
1464 if (constantPS)
1465 {
1466 mConstantTablePS->SetVectorArray(device, constantPS, (D3DXVECTOR4*)v, count);
1467 }
1468
1469 if (constantVS)
1470 {
1471 mConstantTableVS->SetVectorArray(device, constantVS, (D3DXVECTOR4*)v, count);
1472 }
1473
1474 return true;
1475}
1476
1477bool Program::applyUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
1478{
1479 D3DXMATRIX *matrix = new D3DXMATRIX[count];
1480
1481 for (int i = 0; i < count; i++)
1482 {
1483 matrix[i] = D3DXMATRIX(value[0], value[2], 0, 0,
1484 value[1], value[3], 0, 0,
1485 0, 0, 1, 0,
1486 0, 0, 0, 1);
1487
1488 value += 4;
1489 }
1490
1491 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1492 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1493 IDirect3DDevice9 *device = getDevice();
1494
1495 if (constantPS)
1496 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001497 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001498 }
1499
1500 if (constantVS)
1501 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001502 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001503 }
1504
1505 delete[] matrix;
1506
1507 return true;
1508}
1509
1510bool Program::applyUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
1511{
1512 D3DXMATRIX *matrix = new D3DXMATRIX[count];
1513
1514 for (int i = 0; i < count; i++)
1515 {
1516 matrix[i] = D3DXMATRIX(value[0], value[3], value[6], 0,
1517 value[1], value[4], value[7], 0,
1518 value[2], value[5], value[8], 0,
1519 0, 0, 0, 1);
1520
1521 value += 9;
1522 }
1523
1524 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1525 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1526 IDirect3DDevice9 *device = getDevice();
1527
1528 if (constantPS)
1529 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001530 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001531 }
1532
1533 if (constantVS)
1534 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001535 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001536 }
1537
1538 delete[] matrix;
1539
1540 return true;
1541}
1542
1543bool Program::applyUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
1544{
1545 D3DXMATRIX *matrix = new D3DXMATRIX[count];
1546
1547 for (int i = 0; i < count; i++)
1548 {
1549 matrix[i] = D3DXMATRIX(value[0], value[4], value[8], value[12],
1550 value[1], value[5], value[9], value[13],
1551 value[2], value[6], value[10], value[14],
1552 value[3], value[7], value[11], value[15]);
1553
1554 value += 16;
1555 }
1556
1557 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1558 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1559 IDirect3DDevice9 *device = getDevice();
1560
1561 if (constantPS)
1562 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001563 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001564 }
1565
1566 if (constantVS)
1567 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001568 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001569 }
1570
1571 delete[] matrix;
1572
1573 return true;
1574}
1575
1576bool Program::applyUniform1iv(GLint location, GLsizei count, const GLint *v)
1577{
1578 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1579 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1580 IDirect3DDevice9 *device = getDevice();
1581
1582 if (constantPS)
1583 {
1584 D3DXCONSTANT_DESC constantDescription;
1585 UINT descriptionCount = 1;
1586 HRESULT result = mConstantTablePS->GetConstantDesc(constantPS, &constantDescription, &descriptionCount);
1587
daniel@transgaming.com2884b782010-03-08 21:30:48 +00001588 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001589 {
1590 return false;
1591 }
1592
1593 if (constantDescription.RegisterSet == D3DXRS_SAMPLER)
1594 {
1595 unsigned int firstIndex = mConstantTablePS->GetSamplerIndex(constantPS);
1596
1597 for (unsigned int samplerIndex = firstIndex; samplerIndex < firstIndex + count; samplerIndex++)
1598 {
1599 GLint mappedSampler = v[0];
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001600
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001601 if (mappedSampler >= 0 && mappedSampler < MAX_TEXTURE_IMAGE_UNITS)
1602 {
1603 if (samplerIndex >= 0 && samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1604 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001605 ASSERT(mSamplers[samplerIndex].active);
1606 mSamplers[samplerIndex].logicalTextureUnit = mappedSampler;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001607 }
1608 }
1609 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001610
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001611 return true;
1612 }
1613 }
1614
1615 if (constantPS)
1616 {
1617 mConstantTablePS->SetIntArray(device, constantPS, v, count);
1618 }
1619
1620 if (constantVS)
1621 {
1622 mConstantTableVS->SetIntArray(device, constantVS, v, count);
1623 }
1624
1625 return true;
1626}
1627
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +00001628bool Program::applyUniform2iv(GLint location, GLsizei count, const GLint *v)
1629{
1630 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1631
1632 for (int i = 0; i < count; i++)
1633 {
1634 vector[i] = D3DXVECTOR4((float)v[0], (float)v[1], 0, 0);
1635
1636 v += 2;
1637 }
1638
1639 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1640 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1641 IDirect3DDevice9 *device = getDevice();
1642
1643 if (constantPS)
1644 {
1645 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1646 }
1647
1648 if (constantVS)
1649 {
1650 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1651 }
1652
1653 delete[] vector;
1654
1655 return true;
1656}
1657
1658bool Program::applyUniform3iv(GLint location, GLsizei count, const GLint *v)
1659{
1660 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1661
1662 for (int i = 0; i < count; i++)
1663 {
1664 vector[i] = D3DXVECTOR4((float)v[0], (float)v[1], (float)v[2], 0);
1665
1666 v += 3;
1667 }
1668
1669 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1670 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1671 IDirect3DDevice9 *device = getDevice();
1672
1673 if (constantPS)
1674 {
1675 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1676 }
1677
1678 if (constantVS)
1679 {
1680 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1681 }
1682
1683 delete[] vector;
1684
1685 return true;
1686}
1687
1688bool Program::applyUniform4iv(GLint location, GLsizei count, const GLint *v)
1689{
1690 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1691
1692 for (int i = 0; i < count; i++)
1693 {
1694 vector[i] = D3DXVECTOR4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
1695
1696 v += 4;
1697 }
1698
1699 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1700 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1701 IDirect3DDevice9 *device = getDevice();
1702
1703 if (constantPS)
1704 {
1705 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1706 }
1707
1708 if (constantVS)
1709 {
1710 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1711 }
1712
1713 delete [] vector;
1714
1715 return true;
1716}
1717
daniel@transgaming.comcba50572010-03-28 19:36:09 +00001718void Program::appendToInfoLog(const char *info)
1719{
1720 if (!info)
1721 {
1722 return;
1723 }
1724
1725 size_t infoLength = strlen(info);
1726
1727 if (!mInfoLog)
1728 {
1729 mInfoLog = new char[infoLength + 1];
1730 strcpy(mInfoLog, info);
1731 }
1732 else
1733 {
1734 size_t logLength = strlen(mInfoLog);
1735 char *newLog = new char[logLength + infoLength + 1];
1736 strcpy(newLog, mInfoLog);
1737 strcpy(newLog + logLength, info);
1738
1739 delete[] mInfoLog;
1740 mInfoLog = newLog;
1741 }
1742}
1743
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001744// Returns the program object to an unlinked state, after detaching a shader, before re-linking, or at destruction
1745void Program::unlink(bool destroy)
1746{
1747 if (destroy) // Object being destructed
1748 {
1749 if (mFragmentShader)
1750 {
1751 mFragmentShader->detach();
1752 mFragmentShader = NULL;
1753 }
1754
1755 if (mVertexShader)
1756 {
1757 mVertexShader->detach();
1758 mVertexShader = NULL;
1759 }
1760
1761 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
1762 {
1763 delete[] mAttributeName[index];
1764 mAttributeName[index] = NULL;
1765 }
1766 }
1767
1768 if (mPixelExecutable)
1769 {
1770 mPixelExecutable->Release();
1771 mPixelExecutable = NULL;
1772 }
1773
1774 if (mVertexExecutable)
1775 {
1776 mVertexExecutable->Release();
1777 mVertexExecutable = NULL;
1778 }
1779
1780 if (mConstantTablePS)
1781 {
1782 mConstantTablePS->Release();
1783 mConstantTablePS = NULL;
1784 }
1785
1786 if (mConstantTableVS)
1787 {
1788 mConstantTableVS->Release();
1789 mConstantTableVS = NULL;
1790 }
1791
1792 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
1793 {
1794 mInputMapping[index] = 0;
1795 }
1796
1797 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
1798 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001799 mSamplers[index].active = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001800 }
1801
1802 while (!mUniforms.empty())
1803 {
1804 delete mUniforms.back();
1805 mUniforms.pop_back();
1806 }
1807
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +00001808 delete[] mPixelHLSL;
1809 mPixelHLSL = NULL;
1810
1811 delete[] mVertexHLSL;
1812 mVertexHLSL = NULL;
1813
1814 delete[] mInfoLog;
1815 mInfoLog = NULL;
1816
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001817 mLinked = false;
1818}
1819
1820bool Program::isLinked()
1821{
1822 return mLinked;
1823}
1824
daniel@transgaming.comcba50572010-03-28 19:36:09 +00001825int Program::getInfoLogLength() const
1826{
1827 if (!mInfoLog)
1828 {
1829 return 0;
1830 }
1831 else
1832 {
1833 return strlen(mInfoLog) + 1;
1834 }
1835}
1836
1837void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog)
1838{
1839 int index = 0;
1840
1841 if (mInfoLog)
1842 {
1843 while (index < bufSize - 1 && index < (int)strlen(mInfoLog))
1844 {
1845 infoLog[index] = mInfoLog[index];
1846 index++;
1847 }
1848 }
1849
1850 if (bufSize)
1851 {
1852 infoLog[index] = '\0';
1853 }
1854
1855 if (length)
1856 {
1857 *length = index;
1858 }
1859}
1860
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001861void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders)
1862{
1863 int total = 0;
1864
1865 if (mVertexShader)
1866 {
1867 if (total < maxCount)
1868 {
1869 shaders[total] = mVertexShader->getHandle();
1870 }
1871
1872 total++;
1873 }
1874
1875 if (mFragmentShader)
1876 {
1877 if (total < maxCount)
1878 {
1879 shaders[total] = mFragmentShader->getHandle();
1880 }
1881
1882 total++;
1883 }
1884
1885 if (count)
1886 {
1887 *count = total;
1888 }
1889}
1890
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891void Program::flagForDeletion()
1892{
1893 mDeleteStatus = true;
1894}
1895
1896bool Program::isFlaggedForDeletion() const
1897{
1898 return mDeleteStatus;
1899}
1900}