D3D: Run applyTextures before applyShaders

applyShaders will update uniform data by uploading the constant buffer.
In the future uniform data will contain the base level of textures so
that shaders can use it to adjust results of texture functions. The most
natural place to set the base level in uniform data is applyTextures, so
it needs to be run before applyShaders.

updateSamplerMapping needs to be called before applyTextures, but
genericDraw* functions already do that. Remove redundant
updateSamplerMapping call from ProgramD3D::applyUniforms.

BUG=angleproject:596
BUG=angleproject:1092
BUG=angleproject:1261
TEST=angle_end2end_tests

Change-Id: I1828f7bdc75999872a26941a02cb4b6c6a66094d
Reviewed-on: https://chromium-review.googlesource.com/322121
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Tryjob-Request: Olli Etuaho <oetuaho@nvidia.com>
Tested-by: Olli Etuaho <oetuaho@nvidia.com>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libANGLE/renderer/d3d/ProgramD3D.cpp b/src/libANGLE/renderer/d3d/ProgramD3D.cpp
index 7f44d58..aff39b0 100644
--- a/src/libANGLE/renderer/d3d/ProgramD3D.cpp
+++ b/src/libANGLE/renderer/d3d/ProgramD3D.cpp
@@ -1553,7 +1553,7 @@
 
 gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
 {
-    updateSamplerMapping();
+    ASSERT(!mDirtySamplerMapping);
 
     gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
     if (error.isError())
diff --git a/src/libANGLE/renderer/d3d/ProgramD3D.h b/src/libANGLE/renderer/d3d/ProgramD3D.h
index 5ea15aa..240cea1 100644
--- a/src/libANGLE/renderer/d3d/ProgramD3D.h
+++ b/src/libANGLE/renderer/d3d/ProgramD3D.h
@@ -245,6 +245,8 @@
     void updateCachedInputLayout(const gl::State &state);
     const gl::InputLayout &getCachedInputLayout() const { return mCachedInputLayout; }
 
+    bool isSamplerMappingDirty() { return mDirtySamplerMapping; }
+
   private:
     class VertexExecutable
     {
diff --git a/src/libANGLE/renderer/d3d/RendererD3D.cpp b/src/libANGLE/renderer/d3d/RendererD3D.cpp
index 575ecd6..135d749 100644
--- a/src/libANGLE/renderer/d3d/RendererD3D.cpp
+++ b/src/libANGLE/renderer/d3d/RendererD3D.cpp
@@ -183,13 +183,13 @@
         return error;
     }
 
-    error = applyShaders(data, mode);
+    error = applyTextures(data);
     if (error.isError())
     {
         return error;
     }
 
-    error = applyTextures(data);
+    error = applyShaders(data, mode);
     if (error.isError())
     {
         return error;
@@ -251,13 +251,13 @@
         return error;
     }
 
-    error = applyShaders(data, mode);
+    error = applyTextures(data);
     if (error.isError())
     {
         return error;
     }
 
-    error = applyTextures(data);
+    error = applyShaders(data, mode);
     if (error.isError())
     {
         return error;
@@ -386,11 +386,14 @@
 // For each Direct3D sampler of either the pixel or vertex stage,
 // looks up the corresponding OpenGL texture image unit and texture type,
 // and sets the texture and its addressing/filtering state (or NULL when inactive).
+// Sampler mapping needs to be up-to-date on the program object before this is called.
 gl::Error RendererD3D::applyTextures(const gl::Data &data, gl::SamplerType shaderType,
                                      const FramebufferTextureArray &framebufferTextures, size_t framebufferTextureCount)
 {
     ProgramD3D *programD3D = GetImplAs<ProgramD3D>(data.state->getProgram());
 
+    ASSERT(!programD3D->isSamplerMappingDirty());
+
     unsigned int samplerRange = programD3D->getUsedSamplerRange(shaderType);
     for (unsigned int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
     {