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, bool nestedBreak)
42 // Work-around a D3D9 compiler bug that presents itself when using conditional discard, by disabling optimization
43 return rx::ANGLE_D3D_WORKAROUND_SKIP_OPTIMIZATION;
49 // Work-around a D3D9 compiler bug that presents itself when using break in a nested loop, by maximizing optimization
50 // We want to keep the use of ANGLE_D3D_WORKAROUND_MAX_OPTIMIZATION minimal to prevent hangs, so usesDiscard takes precedence
51 return rx::ANGLE_D3D_WORKAROUND_MAX_OPTIMIZATION;
54 return rx::ANGLE_D3D_WORKAROUND_NONE;
57 UniformLocation::UniformLocation(const std::string &name, unsigned int element, unsigned int index)
58 : name(name), element(element), index(index)
62 unsigned int ProgramBinary::mCurrentSerial = 1;
64 ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
66 mPixelExecutable = NULL;
67 mVertexExecutable = NULL;
68 mGeometryExecutable = NULL;
72 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
74 mSemanticIndex[index] = -1;
77 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
79 mSamplersPS[index].active = false;
82 for (int index = 0; index < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; index++)
84 mSamplersVS[index].active = false;
87 mUsedVertexSamplerRange = 0;
88 mUsedPixelSamplerRange = 0;
89 mUsesPointSize = false;
92 ProgramBinary::~ProgramBinary()
94 delete mPixelExecutable;
95 mPixelExecutable = NULL;
97 delete mVertexExecutable;
98 mVertexExecutable = NULL;
100 delete mGeometryExecutable;
101 mGeometryExecutable = NULL;
103 while (!mUniforms.empty())
105 delete mUniforms.back();
106 mUniforms.pop_back();
110 unsigned int ProgramBinary::getSerial() const
115 unsigned int ProgramBinary::issueSerial()
117 return mCurrentSerial++;
120 rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
122 return mPixelExecutable;
125 rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
127 return mVertexExecutable;
130 rx::ShaderExecutable *ProgramBinary::getGeometryExecutable()
132 return mGeometryExecutable;
135 GLuint ProgramBinary::getAttributeLocation(const char *name)
139 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
141 if (mLinkedAttribute[index].name == std::string(name))
151 int ProgramBinary::getSemanticIndex(int attributeIndex)
153 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
155 return mSemanticIndex[attributeIndex];
158 // Returns one more than the highest sampler index used.
159 GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
164 return mUsedPixelSamplerRange;
166 return mUsedVertexSamplerRange;
173 bool ProgramBinary::usesPointSize() const
175 return mUsesPointSize;
178 bool ProgramBinary::usesPointSpriteEmulation() const
180 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
183 bool ProgramBinary::usesGeometryShader() const
185 return usesPointSpriteEmulation();
188 // Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
189 // index (0-15 for the pixel shader and 0-3 for the vertex shader).
190 GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
192 GLint logicalTextureUnit = -1;
197 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
199 if (mSamplersPS[samplerIndex].active)
201 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
205 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
207 if (mSamplersVS[samplerIndex].active)
209 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
212 default: UNREACHABLE();
215 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)mRenderer->getMaxCombinedTextureImageUnits())
217 return logicalTextureUnit;
223 // Returns the texture type for a given Direct3D 9 sampler type and
224 // index (0-15 for the pixel shader and 0-3 for the vertex shader).
225 TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
230 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
231 ASSERT(mSamplersPS[samplerIndex].active);
232 return mSamplersPS[samplerIndex].textureType;
234 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
235 ASSERT(mSamplersVS[samplerIndex].active);
236 return mSamplersVS[samplerIndex].textureType;
237 default: UNREACHABLE();
243 GLint ProgramBinary::getUniformLocation(std::string name)
245 unsigned int subscript = 0;
247 // Strip any trailing array operator and retrieve the subscript
248 size_t open = name.find_last_of('[');
249 size_t close = name.find_last_of(']');
250 if (open != std::string::npos && close == name.length() - 1)
252 subscript = atoi(name.substr(open + 1).c_str());
256 unsigned int numUniforms = mUniformIndex.size();
257 for (unsigned int location = 0; location < numUniforms; location++)
259 if (mUniformIndex[location].name == name &&
260 mUniformIndex[location].element == subscript)
269 bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
271 if (location < 0 || location >= (int)mUniformIndex.size())
276 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
277 targetUniform->dirty = true;
279 int elementCount = targetUniform->elementCount();
281 if (elementCount == 1 && count > 1)
282 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
284 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
286 if (targetUniform->type == GL_FLOAT)
288 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
290 for (int i = 0; i < count; i++)
300 else if (targetUniform->type == GL_BOOL)
302 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
304 for (int i = 0; i < count; i++)
306 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE;
307 boolParams[1] = GL_FALSE;
308 boolParams[2] = GL_FALSE;
309 boolParams[3] = GL_FALSE;
322 bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
324 if (location < 0 || location >= (int)mUniformIndex.size())
329 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
330 targetUniform->dirty = true;
332 int elementCount = targetUniform->elementCount();
334 if (elementCount == 1 && count > 1)
335 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
337 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
339 if (targetUniform->type == GL_FLOAT_VEC2)
341 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
343 for (int i = 0; i < count; i++)
353 else if (targetUniform->type == GL_BOOL_VEC2)
355 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
357 for (int i = 0; i < count; i++)
359 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE;
360 boolParams[1] = (v[1] == 0.0f) ? GL_FALSE : GL_TRUE;
361 boolParams[2] = GL_FALSE;
362 boolParams[3] = GL_FALSE;
375 bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
377 if (location < 0 || location >= (int)mUniformIndex.size())
382 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
383 targetUniform->dirty = true;
385 int elementCount = targetUniform->elementCount();
387 if (elementCount == 1 && count > 1)
388 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
390 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
392 if (targetUniform->type == GL_FLOAT_VEC3)
394 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
396 for (int i = 0; i < count; i++)
406 else if (targetUniform->type == GL_BOOL_VEC3)
408 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
410 for (int i = 0; i < count; i++)
412 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE;
413 boolParams[1] = (v[1] == 0.0f) ? GL_FALSE : GL_TRUE;
414 boolParams[2] = (v[2] == 0.0f) ? GL_FALSE : GL_TRUE;
415 boolParams[3] = GL_FALSE;
428 bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
430 if (location < 0 || location >= (int)mUniformIndex.size())
435 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
436 targetUniform->dirty = true;
438 int elementCount = targetUniform->elementCount();
440 if (elementCount == 1 && count > 1)
441 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
443 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
445 if (targetUniform->type == GL_FLOAT_VEC4)
447 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
449 for (int i = 0; i < count; i++)
459 else if (targetUniform->type == GL_BOOL_VEC4)
461 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
463 for (int i = 0; i < count; i++)
465 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE;
466 boolParams[1] = (v[1] == 0.0f) ? GL_FALSE : GL_TRUE;
467 boolParams[2] = (v[2] == 0.0f) ? GL_FALSE : GL_TRUE;
468 boolParams[3] = (v[3] == 0.0f) ? GL_FALSE : GL_TRUE;
481 template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
482 void transposeMatrix(T *target, const GLfloat *value)
484 int copyWidth = std::min(targetWidth, srcWidth);
485 int copyHeight = std::min(targetHeight, srcHeight);
487 for (int x = 0; x < copyWidth; x++)
489 for (int y = 0; y < copyHeight; y++)
491 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
494 // clear unfilled right side
495 for (int y = 0; y < copyHeight; y++)
497 for (int x = srcWidth; x < targetWidth; x++)
499 target[y * targetWidth + x] = (T)0;
502 // clear unfilled bottom.
503 for (int y = srcHeight; y < targetHeight; y++)
505 for (int x = 0; x < targetWidth; x++)
507 target[y * targetWidth + x] = (T)0;
512 bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
514 if (location < 0 || location >= (int)mUniformIndex.size())
519 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
520 targetUniform->dirty = true;
522 if (targetUniform->type != GL_FLOAT_MAT2)
527 int elementCount = targetUniform->elementCount();
529 if (elementCount == 1 && count > 1)
530 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
532 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
533 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
535 for (int i = 0; i < count; i++)
537 transposeMatrix<GLfloat,4,2,2,2>(target, value);
545 bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
547 if (location < 0 || location >= (int)mUniformIndex.size())
552 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
553 targetUniform->dirty = true;
555 if (targetUniform->type != GL_FLOAT_MAT3)
560 int elementCount = targetUniform->elementCount();
562 if (elementCount == 1 && count > 1)
563 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
565 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
566 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
568 for (int i = 0; i < count; i++)
570 transposeMatrix<GLfloat,4,3,3,3>(target, value);
579 bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
581 if (location < 0 || location >= (int)mUniformIndex.size())
586 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
587 targetUniform->dirty = true;
589 if (targetUniform->type != GL_FLOAT_MAT4)
594 int elementCount = targetUniform->elementCount();
596 if (elementCount == 1 && count > 1)
597 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
599 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
600 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
602 for (int i = 0; i < count; i++)
604 transposeMatrix<GLfloat,4,4,4,4>(target, value);
612 bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
614 if (location < 0 || location >= (int)mUniformIndex.size())
619 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
620 targetUniform->dirty = true;
622 int elementCount = targetUniform->elementCount();
624 if (elementCount == 1 && count > 1)
625 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
627 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
629 if (targetUniform->type == GL_INT ||
630 targetUniform->type == GL_SAMPLER_2D ||
631 targetUniform->type == GL_SAMPLER_CUBE)
633 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
635 for (int i = 0; i < count; i++)
645 else if (targetUniform->type == GL_BOOL)
647 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
649 for (int i = 0; i < count; i++)
651 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
652 boolParams[1] = GL_FALSE;
653 boolParams[2] = GL_FALSE;
654 boolParams[3] = GL_FALSE;
667 bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
669 if (location < 0 || location >= (int)mUniformIndex.size())
674 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
675 targetUniform->dirty = true;
677 int elementCount = targetUniform->elementCount();
679 if (elementCount == 1 && count > 1)
680 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
682 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
684 if (targetUniform->type == GL_INT_VEC2)
686 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
688 for (int i = 0; i < count; i++)
698 else if (targetUniform->type == GL_BOOL_VEC2)
700 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
702 for (int i = 0; i < count; i++)
704 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
705 boolParams[1] = (v[1] == 0) ? GL_FALSE : GL_TRUE;
706 boolParams[2] = GL_FALSE;
707 boolParams[3] = GL_FALSE;
720 bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
722 if (location < 0 || location >= (int)mUniformIndex.size())
727 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
728 targetUniform->dirty = true;
730 int elementCount = targetUniform->elementCount();
732 if (elementCount == 1 && count > 1)
733 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
735 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
737 if (targetUniform->type == GL_INT_VEC3)
739 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
741 for (int i = 0; i < count; i++)
751 else if (targetUniform->type == GL_BOOL_VEC3)
753 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
755 for (int i = 0; i < count; i++)
757 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
758 boolParams[1] = (v[1] == 0) ? GL_FALSE : GL_TRUE;
759 boolParams[2] = (v[2] == 0) ? GL_FALSE : GL_TRUE;
760 boolParams[3] = GL_FALSE;
773 bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
775 if (location < 0 || location >= (int)mUniformIndex.size())
780 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
781 targetUniform->dirty = true;
783 int elementCount = targetUniform->elementCount();
785 if (elementCount == 1 && count > 1)
786 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
788 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
790 if (targetUniform->type == GL_INT_VEC4)
792 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
794 for (int i = 0; i < count; i++)
804 else if (targetUniform->type == GL_BOOL_VEC4)
806 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
808 for (int i = 0; i < count; i++)
810 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
811 boolParams[1] = (v[1] == 0) ? GL_FALSE : GL_TRUE;
812 boolParams[2] = (v[2] == 0) ? GL_FALSE : GL_TRUE;
813 boolParams[3] = (v[3] == 0) ? GL_FALSE : GL_TRUE;
826 bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
828 if (location < 0 || location >= (int)mUniformIndex.size())
833 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
835 // sized queries -- ensure the provided buffer is large enough
838 int requiredBytes = UniformExternalSize(targetUniform->type);
839 if (*bufSize < requiredBytes)
845 switch (targetUniform->type)
848 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
851 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
854 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
858 unsigned int size = UniformComponentCount(targetUniform->type);
860 switch (UniformComponentType(targetUniform->type))
864 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
866 for (unsigned int i = 0; i < size; i++)
868 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
873 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(GLfloat),
874 size * sizeof(GLfloat));
878 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
880 for (unsigned int i = 0; i < size; i++)
882 params[i] = (float)intParams[i];
886 default: UNREACHABLE();
894 bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
896 if (location < 0 || location >= (int)mUniformIndex.size())
901 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
903 // sized queries -- ensure the provided buffer is large enough
906 int requiredBytes = UniformExternalSize(targetUniform->type);
907 if (*bufSize < requiredBytes)
913 switch (targetUniform->type)
916 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
919 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
922 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
926 unsigned int size = VariableColumnCount(targetUniform->type);
928 switch (UniformComponentType(targetUniform->type))
932 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
934 for (unsigned int i = 0; i < size; i++)
936 params[i] = boolParams[i];
942 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
944 for (unsigned int i = 0; i < size; i++)
946 params[i] = (GLint)floatParams[i];
951 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(GLint),
952 size * sizeof(GLint));
954 default: UNREACHABLE();
962 void ProgramBinary::dirtyAllUniforms()
964 unsigned int numUniforms = mUniforms.size();
965 for (unsigned int index = 0; index < numUniforms; index++)
967 mUniforms[index]->dirty = true;
971 // Applies all the uniforms set for this program object to the renderer
972 void ProgramBinary::applyUniforms()
974 // Retrieve sampler uniform values
975 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
977 Uniform *targetUniform = *ub;
979 if (targetUniform->dirty)
981 if (targetUniform->type == GL_SAMPLER_2D ||
982 targetUniform->type == GL_SAMPLER_CUBE)
984 int count = targetUniform->elementCount();
985 GLint (*v)[4] = (GLint(*)[4])targetUniform->data;
987 if (targetUniform->psRegisterIndex >= 0)
989 unsigned int firstIndex = targetUniform->psRegisterIndex;
991 for (int i = 0; i < count; i++)
993 unsigned int samplerIndex = firstIndex + i;
995 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
997 ASSERT(mSamplersPS[samplerIndex].active);
998 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
1003 if (targetUniform->vsRegisterIndex >= 0)
1005 unsigned int firstIndex = targetUniform->vsRegisterIndex;
1007 for (int i = 0; i < count; i++)
1009 unsigned int samplerIndex = firstIndex + i;
1011 if (samplerIndex < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS)
1013 ASSERT(mSamplersVS[samplerIndex].active);
1014 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
1022 mRenderer->applyUniforms(this, &mUniforms);
1025 // Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1026 // Returns the number of used varying registers, or -1 if unsuccesful
1027 int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
1029 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
1031 fragmentShader->resetVaryingsRegisterAssignment();
1033 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1035 int n = VariableRowCount(varying->type) * varying->size;
1036 int m = VariableColumnCount(varying->type);
1037 bool success = false;
1039 if (m == 2 || m == 3 || m == 4)
1041 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1043 bool available = true;
1045 for (int y = 0; y < n && available; y++)
1047 for (int x = 0; x < m && available; x++)
1049 if (packing[r + y][x])
1061 for (int y = 0; y < n; y++)
1063 for (int x = 0; x < m; x++)
1065 packing[r + y][x] = &*varying;
1073 if (!success && m == 2)
1075 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1077 bool available = true;
1079 for (int y = 0; y < n && available; y++)
1081 for (int x = 2; x < 4 && available; x++)
1083 if (packing[r + y][x])
1095 for (int y = 0; y < n; y++)
1097 for (int x = 2; x < 4; x++)
1099 packing[r + y][x] = &*varying;
1112 for (int y = 0; y < maxVaryingVectors; y++)
1114 for (int x = 0; x < 4; x++)
1116 space[x] += packing[y][x] ? 0 : 1;
1122 for (int x = 0; x < 4; x++)
1124 if (space[x] >= n && space[x] < space[column])
1130 if (space[column] >= n)
1132 for (int r = 0; r < maxVaryingVectors; r++)
1134 if (!packing[r][column])
1138 for (int y = r; y < r + n; y++)
1140 packing[y][column] = &*varying;
1147 varying->col = column;
1156 infoLog.append("Could not pack varying %s", varying->name.c_str());
1162 // Return the number of used registers
1165 for (int r = 0; r < maxVaryingVectors; r++)
1167 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1176 bool ProgramBinary::linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4],
1177 std::string& pixelHLSL, std::string& vertexHLSL,
1178 FragmentShader *fragmentShader, VertexShader *vertexShader)
1180 if (pixelHLSL.empty() || vertexHLSL.empty())
1185 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
1186 bool usesFragColor = fragmentShader->mUsesFragColor;
1187 bool usesFragData = fragmentShader->mUsesFragData;
1188 if (usesFragColor && usesFragData)
1190 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
1194 // Write the HLSL input/output declarations
1195 const int shaderModel = mRenderer->getMajorShaderModel();
1196 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
1198 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
1200 // The output color is broadcast to all enabled draw buffers when writing to gl_FragColor
1201 const bool broadcast = fragmentShader->mUsesFragColor;
1202 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
1204 if (registersNeeded > maxVaryingVectors)
1206 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
1211 vertexShader->resetVaryingsRegisterAssignment();
1213 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1215 bool matched = false;
1217 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1219 if (output->name == input->name)
1221 if (output->type != input->type || output->size != input->size)
1223 infoLog.append("Type of vertex varying %s does not match that of the fragment varying", output->name.c_str());
1228 output->reg = input->reg;
1229 output->col = input->col;
1238 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
1244 mUsesPointSize = vertexShader->mUsesPointSize;
1245 std::string varyingSemantic = (mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
1246 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
1247 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
1248 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
1250 // special varyings that use reserved registers
1251 int reservedRegisterIndex = registers;
1252 std::string fragCoordSemantic;
1253 std::string pointCoordSemantic;
1255 if (fragmentShader->mUsesFragCoord)
1257 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1260 if (fragmentShader->mUsesPointCoord)
1262 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
1263 // In DX11 we compute this in the GS.
1264 if (shaderModel == 3)
1266 pointCoordSemantic = "TEXCOORD0";
1268 else if (shaderModel >= 4)
1270 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1274 vertexHLSL += "struct VS_INPUT\n"
1277 int semanticIndex = 0;
1278 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1280 switch (attribute->type)
1282 case GL_FLOAT: vertexHLSL += " float "; break;
1283 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1284 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1285 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1286 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1287 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1288 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1289 default: UNREACHABLE();
1292 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1294 semanticIndex += VariableRowCount(attribute->type);
1297 vertexHLSL += "};\n"
1299 "struct VS_OUTPUT\n"
1302 if (shaderModel < 4)
1304 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1307 for (int r = 0; r < registers; r++)
1309 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1311 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1314 if (fragmentShader->mUsesFragCoord)
1316 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
1319 if (vertexShader->mUsesPointSize && shaderModel >= 3)
1321 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1324 if (shaderModel >= 4)
1326 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1329 vertexHLSL += "};\n"
1331 "VS_OUTPUT main(VS_INPUT input)\n"
1334 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1336 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1338 if (VariableRowCount(attribute->type) > 1) // Matrix
1340 vertexHLSL += "transpose";
1343 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1346 if (shaderModel >= 4)
1351 " VS_OUTPUT output;\n"
1352 " output.gl_Position.x = gl_Position.x;\n"
1353 " output.gl_Position.y = -gl_Position.y;\n"
1354 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1355 " output.gl_Position.w = gl_Position.w;\n";
1362 " VS_OUTPUT output;\n"
1363 " output.gl_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
1364 " output.gl_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
1365 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1366 " output.gl_Position.w = gl_Position.w;\n";
1369 if (vertexShader->mUsesPointSize && shaderModel >= 3)
1371 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
1374 if (fragmentShader->mUsesFragCoord)
1376 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1379 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1381 if (varying->reg >= 0)
1383 for (int i = 0; i < varying->size; i++)
1385 int rows = VariableRowCount(varying->type);
1387 for (int j = 0; j < rows; j++)
1389 int r = varying->reg + i * rows + j;
1390 vertexHLSL += " output.v" + str(r);
1392 bool sharedRegister = false; // Register used by multiple varyings
1394 for (int x = 0; x < 4; x++)
1396 if (packing[r][x] && packing[r][x] != packing[r][0])
1398 sharedRegister = true;
1407 for (int x = 0; x < 4; x++)
1409 if (packing[r][x] == &*varying)
1413 case 0: vertexHLSL += "x"; break;
1414 case 1: vertexHLSL += "y"; break;
1415 case 2: vertexHLSL += "z"; break;
1416 case 3: vertexHLSL += "w"; break;
1422 vertexHLSL += " = " + varying->name;
1426 vertexHLSL += "[" + str(i) + "]";
1431 vertexHLSL += "[" + str(j) + "]";
1434 vertexHLSL += ";\n";
1444 pixelHLSL += "struct PS_INPUT\n"
1447 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1449 if (varying->reg >= 0)
1451 for (int i = 0; i < varying->size; i++)
1453 int rows = VariableRowCount(varying->type);
1454 for (int j = 0; j < rows; j++)
1456 std::string n = str(varying->reg + i * rows + j);
1457 pixelHLSL += " float" + str(VariableColumnCount(varying->type)) + " v" + n + " : " + varyingSemantic + n + ";\n";
1464 if (fragmentShader->mUsesFragCoord)
1466 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
1469 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
1471 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
1474 // Must consume the PSIZE element if the geometry shader is not active
1475 // We won't know if we use a GS until we draw
1476 if (vertexShader->mUsesPointSize && shaderModel >= 4)
1478 pixelHLSL += " float gl_PointSize : PSIZE;\n";
1481 if (fragmentShader->mUsesFragCoord)
1483 if (shaderModel >= 4)
1485 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
1487 else if (shaderModel >= 3)
1489 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1495 "struct PS_OUTPUT\n"
1498 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
1500 pixelHLSL += " float4 gl_Color" + str(renderTargetIndex) + " : " + targetSemantic + str(renderTargetIndex) + ";\n";
1503 if (fragmentShader->mUsesFragDepth)
1505 pixelHLSL += " float gl_Depth : " + depthSemantic + ";\n";
1511 if (fragmentShader->mUsesFrontFacing)
1513 if (shaderModel >= 4)
1515 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
1520 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
1526 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
1530 if (fragmentShader->mUsesFragCoord)
1532 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1534 if (shaderModel >= 4)
1536 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
1537 " gl_FragCoord.y = input.dx_VPos.y;\n";
1539 else if (shaderModel >= 3)
1541 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
1542 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
1546 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
1547 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
1548 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
1551 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
1552 " gl_FragCoord.w = rhw;\n";
1555 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
1557 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1558 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
1561 if (fragmentShader->mUsesFrontFacing)
1563 if (shaderModel <= 3)
1565 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1569 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1573 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1575 if (varying->reg >= 0)
1577 for (int i = 0; i < varying->size; i++)
1579 int rows = VariableRowCount(varying->type);
1580 for (int j = 0; j < rows; j++)
1582 std::string n = str(varying->reg + i * rows + j);
1583 pixelHLSL += " " + varying->name;
1587 pixelHLSL += "[" + str(i) + "]";
1592 pixelHLSL += "[" + str(j) + "]";
1595 switch (VariableColumnCount(varying->type))
1597 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1598 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1599 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1600 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1601 default: UNREACHABLE();
1612 " PS_OUTPUT output;\n";
1614 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
1616 unsigned int sourceColorIndex = broadcast ? 0 : renderTargetIndex;
1618 pixelHLSL += " output.gl_Color" + str(renderTargetIndex) + " = gl_Color[" + str(sourceColorIndex) + "];\n";
1621 if (fragmentShader->mUsesFragDepth)
1623 pixelHLSL += " output.gl_Depth = gl_Depth;\n";
1633 bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1635 BinaryInputStream stream(binary, length);
1638 stream.read(&format);
1639 if (format != GL_PROGRAM_BINARY_ANGLE)
1641 infoLog.append("Invalid program binary format.");
1645 int majorVersion = 0;
1646 int minorVersion = 0;
1647 stream.read(&majorVersion);
1648 stream.read(&minorVersion);
1649 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
1651 infoLog.append("Invalid program binary version.");
1655 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
1656 stream.read(commitString, ANGLE_COMMIT_HASH_SIZE);
1657 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
1659 infoLog.append("Invalid program binary version.");
1663 int compileFlags = 0;
1664 stream.read(&compileFlags);
1665 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
1667 infoLog.append("Mismatched compilation flags.");
1671 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1673 stream.read(&mLinkedAttribute[i].type);
1676 mLinkedAttribute[i].name = name;
1677 stream.read(&mSemanticIndex[i]);
1680 initAttributesByLayout();
1682 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1684 stream.read(&mSamplersPS[i].active);
1685 stream.read(&mSamplersPS[i].logicalTextureUnit);
1688 stream.read(&textureType);
1689 mSamplersPS[i].textureType = (TextureType) textureType;
1692 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
1694 stream.read(&mSamplersVS[i].active);
1695 stream.read(&mSamplersVS[i].logicalTextureUnit);
1698 stream.read(&textureType);
1699 mSamplersVS[i].textureType = (TextureType) textureType;
1702 stream.read(&mUsedVertexSamplerRange);
1703 stream.read(&mUsedPixelSamplerRange);
1704 stream.read(&mUsesPointSize);
1710 infoLog.append("Invalid program binary.");
1714 mUniforms.resize(size);
1715 for (unsigned int i = 0; i < size; ++i)
1720 unsigned int arraySize;
1723 stream.read(&precision);
1725 stream.read(&arraySize);
1727 mUniforms[i] = new Uniform(type, precision, name, arraySize);
1729 stream.read(&mUniforms[i]->psRegisterIndex);
1730 stream.read(&mUniforms[i]->vsRegisterIndex);
1731 stream.read(&mUniforms[i]->registerCount);
1737 infoLog.append("Invalid program binary.");
1741 mUniformIndex.resize(size);
1742 for (unsigned int i = 0; i < size; ++i)
1744 stream.read(&mUniformIndex[i].name);
1745 stream.read(&mUniformIndex[i].element);
1746 stream.read(&mUniformIndex[i].index);
1749 unsigned int pixelShaderSize;
1750 stream.read(&pixelShaderSize);
1752 unsigned int vertexShaderSize;
1753 stream.read(&vertexShaderSize);
1755 unsigned int geometryShaderSize;
1756 stream.read(&geometryShaderSize);
1758 const char *ptr = (const char*) binary + stream.offset();
1760 const GUID *binaryIdentifier = (const GUID *) ptr;
1761 ptr += sizeof(GUID);
1763 GUID identifier = mRenderer->getAdapterIdentifier();
1764 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
1766 infoLog.append("Invalid program binary.");
1770 const char *pixelShaderFunction = ptr;
1771 ptr += pixelShaderSize;
1773 const char *vertexShaderFunction = ptr;
1774 ptr += vertexShaderSize;
1776 const char *geometryShaderFunction = geometryShaderSize > 0 ? ptr : NULL;
1777 ptr += geometryShaderSize;
1779 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1780 pixelShaderSize, rx::SHADER_PIXEL);
1781 if (!mPixelExecutable)
1783 infoLog.append("Could not create pixel shader.");
1787 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1788 vertexShaderSize, rx::SHADER_VERTEX);
1789 if (!mVertexExecutable)
1791 infoLog.append("Could not create vertex shader.");
1792 delete mPixelExecutable;
1793 mPixelExecutable = NULL;
1797 if (geometryShaderFunction != NULL && geometryShaderSize > 0)
1799 mGeometryExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(geometryShaderFunction),
1800 geometryShaderSize, rx::SHADER_GEOMETRY);
1801 if (!mGeometryExecutable)
1803 infoLog.append("Could not create geometry shader.");
1804 delete mPixelExecutable;
1805 mPixelExecutable = NULL;
1806 delete mVertexExecutable;
1807 mVertexExecutable = NULL;
1813 mGeometryExecutable = NULL;
1819 bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1821 BinaryOutputStream stream;
1823 stream.write(GL_PROGRAM_BINARY_ANGLE);
1824 stream.write(ANGLE_MAJOR_VERSION);
1825 stream.write(ANGLE_MINOR_VERSION);
1826 stream.write(ANGLE_COMMIT_HASH, ANGLE_COMMIT_HASH_SIZE);
1827 stream.write(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
1829 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1831 stream.write(mLinkedAttribute[i].type);
1832 stream.write(mLinkedAttribute[i].name);
1833 stream.write(mSemanticIndex[i]);
1836 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1838 stream.write(mSamplersPS[i].active);
1839 stream.write(mSamplersPS[i].logicalTextureUnit);
1840 stream.write((int) mSamplersPS[i].textureType);
1843 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
1845 stream.write(mSamplersVS[i].active);
1846 stream.write(mSamplersVS[i].logicalTextureUnit);
1847 stream.write((int) mSamplersVS[i].textureType);
1850 stream.write(mUsedVertexSamplerRange);
1851 stream.write(mUsedPixelSamplerRange);
1852 stream.write(mUsesPointSize);
1854 stream.write(mUniforms.size());
1855 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1857 stream.write(mUniforms[i]->type);
1858 stream.write(mUniforms[i]->precision);
1859 stream.write(mUniforms[i]->name);
1860 stream.write(mUniforms[i]->arraySize);
1862 stream.write(mUniforms[i]->psRegisterIndex);
1863 stream.write(mUniforms[i]->vsRegisterIndex);
1864 stream.write(mUniforms[i]->registerCount);
1867 stream.write(mUniformIndex.size());
1868 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1870 stream.write(mUniformIndex[i].name);
1871 stream.write(mUniformIndex[i].element);
1872 stream.write(mUniformIndex[i].index);
1875 UINT pixelShaderSize = mPixelExecutable->getLength();
1876 stream.write(pixelShaderSize);
1878 UINT vertexShaderSize = mVertexExecutable->getLength();
1879 stream.write(vertexShaderSize);
1881 UINT geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
1882 stream.write(geometryShaderSize);
1884 GUID identifier = mRenderer->getAdapterIdentifier();
1886 GLsizei streamLength = stream.length();
1887 const void *streamData = stream.data();
1889 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize + geometryShaderSize;
1890 if (totalLength > bufSize)
1902 char *ptr = (char*) binary;
1904 memcpy(ptr, streamData, streamLength);
1905 ptr += streamLength;
1907 memcpy(ptr, &identifier, sizeof(GUID));
1908 ptr += sizeof(GUID);
1910 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
1911 ptr += pixelShaderSize;
1913 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
1914 ptr += vertexShaderSize;
1916 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
1918 memcpy(ptr, mGeometryExecutable->getFunction(), geometryShaderSize);
1919 ptr += geometryShaderSize;
1922 ASSERT(ptr - totalLength == binary);
1927 *length = totalLength;
1933 GLint ProgramBinary::getLength()
1936 if (save(NULL, INT_MAX, &length))
1946 bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
1948 if (!fragmentShader || !fragmentShader->isCompiled())
1953 if (!vertexShader || !vertexShader->isCompiled())
1958 std::string pixelHLSL = fragmentShader->getHLSL();
1959 std::string vertexHLSL = vertexShader->getHLSL();
1961 // Map the varyings to the register file
1962 const Varying *packing[IMPLEMENTATION_MAX_VARYING_VECTORS][4] = {NULL};
1963 int registers = packVaryings(infoLog, packing, fragmentShader);
1970 if (!linkVaryings(infoLog, registers, packing, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
1975 bool success = true;
1977 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1982 if (!linkUniforms(infoLog, vertexShader->getUniforms(), fragmentShader->getUniforms()))
1987 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1988 if (vertexShader->mUsesDepthRange || fragmentShader->mUsesDepthRange)
1990 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0));
1991 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0));
1992 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0));
1997 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), rx::SHADER_VERTEX, DiscardWorkaround(vertexShader->mUsesDiscardRewriting, vertexShader->mUsesNestedBreak));
1998 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), rx::SHADER_PIXEL, DiscardWorkaround(fragmentShader->mUsesDiscardRewriting, fragmentShader->mUsesNestedBreak));
2000 if (usesGeometryShader())
2002 std::string geometryHLSL = generateGeometryShaderHLSL(registers, packing, fragmentShader, vertexShader);
2003 mGeometryExecutable = mRenderer->compileToExecutable(infoLog, geometryHLSL.c_str(), rx::SHADER_GEOMETRY, rx::ANGLE_D3D_WORKAROUND_NONE);
2006 if (!mVertexExecutable || !mPixelExecutable || (usesGeometryShader() && !mGeometryExecutable))
2008 infoLog.append("Failed to create D3D shaders.");
2011 delete mVertexExecutable;
2012 mVertexExecutable = NULL;
2013 delete mPixelExecutable;
2014 mPixelExecutable = NULL;
2015 delete mGeometryExecutable;
2016 mGeometryExecutable = NULL;
2023 // Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
2024 bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
2026 unsigned int usedLocations = 0;
2028 // Link attributes that have a binding location
2029 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
2031 int location = attributeBindings.getAttributeBinding(attribute->name);
2033 if (location != -1) // Set by glBindAttribLocation
2035 if (!mLinkedAttribute[location].name.empty())
2037 // Multiple active attributes bound to the same location; not an error
2040 mLinkedAttribute[location] = *attribute;
2042 int rows = VariableRowCount(attribute->type);
2044 if (rows + location > MAX_VERTEX_ATTRIBS)
2046 infoLog.append("Active attribute (%s) at location %d is too big to fit", attribute->name.c_str(), location);
2051 for (int i = 0; i < rows; i++)
2053 usedLocations |= 1 << (location + i);
2058 // Link attributes that don't have a binding location
2059 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
2061 int location = attributeBindings.getAttributeBinding(attribute->name);
2063 if (location == -1) // Not set by glBindAttribLocation
2065 int rows = VariableRowCount(attribute->type);
2066 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
2068 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
2070 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
2072 return false; // Fail to link
2075 mLinkedAttribute[availableIndex] = *attribute;
2079 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
2081 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
2082 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
2084 for (int r = 0; r < rows; r++)
2086 mSemanticIndex[attributeIndex++] = index++;
2090 initAttributesByLayout();
2095 bool ProgramBinary::linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms)
2097 for (sh::ActiveUniforms::const_iterator uniform = vertexUniforms.begin(); uniform != vertexUniforms.end(); uniform++)
2099 if (!defineUniform(GL_VERTEX_SHADER, *uniform, infoLog))
2105 for (sh::ActiveUniforms::const_iterator uniform = fragmentUniforms.begin(); uniform != fragmentUniforms.end(); uniform++)
2107 if (!defineUniform(GL_FRAGMENT_SHADER, *uniform, infoLog))
2116 bool ProgramBinary::defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog)
2118 if (constant.type == GL_SAMPLER_2D ||
2119 constant.type == GL_SAMPLER_CUBE)
2121 unsigned int samplerIndex = constant.registerIndex;
2125 if (shader == GL_VERTEX_SHADER)
2127 if (samplerIndex < mRenderer->getMaxVertexTextureImageUnits())
2129 mSamplersVS[samplerIndex].active = true;
2130 mSamplersVS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2131 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2132 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2136 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).", mRenderer->getMaxVertexTextureImageUnits());
2140 else if (shader == GL_FRAGMENT_SHADER)
2142 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2144 mSamplersPS[samplerIndex].active = true;
2145 mSamplersPS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2146 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
2147 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
2151 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
2159 while (samplerIndex < constant.registerIndex + constant.arraySize);
2162 Uniform *uniform = NULL;
2163 GLint location = getUniformLocation(constant.name);
2165 if (location >= 0) // Previously defined, type and precision must match
2167 uniform = mUniforms[mUniformIndex[location].index];
2169 if (uniform->type != constant.type)
2171 infoLog.append("Types for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str());
2175 if (uniform->precision != constant.precision)
2177 infoLog.append("Precisions for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str());
2183 uniform = new Uniform(constant.type, constant.precision, constant.name, constant.arraySize);
2191 if (shader == GL_FRAGMENT_SHADER)
2193 uniform->psRegisterIndex = constant.registerIndex;
2195 else if (shader == GL_VERTEX_SHADER)
2197 uniform->vsRegisterIndex = constant.registerIndex;
2203 return uniform->type == constant.type;
2206 mUniforms.push_back(uniform);
2207 unsigned int uniformIndex = mUniforms.size() - 1;
2209 for (unsigned int i = 0; i < uniform->elementCount(); i++)
2211 mUniformIndex.push_back(UniformLocation(constant.name, i, uniformIndex));
2214 if (shader == GL_VERTEX_SHADER)
2216 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedVertexUniformVectors() + mRenderer->getMaxVertexUniformVectors())
2218 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)", mRenderer->getMaxVertexUniformVectors());
2222 else if (shader == GL_FRAGMENT_SHADER)
2224 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedFragmentUniformVectors() + mRenderer->getMaxFragmentUniformVectors())
2226 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)", mRenderer->getMaxFragmentUniformVectors());
2235 std::string ProgramBinary::generateGeometryShaderHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
2237 // for now we only handle point sprite emulation
2238 ASSERT(usesPointSpriteEmulation());
2239 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
2242 std::string ProgramBinary::generatePointSpriteHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
2244 ASSERT(registers >= 0);
2245 ASSERT(vertexShader->mUsesPointSize);
2246 ASSERT(mRenderer->getMajorShaderModel() >= 4);
2248 std::string geomHLSL;
2250 std::string varyingSemantic = "TEXCOORD";
2252 std::string fragCoordSemantic;
2253 std::string pointCoordSemantic;
2255 int reservedRegisterIndex = registers;
2257 if (fragmentShader->mUsesFragCoord)
2259 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
2262 if (fragmentShader->mUsesPointCoord)
2264 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
2267 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
2272 for (int r = 0; r < registers; r++)
2274 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
2276 geomHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
2279 if (fragmentShader->mUsesFragCoord)
2281 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
2284 geomHLSL += " float gl_PointSize : PSIZE;\n"
2285 " float4 gl_Position : SV_Position;\n"
2288 "struct GS_OUTPUT\n"
2291 for (int r = 0; r < registers; r++)
2293 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
2295 geomHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
2298 if (fragmentShader->mUsesFragCoord)
2300 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
2303 if (fragmentShader->mUsesPointCoord)
2305 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
2308 geomHLSL += " float gl_PointSize : PSIZE;\n"
2309 " float4 gl_Position : SV_Position;\n"
2312 "static float2 pointSpriteCorners[] = \n"
2314 " float2( 0.5f, -0.5f),\n"
2315 " float2( 0.5f, 0.5f),\n"
2316 " float2(-0.5f, -0.5f),\n"
2317 " float2(-0.5f, 0.5f)\n"
2320 "static float2 pointSpriteTexcoords[] = \n"
2322 " float2(1.0f, 1.0f),\n"
2323 " float2(1.0f, 0.0f),\n"
2324 " float2(0.0f, 1.0f),\n"
2325 " float2(0.0f, 0.0f)\n"
2328 "static float minPointSize = " + str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
2329 "static float maxPointSize = " + str(mRenderer->getMaxPointSize()) + ".0f;\n"
2331 "[maxvertexcount(4)]\n"
2332 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
2334 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
2335 " output.gl_PointSize = input[0].gl_PointSize;\n";
2337 for (int r = 0; r < registers; r++)
2339 geomHLSL += " output.v" + str(r) + " = input[0].v" + str(r) + ";\n";
2342 if (fragmentShader->mUsesFragCoord)
2344 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
2348 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
2349 " float4 gl_Position = input[0].gl_Position;\n"
2350 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n";
2352 for (int corner = 0; corner < 4; corner++)
2355 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
2357 if (fragmentShader->mUsesPointCoord)
2359 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + str(corner) + "];\n";
2362 geomHLSL += " outStream.Append(output);\n";
2366 " outStream.RestartStrip();\n"
2372 // This method needs to match OutputHLSL::decorate
2373 std::string ProgramBinary::decorateAttribute(const std::string &name)
2375 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2383 bool ProgramBinary::isValidated() const
2388 void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
2390 // Skip over inactive attributes
2391 unsigned int activeAttribute = 0;
2392 unsigned int attribute;
2393 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2395 if (mLinkedAttribute[attribute].name.empty())
2400 if (activeAttribute == index)
2410 const char *string = mLinkedAttribute[attribute].name.c_str();
2412 strncpy(name, string, bufsize);
2413 name[bufsize - 1] = '\0';
2417 *length = strlen(name);
2421 *size = 1; // Always a single 'type' instance
2423 *type = mLinkedAttribute[attribute].type;
2426 GLint ProgramBinary::getActiveAttributeCount() const
2430 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2432 if (!mLinkedAttribute[attributeIndex].name.empty())
2441 GLint ProgramBinary::getActiveAttributeMaxLength() const
2445 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2447 if (!mLinkedAttribute[attributeIndex].name.empty())
2449 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2456 void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
2458 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2462 std::string string = mUniforms[index]->name;
2464 if (mUniforms[index]->isArray())
2469 strncpy(name, string.c_str(), bufsize);
2470 name[bufsize - 1] = '\0';
2474 *length = strlen(name);
2478 *size = mUniforms[index]->elementCount();
2480 *type = mUniforms[index]->type;
2483 GLint ProgramBinary::getActiveUniformCount() const
2485 return mUniforms.size();
2488 GLint ProgramBinary::getActiveUniformMaxLength() const
2492 unsigned int numUniforms = mUniforms.size();
2493 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2495 if (!mUniforms[uniformIndex]->name.empty())
2497 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2498 if (mUniforms[uniformIndex]->isArray())
2500 length += 3; // Counting in "[0]".
2502 maxLength = std::max(length, maxLength);
2509 void ProgramBinary::validate(InfoLog &infoLog)
2512 if (!validateSamplers(&infoLog))
2522 bool ProgramBinary::validateSamplers(InfoLog *infoLog)
2524 // if any two active samplers in a program are of different types, but refer to the same
2525 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2526 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2528 const unsigned int maxCombinedTextureImageUnits = mRenderer->getMaxCombinedTextureImageUnits();
2529 TextureType textureUnitType[IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS];
2531 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i)
2533 textureUnitType[i] = TEXTURE_UNKNOWN;
2536 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2538 if (mSamplersPS[i].active)
2540 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2542 if (unit >= maxCombinedTextureImageUnits)
2546 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
2552 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2554 if (mSamplersPS[i].textureType != textureUnitType[unit])
2558 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
2566 textureUnitType[unit] = mSamplersPS[i].textureType;
2571 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2573 if (mSamplersVS[i].active)
2575 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2577 if (unit >= maxCombinedTextureImageUnits)
2581 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
2587 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2589 if (mSamplersVS[i].textureType != textureUnitType[unit])
2593 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
2601 textureUnitType[unit] = mSamplersVS[i].textureType;
2609 ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2613 struct AttributeSorter
2615 AttributeSorter(const int (&semanticIndices)[MAX_VERTEX_ATTRIBS])
2616 : originalIndices(semanticIndices)
2620 bool operator()(int a, int b)
2622 if (originalIndices[a] == -1) return false;
2623 if (originalIndices[b] == -1) return true;
2624 return (originalIndices[a] < originalIndices[b]);
2627 const int (&originalIndices)[MAX_VERTEX_ATTRIBS];
2630 void ProgramBinary::initAttributesByLayout()
2632 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2634 mAttributesByLayout[i] = i;
2637 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2640 void ProgramBinary::sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const
2642 rx::TranslatedAttribute oldTranslatedAttributes[MAX_VERTEX_ATTRIBS];
2644 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2646 oldTranslatedAttributes[i] = attributes[i];
2649 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2651 int oldIndex = mAttributesByLayout[i];
2652 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2653 attributes[i] = oldTranslatedAttributes[oldIndex];