Move storage for uniform blocks to the program binary.

With dynamic shaders we may have multiple shader executables per
program binary. We must store the uniforms outside the executable,
in the program binary, to be consistent between variations.

Change-Id: I1b29a5d78c72dede8562d4878569b609536ba074
Reviewed-on: https://chromium-review.googlesource.com/183586
Reviewed-by: Shannon Woods <shannonwoods@chromium.org>
Tested-by: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libGLESv2/renderer/d3d11/Renderer11.cpp b/src/libGLESv2/renderer/d3d11/Renderer11.cpp
index 2060de7..e26fced 100644
--- a/src/libGLESv2/renderer/d3d11/Renderer11.cpp
+++ b/src/libGLESv2/renderer/d3d11/Renderer11.cpp
@@ -1448,10 +1448,9 @@
     }
 }
 
-void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
+void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
 {
-    ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
-    ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
+    const gl::UniformArray &uniformArray = programBinary.getUniforms();
 
     unsigned int totalRegisterCountVS = 0;
     unsigned int totalRegisterCountPS = 0;
@@ -1459,25 +1458,30 @@
     bool vertexUniformsDirty = false;
     bool pixelUniformsDirty = false;
 
-    for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
+    for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
     {
-        const gl::Uniform *uniform = *uniform_iterator;
+        const gl::Uniform &uniform = *uniformArray[uniformIndex];
 
-        if (uniform->isReferencedByVertexShader())
+        if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
         {
-            totalRegisterCountVS += uniform->registerCount;
-            vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
+            totalRegisterCountVS += uniform.registerCount;
+            vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
         }
 
-        if (uniform->isReferencedByFragmentShader())
+        if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
         {
-            totalRegisterCountPS += uniform->registerCount;
-            pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
+            totalRegisterCountPS += uniform.registerCount;
+            pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
         }
     }
 
-    ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(this, totalRegisterCountVS);
-    ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(this, totalRegisterCountPS);
+    const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
+    const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
+    ASSERT(vertexUniformStorage);
+    ASSERT(fragmentUniformStorage);
+
+    ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
+    ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
 
     float (*mapVS)[4] = NULL;
     float (*mapPS)[4] = NULL;
@@ -1498,11 +1502,11 @@
         mapPS = (float(*)[4])map.pData;
     }
 
-    for (size_t uniformIndex = 0; uniformIndex < uniformArray->size(); uniformIndex++)
+    for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
     {
-        gl::Uniform *uniform = (*uniformArray)[uniformIndex];
+        gl::Uniform *uniform = uniformArray[uniformIndex];
 
-        if (!gl::IsSampler(uniform->type))
+        if (!uniform->isSampler())
         {
             unsigned int componentCount = (4 - uniform->registerElement);
 
@@ -1519,8 +1523,6 @@
                 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
             }
         }
-
-        uniform->dirty = false;
     }
 
     if (mapVS)
@@ -2841,6 +2843,11 @@
     return executable;
 }
 
+rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
+{
+    return new UniformStorage11(this, storageSize);
+}
+
 VertexBuffer *Renderer11::createVertexBuffer()
 {
     return new VertexBuffer11(this);
diff --git a/src/libGLESv2/renderer/d3d11/Renderer11.h b/src/libGLESv2/renderer/d3d11/Renderer11.h
index ca0eb02..dd51eda 100644
--- a/src/libGLESv2/renderer/d3d11/Renderer11.h
+++ b/src/libGLESv2/renderer/d3d11/Renderer11.h
@@ -76,7 +76,7 @@
     virtual bool applyPrimitiveType(GLenum mode, GLsizei count);
     virtual bool applyRenderTarget(gl::Framebuffer *frameBuffer);
     virtual void applyShaders(gl::ProgramBinary *programBinary);
-    virtual void applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray);
+    virtual void applyUniforms(const gl::ProgramBinary &programBinary);
     virtual GLenum applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
                                      GLint first, GLsizei count, GLsizei instances);
     virtual GLenum applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
@@ -185,6 +185,7 @@
     // Shader operations
     virtual ShaderExecutable *loadExecutable(const void *function, size_t length, rx::ShaderType type);
     virtual ShaderExecutable *compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround);
+    virtual UniformStorage *createUniformStorage(size_t storageSize);
 
     // Image operations
     virtual Image *createImage();
diff --git a/src/libGLESv2/renderer/d3d11/ShaderExecutable11.cpp b/src/libGLESv2/renderer/d3d11/ShaderExecutable11.cpp
index 0e69905..4e326a5 100644
--- a/src/libGLESv2/renderer/d3d11/ShaderExecutable11.cpp
+++ b/src/libGLESv2/renderer/d3d11/ShaderExecutable11.cpp
@@ -21,7 +21,6 @@
     mPixelExecutable = executable;
     mVertexExecutable = NULL;
     mGeometryExecutable = NULL;
-    mUniformStorage = NULL;
 }
 
 ShaderExecutable11::ShaderExecutable11(const void *function, size_t length, ID3D11VertexShader *executable)
@@ -30,7 +29,6 @@
     mVertexExecutable = executable;
     mPixelExecutable = NULL;
     mGeometryExecutable = NULL;
-    mUniformStorage = NULL;
 }
 
 ShaderExecutable11::ShaderExecutable11(const void *function, size_t length, ID3D11GeometryShader *executable)
@@ -39,7 +37,6 @@
     mGeometryExecutable = executable;
     mVertexExecutable = NULL;
     mPixelExecutable = NULL;
-    mUniformStorage = NULL;
 }
 
 ShaderExecutable11::~ShaderExecutable11()
@@ -47,7 +44,6 @@
     SafeRelease(mVertexExecutable);
     SafeRelease(mPixelExecutable);
     SafeRelease(mGeometryExecutable);
-    SafeDelete(mUniformStorage);
 }
 
 ShaderExecutable11 *ShaderExecutable11::makeShaderExecutable11(ShaderExecutable *executable)
@@ -71,19 +67,6 @@
     return mGeometryExecutable;
 }
 
-ID3D11Buffer *ShaderExecutable11::getConstantBuffer(Renderer11 *renderer, unsigned int registerCount)
-{
-    size_t desiredSize = registerCount * 16u;
-
-    if (!mUniformStorage)
-    {
-        mUniformStorage = new UniformStorage11(renderer, desiredSize);
-    }
-
-    ASSERT(mUniformStorage->size() == desiredSize);
-    return mUniformStorage->getConstantBuffer();
-}
-
 UniformStorage11::UniformStorage11(Renderer11 *renderer, size_t initialSize)
     : UniformStorage(initialSize),
       mConstantBuffer(NULL)
diff --git a/src/libGLESv2/renderer/d3d11/ShaderExecutable11.h b/src/libGLESv2/renderer/d3d11/ShaderExecutable11.h
index f0158bc..86327ac 100644
--- a/src/libGLESv2/renderer/d3d11/ShaderExecutable11.h
+++ b/src/libGLESv2/renderer/d3d11/ShaderExecutable11.h
@@ -32,16 +32,12 @@
     ID3D11VertexShader *getVertexShader() const;
     ID3D11GeometryShader *getGeometryShader() const;
 
-    ID3D11Buffer *getConstantBuffer(Renderer11 *renderer, unsigned int registerCount);
-
   private:
     DISALLOW_COPY_AND_ASSIGN(ShaderExecutable11);
 
     ID3D11PixelShader *mPixelExecutable;
     ID3D11VertexShader *mVertexExecutable;
     ID3D11GeometryShader *mGeometryExecutable;
-
-    UniformStorage11 *mUniformStorage;
 };
 
 class UniformStorage11 : public UniformStorage