blob: 3977f20d18dd41657342a79f64a0e6300287a34c [file] [log] [blame]
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001//
2// Copyright (c) 2002-2012 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
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +000010#include "libGLESv2/BinaryStream.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000011#include "libGLESv2/Program.h"
12#include "libGLESv2/ProgramBinary.h"
13
14#include "common/debug.h"
apatrick@chromium.org90080e32012-07-09 22:15:33 +000015#include "common/version.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000016
17#include "libGLESv2/main.h"
18#include "libGLESv2/Shader.h"
19#include "libGLESv2/utilities.h"
20
21#include <string>
22
23#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
24#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
25#endif
26
27namespace gl
28{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000029std::string str(int i)
30{
31 char buffer[20];
32 snprintf(buffer, sizeof(buffer), "%d", i);
33 return buffer;
34}
35
36Uniform::Uniform(GLenum type, const std::string &_name, unsigned int arraySize)
37 : type(type), _name(_name), name(ProgramBinary::undecorateUniform(_name)), arraySize(arraySize)
38{
39 int bytes = UniformInternalSize(type) * arraySize;
40 data = new unsigned char[bytes];
41 memset(data, 0, bytes);
42 dirty = true;
43}
44
45Uniform::~Uniform()
46{
47 delete[] data;
48}
49
50bool Uniform::isArray()
51{
52 return _name.compare(0, 3, "ar_") == 0;
53}
54
55UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
56 : name(ProgramBinary::undecorateUniform(_name)), element(element), index(index)
57{
58}
59
60ProgramBinary::ProgramBinary()
61{
62 mDevice = getDevice();
63
64 mPixelExecutable = NULL;
65 mVertexExecutable = NULL;
66 mConstantTablePS = NULL;
67 mConstantTableVS = NULL;
68
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000069 mValidated = false;
70
71 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
72 {
73 mSemanticIndex[index] = -1;
74 }
75
76 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
77 {
78 mSamplersPS[index].active = false;
79 }
80
81 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
82 {
83 mSamplersVS[index].active = false;
84 }
85
86 mUsedVertexSamplerRange = 0;
87 mUsedPixelSamplerRange = 0;
88
89 mDxDepthRangeLocation = -1;
90 mDxDepthLocation = -1;
91 mDxCoordLocation = -1;
92 mDxHalfPixelSizeLocation = -1;
93 mDxFrontCCWLocation = -1;
94 mDxPointsOrLinesLocation = -1;
95}
96
97ProgramBinary::~ProgramBinary()
98{
99 if (mPixelExecutable)
100 {
101 mPixelExecutable->Release();
102 }
103
104 if (mVertexExecutable)
105 {
106 mVertexExecutable->Release();
107 }
108
109 if (mConstantTablePS)
110 {
111 mConstantTablePS->Release();
112 }
113
114 if (mConstantTableVS)
115 {
116 mConstantTableVS->Release();
117 }
118
119 while (!mUniforms.empty())
120 {
121 delete mUniforms.back();
122 mUniforms.pop_back();
123 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000124}
125
126IDirect3DPixelShader9 *ProgramBinary::getPixelShader()
127{
128 return mPixelExecutable;
129}
130
131IDirect3DVertexShader9 *ProgramBinary::getVertexShader()
132{
133 return mVertexExecutable;
134}
135
136GLuint ProgramBinary::getAttributeLocation(const char *name)
137{
138 if (name)
139 {
140 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
141 {
142 if (mLinkedAttribute[index].name == std::string(name))
143 {
144 return index;
145 }
146 }
147 }
148
149 return -1;
150}
151
152int ProgramBinary::getSemanticIndex(int attributeIndex)
153{
154 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
155
156 return mSemanticIndex[attributeIndex];
157}
158
159// Returns one more than the highest sampler index used.
160GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
161{
162 switch (type)
163 {
164 case SAMPLER_PIXEL:
165 return mUsedPixelSamplerRange;
166 case SAMPLER_VERTEX:
167 return mUsedVertexSamplerRange;
168 default:
169 UNREACHABLE();
170 return 0;
171 }
172}
173
174// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
175// index (0-15 for the pixel shader and 0-3 for the vertex shader).
176GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
177{
178 GLint logicalTextureUnit = -1;
179
180 switch (type)
181 {
182 case SAMPLER_PIXEL:
183 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
184
185 if (mSamplersPS[samplerIndex].active)
186 {
187 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
188 }
189 break;
190 case SAMPLER_VERTEX:
191 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
192
193 if (mSamplersVS[samplerIndex].active)
194 {
195 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
196 }
197 break;
198 default: UNREACHABLE();
199 }
200
201 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
202 {
203 return logicalTextureUnit;
204 }
205
206 return -1;
207}
208
209// Returns the texture type for a given Direct3D 9 sampler type and
210// index (0-15 for the pixel shader and 0-3 for the vertex shader).
211TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
212{
213 switch (type)
214 {
215 case SAMPLER_PIXEL:
216 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
217 ASSERT(mSamplersPS[samplerIndex].active);
218 return mSamplersPS[samplerIndex].textureType;
219 case SAMPLER_VERTEX:
220 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
221 ASSERT(mSamplersVS[samplerIndex].active);
222 return mSamplersVS[samplerIndex].textureType;
223 default: UNREACHABLE();
224 }
225
226 return TEXTURE_2D;
227}
228
229GLint ProgramBinary::getUniformLocation(std::string name)
230{
231 unsigned int subscript = 0;
232
233 // Strip any trailing array operator and retrieve the subscript
234 size_t open = name.find_last_of('[');
235 size_t close = name.find_last_of(']');
236 if (open != std::string::npos && close == name.length() - 1)
237 {
238 subscript = atoi(name.substr(open + 1).c_str());
239 name.erase(open);
240 }
241
242 unsigned int numUniforms = mUniformIndex.size();
243 for (unsigned int location = 0; location < numUniforms; location++)
244 {
245 if (mUniformIndex[location].name == name &&
246 mUniformIndex[location].element == subscript)
247 {
248 return location;
249 }
250 }
251
252 return -1;
253}
254
255bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
256{
257 if (location < 0 || location >= (int)mUniformIndex.size())
258 {
259 return false;
260 }
261
262 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
263 targetUniform->dirty = true;
264
265 if (targetUniform->type == GL_FLOAT)
266 {
267 int arraySize = targetUniform->arraySize;
268
269 if (arraySize == 1 && count > 1)
270 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
271
272 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
273
274 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
275
276 for (int i = 0; i < count; i++)
277 {
278 target[0] = v[0];
279 target[1] = 0;
280 target[2] = 0;
281 target[3] = 0;
282 target += 4;
283 v += 1;
284 }
285 }
286 else if (targetUniform->type == GL_BOOL)
287 {
288 int arraySize = targetUniform->arraySize;
289
290 if (arraySize == 1 && count > 1)
291 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
292
293 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
294 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
295
296 for (int i = 0; i < count; ++i)
297 {
298 if (v[i] == 0.0f)
299 {
300 boolParams[i] = GL_FALSE;
301 }
302 else
303 {
304 boolParams[i] = GL_TRUE;
305 }
306 }
307 }
308 else
309 {
310 return false;
311 }
312
313 return true;
314}
315
316bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
317{
318 if (location < 0 || location >= (int)mUniformIndex.size())
319 {
320 return false;
321 }
322
323 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
324 targetUniform->dirty = true;
325
326 if (targetUniform->type == GL_FLOAT_VEC2)
327 {
328 int arraySize = targetUniform->arraySize;
329
330 if (arraySize == 1 && count > 1)
331 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
332
333 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
334
335 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
336
337 for (int i = 0; i < count; i++)
338 {
339 target[0] = v[0];
340 target[1] = v[1];
341 target[2] = 0;
342 target[3] = 0;
343 target += 4;
344 v += 2;
345 }
346 }
347 else if (targetUniform->type == GL_BOOL_VEC2)
348 {
349 int arraySize = targetUniform->arraySize;
350
351 if (arraySize == 1 && count > 1)
352 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
353
354 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
355
356 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
357
358 for (int i = 0; i < count * 2; ++i)
359 {
360 if (v[i] == 0.0f)
361 {
362 boolParams[i] = GL_FALSE;
363 }
364 else
365 {
366 boolParams[i] = GL_TRUE;
367 }
368 }
369 }
370 else
371 {
372 return false;
373 }
374
375 return true;
376}
377
378bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
379{
380 if (location < 0 || location >= (int)mUniformIndex.size())
381 {
382 return false;
383 }
384
385 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
386 targetUniform->dirty = true;
387
388 if (targetUniform->type == GL_FLOAT_VEC3)
389 {
390 int arraySize = targetUniform->arraySize;
391
392 if (arraySize == 1 && count > 1)
393 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
394
395 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
396
397 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
398
399 for (int i = 0; i < count; i++)
400 {
401 target[0] = v[0];
402 target[1] = v[1];
403 target[2] = v[2];
404 target[3] = 0;
405 target += 4;
406 v += 3;
407 }
408 }
409 else if (targetUniform->type == GL_BOOL_VEC3)
410 {
411 int arraySize = targetUniform->arraySize;
412
413 if (arraySize == 1 && count > 1)
414 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
415
416 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
417 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
418
419 for (int i = 0; i < count * 3; ++i)
420 {
421 if (v[i] == 0.0f)
422 {
423 boolParams[i] = GL_FALSE;
424 }
425 else
426 {
427 boolParams[i] = GL_TRUE;
428 }
429 }
430 }
431 else
432 {
433 return false;
434 }
435
436 return true;
437}
438
439bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
440{
441 if (location < 0 || location >= (int)mUniformIndex.size())
442 {
443 return false;
444 }
445
446 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
447 targetUniform->dirty = true;
448
449 if (targetUniform->type == GL_FLOAT_VEC4)
450 {
451 int arraySize = targetUniform->arraySize;
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 - (int)mUniformIndex[location].element, count);
457
458 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
459 v, 4 * sizeof(GLfloat) * count);
460 }
461 else if (targetUniform->type == GL_BOOL_VEC4)
462 {
463 int arraySize = targetUniform->arraySize;
464
465 if (arraySize == 1 && count > 1)
466 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
467
468 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
469 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
470
471 for (int i = 0; i < count * 4; ++i)
472 {
473 if (v[i] == 0.0f)
474 {
475 boolParams[i] = GL_FALSE;
476 }
477 else
478 {
479 boolParams[i] = GL_TRUE;
480 }
481 }
482 }
483 else
484 {
485 return false;
486 }
487
488 return true;
489}
490
491template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
492void transposeMatrix(T *target, const GLfloat *value)
493{
494 int copyWidth = std::min(targetWidth, srcWidth);
495 int copyHeight = std::min(targetHeight, srcHeight);
496
497 for (int x = 0; x < copyWidth; x++)
498 {
499 for (int y = 0; y < copyHeight; y++)
500 {
501 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
502 }
503 }
504 // clear unfilled right side
505 for (int y = 0; y < copyHeight; y++)
506 {
507 for (int x = srcWidth; x < targetWidth; x++)
508 {
509 target[y * targetWidth + x] = (T)0;
510 }
511 }
512 // clear unfilled bottom.
513 for (int y = srcHeight; y < targetHeight; y++)
514 {
515 for (int x = 0; x < targetWidth; x++)
516 {
517 target[y * targetWidth + x] = (T)0;
518 }
519 }
520}
521
522bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
523{
524 if (location < 0 || location >= (int)mUniformIndex.size())
525 {
526 return false;
527 }
528
529 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
530 targetUniform->dirty = true;
531
532 if (targetUniform->type != GL_FLOAT_MAT2)
533 {
534 return false;
535 }
536
537 int arraySize = targetUniform->arraySize;
538
539 if (arraySize == 1 && count > 1)
540 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
541
542 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
543
544 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
545 for (int i = 0; i < count; i++)
546 {
547 transposeMatrix<GLfloat,4,2,2,2>(target, value);
548 target += 8;
549 value += 4;
550 }
551
552 return true;
553}
554
555bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
556{
557 if (location < 0 || location >= (int)mUniformIndex.size())
558 {
559 return false;
560 }
561
562 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
563 targetUniform->dirty = true;
564
565 if (targetUniform->type != GL_FLOAT_MAT3)
566 {
567 return false;
568 }
569
570 int arraySize = targetUniform->arraySize;
571
572 if (arraySize == 1 && count > 1)
573 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
574
575 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
576
577 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
578 for (int i = 0; i < count; i++)
579 {
580 transposeMatrix<GLfloat,4,3,3,3>(target, value);
581 target += 12;
582 value += 9;
583 }
584
585 return true;
586}
587
588
589bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
590{
591 if (location < 0 || location >= (int)mUniformIndex.size())
592 {
593 return false;
594 }
595
596 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
597 targetUniform->dirty = true;
598
599 if (targetUniform->type != GL_FLOAT_MAT4)
600 {
601 return false;
602 }
603
604 int arraySize = targetUniform->arraySize;
605
606 if (arraySize == 1 && count > 1)
607 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
608
609 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
610
611 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
612 for (int i = 0; i < count; i++)
613 {
614 transposeMatrix<GLfloat,4,4,4,4>(target, value);
615 target += 16;
616 value += 16;
617 }
618
619 return true;
620}
621
622bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
623{
624 if (location < 0 || location >= (int)mUniformIndex.size())
625 {
626 return false;
627 }
628
629 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
630 targetUniform->dirty = true;
631
632 if (targetUniform->type == GL_INT ||
633 targetUniform->type == GL_SAMPLER_2D ||
634 targetUniform->type == GL_SAMPLER_CUBE)
635 {
636 int arraySize = targetUniform->arraySize;
637
638 if (arraySize == 1 && count > 1)
639 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
640
641 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
642
643 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
644 v, sizeof(GLint) * count);
645 }
646 else if (targetUniform->type == GL_BOOL)
647 {
648 int arraySize = targetUniform->arraySize;
649
650 if (arraySize == 1 && count > 1)
651 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
652
653 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
654 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
655
656 for (int i = 0; i < count; ++i)
657 {
658 if (v[i] == 0)
659 {
660 boolParams[i] = GL_FALSE;
661 }
662 else
663 {
664 boolParams[i] = GL_TRUE;
665 }
666 }
667 }
668 else
669 {
670 return false;
671 }
672
673 return true;
674}
675
676bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
677{
678 if (location < 0 || location >= (int)mUniformIndex.size())
679 {
680 return false;
681 }
682
683 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
684 targetUniform->dirty = true;
685
686 if (targetUniform->type == GL_INT_VEC2)
687 {
688 int arraySize = targetUniform->arraySize;
689
690 if (arraySize == 1 && count > 1)
691 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
692
693 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
694
695 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
696 v, 2 * sizeof(GLint) * count);
697 }
698 else if (targetUniform->type == GL_BOOL_VEC2)
699 {
700 int arraySize = targetUniform->arraySize;
701
702 if (arraySize == 1 && count > 1)
703 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
704
705 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
706 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
707
708 for (int i = 0; i < count * 2; ++i)
709 {
710 if (v[i] == 0)
711 {
712 boolParams[i] = GL_FALSE;
713 }
714 else
715 {
716 boolParams[i] = GL_TRUE;
717 }
718 }
719 }
720 else
721 {
722 return false;
723 }
724
725 return true;
726}
727
728bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
729{
730 if (location < 0 || location >= (int)mUniformIndex.size())
731 {
732 return false;
733 }
734
735 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
736 targetUniform->dirty = true;
737
738 if (targetUniform->type == GL_INT_VEC3)
739 {
740 int arraySize = targetUniform->arraySize;
741
742 if (arraySize == 1 && count > 1)
743 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
744
745 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
746
747 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
748 v, 3 * sizeof(GLint) * count);
749 }
750 else if (targetUniform->type == GL_BOOL_VEC3)
751 {
752 int arraySize = targetUniform->arraySize;
753
754 if (arraySize == 1 && count > 1)
755 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
756
757 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
758 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
759
760 for (int i = 0; i < count * 3; ++i)
761 {
762 if (v[i] == 0)
763 {
764 boolParams[i] = GL_FALSE;
765 }
766 else
767 {
768 boolParams[i] = GL_TRUE;
769 }
770 }
771 }
772 else
773 {
774 return false;
775 }
776
777 return true;
778}
779
780bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
781{
782 if (location < 0 || location >= (int)mUniformIndex.size())
783 {
784 return false;
785 }
786
787 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
788 targetUniform->dirty = true;
789
790 if (targetUniform->type == GL_INT_VEC4)
791 {
792 int arraySize = targetUniform->arraySize;
793
794 if (arraySize == 1 && count > 1)
795 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
796
797 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
798
799 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
800 v, 4 * sizeof(GLint) * count);
801 }
802 else if (targetUniform->type == GL_BOOL_VEC4)
803 {
804 int arraySize = targetUniform->arraySize;
805
806 if (arraySize == 1 && count > 1)
807 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
808
809 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
810 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
811
812 for (int i = 0; i < count * 4; ++i)
813 {
814 if (v[i] == 0)
815 {
816 boolParams[i] = GL_FALSE;
817 }
818 else
819 {
820 boolParams[i] = GL_TRUE;
821 }
822 }
823 }
824 else
825 {
826 return false;
827 }
828
829 return true;
830}
831
832bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
833{
834 if (location < 0 || location >= (int)mUniformIndex.size())
835 {
836 return false;
837 }
838
839 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
840
841 // sized queries -- ensure the provided buffer is large enough
842 if (bufSize)
843 {
844 int requiredBytes = UniformExternalSize(targetUniform->type);
845 if (*bufSize < requiredBytes)
846 {
847 return false;
848 }
849 }
850
851 switch (targetUniform->type)
852 {
853 case GL_FLOAT_MAT2:
854 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
855 break;
856 case GL_FLOAT_MAT3:
857 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
858 break;
859 case GL_FLOAT_MAT4:
860 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
861 break;
862 default:
863 {
864 unsigned int count = UniformExternalComponentCount(targetUniform->type);
865 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
866
867 switch (UniformComponentType(targetUniform->type))
868 {
869 case GL_BOOL:
870 {
871 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
872
873 for (unsigned int i = 0; i < count; ++i)
874 {
875 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
876 }
877 }
878 break;
879 case GL_FLOAT:
880 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
881 count * sizeof(GLfloat));
882 break;
883 case GL_INT:
884 {
885 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
886
887 for (unsigned int i = 0; i < count; ++i)
888 {
889 params[i] = (float)intParams[i];
890 }
891 }
892 break;
893 default: UNREACHABLE();
894 }
895 }
896 }
897
898 return true;
899}
900
901bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
902{
903 if (location < 0 || location >= (int)mUniformIndex.size())
904 {
905 return false;
906 }
907
908 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
909
910 // sized queries -- ensure the provided buffer is large enough
911 if (bufSize)
912 {
913 int requiredBytes = UniformExternalSize(targetUniform->type);
914 if (*bufSize < requiredBytes)
915 {
916 return false;
917 }
918 }
919
920 switch (targetUniform->type)
921 {
922 case GL_FLOAT_MAT2:
923 {
924 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
925 }
926 break;
927 case GL_FLOAT_MAT3:
928 {
929 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
930 }
931 break;
932 case GL_FLOAT_MAT4:
933 {
934 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
935 }
936 break;
937 default:
938 {
939 unsigned int count = UniformExternalComponentCount(targetUniform->type);
940 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
941
942 switch (UniformComponentType(targetUniform->type))
943 {
944 case GL_BOOL:
945 {
946 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
947
948 for (unsigned int i = 0; i < count; ++i)
949 {
950 params[i] = (GLint)boolParams[i];
951 }
952 }
953 break;
954 case GL_FLOAT:
955 {
956 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
957
958 for (unsigned int i = 0; i < count; ++i)
959 {
960 params[i] = (GLint)floatParams[i];
961 }
962 }
963 break;
964 case GL_INT:
965 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
966 count * sizeof(GLint));
967 break;
968 default: UNREACHABLE();
969 }
970 }
971 }
972
973 return true;
974}
975
976void ProgramBinary::dirtyAllUniforms()
977{
978 unsigned int numUniforms = mUniforms.size();
979 for (unsigned int index = 0; index < numUniforms; index++)
980 {
981 mUniforms[index]->dirty = true;
982 }
983}
984
985// Applies all the uniforms set for this program object to the Direct3D 9 device
986void ProgramBinary::applyUniforms()
987{
988 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub) {
989 Uniform *targetUniform = *ub;
990
991 if (targetUniform->dirty)
992 {
993 int arraySize = targetUniform->arraySize;
994 GLfloat *f = (GLfloat*)targetUniform->data;
995 GLint *i = (GLint*)targetUniform->data;
996 GLboolean *b = (GLboolean*)targetUniform->data;
997
998 switch (targetUniform->type)
999 {
1000 case GL_BOOL: applyUniformnbv(targetUniform, arraySize, 1, b); break;
1001 case GL_BOOL_VEC2: applyUniformnbv(targetUniform, arraySize, 2, b); break;
1002 case GL_BOOL_VEC3: applyUniformnbv(targetUniform, arraySize, 3, b); break;
1003 case GL_BOOL_VEC4: applyUniformnbv(targetUniform, arraySize, 4, b); break;
1004 case GL_FLOAT:
1005 case GL_FLOAT_VEC2:
1006 case GL_FLOAT_VEC3:
1007 case GL_FLOAT_VEC4:
1008 case GL_FLOAT_MAT2:
1009 case GL_FLOAT_MAT3:
1010 case GL_FLOAT_MAT4: applyUniformnfv(targetUniform, f); break;
1011 case GL_SAMPLER_2D:
1012 case GL_SAMPLER_CUBE:
1013 case GL_INT: applyUniform1iv(targetUniform, arraySize, i); break;
1014 case GL_INT_VEC2: applyUniform2iv(targetUniform, arraySize, i); break;
1015 case GL_INT_VEC3: applyUniform3iv(targetUniform, arraySize, i); break;
1016 case GL_INT_VEC4: applyUniform4iv(targetUniform, arraySize, i); break;
1017 default:
1018 UNREACHABLE();
1019 }
1020
1021 targetUniform->dirty = false;
1022 }
1023 }
1024}
1025
1026// Compiles the HLSL code of the attached shaders into executable binaries
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001027ID3D10Blob *ProgramBinary::compileToBinary(InfoLog &infoLog, const char *hlsl, const char *profile, ID3DXConstantTable **constantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001028{
1029 if (!hlsl)
1030 {
1031 return NULL;
1032 }
1033
1034 DWORD result;
1035 UINT flags = 0;
1036 std::string sourceText;
1037 if (perfActive())
1038 {
1039 flags |= D3DCOMPILE_DEBUG;
1040#ifdef NDEBUG
1041 flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
1042#else
1043 flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
1044#endif
1045
1046 std::string sourcePath = getTempPath();
1047 sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl);
1048 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
1049 }
1050 else
1051 {
1052 flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
1053 sourceText = hlsl;
1054 }
1055
1056 ID3D10Blob *binary = NULL;
1057 ID3D10Blob *errorMessage = NULL;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001058 result = D3DCompile(hlsl, strlen(hlsl), g_fakepath, NULL, NULL, "main", profile, flags, 0, &binary, &errorMessage);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001059
1060 if (errorMessage)
1061 {
1062 const char *message = (const char*)errorMessage->GetBufferPointer();
1063
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001064 infoLog.appendSanitized(message);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001065 TRACE("\n%s", hlsl);
1066 TRACE("\n%s", message);
1067
1068 errorMessage->Release();
1069 errorMessage = NULL;
1070 }
1071
1072 if (FAILED(result))
1073 {
1074 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1075 {
1076 error(GL_OUT_OF_MEMORY);
1077 }
1078
1079 return NULL;
1080 }
1081
1082 result = D3DXGetShaderConstantTable(static_cast<const DWORD*>(binary->GetBufferPointer()), constantTable);
1083
1084 if (FAILED(result))
1085 {
1086 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1087 {
1088 error(GL_OUT_OF_MEMORY);
1089 }
1090
1091 binary->Release();
1092
1093 return NULL;
1094 }
1095
1096 return binary;
1097}
1098
1099// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1100// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001101int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001102{
1103 Context *context = getContext();
1104 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1105
1106 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1107 {
1108 int n = VariableRowCount(varying->type) * varying->size;
1109 int m = VariableColumnCount(varying->type);
1110 bool success = false;
1111
1112 if (m == 2 || m == 3 || m == 4)
1113 {
1114 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1115 {
1116 bool available = true;
1117
1118 for (int y = 0; y < n && available; y++)
1119 {
1120 for (int x = 0; x < m && available; x++)
1121 {
1122 if (packing[r + y][x])
1123 {
1124 available = false;
1125 }
1126 }
1127 }
1128
1129 if (available)
1130 {
1131 varying->reg = r;
1132 varying->col = 0;
1133
1134 for (int y = 0; y < n; y++)
1135 {
1136 for (int x = 0; x < m; x++)
1137 {
1138 packing[r + y][x] = &*varying;
1139 }
1140 }
1141
1142 success = true;
1143 }
1144 }
1145
1146 if (!success && m == 2)
1147 {
1148 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1149 {
1150 bool available = true;
1151
1152 for (int y = 0; y < n && available; y++)
1153 {
1154 for (int x = 2; x < 4 && available; x++)
1155 {
1156 if (packing[r + y][x])
1157 {
1158 available = false;
1159 }
1160 }
1161 }
1162
1163 if (available)
1164 {
1165 varying->reg = r;
1166 varying->col = 2;
1167
1168 for (int y = 0; y < n; y++)
1169 {
1170 for (int x = 2; x < 4; x++)
1171 {
1172 packing[r + y][x] = &*varying;
1173 }
1174 }
1175
1176 success = true;
1177 }
1178 }
1179 }
1180 }
1181 else if (m == 1)
1182 {
1183 int space[4] = {0};
1184
1185 for (int y = 0; y < maxVaryingVectors; y++)
1186 {
1187 for (int x = 0; x < 4; x++)
1188 {
1189 space[x] += packing[y][x] ? 0 : 1;
1190 }
1191 }
1192
1193 int column = 0;
1194
1195 for (int x = 0; x < 4; x++)
1196 {
1197 if (space[x] >= n && space[x] < space[column])
1198 {
1199 column = x;
1200 }
1201 }
1202
1203 if (space[column] >= n)
1204 {
1205 for (int r = 0; r < maxVaryingVectors; r++)
1206 {
1207 if (!packing[r][column])
1208 {
1209 varying->reg = r;
1210
1211 for (int y = r; y < r + n; y++)
1212 {
1213 packing[y][column] = &*varying;
1214 }
1215
1216 break;
1217 }
1218 }
1219
1220 varying->col = column;
1221
1222 success = true;
1223 }
1224 }
1225 else UNREACHABLE();
1226
1227 if (!success)
1228 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001229 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001230
1231 return -1;
1232 }
1233 }
1234
1235 // Return the number of used registers
1236 int registers = 0;
1237
1238 for (int r = 0; r < maxVaryingVectors; r++)
1239 {
1240 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1241 {
1242 registers++;
1243 }
1244 }
1245
1246 return registers;
1247}
1248
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001249bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001250{
1251 if (pixelHLSL.empty() || vertexHLSL.empty())
1252 {
1253 return false;
1254 }
1255
1256 // Reset the varying register assignments
1257 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1258 {
1259 fragVar->reg = -1;
1260 fragVar->col = -1;
1261 }
1262
1263 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1264 {
1265 vtxVar->reg = -1;
1266 vtxVar->col = -1;
1267 }
1268
1269 // Map the varyings to the register file
1270 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001271 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001272
1273 if (registers < 0)
1274 {
1275 return false;
1276 }
1277
1278 // Write the HLSL input/output declarations
1279 Context *context = getContext();
1280 const bool sm3 = context->supportsShaderModel3();
1281 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1282
1283 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1284 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001285 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001286
1287 return false;
1288 }
1289
1290 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1291 {
1292 bool matched = false;
1293
1294 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1295 {
1296 if (output->name == input->name)
1297 {
1298 if (output->type != input->type || output->size != input->size)
1299 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001300 infoLog.append("Type of vertex varying %s does not match that of the fragment varying", output->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001301
1302 return false;
1303 }
1304
1305 output->reg = input->reg;
1306 output->col = input->col;
1307
1308 matched = true;
1309 break;
1310 }
1311 }
1312
1313 if (!matched)
1314 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001315 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001316
1317 return false;
1318 }
1319 }
1320
daniel@transgaming.com08b3e402012-07-04 19:16:35 +00001321 std::string varyingSemantic = (vertexShader->mUsesPointSize && sm3 ? "COLOR" : "TEXCOORD");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001322
1323 vertexHLSL += "struct VS_INPUT\n"
1324 "{\n";
1325
1326 int semanticIndex = 0;
1327 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1328 {
1329 switch (attribute->type)
1330 {
1331 case GL_FLOAT: vertexHLSL += " float "; break;
1332 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1333 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1334 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1335 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1336 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1337 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1338 default: UNREACHABLE();
1339 }
1340
1341 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1342
1343 semanticIndex += VariableRowCount(attribute->type);
1344 }
1345
1346 vertexHLSL += "};\n"
1347 "\n"
1348 "struct VS_OUTPUT\n"
1349 "{\n"
1350 " float4 gl_Position : POSITION;\n";
1351
1352 for (int r = 0; r < registers; r++)
1353 {
1354 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1355
1356 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1357 }
1358
1359 if (fragmentShader->mUsesFragCoord)
1360 {
1361 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1362 }
1363
1364 if (vertexShader->mUsesPointSize && sm3)
1365 {
1366 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1367 }
1368
1369 vertexHLSL += "};\n"
1370 "\n"
1371 "VS_OUTPUT main(VS_INPUT input)\n"
1372 "{\n";
1373
1374 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1375 {
1376 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1377
1378 if (VariableRowCount(attribute->type) > 1) // Matrix
1379 {
1380 vertexHLSL += "transpose";
1381 }
1382
1383 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1384 }
1385
1386 vertexHLSL += "\n"
1387 " gl_main();\n"
1388 "\n"
1389 " VS_OUTPUT output;\n"
1390 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001391 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001392 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1393 " output.gl_Position.w = gl_Position.w;\n";
1394
1395 if (vertexShader->mUsesPointSize && sm3)
1396 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001397 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001398 }
1399
1400 if (fragmentShader->mUsesFragCoord)
1401 {
1402 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1403 }
1404
1405 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1406 {
1407 if (varying->reg >= 0)
1408 {
1409 for (int i = 0; i < varying->size; i++)
1410 {
1411 int rows = VariableRowCount(varying->type);
1412
1413 for (int j = 0; j < rows; j++)
1414 {
1415 int r = varying->reg + i * rows + j;
1416 vertexHLSL += " output.v" + str(r);
1417
1418 bool sharedRegister = false; // Register used by multiple varyings
1419
1420 for (int x = 0; x < 4; x++)
1421 {
1422 if (packing[r][x] && packing[r][x] != packing[r][0])
1423 {
1424 sharedRegister = true;
1425 break;
1426 }
1427 }
1428
1429 if(sharedRegister)
1430 {
1431 vertexHLSL += ".";
1432
1433 for (int x = 0; x < 4; x++)
1434 {
1435 if (packing[r][x] == &*varying)
1436 {
1437 switch(x)
1438 {
1439 case 0: vertexHLSL += "x"; break;
1440 case 1: vertexHLSL += "y"; break;
1441 case 2: vertexHLSL += "z"; break;
1442 case 3: vertexHLSL += "w"; break;
1443 }
1444 }
1445 }
1446 }
1447
1448 vertexHLSL += " = " + varying->name;
1449
1450 if (varying->array)
1451 {
1452 vertexHLSL += "[" + str(i) + "]";
1453 }
1454
1455 if (rows > 1)
1456 {
1457 vertexHLSL += "[" + str(j) + "]";
1458 }
1459
1460 vertexHLSL += ";\n";
1461 }
1462 }
1463 }
1464 }
1465
1466 vertexHLSL += "\n"
1467 " return output;\n"
1468 "}\n";
1469
1470 pixelHLSL += "struct PS_INPUT\n"
1471 "{\n";
1472
1473 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1474 {
1475 if (varying->reg >= 0)
1476 {
1477 for (int i = 0; i < varying->size; i++)
1478 {
1479 int rows = VariableRowCount(varying->type);
1480 for (int j = 0; j < rows; j++)
1481 {
1482 std::string n = str(varying->reg + i * rows + j);
1483 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1484 }
1485 }
1486 }
1487 else UNREACHABLE();
1488 }
1489
1490 if (fragmentShader->mUsesFragCoord)
1491 {
1492 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1493 if (sm3) {
1494 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1495 }
1496 }
1497
1498 if (fragmentShader->mUsesPointCoord && sm3)
1499 {
1500 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1501 }
1502
1503 if (fragmentShader->mUsesFrontFacing)
1504 {
1505 pixelHLSL += " float vFace : VFACE;\n";
1506 }
1507
1508 pixelHLSL += "};\n"
1509 "\n"
1510 "struct PS_OUTPUT\n"
1511 "{\n"
1512 " float4 gl_Color[1] : COLOR;\n"
1513 "};\n"
1514 "\n"
1515 "PS_OUTPUT main(PS_INPUT input)\n"
1516 "{\n";
1517
1518 if (fragmentShader->mUsesFragCoord)
1519 {
1520 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1521
1522 if (sm3)
1523 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001524 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001525 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001526 }
1527 else
1528 {
1529 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1530 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001531 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001532 }
1533
1534 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_Depth.x + dx_Depth.y;\n"
1535 " gl_FragCoord.w = rhw;\n";
1536 }
1537
1538 if (fragmentShader->mUsesPointCoord && sm3)
1539 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001540 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1541 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001542 }
1543
1544 if (fragmentShader->mUsesFrontFacing)
1545 {
1546 pixelHLSL += " gl_FrontFacing = dx_PointsOrLines || (dx_FrontCCW ? (input.vFace >= 0.0) : (input.vFace <= 0.0));\n";
1547 }
1548
1549 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1550 {
1551 if (varying->reg >= 0)
1552 {
1553 for (int i = 0; i < varying->size; i++)
1554 {
1555 int rows = VariableRowCount(varying->type);
1556 for (int j = 0; j < rows; j++)
1557 {
1558 std::string n = str(varying->reg + i * rows + j);
1559 pixelHLSL += " " + varying->name;
1560
1561 if (varying->array)
1562 {
1563 pixelHLSL += "[" + str(i) + "]";
1564 }
1565
1566 if (rows > 1)
1567 {
1568 pixelHLSL += "[" + str(j) + "]";
1569 }
1570
1571 pixelHLSL += " = input.v" + n + ";\n";
1572 }
1573 }
1574 }
1575 else UNREACHABLE();
1576 }
1577
1578 pixelHLSL += "\n"
1579 " gl_main();\n"
1580 "\n"
1581 " PS_OUTPUT output;\n"
1582 " output.gl_Color[0] = gl_Color[0];\n"
1583 "\n"
1584 " return output;\n"
1585 "}\n";
1586
1587 return true;
1588}
1589
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001590bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1591{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001592 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001593
1594 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001595 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001596 if (format != GL_PROGRAM_BINARY_ANGLE)
1597 {
1598 infoLog.append("Invalid program binary format.");
1599 return false;
1600 }
1601
1602 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001603 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001604 if (version != BUILD_REVISION)
1605 {
1606 infoLog.append("Invalid program binary version.");
1607 return false;
1608 }
1609
1610 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1611 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001612 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001613 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001614 stream.read(&name);
1615 mLinkedAttribute[i].name = name;
1616 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001617 }
1618
1619 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1620 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001621 stream.read(&mSamplersPS[i].active);
1622 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001623
1624 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001625 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001626 mSamplersPS[i].textureType = (TextureType) textureType;
1627 }
1628
1629 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1630 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001631 stream.read(&mSamplersVS[i].active);
1632 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001633
1634 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001635 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001636 mSamplersVS[i].textureType = (TextureType) textureType;
1637 }
1638
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001639 stream.read(&mUsedVertexSamplerRange);
1640 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001641
1642 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001643 stream.read(&size);
1644 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001645 {
1646 infoLog.append("Invalid program binary.");
1647 return false;
1648 }
1649
1650 mUniforms.resize(size);
1651 for (unsigned int i = 0; i < size; ++i)
1652 {
1653 GLenum type;
1654 std::string _name;
1655 unsigned int arraySize;
1656
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001657 stream.read(&type);
1658 stream.read(&_name);
1659 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001660
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001661 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001662
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001663 stream.read(&mUniforms[i]->ps.float4Index);
1664 stream.read(&mUniforms[i]->ps.samplerIndex);
1665 stream.read(&mUniforms[i]->ps.boolIndex);
1666 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001667
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001668 stream.read(&mUniforms[i]->vs.float4Index);
1669 stream.read(&mUniforms[i]->vs.samplerIndex);
1670 stream.read(&mUniforms[i]->vs.boolIndex);
1671 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001672 }
1673
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001674 stream.read(&size);
1675 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001676 {
1677 infoLog.append("Invalid program binary.");
1678 return false;
1679 }
1680
1681 mUniformIndex.resize(size);
1682 for (unsigned int i = 0; i < size; ++i)
1683 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001684 stream.read(&mUniformIndex[i].name);
1685 stream.read(&mUniformIndex[i].element);
1686 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001687 }
1688
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001689 stream.read(&mDxDepthRangeLocation);
1690 stream.read(&mDxDepthLocation);
1691 stream.read(&mDxCoordLocation);
1692 stream.read(&mDxHalfPixelSizeLocation);
1693 stream.read(&mDxFrontCCWLocation);
1694 stream.read(&mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001695
1696 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001697 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001698
1699 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001700 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001701
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001702 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001703
1704 const D3DCAPS9 *binaryIdentifier = (const D3DCAPS9*) ptr;
1705 ptr += sizeof(GUID);
1706
1707 D3DADAPTER_IDENTIFIER9 *currentIdentifier = getDisplay()->getAdapterIdentifier();
1708 if (memcmp(&currentIdentifier->DeviceIdentifier, binaryIdentifier, sizeof(GUID)) != 0)
1709 {
1710 infoLog.append("Invalid program binary.");
1711 return false;
1712 }
1713
1714 const char *pixelShaderFunction = ptr;
1715 ptr += pixelShaderSize;
1716
1717 const char *vertexShaderFunction = ptr;
1718 ptr += vertexShaderSize;
1719
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001720 mPixelExecutable = getDisplay()->createPixelShader(reinterpret_cast<const DWORD*>(pixelShaderFunction), pixelShaderSize);
1721 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001722 {
1723 infoLog.append("Could not create pixel shader.");
1724 return false;
1725 }
1726
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001727 mVertexExecutable = getDisplay()->createVertexShader(reinterpret_cast<const DWORD*>(vertexShaderFunction), vertexShaderSize);
1728 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001729 {
1730 infoLog.append("Could not create vertex shader.");
1731 mPixelExecutable->Release();
1732 mPixelExecutable = NULL;
1733 return false;
1734 }
1735
1736 return true;
1737}
1738
1739bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1740{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001741 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001742
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001743 stream.write(GL_PROGRAM_BINARY_ANGLE);
1744 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001745
1746 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1747 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001748 stream.write(mLinkedAttribute[i].type);
1749 stream.write(mLinkedAttribute[i].name);
1750 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001751 }
1752
1753 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1754 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001755 stream.write(mSamplersPS[i].active);
1756 stream.write(mSamplersPS[i].logicalTextureUnit);
1757 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001758 }
1759
1760 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1761 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001762 stream.write(mSamplersVS[i].active);
1763 stream.write(mSamplersVS[i].logicalTextureUnit);
1764 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001765 }
1766
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001767 stream.write(mUsedVertexSamplerRange);
1768 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001769
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001770 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001771 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1772 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001773 stream.write(mUniforms[i]->type);
1774 stream.write(mUniforms[i]->_name);
1775 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001776
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001777 stream.write(mUniforms[i]->ps.float4Index);
1778 stream.write(mUniforms[i]->ps.samplerIndex);
1779 stream.write(mUniforms[i]->ps.boolIndex);
1780 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001781
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001782 stream.write(mUniforms[i]->vs.float4Index);
1783 stream.write(mUniforms[i]->vs.samplerIndex);
1784 stream.write(mUniforms[i]->vs.boolIndex);
1785 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001786 }
1787
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001788 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001789 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1790 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001791 stream.write(mUniformIndex[i].name);
1792 stream.write(mUniformIndex[i].element);
1793 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001794 }
1795
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001796 stream.write(mDxDepthRangeLocation);
1797 stream.write(mDxDepthLocation);
1798 stream.write(mDxCoordLocation);
1799 stream.write(mDxHalfPixelSizeLocation);
1800 stream.write(mDxFrontCCWLocation);
1801 stream.write(mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001802
1803 UINT pixelShaderSize;
1804 HRESULT result = mPixelExecutable->GetFunction(NULL, &pixelShaderSize);
1805 ASSERT(SUCCEEDED(result));
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001806 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001807
1808 UINT vertexShaderSize;
1809 result = mVertexExecutable->GetFunction(NULL, &vertexShaderSize);
1810 ASSERT(SUCCEEDED(result));
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001811 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001812
1813 D3DADAPTER_IDENTIFIER9 *identifier = getDisplay()->getAdapterIdentifier();
1814
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001815 GLsizei streamLength = stream.length();
1816 const void *streamData = stream.data();
1817
1818 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001819 if (totalLength > bufSize)
1820 {
1821 if (length)
1822 {
1823 *length = 0;
1824 }
1825
1826 return false;
1827 }
1828
1829 if (binary)
1830 {
1831 char *ptr = (char*) binary;
1832
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001833 memcpy(ptr, streamData, streamLength);
1834 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001835
1836 memcpy(ptr, &identifier->DeviceIdentifier, sizeof(GUID));
1837 ptr += sizeof(GUID);
1838
1839 result = mPixelExecutable->GetFunction(ptr, &pixelShaderSize);
1840 ASSERT(SUCCEEDED(result));
1841 ptr += pixelShaderSize;
1842
1843 result = mVertexExecutable->GetFunction(ptr, &vertexShaderSize);
1844 ASSERT(SUCCEEDED(result));
1845 ptr += vertexShaderSize;
1846
1847 ASSERT(ptr - totalLength == binary);
1848 }
1849
1850 if (length)
1851 {
1852 *length = totalLength;
1853 }
1854
1855 return true;
1856}
1857
1858GLint ProgramBinary::getLength()
1859{
1860 GLint length;
1861 if (save(NULL, INT_MAX, &length))
1862 {
1863 return length;
1864 }
1865 else
1866 {
1867 return 0;
1868 }
1869}
1870
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001871bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001872{
1873 if (!fragmentShader || !fragmentShader->isCompiled())
1874 {
1875 return false;
1876 }
1877
1878 if (!vertexShader || !vertexShader->isCompiled())
1879 {
1880 return false;
1881 }
1882
1883 std::string pixelHLSL = fragmentShader->getHLSL();
1884 std::string vertexHLSL = vertexShader->getHLSL();
1885
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001886 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001887 {
1888 return false;
1889 }
1890
1891 Context *context = getContext();
1892 const char *vertexProfile = context->supportsShaderModel3() ? "vs_3_0" : "vs_2_0";
1893 const char *pixelProfile = context->supportsShaderModel3() ? "ps_3_0" : "ps_2_0";
1894
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001895 ID3D10Blob *vertexBinary = compileToBinary(infoLog, vertexHLSL.c_str(), vertexProfile, &mConstantTableVS);
1896 ID3D10Blob *pixelBinary = compileToBinary(infoLog, pixelHLSL.c_str(), pixelProfile, &mConstantTablePS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001897
1898 if (vertexBinary && pixelBinary)
1899 {
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001900 mVertexExecutable = getDisplay()->createVertexShader((DWORD*)vertexBinary->GetBufferPointer(), vertexBinary->GetBufferSize());
1901 if (!mVertexExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001902 {
1903 return error(GL_OUT_OF_MEMORY, false);
1904 }
1905
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001906 mPixelExecutable = getDisplay()->createPixelShader((DWORD*)pixelBinary->GetBufferPointer(), pixelBinary->GetBufferSize());
1907 if (!mPixelExecutable)
1908 {
1909 mVertexExecutable->Release();
1910 mVertexExecutable = NULL;
1911 return error(GL_OUT_OF_MEMORY, false);
1912 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001913
1914 vertexBinary->Release();
1915 pixelBinary->Release();
1916 vertexBinary = NULL;
1917 pixelBinary = NULL;
1918
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001919 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001920 {
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001921 return false;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001922 }
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001923
1924 if (!linkUniforms(infoLog, GL_FRAGMENT_SHADER, mConstantTablePS))
1925 {
1926 return false;
1927 }
1928
1929 if (!linkUniforms(infoLog, GL_VERTEX_SHADER, mConstantTableVS))
1930 {
1931 return false;
1932 }
1933
1934 // these uniforms are searched as already-decorated because gl_ and dx_
1935 // are reserved prefixes, and do not receive additional decoration
1936 mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
1937 mDxDepthLocation = getUniformLocation("dx_Depth");
1938 mDxCoordLocation = getUniformLocation("dx_Coord");
1939 mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
1940 mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
1941 mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
1942
1943 context->markDxUniformsDirty();
1944
1945 return true;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001946 }
1947
1948 return false;
1949}
1950
1951// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001952bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001953{
1954 unsigned int usedLocations = 0;
1955
1956 // Link attributes that have a binding location
1957 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1958 {
1959 int location = attributeBindings.getAttributeBinding(attribute->name);
1960
1961 if (location != -1) // Set by glBindAttribLocation
1962 {
1963 if (!mLinkedAttribute[location].name.empty())
1964 {
1965 // Multiple active attributes bound to the same location; not an error
1966 }
1967
1968 mLinkedAttribute[location] = *attribute;
1969
1970 int rows = VariableRowCount(attribute->type);
1971
1972 if (rows + location > MAX_VERTEX_ATTRIBS)
1973 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001974 infoLog.append("Active attribute (%s) at location %d is too big to fit", attribute->name.c_str(), location);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001975
1976 return false;
1977 }
1978
1979 for (int i = 0; i < rows; i++)
1980 {
1981 usedLocations |= 1 << (location + i);
1982 }
1983 }
1984 }
1985
1986 // Link attributes that don't have a binding location
1987 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1988 {
1989 int location = attributeBindings.getAttributeBinding(attribute->name);
1990
1991 if (location == -1) // Not set by glBindAttribLocation
1992 {
1993 int rows = VariableRowCount(attribute->type);
1994 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1995
1996 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1997 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001998 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001999
2000 return false; // Fail to link
2001 }
2002
2003 mLinkedAttribute[availableIndex] = *attribute;
2004 }
2005 }
2006
2007 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
2008 {
2009 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
2010 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
2011
2012 for (int r = 0; r < rows; r++)
2013 {
2014 mSemanticIndex[attributeIndex++] = index++;
2015 }
2016 }
2017
2018 return true;
2019}
2020
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002021bool ProgramBinary::linkUniforms(InfoLog &infoLog, GLenum shader, ID3DXConstantTable *constantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002022{
2023 D3DXCONSTANTTABLE_DESC constantTableDescription;
2024
2025 constantTable->GetDesc(&constantTableDescription);
2026
2027 for (unsigned int constantIndex = 0; constantIndex < constantTableDescription.Constants; constantIndex++)
2028 {
2029 D3DXHANDLE constantHandle = constantTable->GetConstant(0, constantIndex);
2030
2031 D3DXCONSTANT_DESC constantDescription;
2032 UINT descriptionCount = 1;
2033 HRESULT result = constantTable->GetConstantDesc(constantHandle, &constantDescription, &descriptionCount);
2034 ASSERT(SUCCEEDED(result));
2035
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002036 if (!defineUniform(infoLog, shader, constantHandle, constantDescription))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002037 {
2038 return false;
2039 }
2040 }
2041
2042 return true;
2043}
2044
2045// Adds the description of a constant found in the binary shader to the list of uniforms
2046// Returns true if succesful (uniform not already defined)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002047bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const D3DXHANDLE &constantHandle, const D3DXCONSTANT_DESC &constantDescription, std::string name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002048{
2049 if (constantDescription.RegisterSet == D3DXRS_SAMPLER)
2050 {
2051 for (unsigned int i = 0; i < constantDescription.RegisterCount; i++)
2052 {
2053 D3DXHANDLE psConstant = mConstantTablePS->GetConstantByName(NULL, constantDescription.Name);
2054 D3DXHANDLE vsConstant = mConstantTableVS->GetConstantByName(NULL, constantDescription.Name);
2055
2056 if (psConstant)
2057 {
2058 unsigned int samplerIndex = mConstantTablePS->GetSamplerIndex(psConstant) + i;
2059
2060 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2061 {
2062 mSamplersPS[samplerIndex].active = true;
2063 mSamplersPS[samplerIndex].textureType = (constantDescription.Type == D3DXPT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2064 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
2065 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
2066 }
2067 else
2068 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002069 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002070 return false;
2071 }
2072 }
2073
2074 if (vsConstant)
2075 {
2076 unsigned int samplerIndex = mConstantTableVS->GetSamplerIndex(vsConstant) + i;
2077
2078 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
2079 {
2080 mSamplersVS[samplerIndex].active = true;
2081 mSamplersVS[samplerIndex].textureType = (constantDescription.Type == D3DXPT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2082 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2083 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2084 }
2085 else
2086 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002087 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002088 return false;
2089 }
2090 }
2091 }
2092 }
2093
2094 switch(constantDescription.Class)
2095 {
2096 case D3DXPC_STRUCT:
2097 {
2098 for (unsigned int arrayIndex = 0; arrayIndex < constantDescription.Elements; arrayIndex++)
2099 {
2100 for (unsigned int field = 0; field < constantDescription.StructMembers; field++)
2101 {
2102 D3DXHANDLE fieldHandle = mConstantTablePS->GetConstant(constantHandle, field);
2103
2104 D3DXCONSTANT_DESC fieldDescription;
2105 UINT descriptionCount = 1;
2106
2107 HRESULT result = mConstantTablePS->GetConstantDesc(fieldHandle, &fieldDescription, &descriptionCount);
2108 ASSERT(SUCCEEDED(result));
2109
2110 std::string structIndex = (constantDescription.Elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
2111
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002112 if (!defineUniform(infoLog, shader, fieldHandle, fieldDescription, name + constantDescription.Name + structIndex + "."))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002113 {
2114 return false;
2115 }
2116 }
2117 }
2118
2119 return true;
2120 }
2121 case D3DXPC_SCALAR:
2122 case D3DXPC_VECTOR:
2123 case D3DXPC_MATRIX_COLUMNS:
2124 case D3DXPC_OBJECT:
2125 return defineUniform(shader, constantDescription, name + constantDescription.Name);
2126 default:
2127 UNREACHABLE();
2128 return false;
2129 }
2130}
2131
2132bool ProgramBinary::defineUniform(GLenum shader, const D3DXCONSTANT_DESC &constantDescription, const std::string &_name)
2133{
2134 Uniform *uniform = createUniform(constantDescription, _name);
2135
2136 if(!uniform)
2137 {
2138 return false;
2139 }
2140
2141 // Check if already defined
2142 GLint location = getUniformLocation(uniform->name);
2143 GLenum type = uniform->type;
2144
2145 if (location >= 0)
2146 {
2147 delete uniform;
2148 uniform = mUniforms[mUniformIndex[location].index];
2149 }
2150
2151 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constantDescription);
2152 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constantDescription);
2153
2154 if (location >= 0)
2155 {
2156 return uniform->type == type;
2157 }
2158
2159 mUniforms.push_back(uniform);
2160 unsigned int uniformIndex = mUniforms.size() - 1;
2161
2162 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2163 {
2164 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2165 }
2166
2167 return true;
2168}
2169
2170Uniform *ProgramBinary::createUniform(const D3DXCONSTANT_DESC &constantDescription, const std::string &_name)
2171{
2172 if (constantDescription.Rows == 1) // Vectors and scalars
2173 {
2174 switch (constantDescription.Type)
2175 {
2176 case D3DXPT_SAMPLER2D:
2177 switch (constantDescription.Columns)
2178 {
2179 case 1: return new Uniform(GL_SAMPLER_2D, _name, constantDescription.Elements);
2180 default: UNREACHABLE();
2181 }
2182 break;
2183 case D3DXPT_SAMPLERCUBE:
2184 switch (constantDescription.Columns)
2185 {
2186 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constantDescription.Elements);
2187 default: UNREACHABLE();
2188 }
2189 break;
2190 case D3DXPT_BOOL:
2191 switch (constantDescription.Columns)
2192 {
2193 case 1: return new Uniform(GL_BOOL, _name, constantDescription.Elements);
2194 case 2: return new Uniform(GL_BOOL_VEC2, _name, constantDescription.Elements);
2195 case 3: return new Uniform(GL_BOOL_VEC3, _name, constantDescription.Elements);
2196 case 4: return new Uniform(GL_BOOL_VEC4, _name, constantDescription.Elements);
2197 default: UNREACHABLE();
2198 }
2199 break;
2200 case D3DXPT_INT:
2201 switch (constantDescription.Columns)
2202 {
2203 case 1: return new Uniform(GL_INT, _name, constantDescription.Elements);
2204 case 2: return new Uniform(GL_INT_VEC2, _name, constantDescription.Elements);
2205 case 3: return new Uniform(GL_INT_VEC3, _name, constantDescription.Elements);
2206 case 4: return new Uniform(GL_INT_VEC4, _name, constantDescription.Elements);
2207 default: UNREACHABLE();
2208 }
2209 break;
2210 case D3DXPT_FLOAT:
2211 switch (constantDescription.Columns)
2212 {
2213 case 1: return new Uniform(GL_FLOAT, _name, constantDescription.Elements);
2214 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constantDescription.Elements);
2215 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constantDescription.Elements);
2216 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constantDescription.Elements);
2217 default: UNREACHABLE();
2218 }
2219 break;
2220 default:
2221 UNREACHABLE();
2222 }
2223 }
2224 else if (constantDescription.Rows == constantDescription.Columns) // Square matrices
2225 {
2226 switch (constantDescription.Type)
2227 {
2228 case D3DXPT_FLOAT:
2229 switch (constantDescription.Rows)
2230 {
2231 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constantDescription.Elements);
2232 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constantDescription.Elements);
2233 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constantDescription.Elements);
2234 default: UNREACHABLE();
2235 }
2236 break;
2237 default: UNREACHABLE();
2238 }
2239 }
2240 else UNREACHABLE();
2241
2242 return 0;
2243}
2244
2245// This method needs to match OutputHLSL::decorate
2246std::string ProgramBinary::decorateAttribute(const std::string &name)
2247{
2248 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2249 {
2250 return "_" + name;
2251 }
2252
2253 return name;
2254}
2255
2256std::string ProgramBinary::undecorateUniform(const std::string &_name)
2257{
2258 std::string name = _name;
2259
2260 // Remove any structure field decoration
2261 size_t pos = 0;
2262 while ((pos = name.find("._", pos)) != std::string::npos)
2263 {
2264 name.replace(pos, 2, ".");
2265 }
2266
2267 // Remove the leading decoration
2268 if (name[0] == '_')
2269 {
2270 return name.substr(1);
2271 }
2272 else if (name.compare(0, 3, "ar_") == 0)
2273 {
2274 return name.substr(3);
2275 }
2276
2277 return name;
2278}
2279
2280void ProgramBinary::applyUniformnbv(Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
2281{
2282 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2283 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2284
2285 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2286 {
2287 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2288 for (int i = 0; i < count; i++)
2289 {
2290 for (int j = 0; j < 4; j++)
2291 {
2292 if (j < width)
2293 {
2294 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2295 }
2296 else
2297 {
2298 vector[i * 4 + j] = 0.0f;
2299 }
2300 }
2301 }
2302 }
2303
2304 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2305 {
2306 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2307 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2308 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2309 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2310 for (int i = 0; i < copyCount; i++)
2311 {
2312 boolVector[i] = v[i] != GL_FALSE;
2313 }
2314 }
2315
2316 if (targetUniform->ps.float4Index >= 0)
2317 {
2318 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
2319 }
2320
2321 if (targetUniform->ps.boolIndex >= 0)
2322 {
2323 mDevice->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
2324 }
2325
2326 if (targetUniform->vs.float4Index >= 0)
2327 {
2328 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
2329 }
2330
2331 if (targetUniform->vs.boolIndex >= 0)
2332 {
2333 mDevice->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
2334 }
2335}
2336
2337bool ProgramBinary::applyUniformnfv(Uniform *targetUniform, const GLfloat *v)
2338{
2339 if (targetUniform->ps.registerCount)
2340 {
2341 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
2342 }
2343
2344 if (targetUniform->vs.registerCount)
2345 {
2346 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
2347 }
2348
2349 return true;
2350}
2351
2352bool ProgramBinary::applyUniform1iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2353{
2354 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2355 D3DXVECTOR4 vector[D3D9_MAX_FLOAT_CONSTANTS];
2356
2357 for (int i = 0; i < count; i++)
2358 {
2359 vector[i] = D3DXVECTOR4((float)v[i], 0, 0, 0);
2360 }
2361
2362 if (targetUniform->ps.registerCount)
2363 {
2364 if (targetUniform->ps.samplerIndex >= 0)
2365 {
2366 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2367
2368 for (int i = 0; i < count; i++)
2369 {
2370 unsigned int samplerIndex = firstIndex + i;
2371
2372 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2373 {
2374 ASSERT(mSamplersPS[samplerIndex].active);
2375 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2376 }
2377 }
2378 }
2379 else
2380 {
2381 ASSERT(targetUniform->ps.float4Index >= 0);
2382 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
2383 }
2384 }
2385
2386 if (targetUniform->vs.registerCount)
2387 {
2388 if (targetUniform->vs.samplerIndex >= 0)
2389 {
2390 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2391
2392 for (int i = 0; i < count; i++)
2393 {
2394 unsigned int samplerIndex = firstIndex + i;
2395
2396 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2397 {
2398 ASSERT(mSamplersVS[samplerIndex].active);
2399 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2400 }
2401 }
2402 }
2403 else
2404 {
2405 ASSERT(targetUniform->vs.float4Index >= 0);
2406 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
2407 }
2408 }
2409
2410 return true;
2411}
2412
2413bool ProgramBinary::applyUniform2iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2414{
2415 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2416 D3DXVECTOR4 vector[D3D9_MAX_FLOAT_CONSTANTS];
2417
2418 for (int i = 0; i < count; i++)
2419 {
2420 vector[i] = D3DXVECTOR4((float)v[0], (float)v[1], 0, 0);
2421
2422 v += 2;
2423 }
2424
2425 applyUniformniv(targetUniform, count, vector);
2426
2427 return true;
2428}
2429
2430bool ProgramBinary::applyUniform3iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2431{
2432 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2433 D3DXVECTOR4 vector[D3D9_MAX_FLOAT_CONSTANTS];
2434
2435 for (int i = 0; i < count; i++)
2436 {
2437 vector[i] = D3DXVECTOR4((float)v[0], (float)v[1], (float)v[2], 0);
2438
2439 v += 3;
2440 }
2441
2442 applyUniformniv(targetUniform, count, vector);
2443
2444 return true;
2445}
2446
2447bool ProgramBinary::applyUniform4iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2448{
2449 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2450 D3DXVECTOR4 vector[D3D9_MAX_FLOAT_CONSTANTS];
2451
2452 for (int i = 0; i < count; i++)
2453 {
2454 vector[i] = D3DXVECTOR4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
2455
2456 v += 4;
2457 }
2458
2459 applyUniformniv(targetUniform, count, vector);
2460
2461 return true;
2462}
2463
2464void ProgramBinary::applyUniformniv(Uniform *targetUniform, GLsizei count, const D3DXVECTOR4 *vector)
2465{
2466 if (targetUniform->ps.registerCount)
2467 {
2468 ASSERT(targetUniform->ps.float4Index >= 0);
2469 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
2470 }
2471
2472 if (targetUniform->vs.registerCount)
2473 {
2474 ASSERT(targetUniform->vs.float4Index >= 0);
2475 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
2476 }
2477}
2478
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002479bool ProgramBinary::isValidated() const
2480{
2481 return mValidated;
2482}
2483
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002484void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2485{
2486 // Skip over inactive attributes
2487 unsigned int activeAttribute = 0;
2488 unsigned int attribute;
2489 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2490 {
2491 if (mLinkedAttribute[attribute].name.empty())
2492 {
2493 continue;
2494 }
2495
2496 if (activeAttribute == index)
2497 {
2498 break;
2499 }
2500
2501 activeAttribute++;
2502 }
2503
2504 if (bufsize > 0)
2505 {
2506 const char *string = mLinkedAttribute[attribute].name.c_str();
2507
2508 strncpy(name, string, bufsize);
2509 name[bufsize - 1] = '\0';
2510
2511 if (length)
2512 {
2513 *length = strlen(name);
2514 }
2515 }
2516
2517 *size = 1; // Always a single 'type' instance
2518
2519 *type = mLinkedAttribute[attribute].type;
2520}
2521
2522GLint ProgramBinary::getActiveAttributeCount()
2523{
2524 int count = 0;
2525
2526 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2527 {
2528 if (!mLinkedAttribute[attributeIndex].name.empty())
2529 {
2530 count++;
2531 }
2532 }
2533
2534 return count;
2535}
2536
2537GLint ProgramBinary::getActiveAttributeMaxLength()
2538{
2539 int maxLength = 0;
2540
2541 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2542 {
2543 if (!mLinkedAttribute[attributeIndex].name.empty())
2544 {
2545 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2546 }
2547 }
2548
2549 return maxLength;
2550}
2551
2552void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2553{
2554 // Skip over internal uniforms
2555 unsigned int activeUniform = 0;
2556 unsigned int uniform;
2557 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2558 {
2559 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2560 {
2561 continue;
2562 }
2563
2564 if (activeUniform == index)
2565 {
2566 break;
2567 }
2568
2569 activeUniform++;
2570 }
2571
2572 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2573
2574 if (bufsize > 0)
2575 {
2576 std::string string = mUniforms[uniform]->name;
2577
2578 if (mUniforms[uniform]->isArray())
2579 {
2580 string += "[0]";
2581 }
2582
2583 strncpy(name, string.c_str(), bufsize);
2584 name[bufsize - 1] = '\0';
2585
2586 if (length)
2587 {
2588 *length = strlen(name);
2589 }
2590 }
2591
2592 *size = mUniforms[uniform]->arraySize;
2593
2594 *type = mUniforms[uniform]->type;
2595}
2596
2597GLint ProgramBinary::getActiveUniformCount()
2598{
2599 int count = 0;
2600
2601 unsigned int numUniforms = mUniforms.size();
2602 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2603 {
2604 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2605 {
2606 count++;
2607 }
2608 }
2609
2610 return count;
2611}
2612
2613GLint ProgramBinary::getActiveUniformMaxLength()
2614{
2615 int maxLength = 0;
2616
2617 unsigned int numUniforms = mUniforms.size();
2618 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2619 {
2620 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2621 {
2622 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2623 if (mUniforms[uniformIndex]->isArray())
2624 {
2625 length += 3; // Counting in "[0]".
2626 }
2627 maxLength = std::max(length, maxLength);
2628 }
2629 }
2630
2631 return maxLength;
2632}
2633
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002634void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002635{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002636 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002637 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002638 {
2639 mValidated = false;
2640 }
2641 else
2642 {
2643 mValidated = true;
2644 }
2645}
2646
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002647bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002648{
2649 // if any two active samplers in a program are of different types, but refer to the same
2650 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2651 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2652
2653 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2654 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2655
2656 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2657 {
2658 textureUnitType[i] = TEXTURE_UNKNOWN;
2659 }
2660
2661 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2662 {
2663 if (mSamplersPS[i].active)
2664 {
2665 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2666
2667 if (unit >= maxCombinedTextureImageUnits)
2668 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002669 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002670 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002671 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002672 }
2673
2674 return false;
2675 }
2676
2677 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2678 {
2679 if (mSamplersPS[i].textureType != textureUnitType[unit])
2680 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002681 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002682 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002683 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002684 }
2685
2686 return false;
2687 }
2688 }
2689 else
2690 {
2691 textureUnitType[unit] = mSamplersPS[i].textureType;
2692 }
2693 }
2694 }
2695
2696 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2697 {
2698 if (mSamplersVS[i].active)
2699 {
2700 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2701
2702 if (unit >= maxCombinedTextureImageUnits)
2703 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002704 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002705 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002706 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002707 }
2708
2709 return false;
2710 }
2711
2712 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2713 {
2714 if (mSamplersVS[i].textureType != textureUnitType[unit])
2715 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002716 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002717 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002718 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002719 }
2720
2721 return false;
2722 }
2723 }
2724 else
2725 {
2726 textureUnitType[unit] = mSamplersVS[i].textureType;
2727 }
2728 }
2729 }
2730
2731 return true;
2732}
2733
2734GLint ProgramBinary::getDxDepthRangeLocation() const
2735{
2736 return mDxDepthRangeLocation;
2737}
2738
2739GLint ProgramBinary::getDxDepthLocation() const
2740{
2741 return mDxDepthLocation;
2742}
2743
2744GLint ProgramBinary::getDxCoordLocation() const
2745{
2746 return mDxCoordLocation;
2747}
2748
2749GLint ProgramBinary::getDxHalfPixelSizeLocation() const
2750{
2751 return mDxHalfPixelSizeLocation;
2752}
2753
2754GLint ProgramBinary::getDxFrontCCWLocation() const
2755{
2756 return mDxFrontCCWLocation;
2757}
2758
2759GLint ProgramBinary::getDxPointsOrLinesLocation() const
2760{
2761 return mDxPointsOrLinesLocation;
2762}
2763
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002764ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2765{
2766}
2767
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002768}