d3d11: enable BindGroupTests

The cases can pass now with a few fixes.

Bug: dawn:1776
Bug: dawn:1705
Change-Id: I4a38887c51d003b4e9b782fd9217c9ce2c7dd423
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/128980
Commit-Queue: Jie A Chen <jie.a.chen@intel.com>
Reviewed-by: Austin Eng <enga@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/dawn/native/d3d11/BufferD3D11.cpp b/src/dawn/native/d3d11/BufferD3D11.cpp
index 69af90d..5afb0b7 100644
--- a/src/dawn/native/d3d11/BufferD3D11.cpp
+++ b/src/dawn/native/d3d11/BufferD3D11.cpp
@@ -319,10 +319,10 @@
 
     D3D11_SHADER_RESOURCE_VIEW_DESC desc;
     desc.Format = DXGI_FORMAT_R32_TYPELESS;
-    desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
-    desc.Buffer.FirstElement = firstElement;
-    desc.Buffer.NumElements = numElements;
-
+    desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX;
+    desc.BufferEx.FirstElement = firstElement;
+    desc.BufferEx.NumElements = numElements;
+    desc.BufferEx.Flags = D3D11_BUFFEREX_SRV_FLAG_RAW;
     ComPtr<ID3D11ShaderResourceView> srv;
     DAWN_TRY(CheckHRESULT(ToBackend(GetDevice())
                               ->GetD3D11Device()
diff --git a/src/dawn/native/d3d11/CommandBufferD3D11.cpp b/src/dawn/native/d3d11/CommandBufferD3D11.cpp
index f2bf432..b54ef84 100644
--- a/src/dawn/native/d3d11/CommandBufferD3D11.cpp
+++ b/src/dawn/native/d3d11/CommandBufferD3D11.cpp
@@ -105,10 +105,12 @@
                             // Offset and size are measured in shader constants, which are 16 bytes
                             // (4*32-bit components). And the offsets and counts must be multiples
                             // of 16.
-                            ASSERT(IsAligned(offset, 256));
-                            UINT firstConstant = static_cast<UINT>(offset / 16);
-                            UINT size = static_cast<UINT>(binding.size / 16);
-                            UINT numConstants = Align(size, 16);
+                            DAWN_ASSERT(IsAligned(offset, 256));
+                            uint32_t firstConstant = static_cast<uint32_t>(offset / 16);
+                            uint32_t size = static_cast<uint32_t>(Align(binding.size, 16) / 16);
+                            uint32_t numConstants = Align(size, 16);
+                            DAWN_ASSERT(offset + numConstants * 16 <=
+                                        binding.buffer->GetAllocatedSize());
 
                             if (bindingInfo.visibility & wgpu::ShaderStage::Vertex) {
                                 deviceContext->VSSetConstantBuffers1(bindingSlot, 1, &d3d11Buffer,
@@ -146,7 +148,7 @@
                             ComPtr<ID3D11ShaderResourceView> d3d11SRV;
                             DAWN_TRY_ASSIGN(d3d11SRV, ToBackend(binding.buffer)
                                                           ->CreateD3D11ShaderResourceView(
-                                                              binding.offset, binding.size));
+                                                              offset, binding.size));
                             if (bindingInfo.visibility & wgpu::ShaderStage::Vertex) {
                                 deviceContext->VSSetShaderResources(bindingSlot, 1,
                                                                     d3d11SRV.GetAddressOf());
diff --git a/src/dawn/tests/end2end/BindGroupTests.cpp b/src/dawn/tests/end2end/BindGroupTests.cpp
index d1d23cc..999023a 100644
--- a/src/dawn/tests/end2end/BindGroupTests.cpp
+++ b/src/dawn/tests/end2end/BindGroupTests.cpp
@@ -241,6 +241,8 @@
 // shader. In D3D12 for example, these different types of bindings end up in different namespaces,
 // but the register offsets used must match between the shader module and descriptor range.
 TEST_P(BindGroupTests, UBOSamplerAndTexture) {
+    // TODO(dawn:1768): enable this test once computer shader B2T is supported.
+    DAWN_SUPPRESS_TEST_IF(IsD3D11());
     utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize);
 
     wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"(
@@ -992,6 +994,9 @@
 // Regression test for crbug.com/dawn/408 where dynamic offsets were applied in the wrong order.
 // Dynamic offsets should be applied in increasing order of binding number.
 TEST_P(BindGroupTests, DynamicOffsetOrder) {
+    // TODO(dawn:1776): Fix the UpdateSubresource1 16-byte alignment.
+    DAWN_SUPPRESS_TEST_IF(IsD3D11());
+
     // We will put the following values and the respective offsets into a buffer.
     // The test will ensure that the correct dynamic offset is applied to each buffer by reading the
     // value from an offset binding.
@@ -1077,6 +1082,9 @@
     // // TODO(crbug.com/dawn/1106): Test output is wrong on D3D12 using WARP.
     DAWN_SUPPRESS_TEST_IF(IsWARP());
 
+    // TODO(dawn:1776): Fix the UpdateSubresource1 16-byte alignment.
+    DAWN_SUPPRESS_TEST_IF(IsD3D11());
+
     auto RunTestWith = [&](bool dynamicBufferFirst) {
         uint32_t dynamicBufferBindingNumber = dynamicBufferFirst ? 0 : 1;
         uint32_t bufferBindingNumber = dynamicBufferFirst ? 1 : 0;
@@ -1507,6 +1515,7 @@
 }
 
 DAWN_INSTANTIATE_TEST(BindGroupTests,
+                      D3D11Backend(),
                       D3D12Backend(),
                       MetalBackend(),
                       OpenGLBackend(),