Unify the compare function for sampler and depth stencil

Both sampler and depth stencil include compare function, and we
need to change it to appropriate functions for different backends.
In order to avoid code duplication, we put it into a util file
for every backend, then include that file and call the function
from where we need it.

We can put similar functions which can be shared among different
objects into the util file in future.

Shaobo's patch has already created such a util function for compare
function for sampler. This patch removes the duplicated compare
function in RenderPipeline for depth stencil descriptor, and call
the function in util file instead.

BUG=dawn:31, dawn:47

Change-Id: Ia645b48e026b5a372d1023aa7e8ecdf1e4ed7c6d
Reviewed-on: https://dawn-review.googlesource.com/c/3641
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/dawn_native/d3d12/RenderPipelineD3D12.cpp b/src/dawn_native/d3d12/RenderPipelineD3D12.cpp
index ffd103d..53a18b3 100644
--- a/src/dawn_native/d3d12/RenderPipelineD3D12.cpp
+++ b/src/dawn_native/d3d12/RenderPipelineD3D12.cpp
@@ -21,6 +21,7 @@
 #include "dawn_native/d3d12/PlatformFunctions.h"
 #include "dawn_native/d3d12/ShaderModuleD3D12.h"
 #include "dawn_native/d3d12/TextureD3D12.h"
+#include "dawn_native/d3d12/UtilsD3D12.h"
 
 #include <d3dcompiler.h>
 
@@ -165,36 +166,13 @@
             }
         }
 
-        D3D12_COMPARISON_FUNC ComparisonFunc(dawn::CompareFunction func) {
-            switch (func) {
-                case dawn::CompareFunction::Always:
-                    return D3D12_COMPARISON_FUNC_ALWAYS;
-                case dawn::CompareFunction::Equal:
-                    return D3D12_COMPARISON_FUNC_EQUAL;
-                case dawn::CompareFunction::Greater:
-                    return D3D12_COMPARISON_FUNC_GREATER;
-                case dawn::CompareFunction::GreaterEqual:
-                    return D3D12_COMPARISON_FUNC_GREATER_EQUAL;
-                case dawn::CompareFunction::Less:
-                    return D3D12_COMPARISON_FUNC_LESS;
-                case dawn::CompareFunction::LessEqual:
-                    return D3D12_COMPARISON_FUNC_LESS_EQUAL;
-                case dawn::CompareFunction::Never:
-                    return D3D12_COMPARISON_FUNC_NEVER;
-                case dawn::CompareFunction::NotEqual:
-                    return D3D12_COMPARISON_FUNC_NOT_EQUAL;
-                default:
-                    UNREACHABLE();
-            }
-        }
-
         D3D12_DEPTH_STENCILOP_DESC StencilOpDesc(const StencilStateFaceDescriptor descriptor) {
             D3D12_DEPTH_STENCILOP_DESC desc;
 
             desc.StencilFailOp = StencilOp(descriptor.stencilFailOp);
             desc.StencilDepthFailOp = StencilOp(descriptor.depthFailOp);
             desc.StencilPassOp = StencilOp(descriptor.passOp);
-            desc.StencilFunc = ComparisonFunc(descriptor.compare);
+            desc.StencilFunc = ToD3D12ComparisonFunc(descriptor.compare);
 
             return desc;
         }
@@ -206,7 +184,7 @@
             mDepthStencilDescriptor.DepthWriteMask = descriptor->depthWriteEnabled
                                                          ? D3D12_DEPTH_WRITE_MASK_ALL
                                                          : D3D12_DEPTH_WRITE_MASK_ZERO;
-            mDepthStencilDescriptor.DepthFunc = ComparisonFunc(descriptor->depthCompare);
+            mDepthStencilDescriptor.DepthFunc = ToD3D12ComparisonFunc(descriptor->depthCompare);
 
             mDepthStencilDescriptor.StencilEnable = StencilTestEnabled(descriptor) ? TRUE : FALSE;
             mDepthStencilDescriptor.StencilReadMask =
diff --git a/src/dawn_native/metal/RenderPipelineMTL.mm b/src/dawn_native/metal/RenderPipelineMTL.mm
index 15822b4..70887f8 100644
--- a/src/dawn_native/metal/RenderPipelineMTL.mm
+++ b/src/dawn_native/metal/RenderPipelineMTL.mm
@@ -19,6 +19,7 @@
 #include "dawn_native/metal/PipelineLayoutMTL.h"
 #include "dawn_native/metal/ShaderModuleMTL.h"
 #include "dawn_native/metal/TextureMTL.h"
+#include "dawn_native/metal/UtilsMetal.h"
 
 namespace dawn_native { namespace metal {
 
@@ -143,27 +144,6 @@
             attachment.writeMask = MetalColorWriteMask(descriptor->colorWriteMask);
         }
 
-        MTLCompareFunction MetalDepthStencilCompareFunction(dawn::CompareFunction compareFunction) {
-            switch (compareFunction) {
-                case dawn::CompareFunction::Never:
-                    return MTLCompareFunctionNever;
-                case dawn::CompareFunction::Less:
-                    return MTLCompareFunctionLess;
-                case dawn::CompareFunction::LessEqual:
-                    return MTLCompareFunctionLessEqual;
-                case dawn::CompareFunction::Greater:
-                    return MTLCompareFunctionGreater;
-                case dawn::CompareFunction::GreaterEqual:
-                    return MTLCompareFunctionGreaterEqual;
-                case dawn::CompareFunction::NotEqual:
-                    return MTLCompareFunctionNotEqual;
-                case dawn::CompareFunction::Equal:
-                    return MTLCompareFunctionEqual;
-                case dawn::CompareFunction::Always:
-                    return MTLCompareFunctionAlways;
-            }
-        }
-
         MTLStencilOperation MetalStencilOperation(dawn::StencilOperation stencilOperation) {
             switch (stencilOperation) {
                 case dawn::StencilOperation::Keep:
@@ -190,7 +170,7 @@
             MTLDepthStencilDescriptor* mtlDepthStencilDescriptor =
                 [[MTLDepthStencilDescriptor new] autorelease];
             mtlDepthStencilDescriptor.depthCompareFunction =
-                MetalDepthStencilCompareFunction(descriptor->depthCompare);
+                ToMetalCompareFunction(descriptor->depthCompare);
             mtlDepthStencilDescriptor.depthWriteEnabled = descriptor->depthWriteEnabled;
 
             if (StencilTestEnabled(descriptor)) {
@@ -198,7 +178,7 @@
                 MTLStencilDescriptor* frontFaceStencil = [[MTLStencilDescriptor new] autorelease];
 
                 backFaceStencil.stencilCompareFunction =
-                    MetalDepthStencilCompareFunction(descriptor->back.compare);
+                    ToMetalCompareFunction(descriptor->back.compare);
                 backFaceStencil.stencilFailureOperation =
                     MetalStencilOperation(descriptor->back.stencilFailOp);
                 backFaceStencil.depthFailureOperation =
@@ -209,7 +189,7 @@
                 backFaceStencil.writeMask = descriptor->stencilWriteMask;
 
                 frontFaceStencil.stencilCompareFunction =
-                    MetalDepthStencilCompareFunction(descriptor->front.compare);
+                    ToMetalCompareFunction(descriptor->front.compare);
                 frontFaceStencil.stencilFailureOperation =
                     MetalStencilOperation(descriptor->front.stencilFailOp);
                 frontFaceStencil.depthFailureOperation =
diff --git a/src/dawn_native/opengl/RenderPipelineGL.cpp b/src/dawn_native/opengl/RenderPipelineGL.cpp
index 9d14fc4..10b4d37 100644
--- a/src/dawn_native/opengl/RenderPipelineGL.cpp
+++ b/src/dawn_native/opengl/RenderPipelineGL.cpp
@@ -18,6 +18,7 @@
 #include "dawn_native/opengl/Forward.h"
 #include "dawn_native/opengl/InputStateGL.h"
 #include "dawn_native/opengl/PersistentPipelineStateGL.h"
+#include "dawn_native/opengl/UtilsGL.h"
 
 namespace dawn_native { namespace opengl {
 
@@ -108,29 +109,6 @@
                          descriptor->colorWriteMask & dawn::ColorWriteMask::Alpha);
         }
 
-        GLuint OpenGLCompareFunction(dawn::CompareFunction compareFunction) {
-            switch (compareFunction) {
-                case dawn::CompareFunction::Never:
-                    return GL_NEVER;
-                case dawn::CompareFunction::Less:
-                    return GL_LESS;
-                case dawn::CompareFunction::LessEqual:
-                    return GL_LEQUAL;
-                case dawn::CompareFunction::Greater:
-                    return GL_GREATER;
-                case dawn::CompareFunction::GreaterEqual:
-                    return GL_GEQUAL;
-                case dawn::CompareFunction::NotEqual:
-                    return GL_NOTEQUAL;
-                case dawn::CompareFunction::Equal:
-                    return GL_EQUAL;
-                case dawn::CompareFunction::Always:
-                    return GL_ALWAYS;
-                default:
-                    UNREACHABLE();
-            }
-        }
-
         GLuint OpenGLStencilOperation(dawn::StencilOperation stencilOperation) {
             switch (stencilOperation) {
                 case dawn::StencilOperation::Keep:
@@ -170,7 +148,7 @@
                 glDepthMask(GL_FALSE);
             }
 
-            glDepthFunc(OpenGLCompareFunction(descriptor->depthCompare));
+            glDepthFunc(ToOpenGLCompareFunction(descriptor->depthCompare));
 
             if (StencilTestEnabled(descriptor)) {
                 glEnable(GL_STENCIL_TEST);
@@ -178,8 +156,8 @@
                 glDisable(GL_STENCIL_TEST);
             }
 
-            GLenum backCompareFunction = OpenGLCompareFunction(descriptor->back.compare);
-            GLenum frontCompareFunction = OpenGLCompareFunction(descriptor->front.compare);
+            GLenum backCompareFunction = ToOpenGLCompareFunction(descriptor->back.compare);
+            GLenum frontCompareFunction = ToOpenGLCompareFunction(descriptor->front.compare);
             persistentPipelineState->SetStencilFuncsAndMask(
                 backCompareFunction, frontCompareFunction, descriptor->stencilReadMask);
 
diff --git a/src/dawn_native/vulkan/RenderPipelineVk.cpp b/src/dawn_native/vulkan/RenderPipelineVk.cpp
index 481ab1b..ca9358f 100644
--- a/src/dawn_native/vulkan/RenderPipelineVk.cpp
+++ b/src/dawn_native/vulkan/RenderPipelineVk.cpp
@@ -21,6 +21,7 @@
 #include "dawn_native/vulkan/RenderPassCache.h"
 #include "dawn_native/vulkan/RenderPassDescriptorVk.h"
 #include "dawn_native/vulkan/ShaderModuleVk.h"
+#include "dawn_native/vulkan/UtilsVulkan.h"
 
 namespace dawn_native { namespace vulkan {
 
@@ -125,29 +126,6 @@
             return attachment;
         }
 
-        VkCompareOp VulkanCompareOp(dawn::CompareFunction op) {
-            switch (op) {
-                case dawn::CompareFunction::Always:
-                    return VK_COMPARE_OP_ALWAYS;
-                case dawn::CompareFunction::Equal:
-                    return VK_COMPARE_OP_EQUAL;
-                case dawn::CompareFunction::Greater:
-                    return VK_COMPARE_OP_GREATER;
-                case dawn::CompareFunction::GreaterEqual:
-                    return VK_COMPARE_OP_GREATER_OR_EQUAL;
-                case dawn::CompareFunction::Less:
-                    return VK_COMPARE_OP_LESS;
-                case dawn::CompareFunction::LessEqual:
-                    return VK_COMPARE_OP_LESS_OR_EQUAL;
-                case dawn::CompareFunction::Never:
-                    return VK_COMPARE_OP_NEVER;
-                case dawn::CompareFunction::NotEqual:
-                    return VK_COMPARE_OP_NOT_EQUAL;
-                default:
-                    UNREACHABLE();
-            }
-        }
-
         VkStencilOp VulkanStencilOp(dawn::StencilOperation op) {
             switch (op) {
                 case dawn::StencilOperation::Keep:
@@ -185,7 +163,7 @@
                     ? VK_FALSE
                     : VK_TRUE;
             depthStencilState.depthWriteEnable = descriptor->depthWriteEnabled ? VK_TRUE : VK_FALSE;
-            depthStencilState.depthCompareOp = VulkanCompareOp(descriptor->depthCompare);
+            depthStencilState.depthCompareOp = ToVulkanCompareOp(descriptor->depthCompare);
             depthStencilState.depthBoundsTestEnable = false;
             depthStencilState.minDepthBounds = 0.0f;
             depthStencilState.maxDepthBounds = 1.0f;
@@ -196,12 +174,12 @@
             depthStencilState.front.failOp = VulkanStencilOp(descriptor->front.stencilFailOp);
             depthStencilState.front.passOp = VulkanStencilOp(descriptor->front.passOp);
             depthStencilState.front.depthFailOp = VulkanStencilOp(descriptor->front.depthFailOp);
-            depthStencilState.front.compareOp = VulkanCompareOp(descriptor->front.compare);
+            depthStencilState.front.compareOp = ToVulkanCompareOp(descriptor->front.compare);
 
             depthStencilState.back.failOp = VulkanStencilOp(descriptor->back.stencilFailOp);
             depthStencilState.back.passOp = VulkanStencilOp(descriptor->back.passOp);
             depthStencilState.back.depthFailOp = VulkanStencilOp(descriptor->back.depthFailOp);
-            depthStencilState.back.compareOp = VulkanCompareOp(descriptor->back.compare);
+            depthStencilState.back.compareOp = ToVulkanCompareOp(descriptor->back.compare);
 
             // Dawn doesn't have separate front and back stencil masks.
             depthStencilState.front.compareMask = descriptor->stencilReadMask;