ff9d0e47ab8a297f049555eaec76e18e8757ab82
[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
239 return result;
240 }
241
242 static void
243 anv_cmd_buffer_destroy(struct anv_cmd_buffer *cmd_buffer)
244 {
245 list_del(&cmd_buffer->pool_link);
246
247 anv_cmd_buffer_fini_batch_bo_chain(cmd_buffer);
248
249 anv_state_stream_finish(&cmd_buffer->surface_state_stream);
250 anv_state_stream_finish(&cmd_buffer->dynamic_state_stream);
251
252 vk_free(&cmd_buffer->pool->alloc, cmd_buffer->state.attachments);
253 vk_free(&cmd_buffer->pool->alloc, cmd_buffer);
254 }
255
256 void anv_FreeCommandBuffers(
257 VkDevice device,
258 VkCommandPool commandPool,
259 uint32_t commandBufferCount,
260 const VkCommandBuffer* pCommandBuffers)
261 {
262 for (uint32_t i = 0; i < commandBufferCount; i++) {
263 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, pCommandBuffers[i]);
264
265 anv_cmd_buffer_destroy(cmd_buffer);
266 }
267 }
268
269 VkResult
270 anv_cmd_buffer_reset(struct anv_cmd_buffer *cmd_buffer)
271 {
272 cmd_buffer->usage_flags = 0;
273 cmd_buffer->state.current_pipeline = UINT32_MAX;
274 anv_cmd_buffer_reset_batch_bo_chain(cmd_buffer);
275 anv_cmd_state_reset(cmd_buffer);
276
277 anv_state_stream_finish(&cmd_buffer->surface_state_stream);
278 anv_state_stream_init(&cmd_buffer->surface_state_stream,
279 &cmd_buffer->device->surface_state_block_pool);
280
281 anv_state_stream_finish(&cmd_buffer->dynamic_state_stream);
282 anv_state_stream_init(&cmd_buffer->dynamic_state_stream,
283 &cmd_buffer->device->dynamic_state_block_pool);
284 return VK_SUCCESS;
285 }
286
287 VkResult anv_ResetCommandBuffer(
288 VkCommandBuffer commandBuffer,
289 VkCommandBufferResetFlags flags)
290 {
291 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
292 return anv_cmd_buffer_reset(cmd_buffer);
293 }
294
295 void anv_CmdBindPipeline(
296 VkCommandBuffer commandBuffer,
297 VkPipelineBindPoint pipelineBindPoint,
298 VkPipeline _pipeline)
299 {
300 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
301 ANV_FROM_HANDLE(anv_pipeline, pipeline, _pipeline);
302
303 switch (pipelineBindPoint) {
304 case VK_PIPELINE_BIND_POINT_COMPUTE:
305 cmd_buffer->state.compute_pipeline = pipeline;
306 cmd_buffer->state.compute_dirty |= ANV_CMD_DIRTY_PIPELINE;
307 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_COMPUTE_BIT;
308 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_COMPUTE_BIT;
309 break;
310
311 case VK_PIPELINE_BIND_POINT_GRAPHICS:
312 cmd_buffer->state.pipeline = pipeline;
313 cmd_buffer->state.vb_dirty |= pipeline->vb_used;
314 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_PIPELINE;
315 cmd_buffer->state.push_constants_dirty |= pipeline->active_stages;
316 cmd_buffer->state.descriptors_dirty |= pipeline->active_stages;
317
318 /* Apply the dynamic state from the pipeline */
319 cmd_buffer->state.dirty |= pipeline->dynamic_state_mask;
320 anv_dynamic_state_copy(&cmd_buffer->state.dynamic,
321 &pipeline->dynamic_state,
322 pipeline->dynamic_state_mask);
323 break;
324
325 default:
326 assert(!"invalid bind point");
327 break;
328 }
329 }
330
331 void anv_CmdSetViewport(
332 VkCommandBuffer commandBuffer,
333 uint32_t firstViewport,
334 uint32_t viewportCount,
335 const VkViewport* pViewports)
336 {
337 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
338
339 const uint32_t total_count = firstViewport + viewportCount;
340 if (cmd_buffer->state.dynamic.viewport.count < total_count)
341 cmd_buffer->state.dynamic.viewport.count = total_count;
342
343 memcpy(cmd_buffer->state.dynamic.viewport.viewports + firstViewport,
344 pViewports, viewportCount * sizeof(*pViewports));
345
346 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_VIEWPORT;
347 }
348
349 void anv_CmdSetScissor(
350 VkCommandBuffer commandBuffer,
351 uint32_t firstScissor,
352 uint32_t scissorCount,
353 const VkRect2D* pScissors)
354 {
355 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
356
357 const uint32_t total_count = firstScissor + scissorCount;
358 if (cmd_buffer->state.dynamic.scissor.count < total_count)
359 cmd_buffer->state.dynamic.scissor.count = total_count;
360
361 memcpy(cmd_buffer->state.dynamic.scissor.scissors + firstScissor,
362 pScissors, scissorCount * sizeof(*pScissors));
363
364 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_SCISSOR;
365 }
366
367 void anv_CmdSetLineWidth(
368 VkCommandBuffer commandBuffer,
369 float lineWidth)
370 {
371 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
372
373 cmd_buffer->state.dynamic.line_width = lineWidth;
374 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH;
375 }
376
377 void anv_CmdSetDepthBias(
378 VkCommandBuffer commandBuffer,
379 float depthBiasConstantFactor,
380 float depthBiasClamp,
381 float depthBiasSlopeFactor)
382 {
383 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
384
385 cmd_buffer->state.dynamic.depth_bias.bias = depthBiasConstantFactor;
386 cmd_buffer->state.dynamic.depth_bias.clamp = depthBiasClamp;
387 cmd_buffer->state.dynamic.depth_bias.slope = depthBiasSlopeFactor;
388
389 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS;
390 }
391
392 void anv_CmdSetBlendConstants(
393 VkCommandBuffer commandBuffer,
394 const float blendConstants[4])
395 {
396 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
397
398 memcpy(cmd_buffer->state.dynamic.blend_constants,
399 blendConstants, sizeof(float) * 4);
400
401 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS;
402 }
403
404 void anv_CmdSetDepthBounds(
405 VkCommandBuffer commandBuffer,
406 float minDepthBounds,
407 float maxDepthBounds)
408 {
409 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
410
411 cmd_buffer->state.dynamic.depth_bounds.min = minDepthBounds;
412 cmd_buffer->state.dynamic.depth_bounds.max = maxDepthBounds;
413
414 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS;
415 }
416
417 void anv_CmdSetStencilCompareMask(
418 VkCommandBuffer commandBuffer,
419 VkStencilFaceFlags faceMask,
420 uint32_t compareMask)
421 {
422 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
423
424 if (faceMask & VK_STENCIL_FACE_FRONT_BIT)
425 cmd_buffer->state.dynamic.stencil_compare_mask.front = compareMask;
426 if (faceMask & VK_STENCIL_FACE_BACK_BIT)
427 cmd_buffer->state.dynamic.stencil_compare_mask.back = compareMask;
428
429 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK;
430 }
431
432 void anv_CmdSetStencilWriteMask(
433 VkCommandBuffer commandBuffer,
434 VkStencilFaceFlags faceMask,
435 uint32_t writeMask)
436 {
437 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
438
439 if (faceMask & VK_STENCIL_FACE_FRONT_BIT)
440 cmd_buffer->state.dynamic.stencil_write_mask.front = writeMask;
441 if (faceMask & VK_STENCIL_FACE_BACK_BIT)
442 cmd_buffer->state.dynamic.stencil_write_mask.back = writeMask;
443
444 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK;
445 }
446
447 void anv_CmdSetStencilReference(
448 VkCommandBuffer commandBuffer,
449 VkStencilFaceFlags faceMask,
450 uint32_t reference)
451 {
452 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
453
454 if (faceMask & VK_STENCIL_FACE_FRONT_BIT)
455 cmd_buffer->state.dynamic.stencil_reference.front = reference;
456 if (faceMask & VK_STENCIL_FACE_BACK_BIT)
457 cmd_buffer->state.dynamic.stencil_reference.back = reference;
458
459 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE;
460 }
461
462 void anv_CmdBindDescriptorSets(
463 VkCommandBuffer commandBuffer,
464 VkPipelineBindPoint pipelineBindPoint,
465 VkPipelineLayout _layout,
466 uint32_t firstSet,
467 uint32_t descriptorSetCount,
468 const VkDescriptorSet* pDescriptorSets,
469 uint32_t dynamicOffsetCount,
470 const uint32_t* pDynamicOffsets)
471 {
472 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
473 ANV_FROM_HANDLE(anv_pipeline_layout, layout, _layout);
474 struct anv_descriptor_set_layout *set_layout;
475
476 assert(firstSet + descriptorSetCount < MAX_SETS);
477
478 for (uint32_t i = 0; i < descriptorSetCount; i++) {
479 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
480 set_layout = layout->set[firstSet + i].layout;
481
482 if (cmd_buffer->state.descriptors[firstSet + i] != set) {
483 cmd_buffer->state.descriptors[firstSet + i] = set;
484 cmd_buffer->state.descriptors_dirty |= set_layout->shader_stages;
485 }
486
487 if (set_layout->dynamic_offset_count > 0) {
488 anv_foreach_stage(s, set_layout->shader_stages) {
489 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, s, dynamic);
490
491 struct anv_push_constants *push =
492 cmd_buffer->state.push_constants[s];
493
494 unsigned d = layout->set[firstSet + i].dynamic_offset_start;
495 const uint32_t *offsets = pDynamicOffsets;
496 struct anv_descriptor *desc = set->descriptors;
497
498 for (unsigned b = 0; b < set_layout->binding_count; b++) {
499 if (set_layout->binding[b].dynamic_offset_index < 0)
500 continue;
501
502 unsigned array_size = set_layout->binding[b].array_size;
503 for (unsigned j = 0; j < array_size; j++) {
504 push->dynamic[d].offset = *(offsets++);
505 push->dynamic[d].range = (desc->buffer_view) ?
506 desc->buffer_view->range : 0;
507 desc++;
508 d++;
509 }
510 }
511 }
512 cmd_buffer->state.push_constants_dirty |= set_layout->shader_stages;
513 }
514 }
515 }
516
517 void anv_CmdBindVertexBuffers(
518 VkCommandBuffer commandBuffer,
519 uint32_t firstBinding,
520 uint32_t bindingCount,
521 const VkBuffer* pBuffers,
522 const VkDeviceSize* pOffsets)
523 {
524 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
525 struct anv_vertex_binding *vb = cmd_buffer->state.vertex_bindings;
526
527 /* We have to defer setting up vertex buffer since we need the buffer
528 * stride from the pipeline. */
529
530 assert(firstBinding + bindingCount < MAX_VBS);
531 for (uint32_t i = 0; i < bindingCount; i++) {
532 vb[firstBinding + i].buffer = anv_buffer_from_handle(pBuffers[i]);
533 vb[firstBinding + i].offset = pOffsets[i];
534 cmd_buffer->state.vb_dirty |= 1 << (firstBinding + i);
535 }
536 }
537
538 enum isl_format
539 anv_isl_format_for_descriptor_type(VkDescriptorType type)
540 {
541 switch (type) {
542 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
543 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
544 return ISL_FORMAT_R32G32B32A32_FLOAT;
545
546 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
547 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
548 return ISL_FORMAT_RAW;
549
550 default:
551 unreachable("Invalid descriptor type");
552 }
553 }
554
555 struct anv_state
556 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer *cmd_buffer,
557 const void *data, uint32_t size, uint32_t alignment)
558 {
559 struct anv_state state;
560
561 state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, alignment);
562 memcpy(state.map, data, size);
563
564 if (!cmd_buffer->device->info.has_llc)
565 anv_state_clflush(state);
566
567 VG(VALGRIND_CHECK_MEM_IS_DEFINED(state.map, size));
568
569 return state;
570 }
571
572 struct anv_state
573 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
574 uint32_t *a, uint32_t *b,
575 uint32_t dwords, uint32_t alignment)
576 {
577 struct anv_state state;
578 uint32_t *p;
579
580 state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer,
581 dwords * 4, alignment);
582 p = state.map;
583 for (uint32_t i = 0; i < dwords; i++)
584 p[i] = a[i] | b[i];
585
586 if (!cmd_buffer->device->info.has_llc)
587 anv_state_clflush(state);
588
589 VG(VALGRIND_CHECK_MEM_IS_DEFINED(p, dwords * 4));
590
591 return state;
592 }
593
594 struct anv_state
595 anv_cmd_buffer_push_constants(struct anv_cmd_buffer *cmd_buffer,
596 gl_shader_stage stage)
597 {
598 /* If we don't have this stage, bail. */
599 if (!anv_pipeline_has_stage(cmd_buffer->state.pipeline, stage))
600 return (struct anv_state) { .offset = 0 };
601
602 struct anv_push_constants *data =
603 cmd_buffer->state.push_constants[stage];
604 const struct brw_stage_prog_data *prog_data =
605 cmd_buffer->state.pipeline->shaders[stage]->prog_data;
606
607 /* If we don't actually have any push constants, bail. */
608 if (data == NULL || prog_data == NULL || prog_data->nr_params == 0)
609 return (struct anv_state) { .offset = 0 };
610
611 struct anv_state state =
612 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer,
613 prog_data->nr_params * sizeof(float),
614 32 /* bottom 5 bits MBZ */);
615
616 /* Walk through the param array and fill the buffer with data */
617 uint32_t *u32_map = state.map;
618 for (unsigned i = 0; i < prog_data->nr_params; i++) {
619 uint32_t offset = (uintptr_t)prog_data->param[i];
620 u32_map[i] = *(uint32_t *)((uint8_t *)data + offset);
621 }
622
623 if (!cmd_buffer->device->info.has_llc)
624 anv_state_clflush(state);
625
626 return state;
627 }
628
629 struct anv_state
630 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer *cmd_buffer)
631 {
632 struct anv_push_constants *data =
633 cmd_buffer->state.push_constants[MESA_SHADER_COMPUTE];
634 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
635 const struct brw_cs_prog_data *cs_prog_data = get_cs_prog_data(pipeline);
636 const struct brw_stage_prog_data *prog_data = &cs_prog_data->base;
637
638 /* If we don't actually have any push constants, bail. */
639 if (cs_prog_data->push.total.size == 0)
640 return (struct anv_state) { .offset = 0 };
641
642 const unsigned push_constant_alignment =
643 cmd_buffer->device->info.gen < 8 ? 32 : 64;
644 const unsigned aligned_total_push_constants_size =
645 ALIGN(cs_prog_data->push.total.size, push_constant_alignment);
646 struct anv_state state =
647 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer,
648 aligned_total_push_constants_size,
649 push_constant_alignment);
650
651 /* Walk through the param array and fill the buffer with data */
652 uint32_t *u32_map = state.map;
653
654 if (cs_prog_data->push.cross_thread.size > 0) {
655 assert(cs_prog_data->thread_local_id_index < 0 ||
656 cs_prog_data->thread_local_id_index >=
657 cs_prog_data->push.cross_thread.dwords);
658 for (unsigned i = 0;
659 i < cs_prog_data->push.cross_thread.dwords;
660 i++) {
661 uint32_t offset = (uintptr_t)prog_data->param[i];
662 u32_map[i] = *(uint32_t *)((uint8_t *)data + offset);
663 }
664 }
665
666 if (cs_prog_data->push.per_thread.size > 0) {
667 for (unsigned t = 0; t < cs_prog_data->threads; t++) {
668 unsigned dst =
669 8 * (cs_prog_data->push.per_thread.regs * t +
670 cs_prog_data->push.cross_thread.regs);
671 unsigned src = cs_prog_data->push.cross_thread.dwords;
672 for ( ; src < prog_data->nr_params; src++, dst++) {
673 if (src != cs_prog_data->thread_local_id_index) {
674 uint32_t offset = (uintptr_t)prog_data->param[src];
675 u32_map[dst] = *(uint32_t *)((uint8_t *)data + offset);
676 } else {
677 u32_map[dst] = t * cs_prog_data->simd_size;
678 }
679 }
680 }
681 }
682
683 if (!cmd_buffer->device->info.has_llc)
684 anv_state_clflush(state);
685
686 return state;
687 }
688
689 void anv_CmdPushConstants(
690 VkCommandBuffer commandBuffer,
691 VkPipelineLayout layout,
692 VkShaderStageFlags stageFlags,
693 uint32_t offset,
694 uint32_t size,
695 const void* pValues)
696 {
697 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
698
699 anv_foreach_stage(stage, stageFlags) {
700 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, stage, client_data);
701
702 memcpy(cmd_buffer->state.push_constants[stage]->client_data + offset,
703 pValues, size);
704 }
705
706 cmd_buffer->state.push_constants_dirty |= stageFlags;
707 }
708
709 VkResult anv_CreateCommandPool(
710 VkDevice _device,
711 const VkCommandPoolCreateInfo* pCreateInfo,
712 const VkAllocationCallbacks* pAllocator,
713 VkCommandPool* pCmdPool)
714 {
715 ANV_FROM_HANDLE(anv_device, device, _device);
716 struct anv_cmd_pool *pool;
717
718 pool = vk_alloc2(&device->alloc, pAllocator, sizeof(*pool), 8,
719 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
720 if (pool == NULL)
721 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
722
723 if (pAllocator)
724 pool->alloc = *pAllocator;
725 else
726 pool->alloc = device->alloc;
727
728 list_inithead(&pool->cmd_buffers);
729
730 *pCmdPool = anv_cmd_pool_to_handle(pool);
731
732 return VK_SUCCESS;
733 }
734
735 void anv_DestroyCommandPool(
736 VkDevice _device,
737 VkCommandPool commandPool,
738 const VkAllocationCallbacks* pAllocator)
739 {
740 ANV_FROM_HANDLE(anv_device, device, _device);
741 ANV_FROM_HANDLE(anv_cmd_pool, pool, commandPool);
742
743 list_for_each_entry_safe(struct anv_cmd_buffer, cmd_buffer,
744 &pool->cmd_buffers, pool_link) {
745 anv_cmd_buffer_destroy(cmd_buffer);
746 }
747
748 vk_free2(&device->alloc, pAllocator, pool);
749 }
750
751 VkResult anv_ResetCommandPool(
752 VkDevice device,
753 VkCommandPool commandPool,
754 VkCommandPoolResetFlags flags)
755 {
756 ANV_FROM_HANDLE(anv_cmd_pool, pool, commandPool);
757
758 list_for_each_entry(struct anv_cmd_buffer, cmd_buffer,
759 &pool->cmd_buffers, pool_link) {
760 anv_cmd_buffer_reset(cmd_buffer);
761 }
762
763 return VK_SUCCESS;
764 }
765
766 /**
767 * Return NULL if the current subpass has no depthstencil attachment.
768 */
769 const struct anv_image_view *
770 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer *cmd_buffer)
771 {
772 const struct anv_subpass *subpass = cmd_buffer->state.subpass;
773 const struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
774
775 if (subpass->depth_stencil_attachment == VK_ATTACHMENT_UNUSED)
776 return NULL;
777
778 const struct anv_image_view *iview =
779 fb->attachments[subpass->depth_stencil_attachment];
780
781 assert(iview->aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT |
782 VK_IMAGE_ASPECT_STENCIL_BIT));
783
784 return iview;
785 }