Use blend descriptor to replace a blend struct.

Bug=dawn:32

Change-Id: I74db6c1e9cf57a168967131ea2c9e1d802853ab4
Reviewed-on: https://dawn-review.googlesource.com/c/2020
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/dawn.json b/dawn.json
index 07a7f7d..ca5db3a 100644
--- a/dawn.json
+++ b/dawn.json
@@ -96,6 +96,15 @@
             {"value": 3, "name": "storage buffer"}
         ]
     },
+    "blend descriptor": {
+        "category": "structure",
+        "extensible": false,
+        "members": [
+            {"name": "operation", "type": "blend operation"},
+            {"name": "src factor", "type": "blend factor"},
+            {"name": "dst factor", "type": "blend factor"}
+        ]
+    },
     "blend factor": {
         "category": "enum",
         "values": [
@@ -143,17 +152,13 @@
             {
                 "name": "set alpha blend",
                 "args": [
-                    {"name": "blend operation", "type": "blend operation"},
-                    {"name": "src factor",  "type": "blend factor"},
-                    {"name": "dst factor", "type": "blend factor"}
+                    {"name": "alpha blend", "type": "blend descriptor", "annotation": "const*"}
                 ]
             },
             {
                 "name": "set color blend",
                 "args": [
-                    {"name": "blend operation", "type": "blend operation"},
-                    {"name": "src factor",  "type": "blend factor"},
-                    {"name": "dst factor", "type": "blend factor"}
+                    {"name": "color blend", "type": "blend descriptor", "annotation": "const*"}
                 ]
             },
             {
diff --git a/src/dawn_native/BlendState.cpp b/src/dawn_native/BlendState.cpp
index ba1a717..2cf042d 100644
--- a/src/dawn_native/BlendState.cpp
+++ b/src/dawn_native/BlendState.cpp
@@ -55,9 +55,7 @@
         mBlendInfo.blendEnabled = blendEnabled;
     }
 
-    void BlendStateBuilder::SetAlphaBlend(dawn::BlendOperation blendOperation,
-                                          dawn::BlendFactor srcFactor,
-                                          dawn::BlendFactor dstFactor) {
+    void BlendStateBuilder::SetAlphaBlend(const BlendDescriptor* alphaBlend) {
         if ((mPropertiesSet & BLEND_STATE_PROPERTY_ALPHA_BLEND) != 0) {
             HandleError("Alpha blend property set multiple times");
             return;
@@ -65,12 +63,14 @@
 
         mPropertiesSet |= BLEND_STATE_PROPERTY_ALPHA_BLEND;
 
-        mBlendInfo.alphaBlend = {blendOperation, srcFactor, dstFactor};
+        // TODO(yunchao.he@intel.com): validate the enum values in
+        // ValidateBlendStateDescriptor when it is added.
+        mBlendInfo.alphaBlend.operation = alphaBlend->operation;
+        mBlendInfo.alphaBlend.srcFactor = alphaBlend->srcFactor;
+        mBlendInfo.alphaBlend.dstFactor = alphaBlend->dstFactor;
     }
 
-    void BlendStateBuilder::SetColorBlend(dawn::BlendOperation blendOperation,
-                                          dawn::BlendFactor srcFactor,
-                                          dawn::BlendFactor dstFactor) {
+    void BlendStateBuilder::SetColorBlend(const BlendDescriptor* colorBlend) {
         if ((mPropertiesSet & BLEND_STATE_PROPERTY_COLOR_BLEND) != 0) {
             HandleError("Color blend property set multiple times");
             return;
@@ -78,7 +78,11 @@
 
         mPropertiesSet |= BLEND_STATE_PROPERTY_COLOR_BLEND;
 
-        mBlendInfo.colorBlend = {blendOperation, srcFactor, dstFactor};
+        // TODO(yunchao.he@intel.com): validate the enum values in
+        // ValidateBlendStateDescriptor when it is added.
+        mBlendInfo.colorBlend.operation = colorBlend->operation;
+        mBlendInfo.colorBlend.srcFactor = colorBlend->srcFactor;
+        mBlendInfo.colorBlend.dstFactor = colorBlend->dstFactor;
     }
 
     void BlendStateBuilder::SetColorWriteMask(dawn::ColorWriteMask colorWriteMask) {
diff --git a/src/dawn_native/BlendState.h b/src/dawn_native/BlendState.h
index 695a524..67b0cf4 100644
--- a/src/dawn_native/BlendState.h
+++ b/src/dawn_native/BlendState.h
@@ -28,15 +28,9 @@
         BlendStateBase(BlendStateBuilder* builder);
 
         struct BlendInfo {
-            struct BlendOpFactor {
-                dawn::BlendOperation operation = dawn::BlendOperation::Add;
-                dawn::BlendFactor srcFactor = dawn::BlendFactor::One;
-                dawn::BlendFactor dstFactor = dawn::BlendFactor::Zero;
-            };
-
             bool blendEnabled = false;
-            BlendOpFactor alphaBlend;
-            BlendOpFactor colorBlend;
+            BlendDescriptor alphaBlend;
+            BlendDescriptor colorBlend;
             dawn::ColorWriteMask colorWriteMask = dawn::ColorWriteMask::All;
         };
 
@@ -52,12 +46,8 @@
 
         // Dawn API
         void SetBlendEnabled(bool blendEnabled);
-        void SetAlphaBlend(dawn::BlendOperation blendOperation,
-                           dawn::BlendFactor srcFactor,
-                           dawn::BlendFactor dstFactor);
-        void SetColorBlend(dawn::BlendOperation blendOperation,
-                           dawn::BlendFactor srcFactor,
-                           dawn::BlendFactor dstFactor);
+        void SetAlphaBlend(const BlendDescriptor* alphaBlend);
+        void SetColorBlend(const BlendDescriptor* colorBlend);
         void SetColorWriteMask(dawn::ColorWriteMask colorWriteMask);
 
       private:
diff --git a/src/tests/end2end/BlendStateTests.cpp b/src/tests/end2end/BlendStateTests.cpp
index 416d1f1..c18bc30 100644
--- a/src/tests/end2end/BlendStateTests.cpp
+++ b/src/tests/end2end/BlendStateTests.cpp
@@ -133,10 +133,15 @@
 
         // Given a vector of tests where each element is <testColor, expectedColor>, check that all expectations are true for the given blend operation
         void CheckBlendOperation(RGBA8 base, dawn::BlendOperation operation, std::vector<std::pair<RGBA8, RGBA8>> tests) {
+            dawn::BlendDescriptor blend;
+            blend.operation = operation;
+            blend.srcFactor = dawn::BlendFactor::One;
+            blend.dstFactor = dawn::BlendFactor::One;
+
             dawn::BlendState blendState = device.CreateBlendStateBuilder()
                 .SetBlendEnabled(true)
-                .SetColorBlend(operation, dawn::BlendFactor::One, dawn::BlendFactor::One)
-                .SetAlphaBlend(operation, dawn::BlendFactor::One, dawn::BlendFactor::One)
+                .SetColorBlend(&blend)
+                .SetAlphaBlend(&blend)
                 .GetResult();
 
             SetupSingleSourcePipelines(blendState);
@@ -148,10 +153,20 @@
 
         // Given a vector of tests where each element is <testSpec, expectedColor>, check that all expectations are true for the given blend factors
         void CheckBlendFactor(RGBA8 base, dawn::BlendFactor colorSrcFactor, dawn::BlendFactor colorDstFactor, dawn::BlendFactor alphaSrcFactor, dawn::BlendFactor alphaDstFactor, std::vector<std::pair<TriangleSpec, RGBA8>> tests) {
+            dawn::BlendDescriptor colorBlend;
+            colorBlend.operation = dawn::BlendOperation::Add;
+            colorBlend.srcFactor = colorSrcFactor;
+            colorBlend.dstFactor = colorDstFactor;
+
+            dawn::BlendDescriptor alphaBlend;
+            alphaBlend.operation = dawn::BlendOperation::Add;
+            alphaBlend.srcFactor = alphaSrcFactor;
+            alphaBlend.dstFactor = alphaDstFactor;
+
             dawn::BlendState blendState = device.CreateBlendStateBuilder()
                 .SetBlendEnabled(true)
-                .SetColorBlend(dawn::BlendOperation::Add, colorSrcFactor, colorDstFactor)
-                .SetAlphaBlend(dawn::BlendOperation::Add, alphaSrcFactor, alphaDstFactor)
+                .SetColorBlend(&colorBlend)
+                .SetAlphaBlend(&alphaBlend)
                 .GetResult();
 
             SetupSingleSourcePipelines(blendState);
@@ -610,12 +625,17 @@
 
 // Check that the color write mask works
 TEST_P(BlendStateTest, ColorWriteMask) {
+    dawn::BlendDescriptor blend;
+    blend.operation = dawn::BlendOperation::Add;
+    blend.srcFactor = dawn::BlendFactor::One;
+    blend.dstFactor = dawn::BlendFactor::One;
+
     {
         // Test single channel color write
         dawn::BlendState blendState = device.CreateBlendStateBuilder()
             .SetBlendEnabled(true)
-            .SetColorBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
-            .SetAlphaBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
+            .SetColorBlend(&blend)
+            .SetAlphaBlend(&blend)
             .SetColorWriteMask(dawn::ColorWriteMask::Red)
             .GetResult();
         SetupSingleSourcePipelines(blendState);
@@ -631,8 +651,8 @@
         // Test multi channel color write
         dawn::BlendState blendState = device.CreateBlendStateBuilder()
             .SetBlendEnabled(true)
-            .SetColorBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
-            .SetAlphaBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
+            .SetColorBlend(&blend)
+            .SetAlphaBlend(&blend)
             .SetColorWriteMask(dawn::ColorWriteMask::Green | dawn::ColorWriteMask::Alpha)
             .GetResult();
         SetupSingleSourcePipelines(blendState);
@@ -648,8 +668,8 @@
         // Test no channel color write
         dawn::BlendState blendState = device.CreateBlendStateBuilder()
             .SetBlendEnabled(true)
-            .SetColorBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
-            .SetAlphaBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
+            .SetColorBlend(&blend)
+            .SetAlphaBlend(&blend)
             .SetColorWriteMask(dawn::ColorWriteMask::None)
             .GetResult();
         SetupSingleSourcePipelines(blendState);
@@ -739,21 +759,36 @@
         }
     )");
 
+    dawn::BlendDescriptor blend1;
+    blend1.operation = dawn::BlendOperation::Add;
+    blend1.srcFactor = dawn::BlendFactor::One;
+    blend1.dstFactor = dawn::BlendFactor::One;
+
+    dawn::BlendDescriptor blend2;
+    blend2.operation = dawn::BlendOperation::Subtract;
+    blend2.srcFactor = dawn::BlendFactor::One;
+    blend2.dstFactor = dawn::BlendFactor::One;
+
+    dawn::BlendDescriptor blend3;
+    blend3.operation = dawn::BlendOperation::Min;
+    blend3.srcFactor = dawn::BlendFactor::One;
+    blend3.dstFactor = dawn::BlendFactor::One;
+
     std::array<dawn::BlendState, 3> blendStates = { {
         device.CreateBlendStateBuilder()
             .SetBlendEnabled(true)
-            .SetColorBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
-            .SetAlphaBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
+            .SetColorBlend(&blend1)
+            .SetAlphaBlend(&blend1)
             .GetResult(),
         device.CreateBlendStateBuilder()
             .SetBlendEnabled(true)
-            .SetColorBlend(dawn::BlendOperation::Subtract, dawn::BlendFactor::One, dawn::BlendFactor::One)
-            .SetAlphaBlend(dawn::BlendOperation::Subtract, dawn::BlendFactor::One, dawn::BlendFactor::One)
+            .SetColorBlend(&blend2)
+            .SetAlphaBlend(&blend2)
             .GetResult(),
         device.CreateBlendStateBuilder()
             .SetBlendEnabled(true)
-            .SetColorBlend(dawn::BlendOperation::Min, dawn::BlendFactor::One, dawn::BlendFactor::One)
-            .SetAlphaBlend(dawn::BlendOperation::Min, dawn::BlendFactor::One, dawn::BlendFactor::One)
+            .SetColorBlend(&blend3)
+            .SetAlphaBlend(&blend3)
             .GetResult(),
     } };
 
@@ -819,10 +854,15 @@
 
 // Test that the default blend color is correctly set at the beginning of every subpass
 TEST_P(BlendStateTest, DefaultBlendColor) {
+    dawn::BlendDescriptor blend;
+    blend.operation = dawn::BlendOperation::Add;
+    blend.srcFactor = dawn::BlendFactor::BlendColor;
+    blend.dstFactor = dawn::BlendFactor::One;
+
     dawn::BlendState blendState = device.CreateBlendStateBuilder()
         .SetBlendEnabled(true)
-        .SetColorBlend(dawn::BlendOperation::Add, dawn::BlendFactor::BlendColor, dawn::BlendFactor::One)
-        .SetAlphaBlend(dawn::BlendOperation::Add, dawn::BlendFactor::BlendColor, dawn::BlendFactor::One)
+        .SetColorBlend(&blend)
+        .SetAlphaBlend(&blend)
         .GetResult();
 
     dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"(
diff --git a/src/tests/end2end/PushConstantTests.cpp b/src/tests/end2end/PushConstantTests.cpp
index 80fa857..e9bd05e 100644
--- a/src/tests/end2end/PushConstantTests.cpp
+++ b/src/tests/end2end/PushConstantTests.cpp
@@ -182,10 +182,15 @@
                 })").c_str()
             );
 
+            dawn::BlendDescriptor blend;
+            blend.operation = dawn::BlendOperation::Add;
+            blend.srcFactor = dawn::BlendFactor::One;
+            blend.dstFactor = dawn::BlendFactor::One;
+
             dawn::BlendState blendState = device.CreateBlendStateBuilder()
                 .SetBlendEnabled(true)
-                .SetColorBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
-                .SetAlphaBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
+                .SetColorBlend(&blend)
+                .SetAlphaBlend(&blend)
                 .GetResult();
 
             return device.CreateRenderPipelineBuilder()
diff --git a/src/tests/unittests/validation/BlendStateValidationTests.cpp b/src/tests/unittests/validation/BlendStateValidationTests.cpp
index 5a1e83a..030cc63 100644
--- a/src/tests/unittests/validation/BlendStateValidationTests.cpp
+++ b/src/tests/unittests/validation/BlendStateValidationTests.cpp
@@ -21,10 +21,15 @@
 TEST_F(BlendStateValidationTest, CreationSuccess) {
     // Success for setting all properties
     {
+        dawn::BlendDescriptor blend;
+        blend.operation = dawn::BlendOperation::Add;
+        blend.srcFactor = dawn::BlendFactor::One;
+        blend.dstFactor = dawn::BlendFactor::One;
+
         dawn::BlendState state = AssertWillBeSuccess(device.CreateBlendStateBuilder())
             .SetBlendEnabled(true)
-            .SetAlphaBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
-            .SetColorBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
+            .SetAlphaBlend(&blend)
+            .SetColorBlend(&blend)
             .SetColorWriteMask(dawn::ColorWriteMask::Red)
             .GetResult();
     }
@@ -46,19 +51,29 @@
             .GetResult();
     }
 
+    dawn::BlendDescriptor blend1;
+    blend1.operation = dawn::BlendOperation::Add;
+    blend1.srcFactor = dawn::BlendFactor::One;
+    blend1.dstFactor = dawn::BlendFactor::One;
+
+    dawn::BlendDescriptor blend2;
+    blend2.operation = dawn::BlendOperation::Add;
+    blend2.srcFactor = dawn::BlendFactor::Zero;
+    blend2.dstFactor = dawn::BlendFactor::Zero;
+
     // Test failure when specifying alpha blend multiple times
     {
         dawn::BlendState state = AssertWillBeError(device.CreateBlendStateBuilder())
-            .SetAlphaBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
-            .SetAlphaBlend(dawn::BlendOperation::Add, dawn::BlendFactor::Zero, dawn::BlendFactor::Zero)
+            .SetAlphaBlend(&blend1)
+            .SetAlphaBlend(&blend2)
             .GetResult();
     }
 
     // Test failure when specifying color blend multiple times
     {
         dawn::BlendState state = AssertWillBeError(device.CreateBlendStateBuilder())
-            .SetColorBlend(dawn::BlendOperation::Add, dawn::BlendFactor::One, dawn::BlendFactor::One)
-            .SetColorBlend(dawn::BlendOperation::Add, dawn::BlendFactor::Zero, dawn::BlendFactor::Zero)
+            .SetColorBlend(&blend1)
+            .SetColorBlend(&blend2)
             .GetResult();
     }