Combine dx_Depth and dx_FrontCCW into dx_DepthFront.

TRAC #22245
Signed-off-by: Daniel Koch
Signed-off-by: Geoff Lang
Author: Nicolas Capens

git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1582 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/Context.cpp b/src/libGLESv2/Context.cpp
index b500083..017e8b0 100644
--- a/src/libGLESv2/Context.cpp
+++ b/src/libGLESv2/Context.cpp
@@ -1701,8 +1701,8 @@
 }
 
 // Applies the render target surface, depth stencil surface, viewport rectangle and
-// scissor rectangle to the Direct3D 9 device
-bool Context::applyRenderTarget(bool ignoreViewport)
+// scissor rectangle to the renderer
+bool Context::applyRenderTarget(GLenum drawMode, bool ignoreViewport)
 {
     Framebuffer *framebufferObject = getDrawFramebuffer();
 
@@ -1714,8 +1714,8 @@
     mRenderer->applyRenderTarget(framebufferObject);
 
     ProgramBinary *programBinary = mState.currentProgram ? getCurrentProgramBinary() : NULL;
-    if (!mRenderer->setViewport(mState.viewport, mState.zNear, mState.zFar, ignoreViewport,
-                                programBinary, mDxUniformsDirty))
+    if (!mRenderer->setViewport(mState.viewport, mState.zNear, mState.zFar, drawMode, mState.rasterizer.frontFace,
+                                ignoreViewport, programBinary, mDxUniformsDirty))
     {
         return false;
     }
@@ -1729,14 +1729,6 @@
 // Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
 void Context::applyState(GLenum drawMode)
 {
-    ProgramBinary *programBinary = getCurrentProgramBinary();
-
-    Framebuffer *framebufferObject = getDrawFramebuffer();
-
-    GLint frontCCW = programBinary->getDxFrontCCWLocation();
-    GLfloat ccw = !IsTriangleMode(drawMode) ? 0.0f : (mState.rasterizer.frontFace == GL_CCW ? 1.0f : -1.0f);
-    programBinary->setUniform1fv(frontCCW, 1, &ccw);
-
     mRenderer->setRasterizerState(mState.rasterizer);
 
     unsigned int mask = 0;
@@ -1744,6 +1736,7 @@
     {
         if (mState.sampleCoverageValue != 0)
         {
+            Framebuffer *framebufferObject = getDrawFramebuffer();
             float threshold = 0.5f;
 
             for (int i = 0; i < framebufferObject->getSamples(); ++i)
@@ -1946,7 +1939,7 @@
         return error(GL_INVALID_VALUE);
     }
 
-    if (!applyRenderTarget(true))   // Clips the clear to the scissor rectangle but not the viewport
+    if (!applyRenderTarget(GL_TRIANGLES, true))   // Clips the clear to the scissor rectangle but not the viewport
     {
         return;
     }
@@ -1977,7 +1970,7 @@
         return;
     }
 
-    if (!applyRenderTarget(false))
+    if (!applyRenderTarget(mode, false))
     {
         return;
     }
@@ -2023,7 +2016,7 @@
         return;
     }
 
-    if (!applyRenderTarget(false))
+    if (!applyRenderTarget(mode, false))
     {
         return;
     }
diff --git a/src/libGLESv2/Context.h b/src/libGLESv2/Context.h
index 0ceb173..47eb995 100644
--- a/src/libGLESv2/Context.h
+++ b/src/libGLESv2/Context.h
@@ -69,8 +69,8 @@
     MAX_TEXTURE_IMAGE_UNITS = 16,
     MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF = 4,   // For devices supporting vertex texture fetch
     MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF = MAX_TEXTURE_IMAGE_UNITS + MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF,    
-    MAX_FRAGMENT_UNIFORM_VECTORS_SM2 = 32 - 4,    // Reserve space for dx_Coord, dx_Depth, dx_DepthRange and dx_FrontCCW.
-    MAX_FRAGMENT_UNIFORM_VECTORS_SM3 = 224 - 4,
+    MAX_FRAGMENT_UNIFORM_VECTORS_SM2 = 32 - 3,    // Reserve space for dx_Coord, dx_DepthFront and dx_DepthRange.
+    MAX_FRAGMENT_UNIFORM_VECTORS_SM3 = 224 - 3,
     MAX_DRAW_BUFFERS = 1
 };
 
@@ -423,7 +423,7 @@
   private:
     DISALLOW_COPY_AND_ASSIGN(Context);
 
-    bool applyRenderTarget(bool ignoreViewport);
+    bool applyRenderTarget(GLenum drawMode, bool ignoreViewport);
     void applyState(GLenum drawMode);
     void applyShaders();
     void applyTextures();
diff --git a/src/libGLESv2/ProgramBinary.cpp b/src/libGLESv2/ProgramBinary.cpp
index fe03997..1a3695b 100644
--- a/src/libGLESv2/ProgramBinary.cpp
+++ b/src/libGLESv2/ProgramBinary.cpp
@@ -83,10 +83,9 @@
     mUsedPixelSamplerRange = 0;
 
     mDxDepthRangeLocation = -1;
-    mDxDepthLocation = -1;
+    mDxDepthFrontLocation = -1;
     mDxCoordLocation = -1;
     mDxHalfPixelSizeLocation = -1;
-    mDxFrontCCWLocation = -1;
 }
 
 ProgramBinary::~ProgramBinary()
@@ -1462,7 +1461,7 @@
                           "    gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
         }
         
-        pixelHLSL += "    gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_Depth.x + dx_Depth.y;\n"
+        pixelHLSL += "    gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
                       "    gl_FragCoord.w = rhw;\n";
     }
 
@@ -1474,7 +1473,7 @@
 
     if (fragmentShader->mUsesFrontFacing)
     {
-        pixelHLSL += "    gl_FrontFacing = (input.vFace * dx_FrontCCW >= 0.0);\n";
+        pixelHLSL += "    gl_FrontFacing = (input.vFace * dx_DepthFront.z >= 0.0);\n";
     }
 
     for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
@@ -1625,10 +1624,9 @@
     }
 
     stream.read(&mDxDepthRangeLocation);
-    stream.read(&mDxDepthLocation);
+    stream.read(&mDxDepthFrontLocation);
     stream.read(&mDxCoordLocation);
     stream.read(&mDxHalfPixelSizeLocation);
-    stream.read(&mDxFrontCCWLocation);
 
     unsigned int pixelShaderSize;
     stream.read(&pixelShaderSize);
@@ -1733,10 +1731,9 @@
     }
 
     stream.write(mDxDepthRangeLocation);
-    stream.write(mDxDepthLocation);
+    stream.write(mDxDepthFrontLocation);
     stream.write(mDxCoordLocation);
     stream.write(mDxHalfPixelSizeLocation);
-    stream.write(mDxFrontCCWLocation);
 
     UINT pixelShaderSize = mPixelExecutable->getLength();
     stream.write(pixelShaderSize);
@@ -1859,10 +1856,9 @@
     // these uniforms are searched as already-decorated because gl_ and dx_
     // are reserved prefixes, and do not receive additional decoration
     mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
-    mDxDepthLocation = getUniformLocation("dx_Depth");
+    mDxDepthFrontLocation = getUniformLocation("dx_DepthFront");
     mDxCoordLocation = getUniformLocation("dx_Coord");
     mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
-    mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
 
     Context *context = getContext();
     context->markDxUniformsDirty();
@@ -2655,9 +2651,9 @@
     return mDxDepthRangeLocation;
 }
 
-GLint ProgramBinary::getDxDepthLocation() const
+GLint ProgramBinary::getDxDepthFrontLocation() const
 {
-    return mDxDepthLocation;
+    return mDxDepthFrontLocation;
 }
 
 GLint ProgramBinary::getDxCoordLocation() const
@@ -2670,11 +2666,6 @@
     return mDxHalfPixelSizeLocation;
 }
 
-GLint ProgramBinary::getDxFrontCCWLocation() const
-{
-    return mDxFrontCCWLocation;
-}
-
 ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
 {
 }
diff --git a/src/libGLESv2/ProgramBinary.h b/src/libGLESv2/ProgramBinary.h
index 350e52f..cc9c698 100644
--- a/src/libGLESv2/ProgramBinary.h
+++ b/src/libGLESv2/ProgramBinary.h
@@ -130,10 +130,9 @@
     bool getUniformiv(GLint location, GLsizei *bufSize, GLint *params);
 
     GLint getDxDepthRangeLocation() const;
-    GLint getDxDepthLocation() const;
+    GLint getDxDepthFrontLocation() const;
     GLint getDxCoordLocation() const;
     GLint getDxHalfPixelSizeLocation() const;
-    GLint getDxFrontCCWLocation() const;
 
     void dirtyAllUniforms();
     void applyUniforms();
@@ -212,10 +211,9 @@
     UniformIndex mUniformIndex;
 
     GLint mDxDepthRangeLocation;
-    GLint mDxDepthLocation;
+    GLint mDxDepthFrontLocation;
     GLint mDxCoordLocation;
     GLint mDxHalfPixelSizeLocation;
-    GLint mDxFrontCCWLocation;
 
     bool mValidated;
 
diff --git a/src/libGLESv2/renderer/Renderer.h b/src/libGLESv2/renderer/Renderer.h
index 0ecc83c..12f741f 100644
--- a/src/libGLESv2/renderer/Renderer.h
+++ b/src/libGLESv2/renderer/Renderer.h
@@ -90,8 +90,8 @@
                                       int stencilBackRef, bool frontFaceCCW) = 0;
 
     virtual void setScissorRectangle(const gl::Rectangle &scissor, bool enabled) = 0;
-    virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, bool ignoreViewport,
-                             gl::ProgramBinary *currentProgram, bool forceSetUniforms) = 0;
+    virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
+                             bool ignoreViewport, gl::ProgramBinary *currentProgram, bool forceSetUniforms) = 0;
 
     virtual bool applyRenderTarget(gl::Framebuffer *frameBuffer) = 0;
     virtual void applyShaders(gl::ProgramBinary *programBinary) = 0;
diff --git a/src/libGLESv2/renderer/Renderer11.cpp b/src/libGLESv2/renderer/Renderer11.cpp
index 8c3cae6..088f654 100644
--- a/src/libGLESv2/renderer/Renderer11.cpp
+++ b/src/libGLESv2/renderer/Renderer11.cpp
@@ -384,8 +384,8 @@
     mForceSetScissor = false;
 }
 
-bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, bool ignoreViewport,
-                             gl::ProgramBinary *currentProgram, bool forceSetUniforms)
+bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace, 
+                             bool ignoreViewport, gl::ProgramBinary *currentProgram, bool forceSetUniforms)
 {
     gl::Rectangle actualViewport = viewport;
     float actualZNear = gl::clamp01(zNear);
@@ -439,9 +439,10 @@
                             actualViewport.y + (actualViewport.height * 0.5f) };
         currentProgram->setUniform4fv(coord, 1, whxy);
 
-        GLint depth = currentProgram->getDxDepthLocation();
-        GLfloat dz[2] = { (actualZFar - actualZNear) * 0.5f, (actualZNear + actualZFar) * 0.5f };
-        currentProgram->setUniform2fv(depth, 1, dz);
+        GLint depthFront = currentProgram->getDxDepthFrontLocation();
+        GLfloat ccw = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);
+        GLfloat dz[3] = { (actualZFar - actualZNear) * 0.5f, (actualZNear + actualZFar) * 0.5f, ccw };
+        currentProgram->setUniform3fv(depthFront, 1, dz);
 
         GLint depthRange = currentProgram->getDxDepthRangeLocation();
         GLfloat nearFarDiff[3] = { actualZNear, actualZFar, actualZFar - actualZNear };
diff --git a/src/libGLESv2/renderer/Renderer11.h b/src/libGLESv2/renderer/Renderer11.h
index 4d082cc..b437806 100644
--- a/src/libGLESv2/renderer/Renderer11.h
+++ b/src/libGLESv2/renderer/Renderer11.h
@@ -56,8 +56,8 @@
                                       int stencilBackRef, bool frontFaceCCW);
 
     virtual void setScissorRectangle(const gl::Rectangle &scissor, bool enabled);
-    virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, bool ignoreViewport,
-                             gl::ProgramBinary *currentProgram, bool forceSetUniforms);
+    virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
+                             bool ignoreViewport, gl::ProgramBinary *currentProgram, bool forceSetUniforms);
 
     virtual bool applyPrimitiveType(GLenum mode, GLsizei count);
     virtual bool applyRenderTarget(gl::Framebuffer *frameBuffer);
diff --git a/src/libGLESv2/renderer/Renderer9.cpp b/src/libGLESv2/renderer/Renderer9.cpp
index b2e2b70..141d376 100644
--- a/src/libGLESv2/renderer/Renderer9.cpp
+++ b/src/libGLESv2/renderer/Renderer9.cpp
@@ -892,8 +892,8 @@
     mForceSetScissor = false;
 }
 
-bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, bool ignoreViewport,
-                            gl::ProgramBinary *currentProgram, bool forceSetUniforms)
+bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
+                            bool ignoreViewport, gl::ProgramBinary *currentProgram, bool forceSetUniforms)
 {
     gl::Rectangle actualViewport = viewport;
     float actualZNear = gl::clamp01(zNear);
@@ -946,9 +946,10 @@
                             actualViewport.y + (actualViewport.height * 0.5f) };
         currentProgram->setUniform4fv(coord, 1, whxy);
 
-        GLint depth = currentProgram->getDxDepthLocation();
-        GLfloat dz[2] = { (actualZFar - actualZNear) * 0.5f, (actualZNear + actualZFar) * 0.5f };
-        currentProgram->setUniform2fv(depth, 1, dz);
+        GLint depthFront = currentProgram->getDxDepthFrontLocation();
+        GLfloat ccw = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);
+        GLfloat dz[3] = { (actualZFar - actualZNear) * 0.5f, (actualZNear + actualZFar) * 0.5f, ccw };
+        currentProgram->setUniform3fv(depthFront, 1, dz);
 
         GLint depthRange = currentProgram->getDxDepthRangeLocation();
         GLfloat nearFarDiff[3] = { actualZNear, actualZFar, actualZFar - actualZNear };
diff --git a/src/libGLESv2/renderer/Renderer9.h b/src/libGLESv2/renderer/Renderer9.h
index d5ae773..008383a 100644
--- a/src/libGLESv2/renderer/Renderer9.h
+++ b/src/libGLESv2/renderer/Renderer9.h
@@ -85,8 +85,8 @@
                                       int stencilBackRef, bool frontFaceCCW);
 
     virtual void setScissorRectangle(const gl::Rectangle &scissor, bool enabled);
-    virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, bool ignoreViewport,
-                             gl::ProgramBinary *currentProgram, bool forceSetUniforms);
+    virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
+                             bool ignoreViewport, gl::ProgramBinary *currentProgram, bool forceSetUniforms);
 
     virtual bool applyRenderTarget(gl::Framebuffer *frameBuffer);
     virtual void applyShaders(gl::ProgramBinary *programBinary);