Commit 1a8a7e3b by Brandon Jones

Moving Uniform handling into ProgramD3D.

Moves the last references to HLSLBlockEncoder out of ProgramBinary. Only one reference to ShaderD3D remains. BUG=angle:731 Change-Id: Ie23e24e09fbed4c8c5fab3bab814b7092a383c7f Reviewed-on: https://chromium-review.googlesource.com/220940Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Tested-by: 's avatarBrandon Jones <bajones@chromium.org>
parent 8becd0cb
...@@ -455,6 +455,7 @@ ...@@ -455,6 +455,7 @@
<ClCompile Include="..\..\src\libGLESv2\queryconversions.cpp"/> <ClCompile Include="..\..\src\libGLESv2\queryconversions.cpp"/>
<ClCompile Include="..\..\src\libGLESv2\renderer\Image.cpp"/> <ClCompile Include="..\..\src\libGLESv2\renderer\Image.cpp"/>
<ClCompile Include="..\..\src\libGLESv2\renderer\IndexRangeCache.cpp"/> <ClCompile Include="..\..\src\libGLESv2\renderer\IndexRangeCache.cpp"/>
<ClCompile Include="..\..\src\libGLESv2\renderer\ProgramImpl.cpp"/>
<ClCompile Include="..\..\src\libGLESv2\renderer\Renderer.cpp"/> <ClCompile Include="..\..\src\libGLESv2\renderer\Renderer.cpp"/>
<ClCompile Include="..\..\src\libGLESv2\renderer\copyimage.cpp"/> <ClCompile Include="..\..\src\libGLESv2\renderer\copyimage.cpp"/>
<ClCompile Include="..\..\src\libGLESv2\renderer\loadimage.cpp"/> <ClCompile Include="..\..\src\libGLESv2\renderer\loadimage.cpp"/>
......
...@@ -805,6 +805,15 @@ ...@@ -805,6 +805,15 @@
<Filter Include="src\libGLESv2"> <Filter Include="src\libGLESv2">
<UniqueIdentifier>{A62A9415-2E9D-A6D2-631D-1F25A5CD626F}</UniqueIdentifier> <UniqueIdentifier>{A62A9415-2E9D-A6D2-631D-1F25A5CD626F}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="src\libGLESv2\renderer">
<UniqueIdentifier>{AC4EF684-2900-10EA-3D11-A6DF0901358C}</UniqueIdentifier>
</Filter>
<Filter Include="src">
<UniqueIdentifier>{8CDEE807-BC53-E450-C8B8-4DEBB66742D4}</UniqueIdentifier>
</Filter>
<Filter Include="src\libGLESv2">
<UniqueIdentifier>{A62A9415-2E9D-A6D2-631D-1F25A5CD626F}</UniqueIdentifier>
</Filter>
<Filter Include="src"> <Filter Include="src">
<UniqueIdentifier>{8CDEE807-BC53-E450-C8B8-4DEBB66742D4}</UniqueIdentifier> <UniqueIdentifier>{8CDEE807-BC53-E450-C8B8-4DEBB66742D4}</UniqueIdentifier>
</Filter> </Filter>
...@@ -3723,6 +3732,9 @@ ...@@ -3723,6 +3732,9 @@
<ClInclude Include="..\..\src\libGLESv2\renderer\IndexRangeCache.h"> <ClInclude Include="..\..\src\libGLESv2\renderer\IndexRangeCache.h">
<Filter>src\libGLESv2\renderer</Filter> <Filter>src\libGLESv2\renderer</Filter>
</ClInclude> </ClInclude>
<ClCompile Include="..\..\src\libGLESv2\renderer\ProgramImpl.cpp">
<Filter>src\libGLESv2\renderer</Filter>
</ClCompile>
<ClInclude Include="..\..\src\libGLESv2\renderer\ProgramImpl.h"> <ClInclude Include="..\..\src\libGLESv2\renderer\ProgramImpl.h">
<Filter>src\libGLESv2\renderer</Filter> <Filter>src\libGLESv2\renderer</Filter>
</ClInclude> </ClInclude>
......
...@@ -549,6 +549,7 @@ ...@@ -549,6 +549,7 @@
<ClCompile Include="..\..\..\..\src\libGLESv2\queryconversions.cpp"/> <ClCompile Include="..\..\..\..\src\libGLESv2\queryconversions.cpp"/>
<ClCompile Include="..\..\..\..\src\libGLESv2\renderer\Image.cpp"/> <ClCompile Include="..\..\..\..\src\libGLESv2\renderer\Image.cpp"/>
<ClCompile Include="..\..\..\..\src\libGLESv2\renderer\IndexRangeCache.cpp"/> <ClCompile Include="..\..\..\..\src\libGLESv2\renderer\IndexRangeCache.cpp"/>
<ClCompile Include="..\..\..\..\src\libGLESv2\renderer\ProgramImpl.cpp"/>
<ClCompile Include="..\..\..\..\src\libGLESv2\renderer\Renderer.cpp"/> <ClCompile Include="..\..\..\..\src\libGLESv2\renderer\Renderer.cpp"/>
<ClCompile Include="..\..\..\..\src\libGLESv2\renderer\copyimage.cpp"/> <ClCompile Include="..\..\..\..\src\libGLESv2\renderer\copyimage.cpp"/>
<ClCompile Include="..\..\..\..\src\libGLESv2\renderer\loadimage.cpp"/> <ClCompile Include="..\..\..\..\src\libGLESv2\renderer\loadimage.cpp"/>
......
...@@ -805,6 +805,15 @@ ...@@ -805,6 +805,15 @@
<Filter Include="src\libGLESv2"> <Filter Include="src\libGLESv2">
<UniqueIdentifier>{A62A9415-2E9D-A6D2-631D-1F25A5CD626F}</UniqueIdentifier> <UniqueIdentifier>{A62A9415-2E9D-A6D2-631D-1F25A5CD626F}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="src\libGLESv2\renderer">
<UniqueIdentifier>{AC4EF684-2900-10EA-3D11-A6DF0901358C}</UniqueIdentifier>
</Filter>
<Filter Include="src">
<UniqueIdentifier>{8CDEE807-BC53-E450-C8B8-4DEBB66742D4}</UniqueIdentifier>
</Filter>
<Filter Include="src\libGLESv2">
<UniqueIdentifier>{A62A9415-2E9D-A6D2-631D-1F25A5CD626F}</UniqueIdentifier>
</Filter>
<Filter Include="src"> <Filter Include="src">
<UniqueIdentifier>{8CDEE807-BC53-E450-C8B8-4DEBB66742D4}</UniqueIdentifier> <UniqueIdentifier>{8CDEE807-BC53-E450-C8B8-4DEBB66742D4}</UniqueIdentifier>
</Filter> </Filter>
...@@ -3180,6 +3189,9 @@ ...@@ -3180,6 +3189,9 @@
<ClInclude Include="..\..\..\..\src\libGLESv2\renderer\IndexRangeCache.h"> <ClInclude Include="..\..\..\..\src\libGLESv2\renderer\IndexRangeCache.h">
<Filter>src\libGLESv2\renderer</Filter> <Filter>src\libGLESv2\renderer</Filter>
</ClInclude> </ClInclude>
<ClCompile Include="..\..\..\..\src\libGLESv2\renderer\ProgramImpl.cpp">
<Filter>src\libGLESv2\renderer</Filter>
</ClCompile>
<ClInclude Include="..\..\..\..\src\libGLESv2\renderer\ProgramImpl.h"> <ClInclude Include="..\..\..\..\src\libGLESv2\renderer\ProgramImpl.h">
<Filter>src\libGLESv2\renderer</Filter> <Filter>src\libGLESv2\renderer</Filter>
</ClInclude> </ClInclude>
......
...@@ -101,6 +101,7 @@ ...@@ -101,6 +101,7 @@
'libGLESv2/renderer/Image.h', 'libGLESv2/renderer/Image.h',
'libGLESv2/renderer/IndexRangeCache.cpp', 'libGLESv2/renderer/IndexRangeCache.cpp',
'libGLESv2/renderer/IndexRangeCache.h', 'libGLESv2/renderer/IndexRangeCache.h',
'libGLESv2/renderer/ProgramImpl.cpp',
'libGLESv2/renderer/ProgramImpl.h', 'libGLESv2/renderer/ProgramImpl.h',
'libGLESv2/renderer/QueryImpl.h', 'libGLESv2/renderer/QueryImpl.h',
'libGLESv2/renderer/RenderTarget.h', 'libGLESv2/renderer/RenderTarget.h',
......
...@@ -34,36 +34,6 @@ namespace gl ...@@ -34,36 +34,6 @@ namespace gl
namespace namespace
{ {
GLenum GetTextureType(GLenum samplerType)
{
switch (samplerType)
{
case GL_SAMPLER_2D:
case GL_INT_SAMPLER_2D:
case GL_UNSIGNED_INT_SAMPLER_2D:
case GL_SAMPLER_2D_SHADOW:
return GL_TEXTURE_2D;
case GL_SAMPLER_3D:
case GL_INT_SAMPLER_3D:
case GL_UNSIGNED_INT_SAMPLER_3D:
return GL_TEXTURE_3D;
case GL_SAMPLER_CUBE:
case GL_SAMPLER_CUBE_SHADOW:
return GL_TEXTURE_CUBE_MAP;
case GL_INT_SAMPLER_CUBE:
case GL_UNSIGNED_INT_SAMPLER_CUBE:
return GL_TEXTURE_CUBE_MAP;
case GL_SAMPLER_2D_ARRAY:
case GL_INT_SAMPLER_2D_ARRAY:
case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
case GL_SAMPLER_2D_ARRAY_SHADOW:
return GL_TEXTURE_2D_ARRAY;
default: UNREACHABLE();
}
return GL_TEXTURE_2D;
}
unsigned int ParseAndStripArrayIndex(std::string* name) unsigned int ParseAndStripArrayIndex(std::string* name)
{ {
unsigned int subscript = GL_INVALID_INDEX; unsigned int subscript = GL_INVALID_INDEX;
...@@ -80,16 +50,6 @@ unsigned int ParseAndStripArrayIndex(std::string* name) ...@@ -80,16 +50,6 @@ unsigned int ParseAndStripArrayIndex(std::string* name)
return subscript; return subscript;
} }
bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
{
return var.isRowMajorLayout;
}
bool IsRowMajorLayout(const sh::ShaderVariable &var)
{
return false;
}
} }
VariableLocation::VariableLocation(const std::string &name, unsigned int element, unsigned int index) VariableLocation::VariableLocation(const std::string &name, unsigned int element, unsigned int index)
...@@ -118,9 +78,6 @@ unsigned int ProgramBinary::mCurrentSerial = 1; ...@@ -118,9 +78,6 @@ unsigned int ProgramBinary::mCurrentSerial = 1;
ProgramBinary::ProgramBinary(rx::ProgramImpl *impl) ProgramBinary::ProgramBinary(rx::ProgramImpl *impl)
: RefCountObject(0), : RefCountObject(0),
mProgram(impl), mProgram(impl),
mUsedVertexSamplerRange(0),
mUsedPixelSamplerRange(0),
mDirtySamplerMapping(true),
mValidated(false), mValidated(false),
mSerial(issueSerial()) mSerial(issueSerial())
{ {
...@@ -174,16 +131,7 @@ int ProgramBinary::getSemanticIndex(int attributeIndex) ...@@ -174,16 +131,7 @@ int ProgramBinary::getSemanticIndex(int attributeIndex)
// Returns one more than the highest sampler index used. // Returns one more than the highest sampler index used.
GLint ProgramBinary::getUsedSamplerRange(SamplerType type) GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
{ {
switch (type) return mProgram->getUsedSamplerRange(type);
{
case SAMPLER_PIXEL:
return mUsedPixelSamplerRange;
case SAMPLER_VERTEX:
return mUsedVertexSamplerRange;
default:
UNREACHABLE();
return 0;
}
} }
bool ProgramBinary::usesPointSize() const bool ProgramBinary::usesPointSize() const
...@@ -193,128 +141,32 @@ bool ProgramBinary::usesPointSize() const ...@@ -193,128 +141,32 @@ bool ProgramBinary::usesPointSize() const
GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex, const Caps &caps) GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex, const Caps &caps)
{ {
GLint logicalTextureUnit = -1; return mProgram->getSamplerMapping(type, samplerIndex, caps);
switch (type)
{
case SAMPLER_PIXEL:
ASSERT(samplerIndex < caps.maxTextureImageUnits);
if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
{
logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
}
break;
case SAMPLER_VERTEX:
ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
{
logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
}
break;
default: UNREACHABLE();
}
if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
{
return logicalTextureUnit;
}
return -1;
} }
// 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).
GLenum ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex) GLenum ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
{ {
switch (type) return mProgram->getSamplerTextureType(type, samplerIndex);
{
case SAMPLER_PIXEL:
ASSERT(samplerIndex < mSamplersPS.size());
ASSERT(mSamplersPS[samplerIndex].active);
return mSamplersPS[samplerIndex].textureType;
case SAMPLER_VERTEX:
ASSERT(samplerIndex < mSamplersVS.size());
ASSERT(mSamplersVS[samplerIndex].active);
return mSamplersVS[samplerIndex].textureType;
default: UNREACHABLE();
}
return GL_TEXTURE_2D;
} }
GLint ProgramBinary::getUniformLocation(std::string name) GLint ProgramBinary::getUniformLocation(std::string name)
{ {
unsigned int subscript = ParseAndStripArrayIndex(&name); return mProgram->getUniformLocation(name);
unsigned int numUniforms = mUniformIndex.size();
for (unsigned int location = 0; location < numUniforms; location++)
{
if (mUniformIndex[location].name == name)
{
const int index = mUniformIndex[location].index;
const bool isArray = mUniforms[index]->isArray();
if ((isArray && mUniformIndex[location].element == subscript) ||
(subscript == GL_INVALID_INDEX))
{
return location;
}
}
}
return -1;
} }
GLuint ProgramBinary::getUniformIndex(std::string name) GLuint ProgramBinary::getUniformIndex(std::string name)
{ {
unsigned int subscript = ParseAndStripArrayIndex(&name); return mProgram->getUniformIndex(name);
// The app is not allowed to specify array indices other than 0 for arrays of basic types
if (subscript != 0 && subscript != GL_INVALID_INDEX)
{
return GL_INVALID_INDEX;
}
unsigned int numUniforms = mUniforms.size();
for (unsigned int index = 0; index < numUniforms; index++)
{
if (mUniforms[index]->name == name)
{
if (mUniforms[index]->isArray() || subscript == GL_INVALID_INDEX)
{
return index;
}
}
}
return GL_INVALID_INDEX;
} }
GLuint ProgramBinary::getUniformBlockIndex(std::string name) GLuint ProgramBinary::getUniformBlockIndex(std::string name)
{ {
unsigned int subscript = ParseAndStripArrayIndex(&name); return mProgram->getUniformBlockIndex(name);
unsigned int numUniformBlocks = mUniformBlocks.size();
for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
{
const UniformBlock &uniformBlock = *mUniformBlocks[blockIndex];
if (uniformBlock.name == name)
{
const bool arrayElementZero = (subscript == GL_INVALID_INDEX && uniformBlock.elementIndex == 0);
if (subscript == uniformBlock.elementIndex || arrayElementZero)
{
return blockIndex;
}
}
}
return GL_INVALID_INDEX;
} }
UniformBlock *ProgramBinary::getUniformBlockByIndex(GLuint blockIndex) UniformBlock *ProgramBinary::getUniformBlockByIndex(GLuint blockIndex)
{ {
ASSERT(blockIndex < mUniformBlocks.size()); return mProgram->getUniformBlockByIndex(blockIndex);
return mUniformBlocks[blockIndex];
} }
GLint ProgramBinary::getFragDataLocation(const char *name) const GLint ProgramBinary::getFragDataLocation(const char *name) const
...@@ -351,468 +203,116 @@ GLenum ProgramBinary::getTransformFeedbackBufferMode() const ...@@ -351,468 +203,116 @@ GLenum ProgramBinary::getTransformFeedbackBufferMode() const
return mProgram->getTransformFeedbackBufferMode(); return mProgram->getTransformFeedbackBufferMode();
} }
template <typename T> void ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat *v) {
static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag) mProgram->setUniform1fv(location, count, v);
{
ASSERT(dest != NULL);
ASSERT(dirtyFlag != NULL);
*dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
*dest = source;
}
template <typename T>
void ProgramBinary::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
{
const int components = VariableComponentCount(targetUniformType);
const GLenum targetBoolType = VariableBoolVectorType(targetUniformType);
LinkedUniform *targetUniform = getUniformByLocation(location);
int elementCount = targetUniform->elementCount();
count = std::min(elementCount - (int)mUniformIndex[location].element, count);
if (targetUniform->type == targetUniformType)
{
T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
for (int i = 0; i < count; i++)
{
T *dest = target + (i * 4);
const T *source = v + (i * components);
for (int c = 0; c < components; c++)
{
SetIfDirty(dest + c, source[c], &targetUniform->dirty);
}
for (int c = components; c < 4; c++)
{
SetIfDirty(dest + c, T(0), &targetUniform->dirty);
}
}
}
else if (targetUniform->type == targetBoolType)
{
GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
for (int i = 0; i < count; i++)
{
GLint *dest = boolParams + (i * 4);
const T *source = v + (i * components);
for (int c = 0; c < components; c++)
{
SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
}
for (int c = components; c < 4; c++)
{
SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
}
}
}
else if (IsSampler(targetUniform->type))
{
ASSERT(targetUniformType == GL_INT);
GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
bool wasDirty = targetUniform->dirty;
for (int i = 0; i < count; i++)
{
GLint *dest = target + (i * 4);
const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
SetIfDirty(dest + 1, 0, &targetUniform->dirty);
SetIfDirty(dest + 2, 0, &targetUniform->dirty);
SetIfDirty(dest + 3, 0, &targetUniform->dirty);
}
if (!wasDirty && targetUniform->dirty)
{
mDirtySamplerMapping = true;
}
}
else UNREACHABLE();
}
void ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
{
setUniform(location, count, v, GL_FLOAT);
}
void ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
{
setUniform(location, count, v, GL_FLOAT_VEC2);
}
void ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
{
setUniform(location, count, v, GL_FLOAT_VEC3);
}
void ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
{
setUniform(location, count, v, GL_FLOAT_VEC4);
}
template<typename T>
bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
{
bool dirty = false;
int copyWidth = std::min(targetHeight, srcWidth);
int copyHeight = std::min(targetWidth, srcHeight);
for (int x = 0; x < copyWidth; x++)
{
for (int y = 0; y < copyHeight; y++)
{
SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
}
}
// clear unfilled right side
for (int y = 0; y < copyWidth; y++)
{
for (int x = copyHeight; x < targetWidth; x++)
{
SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
}
}
// clear unfilled bottom.
for (int y = copyWidth; y < targetHeight; y++)
{
for (int x = 0; x < targetWidth; x++)
{
SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
}
}
return dirty;
}
template<typename T>
bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
{
bool dirty = false;
int copyWidth = std::min(targetWidth, srcWidth);
int copyHeight = std::min(targetHeight, srcHeight);
for (int y = 0; y < copyHeight; y++)
{
for (int x = 0; x < copyWidth; x++)
{
SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
}
}
// clear unfilled right side
for (int y = 0; y < copyHeight; y++)
{
for (int x = copyWidth; x < targetWidth; x++)
{
SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
}
}
// clear unfilled bottom.
for (int y = copyHeight; y < targetHeight; y++)
{
for (int x = 0; x < targetWidth; x++)
{
SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
}
}
return dirty;
} }
template <int cols, int rows> void ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) {
void ProgramBinary::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType) mProgram->setUniform2fv(location, count, v);
{
LinkedUniform *targetUniform = getUniformByLocation(location);
int elementCount = targetUniform->elementCount();
count = std::min(elementCount - (int)mUniformIndex[location].element, count);
const unsigned int targetMatrixStride = (4 * rows);
GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
for (int i = 0; i < count; i++)
{
// Internally store matrices as transposed versions to accomodate HLSL matrix indexing
if (transpose == GL_FALSE)
{
targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
}
else
{
targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
}
target += targetMatrixStride;
value += cols * rows;
}
} }
void ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) void ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) {
{ mProgram->setUniform3fv(location, count, v);
setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
} }
void ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) void ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) {
{ mProgram->setUniform4fv(location, count, v);
setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
} }
void ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) void ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v) {
{ mProgram->setUniform1iv(location, count, v);
setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
} }
void ProgramBinary::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) void ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v) {
{ mProgram->setUniform2iv(location, count, v);
setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
} }
void ProgramBinary::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) void ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v) {
{ mProgram->setUniform3iv(location, count, v);
setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
} }
void ProgramBinary::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) void ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v) {
{ mProgram->setUniform4iv(location, count, v);
setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
} }
void ProgramBinary::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) void ProgramBinary::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) {
{ mProgram->setUniform1uiv(location, count, v);
setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
} }
void ProgramBinary::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) void ProgramBinary::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) {
{ mProgram->setUniform2uiv(location, count, v);
setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
} }
void ProgramBinary::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) void ProgramBinary::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) {
{ mProgram->setUniform3uiv(location, count, v);
setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
} }
void ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v) void ProgramBinary::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) {
{ mProgram->setUniform4uiv(location, count, v);
setUniform(location, count, v, GL_INT);
} }
void ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v) void ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
{ mProgram->setUniformMatrix2fv(location, count, transpose, v);
setUniform(location, count, v, GL_INT_VEC2);
} }
void ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v) void ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
{ mProgram->setUniformMatrix3fv(location, count, transpose, v);
setUniform(location, count, v, GL_INT_VEC3);
} }
void ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v) void ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
{ mProgram->setUniformMatrix4fv(location, count, transpose, v);
setUniform(location, count, v, GL_INT_VEC4);
} }
void ProgramBinary::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) void ProgramBinary::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
{ mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
setUniform(location, count, v, GL_UNSIGNED_INT);
} }
void ProgramBinary::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) void ProgramBinary::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
{ mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
} }
void ProgramBinary::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) void ProgramBinary::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
{ mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
} }
void ProgramBinary::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) void ProgramBinary::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
{ mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
} }
template <typename T> void ProgramBinary::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
void ProgramBinary::getUniformv(GLint location, T *params, GLenum uniformType) mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
{
LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
if (IsMatrixType(targetUniform->type))
{
const int rows = VariableRowCount(targetUniform->type);
const int cols = VariableColumnCount(targetUniform->type);
transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
}
else if (uniformType == VariableComponentType(targetUniform->type))
{
unsigned int size = VariableComponentCount(targetUniform->type);
memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
size * sizeof(T));
}
else
{
unsigned int size = VariableComponentCount(targetUniform->type);
switch (VariableComponentType(targetUniform->type))
{
case GL_BOOL:
{
GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
for (unsigned int i = 0; i < size; i++)
{
params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
}
}
break;
case GL_FLOAT:
{
GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
for (unsigned int i = 0; i < size; i++)
{
params[i] = static_cast<T>(floatParams[i]);
}
}
break;
case GL_INT:
{
GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
for (unsigned int i = 0; i < size; i++)
{
params[i] = static_cast<T>(intParams[i]);
}
}
break;
case GL_UNSIGNED_INT:
{
GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
for (unsigned int i = 0; i < size; i++)
{
params[i] = static_cast<T>(uintParams[i]);
}
}
break;
default: UNREACHABLE();
}
}
} }
void ProgramBinary::getUniformfv(GLint location, GLfloat *params) void ProgramBinary::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
{ mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
getUniformv(location, params, GL_FLOAT);
} }
void ProgramBinary::getUniformiv(GLint location, GLint *params) void ProgramBinary::getUniformfv(GLint location, GLfloat *v) {
{ mProgram->getUniformfv(location, v);
getUniformv(location, params, GL_INT);
} }
void ProgramBinary::getUniformuiv(GLint location, GLuint *params) void ProgramBinary::getUniformiv(GLint location, GLint *v) {
{ mProgram->getUniformiv(location, v);
getUniformv(location, params, GL_UNSIGNED_INT);
} }
void ProgramBinary::dirtyAllUniforms() void ProgramBinary::getUniformuiv(GLint location, GLuint *v) {
{ mProgram->getUniformuiv(location, v);
unsigned int numUniforms = mUniforms.size();
for (unsigned int index = 0; index < numUniforms; index++)
{
mUniforms[index]->dirty = true;
}
} }
void ProgramBinary::updateSamplerMapping() void ProgramBinary::updateSamplerMapping()
{ {
if (!mDirtySamplerMapping) return mProgram->updateSamplerMapping();
{
return;
}
mDirtySamplerMapping = false;
// Retrieve sampler uniform values
for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
{
LinkedUniform *targetUniform = mUniforms[uniformIndex];
if (targetUniform->dirty)
{
if (IsSampler(targetUniform->type))
{
int count = targetUniform->elementCount();
GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
if (targetUniform->isReferencedByFragmentShader())
{
unsigned int firstIndex = targetUniform->psRegisterIndex;
for (int i = 0; i < count; i++)
{
unsigned int samplerIndex = firstIndex + i;
if (samplerIndex < mSamplersPS.size())
{
ASSERT(mSamplersPS[samplerIndex].active);
mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
}
}
}
if (targetUniform->isReferencedByVertexShader())
{
unsigned int firstIndex = targetUniform->vsRegisterIndex;
for (int i = 0; i < count; i++)
{
unsigned int samplerIndex = firstIndex + i;
if (samplerIndex < mSamplersVS.size())
{
ASSERT(mSamplersVS[samplerIndex].active);
mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
}
}
}
}
}
}
} }
// Applies all the uniforms set for this program object to the renderer // Applies all the uniforms set for this program object to the renderer
Error ProgramBinary::applyUniforms() Error ProgramBinary::applyUniforms()
{ {
updateSamplerMapping(); return mProgram->applyUniforms();
Error error = mProgram->applyUniforms(mUniforms);
if (error.isError())
{
return error;
}
for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
{
mUniforms[uniformIndex]->dirty = false;
}
return gl::Error(GL_NO_ERROR);
} }
Error ProgramBinary::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const Caps &caps) Error ProgramBinary::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const Caps &caps)
{ {
ASSERT(boundBuffers.size() == mUniformBlocks.size()); return mProgram->applyUniformBuffers(boundBuffers, caps);
return mProgram->applyUniformBuffers(mUniformBlocks, boundBuffers, caps);
} }
bool ProgramBinary::linkVaryings(InfoLog &infoLog, Shader *fragmentShader, Shader *vertexShader) bool ProgramBinary::linkVaryings(InfoLog &infoLog, Shader *fragmentShader, Shader *vertexShader)
...@@ -912,113 +412,12 @@ LinkResult ProgramBinary::load(InfoLog &infoLog, GLenum binaryFormat, const void ...@@ -912,113 +412,12 @@ LinkResult ProgramBinary::load(InfoLog &infoLog, GLenum binaryFormat, const void
initAttributesByLayout(); initAttributesByLayout();
const unsigned int psSamplerCount = stream.readInt<unsigned int>();
for (unsigned int i = 0; i < psSamplerCount; ++i)
{
Sampler sampler;
stream.readBool(&sampler.active);
stream.readInt(&sampler.logicalTextureUnit);
stream.readInt(&sampler.textureType);
mSamplersPS.push_back(sampler);
}
const unsigned int vsSamplerCount = stream.readInt<unsigned int>();
for (unsigned int i = 0; i < vsSamplerCount; ++i)
{
Sampler sampler;
stream.readBool(&sampler.active);
stream.readInt(&sampler.logicalTextureUnit);
stream.readInt(&sampler.textureType);
mSamplersVS.push_back(sampler);
}
stream.readInt(&mUsedVertexSamplerRange);
stream.readInt(&mUsedPixelSamplerRange);
const unsigned int uniformCount = stream.readInt<unsigned int>();
if (stream.error())
{
infoLog.append("Invalid program binary.");
return LinkResult(false, Error(GL_NO_ERROR));
}
mUniforms.resize(uniformCount);
for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
{
GLenum type = stream.readInt<GLenum>();
GLenum precision = stream.readInt<GLenum>();
std::string name = stream.readString();
unsigned int arraySize = stream.readInt<unsigned int>();
int blockIndex = stream.readInt<int>();
int offset = stream.readInt<int>();
int arrayStride = stream.readInt<int>();
int matrixStride = stream.readInt<int>();
bool isRowMajorMatrix = stream.readBool();
const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
LinkedUniform *uniform = new LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
stream.readInt(&uniform->psRegisterIndex);
stream.readInt(&uniform->vsRegisterIndex);
stream.readInt(&uniform->registerCount);
stream.readInt(&uniform->registerElement);
mUniforms[uniformIndex] = uniform;
}
unsigned int uniformBlockCount = stream.readInt<unsigned int>();
if (stream.error())
{
infoLog.append("Invalid program binary.");
return LinkResult(false, Error(GL_NO_ERROR));
}
mUniformBlocks.resize(uniformBlockCount);
for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
{
std::string name = stream.readString();
unsigned int elementIndex = stream.readInt<unsigned int>();
unsigned int dataSize = stream.readInt<unsigned int>();
UniformBlock *uniformBlock = new UniformBlock(name, elementIndex, dataSize);
stream.readInt(&uniformBlock->psRegisterIndex);
stream.readInt(&uniformBlock->vsRegisterIndex);
unsigned int numMembers = stream.readInt<unsigned int>();
uniformBlock->memberUniformIndexes.resize(numMembers);
for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
{
stream.readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
}
mUniformBlocks[uniformBlockIndex] = uniformBlock;
}
const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
if (stream.error())
{
infoLog.append("Invalid program binary.");
return LinkResult(false, Error(GL_NO_ERROR));
}
mUniformIndex.resize(uniformIndexCount);
for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
{
stream.readString(&mUniformIndex[uniformIndexIndex].name);
stream.readInt(&mUniformIndex[uniformIndexIndex].element);
stream.readInt(&mUniformIndex[uniformIndexIndex].index);
}
LinkResult result = mProgram->load(infoLog, &stream); LinkResult result = mProgram->load(infoLog, &stream);
if (result.error.isError() || !result.linkSuccess) if (result.error.isError() || !result.linkSuccess)
{ {
return result; return result;
} }
mProgram->initializeUniformStorage(mUniforms);
return LinkResult(true, Error(GL_NO_ERROR)); return LinkResult(true, Error(GL_NO_ERROR));
#endif // #ifdef ANGLE_DISABLE_PROGRAM_BINARY_LOAD #endif // #ifdef ANGLE_DISABLE_PROGRAM_BINARY_LOAD
} }
...@@ -1047,74 +446,6 @@ Error ProgramBinary::save(GLenum *binaryFormat, void *binary, GLsizei bufSize, G ...@@ -1047,74 +446,6 @@ Error ProgramBinary::save(GLenum *binaryFormat, void *binary, GLsizei bufSize, G
stream.writeInt(mSemanticIndex[i]); stream.writeInt(mSemanticIndex[i]);
} }
stream.writeInt(mSamplersPS.size());
for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
{
stream.writeInt(mSamplersPS[i].active);
stream.writeInt(mSamplersPS[i].logicalTextureUnit);
stream.writeInt(mSamplersPS[i].textureType);
}
stream.writeInt(mSamplersVS.size());
for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
{
stream.writeInt(mSamplersVS[i].active);
stream.writeInt(mSamplersVS[i].logicalTextureUnit);
stream.writeInt(mSamplersVS[i].textureType);
}
stream.writeInt(mUsedVertexSamplerRange);
stream.writeInt(mUsedPixelSamplerRange);
stream.writeInt(mUniforms.size());
for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
{
const LinkedUniform &uniform = *mUniforms[uniformIndex];
stream.writeInt(uniform.type);
stream.writeInt(uniform.precision);
stream.writeString(uniform.name);
stream.writeInt(uniform.arraySize);
stream.writeInt(uniform.blockIndex);
stream.writeInt(uniform.blockInfo.offset);
stream.writeInt(uniform.blockInfo.arrayStride);
stream.writeInt(uniform.blockInfo.matrixStride);
stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
stream.writeInt(uniform.psRegisterIndex);
stream.writeInt(uniform.vsRegisterIndex);
stream.writeInt(uniform.registerCount);
stream.writeInt(uniform.registerElement);
}
stream.writeInt(mUniformBlocks.size());
for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
{
const UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
stream.writeString(uniformBlock.name);
stream.writeInt(uniformBlock.elementIndex);
stream.writeInt(uniformBlock.dataSize);
stream.writeInt(uniformBlock.memberUniformIndexes.size());
for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
{
stream.writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
}
stream.writeInt(uniformBlock.psRegisterIndex);
stream.writeInt(uniformBlock.vsRegisterIndex);
}
stream.writeInt(mUniformIndex.size());
for (size_t i = 0; i < mUniformIndex.size(); ++i)
{
stream.writeString(mUniformIndex[i].name);
stream.writeInt(mUniformIndex[i].element);
stream.writeInt(mUniformIndex[i].index);
}
mProgram->save(&stream); mProgram->save(&stream);
GLsizei streamLength = stream.length(); GLsizei streamLength = stream.length();
...@@ -1180,12 +511,6 @@ LinkResult ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attrib ...@@ -1180,12 +511,6 @@ LinkResult ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attrib
reset(); reset();
mSamplersPS.resize(caps.maxTextureImageUnits);
mSamplersVS.resize(caps.maxVertexTextureImageUnits);
rx::ShaderD3D *vertexShaderD3D = rx::ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
rx::ShaderD3D *fragmentShaderD3D = rx::ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
int registers; int registers;
std::vector<LinkedVarying> linkedVaryings; std::vector<LinkedVarying> linkedVaryings;
LinkResult result = mProgram->link(infoLog, fragmentShader, vertexShader, transformFeedbackVaryings, transformFeedbackBufferMode, LinkResult result = mProgram->link(infoLog, fragmentShader, vertexShader, transformFeedbackVaryings, transformFeedbackBufferMode,
...@@ -1200,21 +525,11 @@ LinkResult ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attrib ...@@ -1200,21 +525,11 @@ LinkResult ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attrib
return LinkResult(false, Error(GL_NO_ERROR)); return LinkResult(false, Error(GL_NO_ERROR));
} }
if (!linkUniforms(infoLog, *vertexShader, *fragmentShader, caps)) if (!mProgram->linkUniforms(infoLog, *vertexShader, *fragmentShader, caps))
{ {
return LinkResult(false, Error(GL_NO_ERROR)); return LinkResult(false, Error(GL_NO_ERROR));
} }
// special case for gl_DepthRange, the only built-in uniform (also a struct)
if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
{
const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
mUniforms.push_back(new LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
mUniforms.push_back(new LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
mUniforms.push_back(new LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
}
if (!linkUniformBlocks(infoLog, *vertexShader, *fragmentShader, caps)) if (!linkUniformBlocks(infoLog, *vertexShader, *fragmentShader, caps))
{ {
return LinkResult(false, Error(GL_NO_ERROR)); return LinkResult(false, Error(GL_NO_ERROR));
...@@ -1239,141 +554,106 @@ LinkResult ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attrib ...@@ -1239,141 +554,106 @@ LinkResult ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attrib
return LinkResult(true, Error(GL_NO_ERROR)); return LinkResult(true, Error(GL_NO_ERROR));
} }
// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices bool ProgramBinary::linkUniformBlocks(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, const Shader *vertexShader) const gl::Caps &caps)
{ {
const rx::ShaderD3D *vertexShaderD3D = rx::ShaderD3D::makeShaderD3D(vertexShader->getImplementation()); const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
unsigned int usedLocations = 0; // Check that interface blocks defined in the vertex and fragment shaders are identical
const std::vector<sh::Attribute> &shaderAttributes = vertexShader->getActiveAttributes(); typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
UniformBlockMap linkedUniformBlocks;
// Link attributes that have a binding location for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++)
for (unsigned int attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
{ {
const sh::Attribute &attribute = shaderAttributes[attributeIndex]; const sh::InterfaceBlock &vertexInterfaceBlock = vertexInterfaceBlocks[blockIndex];
linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
ASSERT(attribute.staticUse); }
const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location;
mProgram->getShaderAttributes()[attributeIndex] = attribute;
if (location != -1) // Set by glBindAttribLocation or by location layout qualifier for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++)
{
const sh::InterfaceBlock &fragmentInterfaceBlock = fragmentInterfaceBlocks[blockIndex];
UniformBlockMap::const_iterator entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
if (entry != linkedUniformBlocks.end())
{ {
const int rows = VariableRegisterCount(attribute.type); const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
if (rows + location > MAX_VERTEX_ATTRIBS)
{ {
infoLog.append("Active attribute (%s) at location %d is too big to fit", attribute.name.c_str(), location);
return false; return false;
} }
for (int row = 0; row < rows; row++)
{
const int rowLocation = location + row;
sh::ShaderVariable &linkedAttribute = mLinkedAttribute[rowLocation];
// In GLSL 3.00, attribute aliasing produces a link error
// In GLSL 1.00, attribute aliasing is allowed
if (mProgram->getShaderVersion() >= 300)
{
if (!linkedAttribute.name.empty())
{
infoLog.append("Attribute '%s' aliases attribute '%s' at location %d", attribute.name.c_str(), linkedAttribute.name.c_str(), rowLocation);
return false;
}
}
linkedAttribute = attribute;
usedLocations |= 1 << rowLocation;
}
} }
} }
// Link attributes that don't have a binding location for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++)
for (unsigned int attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
{ {
const sh::Attribute &attribute = shaderAttributes[attributeIndex]; const sh::InterfaceBlock &interfaceBlock = vertexInterfaceBlocks[blockIndex];
ASSERT(attribute.staticUse);
const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location;
if (location == -1) // Not set by glBindAttribLocation or by location layout qualifier // Note: shared and std140 layouts are always considered active
if (interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
{ {
int rows = VariableRegisterCount(attribute.type); if (!mProgram->defineUniformBlock(infoLog, vertexShader, interfaceBlock, caps))
int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
{ {
infoLog.append("Too many active attributes (%s)", attribute.name.c_str()); return false;
return false; // Fail to link
} }
mLinkedAttribute[availableIndex] = attribute;
} }
} }
for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; ) for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++)
{ {
int index = vertexShaderD3D->getSemanticIndex(mLinkedAttribute[attributeIndex].name); const sh::InterfaceBlock &interfaceBlock = fragmentInterfaceBlocks[blockIndex];
int rows = VariableRegisterCount(mLinkedAttribute[attributeIndex].type);
for (int r = 0; r < rows; r++) // Note: shared and std140 layouts are always considered active
if (interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
{ {
mSemanticIndex[attributeIndex++] = index++; if (!mProgram->defineUniformBlock(infoLog, fragmentShader, interfaceBlock, caps))
{
return false;
}
} }
} }
initAttributesByLayout();
return true; return true;
} }
bool ProgramBinary::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable, bool ProgramBinary::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
const sh::ShaderVariable &fragmentVariable, bool validatePrecision) const sh::InterfaceBlock &fragmentInterfaceBlock)
{ {
if (vertexVariable.type != fragmentVariable.type) const char* blockName = vertexInterfaceBlock.name.c_str();
{
infoLog.append("Types for %s differ between vertex and fragment shaders", variableName.c_str()); // validate blocks for the same member types
return false; if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
}
if (vertexVariable.arraySize != fragmentVariable.arraySize)
{ {
infoLog.append("Array sizes for %s differ between vertex and fragment shaders", variableName.c_str()); infoLog.append("Types for interface block '%s' differ between vertex and fragment shaders", blockName);
return false; return false;
} }
if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
{ {
infoLog.append("Precisions for %s differ between vertex and fragment shaders", variableName.c_str()); infoLog.append("Array sizes differ for interface block '%s' between vertex and fragment shaders", blockName);
return false; return false;
} }
if (vertexVariable.fields.size() != fragmentVariable.fields.size()) if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
{ {
infoLog.append("Structure lengths for %s differ between vertex and fragment shaders", variableName.c_str()); infoLog.append("Layout qualifiers differ for interface block '%s' between vertex and fragment shaders", blockName);
return false; return false;
} }
const unsigned int numMembers = vertexVariable.fields.size();
for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++) const unsigned int numBlockMembers = vertexInterfaceBlock.fields.size();
for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
{ {
const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex]; const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex]; const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
if (vertexMember.name != fragmentMember.name) if (vertexMember.name != fragmentMember.name)
{ {
infoLog.append("Name mismatch for field '%d' of %s: (in vertex: '%s', in fragment: '%s')", infoLog.append("Name mismatch for field %d of interface block '%s': (in vertex: '%s', in fragment: '%s')",
memberIndex, variableName.c_str(), blockMemberIndex, blockName, vertexMember.name.c_str(), fragmentMember.name.c_str());
vertexMember.name.c_str(), fragmentMember.name.c_str());
return false; return false;
} }
const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." + std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
vertexMember.name + "'"; if (!gl::ProgramBinary::linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
{ {
return false; return false;
} }
...@@ -1382,320 +662,141 @@ bool ProgramBinary::linkValidateVariablesBase(InfoLog &infoLog, const std::strin ...@@ -1382,320 +662,141 @@ bool ProgramBinary::linkValidateVariablesBase(InfoLog &infoLog, const std::strin
return true; return true;
} }
bool ProgramBinary::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform) // Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
{ bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, const Shader *vertexShader)
if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
{
return false;
}
return true;
}
bool ProgramBinary::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
{
if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
{
return false;
}
if (vertexVarying.interpolation != fragmentVarying.interpolation)
{
infoLog.append("Interpolation types for %s differ between vertex and fragment shaders", varyingName.c_str());
return false;
}
return true;
}
bool ProgramBinary::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
{
if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
{
return false;
}
if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
{
infoLog.append("Matrix packings for %s differ between vertex and fragment shaders", uniformName.c_str());
return false;
}
return true;
}
bool ProgramBinary::linkUniforms(InfoLog &infoLog, const Shader &vertexShader, const Shader &fragmentShader, const Caps &caps)
{ {
const rx::ShaderD3D *vertexShaderD3D = rx::ShaderD3D::makeShaderD3D(vertexShader.getImplementation()); const rx::ShaderD3D *vertexShaderD3D = rx::ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
const rx::ShaderD3D *fragmentShaderD3D = rx::ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
// Check that uniforms defined in the vertex and fragment shaders are identical
typedef std::map<std::string, const sh::Uniform*> UniformMap;
UniformMap linkedUniforms;
for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
{
const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
linkedUniforms[vertexUniform.name] = &vertexUniform;
}
for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
{
const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
if (entry != linkedUniforms.end())
{
const sh::Uniform &vertexUniform = *entry->second;
const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
if (!linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
{
return false;
}
}
}
for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
{
const sh::Uniform &uniform = vertexUniforms[uniformIndex];
if (uniform.staticUse)
{
defineUniformBase(GL_VERTEX_SHADER, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
}
}
for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
{
const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
if (uniform.staticUse) unsigned int usedLocations = 0;
{ const std::vector<sh::Attribute> &shaderAttributes = vertexShader->getActiveAttributes();
defineUniformBase(GL_FRAGMENT_SHADER, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
}
}
if (!indexUniforms(infoLog, caps)) // Link attributes that have a binding location
for (unsigned int attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
{ {
return false; const sh::Attribute &attribute = shaderAttributes[attributeIndex];
}
mProgram->initializeUniformStorage(mUniforms);
return true; ASSERT(attribute.staticUse);
}
void ProgramBinary::defineUniformBase(GLenum shader, const sh::Uniform &uniform, unsigned int uniformRegister) const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location;
{
ShShaderOutput outputType = rx::ShaderD3D::getCompilerOutputType(shader);
sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
encoder.skipRegisters(uniformRegister);
defineUniform(shader, uniform, uniform.name, &encoder); mProgram->getShaderAttributes()[attributeIndex] = attribute;
}
void ProgramBinary::defineUniform(GLenum shader, const sh::ShaderVariable &uniform, if (location != -1) // Set by glBindAttribLocation or by location layout qualifier
const std::string &fullName, sh::HLSLBlockEncoder *encoder)
{
if (uniform.isStruct())
{
for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
{ {
const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : ""); const int rows = VariableRegisterCount(attribute.type);
encoder->enterAggregateType();
for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++) if (rows + location > MAX_VERTEX_ATTRIBS)
{ {
const sh::ShaderVariable &field = uniform.fields[fieldIndex]; infoLog.append("Active attribute (%s) at location %d is too big to fit", attribute.name.c_str(), location);
const std::string &fieldFullName = (fullName + elementString + "." + field.name);
defineUniform(shader, field, fieldFullName, encoder); return false;
} }
encoder->exitAggregateType(); for (int row = 0; row < rows; row++)
} {
} const int rowLocation = location + row;
else // Not a struct sh::ShaderVariable &linkedAttribute = mLinkedAttribute[rowLocation];
{
// Arrays are treated as aggregate types
if (uniform.isArray())
{
encoder->enterAggregateType();
}
LinkedUniform *linkedUniform = getUniformByName(fullName);
if (!linkedUniform)
{
linkedUniform = new LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
-1, sh::BlockMemberInfo::getDefaultBlockInfo());
ASSERT(linkedUniform);
linkedUniform->registerElement = encoder->getCurrentElement();
mUniforms.push_back(linkedUniform);
}
ASSERT(linkedUniform->registerElement == encoder->getCurrentElement());
if (shader == GL_FRAGMENT_SHADER)
{
linkedUniform->psRegisterIndex = encoder->getCurrentRegister();
}
else if (shader == GL_VERTEX_SHADER)
{
linkedUniform->vsRegisterIndex = encoder->getCurrentRegister();
}
else UNREACHABLE();
// Advance the uniform offset, to track registers allocation for structs
encoder->encodeType(uniform.type, uniform.arraySize, false);
// Arrays are treated as aggregate types
if (uniform.isArray())
{
encoder->exitAggregateType();
}
}
}
bool ProgramBinary::indexSamplerUniform(const LinkedUniform &uniform, InfoLog &infoLog, const Caps &caps)
{
ASSERT(IsSampler(uniform.type));
ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
{
if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
&mUsedVertexSamplerRange))
{
infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
mSamplersVS.size());
return false;
}
unsigned int maxVertexVectors = mProgram->getReservedUniformVectors(GL_VERTEX_SHADER) + caps.maxVertexUniformVectors;
if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
{
infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
caps.maxVertexUniformVectors);
return false;
}
}
if (uniform.psRegisterIndex != GL_INVALID_INDEX) // In GLSL 3.00, attribute aliasing produces a link error
{ // In GLSL 1.00, attribute aliasing is allowed
if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS, if (mProgram->getShaderVersion() >= 300)
&mUsedPixelSamplerRange)) {
{ if (!linkedAttribute.name.empty())
infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", {
mSamplersPS.size()); infoLog.append("Attribute '%s' aliases attribute '%s' at location %d", attribute.name.c_str(), linkedAttribute.name.c_str(), rowLocation);
return false; return false;
} }
}
unsigned int maxFragmentVectors = mProgram->getReservedUniformVectors(GL_FRAGMENT_SHADER) + caps.maxFragmentUniformVectors; linkedAttribute = attribute;
if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors) usedLocations |= 1 << rowLocation;
{ }
infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
caps.maxFragmentUniformVectors);
return false;
} }
} }
return true; // Link attributes that don't have a binding location
} for (unsigned int attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
bool ProgramBinary::indexUniforms(InfoLog &infoLog, const Caps &caps)
{
for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
{ {
const LinkedUniform &uniform = *mUniforms[uniformIndex]; const sh::Attribute &attribute = shaderAttributes[attributeIndex];
if (IsSampler(uniform.type)) ASSERT(attribute.staticUse);
const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location;
if (location == -1) // Not set by glBindAttribLocation or by location layout qualifier
{ {
if (!indexSamplerUniform(uniform, infoLog, caps)) int rows = VariableRegisterCount(attribute.type);
int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
{ {
return false; infoLog.append("Too many active attributes (%s)", attribute.name.c_str());
return false; // Fail to link
} }
}
for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++) mLinkedAttribute[availableIndex] = attribute;
{
mUniformIndex.push_back(VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
} }
} }
return true; for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
}
bool ProgramBinary::assignSamplers(unsigned int startSamplerIndex,
GLenum samplerType,
unsigned int samplerCount,
std::vector<Sampler> &outSamplers,
GLuint *outUsedRange)
{
unsigned int samplerIndex = startSamplerIndex;
do
{ {
if (samplerIndex < outSamplers.size()) int index = vertexShaderD3D->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
{ int rows = VariableRegisterCount(mLinkedAttribute[attributeIndex].type);
Sampler& sampler = outSamplers[samplerIndex];
sampler.active = true; for (int r = 0; r < rows; r++)
sampler.textureType = GetTextureType(samplerType);
sampler.logicalTextureUnit = 0;
*outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
}
else
{ {
return false; mSemanticIndex[attributeIndex++] = index++;
} }
}
samplerIndex++; initAttributesByLayout();
} while (samplerIndex < startSamplerIndex + samplerCount);
return true; return true;
} }
bool ProgramBinary::areMatchingInterfaceBlocks(InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock, const sh::InterfaceBlock &fragmentInterfaceBlock) bool ProgramBinary::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
{ {
const char* blockName = vertexInterfaceBlock.name.c_str(); if (vertexVariable.type != fragmentVariable.type)
// validate blocks for the same member types
if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
{ {
infoLog.append("Types for interface block '%s' differ between vertex and fragment shaders", blockName); infoLog.append("Types for %s differ between vertex and fragment shaders", variableName.c_str());
return false; return false;
} }
if (vertexVariable.arraySize != fragmentVariable.arraySize)
if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
{ {
infoLog.append("Array sizes differ for interface block '%s' between vertex and fragment shaders", blockName); infoLog.append("Array sizes for %s differ between vertex and fragment shaders", variableName.c_str());
return false; return false;
} }
if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
{ {
infoLog.append("Layout qualifiers differ for interface block '%s' between vertex and fragment shaders", blockName); infoLog.append("Precisions for %s differ between vertex and fragment shaders", variableName.c_str());
return false; return false;
} }
const unsigned int numBlockMembers = vertexInterfaceBlock.fields.size(); if (vertexVariable.fields.size() != fragmentVariable.fields.size())
for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
{ {
const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex]; infoLog.append("Structure lengths for %s differ between vertex and fragment shaders", variableName.c_str());
const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex]; return false;
}
const unsigned int numMembers = vertexVariable.fields.size();
for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
{
const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
if (vertexMember.name != fragmentMember.name) if (vertexMember.name != fragmentMember.name)
{ {
infoLog.append("Name mismatch for field %d of interface block '%s': (in vertex: '%s', in fragment: '%s')", infoLog.append("Name mismatch for field '%d' of %s: (in vertex: '%s', in fragment: '%s')",
blockMemberIndex, blockName, vertexMember.name.c_str(), fragmentMember.name.c_str()); memberIndex, variableName.c_str(),
vertexMember.name.c_str(), fragmentMember.name.c_str());
return false; return false;
} }
std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'"; const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember)) vertexMember.name + "'";
if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
{ {
return false; return false;
} }
...@@ -1704,61 +805,43 @@ bool ProgramBinary::areMatchingInterfaceBlocks(InfoLog &infoLog, const sh::Inter ...@@ -1704,61 +805,43 @@ bool ProgramBinary::areMatchingInterfaceBlocks(InfoLog &infoLog, const sh::Inter
return true; return true;
} }
bool ProgramBinary::linkUniformBlocks(InfoLog &infoLog, const Shader &vertexShader, const Shader &fragmentShader, const Caps &caps) bool ProgramBinary::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
{ {
const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(); if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(); {
return false;
}
// Check that interface blocks defined in the vertex and fragment shaders are identical return true;
typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap; }
UniformBlockMap linkedUniformBlocks;
for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++) bool ProgramBinary::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
{
if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
{ {
const sh::InterfaceBlock &vertexInterfaceBlock = vertexInterfaceBlocks[blockIndex]; return false;
linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
} }
for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++) if (vertexVarying.interpolation != fragmentVarying.interpolation)
{ {
const sh::InterfaceBlock &fragmentInterfaceBlock = fragmentInterfaceBlocks[blockIndex]; infoLog.append("Interpolation types for %s differ between vertex and fragment shaders", varyingName.c_str());
UniformBlockMap::const_iterator entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name); return false;
if (entry != linkedUniformBlocks.end())
{
const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
{
return false;
}
}
} }
for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++) return true;
{ }
const sh::InterfaceBlock &interfaceBlock = vertexInterfaceBlocks[blockIndex];
// Note: shared and std140 layouts are always considered active bool ProgramBinary::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
if (interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED) {
{ if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
if (!defineUniformBlock(infoLog, vertexShader, interfaceBlock, caps)) {
{ return false;
return false;
}
}
} }
for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++) if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
{ {
const sh::InterfaceBlock &interfaceBlock = fragmentInterfaceBlocks[blockIndex]; infoLog.append("Matrix packings for %s differ between vertex and fragment shaders", uniformName.c_str());
return false;
// Note: shared and std140 layouts are always considered active
if (interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
{
if (!defineUniformBlock(infoLog, fragmentShader, interfaceBlock, caps))
{
return false;
}
}
} }
return true; return true;
...@@ -1821,117 +904,6 @@ bool ProgramBinary::gatherTransformFeedbackLinkedVaryings(InfoLog &infoLog, cons ...@@ -1821,117 +904,6 @@ bool ProgramBinary::gatherTransformFeedbackLinkedVaryings(InfoLog &infoLog, cons
return true; return true;
} }
template <typename VarT>
void ProgramBinary::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
bool inRowMajorLayout)
{
for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
{
const VarT &field = fields[uniformIndex];
const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
if (field.isStruct())
{
bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
{
encoder->enterAggregateType();
const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
encoder->exitAggregateType();
}
}
else
{
bool isRowMajorMatrix = (IsMatrixType(field.type) && inRowMajorLayout);
sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
LinkedUniform *newUniform = new LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
blockIndex, memberInfo);
// add to uniform list, but not index, since uniform block uniforms have no location
blockUniformIndexes->push_back(mUniforms.size());
mUniforms.push_back(newUniform);
}
}
}
bool ProgramBinary::defineUniformBlock(InfoLog &infoLog, const Shader &shader, const sh::InterfaceBlock &interfaceBlock, const Caps &caps)
{
const rx::ShaderD3D* shaderD3D = rx::ShaderD3D::makeShaderD3D(shader.getImplementation());
// create uniform block entries if they do not exist
if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
{
std::vector<unsigned int> blockUniformIndexes;
const unsigned int blockIndex = mUniformBlocks.size();
// define member uniforms
sh::BlockLayoutEncoder *encoder = NULL;
if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
{
encoder = new sh::Std140BlockEncoder;
}
else
{
encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
}
ASSERT(encoder);
defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
size_t dataSize = encoder->getBlockSize();
// create all the uniform blocks
if (interfaceBlock.arraySize > 0)
{
for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
{
UniformBlock *newUniformBlock = new UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
newUniformBlock->memberUniformIndexes = blockUniformIndexes;
mUniformBlocks.push_back(newUniformBlock);
}
}
else
{
UniformBlock *newUniformBlock = new UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
newUniformBlock->memberUniformIndexes = blockUniformIndexes;
mUniformBlocks.push_back(newUniformBlock);
}
}
if (interfaceBlock.staticUse)
{
// Assign registers to the uniform blocks
const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
ASSERT(blockIndex != GL_INVALID_INDEX);
ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
{
UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
ASSERT(uniformBlock->name == interfaceBlock.name);
if (!mProgram->assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
interfaceBlockRegister + uniformBlockElement, caps))
{
return false;
}
}
}
return true;
}
bool ProgramBinary::isValidated() const bool ProgramBinary::isValidated() const
{ {
return mValidated; return mValidated;
...@@ -2007,13 +979,13 @@ GLint ProgramBinary::getActiveAttributeMaxLength() const ...@@ -2007,13 +979,13 @@ GLint ProgramBinary::getActiveAttributeMaxLength() const
void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
{ {
ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount() ASSERT(index < mProgram->getUniforms().size()); // index must be smaller than getActiveUniformCount()
if (bufsize > 0) if (bufsize > 0)
{ {
std::string string = mUniforms[index]->name; std::string string = mProgram->getUniforms()[index]->name;
if (mUniforms[index]->isArray()) if (mProgram->getUniforms()[index]->isArray())
{ {
string += "[0]"; string += "[0]";
} }
...@@ -2027,27 +999,27 @@ void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *len ...@@ -2027,27 +999,27 @@ void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *len
} }
} }
*size = mUniforms[index]->elementCount(); *size = mProgram->getUniforms()[index]->elementCount();
*type = mUniforms[index]->type; *type = mProgram->getUniforms()[index]->type;
} }
GLint ProgramBinary::getActiveUniformCount() const GLint ProgramBinary::getActiveUniformCount() const
{ {
return mUniforms.size(); return mProgram->getUniforms().size();
} }
GLint ProgramBinary::getActiveUniformMaxLength() const GLint ProgramBinary::getActiveUniformMaxLength() const
{ {
int maxLength = 0; int maxLength = 0;
unsigned int numUniforms = mUniforms.size(); unsigned int numUniforms = mProgram->getUniforms().size();
for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++) for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
{ {
if (!mUniforms[uniformIndex]->name.empty()) if (!mProgram->getUniforms()[uniformIndex]->name.empty())
{ {
int length = (int)(mUniforms[uniformIndex]->name.length() + 1); int length = (int)(mProgram->getUniforms()[uniformIndex]->name.length() + 1);
if (mUniforms[uniformIndex]->isArray()) if (mProgram->getUniforms()[uniformIndex]->isArray())
{ {
length += 3; // Counting in "[0]". length += 3; // Counting in "[0]".
} }
...@@ -2060,7 +1032,7 @@ GLint ProgramBinary::getActiveUniformMaxLength() const ...@@ -2060,7 +1032,7 @@ GLint ProgramBinary::getActiveUniformMaxLength() const
GLint ProgramBinary::getActiveUniformi(GLuint index, GLenum pname) const GLint ProgramBinary::getActiveUniformi(GLuint index, GLenum pname) const
{ {
const gl::LinkedUniform& uniform = *mUniforms[index]; const gl::LinkedUniform& uniform = *mProgram->getUniforms()[index];
switch (pname) switch (pname)
{ {
...@@ -2083,34 +1055,25 @@ GLint ProgramBinary::getActiveUniformi(GLuint index, GLenum pname) const ...@@ -2083,34 +1055,25 @@ GLint ProgramBinary::getActiveUniformi(GLuint index, GLenum pname) const
bool ProgramBinary::isValidUniformLocation(GLint location) const bool ProgramBinary::isValidUniformLocation(GLint location) const
{ {
ASSERT(rx::IsIntegerCastSafe<GLint>(mUniformIndex.size())); ASSERT(rx::IsIntegerCastSafe<GLint>(mProgram->getUniformIndices().size()));
return (location >= 0 && location < static_cast<GLint>(mUniformIndex.size())); return (location >= 0 && location < static_cast<GLint>(mProgram->getUniformIndices().size()));
} }
LinkedUniform *ProgramBinary::getUniformByLocation(GLint location) const LinkedUniform *ProgramBinary::getUniformByLocation(GLint location) const
{ {
ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformIndex.size()); return mProgram->getUniformByLocation(location);
return mUniforms[mUniformIndex[location].index];
} }
LinkedUniform *ProgramBinary::getUniformByName(const std::string &name) const LinkedUniform *ProgramBinary::getUniformByName(const std::string &name) const
{ {
for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++) return mProgram->getUniformByName(name);
{
if (mUniforms[uniformIndex]->name == name)
{
return mUniforms[uniformIndex];
}
}
return NULL;
} }
void ProgramBinary::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const void ProgramBinary::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
{ {
ASSERT(uniformBlockIndex < mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount() ASSERT(uniformBlockIndex < mProgram->getUniformBlocks().size()); // index must be smaller than getActiveUniformBlockCount()
const UniformBlock &uniformBlock = *mUniformBlocks[uniformBlockIndex]; const UniformBlock &uniformBlock = *mProgram->getUniformBlocks()[uniformBlockIndex];
if (bufSize > 0) if (bufSize > 0)
{ {
...@@ -2133,9 +1096,9 @@ void ProgramBinary::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei ...@@ -2133,9 +1096,9 @@ void ProgramBinary::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei
void ProgramBinary::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const void ProgramBinary::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
{ {
ASSERT(uniformBlockIndex < mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount() ASSERT(uniformBlockIndex < mProgram->getUniformBlocks().size()); // index must be smaller than getActiveUniformBlockCount()
const UniformBlock &uniformBlock = *mUniformBlocks[uniformBlockIndex]; const UniformBlock &uniformBlock = *mProgram->getUniformBlocks()[uniformBlockIndex];
switch (pname) switch (pname)
{ {
...@@ -2168,17 +1131,17 @@ void ProgramBinary::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pna ...@@ -2168,17 +1131,17 @@ void ProgramBinary::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pna
GLuint ProgramBinary::getActiveUniformBlockCount() const GLuint ProgramBinary::getActiveUniformBlockCount() const
{ {
return mUniformBlocks.size(); return mProgram->getUniformBlocks().size();
} }
GLuint ProgramBinary::getActiveUniformBlockMaxLength() const GLuint ProgramBinary::getActiveUniformBlockMaxLength() const
{ {
unsigned int maxLength = 0; unsigned int maxLength = 0;
unsigned int numUniformBlocks = mUniformBlocks.size(); unsigned int numUniformBlocks = mProgram->getUniformBlocks().size();
for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++) for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
{ {
const UniformBlock &uniformBlock = *mUniformBlocks[uniformBlockIndex]; const UniformBlock &uniformBlock = *mProgram->getUniformBlocks()[uniformBlockIndex];
if (!uniformBlock.name.empty()) if (!uniformBlock.name.empty())
{ {
const unsigned int length = uniformBlock.name.length() + 1; const unsigned int length = uniformBlock.name.length() + 1;
...@@ -2208,88 +1171,7 @@ void ProgramBinary::validate(InfoLog &infoLog, const Caps &caps) ...@@ -2208,88 +1171,7 @@ void ProgramBinary::validate(InfoLog &infoLog, const Caps &caps)
bool ProgramBinary::validateSamplers(InfoLog *infoLog, const Caps &caps) bool ProgramBinary::validateSamplers(InfoLog *infoLog, const Caps &caps)
{ {
// if any two active samplers in a program are of different types, but refer to the same return mProgram->validateSamplers(infoLog, caps);
// texture image unit, and this is the current program, then ValidateProgram will fail, and
// DrawArrays and DrawElements will issue the INVALID_OPERATION error.
updateSamplerMapping();
std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
{
if (mSamplersPS[i].active)
{
unsigned int unit = mSamplersPS[i].logicalTextureUnit;
if (unit >= textureUnitTypes.size())
{
if (infoLog)
{
infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
}
return false;
}
if (textureUnitTypes[unit] != GL_NONE)
{
if (mSamplersPS[i].textureType != textureUnitTypes[unit])
{
if (infoLog)
{
infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
}
return false;
}
}
else
{
textureUnitTypes[unit] = mSamplersPS[i].textureType;
}
}
}
for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
{
if (mSamplersVS[i].active)
{
unsigned int unit = mSamplersVS[i].logicalTextureUnit;
if (unit >= textureUnitTypes.size())
{
if (infoLog)
{
infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
}
return false;
}
if (textureUnitTypes[unit] != GL_NONE)
{
if (mSamplersVS[i].textureType != textureUnitTypes[unit])
{
if (infoLog)
{
infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
}
return false;
}
}
else
{
textureUnitTypes[unit] = mSamplersVS[i].textureType;
}
}
}
return true;
}
ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
{
} }
struct AttributeSorter struct AttributeSorter
...@@ -2338,16 +1220,6 @@ void ProgramBinary::sortAttributesByLayout(rx::TranslatedAttribute attributes[MA ...@@ -2338,16 +1220,6 @@ void ProgramBinary::sortAttributesByLayout(rx::TranslatedAttribute attributes[MA
void ProgramBinary::reset() void ProgramBinary::reset()
{ {
mSamplersPS.clear();
mSamplersVS.clear();
mUsedVertexSamplerRange = 0;
mUsedPixelSamplerRange = 0;
mDirtySamplerMapping = true;
SafeDeleteContainer(mUniforms);
SafeDeleteContainer(mUniformBlocks);
mUniformIndex.clear();
mOutputVariables.clear(); mOutputVariables.clear();
mProgram->reset(); mProgram->reset();
......
...@@ -141,8 +141,6 @@ class ProgramBinary : public RefCountObject ...@@ -141,8 +141,6 @@ class ProgramBinary : public RefCountObject
void getUniformiv(GLint location, GLint *params); void getUniformiv(GLint location, GLint *params);
void getUniformuiv(GLint location, GLuint *params); void getUniformuiv(GLint location, GLuint *params);
void dirtyAllUniforms();
Error applyUniforms(); Error applyUniforms();
Error applyUniformBuffers(const std::vector<Buffer*> boundBuffers, const Caps &caps); Error applyUniformBuffers(const std::vector<Buffer*> boundBuffers, const Caps &caps);
...@@ -188,25 +186,19 @@ class ProgramBinary : public RefCountObject ...@@ -188,25 +186,19 @@ class ProgramBinary : public RefCountObject
void initAttributesByLayout(); void initAttributesByLayout();
void sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const; void sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const;
const std::vector<LinkedUniform*> &getUniforms() const { return mUniforms; }
static bool linkVaryings(InfoLog &infoLog, Shader *fragmentShader, Shader *vertexShader); static bool linkVaryings(InfoLog &infoLog, Shader *fragmentShader, Shader *vertexShader);
static bool linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform);
static bool linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform);
private: private:
DISALLOW_COPY_AND_ASSIGN(ProgramBinary); DISALLOW_COPY_AND_ASSIGN(ProgramBinary);
struct Sampler
{
Sampler();
bool active;
GLint logicalTextureUnit;
GLenum textureType;
};
void reset(); void reset();
bool linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, const Shader *vertexShader); bool linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, const Shader *vertexShader);
bool linkUniformBlocks(InfoLog &infoLog, const Shader &vertexShader, const Shader &fragmentShader, const Caps &caps);
bool areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
const sh::InterfaceBlock &fragmentInterfaceBlock);
static bool linkValidateVariablesBase(InfoLog &infoLog, static bool linkValidateVariablesBase(InfoLog &infoLog,
const std::string &variableName, const std::string &variableName,
...@@ -214,55 +206,21 @@ class ProgramBinary : public RefCountObject ...@@ -214,55 +206,21 @@ class ProgramBinary : public RefCountObject
const sh::ShaderVariable &fragmentVariable, const sh::ShaderVariable &fragmentVariable,
bool validatePrecision); bool validatePrecision);
static bool linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform);
static bool linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying); static bool linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying);
static bool linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform);
bool linkUniforms(InfoLog &infoLog, const Shader &vertexShader, const Shader &fragmentShader, const Caps &caps);
void defineUniformBase(GLenum shader, const sh::Uniform &uniform, unsigned int uniformRegister);
void defineUniform(GLenum shader, const sh::ShaderVariable &uniform, const std::string &fullName, sh::HLSLBlockEncoder *encoder);
bool indexSamplerUniform(const LinkedUniform &uniform, InfoLog &infoLog, const Caps &caps);
bool indexUniforms(InfoLog &infoLog, const Caps &caps);
static bool assignSamplers(unsigned int startSamplerIndex, GLenum samplerType, unsigned int samplerCount,
std::vector<Sampler> &outSamplers, GLuint *outUsedRange);
bool areMatchingInterfaceBlocks(InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock, const sh::InterfaceBlock &fragmentInterfaceBlock);
bool linkUniformBlocks(InfoLog &infoLog, const Shader &vertexShader, const Shader &fragmentShader, const Caps &caps);
bool gatherTransformFeedbackLinkedVaryings(InfoLog &infoLog, const std::vector<LinkedVarying> &linkedVaryings, bool gatherTransformFeedbackLinkedVaryings(InfoLog &infoLog, const std::vector<LinkedVarying> &linkedVaryings,
const std::vector<std::string> &transformFeedbackVaryingNames, const std::vector<std::string> &transformFeedbackVaryingNames,
GLenum transformFeedbackBufferMode, GLenum transformFeedbackBufferMode,
std::vector<LinkedVarying> *outTransformFeedbackLinkedVaryings, std::vector<LinkedVarying> *outTransformFeedbackLinkedVaryings,
const Caps &caps) const; const Caps &caps) const;
template <typename VarT>
void defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
bool inRowMajorLayout);
bool defineUniformBlock(InfoLog &infoLog, const Shader &shader, const sh::InterfaceBlock &interfaceBlock, const Caps &caps);
bool assignUniformBlockRegister(InfoLog &infoLog, UniformBlock *uniformBlock, GLenum shader, unsigned int registerIndex, const Caps &caps); bool assignUniformBlockRegister(InfoLog &infoLog, UniformBlock *uniformBlock, GLenum shader, unsigned int registerIndex, const Caps &caps);
void defineOutputVariables(Shader *fragmentShader); void defineOutputVariables(Shader *fragmentShader);
template <typename T>
void setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType);
template <int cols, int rows>
void setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType);
template <typename T>
void getUniformv(GLint location, T *params, GLenum uniformType);
rx::ProgramImpl *mProgram; rx::ProgramImpl *mProgram;
sh::Attribute mLinkedAttribute[MAX_VERTEX_ATTRIBS]; sh::Attribute mLinkedAttribute[MAX_VERTEX_ATTRIBS];
int mSemanticIndex[MAX_VERTEX_ATTRIBS]; int mSemanticIndex[MAX_VERTEX_ATTRIBS];
int mAttributesByLayout[MAX_VERTEX_ATTRIBS]; int mAttributesByLayout[MAX_VERTEX_ATTRIBS];
std::vector<Sampler> mSamplersPS;
std::vector<Sampler> mSamplersVS;
GLuint mUsedVertexSamplerRange;
GLuint mUsedPixelSamplerRange;
bool mDirtySamplerMapping;
std::vector<LinkedUniform*> mUniforms;
std::vector<UniformBlock*> mUniformBlocks;
std::vector<VariableLocation> mUniformIndex;
std::map<int, VariableLocation> mOutputVariables; std::map<int, VariableLocation> mOutputVariables;
bool mValidated; bool mValidated;
......
//
// Copyright (c) 2014 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.
//
// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
#include "libGLESv2/renderer/ProgramImpl.h"
#include "common/utilities.h"
#include "libGLESv2/main.h"
namespace rx
{
namespace
{
unsigned int ParseAndStripArrayIndex(std::string* name)
{
unsigned int subscript = GL_INVALID_INDEX;
// 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)
{
subscript = atoi(name->substr(open + 1).c_str());
name->erase(open);
}
return subscript;
}
}
ProgramImpl::~ProgramImpl()
{
// Ensure that reset was called by the inherited class during destruction
ASSERT(mUniformIndex.size() == 0);
}
gl::LinkedUniform *ProgramImpl::getUniformByLocation(GLint location) const
{
ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformIndex.size());
return mUniforms[mUniformIndex[location].index];
}
gl::LinkedUniform *ProgramImpl::getUniformByName(const std::string &name) const
{
for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
{
if (mUniforms[uniformIndex]->name == name)
{
return mUniforms[uniformIndex];
}
}
return NULL;
}
gl::UniformBlock *ProgramImpl::getUniformBlockByIndex(GLuint blockIndex) const
{
ASSERT(blockIndex < mUniformBlocks.size());
return mUniformBlocks[blockIndex];
}
GLint ProgramImpl::getUniformLocation(std::string name)
{
unsigned int subscript = ParseAndStripArrayIndex(&name);
unsigned int numUniforms = mUniformIndex.size();
for (unsigned int location = 0; location < numUniforms; location++)
{
if (mUniformIndex[location].name == name)
{
const int index = mUniformIndex[location].index;
const bool isArray = mUniforms[index]->isArray();
if ((isArray && mUniformIndex[location].element == subscript) ||
(subscript == GL_INVALID_INDEX))
{
return location;
}
}
}
return -1;
}
GLuint ProgramImpl::getUniformIndex(std::string name)
{
unsigned int subscript = ParseAndStripArrayIndex(&name);
// The app is not allowed to specify array indices other than 0 for arrays of basic types
if (subscript != 0 && subscript != GL_INVALID_INDEX)
{
return GL_INVALID_INDEX;
}
unsigned int numUniforms = mUniforms.size();
for (unsigned int index = 0; index < numUniforms; index++)
{
if (mUniforms[index]->name == name)
{
if (mUniforms[index]->isArray() || subscript == GL_INVALID_INDEX)
{
return index;
}
}
}
return GL_INVALID_INDEX;
}
GLuint ProgramImpl::getUniformBlockIndex(std::string name) const
{
unsigned int subscript = ParseAndStripArrayIndex(&name);
unsigned int numUniformBlocks = mUniformBlocks.size();
for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
{
const gl::UniformBlock &uniformBlock = *mUniformBlocks[blockIndex];
if (uniformBlock.name == name)
{
const bool arrayElementZero = (subscript == GL_INVALID_INDEX && uniformBlock.elementIndex == 0);
if (subscript == uniformBlock.elementIndex || arrayElementZero)
{
return blockIndex;
}
}
}
return GL_INVALID_INDEX;
}
void ProgramImpl::reset()
{
SafeDeleteContainer(mUniforms);
mUniformIndex.clear();
SafeDeleteContainer(mUniformBlocks);
mTransformFeedbackLinkedVaryings.clear();
}
}
...@@ -13,8 +13,11 @@ ...@@ -13,8 +13,11 @@
#include "libGLESv2/BinaryStream.h" #include "libGLESv2/BinaryStream.h"
#include "libGLESv2/Constants.h" #include "libGLESv2/Constants.h"
#include "libGLESv2/ProgramBinary.h" #include "libGLESv2/ProgramBinary.h"
#include "libGLESv2/Shader.h"
#include "libGLESv2/renderer/Renderer.h" #include "libGLESv2/renderer/Renderer.h"
#include <map>
namespace rx namespace rx
{ {
...@@ -22,37 +25,101 @@ class Renderer; ...@@ -22,37 +25,101 @@ class Renderer;
class ProgramImpl class ProgramImpl
{ {
public: public:
virtual ~ProgramImpl() { } ProgramImpl() { }
virtual ~ProgramImpl();
const std::vector<gl::LinkedUniform*> &getUniforms() const { return mUniforms; }
const std::vector<gl::VariableLocation> &getUniformIndices() const { return mUniformIndex; }
const std::vector<gl::UniformBlock*> &getUniformBlocks() const { return mUniformBlocks; }
const std::vector<gl::LinkedVarying> &getTransformFeedbackLinkedVaryings() const { return mTransformFeedbackLinkedVaryings; }
const sh::Attribute *getShaderAttributes() const { return mShaderAttributes; }
std::vector<gl::LinkedUniform*> &getUniforms() { return mUniforms; }
std::vector<gl::VariableLocation> &getUniformIndices() { return mUniformIndex; }
std::vector<gl::UniformBlock*> &getUniformBlocks() { return mUniformBlocks; }
std::vector<gl::LinkedVarying> &getTransformFeedbackLinkedVaryings() { return mTransformFeedbackLinkedVaryings; }
sh::Attribute *getShaderAttributes() { return mShaderAttributes; }
gl::LinkedUniform *getUniformByLocation(GLint location) const;
gl::LinkedUniform *getUniformByName(const std::string &name) const;
gl::UniformBlock *getUniformBlockByIndex(GLuint blockIndex) const;
GLint getUniformLocation(std::string name);
GLuint getUniformIndex(std::string name);
GLuint getUniformBlockIndex(std::string name) const;
virtual bool usesPointSize() const = 0; virtual bool usesPointSize() const = 0;
virtual int getShaderVersion() const = 0; virtual int getShaderVersion() const = 0;
virtual GLenum getTransformFeedbackBufferMode() const = 0; virtual GLenum getTransformFeedbackBufferMode() const = 0;
virtual std::vector<gl::LinkedVarying> &getTransformFeedbackLinkedVaryings() = 0;
virtual sh::Attribute *getShaderAttributes() = 0;
virtual GLenum getBinaryFormat() = 0; virtual GLenum getBinaryFormat() = 0;
virtual gl::LinkResult load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream) = 0; virtual gl::LinkResult load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream) = 0;
virtual gl::Error save(gl::BinaryOutputStream *stream) = 0; virtual gl::Error save(gl::BinaryOutputStream *stream) = 0;
virtual gl::LinkResult compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
int registers) = 0;
virtual gl::LinkResult link(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader, virtual gl::LinkResult link(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
const std::vector<std::string> &transformFeedbackVaryings, GLenum transformFeedbackBufferMode, const std::vector<std::string> &transformFeedbackVaryings, GLenum transformFeedbackBufferMode,
int *registers, std::vector<gl::LinkedVarying> *linkedVaryings, int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
std::map<int, gl::VariableLocation> *outputVariables, const gl::Caps &caps) = 0; std::map<int, gl::VariableLocation> *outputVariables, const gl::Caps &caps) = 0;
virtual void initializeUniformStorage(const std::vector<gl::LinkedUniform*> &uniforms) = 0; virtual void setUniform1fv(GLint location, GLsizei count, const GLfloat *v) = 0;
virtual void setUniform2fv(GLint location, GLsizei count, const GLfloat *v) = 0;
virtual void setUniform3fv(GLint location, GLsizei count, const GLfloat *v) = 0;
virtual void setUniform4fv(GLint location, GLsizei count, const GLfloat *v) = 0;
virtual void setUniform1iv(GLint location, GLsizei count, const GLint *v) = 0;
virtual void setUniform2iv(GLint location, GLsizei count, const GLint *v) = 0;
virtual void setUniform3iv(GLint location, GLsizei count, const GLint *v) = 0;
virtual void setUniform4iv(GLint location, GLsizei count, const GLint *v) = 0;
virtual void setUniform1uiv(GLint location, GLsizei count, const GLuint *v) = 0;
virtual void setUniform2uiv(GLint location, GLsizei count, const GLuint *v) = 0;
virtual void setUniform3uiv(GLint location, GLsizei count, const GLuint *v) = 0;
virtual void setUniform4uiv(GLint location, GLsizei count, const GLuint *v) = 0;
virtual void setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) = 0;
virtual void setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) = 0;
virtual void setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) = 0;
virtual void setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) = 0;
virtual void setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) = 0;
virtual void setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) = 0;
virtual void setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) = 0;
virtual void setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) = 0;
virtual void setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) = 0;
virtual void getUniformfv(GLint location, GLfloat *params) = 0;
virtual void getUniformiv(GLint location, GLint *params) = 0;
virtual void getUniformuiv(GLint location, GLuint *params) = 0;
virtual gl::Error applyUniforms(const std::vector<gl::LinkedUniform*> &uniforms) = 0; virtual void reset();
virtual gl::Error applyUniformBuffers(const std::vector<gl::UniformBlock*> uniformBlocks, const std::vector<gl::Buffer*> boundBuffers,
const gl::Caps &caps) = 0; // TODO: The following functions are possibly only applicable to D3D backends. The should be carefully evaluated to
// determine if they can be removed from this interface.
virtual GLint getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const = 0;
virtual GLenum getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const = 0;
virtual GLint getUsedSamplerRange(gl::SamplerType type) const = 0;
virtual void updateSamplerMapping() = 0;
virtual bool validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps) = 0;
virtual gl::LinkResult compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
int registers) = 0;
virtual bool linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
const gl::Caps &caps) = 0;
virtual bool defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
const gl::Caps &caps) = 0;
virtual gl::Error applyUniforms() = 0;
virtual gl::Error applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps) = 0;
virtual bool assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader, virtual bool assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
unsigned int registerIndex, const gl::Caps &caps) = 0; unsigned int registerIndex, const gl::Caps &caps) = 0;
virtual unsigned int getReservedUniformVectors(GLenum shader) = 0;
virtual void reset() = 0; protected:
DISALLOW_COPY_AND_ASSIGN(ProgramImpl);
std::vector<gl::LinkedUniform*> mUniforms;
std::vector<gl::VariableLocation> mUniformIndex;
std::vector<gl::UniformBlock*> mUniformBlocks;
std::vector<gl::LinkedVarying> mTransformFeedbackLinkedVaryings;
sh::Attribute mShaderAttributes[gl::MAX_VERTEX_ATTRIBS];
}; };
} }
......
...@@ -25,6 +25,36 @@ namespace rx ...@@ -25,6 +25,36 @@ namespace rx
namespace namespace
{ {
GLenum GetTextureType(GLenum samplerType)
{
switch (samplerType)
{
case GL_SAMPLER_2D:
case GL_INT_SAMPLER_2D:
case GL_UNSIGNED_INT_SAMPLER_2D:
case GL_SAMPLER_2D_SHADOW:
return GL_TEXTURE_2D;
case GL_SAMPLER_3D:
case GL_INT_SAMPLER_3D:
case GL_UNSIGNED_INT_SAMPLER_3D:
return GL_TEXTURE_3D;
case GL_SAMPLER_CUBE:
case GL_SAMPLER_CUBE_SHADOW:
return GL_TEXTURE_CUBE_MAP;
case GL_INT_SAMPLER_CUBE:
case GL_UNSIGNED_INT_SAMPLER_CUBE:
return GL_TEXTURE_CUBE_MAP;
case GL_SAMPLER_2D_ARRAY:
case GL_INT_SAMPLER_2D_ARRAY:
case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
case GL_SAMPLER_2D_ARRAY_SHADOW:
return GL_TEXTURE_2D_ARRAY;
default: UNREACHABLE();
}
return GL_TEXTURE_2D;
}
void GetDefaultInputLayoutFromShader(const std::vector<sh::Attribute> &shaderAttributes, gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS]) void GetDefaultInputLayoutFromShader(const std::vector<sh::Attribute> &shaderAttributes, gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS])
{ {
size_t layoutIndex = 0; size_t layoutIndex = 0;
...@@ -61,6 +91,16 @@ std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShad ...@@ -61,6 +91,16 @@ std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShad
return defaultPixelOutput; return defaultPixelOutput;
} }
bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
{
return var.isRowMajorLayout;
}
bool IsRowMajorLayout(const sh::ShaderVariable &var)
{
return false;
}
} }
ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[], ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[],
...@@ -104,6 +144,10 @@ ProgramD3D::PixelExecutable::~PixelExecutable() ...@@ -104,6 +144,10 @@ ProgramD3D::PixelExecutable::~PixelExecutable()
SafeDelete(mShaderExecutable); SafeDelete(mShaderExecutable);
} }
ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
{
}
ProgramD3D::ProgramD3D(Renderer *renderer) ProgramD3D::ProgramD3D(Renderer *renderer)
: ProgramImpl(), : ProgramImpl(),
mRenderer(renderer), mRenderer(renderer),
...@@ -114,6 +158,9 @@ ProgramD3D::ProgramD3D(Renderer *renderer) ...@@ -114,6 +158,9 @@ ProgramD3D::ProgramD3D(Renderer *renderer)
mUsesPointSize(false), mUsesPointSize(false),
mVertexUniformStorage(NULL), mVertexUniformStorage(NULL),
mFragmentUniformStorage(NULL), mFragmentUniformStorage(NULL),
mUsedVertexSamplerRange(0),
mUsedPixelSamplerRange(0),
mDirtySamplerMapping(true),
mShaderVersion(100) mShaderVersion(100)
{ {
mDynamicHLSL = new DynamicHLSL(renderer); mDynamicHLSL = new DynamicHLSL(renderer);
...@@ -147,10 +194,313 @@ bool ProgramD3D::usesGeometryShader() const ...@@ -147,10 +194,313 @@ bool ProgramD3D::usesGeometryShader() const
return usesPointSpriteEmulation(); return usesPointSpriteEmulation();
} }
GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
{
GLint logicalTextureUnit = -1;
switch (type)
{
case gl::SAMPLER_PIXEL:
ASSERT(samplerIndex < caps.maxTextureImageUnits);
if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
{
logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
}
break;
case gl::SAMPLER_VERTEX:
ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
{
logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
}
break;
default: UNREACHABLE();
}
if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
{
return logicalTextureUnit;
}
return -1;
}
// 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).
GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
{
switch (type)
{
case gl::SAMPLER_PIXEL:
ASSERT(samplerIndex < mSamplersPS.size());
ASSERT(mSamplersPS[samplerIndex].active);
return mSamplersPS[samplerIndex].textureType;
case gl::SAMPLER_VERTEX:
ASSERT(samplerIndex < mSamplersVS.size());
ASSERT(mSamplersVS[samplerIndex].active);
return mSamplersVS[samplerIndex].textureType;
default: UNREACHABLE();
}
return GL_TEXTURE_2D;
}
GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
{
switch (type)
{
case gl::SAMPLER_PIXEL:
return mUsedPixelSamplerRange;
case gl::SAMPLER_VERTEX:
return mUsedVertexSamplerRange;
default:
UNREACHABLE();
return 0;
}
}
void ProgramD3D::updateSamplerMapping()
{
if (!mDirtySamplerMapping)
{
return;
}
mDirtySamplerMapping = false;
// Retrieve sampler uniform values
for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
{
gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
if (targetUniform->dirty)
{
if (gl::IsSampler(targetUniform->type))
{
int count = targetUniform->elementCount();
GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
if (targetUniform->isReferencedByFragmentShader())
{
unsigned int firstIndex = targetUniform->psRegisterIndex;
for (int i = 0; i < count; i++)
{
unsigned int samplerIndex = firstIndex + i;
if (samplerIndex < mSamplersPS.size())
{
ASSERT(mSamplersPS[samplerIndex].active);
mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
}
}
}
if (targetUniform->isReferencedByVertexShader())
{
unsigned int firstIndex = targetUniform->vsRegisterIndex;
for (int i = 0; i < count; i++)
{
unsigned int samplerIndex = firstIndex + i;
if (samplerIndex < mSamplersVS.size())
{
ASSERT(mSamplersVS[samplerIndex].active);
mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
}
}
}
}
}
}
}
bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
{
// 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.
updateSamplerMapping();
std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
{
if (mSamplersPS[i].active)
{
unsigned int unit = mSamplersPS[i].logicalTextureUnit;
if (unit >= textureUnitTypes.size())
{
if (infoLog)
{
infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
}
return false;
}
if (textureUnitTypes[unit] != GL_NONE)
{
if (mSamplersPS[i].textureType != textureUnitTypes[unit])
{
if (infoLog)
{
infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
}
return false;
}
}
else
{
textureUnitTypes[unit] = mSamplersPS[i].textureType;
}
}
}
for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
{
if (mSamplersVS[i].active)
{
unsigned int unit = mSamplersVS[i].logicalTextureUnit;
if (unit >= textureUnitTypes.size())
{
if (infoLog)
{
infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
}
return false;
}
if (textureUnitTypes[unit] != GL_NONE)
{
if (mSamplersVS[i].textureType != textureUnitTypes[unit])
{
if (infoLog)
{
infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
}
return false;
}
}
else
{
textureUnitTypes[unit] = mSamplersVS[i].textureType;
}
}
}
return true;
}
gl::LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream) gl::LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
{ {
stream->readInt(&mShaderVersion); stream->readInt(&mShaderVersion);
const unsigned int psSamplerCount = stream->readInt<unsigned int>();
for (unsigned int i = 0; i < psSamplerCount; ++i)
{
Sampler sampler;
stream->readBool(&sampler.active);
stream->readInt(&sampler.logicalTextureUnit);
stream->readInt(&sampler.textureType);
mSamplersPS.push_back(sampler);
}
const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
for (unsigned int i = 0; i < vsSamplerCount; ++i)
{
Sampler sampler;
stream->readBool(&sampler.active);
stream->readInt(&sampler.logicalTextureUnit);
stream->readInt(&sampler.textureType);
mSamplersVS.push_back(sampler);
}
stream->readInt(&mUsedVertexSamplerRange);
stream->readInt(&mUsedPixelSamplerRange);
const unsigned int uniformCount = stream->readInt<unsigned int>();
if (stream->error())
{
infoLog.append("Invalid program binary.");
return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
}
mUniforms.resize(uniformCount);
for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
{
GLenum type = stream->readInt<GLenum>();
GLenum precision = stream->readInt<GLenum>();
std::string name = stream->readString();
unsigned int arraySize = stream->readInt<unsigned int>();
int blockIndex = stream->readInt<int>();
int offset = stream->readInt<int>();
int arrayStride = stream->readInt<int>();
int matrixStride = stream->readInt<int>();
bool isRowMajorMatrix = stream->readBool();
const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
stream->readInt(&uniform->psRegisterIndex);
stream->readInt(&uniform->vsRegisterIndex);
stream->readInt(&uniform->registerCount);
stream->readInt(&uniform->registerElement);
mUniforms[uniformIndex] = uniform;
}
const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
if (stream->error())
{
infoLog.append("Invalid program binary.");
return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
}
mUniformIndex.resize(uniformIndexCount);
for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
{
stream->readString(&mUniformIndex[uniformIndexIndex].name);
stream->readInt(&mUniformIndex[uniformIndexIndex].element);
stream->readInt(&mUniformIndex[uniformIndexIndex].index);
}
unsigned int uniformBlockCount = stream->readInt<unsigned int>();
if (stream->error())
{
infoLog.append("Invalid program binary.");
return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
}
mUniformBlocks.resize(uniformBlockCount);
for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
{
std::string name = stream->readString();
unsigned int elementIndex = stream->readInt<unsigned int>();
unsigned int dataSize = stream->readInt<unsigned int>();
gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
stream->readInt(&uniformBlock->psRegisterIndex);
stream->readInt(&uniformBlock->vsRegisterIndex);
unsigned int numMembers = stream->readInt<unsigned int>();
uniformBlock->memberUniformIndexes.resize(numMembers);
for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
{
stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
}
mUniformBlocks[uniformBlockIndex] = uniformBlock;
}
stream->readInt(&mTransformFeedbackBufferMode); stream->readInt(&mTransformFeedbackBufferMode);
const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>(); const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount); mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
...@@ -295,6 +645,8 @@ gl::LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *str ...@@ -295,6 +645,8 @@ gl::LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *str
return gl::LinkResult(false, gl::Error(GL_NO_ERROR)); return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
} }
initializeUniformStorage();
return gl::LinkResult(true, gl::Error(GL_NO_ERROR)); return gl::LinkResult(true, gl::Error(GL_NO_ERROR));
} }
...@@ -302,6 +654,74 @@ gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream) ...@@ -302,6 +654,74 @@ gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
{ {
stream->writeInt(mShaderVersion); stream->writeInt(mShaderVersion);
stream->writeInt(mSamplersPS.size());
for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
{
stream->writeInt(mSamplersPS[i].active);
stream->writeInt(mSamplersPS[i].logicalTextureUnit);
stream->writeInt(mSamplersPS[i].textureType);
}
stream->writeInt(mSamplersVS.size());
for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
{
stream->writeInt(mSamplersVS[i].active);
stream->writeInt(mSamplersVS[i].logicalTextureUnit);
stream->writeInt(mSamplersVS[i].textureType);
}
stream->writeInt(mUsedVertexSamplerRange);
stream->writeInt(mUsedPixelSamplerRange);
stream->writeInt(mUniforms.size());
for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
{
const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
stream->writeInt(uniform.type);
stream->writeInt(uniform.precision);
stream->writeString(uniform.name);
stream->writeInt(uniform.arraySize);
stream->writeInt(uniform.blockIndex);
stream->writeInt(uniform.blockInfo.offset);
stream->writeInt(uniform.blockInfo.arrayStride);
stream->writeInt(uniform.blockInfo.matrixStride);
stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
stream->writeInt(uniform.psRegisterIndex);
stream->writeInt(uniform.vsRegisterIndex);
stream->writeInt(uniform.registerCount);
stream->writeInt(uniform.registerElement);
}
stream->writeInt(mUniformIndex.size());
for (size_t i = 0; i < mUniformIndex.size(); ++i)
{
stream->writeString(mUniformIndex[i].name);
stream->writeInt(mUniformIndex[i].element);
stream->writeInt(mUniformIndex[i].index);
}
stream->writeInt(mUniformBlocks.size());
for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
{
const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
stream->writeString(uniformBlock.name);
stream->writeInt(uniformBlock.elementIndex);
stream->writeInt(uniformBlock.dataSize);
stream->writeInt(uniformBlock.memberUniformIndexes.size());
for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
{
stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
}
stream->writeInt(uniformBlock.psRegisterIndex);
stream->writeInt(uniformBlock.vsRegisterIndex);
}
stream->writeInt(mTransformFeedbackBufferMode); stream->writeInt(mTransformFeedbackBufferMode);
stream->writeInt(mTransformFeedbackLinkedVaryings.size()); stream->writeInt(mTransformFeedbackLinkedVaryings.size());
for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++) for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
...@@ -567,6 +987,9 @@ gl::LinkResult ProgramD3D::link(gl::InfoLog &infoLog, gl::Shader *fragmentShader ...@@ -567,6 +987,9 @@ gl::LinkResult ProgramD3D::link(gl::InfoLog &infoLog, gl::Shader *fragmentShader
ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation()); ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation()); ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
mSamplersPS.resize(caps.maxTextureImageUnits);
mSamplersVS.resize(caps.maxVertexTextureImageUnits);
mTransformFeedbackBufferMode = transformFeedbackBufferMode; mTransformFeedbackBufferMode = transformFeedbackBufferMode;
mPixelHLSL = fragmentShaderD3D->getTranslatedSource(); mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
...@@ -607,14 +1030,14 @@ void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], G ...@@ -607,14 +1030,14 @@ void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], G
mDynamicHLSL->getInputLayoutSignature(inputLayout, signature); mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
} }
void ProgramD3D::initializeUniformStorage(const std::vector<gl::LinkedUniform*> &uniforms) void ProgramD3D::initializeUniformStorage()
{ {
// Compute total default block size // Compute total default block size
unsigned int vertexRegisters = 0; unsigned int vertexRegisters = 0;
unsigned int fragmentRegisters = 0; unsigned int fragmentRegisters = 0;
for (size_t uniformIndex = 0; uniformIndex < uniforms.size(); uniformIndex++) for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
{ {
const gl::LinkedUniform &uniform = *uniforms[uniformIndex]; const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
if (!gl::IsSampler(uniform.type)) if (!gl::IsSampler(uniform.type))
{ {
...@@ -633,23 +1056,37 @@ void ProgramD3D::initializeUniformStorage(const std::vector<gl::LinkedUniform*> ...@@ -633,23 +1056,37 @@ void ProgramD3D::initializeUniformStorage(const std::vector<gl::LinkedUniform*>
mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u); mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
} }
gl::Error ProgramD3D::applyUniforms(const std::vector<gl::LinkedUniform*> &uniforms) gl::Error ProgramD3D::applyUniforms()
{ {
return mRenderer->applyUniforms(*this, uniforms); updateSamplerMapping();
gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
if (error.isError())
{
return error;
}
for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
{
mUniforms[uniformIndex]->dirty = false;
}
return gl::Error(GL_NO_ERROR);
} }
gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::UniformBlock*> uniformBlocks, const std::vector<gl::Buffer*> boundBuffers, gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
const gl::Caps &caps)
{ {
ASSERT(boundBuffers.size() == mUniformBlocks.size());
const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL}; const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL}; const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers(); const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers(); const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlocks.size(); uniformBlockIndex++) for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
{ {
gl::UniformBlock *uniformBlock = uniformBlocks[uniformBlockIndex]; gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex]; gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
ASSERT(uniformBlock && uniformBuffer); ASSERT(uniformBlock && uniformBuffer);
...@@ -712,42 +1149,774 @@ bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlo ...@@ -712,42 +1149,774 @@ bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlo
return true; return true;
} }
unsigned int ProgramD3D::getReservedUniformVectors(GLenum shader) void ProgramD3D::dirtyAllUniforms()
{ {
if (shader == GL_VERTEX_SHADER) unsigned int numUniforms = mUniforms.size();
for (unsigned int index = 0; index < numUniforms; index++)
{ {
return mRenderer->getReservedVertexUniformVectors(); mUniforms[index]->dirty = true;
} }
else if (shader == GL_FRAGMENT_SHADER) }
{
return mRenderer->getReservedFragmentUniformVectors();
}
else UNREACHABLE();
return 0; void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
{
setUniform(location, count, v, GL_FLOAT);
} }
void ProgramD3D::reset() void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
{ {
SafeDeleteContainer(mVertexExecutables); setUniform(location, count, v, GL_FLOAT_VEC2);
SafeDeleteContainer(mPixelExecutables); }
SafeDelete(mGeometryExecutable);
mTransformFeedbackBufferMode = GL_NONE; void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
mTransformFeedbackLinkedVaryings.clear(); {
setUniform(location, count, v, GL_FLOAT_VEC3);
}
mVertexHLSL.clear(); void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE; {
mShaderVersion = 100; setUniform(location, count, v, GL_FLOAT_VEC4);
}
mPixelHLSL.clear(); void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE; {
mUsesFragDepth = false; setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
mPixelShaderKey.clear(); }
mUsesPointSize = false;
SafeDelete(mVertexUniformStorage); void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
SafeDelete(mFragmentUniformStorage); {
setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
}
void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
{
setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
}
void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
{
setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
}
void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
{
setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
}
void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
{
setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
}
void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
{
setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
}
void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
{
setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
}
void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
{
setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
}
void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
{
setUniform(location, count, v, GL_INT);
}
void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
{
setUniform(location, count, v, GL_INT_VEC2);
}
void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
{
setUniform(location, count, v, GL_INT_VEC3);
}
void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
{
setUniform(location, count, v, GL_INT_VEC4);
}
void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
{
setUniform(location, count, v, GL_UNSIGNED_INT);
}
void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
{
setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
}
void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
{
setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
}
void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
{
setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
}
void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
{
getUniformv(location, params, GL_FLOAT);
}
void ProgramD3D::getUniformiv(GLint location, GLint *params)
{
getUniformv(location, params, GL_INT);
}
void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
{
getUniformv(location, params, GL_UNSIGNED_INT);
}
bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
const gl::Caps &caps)
{
const rx::ShaderD3D *vertexShaderD3D = rx::ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
const rx::ShaderD3D *fragmentShaderD3D = rx::ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
// Check that uniforms defined in the vertex and fragment shaders are identical
typedef std::map<std::string, const sh::Uniform*> UniformMap;
UniformMap linkedUniforms;
for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
{
const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
linkedUniforms[vertexUniform.name] = &vertexUniform;
}
for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
{
const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
if (entry != linkedUniforms.end())
{
const sh::Uniform &vertexUniform = *entry->second;
const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
if (!gl::ProgramBinary::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
{
return false;
}
}
}
for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
{
const sh::Uniform &uniform = vertexUniforms[uniformIndex];
if (uniform.staticUse)
{
defineUniformBase(GL_VERTEX_SHADER, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
}
}
for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
{
const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
if (uniform.staticUse)
{
defineUniformBase(GL_FRAGMENT_SHADER, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
}
}
if (!indexUniforms(infoLog, caps))
{
return false;
}
initializeUniformStorage();
// special case for gl_DepthRange, the only built-in uniform (also a struct)
if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
{
const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
}
return true;
}
void ProgramD3D::defineUniformBase(GLenum shader, const sh::Uniform &uniform, unsigned int uniformRegister)
{
ShShaderOutput outputType = rx::ShaderD3D::getCompilerOutputType(shader);
sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
encoder.skipRegisters(uniformRegister);
defineUniform(shader, uniform, uniform.name, &encoder);
}
void ProgramD3D::defineUniform(GLenum shader, const sh::ShaderVariable &uniform,
const std::string &fullName, sh::HLSLBlockEncoder *encoder)
{
if (uniform.isStruct())
{
for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
{
const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
encoder->enterAggregateType();
for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
{
const sh::ShaderVariable &field = uniform.fields[fieldIndex];
const std::string &fieldFullName = (fullName + elementString + "." + field.name);
defineUniform(shader, field, fieldFullName, encoder);
}
encoder->exitAggregateType();
}
}
else // Not a struct
{
// Arrays are treated as aggregate types
if (uniform.isArray())
{
encoder->enterAggregateType();
}
gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
if (!linkedUniform)
{
linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
-1, sh::BlockMemberInfo::getDefaultBlockInfo());
ASSERT(linkedUniform);
linkedUniform->registerElement = encoder->getCurrentElement();
mUniforms.push_back(linkedUniform);
}
ASSERT(linkedUniform->registerElement == encoder->getCurrentElement());
if (shader == GL_FRAGMENT_SHADER)
{
linkedUniform->psRegisterIndex = encoder->getCurrentRegister();
}
else if (shader == GL_VERTEX_SHADER)
{
linkedUniform->vsRegisterIndex = encoder->getCurrentRegister();
}
else UNREACHABLE();
// Advance the uniform offset, to track registers allocation for structs
encoder->encodeType(uniform.type, uniform.arraySize, false);
// Arrays are treated as aggregate types
if (uniform.isArray())
{
encoder->exitAggregateType();
}
}
}
template <typename T>
static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
{
ASSERT(dest != NULL);
ASSERT(dirtyFlag != NULL);
*dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
*dest = source;
}
template <typename T>
void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
{
const int components = gl::VariableComponentCount(targetUniformType);
const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
gl::LinkedUniform *targetUniform = getUniformByLocation(location);
int elementCount = targetUniform->elementCount();
count = std::min(elementCount - (int)mUniformIndex[location].element, count);
if (targetUniform->type == targetUniformType)
{
T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
for (int i = 0; i < count; i++)
{
T *dest = target + (i * 4);
const T *source = v + (i * components);
for (int c = 0; c < components; c++)
{
SetIfDirty(dest + c, source[c], &targetUniform->dirty);
}
for (int c = components; c < 4; c++)
{
SetIfDirty(dest + c, T(0), &targetUniform->dirty);
}
}
}
else if (targetUniform->type == targetBoolType)
{
GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
for (int i = 0; i < count; i++)
{
GLint *dest = boolParams + (i * 4);
const T *source = v + (i * components);
for (int c = 0; c < components; c++)
{
SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
}
for (int c = components; c < 4; c++)
{
SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
}
}
}
else if (gl::IsSampler(targetUniform->type))
{
ASSERT(targetUniformType == GL_INT);
GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
bool wasDirty = targetUniform->dirty;
for (int i = 0; i < count; i++)
{
GLint *dest = target + (i * 4);
const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
SetIfDirty(dest + 1, 0, &targetUniform->dirty);
SetIfDirty(dest + 2, 0, &targetUniform->dirty);
SetIfDirty(dest + 3, 0, &targetUniform->dirty);
}
if (!wasDirty && targetUniform->dirty)
{
mDirtySamplerMapping = true;
}
}
else UNREACHABLE();
}
template<typename T>
bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
{
bool dirty = false;
int copyWidth = std::min(targetHeight, srcWidth);
int copyHeight = std::min(targetWidth, srcHeight);
for (int x = 0; x < copyWidth; x++)
{
for (int y = 0; y < copyHeight; y++)
{
SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
}
}
// clear unfilled right side
for (int y = 0; y < copyWidth; y++)
{
for (int x = copyHeight; x < targetWidth; x++)
{
SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
}
}
// clear unfilled bottom.
for (int y = copyWidth; y < targetHeight; y++)
{
for (int x = 0; x < targetWidth; x++)
{
SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
}
}
return dirty;
}
template<typename T>
bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
{
bool dirty = false;
int copyWidth = std::min(targetWidth, srcWidth);
int copyHeight = std::min(targetHeight, srcHeight);
for (int y = 0; y < copyHeight; y++)
{
for (int x = 0; x < copyWidth; x++)
{
SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
}
}
// clear unfilled right side
for (int y = 0; y < copyHeight; y++)
{
for (int x = copyWidth; x < targetWidth; x++)
{
SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
}
}
// clear unfilled bottom.
for (int y = copyHeight; y < targetHeight; y++)
{
for (int x = 0; x < targetWidth; x++)
{
SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
}
}
return dirty;
}
template <int cols, int rows>
void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
{
gl::LinkedUniform *targetUniform = getUniformByLocation(location);
int elementCount = targetUniform->elementCount();
count = std::min(elementCount - (int)mUniformIndex[location].element, count);
const unsigned int targetMatrixStride = (4 * rows);
GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
for (int i = 0; i < count; i++)
{
// Internally store matrices as transposed versions to accomodate HLSL matrix indexing
if (transpose == GL_FALSE)
{
targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
}
else
{
targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
}
target += targetMatrixStride;
value += cols * rows;
}
}
template <typename T>
void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
{
gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
if (gl::IsMatrixType(targetUniform->type))
{
const int rows = gl::VariableRowCount(targetUniform->type);
const int cols = gl::VariableColumnCount(targetUniform->type);
transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
}
else if (uniformType == gl::VariableComponentType(targetUniform->type))
{
unsigned int size = gl::VariableComponentCount(targetUniform->type);
memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
size * sizeof(T));
}
else
{
unsigned int size = gl::VariableComponentCount(targetUniform->type);
switch (gl::VariableComponentType(targetUniform->type))
{
case GL_BOOL:
{
GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
for (unsigned int i = 0; i < size; i++)
{
params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
}
}
break;
case GL_FLOAT:
{
GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
for (unsigned int i = 0; i < size; i++)
{
params[i] = static_cast<T>(floatParams[i]);
}
}
break;
case GL_INT:
{
GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
for (unsigned int i = 0; i < size; i++)
{
params[i] = static_cast<T>(intParams[i]);
}
}
break;
case GL_UNSIGNED_INT:
{
GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
for (unsigned int i = 0; i < size; i++)
{
params[i] = static_cast<T>(uintParams[i]);
}
}
break;
default: UNREACHABLE();
}
}
}
template <typename VarT>
void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
bool inRowMajorLayout)
{
for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
{
const VarT &field = fields[uniformIndex];
const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
if (field.isStruct())
{
bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
{
encoder->enterAggregateType();
const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
encoder->exitAggregateType();
}
}
else
{
bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
blockIndex, memberInfo);
// add to uniform list, but not index, since uniform block uniforms have no location
blockUniformIndexes->push_back(mUniforms.size());
mUniforms.push_back(newUniform);
}
}
}
bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
const gl::Caps &caps)
{
const rx::ShaderD3D* shaderD3D = rx::ShaderD3D::makeShaderD3D(shader.getImplementation());
// create uniform block entries if they do not exist
if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
{
std::vector<unsigned int> blockUniformIndexes;
const unsigned int blockIndex = mUniformBlocks.size();
// define member uniforms
sh::BlockLayoutEncoder *encoder = NULL;
if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
{
encoder = new sh::Std140BlockEncoder;
}
else
{
encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
}
ASSERT(encoder);
defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
size_t dataSize = encoder->getBlockSize();
// create all the uniform blocks
if (interfaceBlock.arraySize > 0)
{
for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
{
gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
newUniformBlock->memberUniformIndexes = blockUniformIndexes;
mUniformBlocks.push_back(newUniformBlock);
}
}
else
{
gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
newUniformBlock->memberUniformIndexes = blockUniformIndexes;
mUniformBlocks.push_back(newUniformBlock);
}
}
if (interfaceBlock.staticUse)
{
// Assign registers to the uniform blocks
const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
ASSERT(blockIndex != GL_INVALID_INDEX);
ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
{
gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
ASSERT(uniformBlock->name == interfaceBlock.name);
if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
interfaceBlockRegister + uniformBlockElement, caps))
{
return false;
}
}
}
return true;
}
bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
GLenum samplerType,
unsigned int samplerCount,
std::vector<Sampler> &outSamplers,
GLuint *outUsedRange)
{
unsigned int samplerIndex = startSamplerIndex;
do
{
if (samplerIndex < outSamplers.size())
{
Sampler& sampler = outSamplers[samplerIndex];
sampler.active = true;
sampler.textureType = GetTextureType(samplerType);
sampler.logicalTextureUnit = 0;
*outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
}
else
{
return false;
}
samplerIndex++;
} while (samplerIndex < startSamplerIndex + samplerCount);
return true;
}
bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
{
ASSERT(gl::IsSampler(uniform.type));
ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
{
if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
&mUsedVertexSamplerRange))
{
infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
mSamplersVS.size());
return false;
}
unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
{
infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
caps.maxVertexUniformVectors);
return false;
}
}
if (uniform.psRegisterIndex != GL_INVALID_INDEX)
{
if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
&mUsedPixelSamplerRange))
{
infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
mSamplersPS.size());
return false;
}
unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
{
infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
caps.maxFragmentUniformVectors);
return false;
}
}
return true;
}
bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
{
for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
{
const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
if (gl::IsSampler(uniform.type))
{
if (!indexSamplerUniform(uniform, infoLog, caps))
{
return false;
}
}
for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
{
mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
}
}
return true;
}
void ProgramD3D::reset()
{
ProgramImpl::reset();
SafeDeleteContainer(mVertexExecutables);
SafeDeleteContainer(mPixelExecutables);
SafeDelete(mGeometryExecutable);
mTransformFeedbackBufferMode = GL_NONE;
mVertexHLSL.clear();
mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
mShaderVersion = 100;
mPixelHLSL.clear();
mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
mUsesFragDepth = false;
mPixelShaderKey.clear();
mUsesPointSize = false;
SafeDelete(mVertexUniformStorage);
SafeDelete(mFragmentUniformStorage);
mSamplersPS.clear();
mSamplersVS.clear();
mUsedVertexSamplerRange = 0;
mUsedPixelSamplerRange = 0;
mDirtySamplerMapping = true;
} }
} }
...@@ -39,8 +39,12 @@ class ProgramD3D : public ProgramImpl ...@@ -39,8 +39,12 @@ class ProgramD3D : public ProgramImpl
const std::vector<rx::PixelShaderOutputVariable> &getPixelShaderKey() { return mPixelShaderKey; } const std::vector<rx::PixelShaderOutputVariable> &getPixelShaderKey() { return mPixelShaderKey; }
int getShaderVersion() const { return mShaderVersion; } int getShaderVersion() const { return mShaderVersion; }
GLenum getTransformFeedbackBufferMode() const { return mTransformFeedbackBufferMode; } GLenum getTransformFeedbackBufferMode() const { return mTransformFeedbackBufferMode; }
std::vector<gl::LinkedVarying> &getTransformFeedbackLinkedVaryings() { return mTransformFeedbackLinkedVaryings; }
sh::Attribute *getShaderAttributes() { return mShaderAttributes; } GLint getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const;
GLenum getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const;
GLint getUsedSamplerRange(gl::SamplerType type) const;
void updateSamplerMapping();
bool validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps);
bool usesPointSize() const { return mUsesPointSize; } bool usesPointSize() const { return mUsesPointSize; }
bool usesPointSpriteEmulation() const; bool usesPointSpriteEmulation() const;
...@@ -65,17 +69,46 @@ class ProgramD3D : public ProgramImpl ...@@ -65,17 +69,46 @@ class ProgramD3D : public ProgramImpl
void getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const; void getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const;
void initializeUniformStorage(const std::vector<gl::LinkedUniform*> &uniforms); void initializeUniformStorage();
gl::Error applyUniforms(const std::vector<gl::LinkedUniform*> &uniforms); gl::Error applyUniforms();
gl::Error applyUniformBuffers(const std::vector<gl::UniformBlock*> uniformBlocks, const std::vector<gl::Buffer*> boundBuffers, gl::Error applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps);
const gl::Caps &caps);
bool assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader, bool assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
unsigned int registerIndex, const gl::Caps &caps); unsigned int registerIndex, const gl::Caps &caps);
unsigned int getReservedUniformVectors(GLenum shader); void dirtyAllUniforms();
void setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
void setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
void setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
void setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
void setUniform1iv(GLint location, GLsizei count, const GLint *v);
void setUniform2iv(GLint location, GLsizei count, const GLint *v);
void setUniform3iv(GLint location, GLsizei count, const GLint *v);
void setUniform4iv(GLint location, GLsizei count, const GLint *v);
void setUniform1uiv(GLint location, GLsizei count, const GLuint *v);
void setUniform2uiv(GLint location, GLsizei count, const GLuint *v);
void setUniform3uiv(GLint location, GLsizei count, const GLuint *v);
void setUniform4uiv(GLint location, GLsizei count, const GLuint *v);
void setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
void setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
void setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
void setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
void setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
void setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
void setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
void setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
void setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
void getUniformfv(GLint location, GLfloat *params);
void getUniformiv(GLint location, GLint *params);
void getUniformuiv(GLint location, GLuint *params);
const UniformStorage &getVertexUniformStorage() const { return *mVertexUniformStorage; } const UniformStorage &getVertexUniformStorage() const { return *mVertexUniformStorage; }
const UniformStorage &getFragmentUniformStorage() const { return *mFragmentUniformStorage; } const UniformStorage &getFragmentUniformStorage() const { return *mFragmentUniformStorage; }
bool linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
const gl::Caps &caps);
bool defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock, const gl::Caps &caps);
void reset(); void reset();
private: private:
...@@ -117,6 +150,37 @@ class ProgramD3D : public ProgramImpl ...@@ -117,6 +150,37 @@ class ProgramD3D : public ProgramImpl
rx::ShaderExecutable *mShaderExecutable; rx::ShaderExecutable *mShaderExecutable;
}; };
struct Sampler
{
Sampler();
bool active;
GLint logicalTextureUnit;
GLenum textureType;
};
void defineUniformBase(GLenum shader, const sh::Uniform &uniform, unsigned int uniformRegister);
void defineUniform(GLenum shader, const sh::ShaderVariable &uniform, const std::string &fullName,
sh::HLSLBlockEncoder *encoder);
bool indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps);
bool indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps);
static bool assignSamplers(unsigned int startSamplerIndex, GLenum samplerType, unsigned int samplerCount,
std::vector<Sampler> &outSamplers, GLuint *outUsedRange);
template <typename T>
void setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType);
template <int cols, int rows>
void setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType);
template <typename T>
void getUniformv(GLint location, T *params, GLenum uniformType);
template <typename VarT>
void defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
bool inRowMajorLayout);
Renderer *mRenderer; Renderer *mRenderer;
DynamicHLSL *mDynamicHLSL; DynamicHLSL *mDynamicHLSL;
...@@ -138,9 +202,12 @@ class ProgramD3D : public ProgramImpl ...@@ -138,9 +202,12 @@ class ProgramD3D : public ProgramImpl
UniformStorage *mFragmentUniformStorage; UniformStorage *mFragmentUniformStorage;
GLenum mTransformFeedbackBufferMode; GLenum mTransformFeedbackBufferMode;
std::vector<gl::LinkedVarying> mTransformFeedbackLinkedVaryings;
sh::Attribute mShaderAttributes[gl::MAX_VERTEX_ATTRIBS]; std::vector<Sampler> mSamplersPS;
std::vector<Sampler> mSamplersVS;
GLuint mUsedVertexSamplerRange;
GLuint mUsedPixelSamplerRange;
bool mDirtySamplerMapping;
int mShaderVersion; int mShaderVersion;
}; };
......
...@@ -1467,7 +1467,7 @@ gl::Error Renderer11::applyShaders(gl::ProgramBinary *programBinary, const gl::V ...@@ -1467,7 +1467,7 @@ gl::Error Renderer11::applyShaders(gl::ProgramBinary *programBinary, const gl::V
if (dirtyUniforms) if (dirtyUniforms)
{ {
programBinary->dirtyAllUniforms(); programD3D->dirtyAllUniforms();
} }
return gl::Error(GL_NO_ERROR); return gl::Error(GL_NO_ERROR);
......
...@@ -1758,7 +1758,7 @@ gl::Error Renderer9::applyShaders(gl::ProgramBinary *programBinary, const gl::Ve ...@@ -1758,7 +1758,7 @@ gl::Error Renderer9::applyShaders(gl::ProgramBinary *programBinary, const gl::Ve
unsigned int programSerial = programBinary->getSerial(); unsigned int programSerial = programBinary->getSerial();
if (programSerial != mAppliedProgramSerial) if (programSerial != mAppliedProgramSerial)
{ {
programBinary->dirtyAllUniforms(); programD3D->dirtyAllUniforms();
mDxUniformsDirty = true; mDxUniformsDirty = true;
mAppliedProgramSerial = programSerial; mAppliedProgramSerial = programSerial;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment