Introduce [Render|Compute]PassEncoder.

This splits off part of CommandBufferBuilder in separate
RenderPassEncoder and ComputePassEncoder objects. To match the WebGPU
IDL and factor some code, both these encoders inherit from
ProgrammablePassEncoder.

These encoders are pure frontend objects and record into the
CommandBufferBuilder command allocator objects, so no changes to the
backends were needed.

Error handling is still ew, because the "builder" mechanism we had
doesn't allow for "split builders". Nicer error handling will have to
wait on Dawn matching WebGPU.

All the tests and samples were updated to the new structure.

BUG=dawn:5

Change-Id: I5f5d4ad866e2c07fedd1ba7a122258c6610941f1
Reviewed-on: https://dawn-review.googlesource.com/1543
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/examples/Animometer.cpp b/examples/Animometer.cpp
index ccbcb94..9e0eff9 100644
--- a/examples/Animometer.cpp
+++ b/examples/Animometer.cpp
@@ -137,24 +137,22 @@
 
     size_t i = 0;
 
-    dawn::CommandBuffer commands;
+    dawn::CommandBufferBuilder builder = device.CreateCommandBufferBuilder();
     {
-        dawn::CommandBufferBuilder builder = device.CreateCommandBufferBuilder()
-            .BeginRenderPass(renderPass)
-            .SetRenderPipeline(pipeline)
-            .Clone();
+        dawn::RenderPassEncoder pass = builder.BeginRenderPass(renderPass);
+        pass.SetRenderPipeline(pipeline);
 
         for (int k = 0; k < 10000; k++) {
             shaderData[i].time = f / 60.0f;
-            builder.SetPushConstants(dawn::ShaderStageBit::Vertex, 0, 6, reinterpret_cast<uint32_t*>(&shaderData[i]))
-                   .DrawArrays(3, 1, 0, 0);
+            pass.SetPushConstants(dawn::ShaderStageBit::Vertex, 0, 6, reinterpret_cast<uint32_t*>(&shaderData[i]));
+            pass.DrawArrays(3, 1, 0, 0);
             i++;
         }
 
-        builder.EndRenderPass();
-        commands = builder.GetResult();
+        pass.EndPass();
     }
 
+    dawn::CommandBuffer commands = builder.GetResult();
     queue.Submit(1, &commands);
     swapchain.Present(backbuffer);
     DoFlush();
diff --git a/examples/CHelloTriangle.cpp b/examples/CHelloTriangle.cpp
index 075b442..1d87073 100644
--- a/examples/CHelloTriangle.cpp
+++ b/examples/CHelloTriangle.cpp
@@ -84,10 +84,13 @@
     dawnCommandBuffer commands;
     {
         dawnCommandBufferBuilder builder = dawnDeviceCreateCommandBufferBuilder(device);
-        dawnCommandBufferBuilderBeginRenderPass(builder, renderpassInfo);
-        dawnCommandBufferBuilderSetRenderPipeline(builder, pipeline);
-        dawnCommandBufferBuilderDrawArrays(builder, 3, 1, 0, 0);
-        dawnCommandBufferBuilderEndRenderPass(builder);
+
+        dawnRenderPassEncoder pass = dawnCommandBufferBuilderBeginRenderPass(builder, renderpassInfo);
+        dawnRenderPassEncoderSetRenderPipeline(pass, pipeline);
+        dawnRenderPassEncoderDrawArrays(pass, 3, 1, 0, 0);
+        dawnRenderPassEncoderEndPass(pass);
+        dawnRenderPassEncoderRelease(pass);
+
         commands = dawnCommandBufferBuilderGetResult(builder);
         dawnCommandBufferBuilderRelease(builder);
     }
diff --git a/examples/ComputeBoids.cpp b/examples/ComputeBoids.cpp
index 4a69710..77b2ad8 100644
--- a/examples/ComputeBoids.cpp
+++ b/examples/ComputeBoids.cpp
@@ -261,21 +261,26 @@
 dawn::CommandBuffer createCommandBuffer(const dawn::RenderPassDescriptor& renderPass, size_t i) {
     static const uint32_t zeroOffsets[1] = {0};
     auto& bufferDst = particleBuffers[(i + 1) % 2];
-    return device.CreateCommandBufferBuilder()
-        .BeginComputePass()
-            .SetComputePipeline(updatePipeline)
-            .SetBindGroup(0, updateBGs[i])
-            .Dispatch(kNumParticles, 1, 1)
-        .EndComputePass()
+    dawn::CommandBufferBuilder builder = device.CreateCommandBufferBuilder();
 
-        .BeginRenderPass(renderPass)
-            .SetRenderPipeline(renderPipeline)
-            .SetVertexBuffers(0, 1, &bufferDst, zeroOffsets)
-            .SetVertexBuffers(1, 1, &modelBuffer, zeroOffsets)
-            .DrawArrays(3, kNumParticles, 0, 0)
-        .EndRenderPass()
+    {
+        dawn::ComputePassEncoder pass = builder.BeginComputePass();
+        pass.SetComputePipeline(updatePipeline);
+        pass.SetBindGroup(0, updateBGs[i]);
+        pass.Dispatch(kNumParticles, 1, 1);
+        pass.EndPass();
+    }
 
-        .GetResult();
+    {
+        dawn::RenderPassEncoder pass = builder.BeginRenderPass(renderPass);
+        pass.SetRenderPipeline(renderPipeline);
+        pass.SetVertexBuffers(0, 1, &bufferDst, zeroOffsets);
+        pass.SetVertexBuffers(1, 1, &modelBuffer, zeroOffsets);
+        pass.DrawArrays(3, kNumParticles, 0, 0);
+        pass.EndPass();
+    }
+
+    return builder.GetResult();
 }
 
 void init() {
diff --git a/examples/CppHelloTriangle.cpp b/examples/CppHelloTriangle.cpp
index 3be8973..c9aa88c 100644
--- a/examples/CppHelloTriangle.cpp
+++ b/examples/CppHelloTriangle.cpp
@@ -151,16 +151,18 @@
     GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
 
     static const uint32_t vertexBufferOffsets[1] = {0};
-    dawn::CommandBuffer commands = device.CreateCommandBufferBuilder()
-        .BeginRenderPass(renderPass)
-            .SetRenderPipeline(pipeline)
-            .SetBindGroup(0, bindGroup)
-            .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
-            .SetIndexBuffer(indexBuffer, 0)
-            .DrawElements(3, 1, 0, 0)
-        .EndRenderPass()
-        .GetResult();
+    dawn::CommandBufferBuilder builder = device.CreateCommandBufferBuilder();
+    {
+        dawn::RenderPassEncoder pass = builder.BeginRenderPass(renderPass);
+        pass.SetRenderPipeline(pipeline);
+        pass.SetBindGroup(0, bindGroup);
+        pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
+        pass.SetIndexBuffer(indexBuffer, 0);
+        pass.DrawElements(3, 1, 0, 0);
+        pass.EndPass();
+    }
 
+    dawn::CommandBuffer commands = builder.GetResult();
     queue.Submit(1, &commands);
     swapchain.Present(backbuffer);
     DoFlush();
diff --git a/examples/CubeReflection.cpp b/examples/CubeReflection.cpp
index 75ed5d7..5a5f658 100644
--- a/examples/CubeReflection.cpp
+++ b/examples/CubeReflection.cpp
@@ -277,27 +277,30 @@
     dawn::RenderPassDescriptor renderPass;
     GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
 
-    dawn::CommandBuffer commands = device.CreateCommandBufferBuilder()
-        .BeginRenderPass(renderPass)
-            .SetRenderPipeline(pipeline)
-            .SetBindGroup(0, bindGroup[0])
-            .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
-            .SetIndexBuffer(indexBuffer, 0)
-            .DrawElements(36, 1, 0, 0)
+    dawn::CommandBufferBuilder builder = device.CreateCommandBufferBuilder();
+    {
+        dawn::RenderPassEncoder pass = builder.BeginRenderPass(renderPass);
+        pass.SetRenderPipeline(pipeline);
+        pass.SetBindGroup(0, bindGroup[0]);
+        pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
+        pass.SetIndexBuffer(indexBuffer, 0);
+        pass.DrawElements(36, 1, 0, 0);
 
-            .SetStencilReference(0x1)
-            .SetRenderPipeline(planePipeline)
-            .SetBindGroup(0, bindGroup[0])
-            .SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets)
-            .DrawElements(6, 1, 0, 0)
+        pass.SetStencilReference(0x1);
+        pass.SetRenderPipeline(planePipeline);
+        pass.SetBindGroup(0, bindGroup[0]);
+        pass.SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets);
+        pass.DrawElements(6, 1, 0, 0);
 
-            .SetRenderPipeline(reflectionPipeline)
-            .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
-            .SetBindGroup(0, bindGroup[1])
-            .DrawElements(36, 1, 0, 0)
-        .EndRenderPass()
-        .GetResult();
+        pass.SetRenderPipeline(reflectionPipeline);
+        pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
+        pass.SetBindGroup(0, bindGroup[1]);
+        pass.DrawElements(36, 1, 0, 0);
 
+        pass.EndPass();
+    }
+
+    dawn::CommandBuffer commands = builder.GetResult();
     queue.Submit(1, &commands);
     swapchain.Present(backbuffer);
     DoFlush();
diff --git a/examples/glTFViewer/glTFViewer.cpp b/examples/glTFViewer/glTFViewer.cpp
index ecded7f..2418dba 100644
--- a/examples/glTFViewer/glTFViewer.cpp
+++ b/examples/glTFViewer/glTFViewer.cpp
@@ -460,7 +460,7 @@
 
 // Drawing
 namespace {
-    void drawMesh(dawn::CommandBufferBuilder& cmd, const tinygltf::Mesh& iMesh, const glm::mat4& model) {
+    void drawMesh(dawn::RenderPassEncoder& pass, const tinygltf::Mesh& iMesh, const glm::mat4& model) {
         for (const auto& iPrim : iMesh.primitives) {
             if (iPrim.mode != gl::Triangles) {
                 fprintf(stderr, "unsupported primitive mode %d\n", iPrim.mode);
@@ -484,9 +484,9 @@
                 }
             }
             const MaterialInfo& material = getMaterial(iPrim.material, strides[0], strides[1], strides[2]);
-            cmd.SetRenderPipeline(material.pipeline);
-            cmd.SetBindGroup(0, material.bindGroup0);
-            cmd.SetPushConstants(dawn::ShaderStageBit::Vertex,
+            pass.SetRenderPipeline(material.pipeline);
+            pass.SetBindGroup(0, material.bindGroup0);
+            pass.SetPushConstants(dawn::ShaderStageBit::Vertex,
                     0, sizeof(u_transform_block) / sizeof(uint32_t),
                     reinterpret_cast<const uint32_t*>(&transforms));
 
@@ -496,7 +496,7 @@
                 auto it = iPrim.attributes.find(s.second);
                 if (it == iPrim.attributes.end()) {
                     uint32_t zero = 0;
-                    cmd.SetVertexBuffers(slot, 1, &defaultBuffer, &zero);
+                    pass.SetVertexBuffers(slot, 1, &defaultBuffer, &zero);
                     continue;
                 }
                 const auto& iAccessor = scene.accessors.at(it->second);
@@ -511,7 +511,7 @@
                 }
                 const auto& oBuffer = buffers.at(iAccessor.bufferView);
                 uint32_t iBufferOffset = static_cast<uint32_t>(iAccessor.byteOffset);
-                cmd.SetVertexBuffers(slot, 1, &oBuffer, &iBufferOffset);
+                pass.SetVertexBuffers(slot, 1, &oBuffer, &iBufferOffset);
             }
 
             if (!iPrim.indices.empty()) {
@@ -522,16 +522,16 @@
                     continue;
                 }
                 const auto& oIndicesBuffer = buffers.at(iIndices.bufferView);
-                cmd.SetIndexBuffer(oIndicesBuffer, static_cast<uint32_t>(iIndices.byteOffset));
-                cmd.DrawElements(static_cast<uint32_t>(iIndices.count), 1, 0, 0);
+                pass.SetIndexBuffer(oIndicesBuffer, static_cast<uint32_t>(iIndices.byteOffset));
+                pass.DrawElements(static_cast<uint32_t>(iIndices.count), 1, 0, 0);
             } else {
                 // DrawArrays
-                cmd.DrawArrays(vertexCount, 1, 0, 0);
+                pass.DrawArrays(vertexCount, 1, 0, 0);
             }
         }
     }
 
-    void drawNode(dawn::CommandBufferBuilder& cmd, const tinygltf::Node& node, const glm::mat4& parent = glm::mat4()) {
+    void drawNode(dawn::RenderPassEncoder& pass, const tinygltf::Node& node, const glm::mat4& parent = glm::mat4()) {
         glm::mat4 model;
         if (node.matrix.size() == 16) {
             model = glm::make_mat4(node.matrix.data());
@@ -552,10 +552,10 @@
         model = parent * model;
 
         for (const auto& meshID : node.meshes) {
-            drawMesh(cmd, scene.meshes[meshID], model);
+            drawMesh(pass, scene.meshes[meshID], model);
         }
         for (const auto& child : node.children) {
-            drawNode(cmd, scene.nodes.at(child), model);
+            drawNode(pass, scene.nodes.at(child), model);
         }
     }
 
@@ -565,15 +565,17 @@
         GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
 
         const auto& defaultSceneNodes = scene.scenes.at(scene.defaultScene);
-        dawn::CommandBufferBuilder cmd = device.CreateCommandBufferBuilder()
-            .BeginRenderPass(renderPass)
-            .Clone();
-        for (const auto& n : defaultSceneNodes) {
-            const auto& node = scene.nodes.at(n);
-            drawNode(cmd, node);
+        dawn::CommandBufferBuilder builder = device.CreateCommandBufferBuilder();
+        {
+            dawn::RenderPassEncoder pass = builder.BeginRenderPass(renderPass);
+            for (const auto& n : defaultSceneNodes) {
+                const auto& node = scene.nodes.at(n);
+                drawNode(pass, node);
+            }
+            pass.EndPass();
         }
-        auto commands = cmd.EndRenderPass()
-            .GetResult();
+
+        dawn::CommandBuffer commands = builder.GetResult();
         queue.Submit(1, &commands);
 
         swapchain.Present(backbuffer);