• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2019 Red Hat.
3  * Copyright © 2022 Collabora, LTD
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "vk_alloc.h"
26 #include "vk_cmd_enqueue_entrypoints.h"
27 #include "vk_command_buffer.h"
28 #include "vk_device.h"
29 #include "vk_pipeline_layout.h"
30 #include "vk_util.h"
31 
32 VKAPI_ATTR void VKAPI_CALL
vk_cmd_enqueue_CmdDrawMultiEXT(VkCommandBuffer commandBuffer,uint32_t drawCount,const VkMultiDrawInfoEXT * pVertexInfo,uint32_t instanceCount,uint32_t firstInstance,uint32_t stride)33 vk_cmd_enqueue_CmdDrawMultiEXT(VkCommandBuffer commandBuffer,
34                                uint32_t drawCount,
35                                const VkMultiDrawInfoEXT *pVertexInfo,
36                                uint32_t instanceCount,
37                                uint32_t firstInstance,
38                                uint32_t stride)
39 {
40    VK_FROM_HANDLE(vk_command_buffer, cmd_buffer, commandBuffer);
41 
42    struct vk_cmd_queue_entry *cmd =
43       vk_zalloc(cmd_buffer->cmd_queue.alloc, sizeof(*cmd), 8,
44                 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
45    if (!cmd)
46       return;
47 
48    cmd->type = VK_CMD_DRAW_MULTI_EXT;
49    list_addtail(&cmd->cmd_link, &cmd_buffer->cmd_queue.cmds);
50 
51    cmd->u.draw_multi_ext.draw_count = drawCount;
52    if (pVertexInfo) {
53       unsigned i = 0;
54       cmd->u.draw_multi_ext.vertex_info =
55          vk_zalloc(cmd_buffer->cmd_queue.alloc,
56                    sizeof(*cmd->u.draw_multi_ext.vertex_info) * drawCount, 8,
57                    VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
58 
59       vk_foreach_multi_draw(draw, i, pVertexInfo, drawCount, stride) {
60          memcpy(&cmd->u.draw_multi_ext.vertex_info[i], draw,
61                 sizeof(*cmd->u.draw_multi_ext.vertex_info));
62       }
63    }
64    cmd->u.draw_multi_ext.instance_count = instanceCount;
65    cmd->u.draw_multi_ext.first_instance = firstInstance;
66    cmd->u.draw_multi_ext.stride = stride;
67 }
68 
69 VKAPI_ATTR void VKAPI_CALL
vk_cmd_enqueue_CmdDrawMultiIndexedEXT(VkCommandBuffer commandBuffer,uint32_t drawCount,const VkMultiDrawIndexedInfoEXT * pIndexInfo,uint32_t instanceCount,uint32_t firstInstance,uint32_t stride,const int32_t * pVertexOffset)70 vk_cmd_enqueue_CmdDrawMultiIndexedEXT(VkCommandBuffer commandBuffer,
71                                       uint32_t drawCount,
72                                       const VkMultiDrawIndexedInfoEXT *pIndexInfo,
73                                       uint32_t instanceCount,
74                                       uint32_t firstInstance,
75                                       uint32_t stride,
76                                       const int32_t *pVertexOffset)
77 {
78    VK_FROM_HANDLE(vk_command_buffer, cmd_buffer, commandBuffer);
79 
80    struct vk_cmd_queue_entry *cmd =
81       vk_zalloc(cmd_buffer->cmd_queue.alloc, sizeof(*cmd), 8,
82                 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
83    if (!cmd)
84       return;
85 
86    cmd->type = VK_CMD_DRAW_MULTI_INDEXED_EXT;
87    list_addtail(&cmd->cmd_link, &cmd_buffer->cmd_queue.cmds);
88 
89    cmd->u.draw_multi_indexed_ext.draw_count = drawCount;
90 
91    if (pIndexInfo) {
92       unsigned i = 0;
93       cmd->u.draw_multi_indexed_ext.index_info =
94          vk_zalloc(cmd_buffer->cmd_queue.alloc,
95                    sizeof(*cmd->u.draw_multi_indexed_ext.index_info) * drawCount, 8,
96                    VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
97 
98       vk_foreach_multi_draw_indexed(draw, i, pIndexInfo, drawCount, stride) {
99          cmd->u.draw_multi_indexed_ext.index_info[i].firstIndex = draw->firstIndex;
100          cmd->u.draw_multi_indexed_ext.index_info[i].indexCount = draw->indexCount;
101          if (pVertexOffset == NULL)
102             cmd->u.draw_multi_indexed_ext.index_info[i].vertexOffset = draw->vertexOffset;
103       }
104    }
105 
106    cmd->u.draw_multi_indexed_ext.instance_count = instanceCount;
107    cmd->u.draw_multi_indexed_ext.first_instance = firstInstance;
108    cmd->u.draw_multi_indexed_ext.stride = stride;
109 
110    if (pVertexOffset) {
111       cmd->u.draw_multi_indexed_ext.vertex_offset =
112          vk_zalloc(cmd_buffer->cmd_queue.alloc,
113                    sizeof(*cmd->u.draw_multi_indexed_ext.vertex_offset), 8,
114                    VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
115 
116       memcpy(cmd->u.draw_multi_indexed_ext.vertex_offset, pVertexOffset,
117              sizeof(*cmd->u.draw_multi_indexed_ext.vertex_offset));
118    }
119 }
120 
121 VKAPI_ATTR void VKAPI_CALL
vk_cmd_enqueue_CmdPushDescriptorSetKHR(VkCommandBuffer commandBuffer,VkPipelineBindPoint pipelineBindPoint,VkPipelineLayout layout,uint32_t set,uint32_t descriptorWriteCount,const VkWriteDescriptorSet * pDescriptorWrites)122 vk_cmd_enqueue_CmdPushDescriptorSetKHR(VkCommandBuffer commandBuffer,
123                                        VkPipelineBindPoint pipelineBindPoint,
124                                        VkPipelineLayout layout,
125                                        uint32_t set,
126                                        uint32_t descriptorWriteCount,
127                                        const VkWriteDescriptorSet *pDescriptorWrites)
128 {
129    VK_FROM_HANDLE(vk_command_buffer, cmd_buffer, commandBuffer);
130    struct vk_cmd_push_descriptor_set_khr *pds;
131 
132    struct vk_cmd_queue_entry *cmd =
133       vk_zalloc(cmd_buffer->cmd_queue.alloc, sizeof(*cmd), 8,
134                 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
135    if (!cmd)
136       return;
137 
138    pds = &cmd->u.push_descriptor_set_khr;
139 
140    cmd->type = VK_CMD_PUSH_DESCRIPTOR_SET_KHR;
141    list_addtail(&cmd->cmd_link, &cmd_buffer->cmd_queue.cmds);
142 
143    pds->pipeline_bind_point = pipelineBindPoint;
144    pds->layout = layout;
145    pds->set = set;
146    pds->descriptor_write_count = descriptorWriteCount;
147 
148    if (pDescriptorWrites) {
149       pds->descriptor_writes =
150          vk_zalloc(cmd_buffer->cmd_queue.alloc,
151                    sizeof(*pds->descriptor_writes) * descriptorWriteCount, 8,
152                    VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
153       memcpy(pds->descriptor_writes,
154              pDescriptorWrites,
155              sizeof(*pds->descriptor_writes) * descriptorWriteCount);
156 
157       for (unsigned i = 0; i < descriptorWriteCount; i++) {
158          switch (pds->descriptor_writes[i].descriptorType) {
159          case VK_DESCRIPTOR_TYPE_SAMPLER:
160          case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
161          case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
162          case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
163          case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
164             pds->descriptor_writes[i].pImageInfo =
165                vk_zalloc(cmd_buffer->cmd_queue.alloc,
166                          sizeof(VkDescriptorImageInfo) * pds->descriptor_writes[i].descriptorCount, 8,
167                          VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
168             memcpy((VkDescriptorImageInfo *)pds->descriptor_writes[i].pImageInfo,
169                    pDescriptorWrites[i].pImageInfo,
170                    sizeof(VkDescriptorImageInfo) * pds->descriptor_writes[i].descriptorCount);
171             break;
172          case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
173          case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
174             pds->descriptor_writes[i].pTexelBufferView =
175                vk_zalloc(cmd_buffer->cmd_queue.alloc,
176                          sizeof(VkBufferView) * pds->descriptor_writes[i].descriptorCount, 8,
177                          VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
178             memcpy((VkBufferView *)pds->descriptor_writes[i].pTexelBufferView,
179                    pDescriptorWrites[i].pTexelBufferView,
180                    sizeof(VkBufferView) * pds->descriptor_writes[i].descriptorCount);
181             break;
182          case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
183          case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
184          case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
185          case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
186          default:
187             pds->descriptor_writes[i].pBufferInfo =
188                vk_zalloc(cmd_buffer->cmd_queue.alloc,
189                          sizeof(VkDescriptorBufferInfo) * pds->descriptor_writes[i].descriptorCount, 8,
190                          VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
191             memcpy((VkDescriptorBufferInfo *)pds->descriptor_writes[i].pBufferInfo,
192                    pDescriptorWrites[i].pBufferInfo,
193                    sizeof(VkDescriptorBufferInfo) * pds->descriptor_writes[i].descriptorCount);
194             break;
195          }
196       }
197    }
198 }
199 
200 static void
unref_pipeline_layout(struct vk_cmd_queue * queue,struct vk_cmd_queue_entry * cmd)201 unref_pipeline_layout(struct vk_cmd_queue *queue,
202                       struct vk_cmd_queue_entry *cmd)
203 {
204    struct vk_command_buffer *cmd_buffer =
205       container_of(queue, struct vk_command_buffer, cmd_queue);
206    VK_FROM_HANDLE(vk_pipeline_layout, layout,
207                   cmd->u.bind_descriptor_sets.layout);
208 
209    assert(cmd->type == VK_CMD_BIND_DESCRIPTOR_SETS);
210 
211    vk_pipeline_layout_unref(cmd_buffer->base.device, layout);
212 }
213 
214 VKAPI_ATTR void VKAPI_CALL
vk_cmd_enqueue_CmdBindDescriptorSets(VkCommandBuffer commandBuffer,VkPipelineBindPoint pipelineBindPoint,VkPipelineLayout layout,uint32_t firstSet,uint32_t descriptorSetCount,const VkDescriptorSet * pDescriptorSets,uint32_t dynamicOffsetCount,const uint32_t * pDynamicOffsets)215 vk_cmd_enqueue_CmdBindDescriptorSets(VkCommandBuffer commandBuffer,
216                                      VkPipelineBindPoint pipelineBindPoint,
217                                      VkPipelineLayout layout,
218                                      uint32_t firstSet,
219                                      uint32_t descriptorSetCount,
220                                      const VkDescriptorSet* pDescriptorSets,
221                                      uint32_t dynamicOffsetCount,
222                                      const uint32_t *pDynamicOffsets)
223 {
224    VK_FROM_HANDLE(vk_command_buffer, cmd_buffer, commandBuffer);
225 
226    struct vk_cmd_queue_entry *cmd =
227       vk_zalloc(cmd_buffer->cmd_queue.alloc, sizeof(*cmd), 8,
228                 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
229    if (!cmd)
230       return;
231 
232    cmd->type = VK_CMD_BIND_DESCRIPTOR_SETS;
233    list_addtail(&cmd->cmd_link, &cmd_buffer->cmd_queue.cmds);
234 
235    /* We need to hold a reference to the descriptor set as long as this
236     * command is in the queue.  Otherwise, it may get deleted out from under
237     * us before the command is replayed.
238     */
239    vk_pipeline_layout_ref(vk_pipeline_layout_from_handle(layout));
240    cmd->u.bind_descriptor_sets.layout = layout;
241    cmd->driver_free_cb = unref_pipeline_layout;
242 
243    cmd->u.bind_descriptor_sets.pipeline_bind_point = pipelineBindPoint;
244    cmd->u.bind_descriptor_sets.first_set = firstSet;
245    cmd->u.bind_descriptor_sets.descriptor_set_count = descriptorSetCount;
246    if (pDescriptorSets) {
247       cmd->u.bind_descriptor_sets.descriptor_sets =
248          vk_zalloc(cmd_buffer->cmd_queue.alloc,
249                    sizeof(*cmd->u.bind_descriptor_sets.descriptor_sets) * descriptorSetCount, 8,
250                    VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
251 
252       memcpy(cmd->u.bind_descriptor_sets.descriptor_sets, pDescriptorSets,
253              sizeof(*cmd->u.bind_descriptor_sets.descriptor_sets) * descriptorSetCount);
254    }
255    cmd->u.bind_descriptor_sets.dynamic_offset_count = dynamicOffsetCount;
256    if (pDynamicOffsets) {
257       cmd->u.bind_descriptor_sets.dynamic_offsets =
258          vk_zalloc(cmd_buffer->cmd_queue.alloc,
259                    sizeof(*cmd->u.bind_descriptor_sets.dynamic_offsets) * dynamicOffsetCount, 8,
260                    VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
261 
262       memcpy(cmd->u.bind_descriptor_sets.dynamic_offsets, pDynamicOffsets,
263              sizeof(*cmd->u.bind_descriptor_sets.dynamic_offsets) * dynamicOffsetCount);
264    }
265 }
266