Reuse the constant buffers of the shader executable.

TRAC #22327 Signed-off-by: Daniel Koch Signed-off-by: Geoff Lang Author: Nicolas Capens git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1767 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 358e88d7
...@@ -1045,37 +1045,32 @@ void Renderer11::applyShaders(gl::ProgramBinary *programBinary) ...@@ -1045,37 +1045,32 @@ void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::UniformArray *uniformArray) void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::UniformArray *uniformArray)
{ {
D3D11_BUFFER_DESC constantBufferDescriptionVS = {0}; ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
constantBufferDescriptionVS.ByteWidth = D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * sizeof(float[4]); ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
constantBufferDescriptionVS.Usage = D3D11_USAGE_DYNAMIC;
constantBufferDescriptionVS.BindFlags = D3D11_BIND_CONSTANT_BUFFER; unsigned int totalRegisterCountVS = 0;
constantBufferDescriptionVS.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; unsigned int totalRegisterCountPS = 0;
constantBufferDescriptionVS.MiscFlags = 0;
constantBufferDescriptionVS.StructureByteStride = 0; for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
{
ID3D11Buffer *constantBufferVS = NULL; const gl::Uniform *uniform = *uniform_iterator;
HRESULT result = mDevice->CreateBuffer(&constantBufferDescriptionVS, NULL, &constantBufferVS);
ASSERT(SUCCEEDED(result)); if (uniform->vsRegisterIndex >= 0)
{
D3D11_BUFFER_DESC constantBufferDescriptionPS = {0}; totalRegisterCountVS += uniform->registerCount;
constantBufferDescriptionPS.ByteWidth = D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * sizeof(float[4]); }
constantBufferDescriptionPS.Usage = D3D11_USAGE_DYNAMIC;
constantBufferDescriptionPS.BindFlags = D3D11_BIND_CONSTANT_BUFFER; if (uniform->psRegisterIndex >= 0)
constantBufferDescriptionPS.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; {
constantBufferDescriptionPS.MiscFlags = 0; totalRegisterCountPS += uniform->registerCount;
constantBufferDescriptionPS.StructureByteStride = 0; }
}
ID3D11Buffer *constantBufferPS = NULL;
result = mDevice->CreateBuffer(&constantBufferDescriptionPS, NULL, &constantBufferPS); ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
ASSERT(SUCCEEDED(result)); ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
D3D11_MAPPED_SUBRESOURCE mapVS = {0}; void *mapVS = new float[4 * totalRegisterCountVS];
result = mDeviceContext->Map(constantBufferVS, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapVS); void *mapPS = new float[4 * totalRegisterCountPS];
ASSERT(SUCCEEDED(result));
D3D11_MAPPED_SUBRESOURCE mapPS = {0};
result = mDeviceContext->Map(constantBufferPS, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapPS);
ASSERT(SUCCEEDED(result));
for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++) for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
{ {
...@@ -1095,7 +1090,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo ...@@ -1095,7 +1090,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo
case GL_FLOAT_MAT4: case GL_FLOAT_MAT4:
if (uniform->vsRegisterIndex >= 0) if (uniform->vsRegisterIndex >= 0)
{ {
GLfloat (*c)[4] = (GLfloat(*)[4])mapVS.pData; GLfloat (*c)[4] = (GLfloat(*)[4])mapVS;
float (*f)[4] = (float(*)[4])uniform->data; float (*f)[4] = (float(*)[4])uniform->data;
for (unsigned int i = 0; i < uniform->registerCount; i++) for (unsigned int i = 0; i < uniform->registerCount; i++)
...@@ -1108,7 +1103,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo ...@@ -1108,7 +1103,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo
} }
if (uniform->psRegisterIndex >= 0) if (uniform->psRegisterIndex >= 0)
{ {
GLfloat (*c)[4] = (GLfloat(*)[4])mapPS.pData; GLfloat (*c)[4] = (GLfloat(*)[4])mapPS;
float (*f)[4] = (float(*)[4])uniform->data; float (*f)[4] = (float(*)[4])uniform->data;
for (unsigned int i = 0; i < uniform->registerCount; i++) for (unsigned int i = 0; i < uniform->registerCount; i++)
...@@ -1126,7 +1121,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo ...@@ -1126,7 +1121,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo
case GL_INT_VEC4: case GL_INT_VEC4:
if (uniform->vsRegisterIndex >= 0) if (uniform->vsRegisterIndex >= 0)
{ {
int (*c)[4] = (int(*)[4])mapVS.pData; int (*c)[4] = (int(*)[4])mapVS;
GLint *x = (GLint*)uniform->data; GLint *x = (GLint*)uniform->data;
int count = gl::VariableColumnCount(uniform->type); int count = gl::VariableColumnCount(uniform->type);
...@@ -1140,7 +1135,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo ...@@ -1140,7 +1135,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo
} }
if (uniform->psRegisterIndex >= 0) if (uniform->psRegisterIndex >= 0)
{ {
int (*c)[4] = (int(*)[4])mapPS.pData; int (*c)[4] = (int(*)[4])mapPS;
GLint *x = (GLint*)uniform->data; GLint *x = (GLint*)uniform->data;
int count = gl::VariableColumnCount(uniform->type); int count = gl::VariableColumnCount(uniform->type);
...@@ -1159,7 +1154,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo ...@@ -1159,7 +1154,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo
case GL_BOOL_VEC4: case GL_BOOL_VEC4:
if (uniform->vsRegisterIndex >= 0) if (uniform->vsRegisterIndex >= 0)
{ {
int (*c)[4] = (int(*)[4])mapVS.pData; int (*c)[4] = (int(*)[4])mapVS;
GLboolean *b = (GLboolean*)uniform->data; GLboolean *b = (GLboolean*)uniform->data;
int count = gl::VariableColumnCount(uniform->type); int count = gl::VariableColumnCount(uniform->type);
...@@ -1173,7 +1168,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo ...@@ -1173,7 +1168,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo
} }
if (uniform->psRegisterIndex >= 0) if (uniform->psRegisterIndex >= 0)
{ {
int (*c)[4] = (int(*)[4])mapPS.pData; int (*c)[4] = (int(*)[4])mapPS;
GLboolean *b = (GLboolean*)uniform->data; GLboolean *b = (GLboolean*)uniform->data;
int count = gl::VariableColumnCount(uniform->type); int count = gl::VariableColumnCount(uniform->type);
...@@ -1191,13 +1186,21 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo ...@@ -1191,13 +1186,21 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo
} }
} }
mDeviceContext->Unmap(constantBufferVS, 0); if (vertexConstantBuffer)
mDeviceContext->VSSetConstantBuffers(0, 1, &constantBufferVS); {
constantBufferVS->Release(); mDeviceContext->UpdateSubresource(vertexConstantBuffer, 0, NULL, mapVS, 0, 0);
}
if (pixelConstantBuffer)
{
mDeviceContext->UpdateSubresource(pixelConstantBuffer, 0, NULL, mapPS, 0, 0);
}
mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
mDeviceContext->Unmap(constantBufferPS, 0); delete[] mapVS;
mDeviceContext->PSSetConstantBuffers(0, 1, &constantBufferPS); delete[] mapPS;
constantBufferPS->Release();
// Driver uniforms // Driver uniforms
if (!mDriverConstantBufferVS) if (!mDriverConstantBufferVS)
...@@ -1210,7 +1213,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo ...@@ -1210,7 +1213,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo
constantBufferDescription.MiscFlags = 0; constantBufferDescription.MiscFlags = 0;
constantBufferDescription.StructureByteStride = 0; constantBufferDescription.StructureByteStride = 0;
result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS); HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
ASSERT(SUCCEEDED(result)); ASSERT(SUCCEEDED(result));
mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS); mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
...@@ -1226,7 +1229,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo ...@@ -1226,7 +1229,7 @@ void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, const gl::Unifo
constantBufferDescription.MiscFlags = 0; constantBufferDescription.MiscFlags = 0;
constantBufferDescription.StructureByteStride = 0; constantBufferDescription.StructureByteStride = 0;
result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS); HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
ASSERT(SUCCEEDED(result)); ASSERT(SUCCEEDED(result));
mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS); mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
......
...@@ -65,8 +65,22 @@ ID3D11PixelShader *ShaderExecutable11::getPixelShader() const ...@@ -65,8 +65,22 @@ ID3D11PixelShader *ShaderExecutable11::getPixelShader() const
return mPixelExecutable; return mPixelExecutable;
} }
ID3D11Buffer *ShaderExecutable11::getConstantBuffer() ID3D11Buffer *ShaderExecutable11::getConstantBuffer(ID3D11Device *device, unsigned int registerCount)
{ {
if (!mConstantBuffer && registerCount > 0)
{
D3D11_BUFFER_DESC constantBufferDescription = {0};
constantBufferDescription.ByteWidth = registerCount * sizeof(float[4]);
constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constantBufferDescription.CPUAccessFlags = 0;
constantBufferDescription.MiscFlags = 0;
constantBufferDescription.StructureByteStride = 0;
HRESULT result = device->CreateBuffer(&constantBufferDescription, NULL, &mConstantBuffer);
ASSERT(SUCCEEDED(result));
}
return mConstantBuffer; return mConstantBuffer;
} }
......
...@@ -30,7 +30,7 @@ class ShaderExecutable11 : public ShaderExecutable ...@@ -30,7 +30,7 @@ class ShaderExecutable11 : public ShaderExecutable
ID3D11PixelShader *getPixelShader() const; ID3D11PixelShader *getPixelShader() const;
ID3D11VertexShader *getVertexShader() const; ID3D11VertexShader *getVertexShader() const;
ID3D11Buffer *getConstantBuffer(); ID3D11Buffer *getConstantBuffer(ID3D11Device *device, unsigned int registerCount);
private: private:
DISALLOW_COPY_AND_ASSIGN(ShaderExecutable11); DISALLOW_COPY_AND_ASSIGN(ShaderExecutable11);
......
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