Wire up cull mode handling to Vulkan pipeline

Bug: b/124177079
Change-Id: I8e55607c8ff7f9c2d2356268bedf170cf27eeb99
Reviewed-on: https://swiftshader-review.googlesource.com/c/25268
Tested-by: Chris Forbes <chrisforbes@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Device/Context.hpp b/src/Device/Context.hpp
index 79d9c10..704d126 100644
--- a/src/Device/Context.hpp
+++ b/src/Device/Context.hpp
@@ -177,7 +177,7 @@
 		int stencilWriteMaskCCW;
 
 		// Pixel processor states
-		CullMode cullMode;
+		VkCullModeFlags cullMode;
 		bool frontFacingCCW;
 		float alphaReference;
 
diff --git a/src/Device/SetupProcessor.cpp b/src/Device/SetupProcessor.cpp
index 7b04d5f..a0d4793 100644
--- a/src/Device/SetupProcessor.cpp
+++ b/src/Device/SetupProcessor.cpp
@@ -82,6 +82,7 @@
 		state.interpolateZ = context->depthBufferActive() || vPosZW;
 		state.interpolateW = context->perspectiveActive() || vPosZW;
 		state.perspective = context->perspectiveActive();
+		state.frontFacingCCW = context->frontFacingCCW;
 		state.cullMode = context->cullMode;
 		state.twoSidedStencil = context->stencilActive() && context->twoSidedStencil;
 		state.slopeDepthBias = context->slopeDepthBias != 0.0f;
diff --git a/src/Device/SetupProcessor.hpp b/src/Device/SetupProcessor.hpp
index 7001bce..d5b40f6 100644
--- a/src/Device/SetupProcessor.hpp
+++ b/src/Device/SetupProcessor.hpp
@@ -42,7 +42,8 @@
 			bool interpolateZ              : 1;
 			bool interpolateW              : 1;
 			bool perspective               : 1;
-			CullMode cullMode              : BITS(CULL_LAST);
+			bool frontFacingCCW            : 1;
+			VkCullModeFlags cullMode       : BITS(VK_CULL_MODE_FLAG_BITS_MAX_ENUM);
 			bool twoSidedStencil           : 1;
 			bool slopeDepthBias            : 1;
 			bool vFace                     : 1;
diff --git a/src/Pipeline/SetupRoutine.cpp b/src/Pipeline/SetupRoutine.cpp
index 80a4557..67c861c 100644
--- a/src/Pipeline/SetupRoutine.cpp
+++ b/src/Pipeline/SetupRoutine.cpp
@@ -96,20 +96,22 @@
 
 				A = IfThenElse(w0w1w2 < 0, -A, A);
 
-				if(state.cullMode == CULL_CLOCKWISE)
+				Bool frontFacing = state.frontFacingCCW ? A > 0.0f : A < 0.0f;
+
+				if(state.cullMode & VK_CULL_MODE_FRONT_BIT)
 				{
-					If(A >= 0.0f) Return(false);
+					If(frontFacing) Return(false);
 				}
-				else if(state.cullMode == CULL_COUNTERCLOCKWISE)
+				if(state.cullMode & VK_CULL_MODE_BACK_BIT)
 				{
-					If(A <= 0.0f) Return(false);
+					If(!frontFacing) Return(false);
 				}
 
 				d = IfThenElse(A < 0.0f, d, Int(0));
 
 				if(state.twoSidedStencil)
 				{
-					If(A > 0.0f)
+					If(frontFacing)
 					{
 						*Pointer<Byte8>(primitive + OFFSET(Primitive,clockwiseMask)) = Byte8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
 						*Pointer<Byte8>(primitive + OFFSET(Primitive,invClockwiseMask)) = Byte8(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
diff --git a/src/Vulkan/VkPipeline.cpp b/src/Vulkan/VkPipeline.cpp
index 3205c04..82591f4 100644
--- a/src/Vulkan/VkPipeline.cpp
+++ b/src/Vulkan/VkPipeline.cpp
@@ -289,6 +289,7 @@
 	}
 
 	context.rasterizerDiscard = rasterizationState->rasterizerDiscardEnable;
+	context.cullMode = rasterizationState->cullMode;
 	context.frontFacingCCW = rasterizationState->frontFace == VK_FRONT_FACE_COUNTER_CLOCKWISE;
 	context.depthBias = (rasterizationState->depthBiasEnable ? rasterizationState->depthBiasConstantFactor : 0.0f);
 	context.slopeDepthBias = (rasterizationState->depthBiasEnable ? rasterizationState->depthBiasSlopeFactor : 0.0f);