blob: 5919c41f56bd810586938334d7603bcb092e18a3 [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
695// Applies all the uniforms set for this program object to the Direct3D 9 device
696void Program::applyUniforms()
697{
698 for (unsigned int location = 0; location < mUniforms.size(); location++)
699 {
700 int bytes = mUniforms[location]->bytes;
701 GLfloat *f = (GLfloat*)mUniforms[location]->data;
702 GLint *i = (GLint*)mUniforms[location]->data;
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +0000703 GLboolean *b = (GLboolean*)mUniforms[location]->data;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704
705 switch (mUniforms[location]->type)
706 {
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +0000707 case GL_BOOL: applyUniform1bv(location, bytes / sizeof(GLboolean), b); break;
708 case GL_BOOL_VEC2: applyUniform2bv(location, bytes / 2 / sizeof(GLboolean), b); break;
709 case GL_BOOL_VEC3: applyUniform3bv(location, bytes / 3 / sizeof(GLboolean), b); break;
710 case GL_BOOL_VEC4: applyUniform4bv(location, bytes / 4 / sizeof(GLboolean), b); break;
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000711 case GL_FLOAT: applyUniform1fv(location, bytes / sizeof(GLfloat), f); break;
712 case GL_FLOAT_VEC2: applyUniform2fv(location, bytes / 2 / sizeof(GLfloat), f); break;
713 case GL_FLOAT_VEC3: applyUniform3fv(location, bytes / 3 / sizeof(GLfloat), f); break;
714 case GL_FLOAT_VEC4: applyUniform4fv(location, bytes / 4 / sizeof(GLfloat), f); break;
715 case GL_FLOAT_MAT2: applyUniformMatrix2fv(location, bytes / 4 / sizeof(GLfloat), f); break;
716 case GL_FLOAT_MAT3: applyUniformMatrix3fv(location, bytes / 9 / sizeof(GLfloat), f); break;
717 case GL_FLOAT_MAT4: applyUniformMatrix4fv(location, bytes / 16 / sizeof(GLfloat), f); break;
718 case GL_INT: applyUniform1iv(location, bytes / sizeof(GLint), i); break;
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +0000719 case GL_INT_VEC2: applyUniform2iv(location, bytes / 2 / sizeof(GLint), i); break;
720 case GL_INT_VEC3: applyUniform3iv(location, bytes / 3 / sizeof(GLint), i); break;
721 case GL_INT_VEC4: applyUniform4iv(location, bytes / 4 / sizeof(GLint), i); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000722 default:
723 UNIMPLEMENTED(); // FIXME
724 UNREACHABLE();
725 }
726 }
727}
728
729// Compiles the HLSL code of the attached shaders into executable binaries
730ID3DXBuffer *Program::compileToBinary(const char *hlsl, const char *profile, ID3DXConstantTable **constantTable)
731{
732 if (!hlsl)
733 {
734 return NULL;
735 }
736
737 ID3DXBuffer *binary = NULL;
738 ID3DXBuffer *errorMessage = NULL;
739
daniel@transgaming.comcbbca002010-04-01 13:39:32 +0000740 HRESULT result = D3DXCompileShader(hlsl, (UINT)strlen(hlsl), NULL, 0, "main", profile, 0, &binary, &errorMessage, constantTable);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741
742 if (SUCCEEDED(result))
743 {
744 return binary;
745 }
746
747 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
748 {
749 return error(GL_OUT_OF_MEMORY, (ID3DXBuffer*)NULL);
750 }
751
752 if (errorMessage)
753 {
754 const char *message = (const char*)errorMessage->GetBufferPointer();
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000755
daniel@transgaming.com0599dc62010-03-21 04:31:36 +0000756 TRACE("\n%s", hlsl);
757 TRACE("\n%s", message);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758 }
759
760 return NULL;
761}
762
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000763void Program::parseVaryings(const char *structure, char *hlsl, VaryingArray &varyings)
764{
765 char *input = strstr(hlsl, structure);
766 input += strlen(structure);
767
768 while (input && *input != '}')
769 {
770 char varyingType[256];
771 char varyingName[256];
772 unsigned int semanticIndex;
773 int matches = sscanf(input, " %s %s : TEXCOORD%d;", varyingType, varyingName, &semanticIndex);
774
775 if (matches == 3)
776 {
777 ASSERT(semanticIndex <= 9); // Single character
778
779 varyings.push_back(Varying(varyingName, input));
780 }
781
782 input = strstr(input, ";");
783 input += 2;
784 }
785}
786
787bool Program::linkVaryings()
788{
789 if (!mPixelHLSL || !mVertexHLSL)
790 {
791 return false;
792 }
793
794 VaryingArray vertexVaryings;
795 VaryingArray pixelVaryings;
796
797 parseVaryings("struct VS_OUTPUT\n{\n", mVertexHLSL, vertexVaryings);
798 parseVaryings("struct PS_INPUT\n{\n", mPixelHLSL, pixelVaryings);
799
800 for (unsigned int out = 0; out < vertexVaryings.size(); out++)
801 {
802 unsigned int in;
803 for (in = 0; in < pixelVaryings.size(); in++)
804 {
805 if (vertexVaryings[out].name == pixelVaryings[in].name)
806 {
807 pixelVaryings[in].link = out;
808 vertexVaryings[out].link = in;
809
810 break;
811 }
812 }
813
814 if (in != pixelVaryings.size())
815 {
816 // FIXME: Verify matching type and qualifiers
817
818 char *outputSemantic = strstr(vertexVaryings[out].declaration, " : TEXCOORD");
819 char *inputSemantic = strstr(pixelVaryings[in].declaration, " : TEXCOORD");
820 outputSemantic[11] = inputSemantic[11];
821 }
822 else
823 {
824 // Comment out the declaration and output assignment
825 vertexVaryings[out].declaration[0] = '/';
826 vertexVaryings[out].declaration[1] = '/';
827
828 char outputString[256];
829 sprintf(outputString, " output.%s = ", vertexVaryings[out].name.c_str());
830 char *varyingOutput = strstr(mVertexHLSL, outputString);
831
832 varyingOutput[0] = '/';
833 varyingOutput[1] = '/';
834 }
835 }
836
837 // Verify that each pixel varying has been linked to a vertex varying
838 for (unsigned int in = 0; in < pixelVaryings.size(); in++)
839 {
840 if (pixelVaryings[in].link < 0)
841 {
842 return false;
843 }
844 }
845
846 return true;
847}
848
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849// Links the HLSL code of the vertex and pixel shader by matching up their varyings,
850// compiling them into binaries, determining the attribute mappings, and collecting
851// a list of uniforms
852void Program::link()
853{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854 unlink();
855
856 if (!mFragmentShader || !mFragmentShader->isCompiled())
857 {
858 return;
859 }
860
861 if (!mVertexShader || !mVertexShader->isCompiled())
862 {
863 return;
864 }
865
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000866 Context *context = getContext();
867 const char *vertexProfile = context->getVertexShaderProfile();
868 const char *pixelProfile = context->getPixelShaderProfile();
daniel@transgaming.comdebe2592010-03-24 09:44:08 +0000869
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000870 const char *ps = mFragmentShader->getHLSL();
871 const char *vs = mVertexShader->getHLSL();
872
873 mPixelHLSL = new char[strlen(ps) + 1];
874 strcpy(mPixelHLSL, ps);
875 mVertexHLSL = new char[strlen(vs) + 1];
876 strcpy(mVertexHLSL, vs);
877
878 if (!linkVaryings())
879 {
880 return;
881 }
882
883 ID3DXBuffer *vertexBinary = compileToBinary(mVertexHLSL, vertexProfile, &mConstantTableVS);
884 ID3DXBuffer *pixelBinary = compileToBinary(mPixelHLSL, pixelProfile, &mConstantTablePS);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885
886 if (vertexBinary && pixelBinary)
887 {
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000888 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889 HRESULT vertexResult = device->CreateVertexShader((DWORD*)vertexBinary->GetBufferPointer(), &mVertexExecutable);
890 HRESULT pixelResult = device->CreatePixelShader((DWORD*)pixelBinary->GetBufferPointer(), &mPixelExecutable);
891
892 if (vertexResult == D3DERR_OUTOFVIDEOMEMORY || vertexResult == E_OUTOFMEMORY || pixelResult == D3DERR_OUTOFVIDEOMEMORY || pixelResult == E_OUTOFMEMORY)
893 {
894 return error(GL_OUT_OF_MEMORY);
895 }
896
897 ASSERT(SUCCEEDED(vertexResult) && SUCCEEDED(pixelResult));
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000898
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899 vertexBinary->Release();
900 pixelBinary->Release();
901 vertexBinary = NULL;
902 pixelBinary = NULL;
903
904 if (mVertexExecutable && mPixelExecutable)
905 {
906 if (!linkAttributes())
907 {
908 return;
909 }
910
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000911 for (int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
912 {
913 mSamplers[i].active = false;
914 }
915
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000916 if (!linkUniforms(mConstantTablePS))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917 {
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000918 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919 }
920
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000921 if (!linkUniforms(mConstantTableVS))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922 {
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000923 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924 }
925
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000926 mLinked = true; // Success
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927 }
928 }
929}
930
931// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
932bool Program::linkAttributes()
933{
934 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
935 {
936 const char *name = mVertexShader->getAttributeName(attributeIndex);
937
938 if (name)
939 {
940 GLuint location = getAttributeLocation(name);
941
942 if (location == -1) // Not set by glBindAttribLocation
943 {
944 int availableIndex = 0;
945
946 while (availableIndex < MAX_VERTEX_ATTRIBS && mAttributeName[availableIndex] && mVertexShader->isActiveAttribute(mAttributeName[availableIndex]))
947 {
948 availableIndex++;
949 }
950
951 if (availableIndex == MAX_VERTEX_ATTRIBS)
952 {
953 return false; // Fail to link
954 }
955
956 delete[] mAttributeName[availableIndex];
957 mAttributeName[availableIndex] = new char[strlen(name) + 1]; // FIXME: Check allocation
958 strcpy(mAttributeName[availableIndex], name);
959 }
960 }
961 }
962
963 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
964 {
965 mInputMapping[attributeIndex] = mVertexShader->getInputMapping(mAttributeName[attributeIndex]);
966 }
967
968 return true;
969}
970
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000971bool Program::linkUniforms(ID3DXConstantTable *constantTable)
972{
973 D3DXCONSTANTTABLE_DESC constantTableDescription;
974 D3DXCONSTANT_DESC constantDescription;
975 UINT descriptionCount = 1;
976
977 constantTable->GetDesc(&constantTableDescription);
978
979 for (unsigned int constantIndex = 0; constantIndex < constantTableDescription.Constants; constantIndex++)
980 {
981 D3DXHANDLE constantHandle = constantTable->GetConstant(0, constantIndex);
982 constantTable->GetConstantDesc(constantHandle, &constantDescription, &descriptionCount);
983
984 if (!defineUniform(constantHandle, constantDescription))
985 {
986 return false;
987 }
988 }
989
990 return true;
991}
992
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993// Adds the description of a constant found in the binary shader to the list of uniforms
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000994// Returns true if succesful (uniform not already defined)
995bool Program::defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT_DESC &constantDescription, std::string name)
996{
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000997 if (constantDescription.RegisterSet == D3DXRS_SAMPLER)
998 {
999 unsigned int samplerIndex = constantDescription.RegisterIndex;
1000
1001 assert(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0]));
1002
1003 mSamplers[samplerIndex].active = true;
1004 mSamplers[samplerIndex].type = (constantDescription.Type == D3DXPT_SAMPLERCUBE) ? SAMPLER_CUBE : SAMPLER_2D;
1005 mSamplers[samplerIndex].logicalTextureUnit = 0;
1006 }
1007
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001008 switch(constantDescription.Class)
1009 {
1010 case D3DXPC_STRUCT:
1011 {
1012 for (unsigned int field = 0; field < constantDescription.StructMembers; field++)
1013 {
1014 D3DXHANDLE fieldHandle = mConstantTablePS->GetConstant(constantHandle, field);
1015
1016 D3DXCONSTANT_DESC fieldDescription;
1017 UINT descriptionCount = 1;
1018
1019 mConstantTablePS->GetConstantDesc(fieldHandle, &fieldDescription, &descriptionCount);
1020
1021 if (!defineUniform(fieldHandle, fieldDescription, name + constantDescription.Name + "."))
1022 {
1023 return false;
1024 }
1025 }
1026
1027 return true;
1028 }
1029 case D3DXPC_SCALAR:
1030 case D3DXPC_VECTOR:
1031 case D3DXPC_MATRIX_COLUMNS:
1032 case D3DXPC_OBJECT:
1033 return defineUniform(constantDescription, name + constantDescription.Name);
1034 default:
1035 UNREACHABLE();
1036 return false;
1037 }
1038}
1039
1040bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name)
1041{
1042 Uniform *uniform = createUniform(constantDescription, name);
1043
1044 if(!uniform)
1045 {
1046 return false;
1047 }
1048
1049 // Check if already defined
1050 GLint location = getUniformLocation(name.c_str());
daniel@transgaming.com0361b922010-03-28 19:36:15 +00001051 GLenum type = uniform->type;
daniel@transgaming.comc7d8a932010-03-16 06:16:45 +00001052
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001053 if (location >= 0)
1054 {
1055 delete uniform;
1056
1057 if (mUniforms[location]->type != type)
1058 {
1059 return false;
1060 }
1061 else
1062 {
1063 return true;
1064 }
1065 }
1066
1067 mUniforms.push_back(uniform);
1068
1069 return true;
1070}
1071
1072Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073{
1074 if (constantDescription.Rows == 1) // Vectors and scalars
1075 {
1076 switch (constantDescription.Type)
1077 {
1078 case D3DXPT_SAMPLER2D:
1079 case D3DXPT_SAMPLERCUBE:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001080 switch (constantDescription.Columns)
1081 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +00001082 case 1: return new Uniform(GL_INT, name, 1 * sizeof(GLint) * constantDescription.Elements);
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +00001083 default: UNREACHABLE();
1084 }
1085 break;
1086 case D3DXPT_BOOL:
1087 switch (constantDescription.Columns)
1088 {
1089 case 1: return new Uniform(GL_BOOL, name, 1 * sizeof(GLboolean) * constantDescription.Elements);
1090 case 2: return new Uniform(GL_BOOL_VEC2, name, 2 * sizeof(GLboolean) * constantDescription.Elements);
1091 case 3: return new Uniform(GL_BOOL_VEC3, name, 3 * sizeof(GLboolean) * constantDescription.Elements);
1092 case 4: return new Uniform(GL_BOOL_VEC4, name, 4 * sizeof(GLboolean) * constantDescription.Elements);
1093 default: UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094 }
1095 break;
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +00001096 case D3DXPT_INT:
1097 switch (constantDescription.Columns)
1098 {
1099 case 1: return new Uniform(GL_INT, name, 1 * sizeof(GLint) * constantDescription.Elements);
1100 case 2: return new Uniform(GL_INT_VEC2, name, 2 * sizeof(GLint) * constantDescription.Elements);
1101 case 3: return new Uniform(GL_INT_VEC3, name, 3 * sizeof(GLint) * constantDescription.Elements);
1102 case 4: return new Uniform(GL_INT_VEC4, name, 4 * sizeof(GLint) * constantDescription.Elements);
1103 default: UNREACHABLE();
1104 }
1105 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106 case D3DXPT_FLOAT:
1107 switch (constantDescription.Columns)
1108 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +00001109 case 1: return new Uniform(GL_FLOAT, name, 1 * sizeof(GLfloat) * constantDescription.Elements);
1110 case 2: return new Uniform(GL_FLOAT_VEC2, name, 2 * sizeof(GLfloat) * constantDescription.Elements);
1111 case 3: return new Uniform(GL_FLOAT_VEC3, name, 3 * sizeof(GLfloat) * constantDescription.Elements);
1112 case 4: return new Uniform(GL_FLOAT_VEC4, name, 4 * sizeof(GLfloat) * constantDescription.Elements);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001113 default: UNREACHABLE();
1114 }
1115 break;
1116 default:
1117 UNIMPLEMENTED(); // FIXME
1118 UNREACHABLE();
1119 }
1120 }
1121 else if (constantDescription.Rows == constantDescription.Columns) // Square matrices
1122 {
1123 switch (constantDescription.Type)
1124 {
1125 case D3DXPT_FLOAT:
1126 switch (constantDescription.Rows)
1127 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +00001128 case 2: return new Uniform(GL_FLOAT_MAT2, name, 2 * 2 * sizeof(GLfloat) * constantDescription.Elements);
1129 case 3: return new Uniform(GL_FLOAT_MAT3, name, 3 * 3 * sizeof(GLfloat) * constantDescription.Elements);
1130 case 4: return new Uniform(GL_FLOAT_MAT4, name, 4 * 4 * sizeof(GLfloat) * constantDescription.Elements);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131 default: UNREACHABLE();
1132 }
1133 break;
1134 default: UNREACHABLE();
1135 }
1136 }
1137 else UNREACHABLE();
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001138
1139 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001140}
1141
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +00001142bool Program::applyUniform1bv(GLint location, GLsizei count, const GLboolean *v)
1143{
1144 BOOL *vector = new BOOL[count];
1145 for (int i = 0; i < count; i++)
1146 {
1147 if (v[i] == GL_FALSE)
1148 vector[i] = 0;
1149 else
1150 vector[i] = 1;
1151 }
1152
1153 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1154 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1155 IDirect3DDevice9 *device = getDevice();
1156
1157 if (constantPS)
1158 {
1159 mConstantTablePS->SetBoolArray(device, constantPS, vector, count);
1160 }
1161
1162 if (constantVS)
1163 {
1164 mConstantTableVS->SetBoolArray(device, constantVS, vector, count);
1165 }
1166
1167 delete [] vector;
1168
1169 return true;
1170}
1171
1172bool Program::applyUniform2bv(GLint location, GLsizei count, const GLboolean *v)
1173{
1174 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1175
1176 for (int i = 0; i < count; i++)
1177 {
1178 vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f),
1179 (v[1] == GL_FALSE ? 0.0f : 1.0f), 0, 0);
1180
1181 v += 2;
1182 }
1183
1184 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1185 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1186 IDirect3DDevice9 *device = getDevice();
1187
1188 if (constantPS)
1189 {
1190 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1191 }
1192
1193 if (constantVS)
1194 {
1195 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1196 }
1197
1198 delete[] vector;
1199
1200 return true;
1201}
1202
1203bool Program::applyUniform3bv(GLint location, GLsizei count, const GLboolean *v)
1204{
1205 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1206
1207 for (int i = 0; i < count; i++)
1208 {
1209 vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f),
1210 (v[1] == GL_FALSE ? 0.0f : 1.0f),
1211 (v[2] == GL_FALSE ? 0.0f : 1.0f), 0);
1212
1213 v += 3;
1214 }
1215
1216 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1217 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1218 IDirect3DDevice9 *device = getDevice();
1219
1220 if (constantPS)
1221 {
1222 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1223 }
1224
1225 if (constantVS)
1226 {
1227 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1228 }
1229
1230 delete[] vector;
1231
1232 return true;
1233}
1234
1235bool Program::applyUniform4bv(GLint location, GLsizei count, const GLboolean *v)
1236{
1237 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1238
1239 for (int i = 0; i < count; i++)
1240 {
1241 vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f),
1242 (v[1] == GL_FALSE ? 0.0f : 1.0f),
1243 (v[2] == GL_FALSE ? 0.0f : 1.0f),
1244 (v[3] == GL_FALSE ? 0.0f : 1.0f));
1245
1246 v += 3;
1247 }
1248
1249 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1250 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1251 IDirect3DDevice9 *device = getDevice();
1252
1253 if (constantPS)
1254 {
1255 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1256 }
1257
1258 if (constantVS)
1259 {
1260 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1261 }
1262
1263 delete [] vector;
1264
1265 return true;
1266}
1267
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001268bool Program::applyUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1269{
1270 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1271 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1272 IDirect3DDevice9 *device = getDevice();
1273
1274 if (constantPS)
1275 {
1276 mConstantTablePS->SetFloatArray(device, constantPS, v, count);
1277 }
1278
1279 if (constantVS)
1280 {
1281 mConstantTableVS->SetFloatArray(device, constantVS, v, count);
1282 }
1283
1284 return true;
1285}
1286
1287bool Program::applyUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1288{
1289 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1290
1291 for (int i = 0; i < count; i++)
1292 {
1293 vector[i] = D3DXVECTOR4(v[0], v[1], 0, 0);
1294
1295 v += 2;
1296 }
1297
1298 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1299 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1300 IDirect3DDevice9 *device = getDevice();
1301
1302 if (constantPS)
1303 {
1304 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1305 }
1306
1307 if (constantVS)
1308 {
1309 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1310 }
1311
1312 delete[] vector;
1313
1314 return true;
1315}
1316
1317bool Program::applyUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1318{
1319 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1320
1321 for (int i = 0; i < count; i++)
1322 {
1323 vector[i] = D3DXVECTOR4(v[0], v[1], v[2], 0);
1324
1325 v += 3;
1326 }
1327
1328 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1329 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1330 IDirect3DDevice9 *device = getDevice();
1331
1332 if (constantPS)
1333 {
1334 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1335 }
1336
1337 if (constantVS)
1338 {
1339 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1340 }
1341
1342 delete[] vector;
1343
1344 return true;
1345}
1346
1347bool Program::applyUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1348{
1349 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1350 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1351 IDirect3DDevice9 *device = getDevice();
1352
1353 if (constantPS)
1354 {
1355 mConstantTablePS->SetVectorArray(device, constantPS, (D3DXVECTOR4*)v, count);
1356 }
1357
1358 if (constantVS)
1359 {
1360 mConstantTableVS->SetVectorArray(device, constantVS, (D3DXVECTOR4*)v, count);
1361 }
1362
1363 return true;
1364}
1365
1366bool Program::applyUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
1367{
1368 D3DXMATRIX *matrix = new D3DXMATRIX[count];
1369
1370 for (int i = 0; i < count; i++)
1371 {
1372 matrix[i] = D3DXMATRIX(value[0], value[2], 0, 0,
1373 value[1], value[3], 0, 0,
1374 0, 0, 1, 0,
1375 0, 0, 0, 1);
1376
1377 value += 4;
1378 }
1379
1380 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1381 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1382 IDirect3DDevice9 *device = getDevice();
1383
1384 if (constantPS)
1385 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001386 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001387 }
1388
1389 if (constantVS)
1390 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001391 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001392 }
1393
1394 delete[] matrix;
1395
1396 return true;
1397}
1398
1399bool Program::applyUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
1400{
1401 D3DXMATRIX *matrix = new D3DXMATRIX[count];
1402
1403 for (int i = 0; i < count; i++)
1404 {
1405 matrix[i] = D3DXMATRIX(value[0], value[3], value[6], 0,
1406 value[1], value[4], value[7], 0,
1407 value[2], value[5], value[8], 0,
1408 0, 0, 0, 1);
1409
1410 value += 9;
1411 }
1412
1413 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1414 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1415 IDirect3DDevice9 *device = getDevice();
1416
1417 if (constantPS)
1418 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001419 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001420 }
1421
1422 if (constantVS)
1423 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001424 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001425 }
1426
1427 delete[] matrix;
1428
1429 return true;
1430}
1431
1432bool Program::applyUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
1433{
1434 D3DXMATRIX *matrix = new D3DXMATRIX[count];
1435
1436 for (int i = 0; i < count; i++)
1437 {
1438 matrix[i] = D3DXMATRIX(value[0], value[4], value[8], value[12],
1439 value[1], value[5], value[9], value[13],
1440 value[2], value[6], value[10], value[14],
1441 value[3], value[7], value[11], value[15]);
1442
1443 value += 16;
1444 }
1445
1446 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1447 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1448 IDirect3DDevice9 *device = getDevice();
1449
1450 if (constantPS)
1451 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001452 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001453 }
1454
1455 if (constantVS)
1456 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +00001457 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001458 }
1459
1460 delete[] matrix;
1461
1462 return true;
1463}
1464
1465bool Program::applyUniform1iv(GLint location, GLsizei count, const GLint *v)
1466{
1467 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1468 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1469 IDirect3DDevice9 *device = getDevice();
1470
1471 if (constantPS)
1472 {
1473 D3DXCONSTANT_DESC constantDescription;
1474 UINT descriptionCount = 1;
1475 HRESULT result = mConstantTablePS->GetConstantDesc(constantPS, &constantDescription, &descriptionCount);
1476
daniel@transgaming.com2884b782010-03-08 21:30:48 +00001477 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001478 {
1479 return false;
1480 }
1481
1482 if (constantDescription.RegisterSet == D3DXRS_SAMPLER)
1483 {
1484 unsigned int firstIndex = mConstantTablePS->GetSamplerIndex(constantPS);
1485
1486 for (unsigned int samplerIndex = firstIndex; samplerIndex < firstIndex + count; samplerIndex++)
1487 {
1488 GLint mappedSampler = v[0];
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001489
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001490 if (mappedSampler >= 0 && mappedSampler < MAX_TEXTURE_IMAGE_UNITS)
1491 {
1492 if (samplerIndex >= 0 && samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1493 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001494 ASSERT(mSamplers[samplerIndex].active);
1495 mSamplers[samplerIndex].logicalTextureUnit = mappedSampler;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001496 }
1497 }
1498 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001499
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001500 return true;
1501 }
1502 }
1503
1504 if (constantPS)
1505 {
1506 mConstantTablePS->SetIntArray(device, constantPS, v, count);
1507 }
1508
1509 if (constantVS)
1510 {
1511 mConstantTableVS->SetIntArray(device, constantVS, v, count);
1512 }
1513
1514 return true;
1515}
1516
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +00001517bool Program::applyUniform2iv(GLint location, GLsizei count, const GLint *v)
1518{
1519 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1520
1521 for (int i = 0; i < count; i++)
1522 {
1523 vector[i] = D3DXVECTOR4((float)v[0], (float)v[1], 0, 0);
1524
1525 v += 2;
1526 }
1527
1528 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1529 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1530 IDirect3DDevice9 *device = getDevice();
1531
1532 if (constantPS)
1533 {
1534 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1535 }
1536
1537 if (constantVS)
1538 {
1539 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1540 }
1541
1542 delete[] vector;
1543
1544 return true;
1545}
1546
1547bool Program::applyUniform3iv(GLint location, GLsizei count, const GLint *v)
1548{
1549 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1550
1551 for (int i = 0; i < count; i++)
1552 {
1553 vector[i] = D3DXVECTOR4((float)v[0], (float)v[1], (float)v[2], 0);
1554
1555 v += 3;
1556 }
1557
1558 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1559 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1560 IDirect3DDevice9 *device = getDevice();
1561
1562 if (constantPS)
1563 {
1564 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1565 }
1566
1567 if (constantVS)
1568 {
1569 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1570 }
1571
1572 delete[] vector;
1573
1574 return true;
1575}
1576
1577bool Program::applyUniform4iv(GLint location, GLsizei count, const GLint *v)
1578{
1579 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
1580
1581 for (int i = 0; i < count; i++)
1582 {
1583 vector[i] = D3DXVECTOR4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
1584
1585 v += 4;
1586 }
1587
1588 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
1589 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
1590 IDirect3DDevice9 *device = getDevice();
1591
1592 if (constantPS)
1593 {
1594 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
1595 }
1596
1597 if (constantVS)
1598 {
1599 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
1600 }
1601
1602 delete [] vector;
1603
1604 return true;
1605}
1606
daniel@transgaming.comcba50572010-03-28 19:36:09 +00001607void Program::appendToInfoLog(const char *info)
1608{
1609 if (!info)
1610 {
1611 return;
1612 }
1613
1614 size_t infoLength = strlen(info);
1615
1616 if (!mInfoLog)
1617 {
1618 mInfoLog = new char[infoLength + 1];
1619 strcpy(mInfoLog, info);
1620 }
1621 else
1622 {
1623 size_t logLength = strlen(mInfoLog);
1624 char *newLog = new char[logLength + infoLength + 1];
1625 strcpy(newLog, mInfoLog);
1626 strcpy(newLog + logLength, info);
1627
1628 delete[] mInfoLog;
1629 mInfoLog = newLog;
1630 }
1631}
1632
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001633// Returns the program object to an unlinked state, after detaching a shader, before re-linking, or at destruction
1634void Program::unlink(bool destroy)
1635{
1636 if (destroy) // Object being destructed
1637 {
1638 if (mFragmentShader)
1639 {
1640 mFragmentShader->detach();
1641 mFragmentShader = NULL;
1642 }
1643
1644 if (mVertexShader)
1645 {
1646 mVertexShader->detach();
1647 mVertexShader = NULL;
1648 }
1649
1650 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
1651 {
1652 delete[] mAttributeName[index];
1653 mAttributeName[index] = NULL;
1654 }
1655 }
1656
1657 if (mPixelExecutable)
1658 {
1659 mPixelExecutable->Release();
1660 mPixelExecutable = NULL;
1661 }
1662
1663 if (mVertexExecutable)
1664 {
1665 mVertexExecutable->Release();
1666 mVertexExecutable = NULL;
1667 }
1668
1669 if (mConstantTablePS)
1670 {
1671 mConstantTablePS->Release();
1672 mConstantTablePS = NULL;
1673 }
1674
1675 if (mConstantTableVS)
1676 {
1677 mConstantTableVS->Release();
1678 mConstantTableVS = NULL;
1679 }
1680
1681 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
1682 {
1683 mInputMapping[index] = 0;
1684 }
1685
1686 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
1687 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001688 mSamplers[index].active = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689 }
1690
1691 while (!mUniforms.empty())
1692 {
1693 delete mUniforms.back();
1694 mUniforms.pop_back();
1695 }
1696
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +00001697 delete[] mPixelHLSL;
1698 mPixelHLSL = NULL;
1699
1700 delete[] mVertexHLSL;
1701 mVertexHLSL = NULL;
1702
1703 delete[] mInfoLog;
1704 mInfoLog = NULL;
1705
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001706 mLinked = false;
1707}
1708
1709bool Program::isLinked()
1710{
1711 return mLinked;
1712}
1713
daniel@transgaming.comcba50572010-03-28 19:36:09 +00001714int Program::getInfoLogLength() const
1715{
1716 if (!mInfoLog)
1717 {
1718 return 0;
1719 }
1720 else
1721 {
1722 return strlen(mInfoLog) + 1;
1723 }
1724}
1725
1726void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog)
1727{
1728 int index = 0;
1729
1730 if (mInfoLog)
1731 {
1732 while (index < bufSize - 1 && index < (int)strlen(mInfoLog))
1733 {
1734 infoLog[index] = mInfoLog[index];
1735 index++;
1736 }
1737 }
1738
1739 if (bufSize)
1740 {
1741 infoLog[index] = '\0';
1742 }
1743
1744 if (length)
1745 {
1746 *length = index;
1747 }
1748}
1749
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001750void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders)
1751{
1752 int total = 0;
1753
1754 if (mVertexShader)
1755 {
1756 if (total < maxCount)
1757 {
1758 shaders[total] = mVertexShader->getHandle();
1759 }
1760
1761 total++;
1762 }
1763
1764 if (mFragmentShader)
1765 {
1766 if (total < maxCount)
1767 {
1768 shaders[total] = mFragmentShader->getHandle();
1769 }
1770
1771 total++;
1772 }
1773
1774 if (count)
1775 {
1776 *count = total;
1777 }
1778}
1779
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001780void Program::flagForDeletion()
1781{
1782 mDeleteStatus = true;
1783}
1784
1785bool Program::isFlaggedForDeletion() const
1786{
1787 return mDeleteStatus;
1788}
1789}