1 #include "precompiled.h"
3 // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
8 // Program.cpp: Implements the gl::Program class. Implements GL program objects
9 // and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
11 #include "libGLESv2/BinaryStream.h"
12 #include "libGLESv2/ProgramBinary.h"
13 #include "libGLESv2/renderer/ShaderExecutable.h"
15 #include "common/debug.h"
16 #include "common/version.h"
17 #include "utilities.h"
19 #include "libGLESv2/main.h"
20 #include "libGLESv2/Shader.h"
21 #include "libGLESv2/Program.h"
22 #include "libGLESv2/renderer/Renderer.h"
23 #include "libGLESv2/renderer/VertexDataManager.h"
30 std::string str(int i)
33 snprintf(buffer, sizeof(buffer), "%d", i);
37 static rx::D3DWorkaroundType DiscardWorkaround(bool usesDiscard)
39 return (usesDiscard ? rx::ANGLE_D3D_WORKAROUND_SM3_OPTIMIZER : rx::ANGLE_D3D_WORKAROUND_NONE);
42 UniformLocation::UniformLocation(const std::string &name, unsigned int element, unsigned int index)
43 : name(name), element(element), index(index)
47 unsigned int ProgramBinary::mCurrentSerial = 1;
49 ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
51 mPixelExecutable = NULL;
52 mVertexExecutable = NULL;
53 mGeometryExecutable = NULL;
57 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
59 mSemanticIndex[index] = -1;
62 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
64 mSamplersPS[index].active = false;
67 for (int index = 0; index < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; index++)
69 mSamplersVS[index].active = false;
72 mUsedVertexSamplerRange = 0;
73 mUsedPixelSamplerRange = 0;
74 mUsesPointSize = false;
77 ProgramBinary::~ProgramBinary()
79 delete mPixelExecutable;
80 mPixelExecutable = NULL;
82 delete mVertexExecutable;
83 mVertexExecutable = NULL;
85 delete mGeometryExecutable;
86 mGeometryExecutable = NULL;
88 while (!mUniforms.empty())
90 delete mUniforms.back();
95 unsigned int ProgramBinary::getSerial() const
100 unsigned int ProgramBinary::issueSerial()
102 return mCurrentSerial++;
105 rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
107 return mPixelExecutable;
110 rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
112 return mVertexExecutable;
115 rx::ShaderExecutable *ProgramBinary::getGeometryExecutable()
117 return mGeometryExecutable;
120 GLuint ProgramBinary::getAttributeLocation(const char *name)
124 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
126 if (mLinkedAttribute[index].name == std::string(name))
136 int ProgramBinary::getSemanticIndex(int attributeIndex)
138 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
140 return mSemanticIndex[attributeIndex];
143 // Returns one more than the highest sampler index used.
144 GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
149 return mUsedPixelSamplerRange;
151 return mUsedVertexSamplerRange;
158 bool ProgramBinary::usesPointSize() const
160 return mUsesPointSize;
163 bool ProgramBinary::usesPointSpriteEmulation() const
165 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
168 bool ProgramBinary::usesGeometryShader() const
170 return usesPointSpriteEmulation();
173 // Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
174 // index (0-15 for the pixel shader and 0-3 for the vertex shader).
175 GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
177 GLint logicalTextureUnit = -1;
182 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
184 if (mSamplersPS[samplerIndex].active)
186 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
190 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
192 if (mSamplersVS[samplerIndex].active)
194 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
197 default: UNREACHABLE();
200 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)mRenderer->getMaxCombinedTextureImageUnits())
202 return logicalTextureUnit;
208 // Returns the texture type for a given Direct3D 9 sampler type and
209 // index (0-15 for the pixel shader and 0-3 for the vertex shader).
210 TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
215 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
216 ASSERT(mSamplersPS[samplerIndex].active);
217 return mSamplersPS[samplerIndex].textureType;
219 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
220 ASSERT(mSamplersVS[samplerIndex].active);
221 return mSamplersVS[samplerIndex].textureType;
222 default: UNREACHABLE();
228 GLint ProgramBinary::getUniformLocation(std::string name)
230 unsigned int subscript = 0;
232 // Strip any trailing array operator and retrieve the subscript
233 size_t open = name.find_last_of('[');
234 size_t close = name.find_last_of(']');
235 if (open != std::string::npos && close == name.length() - 1)
237 subscript = atoi(name.substr(open + 1).c_str());
241 unsigned int numUniforms = mUniformIndex.size();
242 for (unsigned int location = 0; location < numUniforms; location++)
244 if (mUniformIndex[location].name == name &&
245 mUniformIndex[location].element == subscript)
254 bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
256 if (location < 0 || location >= (int)mUniformIndex.size())
261 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
262 targetUniform->dirty = true;
264 int elementCount = targetUniform->elementCount();
266 if (elementCount == 1 && count > 1)
267 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
269 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
271 if (targetUniform->type == GL_FLOAT)
273 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
275 for (int i = 0; i < count; i++)
285 else if (targetUniform->type == GL_BOOL)
287 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
289 for (int i = 0; i < count; i++)
291 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE;
292 boolParams[1] = GL_FALSE;
293 boolParams[2] = GL_FALSE;
294 boolParams[3] = GL_FALSE;
307 bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
309 if (location < 0 || location >= (int)mUniformIndex.size())
314 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
315 targetUniform->dirty = true;
317 int elementCount = targetUniform->elementCount();
319 if (elementCount == 1 && count > 1)
320 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
322 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
324 if (targetUniform->type == GL_FLOAT_VEC2)
326 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
328 for (int i = 0; i < count; i++)
338 else if (targetUniform->type == GL_BOOL_VEC2)
340 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
342 for (int i = 0; i < count; i++)
344 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE;
345 boolParams[1] = (v[1] == 0.0f) ? GL_FALSE : GL_TRUE;
346 boolParams[2] = GL_FALSE;
347 boolParams[3] = GL_FALSE;
360 bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
362 if (location < 0 || location >= (int)mUniformIndex.size())
367 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
368 targetUniform->dirty = true;
370 int elementCount = targetUniform->elementCount();
372 if (elementCount == 1 && count > 1)
373 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
375 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
377 if (targetUniform->type == GL_FLOAT_VEC3)
379 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
381 for (int i = 0; i < count; i++)
391 else if (targetUniform->type == GL_BOOL_VEC3)
393 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
395 for (int i = 0; i < count; i++)
397 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE;
398 boolParams[1] = (v[1] == 0.0f) ? GL_FALSE : GL_TRUE;
399 boolParams[2] = (v[2] == 0.0f) ? GL_FALSE : GL_TRUE;
400 boolParams[3] = GL_FALSE;
413 bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
415 if (location < 0 || location >= (int)mUniformIndex.size())
420 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
421 targetUniform->dirty = true;
423 int elementCount = targetUniform->elementCount();
425 if (elementCount == 1 && count > 1)
426 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
428 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
430 if (targetUniform->type == GL_FLOAT_VEC4)
432 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
434 for (int i = 0; i < count; i++)
444 else if (targetUniform->type == GL_BOOL_VEC4)
446 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
448 for (int i = 0; i < count; i++)
450 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE;
451 boolParams[1] = (v[1] == 0.0f) ? GL_FALSE : GL_TRUE;
452 boolParams[2] = (v[2] == 0.0f) ? GL_FALSE : GL_TRUE;
453 boolParams[3] = (v[3] == 0.0f) ? GL_FALSE : GL_TRUE;
466 template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
467 void transposeMatrix(T *target, const GLfloat *value)
469 int copyWidth = std::min(targetWidth, srcWidth);
470 int copyHeight = std::min(targetHeight, srcHeight);
472 for (int x = 0; x < copyWidth; x++)
474 for (int y = 0; y < copyHeight; y++)
476 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
479 // clear unfilled right side
480 for (int y = 0; y < copyHeight; y++)
482 for (int x = srcWidth; x < targetWidth; x++)
484 target[y * targetWidth + x] = (T)0;
487 // clear unfilled bottom.
488 for (int y = srcHeight; y < targetHeight; y++)
490 for (int x = 0; x < targetWidth; x++)
492 target[y * targetWidth + x] = (T)0;
497 bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
499 if (location < 0 || location >= (int)mUniformIndex.size())
504 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
505 targetUniform->dirty = true;
507 if (targetUniform->type != GL_FLOAT_MAT2)
512 int elementCount = targetUniform->elementCount();
514 if (elementCount == 1 && count > 1)
515 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
517 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
518 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
520 for (int i = 0; i < count; i++)
522 transposeMatrix<GLfloat,4,2,2,2>(target, value);
530 bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
532 if (location < 0 || location >= (int)mUniformIndex.size())
537 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
538 targetUniform->dirty = true;
540 if (targetUniform->type != GL_FLOAT_MAT3)
545 int elementCount = targetUniform->elementCount();
547 if (elementCount == 1 && count > 1)
548 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
550 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
551 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
553 for (int i = 0; i < count; i++)
555 transposeMatrix<GLfloat,4,3,3,3>(target, value);
564 bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
566 if (location < 0 || location >= (int)mUniformIndex.size())
571 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
572 targetUniform->dirty = true;
574 if (targetUniform->type != GL_FLOAT_MAT4)
579 int elementCount = targetUniform->elementCount();
581 if (elementCount == 1 && count > 1)
582 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
584 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
585 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
587 for (int i = 0; i < count; i++)
589 transposeMatrix<GLfloat,4,4,4,4>(target, value);
597 bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
599 if (location < 0 || location >= (int)mUniformIndex.size())
604 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
605 targetUniform->dirty = true;
607 int elementCount = targetUniform->elementCount();
609 if (elementCount == 1 && count > 1)
610 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
612 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
614 if (targetUniform->type == GL_INT ||
615 targetUniform->type == GL_SAMPLER_2D ||
616 targetUniform->type == GL_SAMPLER_CUBE)
618 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
620 for (int i = 0; i < count; i++)
630 else if (targetUniform->type == GL_BOOL)
632 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
634 for (int i = 0; i < count; i++)
636 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
637 boolParams[1] = GL_FALSE;
638 boolParams[2] = GL_FALSE;
639 boolParams[3] = GL_FALSE;
652 bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
654 if (location < 0 || location >= (int)mUniformIndex.size())
659 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
660 targetUniform->dirty = true;
662 int elementCount = targetUniform->elementCount();
664 if (elementCount == 1 && count > 1)
665 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
667 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
669 if (targetUniform->type == GL_INT_VEC2)
671 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
673 for (int i = 0; i < count; i++)
683 else if (targetUniform->type == GL_BOOL_VEC2)
685 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
687 for (int i = 0; i < count; i++)
689 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
690 boolParams[1] = (v[1] == 0) ? GL_FALSE : GL_TRUE;
691 boolParams[2] = GL_FALSE;
692 boolParams[3] = GL_FALSE;
705 bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
707 if (location < 0 || location >= (int)mUniformIndex.size())
712 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
713 targetUniform->dirty = true;
715 int elementCount = targetUniform->elementCount();
717 if (elementCount == 1 && count > 1)
718 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
720 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
722 if (targetUniform->type == GL_INT_VEC3)
724 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
726 for (int i = 0; i < count; i++)
736 else if (targetUniform->type == GL_BOOL_VEC3)
738 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
740 for (int i = 0; i < count; i++)
742 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
743 boolParams[1] = (v[1] == 0) ? GL_FALSE : GL_TRUE;
744 boolParams[2] = (v[2] == 0) ? GL_FALSE : GL_TRUE;
745 boolParams[3] = GL_FALSE;
758 bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
760 if (location < 0 || location >= (int)mUniformIndex.size())
765 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
766 targetUniform->dirty = true;
768 int elementCount = targetUniform->elementCount();
770 if (elementCount == 1 && count > 1)
771 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
773 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
775 if (targetUniform->type == GL_INT_VEC4)
777 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
779 for (int i = 0; i < count; i++)
789 else if (targetUniform->type == GL_BOOL_VEC4)
791 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
793 for (int i = 0; i < count; i++)
795 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
796 boolParams[1] = (v[1] == 0) ? GL_FALSE : GL_TRUE;
797 boolParams[2] = (v[2] == 0) ? GL_FALSE : GL_TRUE;
798 boolParams[3] = (v[3] == 0) ? GL_FALSE : GL_TRUE;
811 bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
813 if (location < 0 || location >= (int)mUniformIndex.size())
818 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
820 // sized queries -- ensure the provided buffer is large enough
823 int requiredBytes = UniformExternalSize(targetUniform->type);
824 if (*bufSize < requiredBytes)
830 switch (targetUniform->type)
833 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
836 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
839 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
843 unsigned int size = UniformComponentCount(targetUniform->type);
845 switch (UniformComponentType(targetUniform->type))
849 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
851 for (unsigned int i = 0; i < size; i++)
853 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
858 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(GLfloat),
859 size * sizeof(GLfloat));
863 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
865 for (unsigned int i = 0; i < size; i++)
867 params[i] = (float)intParams[i];
871 default: UNREACHABLE();
879 bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
881 if (location < 0 || location >= (int)mUniformIndex.size())
886 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
888 // sized queries -- ensure the provided buffer is large enough
891 int requiredBytes = UniformExternalSize(targetUniform->type);
892 if (*bufSize < requiredBytes)
898 switch (targetUniform->type)
901 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
904 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
907 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
911 unsigned int size = VariableColumnCount(targetUniform->type);
913 switch (UniformComponentType(targetUniform->type))
917 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
919 for (unsigned int i = 0; i < size; i++)
921 params[i] = boolParams[i];
927 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
929 for (unsigned int i = 0; i < size; i++)
931 params[i] = (GLint)floatParams[i];
936 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(GLint),
937 size * sizeof(GLint));
939 default: UNREACHABLE();
947 void ProgramBinary::dirtyAllUniforms()
949 unsigned int numUniforms = mUniforms.size();
950 for (unsigned int index = 0; index < numUniforms; index++)
952 mUniforms[index]->dirty = true;
956 // Applies all the uniforms set for this program object to the renderer
957 void ProgramBinary::applyUniforms()
959 // Retrieve sampler uniform values
960 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
962 Uniform *targetUniform = *ub;
964 if (targetUniform->dirty)
966 if (targetUniform->type == GL_SAMPLER_2D ||
967 targetUniform->type == GL_SAMPLER_CUBE)
969 int count = targetUniform->elementCount();
970 GLint (*v)[4] = (GLint(*)[4])targetUniform->data;
972 if (targetUniform->psRegisterIndex >= 0)
974 unsigned int firstIndex = targetUniform->psRegisterIndex;
976 for (int i = 0; i < count; i++)
978 unsigned int samplerIndex = firstIndex + i;
980 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
982 ASSERT(mSamplersPS[samplerIndex].active);
983 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
988 if (targetUniform->vsRegisterIndex >= 0)
990 unsigned int firstIndex = targetUniform->vsRegisterIndex;
992 for (int i = 0; i < count; i++)
994 unsigned int samplerIndex = firstIndex + i;
996 if (samplerIndex < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS)
998 ASSERT(mSamplersVS[samplerIndex].active);
999 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
1007 mRenderer->applyUniforms(this, &mUniforms);
1010 // Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1011 // Returns the number of used varying registers, or -1 if unsuccesful
1012 int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
1014 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
1016 fragmentShader->resetVaryingsRegisterAssignment();
1018 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1020 int n = VariableRowCount(varying->type) * varying->size;
1021 int m = VariableColumnCount(varying->type);
1022 bool success = false;
1024 if (m == 2 || m == 3 || m == 4)
1026 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1028 bool available = true;
1030 for (int y = 0; y < n && available; y++)
1032 for (int x = 0; x < m && available; x++)
1034 if (packing[r + y][x])
1046 for (int y = 0; y < n; y++)
1048 for (int x = 0; x < m; x++)
1050 packing[r + y][x] = &*varying;
1058 if (!success && m == 2)
1060 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1062 bool available = true;
1064 for (int y = 0; y < n && available; y++)
1066 for (int x = 2; x < 4 && available; x++)
1068 if (packing[r + y][x])
1080 for (int y = 0; y < n; y++)
1082 for (int x = 2; x < 4; x++)
1084 packing[r + y][x] = &*varying;
1097 for (int y = 0; y < maxVaryingVectors; y++)
1099 for (int x = 0; x < 4; x++)
1101 space[x] += packing[y][x] ? 0 : 1;
1107 for (int x = 0; x < 4; x++)
1109 if (space[x] >= n && space[x] < space[column])
1115 if (space[column] >= n)
1117 for (int r = 0; r < maxVaryingVectors; r++)
1119 if (!packing[r][column])
1123 for (int y = r; y < r + n; y++)
1125 packing[y][column] = &*varying;
1132 varying->col = column;
1141 infoLog.append("Could not pack varying %s", varying->name.c_str());
1147 // Return the number of used registers
1150 for (int r = 0; r < maxVaryingVectors; r++)
1152 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1161 bool ProgramBinary::linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4],
1162 std::string& pixelHLSL, std::string& vertexHLSL,
1163 FragmentShader *fragmentShader, VertexShader *vertexShader)
1165 if (pixelHLSL.empty() || vertexHLSL.empty())
1170 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
1171 bool usesFragColor = fragmentShader->mUsesFragColor;
1172 bool usesFragData = fragmentShader->mUsesFragData;
1173 if (usesFragColor && usesFragData)
1175 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
1179 // Write the HLSL input/output declarations
1180 const int shaderModel = mRenderer->getMajorShaderModel();
1181 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
1183 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
1185 // The output color is broadcast to all enabled draw buffers when writing to gl_FragColor
1186 const bool broadcast = fragmentShader->mUsesFragColor;
1187 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
1189 if (registersNeeded > maxVaryingVectors)
1191 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
1196 vertexShader->resetVaryingsRegisterAssignment();
1198 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1200 bool matched = false;
1202 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1204 if (output->name == input->name)
1206 if (output->type != input->type || output->size != input->size)
1208 infoLog.append("Type of vertex varying %s does not match that of the fragment varying", output->name.c_str());
1213 output->reg = input->reg;
1214 output->col = input->col;
1223 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
1229 mUsesPointSize = vertexShader->mUsesPointSize;
1230 std::string varyingSemantic = (mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
1231 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
1232 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
1233 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
1235 // special varyings that use reserved registers
1236 int reservedRegisterIndex = registers;
1237 std::string fragCoordSemantic;
1238 std::string pointCoordSemantic;
1240 if (fragmentShader->mUsesFragCoord)
1242 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1245 if (fragmentShader->mUsesPointCoord)
1247 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
1248 // In DX11 we compute this in the GS.
1249 if (shaderModel == 3)
1251 pointCoordSemantic = "TEXCOORD0";
1253 else if (shaderModel >= 4)
1255 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1259 vertexHLSL += "struct VS_INPUT\n"
1262 int semanticIndex = 0;
1263 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1265 switch (attribute->type)
1267 case GL_FLOAT: vertexHLSL += " float "; break;
1268 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1269 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1270 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1271 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1272 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1273 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1274 default: UNREACHABLE();
1277 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1279 semanticIndex += VariableRowCount(attribute->type);
1282 vertexHLSL += "};\n"
1284 "struct VS_OUTPUT\n"
1287 if (shaderModel < 4)
1289 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1292 for (int r = 0; r < registers; r++)
1294 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1296 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1299 if (fragmentShader->mUsesFragCoord)
1301 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
1304 if (vertexShader->mUsesPointSize && shaderModel >= 3)
1306 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1309 if (shaderModel >= 4)
1311 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1314 vertexHLSL += "};\n"
1316 "VS_OUTPUT main(VS_INPUT input)\n"
1319 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1321 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1323 if (VariableRowCount(attribute->type) > 1) // Matrix
1325 vertexHLSL += "transpose";
1328 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1331 if (shaderModel >= 4)
1336 " VS_OUTPUT output;\n"
1337 " output.gl_Position.x = gl_Position.x;\n"
1338 " output.gl_Position.y = -gl_Position.y;\n"
1339 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1340 " output.gl_Position.w = gl_Position.w;\n";
1347 " VS_OUTPUT output;\n"
1348 " output.gl_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
1349 " output.gl_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
1350 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1351 " output.gl_Position.w = gl_Position.w;\n";
1354 if (vertexShader->mUsesPointSize && shaderModel >= 3)
1356 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
1359 if (fragmentShader->mUsesFragCoord)
1361 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1364 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1366 if (varying->reg >= 0)
1368 for (int i = 0; i < varying->size; i++)
1370 int rows = VariableRowCount(varying->type);
1372 for (int j = 0; j < rows; j++)
1374 int r = varying->reg + i * rows + j;
1375 vertexHLSL += " output.v" + str(r);
1377 bool sharedRegister = false; // Register used by multiple varyings
1379 for (int x = 0; x < 4; x++)
1381 if (packing[r][x] && packing[r][x] != packing[r][0])
1383 sharedRegister = true;
1392 for (int x = 0; x < 4; x++)
1394 if (packing[r][x] == &*varying)
1398 case 0: vertexHLSL += "x"; break;
1399 case 1: vertexHLSL += "y"; break;
1400 case 2: vertexHLSL += "z"; break;
1401 case 3: vertexHLSL += "w"; break;
1407 vertexHLSL += " = " + varying->name;
1411 vertexHLSL += "[" + str(i) + "]";
1416 vertexHLSL += "[" + str(j) + "]";
1419 vertexHLSL += ";\n";
1429 pixelHLSL += "struct PS_INPUT\n"
1432 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1434 if (varying->reg >= 0)
1436 for (int i = 0; i < varying->size; i++)
1438 int rows = VariableRowCount(varying->type);
1439 for (int j = 0; j < rows; j++)
1441 std::string n = str(varying->reg + i * rows + j);
1442 pixelHLSL += " float" + str(VariableColumnCount(varying->type)) + " v" + n + " : " + varyingSemantic + n + ";\n";
1449 if (fragmentShader->mUsesFragCoord)
1451 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
1454 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
1456 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
1459 // Must consume the PSIZE element if the geometry shader is not active
1460 // We won't know if we use a GS until we draw
1461 if (vertexShader->mUsesPointSize && shaderModel >= 4)
1463 pixelHLSL += " float gl_PointSize : PSIZE;\n";
1466 if (fragmentShader->mUsesFragCoord)
1468 if (shaderModel >= 4)
1470 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
1472 else if (shaderModel >= 3)
1474 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1480 "struct PS_OUTPUT\n"
1483 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
1485 pixelHLSL += " float4 gl_Color" + str(renderTargetIndex) + " : " + targetSemantic + str(renderTargetIndex) + ";\n";
1488 if (fragmentShader->mUsesFragDepth)
1490 pixelHLSL += " float gl_Depth : " + depthSemantic + ";\n";
1496 if (fragmentShader->mUsesFrontFacing)
1498 if (shaderModel >= 4)
1500 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
1505 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
1511 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
1515 if (fragmentShader->mUsesFragCoord)
1517 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1519 if (shaderModel >= 4)
1521 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
1522 " gl_FragCoord.y = input.dx_VPos.y;\n";
1524 else if (shaderModel >= 3)
1526 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
1527 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
1531 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
1532 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
1533 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
1536 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
1537 " gl_FragCoord.w = rhw;\n";
1540 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
1542 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1543 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
1546 if (fragmentShader->mUsesFrontFacing)
1548 if (shaderModel <= 3)
1550 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1554 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1558 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1560 if (varying->reg >= 0)
1562 for (int i = 0; i < varying->size; i++)
1564 int rows = VariableRowCount(varying->type);
1565 for (int j = 0; j < rows; j++)
1567 std::string n = str(varying->reg + i * rows + j);
1568 pixelHLSL += " " + varying->name;
1572 pixelHLSL += "[" + str(i) + "]";
1577 pixelHLSL += "[" + str(j) + "]";
1580 switch (VariableColumnCount(varying->type))
1582 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1583 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1584 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1585 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1586 default: UNREACHABLE();
1597 " PS_OUTPUT output;\n";
1599 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
1601 unsigned int sourceColorIndex = broadcast ? 0 : renderTargetIndex;
1603 pixelHLSL += " output.gl_Color" + str(renderTargetIndex) + " = gl_Color[" + str(sourceColorIndex) + "];\n";
1606 if (fragmentShader->mUsesFragDepth)
1608 pixelHLSL += " output.gl_Depth = gl_Depth;\n";
1618 bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1620 BinaryInputStream stream(binary, length);
1623 stream.read(&format);
1624 if (format != GL_PROGRAM_BINARY_ANGLE)
1626 infoLog.append("Invalid program binary format.");
1630 int majorVersion = 0;
1631 int minorVersion = 0;
1632 stream.read(&majorVersion);
1633 stream.read(&minorVersion);
1634 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
1636 infoLog.append("Invalid program binary version.");
1640 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
1641 stream.read(commitString, ANGLE_COMMIT_HASH_SIZE);
1642 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
1644 infoLog.append("Invalid program binary version.");
1648 int compileFlags = 0;
1649 stream.read(&compileFlags);
1650 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
1652 infoLog.append("Mismatched compilation flags.");
1656 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1658 stream.read(&mLinkedAttribute[i].type);
1661 mLinkedAttribute[i].name = name;
1662 stream.read(&mSemanticIndex[i]);
1665 initAttributesByLayout();
1667 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1669 stream.read(&mSamplersPS[i].active);
1670 stream.read(&mSamplersPS[i].logicalTextureUnit);
1673 stream.read(&textureType);
1674 mSamplersPS[i].textureType = (TextureType) textureType;
1677 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
1679 stream.read(&mSamplersVS[i].active);
1680 stream.read(&mSamplersVS[i].logicalTextureUnit);
1683 stream.read(&textureType);
1684 mSamplersVS[i].textureType = (TextureType) textureType;
1687 stream.read(&mUsedVertexSamplerRange);
1688 stream.read(&mUsedPixelSamplerRange);
1689 stream.read(&mUsesPointSize);
1695 infoLog.append("Invalid program binary.");
1699 mUniforms.resize(size);
1700 for (unsigned int i = 0; i < size; ++i)
1705 unsigned int arraySize;
1708 stream.read(&precision);
1710 stream.read(&arraySize);
1712 mUniforms[i] = new Uniform(type, precision, name, arraySize);
1714 stream.read(&mUniforms[i]->psRegisterIndex);
1715 stream.read(&mUniforms[i]->vsRegisterIndex);
1716 stream.read(&mUniforms[i]->registerCount);
1722 infoLog.append("Invalid program binary.");
1726 mUniformIndex.resize(size);
1727 for (unsigned int i = 0; i < size; ++i)
1729 stream.read(&mUniformIndex[i].name);
1730 stream.read(&mUniformIndex[i].element);
1731 stream.read(&mUniformIndex[i].index);
1734 unsigned int pixelShaderSize;
1735 stream.read(&pixelShaderSize);
1737 unsigned int vertexShaderSize;
1738 stream.read(&vertexShaderSize);
1740 unsigned int geometryShaderSize;
1741 stream.read(&geometryShaderSize);
1743 const char *ptr = (const char*) binary + stream.offset();
1745 const GUID *binaryIdentifier = (const GUID *) ptr;
1746 ptr += sizeof(GUID);
1748 GUID identifier = mRenderer->getAdapterIdentifier();
1749 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
1751 infoLog.append("Invalid program binary.");
1755 const char *pixelShaderFunction = ptr;
1756 ptr += pixelShaderSize;
1758 const char *vertexShaderFunction = ptr;
1759 ptr += vertexShaderSize;
1761 const char *geometryShaderFunction = geometryShaderSize > 0 ? ptr : NULL;
1762 ptr += geometryShaderSize;
1764 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1765 pixelShaderSize, rx::SHADER_PIXEL);
1766 if (!mPixelExecutable)
1768 infoLog.append("Could not create pixel shader.");
1772 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1773 vertexShaderSize, rx::SHADER_VERTEX);
1774 if (!mVertexExecutable)
1776 infoLog.append("Could not create vertex shader.");
1777 delete mPixelExecutable;
1778 mPixelExecutable = NULL;
1782 if (geometryShaderFunction != NULL && geometryShaderSize > 0)
1784 mGeometryExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(geometryShaderFunction),
1785 geometryShaderSize, rx::SHADER_GEOMETRY);
1786 if (!mGeometryExecutable)
1788 infoLog.append("Could not create geometry shader.");
1789 delete mPixelExecutable;
1790 mPixelExecutable = NULL;
1791 delete mVertexExecutable;
1792 mVertexExecutable = NULL;
1798 mGeometryExecutable = NULL;
1804 bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1806 BinaryOutputStream stream;
1808 stream.write(GL_PROGRAM_BINARY_ANGLE);
1809 stream.write(ANGLE_MAJOR_VERSION);
1810 stream.write(ANGLE_MINOR_VERSION);
1811 stream.write(ANGLE_COMMIT_HASH, ANGLE_COMMIT_HASH_SIZE);
1812 stream.write(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
1814 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1816 stream.write(mLinkedAttribute[i].type);
1817 stream.write(mLinkedAttribute[i].name);
1818 stream.write(mSemanticIndex[i]);
1821 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1823 stream.write(mSamplersPS[i].active);
1824 stream.write(mSamplersPS[i].logicalTextureUnit);
1825 stream.write((int) mSamplersPS[i].textureType);
1828 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
1830 stream.write(mSamplersVS[i].active);
1831 stream.write(mSamplersVS[i].logicalTextureUnit);
1832 stream.write((int) mSamplersVS[i].textureType);
1835 stream.write(mUsedVertexSamplerRange);
1836 stream.write(mUsedPixelSamplerRange);
1837 stream.write(mUsesPointSize);
1839 stream.write(mUniforms.size());
1840 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1842 stream.write(mUniforms[i]->type);
1843 stream.write(mUniforms[i]->precision);
1844 stream.write(mUniforms[i]->name);
1845 stream.write(mUniforms[i]->arraySize);
1847 stream.write(mUniforms[i]->psRegisterIndex);
1848 stream.write(mUniforms[i]->vsRegisterIndex);
1849 stream.write(mUniforms[i]->registerCount);
1852 stream.write(mUniformIndex.size());
1853 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1855 stream.write(mUniformIndex[i].name);
1856 stream.write(mUniformIndex[i].element);
1857 stream.write(mUniformIndex[i].index);
1860 UINT pixelShaderSize = mPixelExecutable->getLength();
1861 stream.write(pixelShaderSize);
1863 UINT vertexShaderSize = mVertexExecutable->getLength();
1864 stream.write(vertexShaderSize);
1866 UINT geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
1867 stream.write(geometryShaderSize);
1869 GUID identifier = mRenderer->getAdapterIdentifier();
1871 GLsizei streamLength = stream.length();
1872 const void *streamData = stream.data();
1874 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize + geometryShaderSize;
1875 if (totalLength > bufSize)
1887 char *ptr = (char*) binary;
1889 memcpy(ptr, streamData, streamLength);
1890 ptr += streamLength;
1892 memcpy(ptr, &identifier, sizeof(GUID));
1893 ptr += sizeof(GUID);
1895 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
1896 ptr += pixelShaderSize;
1898 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
1899 ptr += vertexShaderSize;
1901 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
1903 memcpy(ptr, mGeometryExecutable->getFunction(), geometryShaderSize);
1904 ptr += geometryShaderSize;
1907 ASSERT(ptr - totalLength == binary);
1912 *length = totalLength;
1918 GLint ProgramBinary::getLength()
1921 if (save(NULL, INT_MAX, &length))
1931 bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
1933 if (!fragmentShader || !fragmentShader->isCompiled())
1938 if (!vertexShader || !vertexShader->isCompiled())
1943 std::string pixelHLSL = fragmentShader->getHLSL();
1944 std::string vertexHLSL = vertexShader->getHLSL();
1946 // Map the varyings to the register file
1947 const Varying *packing[IMPLEMENTATION_MAX_VARYING_VECTORS][4] = {NULL};
1948 int registers = packVaryings(infoLog, packing, fragmentShader);
1955 if (!linkVaryings(infoLog, registers, packing, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
1960 bool success = true;
1962 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1967 if (!linkUniforms(infoLog, vertexShader->getUniforms(), fragmentShader->getUniforms()))
1972 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1973 if (vertexShader->mUsesDepthRange || fragmentShader->mUsesDepthRange)
1975 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0));
1976 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0));
1977 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0));
1982 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), rx::SHADER_VERTEX, DiscardWorkaround(vertexShader->mUsesDiscardRewriting));
1983 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), rx::SHADER_PIXEL, DiscardWorkaround(fragmentShader->mUsesDiscardRewriting));
1985 if (usesGeometryShader())
1987 std::string geometryHLSL = generateGeometryShaderHLSL(registers, packing, fragmentShader, vertexShader);
1988 mGeometryExecutable = mRenderer->compileToExecutable(infoLog, geometryHLSL.c_str(), rx::SHADER_GEOMETRY, rx::ANGLE_D3D_WORKAROUND_NONE);
1991 if (!mVertexExecutable || !mPixelExecutable || (usesGeometryShader() && !mGeometryExecutable))
1993 infoLog.append("Failed to create D3D shaders.");
1996 delete mVertexExecutable;
1997 mVertexExecutable = NULL;
1998 delete mPixelExecutable;
1999 mPixelExecutable = NULL;
2000 delete mGeometryExecutable;
2001 mGeometryExecutable = NULL;
2008 // Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
2009 bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
2011 unsigned int usedLocations = 0;
2013 // Link attributes that have a binding location
2014 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
2016 int location = attributeBindings.getAttributeBinding(attribute->name);
2018 if (location != -1) // Set by glBindAttribLocation
2020 if (!mLinkedAttribute[location].name.empty())
2022 // Multiple active attributes bound to the same location; not an error
2025 mLinkedAttribute[location] = *attribute;
2027 int rows = VariableRowCount(attribute->type);
2029 if (rows + location > MAX_VERTEX_ATTRIBS)
2031 infoLog.append("Active attribute (%s) at location %d is too big to fit", attribute->name.c_str(), location);
2036 for (int i = 0; i < rows; i++)
2038 usedLocations |= 1 << (location + i);
2043 // Link attributes that don't have a binding location
2044 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
2046 int location = attributeBindings.getAttributeBinding(attribute->name);
2048 if (location == -1) // Not set by glBindAttribLocation
2050 int rows = VariableRowCount(attribute->type);
2051 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
2053 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
2055 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
2057 return false; // Fail to link
2060 mLinkedAttribute[availableIndex] = *attribute;
2064 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
2066 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
2067 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
2069 for (int r = 0; r < rows; r++)
2071 mSemanticIndex[attributeIndex++] = index++;
2075 initAttributesByLayout();
2080 bool ProgramBinary::linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms)
2082 for (sh::ActiveUniforms::const_iterator uniform = vertexUniforms.begin(); uniform != vertexUniforms.end(); uniform++)
2084 if (!defineUniform(GL_VERTEX_SHADER, *uniform, infoLog))
2090 for (sh::ActiveUniforms::const_iterator uniform = fragmentUniforms.begin(); uniform != fragmentUniforms.end(); uniform++)
2092 if (!defineUniform(GL_FRAGMENT_SHADER, *uniform, infoLog))
2101 bool ProgramBinary::defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog)
2103 if (constant.type == GL_SAMPLER_2D ||
2104 constant.type == GL_SAMPLER_CUBE)
2106 unsigned int samplerIndex = constant.registerIndex;
2110 if (shader == GL_VERTEX_SHADER)
2112 if (samplerIndex < mRenderer->getMaxVertexTextureImageUnits())
2114 mSamplersVS[samplerIndex].active = true;
2115 mSamplersVS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2116 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2117 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2121 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).", mRenderer->getMaxVertexTextureImageUnits());
2125 else if (shader == GL_FRAGMENT_SHADER)
2127 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2129 mSamplersPS[samplerIndex].active = true;
2130 mSamplersPS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2131 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
2132 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
2136 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
2144 while (samplerIndex < constant.registerIndex + constant.arraySize);
2147 Uniform *uniform = NULL;
2148 GLint location = getUniformLocation(constant.name);
2150 if (location >= 0) // Previously defined, type and precision must match
2152 uniform = mUniforms[mUniformIndex[location].index];
2154 if (uniform->type != constant.type)
2156 infoLog.append("Types for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str());
2160 if (uniform->precision != constant.precision)
2162 infoLog.append("Precisions for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str());
2168 uniform = new Uniform(constant.type, constant.precision, constant.name, constant.arraySize);
2176 if (shader == GL_FRAGMENT_SHADER)
2178 uniform->psRegisterIndex = constant.registerIndex;
2180 else if (shader == GL_VERTEX_SHADER)
2182 uniform->vsRegisterIndex = constant.registerIndex;
2188 return uniform->type == constant.type;
2191 mUniforms.push_back(uniform);
2192 unsigned int uniformIndex = mUniforms.size() - 1;
2194 for (unsigned int i = 0; i < uniform->elementCount(); i++)
2196 mUniformIndex.push_back(UniformLocation(constant.name, i, uniformIndex));
2199 if (shader == GL_VERTEX_SHADER)
2201 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedVertexUniformVectors() + mRenderer->getMaxVertexUniformVectors())
2203 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)", mRenderer->getMaxVertexUniformVectors());
2207 else if (shader == GL_FRAGMENT_SHADER)
2209 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedFragmentUniformVectors() + mRenderer->getMaxFragmentUniformVectors())
2211 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)", mRenderer->getMaxFragmentUniformVectors());
2220 std::string ProgramBinary::generateGeometryShaderHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
2222 // for now we only handle point sprite emulation
2223 ASSERT(usesPointSpriteEmulation());
2224 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
2227 std::string ProgramBinary::generatePointSpriteHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
2229 ASSERT(registers >= 0);
2230 ASSERT(vertexShader->mUsesPointSize);
2231 ASSERT(mRenderer->getMajorShaderModel() >= 4);
2233 std::string geomHLSL;
2235 std::string varyingSemantic = "TEXCOORD";
2237 std::string fragCoordSemantic;
2238 std::string pointCoordSemantic;
2240 int reservedRegisterIndex = registers;
2242 if (fragmentShader->mUsesFragCoord)
2244 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
2247 if (fragmentShader->mUsesPointCoord)
2249 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
2252 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
2257 for (int r = 0; r < registers; r++)
2259 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
2261 geomHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
2264 if (fragmentShader->mUsesFragCoord)
2266 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
2269 geomHLSL += " float gl_PointSize : PSIZE;\n"
2270 " float4 gl_Position : SV_Position;\n"
2273 "struct GS_OUTPUT\n"
2276 for (int r = 0; r < registers; r++)
2278 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
2280 geomHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
2283 if (fragmentShader->mUsesFragCoord)
2285 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
2288 if (fragmentShader->mUsesPointCoord)
2290 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
2293 geomHLSL += " float gl_PointSize : PSIZE;\n"
2294 " float4 gl_Position : SV_Position;\n"
2297 "static float2 pointSpriteCorners[] = \n"
2299 " float2( 0.5f, -0.5f),\n"
2300 " float2( 0.5f, 0.5f),\n"
2301 " float2(-0.5f, -0.5f),\n"
2302 " float2(-0.5f, 0.5f)\n"
2305 "static float2 pointSpriteTexcoords[] = \n"
2307 " float2(1.0f, 1.0f),\n"
2308 " float2(1.0f, 0.0f),\n"
2309 " float2(0.0f, 1.0f),\n"
2310 " float2(0.0f, 0.0f)\n"
2313 "static float minPointSize = " + str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
2314 "static float maxPointSize = " + str(mRenderer->getMaxPointSize()) + ".0f;\n"
2316 "[maxvertexcount(4)]\n"
2317 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
2319 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
2320 " output.gl_PointSize = input[0].gl_PointSize;\n";
2322 for (int r = 0; r < registers; r++)
2324 geomHLSL += " output.v" + str(r) + " = input[0].v" + str(r) + ";\n";
2327 if (fragmentShader->mUsesFragCoord)
2329 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
2333 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
2334 " float4 gl_Position = input[0].gl_Position;\n"
2335 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n";
2337 for (int corner = 0; corner < 4; corner++)
2340 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
2342 if (fragmentShader->mUsesPointCoord)
2344 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + str(corner) + "];\n";
2347 geomHLSL += " outStream.Append(output);\n";
2351 " outStream.RestartStrip();\n"
2357 // This method needs to match OutputHLSL::decorate
2358 std::string ProgramBinary::decorateAttribute(const std::string &name)
2360 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2368 bool ProgramBinary::isValidated() const
2373 void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
2375 // Skip over inactive attributes
2376 unsigned int activeAttribute = 0;
2377 unsigned int attribute;
2378 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2380 if (mLinkedAttribute[attribute].name.empty())
2385 if (activeAttribute == index)
2395 const char *string = mLinkedAttribute[attribute].name.c_str();
2397 strncpy(name, string, bufsize);
2398 name[bufsize - 1] = '\0';
2402 *length = strlen(name);
2406 *size = 1; // Always a single 'type' instance
2408 *type = mLinkedAttribute[attribute].type;
2411 GLint ProgramBinary::getActiveAttributeCount() const
2415 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2417 if (!mLinkedAttribute[attributeIndex].name.empty())
2426 GLint ProgramBinary::getActiveAttributeMaxLength() const
2430 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2432 if (!mLinkedAttribute[attributeIndex].name.empty())
2434 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2441 void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
2443 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2447 std::string string = mUniforms[index]->name;
2449 if (mUniforms[index]->isArray())
2454 strncpy(name, string.c_str(), bufsize);
2455 name[bufsize - 1] = '\0';
2459 *length = strlen(name);
2463 *size = mUniforms[index]->elementCount();
2465 *type = mUniforms[index]->type;
2468 GLint ProgramBinary::getActiveUniformCount() const
2470 return mUniforms.size();
2473 GLint ProgramBinary::getActiveUniformMaxLength() const
2477 unsigned int numUniforms = mUniforms.size();
2478 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2480 if (!mUniforms[uniformIndex]->name.empty())
2482 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2483 if (mUniforms[uniformIndex]->isArray())
2485 length += 3; // Counting in "[0]".
2487 maxLength = std::max(length, maxLength);
2494 void ProgramBinary::validate(InfoLog &infoLog)
2497 if (!validateSamplers(&infoLog))
2507 bool ProgramBinary::validateSamplers(InfoLog *infoLog)
2509 // if any two active samplers in a program are of different types, but refer to the same
2510 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2511 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2513 const unsigned int maxCombinedTextureImageUnits = mRenderer->getMaxCombinedTextureImageUnits();
2514 TextureType textureUnitType[IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS];
2516 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i)
2518 textureUnitType[i] = TEXTURE_UNKNOWN;
2521 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2523 if (mSamplersPS[i].active)
2525 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2527 if (unit >= maxCombinedTextureImageUnits)
2531 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
2537 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2539 if (mSamplersPS[i].textureType != textureUnitType[unit])
2543 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
2551 textureUnitType[unit] = mSamplersPS[i].textureType;
2556 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2558 if (mSamplersVS[i].active)
2560 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2562 if (unit >= maxCombinedTextureImageUnits)
2566 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
2572 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2574 if (mSamplersVS[i].textureType != textureUnitType[unit])
2578 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
2586 textureUnitType[unit] = mSamplersVS[i].textureType;
2594 ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2598 struct AttributeSorter
2600 AttributeSorter(const int (&semanticIndices)[MAX_VERTEX_ATTRIBS])
2601 : originalIndices(semanticIndices)
2605 bool operator()(int a, int b)
2607 if (originalIndices[a] == -1) return false;
2608 if (originalIndices[b] == -1) return true;
2609 return (originalIndices[a] < originalIndices[b]);
2612 const int (&originalIndices)[MAX_VERTEX_ATTRIBS];
2615 void ProgramBinary::initAttributesByLayout()
2617 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2619 mAttributesByLayout[i] = i;
2622 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2625 void ProgramBinary::sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const
2627 rx::TranslatedAttribute oldTranslatedAttributes[MAX_VERTEX_ATTRIBS];
2629 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2631 oldTranslatedAttributes[i] = attributes[i];
2634 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2636 int oldIndex = mAttributesByLayout[i];
2637 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2638 attributes[i] = oldTranslatedAttributes[oldIndex];