d3d11: implement ComputePipeline

Bug: dawn:1705
Change-Id: I915000a747cc913ed52a423940d20e169b829315
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/126664
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Peng Huang <penghuang@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/dawn/native/BUILD.gn b/src/dawn/native/BUILD.gn
index a9c3ae6..2beee92 100644
--- a/src/dawn/native/BUILD.gn
+++ b/src/dawn/native/BUILD.gn
@@ -433,6 +433,8 @@
       "d3d11/BindGroupLayoutD3D11.h",
       "d3d11/CommandRecordingContextD3D11.cpp",
       "d3d11/CommandRecordingContextD3D11.h",
+      "d3d11/ComputePipelineD3D11.cpp",
+      "d3d11/ComputePipelineD3D11.h",
       "d3d11/D3D11Backend.cpp",
       "d3d11/DeviceD3D11.cpp",
       "d3d11/DeviceD3D11.h",
diff --git a/src/dawn/native/CMakeLists.txt b/src/dawn/native/CMakeLists.txt
index 533d63d..6e5a6a0 100644
--- a/src/dawn/native/CMakeLists.txt
+++ b/src/dawn/native/CMakeLists.txt
@@ -290,6 +290,8 @@
         "d3d11/BindGroupLayoutD3D11.h"
         "d3d11/CommandRecordingContextD3D11.cpp"
         "d3d11/CommandRecordingContextD3D11.h"
+        "d3d11/ComputePipelineD3D11.cpp"
+        "d3d11/ComputePipelineD3D11.h"
         "d3d11/D3D11Backend.cpp"
         "d3d11/DeviceD3D11.cpp"
         "d3d11/DeviceD3D11.h"
diff --git a/src/dawn/native/d3d11/ComputePipelineD3D11.cpp b/src/dawn/native/d3d11/ComputePipelineD3D11.cpp
new file mode 100644
index 0000000..8744a0f
--- /dev/null
+++ b/src/dawn/native/d3d11/ComputePipelineD3D11.cpp
@@ -0,0 +1,88 @@
+// Copyright 2023 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/d3d11/ComputePipelineD3D11.h"
+
+#include <memory>
+#include <utility>
+
+#include "dawn/native/CreatePipelineAsyncTask.h"
+#include "dawn/native/d3d/D3DError.h"
+#include "dawn/native/d3d11/DeviceD3D11.h"
+#include "dawn/native/d3d11/ShaderModuleD3D11.h"
+
+namespace dawn::native::d3d11 {
+
+// static
+Ref<ComputePipeline> ComputePipeline::CreateUninitialized(
+    Device* device,
+    const ComputePipelineDescriptor* descriptor) {
+    return AcquireRef(new ComputePipeline(device, descriptor));
+}
+
+ComputePipeline::~ComputePipeline() = default;
+
+void ComputePipeline::DestroyImpl() {
+    ComputePipelineBase::DestroyImpl();
+}
+
+MaybeError ComputePipeline::Initialize() {
+    Device* device = ToBackend(GetDevice());
+    uint32_t compileFlags = 0;
+
+    if (!device->IsToggleEnabled(Toggle::UseDXC) &&
+        !device->IsToggleEnabled(Toggle::FxcOptimizations)) {
+        compileFlags |= D3DCOMPILE_OPTIMIZATION_LEVEL0;
+    }
+
+    if (device->IsToggleEnabled(Toggle::EmitHLSLDebugSymbols)) {
+        compileFlags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
+    }
+
+    // Tint does matrix multiplication expecting row major matrices
+    compileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
+
+    // FXC can miscompile code that depends on special float values (NaN, INF, etc) when IEEE
+    // strictness is not enabled. See crbug.com/tint/976.
+    compileFlags |= D3DCOMPILE_IEEE_STRICTNESS;
+
+    const ProgrammableStage& programmableStage = GetStage(SingleShaderStage::Compute);
+
+    d3d::CompiledShader compiledShader;
+    DAWN_TRY_ASSIGN(compiledShader, ToBackend(programmableStage.module)
+                                        ->Compile(programmableStage, SingleShaderStage::Compute,
+                                                  ToBackend(GetLayout()), compileFlags));
+    DAWN_TRY(CheckHRESULT(device->GetD3D11Device()->CreateComputeShader(
+                              compiledShader.shaderBlob.Data(), compiledShader.shaderBlob.Size(),
+                              nullptr, &mComputeShader),
+                          "D3D11 create compute shader"));
+
+    return {};
+}
+
+void ComputePipeline::ApplyNow(CommandRecordingContext* commandContext) {
+    ID3D11DeviceContext1* d3dDeviceContext1 = commandContext->GetD3D11DeviceContext1();
+    d3dDeviceContext1->CSSetShader(mComputeShader.Get(), nullptr, 0);
+}
+
+void ComputePipeline::InitializeAsync(Ref<ComputePipelineBase> computePipeline,
+                                      WGPUCreateComputePipelineAsyncCallback callback,
+                                      void* userdata) {
+    std::unique_ptr<CreateComputePipelineAsyncTask> asyncTask =
+        std::make_unique<CreateComputePipelineAsyncTask>(std::move(computePipeline), callback,
+                                                         userdata);
+    CreateComputePipelineAsyncTask::RunAsync(std::move(asyncTask));
+}
+
+}  // namespace dawn::native::d3d11
diff --git a/src/dawn/native/d3d11/ComputePipelineD3D11.h b/src/dawn/native/d3d11/ComputePipelineD3D11.h
new file mode 100644
index 0000000..7da11e8
--- /dev/null
+++ b/src/dawn/native/d3d11/ComputePipelineD3D11.h
@@ -0,0 +1,49 @@
+// Copyright 2023 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 SRC_DAWN_NATIVE_D3D11_COMPUTEPIPELINEGL_H_
+#define SRC_DAWN_NATIVE_D3D11_COMPUTEPIPELINEGL_H_
+
+#include "dawn/native/ComputePipeline.h"
+
+#include "dawn/native/d3d/d3d_platform.h"
+
+namespace dawn::native::d3d11 {
+
+class CommandRecordingContext;
+class Device;
+
+class ComputePipeline final : public ComputePipelineBase {
+  public:
+    static Ref<ComputePipeline> CreateUninitialized(Device* device,
+                                                    const ComputePipelineDescriptor* descriptor);
+    static void InitializeAsync(Ref<ComputePipelineBase> computePipeline,
+                                WGPUCreateComputePipelineAsyncCallback callback,
+                                void* userdata);
+
+    void ApplyNow(CommandRecordingContext* commandContext);
+
+    MaybeError Initialize() override;
+
+  private:
+    using ComputePipelineBase::ComputePipelineBase;
+    ~ComputePipeline() override;
+    void DestroyImpl() override;
+
+    ComPtr<ID3D11ComputeShader> mComputeShader;
+};
+
+}  // namespace dawn::native::d3d11
+
+#endif  // SRC_DAWN_NATIVE_D3D11_COMPUTEPIPELINEGL_H_
diff --git a/src/dawn/native/d3d11/DeviceD3D11.cpp b/src/dawn/native/d3d11/DeviceD3D11.cpp
index d24902b..1c3a9ec 100644
--- a/src/dawn/native/d3d11/DeviceD3D11.cpp
+++ b/src/dawn/native/d3d11/DeviceD3D11.cpp
@@ -32,6 +32,7 @@
 #include "dawn/native/d3d11/BackendD3D11.h"
 #include "dawn/native/d3d11/BindGroupD3D11.h"
 #include "dawn/native/d3d11/BindGroupLayoutD3D11.h"
+#include "dawn/native/d3d11/ComputePipelineD3D11.h"
 #include "dawn/native/d3d11/PipelineLayoutD3D11.h"
 #include "dawn/native/d3d11/PlatformFunctionsD3D11.h"
 #include "dawn/native/d3d11/QueueD3D11.h"
@@ -255,7 +256,7 @@
 
 Ref<ComputePipelineBase> Device::CreateUninitializedComputePipelineImpl(
     const ComputePipelineDescriptor* descriptor) {
-    return nullptr;
+    return ComputePipeline::CreateUninitialized(this, descriptor);
 }
 
 ResultOrError<Ref<PipelineLayoutBase>> Device::CreatePipelineLayoutImpl(
@@ -307,7 +308,9 @@
 
 void Device::InitializeComputePipelineAsyncImpl(Ref<ComputePipelineBase> computePipeline,
                                                 WGPUCreateComputePipelineAsyncCallback callback,
-                                                void* userdata) {}
+                                                void* userdata) {
+    ComputePipeline::InitializeAsync(std::move(computePipeline), callback, userdata);
+}
 
 void Device::InitializeRenderPipelineAsyncImpl(Ref<RenderPipelineBase> renderPipeline,
                                                WGPUCreateRenderPipelineAsyncCallback callback,
diff --git a/src/dawn/native/d3d11/TextureD3D11.h b/src/dawn/native/d3d11/TextureD3D11.h
index 2476fe0..725e9b5 100644
--- a/src/dawn/native/d3d11/TextureD3D11.h
+++ b/src/dawn/native/d3d11/TextureD3D11.h
@@ -40,13 +40,8 @@
     DXGI_FORMAT GetD3D11CopyableSubresourceFormat(Aspect aspect) const;
 
     D3D11_RENDER_TARGET_VIEW_DESC GetRTVDescriptor(const Format& format,
-                                                   uint32_t mipLevel,
-                                                   uint32_t baseSlice,
-                                                   uint32_t sliceCount) const;
-    D3D11_DEPTH_STENCIL_VIEW_DESC GetDSVDescriptor(uint32_t mipLevel,
-                                                   uint32_t baseArrayLayer,
-                                                   uint32_t layerCount,
-                                                   Aspect aspects,
+                                                   const SubresourceRange& range) const;
+    D3D11_DEPTH_STENCIL_VIEW_DESC GetDSVDescriptor(const SubresourceRange& range,
                                                    bool depthReadOnly,
                                                    bool stencilReadOnly) const;
 
diff --git a/src/dawn/native/d3d12/ComputePipelineD3D12.cpp b/src/dawn/native/d3d12/ComputePipelineD3D12.cpp
index 9c6845a..db3de1b 100644
--- a/src/dawn/native/d3d12/ComputePipelineD3D12.cpp
+++ b/src/dawn/native/d3d12/ComputePipelineD3D12.cpp
@@ -47,7 +47,7 @@
         compileFlags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
     }
 
-    // SPRIV-cross does matrix multiplication expecting row major matrices
+    // Tint does matrix multiplication expecting row major matrices
     compileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
 
     // FXC can miscompile code that depends on special float values (NaN, INF, etc) when IEEE
diff --git a/src/dawn/native/d3d12/RenderPipelineD3D12.cpp b/src/dawn/native/d3d12/RenderPipelineD3D12.cpp
index ab9cbb1..c25bc02 100644
--- a/src/dawn/native/d3d12/RenderPipelineD3D12.cpp
+++ b/src/dawn/native/d3d12/RenderPipelineD3D12.cpp
@@ -367,7 +367,7 @@
         compileFlags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
     }
 
-    // SPRIV-cross does matrix multiplication expecting row major matrices
+    // Tint does matrix multiplication expecting row major matrices
     compileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
 
     // FXC can miscompile code that depends on special float values (NaN, INF, etc) when IEEE