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