Add RenderEncoderBase to match WebGPU class hierarchy.

This is needed so that RenderBundleEncoder and RenderPassEncoder can
share code. This patch also moves EndPass out of ProgrammablePassEncoder
and into both RenderPassEncoder and ComputePassEncoder. Render bundles
do not have EndPass.

Bug: dawn:154
Change-Id: Ib7126b2ba718b0b93e3d6f15c429ac910c0d5d31
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/9180
Commit-Queue: Austin Eng <enga@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index ead3c39..3f443c0 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -153,6 +153,8 @@
     "src/dawn_native/Queue.h",
     "src/dawn_native/RefCounted.cpp",
     "src/dawn_native/RefCounted.h",
+    "src/dawn_native/RenderEncoderBase.cpp",
+    "src/dawn_native/RenderEncoderBase.h",
     "src/dawn_native/RenderPassEncoder.cpp",
     "src/dawn_native/RenderPassEncoder.h",
     "src/dawn_native/RenderPipeline.cpp",
diff --git a/src/dawn_native/ComputePassEncoder.cpp b/src/dawn_native/ComputePassEncoder.cpp
index 708b2cb..e039241 100644
--- a/src/dawn_native/ComputePassEncoder.cpp
+++ b/src/dawn_native/ComputePassEncoder.cpp
@@ -39,6 +39,15 @@
         return new ComputePassEncoderBase(device, topLevelEncoder, ObjectBase::kError);
     }
 
+    void ComputePassEncoderBase::EndPass() {
+        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
+            return;
+        }
+
+        mTopLevelEncoder->PassEnded();
+        mAllocator = nullptr;
+    }
+
     void ComputePassEncoderBase::Dispatch(uint32_t x, uint32_t y, uint32_t z) {
         if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
             return;
diff --git a/src/dawn_native/ComputePassEncoder.h b/src/dawn_native/ComputePassEncoder.h
index 36a7e31..3e645bb 100644
--- a/src/dawn_native/ComputePassEncoder.h
+++ b/src/dawn_native/ComputePassEncoder.h
@@ -33,6 +33,8 @@
         static ComputePassEncoderBase* MakeError(DeviceBase* device,
                                                  CommandEncoderBase* topLevelEncoder);
 
+        void EndPass();
+
         void Dispatch(uint32_t x, uint32_t y, uint32_t z);
         void DispatchIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset);
         void SetPipeline(ComputePipelineBase* pipeline);
diff --git a/src/dawn_native/ProgrammablePassEncoder.cpp b/src/dawn_native/ProgrammablePassEncoder.cpp
index c46639a..da2f145 100644
--- a/src/dawn_native/ProgrammablePassEncoder.cpp
+++ b/src/dawn_native/ProgrammablePassEncoder.cpp
@@ -38,15 +38,6 @@
         : ObjectBase(device, errorTag), mTopLevelEncoder(topLevelEncoder), mAllocator(nullptr) {
     }
 
-    void ProgrammablePassEncoder::EndPass() {
-        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
-            return;
-        }
-
-        mTopLevelEncoder->PassEnded();
-        mAllocator = nullptr;
-    }
-
     void ProgrammablePassEncoder::InsertDebugMarker(const char* groupLabel) {
         if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
             return;
diff --git a/src/dawn_native/ProgrammablePassEncoder.h b/src/dawn_native/ProgrammablePassEncoder.h
index 0389447..e64592c 100644
--- a/src/dawn_native/ProgrammablePassEncoder.h
+++ b/src/dawn_native/ProgrammablePassEncoder.h
@@ -33,8 +33,6 @@
                                 CommandEncoderBase* topLevelEncoder,
                                 CommandAllocator* allocator);
 
-        void EndPass();
-
         void InsertDebugMarker(const char* groupLabel);
         void PopDebugGroup();
         void PushDebugGroup(const char* groupLabel);
diff --git a/src/dawn_native/RenderEncoderBase.cpp b/src/dawn_native/RenderEncoderBase.cpp
new file mode 100644
index 0000000..060e300
--- /dev/null
+++ b/src/dawn_native/RenderEncoderBase.cpp
@@ -0,0 +1,159 @@
+// Copyright 2019 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "dawn_native/RenderEncoderBase.h"
+
+#include "common/Constants.h"
+#include "dawn_native/Buffer.h"
+#include "dawn_native/CommandEncoder.h"
+#include "dawn_native/Commands.h"
+#include "dawn_native/Device.h"
+#include "dawn_native/RenderPipeline.h"
+
+#include <math.h>
+#include <string.h>
+
+namespace dawn_native {
+
+    RenderEncoderBase::RenderEncoderBase(DeviceBase* device,
+                                         CommandEncoderBase* topLevelEncoder,
+                                         CommandAllocator* allocator)
+        : ProgrammablePassEncoder(device, topLevelEncoder, allocator) {
+    }
+
+    RenderEncoderBase::RenderEncoderBase(DeviceBase* device,
+                                         CommandEncoderBase* topLevelEncoder,
+                                         ErrorTag errorTag)
+        : ProgrammablePassEncoder(device, topLevelEncoder, errorTag) {
+    }
+
+    void RenderEncoderBase::Draw(uint32_t vertexCount,
+                                 uint32_t instanceCount,
+                                 uint32_t firstVertex,
+                                 uint32_t firstInstance) {
+        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
+            return;
+        }
+
+        DrawCmd* draw = mAllocator->Allocate<DrawCmd>(Command::Draw);
+        draw->vertexCount = vertexCount;
+        draw->instanceCount = instanceCount;
+        draw->firstVertex = firstVertex;
+        draw->firstInstance = firstInstance;
+    }
+
+    void RenderEncoderBase::DrawIndexed(uint32_t indexCount,
+                                        uint32_t instanceCount,
+                                        uint32_t firstIndex,
+                                        int32_t baseVertex,
+                                        uint32_t firstInstance) {
+        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
+            return;
+        }
+
+        DrawIndexedCmd* draw = mAllocator->Allocate<DrawIndexedCmd>(Command::DrawIndexed);
+        draw->indexCount = indexCount;
+        draw->instanceCount = instanceCount;
+        draw->firstIndex = firstIndex;
+        draw->baseVertex = baseVertex;
+        draw->firstInstance = firstInstance;
+    }
+
+    void RenderEncoderBase::DrawIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset) {
+        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) ||
+            mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(indirectBuffer))) {
+            return;
+        }
+
+        if (indirectOffset >= indirectBuffer->GetSize() ||
+            indirectOffset + kDrawIndirectSize > indirectBuffer->GetSize()) {
+            mTopLevelEncoder->HandleError("Indirect offset out of bounds");
+            return;
+        }
+
+        DrawIndirectCmd* cmd = mAllocator->Allocate<DrawIndirectCmd>(Command::DrawIndirect);
+        cmd->indirectBuffer = indirectBuffer;
+        cmd->indirectOffset = indirectOffset;
+    }
+
+    void RenderEncoderBase::DrawIndexedIndirect(BufferBase* indirectBuffer,
+                                                uint64_t indirectOffset) {
+        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) ||
+            mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(indirectBuffer))) {
+            return;
+        }
+
+        if (indirectOffset >= indirectBuffer->GetSize() ||
+            indirectOffset + kDrawIndexedIndirectSize > indirectBuffer->GetSize()) {
+            mTopLevelEncoder->HandleError("Indirect offset out of bounds");
+            return;
+        }
+
+        DrawIndexedIndirectCmd* cmd =
+            mAllocator->Allocate<DrawIndexedIndirectCmd>(Command::DrawIndexedIndirect);
+        cmd->indirectBuffer = indirectBuffer;
+        cmd->indirectOffset = indirectOffset;
+    }
+
+    void RenderEncoderBase::SetPipeline(RenderPipelineBase* pipeline) {
+        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) ||
+            mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(pipeline))) {
+            return;
+        }
+
+        SetRenderPipelineCmd* cmd =
+            mAllocator->Allocate<SetRenderPipelineCmd>(Command::SetRenderPipeline);
+        cmd->pipeline = pipeline;
+    }
+
+    void RenderEncoderBase::SetIndexBuffer(BufferBase* buffer, uint64_t offset) {
+        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) ||
+            mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(buffer))) {
+            return;
+        }
+
+        SetIndexBufferCmd* cmd = mAllocator->Allocate<SetIndexBufferCmd>(Command::SetIndexBuffer);
+        cmd->buffer = buffer;
+        cmd->offset = offset;
+    }
+
+    void RenderEncoderBase::SetVertexBuffers(uint32_t startSlot,
+                                             uint32_t count,
+                                             BufferBase* const* buffers,
+                                             uint64_t const* offsets) {
+        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
+            return;
+        }
+
+        for (size_t i = 0; i < count; ++i) {
+            if (mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(buffers[i]))) {
+                return;
+            }
+        }
+
+        SetVertexBuffersCmd* cmd =
+            mAllocator->Allocate<SetVertexBuffersCmd>(Command::SetVertexBuffers);
+        cmd->startSlot = startSlot;
+        cmd->count = count;
+
+        Ref<BufferBase>* cmdBuffers = mAllocator->AllocateData<Ref<BufferBase>>(count);
+        for (size_t i = 0; i < count; ++i) {
+            cmdBuffers[i] = buffers[i];
+        }
+
+        uint64_t* cmdOffsets = mAllocator->AllocateData<uint64_t>(count);
+        memcpy(cmdOffsets, offsets, count * sizeof(uint64_t));
+    }
+
+}  // namespace dawn_native
diff --git a/src/dawn_native/RenderEncoderBase.h b/src/dawn_native/RenderEncoderBase.h
new file mode 100644
index 0000000..adeead3
--- /dev/null
+++ b/src/dawn_native/RenderEncoderBase.h
@@ -0,0 +1,67 @@
+// Copyright 2019 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef DAWNNATIVE_RENDERENCODERBASE_H_
+#define DAWNNATIVE_RENDERENCODERBASE_H_
+
+#include "dawn_native/Error.h"
+#include "dawn_native/ProgrammablePassEncoder.h"
+
+namespace dawn_native {
+
+    class RenderEncoderBase : public ProgrammablePassEncoder {
+      public:
+        RenderEncoderBase(DeviceBase* device,
+                          CommandEncoderBase* topLevelEncoder,
+                          CommandAllocator* allocator);
+
+        void Draw(uint32_t vertexCount,
+                  uint32_t instanceCount,
+                  uint32_t firstVertex,
+                  uint32_t firstInstance);
+        void DrawIndexed(uint32_t vertexCount,
+                         uint32_t instanceCount,
+                         uint32_t firstIndex,
+                         int32_t baseVertex,
+                         uint32_t firstInstance);
+
+        void DrawIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset);
+        void DrawIndexedIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset);
+
+        void SetPipeline(RenderPipelineBase* pipeline);
+
+        template <typename T>
+        void SetVertexBuffers(uint32_t startSlot,
+                              uint32_t count,
+                              T* const* buffers,
+                              uint64_t const* offsets) {
+            static_assert(std::is_base_of<BufferBase, T>::value, "");
+            SetVertexBuffers(startSlot, count, buffers, offsets);
+        }
+        void SetVertexBuffers(uint32_t startSlot,
+                              uint32_t count,
+                              BufferBase* const* buffers,
+                              uint64_t const* offsets);
+        void SetIndexBuffer(BufferBase* buffer, uint64_t offset);
+
+      protected:
+        // Construct an "error" render encoder base.
+        RenderEncoderBase(DeviceBase* device,
+                          CommandEncoderBase* topLevelEncoder,
+                          ErrorTag errorTag);
+    };
+
+}  // namespace dawn_native
+
+#endif  // DAWNNATIVE_RENDERENCODERBASE_H_
diff --git a/src/dawn_native/RenderPassEncoder.cpp b/src/dawn_native/RenderPassEncoder.cpp
index 026a71a..830a6b7 100644
--- a/src/dawn_native/RenderPassEncoder.cpp
+++ b/src/dawn_native/RenderPassEncoder.cpp
@@ -29,13 +29,13 @@
     RenderPassEncoderBase::RenderPassEncoderBase(DeviceBase* device,
                                                  CommandEncoderBase* topLevelEncoder,
                                                  CommandAllocator* allocator)
-        : ProgrammablePassEncoder(device, topLevelEncoder, allocator) {
+        : RenderEncoderBase(device, topLevelEncoder, allocator) {
     }
 
     RenderPassEncoderBase::RenderPassEncoderBase(DeviceBase* device,
                                                  CommandEncoderBase* topLevelEncoder,
                                                  ErrorTag errorTag)
-        : ProgrammablePassEncoder(device, topLevelEncoder, errorTag) {
+        : RenderEncoderBase(device, topLevelEncoder, errorTag) {
     }
 
     RenderPassEncoderBase* RenderPassEncoderBase::MakeError(DeviceBase* device,
@@ -43,83 +43,13 @@
         return new RenderPassEncoderBase(device, topLevelEncoder, ObjectBase::kError);
     }
 
-    void RenderPassEncoderBase::Draw(uint32_t vertexCount,
-                                     uint32_t instanceCount,
-                                     uint32_t firstVertex,
-                                     uint32_t firstInstance) {
+    void RenderPassEncoderBase::EndPass() {
         if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
             return;
         }
 
-        DrawCmd* draw = mAllocator->Allocate<DrawCmd>(Command::Draw);
-        draw->vertexCount = vertexCount;
-        draw->instanceCount = instanceCount;
-        draw->firstVertex = firstVertex;
-        draw->firstInstance = firstInstance;
-    }
-
-    void RenderPassEncoderBase::DrawIndexed(uint32_t indexCount,
-                                            uint32_t instanceCount,
-                                            uint32_t firstIndex,
-                                            int32_t baseVertex,
-                                            uint32_t firstInstance) {
-        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
-            return;
-        }
-
-        DrawIndexedCmd* draw = mAllocator->Allocate<DrawIndexedCmd>(Command::DrawIndexed);
-        draw->indexCount = indexCount;
-        draw->instanceCount = instanceCount;
-        draw->firstIndex = firstIndex;
-        draw->baseVertex = baseVertex;
-        draw->firstInstance = firstInstance;
-    }
-
-    void RenderPassEncoderBase::DrawIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset) {
-        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) ||
-            mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(indirectBuffer))) {
-            return;
-        }
-
-        if (indirectOffset >= indirectBuffer->GetSize() ||
-            indirectOffset + kDrawIndirectSize > indirectBuffer->GetSize()) {
-            mTopLevelEncoder->HandleError("Indirect offset out of bounds");
-            return;
-        }
-
-        DrawIndirectCmd* cmd = mAllocator->Allocate<DrawIndirectCmd>(Command::DrawIndirect);
-        cmd->indirectBuffer = indirectBuffer;
-        cmd->indirectOffset = indirectOffset;
-    }
-
-    void RenderPassEncoderBase::DrawIndexedIndirect(BufferBase* indirectBuffer,
-                                                    uint64_t indirectOffset) {
-        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) ||
-            mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(indirectBuffer))) {
-            return;
-        }
-
-        if (indirectOffset >= indirectBuffer->GetSize() ||
-            indirectOffset + kDrawIndexedIndirectSize > indirectBuffer->GetSize()) {
-            mTopLevelEncoder->HandleError("Indirect offset out of bounds");
-            return;
-        }
-
-        DrawIndexedIndirectCmd* cmd =
-            mAllocator->Allocate<DrawIndexedIndirectCmd>(Command::DrawIndexedIndirect);
-        cmd->indirectBuffer = indirectBuffer;
-        cmd->indirectOffset = indirectOffset;
-    }
-
-    void RenderPassEncoderBase::SetPipeline(RenderPipelineBase* pipeline) {
-        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) ||
-            mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(pipeline))) {
-            return;
-        }
-
-        SetRenderPipelineCmd* cmd =
-            mAllocator->Allocate<SetRenderPipelineCmd>(Command::SetRenderPipeline);
-        cmd->pipeline = pipeline;
+        mTopLevelEncoder->PassEnded();
+        mAllocator = nullptr;
     }
 
     void RenderPassEncoderBase::SetStencilReference(uint32_t reference) {
@@ -198,43 +128,4 @@
         cmd->height = height;
     }
 
-    void RenderPassEncoderBase::SetIndexBuffer(BufferBase* buffer, uint64_t offset) {
-        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) ||
-            mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(buffer))) {
-            return;
-        }
-
-        SetIndexBufferCmd* cmd = mAllocator->Allocate<SetIndexBufferCmd>(Command::SetIndexBuffer);
-        cmd->buffer = buffer;
-        cmd->offset = offset;
-    }
-
-    void RenderPassEncoderBase::SetVertexBuffers(uint32_t startSlot,
-                                                 uint32_t count,
-                                                 BufferBase* const* buffers,
-                                                 uint64_t const* offsets) {
-        if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
-            return;
-        }
-
-        for (size_t i = 0; i < count; ++i) {
-            if (mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(buffers[i]))) {
-                return;
-            }
-        }
-
-        SetVertexBuffersCmd* cmd =
-            mAllocator->Allocate<SetVertexBuffersCmd>(Command::SetVertexBuffers);
-        cmd->startSlot = startSlot;
-        cmd->count = count;
-
-        Ref<BufferBase>* cmdBuffers = mAllocator->AllocateData<Ref<BufferBase>>(count);
-        for (size_t i = 0; i < count; ++i) {
-            cmdBuffers[i] = buffers[i];
-        }
-
-        uint64_t* cmdOffsets = mAllocator->AllocateData<uint64_t>(count);
-        memcpy(cmdOffsets, offsets, count * sizeof(uint64_t));
-    }
-
 }  // namespace dawn_native
diff --git a/src/dawn_native/RenderPassEncoder.h b/src/dawn_native/RenderPassEncoder.h
index 8250464..ed947e5 100644
--- a/src/dawn_native/RenderPassEncoder.h
+++ b/src/dawn_native/RenderPassEncoder.h
@@ -16,7 +16,7 @@
 #define DAWNNATIVE_RENDERPASSENCODER_H_
 
 #include "dawn_native/Error.h"
-#include "dawn_native/ProgrammablePassEncoder.h"
+#include "dawn_native/RenderEncoderBase.h"
 
 namespace dawn_native {
 
@@ -24,7 +24,7 @@
     // is a pure frontend type to record in its parent CommandEncoder and never has a backend
     // implementation.
     // TODO(cwallez@chromium.org): Remove that generator limitation and rename to ComputePassEncoder
-    class RenderPassEncoderBase : public ProgrammablePassEncoder {
+    class RenderPassEncoderBase : public RenderEncoderBase {
       public:
         RenderPassEncoderBase(DeviceBase* device,
                               CommandEncoderBase* topLevelEncoder,
@@ -33,20 +33,7 @@
         static RenderPassEncoderBase* MakeError(DeviceBase* device,
                                                 CommandEncoderBase* topLevelEncoder);
 
-        void Draw(uint32_t vertexCount,
-                  uint32_t instanceCount,
-                  uint32_t firstVertex,
-                  uint32_t firstInstance);
-        void DrawIndexed(uint32_t vertexCount,
-                         uint32_t instanceCount,
-                         uint32_t firstIndex,
-                         int32_t baseVertex,
-                         uint32_t firstInstance);
-
-        void DrawIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset);
-        void DrawIndexedIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset);
-
-        void SetPipeline(RenderPipelineBase* pipeline);
+        void EndPass();
 
         void SetStencilReference(uint32_t reference);
         void SetBlendColor(const Color* color);
@@ -58,20 +45,6 @@
                          float maxDepth);
         void SetScissorRect(uint32_t x, uint32_t y, uint32_t width, uint32_t height);
 
-        template <typename T>
-        void SetVertexBuffers(uint32_t startSlot,
-                              uint32_t count,
-                              T* const* buffers,
-                              uint64_t const* offsets) {
-            static_assert(std::is_base_of<BufferBase, T>::value, "");
-            SetVertexBuffers(startSlot, count, buffers, offsets);
-        }
-        void SetVertexBuffers(uint32_t startSlot,
-                              uint32_t count,
-                              BufferBase* const* buffers,
-                              uint64_t const* offsets);
-        void SetIndexBuffer(BufferBase* buffer, uint64_t offset);
-
       protected:
         RenderPassEncoderBase(DeviceBase* device,
                               CommandEncoderBase* topLevelEncoder,