anv: set command buffer to NULL when allocations fail
[mesa.git] / src / intel / vulkan / anv_cmd_buffer.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 #include "vk_format_info.h"
33
34 /** \file anv_cmd_buffer.c
35 *
36 * This file contains all of the stuff for emitting commands into a command
37 * buffer. This includes implementations of most of the vkCmd*
38 * entrypoints. This file is concerned entirely with state emission and
39 * not with the command buffer data structure itself. As far as this file
40 * is concerned, most of anv_cmd_buffer is magic.
41 */
42
43 /* TODO: These are taken from GLES. We should check the Vulkan spec */
44 const struct anv_dynamic_state default_dynamic_state = {
45 .viewport = {
46 .count = 0,
47 },
48 .scissor = {
49 .count = 0,
50 },
51 .line_width = 1.0f,
52 .depth_bias = {
53 .bias = 0.0f,
54 .clamp = 0.0f,
55 .slope = 0.0f,
56 },
57 .blend_constants = { 0.0f, 0.0f, 0.0f, 0.0f },
58 .depth_bounds = {
59 .min = 0.0f,
60 .max = 1.0f,
61 },
62 .stencil_compare_mask = {
63 .front = ~0u,
64 .back = ~0u,
65 },
66 .stencil_write_mask = {
67 .front = ~0u,
68 .back = ~0u,
69 },
70 .stencil_reference = {
71 .front = 0u,
72 .back = 0u,
73 },
74 };
75
76 void
77 anv_dynamic_state_copy(struct anv_dynamic_state *dest,
78 const struct anv_dynamic_state *src,
79 uint32_t copy_mask)
80 {
81 if (copy_mask & (1 << VK_DYNAMIC_STATE_VIEWPORT)) {
82 dest->viewport.count = src->viewport.count;
83 typed_memcpy(dest->viewport.viewports, src->viewport.viewports,
84 src->viewport.count);
85 }
86
87 if (copy_mask & (1 << VK_DYNAMIC_STATE_SCISSOR)) {
88 dest->scissor.count = src->scissor.count;
89 typed_memcpy(dest->scissor.scissors, src->scissor.scissors,
90 src->scissor.count);
91 }
92
93 if (copy_mask & (1 << VK_DYNAMIC_STATE_LINE_WIDTH))
94 dest->line_width = src->line_width;
95
96 if (copy_mask & (1 << VK_DYNAMIC_STATE_DEPTH_BIAS))
97 dest->depth_bias = src->depth_bias;
98
99 if (copy_mask & (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS))
100 typed_memcpy(dest->blend_constants, src->blend_constants, 4);
101
102 if (copy_mask & (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS))
103 dest->depth_bounds = src->depth_bounds;
104
105 if (copy_mask & (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK))
106 dest->stencil_compare_mask = src->stencil_compare_mask;
107
108 if (copy_mask & (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK))
109 dest->stencil_write_mask = src->stencil_write_mask;
110
111 if (copy_mask & (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE))
112 dest->stencil_reference = src->stencil_reference;
113 }
114
115 static void
116 anv_cmd_state_reset(struct anv_cmd_buffer *cmd_buffer)
117 {
118 struct anv_cmd_state *state = &cmd_buffer->state;
119
120 memset(&state->descriptors, 0, sizeof(state->descriptors));
121 memset(&state->push_constants, 0, sizeof(state->push_constants));
122 memset(state->binding_tables, 0, sizeof(state->binding_tables));
123 memset(state->samplers, 0, sizeof(state->samplers));
124
125 /* 0 isn't a valid config. This ensures that we always configure L3$. */
126 cmd_buffer->state.current_l3_config = 0;
127
128 state->dirty = 0;
129 state->vb_dirty = 0;
130 state->pending_pipe_bits = 0;
131 state->descriptors_dirty = 0;
132 state->push_constants_dirty = 0;
133 state->pipeline = NULL;
134 state->push_constant_stages = 0;
135 state->restart_index = UINT32_MAX;
136 state->dynamic = default_dynamic_state;
137 state->need_query_wa = true;
138
139 if (state->attachments != NULL) {
140 vk_free(&cmd_buffer->pool->alloc, state->attachments);
141 state->attachments = NULL;
142 }
143
144 state->gen7.index_buffer = NULL;
145 }
146
147 VkResult
148 anv_cmd_buffer_ensure_push_constants_size(struct anv_cmd_buffer *cmd_buffer,
149 gl_shader_stage stage, uint32_t size)
150 {
151 struct anv_push_constants **ptr = &cmd_buffer->state.push_constants[stage];
152
153 if (*ptr == NULL) {
154 *ptr = vk_alloc(&cmd_buffer->pool->alloc, size, 8,
155 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
156 if (*ptr == NULL)
157 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
158 } else if ((*ptr)->size < size) {
159 *ptr = vk_realloc(&cmd_buffer->pool->alloc, *ptr, size, 8,
160 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
161 if (*ptr == NULL)
162 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
163 }
164 (*ptr)->size = size;
165
166 return VK_SUCCESS;
167 }
168
169 static VkResult anv_create_cmd_buffer(
170 struct anv_device * device,
171 struct anv_cmd_pool * pool,
172 VkCommandBufferLevel level,
173 VkCommandBuffer* pCommandBuffer)
174 {
175 struct anv_cmd_buffer *cmd_buffer;
176 VkResult result;
177
178 cmd_buffer = vk_alloc(&pool->alloc, sizeof(*cmd_buffer), 8,
179 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
180 if (cmd_buffer == NULL)
181 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
182
183 cmd_buffer->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
184 cmd_buffer->device = device;
185 cmd_buffer->pool = pool;
186 cmd_buffer->level = level;
187 cmd_buffer->state.attachments = NULL;
188
189 result = anv_cmd_buffer_init_batch_bo_chain(cmd_buffer);
190 if (result != VK_SUCCESS)
191 goto fail;
192
193 anv_state_stream_init(&cmd_buffer->surface_state_stream,
194 &device->surface_state_block_pool);
195 anv_state_stream_init(&cmd_buffer->dynamic_state_stream,
196 &device->dynamic_state_block_pool);
197
198 if (pool) {
199 list_addtail(&cmd_buffer->pool_link, &pool->cmd_buffers);
200 } else {
201 /* Init the pool_link so we can safefly call list_del when we destroy
202 * the command buffer
203 */
204 list_inithead(&cmd_buffer->pool_link);
205 }
206
207 *pCommandBuffer = anv_cmd_buffer_to_handle(cmd_buffer);
208
209 return VK_SUCCESS;
210
211 fail:
212 vk_free(&cmd_buffer->pool->alloc, cmd_buffer);
213
214 return result;
215 }
216
217 VkResult anv_AllocateCommandBuffers(
218 VkDevice _device,
219 const VkCommandBufferAllocateInfo* pAllocateInfo,
220 VkCommandBuffer* pCommandBuffers)
221 {
222 ANV_FROM_HANDLE(anv_device, device, _device);
223 ANV_FROM_HANDLE(anv_cmd_pool, pool, pAllocateInfo->commandPool);
224
225 VkResult result = VK_SUCCESS;
226 uint32_t i;
227
228 for (i = 0; i < pAllocateInfo->commandBufferCount; i++) {
229 result = anv_create_cmd_buffer(device, pool, pAllocateInfo->level,
230 &pCommandBuffers[i]);
231 if (result != VK_SUCCESS)
232 break;
233 }
234
235 if (result != VK_SUCCESS) {
236 anv_FreeCommandBuffers(_device, pAllocateInfo->commandPool,
237 i, pCommandBuffers);
238 for (i = 0; i < pAllocateInfo->commandBufferCount; i++)
239 pCommandBuffers[i] = VK_NULL_HANDLE;
240 }
241
242 return result;
243 }
244
245 static void
246 anv_cmd_buffer_destroy(struct anv_cmd_buffer *cmd_buffer)
247 {
248 list_del(&cmd_buffer->pool_link);
249
250 anv_cmd_buffer_fini_batch_bo_chain(cmd_buffer);
251
252 anv_state_stream_finish(&cmd_buffer->surface_state_stream);
253 anv_state_stream_finish(&cmd_buffer->dynamic_state_stream);
254
255 vk_free(&cmd_buffer->pool->alloc, cmd_buffer->state.attachments);
256 vk_free(&cmd_buffer->pool->alloc, cmd_buffer);
257 }
258
259 void anv_FreeCommandBuffers(
260 VkDevice device,
261 VkCommandPool commandPool,
262 uint32_t commandBufferCount,
263 const VkCommandBuffer* pCommandBuffers)
264 {
265 for (uint32_t i = 0; i < commandBufferCount; i++) {
266 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, pCommandBuffers[i]);
267
268 if (!cmd_buffer)
269 continue;
270
271 anv_cmd_buffer_destroy(cmd_buffer);
272 }
273 }
274
275 VkResult
276 anv_cmd_buffer_reset(struct anv_cmd_buffer *cmd_buffer)
277 {
278 cmd_buffer->usage_flags = 0;
279 cmd_buffer->state.current_pipeline = UINT32_MAX;
280 anv_cmd_buffer_reset_batch_bo_chain(cmd_buffer);
281 anv_cmd_state_reset(cmd_buffer);
282
283 anv_state_stream_finish(&cmd_buffer->surface_state_stream);
284 anv_state_stream_init(&cmd_buffer->surface_state_stream,
285 &cmd_buffer->device->surface_state_block_pool);
286
287 anv_state_stream_finish(&cmd_buffer->dynamic_state_stream);
288 anv_state_stream_init(&cmd_buffer->dynamic_state_stream,
289 &cmd_buffer->device->dynamic_state_block_pool);
290 return VK_SUCCESS;
291 }
292
293 VkResult anv_ResetCommandBuffer(
294 VkCommandBuffer commandBuffer,
295 VkCommandBufferResetFlags flags)
296 {
297 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
298 return anv_cmd_buffer_reset(cmd_buffer);
299 }
300
301 void
302 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer)
303 {
304 switch (cmd_buffer->device->info.gen) {
305 case 7:
306 if (cmd_buffer->device->info.is_haswell)
307 return gen75_cmd_buffer_emit_state_base_address(cmd_buffer);
308 else
309 return gen7_cmd_buffer_emit_state_base_address(cmd_buffer);
310 case 8:
311 return gen8_cmd_buffer_emit_state_base_address(cmd_buffer);
312 case 9:
313 return gen9_cmd_buffer_emit_state_base_address(cmd_buffer);
314 default:
315 unreachable("unsupported gen\n");
316 }
317 }
318
319 void anv_CmdBindPipeline(
320 VkCommandBuffer commandBuffer,
321 VkPipelineBindPoint pipelineBindPoint,
322 VkPipeline _pipeline)
323 {
324 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
325 ANV_FROM_HANDLE(anv_pipeline, pipeline, _pipeline);
326
327 switch (pipelineBindPoint) {
328 case VK_PIPELINE_BIND_POINT_COMPUTE:
329 cmd_buffer->state.compute_pipeline = pipeline;
330 cmd_buffer->state.compute_dirty |= ANV_CMD_DIRTY_PIPELINE;
331 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_COMPUTE_BIT;
332 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_COMPUTE_BIT;
333 break;
334
335 case VK_PIPELINE_BIND_POINT_GRAPHICS:
336 cmd_buffer->state.pipeline = pipeline;
337 cmd_buffer->state.vb_dirty |= pipeline->vb_used;
338 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_PIPELINE;
339 cmd_buffer->state.push_constants_dirty |= pipeline->active_stages;
340 cmd_buffer->state.descriptors_dirty |= pipeline->active_stages;
341
342 /* Apply the dynamic state from the pipeline */
343 cmd_buffer->state.dirty |= pipeline->dynamic_state_mask;
344 anv_dynamic_state_copy(&cmd_buffer->state.dynamic,
345 &pipeline->dynamic_state,
346 pipeline->dynamic_state_mask);
347 break;
348
349 default:
350 assert(!"invalid bind point");
351 break;
352 }
353 }
354
355 void anv_CmdSetViewport(
356 VkCommandBuffer commandBuffer,
357 uint32_t firstViewport,
358 uint32_t viewportCount,
359 const VkViewport* pViewports)
360 {
361 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
362
363 const uint32_t total_count = firstViewport + viewportCount;
364 if (cmd_buffer->state.dynamic.viewport.count < total_count)
365 cmd_buffer->state.dynamic.viewport.count = total_count;
366
367 memcpy(cmd_buffer->state.dynamic.viewport.viewports + firstViewport,
368 pViewports, viewportCount * sizeof(*pViewports));
369
370 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_VIEWPORT;
371 }
372
373 void anv_CmdSetScissor(
374 VkCommandBuffer commandBuffer,
375 uint32_t firstScissor,
376 uint32_t scissorCount,
377 const VkRect2D* pScissors)
378 {
379 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
380
381 const uint32_t total_count = firstScissor + scissorCount;
382 if (cmd_buffer->state.dynamic.scissor.count < total_count)
383 cmd_buffer->state.dynamic.scissor.count = total_count;
384
385 memcpy(cmd_buffer->state.dynamic.scissor.scissors + firstScissor,
386 pScissors, scissorCount * sizeof(*pScissors));
387
388 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_SCISSOR;
389 }
390
391 void anv_CmdSetLineWidth(
392 VkCommandBuffer commandBuffer,
393 float lineWidth)
394 {
395 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
396
397 cmd_buffer->state.dynamic.line_width = lineWidth;
398 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH;
399 }
400
401 void anv_CmdSetDepthBias(
402 VkCommandBuffer commandBuffer,
403 float depthBiasConstantFactor,
404 float depthBiasClamp,
405 float depthBiasSlopeFactor)
406 {
407 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
408
409 cmd_buffer->state.dynamic.depth_bias.bias = depthBiasConstantFactor;
410 cmd_buffer->state.dynamic.depth_bias.clamp = depthBiasClamp;
411 cmd_buffer->state.dynamic.depth_bias.slope = depthBiasSlopeFactor;
412
413 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS;
414 }
415
416 void anv_CmdSetBlendConstants(
417 VkCommandBuffer commandBuffer,
418 const float blendConstants[4])
419 {
420 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
421
422 memcpy(cmd_buffer->state.dynamic.blend_constants,
423 blendConstants, sizeof(float) * 4);
424
425 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS;
426 }
427
428 void anv_CmdSetDepthBounds(
429 VkCommandBuffer commandBuffer,
430 float minDepthBounds,
431 float maxDepthBounds)
432 {
433 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
434
435 cmd_buffer->state.dynamic.depth_bounds.min = minDepthBounds;
436 cmd_buffer->state.dynamic.depth_bounds.max = maxDepthBounds;
437
438 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS;
439 }
440
441 void anv_CmdSetStencilCompareMask(
442 VkCommandBuffer commandBuffer,
443 VkStencilFaceFlags faceMask,
444 uint32_t compareMask)
445 {
446 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
447
448 if (faceMask & VK_STENCIL_FACE_FRONT_BIT)
449 cmd_buffer->state.dynamic.stencil_compare_mask.front = compareMask;
450 if (faceMask & VK_STENCIL_FACE_BACK_BIT)
451 cmd_buffer->state.dynamic.stencil_compare_mask.back = compareMask;
452
453 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK;
454 }
455
456 void anv_CmdSetStencilWriteMask(
457 VkCommandBuffer commandBuffer,
458 VkStencilFaceFlags faceMask,
459 uint32_t writeMask)
460 {
461 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
462
463 if (faceMask & VK_STENCIL_FACE_FRONT_BIT)
464 cmd_buffer->state.dynamic.stencil_write_mask.front = writeMask;
465 if (faceMask & VK_STENCIL_FACE_BACK_BIT)
466 cmd_buffer->state.dynamic.stencil_write_mask.back = writeMask;
467
468 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK;
469 }
470
471 void anv_CmdSetStencilReference(
472 VkCommandBuffer commandBuffer,
473 VkStencilFaceFlags faceMask,
474 uint32_t reference)
475 {
476 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
477
478 if (faceMask & VK_STENCIL_FACE_FRONT_BIT)
479 cmd_buffer->state.dynamic.stencil_reference.front = reference;
480 if (faceMask & VK_STENCIL_FACE_BACK_BIT)
481 cmd_buffer->state.dynamic.stencil_reference.back = reference;
482
483 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE;
484 }
485
486 void anv_CmdBindDescriptorSets(
487 VkCommandBuffer commandBuffer,
488 VkPipelineBindPoint pipelineBindPoint,
489 VkPipelineLayout _layout,
490 uint32_t firstSet,
491 uint32_t descriptorSetCount,
492 const VkDescriptorSet* pDescriptorSets,
493 uint32_t dynamicOffsetCount,
494 const uint32_t* pDynamicOffsets)
495 {
496 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
497 ANV_FROM_HANDLE(anv_pipeline_layout, layout, _layout);
498 struct anv_descriptor_set_layout *set_layout;
499
500 assert(firstSet + descriptorSetCount < MAX_SETS);
501
502 for (uint32_t i = 0; i < descriptorSetCount; i++) {
503 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
504 set_layout = layout->set[firstSet + i].layout;
505
506 if (cmd_buffer->state.descriptors[firstSet + i] != set) {
507 cmd_buffer->state.descriptors[firstSet + i] = set;
508 cmd_buffer->state.descriptors_dirty |= set_layout->shader_stages;
509 }
510
511 if (set_layout->dynamic_offset_count > 0) {
512 anv_foreach_stage(s, set_layout->shader_stages) {
513 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, s, dynamic);
514
515 struct anv_push_constants *push =
516 cmd_buffer->state.push_constants[s];
517
518 unsigned d = layout->set[firstSet + i].dynamic_offset_start;
519 const uint32_t *offsets = pDynamicOffsets;
520 struct anv_descriptor *desc = set->descriptors;
521
522 for (unsigned b = 0; b < set_layout->binding_count; b++) {
523 if (set_layout->binding[b].dynamic_offset_index < 0)
524 continue;
525
526 unsigned array_size = set_layout->binding[b].array_size;
527 for (unsigned j = 0; j < array_size; j++) {
528 push->dynamic[d].offset = *(offsets++);
529 push->dynamic[d].range = (desc->buffer_view) ?
530 desc->buffer_view->range : 0;
531 desc++;
532 d++;
533 }
534 }
535 }
536 cmd_buffer->state.push_constants_dirty |= set_layout->shader_stages;
537 }
538 }
539 }
540
541 void anv_CmdBindVertexBuffers(
542 VkCommandBuffer commandBuffer,
543 uint32_t firstBinding,
544 uint32_t bindingCount,
545 const VkBuffer* pBuffers,
546 const VkDeviceSize* pOffsets)
547 {
548 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
549 struct anv_vertex_binding *vb = cmd_buffer->state.vertex_bindings;
550
551 /* We have to defer setting up vertex buffer since we need the buffer
552 * stride from the pipeline. */
553
554 assert(firstBinding + bindingCount < MAX_VBS);
555 for (uint32_t i = 0; i < bindingCount; i++) {
556 vb[firstBinding + i].buffer = anv_buffer_from_handle(pBuffers[i]);
557 vb[firstBinding + i].offset = pOffsets[i];
558 cmd_buffer->state.vb_dirty |= 1 << (firstBinding + i);
559 }
560 }
561
562 enum isl_format
563 anv_isl_format_for_descriptor_type(VkDescriptorType type)
564 {
565 switch (type) {
566 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
567 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
568 return ISL_FORMAT_R32G32B32A32_FLOAT;
569
570 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
571 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
572 return ISL_FORMAT_RAW;
573
574 default:
575 unreachable("Invalid descriptor type");
576 }
577 }
578
579 struct anv_state
580 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer *cmd_buffer,
581 const void *data, uint32_t size, uint32_t alignment)
582 {
583 struct anv_state state;
584
585 state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, alignment);
586 memcpy(state.map, data, size);
587
588 if (!cmd_buffer->device->info.has_llc)
589 anv_state_clflush(state);
590
591 VG(VALGRIND_CHECK_MEM_IS_DEFINED(state.map, size));
592
593 return state;
594 }
595
596 struct anv_state
597 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
598 uint32_t *a, uint32_t *b,
599 uint32_t dwords, uint32_t alignment)
600 {
601 struct anv_state state;
602 uint32_t *p;
603
604 state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer,
605 dwords * 4, alignment);
606 p = state.map;
607 for (uint32_t i = 0; i < dwords; i++)
608 p[i] = a[i] | b[i];
609
610 if (!cmd_buffer->device->info.has_llc)
611 anv_state_clflush(state);
612
613 VG(VALGRIND_CHECK_MEM_IS_DEFINED(p, dwords * 4));
614
615 return state;
616 }
617
618 struct anv_state
619 anv_cmd_buffer_push_constants(struct anv_cmd_buffer *cmd_buffer,
620 gl_shader_stage stage)
621 {
622 /* If we don't have this stage, bail. */
623 if (!anv_pipeline_has_stage(cmd_buffer->state.pipeline, stage))
624 return (struct anv_state) { .offset = 0 };
625
626 struct anv_push_constants *data =
627 cmd_buffer->state.push_constants[stage];
628 const struct brw_stage_prog_data *prog_data =
629 cmd_buffer->state.pipeline->shaders[stage]->prog_data;
630
631 /* If we don't actually have any push constants, bail. */
632 if (data == NULL || prog_data == NULL || prog_data->nr_params == 0)
633 return (struct anv_state) { .offset = 0 };
634
635 struct anv_state state =
636 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer,
637 prog_data->nr_params * sizeof(float),
638 32 /* bottom 5 bits MBZ */);
639
640 /* Walk through the param array and fill the buffer with data */
641 uint32_t *u32_map = state.map;
642 for (unsigned i = 0; i < prog_data->nr_params; i++) {
643 uint32_t offset = (uintptr_t)prog_data->param[i];
644 u32_map[i] = *(uint32_t *)((uint8_t *)data + offset);
645 }
646
647 if (!cmd_buffer->device->info.has_llc)
648 anv_state_clflush(state);
649
650 return state;
651 }
652
653 struct anv_state
654 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer *cmd_buffer)
655 {
656 struct anv_push_constants *data =
657 cmd_buffer->state.push_constants[MESA_SHADER_COMPUTE];
658 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
659 const struct brw_cs_prog_data *cs_prog_data = get_cs_prog_data(pipeline);
660 const struct brw_stage_prog_data *prog_data = &cs_prog_data->base;
661
662 /* If we don't actually have any push constants, bail. */
663 if (cs_prog_data->push.total.size == 0)
664 return (struct anv_state) { .offset = 0 };
665
666 const unsigned push_constant_alignment =
667 cmd_buffer->device->info.gen < 8 ? 32 : 64;
668 const unsigned aligned_total_push_constants_size =
669 ALIGN(cs_prog_data->push.total.size, push_constant_alignment);
670 struct anv_state state =
671 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer,
672 aligned_total_push_constants_size,
673 push_constant_alignment);
674
675 /* Walk through the param array and fill the buffer with data */
676 uint32_t *u32_map = state.map;
677
678 if (cs_prog_data->push.cross_thread.size > 0) {
679 assert(cs_prog_data->thread_local_id_index < 0 ||
680 cs_prog_data->thread_local_id_index >=
681 cs_prog_data->push.cross_thread.dwords);
682 for (unsigned i = 0;
683 i < cs_prog_data->push.cross_thread.dwords;
684 i++) {
685 uint32_t offset = (uintptr_t)prog_data->param[i];
686 u32_map[i] = *(uint32_t *)((uint8_t *)data + offset);
687 }
688 }
689
690 if (cs_prog_data->push.per_thread.size > 0) {
691 for (unsigned t = 0; t < cs_prog_data->threads; t++) {
692 unsigned dst =
693 8 * (cs_prog_data->push.per_thread.regs * t +
694 cs_prog_data->push.cross_thread.regs);
695 unsigned src = cs_prog_data->push.cross_thread.dwords;
696 for ( ; src < prog_data->nr_params; src++, dst++) {
697 if (src != cs_prog_data->thread_local_id_index) {
698 uint32_t offset = (uintptr_t)prog_data->param[src];
699 u32_map[dst] = *(uint32_t *)((uint8_t *)data + offset);
700 } else {
701 u32_map[dst] = t * cs_prog_data->simd_size;
702 }
703 }
704 }
705 }
706
707 if (!cmd_buffer->device->info.has_llc)
708 anv_state_clflush(state);
709
710 return state;
711 }
712
713 void anv_CmdPushConstants(
714 VkCommandBuffer commandBuffer,
715 VkPipelineLayout layout,
716 VkShaderStageFlags stageFlags,
717 uint32_t offset,
718 uint32_t size,
719 const void* pValues)
720 {
721 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
722
723 anv_foreach_stage(stage, stageFlags) {
724 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, stage, client_data);
725
726 memcpy(cmd_buffer->state.push_constants[stage]->client_data + offset,
727 pValues, size);
728 }
729
730 cmd_buffer->state.push_constants_dirty |= stageFlags;
731 }
732
733 VkResult anv_CreateCommandPool(
734 VkDevice _device,
735 const VkCommandPoolCreateInfo* pCreateInfo,
736 const VkAllocationCallbacks* pAllocator,
737 VkCommandPool* pCmdPool)
738 {
739 ANV_FROM_HANDLE(anv_device, device, _device);
740 struct anv_cmd_pool *pool;
741
742 pool = vk_alloc2(&device->alloc, pAllocator, sizeof(*pool), 8,
743 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
744 if (pool == NULL)
745 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
746
747 if (pAllocator)
748 pool->alloc = *pAllocator;
749 else
750 pool->alloc = device->alloc;
751
752 list_inithead(&pool->cmd_buffers);
753
754 *pCmdPool = anv_cmd_pool_to_handle(pool);
755
756 return VK_SUCCESS;
757 }
758
759 void anv_DestroyCommandPool(
760 VkDevice _device,
761 VkCommandPool commandPool,
762 const VkAllocationCallbacks* pAllocator)
763 {
764 ANV_FROM_HANDLE(anv_device, device, _device);
765 ANV_FROM_HANDLE(anv_cmd_pool, pool, commandPool);
766
767 if (!pool)
768 return;
769
770 list_for_each_entry_safe(struct anv_cmd_buffer, cmd_buffer,
771 &pool->cmd_buffers, pool_link) {
772 anv_cmd_buffer_destroy(cmd_buffer);
773 }
774
775 vk_free2(&device->alloc, pAllocator, pool);
776 }
777
778 VkResult anv_ResetCommandPool(
779 VkDevice device,
780 VkCommandPool commandPool,
781 VkCommandPoolResetFlags flags)
782 {
783 ANV_FROM_HANDLE(anv_cmd_pool, pool, commandPool);
784
785 list_for_each_entry(struct anv_cmd_buffer, cmd_buffer,
786 &pool->cmd_buffers, pool_link) {
787 anv_cmd_buffer_reset(cmd_buffer);
788 }
789
790 return VK_SUCCESS;
791 }
792
793 void anv_TrimCommandPoolKHR(
794 VkDevice device,
795 VkCommandPool commandPool,
796 VkCommandPoolTrimFlagsKHR flags)
797 {
798 /* Nothing for us to do here. Our pools stay pretty tidy. */
799 }
800
801 /**
802 * Return NULL if the current subpass has no depthstencil attachment.
803 */
804 const struct anv_image_view *
805 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer *cmd_buffer)
806 {
807 const struct anv_subpass *subpass = cmd_buffer->state.subpass;
808 const struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
809
810 if (subpass->depth_stencil_attachment == VK_ATTACHMENT_UNUSED)
811 return NULL;
812
813 const struct anv_image_view *iview =
814 fb->attachments[subpass->depth_stencil_attachment];
815
816 assert(iview->aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT |
817 VK_IMAGE_ASPECT_STENCIL_BIT));
818
819 return iview;
820 }