diff options
Diffstat (limited to 'Source/ThirdParty/ANGLE/src/libGLESv2/Program.cpp')
-rw-r--r-- | Source/ThirdParty/ANGLE/src/libGLESv2/Program.cpp | 1427 |
1 files changed, 710 insertions, 717 deletions
diff --git a/Source/ThirdParty/ANGLE/src/libGLESv2/Program.cpp b/Source/ThirdParty/ANGLE/src/libGLESv2/Program.cpp index 780c3e3..a5e38bf 100644 --- a/Source/ThirdParty/ANGLE/src/libGLESv2/Program.cpp +++ b/Source/ThirdParty/ANGLE/src/libGLESv2/Program.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. +// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // @@ -15,9 +15,16 @@ #include "libGLESv2/Shader.h" #include "libGLESv2/utilities.h" +#include <string> + +#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL) +#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3 +#endif + namespace gl { unsigned int Program::mCurrentSerial = 1; +const char *fakepath = "C:\\fakepath"; std::string str(int i) { @@ -26,13 +33,13 @@ std::string str(int i) return buffer; } -Uniform::Uniform(GLenum type, const std::string &name, unsigned int arraySize) : type(type), name(name), arraySize(arraySize) +Uniform::Uniform(GLenum type, const std::string &_name, unsigned int arraySize) + : type(type), _name(_name), name(Program::undecorateUniform(_name)), arraySize(arraySize) { - int bytes = UniformTypeSize(type) * arraySize; + int bytes = UniformInternalSize(type) * arraySize; data = new unsigned char[bytes]; memset(data, 0, bytes); dirty = true; - handlesSet = false; } Uniform::~Uniform() @@ -40,13 +47,19 @@ Uniform::~Uniform() delete[] data; } -UniformLocation::UniformLocation(const std::string &name, unsigned int element, unsigned int index) - : name(name), element(element), index(index) +bool Uniform::isArray() +{ + return _name.compare(0, 3, "ar_") == 0; +} + +UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index) + : name(Program::undecorateUniform(_name)), element(element), index(index) { } Program::Program(ResourceManager *manager, GLuint handle) : mResourceManager(manager), mHandle(handle), mSerial(issueSerial()) { + mDevice = getDevice(); mFragmentShader = NULL; mVertexShader = NULL; @@ -131,8 +144,6 @@ bool Program::detachShader(Shader *shader) } else UNREACHABLE(); - unlink(); - return true; } @@ -182,28 +193,54 @@ GLuint Program::getAttributeLocation(const char *name) int Program::getSemanticIndex(int attributeIndex) { - if (attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS) + ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS); + + return mSemanticIndex[attributeIndex]; +} + +// Returns one more than the highest sampler index used. +GLint Program::getUsedSamplerRange(SamplerType type) +{ + switch (type) { - return mSemanticIndex[attributeIndex]; + case SAMPLER_PIXEL: + return mUsedPixelSamplerRange; + case SAMPLER_VERTEX: + return mUsedVertexSamplerRange; + default: + UNREACHABLE(); + return 0; } - - return -1; } -// Returns the index of the texture unit corresponding to a Direct3D 9 sampler -// index referenced in the compiled HLSL shader -GLint Program::getSamplerMapping(unsigned int samplerIndex) +// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler +// index (0-15 for the pixel shader and 0-3 for the vertex shader). +GLint Program::getSamplerMapping(SamplerType type, unsigned int samplerIndex) { - assert(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0])); - GLint logicalTextureUnit = -1; - if (mSamplers[samplerIndex].active) + switch (type) { - logicalTextureUnit = mSamplers[samplerIndex].logicalTextureUnit; + case SAMPLER_PIXEL: + ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0])); + + if (mSamplersPS[samplerIndex].active) + { + logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit; + } + break; + case SAMPLER_VERTEX: + ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0])); + + if (mSamplersVS[samplerIndex].active) + { + logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit; + } + break; + default: UNREACHABLE(); } - if (logicalTextureUnit >= 0 && logicalTextureUnit < MAX_TEXTURE_IMAGE_UNITS) + if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits()) { return logicalTextureUnit; } @@ -211,52 +248,43 @@ GLint Program::getSamplerMapping(unsigned int samplerIndex) return -1; } -SamplerType Program::getSamplerType(unsigned int samplerIndex) +// Returns the texture type for a given Direct3D 9 sampler type and +// index (0-15 for the pixel shader and 0-3 for the vertex shader). +TextureType Program::getSamplerTextureType(SamplerType type, unsigned int samplerIndex) { - assert(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0])); - assert(mSamplers[samplerIndex].active); - - return mSamplers[samplerIndex].type; -} - -bool Program::isSamplerDirty(unsigned int samplerIndex) const -{ - if (samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0])) + switch (type) { - return mSamplers[samplerIndex].dirty; + case SAMPLER_PIXEL: + ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0])); + ASSERT(mSamplersPS[samplerIndex].active); + return mSamplersPS[samplerIndex].textureType; + case SAMPLER_VERTEX: + ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0])); + ASSERT(mSamplersVS[samplerIndex].active); + return mSamplersVS[samplerIndex].textureType; + default: UNREACHABLE(); } - else UNREACHABLE(); - - return false; -} -void Program::setSamplerDirty(unsigned int samplerIndex, bool dirty) -{ - if (samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0])) - { - mSamplers[samplerIndex].dirty = dirty; - } - else UNREACHABLE(); + return TEXTURE_2D; } -GLint Program::getUniformLocation(const char *name, bool decorated) +GLint Program::getUniformLocation(std::string name) { - std::string _name = decorated ? name : decorate(name); - int subscript = 0; + unsigned int subscript = 0; // Strip any trailing array operator and retrieve the subscript - size_t open = _name.find_last_of('['); - size_t close = _name.find_last_of(']'); - if (open != std::string::npos && close == _name.length() - 1) + size_t open = name.find_last_of('['); + size_t close = name.find_last_of(']'); + if (open != std::string::npos && close == name.length() - 1) { - subscript = atoi(_name.substr(open + 1).c_str()); - _name.erase(open); + subscript = atoi(name.substr(open + 1).c_str()); + name.erase(open); } unsigned int numUniforms = mUniformIndex.size(); for (unsigned int location = 0; location < numUniforms; location++) { - if (mUniformIndex[location].name == _name && + if (mUniformIndex[location].name == name && mUniformIndex[location].element == subscript) { return location; @@ -285,8 +313,17 @@ bool Program::setUniform1fv(GLint location, GLsizei count, const GLfloat* v) count = std::min(arraySize - (int)mUniformIndex[location].element, count); - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat), - v, sizeof(GLfloat) * count); + GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; + + for (int i = 0; i < count; i++) + { + target[0] = v[0]; + target[1] = 0; + target[2] = 0; + target[3] = 0; + target += 4; + v += 1; + } } else if (targetUniform->type == GL_BOOL) { @@ -296,7 +333,7 @@ bool Program::setUniform1fv(GLint location, GLsizei count, const GLfloat* v) return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION count = std::min(arraySize - (int)mUniformIndex[location].element, count); - GLboolean *boolParams = new GLboolean[count]; + GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element; for (int i = 0; i < count; ++i) { @@ -309,11 +346,6 @@ bool Program::setUniform1fv(GLint location, GLsizei count, const GLfloat* v) boolParams[i] = GL_TRUE; } } - - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLboolean), - boolParams, sizeof(GLboolean) * count); - - delete [] boolParams; } else { @@ -342,8 +374,17 @@ bool Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) count = std::min(arraySize - (int)mUniformIndex[location].element, count); - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 2, - v, 2 * sizeof(GLfloat) * count); + GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; + + for (int i = 0; i < count; i++) + { + target[0] = v[0]; + target[1] = v[1]; + target[2] = 0; + target[3] = 0; + target += 4; + v += 2; + } } else if (targetUniform->type == GL_BOOL_VEC2) { @@ -354,7 +395,7 @@ bool Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) count = std::min(arraySize - (int)mUniformIndex[location].element, count); - GLboolean *boolParams = new GLboolean[count * 2]; + GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2; for (int i = 0; i < count * 2; ++i) { @@ -367,11 +408,6 @@ bool Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) boolParams[i] = GL_TRUE; } } - - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLboolean) * 2, - boolParams, 2 * sizeof(GLboolean) * count); - - delete [] boolParams; } else { @@ -400,8 +436,17 @@ bool Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) count = std::min(arraySize - (int)mUniformIndex[location].element, count); - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 3, - v, 3 * sizeof(GLfloat) * count); + GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; + + for (int i = 0; i < count; i++) + { + target[0] = v[0]; + target[1] = v[1]; + target[2] = v[2]; + target[3] = 0; + target += 4; + v += 3; + } } else if (targetUniform->type == GL_BOOL_VEC3) { @@ -411,7 +456,7 @@ bool Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION count = std::min(arraySize - (int)mUniformIndex[location].element, count); - GLboolean *boolParams = new GLboolean[count * 3]; + GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3; for (int i = 0; i < count * 3; ++i) { @@ -424,11 +469,6 @@ bool Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) boolParams[i] = GL_TRUE; } } - - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLboolean) * 3, - boolParams, 3 * sizeof(GLboolean) * count); - - delete [] boolParams; } else { @@ -468,7 +508,7 @@ bool Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION count = std::min(arraySize - (int)mUniformIndex[location].element, count); - GLboolean *boolParams = new GLboolean[count * 4]; + GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4; for (int i = 0; i < count * 4; ++i) { @@ -481,11 +521,6 @@ bool Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) boolParams[i] = GL_TRUE; } } - - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLboolean) * 4, - boolParams, 4 * sizeof(GLboolean) * count); - - delete [] boolParams; } else { @@ -495,6 +530,37 @@ bool Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) return true; } +template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight> +void transposeMatrix(T *target, const GLfloat *value) +{ + int copyWidth = std::min(targetWidth, srcWidth); + int copyHeight = std::min(targetHeight, srcHeight); + + for (int x = 0; x < copyWidth; x++) + { + for (int y = 0; y < copyHeight; y++) + { + target[x * targetWidth + y] = (T)value[y * srcWidth + x]; + } + } + // clear unfilled right side + for (int y = 0; y < copyHeight; y++) + { + for (int x = srcWidth; x < targetWidth; x++) + { + target[y * targetWidth + x] = (T)0; + } + } + // clear unfilled bottom. + for (int y = srcHeight; y < targetHeight; y++) + { + for (int x = 0; x < targetWidth; x++) + { + target[y * targetWidth + x] = (T)0; + } + } +} + bool Program::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value) { if (location < 0 || location >= (int)mUniformIndex.size()) @@ -517,8 +583,13 @@ bool Program::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat * count = std::min(arraySize - (int)mUniformIndex[location].element, count); - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4, - value, 4 * sizeof(GLfloat) * count); + GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8; + for (int i = 0; i < count; i++) + { + transposeMatrix<GLfloat,4,2,2,2>(target, value); + target += 8; + value += 4; + } return true; } @@ -545,12 +616,18 @@ bool Program::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat * count = std::min(arraySize - (int)mUniformIndex[location].element, count); - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 9, - value, 9 * sizeof(GLfloat) * count); + GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12; + for (int i = 0; i < count; i++) + { + transposeMatrix<GLfloat,4,3,3,3>(target, value); + target += 12; + value += 9; + } return true; } + bool Program::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value) { if (location < 0 || location >= (int)mUniformIndex.size()) @@ -573,8 +650,13 @@ bool Program::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat * count = std::min(arraySize - (int)mUniformIndex[location].element, count); - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16, - value, 16 * sizeof(GLfloat) * count); + GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16); + for (int i = 0; i < count; i++) + { + transposeMatrix<GLfloat,4,4,4,4>(target, value); + target += 16; + value += 16; + } return true; } @@ -611,7 +693,7 @@ bool Program::setUniform1iv(GLint location, GLsizei count, const GLint *v) return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION count = std::min(arraySize - (int)mUniformIndex[location].element, count); - GLboolean *boolParams = new GLboolean[count]; + GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element; for (int i = 0; i < count; ++i) { @@ -624,11 +706,6 @@ bool Program::setUniform1iv(GLint location, GLsizei count, const GLint *v) boolParams[i] = GL_TRUE; } } - - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLboolean), - boolParams, sizeof(GLboolean) * count); - - delete [] boolParams; } else { @@ -668,7 +745,7 @@ bool Program::setUniform2iv(GLint location, GLsizei count, const GLint *v) return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION count = std::min(arraySize - (int)mUniformIndex[location].element, count); - GLboolean *boolParams = new GLboolean[count * 2]; + GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2; for (int i = 0; i < count * 2; ++i) { @@ -681,11 +758,6 @@ bool Program::setUniform2iv(GLint location, GLsizei count, const GLint *v) boolParams[i] = GL_TRUE; } } - - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLboolean) * 2, - boolParams, 2 * sizeof(GLboolean) * count); - - delete [] boolParams; } else { @@ -725,7 +797,7 @@ bool Program::setUniform3iv(GLint location, GLsizei count, const GLint *v) return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION count = std::min(arraySize - (int)mUniformIndex[location].element, count); - GLboolean *boolParams = new GLboolean[count * 3]; + GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3; for (int i = 0; i < count * 3; ++i) { @@ -738,11 +810,6 @@ bool Program::setUniform3iv(GLint location, GLsizei count, const GLint *v) boolParams[i] = GL_TRUE; } } - - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLboolean) * 3, - boolParams, 3 * sizeof(GLboolean) * count); - - delete [] boolParams; } else { @@ -782,7 +849,7 @@ bool Program::setUniform4iv(GLint location, GLsizei count, const GLint *v) return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION count = std::min(arraySize - (int)mUniformIndex[location].element, count); - GLboolean *boolParams = new GLboolean[count * 4]; + GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4; for (int i = 0; i < count * 4; ++i) { @@ -795,11 +862,6 @@ bool Program::setUniform4iv(GLint location, GLsizei count, const GLint *v) boolParams[i] = GL_TRUE; } } - - memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLboolean) * 4, - boolParams, 4 * sizeof(GLboolean) * count); - - delete [] boolParams; } else { @@ -809,7 +871,7 @@ bool Program::setUniform4iv(GLint location, GLsizei count, const GLint *v) return true; } -bool Program::getUniformfv(GLint location, GLfloat *params) +bool Program::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params) { if (location < 0 || location >= (int)mUniformIndex.size()) { @@ -818,41 +880,67 @@ bool Program::getUniformfv(GLint location, GLfloat *params) Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - unsigned int count = UniformComponentCount(targetUniform->type); - - switch (UniformComponentType(targetUniform->type)) + // sized queries -- ensure the provided buffer is large enough + if (bufSize) { - case GL_BOOL: + int requiredBytes = UniformExternalSize(targetUniform->type); + if (*bufSize < requiredBytes) { - GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * count; - - for (unsigned int i = 0; i < count; ++i) - { - params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f; - } + return false; } + } + + switch (targetUniform->type) + { + case GL_FLOAT_MAT2: + transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8); + break; + case GL_FLOAT_MAT3: + transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12); break; - case GL_FLOAT: - memcpy(params, targetUniform->data + mUniformIndex[location].element * count * sizeof(GLfloat), - count * sizeof(GLfloat)); + case GL_FLOAT_MAT4: + transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16); break; - case GL_INT: + default: { - GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * count; + unsigned int count = UniformExternalComponentCount(targetUniform->type); + unsigned int internalCount = UniformInternalComponentCount(targetUniform->type); - for (unsigned int i = 0; i < count; ++i) + switch (UniformComponentType(targetUniform->type)) { - params[i] = (float)intParams[i]; + case GL_BOOL: + { + GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount; + + for (unsigned int i = 0; i < count; ++i) + { + params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f; + } + } + break; + case GL_FLOAT: + memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat), + count * sizeof(GLfloat)); + break; + case GL_INT: + { + GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount; + + for (unsigned int i = 0; i < count; ++i) + { + params[i] = (float)intParams[i]; + } + } + break; + default: UNREACHABLE(); } } - break; - default: UNREACHABLE(); } return true; } -bool Program::getUniformiv(GLint location, GLint *params) +bool Program::getUniformiv(GLint location, GLsizei *bufSize, GLint *params) { if (location < 0 || location >= (int)mUniformIndex.size()) { @@ -861,35 +949,67 @@ bool Program::getUniformiv(GLint location, GLint *params) Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - unsigned int count = UniformComponentCount(targetUniform->type); - - switch (UniformComponentType(targetUniform->type)) + // sized queries -- ensure the provided buffer is large enough + if (bufSize) { - case GL_BOOL: + int requiredBytes = UniformExternalSize(targetUniform->type); + if (*bufSize < requiredBytes) { - GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * count; + return false; + } + } - for (unsigned int i = 0; i < count; ++i) - { - params[i] = (GLint)boolParams[i]; - } + switch (targetUniform->type) + { + case GL_FLOAT_MAT2: + { + transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8); + } + break; + case GL_FLOAT_MAT3: + { + transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12); } break; - case GL_FLOAT: + case GL_FLOAT_MAT4: + { + transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16); + } + break; + default: { - GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * count; + unsigned int count = UniformExternalComponentCount(targetUniform->type); + unsigned int internalCount = UniformInternalComponentCount(targetUniform->type); - for (unsigned int i = 0; i < count; ++i) + switch (UniformComponentType(targetUniform->type)) { - params[i] = (GLint)floatParams[i]; + case GL_BOOL: + { + GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount; + + for (unsigned int i = 0; i < count; ++i) + { + params[i] = (GLint)boolParams[i]; + } + } + break; + case GL_FLOAT: + { + GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount; + + for (unsigned int i = 0; i < count; ++i) + { + params[i] = (GLint)floatParams[i]; + } + } + break; + case GL_INT: + memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint), + count * sizeof(GLint)); + break; + default: UNREACHABLE(); } } - break; - case GL_INT: - memcpy(params, targetUniform->data + mUniformIndex[location].element * count * sizeof(GLint), - count * sizeof(GLint)); - break; - default: UNREACHABLE(); } return true; @@ -904,26 +1024,11 @@ void Program::dirtyAllUniforms() } } -void Program::dirtyAllSamplers() -{ - for (unsigned int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; ++index) - { - mSamplers[index].dirty = true; - } -} - // Applies all the uniforms set for this program object to the Direct3D 9 device void Program::applyUniforms() { - unsigned int numUniforms = mUniformIndex.size(); - for (unsigned int location = 0; location < numUniforms; location++) - { - if (mUniformIndex[location].element != 0) - { - continue; - } - - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; + for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub) { + Uniform *targetUniform = *ub; if (targetUniform->dirty) { @@ -934,23 +1039,23 @@ void Program::applyUniforms() switch (targetUniform->type) { - case GL_BOOL: applyUniform1bv(location, arraySize, b); break; - case GL_BOOL_VEC2: applyUniform2bv(location, arraySize, b); break; - case GL_BOOL_VEC3: applyUniform3bv(location, arraySize, b); break; - case GL_BOOL_VEC4: applyUniform4bv(location, arraySize, b); break; - case GL_FLOAT: applyUniform1fv(location, arraySize, f); break; - case GL_FLOAT_VEC2: applyUniform2fv(location, arraySize, f); break; - case GL_FLOAT_VEC3: applyUniform3fv(location, arraySize, f); break; - case GL_FLOAT_VEC4: applyUniform4fv(location, arraySize, f); break; - case GL_FLOAT_MAT2: applyUniformMatrix2fv(location, arraySize, f); break; - case GL_FLOAT_MAT3: applyUniformMatrix3fv(location, arraySize, f); break; - case GL_FLOAT_MAT4: applyUniformMatrix4fv(location, arraySize, f); break; + case GL_BOOL: applyUniformnbv(targetUniform, arraySize, 1, b); break; + case GL_BOOL_VEC2: applyUniformnbv(targetUniform, arraySize, 2, b); break; + case GL_BOOL_VEC3: applyUniformnbv(targetUniform, arraySize, 3, b); break; + case GL_BOOL_VEC4: applyUniformnbv(targetUniform, arraySize, 4, b); break; + case GL_FLOAT: + case GL_FLOAT_VEC2: + case GL_FLOAT_VEC3: + case GL_FLOAT_VEC4: + case GL_FLOAT_MAT2: + case GL_FLOAT_MAT3: + case GL_FLOAT_MAT4: applyUniformnfv(targetUniform, f); break; case GL_SAMPLER_2D: case GL_SAMPLER_CUBE: - case GL_INT: applyUniform1iv(location, arraySize, i); break; - case GL_INT_VEC2: applyUniform2iv(location, arraySize, i); break; - case GL_INT_VEC3: applyUniform3iv(location, arraySize, i); break; - case GL_INT_VEC4: applyUniform4iv(location, arraySize, i); break; + case GL_INT: applyUniform1iv(targetUniform, arraySize, i); break; + case GL_INT_VEC2: applyUniform2iv(targetUniform, arraySize, i); break; + case GL_INT_VEC3: applyUniform3iv(targetUniform, arraySize, i); break; + case GL_INT_VEC4: applyUniform4iv(targetUniform, arraySize, i); break; default: UNREACHABLE(); } @@ -961,38 +1066,76 @@ void Program::applyUniforms() } // Compiles the HLSL code of the attached shaders into executable binaries -ID3DXBuffer *Program::compileToBinary(const char *hlsl, const char *profile, ID3DXConstantTable **constantTable) +ID3D10Blob *Program::compileToBinary(const char *hlsl, const char *profile, ID3DXConstantTable **constantTable) { if (!hlsl) { return NULL; } - ID3DXBuffer *binary = NULL; - ID3DXBuffer *errorMessage = NULL; - - HRESULT result = D3DXCompileShader(hlsl, (UINT)strlen(hlsl), NULL, NULL, "main", profile, 0, &binary, &errorMessage, constantTable); - - if (SUCCEEDED(result)) + DWORD result; + UINT flags = 0; + std::string sourceText; + if (perfActive()) { - return binary; - } + flags |= D3DCOMPILE_DEBUG; +#ifdef NDEBUG + flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL; +#else + flags |= D3DCOMPILE_SKIP_OPTIMIZATION; +#endif - if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) + std::string sourcePath = getTempPath(); + sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl); + writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size()); + } + else { - return error(GL_OUT_OF_MEMORY, (ID3DXBuffer*)NULL); + flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL; + sourceText = hlsl; } + ID3D10Blob *binary = NULL; + ID3D10Blob *errorMessage = NULL; + result = D3DCompile(hlsl, strlen(hlsl), fakepath, NULL, NULL, "main", profile, flags, 0, &binary, &errorMessage); + if (errorMessage) { const char *message = (const char*)errorMessage->GetBufferPointer(); - appendToInfoLog("%s\n", message); + appendToInfoLogSanitized(message); TRACE("\n%s", hlsl); TRACE("\n%s", message); + + errorMessage->Release(); + errorMessage = NULL; + } + + if (FAILED(result)) + { + if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) + { + error(GL_OUT_OF_MEMORY); + } + + return NULL; + } + + result = D3DXGetShaderConstantTable(static_cast<const DWORD*>(binary->GetBufferPointer()), constantTable); + + if (FAILED(result)) + { + if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) + { + error(GL_OUT_OF_MEMORY); + } + + binary->Release(); + + return NULL; } - return NULL; + return binary; } // Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111 @@ -1002,7 +1145,7 @@ int Program::packVaryings(const Varying *packing[][4]) Context *context = getContext(); const int maxVaryingVectors = context->getMaximumVaryingVectors(); - for (VaryingList::iterator varying = mFragmentShader->varyings.begin(); varying != mFragmentShader->varyings.end(); varying++) + for (VaryingList::iterator varying = mFragmentShader->mVaryings.begin(); varying != mFragmentShader->mVaryings.end(); varying++) { int n = VariableRowCount(varying->type) * varying->size; int m = VariableColumnCount(varying->type); @@ -1093,13 +1236,13 @@ int Program::packVaryings(const Varying *packing[][4]) for (int x = 0; x < 4; x++) { - if (space[x] > n && space[x] < space[column]) + if (space[x] >= n && space[x] < space[column]) { column = x; } } - if (space[column] > n) + if (space[column] >= n) { for (int r = 0; r < maxVaryingVectors; r++) { @@ -1152,6 +1295,20 @@ bool Program::linkVaryings() return false; } + // Reset the varying register assignments + for (VaryingList::iterator fragVar = mFragmentShader->mVaryings.begin(); fragVar != mFragmentShader->mVaryings.end(); fragVar++) + { + fragVar->reg = -1; + fragVar->col = -1; + } + + for (VaryingList::iterator vtxVar = mVertexShader->mVaryings.begin(); vtxVar != mVertexShader->mVaryings.end(); vtxVar++) + { + vtxVar->reg = -1; + vtxVar->col = -1; + } + + // Map the varyings to the register file const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL}; int registers = packVaryings(packing); @@ -1160,6 +1317,7 @@ bool Program::linkVaryings() return false; } + // Write the HLSL input/output declarations Context *context = getContext(); const bool sm3 = context->supportsShaderModel3(); const int maxVaryingVectors = context->getMaximumVaryingVectors(); @@ -1171,11 +1329,11 @@ bool Program::linkVaryings() return false; } - for (VaryingList::iterator input = mFragmentShader->varyings.begin(); input != mFragmentShader->varyings.end(); input++) + for (VaryingList::iterator input = mFragmentShader->mVaryings.begin(); input != mFragmentShader->mVaryings.end(); input++) { bool matched = false; - for (VaryingList::iterator output = mVertexShader->varyings.begin(); output != mVertexShader->varyings.end(); output++) + for (VaryingList::iterator output = mVertexShader->mVaryings.begin(); output != mVertexShader->mVaryings.end(); output++) { if (output->name == input->name) { @@ -1196,7 +1354,7 @@ bool Program::linkVaryings() if (!matched) { - appendToInfoLog("Fragment varying varying %s does not match any vertex varying", input->name.c_str()); + appendToInfoLog("Fragment varying %s does not match any vertex varying", input->name.c_str()); return false; } @@ -1222,7 +1380,7 @@ bool Program::linkVaryings() default: UNREACHABLE(); } - mVertexHLSL += decorate(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n"; + mVertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n"; semanticIndex += VariableRowCount(attribute->type); } @@ -1257,14 +1415,14 @@ bool Program::linkVaryings() for (AttributeArray::iterator attribute = mVertexShader->mAttributes.begin(); attribute != mVertexShader->mAttributes.end(); attribute++) { - mVertexHLSL += " " + decorate(attribute->name) + " = "; + mVertexHLSL += " " + decorateAttribute(attribute->name) + " = "; if (VariableRowCount(attribute->type) > 1) // Matrix { mVertexHLSL += "transpose"; } - mVertexHLSL += "(input." + decorate(attribute->name) + ");\n"; + mVertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n"; } mVertexHLSL += "\n" @@ -1272,7 +1430,7 @@ bool Program::linkVaryings() "\n" " VS_OUTPUT output;\n" " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n" - " output.gl_Position.y = -(gl_Position.y - dx_HalfPixelSize.y * gl_Position.w);\n" + " output.gl_Position.y = gl_Position.y - dx_HalfPixelSize.y * gl_Position.w;\n" " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n" " output.gl_Position.w = gl_Position.w;\n"; @@ -1286,7 +1444,7 @@ bool Program::linkVaryings() mVertexHLSL += " output.gl_FragCoord = gl_Position;\n"; } - for (VaryingList::iterator varying = mVertexShader->varyings.begin(); varying != mVertexShader->varyings.end(); varying++) + for (VaryingList::iterator varying = mVertexShader->mVaryings.begin(); varying != mVertexShader->mVaryings.end(); varying++) { if (varying->reg >= 0) { @@ -1354,7 +1512,7 @@ bool Program::linkVaryings() mPixelHLSL += "struct PS_INPUT\n" "{\n"; - for (VaryingList::iterator varying = mFragmentShader->varyings.begin(); varying != mFragmentShader->varyings.end(); varying++) + for (VaryingList::iterator varying = mFragmentShader->mVaryings.begin(); varying != mFragmentShader->mVaryings.end(); varying++) { if (varying->reg >= 0) { @@ -1402,20 +1560,27 @@ bool Program::linkVaryings() if (mFragmentShader->mUsesFragCoord) { mPixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n"; - if (sm3) { - mPixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n" - " gl_FragCoord.y = input.dx_VPos.y;\n"; - } else { - mPixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Viewport.x + dx_Viewport.z;\n" - " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Viewport.y + dx_Viewport.w;\n"; + + if (sm3) + { + // dx_Coord.y contains the render target height. See Context::applyRenderTarget() + mPixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n" + " gl_FragCoord.y = dx_Coord.y - input.dx_VPos.y - 0.5;\n"; } + else + { + // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget() + mPixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n" + " gl_FragCoord.y = -(input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n"; + } + mPixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_Depth.x + dx_Depth.y;\n" " gl_FragCoord.w = rhw;\n"; } if (mFragmentShader->mUsesPointCoord && sm3) { - mPixelHLSL += " gl_PointCoord = float2(input.gl_PointCoord.x, 1.0 - input.gl_PointCoord.y);\n"; + mPixelHLSL += " gl_PointCoord = input.gl_PointCoord;\n"; } if (mFragmentShader->mUsesFrontFacing) @@ -1423,7 +1588,7 @@ bool Program::linkVaryings() mPixelHLSL += " gl_FrontFacing = dx_PointsOrLines || (dx_FrontCCW ? (input.vFace >= 0.0) : (input.vFace <= 0.0));\n"; } - for (VaryingList::iterator varying = mFragmentShader->varyings.begin(); varying != mFragmentShader->varyings.end(); varying++) + for (VaryingList::iterator varying = mFragmentShader->mVaryings.begin(); varying != mFragmentShader->mVaryings.end(); varying++) { if (varying->reg >= 0) { @@ -1461,9 +1626,6 @@ bool Program::linkVaryings() " return output;\n" "}\n"; - TRACE("\n%s", mPixelHLSL.c_str()); - TRACE("\n%s", mVertexHLSL.c_str()); - return true; } @@ -1496,14 +1658,13 @@ void Program::link() const char *vertexProfile = context->supportsShaderModel3() ? "vs_3_0" : "vs_2_0"; const char *pixelProfile = context->supportsShaderModel3() ? "ps_3_0" : "ps_2_0"; - ID3DXBuffer *vertexBinary = compileToBinary(mVertexHLSL.c_str(), vertexProfile, &mConstantTableVS); - ID3DXBuffer *pixelBinary = compileToBinary(mPixelHLSL.c_str(), pixelProfile, &mConstantTablePS); + ID3D10Blob *vertexBinary = compileToBinary(mVertexHLSL.c_str(), vertexProfile, &mConstantTableVS); + ID3D10Blob *pixelBinary = compileToBinary(mPixelHLSL.c_str(), pixelProfile, &mConstantTablePS); if (vertexBinary && pixelBinary) { - IDirect3DDevice9 *device = getDevice(); - HRESULT vertexResult = device->CreateVertexShader((DWORD*)vertexBinary->GetBufferPointer(), &mVertexExecutable); - HRESULT pixelResult = device->CreatePixelShader((DWORD*)pixelBinary->GetBufferPointer(), &mPixelExecutable); + HRESULT vertexResult = mDevice->CreateVertexShader((DWORD*)vertexBinary->GetBufferPointer(), &mVertexExecutable); + HRESULT pixelResult = mDevice->CreatePixelShader((DWORD*)pixelBinary->GetBufferPointer(), &mPixelExecutable); if (vertexResult == D3DERR_OUTOFVIDEOMEMORY || vertexResult == E_OUTOFMEMORY || pixelResult == D3DERR_OUTOFVIDEOMEMORY || pixelResult == E_OUTOFMEMORY) { @@ -1536,12 +1697,12 @@ void Program::link() // these uniforms are searched as already-decorated because gl_ and dx_ // are reserved prefixes, and do not receive additional decoration - mDxDepthRangeLocation = getUniformLocation("dx_DepthRange", true); - mDxDepthLocation = getUniformLocation("dx_Depth", true); - mDxViewportLocation = getUniformLocation("dx_Viewport", true); - mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize", true); - mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW", true); - mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines", true); + mDxDepthRangeLocation = getUniformLocation("dx_DepthRange"); + mDxDepthLocation = getUniformLocation("dx_Depth"); + mDxCoordLocation = getUniformLocation("dx_Coord"); + mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize"); + mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW"); + mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines"); mLinked = true; // Success } @@ -1642,7 +1803,8 @@ bool Program::linkUniforms(ID3DXConstantTable *constantTable) for (unsigned int constantIndex = 0; constantIndex < constantTableDescription.Constants; constantIndex++) { D3DXHANDLE constantHandle = constantTable->GetConstant(0, constantIndex); - constantTable->GetConstantDesc(constantHandle, &constantDescription, &descriptionCount); + HRESULT result = constantTable->GetConstantDesc(constantHandle, &constantDescription, &descriptionCount); + ASSERT(SUCCEEDED(result)); if (!defineUniform(constantHandle, constantDescription)) { @@ -1659,14 +1821,46 @@ bool Program::defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT { if (constantDescription.RegisterSet == D3DXRS_SAMPLER) { - for (unsigned int samplerIndex = constantDescription.RegisterIndex; samplerIndex < constantDescription.RegisterIndex + constantDescription.RegisterCount; samplerIndex++) + for (unsigned int i = 0; i < constantDescription.RegisterCount; i++) { - ASSERT(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0])); + D3DXHANDLE psConstant = mConstantTablePS->GetConstantByName(NULL, constantDescription.Name); + D3DXHANDLE vsConstant = mConstantTableVS->GetConstantByName(NULL, constantDescription.Name); - mSamplers[samplerIndex].active = true; - mSamplers[samplerIndex].type = (constantDescription.Type == D3DXPT_SAMPLERCUBE) ? SAMPLER_CUBE : SAMPLER_2D; - mSamplers[samplerIndex].logicalTextureUnit = 0; - mSamplers[samplerIndex].dirty = true; + if (psConstant) + { + unsigned int samplerIndex = mConstantTablePS->GetSamplerIndex(psConstant) + i; + + if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS) + { + mSamplersPS[samplerIndex].active = true; + mSamplersPS[samplerIndex].textureType = (constantDescription.Type == D3DXPT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D; + mSamplersPS[samplerIndex].logicalTextureUnit = 0; + mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange); + } + else + { + appendToInfoLog("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS); + return false; + } + } + + if (vsConstant) + { + unsigned int samplerIndex = mConstantTableVS->GetSamplerIndex(vsConstant) + i; + + if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits()) + { + mSamplersVS[samplerIndex].active = true; + mSamplersVS[samplerIndex].textureType = (constantDescription.Type == D3DXPT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D; + mSamplersVS[samplerIndex].logicalTextureUnit = 0; + mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange); + } + else + { + appendToInfoLog("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits()); + return false; + } + } } } @@ -1683,7 +1877,8 @@ bool Program::defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT D3DXCONSTANT_DESC fieldDescription; UINT descriptionCount = 1; - mConstantTablePS->GetConstantDesc(fieldHandle, &fieldDescription, &descriptionCount); + HRESULT result = mConstantTablePS->GetConstantDesc(fieldHandle, &fieldDescription, &descriptionCount); + ASSERT(SUCCEEDED(result)); std::string structIndex = (constantDescription.Elements > 1) ? ("[" + str(arrayIndex) + "]") : ""; @@ -1707,9 +1902,9 @@ bool Program::defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT } } -bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name) +bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, const std::string &_name) { - Uniform *uniform = createUniform(constantDescription, name); + Uniform *uniform = createUniform(constantDescription, _name); if(!uniform) { @@ -1717,7 +1912,7 @@ bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::s } // Check if already defined - GLint location = getUniformLocation(name.c_str(), true); + GLint location = getUniformLocation(uniform->name); GLenum type = uniform->type; if (location >= 0) @@ -1734,18 +1929,21 @@ bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::s } } + initializeConstantHandles(uniform, &uniform->ps, mConstantTablePS); + initializeConstantHandles(uniform, &uniform->vs, mConstantTableVS); + mUniforms.push_back(uniform); unsigned int uniformIndex = mUniforms.size() - 1; for (unsigned int i = 0; i < uniform->arraySize; ++i) { - mUniformIndex.push_back(UniformLocation(name, i, uniformIndex)); + mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex)); } return true; } -Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name) +Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, const std::string &_name) { if (constantDescription.Rows == 1) // Vectors and scalars { @@ -1754,44 +1952,44 @@ Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, st case D3DXPT_SAMPLER2D: switch (constantDescription.Columns) { - case 1: return new Uniform(GL_SAMPLER_2D, name, constantDescription.Elements); + case 1: return new Uniform(GL_SAMPLER_2D, _name, constantDescription.Elements); default: UNREACHABLE(); } break; case D3DXPT_SAMPLERCUBE: switch (constantDescription.Columns) { - case 1: return new Uniform(GL_SAMPLER_CUBE, name, constantDescription.Elements); + case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constantDescription.Elements); default: UNREACHABLE(); } break; case D3DXPT_BOOL: switch (constantDescription.Columns) { - case 1: return new Uniform(GL_BOOL, name, constantDescription.Elements); - case 2: return new Uniform(GL_BOOL_VEC2, name, constantDescription.Elements); - case 3: return new Uniform(GL_BOOL_VEC3, name, constantDescription.Elements); - case 4: return new Uniform(GL_BOOL_VEC4, name, constantDescription.Elements); + case 1: return new Uniform(GL_BOOL, _name, constantDescription.Elements); + case 2: return new Uniform(GL_BOOL_VEC2, _name, constantDescription.Elements); + case 3: return new Uniform(GL_BOOL_VEC3, _name, constantDescription.Elements); + case 4: return new Uniform(GL_BOOL_VEC4, _name, constantDescription.Elements); default: UNREACHABLE(); } break; case D3DXPT_INT: switch (constantDescription.Columns) { - case 1: return new Uniform(GL_INT, name, constantDescription.Elements); - case 2: return new Uniform(GL_INT_VEC2, name, constantDescription.Elements); - case 3: return new Uniform(GL_INT_VEC3, name, constantDescription.Elements); - case 4: return new Uniform(GL_INT_VEC4, name, constantDescription.Elements); + case 1: return new Uniform(GL_INT, _name, constantDescription.Elements); + case 2: return new Uniform(GL_INT_VEC2, _name, constantDescription.Elements); + case 3: return new Uniform(GL_INT_VEC3, _name, constantDescription.Elements); + case 4: return new Uniform(GL_INT_VEC4, _name, constantDescription.Elements); default: UNREACHABLE(); } break; case D3DXPT_FLOAT: switch (constantDescription.Columns) { - case 1: return new Uniform(GL_FLOAT, name, constantDescription.Elements); - case 2: return new Uniform(GL_FLOAT_VEC2, name, constantDescription.Elements); - case 3: return new Uniform(GL_FLOAT_VEC3, name, constantDescription.Elements); - case 4: return new Uniform(GL_FLOAT_VEC4, name, constantDescription.Elements); + case 1: return new Uniform(GL_FLOAT, _name, constantDescription.Elements); + case 2: return new Uniform(GL_FLOAT_VEC2, _name, constantDescription.Elements); + case 3: return new Uniform(GL_FLOAT_VEC3, _name, constantDescription.Elements); + case 4: return new Uniform(GL_FLOAT_VEC4, _name, constantDescription.Elements); default: UNREACHABLE(); } break; @@ -1806,9 +2004,9 @@ Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, st case D3DXPT_FLOAT: switch (constantDescription.Rows) { - case 2: return new Uniform(GL_FLOAT_MAT2, name, constantDescription.Elements); - case 3: return new Uniform(GL_FLOAT_MAT3, name, constantDescription.Elements); - case 4: return new Uniform(GL_FLOAT_MAT4, name, constantDescription.Elements); + case 2: return new Uniform(GL_FLOAT_MAT2, _name, constantDescription.Elements); + case 3: return new Uniform(GL_FLOAT_MAT3, _name, constantDescription.Elements); + case 4: return new Uniform(GL_FLOAT_MAT4, _name, constantDescription.Elements); default: UNREACHABLE(); } break; @@ -1821,410 +2019,125 @@ Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, st } // This method needs to match OutputHLSL::decorate -std::string Program::decorate(const std::string &string) -{ - if (string.substr(0, 3) != "gl_" && string.substr(0, 3) != "dx_") - { - return "_" + string; - } - else - { - return string; - } -} - -std::string Program::undecorate(const std::string &string) +std::string Program::decorateAttribute(const std::string &name) { - if (string.substr(0, 1) == "_") + if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0) { - return string.substr(1); + return "_" + name; } - else - { - return string; - } -} - -bool Program::applyUniform1bv(GLint location, GLsizei count, const GLboolean *v) -{ - BOOL *vector = new BOOL[count]; - for (int i = 0; i < count; i++) - { - if (v[i] == GL_FALSE) - vector[i] = 0; - else - vector[i] = 1; - } - - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - mConstantTablePS->SetBoolArray(device, constantPS, vector, count); - } - - if (constantVS) - { - mConstantTableVS->SetBoolArray(device, constantVS, vector, count); - } - - delete [] vector; - - return true; -} - -bool Program::applyUniform2bv(GLint location, GLsizei count, const GLboolean *v) -{ - D3DXVECTOR4 *vector = new D3DXVECTOR4[count]; - - for (int i = 0; i < count; i++) - { - vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f), - (v[1] == GL_FALSE ? 0.0f : 1.0f), 0, 0); - - v += 2; - } - - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - mConstantTablePS->SetVectorArray(device, constantPS, vector, count); - } - - if (constantVS) - { - mConstantTableVS->SetVectorArray(device, constantVS, vector, count); - } - - delete[] vector; - - return true; -} - -bool Program::applyUniform3bv(GLint location, GLsizei count, const GLboolean *v) -{ - D3DXVECTOR4 *vector = new D3DXVECTOR4[count]; - - for (int i = 0; i < count; i++) - { - vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f), - (v[1] == GL_FALSE ? 0.0f : 1.0f), - (v[2] == GL_FALSE ? 0.0f : 1.0f), 0); - - v += 3; - } - - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - mConstantTablePS->SetVectorArray(device, constantPS, vector, count); - } - - if (constantVS) - { - mConstantTableVS->SetVectorArray(device, constantVS, vector, count); - } - - delete[] vector; - - return true; -} - -bool Program::applyUniform4bv(GLint location, GLsizei count, const GLboolean *v) -{ - D3DXVECTOR4 *vector = new D3DXVECTOR4[count]; - - for (int i = 0; i < count; i++) - { - vector[i] = D3DXVECTOR4((v[0] == GL_FALSE ? 0.0f : 1.0f), - (v[1] == GL_FALSE ? 0.0f : 1.0f), - (v[2] == GL_FALSE ? 0.0f : 1.0f), - (v[3] == GL_FALSE ? 0.0f : 1.0f)); - - v += 3; - } - - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - mConstantTablePS->SetVectorArray(device, constantPS, vector, count); - } - - if (constantVS) - { - mConstantTableVS->SetVectorArray(device, constantVS, vector, count); - } - - delete [] vector; - - return true; -} - -bool Program::applyUniform1fv(GLint location, GLsizei count, const GLfloat *v) -{ - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - mConstantTablePS->SetFloatArray(device, constantPS, v, count); - } - - if (constantVS) - { - mConstantTableVS->SetFloatArray(device, constantVS, v, count); - } - - return true; + + return name; } -bool Program::applyUniform2fv(GLint location, GLsizei count, const GLfloat *v) +std::string Program::undecorateUniform(const std::string &_name) { - D3DXVECTOR4 *vector = new D3DXVECTOR4[count]; - - for (int i = 0; i < count; i++) + if (_name[0] == '_') { - vector[i] = D3DXVECTOR4(v[0], v[1], 0, 0); - - v += 2; + return _name.substr(1); } - - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - mConstantTablePS->SetVectorArray(device, constantPS, vector, count); - } - - if (constantVS) + else if (_name.compare(0, 3, "ar_") == 0) { - mConstantTableVS->SetVectorArray(device, constantVS, vector, count); + return _name.substr(3); } - - delete[] vector; - - return true; + + return _name; } -bool Program::applyUniform3fv(GLint location, GLsizei count, const GLfloat *v) +void Program::applyUniformnbv(Uniform *targetUniform, GLsizei count, int width, const GLboolean *v) { - D3DXVECTOR4 *vector = new D3DXVECTOR4[count]; + float vector[D3D9_MAX_FLOAT_CONSTANTS * 4]; + BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS]; - for (int i = 0; i < count; i++) + if (targetUniform->ps.registerCount && targetUniform->ps.registerSet == D3DXRS_FLOAT4 || + targetUniform->vs.registerCount && targetUniform->vs.registerSet == D3DXRS_FLOAT4) { - vector[i] = D3DXVECTOR4(v[0], v[1], v[2], 0); - - v += 3; - } - - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - mConstantTablePS->SetVectorArray(device, constantPS, vector, count); + ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS); + for (int i = 0; i < count; i++) + { + for (int j = 0; j < 4; j++) + { + if (j < width) + { + vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f; + } + else + { + vector[i * 4 + j] = 0.0f; + } + } + } } - if (constantVS) + if (targetUniform->ps.registerCount && targetUniform->ps.registerSet == D3DXRS_BOOL || + targetUniform->vs.registerCount && targetUniform->vs.registerSet == D3DXRS_BOOL) { - mConstantTableVS->SetVectorArray(device, constantVS, vector, count); + int psCount = targetUniform->ps.registerSet == D3DXRS_BOOL ? targetUniform->ps.registerCount : 0; + int vsCount = targetUniform->vs.registerSet == D3DXRS_BOOL ? targetUniform->vs.registerCount : 0; + int copyCount = std::min(count * width, std::max(psCount, vsCount)); + ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS); + for (int i = 0; i < copyCount; i++) + { + boolVector[i] = v[i] != GL_FALSE; + } } - delete[] vector; - - return true; -} - -bool Program::applyUniform4fv(GLint location, GLsizei count, const GLfloat *v) -{ - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) + if (targetUniform->ps.registerCount) { - mConstantTablePS->SetVectorArray(device, constantPS, (D3DXVECTOR4*)v, count); + if (targetUniform->ps.registerSet == D3DXRS_FLOAT4) + { + mDevice->SetPixelShaderConstantF(targetUniform->ps.registerIndex, vector, targetUniform->ps.registerCount); + } + else if (targetUniform->ps.registerSet == D3DXRS_BOOL) + { + mDevice->SetPixelShaderConstantB(targetUniform->ps.registerIndex, boolVector, targetUniform->ps.registerCount); + } + else UNREACHABLE(); } - if (constantVS) + if (targetUniform->vs.registerCount) { - mConstantTableVS->SetVectorArray(device, constantVS, (D3DXVECTOR4*)v, count); + if (targetUniform->vs.registerSet == D3DXRS_FLOAT4) + { + mDevice->SetVertexShaderConstantF(targetUniform->vs.registerIndex, vector, targetUniform->vs.registerCount); + } + else if (targetUniform->vs.registerSet == D3DXRS_BOOL) + { + mDevice->SetVertexShaderConstantB(targetUniform->vs.registerIndex, boolVector, targetUniform->vs.registerCount); + } + else UNREACHABLE(); } - - return true; } -bool Program::applyUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value) +bool Program::applyUniformnfv(Uniform *targetUniform, const GLfloat *v) { - D3DXMATRIX *matrix = new D3DXMATRIX[count]; - - for (int i = 0; i < count; i++) + if (targetUniform->ps.registerCount) { - matrix[i] = D3DXMATRIX(value[0], value[2], 0, 0, - value[1], value[3], 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1); - - value += 4; - } - - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count); + mDevice->SetPixelShaderConstantF(targetUniform->ps.registerIndex, v, targetUniform->ps.registerCount); } - if (constantVS) + if (targetUniform->vs.registerCount) { - mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count); + mDevice->SetVertexShaderConstantF(targetUniform->vs.registerIndex, v, targetUniform->vs.registerCount); } - delete[] matrix; - return true; } -bool Program::applyUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value) +bool Program::applyUniform1iv(Uniform *targetUniform, GLsizei count, const GLint *v) { - D3DXMATRIX *matrix = new D3DXMATRIX[count]; + ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS); + D3DXVECTOR4 vector[D3D9_MAX_FLOAT_CONSTANTS]; for (int i = 0; i < count; i++) { - matrix[i] = D3DXMATRIX(value[0], value[3], value[6], 0, - value[1], value[4], value[7], 0, - value[2], value[5], value[8], 0, - 0, 0, 0, 1); - - value += 9; - } - - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count); + vector[i] = D3DXVECTOR4((float)v[i], 0, 0, 0); } - if (constantVS) + if (targetUniform->ps.registerCount) { - mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count); - } - - delete[] matrix; - - return true; -} - -bool Program::applyUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value) -{ - D3DXMATRIX *matrix = new D3DXMATRIX[count]; - - for (int i = 0; i < count; i++) - { - matrix[i] = D3DXMATRIX(value[0], value[4], value[8], value[12], - value[1], value[5], value[9], value[13], - value[2], value[6], value[10], value[14], - value[3], value[7], value[11], value[15]); - - value += 16; - } - - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count); - } - - if (constantVS) - { - mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count); - } - - delete[] matrix; - - return true; -} - -bool Program::applyUniform1iv(GLint location, GLsizei count, const GLint *v) -{ - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - D3DXCONSTANT_DESC constantDescription; - UINT descriptionCount = 1; - HRESULT result = mConstantTablePS->GetConstantDesc(constantPS, &constantDescription, &descriptionCount); - - if (FAILED(result)) - { - return false; - } - - if (constantDescription.RegisterSet == D3DXRS_SAMPLER) + if (targetUniform->ps.registerSet == D3DXRS_SAMPLER) { - unsigned int firstIndex = mConstantTablePS->GetSamplerIndex(constantPS); + unsigned int firstIndex = targetUniform->ps.registerIndex; for (int i = 0; i < count; i++) { @@ -2232,32 +2145,49 @@ bool Program::applyUniform1iv(GLint location, GLsizei count, const GLint *v) if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS) { - ASSERT(mSamplers[samplerIndex].active); - mSamplers[samplerIndex].logicalTextureUnit = v[i]; - mSamplers[samplerIndex].dirty = true; + ASSERT(mSamplersPS[samplerIndex].active); + mSamplersPS[samplerIndex].logicalTextureUnit = v[i]; } } - - return true; + } + else + { + ASSERT(targetUniform->ps.registerSet == D3DXRS_FLOAT4); + mDevice->SetPixelShaderConstantF(targetUniform->ps.registerIndex, (const float*)vector, targetUniform->ps.registerCount); } } - if (constantPS) + if (targetUniform->vs.registerCount) { - mConstantTablePS->SetIntArray(device, constantPS, v, count); - } + if (targetUniform->vs.registerSet == D3DXRS_SAMPLER) + { + unsigned int firstIndex = targetUniform->vs.registerIndex; - if (constantVS) - { - mConstantTableVS->SetIntArray(device, constantVS, v, count); + for (int i = 0; i < count; i++) + { + unsigned int samplerIndex = firstIndex + i; + + if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF) + { + ASSERT(mSamplersVS[samplerIndex].active); + mSamplersVS[samplerIndex].logicalTextureUnit = v[i]; + } + } + } + else + { + ASSERT(targetUniform->vs.registerSet == D3DXRS_FLOAT4); + mDevice->SetVertexShaderConstantF(targetUniform->vs.registerIndex, (const float *)vector, targetUniform->vs.registerCount); + } } return true; } -bool Program::applyUniform2iv(GLint location, GLsizei count, const GLint *v) +bool Program::applyUniform2iv(Uniform *targetUniform, GLsizei count, const GLint *v) { - D3DXVECTOR4 *vector = new D3DXVECTOR4[count]; + ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS); + D3DXVECTOR4 vector[D3D9_MAX_FLOAT_CONSTANTS]; for (int i = 0; i < count; i++) { @@ -2266,31 +2196,15 @@ bool Program::applyUniform2iv(GLint location, GLsizei count, const GLint *v) v += 2; } - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - mConstantTablePS->SetVectorArray(device, constantPS, vector, count); - } - - if (constantVS) - { - mConstantTableVS->SetVectorArray(device, constantVS, vector, count); - } - - delete[] vector; + applyUniformniv(targetUniform, count, vector); return true; } -bool Program::applyUniform3iv(GLint location, GLsizei count, const GLint *v) +bool Program::applyUniform3iv(Uniform *targetUniform, GLsizei count, const GLint *v) { - D3DXVECTOR4 *vector = new D3DXVECTOR4[count]; + ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS); + D3DXVECTOR4 vector[D3D9_MAX_FLOAT_CONSTANTS]; for (int i = 0; i < count; i++) { @@ -2299,31 +2213,15 @@ bool Program::applyUniform3iv(GLint location, GLsizei count, const GLint *v) v += 3; } - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; - - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); - - if (constantPS) - { - mConstantTablePS->SetVectorArray(device, constantPS, vector, count); - } - - if (constantVS) - { - mConstantTableVS->SetVectorArray(device, constantVS, vector, count); - } - - delete[] vector; + applyUniformniv(targetUniform, count, vector); return true; } -bool Program::applyUniform4iv(GLint location, GLsizei count, const GLint *v) +bool Program::applyUniform4iv(Uniform *targetUniform, GLsizei count, const GLint *v) { - D3DXVECTOR4 *vector = new D3DXVECTOR4[count]; + ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS); + D3DXVECTOR4 vector[D3D9_MAX_FLOAT_CONSTANTS]; for (int i = 0; i < count; i++) { @@ -2332,26 +2230,45 @@ bool Program::applyUniform4iv(GLint location, GLsizei count, const GLint *v) v += 4; } - Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; + applyUniformniv(targetUniform, count, vector); - D3DXHANDLE constantPS; - D3DXHANDLE constantVS; - getConstantHandles(targetUniform, &constantPS, &constantVS); - IDirect3DDevice9 *device = getDevice(); + return true; +} - if (constantPS) +void Program::applyUniformniv(Uniform *targetUniform, GLsizei count, const D3DXVECTOR4 *vector) +{ + if (targetUniform->ps.registerCount) { - mConstantTablePS->SetVectorArray(device, constantPS, vector, count); + ASSERT(targetUniform->ps.registerSet == D3DXRS_FLOAT4); + mDevice->SetPixelShaderConstantF(targetUniform->ps.registerIndex, (const float *)vector, targetUniform->ps.registerCount); } - if (constantVS) + if (targetUniform->vs.registerCount) { - mConstantTableVS->SetVectorArray(device, constantVS, vector, count); + ASSERT(targetUniform->vs.registerSet == D3DXRS_FLOAT4); + mDevice->SetVertexShaderConstantF(targetUniform->vs.registerIndex, (const float *)vector, targetUniform->vs.registerCount); } +} - delete [] vector; +// append a santized message to the program info log. +// The D3D compiler includes a fake file path in some of the warning or error +// messages, so lets remove all occurrences of this fake file path from the log. +void Program::appendToInfoLogSanitized(const char *message) +{ + std::string msg(message); - return true; + size_t found; + do + { + found = msg.find(fakepath); + if (found != std::string::npos) + { + msg.erase(found, strlen(fakepath)); + } + } + while (found != std::string::npos); + + appendToInfoLog("%s\n", msg.c_str()); } void Program::appendToInfoLog(const char *format, ...) @@ -2396,7 +2313,7 @@ void Program::resetInfoLog() } } -// Returns the program object to an unlinked state, after detaching a shader, before re-linking, or at destruction +// Returns the program object to an unlinked state, before re-linking, or at destruction void Program::unlink(bool destroy) { if (destroy) // Object being destructed @@ -2446,10 +2363,17 @@ void Program::unlink(bool destroy) for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++) { - mSamplers[index].active = false; - mSamplers[index].dirty = true; + mSamplersPS[index].active = false; } + for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++) + { + mSamplersVS[index].active = false; + } + + mUsedVertexSamplerRange = 0; + mUsedPixelSamplerRange = 0; + while (!mUniforms.empty()) { delete mUniforms.back(); @@ -2458,7 +2382,7 @@ void Program::unlink(bool destroy) mDxDepthRangeLocation = -1; mDxDepthLocation = -1; - mDxViewportLocation = -1; + mDxCoordLocation = -1; mDxHalfPixelSizeLocation = -1; mDxFrontCCWLocation = -1; mDxPointsOrLinesLocation = -1; @@ -2532,11 +2456,8 @@ void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) if (mInfoLog) { - while (index < bufSize - 1 && index < (int)strlen(mInfoLog)) - { - infoLog[index] = mInfoLog[index]; - index++; - } + index = std::min(bufSize - 1, (int)strlen(mInfoLog)); + memcpy(infoLog, mInfoLog, index); } if (bufSize) @@ -2655,7 +2576,7 @@ void Program::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, G unsigned int uniform; for (uniform = 0; uniform < mUniforms.size(); uniform++) { - if (mUniforms[uniform]->name.substr(0, 3) == "dx_") + if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0) { continue; } @@ -2672,9 +2593,9 @@ void Program::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, G if (bufsize > 0) { - std::string string = undecorate(mUniforms[uniform]->name); + std::string string = mUniforms[uniform]->name; - if (mUniforms[uniform]->arraySize != 1) + if (mUniforms[uniform]->isArray()) { string += "[0]"; } @@ -2700,7 +2621,7 @@ GLint Program::getActiveUniformCount() unsigned int numUniforms = mUniforms.size(); for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++) { - if (mUniforms[uniformIndex]->name.substr(0, 3) != "dx_") + if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0) { count++; } @@ -2716,9 +2637,14 @@ GLint Program::getActiveUniformMaxLength() unsigned int numUniforms = mUniforms.size(); for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++) { - if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.substr(0, 3) != "dx_") + if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0) { - maxLength = std::max((int)(undecorate(mUniforms[uniformIndex]->name).length() + 1), maxLength); + int length = (int)(mUniforms[uniformIndex]->name.length() + 1); + if (mUniforms[uniformIndex]->isArray()) + { + length += 3; // Counting in "[0]". + } + maxLength = std::max(length, maxLength); } } @@ -2747,9 +2673,8 @@ void Program::validate() else { applyUniforms(); - if (!validateSamplers()) + if (!validateSamplers(true)) { - appendToInfoLog("Samplers of conflicting types refer to the same texture image unit."); mValidated = false; } else @@ -2759,24 +2684,86 @@ void Program::validate() } } -bool Program::validateSamplers() const +bool Program::validateSamplers(bool logErrors) { // if any two active samplers in a program are of different types, but refer to the same // texture image unit, and this is the current program, then ValidateProgram will fail, and // DrawArrays and DrawElements will issue the INVALID_OPERATION error. - std::map<int, SamplerType> samplerMap; - for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i) + + const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits(); + TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF]; + + for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i) + { + textureUnitType[i] = TEXTURE_UNKNOWN; + } + + for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i) { - if (mSamplers[i].active) + if (mSamplersPS[i].active) { - if (samplerMap.find(mSamplers[i].logicalTextureUnit) != samplerMap.end()) + unsigned int unit = mSamplersPS[i].logicalTextureUnit; + + if (unit >= maxCombinedTextureImageUnits) { - if (mSamplers[i].type != samplerMap[mSamplers[i].logicalTextureUnit]) + if (logErrors) + { + appendToInfoLog("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits); + } + + return false; + } + + if (textureUnitType[unit] != TEXTURE_UNKNOWN) + { + if (mSamplersPS[i].textureType != textureUnitType[unit]) + { + if (logErrors) + { + appendToInfoLog("Samplers of conflicting types refer to the same texture image unit (%d).", unit); + } + return false; + } } else { - samplerMap[mSamplers[i].logicalTextureUnit] = mSamplers[i].type; + textureUnitType[unit] = mSamplersPS[i].textureType; + } + } + } + + for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i) + { + if (mSamplersVS[i].active) + { + unsigned int unit = mSamplersVS[i].logicalTextureUnit; + + if (unit >= maxCombinedTextureImageUnits) + { + if (logErrors) + { + appendToInfoLog("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits); + } + + return false; + } + + if (textureUnitType[unit] != TEXTURE_UNKNOWN) + { + if (mSamplersVS[i].textureType != textureUnitType[unit]) + { + if (logErrors) + { + appendToInfoLog("Samplers of conflicting types refer to the same texture image unit (%d).", unit); + } + + return false; + } + } + else + { + textureUnitType[unit] = mSamplersVS[i].textureType; } } } @@ -2784,17 +2771,23 @@ bool Program::validateSamplers() const return true; } -void Program::getConstantHandles(Uniform *targetUniform, D3DXHANDLE *constantPS, D3DXHANDLE *constantVS) +void Program::initializeConstantHandles(Uniform *targetUniform, Uniform::RegisterInfo *ri, ID3DXConstantTable *constantTable) { - if (!targetUniform->handlesSet) + D3DXHANDLE handle = constantTable->GetConstantByName(0, targetUniform->_name.c_str()); + if (handle) + { + UINT descriptionCount = 1; + D3DXCONSTANT_DESC constantDescription; + HRESULT result = constantTable->GetConstantDesc(handle, &constantDescription, &descriptionCount); + ASSERT(SUCCEEDED(result)); + ri->registerIndex = constantDescription.RegisterIndex; + ri->registerCount = constantDescription.RegisterCount; + ri->registerSet = constantDescription.RegisterSet; + } + else { - targetUniform->psHandle = mConstantTablePS->GetConstantByName(0, targetUniform->name.c_str()); - targetUniform->vsHandle = mConstantTableVS->GetConstantByName(0, targetUniform->name.c_str()); - targetUniform->handlesSet = true; + ri->registerCount = 0; } - - *constantPS = targetUniform->psHandle; - *constantVS = targetUniform->vsHandle; } GLint Program::getDxDepthRangeLocation() const @@ -2807,9 +2800,9 @@ GLint Program::getDxDepthLocation() const return mDxDepthLocation; } -GLint Program::getDxViewportLocation() const +GLint Program::getDxCoordLocation() const { - return mDxViewportLocation; + return mDxCoordLocation; } GLint Program::getDxHalfPixelSizeLocation() const |