Updating RenderPipelineDescriptor to the newest layout

Currently normalizes anything using the new layout to the old one for
the sake of getting things working as quickly as possible. Follow up
changes will gradually push the new layout through more of the stack.

Bug: dawn:642
Change-Id: Ie92fa9dde21174f62ceba1a1f4866cbc24c5fc6f
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/38600
Commit-Queue: Brandon Jones <bajones@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/utils/ComboRenderPipelineDescriptor.cpp b/src/utils/ComboRenderPipelineDescriptor.cpp
index 73defe6..046ceaa 100644
--- a/src/utils/ComboRenderPipelineDescriptor.cpp
+++ b/src/utils/ComboRenderPipelineDescriptor.cpp
@@ -18,6 +18,8 @@
 
 namespace utils {
 
+    // For creating deprecated render pipeline descriptors
+
     ComboVertexStateDescriptor::ComboVertexStateDescriptor() {
         wgpu::VertexStateDescriptor* descriptor = this;
 
@@ -114,4 +116,96 @@
         }
     }
 
+    ComboRenderPipelineDescriptor2::ComboRenderPipelineDescriptor2() {
+        wgpu::RenderPipelineDescriptor2* descriptor = this;
+
+        // Set defaults for the vertex state.
+        {
+            wgpu::VertexState* vertex = &descriptor->vertex;
+            vertex->module = nullptr;
+            vertex->entryPoint = "main";
+            vertex->bufferCount = 0;
+
+            // Fill the default values for vertexBuffers and vertexAttributes in buffers.
+            for (uint32_t i = 0; i < kMaxVertexAttributes; ++i) {
+                cAttributes[i].shaderLocation = 0;
+                cAttributes[i].offset = 0;
+                cAttributes[i].format = wgpu::VertexFormat::Float32;
+            }
+            for (uint32_t i = 0; i < kMaxVertexBuffers; ++i) {
+                cBuffers[i].arrayStride = 0;
+                cBuffers[i].stepMode = wgpu::InputStepMode::Vertex;
+                cBuffers[i].attributeCount = 0;
+                cBuffers[i].attributes = nullptr;
+            }
+            // cBuffers[i].attributes points to somewhere in cAttributes.
+            // cBuffers[0].attributes points to &cAttributes[0] by default. Assuming
+            // cBuffers[0] has two attributes, then cBuffers[1].attributes should point to
+            // &cAttributes[2]. Likewise, if cBuffers[1] has 3 attributes, then
+            // cBuffers[2].attributes should point to &cAttributes[5].
+            cBuffers[0].attributes = &cAttributes[0];
+            vertex->buffers = &cBuffers[0];
+        }
+
+        // Set the defaults for the primitive state
+        {
+            wgpu::PrimitiveState* primitive = &descriptor->primitive;
+            primitive->topology = wgpu::PrimitiveTopology::TriangleList;
+            primitive->stripIndexFormat = wgpu::IndexFormat::Undefined;
+            primitive->frontFace = wgpu::FrontFace::CCW;
+            primitive->cullMode = wgpu::CullMode::None;
+        }
+
+        // Set the defaults for the depth-stencil state
+        {
+            wgpu::StencilFaceState stencilFace;
+            stencilFace.compare = wgpu::CompareFunction::Always;
+            stencilFace.failOp = wgpu::StencilOperation::Keep;
+            stencilFace.depthFailOp = wgpu::StencilOperation::Keep;
+            stencilFace.passOp = wgpu::StencilOperation::Keep;
+
+            cDepthStencil.format = wgpu::TextureFormat::Depth24PlusStencil8;
+            cDepthStencil.depthWriteEnabled = false;
+            cDepthStencil.depthCompare = wgpu::CompareFunction::Always;
+            cDepthStencil.stencilBack = stencilFace;
+            cDepthStencil.stencilFront = stencilFace;
+            cDepthStencil.stencilReadMask = 0xff;
+            cDepthStencil.stencilWriteMask = 0xff;
+            cDepthStencil.depthBias = 0;
+            cDepthStencil.depthBiasSlopeScale = 0.0;
+            cDepthStencil.depthBiasClamp = 0.0;
+        }
+
+        // Set the defaults for the multisample state
+        {
+            wgpu::MultisampleState* multisample = &descriptor->multisample;
+            multisample->count = 1;
+            multisample->mask = 0xFFFFFFFF;
+            multisample->alphaToCoverageEnabled = false;
+        }
+
+        // Set the defaults for the fragment state
+        {
+            cFragment.module = nullptr;
+            cFragment.entryPoint = "main";
+            cFragment.targetCount = 1;
+            cFragment.targets = &cTargets[0];
+            descriptor->fragment = &cFragment;
+
+            wgpu::BlendComponent blendComponent;
+            blendComponent.srcFactor = wgpu::BlendFactor::One;
+            blendComponent.dstFactor = wgpu::BlendFactor::Zero;
+            blendComponent.operation = wgpu::BlendOperation::Add;
+
+            for (uint32_t i = 0; i < kMaxColorAttachments; ++i) {
+                cTargets[i].format = wgpu::TextureFormat::RGBA8Unorm;
+                cTargets[i].blend = nullptr;
+                cTargets[i].writeMask = wgpu::ColorWriteMask::All;
+
+                cBlends[i].color = blendComponent;
+                cBlends[i].alpha = blendComponent;
+            }
+        }
+    }
+
 }  // namespace utils
diff --git a/src/utils/ComboRenderPipelineDescriptor.h b/src/utils/ComboRenderPipelineDescriptor.h
index ce8eb30..67ee058 100644
--- a/src/utils/ComboRenderPipelineDescriptor.h
+++ b/src/utils/ComboRenderPipelineDescriptor.h
@@ -23,6 +23,7 @@
 
 namespace utils {
 
+    // For creating deprecated render pipeline descriptors
     class ComboVertexStateDescriptor : public wgpu::VertexStateDescriptor {
       public:
         ComboVertexStateDescriptor();
@@ -53,6 +54,25 @@
         wgpu::DepthStencilStateDescriptor cDepthStencilState;
     };
 
+    // For creating the new style of render pipeline descriptors
+    class ComboRenderPipelineDescriptor2 : public wgpu::RenderPipelineDescriptor2 {
+      public:
+        ComboRenderPipelineDescriptor2();
+
+        ComboRenderPipelineDescriptor2(const ComboRenderPipelineDescriptor2&) = delete;
+        ComboRenderPipelineDescriptor2& operator=(const ComboRenderPipelineDescriptor2&) = delete;
+        ComboRenderPipelineDescriptor2(ComboRenderPipelineDescriptor2&&) = delete;
+        ComboRenderPipelineDescriptor2& operator=(ComboRenderPipelineDescriptor2&&) = delete;
+
+        std::array<wgpu::VertexBufferLayout, kMaxVertexBuffers> cBuffers;
+        std::array<wgpu::VertexAttribute, kMaxVertexAttributes> cAttributes;
+        std::array<wgpu::ColorTargetState, kMaxColorAttachments> cTargets;
+        std::array<wgpu::BlendState, kMaxColorAttachments> cBlends;
+
+        wgpu::DepthStencilState cDepthStencil;
+        wgpu::FragmentState cFragment;
+    };
+
 }  // namespace utils
 
 #endif  // UTILS_COMBORENDERPIPELINEDESCRIPTOR_H_