blob: b3f5e3b86703e02c37aa03557da577766ad9c8f9 [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{
daniel@transgaming.com22ba0f72012-09-27 17:46:04 +000052 size_t dot = _name.find_last_of('.');
53 if (dot == std::string::npos) dot = -1;
54
55 return _name.compare(dot + 1, dot + 4, "ar_") == 0;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000056}
57
58UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
59 : name(ProgramBinary::undecorateUniform(_name)), element(element), index(index)
60{
61}
62
daniel@transgaming.come87ca002012-07-24 18:30:43 +000063unsigned int ProgramBinary::mCurrentSerial = 1;
64
daniel@transgaming.com989c1c82012-07-24 18:40:38 +000065ProgramBinary::ProgramBinary() : RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000066{
67 mDevice = getDevice();
68
69 mPixelExecutable = NULL;
70 mVertexExecutable = NULL;
71 mConstantTablePS = NULL;
72 mConstantTableVS = NULL;
73
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000074 mValidated = false;
75
76 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
77 {
78 mSemanticIndex[index] = -1;
79 }
80
81 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
82 {
83 mSamplersPS[index].active = false;
84 }
85
86 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
87 {
88 mSamplersVS[index].active = false;
89 }
90
91 mUsedVertexSamplerRange = 0;
92 mUsedPixelSamplerRange = 0;
93
94 mDxDepthRangeLocation = -1;
95 mDxDepthLocation = -1;
96 mDxCoordLocation = -1;
97 mDxHalfPixelSizeLocation = -1;
98 mDxFrontCCWLocation = -1;
99 mDxPointsOrLinesLocation = -1;
100}
101
102ProgramBinary::~ProgramBinary()
103{
104 if (mPixelExecutable)
105 {
106 mPixelExecutable->Release();
107 }
108
109 if (mVertexExecutable)
110 {
111 mVertexExecutable->Release();
112 }
113
apatrick@chromium.org60dafe82012-09-05 22:26:10 +0000114 delete mConstantTablePS;
115 delete mConstantTableVS;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000116
117 while (!mUniforms.empty())
118 {
119 delete mUniforms.back();
120 mUniforms.pop_back();
121 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000122}
123
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000124unsigned int ProgramBinary::getSerial() const
125{
126 return mSerial;
127}
128
129unsigned int ProgramBinary::issueSerial()
130{
131 return mCurrentSerial++;
132}
133
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000134IDirect3DPixelShader9 *ProgramBinary::getPixelShader()
135{
136 return mPixelExecutable;
137}
138
139IDirect3DVertexShader9 *ProgramBinary::getVertexShader()
140{
141 return mVertexExecutable;
142}
143
144GLuint ProgramBinary::getAttributeLocation(const char *name)
145{
146 if (name)
147 {
148 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
149 {
150 if (mLinkedAttribute[index].name == std::string(name))
151 {
152 return index;
153 }
154 }
155 }
156
157 return -1;
158}
159
160int ProgramBinary::getSemanticIndex(int attributeIndex)
161{
162 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
163
164 return mSemanticIndex[attributeIndex];
165}
166
167// Returns one more than the highest sampler index used.
168GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
169{
170 switch (type)
171 {
172 case SAMPLER_PIXEL:
173 return mUsedPixelSamplerRange;
174 case SAMPLER_VERTEX:
175 return mUsedVertexSamplerRange;
176 default:
177 UNREACHABLE();
178 return 0;
179 }
180}
181
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000182bool ProgramBinary::usesPointSize() const
183{
184 return mUsesPointSize;
185}
186
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000187// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
188// index (0-15 for the pixel shader and 0-3 for the vertex shader).
189GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
190{
191 GLint logicalTextureUnit = -1;
192
193 switch (type)
194 {
195 case SAMPLER_PIXEL:
196 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
197
198 if (mSamplersPS[samplerIndex].active)
199 {
200 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
201 }
202 break;
203 case SAMPLER_VERTEX:
204 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
205
206 if (mSamplersVS[samplerIndex].active)
207 {
208 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
209 }
210 break;
211 default: UNREACHABLE();
212 }
213
214 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
215 {
216 return logicalTextureUnit;
217 }
218
219 return -1;
220}
221
222// Returns the texture type for a given Direct3D 9 sampler type and
223// index (0-15 for the pixel shader and 0-3 for the vertex shader).
224TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
225{
226 switch (type)
227 {
228 case SAMPLER_PIXEL:
229 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
230 ASSERT(mSamplersPS[samplerIndex].active);
231 return mSamplersPS[samplerIndex].textureType;
232 case SAMPLER_VERTEX:
233 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
234 ASSERT(mSamplersVS[samplerIndex].active);
235 return mSamplersVS[samplerIndex].textureType;
236 default: UNREACHABLE();
237 }
238
239 return TEXTURE_2D;
240}
241
242GLint ProgramBinary::getUniformLocation(std::string name)
243{
244 unsigned int subscript = 0;
245
246 // Strip any trailing array operator and retrieve the subscript
247 size_t open = name.find_last_of('[');
248 size_t close = name.find_last_of(']');
249 if (open != std::string::npos && close == name.length() - 1)
250 {
251 subscript = atoi(name.substr(open + 1).c_str());
252 name.erase(open);
253 }
254
255 unsigned int numUniforms = mUniformIndex.size();
256 for (unsigned int location = 0; location < numUniforms; location++)
257 {
258 if (mUniformIndex[location].name == name &&
259 mUniformIndex[location].element == subscript)
260 {
261 return location;
262 }
263 }
264
265 return -1;
266}
267
268bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
269{
270 if (location < 0 || location >= (int)mUniformIndex.size())
271 {
272 return false;
273 }
274
275 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
276 targetUniform->dirty = true;
277
278 if (targetUniform->type == GL_FLOAT)
279 {
280 int arraySize = targetUniform->arraySize;
281
282 if (arraySize == 1 && count > 1)
283 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
284
285 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
286
287 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
288
289 for (int i = 0; i < count; i++)
290 {
291 target[0] = v[0];
292 target[1] = 0;
293 target[2] = 0;
294 target[3] = 0;
295 target += 4;
296 v += 1;
297 }
298 }
299 else if (targetUniform->type == GL_BOOL)
300 {
301 int arraySize = targetUniform->arraySize;
302
303 if (arraySize == 1 && count > 1)
304 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
305
306 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
307 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
308
309 for (int i = 0; i < count; ++i)
310 {
311 if (v[i] == 0.0f)
312 {
313 boolParams[i] = GL_FALSE;
314 }
315 else
316 {
317 boolParams[i] = GL_TRUE;
318 }
319 }
320 }
321 else
322 {
323 return false;
324 }
325
326 return true;
327}
328
329bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
330{
331 if (location < 0 || location >= (int)mUniformIndex.size())
332 {
333 return false;
334 }
335
336 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
337 targetUniform->dirty = true;
338
339 if (targetUniform->type == GL_FLOAT_VEC2)
340 {
341 int arraySize = targetUniform->arraySize;
342
343 if (arraySize == 1 && count > 1)
344 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
345
346 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
347
348 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
349
350 for (int i = 0; i < count; i++)
351 {
352 target[0] = v[0];
353 target[1] = v[1];
354 target[2] = 0;
355 target[3] = 0;
356 target += 4;
357 v += 2;
358 }
359 }
360 else if (targetUniform->type == GL_BOOL_VEC2)
361 {
362 int arraySize = targetUniform->arraySize;
363
364 if (arraySize == 1 && count > 1)
365 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
366
367 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
368
369 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
370
371 for (int i = 0; i < count * 2; ++i)
372 {
373 if (v[i] == 0.0f)
374 {
375 boolParams[i] = GL_FALSE;
376 }
377 else
378 {
379 boolParams[i] = GL_TRUE;
380 }
381 }
382 }
383 else
384 {
385 return false;
386 }
387
388 return true;
389}
390
391bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
392{
393 if (location < 0 || location >= (int)mUniformIndex.size())
394 {
395 return false;
396 }
397
398 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
399 targetUniform->dirty = true;
400
401 if (targetUniform->type == GL_FLOAT_VEC3)
402 {
403 int arraySize = targetUniform->arraySize;
404
405 if (arraySize == 1 && count > 1)
406 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
407
408 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
409
410 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
411
412 for (int i = 0; i < count; i++)
413 {
414 target[0] = v[0];
415 target[1] = v[1];
416 target[2] = v[2];
417 target[3] = 0;
418 target += 4;
419 v += 3;
420 }
421 }
422 else if (targetUniform->type == GL_BOOL_VEC3)
423 {
424 int arraySize = targetUniform->arraySize;
425
426 if (arraySize == 1 && count > 1)
427 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
428
429 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
430 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
431
432 for (int i = 0; i < count * 3; ++i)
433 {
434 if (v[i] == 0.0f)
435 {
436 boolParams[i] = GL_FALSE;
437 }
438 else
439 {
440 boolParams[i] = GL_TRUE;
441 }
442 }
443 }
444 else
445 {
446 return false;
447 }
448
449 return true;
450}
451
452bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
453{
454 if (location < 0 || location >= (int)mUniformIndex.size())
455 {
456 return false;
457 }
458
459 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
460 targetUniform->dirty = true;
461
462 if (targetUniform->type == GL_FLOAT_VEC4)
463 {
464 int arraySize = targetUniform->arraySize;
465
466 if (arraySize == 1 && count > 1)
467 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
468
469 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
470
471 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
472 v, 4 * sizeof(GLfloat) * count);
473 }
474 else if (targetUniform->type == GL_BOOL_VEC4)
475 {
476 int arraySize = targetUniform->arraySize;
477
478 if (arraySize == 1 && count > 1)
479 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
480
481 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
482 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
483
484 for (int i = 0; i < count * 4; ++i)
485 {
486 if (v[i] == 0.0f)
487 {
488 boolParams[i] = GL_FALSE;
489 }
490 else
491 {
492 boolParams[i] = GL_TRUE;
493 }
494 }
495 }
496 else
497 {
498 return false;
499 }
500
501 return true;
502}
503
504template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
505void transposeMatrix(T *target, const GLfloat *value)
506{
507 int copyWidth = std::min(targetWidth, srcWidth);
508 int copyHeight = std::min(targetHeight, srcHeight);
509
510 for (int x = 0; x < copyWidth; x++)
511 {
512 for (int y = 0; y < copyHeight; y++)
513 {
514 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
515 }
516 }
517 // clear unfilled right side
518 for (int y = 0; y < copyHeight; y++)
519 {
520 for (int x = srcWidth; x < targetWidth; x++)
521 {
522 target[y * targetWidth + x] = (T)0;
523 }
524 }
525 // clear unfilled bottom.
526 for (int y = srcHeight; y < targetHeight; y++)
527 {
528 for (int x = 0; x < targetWidth; x++)
529 {
530 target[y * targetWidth + x] = (T)0;
531 }
532 }
533}
534
535bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
536{
537 if (location < 0 || location >= (int)mUniformIndex.size())
538 {
539 return false;
540 }
541
542 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
543 targetUniform->dirty = true;
544
545 if (targetUniform->type != GL_FLOAT_MAT2)
546 {
547 return false;
548 }
549
550 int arraySize = targetUniform->arraySize;
551
552 if (arraySize == 1 && count > 1)
553 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
554
555 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
556
557 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
558 for (int i = 0; i < count; i++)
559 {
560 transposeMatrix<GLfloat,4,2,2,2>(target, value);
561 target += 8;
562 value += 4;
563 }
564
565 return true;
566}
567
568bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
569{
570 if (location < 0 || location >= (int)mUniformIndex.size())
571 {
572 return false;
573 }
574
575 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
576 targetUniform->dirty = true;
577
578 if (targetUniform->type != GL_FLOAT_MAT3)
579 {
580 return false;
581 }
582
583 int arraySize = targetUniform->arraySize;
584
585 if (arraySize == 1 && count > 1)
586 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
587
588 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
589
590 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
591 for (int i = 0; i < count; i++)
592 {
593 transposeMatrix<GLfloat,4,3,3,3>(target, value);
594 target += 12;
595 value += 9;
596 }
597
598 return true;
599}
600
601
602bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
603{
604 if (location < 0 || location >= (int)mUniformIndex.size())
605 {
606 return false;
607 }
608
609 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
610 targetUniform->dirty = true;
611
612 if (targetUniform->type != GL_FLOAT_MAT4)
613 {
614 return false;
615 }
616
617 int arraySize = targetUniform->arraySize;
618
619 if (arraySize == 1 && count > 1)
620 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
621
622 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
623
624 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
625 for (int i = 0; i < count; i++)
626 {
627 transposeMatrix<GLfloat,4,4,4,4>(target, value);
628 target += 16;
629 value += 16;
630 }
631
632 return true;
633}
634
635bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
636{
637 if (location < 0 || location >= (int)mUniformIndex.size())
638 {
639 return false;
640 }
641
642 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
643 targetUniform->dirty = true;
644
645 if (targetUniform->type == GL_INT ||
646 targetUniform->type == GL_SAMPLER_2D ||
647 targetUniform->type == GL_SAMPLER_CUBE)
648 {
649 int arraySize = targetUniform->arraySize;
650
651 if (arraySize == 1 && count > 1)
652 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
653
654 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
655
656 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
657 v, sizeof(GLint) * count);
658 }
659 else if (targetUniform->type == GL_BOOL)
660 {
661 int arraySize = targetUniform->arraySize;
662
663 if (arraySize == 1 && count > 1)
664 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
665
666 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
667 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
668
669 for (int i = 0; i < count; ++i)
670 {
671 if (v[i] == 0)
672 {
673 boolParams[i] = GL_FALSE;
674 }
675 else
676 {
677 boolParams[i] = GL_TRUE;
678 }
679 }
680 }
681 else
682 {
683 return false;
684 }
685
686 return true;
687}
688
689bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
690{
691 if (location < 0 || location >= (int)mUniformIndex.size())
692 {
693 return false;
694 }
695
696 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
697 targetUniform->dirty = true;
698
699 if (targetUniform->type == GL_INT_VEC2)
700 {
701 int arraySize = targetUniform->arraySize;
702
703 if (arraySize == 1 && count > 1)
704 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
705
706 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
707
708 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
709 v, 2 * sizeof(GLint) * count);
710 }
711 else if (targetUniform->type == GL_BOOL_VEC2)
712 {
713 int arraySize = targetUniform->arraySize;
714
715 if (arraySize == 1 && count > 1)
716 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
717
718 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
719 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
720
721 for (int i = 0; i < count * 2; ++i)
722 {
723 if (v[i] == 0)
724 {
725 boolParams[i] = GL_FALSE;
726 }
727 else
728 {
729 boolParams[i] = GL_TRUE;
730 }
731 }
732 }
733 else
734 {
735 return false;
736 }
737
738 return true;
739}
740
741bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
742{
743 if (location < 0 || location >= (int)mUniformIndex.size())
744 {
745 return false;
746 }
747
748 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
749 targetUniform->dirty = true;
750
751 if (targetUniform->type == GL_INT_VEC3)
752 {
753 int arraySize = targetUniform->arraySize;
754
755 if (arraySize == 1 && count > 1)
756 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
757
758 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
759
760 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
761 v, 3 * sizeof(GLint) * count);
762 }
763 else if (targetUniform->type == GL_BOOL_VEC3)
764 {
765 int arraySize = targetUniform->arraySize;
766
767 if (arraySize == 1 && count > 1)
768 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
769
770 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
771 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
772
773 for (int i = 0; i < count * 3; ++i)
774 {
775 if (v[i] == 0)
776 {
777 boolParams[i] = GL_FALSE;
778 }
779 else
780 {
781 boolParams[i] = GL_TRUE;
782 }
783 }
784 }
785 else
786 {
787 return false;
788 }
789
790 return true;
791}
792
793bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
794{
795 if (location < 0 || location >= (int)mUniformIndex.size())
796 {
797 return false;
798 }
799
800 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
801 targetUniform->dirty = true;
802
803 if (targetUniform->type == GL_INT_VEC4)
804 {
805 int arraySize = targetUniform->arraySize;
806
807 if (arraySize == 1 && count > 1)
808 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
809
810 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
811
812 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
813 v, 4 * sizeof(GLint) * count);
814 }
815 else if (targetUniform->type == GL_BOOL_VEC4)
816 {
817 int arraySize = targetUniform->arraySize;
818
819 if (arraySize == 1 && count > 1)
820 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
821
822 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
823 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
824
825 for (int i = 0; i < count * 4; ++i)
826 {
827 if (v[i] == 0)
828 {
829 boolParams[i] = GL_FALSE;
830 }
831 else
832 {
833 boolParams[i] = GL_TRUE;
834 }
835 }
836 }
837 else
838 {
839 return false;
840 }
841
842 return true;
843}
844
845bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
846{
847 if (location < 0 || location >= (int)mUniformIndex.size())
848 {
849 return false;
850 }
851
852 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
853
854 // sized queries -- ensure the provided buffer is large enough
855 if (bufSize)
856 {
857 int requiredBytes = UniformExternalSize(targetUniform->type);
858 if (*bufSize < requiredBytes)
859 {
860 return false;
861 }
862 }
863
864 switch (targetUniform->type)
865 {
866 case GL_FLOAT_MAT2:
867 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
868 break;
869 case GL_FLOAT_MAT3:
870 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
871 break;
872 case GL_FLOAT_MAT4:
873 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
874 break;
875 default:
876 {
877 unsigned int count = UniformExternalComponentCount(targetUniform->type);
878 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
879
880 switch (UniformComponentType(targetUniform->type))
881 {
882 case GL_BOOL:
883 {
884 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
885
886 for (unsigned int i = 0; i < count; ++i)
887 {
888 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
889 }
890 }
891 break;
892 case GL_FLOAT:
893 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
894 count * sizeof(GLfloat));
895 break;
896 case GL_INT:
897 {
898 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
899
900 for (unsigned int i = 0; i < count; ++i)
901 {
902 params[i] = (float)intParams[i];
903 }
904 }
905 break;
906 default: UNREACHABLE();
907 }
908 }
909 }
910
911 return true;
912}
913
914bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
915{
916 if (location < 0 || location >= (int)mUniformIndex.size())
917 {
918 return false;
919 }
920
921 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
922
923 // sized queries -- ensure the provided buffer is large enough
924 if (bufSize)
925 {
926 int requiredBytes = UniformExternalSize(targetUniform->type);
927 if (*bufSize < requiredBytes)
928 {
929 return false;
930 }
931 }
932
933 switch (targetUniform->type)
934 {
935 case GL_FLOAT_MAT2:
936 {
937 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
938 }
939 break;
940 case GL_FLOAT_MAT3:
941 {
942 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
943 }
944 break;
945 case GL_FLOAT_MAT4:
946 {
947 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
948 }
949 break;
950 default:
951 {
952 unsigned int count = UniformExternalComponentCount(targetUniform->type);
953 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
954
955 switch (UniformComponentType(targetUniform->type))
956 {
957 case GL_BOOL:
958 {
959 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
960
961 for (unsigned int i = 0; i < count; ++i)
962 {
963 params[i] = (GLint)boolParams[i];
964 }
965 }
966 break;
967 case GL_FLOAT:
968 {
969 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
970
971 for (unsigned int i = 0; i < count; ++i)
972 {
973 params[i] = (GLint)floatParams[i];
974 }
975 }
976 break;
977 case GL_INT:
978 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
979 count * sizeof(GLint));
980 break;
981 default: UNREACHABLE();
982 }
983 }
984 }
985
986 return true;
987}
988
989void ProgramBinary::dirtyAllUniforms()
990{
991 unsigned int numUniforms = mUniforms.size();
992 for (unsigned int index = 0; index < numUniforms; index++)
993 {
994 mUniforms[index]->dirty = true;
995 }
996}
997
998// Applies all the uniforms set for this program object to the Direct3D 9 device
999void ProgramBinary::applyUniforms()
1000{
1001 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub) {
1002 Uniform *targetUniform = *ub;
1003
1004 if (targetUniform->dirty)
1005 {
1006 int arraySize = targetUniform->arraySize;
1007 GLfloat *f = (GLfloat*)targetUniform->data;
1008 GLint *i = (GLint*)targetUniform->data;
1009 GLboolean *b = (GLboolean*)targetUniform->data;
1010
1011 switch (targetUniform->type)
1012 {
1013 case GL_BOOL: applyUniformnbv(targetUniform, arraySize, 1, b); break;
1014 case GL_BOOL_VEC2: applyUniformnbv(targetUniform, arraySize, 2, b); break;
1015 case GL_BOOL_VEC3: applyUniformnbv(targetUniform, arraySize, 3, b); break;
1016 case GL_BOOL_VEC4: applyUniformnbv(targetUniform, arraySize, 4, b); break;
1017 case GL_FLOAT:
1018 case GL_FLOAT_VEC2:
1019 case GL_FLOAT_VEC3:
1020 case GL_FLOAT_VEC4:
1021 case GL_FLOAT_MAT2:
1022 case GL_FLOAT_MAT3:
1023 case GL_FLOAT_MAT4: applyUniformnfv(targetUniform, f); break;
1024 case GL_SAMPLER_2D:
1025 case GL_SAMPLER_CUBE:
1026 case GL_INT: applyUniform1iv(targetUniform, arraySize, i); break;
1027 case GL_INT_VEC2: applyUniform2iv(targetUniform, arraySize, i); break;
1028 case GL_INT_VEC3: applyUniform3iv(targetUniform, arraySize, i); break;
1029 case GL_INT_VEC4: applyUniform4iv(targetUniform, arraySize, i); break;
1030 default:
1031 UNREACHABLE();
1032 }
1033
1034 targetUniform->dirty = false;
1035 }
1036 }
1037}
1038
1039// Compiles the HLSL code of the attached shaders into executable binaries
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001040ID3D10Blob *ProgramBinary::compileToBinary(InfoLog &infoLog, const char *hlsl, const char *profile, D3DConstantTable **constantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001041{
1042 if (!hlsl)
1043 {
1044 return NULL;
1045 }
1046
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001047 DWORD result = NOERROR;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001048 UINT flags = 0;
1049 std::string sourceText;
1050 if (perfActive())
1051 {
1052 flags |= D3DCOMPILE_DEBUG;
1053#ifdef NDEBUG
1054 flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
1055#else
1056 flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
1057#endif
1058
1059 std::string sourcePath = getTempPath();
1060 sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl);
1061 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
1062 }
1063 else
1064 {
1065 flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
1066 sourceText = hlsl;
1067 }
1068
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001069 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
1070 // Try the default flags first and if compilation fails, try some alternatives.
1071 const static UINT extraFlags[] =
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001072 {
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001073 0,
1074 D3DCOMPILE_AVOID_FLOW_CONTROL,
1075 D3DCOMPILE_PREFER_FLOW_CONTROL
1076 };
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001077
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001078 const static char * const extraFlagNames[] =
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001079 {
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001080 "default",
1081 "avoid flow control",
1082 "prefer flow control"
1083 };
1084
1085 for (int i = 0; i < sizeof(extraFlags) / sizeof(UINT); ++i)
1086 {
1087 ID3D10Blob *errorMessage = NULL;
1088 ID3D10Blob *binary = NULL;
1089 result = D3DCompile(hlsl, strlen(hlsl), g_fakepath, NULL, NULL, "main", profile, flags | extraFlags[i], 0, &binary, &errorMessage);
1090
1091 if (errorMessage)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001092 {
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001093 const char *message = (const char*)errorMessage->GetBufferPointer();
1094
1095 infoLog.appendSanitized(message);
1096 TRACE("\n%s", hlsl);
1097 TRACE("\n%s", message);
1098
1099 errorMessage->Release();
1100 errorMessage = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001101 }
1102
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001103 if (SUCCEEDED(result))
1104 {
1105 D3DConstantTable *table = new D3DConstantTable(binary->GetBufferPointer(), binary->GetBufferSize());
1106 if (table->error())
1107 {
1108 delete table;
1109 binary->Release();
1110 return NULL;
1111 }
1112
1113 *constantTable = table;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001114
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001115 return binary;
1116 }
1117 else
1118 {
1119 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1120 {
1121 return error(GL_OUT_OF_MEMORY, (ID3D10Blob*) NULL);
1122 }
1123
1124 infoLog.append("Warning: D3D shader compilation failed with ");
1125 infoLog.append(extraFlagNames[i]);
1126 infoLog.append(" flags.");
1127 if (i + 1 < sizeof(extraFlagNames) / sizeof(char*))
1128 {
1129 infoLog.append(" Retrying with ");
1130 infoLog.append(extraFlagNames[i + 1]);
1131 infoLog.append(".\n");
1132 }
1133 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001134 }
1135
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001136 return NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001137}
1138
1139// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1140// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001141int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001142{
1143 Context *context = getContext();
1144 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1145
1146 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1147 {
1148 int n = VariableRowCount(varying->type) * varying->size;
1149 int m = VariableColumnCount(varying->type);
1150 bool success = false;
1151
1152 if (m == 2 || m == 3 || m == 4)
1153 {
1154 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1155 {
1156 bool available = true;
1157
1158 for (int y = 0; y < n && available; y++)
1159 {
1160 for (int x = 0; x < m && available; x++)
1161 {
1162 if (packing[r + y][x])
1163 {
1164 available = false;
1165 }
1166 }
1167 }
1168
1169 if (available)
1170 {
1171 varying->reg = r;
1172 varying->col = 0;
1173
1174 for (int y = 0; y < n; y++)
1175 {
1176 for (int x = 0; x < m; x++)
1177 {
1178 packing[r + y][x] = &*varying;
1179 }
1180 }
1181
1182 success = true;
1183 }
1184 }
1185
1186 if (!success && m == 2)
1187 {
1188 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1189 {
1190 bool available = true;
1191
1192 for (int y = 0; y < n && available; y++)
1193 {
1194 for (int x = 2; x < 4 && available; x++)
1195 {
1196 if (packing[r + y][x])
1197 {
1198 available = false;
1199 }
1200 }
1201 }
1202
1203 if (available)
1204 {
1205 varying->reg = r;
1206 varying->col = 2;
1207
1208 for (int y = 0; y < n; y++)
1209 {
1210 for (int x = 2; x < 4; x++)
1211 {
1212 packing[r + y][x] = &*varying;
1213 }
1214 }
1215
1216 success = true;
1217 }
1218 }
1219 }
1220 }
1221 else if (m == 1)
1222 {
1223 int space[4] = {0};
1224
1225 for (int y = 0; y < maxVaryingVectors; y++)
1226 {
1227 for (int x = 0; x < 4; x++)
1228 {
1229 space[x] += packing[y][x] ? 0 : 1;
1230 }
1231 }
1232
1233 int column = 0;
1234
1235 for (int x = 0; x < 4; x++)
1236 {
1237 if (space[x] >= n && space[x] < space[column])
1238 {
1239 column = x;
1240 }
1241 }
1242
1243 if (space[column] >= n)
1244 {
1245 for (int r = 0; r < maxVaryingVectors; r++)
1246 {
1247 if (!packing[r][column])
1248 {
1249 varying->reg = r;
1250
1251 for (int y = r; y < r + n; y++)
1252 {
1253 packing[y][column] = &*varying;
1254 }
1255
1256 break;
1257 }
1258 }
1259
1260 varying->col = column;
1261
1262 success = true;
1263 }
1264 }
1265 else UNREACHABLE();
1266
1267 if (!success)
1268 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001269 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001270
1271 return -1;
1272 }
1273 }
1274
1275 // Return the number of used registers
1276 int registers = 0;
1277
1278 for (int r = 0; r < maxVaryingVectors; r++)
1279 {
1280 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1281 {
1282 registers++;
1283 }
1284 }
1285
1286 return registers;
1287}
1288
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001289bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001290{
1291 if (pixelHLSL.empty() || vertexHLSL.empty())
1292 {
1293 return false;
1294 }
1295
1296 // Reset the varying register assignments
1297 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1298 {
1299 fragVar->reg = -1;
1300 fragVar->col = -1;
1301 }
1302
1303 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1304 {
1305 vtxVar->reg = -1;
1306 vtxVar->col = -1;
1307 }
1308
1309 // Map the varyings to the register file
1310 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001311 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001312
1313 if (registers < 0)
1314 {
1315 return false;
1316 }
1317
1318 // Write the HLSL input/output declarations
1319 Context *context = getContext();
1320 const bool sm3 = context->supportsShaderModel3();
1321 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1322
1323 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1324 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001325 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001326
1327 return false;
1328 }
1329
1330 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1331 {
1332 bool matched = false;
1333
1334 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1335 {
1336 if (output->name == input->name)
1337 {
1338 if (output->type != input->type || output->size != input->size)
1339 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001340 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 +00001341
1342 return false;
1343 }
1344
1345 output->reg = input->reg;
1346 output->col = input->col;
1347
1348 matched = true;
1349 break;
1350 }
1351 }
1352
1353 if (!matched)
1354 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001355 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001356
1357 return false;
1358 }
1359 }
1360
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001361 mUsesPointSize = vertexShader->mUsesPointSize;
1362 std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001363
1364 vertexHLSL += "struct VS_INPUT\n"
1365 "{\n";
1366
1367 int semanticIndex = 0;
1368 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1369 {
1370 switch (attribute->type)
1371 {
1372 case GL_FLOAT: vertexHLSL += " float "; break;
1373 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1374 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1375 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1376 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1377 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1378 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1379 default: UNREACHABLE();
1380 }
1381
1382 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1383
1384 semanticIndex += VariableRowCount(attribute->type);
1385 }
1386
1387 vertexHLSL += "};\n"
1388 "\n"
1389 "struct VS_OUTPUT\n"
1390 "{\n"
1391 " float4 gl_Position : POSITION;\n";
1392
1393 for (int r = 0; r < registers; r++)
1394 {
1395 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1396
1397 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1398 }
1399
1400 if (fragmentShader->mUsesFragCoord)
1401 {
1402 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1403 }
1404
1405 if (vertexShader->mUsesPointSize && sm3)
1406 {
1407 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1408 }
1409
1410 vertexHLSL += "};\n"
1411 "\n"
1412 "VS_OUTPUT main(VS_INPUT input)\n"
1413 "{\n";
1414
1415 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1416 {
1417 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1418
1419 if (VariableRowCount(attribute->type) > 1) // Matrix
1420 {
1421 vertexHLSL += "transpose";
1422 }
1423
1424 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1425 }
1426
1427 vertexHLSL += "\n"
1428 " gl_main();\n"
1429 "\n"
1430 " VS_OUTPUT output;\n"
1431 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001432 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001433 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1434 " output.gl_Position.w = gl_Position.w;\n";
1435
1436 if (vertexShader->mUsesPointSize && sm3)
1437 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001438 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001439 }
1440
1441 if (fragmentShader->mUsesFragCoord)
1442 {
1443 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1444 }
1445
1446 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1447 {
1448 if (varying->reg >= 0)
1449 {
1450 for (int i = 0; i < varying->size; i++)
1451 {
1452 int rows = VariableRowCount(varying->type);
1453
1454 for (int j = 0; j < rows; j++)
1455 {
1456 int r = varying->reg + i * rows + j;
1457 vertexHLSL += " output.v" + str(r);
1458
1459 bool sharedRegister = false; // Register used by multiple varyings
1460
1461 for (int x = 0; x < 4; x++)
1462 {
1463 if (packing[r][x] && packing[r][x] != packing[r][0])
1464 {
1465 sharedRegister = true;
1466 break;
1467 }
1468 }
1469
1470 if(sharedRegister)
1471 {
1472 vertexHLSL += ".";
1473
1474 for (int x = 0; x < 4; x++)
1475 {
1476 if (packing[r][x] == &*varying)
1477 {
1478 switch(x)
1479 {
1480 case 0: vertexHLSL += "x"; break;
1481 case 1: vertexHLSL += "y"; break;
1482 case 2: vertexHLSL += "z"; break;
1483 case 3: vertexHLSL += "w"; break;
1484 }
1485 }
1486 }
1487 }
1488
1489 vertexHLSL += " = " + varying->name;
1490
1491 if (varying->array)
1492 {
1493 vertexHLSL += "[" + str(i) + "]";
1494 }
1495
1496 if (rows > 1)
1497 {
1498 vertexHLSL += "[" + str(j) + "]";
1499 }
1500
1501 vertexHLSL += ";\n";
1502 }
1503 }
1504 }
1505 }
1506
1507 vertexHLSL += "\n"
1508 " return output;\n"
1509 "}\n";
1510
1511 pixelHLSL += "struct PS_INPUT\n"
1512 "{\n";
1513
1514 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1515 {
1516 if (varying->reg >= 0)
1517 {
1518 for (int i = 0; i < varying->size; i++)
1519 {
1520 int rows = VariableRowCount(varying->type);
1521 for (int j = 0; j < rows; j++)
1522 {
1523 std::string n = str(varying->reg + i * rows + j);
1524 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1525 }
1526 }
1527 }
1528 else UNREACHABLE();
1529 }
1530
1531 if (fragmentShader->mUsesFragCoord)
1532 {
1533 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1534 if (sm3) {
1535 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1536 }
1537 }
1538
1539 if (fragmentShader->mUsesPointCoord && sm3)
1540 {
1541 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1542 }
1543
1544 if (fragmentShader->mUsesFrontFacing)
1545 {
1546 pixelHLSL += " float vFace : VFACE;\n";
1547 }
1548
1549 pixelHLSL += "};\n"
1550 "\n"
1551 "struct PS_OUTPUT\n"
1552 "{\n"
1553 " float4 gl_Color[1] : COLOR;\n"
1554 "};\n"
1555 "\n"
1556 "PS_OUTPUT main(PS_INPUT input)\n"
1557 "{\n";
1558
1559 if (fragmentShader->mUsesFragCoord)
1560 {
1561 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1562
1563 if (sm3)
1564 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001565 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001566 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001567 }
1568 else
1569 {
1570 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1571 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001572 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001573 }
1574
1575 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_Depth.x + dx_Depth.y;\n"
1576 " gl_FragCoord.w = rhw;\n";
1577 }
1578
1579 if (fragmentShader->mUsesPointCoord && sm3)
1580 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001581 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1582 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001583 }
1584
1585 if (fragmentShader->mUsesFrontFacing)
1586 {
1587 pixelHLSL += " gl_FrontFacing = dx_PointsOrLines || (dx_FrontCCW ? (input.vFace >= 0.0) : (input.vFace <= 0.0));\n";
1588 }
1589
1590 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1591 {
1592 if (varying->reg >= 0)
1593 {
1594 for (int i = 0; i < varying->size; i++)
1595 {
1596 int rows = VariableRowCount(varying->type);
1597 for (int j = 0; j < rows; j++)
1598 {
1599 std::string n = str(varying->reg + i * rows + j);
1600 pixelHLSL += " " + varying->name;
1601
1602 if (varying->array)
1603 {
1604 pixelHLSL += "[" + str(i) + "]";
1605 }
1606
1607 if (rows > 1)
1608 {
1609 pixelHLSL += "[" + str(j) + "]";
1610 }
1611
1612 pixelHLSL += " = input.v" + n + ";\n";
1613 }
1614 }
1615 }
1616 else UNREACHABLE();
1617 }
1618
1619 pixelHLSL += "\n"
1620 " gl_main();\n"
1621 "\n"
1622 " PS_OUTPUT output;\n"
1623 " output.gl_Color[0] = gl_Color[0];\n"
1624 "\n"
1625 " return output;\n"
1626 "}\n";
1627
1628 return true;
1629}
1630
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001631bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1632{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001633 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001634
1635 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001636 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001637 if (format != GL_PROGRAM_BINARY_ANGLE)
1638 {
1639 infoLog.append("Invalid program binary format.");
1640 return false;
1641 }
1642
1643 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001644 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001645 if (version != BUILD_REVISION)
1646 {
1647 infoLog.append("Invalid program binary version.");
1648 return false;
1649 }
1650
1651 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1652 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001653 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001654 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001655 stream.read(&name);
1656 mLinkedAttribute[i].name = name;
1657 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001658 }
1659
1660 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1661 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001662 stream.read(&mSamplersPS[i].active);
1663 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001664
1665 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001666 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001667 mSamplersPS[i].textureType = (TextureType) textureType;
1668 }
1669
1670 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1671 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001672 stream.read(&mSamplersVS[i].active);
1673 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001674
1675 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001676 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001677 mSamplersVS[i].textureType = (TextureType) textureType;
1678 }
1679
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001680 stream.read(&mUsedVertexSamplerRange);
1681 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001682
1683 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001684 stream.read(&size);
1685 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001686 {
1687 infoLog.append("Invalid program binary.");
1688 return false;
1689 }
1690
1691 mUniforms.resize(size);
1692 for (unsigned int i = 0; i < size; ++i)
1693 {
1694 GLenum type;
1695 std::string _name;
1696 unsigned int arraySize;
1697
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001698 stream.read(&type);
1699 stream.read(&_name);
1700 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001701
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001702 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001703
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001704 stream.read(&mUniforms[i]->ps.float4Index);
1705 stream.read(&mUniforms[i]->ps.samplerIndex);
1706 stream.read(&mUniforms[i]->ps.boolIndex);
1707 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001708
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001709 stream.read(&mUniforms[i]->vs.float4Index);
1710 stream.read(&mUniforms[i]->vs.samplerIndex);
1711 stream.read(&mUniforms[i]->vs.boolIndex);
1712 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001713 }
1714
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001715 stream.read(&size);
1716 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001717 {
1718 infoLog.append("Invalid program binary.");
1719 return false;
1720 }
1721
1722 mUniformIndex.resize(size);
1723 for (unsigned int i = 0; i < size; ++i)
1724 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001725 stream.read(&mUniformIndex[i].name);
1726 stream.read(&mUniformIndex[i].element);
1727 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001728 }
1729
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001730 stream.read(&mDxDepthRangeLocation);
1731 stream.read(&mDxDepthLocation);
1732 stream.read(&mDxCoordLocation);
1733 stream.read(&mDxHalfPixelSizeLocation);
1734 stream.read(&mDxFrontCCWLocation);
1735 stream.read(&mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001736
1737 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001738 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001739
1740 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001741 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001742
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001743 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001744
1745 const D3DCAPS9 *binaryIdentifier = (const D3DCAPS9*) ptr;
1746 ptr += sizeof(GUID);
1747
1748 D3DADAPTER_IDENTIFIER9 *currentIdentifier = getDisplay()->getAdapterIdentifier();
1749 if (memcmp(&currentIdentifier->DeviceIdentifier, binaryIdentifier, sizeof(GUID)) != 0)
1750 {
1751 infoLog.append("Invalid program binary.");
1752 return false;
1753 }
1754
1755 const char *pixelShaderFunction = ptr;
1756 ptr += pixelShaderSize;
1757
1758 const char *vertexShaderFunction = ptr;
1759 ptr += vertexShaderSize;
1760
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001761 mPixelExecutable = getDisplay()->createPixelShader(reinterpret_cast<const DWORD*>(pixelShaderFunction), pixelShaderSize);
1762 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001763 {
1764 infoLog.append("Could not create pixel shader.");
1765 return false;
1766 }
1767
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001768 mVertexExecutable = getDisplay()->createVertexShader(reinterpret_cast<const DWORD*>(vertexShaderFunction), vertexShaderSize);
1769 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001770 {
1771 infoLog.append("Could not create vertex shader.");
1772 mPixelExecutable->Release();
1773 mPixelExecutable = NULL;
1774 return false;
1775 }
1776
1777 return true;
1778}
1779
1780bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1781{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001782 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001783
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001784 stream.write(GL_PROGRAM_BINARY_ANGLE);
1785 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001786
1787 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1788 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001789 stream.write(mLinkedAttribute[i].type);
1790 stream.write(mLinkedAttribute[i].name);
1791 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001792 }
1793
1794 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1795 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001796 stream.write(mSamplersPS[i].active);
1797 stream.write(mSamplersPS[i].logicalTextureUnit);
1798 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001799 }
1800
1801 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1802 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001803 stream.write(mSamplersVS[i].active);
1804 stream.write(mSamplersVS[i].logicalTextureUnit);
1805 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001806 }
1807
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001808 stream.write(mUsedVertexSamplerRange);
1809 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001810
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001811 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001812 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1813 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001814 stream.write(mUniforms[i]->type);
1815 stream.write(mUniforms[i]->_name);
1816 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001817
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001818 stream.write(mUniforms[i]->ps.float4Index);
1819 stream.write(mUniforms[i]->ps.samplerIndex);
1820 stream.write(mUniforms[i]->ps.boolIndex);
1821 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001822
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001823 stream.write(mUniforms[i]->vs.float4Index);
1824 stream.write(mUniforms[i]->vs.samplerIndex);
1825 stream.write(mUniforms[i]->vs.boolIndex);
1826 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001827 }
1828
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001829 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001830 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1831 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001832 stream.write(mUniformIndex[i].name);
1833 stream.write(mUniformIndex[i].element);
1834 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001835 }
1836
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001837 stream.write(mDxDepthRangeLocation);
1838 stream.write(mDxDepthLocation);
1839 stream.write(mDxCoordLocation);
1840 stream.write(mDxHalfPixelSizeLocation);
1841 stream.write(mDxFrontCCWLocation);
1842 stream.write(mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001843
1844 UINT pixelShaderSize;
1845 HRESULT result = mPixelExecutable->GetFunction(NULL, &pixelShaderSize);
1846 ASSERT(SUCCEEDED(result));
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001847 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001848
1849 UINT vertexShaderSize;
1850 result = mVertexExecutable->GetFunction(NULL, &vertexShaderSize);
1851 ASSERT(SUCCEEDED(result));
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001852 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001853
1854 D3DADAPTER_IDENTIFIER9 *identifier = getDisplay()->getAdapterIdentifier();
1855
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001856 GLsizei streamLength = stream.length();
1857 const void *streamData = stream.data();
1858
1859 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001860 if (totalLength > bufSize)
1861 {
1862 if (length)
1863 {
1864 *length = 0;
1865 }
1866
1867 return false;
1868 }
1869
1870 if (binary)
1871 {
1872 char *ptr = (char*) binary;
1873
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001874 memcpy(ptr, streamData, streamLength);
1875 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001876
1877 memcpy(ptr, &identifier->DeviceIdentifier, sizeof(GUID));
1878 ptr += sizeof(GUID);
1879
1880 result = mPixelExecutable->GetFunction(ptr, &pixelShaderSize);
1881 ASSERT(SUCCEEDED(result));
1882 ptr += pixelShaderSize;
1883
1884 result = mVertexExecutable->GetFunction(ptr, &vertexShaderSize);
1885 ASSERT(SUCCEEDED(result));
1886 ptr += vertexShaderSize;
1887
1888 ASSERT(ptr - totalLength == binary);
1889 }
1890
1891 if (length)
1892 {
1893 *length = totalLength;
1894 }
1895
1896 return true;
1897}
1898
1899GLint ProgramBinary::getLength()
1900{
1901 GLint length;
1902 if (save(NULL, INT_MAX, &length))
1903 {
1904 return length;
1905 }
1906 else
1907 {
1908 return 0;
1909 }
1910}
1911
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001912bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001913{
1914 if (!fragmentShader || !fragmentShader->isCompiled())
1915 {
1916 return false;
1917 }
1918
1919 if (!vertexShader || !vertexShader->isCompiled())
1920 {
1921 return false;
1922 }
1923
1924 std::string pixelHLSL = fragmentShader->getHLSL();
1925 std::string vertexHLSL = vertexShader->getHLSL();
1926
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001927 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001928 {
1929 return false;
1930 }
1931
1932 Context *context = getContext();
1933 const char *vertexProfile = context->supportsShaderModel3() ? "vs_3_0" : "vs_2_0";
1934 const char *pixelProfile = context->supportsShaderModel3() ? "ps_3_0" : "ps_2_0";
1935
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001936 ID3D10Blob *vertexBinary = compileToBinary(infoLog, vertexHLSL.c_str(), vertexProfile, &mConstantTableVS);
1937 ID3D10Blob *pixelBinary = compileToBinary(infoLog, pixelHLSL.c_str(), pixelProfile, &mConstantTablePS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001938
1939 if (vertexBinary && pixelBinary)
1940 {
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001941 mVertexExecutable = getDisplay()->createVertexShader((DWORD*)vertexBinary->GetBufferPointer(), vertexBinary->GetBufferSize());
1942 if (!mVertexExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001943 {
1944 return error(GL_OUT_OF_MEMORY, false);
1945 }
1946
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001947 mPixelExecutable = getDisplay()->createPixelShader((DWORD*)pixelBinary->GetBufferPointer(), pixelBinary->GetBufferSize());
1948 if (!mPixelExecutable)
1949 {
1950 mVertexExecutable->Release();
1951 mVertexExecutable = NULL;
1952 return error(GL_OUT_OF_MEMORY, false);
1953 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001954
1955 vertexBinary->Release();
1956 pixelBinary->Release();
1957 vertexBinary = NULL;
1958 pixelBinary = NULL;
1959
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001960 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001961 {
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001962 return false;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001963 }
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001964
1965 if (!linkUniforms(infoLog, GL_FRAGMENT_SHADER, mConstantTablePS))
1966 {
1967 return false;
1968 }
1969
1970 if (!linkUniforms(infoLog, GL_VERTEX_SHADER, mConstantTableVS))
1971 {
1972 return false;
1973 }
1974
1975 // these uniforms are searched as already-decorated because gl_ and dx_
1976 // are reserved prefixes, and do not receive additional decoration
1977 mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
1978 mDxDepthLocation = getUniformLocation("dx_Depth");
1979 mDxCoordLocation = getUniformLocation("dx_Coord");
1980 mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
1981 mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
1982 mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
1983
1984 context->markDxUniformsDirty();
1985
1986 return true;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001987 }
1988
1989 return false;
1990}
1991
1992// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001993bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001994{
1995 unsigned int usedLocations = 0;
1996
1997 // Link attributes that have a binding location
1998 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1999 {
2000 int location = attributeBindings.getAttributeBinding(attribute->name);
2001
2002 if (location != -1) // Set by glBindAttribLocation
2003 {
2004 if (!mLinkedAttribute[location].name.empty())
2005 {
2006 // Multiple active attributes bound to the same location; not an error
2007 }
2008
2009 mLinkedAttribute[location] = *attribute;
2010
2011 int rows = VariableRowCount(attribute->type);
2012
2013 if (rows + location > MAX_VERTEX_ATTRIBS)
2014 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002015 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 +00002016
2017 return false;
2018 }
2019
2020 for (int i = 0; i < rows; i++)
2021 {
2022 usedLocations |= 1 << (location + i);
2023 }
2024 }
2025 }
2026
2027 // Link attributes that don't have a binding location
2028 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
2029 {
2030 int location = attributeBindings.getAttributeBinding(attribute->name);
2031
2032 if (location == -1) // Not set by glBindAttribLocation
2033 {
2034 int rows = VariableRowCount(attribute->type);
2035 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
2036
2037 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
2038 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002039 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002040
2041 return false; // Fail to link
2042 }
2043
2044 mLinkedAttribute[availableIndex] = *attribute;
2045 }
2046 }
2047
2048 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
2049 {
2050 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
2051 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
2052
2053 for (int r = 0; r < rows; r++)
2054 {
2055 mSemanticIndex[attributeIndex++] = index++;
2056 }
2057 }
2058
2059 return true;
2060}
2061
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002062bool ProgramBinary::linkUniforms(InfoLog &infoLog, GLenum shader, D3DConstantTable *constantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002063{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002064 for (unsigned int constantIndex = 0; constantIndex < constantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002065 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002066 const D3DConstant *constant = constantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002067
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002068 if (!defineUniform(infoLog, shader, constant))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002069 {
2070 return false;
2071 }
2072 }
2073
2074 return true;
2075}
2076
2077// Adds the description of a constant found in the binary shader to the list of uniforms
2078// Returns true if succesful (uniform not already defined)
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002079bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const D3DConstant *constant, std::string name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002080{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002081 if (constant->registerSet == D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002082 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002083 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002084 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002085 const D3DConstant *psConstant = mConstantTablePS->getConstantByName(constant->name.c_str());
2086 const D3DConstant *vsConstant = mConstantTableVS->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002087
2088 if (psConstant)
2089 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002090 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002091
2092 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2093 {
2094 mSamplersPS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002095 mSamplersPS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002096 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
2097 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
2098 }
2099 else
2100 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002101 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002102 return false;
2103 }
2104 }
2105
2106 if (vsConstant)
2107 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002108 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002109
2110 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
2111 {
2112 mSamplersVS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002113 mSamplersVS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002114 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2115 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2116 }
2117 else
2118 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002119 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002120 return false;
2121 }
2122 }
2123 }
2124 }
2125
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002126 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002127 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002128 case D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002129 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002130 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002131 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002132 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002133 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002134 const D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002135
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002136 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002137
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002138 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + "."))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002139 {
2140 return false;
2141 }
2142 }
2143 }
2144
2145 return true;
2146 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002147 case D3DConstant::CLASS_SCALAR:
2148 case D3DConstant::CLASS_VECTOR:
2149 case D3DConstant::CLASS_MATRIX_COLUMNS:
2150 case D3DConstant::CLASS_OBJECT:
2151 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002152 default:
2153 UNREACHABLE();
2154 return false;
2155 }
2156}
2157
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002158bool ProgramBinary::defineUniform(GLenum shader, const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002159{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002160 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002161
2162 if(!uniform)
2163 {
2164 return false;
2165 }
2166
2167 // Check if already defined
2168 GLint location = getUniformLocation(uniform->name);
2169 GLenum type = uniform->type;
2170
2171 if (location >= 0)
2172 {
2173 delete uniform;
2174 uniform = mUniforms[mUniformIndex[location].index];
2175 }
2176
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002177 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2178 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002179
2180 if (location >= 0)
2181 {
2182 return uniform->type == type;
2183 }
2184
2185 mUniforms.push_back(uniform);
2186 unsigned int uniformIndex = mUniforms.size() - 1;
2187
2188 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2189 {
2190 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2191 }
2192
2193 return true;
2194}
2195
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002196Uniform *ProgramBinary::createUniform(const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002197{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002198 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002199 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002200 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002201 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002202 case D3DConstant::PT_SAMPLER2D:
2203 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002204 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002205 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002206 default: UNREACHABLE();
2207 }
2208 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002209 case D3DConstant::PT_SAMPLERCUBE:
2210 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002211 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002212 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002213 default: UNREACHABLE();
2214 }
2215 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002216 case D3DConstant::PT_BOOL:
2217 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002218 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002219 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2220 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2221 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2222 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002223 default: UNREACHABLE();
2224 }
2225 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002226 case D3DConstant::PT_INT:
2227 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002228 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002229 case 1: return new Uniform(GL_INT, _name, constant->elements);
2230 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2231 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2232 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002233 default: UNREACHABLE();
2234 }
2235 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002236 case D3DConstant::PT_FLOAT:
2237 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002238 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002239 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2240 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2241 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2242 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002243 default: UNREACHABLE();
2244 }
2245 break;
2246 default:
2247 UNREACHABLE();
2248 }
2249 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002250 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002251 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002252 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002253 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002254 case D3DConstant::PT_FLOAT:
2255 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002256 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002257 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2258 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2259 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002260 default: UNREACHABLE();
2261 }
2262 break;
2263 default: UNREACHABLE();
2264 }
2265 }
2266 else UNREACHABLE();
2267
2268 return 0;
2269}
2270
2271// This method needs to match OutputHLSL::decorate
2272std::string ProgramBinary::decorateAttribute(const std::string &name)
2273{
2274 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2275 {
2276 return "_" + name;
2277 }
2278
2279 return name;
2280}
2281
2282std::string ProgramBinary::undecorateUniform(const std::string &_name)
2283{
2284 std::string name = _name;
2285
2286 // Remove any structure field decoration
2287 size_t pos = 0;
2288 while ((pos = name.find("._", pos)) != std::string::npos)
2289 {
2290 name.replace(pos, 2, ".");
2291 }
2292
2293 // Remove the leading decoration
2294 if (name[0] == '_')
2295 {
2296 return name.substr(1);
2297 }
2298 else if (name.compare(0, 3, "ar_") == 0)
2299 {
2300 return name.substr(3);
2301 }
2302
2303 return name;
2304}
2305
2306void ProgramBinary::applyUniformnbv(Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
2307{
2308 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2309 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2310
2311 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2312 {
2313 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2314 for (int i = 0; i < count; i++)
2315 {
2316 for (int j = 0; j < 4; j++)
2317 {
2318 if (j < width)
2319 {
2320 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2321 }
2322 else
2323 {
2324 vector[i * 4 + j] = 0.0f;
2325 }
2326 }
2327 }
2328 }
2329
2330 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2331 {
2332 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2333 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2334 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2335 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2336 for (int i = 0; i < copyCount; i++)
2337 {
2338 boolVector[i] = v[i] != GL_FALSE;
2339 }
2340 }
2341
2342 if (targetUniform->ps.float4Index >= 0)
2343 {
2344 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
2345 }
2346
2347 if (targetUniform->ps.boolIndex >= 0)
2348 {
2349 mDevice->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
2350 }
2351
2352 if (targetUniform->vs.float4Index >= 0)
2353 {
2354 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
2355 }
2356
2357 if (targetUniform->vs.boolIndex >= 0)
2358 {
2359 mDevice->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
2360 }
2361}
2362
2363bool ProgramBinary::applyUniformnfv(Uniform *targetUniform, const GLfloat *v)
2364{
2365 if (targetUniform->ps.registerCount)
2366 {
2367 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
2368 }
2369
2370 if (targetUniform->vs.registerCount)
2371 {
2372 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
2373 }
2374
2375 return true;
2376}
2377
2378bool ProgramBinary::applyUniform1iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2379{
2380 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002381 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002382
2383 for (int i = 0; i < count; i++)
2384 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002385 vector[i] = Vector4((float)v[i], 0, 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002386 }
2387
2388 if (targetUniform->ps.registerCount)
2389 {
2390 if (targetUniform->ps.samplerIndex >= 0)
2391 {
2392 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2393
2394 for (int i = 0; i < count; i++)
2395 {
2396 unsigned int samplerIndex = firstIndex + i;
2397
2398 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2399 {
2400 ASSERT(mSamplersPS[samplerIndex].active);
2401 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2402 }
2403 }
2404 }
2405 else
2406 {
2407 ASSERT(targetUniform->ps.float4Index >= 0);
2408 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
2409 }
2410 }
2411
2412 if (targetUniform->vs.registerCount)
2413 {
2414 if (targetUniform->vs.samplerIndex >= 0)
2415 {
2416 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2417
2418 for (int i = 0; i < count; i++)
2419 {
2420 unsigned int samplerIndex = firstIndex + i;
2421
2422 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2423 {
2424 ASSERT(mSamplersVS[samplerIndex].active);
2425 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2426 }
2427 }
2428 }
2429 else
2430 {
2431 ASSERT(targetUniform->vs.float4Index >= 0);
2432 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
2433 }
2434 }
2435
2436 return true;
2437}
2438
2439bool ProgramBinary::applyUniform2iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2440{
2441 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002442 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002443
2444 for (int i = 0; i < count; i++)
2445 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002446 vector[i] = Vector4((float)v[0], (float)v[1], 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002447
2448 v += 2;
2449 }
2450
2451 applyUniformniv(targetUniform, count, vector);
2452
2453 return true;
2454}
2455
2456bool ProgramBinary::applyUniform3iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2457{
2458 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002459 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002460
2461 for (int i = 0; i < count; i++)
2462 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002463 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002464
2465 v += 3;
2466 }
2467
2468 applyUniformniv(targetUniform, count, vector);
2469
2470 return true;
2471}
2472
2473bool ProgramBinary::applyUniform4iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2474{
2475 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002476 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002477
2478 for (int i = 0; i < count; i++)
2479 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002480 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002481
2482 v += 4;
2483 }
2484
2485 applyUniformniv(targetUniform, count, vector);
2486
2487 return true;
2488}
2489
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002490void ProgramBinary::applyUniformniv(Uniform *targetUniform, GLsizei count, const Vector4 *vector)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002491{
2492 if (targetUniform->ps.registerCount)
2493 {
2494 ASSERT(targetUniform->ps.float4Index >= 0);
2495 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
2496 }
2497
2498 if (targetUniform->vs.registerCount)
2499 {
2500 ASSERT(targetUniform->vs.float4Index >= 0);
2501 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
2502 }
2503}
2504
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002505bool ProgramBinary::isValidated() const
2506{
2507 return mValidated;
2508}
2509
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002510void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2511{
2512 // Skip over inactive attributes
2513 unsigned int activeAttribute = 0;
2514 unsigned int attribute;
2515 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2516 {
2517 if (mLinkedAttribute[attribute].name.empty())
2518 {
2519 continue;
2520 }
2521
2522 if (activeAttribute == index)
2523 {
2524 break;
2525 }
2526
2527 activeAttribute++;
2528 }
2529
2530 if (bufsize > 0)
2531 {
2532 const char *string = mLinkedAttribute[attribute].name.c_str();
2533
2534 strncpy(name, string, bufsize);
2535 name[bufsize - 1] = '\0';
2536
2537 if (length)
2538 {
2539 *length = strlen(name);
2540 }
2541 }
2542
2543 *size = 1; // Always a single 'type' instance
2544
2545 *type = mLinkedAttribute[attribute].type;
2546}
2547
2548GLint ProgramBinary::getActiveAttributeCount()
2549{
2550 int count = 0;
2551
2552 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2553 {
2554 if (!mLinkedAttribute[attributeIndex].name.empty())
2555 {
2556 count++;
2557 }
2558 }
2559
2560 return count;
2561}
2562
2563GLint ProgramBinary::getActiveAttributeMaxLength()
2564{
2565 int maxLength = 0;
2566
2567 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2568 {
2569 if (!mLinkedAttribute[attributeIndex].name.empty())
2570 {
2571 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2572 }
2573 }
2574
2575 return maxLength;
2576}
2577
2578void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2579{
2580 // Skip over internal uniforms
2581 unsigned int activeUniform = 0;
2582 unsigned int uniform;
2583 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2584 {
2585 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2586 {
2587 continue;
2588 }
2589
2590 if (activeUniform == index)
2591 {
2592 break;
2593 }
2594
2595 activeUniform++;
2596 }
2597
2598 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2599
2600 if (bufsize > 0)
2601 {
2602 std::string string = mUniforms[uniform]->name;
2603
2604 if (mUniforms[uniform]->isArray())
2605 {
2606 string += "[0]";
2607 }
2608
2609 strncpy(name, string.c_str(), bufsize);
2610 name[bufsize - 1] = '\0';
2611
2612 if (length)
2613 {
2614 *length = strlen(name);
2615 }
2616 }
2617
2618 *size = mUniforms[uniform]->arraySize;
2619
2620 *type = mUniforms[uniform]->type;
2621}
2622
2623GLint ProgramBinary::getActiveUniformCount()
2624{
2625 int count = 0;
2626
2627 unsigned int numUniforms = mUniforms.size();
2628 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2629 {
2630 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2631 {
2632 count++;
2633 }
2634 }
2635
2636 return count;
2637}
2638
2639GLint ProgramBinary::getActiveUniformMaxLength()
2640{
2641 int maxLength = 0;
2642
2643 unsigned int numUniforms = mUniforms.size();
2644 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2645 {
2646 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2647 {
2648 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2649 if (mUniforms[uniformIndex]->isArray())
2650 {
2651 length += 3; // Counting in "[0]".
2652 }
2653 maxLength = std::max(length, maxLength);
2654 }
2655 }
2656
2657 return maxLength;
2658}
2659
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002660void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002661{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002662 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002663 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002664 {
2665 mValidated = false;
2666 }
2667 else
2668 {
2669 mValidated = true;
2670 }
2671}
2672
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002673bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002674{
2675 // if any two active samplers in a program are of different types, but refer to the same
2676 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2677 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2678
2679 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2680 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2681
2682 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2683 {
2684 textureUnitType[i] = TEXTURE_UNKNOWN;
2685 }
2686
2687 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2688 {
2689 if (mSamplersPS[i].active)
2690 {
2691 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2692
2693 if (unit >= maxCombinedTextureImageUnits)
2694 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002695 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002696 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002697 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002698 }
2699
2700 return false;
2701 }
2702
2703 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2704 {
2705 if (mSamplersPS[i].textureType != textureUnitType[unit])
2706 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002707 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002708 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002709 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002710 }
2711
2712 return false;
2713 }
2714 }
2715 else
2716 {
2717 textureUnitType[unit] = mSamplersPS[i].textureType;
2718 }
2719 }
2720 }
2721
2722 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2723 {
2724 if (mSamplersVS[i].active)
2725 {
2726 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2727
2728 if (unit >= maxCombinedTextureImageUnits)
2729 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002730 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002731 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002732 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002733 }
2734
2735 return false;
2736 }
2737
2738 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2739 {
2740 if (mSamplersVS[i].textureType != textureUnitType[unit])
2741 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002742 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002743 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002744 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002745 }
2746
2747 return false;
2748 }
2749 }
2750 else
2751 {
2752 textureUnitType[unit] = mSamplersVS[i].textureType;
2753 }
2754 }
2755 }
2756
2757 return true;
2758}
2759
2760GLint ProgramBinary::getDxDepthRangeLocation() const
2761{
2762 return mDxDepthRangeLocation;
2763}
2764
2765GLint ProgramBinary::getDxDepthLocation() const
2766{
2767 return mDxDepthLocation;
2768}
2769
2770GLint ProgramBinary::getDxCoordLocation() const
2771{
2772 return mDxCoordLocation;
2773}
2774
2775GLint ProgramBinary::getDxHalfPixelSizeLocation() const
2776{
2777 return mDxHalfPixelSizeLocation;
2778}
2779
2780GLint ProgramBinary::getDxFrontCCWLocation() const
2781{
2782 return mDxFrontCCWLocation;
2783}
2784
2785GLint ProgramBinary::getDxPointsOrLinesLocation() const
2786{
2787 return mDxPointsOrLinesLocation;
2788}
2789
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002790ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2791{
2792}
2793
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002794}