1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // Program.cpp: Implements the Program class. Implements GL program objects
16 // and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
23 #include "TransformFeedback.h"
24 #include "utilities.h"
25 #include "common/debug.h"
26 #include "Shader/PixelShader.hpp"
27 #include "Shader/VertexShader.hpp"
35 unsigned int Program::currentSerial = 1;
37 std::string str(int i)
40 sprintf(buffer, "%d", i);
44 Uniform::BlockInfo::BlockInfo(const glsl::Uniform& uniform, int blockIndex)
49 offset = uniform.blockInfo.offset;
50 arrayStride = uniform.blockInfo.arrayStride;
51 matrixStride = uniform.blockInfo.matrixStride;
52 isRowMajorMatrix = uniform.blockInfo.isRowMajorMatrix;
60 isRowMajorMatrix = false;
64 Uniform::Uniform(GLenum type, GLenum precision, const std::string &name, unsigned int arraySize,
65 const BlockInfo &blockInfo)
66 : type(type), precision(precision), name(name), arraySize(arraySize), blockInfo(blockInfo)
68 if(blockInfo.index == -1)
70 size_t bytes = UniformTypeSize(type) * size();
71 data = new unsigned char[bytes];
72 memset(data, 0, bytes);
89 bool Uniform::isArray() const
91 return arraySize >= 1;
94 int Uniform::size() const
96 return arraySize > 0 ? arraySize : 1;
99 int Uniform::registerCount() const
101 return size() * VariableRegisterCount(type);
104 UniformBlock::UniformBlock(const std::string &name, unsigned int elementIndex, unsigned int dataSize, std::vector<unsigned int> memberUniformIndexes) :
105 name(name), elementIndex(elementIndex), dataSize(dataSize), memberUniformIndexes(memberUniformIndexes), psRegisterIndex(GL_INVALID_INDEX), vsRegisterIndex(GL_INVALID_INDEX)
109 void UniformBlock::setRegisterIndex(GLenum shader, unsigned int registerIndex)
113 case GL_VERTEX_SHADER:
114 vsRegisterIndex = registerIndex;
116 case GL_FRAGMENT_SHADER:
117 psRegisterIndex = registerIndex;
124 bool UniformBlock::isArrayElement() const
126 return elementIndex != GL_INVALID_INDEX;
129 bool UniformBlock::isReferencedByVertexShader() const
131 return vsRegisterIndex != GL_INVALID_INDEX;
134 bool UniformBlock::isReferencedByFragmentShader() const
136 return psRegisterIndex != GL_INVALID_INDEX;
139 UniformLocation::UniformLocation(const std::string &name, unsigned int element, unsigned int index) : name(name), element(element), index(index)
143 LinkedVarying::LinkedVarying()
147 LinkedVarying::LinkedVarying(const std::string &name, GLenum type, GLsizei size, int reg, int col)
148 : name(name), type(type), size(size), reg(reg), col(col)
152 Program::Program(ResourceManager *manager, GLuint handle) : serial(issueSerial()), resourceManager(manager), handle(handle)
159 transformFeedbackBufferMode = GL_INTERLEAVED_ATTRIBS;
160 totalLinkedVaryingsComponents = 0;
165 resetUniformBlockBindings();
169 retrievableBinary = false;
179 vertexShader->release();
184 fragmentShader->release();
188 bool Program::attachShader(Shader *shader)
190 if(shader->getType() == GL_VERTEX_SHADER)
197 vertexShader = (VertexShader*)shader;
198 vertexShader->addRef();
200 else if(shader->getType() == GL_FRAGMENT_SHADER)
207 fragmentShader = (FragmentShader*)shader;
208 fragmentShader->addRef();
210 else UNREACHABLE(shader->getType());
215 bool Program::detachShader(Shader *shader)
217 if(shader->getType() == GL_VERTEX_SHADER)
219 if(vertexShader != shader)
224 vertexShader->release();
227 else if(shader->getType() == GL_FRAGMENT_SHADER)
229 if(fragmentShader != shader)
234 fragmentShader->release();
237 else UNREACHABLE(shader->getType());
242 int Program::getAttachedShadersCount() const
244 return (vertexShader ? 1 : 0) + (fragmentShader ? 1 : 0);
247 sw::PixelShader *Program::getPixelShader()
252 sw::VertexShader *Program::getVertexShader()
257 GLint Program::getFragDataLocation(const GLchar *name)
261 std::string baseName(name);
262 unsigned int subscript = GL_INVALID_INDEX;
263 baseName = ParseUniformName(baseName, &subscript);
264 for(auto const &input : fragmentShader->varyings)
266 if(input.name == baseName)
268 int rowCount = VariableRowCount(input.type);
269 int colCount = VariableColumnCount(input.type);
270 return (subscript == GL_INVALID_INDEX) ? input.reg : input.reg + (rowCount > 1 ? colCount * subscript : subscript);
278 void Program::bindAttributeLocation(GLuint index, const char *name)
280 attributeBinding[name] = index;
283 GLint Program::getAttributeLocation(const char *name)
287 std::string strName(name);
288 for(auto const &it : linkedAttribute)
290 if(it.name == strName)
292 return getAttributeBinding(it);
300 int Program::getAttributeStream(int attributeIndex)
302 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
304 return attributeStream[attributeIndex];
307 // Returns the index of the texture image unit (0-19) corresponding to a sampler index (0-15 for the pixel shader and 0-3 for the vertex shader)
308 GLint Program::getSamplerMapping(sw::SamplerType type, unsigned int samplerIndex)
310 GLint logicalTextureUnit = -1;
314 case sw::SAMPLER_PIXEL:
315 ASSERT(samplerIndex < sizeof(samplersPS) / sizeof(samplersPS[0]));
317 if(samplersPS[samplerIndex].active)
319 logicalTextureUnit = samplersPS[samplerIndex].logicalTextureUnit;
322 case sw::SAMPLER_VERTEX:
323 ASSERT(samplerIndex < sizeof(samplersVS) / sizeof(samplersVS[0]));
325 if(samplersVS[samplerIndex].active)
327 logicalTextureUnit = samplersVS[samplerIndex].logicalTextureUnit;
330 default: UNREACHABLE(type);
333 if(logicalTextureUnit < MAX_COMBINED_TEXTURE_IMAGE_UNITS)
335 return logicalTextureUnit;
341 // Returns the texture type for a given sampler type and index (0-15 for the pixel shader and 0-3 for the vertex shader)
342 TextureType Program::getSamplerTextureType(sw::SamplerType type, unsigned int samplerIndex)
346 case sw::SAMPLER_PIXEL:
347 ASSERT(samplerIndex < sizeof(samplersPS)/sizeof(samplersPS[0]));
348 ASSERT(samplersPS[samplerIndex].active);
349 return samplersPS[samplerIndex].textureType;
350 case sw::SAMPLER_VERTEX:
351 ASSERT(samplerIndex < sizeof(samplersVS)/sizeof(samplersVS[0]));
352 ASSERT(samplersVS[samplerIndex].active);
353 return samplersVS[samplerIndex].textureType;
354 default: UNREACHABLE(type);
360 bool Program::isUniformDefined(const std::string &name) const
362 unsigned int subscript = GL_INVALID_INDEX;
363 std::string baseName = es2::ParseUniformName(name, &subscript);
365 size_t numUniforms = uniformIndex.size();
366 for(size_t location = 0; location < numUniforms; location++)
368 const unsigned int index = uniformIndex[location].index;
369 if((uniformIndex[location].name == baseName) && ((index == GL_INVALID_INDEX) ||
370 ((uniforms[index]->isArray() && uniformIndex[location].element == subscript) ||
371 (subscript == GL_INVALID_INDEX))))
380 GLint Program::getUniformLocation(const std::string &name) const
382 unsigned int subscript = GL_INVALID_INDEX;
383 std::string baseName = es2::ParseUniformName(name, &subscript);
385 size_t numUniforms = uniformIndex.size();
386 for(size_t location = 0; location < numUniforms; location++)
388 const unsigned int index = uniformIndex[location].index;
389 if((index != GL_INVALID_INDEX) && (uniformIndex[location].name == baseName) &&
390 ((uniforms[index]->isArray() && uniformIndex[location].element == subscript) ||
391 (subscript == GL_INVALID_INDEX)))
393 return (GLint)location;
400 GLuint Program::getUniformIndex(const std::string &name) const
402 unsigned int subscript = GL_INVALID_INDEX;
403 std::string baseName = es2::ParseUniformName(name, &subscript);
405 // The app is not allowed to specify array indices other than 0 for arrays of basic types
406 if(subscript != 0 && subscript != GL_INVALID_INDEX)
408 return GL_INVALID_INDEX;
411 size_t numUniforms = uniforms.size();
412 for(GLuint index = 0; index < numUniforms; index++)
414 if(uniforms[index]->name == baseName)
416 if(uniforms[index]->isArray() || subscript == GL_INVALID_INDEX)
423 return GL_INVALID_INDEX;
426 void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
428 ASSERT(uniformBlockIndex < getActiveUniformBlockCount());
430 const UniformBlock &uniformBlock = *uniformBlocks[uniformBlockIndex];
434 case GL_UNIFORM_BLOCK_DATA_SIZE:
435 *params = static_cast<GLint>(uniformBlock.dataSize);
437 case GL_UNIFORM_BLOCK_NAME_LENGTH:
438 *params = static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArrayElement() ? 3 : 0));
440 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
441 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
443 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
445 for(unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
447 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
451 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
452 *params = static_cast<GLint>(uniformBlock.isReferencedByVertexShader());
454 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
455 *params = static_cast<GLint>(uniformBlock.isReferencedByFragmentShader());
457 default: UNREACHABLE(pname);
461 GLuint Program::getUniformBlockIndex(const std::string &name) const
463 unsigned int subscript = GL_INVALID_INDEX;
464 std::string baseName = es2::ParseUniformName(name, &subscript);
466 size_t numUniformBlocks = getActiveUniformBlockCount();
467 for(GLuint blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
469 const UniformBlock &uniformBlock = *uniformBlocks[blockIndex];
470 if(uniformBlock.name == baseName)
472 const bool arrayElementZero = (subscript == GL_INVALID_INDEX && uniformBlock.elementIndex == 0);
473 if(subscript == uniformBlock.elementIndex || arrayElementZero)
480 return GL_INVALID_INDEX;
483 void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
485 if(uniformBlockIndex >= getActiveUniformBlockCount())
487 return error(GL_INVALID_VALUE);
490 uniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
493 GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
495 if(uniformBlockIndex >= getActiveUniformBlockCount())
497 return error(GL_INVALID_VALUE, GL_INVALID_INDEX);
499 return uniformBlockBindings[uniformBlockIndex];
502 void Program::resetUniformBlockBindings()
504 for(unsigned int blockId = 0; blockId < MAX_UNIFORM_BUFFER_BINDINGS; blockId++)
506 uniformBlockBindings[blockId] = 0;
510 bool Program::setUniformfv(GLint location, GLsizei count, const GLfloat *v, int numElements)
512 ASSERT(numElements >= 1 && numElements <= 4);
514 static GLenum floatType[] = { GL_FLOAT, GL_FLOAT_VEC2, GL_FLOAT_VEC3, GL_FLOAT_VEC4 };
515 static GLenum boolType[] = { GL_BOOL, GL_BOOL_VEC2, GL_BOOL_VEC3, GL_BOOL_VEC4 };
517 if(location < 0 || location >= (int)uniformIndex.size() || (uniformIndex[location].index == GL_INVALID_INDEX))
522 Uniform *targetUniform = uniforms[uniformIndex[location].index];
523 targetUniform->dirty = true;
525 int size = targetUniform->size();
527 if(size == 1 && count > 1)
529 return false; // Attempting to write an array to a non-array uniform is an INVALID_OPERATION
532 count = std::min(size - (int)uniformIndex[location].element, count);
534 int index = numElements - 1;
535 if(targetUniform->type == floatType[index])
537 memcpy(targetUniform->data + uniformIndex[location].element * sizeof(GLfloat)* numElements,
538 v, numElements * sizeof(GLfloat) * count);
540 else if(targetUniform->type == boolType[index])
542 GLboolean *boolParams = (GLboolean*)targetUniform->data + uniformIndex[location].element * numElements;
544 for(int i = 0; i < count * numElements; i++)
546 boolParams[i] = (v[i] == 0.0f) ? GL_FALSE : GL_TRUE;
557 bool Program::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
559 return setUniformfv(location, count, v, 1);
562 bool Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
564 return setUniformfv(location, count, v, 2);
567 bool Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
569 return setUniformfv(location, count, v, 3);
572 bool Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
574 return setUniformfv(location, count, v, 4);
577 bool Program::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum type)
585 case GL_FLOAT_MAT2x3:
586 case GL_FLOAT_MAT3x2:
589 case GL_FLOAT_MAT2x4:
590 case GL_FLOAT_MAT4x2:
596 case GL_FLOAT_MAT3x4:
597 case GL_FLOAT_MAT4x3:
607 if(location < 0 || location >= (int)uniformIndex.size() || (uniformIndex[location].index == GL_INVALID_INDEX))
612 Uniform *targetUniform = uniforms[uniformIndex[location].index];
613 targetUniform->dirty = true;
615 if(targetUniform->type != type)
620 int size = targetUniform->size();
622 if(size == 1 && count > 1)
624 return false; // Attempting to write an array to a non-array uniform is an INVALID_OPERATION
627 count = std::min(size - (int)uniformIndex[location].element, count);
629 GLfloat* dst = reinterpret_cast<GLfloat*>(targetUniform->data + uniformIndex[location].element * sizeof(GLfloat) * numElements);
631 if(transpose == GL_FALSE)
633 memcpy(dst, value, numElements * sizeof(GLfloat) * count);
637 const int rowSize = VariableRowCount(type);
638 const int colSize = VariableColumnCount(type);
639 for(int n = 0; n < count; ++n)
641 for(int i = 0; i < colSize; ++i)
643 for(int j = 0; j < rowSize; ++j)
645 dst[i * rowSize + j] = value[j * colSize + i];
649 value += numElements;
657 bool Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
659 return setUniformMatrixfv(location, count, transpose, value, GL_FLOAT_MAT2);
662 bool Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
664 return setUniformMatrixfv(location, count, transpose, value, GL_FLOAT_MAT2x3);
667 bool Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
669 return setUniformMatrixfv(location, count, transpose, value, GL_FLOAT_MAT2x4);
672 bool Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
674 return setUniformMatrixfv(location, count, transpose, value, GL_FLOAT_MAT3);
677 bool Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
679 return setUniformMatrixfv(location, count, transpose, value, GL_FLOAT_MAT3x2);
682 bool Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
684 return setUniformMatrixfv(location, count, transpose, value, GL_FLOAT_MAT3x4);
687 bool Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
689 return setUniformMatrixfv(location, count, transpose, value, GL_FLOAT_MAT4);
692 bool Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
694 return setUniformMatrixfv(location, count, transpose, value, GL_FLOAT_MAT4x2);
697 bool Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
699 return setUniformMatrixfv(location, count, transpose, value, GL_FLOAT_MAT4x3);
702 bool Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
704 if(location < 0 || location >= (int)uniformIndex.size() || (uniformIndex[location].index == GL_INVALID_INDEX))
709 Uniform *targetUniform = uniforms[uniformIndex[location].index];
710 targetUniform->dirty = true;
712 int size = targetUniform->size();
714 if(size == 1 && count > 1)
716 return false; // Attempting to write an array to a non-array uniform is an INVALID_OPERATION
719 count = std::min(size - (int)uniformIndex[location].element, count);
721 if(targetUniform->type == GL_INT || IsSamplerUniform(targetUniform->type))
723 memcpy(targetUniform->data + uniformIndex[location].element * sizeof(GLint),
724 v, sizeof(GLint) * count);
726 else if(targetUniform->type == GL_BOOL)
728 GLboolean *boolParams = new GLboolean[count];
730 for(int i = 0; i < count; i++)
734 boolParams[i] = GL_FALSE;
738 boolParams[i] = GL_TRUE;
742 memcpy(targetUniform->data + uniformIndex[location].element * sizeof(GLboolean),
743 boolParams, sizeof(GLboolean) * count);
755 bool Program::setUniformiv(GLint location, GLsizei count, const GLint *v, int numElements)
757 static GLenum intType[] = { GL_INT, GL_INT_VEC2, GL_INT_VEC3, GL_INT_VEC4 };
758 static GLenum boolType[] = { GL_BOOL, GL_BOOL_VEC2, GL_BOOL_VEC3, GL_BOOL_VEC4 };
760 if(location < 0 || location >= (int)uniformIndex.size() || (uniformIndex[location].index == GL_INVALID_INDEX))
765 Uniform *targetUniform = uniforms[uniformIndex[location].index];
766 targetUniform->dirty = true;
768 int size = targetUniform->size();
770 if(size == 1 && count > 1)
772 return false; // Attempting to write an array to a non-array uniform is an INVALID_OPERATION
775 count = std::min(size - (int)uniformIndex[location].element, count);
777 int index = numElements - 1;
778 if(targetUniform->type == intType[index])
780 memcpy(targetUniform->data + uniformIndex[location].element * sizeof(GLint)* numElements,
781 v, numElements * sizeof(GLint)* count);
783 else if(targetUniform->type == boolType[index])
785 GLboolean *boolParams = new GLboolean[count * numElements];
787 for(int i = 0; i < count * numElements; i++)
789 boolParams[i] = (v[i] == 0) ? GL_FALSE : GL_TRUE;
792 memcpy(targetUniform->data + uniformIndex[location].element * sizeof(GLboolean)* numElements,
793 boolParams, numElements * sizeof(GLboolean)* count);
805 bool Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
807 return setUniformiv(location, count, v, 2);
810 bool Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
812 return setUniformiv(location, count, v, 3);
815 bool Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
817 return setUniformiv(location, count, v, 4);
820 bool Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
822 if(location < 0 || location >= (int)uniformIndex.size() || (uniformIndex[location].index == GL_INVALID_INDEX))
827 Uniform *targetUniform = uniforms[uniformIndex[location].index];
828 targetUniform->dirty = true;
830 int size = targetUniform->size();
832 if(size == 1 && count > 1)
834 return false; // Attempting to write an array to a non-array uniform is an INVALID_OPERATION
837 count = std::min(size - (int)uniformIndex[location].element, count);
839 if(targetUniform->type == GL_UNSIGNED_INT)
841 memcpy(targetUniform->data + uniformIndex[location].element * sizeof(GLuint),
842 v, sizeof(GLuint)* count);
844 else if(targetUniform->type == GL_BOOL)
846 GLboolean *boolParams = new GLboolean[count];
848 for(int i = 0; i < count; i++)
852 boolParams[i] = GL_FALSE;
856 boolParams[i] = GL_TRUE;
860 memcpy(targetUniform->data + uniformIndex[location].element * sizeof(GLboolean),
861 boolParams, sizeof(GLboolean)* count);
873 bool Program::setUniformuiv(GLint location, GLsizei count, const GLuint *v, int numElements)
875 static GLenum uintType[] = { GL_UNSIGNED_INT, GL_UNSIGNED_INT_VEC2, GL_UNSIGNED_INT_VEC3, GL_UNSIGNED_INT_VEC4 };
876 static GLenum boolType[] = { GL_BOOL, GL_BOOL_VEC2, GL_BOOL_VEC3, GL_BOOL_VEC4 };
878 if(location < 0 || location >= (int)uniformIndex.size() || (uniformIndex[location].index == GL_INVALID_INDEX))
883 Uniform *targetUniform = uniforms[uniformIndex[location].index];
884 targetUniform->dirty = true;
886 int size = targetUniform->size();
888 if(size == 1 && count > 1)
890 return false; // Attempting to write an array to a non-array uniform is an INVALID_OPERATION
893 count = std::min(size - (int)uniformIndex[location].element, count);
895 int index = numElements - 1;
896 if(targetUniform->type == uintType[index])
898 memcpy(targetUniform->data + uniformIndex[location].element * sizeof(GLuint)* numElements,
899 v, numElements * sizeof(GLuint)* count);
901 else if(targetUniform->type == boolType[index])
903 GLboolean *boolParams = new GLboolean[count * numElements];
905 for(int i = 0; i < count * numElements; i++)
907 boolParams[i] = (v[i] == 0) ? GL_FALSE : GL_TRUE;
910 memcpy(targetUniform->data + uniformIndex[location].element * sizeof(GLboolean)* numElements,
911 boolParams, numElements * sizeof(GLboolean)* count);
923 bool Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
925 return setUniformuiv(location, count, v, 2);
928 bool Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
930 return setUniformuiv(location, count, v, 3);
933 bool Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
935 return setUniformuiv(location, count, v, 4);
938 bool Program::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
940 if(location < 0 || location >= (int)uniformIndex.size() || (uniformIndex[location].index == GL_INVALID_INDEX))
945 Uniform *targetUniform = uniforms[uniformIndex[location].index];
946 unsigned int count = UniformComponentCount(targetUniform->type);
948 // Sized query - ensure the provided buffer is large enough
949 if(bufSize && static_cast<unsigned int>(*bufSize) < count * sizeof(GLfloat))
954 switch(UniformComponentType(targetUniform->type))
958 GLboolean *boolParams = (GLboolean*)targetUniform->data + uniformIndex[location].element * count;
960 for(unsigned int i = 0; i < count; i++)
962 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
967 memcpy(params, targetUniform->data + uniformIndex[location].element * count * sizeof(GLfloat),
968 count * sizeof(GLfloat));
972 GLint *intParams = (GLint*)targetUniform->data + uniformIndex[location].element * count;
974 for(unsigned int i = 0; i < count; i++)
976 params[i] = (float)intParams[i];
980 case GL_UNSIGNED_INT:
982 GLuint *uintParams = (GLuint*)targetUniform->data + uniformIndex[location].element * count;
984 for(unsigned int i = 0; i < count; i++)
986 params[i] = (float)uintParams[i];
990 default: UNREACHABLE(targetUniform->type);
996 bool Program::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
998 if(location < 0 || location >= (int)uniformIndex.size() || (uniformIndex[location].index == GL_INVALID_INDEX))
1003 Uniform *targetUniform = uniforms[uniformIndex[location].index];
1004 unsigned int count = UniformComponentCount(targetUniform->type);
1006 // Sized query - ensure the provided buffer is large enough
1007 if(bufSize && static_cast<unsigned int>(*bufSize) < count * sizeof(GLint))
1012 switch(UniformComponentType(targetUniform->type))
1016 GLboolean *boolParams = targetUniform->data + uniformIndex[location].element * count;
1018 for(unsigned int i = 0; i < count; i++)
1020 params[i] = (GLint)boolParams[i];
1026 GLfloat *floatParams = (GLfloat*)targetUniform->data + uniformIndex[location].element * count;
1028 for(unsigned int i = 0; i < count; i++)
1030 params[i] = (GLint)floatParams[i];
1035 case GL_UNSIGNED_INT:
1036 memcpy(params, targetUniform->data + uniformIndex[location].element * count * sizeof(GLint),
1037 count * sizeof(GLint));
1039 default: UNREACHABLE(targetUniform->type);
1045 bool Program::getUniformuiv(GLint location, GLsizei *bufSize, GLuint *params)
1047 if(location < 0 || location >= (int)uniformIndex.size() || (uniformIndex[location].index == GL_INVALID_INDEX))
1052 Uniform *targetUniform = uniforms[uniformIndex[location].index];
1053 unsigned int count = UniformComponentCount(targetUniform->type);
1055 // Sized query - ensure the provided buffer is large enough
1056 if(bufSize && static_cast<unsigned int>(*bufSize) < count * sizeof(GLuint))
1061 switch(UniformComponentType(targetUniform->type))
1065 GLboolean *boolParams = targetUniform->data + uniformIndex[location].element * count;
1067 for(unsigned int i = 0; i < count; i++)
1069 params[i] = (GLuint)boolParams[i];
1075 GLfloat *floatParams = (GLfloat*)targetUniform->data + uniformIndex[location].element * count;
1077 for(unsigned int i = 0; i < count; i++)
1079 params[i] = (GLuint)floatParams[i];
1084 case GL_UNSIGNED_INT:
1085 memcpy(params, targetUniform->data + uniformIndex[location].element * count * sizeof(GLuint),
1086 count * sizeof(GLuint));
1088 default: UNREACHABLE(targetUniform->type);
1094 void Program::dirtyAllUniforms()
1096 size_t numUniforms = uniforms.size();
1097 for(size_t index = 0; index < numUniforms; index++)
1099 uniforms[index]->dirty = true;
1103 // Applies all the uniforms set for this program object to the device
1104 void Program::applyUniforms(Device *device)
1106 GLint numUniforms = static_cast<GLint>(uniformIndex.size());
1107 for(GLint location = 0; location < numUniforms; location++)
1109 if((uniformIndex[location].element != 0) || (uniformIndex[location].index == GL_INVALID_INDEX))
1114 Uniform *targetUniform = uniforms[uniformIndex[location].index];
1116 if(targetUniform->dirty && (targetUniform->blockInfo.index == -1))
1118 GLsizei size = targetUniform->size();
1119 GLfloat *f = (GLfloat*)targetUniform->data;
1120 GLint *i = (GLint*)targetUniform->data;
1121 GLuint *ui = (GLuint*)targetUniform->data;
1122 GLboolean *b = (GLboolean*)targetUniform->data;
1124 switch(targetUniform->type)
1126 case GL_BOOL: applyUniform1bv(device, location, size, b); break;
1127 case GL_BOOL_VEC2: applyUniform2bv(device, location, size, b); break;
1128 case GL_BOOL_VEC3: applyUniform3bv(device, location, size, b); break;
1129 case GL_BOOL_VEC4: applyUniform4bv(device, location, size, b); break;
1130 case GL_FLOAT: applyUniform1fv(device, location, size, f); break;
1131 case GL_FLOAT_VEC2: applyUniform2fv(device, location, size, f); break;
1132 case GL_FLOAT_VEC3: applyUniform3fv(device, location, size, f); break;
1133 case GL_FLOAT_VEC4: applyUniform4fv(device, location, size, f); break;
1134 case GL_FLOAT_MAT2: applyUniformMatrix2fv(device, location, size, f); break;
1135 case GL_FLOAT_MAT2x3: applyUniformMatrix2x3fv(device, location, size, f); break;
1136 case GL_FLOAT_MAT2x4: applyUniformMatrix2x4fv(device, location, size, f); break;
1137 case GL_FLOAT_MAT3x2: applyUniformMatrix3x2fv(device, location, size, f); break;
1138 case GL_FLOAT_MAT3: applyUniformMatrix3fv(device, location, size, f); break;
1139 case GL_FLOAT_MAT3x4: applyUniformMatrix3x4fv(device, location, size, f); break;
1140 case GL_FLOAT_MAT4x2: applyUniformMatrix4x2fv(device, location, size, f); break;
1141 case GL_FLOAT_MAT4x3: applyUniformMatrix4x3fv(device, location, size, f); break;
1142 case GL_FLOAT_MAT4: applyUniformMatrix4fv(device, location, size, f); break;
1144 case GL_SAMPLER_CUBE:
1145 case GL_SAMPLER_EXTERNAL_OES:
1146 case GL_SAMPLER_3D_OES:
1147 case GL_SAMPLER_2D_ARRAY:
1148 case GL_SAMPLER_2D_SHADOW:
1149 case GL_SAMPLER_CUBE_SHADOW:
1150 case GL_SAMPLER_2D_ARRAY_SHADOW:
1151 case GL_INT_SAMPLER_2D:
1152 case GL_UNSIGNED_INT_SAMPLER_2D:
1153 case GL_INT_SAMPLER_CUBE:
1154 case GL_UNSIGNED_INT_SAMPLER_CUBE:
1155 case GL_INT_SAMPLER_3D:
1156 case GL_UNSIGNED_INT_SAMPLER_3D:
1157 case GL_INT_SAMPLER_2D_ARRAY:
1158 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
1159 case GL_INT: applyUniform1iv(device, location, size, i); break;
1160 case GL_INT_VEC2: applyUniform2iv(device, location, size, i); break;
1161 case GL_INT_VEC3: applyUniform3iv(device, location, size, i); break;
1162 case GL_INT_VEC4: applyUniform4iv(device, location, size, i); break;
1163 case GL_UNSIGNED_INT: applyUniform1uiv(device, location, size, ui); break;
1164 case GL_UNSIGNED_INT_VEC2: applyUniform2uiv(device, location, size, ui); break;
1165 case GL_UNSIGNED_INT_VEC3: applyUniform3uiv(device, location, size, ui); break;
1166 case GL_UNSIGNED_INT_VEC4: applyUniform4uiv(device, location, size, ui); break;
1168 UNREACHABLE(targetUniform->type);
1171 targetUniform->dirty = false;
1176 void Program::applyUniformBuffers(Device *device, BufferBinding* uniformBuffers)
1178 GLint vertexUniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS];
1179 GLint fragmentUniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS];
1181 for(unsigned int bufferBindingIndex = 0; bufferBindingIndex < MAX_UNIFORM_BUFFER_BINDINGS; bufferBindingIndex++)
1183 vertexUniformBuffers[bufferBindingIndex] = -1;
1186 for(unsigned int bufferBindingIndex = 0; bufferBindingIndex < MAX_UNIFORM_BUFFER_BINDINGS; bufferBindingIndex++)
1188 fragmentUniformBuffers[bufferBindingIndex] = -1;
1191 int vertexUniformBufferIndex = 0;
1192 int fragmentUniformBufferIndex = 0;
1193 for(unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlocks.size(); uniformBlockIndex++)
1195 UniformBlock &uniformBlock = *uniformBlocks[uniformBlockIndex];
1197 // Unnecessary to apply an unreferenced standard or shared UBO
1198 if(!uniformBlock.isReferencedByVertexShader() && !uniformBlock.isReferencedByFragmentShader())
1203 GLuint blockBinding = uniformBlockBindings[uniformBlockIndex];
1205 if(uniformBlock.isReferencedByVertexShader())
1207 vertexUniformBuffers[vertexUniformBufferIndex++] = blockBinding;
1210 if(uniformBlock.isReferencedByFragmentShader())
1212 fragmentUniformBuffers[fragmentUniformBufferIndex++] = blockBinding;
1216 for(unsigned int bufferBindingIndex = 0; bufferBindingIndex < MAX_UNIFORM_BUFFER_BINDINGS; bufferBindingIndex++)
1218 int index = vertexUniformBuffers[bufferBindingIndex];
1219 Buffer* vsBuffer = (index != -1) ? (Buffer*)uniformBuffers[index].get() : nullptr;
1220 device->VertexProcessor::setUniformBuffer(bufferBindingIndex,
1221 vsBuffer ? vsBuffer->getResource() : nullptr, (index != -1) ? uniformBuffers[index].getOffset() : 0);
1222 index = fragmentUniformBuffers[bufferBindingIndex];
1223 Buffer* psBuffer = (index != -1) ? (Buffer*)uniformBuffers[index].get() : nullptr;
1224 device->PixelProcessor::setUniformBuffer(bufferBindingIndex,
1225 psBuffer ? psBuffer->getResource() : nullptr, (index != -1) ? uniformBuffers[index].getOffset() : 0);
1229 void Program::applyTransformFeedback(Device *device, TransformFeedback* transformFeedback)
1231 // Make sure the flags will fit in a 64 bit unsigned int variable
1232 ASSERT(sw::max<int>(MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, sw::MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS) <= 64);
1234 BufferBinding* transformFeedbackBuffers = (transformFeedback && transformFeedback->isActive() && !transformFeedback->isPaused()) ? transformFeedback->getBuffers() : nullptr;
1236 uint64_t enableTransformFeedback = 0;
1237 if(!transformFeedbackBuffers)
1239 for(unsigned int index = 0; index < sw::MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS; ++index)
1241 device->VertexProcessor::setTransformFeedbackBuffer(index, nullptr, 0, 0, 0, 0, 0);
1243 device->VertexProcessor::enableTransformFeedback(enableTransformFeedback);
1247 unsigned int maxVaryings = static_cast<unsigned int>(transformFeedbackLinkedVaryings.size());
1248 switch(transformFeedbackBufferMode)
1250 case GL_SEPARATE_ATTRIBS:
1252 maxVaryings = sw::min(maxVaryings, (unsigned int)MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS);
1253 // Attribs go to separate buffers
1254 for(unsigned int index = 0; index < maxVaryings; ++index)
1256 int size = transformFeedbackLinkedVaryings[index].size;
1257 int rowCount = VariableRowCount(transformFeedbackLinkedVaryings[index].type);
1258 int colCount = VariableColumnCount(transformFeedbackLinkedVaryings[index].type);
1259 int nbRegs = rowCount > 1 ? colCount * size : size;
1260 int nbComponentsPerReg = rowCount > 1 ? rowCount : colCount;
1261 int componentStride = rowCount * colCount * size;
1262 int baseOffset = transformFeedback->vertexOffset() * componentStride * sizeof(float);
1263 device->VertexProcessor::setTransformFeedbackBuffer(index,
1264 transformFeedbackBuffers[index].get()->getResource(),
1265 transformFeedbackBuffers[index].getOffset() + baseOffset,
1266 transformFeedbackLinkedVaryings[index].reg * 4 + transformFeedbackLinkedVaryings[index].col,
1267 nbRegs, nbComponentsPerReg, componentStride);
1268 enableTransformFeedback |= 1ULL << index;
1272 case GL_INTERLEAVED_ATTRIBS:
1274 // OpenGL ES 3.0.4 spec, section 2.15.2:
1275 // In INTERLEAVED_ATTRIBS mode, the values of one or more output variables
1276 // written by a vertex shader are written, interleaved, into the buffer object
1277 // bound to the first transform feedback binding point (index = 0).
1278 sw::Resource* resource = transformFeedbackBuffers[0].get()->getResource();
1279 int componentStride = static_cast<int>(totalLinkedVaryingsComponents);
1280 int baseOffset = transformFeedbackBuffers[0].getOffset() + (transformFeedback->vertexOffset() * componentStride * sizeof(float));
1281 maxVaryings = sw::min(maxVaryings, (unsigned int)sw::MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS);
1282 int totalComponents = 0;
1283 for(unsigned int index = 0; index < maxVaryings; ++index)
1285 int size = transformFeedbackLinkedVaryings[index].size;
1286 int rowCount = VariableRowCount(transformFeedbackLinkedVaryings[index].type);
1287 int colCount = VariableColumnCount(transformFeedbackLinkedVaryings[index].type);
1288 int nbRegs = rowCount > 1 ? colCount * size : size;
1289 int nbComponentsPerReg = rowCount > 1 ? rowCount : colCount;
1290 device->VertexProcessor::setTransformFeedbackBuffer(index, resource,
1291 baseOffset + (totalComponents * sizeof(float)),
1292 transformFeedbackLinkedVaryings[index].reg * 4 + transformFeedbackLinkedVaryings[index].col,
1293 nbRegs, nbComponentsPerReg, componentStride);
1294 totalComponents += rowCount * colCount * size;
1295 enableTransformFeedback |= 1ULL << index;
1300 UNREACHABLE(transformFeedbackBufferMode);
1304 // Unset all other transform feedback buffers
1305 for(unsigned int index = maxVaryings; index < sw::MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS; ++index)
1307 device->VertexProcessor::setTransformFeedbackBuffer(index, nullptr, 0, 0, 0, 0, 0);
1310 device->VertexProcessor::enableTransformFeedback(enableTransformFeedback);
1313 bool Program::linkVaryings()
1315 glsl::VaryingList &psVaryings = fragmentShader->varyings;
1316 glsl::VaryingList &vsVaryings = vertexShader->varyings;
1318 for(auto const &input : psVaryings)
1320 bool matched = false;
1322 for(auto const &output : vsVaryings)
1324 if(output.name == input.name)
1326 if(output.type != input.type || output.size() != input.size())
1328 appendToInfoLog("Type of vertex varying %s does not match that of the fragment varying", output.name.c_str());
1340 appendToInfoLog("Fragment varying %s does not match any vertex varying", input.name.c_str());
1346 for(auto const &output : vsVaryings)
1348 bool matched = false;
1350 for(auto const &input : psVaryings)
1352 if(output.name == input.name)
1355 int out = output.reg;
1356 int components = VariableRegisterSize(output.type);
1357 int registers = VariableRegisterCount(output.type) * output.size();
1361 if(in + registers > MAX_VARYING_VECTORS)
1363 appendToInfoLog("Too many varyings");
1369 if(out + registers > MAX_VARYING_VECTORS)
1371 appendToInfoLog("Too many varyings");
1375 for(int i = 0; i < registers; i++)
1377 vertexBinary->setOutput(out + i, components, sw::Shader::Semantic(sw::Shader::USAGE_COLOR, in + i, pixelBinary->getInput(in + i, 0).flat));
1380 else // Vertex varying is declared but not written to
1382 for(int i = 0; i < registers; i++)
1384 pixelBinary->setInput(in + i, components, sw::Shader::Semantic());
1395 // For openGL ES 3.0, we need to still add the vertex shader outputs for unmatched varyings, for transform feedback.
1396 for(const std::string &indexedTfVaryingName : transformFeedbackVaryings)
1398 std::string tfVaryingName = es2::ParseUniformName(indexedTfVaryingName, nullptr);
1400 if(tfVaryingName == output.name)
1402 int out = output.reg;
1403 int components = VariableRegisterSize(output.type);
1404 int registers = VariableRegisterCount(output.type) * output.size();
1408 if(out + registers > MAX_VARYING_VECTORS)
1410 appendToInfoLog("Too many varyings");
1414 for(int i = 0; i < registers; i++)
1416 vertexBinary->setOutput(out + i, components, sw::Shader::Semantic(sw::Shader::USAGE_COLOR));
1428 bool Program::linkTransformFeedback()
1430 size_t totalComponents = 0;
1431 totalLinkedVaryingsComponents = 0;
1433 std::set<std::string> uniqueNames;
1435 for(const std::string &indexedTfVaryingName : transformFeedbackVaryings)
1437 unsigned int subscript = GL_INVALID_INDEX;
1438 std::string tfVaryingName = es2::ParseUniformName(indexedTfVaryingName, &subscript);
1439 bool hasSubscript = (subscript != GL_INVALID_INDEX);
1441 if(tfVaryingName.find('[') != std::string::npos)
1443 appendToInfoLog("Capture of array sub-elements is undefined and not supported.");
1448 for(const glsl::Varying varying : vertexShader->varyings)
1450 if(tfVaryingName == varying.name)
1452 if(uniqueNames.count(indexedTfVaryingName) > 0)
1454 appendToInfoLog("Two transform feedback varyings specify the same output variable (%s)", indexedTfVaryingName.c_str());
1457 uniqueNames.insert(indexedTfVaryingName);
1459 if(hasSubscript && ((static_cast<int>(subscript)) >= varying.size()))
1461 appendToInfoLog("Specified transform feedback varying index out of bounds (%s)", indexedTfVaryingName.c_str());
1465 int size = hasSubscript ? 1 : varying.size();
1467 int rowCount = VariableRowCount(varying.type);
1468 int colCount = VariableColumnCount(varying.type);
1469 int componentCount = rowCount * colCount * size;
1470 if(transformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
1471 componentCount > sw::MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS)
1473 appendToInfoLog("Transform feedback varying's %s components (%d) exceed the maximum separate components (%d).",
1474 varying.name.c_str(), componentCount, sw::MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS);
1478 totalComponents += componentCount;
1480 int reg = varying.reg;
1483 reg += rowCount > 1 ? colCount * subscript : subscript;
1485 int col = varying.col;
1486 if(tfVaryingName == "gl_PointSize")
1488 // Point size is stored in the y element of the vector, not the x element
1489 col = 1; // FIXME: varying.col could already contain this information
1491 transformFeedbackLinkedVaryings.push_back(LinkedVarying(varying.name, varying.type, size, reg, col));
1500 appendToInfoLog("Transform feedback varying %s does not exist in the vertex shader.", tfVaryingName.c_str());
1505 if(transformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
1506 totalComponents > sw::MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS)
1508 appendToInfoLog("Transform feedback varying total components (%d) exceed the maximum separate components (%d).",
1509 totalComponents, sw::MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS);
1513 totalLinkedVaryingsComponents = totalComponents;
1518 // Links the code of the vertex and pixel shader by matching up their varyings,
1519 // compiling them into binaries, determining the attribute mappings, and collecting
1520 // a list of uniforms
1521 void Program::link()
1525 resetUniformBlockBindings();
1527 if(!fragmentShader || !fragmentShader->isCompiled())
1532 if(!vertexShader || !vertexShader->isCompiled())
1537 vertexBinary = new sw::VertexShader(vertexShader->getVertexShader());
1538 pixelBinary = new sw::PixelShader(fragmentShader->getPixelShader());
1545 if(!linkAttributes())
1550 // Link uniform blocks before uniforms to make it easy to assign block indices to fields
1551 if(!linkUniformBlocks(vertexShader, fragmentShader))
1556 if(!linkUniforms(fragmentShader))
1561 if(!linkUniforms(vertexShader))
1566 if(!linkTransformFeedback())
1571 linked = true; // Success
1574 // Determines the mapping between GL attributes and vertex stream usage indices
1575 bool Program::linkAttributes()
1577 unsigned int usedLocations = 0;
1579 // Link attributes that have a binding location
1580 for(auto const &attribute : vertexShader->activeAttributes)
1582 int location = (attributeBinding.find(attribute.name) != attributeBinding.end()) ? attributeBinding[attribute.name] : -1;
1584 if(location != -1) // Set by glBindAttribLocation
1586 int rows = VariableRegisterCount(attribute.type);
1588 if(rows + location > MAX_VERTEX_ATTRIBS)
1590 appendToInfoLog("Active attribute (%s) at location %d is too big to fit", attribute.name.c_str(), location);
1594 // In GLSL 3.00, attribute aliasing produces a link error
1595 // In GLSL 1.00, attribute aliasing is allowed
1596 if(vertexShader->getShaderVersion() >= 300)
1598 for(auto const &it : linkedAttribute)
1600 int itLocStart = getAttributeBinding(it);
1601 ASSERT(itLocStart >= 0);
1602 int itLocEnd = itLocStart + VariableRegisterCount(it.type);
1603 for(int i = 0; i < rows; i++)
1605 int loc = location + i;
1606 if((loc >= itLocStart) && (loc < itLocEnd))
1608 appendToInfoLog("Attribute '%s' aliases attribute '%s' at location %d", attribute.name.c_str(), it.name.c_str(), location);
1615 linkedAttributeLocation[attribute.name] = location;
1616 linkedAttribute.push_back(attribute);
1617 for(int i = 0; i < rows; i++)
1619 usedLocations |= 1 << (location + i);
1624 // Link attributes that don't have a binding location
1625 for(auto const &attribute : vertexShader->activeAttributes)
1627 int location = (attributeBinding.find(attribute.name) != attributeBinding.end()) ? attributeBinding[attribute.name] : -1;
1629 if(location == -1) // Not set by glBindAttribLocation
1631 int rows = VariableRegisterCount(attribute.type);
1632 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1634 if(availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1636 appendToInfoLog("Too many active attributes (%s)", attribute.name.c_str());
1637 return false; // Fail to link
1640 linkedAttributeLocation[attribute.name] = availableIndex;
1641 linkedAttribute.push_back(attribute);
1645 for(auto const &it : linkedAttribute)
1647 int location = getAttributeBinding(it);
1648 ASSERT(location >= 0);
1649 int index = vertexShader->getSemanticIndex(it.name);
1650 int rows = std::max(VariableRegisterCount(it.type), 1);
1652 for(int r = 0; r < rows; r++)
1654 attributeStream[r + location] = index++;
1661 int Program::getAttributeBinding(const glsl::Attribute &attribute)
1663 if(attribute.location != -1)
1665 return attribute.location;
1668 std::unordered_map<std::string, GLuint>::const_iterator it = linkedAttributeLocation.find(attribute.name);
1669 if(it != linkedAttributeLocation.end())
1677 bool Program::linkUniforms(const Shader *shader)
1679 const glsl::ActiveUniforms &activeUniforms = shader->activeUniforms;
1681 for(unsigned int uniformIndex = 0; uniformIndex < activeUniforms.size(); uniformIndex++)
1683 const glsl::Uniform &uniform = activeUniforms[uniformIndex];
1685 unsigned int blockIndex = GL_INVALID_INDEX;
1686 if(uniform.blockId >= 0)
1688 const glsl::ActiveUniformBlocks &activeUniformBlocks = shader->activeUniformBlocks;
1689 ASSERT(static_cast<size_t>(uniform.blockId) < activeUniformBlocks.size());
1690 blockIndex = getUniformBlockIndex(activeUniformBlocks[uniform.blockId].name);
1691 ASSERT(blockIndex != GL_INVALID_INDEX);
1693 if(!defineUniform(shader->getType(), uniform.type, uniform.precision, uniform.name, uniform.arraySize, uniform.registerIndex, Uniform::BlockInfo(uniform, blockIndex)))
1702 bool Program::defineUniform(GLenum shader, GLenum type, GLenum precision, const std::string &name, unsigned int arraySize, int registerIndex, const Uniform::BlockInfo& blockInfo)
1704 if(IsSamplerUniform(type))
1706 int index = registerIndex;
1710 if(shader == GL_VERTEX_SHADER)
1712 if(index < MAX_VERTEX_TEXTURE_IMAGE_UNITS)
1714 samplersVS[index].active = true;
1718 default: UNREACHABLE(type);
1719 case GL_INT_SAMPLER_2D:
1720 case GL_UNSIGNED_INT_SAMPLER_2D:
1721 case GL_SAMPLER_2D_SHADOW:
1722 case GL_SAMPLER_2D: samplersVS[index].textureType = TEXTURE_2D; break;
1723 case GL_INT_SAMPLER_CUBE:
1724 case GL_UNSIGNED_INT_SAMPLER_CUBE:
1725 case GL_SAMPLER_CUBE_SHADOW:
1726 case GL_SAMPLER_CUBE: samplersVS[index].textureType = TEXTURE_CUBE; break;
1727 case GL_INT_SAMPLER_3D:
1728 case GL_UNSIGNED_INT_SAMPLER_3D:
1729 case GL_SAMPLER_3D_OES: samplersVS[index].textureType = TEXTURE_3D; break;
1730 case GL_SAMPLER_EXTERNAL_OES: samplersVS[index].textureType = TEXTURE_EXTERNAL; break;
1731 case GL_INT_SAMPLER_2D_ARRAY:
1732 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
1733 case GL_SAMPLER_2D_ARRAY_SHADOW:
1734 case GL_SAMPLER_2D_ARRAY: samplersVS[index].textureType = TEXTURE_2D_ARRAY; break;
1737 samplersVS[index].logicalTextureUnit = 0;
1741 appendToInfoLog("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1745 else if(shader == GL_FRAGMENT_SHADER)
1747 if(index < MAX_TEXTURE_IMAGE_UNITS)
1749 samplersPS[index].active = true;
1753 default: UNREACHABLE(type);
1754 case GL_INT_SAMPLER_2D:
1755 case GL_UNSIGNED_INT_SAMPLER_2D:
1756 case GL_SAMPLER_2D_SHADOW:
1757 case GL_SAMPLER_2D: samplersPS[index].textureType = TEXTURE_2D; break;
1758 case GL_INT_SAMPLER_CUBE:
1759 case GL_UNSIGNED_INT_SAMPLER_CUBE:
1760 case GL_SAMPLER_CUBE_SHADOW:
1761 case GL_SAMPLER_CUBE: samplersPS[index].textureType = TEXTURE_CUBE; break;
1762 case GL_INT_SAMPLER_3D:
1763 case GL_UNSIGNED_INT_SAMPLER_3D:
1764 case GL_SAMPLER_3D_OES: samplersPS[index].textureType = TEXTURE_3D; break;
1765 case GL_SAMPLER_EXTERNAL_OES: samplersPS[index].textureType = TEXTURE_EXTERNAL; break;
1766 case GL_INT_SAMPLER_2D_ARRAY:
1767 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
1768 case GL_SAMPLER_2D_ARRAY_SHADOW:
1769 case GL_SAMPLER_2D_ARRAY: samplersPS[index].textureType = TEXTURE_2D_ARRAY; break;
1772 samplersPS[index].logicalTextureUnit = 0;
1776 appendToInfoLog("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
1780 else UNREACHABLE(shader);
1784 while(index < registerIndex + static_cast<int>(arraySize));
1787 Uniform *uniform = 0;
1788 GLint location = getUniformLocation(name);
1790 if(location >= 0) // Previously defined, types must match
1792 uniform = uniforms[uniformIndex[location].index];
1794 if(uniform->type != type)
1796 appendToInfoLog("Types for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str());
1800 if(uniform->precision != precision)
1802 appendToInfoLog("Precisions for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str());
1808 uniform = new Uniform(type, precision, name, arraySize, blockInfo);
1816 if(shader == GL_VERTEX_SHADER)
1818 uniform->vsRegisterIndex = registerIndex;
1820 else if(shader == GL_FRAGMENT_SHADER)
1822 uniform->psRegisterIndex = registerIndex;
1824 else UNREACHABLE(shader);
1826 if(!isUniformDefined(name))
1828 uniforms.push_back(uniform);
1829 unsigned int index = (blockInfo.index == -1) ? static_cast<unsigned int>(uniforms.size() - 1) : GL_INVALID_INDEX;
1831 for(int i = 0; i < uniform->size(); i++)
1833 uniformIndex.push_back(UniformLocation(name, i, index));
1837 if(shader == GL_VERTEX_SHADER)
1839 if(registerIndex + uniform->registerCount() > MAX_VERTEX_UNIFORM_VECTORS)
1841 appendToInfoLog("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%d)", MAX_VERTEX_UNIFORM_VECTORS);
1845 else if(shader == GL_FRAGMENT_SHADER)
1847 if(registerIndex + uniform->registerCount() > MAX_FRAGMENT_UNIFORM_VECTORS)
1849 appendToInfoLog("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%d)", MAX_FRAGMENT_UNIFORM_VECTORS);
1853 else UNREACHABLE(shader);
1858 bool Program::areMatchingUniformBlocks(const glsl::UniformBlock &block1, const glsl::UniformBlock &block2, const Shader *shader1, const Shader *shader2)
1860 // validate blocks for the same member types
1861 if(block1.fields.size() != block2.fields.size())
1865 if(block1.arraySize != block2.arraySize)
1869 if(block1.layout != block2.layout || block1.isRowMajorLayout != block2.isRowMajorLayout)
1873 const size_t numBlockMembers = block1.fields.size();
1874 for(size_t blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
1876 const glsl::Uniform& member1 = shader1->activeUniforms[block1.fields[blockMemberIndex]];
1877 const glsl::Uniform& member2 = shader2->activeUniforms[block2.fields[blockMemberIndex]];
1878 if(member1.name != member2.name ||
1879 member1.arraySize != member2.arraySize ||
1880 member1.precision != member2.precision ||
1881 member1.type != member2.type)
1889 bool Program::linkUniformBlocks(const Shader *vertexShader, const Shader *fragmentShader)
1891 const glsl::ActiveUniformBlocks &vertexUniformBlocks = vertexShader->activeUniformBlocks;
1892 const glsl::ActiveUniformBlocks &fragmentUniformBlocks = fragmentShader->activeUniformBlocks;
1893 // Check that interface blocks defined in the vertex and fragment shaders are identical
1894 typedef std::map<std::string, const glsl::UniformBlock*> UniformBlockMap;
1895 UniformBlockMap linkedUniformBlocks;
1896 for(unsigned int blockIndex = 0; blockIndex < vertexUniformBlocks.size(); blockIndex++)
1898 const glsl::UniformBlock &vertexUniformBlock = vertexUniformBlocks[blockIndex];
1899 linkedUniformBlocks[vertexUniformBlock.name] = &vertexUniformBlock;
1901 for(unsigned int blockIndex = 0; blockIndex < fragmentUniformBlocks.size(); blockIndex++)
1903 const glsl::UniformBlock &fragmentUniformBlock = fragmentUniformBlocks[blockIndex];
1904 UniformBlockMap::const_iterator entry = linkedUniformBlocks.find(fragmentUniformBlock.name);
1905 if(entry != linkedUniformBlocks.end())
1907 const glsl::UniformBlock &vertexUniformBlock = *entry->second;
1908 if(!areMatchingUniformBlocks(vertexUniformBlock, fragmentUniformBlock, vertexShader, fragmentShader))
1914 for(unsigned int blockIndex = 0; blockIndex < vertexUniformBlocks.size(); blockIndex++)
1916 const glsl::UniformBlock &uniformBlock = vertexUniformBlocks[blockIndex];
1917 if(!defineUniformBlock(vertexShader, uniformBlock))
1922 for(unsigned int blockIndex = 0; blockIndex < fragmentUniformBlocks.size(); blockIndex++)
1924 const glsl::UniformBlock &uniformBlock = fragmentUniformBlocks[blockIndex];
1925 if(!defineUniformBlock(fragmentShader, uniformBlock))
1933 bool Program::defineUniformBlock(const Shader *shader, const glsl::UniformBlock &block)
1935 GLuint blockIndex = getUniformBlockIndex(block.name);
1937 if(blockIndex == GL_INVALID_INDEX)
1939 const std::vector<int>& fields = block.fields;
1940 std::vector<unsigned int> memberUniformIndexes;
1941 for(size_t i = 0; i < fields.size(); ++i)
1943 memberUniformIndexes.push_back(fields[i]);
1946 if(block.arraySize > 0)
1948 int regIndex = block.registerIndex;
1949 int regInc = block.dataSize / (glsl::BlockLayoutEncoder::BytesPerComponent * glsl::BlockLayoutEncoder::ComponentsPerRegister);
1950 for(unsigned int i = 0; i < block.arraySize; ++i, regIndex += regInc)
1952 uniformBlocks.push_back(new UniformBlock(block.name, i, block.dataSize, memberUniformIndexes));
1953 uniformBlocks[uniformBlocks.size() - 1]->setRegisterIndex(shader->getType(), regIndex);
1958 uniformBlocks.push_back(new UniformBlock(block.name, GL_INVALID_INDEX, block.dataSize, memberUniformIndexes));
1959 uniformBlocks[uniformBlocks.size() - 1]->setRegisterIndex(shader->getType(), block.registerIndex);
1964 int regIndex = block.registerIndex;
1965 int regInc = block.dataSize / (glsl::BlockLayoutEncoder::BytesPerComponent * glsl::BlockLayoutEncoder::ComponentsPerRegister);
1966 int nbBlocks = (block.arraySize > 0) ? block.arraySize : 1;
1967 for(int i = 0; i < nbBlocks; ++i, regIndex += regInc)
1969 uniformBlocks[blockIndex + i]->setRegisterIndex(shader->getType(), regIndex);
1976 bool Program::applyUniform(Device *device, GLint location, float* data)
1978 Uniform *targetUniform = uniforms[uniformIndex[location].index];
1980 if(targetUniform->psRegisterIndex != -1)
1982 device->setPixelShaderConstantF(targetUniform->psRegisterIndex, data, targetUniform->registerCount());
1985 if(targetUniform->vsRegisterIndex != -1)
1987 device->setVertexShaderConstantF(targetUniform->vsRegisterIndex, data, targetUniform->registerCount());
1993 bool Program::applyUniform1bv(Device *device, GLint location, GLsizei count, const GLboolean *v)
1995 int vector[MAX_UNIFORM_VECTORS][4];
1997 for(int i = 0; i < count; i++)
1999 vector[i][0] = (v[0] == GL_FALSE ? 0x00000000 : 0xFFFFFFFF);
2007 return applyUniform(device, location, (float*)vector);
2010 bool Program::applyUniform2bv(Device *device, GLint location, GLsizei count, const GLboolean *v)
2012 int vector[MAX_UNIFORM_VECTORS][4];
2014 for(int i = 0; i < count; i++)
2016 vector[i][0] = (v[0] == GL_FALSE ? 0x00000000 : 0xFFFFFFFF);
2017 vector[i][1] = (v[1] == GL_FALSE ? 0x00000000 : 0xFFFFFFFF);
2024 return applyUniform(device, location, (float*)vector);
2027 bool Program::applyUniform3bv(Device *device, GLint location, GLsizei count, const GLboolean *v)
2029 int vector[MAX_UNIFORM_VECTORS][4];
2031 for(int i = 0; i < count; i++)
2033 vector[i][0] = (v[0] == GL_FALSE ? 0x00000000 : 0xFFFFFFFF);
2034 vector[i][1] = (v[1] == GL_FALSE ? 0x00000000 : 0xFFFFFFFF);
2035 vector[i][2] = (v[2] == GL_FALSE ? 0x00000000 : 0xFFFFFFFF);
2041 return applyUniform(device, location, (float*)vector);
2044 bool Program::applyUniform4bv(Device *device, GLint location, GLsizei count, const GLboolean *v)
2046 int vector[MAX_UNIFORM_VECTORS][4];
2048 for(int i = 0; i < count; i++)
2050 vector[i][0] = (v[0] == GL_FALSE ? 0x00000000 : 0xFFFFFFFF);
2051 vector[i][1] = (v[1] == GL_FALSE ? 0x00000000 : 0xFFFFFFFF);
2052 vector[i][2] = (v[2] == GL_FALSE ? 0x00000000 : 0xFFFFFFFF);
2053 vector[i][3] = (v[3] == GL_FALSE ? 0x00000000 : 0xFFFFFFFF);
2058 return applyUniform(device, location, (float*)vector);
2061 bool Program::applyUniform1fv(Device *device, GLint location, GLsizei count, const GLfloat *v)
2063 float vector[MAX_UNIFORM_VECTORS][4];
2065 for(int i = 0; i < count; i++)
2067 vector[i][0] = v[0];
2075 return applyUniform(device, location, (float*)vector);
2078 bool Program::applyUniform2fv(Device *device, GLint location, GLsizei count, const GLfloat *v)
2080 float vector[MAX_UNIFORM_VECTORS][4];
2082 for(int i = 0; i < count; i++)
2084 vector[i][0] = v[0];
2085 vector[i][1] = v[1];
2092 return applyUniform(device, location, (float*)vector);
2095 bool Program::applyUniform3fv(Device *device, GLint location, GLsizei count, const GLfloat *v)
2097 float vector[MAX_UNIFORM_VECTORS][4];
2099 for(int i = 0; i < count; i++)
2101 vector[i][0] = v[0];
2102 vector[i][1] = v[1];
2103 vector[i][2] = v[2];
2109 return applyUniform(device, location, (float*)vector);
2112 bool Program::applyUniform4fv(Device *device, GLint location, GLsizei count, const GLfloat *v)
2114 return applyUniform(device, location, (float*)v);
2117 bool Program::applyUniformMatrix2fv(Device *device, GLint location, GLsizei count, const GLfloat *value)
2119 float matrix[(MAX_UNIFORM_VECTORS + 1) / 2][2][4];
2121 for(int i = 0; i < count; i++)
2123 matrix[i][0][0] = value[0]; matrix[i][0][1] = value[1]; matrix[i][0][2] = 0; matrix[i][0][3] = 0;
2124 matrix[i][1][0] = value[2]; matrix[i][1][1] = value[3]; matrix[i][1][2] = 0; matrix[i][1][3] = 0;
2129 return applyUniform(device, location, (float*)matrix);
2132 bool Program::applyUniformMatrix2x3fv(Device *device, GLint location, GLsizei count, const GLfloat *value)
2134 float matrix[(MAX_UNIFORM_VECTORS + 1) / 2][2][4];
2136 for(int i = 0; i < count; i++)
2138 matrix[i][0][0] = value[0]; matrix[i][0][1] = value[1]; matrix[i][0][2] = value[2]; matrix[i][0][3] = 0;
2139 matrix[i][1][0] = value[3]; matrix[i][1][1] = value[4]; matrix[i][1][2] = value[5]; matrix[i][1][3] = 0;
2144 return applyUniform(device, location, (float*)matrix);
2147 bool Program::applyUniformMatrix2x4fv(Device *device, GLint location, GLsizei count, const GLfloat *value)
2149 float matrix[(MAX_UNIFORM_VECTORS + 1) / 2][2][4];
2151 for(int i = 0; i < count; i++)
2153 matrix[i][0][0] = value[0]; matrix[i][0][1] = value[1]; matrix[i][0][2] = value[2]; matrix[i][0][3] = value[3];
2154 matrix[i][1][0] = value[4]; matrix[i][1][1] = value[5]; matrix[i][1][2] = value[6]; matrix[i][1][3] = value[7];
2159 return applyUniform(device, location, (float*)matrix);
2162 bool Program::applyUniformMatrix3fv(Device *device, GLint location, GLsizei count, const GLfloat *value)
2164 float matrix[(MAX_UNIFORM_VECTORS + 2) / 3][3][4];
2166 for(int i = 0; i < count; i++)
2168 matrix[i][0][0] = value[0]; matrix[i][0][1] = value[1]; matrix[i][0][2] = value[2]; matrix[i][0][3] = 0;
2169 matrix[i][1][0] = value[3]; matrix[i][1][1] = value[4]; matrix[i][1][2] = value[5]; matrix[i][1][3] = 0;
2170 matrix[i][2][0] = value[6]; matrix[i][2][1] = value[7]; matrix[i][2][2] = value[8]; matrix[i][2][3] = 0;
2175 return applyUniform(device, location, (float*)matrix);
2178 bool Program::applyUniformMatrix3x2fv(Device *device, GLint location, GLsizei count, const GLfloat *value)
2180 float matrix[(MAX_UNIFORM_VECTORS + 2) / 3][3][4];
2182 for(int i = 0; i < count; i++)
2184 matrix[i][0][0] = value[0]; matrix[i][0][1] = value[1]; matrix[i][0][2] = 0; matrix[i][0][3] = 0;
2185 matrix[i][1][0] = value[2]; matrix[i][1][1] = value[3]; matrix[i][1][2] = 0; matrix[i][1][3] = 0;
2186 matrix[i][2][0] = value[4]; matrix[i][2][1] = value[5]; matrix[i][2][2] = 0; matrix[i][2][3] = 0;
2191 return applyUniform(device, location, (float*)matrix);
2194 bool Program::applyUniformMatrix3x4fv(Device *device, GLint location, GLsizei count, const GLfloat *value)
2196 float matrix[(MAX_UNIFORM_VECTORS + 2) / 3][3][4];
2198 for(int i = 0; i < count; i++)
2200 matrix[i][0][0] = value[0]; matrix[i][0][1] = value[1]; matrix[i][0][2] = value[2]; matrix[i][0][3] = value[3];
2201 matrix[i][1][0] = value[4]; matrix[i][1][1] = value[5]; matrix[i][1][2] = value[6]; matrix[i][1][3] = value[7];
2202 matrix[i][2][0] = value[8]; matrix[i][2][1] = value[9]; matrix[i][2][2] = value[10]; matrix[i][2][3] = value[11];
2207 return applyUniform(device, location, (float*)matrix);
2210 bool Program::applyUniformMatrix4fv(Device *device, GLint location, GLsizei count, const GLfloat *value)
2212 return applyUniform(device, location, (float*)value);
2215 bool Program::applyUniformMatrix4x2fv(Device *device, GLint location, GLsizei count, const GLfloat *value)
2217 float matrix[(MAX_UNIFORM_VECTORS + 3) / 4][4][4];
2219 for(int i = 0; i < count; i++)
2221 matrix[i][0][0] = value[0]; matrix[i][0][1] = value[1]; matrix[i][0][2] = 0; matrix[i][0][3] = 0;
2222 matrix[i][1][0] = value[2]; matrix[i][1][1] = value[3]; matrix[i][1][2] = 0; matrix[i][1][3] = 0;
2223 matrix[i][2][0] = value[4]; matrix[i][2][1] = value[5]; matrix[i][2][2] = 0; matrix[i][2][3] = 0;
2224 matrix[i][3][0] = value[6]; matrix[i][3][1] = value[7]; matrix[i][3][2] = 0; matrix[i][3][3] = 0;
2229 return applyUniform(device, location, (float*)matrix);
2232 bool Program::applyUniformMatrix4x3fv(Device *device, GLint location, GLsizei count, const GLfloat *value)
2234 float matrix[(MAX_UNIFORM_VECTORS + 3) / 4][4][4];
2236 for(int i = 0; i < count; i++)
2238 matrix[i][0][0] = value[0]; matrix[i][0][1] = value[1]; matrix[i][0][2] = value[2]; matrix[i][0][3] = 0;
2239 matrix[i][1][0] = value[3]; matrix[i][1][1] = value[4]; matrix[i][1][2] = value[5]; matrix[i][1][3] = 0;
2240 matrix[i][2][0] = value[6]; matrix[i][2][1] = value[7]; matrix[i][2][2] = value[8]; matrix[i][2][3] = 0;
2241 matrix[i][3][0] = value[9]; matrix[i][3][1] = value[10]; matrix[i][3][2] = value[11]; matrix[i][3][3] = 0;
2246 return applyUniform(device, location, (float*)matrix);
2249 bool Program::applyUniform1iv(Device *device, GLint location, GLsizei count, const GLint *v)
2251 GLint vector[MAX_UNIFORM_VECTORS][4];
2253 for(int i = 0; i < count; i++)
2255 vector[i][0] = v[i];
2261 Uniform *targetUniform = uniforms[uniformIndex[location].index];
2262 if(IsSamplerUniform(targetUniform->type))
2264 if(targetUniform->psRegisterIndex != -1)
2266 for(int i = 0; i < count; i++)
2268 unsigned int samplerIndex = targetUniform->psRegisterIndex + i;
2270 if(samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2272 ASSERT(samplersPS[samplerIndex].active);
2273 samplersPS[samplerIndex].logicalTextureUnit = v[i];
2278 if(targetUniform->vsRegisterIndex != -1)
2280 for(int i = 0; i < count; i++)
2282 unsigned int samplerIndex = targetUniform->vsRegisterIndex + i;
2284 if(samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS)
2286 ASSERT(samplersVS[samplerIndex].active);
2287 samplersVS[samplerIndex].logicalTextureUnit = v[i];
2294 return applyUniform(device, location, (float*)vector);
2300 bool Program::applyUniform2iv(Device *device, GLint location, GLsizei count, const GLint *v)
2302 GLint vector[MAX_UNIFORM_VECTORS][4];
2304 for(int i = 0; i < count; i++)
2306 vector[i][0] = v[0];
2307 vector[i][1] = v[1];
2314 return applyUniform(device, location, (float*)vector);
2317 bool Program::applyUniform3iv(Device *device, GLint location, GLsizei count, const GLint *v)
2319 GLint vector[MAX_UNIFORM_VECTORS][4];
2321 for(int i = 0; i < count; i++)
2323 vector[i][0] = v[0];
2324 vector[i][1] = v[1];
2325 vector[i][2] = v[2];
2331 return applyUniform(device, location, (float*)vector);
2334 bool Program::applyUniform4iv(Device *device, GLint location, GLsizei count, const GLint *v)
2336 GLint vector[MAX_UNIFORM_VECTORS][4];
2338 for(int i = 0; i < count; i++)
2340 vector[i][0] = v[0];
2341 vector[i][1] = v[1];
2342 vector[i][2] = v[2];
2343 vector[i][3] = v[3];
2348 return applyUniform(device, location, (float*)vector);
2351 bool Program::applyUniform1uiv(Device *device, GLint location, GLsizei count, const GLuint *v)
2353 GLuint vector[MAX_UNIFORM_VECTORS][4];
2355 for(int i = 0; i < count; i++)
2357 vector[i][0] = v[i];
2363 Uniform *targetUniform = uniforms[uniformIndex[location].index];
2364 if(IsSamplerUniform(targetUniform->type))
2366 if(targetUniform->psRegisterIndex != -1)
2368 for(int i = 0; i < count; i++)
2370 unsigned int samplerIndex = targetUniform->psRegisterIndex + i;
2372 if(samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2374 ASSERT(samplersPS[samplerIndex].active);
2375 samplersPS[samplerIndex].logicalTextureUnit = v[i];
2380 if(targetUniform->vsRegisterIndex != -1)
2382 for(int i = 0; i < count; i++)
2384 unsigned int samplerIndex = targetUniform->vsRegisterIndex + i;
2386 if(samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS)
2388 ASSERT(samplersVS[samplerIndex].active);
2389 samplersVS[samplerIndex].logicalTextureUnit = v[i];
2396 return applyUniform(device, location, (float*)vector);
2402 bool Program::applyUniform2uiv(Device *device, GLint location, GLsizei count, const GLuint *v)
2404 GLuint vector[MAX_UNIFORM_VECTORS][4];
2406 for(int i = 0; i < count; i++)
2408 vector[i][0] = v[0];
2409 vector[i][1] = v[1];
2416 return applyUniform(device, location, (float*)vector);
2419 bool Program::applyUniform3uiv(Device *device, GLint location, GLsizei count, const GLuint *v)
2421 GLuint vector[MAX_UNIFORM_VECTORS][4];
2423 for(int i = 0; i < count; i++)
2425 vector[i][0] = v[0];
2426 vector[i][1] = v[1];
2427 vector[i][2] = v[2];
2433 return applyUniform(device, location, (float*)vector);
2436 bool Program::applyUniform4uiv(Device *device, GLint location, GLsizei count, const GLuint *v)
2438 GLuint vector[MAX_UNIFORM_VECTORS][4];
2440 for(int i = 0; i < count; i++)
2442 vector[i][0] = v[0];
2443 vector[i][1] = v[1];
2444 vector[i][2] = v[2];
2445 vector[i][3] = v[3];
2450 return applyUniform(device, location, (float*)vector);
2453 void Program::appendToInfoLog(const char *format, ...)
2463 va_start(vararg, format);
2464 vsnprintf(info, sizeof(info), format, vararg);
2467 size_t infoLength = strlen(info);
2471 infoLog = new char[infoLength + 2];
2472 strcpy(infoLog, info);
2473 strcpy(infoLog + infoLength, "\n");
2477 size_t logLength = strlen(infoLog);
2478 char *newLog = new char[logLength + infoLength + 2];
2479 strcpy(newLog, infoLog);
2480 strcpy(newLog + logLength, info);
2481 strcpy(newLog + logLength + infoLength, "\n");
2488 void Program::resetInfoLog()
2497 // Returns the program object to an unlinked state, before re-linking, or at destruction
2498 void Program::unlink()
2500 delete vertexBinary;
2505 linkedAttribute.clear();
2506 linkedAttributeLocation.clear();
2508 for(int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
2510 attributeStream[index] = -1;
2513 for(int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
2515 samplersPS[index].active = false;
2518 for(int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS; index++)
2520 samplersVS[index].active = false;
2523 while(!uniforms.empty())
2525 delete uniforms.back();
2526 uniforms.pop_back();
2529 while(!uniformBlocks.empty())
2531 delete uniformBlocks.back();
2532 uniformBlocks.pop_back();
2535 uniformIndex.clear();
2536 transformFeedbackLinkedVaryings.clear();
2544 bool Program::isLinked() const
2549 bool Program::isValidated() const
2554 GLint Program::getBinaryLength() const
2560 void Program::release()
2564 if(referenceCount == 0 && orphaned)
2566 resourceManager->deleteProgram(handle);
2570 void Program::addRef()
2575 unsigned int Program::getRefCount() const
2577 return referenceCount;
2580 unsigned int Program::getSerial() const
2585 unsigned int Program::issueSerial()
2587 return currentSerial++;
2590 size_t Program::getInfoLogLength() const
2598 return strlen(infoLog) + 1;
2602 void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *buffer)
2610 index = std::min(bufSize - 1, (int)strlen(infoLog));
2611 memcpy(buffer, infoLog, index);
2614 buffer[index] = '\0';
2623 void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders)
2627 if(vertexShader && (total < maxCount))
2629 shaders[total++] = vertexShader->getName();
2632 if(fragmentShader && (total < maxCount))
2634 shaders[total++] = fragmentShader->getName();
2643 void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
2645 ASSERT(index < linkedAttribute.size());
2647 std::vector<glsl::Attribute>::const_iterator it = linkedAttribute.begin() + index;
2651 const char *string = it->name.c_str();
2653 strncpy(name, string, bufsize);
2654 name[bufsize - 1] = '\0';
2658 *length = static_cast<GLsizei>(strlen(name));
2662 *size = 1; // Always a single 'type' instance
2667 size_t Program::getActiveAttributeCount() const
2669 return linkedAttribute.size();
2672 GLint Program::getActiveAttributeMaxLength() const
2676 std::vector<glsl::Attribute>::const_iterator it = linkedAttribute.begin();
2677 std::vector<glsl::Attribute>::const_iterator itEnd = linkedAttribute.end();
2678 for(; it != itEnd; ++it)
2680 maxLength = std::max((int)(it->name.length() + 1), maxLength);
2686 void Program::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
2690 std::string string = uniforms[index]->name;
2692 if(uniforms[index]->isArray())
2697 strncpy(name, string.c_str(), bufsize);
2698 name[bufsize - 1] = '\0';
2702 *length = static_cast<GLsizei>(strlen(name));
2706 *size = uniforms[index]->size();
2708 *type = uniforms[index]->type;
2711 size_t Program::getActiveUniformCount() const
2713 return uniforms.size();
2716 GLint Program::getActiveUniformMaxLength() const
2720 size_t numUniforms = uniforms.size();
2721 for(size_t uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2723 if(!uniforms[uniformIndex]->name.empty())
2725 int length = (int)(uniforms[uniformIndex]->name.length() + 1);
2726 if(uniforms[uniformIndex]->isArray())
2728 length += 3; // Counting in "[0]".
2730 maxLength = std::max(length, maxLength);
2737 GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
2739 const Uniform& uniform = *uniforms[index];
2742 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
2743 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.size());
2744 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
2745 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockInfo.index;
2746 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
2747 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
2748 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
2749 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
2757 void Program::getActiveUniformBlockName(GLuint index, GLsizei bufSize, GLsizei *length, GLchar *name) const
2759 if(index >= getActiveUniformBlockCount())
2761 return error(GL_INVALID_VALUE);
2764 const UniformBlock &uniformBlock = *uniformBlocks[index];
2768 std::string string = uniformBlock.name;
2770 if(uniformBlock.isArrayElement())
2772 std::ostringstream elementIndex;
2773 elementIndex << uniformBlock.elementIndex;
2774 string += "[" + elementIndex.str() + "]";
2777 strncpy(name, string.c_str(), bufSize);
2778 name[bufSize - 1] = '\0';
2782 *length = static_cast<GLsizei>(strlen(name));
2787 size_t Program::getActiveUniformBlockCount() const
2789 return uniformBlocks.size();
2792 GLint Program::getActiveUniformBlockMaxLength() const
2794 GLint maxLength = 0;
2798 size_t numUniformBlocks = getActiveUniformBlockCount();
2799 for(size_t uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
2801 const UniformBlock &uniformBlock = *uniformBlocks[uniformBlockIndex];
2802 if(!uniformBlock.name.empty())
2804 GLint length = static_cast<GLint>(uniformBlock.name.length() + 1);
2806 // Counting in "[0]".
2807 const GLint arrayLength = (uniformBlock.isArrayElement() ? 3 : 0);
2809 maxLength = std::max(length + arrayLength, maxLength);
2817 void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
2819 transformFeedbackVaryings.resize(count);
2820 for(GLsizei i = 0; i < count; i++)
2822 transformFeedbackVaryings[i] = varyings[i];
2825 transformFeedbackBufferMode = bufferMode;
2828 void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
2832 ASSERT(index < transformFeedbackLinkedVaryings.size());
2833 const LinkedVarying &varying = transformFeedbackLinkedVaryings[index];
2834 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
2837 *length = lastNameIdx;
2841 *size = varying.size;
2845 *type = varying.type;
2849 memcpy(name, varying.name.c_str(), lastNameIdx);
2850 name[lastNameIdx] = '\0';
2855 GLsizei Program::getTransformFeedbackVaryingCount() const
2859 return static_cast<GLsizei>(transformFeedbackLinkedVaryings.size());
2867 GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2871 GLsizei maxSize = 0;
2872 for(size_t i = 0; i < transformFeedbackLinkedVaryings.size(); i++)
2874 const LinkedVarying &varying = transformFeedbackLinkedVaryings[i];
2875 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
2886 GLenum Program::getTransformFeedbackBufferMode() const
2888 return transformFeedbackBufferMode;
2891 void Program::flagForDeletion()
2896 bool Program::isFlaggedForDeletion() const
2901 void Program::validate(Device* device)
2907 appendToInfoLog("Program has not been successfully linked.");
2912 applyUniforms(device);
2913 if(!validateSamplers(true))
2924 bool Program::validateSamplers(bool logErrors)
2926 // if any two active samplers in a program are of different types, but refer to the same
2927 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2928 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2930 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
2932 for(unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS; i++)
2934 textureUnitType[i] = TEXTURE_UNKNOWN;
2937 for(unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
2939 if(samplersPS[i].active)
2941 unsigned int unit = samplersPS[i].logicalTextureUnit;
2943 if(unit >= MAX_COMBINED_TEXTURE_IMAGE_UNITS)
2947 appendToInfoLog("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, MAX_COMBINED_TEXTURE_IMAGE_UNITS);
2953 if(textureUnitType[unit] != TEXTURE_UNKNOWN)
2955 if(samplersPS[i].textureType != textureUnitType[unit])
2959 appendToInfoLog("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
2967 textureUnitType[unit] = samplersPS[i].textureType;
2972 for(unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
2974 if(samplersVS[i].active)
2976 unsigned int unit = samplersVS[i].logicalTextureUnit;
2978 if(unit >= MAX_COMBINED_TEXTURE_IMAGE_UNITS)
2982 appendToInfoLog("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, MAX_COMBINED_TEXTURE_IMAGE_UNITS);
2988 if(textureUnitType[unit] != TEXTURE_UNKNOWN)
2990 if(samplersVS[i].textureType != textureUnitType[unit])
2994 appendToInfoLog("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
3002 textureUnitType[unit] = samplersVS[i].textureType;