blob: a3afc98b553ce163448c4c7536388a899e71a115 [file] [log] [blame]
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001//
2// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +000010#include "libGLESv2/BinaryStream.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000011#include "libGLESv2/Program.h"
12#include "libGLESv2/ProgramBinary.h"
13
14#include "common/debug.h"
apatrick@chromium.org90080e32012-07-09 22:15:33 +000015#include "common/version.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000016
17#include "libGLESv2/main.h"
18#include "libGLESv2/Shader.h"
19#include "libGLESv2/utilities.h"
20
21#include <string>
22
23#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
24#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
25#endif
26
27namespace gl
28{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000029std::string str(int i)
30{
31 char buffer[20];
32 snprintf(buffer, sizeof(buffer), "%d", i);
33 return buffer;
34}
35
36Uniform::Uniform(GLenum type, const std::string &_name, unsigned int arraySize)
37 : type(type), _name(_name), name(ProgramBinary::undecorateUniform(_name)), arraySize(arraySize)
38{
39 int bytes = UniformInternalSize(type) * arraySize;
40 data = new unsigned char[bytes];
41 memset(data, 0, bytes);
42 dirty = true;
43}
44
45Uniform::~Uniform()
46{
47 delete[] data;
48}
49
50bool Uniform::isArray()
51{
52 return _name.compare(0, 3, "ar_") == 0;
53}
54
55UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
56 : name(ProgramBinary::undecorateUniform(_name)), element(element), index(index)
57{
58}
59
daniel@transgaming.come87ca002012-07-24 18:30:43 +000060unsigned int ProgramBinary::mCurrentSerial = 1;
61
daniel@transgaming.com989c1c82012-07-24 18:40:38 +000062ProgramBinary::ProgramBinary() : RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000063{
64 mDevice = getDevice();
65
66 mPixelExecutable = NULL;
67 mVertexExecutable = NULL;
68 mConstantTablePS = NULL;
69 mConstantTableVS = NULL;
70
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000071 mValidated = false;
72
73 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
74 {
75 mSemanticIndex[index] = -1;
76 }
77
78 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
79 {
80 mSamplersPS[index].active = false;
81 }
82
83 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
84 {
85 mSamplersVS[index].active = false;
86 }
87
88 mUsedVertexSamplerRange = 0;
89 mUsedPixelSamplerRange = 0;
90
91 mDxDepthRangeLocation = -1;
92 mDxDepthLocation = -1;
93 mDxCoordLocation = -1;
94 mDxHalfPixelSizeLocation = -1;
95 mDxFrontCCWLocation = -1;
96 mDxPointsOrLinesLocation = -1;
97}
98
99ProgramBinary::~ProgramBinary()
100{
101 if (mPixelExecutable)
102 {
103 mPixelExecutable->Release();
104 }
105
106 if (mVertexExecutable)
107 {
108 mVertexExecutable->Release();
109 }
110
apatrick@chromium.org60dafe82012-09-05 22:26:10 +0000111 delete mConstantTablePS;
112 delete mConstantTableVS;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000113
114 while (!mUniforms.empty())
115 {
116 delete mUniforms.back();
117 mUniforms.pop_back();
118 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000119}
120
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000121unsigned int ProgramBinary::getSerial() const
122{
123 return mSerial;
124}
125
126unsigned int ProgramBinary::issueSerial()
127{
128 return mCurrentSerial++;
129}
130
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000131IDirect3DPixelShader9 *ProgramBinary::getPixelShader()
132{
133 return mPixelExecutable;
134}
135
136IDirect3DVertexShader9 *ProgramBinary::getVertexShader()
137{
138 return mVertexExecutable;
139}
140
141GLuint ProgramBinary::getAttributeLocation(const char *name)
142{
143 if (name)
144 {
145 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
146 {
147 if (mLinkedAttribute[index].name == std::string(name))
148 {
149 return index;
150 }
151 }
152 }
153
154 return -1;
155}
156
157int ProgramBinary::getSemanticIndex(int attributeIndex)
158{
159 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
160
161 return mSemanticIndex[attributeIndex];
162}
163
164// Returns one more than the highest sampler index used.
165GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
166{
167 switch (type)
168 {
169 case SAMPLER_PIXEL:
170 return mUsedPixelSamplerRange;
171 case SAMPLER_VERTEX:
172 return mUsedVertexSamplerRange;
173 default:
174 UNREACHABLE();
175 return 0;
176 }
177}
178
179// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
180// index (0-15 for the pixel shader and 0-3 for the vertex shader).
181GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
182{
183 GLint logicalTextureUnit = -1;
184
185 switch (type)
186 {
187 case SAMPLER_PIXEL:
188 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
189
190 if (mSamplersPS[samplerIndex].active)
191 {
192 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
193 }
194 break;
195 case SAMPLER_VERTEX:
196 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
197
198 if (mSamplersVS[samplerIndex].active)
199 {
200 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
201 }
202 break;
203 default: UNREACHABLE();
204 }
205
206 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
207 {
208 return logicalTextureUnit;
209 }
210
211 return -1;
212}
213
214// Returns the texture type for a given Direct3D 9 sampler type and
215// index (0-15 for the pixel shader and 0-3 for the vertex shader).
216TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
217{
218 switch (type)
219 {
220 case SAMPLER_PIXEL:
221 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
222 ASSERT(mSamplersPS[samplerIndex].active);
223 return mSamplersPS[samplerIndex].textureType;
224 case SAMPLER_VERTEX:
225 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
226 ASSERT(mSamplersVS[samplerIndex].active);
227 return mSamplersVS[samplerIndex].textureType;
228 default: UNREACHABLE();
229 }
230
231 return TEXTURE_2D;
232}
233
234GLint ProgramBinary::getUniformLocation(std::string name)
235{
236 unsigned int subscript = 0;
237
238 // Strip any trailing array operator and retrieve the subscript
239 size_t open = name.find_last_of('[');
240 size_t close = name.find_last_of(']');
241 if (open != std::string::npos && close == name.length() - 1)
242 {
243 subscript = atoi(name.substr(open + 1).c_str());
244 name.erase(open);
245 }
246
247 unsigned int numUniforms = mUniformIndex.size();
248 for (unsigned int location = 0; location < numUniforms; location++)
249 {
250 if (mUniformIndex[location].name == name &&
251 mUniformIndex[location].element == subscript)
252 {
253 return location;
254 }
255 }
256
257 return -1;
258}
259
260bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
261{
262 if (location < 0 || location >= (int)mUniformIndex.size())
263 {
264 return false;
265 }
266
267 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
268 targetUniform->dirty = true;
269
270 if (targetUniform->type == GL_FLOAT)
271 {
272 int arraySize = targetUniform->arraySize;
273
274 if (arraySize == 1 && count > 1)
275 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
276
277 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
278
279 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
280
281 for (int i = 0; i < count; i++)
282 {
283 target[0] = v[0];
284 target[1] = 0;
285 target[2] = 0;
286 target[3] = 0;
287 target += 4;
288 v += 1;
289 }
290 }
291 else if (targetUniform->type == GL_BOOL)
292 {
293 int arraySize = targetUniform->arraySize;
294
295 if (arraySize == 1 && count > 1)
296 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
297
298 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
299 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
300
301 for (int i = 0; i < count; ++i)
302 {
303 if (v[i] == 0.0f)
304 {
305 boolParams[i] = GL_FALSE;
306 }
307 else
308 {
309 boolParams[i] = GL_TRUE;
310 }
311 }
312 }
313 else
314 {
315 return false;
316 }
317
318 return true;
319}
320
321bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
322{
323 if (location < 0 || location >= (int)mUniformIndex.size())
324 {
325 return false;
326 }
327
328 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
329 targetUniform->dirty = true;
330
331 if (targetUniform->type == GL_FLOAT_VEC2)
332 {
333 int arraySize = targetUniform->arraySize;
334
335 if (arraySize == 1 && count > 1)
336 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
337
338 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
339
340 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
341
342 for (int i = 0; i < count; i++)
343 {
344 target[0] = v[0];
345 target[1] = v[1];
346 target[2] = 0;
347 target[3] = 0;
348 target += 4;
349 v += 2;
350 }
351 }
352 else if (targetUniform->type == GL_BOOL_VEC2)
353 {
354 int arraySize = targetUniform->arraySize;
355
356 if (arraySize == 1 && count > 1)
357 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
358
359 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
360
361 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
362
363 for (int i = 0; i < count * 2; ++i)
364 {
365 if (v[i] == 0.0f)
366 {
367 boolParams[i] = GL_FALSE;
368 }
369 else
370 {
371 boolParams[i] = GL_TRUE;
372 }
373 }
374 }
375 else
376 {
377 return false;
378 }
379
380 return true;
381}
382
383bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
384{
385 if (location < 0 || location >= (int)mUniformIndex.size())
386 {
387 return false;
388 }
389
390 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
391 targetUniform->dirty = true;
392
393 if (targetUniform->type == GL_FLOAT_VEC3)
394 {
395 int arraySize = targetUniform->arraySize;
396
397 if (arraySize == 1 && count > 1)
398 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
399
400 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
401
402 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
403
404 for (int i = 0; i < count; i++)
405 {
406 target[0] = v[0];
407 target[1] = v[1];
408 target[2] = v[2];
409 target[3] = 0;
410 target += 4;
411 v += 3;
412 }
413 }
414 else if (targetUniform->type == GL_BOOL_VEC3)
415 {
416 int arraySize = targetUniform->arraySize;
417
418 if (arraySize == 1 && count > 1)
419 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
420
421 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
422 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
423
424 for (int i = 0; i < count * 3; ++i)
425 {
426 if (v[i] == 0.0f)
427 {
428 boolParams[i] = GL_FALSE;
429 }
430 else
431 {
432 boolParams[i] = GL_TRUE;
433 }
434 }
435 }
436 else
437 {
438 return false;
439 }
440
441 return true;
442}
443
444bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
445{
446 if (location < 0 || location >= (int)mUniformIndex.size())
447 {
448 return false;
449 }
450
451 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
452 targetUniform->dirty = true;
453
454 if (targetUniform->type == GL_FLOAT_VEC4)
455 {
456 int arraySize = targetUniform->arraySize;
457
458 if (arraySize == 1 && count > 1)
459 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
460
461 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
462
463 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
464 v, 4 * sizeof(GLfloat) * count);
465 }
466 else if (targetUniform->type == GL_BOOL_VEC4)
467 {
468 int arraySize = targetUniform->arraySize;
469
470 if (arraySize == 1 && count > 1)
471 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
472
473 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
474 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
475
476 for (int i = 0; i < count * 4; ++i)
477 {
478 if (v[i] == 0.0f)
479 {
480 boolParams[i] = GL_FALSE;
481 }
482 else
483 {
484 boolParams[i] = GL_TRUE;
485 }
486 }
487 }
488 else
489 {
490 return false;
491 }
492
493 return true;
494}
495
496template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
497void transposeMatrix(T *target, const GLfloat *value)
498{
499 int copyWidth = std::min(targetWidth, srcWidth);
500 int copyHeight = std::min(targetHeight, srcHeight);
501
502 for (int x = 0; x < copyWidth; x++)
503 {
504 for (int y = 0; y < copyHeight; y++)
505 {
506 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
507 }
508 }
509 // clear unfilled right side
510 for (int y = 0; y < copyHeight; y++)
511 {
512 for (int x = srcWidth; x < targetWidth; x++)
513 {
514 target[y * targetWidth + x] = (T)0;
515 }
516 }
517 // clear unfilled bottom.
518 for (int y = srcHeight; y < targetHeight; y++)
519 {
520 for (int x = 0; x < targetWidth; x++)
521 {
522 target[y * targetWidth + x] = (T)0;
523 }
524 }
525}
526
527bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
528{
529 if (location < 0 || location >= (int)mUniformIndex.size())
530 {
531 return false;
532 }
533
534 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
535 targetUniform->dirty = true;
536
537 if (targetUniform->type != GL_FLOAT_MAT2)
538 {
539 return false;
540 }
541
542 int arraySize = targetUniform->arraySize;
543
544 if (arraySize == 1 && count > 1)
545 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
546
547 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
548
549 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
550 for (int i = 0; i < count; i++)
551 {
552 transposeMatrix<GLfloat,4,2,2,2>(target, value);
553 target += 8;
554 value += 4;
555 }
556
557 return true;
558}
559
560bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
561{
562 if (location < 0 || location >= (int)mUniformIndex.size())
563 {
564 return false;
565 }
566
567 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
568 targetUniform->dirty = true;
569
570 if (targetUniform->type != GL_FLOAT_MAT3)
571 {
572 return false;
573 }
574
575 int arraySize = targetUniform->arraySize;
576
577 if (arraySize == 1 && count > 1)
578 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
579
580 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
581
582 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
583 for (int i = 0; i < count; i++)
584 {
585 transposeMatrix<GLfloat,4,3,3,3>(target, value);
586 target += 12;
587 value += 9;
588 }
589
590 return true;
591}
592
593
594bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
595{
596 if (location < 0 || location >= (int)mUniformIndex.size())
597 {
598 return false;
599 }
600
601 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
602 targetUniform->dirty = true;
603
604 if (targetUniform->type != GL_FLOAT_MAT4)
605 {
606 return false;
607 }
608
609 int arraySize = targetUniform->arraySize;
610
611 if (arraySize == 1 && count > 1)
612 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
613
614 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
615
616 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
617 for (int i = 0; i < count; i++)
618 {
619 transposeMatrix<GLfloat,4,4,4,4>(target, value);
620 target += 16;
621 value += 16;
622 }
623
624 return true;
625}
626
627bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
628{
629 if (location < 0 || location >= (int)mUniformIndex.size())
630 {
631 return false;
632 }
633
634 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
635 targetUniform->dirty = true;
636
637 if (targetUniform->type == GL_INT ||
638 targetUniform->type == GL_SAMPLER_2D ||
639 targetUniform->type == GL_SAMPLER_CUBE)
640 {
641 int arraySize = targetUniform->arraySize;
642
643 if (arraySize == 1 && count > 1)
644 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
645
646 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
647
648 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
649 v, sizeof(GLint) * count);
650 }
651 else if (targetUniform->type == GL_BOOL)
652 {
653 int arraySize = targetUniform->arraySize;
654
655 if (arraySize == 1 && count > 1)
656 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
657
658 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
659 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
660
661 for (int i = 0; i < count; ++i)
662 {
663 if (v[i] == 0)
664 {
665 boolParams[i] = GL_FALSE;
666 }
667 else
668 {
669 boolParams[i] = GL_TRUE;
670 }
671 }
672 }
673 else
674 {
675 return false;
676 }
677
678 return true;
679}
680
681bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
682{
683 if (location < 0 || location >= (int)mUniformIndex.size())
684 {
685 return false;
686 }
687
688 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
689 targetUniform->dirty = true;
690
691 if (targetUniform->type == GL_INT_VEC2)
692 {
693 int arraySize = targetUniform->arraySize;
694
695 if (arraySize == 1 && count > 1)
696 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
697
698 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
699
700 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
701 v, 2 * sizeof(GLint) * count);
702 }
703 else if (targetUniform->type == GL_BOOL_VEC2)
704 {
705 int arraySize = targetUniform->arraySize;
706
707 if (arraySize == 1 && count > 1)
708 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
709
710 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
711 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
712
713 for (int i = 0; i < count * 2; ++i)
714 {
715 if (v[i] == 0)
716 {
717 boolParams[i] = GL_FALSE;
718 }
719 else
720 {
721 boolParams[i] = GL_TRUE;
722 }
723 }
724 }
725 else
726 {
727 return false;
728 }
729
730 return true;
731}
732
733bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
734{
735 if (location < 0 || location >= (int)mUniformIndex.size())
736 {
737 return false;
738 }
739
740 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
741 targetUniform->dirty = true;
742
743 if (targetUniform->type == GL_INT_VEC3)
744 {
745 int arraySize = targetUniform->arraySize;
746
747 if (arraySize == 1 && count > 1)
748 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
749
750 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
751
752 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
753 v, 3 * sizeof(GLint) * count);
754 }
755 else if (targetUniform->type == GL_BOOL_VEC3)
756 {
757 int arraySize = targetUniform->arraySize;
758
759 if (arraySize == 1 && count > 1)
760 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
761
762 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
763 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
764
765 for (int i = 0; i < count * 3; ++i)
766 {
767 if (v[i] == 0)
768 {
769 boolParams[i] = GL_FALSE;
770 }
771 else
772 {
773 boolParams[i] = GL_TRUE;
774 }
775 }
776 }
777 else
778 {
779 return false;
780 }
781
782 return true;
783}
784
785bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
786{
787 if (location < 0 || location >= (int)mUniformIndex.size())
788 {
789 return false;
790 }
791
792 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
793 targetUniform->dirty = true;
794
795 if (targetUniform->type == GL_INT_VEC4)
796 {
797 int arraySize = targetUniform->arraySize;
798
799 if (arraySize == 1 && count > 1)
800 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
801
802 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
803
804 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
805 v, 4 * sizeof(GLint) * count);
806 }
807 else if (targetUniform->type == GL_BOOL_VEC4)
808 {
809 int arraySize = targetUniform->arraySize;
810
811 if (arraySize == 1 && count > 1)
812 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
813
814 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
815 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
816
817 for (int i = 0; i < count * 4; ++i)
818 {
819 if (v[i] == 0)
820 {
821 boolParams[i] = GL_FALSE;
822 }
823 else
824 {
825 boolParams[i] = GL_TRUE;
826 }
827 }
828 }
829 else
830 {
831 return false;
832 }
833
834 return true;
835}
836
837bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
838{
839 if (location < 0 || location >= (int)mUniformIndex.size())
840 {
841 return false;
842 }
843
844 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
845
846 // sized queries -- ensure the provided buffer is large enough
847 if (bufSize)
848 {
849 int requiredBytes = UniformExternalSize(targetUniform->type);
850 if (*bufSize < requiredBytes)
851 {
852 return false;
853 }
854 }
855
856 switch (targetUniform->type)
857 {
858 case GL_FLOAT_MAT2:
859 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
860 break;
861 case GL_FLOAT_MAT3:
862 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
863 break;
864 case GL_FLOAT_MAT4:
865 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
866 break;
867 default:
868 {
869 unsigned int count = UniformExternalComponentCount(targetUniform->type);
870 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
871
872 switch (UniformComponentType(targetUniform->type))
873 {
874 case GL_BOOL:
875 {
876 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
877
878 for (unsigned int i = 0; i < count; ++i)
879 {
880 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
881 }
882 }
883 break;
884 case GL_FLOAT:
885 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
886 count * sizeof(GLfloat));
887 break;
888 case GL_INT:
889 {
890 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
891
892 for (unsigned int i = 0; i < count; ++i)
893 {
894 params[i] = (float)intParams[i];
895 }
896 }
897 break;
898 default: UNREACHABLE();
899 }
900 }
901 }
902
903 return true;
904}
905
906bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
907{
908 if (location < 0 || location >= (int)mUniformIndex.size())
909 {
910 return false;
911 }
912
913 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
914
915 // sized queries -- ensure the provided buffer is large enough
916 if (bufSize)
917 {
918 int requiredBytes = UniformExternalSize(targetUniform->type);
919 if (*bufSize < requiredBytes)
920 {
921 return false;
922 }
923 }
924
925 switch (targetUniform->type)
926 {
927 case GL_FLOAT_MAT2:
928 {
929 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
930 }
931 break;
932 case GL_FLOAT_MAT3:
933 {
934 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
935 }
936 break;
937 case GL_FLOAT_MAT4:
938 {
939 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
940 }
941 break;
942 default:
943 {
944 unsigned int count = UniformExternalComponentCount(targetUniform->type);
945 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
946
947 switch (UniformComponentType(targetUniform->type))
948 {
949 case GL_BOOL:
950 {
951 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
952
953 for (unsigned int i = 0; i < count; ++i)
954 {
955 params[i] = (GLint)boolParams[i];
956 }
957 }
958 break;
959 case GL_FLOAT:
960 {
961 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
962
963 for (unsigned int i = 0; i < count; ++i)
964 {
965 params[i] = (GLint)floatParams[i];
966 }
967 }
968 break;
969 case GL_INT:
970 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
971 count * sizeof(GLint));
972 break;
973 default: UNREACHABLE();
974 }
975 }
976 }
977
978 return true;
979}
980
981void ProgramBinary::dirtyAllUniforms()
982{
983 unsigned int numUniforms = mUniforms.size();
984 for (unsigned int index = 0; index < numUniforms; index++)
985 {
986 mUniforms[index]->dirty = true;
987 }
988}
989
990// Applies all the uniforms set for this program object to the Direct3D 9 device
991void ProgramBinary::applyUniforms()
992{
993 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub) {
994 Uniform *targetUniform = *ub;
995
996 if (targetUniform->dirty)
997 {
998 int arraySize = targetUniform->arraySize;
999 GLfloat *f = (GLfloat*)targetUniform->data;
1000 GLint *i = (GLint*)targetUniform->data;
1001 GLboolean *b = (GLboolean*)targetUniform->data;
1002
1003 switch (targetUniform->type)
1004 {
1005 case GL_BOOL: applyUniformnbv(targetUniform, arraySize, 1, b); break;
1006 case GL_BOOL_VEC2: applyUniformnbv(targetUniform, arraySize, 2, b); break;
1007 case GL_BOOL_VEC3: applyUniformnbv(targetUniform, arraySize, 3, b); break;
1008 case GL_BOOL_VEC4: applyUniformnbv(targetUniform, arraySize, 4, b); break;
1009 case GL_FLOAT:
1010 case GL_FLOAT_VEC2:
1011 case GL_FLOAT_VEC3:
1012 case GL_FLOAT_VEC4:
1013 case GL_FLOAT_MAT2:
1014 case GL_FLOAT_MAT3:
1015 case GL_FLOAT_MAT4: applyUniformnfv(targetUniform, f); break;
1016 case GL_SAMPLER_2D:
1017 case GL_SAMPLER_CUBE:
1018 case GL_INT: applyUniform1iv(targetUniform, arraySize, i); break;
1019 case GL_INT_VEC2: applyUniform2iv(targetUniform, arraySize, i); break;
1020 case GL_INT_VEC3: applyUniform3iv(targetUniform, arraySize, i); break;
1021 case GL_INT_VEC4: applyUniform4iv(targetUniform, arraySize, i); break;
1022 default:
1023 UNREACHABLE();
1024 }
1025
1026 targetUniform->dirty = false;
1027 }
1028 }
1029}
1030
1031// Compiles the HLSL code of the attached shaders into executable binaries
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001032ID3D10Blob *ProgramBinary::compileToBinary(InfoLog &infoLog, const char *hlsl, const char *profile, D3DConstantTable **constantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001033{
1034 if (!hlsl)
1035 {
1036 return NULL;
1037 }
1038
1039 DWORD result;
1040 UINT flags = 0;
1041 std::string sourceText;
1042 if (perfActive())
1043 {
1044 flags |= D3DCOMPILE_DEBUG;
1045#ifdef NDEBUG
1046 flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
1047#else
1048 flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
1049#endif
1050
1051 std::string sourcePath = getTempPath();
1052 sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl);
1053 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
1054 }
1055 else
1056 {
1057 flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
1058 sourceText = hlsl;
1059 }
1060
1061 ID3D10Blob *binary = NULL;
1062 ID3D10Blob *errorMessage = NULL;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001063 result = D3DCompile(hlsl, strlen(hlsl), g_fakepath, NULL, NULL, "main", profile, flags, 0, &binary, &errorMessage);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001064
1065 if (errorMessage)
1066 {
1067 const char *message = (const char*)errorMessage->GetBufferPointer();
1068
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001069 infoLog.appendSanitized(message);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001070 TRACE("\n%s", hlsl);
1071 TRACE("\n%s", message);
1072
1073 errorMessage->Release();
1074 errorMessage = NULL;
1075 }
1076
1077 if (FAILED(result))
1078 {
1079 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1080 {
1081 error(GL_OUT_OF_MEMORY);
1082 }
1083
1084 return NULL;
1085 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001086
1087 D3DConstantTable *table = new D3DConstantTable(binary->GetBufferPointer(), binary->GetBufferSize());
1088 if (table->error())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001089 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001090 delete table;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001091 binary->Release();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001092 return NULL;
1093 }
1094
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001095 *constantTable = table;
1096
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001097 return binary;
1098}
1099
1100// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1101// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001102int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001103{
1104 Context *context = getContext();
1105 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1106
1107 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1108 {
1109 int n = VariableRowCount(varying->type) * varying->size;
1110 int m = VariableColumnCount(varying->type);
1111 bool success = false;
1112
1113 if (m == 2 || m == 3 || m == 4)
1114 {
1115 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1116 {
1117 bool available = true;
1118
1119 for (int y = 0; y < n && available; y++)
1120 {
1121 for (int x = 0; x < m && available; x++)
1122 {
1123 if (packing[r + y][x])
1124 {
1125 available = false;
1126 }
1127 }
1128 }
1129
1130 if (available)
1131 {
1132 varying->reg = r;
1133 varying->col = 0;
1134
1135 for (int y = 0; y < n; y++)
1136 {
1137 for (int x = 0; x < m; x++)
1138 {
1139 packing[r + y][x] = &*varying;
1140 }
1141 }
1142
1143 success = true;
1144 }
1145 }
1146
1147 if (!success && m == 2)
1148 {
1149 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1150 {
1151 bool available = true;
1152
1153 for (int y = 0; y < n && available; y++)
1154 {
1155 for (int x = 2; x < 4 && available; x++)
1156 {
1157 if (packing[r + y][x])
1158 {
1159 available = false;
1160 }
1161 }
1162 }
1163
1164 if (available)
1165 {
1166 varying->reg = r;
1167 varying->col = 2;
1168
1169 for (int y = 0; y < n; y++)
1170 {
1171 for (int x = 2; x < 4; x++)
1172 {
1173 packing[r + y][x] = &*varying;
1174 }
1175 }
1176
1177 success = true;
1178 }
1179 }
1180 }
1181 }
1182 else if (m == 1)
1183 {
1184 int space[4] = {0};
1185
1186 for (int y = 0; y < maxVaryingVectors; y++)
1187 {
1188 for (int x = 0; x < 4; x++)
1189 {
1190 space[x] += packing[y][x] ? 0 : 1;
1191 }
1192 }
1193
1194 int column = 0;
1195
1196 for (int x = 0; x < 4; x++)
1197 {
1198 if (space[x] >= n && space[x] < space[column])
1199 {
1200 column = x;
1201 }
1202 }
1203
1204 if (space[column] >= n)
1205 {
1206 for (int r = 0; r < maxVaryingVectors; r++)
1207 {
1208 if (!packing[r][column])
1209 {
1210 varying->reg = r;
1211
1212 for (int y = r; y < r + n; y++)
1213 {
1214 packing[y][column] = &*varying;
1215 }
1216
1217 break;
1218 }
1219 }
1220
1221 varying->col = column;
1222
1223 success = true;
1224 }
1225 }
1226 else UNREACHABLE();
1227
1228 if (!success)
1229 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001230 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001231
1232 return -1;
1233 }
1234 }
1235
1236 // Return the number of used registers
1237 int registers = 0;
1238
1239 for (int r = 0; r < maxVaryingVectors; r++)
1240 {
1241 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1242 {
1243 registers++;
1244 }
1245 }
1246
1247 return registers;
1248}
1249
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001250bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001251{
1252 if (pixelHLSL.empty() || vertexHLSL.empty())
1253 {
1254 return false;
1255 }
1256
1257 // Reset the varying register assignments
1258 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1259 {
1260 fragVar->reg = -1;
1261 fragVar->col = -1;
1262 }
1263
1264 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1265 {
1266 vtxVar->reg = -1;
1267 vtxVar->col = -1;
1268 }
1269
1270 // Map the varyings to the register file
1271 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001272 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001273
1274 if (registers < 0)
1275 {
1276 return false;
1277 }
1278
1279 // Write the HLSL input/output declarations
1280 Context *context = getContext();
1281 const bool sm3 = context->supportsShaderModel3();
1282 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1283
1284 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1285 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001286 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001287
1288 return false;
1289 }
1290
1291 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1292 {
1293 bool matched = false;
1294
1295 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1296 {
1297 if (output->name == input->name)
1298 {
1299 if (output->type != input->type || output->size != input->size)
1300 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001301 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 +00001302
1303 return false;
1304 }
1305
1306 output->reg = input->reg;
1307 output->col = input->col;
1308
1309 matched = true;
1310 break;
1311 }
1312 }
1313
1314 if (!matched)
1315 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001316 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001317
1318 return false;
1319 }
1320 }
1321
daniel@transgaming.com08b3e402012-07-04 19:16:35 +00001322 std::string varyingSemantic = (vertexShader->mUsesPointSize && sm3 ? "COLOR" : "TEXCOORD");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001323
1324 vertexHLSL += "struct VS_INPUT\n"
1325 "{\n";
1326
1327 int semanticIndex = 0;
1328 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1329 {
1330 switch (attribute->type)
1331 {
1332 case GL_FLOAT: vertexHLSL += " float "; break;
1333 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1334 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1335 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1336 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1337 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1338 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1339 default: UNREACHABLE();
1340 }
1341
1342 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1343
1344 semanticIndex += VariableRowCount(attribute->type);
1345 }
1346
1347 vertexHLSL += "};\n"
1348 "\n"
1349 "struct VS_OUTPUT\n"
1350 "{\n"
1351 " float4 gl_Position : POSITION;\n";
1352
1353 for (int r = 0; r < registers; r++)
1354 {
1355 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1356
1357 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1358 }
1359
1360 if (fragmentShader->mUsesFragCoord)
1361 {
1362 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1363 }
1364
1365 if (vertexShader->mUsesPointSize && sm3)
1366 {
1367 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1368 }
1369
1370 vertexHLSL += "};\n"
1371 "\n"
1372 "VS_OUTPUT main(VS_INPUT input)\n"
1373 "{\n";
1374
1375 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1376 {
1377 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1378
1379 if (VariableRowCount(attribute->type) > 1) // Matrix
1380 {
1381 vertexHLSL += "transpose";
1382 }
1383
1384 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1385 }
1386
1387 vertexHLSL += "\n"
1388 " gl_main();\n"
1389 "\n"
1390 " VS_OUTPUT output;\n"
1391 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001392 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001393 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1394 " output.gl_Position.w = gl_Position.w;\n";
1395
1396 if (vertexShader->mUsesPointSize && sm3)
1397 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001398 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001399 }
1400
1401 if (fragmentShader->mUsesFragCoord)
1402 {
1403 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1404 }
1405
1406 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1407 {
1408 if (varying->reg >= 0)
1409 {
1410 for (int i = 0; i < varying->size; i++)
1411 {
1412 int rows = VariableRowCount(varying->type);
1413
1414 for (int j = 0; j < rows; j++)
1415 {
1416 int r = varying->reg + i * rows + j;
1417 vertexHLSL += " output.v" + str(r);
1418
1419 bool sharedRegister = false; // Register used by multiple varyings
1420
1421 for (int x = 0; x < 4; x++)
1422 {
1423 if (packing[r][x] && packing[r][x] != packing[r][0])
1424 {
1425 sharedRegister = true;
1426 break;
1427 }
1428 }
1429
1430 if(sharedRegister)
1431 {
1432 vertexHLSL += ".";
1433
1434 for (int x = 0; x < 4; x++)
1435 {
1436 if (packing[r][x] == &*varying)
1437 {
1438 switch(x)
1439 {
1440 case 0: vertexHLSL += "x"; break;
1441 case 1: vertexHLSL += "y"; break;
1442 case 2: vertexHLSL += "z"; break;
1443 case 3: vertexHLSL += "w"; break;
1444 }
1445 }
1446 }
1447 }
1448
1449 vertexHLSL += " = " + varying->name;
1450
1451 if (varying->array)
1452 {
1453 vertexHLSL += "[" + str(i) + "]";
1454 }
1455
1456 if (rows > 1)
1457 {
1458 vertexHLSL += "[" + str(j) + "]";
1459 }
1460
1461 vertexHLSL += ";\n";
1462 }
1463 }
1464 }
1465 }
1466
1467 vertexHLSL += "\n"
1468 " return output;\n"
1469 "}\n";
1470
1471 pixelHLSL += "struct PS_INPUT\n"
1472 "{\n";
1473
1474 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1475 {
1476 if (varying->reg >= 0)
1477 {
1478 for (int i = 0; i < varying->size; i++)
1479 {
1480 int rows = VariableRowCount(varying->type);
1481 for (int j = 0; j < rows; j++)
1482 {
1483 std::string n = str(varying->reg + i * rows + j);
1484 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1485 }
1486 }
1487 }
1488 else UNREACHABLE();
1489 }
1490
1491 if (fragmentShader->mUsesFragCoord)
1492 {
1493 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1494 if (sm3) {
1495 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1496 }
1497 }
1498
1499 if (fragmentShader->mUsesPointCoord && sm3)
1500 {
1501 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1502 }
1503
1504 if (fragmentShader->mUsesFrontFacing)
1505 {
1506 pixelHLSL += " float vFace : VFACE;\n";
1507 }
1508
1509 pixelHLSL += "};\n"
1510 "\n"
1511 "struct PS_OUTPUT\n"
1512 "{\n"
1513 " float4 gl_Color[1] : COLOR;\n"
1514 "};\n"
1515 "\n"
1516 "PS_OUTPUT main(PS_INPUT input)\n"
1517 "{\n";
1518
1519 if (fragmentShader->mUsesFragCoord)
1520 {
1521 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1522
1523 if (sm3)
1524 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001525 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001526 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001527 }
1528 else
1529 {
1530 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1531 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001532 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001533 }
1534
1535 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_Depth.x + dx_Depth.y;\n"
1536 " gl_FragCoord.w = rhw;\n";
1537 }
1538
1539 if (fragmentShader->mUsesPointCoord && sm3)
1540 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001541 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1542 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001543 }
1544
1545 if (fragmentShader->mUsesFrontFacing)
1546 {
1547 pixelHLSL += " gl_FrontFacing = dx_PointsOrLines || (dx_FrontCCW ? (input.vFace >= 0.0) : (input.vFace <= 0.0));\n";
1548 }
1549
1550 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1551 {
1552 if (varying->reg >= 0)
1553 {
1554 for (int i = 0; i < varying->size; i++)
1555 {
1556 int rows = VariableRowCount(varying->type);
1557 for (int j = 0; j < rows; j++)
1558 {
1559 std::string n = str(varying->reg + i * rows + j);
1560 pixelHLSL += " " + varying->name;
1561
1562 if (varying->array)
1563 {
1564 pixelHLSL += "[" + str(i) + "]";
1565 }
1566
1567 if (rows > 1)
1568 {
1569 pixelHLSL += "[" + str(j) + "]";
1570 }
1571
1572 pixelHLSL += " = input.v" + n + ";\n";
1573 }
1574 }
1575 }
1576 else UNREACHABLE();
1577 }
1578
1579 pixelHLSL += "\n"
1580 " gl_main();\n"
1581 "\n"
1582 " PS_OUTPUT output;\n"
1583 " output.gl_Color[0] = gl_Color[0];\n"
1584 "\n"
1585 " return output;\n"
1586 "}\n";
1587
1588 return true;
1589}
1590
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001591bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1592{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001593 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001594
1595 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001596 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001597 if (format != GL_PROGRAM_BINARY_ANGLE)
1598 {
1599 infoLog.append("Invalid program binary format.");
1600 return false;
1601 }
1602
1603 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001604 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001605 if (version != BUILD_REVISION)
1606 {
1607 infoLog.append("Invalid program binary version.");
1608 return false;
1609 }
1610
1611 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1612 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001613 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001614 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001615 stream.read(&name);
1616 mLinkedAttribute[i].name = name;
1617 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001618 }
1619
1620 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1621 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001622 stream.read(&mSamplersPS[i].active);
1623 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001624
1625 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001626 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001627 mSamplersPS[i].textureType = (TextureType) textureType;
1628 }
1629
1630 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1631 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001632 stream.read(&mSamplersVS[i].active);
1633 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001634
1635 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001636 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001637 mSamplersVS[i].textureType = (TextureType) textureType;
1638 }
1639
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001640 stream.read(&mUsedVertexSamplerRange);
1641 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001642
1643 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001644 stream.read(&size);
1645 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001646 {
1647 infoLog.append("Invalid program binary.");
1648 return false;
1649 }
1650
1651 mUniforms.resize(size);
1652 for (unsigned int i = 0; i < size; ++i)
1653 {
1654 GLenum type;
1655 std::string _name;
1656 unsigned int arraySize;
1657
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001658 stream.read(&type);
1659 stream.read(&_name);
1660 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001661
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001662 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001663
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001664 stream.read(&mUniforms[i]->ps.float4Index);
1665 stream.read(&mUniforms[i]->ps.samplerIndex);
1666 stream.read(&mUniforms[i]->ps.boolIndex);
1667 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001668
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001669 stream.read(&mUniforms[i]->vs.float4Index);
1670 stream.read(&mUniforms[i]->vs.samplerIndex);
1671 stream.read(&mUniforms[i]->vs.boolIndex);
1672 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001673 }
1674
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001675 stream.read(&size);
1676 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001677 {
1678 infoLog.append("Invalid program binary.");
1679 return false;
1680 }
1681
1682 mUniformIndex.resize(size);
1683 for (unsigned int i = 0; i < size; ++i)
1684 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001685 stream.read(&mUniformIndex[i].name);
1686 stream.read(&mUniformIndex[i].element);
1687 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001688 }
1689
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001690 stream.read(&mDxDepthRangeLocation);
1691 stream.read(&mDxDepthLocation);
1692 stream.read(&mDxCoordLocation);
1693 stream.read(&mDxHalfPixelSizeLocation);
1694 stream.read(&mDxFrontCCWLocation);
1695 stream.read(&mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001696
1697 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001698 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001699
1700 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001701 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001702
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001703 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001704
1705 const D3DCAPS9 *binaryIdentifier = (const D3DCAPS9*) ptr;
1706 ptr += sizeof(GUID);
1707
1708 D3DADAPTER_IDENTIFIER9 *currentIdentifier = getDisplay()->getAdapterIdentifier();
1709 if (memcmp(&currentIdentifier->DeviceIdentifier, binaryIdentifier, sizeof(GUID)) != 0)
1710 {
1711 infoLog.append("Invalid program binary.");
1712 return false;
1713 }
1714
1715 const char *pixelShaderFunction = ptr;
1716 ptr += pixelShaderSize;
1717
1718 const char *vertexShaderFunction = ptr;
1719 ptr += vertexShaderSize;
1720
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001721 mPixelExecutable = getDisplay()->createPixelShader(reinterpret_cast<const DWORD*>(pixelShaderFunction), pixelShaderSize);
1722 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001723 {
1724 infoLog.append("Could not create pixel shader.");
1725 return false;
1726 }
1727
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001728 mVertexExecutable = getDisplay()->createVertexShader(reinterpret_cast<const DWORD*>(vertexShaderFunction), vertexShaderSize);
1729 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001730 {
1731 infoLog.append("Could not create vertex shader.");
1732 mPixelExecutable->Release();
1733 mPixelExecutable = NULL;
1734 return false;
1735 }
1736
1737 return true;
1738}
1739
1740bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1741{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001742 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001743
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001744 stream.write(GL_PROGRAM_BINARY_ANGLE);
1745 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001746
1747 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1748 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001749 stream.write(mLinkedAttribute[i].type);
1750 stream.write(mLinkedAttribute[i].name);
1751 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001752 }
1753
1754 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1755 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001756 stream.write(mSamplersPS[i].active);
1757 stream.write(mSamplersPS[i].logicalTextureUnit);
1758 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001759 }
1760
1761 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1762 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001763 stream.write(mSamplersVS[i].active);
1764 stream.write(mSamplersVS[i].logicalTextureUnit);
1765 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001766 }
1767
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001768 stream.write(mUsedVertexSamplerRange);
1769 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001770
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001771 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001772 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1773 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001774 stream.write(mUniforms[i]->type);
1775 stream.write(mUniforms[i]->_name);
1776 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001777
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001778 stream.write(mUniforms[i]->ps.float4Index);
1779 stream.write(mUniforms[i]->ps.samplerIndex);
1780 stream.write(mUniforms[i]->ps.boolIndex);
1781 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001782
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001783 stream.write(mUniforms[i]->vs.float4Index);
1784 stream.write(mUniforms[i]->vs.samplerIndex);
1785 stream.write(mUniforms[i]->vs.boolIndex);
1786 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001787 }
1788
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001789 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001790 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1791 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001792 stream.write(mUniformIndex[i].name);
1793 stream.write(mUniformIndex[i].element);
1794 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001795 }
1796
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001797 stream.write(mDxDepthRangeLocation);
1798 stream.write(mDxDepthLocation);
1799 stream.write(mDxCoordLocation);
1800 stream.write(mDxHalfPixelSizeLocation);
1801 stream.write(mDxFrontCCWLocation);
1802 stream.write(mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001803
1804 UINT pixelShaderSize;
1805 HRESULT result = mPixelExecutable->GetFunction(NULL, &pixelShaderSize);
1806 ASSERT(SUCCEEDED(result));
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001807 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001808
1809 UINT vertexShaderSize;
1810 result = mVertexExecutable->GetFunction(NULL, &vertexShaderSize);
1811 ASSERT(SUCCEEDED(result));
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001812 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001813
1814 D3DADAPTER_IDENTIFIER9 *identifier = getDisplay()->getAdapterIdentifier();
1815
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001816 GLsizei streamLength = stream.length();
1817 const void *streamData = stream.data();
1818
1819 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001820 if (totalLength > bufSize)
1821 {
1822 if (length)
1823 {
1824 *length = 0;
1825 }
1826
1827 return false;
1828 }
1829
1830 if (binary)
1831 {
1832 char *ptr = (char*) binary;
1833
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001834 memcpy(ptr, streamData, streamLength);
1835 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001836
1837 memcpy(ptr, &identifier->DeviceIdentifier, sizeof(GUID));
1838 ptr += sizeof(GUID);
1839
1840 result = mPixelExecutable->GetFunction(ptr, &pixelShaderSize);
1841 ASSERT(SUCCEEDED(result));
1842 ptr += pixelShaderSize;
1843
1844 result = mVertexExecutable->GetFunction(ptr, &vertexShaderSize);
1845 ASSERT(SUCCEEDED(result));
1846 ptr += vertexShaderSize;
1847
1848 ASSERT(ptr - totalLength == binary);
1849 }
1850
1851 if (length)
1852 {
1853 *length = totalLength;
1854 }
1855
1856 return true;
1857}
1858
1859GLint ProgramBinary::getLength()
1860{
1861 GLint length;
1862 if (save(NULL, INT_MAX, &length))
1863 {
1864 return length;
1865 }
1866 else
1867 {
1868 return 0;
1869 }
1870}
1871
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001872bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001873{
1874 if (!fragmentShader || !fragmentShader->isCompiled())
1875 {
1876 return false;
1877 }
1878
1879 if (!vertexShader || !vertexShader->isCompiled())
1880 {
1881 return false;
1882 }
1883
1884 std::string pixelHLSL = fragmentShader->getHLSL();
1885 std::string vertexHLSL = vertexShader->getHLSL();
1886
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001887 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001888 {
1889 return false;
1890 }
1891
1892 Context *context = getContext();
1893 const char *vertexProfile = context->supportsShaderModel3() ? "vs_3_0" : "vs_2_0";
1894 const char *pixelProfile = context->supportsShaderModel3() ? "ps_3_0" : "ps_2_0";
1895
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001896 ID3D10Blob *vertexBinary = compileToBinary(infoLog, vertexHLSL.c_str(), vertexProfile, &mConstantTableVS);
1897 ID3D10Blob *pixelBinary = compileToBinary(infoLog, pixelHLSL.c_str(), pixelProfile, &mConstantTablePS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001898
1899 if (vertexBinary && pixelBinary)
1900 {
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001901 mVertexExecutable = getDisplay()->createVertexShader((DWORD*)vertexBinary->GetBufferPointer(), vertexBinary->GetBufferSize());
1902 if (!mVertexExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001903 {
1904 return error(GL_OUT_OF_MEMORY, false);
1905 }
1906
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001907 mPixelExecutable = getDisplay()->createPixelShader((DWORD*)pixelBinary->GetBufferPointer(), pixelBinary->GetBufferSize());
1908 if (!mPixelExecutable)
1909 {
1910 mVertexExecutable->Release();
1911 mVertexExecutable = NULL;
1912 return error(GL_OUT_OF_MEMORY, false);
1913 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001914
1915 vertexBinary->Release();
1916 pixelBinary->Release();
1917 vertexBinary = NULL;
1918 pixelBinary = NULL;
1919
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001920 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001921 {
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001922 return false;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001923 }
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001924
1925 if (!linkUniforms(infoLog, GL_FRAGMENT_SHADER, mConstantTablePS))
1926 {
1927 return false;
1928 }
1929
1930 if (!linkUniforms(infoLog, GL_VERTEX_SHADER, mConstantTableVS))
1931 {
1932 return false;
1933 }
1934
1935 // these uniforms are searched as already-decorated because gl_ and dx_
1936 // are reserved prefixes, and do not receive additional decoration
1937 mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
1938 mDxDepthLocation = getUniformLocation("dx_Depth");
1939 mDxCoordLocation = getUniformLocation("dx_Coord");
1940 mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
1941 mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
1942 mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
1943
1944 context->markDxUniformsDirty();
1945
1946 return true;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001947 }
1948
1949 return false;
1950}
1951
1952// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001953bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001954{
1955 unsigned int usedLocations = 0;
1956
1957 // Link attributes that have a binding location
1958 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1959 {
1960 int location = attributeBindings.getAttributeBinding(attribute->name);
1961
1962 if (location != -1) // Set by glBindAttribLocation
1963 {
1964 if (!mLinkedAttribute[location].name.empty())
1965 {
1966 // Multiple active attributes bound to the same location; not an error
1967 }
1968
1969 mLinkedAttribute[location] = *attribute;
1970
1971 int rows = VariableRowCount(attribute->type);
1972
1973 if (rows + location > MAX_VERTEX_ATTRIBS)
1974 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001975 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 +00001976
1977 return false;
1978 }
1979
1980 for (int i = 0; i < rows; i++)
1981 {
1982 usedLocations |= 1 << (location + i);
1983 }
1984 }
1985 }
1986
1987 // Link attributes that don't have a binding location
1988 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1989 {
1990 int location = attributeBindings.getAttributeBinding(attribute->name);
1991
1992 if (location == -1) // Not set by glBindAttribLocation
1993 {
1994 int rows = VariableRowCount(attribute->type);
1995 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1996
1997 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1998 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001999 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002000
2001 return false; // Fail to link
2002 }
2003
2004 mLinkedAttribute[availableIndex] = *attribute;
2005 }
2006 }
2007
2008 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
2009 {
2010 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
2011 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
2012
2013 for (int r = 0; r < rows; r++)
2014 {
2015 mSemanticIndex[attributeIndex++] = index++;
2016 }
2017 }
2018
2019 return true;
2020}
2021
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002022bool ProgramBinary::linkUniforms(InfoLog &infoLog, GLenum shader, D3DConstantTable *constantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002023{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002024 for (unsigned int constantIndex = 0; constantIndex < constantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002025 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002026 const D3DConstant *constant = constantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002027
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002028 if (!defineUniform(infoLog, shader, constant))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002029 {
2030 return false;
2031 }
2032 }
2033
2034 return true;
2035}
2036
2037// Adds the description of a constant found in the binary shader to the list of uniforms
2038// Returns true if succesful (uniform not already defined)
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002039bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const D3DConstant *constant, std::string name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002040{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002041 if (constant->registerSet == D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002042 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002043 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002044 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002045 const D3DConstant *psConstant = mConstantTablePS->getConstantByName(constant->name.c_str());
2046 const D3DConstant *vsConstant = mConstantTableVS->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002047
2048 if (psConstant)
2049 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002050 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002051
2052 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2053 {
2054 mSamplersPS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002055 mSamplersPS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002056 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
2057 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
2058 }
2059 else
2060 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002061 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002062 return false;
2063 }
2064 }
2065
2066 if (vsConstant)
2067 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002068 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002069
2070 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
2071 {
2072 mSamplersVS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002073 mSamplersVS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002074 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2075 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2076 }
2077 else
2078 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002079 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002080 return false;
2081 }
2082 }
2083 }
2084 }
2085
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002086 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002087 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002088 case D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002089 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002090 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002091 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002092 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002093 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002094 const D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002095
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002096 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002097
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002098 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + "."))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002099 {
2100 return false;
2101 }
2102 }
2103 }
2104
2105 return true;
2106 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002107 case D3DConstant::CLASS_SCALAR:
2108 case D3DConstant::CLASS_VECTOR:
2109 case D3DConstant::CLASS_MATRIX_COLUMNS:
2110 case D3DConstant::CLASS_OBJECT:
2111 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002112 default:
2113 UNREACHABLE();
2114 return false;
2115 }
2116}
2117
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002118bool ProgramBinary::defineUniform(GLenum shader, const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002119{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002120 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002121
2122 if(!uniform)
2123 {
2124 return false;
2125 }
2126
2127 // Check if already defined
2128 GLint location = getUniformLocation(uniform->name);
2129 GLenum type = uniform->type;
2130
2131 if (location >= 0)
2132 {
2133 delete uniform;
2134 uniform = mUniforms[mUniformIndex[location].index];
2135 }
2136
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002137 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2138 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002139
2140 if (location >= 0)
2141 {
2142 return uniform->type == type;
2143 }
2144
2145 mUniforms.push_back(uniform);
2146 unsigned int uniformIndex = mUniforms.size() - 1;
2147
2148 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2149 {
2150 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2151 }
2152
2153 return true;
2154}
2155
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002156Uniform *ProgramBinary::createUniform(const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002157{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002158 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002159 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002160 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002161 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002162 case D3DConstant::PT_SAMPLER2D:
2163 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002164 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002165 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002166 default: UNREACHABLE();
2167 }
2168 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002169 case D3DConstant::PT_SAMPLERCUBE:
2170 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002171 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002172 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002173 default: UNREACHABLE();
2174 }
2175 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002176 case D3DConstant::PT_BOOL:
2177 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002178 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002179 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2180 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2181 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2182 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002183 default: UNREACHABLE();
2184 }
2185 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002186 case D3DConstant::PT_INT:
2187 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002188 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002189 case 1: return new Uniform(GL_INT, _name, constant->elements);
2190 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2191 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2192 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002193 default: UNREACHABLE();
2194 }
2195 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002196 case D3DConstant::PT_FLOAT:
2197 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002198 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002199 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2200 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2201 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2202 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002203 default: UNREACHABLE();
2204 }
2205 break;
2206 default:
2207 UNREACHABLE();
2208 }
2209 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002210 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002211 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002212 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002213 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002214 case D3DConstant::PT_FLOAT:
2215 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002216 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002217 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2218 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2219 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002220 default: UNREACHABLE();
2221 }
2222 break;
2223 default: UNREACHABLE();
2224 }
2225 }
2226 else UNREACHABLE();
2227
2228 return 0;
2229}
2230
2231// This method needs to match OutputHLSL::decorate
2232std::string ProgramBinary::decorateAttribute(const std::string &name)
2233{
2234 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2235 {
2236 return "_" + name;
2237 }
2238
2239 return name;
2240}
2241
2242std::string ProgramBinary::undecorateUniform(const std::string &_name)
2243{
2244 std::string name = _name;
2245
2246 // Remove any structure field decoration
2247 size_t pos = 0;
2248 while ((pos = name.find("._", pos)) != std::string::npos)
2249 {
2250 name.replace(pos, 2, ".");
2251 }
2252
2253 // Remove the leading decoration
2254 if (name[0] == '_')
2255 {
2256 return name.substr(1);
2257 }
2258 else if (name.compare(0, 3, "ar_") == 0)
2259 {
2260 return name.substr(3);
2261 }
2262
2263 return name;
2264}
2265
2266void ProgramBinary::applyUniformnbv(Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
2267{
2268 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2269 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2270
2271 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2272 {
2273 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2274 for (int i = 0; i < count; i++)
2275 {
2276 for (int j = 0; j < 4; j++)
2277 {
2278 if (j < width)
2279 {
2280 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2281 }
2282 else
2283 {
2284 vector[i * 4 + j] = 0.0f;
2285 }
2286 }
2287 }
2288 }
2289
2290 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2291 {
2292 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2293 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2294 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2295 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2296 for (int i = 0; i < copyCount; i++)
2297 {
2298 boolVector[i] = v[i] != GL_FALSE;
2299 }
2300 }
2301
2302 if (targetUniform->ps.float4Index >= 0)
2303 {
2304 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
2305 }
2306
2307 if (targetUniform->ps.boolIndex >= 0)
2308 {
2309 mDevice->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
2310 }
2311
2312 if (targetUniform->vs.float4Index >= 0)
2313 {
2314 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
2315 }
2316
2317 if (targetUniform->vs.boolIndex >= 0)
2318 {
2319 mDevice->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
2320 }
2321}
2322
2323bool ProgramBinary::applyUniformnfv(Uniform *targetUniform, const GLfloat *v)
2324{
2325 if (targetUniform->ps.registerCount)
2326 {
2327 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
2328 }
2329
2330 if (targetUniform->vs.registerCount)
2331 {
2332 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
2333 }
2334
2335 return true;
2336}
2337
2338bool ProgramBinary::applyUniform1iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2339{
2340 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002341 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002342
2343 for (int i = 0; i < count; i++)
2344 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002345 vector[i] = Vector4((float)v[i], 0, 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002346 }
2347
2348 if (targetUniform->ps.registerCount)
2349 {
2350 if (targetUniform->ps.samplerIndex >= 0)
2351 {
2352 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2353
2354 for (int i = 0; i < count; i++)
2355 {
2356 unsigned int samplerIndex = firstIndex + i;
2357
2358 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2359 {
2360 ASSERT(mSamplersPS[samplerIndex].active);
2361 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2362 }
2363 }
2364 }
2365 else
2366 {
2367 ASSERT(targetUniform->ps.float4Index >= 0);
2368 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
2369 }
2370 }
2371
2372 if (targetUniform->vs.registerCount)
2373 {
2374 if (targetUniform->vs.samplerIndex >= 0)
2375 {
2376 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2377
2378 for (int i = 0; i < count; i++)
2379 {
2380 unsigned int samplerIndex = firstIndex + i;
2381
2382 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2383 {
2384 ASSERT(mSamplersVS[samplerIndex].active);
2385 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2386 }
2387 }
2388 }
2389 else
2390 {
2391 ASSERT(targetUniform->vs.float4Index >= 0);
2392 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
2393 }
2394 }
2395
2396 return true;
2397}
2398
2399bool ProgramBinary::applyUniform2iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2400{
2401 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002402 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002403
2404 for (int i = 0; i < count; i++)
2405 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002406 vector[i] = Vector4((float)v[0], (float)v[1], 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002407
2408 v += 2;
2409 }
2410
2411 applyUniformniv(targetUniform, count, vector);
2412
2413 return true;
2414}
2415
2416bool ProgramBinary::applyUniform3iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2417{
2418 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002419 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002420
2421 for (int i = 0; i < count; i++)
2422 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002423 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002424
2425 v += 3;
2426 }
2427
2428 applyUniformniv(targetUniform, count, vector);
2429
2430 return true;
2431}
2432
2433bool ProgramBinary::applyUniform4iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2434{
2435 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002436 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002437
2438 for (int i = 0; i < count; i++)
2439 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002440 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002441
2442 v += 4;
2443 }
2444
2445 applyUniformniv(targetUniform, count, vector);
2446
2447 return true;
2448}
2449
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002450void ProgramBinary::applyUniformniv(Uniform *targetUniform, GLsizei count, const Vector4 *vector)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002451{
2452 if (targetUniform->ps.registerCount)
2453 {
2454 ASSERT(targetUniform->ps.float4Index >= 0);
2455 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
2456 }
2457
2458 if (targetUniform->vs.registerCount)
2459 {
2460 ASSERT(targetUniform->vs.float4Index >= 0);
2461 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
2462 }
2463}
2464
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002465bool ProgramBinary::isValidated() const
2466{
2467 return mValidated;
2468}
2469
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002470void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2471{
2472 // Skip over inactive attributes
2473 unsigned int activeAttribute = 0;
2474 unsigned int attribute;
2475 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2476 {
2477 if (mLinkedAttribute[attribute].name.empty())
2478 {
2479 continue;
2480 }
2481
2482 if (activeAttribute == index)
2483 {
2484 break;
2485 }
2486
2487 activeAttribute++;
2488 }
2489
2490 if (bufsize > 0)
2491 {
2492 const char *string = mLinkedAttribute[attribute].name.c_str();
2493
2494 strncpy(name, string, bufsize);
2495 name[bufsize - 1] = '\0';
2496
2497 if (length)
2498 {
2499 *length = strlen(name);
2500 }
2501 }
2502
2503 *size = 1; // Always a single 'type' instance
2504
2505 *type = mLinkedAttribute[attribute].type;
2506}
2507
2508GLint ProgramBinary::getActiveAttributeCount()
2509{
2510 int count = 0;
2511
2512 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2513 {
2514 if (!mLinkedAttribute[attributeIndex].name.empty())
2515 {
2516 count++;
2517 }
2518 }
2519
2520 return count;
2521}
2522
2523GLint ProgramBinary::getActiveAttributeMaxLength()
2524{
2525 int maxLength = 0;
2526
2527 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2528 {
2529 if (!mLinkedAttribute[attributeIndex].name.empty())
2530 {
2531 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2532 }
2533 }
2534
2535 return maxLength;
2536}
2537
2538void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2539{
2540 // Skip over internal uniforms
2541 unsigned int activeUniform = 0;
2542 unsigned int uniform;
2543 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2544 {
2545 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2546 {
2547 continue;
2548 }
2549
2550 if (activeUniform == index)
2551 {
2552 break;
2553 }
2554
2555 activeUniform++;
2556 }
2557
2558 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2559
2560 if (bufsize > 0)
2561 {
2562 std::string string = mUniforms[uniform]->name;
2563
2564 if (mUniforms[uniform]->isArray())
2565 {
2566 string += "[0]";
2567 }
2568
2569 strncpy(name, string.c_str(), bufsize);
2570 name[bufsize - 1] = '\0';
2571
2572 if (length)
2573 {
2574 *length = strlen(name);
2575 }
2576 }
2577
2578 *size = mUniforms[uniform]->arraySize;
2579
2580 *type = mUniforms[uniform]->type;
2581}
2582
2583GLint ProgramBinary::getActiveUniformCount()
2584{
2585 int count = 0;
2586
2587 unsigned int numUniforms = mUniforms.size();
2588 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2589 {
2590 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2591 {
2592 count++;
2593 }
2594 }
2595
2596 return count;
2597}
2598
2599GLint ProgramBinary::getActiveUniformMaxLength()
2600{
2601 int maxLength = 0;
2602
2603 unsigned int numUniforms = mUniforms.size();
2604 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2605 {
2606 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2607 {
2608 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2609 if (mUniforms[uniformIndex]->isArray())
2610 {
2611 length += 3; // Counting in "[0]".
2612 }
2613 maxLength = std::max(length, maxLength);
2614 }
2615 }
2616
2617 return maxLength;
2618}
2619
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002620void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002621{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002622 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002623 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002624 {
2625 mValidated = false;
2626 }
2627 else
2628 {
2629 mValidated = true;
2630 }
2631}
2632
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002633bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002634{
2635 // if any two active samplers in a program are of different types, but refer to the same
2636 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2637 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2638
2639 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2640 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2641
2642 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2643 {
2644 textureUnitType[i] = TEXTURE_UNKNOWN;
2645 }
2646
2647 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2648 {
2649 if (mSamplersPS[i].active)
2650 {
2651 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2652
2653 if (unit >= maxCombinedTextureImageUnits)
2654 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002655 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002656 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002657 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002658 }
2659
2660 return false;
2661 }
2662
2663 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2664 {
2665 if (mSamplersPS[i].textureType != textureUnitType[unit])
2666 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002667 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002668 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002669 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002670 }
2671
2672 return false;
2673 }
2674 }
2675 else
2676 {
2677 textureUnitType[unit] = mSamplersPS[i].textureType;
2678 }
2679 }
2680 }
2681
2682 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2683 {
2684 if (mSamplersVS[i].active)
2685 {
2686 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2687
2688 if (unit >= maxCombinedTextureImageUnits)
2689 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002690 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002691 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002692 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002693 }
2694
2695 return false;
2696 }
2697
2698 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2699 {
2700 if (mSamplersVS[i].textureType != textureUnitType[unit])
2701 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002702 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002703 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002704 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002705 }
2706
2707 return false;
2708 }
2709 }
2710 else
2711 {
2712 textureUnitType[unit] = mSamplersVS[i].textureType;
2713 }
2714 }
2715 }
2716
2717 return true;
2718}
2719
2720GLint ProgramBinary::getDxDepthRangeLocation() const
2721{
2722 return mDxDepthRangeLocation;
2723}
2724
2725GLint ProgramBinary::getDxDepthLocation() const
2726{
2727 return mDxDepthLocation;
2728}
2729
2730GLint ProgramBinary::getDxCoordLocation() const
2731{
2732 return mDxCoordLocation;
2733}
2734
2735GLint ProgramBinary::getDxHalfPixelSizeLocation() const
2736{
2737 return mDxHalfPixelSizeLocation;
2738}
2739
2740GLint ProgramBinary::getDxFrontCCWLocation() const
2741{
2742 return mDxFrontCCWLocation;
2743}
2744
2745GLint ProgramBinary::getDxPointsOrLinesLocation() const
2746{
2747 return mDxPointsOrLinesLocation;
2748}
2749
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002750ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2751{
2752}
2753
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002754}