From bd0c886281452c84017aa53d3bf4d4bc4a28ee49 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B3zef=20Kucia?= <jkucia@codeweavers.com>
Date: Thu, 7 Sep 2017 17:15:54 +0200
Subject: [PATCH] libs/vkd3d: Store push constant ranges in root signatures.

In prep for UAV counters support.
---
 libs/vkd3d/state.c         | 44 ++++++++++++++++++--------------------
 libs/vkd3d/vkd3d_private.h |  8 +++++--
 2 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/libs/vkd3d/state.c b/libs/vkd3d/state.c
index 90a63462..e5025a62 100644
--- a/libs/vkd3d/state.c
+++ b/libs/vkd3d/state.c
@@ -83,8 +83,8 @@ static void d3d12_root_signature_cleanup(struct d3d12_root_signature *root_signa
 
     if (root_signature->descriptor_mapping)
         vkd3d_free(root_signature->descriptor_mapping);
-    if (root_signature->push_constants)
-        vkd3d_free(root_signature->push_constants);
+    if (root_signature->root_constants)
+        vkd3d_free(root_signature->root_constants);
 
     for (i = 0; i < root_signature->static_sampler_count; ++i)
     {
@@ -492,10 +492,10 @@ static HRESULT d3d12_root_signature_init_descriptor_pool_size(struct d3d12_root_
 static HRESULT d3d12_root_signature_init_push_constants(struct d3d12_root_signature *root_signature,
         const D3D12_ROOT_SIGNATURE_DESC *desc, const struct d3d12_root_signature_info *info,
         struct VkPushConstantRange push_constants[D3D12_SHADER_VISIBILITY_PIXEL + 1],
-        uint32_t *push_constant_count)
+        uint32_t *push_constant_range_count)
 {
     uint32_t push_constants_offset[D3D12_SHADER_VISIBILITY_PIXEL + 1];
-    unsigned int i, j, push_count;
+    unsigned int i, j, push_constant_count;
     uint32_t offset;
 
     memset(push_constants, 0, (D3D12_SHADER_VISIBILITY_PIXEL + 1) * sizeof(*push_constants));
@@ -518,7 +518,7 @@ static HRESULT d3d12_root_signature_init_push_constants(struct d3d12_root_signat
          *   "Any two elements of pPushConstantRanges must not include the same
          *   stage in stageFlags".
          */
-        push_count = 1;
+        push_constant_count = 1;
         for (i = 0; i <= D3D12_SHADER_VISIBILITY_PIXEL; ++i)
         {
             if (i == D3D12_SHADER_VISIBILITY_ALL)
@@ -543,7 +543,7 @@ static HRESULT d3d12_root_signature_init_push_constants(struct d3d12_root_signat
                 ++j;
             }
         }
-        push_count = j;
+        push_constant_count = j;
     }
 
     for (i = 0, j = 0; i < desc->NumParameters; ++i)
@@ -561,25 +561,25 @@ static HRESULT d3d12_root_signature_init_push_constants(struct d3d12_root_signat
             return E_NOTIMPL;
         }
 
-        idx = push_count == 1 ? 0 : p->ShaderVisibility;
+        idx = push_constant_count == 1 ? 0 : p->ShaderVisibility;
         offset = push_constants_offset[idx];
         push_constants_offset[idx] += p->u.Constants.Num32BitValues * sizeof(uint32_t);
 
         root_signature->parameters[i].parameter_type = p->ParameterType;
-        root_constant->stage_flags = push_count == 1
+        root_constant->stage_flags = push_constant_count == 1
                 ? push_constants[0].stageFlags : stage_flags_from_visibility(p->ShaderVisibility);
         root_constant->offset = offset;
 
-        root_signature->push_constants[j].register_index = p->u.Constants.ShaderRegister;
-        root_signature->push_constants[j].shader_visibility
+        root_signature->root_constants[j].register_index = p->u.Constants.ShaderRegister;
+        root_signature->root_constants[j].shader_visibility
                 = vkd3d_shader_visibility_from_d3d12(p->ShaderVisibility);
-        root_signature->push_constants[j].offset = offset;
-        root_signature->push_constants[j].size = p->u.Constants.Num32BitValues * sizeof(uint32_t);
+        root_signature->root_constants[j].offset = offset;
+        root_signature->root_constants[j].size = p->u.Constants.Num32BitValues * sizeof(uint32_t);
 
         ++j;
     }
 
-    *push_constant_count = push_count;
+    *push_constant_range_count = push_constant_count;
 
     return S_OK;
 }
@@ -854,14 +854,11 @@ static HRESULT vkd3d_create_pipeline_layout(struct d3d12_device *device,
 static HRESULT d3d12_root_signature_init(struct d3d12_root_signature *root_signature,
         struct d3d12_device *device, const D3D12_ROOT_SIGNATURE_DESC *desc)
 {
-    /* Only a single push constant range may include the same stage in Vulkan. */
-    struct VkPushConstantRange push_constants[D3D12_SHADER_VISIBILITY_PIXEL + 1];
     const struct vkd3d_vulkan_info *vk_info = &device->vk_info;
     struct vkd3d_descriptor_set_context context;
     VkDescriptorSetLayoutBinding *binding_desc;
     struct d3d12_root_signature_info info;
     VkDescriptorSetLayout set_layouts[2];
-    uint32_t push_constant_count;
     HRESULT hr;
 
     memset(&context, 0, sizeof(context));
@@ -915,9 +912,9 @@ static HRESULT d3d12_root_signature_init(struct d3d12_root_signature *root_signa
     if (!(root_signature->descriptor_mapping = vkd3d_calloc(root_signature->descriptor_count,
             sizeof(*root_signature->descriptor_mapping))))
         goto fail;
-    root_signature->constant_count = info.root_constant_count;
-    if (!(root_signature->push_constants = vkd3d_calloc(root_signature->constant_count,
-            sizeof(*root_signature->push_constants))))
+    root_signature->root_constant_count = info.root_constant_count;
+    if (!(root_signature->root_constants = vkd3d_calloc(root_signature->root_constant_count,
+            sizeof(*root_signature->root_constants))))
         goto fail;
     if (!(root_signature->static_samplers = vkd3d_calloc(root_signature->static_sampler_count,
             sizeof(*root_signature->static_samplers))))
@@ -947,7 +944,7 @@ static HRESULT d3d12_root_signature_init(struct d3d12_root_signature *root_signa
     }
 
     if (FAILED(hr = d3d12_root_signature_init_push_constants(root_signature, desc, &info,
-            push_constants, &push_constant_count)))
+            root_signature->push_constant_ranges, &root_signature->push_constant_range_count)))
         goto fail;
     if (FAILED(hr = d3d12_root_signature_init_root_descriptor_tables(root_signature, desc, &context)))
         goto fail;
@@ -968,7 +965,8 @@ static HRESULT d3d12_root_signature_init(struct d3d12_root_signature *root_signa
     binding_desc = NULL;
 
     if (FAILED(hr = vkd3d_create_pipeline_layout(device, context.set_index, set_layouts,
-            push_constant_count, push_constants, &root_signature->vk_pipeline_layout)))
+            root_signature->push_constant_range_count, root_signature->push_constant_ranges,
+            &root_signature->vk_pipeline_layout)))
         goto fail;
 
     root_signature->device = device;
@@ -1177,8 +1175,8 @@ static HRESULT create_shader_stage(struct d3d12_device *device,
 
     shader_interface.bindings = root_signature->descriptor_mapping;
     shader_interface.binding_count = root_signature->descriptor_count;
-    shader_interface.push_constants = root_signature->push_constants;
-    shader_interface.push_constant_count = root_signature->constant_count;
+    shader_interface.push_constants = root_signature->root_constants;
+    shader_interface.push_constant_count = root_signature->root_constant_count;
     shader_interface.default_sampler = root_signature->default_sampler;
     if (FAILED(hr = vkd3d_shader_compile_dxbc(&dxbc, &spirv, 0, &shader_interface)))
     {
diff --git a/libs/vkd3d/vkd3d_private.h b/libs/vkd3d/vkd3d_private.h
index 7a9391a8..a9da4d52 100644
--- a/libs/vkd3d/vkd3d_private.h
+++ b/libs/vkd3d/vkd3d_private.h
@@ -343,8 +343,12 @@ struct d3d12_root_signature
     struct vkd3d_shader_resource_binding *descriptor_mapping;
     struct vkd3d_shader_descriptor_binding default_sampler;
 
-    unsigned int constant_count;
-    struct vkd3d_shader_push_constant *push_constants;
+    unsigned int root_constant_count;
+    struct vkd3d_shader_push_constant *root_constants;
+
+    unsigned int push_constant_range_count;
+    /* Only a single push constant range may include the same stage in Vulkan. */
+    VkPushConstantRange push_constant_ranges[D3D12_SHADER_VISIBILITY_PIXEL + 1];
 
     unsigned int static_sampler_count;
     VkSampler *static_samplers;