blob: 9683956eab8b32797464aaa705e2152963a90296 [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 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000198 if (mUniforms[location]->name == decorate(name))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199 {
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.com72d0b522010-04-13 19:53:44 +00001253// This methods needs to match OutputHLSL::decorate
1254std::string Program::decorate(const std::string &string)
1255{
1256 if (string.substr(0, 3) != "gl_")
1257 {
1258 return "_" + string;
1259 }
1260 else
1261 {
1262 return string;
1263 }
1264}
1265
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +00001266bool Program::applyUniform1bv(GLint location, GLsizei count, const GLboolean *v)
1267{
1268 BOOL *vector = new BOOL[count];
1269 for (int i = 0; i < count; i++)
1270 {
1271 if (v[i] == GL_FALSE)
1272 vector[i] = 0;
1273 else
1274 vector[i] = 1;
1275 }
1276
1277 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1278 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1279 IDirect3DDevice9 *device = getDevice();
1280
1281 if (constantPS)
1282 {
1283 mConstantTablePS->SetBoolArray(device, constantPS, vector, count);
1284 }
1285
1286 if (constantVS)
1287 {
1288 mConstantTableVS->SetBoolArray(device, constantVS, vector, count);
1289 }
1290
1291 delete [] vector;
1292
1293 return true;
1294}
1295
1296bool Program::applyUniform2bv(GLint location, GLsizei count, const GLboolean *v)
1297{
1298 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1299
1300 for (int i = 0; i < count; i++)
1301 {
1302 vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f),
1303 (v[1] == GL_FALSE ? 0.0f : 1.0f), 0, 0);
1304
1305 v += 2;
1306 }
1307
1308 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1309 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1310 IDirect3DDevice9 *device = getDevice();
1311
1312 if (constantPS)
1313 {
1314 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1315 }
1316
1317 if (constantVS)
1318 {
1319 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1320 }
1321
1322 delete[] vector;
1323
1324 return true;
1325}
1326
1327bool Program::applyUniform3bv(GLint location, GLsizei count, const GLboolean *v)
1328{
1329 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1330
1331 for (int i = 0; i < count; i++)
1332 {
1333 vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f),
1334 (v[1] == GL_FALSE ? 0.0f : 1.0f),
1335 (v[2] == GL_FALSE ? 0.0f : 1.0f), 0);
1336
1337 v += 3;
1338 }
1339
1340 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1341 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1342 IDirect3DDevice9 *device = getDevice();
1343
1344 if (constantPS)
1345 {
1346 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1347 }
1348
1349 if (constantVS)
1350 {
1351 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1352 }
1353
1354 delete[] vector;
1355
1356 return true;
1357}
1358
1359bool Program::applyUniform4bv(GLint location, GLsizei count, const GLboolean *v)
1360{
1361 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1362
1363 for (int i = 0; i < count; i++)
1364 {
1365 vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f),
1366 (v[1] == GL_FALSE ? 0.0f : 1.0f),
1367 (v[2] == GL_FALSE ? 0.0f : 1.0f),
1368 (v[3] == GL_FALSE ? 0.0f : 1.0f));
1369
1370 v += 3;
1371 }
1372
1373 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1374 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1375 IDirect3DDevice9 *device = getDevice();
1376
1377 if (constantPS)
1378 {
1379 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1380 }
1381
1382 if (constantVS)
1383 {
1384 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1385 }
1386
1387 delete [] vector;
1388
1389 return true;
1390}
1391
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001392bool Program::applyUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1393{
1394 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1395 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1396 IDirect3DDevice9 *device = getDevice();
1397
1398 if (constantPS)
1399 {
1400 mConstantTablePS->SetFloatArray(device, constantPS, v, count);
1401 }
1402
1403 if (constantVS)
1404 {
1405 mConstantTableVS->SetFloatArray(device, constantVS, v, count);
1406 }
1407
1408 return true;
1409}
1410
1411bool Program::applyUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1412{
1413 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1414
1415 for (int i = 0; i < count; i++)
1416 {
1417 vector[i] = D3DXVECTOR4(v[0], v[1], 0, 0);
1418
1419 v += 2;
1420 }
1421
1422 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1423 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1424 IDirect3DDevice9 *device = getDevice();
1425
1426 if (constantPS)
1427 {
1428 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1429 }
1430
1431 if (constantVS)
1432 {
1433 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1434 }
1435
1436 delete[] vector;
1437
1438 return true;
1439}
1440
1441bool Program::applyUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1442{
1443 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1444
1445 for (int i = 0; i < count; i++)
1446 {
1447 vector[i] = D3DXVECTOR4(v[0], v[1], v[2], 0);
1448
1449 v += 3;
1450 }
1451
1452 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1453 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1454 IDirect3DDevice9 *device = getDevice();
1455
1456 if (constantPS)
1457 {
1458 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1459 }
1460
1461 if (constantVS)
1462 {
1463 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1464 }
1465
1466 delete[] vector;
1467
1468 return true;
1469}
1470
1471bool Program::applyUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1472{
1473 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1474 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1475 IDirect3DDevice9 *device = getDevice();
1476
1477 if (constantPS)
1478 {
1479 mConstantTablePS->SetVectorArray(device, constantPS, (D3DXVECTOR4*)v, count);
1480 }
1481
1482 if (constantVS)
1483 {
1484 mConstantTableVS->SetVectorArray(device, constantVS, (D3DXVECTOR4*)v, count);
1485 }
1486
1487 return true;
1488}
1489
1490bool Program::applyUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
1491{
1492 D3DXMATRIX *matrix = new D3DXMATRIX[count];
1493
1494 for (int i = 0; i < count; i++)
1495 {
1496 matrix[i] = D3DXMATRIX(value[0], value[2], 0, 0,
1497 value[1], value[3], 0, 0,
1498 0, 0, 1, 0,
1499 0, 0, 0, 1);
1500
1501 value += 4;
1502 }
1503
1504 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1505 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1506 IDirect3DDevice9 *device = getDevice();
1507
1508 if (constantPS)
1509 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001510 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001511 }
1512
1513 if (constantVS)
1514 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001515 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001516 }
1517
1518 delete[] matrix;
1519
1520 return true;
1521}
1522
1523bool Program::applyUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
1524{
1525 D3DXMATRIX *matrix = new D3DXMATRIX[count];
1526
1527 for (int i = 0; i < count; i++)
1528 {
1529 matrix[i] = D3DXMATRIX(value[0], value[3], value[6], 0,
1530 value[1], value[4], value[7], 0,
1531 value[2], value[5], value[8], 0,
1532 0, 0, 0, 1);
1533
1534 value += 9;
1535 }
1536
1537 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1538 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1539 IDirect3DDevice9 *device = getDevice();
1540
1541 if (constantPS)
1542 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001543 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001544 }
1545
1546 if (constantVS)
1547 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001548 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001549 }
1550
1551 delete[] matrix;
1552
1553 return true;
1554}
1555
1556bool Program::applyUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
1557{
1558 D3DXMATRIX *matrix = new D3DXMATRIX[count];
1559
1560 for (int i = 0; i < count; i++)
1561 {
1562 matrix[i] = D3DXMATRIX(value[0], value[4], value[8], value[12],
1563 value[1], value[5], value[9], value[13],
1564 value[2], value[6], value[10], value[14],
1565 value[3], value[7], value[11], value[15]);
1566
1567 value += 16;
1568 }
1569
1570 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1571 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1572 IDirect3DDevice9 *device = getDevice();
1573
1574 if (constantPS)
1575 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001576 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577 }
1578
1579 if (constantVS)
1580 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001581 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001582 }
1583
1584 delete[] matrix;
1585
1586 return true;
1587}
1588
1589bool Program::applyUniform1iv(GLint location, GLsizei count, const GLint *v)
1590{
1591 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1592 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1593 IDirect3DDevice9 *device = getDevice();
1594
1595 if (constantPS)
1596 {
1597 D3DXCONSTANT_DESC constantDescription;
1598 UINT descriptionCount = 1;
1599 HRESULT result = mConstantTablePS->GetConstantDesc(constantPS, &constantDescription, &descriptionCount);
1600
daniel@transgaming.com2884b782010-03-08 21:30:48 +00001601 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001602 {
1603 return false;
1604 }
1605
1606 if (constantDescription.RegisterSet == D3DXRS_SAMPLER)
1607 {
1608 unsigned int firstIndex = mConstantTablePS->GetSamplerIndex(constantPS);
1609
1610 for (unsigned int samplerIndex = firstIndex; samplerIndex < firstIndex + count; samplerIndex++)
1611 {
1612 GLint mappedSampler = v[0];
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001613
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001614 if (mappedSampler >= 0 && mappedSampler < MAX_TEXTURE_IMAGE_UNITS)
1615 {
1616 if (samplerIndex >= 0 && samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1617 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001618 ASSERT(mSamplers[samplerIndex].active);
1619 mSamplers[samplerIndex].logicalTextureUnit = mappedSampler;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001620 }
1621 }
1622 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001623
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001624 return true;
1625 }
1626 }
1627
1628 if (constantPS)
1629 {
1630 mConstantTablePS->SetIntArray(device, constantPS, v, count);
1631 }
1632
1633 if (constantVS)
1634 {
1635 mConstantTableVS->SetIntArray(device, constantVS, v, count);
1636 }
1637
1638 return true;
1639}
1640
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +00001641bool Program::applyUniform2iv(GLint location, GLsizei count, const GLint *v)
1642{
1643 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1644
1645 for (int i = 0; i < count; i++)
1646 {
1647 vector[i] = D3DXVECTOR4((float)v[0], (float)v[1], 0, 0);
1648
1649 v += 2;
1650 }
1651
1652 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1653 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1654 IDirect3DDevice9 *device = getDevice();
1655
1656 if (constantPS)
1657 {
1658 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1659 }
1660
1661 if (constantVS)
1662 {
1663 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1664 }
1665
1666 delete[] vector;
1667
1668 return true;
1669}
1670
1671bool Program::applyUniform3iv(GLint location, GLsizei count, const GLint *v)
1672{
1673 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1674
1675 for (int i = 0; i < count; i++)
1676 {
1677 vector[i] = D3DXVECTOR4((float)v[0], (float)v[1], (float)v[2], 0);
1678
1679 v += 3;
1680 }
1681
1682 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1683 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1684 IDirect3DDevice9 *device = getDevice();
1685
1686 if (constantPS)
1687 {
1688 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1689 }
1690
1691 if (constantVS)
1692 {
1693 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1694 }
1695
1696 delete[] vector;
1697
1698 return true;
1699}
1700
1701bool Program::applyUniform4iv(GLint location, GLsizei count, const GLint *v)
1702{
1703 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1704
1705 for (int i = 0; i < count; i++)
1706 {
1707 vector[i] = D3DXVECTOR4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
1708
1709 v += 4;
1710 }
1711
1712 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1713 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1714 IDirect3DDevice9 *device = getDevice();
1715
1716 if (constantPS)
1717 {
1718 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1719 }
1720
1721 if (constantVS)
1722 {
1723 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1724 }
1725
1726 delete [] vector;
1727
1728 return true;
1729}
1730
daniel@transgaming.comcba50572010-03-28 19:36:09 +00001731void Program::appendToInfoLog(const char *info)
1732{
1733 if (!info)
1734 {
1735 return;
1736 }
1737
1738 size_t infoLength = strlen(info);
1739
1740 if (!mInfoLog)
1741 {
1742 mInfoLog = new char[infoLength + 1];
1743 strcpy(mInfoLog, info);
1744 }
1745 else
1746 {
1747 size_t logLength = strlen(mInfoLog);
1748 char *newLog = new char[logLength + infoLength + 1];
1749 strcpy(newLog, mInfoLog);
1750 strcpy(newLog + logLength, info);
1751
1752 delete[] mInfoLog;
1753 mInfoLog = newLog;
1754 }
1755}
1756
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757// Returns the program object to an unlinked state, after detaching a shader, before re-linking, or at destruction
1758void Program::unlink(bool destroy)
1759{
1760 if (destroy) // Object being destructed
1761 {
1762 if (mFragmentShader)
1763 {
1764 mFragmentShader->detach();
1765 mFragmentShader = NULL;
1766 }
1767
1768 if (mVertexShader)
1769 {
1770 mVertexShader->detach();
1771 mVertexShader = NULL;
1772 }
1773
1774 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
1775 {
1776 delete[] mAttributeName[index];
1777 mAttributeName[index] = NULL;
1778 }
1779 }
1780
1781 if (mPixelExecutable)
1782 {
1783 mPixelExecutable->Release();
1784 mPixelExecutable = NULL;
1785 }
1786
1787 if (mVertexExecutable)
1788 {
1789 mVertexExecutable->Release();
1790 mVertexExecutable = NULL;
1791 }
1792
1793 if (mConstantTablePS)
1794 {
1795 mConstantTablePS->Release();
1796 mConstantTablePS = NULL;
1797 }
1798
1799 if (mConstantTableVS)
1800 {
1801 mConstantTableVS->Release();
1802 mConstantTableVS = NULL;
1803 }
1804
1805 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
1806 {
1807 mInputMapping[index] = 0;
1808 }
1809
1810 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
1811 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001812 mSamplers[index].active = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813 }
1814
1815 while (!mUniforms.empty())
1816 {
1817 delete mUniforms.back();
1818 mUniforms.pop_back();
1819 }
1820
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +00001821 delete[] mPixelHLSL;
1822 mPixelHLSL = NULL;
1823
1824 delete[] mVertexHLSL;
1825 mVertexHLSL = NULL;
1826
1827 delete[] mInfoLog;
1828 mInfoLog = NULL;
1829
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830 mLinked = false;
1831}
1832
1833bool Program::isLinked()
1834{
1835 return mLinked;
1836}
1837
daniel@transgaming.comcba50572010-03-28 19:36:09 +00001838int Program::getInfoLogLength() const
1839{
1840 if (!mInfoLog)
1841 {
1842 return 0;
1843 }
1844 else
1845 {
1846 return strlen(mInfoLog) + 1;
1847 }
1848}
1849
1850void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog)
1851{
1852 int index = 0;
1853
1854 if (mInfoLog)
1855 {
1856 while (index < bufSize - 1 && index < (int)strlen(mInfoLog))
1857 {
1858 infoLog[index] = mInfoLog[index];
1859 index++;
1860 }
1861 }
1862
1863 if (bufSize)
1864 {
1865 infoLog[index] = '\0';
1866 }
1867
1868 if (length)
1869 {
1870 *length = index;
1871 }
1872}
1873
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001874void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders)
1875{
1876 int total = 0;
1877
1878 if (mVertexShader)
1879 {
1880 if (total < maxCount)
1881 {
1882 shaders[total] = mVertexShader->getHandle();
1883 }
1884
1885 total++;
1886 }
1887
1888 if (mFragmentShader)
1889 {
1890 if (total < maxCount)
1891 {
1892 shaders[total] = mFragmentShader->getHandle();
1893 }
1894
1895 total++;
1896 }
1897
1898 if (count)
1899 {
1900 *count = total;
1901 }
1902}
1903
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904void Program::flagForDeletion()
1905{
1906 mDeleteStatus = true;
1907}
1908
1909bool Program::isFlaggedForDeletion() const
1910{
1911 return mDeleteStatus;
1912}
1913}