From f4844548543f14e743c522e935310cb23ba8c43c Mon Sep 17 00:00:00 2001
From: Philip Rebohle <philip.rebohle@tu-dortmund.de>
Date: Sat, 9 Dec 2017 15:57:05 +0100
Subject: [PATCH] [d3d11] Device children actually do hold a reference to the
 device

---
 src/d3d11/d3d11_buffer.cpp          |  2 +-
 src/d3d11/d3d11_buffer.h            |  2 +-
 src/d3d11/d3d11_context.cpp         | 26 +++++++++++++++++---------
 src/d3d11/d3d11_device.cpp          |  6 +++++-
 src/d3d11/d3d11_input_layout.cpp    |  2 +-
 src/d3d11/d3d11_input_layout.h      |  2 +-
 src/d3d11/d3d11_shader.h            |  6 +++---
 src/d3d11/d3d11_texture.cpp         |  2 +-
 src/d3d11/d3d11_texture.h           |  2 +-
 src/d3d11/d3d11_view.h              |  4 ++--
 tests/d3d11/test_d3d11_triangle.cpp |  2 +-
 11 files changed, 34 insertions(+), 22 deletions(-)

diff --git a/src/d3d11/d3d11_buffer.cpp b/src/d3d11/d3d11_buffer.cpp
index 63b898ecb..40517ad9a 100644
--- a/src/d3d11/d3d11_buffer.cpp
+++ b/src/d3d11/d3d11_buffer.cpp
@@ -35,7 +35,7 @@ namespace dxvk {
   
   
   void D3D11Buffer::GetDevice(ID3D11Device** ppDevice) {
-    *ppDevice = ref(m_device);
+    *ppDevice = m_device.ref();
   }
   
   
diff --git a/src/d3d11/d3d11_buffer.h b/src/d3d11/d3d11_buffer.h
index 9a8c69176..f3db42c53 100644
--- a/src/d3d11/d3d11_buffer.h
+++ b/src/d3d11/d3d11_buffer.h
@@ -41,7 +41,7 @@ namespace dxvk {
     
   private:
     
-    D3D11Device* const              m_device;
+    Com<D3D11Device>                m_device;
     Com<IDXGIBufferResourcePrivate> m_resource;
     D3D11_BUFFER_DESC               m_desc;
     
diff --git a/src/d3d11/d3d11_context.cpp b/src/d3d11/d3d11_context.cpp
index 897fe724e..b80308e4b 100644
--- a/src/d3d11/d3d11_context.cpp
+++ b/src/d3d11/d3d11_context.cpp
@@ -111,8 +111,8 @@ namespace dxvk {
   void D3D11DeviceContext::ClearState() {
     this->IASetInputLayout(nullptr);
     this->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED);
-//     this->IASetVertexBuffers(0, D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT, nullptr, nullptr, nullptr);
-//     this->IASetIndexBuffer(nullptr, DXGI_FORMAT_UNKNOWN, 0);
+    this->IASetVertexBuffers(0, D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT, nullptr, nullptr, nullptr);
+    this->IASetIndexBuffer(nullptr, DXGI_FORMAT_UNKNOWN, 0);
     
     this->VSSetShader(nullptr, nullptr, 0);
     this->VSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, nullptr);
@@ -625,11 +625,17 @@ namespace dxvk {
     // are bound as UAVs or stream outputs
     for (uint32_t i = 0; i < NumBuffers; i++) {
       D3D11VertexBufferBinding binding;
-      binding.buffer = static_cast<D3D11Buffer*>(ppVertexBuffers[i]);
-      binding.offset = pOffsets[i];
-      binding.stride = pStrides[i];
+      binding.buffer = nullptr;
+      binding.offset = 0;
+      binding.stride = 0;
       m_state.ia.vertexBuffers.at(StartSlot + i) = binding;
       
+      if (ppVertexBuffers != nullptr) {
+        binding.buffer = static_cast<D3D11Buffer*>(ppVertexBuffers[i]);
+        binding.offset = pOffsets[i];
+        binding.stride = pStrides[i];
+      }
+      
       DxvkBufferBinding dxvkBinding;
       
       if (binding.buffer != nullptr) {
@@ -673,10 +679,12 @@ namespace dxvk {
     // formats are allowed.
     VkIndexType indexType = VK_INDEX_TYPE_UINT32;
     
-    switch (binding.format) {
-      case DXGI_FORMAT_R16_UINT: indexType = VK_INDEX_TYPE_UINT16; break;
-      case DXGI_FORMAT_R32_UINT: indexType = VK_INDEX_TYPE_UINT32; break;
-      default: Logger::err(str::format("D3D11: Invalid index format: ", binding.format));
+    if (binding.buffer != nullptr) {
+      switch (binding.format) {
+        case DXGI_FORMAT_R16_UINT: indexType = VK_INDEX_TYPE_UINT16; break;
+        case DXGI_FORMAT_R32_UINT: indexType = VK_INDEX_TYPE_UINT32; break;
+        default: Logger::err(str::format("D3D11: Invalid index format: ", binding.format));
+      }
     }
     
     m_context->bindIndexBuffer(
diff --git a/src/d3d11/d3d11_device.cpp b/src/d3d11/d3d11_device.cpp
index 0f695d584..2a3d97a1c 100644
--- a/src/d3d11/d3d11_device.cpp
+++ b/src/d3d11/d3d11_device.cpp
@@ -21,7 +21,6 @@ namespace dxvk {
     m_featureFlags  (featureFlags),
     m_dxvkDevice    (m_dxgiDevice->GetDXVKDevice()),
     m_dxvkAdapter   (m_dxvkDevice->adapter()) {
-    
     Com<IDXGIAdapter> adapter;
     
     if (FAILED(m_dxgiDevice->GetAdapter(&adapter))
@@ -56,6 +55,7 @@ namespace dxvk {
       return m_presentDevice->QueryInterface(riid, ppvObject);
     
     Logger::warn("D3D11Device::QueryInterface: Unknown interface query");
+    Logger::warn(str::format(riid));
     return E_NOINTERFACE;
   }
     
@@ -247,6 +247,10 @@ namespace dxvk {
     // Shader resource views can access pretty much anything
     D3D11_RESOURCE_DIMENSION resourceDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
     pResource->GetType(&resourceDim);
+    
+    if (ppSRView != nullptr)
+      *ppSRView = nullptr;
+    
     Logger::err("D3D11Device::CreateShaderResourceView: Not implemented");
     return E_NOTIMPL;
   }
diff --git a/src/d3d11/d3d11_input_layout.cpp b/src/d3d11/d3d11_input_layout.cpp
index 9652a04ed..4eac95e2c 100644
--- a/src/d3d11/d3d11_input_layout.cpp
+++ b/src/d3d11/d3d11_input_layout.cpp
@@ -37,7 +37,7 @@ namespace dxvk {
   
   
   void D3D11InputLayout::GetDevice(ID3D11Device** ppDevice) {
-    *ppDevice = ref(m_device);
+    *ppDevice = m_device.ref();
   }
   
   
diff --git a/src/d3d11/d3d11_input_layout.h b/src/d3d11/d3d11_input_layout.h
index 2b3ee5236..038027c14 100644
--- a/src/d3d11/d3d11_input_layout.h
+++ b/src/d3d11/d3d11_input_layout.h
@@ -31,7 +31,7 @@ namespace dxvk {
     
   private:
     
-    D3D11Device* const  m_device;
+    Com<D3D11Device> m_device;
     
     std::vector<DxvkVertexAttribute> m_attributes;
     std::vector<DxvkVertexBinding>   m_bindings;
diff --git a/src/d3d11/d3d11_shader.h b/src/d3d11/d3d11_shader.h
index a9c39e134..7b80464e2 100644
--- a/src/d3d11/d3d11_shader.h
+++ b/src/d3d11/d3d11_shader.h
@@ -76,7 +76,7 @@ namespace dxvk {
     }
     
     void GetDevice(ID3D11Device **ppDevice) final {
-      *ppDevice = ref(m_device);
+      *ppDevice = m_device.ref();
     }
     
     Rc<DxvkShader> GetShader() const {
@@ -85,8 +85,8 @@ namespace dxvk {
     
   private:
     
-    D3D11Device* const m_device;
-    D3D11ShaderModule  m_module;
+    Com<D3D11Device>  m_device;
+    D3D11ShaderModule m_module;
     
   };
   
diff --git a/src/d3d11/d3d11_texture.cpp b/src/d3d11/d3d11_texture.cpp
index 0616eb158..a33250ff3 100644
--- a/src/d3d11/d3d11_texture.cpp
+++ b/src/d3d11/d3d11_texture.cpp
@@ -35,7 +35,7 @@ namespace dxvk {
   
     
   void D3D11Texture2D::GetDevice(ID3D11Device** ppDevice) {
-    *ppDevice = ref(m_device);
+    *ppDevice = m_device.ref();
   }
   
   
diff --git a/src/d3d11/d3d11_texture.h b/src/d3d11/d3d11_texture.h
index 26ba15060..623542a86 100644
--- a/src/d3d11/d3d11_texture.h
+++ b/src/d3d11/d3d11_texture.h
@@ -41,7 +41,7 @@ namespace dxvk {
     
   private:
     
-    D3D11Device* const              m_device;
+    Com<D3D11Device>                m_device;
     Com<IDXGIImageResourcePrivate>  m_resource;
     D3D11_TEXTURE2D_DESC            m_desc;
     
diff --git a/src/d3d11/d3d11_view.h b/src/d3d11/d3d11_view.h
index 52323262d..de845ede8 100644
--- a/src/d3d11/d3d11_view.h
+++ b/src/d3d11/d3d11_view.h
@@ -42,7 +42,7 @@ namespace dxvk {
     }
     
     void GetDevice(ID3D11Device** ppDevice) final {
-      *ppDevice = ref(m_device);
+      *ppDevice = m_device.ref();
     }
     
     void GetResource(ID3D11Resource** ppResource) final {
@@ -63,7 +63,7 @@ namespace dxvk {
     
   private:
     
-    D3D11Device* const  m_device;
+    Com<D3D11Device>    m_device;
     Com<ID3D11Resource> m_resource;
     DescType            m_desc;
     Rc<DxvkBufferView>  m_bufferView;
diff --git a/tests/d3d11/test_d3d11_triangle.cpp b/tests/d3d11/test_d3d11_triangle.cpp
index c60d99927..16eb4b229 100644
--- a/tests/d3d11/test_d3d11_triangle.cpp
+++ b/tests/d3d11/test_d3d11_triangle.cpp
@@ -175,7 +175,7 @@ public:
   
   
   ~TriangleApp() {
-    
+    m_context->ClearState();
   }