anv/cmd_buffer: Flush the VF cache at the top of all primaries
[mesa.git] / src / intel / vulkan / genX_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
27 #include "anv_private.h"
28 #include "vk_format_info.h"
29
30 #include "common/gen_l3_config.h"
31 #include "genxml/gen_macros.h"
32 #include "genxml/genX_pack.h"
33
34 static void
35 emit_lrm(struct anv_batch *batch,
36 uint32_t reg, struct anv_bo *bo, uint32_t offset)
37 {
38 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
39 lrm.RegisterAddress = reg;
40 lrm.MemoryAddress = (struct anv_address) { bo, offset };
41 }
42 }
43
44 static void
45 emit_lri(struct anv_batch *batch, uint32_t reg, uint32_t imm)
46 {
47 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
48 lri.RegisterOffset = reg;
49 lri.DataDWord = imm;
50 }
51 }
52
53 void
54 genX(cmd_buffer_emit_state_base_address)(struct anv_cmd_buffer *cmd_buffer)
55 {
56 struct anv_device *device = cmd_buffer->device;
57
58 /* Emit a render target cache flush.
59 *
60 * This isn't documented anywhere in the PRM. However, it seems to be
61 * necessary prior to changing the surface state base adress. Without
62 * this, we get GPU hangs when using multi-level command buffers which
63 * clear depth, reset state base address, and then go render stuff.
64 */
65 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
66 pc.DCFlushEnable = true;
67 pc.RenderTargetCacheFlushEnable = true;
68 pc.CommandStreamerStallEnable = true;
69 }
70
71 anv_batch_emit(&cmd_buffer->batch, GENX(STATE_BASE_ADDRESS), sba) {
72 sba.GeneralStateBaseAddress = (struct anv_address) { NULL, 0 };
73 sba.GeneralStateMemoryObjectControlState = GENX(MOCS);
74 sba.GeneralStateBaseAddressModifyEnable = true;
75
76 sba.SurfaceStateBaseAddress =
77 anv_cmd_buffer_surface_base_address(cmd_buffer);
78 sba.SurfaceStateMemoryObjectControlState = GENX(MOCS);
79 sba.SurfaceStateBaseAddressModifyEnable = true;
80
81 sba.DynamicStateBaseAddress =
82 (struct anv_address) { &device->dynamic_state_block_pool.bo, 0 };
83 sba.DynamicStateMemoryObjectControlState = GENX(MOCS);
84 sba.DynamicStateBaseAddressModifyEnable = true;
85
86 sba.IndirectObjectBaseAddress = (struct anv_address) { NULL, 0 };
87 sba.IndirectObjectMemoryObjectControlState = GENX(MOCS);
88 sba.IndirectObjectBaseAddressModifyEnable = true;
89
90 sba.InstructionBaseAddress =
91 (struct anv_address) { &device->instruction_block_pool.bo, 0 };
92 sba.InstructionMemoryObjectControlState = GENX(MOCS);
93 sba.InstructionBaseAddressModifyEnable = true;
94
95 # if (GEN_GEN >= 8)
96 /* Broadwell requires that we specify a buffer size for a bunch of
97 * these fields. However, since we will be growing the BO's live, we
98 * just set them all to the maximum.
99 */
100 sba.GeneralStateBufferSize = 0xfffff;
101 sba.GeneralStateBufferSizeModifyEnable = true;
102 sba.DynamicStateBufferSize = 0xfffff;
103 sba.DynamicStateBufferSizeModifyEnable = true;
104 sba.IndirectObjectBufferSize = 0xfffff;
105 sba.IndirectObjectBufferSizeModifyEnable = true;
106 sba.InstructionBufferSize = 0xfffff;
107 sba.InstructionBuffersizeModifyEnable = true;
108 # endif
109 }
110
111 /* After re-setting the surface state base address, we have to do some
112 * cache flusing so that the sampler engine will pick up the new
113 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
114 * Shared Function > 3D Sampler > State > State Caching (page 96):
115 *
116 * Coherency with system memory in the state cache, like the texture
117 * cache is handled partially by software. It is expected that the
118 * command stream or shader will issue Cache Flush operation or
119 * Cache_Flush sampler message to ensure that the L1 cache remains
120 * coherent with system memory.
121 *
122 * [...]
123 *
124 * Whenever the value of the Dynamic_State_Base_Addr,
125 * Surface_State_Base_Addr are altered, the L1 state cache must be
126 * invalidated to ensure the new surface or sampler state is fetched
127 * from system memory.
128 *
129 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
130 * which, according the PIPE_CONTROL instruction documentation in the
131 * Broadwell PRM:
132 *
133 * Setting this bit is independent of any other bit in this packet.
134 * This bit controls the invalidation of the L1 and L2 state caches
135 * at the top of the pipe i.e. at the parsing time.
136 *
137 * Unfortunately, experimentation seems to indicate that state cache
138 * invalidation through a PIPE_CONTROL does nothing whatsoever in
139 * regards to surface state and binding tables. In stead, it seems that
140 * invalidating the texture cache is what is actually needed.
141 *
142 * XXX: As far as we have been able to determine through
143 * experimentation, shows that flush the texture cache appears to be
144 * sufficient. The theory here is that all of the sampling/rendering
145 * units cache the binding table in the texture cache. However, we have
146 * yet to be able to actually confirm this.
147 */
148 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
149 pc.TextureCacheInvalidationEnable = true;
150 pc.ConstantCacheInvalidationEnable = true;
151 pc.StateCacheInvalidationEnable = true;
152 }
153 }
154
155 static void
156 add_surface_state_reloc(struct anv_cmd_buffer *cmd_buffer,
157 struct anv_state state,
158 struct anv_bo *bo, uint32_t offset)
159 {
160 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
161
162 VkResult result =
163 anv_reloc_list_add(&cmd_buffer->surface_relocs, &cmd_buffer->pool->alloc,
164 state.offset + isl_dev->ss.addr_offset, bo, offset);
165 if (result != VK_SUCCESS)
166 anv_batch_set_error(&cmd_buffer->batch, result);
167 }
168
169 static void
170 add_image_view_relocs(struct anv_cmd_buffer *cmd_buffer,
171 const struct anv_image_view *iview,
172 enum isl_aux_usage aux_usage,
173 struct anv_state state)
174 {
175 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
176
177 add_surface_state_reloc(cmd_buffer, state, iview->bo, iview->offset);
178
179 if (aux_usage != ISL_AUX_USAGE_NONE) {
180 uint32_t aux_offset = iview->offset + iview->image->aux_surface.offset;
181
182 /* On gen7 and prior, the bottom 12 bits of the MCS base address are
183 * used to store other information. This should be ok, however, because
184 * surface buffer addresses are always 4K page alinged.
185 */
186 assert((aux_offset & 0xfff) == 0);
187 uint32_t *aux_addr_dw = state.map + isl_dev->ss.aux_addr_offset;
188 aux_offset += *aux_addr_dw & 0xfff;
189
190 VkResult result =
191 anv_reloc_list_add(&cmd_buffer->surface_relocs,
192 &cmd_buffer->pool->alloc,
193 state.offset + isl_dev->ss.aux_addr_offset,
194 iview->bo, aux_offset);
195 if (result != VK_SUCCESS)
196 anv_batch_set_error(&cmd_buffer->batch, result);
197 }
198 }
199
200 static bool
201 color_is_zero_one(VkClearColorValue value, enum isl_format format)
202 {
203 if (isl_format_has_int_channel(format)) {
204 for (unsigned i = 0; i < 4; i++) {
205 if (value.int32[i] != 0 && value.int32[i] != 1)
206 return false;
207 }
208 } else {
209 for (unsigned i = 0; i < 4; i++) {
210 if (value.float32[i] != 0.0f && value.float32[i] != 1.0f)
211 return false;
212 }
213 }
214
215 return true;
216 }
217
218 static void
219 color_attachment_compute_aux_usage(struct anv_device *device,
220 struct anv_attachment_state *att_state,
221 struct anv_image_view *iview,
222 VkRect2D render_area,
223 union isl_color_value *fast_clear_color)
224 {
225 if (iview->image->aux_surface.isl.size == 0) {
226 att_state->aux_usage = ISL_AUX_USAGE_NONE;
227 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
228 att_state->fast_clear = false;
229 return;
230 } else if (iview->image->aux_usage == ISL_AUX_USAGE_MCS) {
231 att_state->aux_usage = ISL_AUX_USAGE_MCS;
232 att_state->input_aux_usage = ISL_AUX_USAGE_MCS;
233 att_state->fast_clear = false;
234 return;
235 }
236
237 assert(iview->image->aux_surface.isl.usage & ISL_SURF_USAGE_CCS_BIT);
238
239 att_state->clear_color_is_zero_one =
240 color_is_zero_one(att_state->clear_value.color, iview->isl.format);
241
242 if (att_state->pending_clear_aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
243 /* Start off assuming fast clears are possible */
244 att_state->fast_clear = true;
245
246 /* Potentially, we could do partial fast-clears but doing so has crazy
247 * alignment restrictions. It's easier to just restrict to full size
248 * fast clears for now.
249 */
250 if (render_area.offset.x != 0 ||
251 render_area.offset.y != 0 ||
252 render_area.extent.width != iview->extent.width ||
253 render_area.extent.height != iview->extent.height)
254 att_state->fast_clear = false;
255
256 if (GEN_GEN <= 7) {
257 /* On gen7, we can't do multi-LOD or multi-layer fast-clears. We
258 * technically can, but it comes with crazy restrictions that we
259 * don't want to deal with now.
260 */
261 if (iview->isl.base_level > 0 ||
262 iview->isl.base_array_layer > 0 ||
263 iview->isl.array_len > 1)
264 att_state->fast_clear = false;
265 }
266
267 /* On Broadwell and earlier, we can only handle 0/1 clear colors */
268 if (GEN_GEN <= 8 && !att_state->clear_color_is_zero_one)
269 att_state->fast_clear = false;
270
271 if (att_state->fast_clear) {
272 memcpy(fast_clear_color->u32, att_state->clear_value.color.uint32,
273 sizeof(fast_clear_color->u32));
274 }
275 } else {
276 att_state->fast_clear = false;
277 }
278
279 /**
280 * TODO: Consider using a heuristic to determine if temporarily enabling
281 * CCS_E for this image view would be beneficial.
282 *
283 * While fast-clear resolves and partial resolves are fairly cheap in the
284 * case where you render to most of the pixels, full resolves are not
285 * because they potentially involve reading and writing the entire
286 * framebuffer. If we can't texture with CCS_E, we should leave it off and
287 * limit ourselves to fast clears.
288 */
289 if (iview->image->aux_usage == ISL_AUX_USAGE_CCS_E) {
290 att_state->aux_usage = ISL_AUX_USAGE_CCS_E;
291 att_state->input_aux_usage = ISL_AUX_USAGE_CCS_E;
292 } else if (att_state->fast_clear) {
293 att_state->aux_usage = ISL_AUX_USAGE_CCS_D;
294 if (GEN_GEN >= 9 &&
295 !isl_format_supports_ccs_e(&device->info, iview->isl.format)) {
296 /* From the Sky Lake PRM, RENDER_SURFACE_STATE::AuxiliarySurfaceMode:
297 *
298 * "If Number of Multisamples is MULTISAMPLECOUNT_1, AUX_CCS_D
299 * setting is only allowed if Surface Format supported for Fast
300 * Clear. In addition, if the surface is bound to the sampling
301 * engine, Surface Format must be supported for Render Target
302 * Compression for surfaces bound to the sampling engine."
303 *
304 * In other words, we can't sample from a fast-cleared image if it
305 * doesn't also support color compression.
306 */
307 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
308 } else if (GEN_GEN >= 8) {
309 /* Broadwell/Skylake can sample from fast-cleared images */
310 att_state->input_aux_usage = ISL_AUX_USAGE_CCS_D;
311 } else {
312 /* Ivy Bridge and Haswell cannot */
313 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
314 }
315 } else {
316 att_state->aux_usage = ISL_AUX_USAGE_NONE;
317 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
318 }
319 }
320
321 static bool
322 need_input_attachment_state(const struct anv_render_pass_attachment *att)
323 {
324 if (!(att->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
325 return false;
326
327 /* We only allocate input attachment states for color surfaces. Compression
328 * is not yet enabled for depth textures and stencil doesn't allow
329 * compression so we can just use the texture surface state from the view.
330 */
331 return vk_format_is_color(att->format);
332 }
333
334 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
335 * the initial layout is undefined, the HiZ buffer and depth buffer will
336 * represent the same data at the end of this operation.
337 */
338 static void
339 transition_depth_buffer(struct anv_cmd_buffer *cmd_buffer,
340 const struct anv_image *image,
341 VkImageLayout initial_layout,
342 VkImageLayout final_layout)
343 {
344 assert(image);
345
346 /* A transition is a no-op if HiZ is not enabled, or if the initial and
347 * final layouts are equal.
348 *
349 * The undefined layout indicates that the user doesn't care about the data
350 * that's currently in the buffer. Therefore, a data-preserving resolve
351 * operation is not needed.
352 *
353 * The pre-initialized layout is equivalent to the undefined layout for
354 * optimally-tiled images. Anv only exposes support for optimally-tiled
355 * depth buffers.
356 */
357 if (image->aux_usage != ISL_AUX_USAGE_HIZ ||
358 initial_layout == final_layout ||
359 initial_layout == VK_IMAGE_LAYOUT_UNDEFINED ||
360 initial_layout == VK_IMAGE_LAYOUT_PREINITIALIZED)
361 return;
362
363 const bool hiz_enabled = ISL_AUX_USAGE_HIZ ==
364 anv_layout_to_aux_usage(&cmd_buffer->device->info, image, image->aspects,
365 initial_layout);
366 const bool enable_hiz = ISL_AUX_USAGE_HIZ ==
367 anv_layout_to_aux_usage(&cmd_buffer->device->info, image, image->aspects,
368 final_layout);
369
370 enum blorp_hiz_op hiz_op;
371 if (hiz_enabled && !enable_hiz) {
372 hiz_op = BLORP_HIZ_OP_DEPTH_RESOLVE;
373 } else if (!hiz_enabled && enable_hiz) {
374 hiz_op = BLORP_HIZ_OP_HIZ_RESOLVE;
375 } else {
376 assert(hiz_enabled == enable_hiz);
377 /* If the same buffer will be used, no resolves are necessary. */
378 hiz_op = BLORP_HIZ_OP_NONE;
379 }
380
381 if (hiz_op != BLORP_HIZ_OP_NONE)
382 anv_gen8_hiz_op_resolve(cmd_buffer, image, hiz_op);
383 }
384
385
386 /**
387 * Setup anv_cmd_state::attachments for vkCmdBeginRenderPass.
388 */
389 static VkResult
390 genX(cmd_buffer_setup_attachments)(struct anv_cmd_buffer *cmd_buffer,
391 struct anv_render_pass *pass,
392 const VkRenderPassBeginInfo *begin)
393 {
394 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
395 struct anv_cmd_state *state = &cmd_buffer->state;
396
397 vk_free(&cmd_buffer->pool->alloc, state->attachments);
398
399 if (pass->attachment_count == 0) {
400 state->attachments = NULL;
401 return VK_SUCCESS;
402 }
403
404 state->attachments = vk_alloc(&cmd_buffer->pool->alloc,
405 pass->attachment_count *
406 sizeof(state->attachments[0]),
407 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
408 if (state->attachments == NULL) {
409 /* Propagate VK_ERROR_OUT_OF_HOST_MEMORY to vkEndCommandBuffer */
410 return anv_batch_set_error(&cmd_buffer->batch,
411 VK_ERROR_OUT_OF_HOST_MEMORY);
412 }
413
414 bool need_null_state = false;
415 unsigned num_states = 0;
416 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
417 if (vk_format_is_color(pass->attachments[i].format)) {
418 num_states++;
419 } else {
420 /* We need a null state for any depth-stencil-only subpasses.
421 * Importantly, this includes depth/stencil clears so we create one
422 * whenever we have depth or stencil
423 */
424 need_null_state = true;
425 }
426
427 if (need_input_attachment_state(&pass->attachments[i]))
428 num_states++;
429 }
430 num_states += need_null_state;
431
432 const uint32_t ss_stride = align_u32(isl_dev->ss.size, isl_dev->ss.align);
433 state->render_pass_states =
434 anv_state_stream_alloc(&cmd_buffer->surface_state_stream,
435 num_states * ss_stride, isl_dev->ss.align);
436
437 struct anv_state next_state = state->render_pass_states;
438 next_state.alloc_size = isl_dev->ss.size;
439
440 if (need_null_state) {
441 state->null_surface_state = next_state;
442 next_state.offset += ss_stride;
443 next_state.map += ss_stride;
444 }
445
446 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
447 if (vk_format_is_color(pass->attachments[i].format)) {
448 state->attachments[i].color_rt_state = next_state;
449 next_state.offset += ss_stride;
450 next_state.map += ss_stride;
451 }
452
453 if (need_input_attachment_state(&pass->attachments[i])) {
454 state->attachments[i].input_att_state = next_state;
455 next_state.offset += ss_stride;
456 next_state.map += ss_stride;
457 }
458 }
459 assert(next_state.offset == state->render_pass_states.offset +
460 state->render_pass_states.alloc_size);
461
462 if (begin) {
463 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, begin->framebuffer);
464 assert(pass->attachment_count == framebuffer->attachment_count);
465
466 if (need_null_state) {
467 struct GENX(RENDER_SURFACE_STATE) null_ss = {
468 .SurfaceType = SURFTYPE_NULL,
469 .SurfaceArray = framebuffer->layers > 0,
470 .SurfaceFormat = ISL_FORMAT_R8G8B8A8_UNORM,
471 #if GEN_GEN >= 8
472 .TileMode = YMAJOR,
473 #else
474 .TiledSurface = true,
475 #endif
476 .Width = framebuffer->width - 1,
477 .Height = framebuffer->height - 1,
478 .Depth = framebuffer->layers - 1,
479 .RenderTargetViewExtent = framebuffer->layers - 1,
480 };
481 GENX(RENDER_SURFACE_STATE_pack)(NULL, state->null_surface_state.map,
482 &null_ss);
483 }
484
485 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
486 struct anv_render_pass_attachment *att = &pass->attachments[i];
487 VkImageAspectFlags att_aspects = vk_format_aspects(att->format);
488 VkImageAspectFlags clear_aspects = 0;
489
490 if (att_aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
491 /* color attachment */
492 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
493 clear_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
494 }
495 } else {
496 /* depthstencil attachment */
497 if ((att_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
498 att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
499 clear_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
500 }
501 if ((att_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
502 att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
503 clear_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
504 }
505 }
506
507 state->attachments[i].current_layout = att->initial_layout;
508 state->attachments[i].pending_clear_aspects = clear_aspects;
509 if (clear_aspects)
510 state->attachments[i].clear_value = begin->pClearValues[i];
511
512 struct anv_image_view *iview = framebuffer->attachments[i];
513 anv_assert(iview->vk_format == att->format);
514
515 union isl_color_value clear_color = { .u32 = { 0, } };
516 if (att_aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
517 color_attachment_compute_aux_usage(cmd_buffer->device,
518 &state->attachments[i],
519 iview, begin->renderArea,
520 &clear_color);
521
522 struct isl_view view = iview->isl;
523 view.usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
524 view.swizzle = anv_swizzle_for_render(view.swizzle);
525 isl_surf_fill_state(isl_dev,
526 state->attachments[i].color_rt_state.map,
527 .surf = &iview->image->color_surface.isl,
528 .view = &view,
529 .aux_surf = &iview->image->aux_surface.isl,
530 .aux_usage = state->attachments[i].aux_usage,
531 .clear_color = clear_color,
532 .mocs = cmd_buffer->device->default_mocs);
533
534 add_image_view_relocs(cmd_buffer, iview,
535 state->attachments[i].aux_usage,
536 state->attachments[i].color_rt_state);
537 } else {
538 /* This field will be initialized after the first subpass
539 * transition.
540 */
541 state->attachments[i].aux_usage = ISL_AUX_USAGE_NONE;
542
543 state->attachments[i].input_aux_usage = ISL_AUX_USAGE_NONE;
544 }
545
546 if (need_input_attachment_state(&pass->attachments[i])) {
547 struct isl_view view = iview->isl;
548 view.usage |= ISL_SURF_USAGE_TEXTURE_BIT;
549 isl_surf_fill_state(isl_dev,
550 state->attachments[i].input_att_state.map,
551 .surf = &iview->image->color_surface.isl,
552 .view = &view,
553 .aux_surf = &iview->image->aux_surface.isl,
554 .aux_usage = state->attachments[i].input_aux_usage,
555 .clear_color = clear_color,
556 .mocs = cmd_buffer->device->default_mocs);
557
558 add_image_view_relocs(cmd_buffer, iview,
559 state->attachments[i].input_aux_usage,
560 state->attachments[i].input_att_state);
561 }
562 }
563
564 anv_state_flush(cmd_buffer->device, state->render_pass_states);
565 }
566
567 return VK_SUCCESS;
568 }
569
570 VkResult
571 genX(BeginCommandBuffer)(
572 VkCommandBuffer commandBuffer,
573 const VkCommandBufferBeginInfo* pBeginInfo)
574 {
575 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
576
577 /* If this is the first vkBeginCommandBuffer, we must *initialize* the
578 * command buffer's state. Otherwise, we must *reset* its state. In both
579 * cases we reset it.
580 *
581 * From the Vulkan 1.0 spec:
582 *
583 * If a command buffer is in the executable state and the command buffer
584 * was allocated from a command pool with the
585 * VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT flag set, then
586 * vkBeginCommandBuffer implicitly resets the command buffer, behaving
587 * as if vkResetCommandBuffer had been called with
588 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT not set. It then puts
589 * the command buffer in the recording state.
590 */
591 anv_cmd_buffer_reset(cmd_buffer);
592
593 cmd_buffer->usage_flags = pBeginInfo->flags;
594
595 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY ||
596 !(cmd_buffer->usage_flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT));
597
598 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
599
600 /* We sometimes store vertex data in the dynamic state buffer for blorp
601 * operations and our dynamic state stream may re-use data from previous
602 * command buffers. In order to prevent stale cache data, we flush the VF
603 * cache. We could do this on every blorp call but that's not really
604 * needed as all of the data will get written by the CPU prior to the GPU
605 * executing anything. The chances are fairly high that they will use
606 * blorp at least once per primary command buffer so it shouldn't be
607 * wasted.
608 */
609 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY)
610 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
611
612 VkResult result = VK_SUCCESS;
613 if (cmd_buffer->usage_flags &
614 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
615 cmd_buffer->state.pass =
616 anv_render_pass_from_handle(pBeginInfo->pInheritanceInfo->renderPass);
617 cmd_buffer->state.subpass =
618 &cmd_buffer->state.pass->subpasses[pBeginInfo->pInheritanceInfo->subpass];
619 cmd_buffer->state.framebuffer = NULL;
620
621 result = genX(cmd_buffer_setup_attachments)(cmd_buffer,
622 cmd_buffer->state.pass, NULL);
623
624 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
625 }
626
627 return result;
628 }
629
630 VkResult
631 genX(EndCommandBuffer)(
632 VkCommandBuffer commandBuffer)
633 {
634 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
635
636 if (anv_batch_has_error(&cmd_buffer->batch))
637 return cmd_buffer->batch.status;
638
639 /* We want every command buffer to start with the PMA fix in a known state,
640 * so we disable it at the end of the command buffer.
641 */
642 genX(cmd_buffer_enable_pma_fix)(cmd_buffer, false);
643
644 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
645
646 anv_cmd_buffer_end_batch_buffer(cmd_buffer);
647
648 return VK_SUCCESS;
649 }
650
651 void
652 genX(CmdExecuteCommands)(
653 VkCommandBuffer commandBuffer,
654 uint32_t commandBufferCount,
655 const VkCommandBuffer* pCmdBuffers)
656 {
657 ANV_FROM_HANDLE(anv_cmd_buffer, primary, commandBuffer);
658
659 assert(primary->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
660
661 if (anv_batch_has_error(&primary->batch))
662 return;
663
664 /* The secondary command buffers will assume that the PMA fix is disabled
665 * when they begin executing. Make sure this is true.
666 */
667 genX(cmd_buffer_enable_pma_fix)(primary, false);
668
669 /* The secondary command buffer doesn't know which textures etc. have been
670 * flushed prior to their execution. Apply those flushes now.
671 */
672 genX(cmd_buffer_apply_pipe_flushes)(primary);
673
674 for (uint32_t i = 0; i < commandBufferCount; i++) {
675 ANV_FROM_HANDLE(anv_cmd_buffer, secondary, pCmdBuffers[i]);
676
677 assert(secondary->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY);
678 assert(!anv_batch_has_error(&secondary->batch));
679
680 if (secondary->usage_flags &
681 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
682 /* If we're continuing a render pass from the primary, we need to
683 * copy the surface states for the current subpass into the storage
684 * we allocated for them in BeginCommandBuffer.
685 */
686 struct anv_bo *ss_bo = &primary->device->surface_state_block_pool.bo;
687 struct anv_state src_state = primary->state.render_pass_states;
688 struct anv_state dst_state = secondary->state.render_pass_states;
689 assert(src_state.alloc_size == dst_state.alloc_size);
690
691 genX(cmd_buffer_gpu_memcpy)(primary, ss_bo, dst_state.offset,
692 ss_bo, src_state.offset,
693 src_state.alloc_size);
694 }
695
696 anv_cmd_buffer_add_secondary(primary, secondary);
697 }
698
699 /* Each of the secondary command buffers will use its own state base
700 * address. We need to re-emit state base address for the primary after
701 * all of the secondaries are done.
702 *
703 * TODO: Maybe we want to make this a dirty bit to avoid extra state base
704 * address calls?
705 */
706 genX(cmd_buffer_emit_state_base_address)(primary);
707 }
708
709 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
710 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
711 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
712
713 /**
714 * Program the hardware to use the specified L3 configuration.
715 */
716 void
717 genX(cmd_buffer_config_l3)(struct anv_cmd_buffer *cmd_buffer,
718 const struct gen_l3_config *cfg)
719 {
720 assert(cfg);
721 if (cfg == cmd_buffer->state.current_l3_config)
722 return;
723
724 if (unlikely(INTEL_DEBUG & DEBUG_L3)) {
725 fprintf(stderr, "L3 config transition: ");
726 gen_dump_l3_config(cfg, stderr);
727 }
728
729 const bool has_slm = cfg->n[GEN_L3P_SLM];
730
731 /* According to the hardware docs, the L3 partitioning can only be changed
732 * while the pipeline is completely drained and the caches are flushed,
733 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
734 */
735 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
736 pc.DCFlushEnable = true;
737 pc.PostSyncOperation = NoWrite;
738 pc.CommandStreamerStallEnable = true;
739 }
740
741 /* ...followed by a second pipelined PIPE_CONTROL that initiates
742 * invalidation of the relevant caches. Note that because RO invalidation
743 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
744 * command is processed by the CS) we cannot combine it with the previous
745 * stalling flush as the hardware documentation suggests, because that
746 * would cause the CS to stall on previous rendering *after* RO
747 * invalidation and wouldn't prevent the RO caches from being polluted by
748 * concurrent rendering before the stall completes. This intentionally
749 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
750 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
751 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
752 * already guarantee that there is no concurrent GPGPU kernel execution
753 * (see SKL HSD 2132585).
754 */
755 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
756 pc.TextureCacheInvalidationEnable = true;
757 pc.ConstantCacheInvalidationEnable = true;
758 pc.InstructionCacheInvalidateEnable = true;
759 pc.StateCacheInvalidationEnable = true;
760 pc.PostSyncOperation = NoWrite;
761 }
762
763 /* Now send a third stalling flush to make sure that invalidation is
764 * complete when the L3 configuration registers are modified.
765 */
766 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
767 pc.DCFlushEnable = true;
768 pc.PostSyncOperation = NoWrite;
769 pc.CommandStreamerStallEnable = true;
770 }
771
772 #if GEN_GEN >= 8
773
774 assert(!cfg->n[GEN_L3P_IS] && !cfg->n[GEN_L3P_C] && !cfg->n[GEN_L3P_T]);
775
776 uint32_t l3cr;
777 anv_pack_struct(&l3cr, GENX(L3CNTLREG),
778 .SLMEnable = has_slm,
779 .URBAllocation = cfg->n[GEN_L3P_URB],
780 .ROAllocation = cfg->n[GEN_L3P_RO],
781 .DCAllocation = cfg->n[GEN_L3P_DC],
782 .AllAllocation = cfg->n[GEN_L3P_ALL]);
783
784 /* Set up the L3 partitioning. */
785 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG_num), l3cr);
786
787 #else
788
789 const bool has_dc = cfg->n[GEN_L3P_DC] || cfg->n[GEN_L3P_ALL];
790 const bool has_is = cfg->n[GEN_L3P_IS] || cfg->n[GEN_L3P_RO] ||
791 cfg->n[GEN_L3P_ALL];
792 const bool has_c = cfg->n[GEN_L3P_C] || cfg->n[GEN_L3P_RO] ||
793 cfg->n[GEN_L3P_ALL];
794 const bool has_t = cfg->n[GEN_L3P_T] || cfg->n[GEN_L3P_RO] ||
795 cfg->n[GEN_L3P_ALL];
796
797 assert(!cfg->n[GEN_L3P_ALL]);
798
799 /* When enabled SLM only uses a portion of the L3 on half of the banks,
800 * the matching space on the remaining banks has to be allocated to a
801 * client (URB for all validated configurations) set to the
802 * lower-bandwidth 2-bank address hashing mode.
803 */
804 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
805 const bool urb_low_bw = has_slm && !devinfo->is_baytrail;
806 assert(!urb_low_bw || cfg->n[GEN_L3P_URB] == cfg->n[GEN_L3P_SLM]);
807
808 /* Minimum number of ways that can be allocated to the URB. */
809 MAYBE_UNUSED const unsigned n0_urb = devinfo->is_baytrail ? 32 : 0;
810 assert(cfg->n[GEN_L3P_URB] >= n0_urb);
811
812 uint32_t l3sqcr1, l3cr2, l3cr3;
813 anv_pack_struct(&l3sqcr1, GENX(L3SQCREG1),
814 .ConvertDC_UC = !has_dc,
815 .ConvertIS_UC = !has_is,
816 .ConvertC_UC = !has_c,
817 .ConvertT_UC = !has_t);
818 l3sqcr1 |=
819 GEN_IS_HASWELL ? HSW_L3SQCREG1_SQGHPCI_DEFAULT :
820 devinfo->is_baytrail ? VLV_L3SQCREG1_SQGHPCI_DEFAULT :
821 IVB_L3SQCREG1_SQGHPCI_DEFAULT;
822
823 anv_pack_struct(&l3cr2, GENX(L3CNTLREG2),
824 .SLMEnable = has_slm,
825 .URBLowBandwidth = urb_low_bw,
826 .URBAllocation = cfg->n[GEN_L3P_URB],
827 #if !GEN_IS_HASWELL
828 .ALLAllocation = cfg->n[GEN_L3P_ALL],
829 #endif
830 .ROAllocation = cfg->n[GEN_L3P_RO],
831 .DCAllocation = cfg->n[GEN_L3P_DC]);
832
833 anv_pack_struct(&l3cr3, GENX(L3CNTLREG3),
834 .ISAllocation = cfg->n[GEN_L3P_IS],
835 .ISLowBandwidth = 0,
836 .CAllocation = cfg->n[GEN_L3P_C],
837 .CLowBandwidth = 0,
838 .TAllocation = cfg->n[GEN_L3P_T],
839 .TLowBandwidth = 0);
840
841 /* Set up the L3 partitioning. */
842 emit_lri(&cmd_buffer->batch, GENX(L3SQCREG1_num), l3sqcr1);
843 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG2_num), l3cr2);
844 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG3_num), l3cr3);
845
846 #if GEN_IS_HASWELL
847 if (cmd_buffer->device->instance->physicalDevice.cmd_parser_version >= 4) {
848 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
849 * them disabled to avoid crashing the system hard.
850 */
851 uint32_t scratch1, chicken3;
852 anv_pack_struct(&scratch1, GENX(SCRATCH1),
853 .L3AtomicDisable = !has_dc);
854 anv_pack_struct(&chicken3, GENX(CHICKEN3),
855 .L3AtomicDisableMask = true,
856 .L3AtomicDisable = !has_dc);
857 emit_lri(&cmd_buffer->batch, GENX(SCRATCH1_num), scratch1);
858 emit_lri(&cmd_buffer->batch, GENX(CHICKEN3_num), chicken3);
859 }
860 #endif
861
862 #endif
863
864 cmd_buffer->state.current_l3_config = cfg;
865 }
866
867 void
868 genX(cmd_buffer_apply_pipe_flushes)(struct anv_cmd_buffer *cmd_buffer)
869 {
870 enum anv_pipe_bits bits = cmd_buffer->state.pending_pipe_bits;
871
872 /* Flushes are pipelined while invalidations are handled immediately.
873 * Therefore, if we're flushing anything then we need to schedule a stall
874 * before any invalidations can happen.
875 */
876 if (bits & ANV_PIPE_FLUSH_BITS)
877 bits |= ANV_PIPE_NEEDS_CS_STALL_BIT;
878
879 /* If we're going to do an invalidate and we have a pending CS stall that
880 * has yet to be resolved, we do the CS stall now.
881 */
882 if ((bits & ANV_PIPE_INVALIDATE_BITS) &&
883 (bits & ANV_PIPE_NEEDS_CS_STALL_BIT)) {
884 bits |= ANV_PIPE_CS_STALL_BIT;
885 bits &= ~ANV_PIPE_NEEDS_CS_STALL_BIT;
886 }
887
888 if (bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT)) {
889 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
890 pipe.DepthCacheFlushEnable = bits & ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
891 pipe.DCFlushEnable = bits & ANV_PIPE_DATA_CACHE_FLUSH_BIT;
892 pipe.RenderTargetCacheFlushEnable =
893 bits & ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
894
895 pipe.DepthStallEnable = bits & ANV_PIPE_DEPTH_STALL_BIT;
896 pipe.CommandStreamerStallEnable = bits & ANV_PIPE_CS_STALL_BIT;
897 pipe.StallAtPixelScoreboard = bits & ANV_PIPE_STALL_AT_SCOREBOARD_BIT;
898
899 /*
900 * According to the Broadwell documentation, any PIPE_CONTROL with the
901 * "Command Streamer Stall" bit set must also have another bit set,
902 * with five different options:
903 *
904 * - Render Target Cache Flush
905 * - Depth Cache Flush
906 * - Stall at Pixel Scoreboard
907 * - Post-Sync Operation
908 * - Depth Stall
909 * - DC Flush Enable
910 *
911 * I chose "Stall at Pixel Scoreboard" since that's what we use in
912 * mesa and it seems to work fine. The choice is fairly arbitrary.
913 */
914 if ((bits & ANV_PIPE_CS_STALL_BIT) &&
915 !(bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_DEPTH_STALL_BIT |
916 ANV_PIPE_STALL_AT_SCOREBOARD_BIT)))
917 pipe.StallAtPixelScoreboard = true;
918 }
919
920 bits &= ~(ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT);
921 }
922
923 if (bits & ANV_PIPE_INVALIDATE_BITS) {
924 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
925 pipe.StateCacheInvalidationEnable =
926 bits & ANV_PIPE_STATE_CACHE_INVALIDATE_BIT;
927 pipe.ConstantCacheInvalidationEnable =
928 bits & ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT;
929 pipe.VFCacheInvalidationEnable =
930 bits & ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
931 pipe.TextureCacheInvalidationEnable =
932 bits & ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
933 pipe.InstructionCacheInvalidateEnable =
934 bits & ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT;
935 }
936
937 bits &= ~ANV_PIPE_INVALIDATE_BITS;
938 }
939
940 cmd_buffer->state.pending_pipe_bits = bits;
941 }
942
943 void genX(CmdPipelineBarrier)(
944 VkCommandBuffer commandBuffer,
945 VkPipelineStageFlags srcStageMask,
946 VkPipelineStageFlags destStageMask,
947 VkBool32 byRegion,
948 uint32_t memoryBarrierCount,
949 const VkMemoryBarrier* pMemoryBarriers,
950 uint32_t bufferMemoryBarrierCount,
951 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
952 uint32_t imageMemoryBarrierCount,
953 const VkImageMemoryBarrier* pImageMemoryBarriers)
954 {
955 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
956
957 /* XXX: Right now, we're really dumb and just flush whatever categories
958 * the app asks for. One of these days we may make this a bit better
959 * but right now that's all the hardware allows for in most areas.
960 */
961 VkAccessFlags src_flags = 0;
962 VkAccessFlags dst_flags = 0;
963
964 for (uint32_t i = 0; i < memoryBarrierCount; i++) {
965 src_flags |= pMemoryBarriers[i].srcAccessMask;
966 dst_flags |= pMemoryBarriers[i].dstAccessMask;
967 }
968
969 for (uint32_t i = 0; i < bufferMemoryBarrierCount; i++) {
970 src_flags |= pBufferMemoryBarriers[i].srcAccessMask;
971 dst_flags |= pBufferMemoryBarriers[i].dstAccessMask;
972 }
973
974 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
975 src_flags |= pImageMemoryBarriers[i].srcAccessMask;
976 dst_flags |= pImageMemoryBarriers[i].dstAccessMask;
977 ANV_FROM_HANDLE(anv_image, image, pImageMemoryBarriers[i].image);
978 if (pImageMemoryBarriers[i].subresourceRange.aspectMask &
979 VK_IMAGE_ASPECT_DEPTH_BIT) {
980 transition_depth_buffer(cmd_buffer, image,
981 pImageMemoryBarriers[i].oldLayout,
982 pImageMemoryBarriers[i].newLayout);
983 }
984 }
985
986 cmd_buffer->state.pending_pipe_bits |=
987 anv_pipe_flush_bits_for_access_flags(src_flags) |
988 anv_pipe_invalidate_bits_for_access_flags(dst_flags);
989 }
990
991 static void
992 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer *cmd_buffer)
993 {
994 VkShaderStageFlags stages = cmd_buffer->state.pipeline->active_stages;
995
996 /* In order to avoid thrash, we assume that vertex and fragment stages
997 * always exist. In the rare case where one is missing *and* the other
998 * uses push concstants, this may be suboptimal. However, avoiding stalls
999 * seems more important.
1000 */
1001 stages |= VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT;
1002
1003 if (stages == cmd_buffer->state.push_constant_stages)
1004 return;
1005
1006 #if GEN_GEN >= 8
1007 const unsigned push_constant_kb = 32;
1008 #elif GEN_IS_HASWELL
1009 const unsigned push_constant_kb = cmd_buffer->device->info.gt == 3 ? 32 : 16;
1010 #else
1011 const unsigned push_constant_kb = 16;
1012 #endif
1013
1014 const unsigned num_stages =
1015 _mesa_bitcount(stages & VK_SHADER_STAGE_ALL_GRAPHICS);
1016 unsigned size_per_stage = push_constant_kb / num_stages;
1017
1018 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
1019 * units of 2KB. Incidentally, these are the same platforms that have
1020 * 32KB worth of push constant space.
1021 */
1022 if (push_constant_kb == 32)
1023 size_per_stage &= ~1u;
1024
1025 uint32_t kb_used = 0;
1026 for (int i = MESA_SHADER_VERTEX; i < MESA_SHADER_FRAGMENT; i++) {
1027 unsigned push_size = (stages & (1 << i)) ? size_per_stage : 0;
1028 anv_batch_emit(&cmd_buffer->batch,
1029 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
1030 alloc._3DCommandSubOpcode = 18 + i;
1031 alloc.ConstantBufferOffset = (push_size > 0) ? kb_used : 0;
1032 alloc.ConstantBufferSize = push_size;
1033 }
1034 kb_used += push_size;
1035 }
1036
1037 anv_batch_emit(&cmd_buffer->batch,
1038 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS), alloc) {
1039 alloc.ConstantBufferOffset = kb_used;
1040 alloc.ConstantBufferSize = push_constant_kb - kb_used;
1041 }
1042
1043 cmd_buffer->state.push_constant_stages = stages;
1044
1045 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
1046 *
1047 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
1048 * the next 3DPRIMITIVE command after programming the
1049 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
1050 *
1051 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
1052 * pipeline setup, we need to dirty push constants.
1053 */
1054 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_ALL_GRAPHICS;
1055 }
1056
1057 static VkResult
1058 emit_binding_table(struct anv_cmd_buffer *cmd_buffer,
1059 gl_shader_stage stage,
1060 struct anv_state *bt_state)
1061 {
1062 struct anv_subpass *subpass = cmd_buffer->state.subpass;
1063 struct anv_pipeline *pipeline;
1064 uint32_t bias, state_offset;
1065
1066 switch (stage) {
1067 case MESA_SHADER_COMPUTE:
1068 pipeline = cmd_buffer->state.compute_pipeline;
1069 bias = 1;
1070 break;
1071 default:
1072 pipeline = cmd_buffer->state.pipeline;
1073 bias = 0;
1074 break;
1075 }
1076
1077 if (!anv_pipeline_has_stage(pipeline, stage)) {
1078 *bt_state = (struct anv_state) { 0, };
1079 return VK_SUCCESS;
1080 }
1081
1082 struct anv_pipeline_bind_map *map = &pipeline->shaders[stage]->bind_map;
1083 if (bias + map->surface_count == 0) {
1084 *bt_state = (struct anv_state) { 0, };
1085 return VK_SUCCESS;
1086 }
1087
1088 *bt_state = anv_cmd_buffer_alloc_binding_table(cmd_buffer,
1089 bias + map->surface_count,
1090 &state_offset);
1091 uint32_t *bt_map = bt_state->map;
1092
1093 if (bt_state->map == NULL)
1094 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
1095
1096 if (stage == MESA_SHADER_COMPUTE &&
1097 get_cs_prog_data(cmd_buffer->state.compute_pipeline)->uses_num_work_groups) {
1098 struct anv_bo *bo = cmd_buffer->state.num_workgroups_bo;
1099 uint32_t bo_offset = cmd_buffer->state.num_workgroups_offset;
1100
1101 struct anv_state surface_state;
1102 surface_state =
1103 anv_cmd_buffer_alloc_surface_state(cmd_buffer);
1104
1105 const enum isl_format format =
1106 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
1107 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
1108 format, bo_offset, 12, 1);
1109
1110 bt_map[0] = surface_state.offset + state_offset;
1111 add_surface_state_reloc(cmd_buffer, surface_state, bo, bo_offset);
1112 }
1113
1114 if (map->surface_count == 0)
1115 goto out;
1116
1117 if (map->image_count > 0) {
1118 VkResult result =
1119 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, stage, images);
1120 if (result != VK_SUCCESS)
1121 return result;
1122
1123 cmd_buffer->state.push_constants_dirty |= 1 << stage;
1124 }
1125
1126 uint32_t image = 0;
1127 for (uint32_t s = 0; s < map->surface_count; s++) {
1128 struct anv_pipeline_binding *binding = &map->surface_to_descriptor[s];
1129
1130 struct anv_state surface_state;
1131
1132 if (binding->set == ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS) {
1133 /* Color attachment binding */
1134 assert(stage == MESA_SHADER_FRAGMENT);
1135 assert(binding->binding == 0);
1136 if (binding->index < subpass->color_count) {
1137 const unsigned att = subpass->color_attachments[binding->index].attachment;
1138 surface_state = cmd_buffer->state.attachments[att].color_rt_state;
1139 } else {
1140 surface_state = cmd_buffer->state.null_surface_state;
1141 }
1142
1143 bt_map[bias + s] = surface_state.offset + state_offset;
1144 continue;
1145 }
1146
1147 struct anv_descriptor_set *set =
1148 cmd_buffer->state.descriptors[binding->set];
1149 uint32_t offset = set->layout->binding[binding->binding].descriptor_index;
1150 struct anv_descriptor *desc = &set->descriptors[offset + binding->index];
1151
1152 switch (desc->type) {
1153 case VK_DESCRIPTOR_TYPE_SAMPLER:
1154 /* Nothing for us to do here */
1155 continue;
1156
1157 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1158 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1159 surface_state = desc->aux_usage == ISL_AUX_USAGE_NONE ?
1160 desc->image_view->no_aux_sampler_surface_state :
1161 desc->image_view->sampler_surface_state;
1162 assert(surface_state.alloc_size);
1163 add_image_view_relocs(cmd_buffer, desc->image_view,
1164 desc->aux_usage, surface_state);
1165 break;
1166 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1167 assert(stage == MESA_SHADER_FRAGMENT);
1168 if (desc->image_view->aspect_mask != VK_IMAGE_ASPECT_COLOR_BIT) {
1169 /* For depth and stencil input attachments, we treat it like any
1170 * old texture that a user may have bound.
1171 */
1172 surface_state = desc->aux_usage == ISL_AUX_USAGE_NONE ?
1173 desc->image_view->no_aux_sampler_surface_state :
1174 desc->image_view->sampler_surface_state;
1175 assert(surface_state.alloc_size);
1176 add_image_view_relocs(cmd_buffer, desc->image_view,
1177 desc->aux_usage, surface_state);
1178 } else {
1179 /* For color input attachments, we create the surface state at
1180 * vkBeginRenderPass time so that we can include aux and clear
1181 * color information.
1182 */
1183 assert(binding->input_attachment_index < subpass->input_count);
1184 const unsigned subpass_att = binding->input_attachment_index;
1185 const unsigned att = subpass->input_attachments[subpass_att].attachment;
1186 surface_state = cmd_buffer->state.attachments[att].input_att_state;
1187 }
1188 break;
1189
1190 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1191 surface_state = (binding->write_only)
1192 ? desc->image_view->writeonly_storage_surface_state
1193 : desc->image_view->storage_surface_state;
1194 assert(surface_state.alloc_size);
1195 add_image_view_relocs(cmd_buffer, desc->image_view,
1196 desc->image_view->image->aux_usage,
1197 surface_state);
1198
1199 struct brw_image_param *image_param =
1200 &cmd_buffer->state.push_constants[stage]->images[image++];
1201
1202 *image_param = desc->image_view->storage_image_param;
1203 image_param->surface_idx = bias + s;
1204 break;
1205 }
1206
1207 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1208 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1209 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1210 surface_state = desc->buffer_view->surface_state;
1211 assert(surface_state.alloc_size);
1212 add_surface_state_reloc(cmd_buffer, surface_state,
1213 desc->buffer_view->bo,
1214 desc->buffer_view->offset);
1215 break;
1216
1217 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1218 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1219 uint32_t dynamic_offset_idx =
1220 pipeline->layout->set[binding->set].dynamic_offset_start +
1221 set->layout->binding[binding->binding].dynamic_offset_index +
1222 binding->index;
1223
1224 /* Compute the offset within the buffer */
1225 uint64_t offset = desc->offset +
1226 cmd_buffer->state.dynamic_offsets[dynamic_offset_idx];
1227 /* Clamp to the buffer size */
1228 offset = MIN2(offset, desc->buffer->size);
1229 /* Clamp the range to the buffer size */
1230 uint32_t range = MIN2(desc->range, desc->buffer->size - offset);
1231
1232 surface_state =
1233 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
1234 enum isl_format format =
1235 anv_isl_format_for_descriptor_type(desc->type);
1236
1237 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
1238 format, offset, range, 1);
1239 add_surface_state_reloc(cmd_buffer, surface_state,
1240 desc->buffer->bo,
1241 desc->buffer->offset + offset);
1242 break;
1243 }
1244
1245 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1246 surface_state = (binding->write_only)
1247 ? desc->buffer_view->writeonly_storage_surface_state
1248 : desc->buffer_view->storage_surface_state;
1249 assert(surface_state.alloc_size);
1250 add_surface_state_reloc(cmd_buffer, surface_state,
1251 desc->buffer_view->bo,
1252 desc->buffer_view->offset);
1253
1254 struct brw_image_param *image_param =
1255 &cmd_buffer->state.push_constants[stage]->images[image++];
1256
1257 *image_param = desc->buffer_view->storage_image_param;
1258 image_param->surface_idx = bias + s;
1259 break;
1260
1261 default:
1262 assert(!"Invalid descriptor type");
1263 continue;
1264 }
1265
1266 bt_map[bias + s] = surface_state.offset + state_offset;
1267 }
1268 assert(image == map->image_count);
1269
1270 out:
1271 anv_state_flush(cmd_buffer->device, *bt_state);
1272
1273 return VK_SUCCESS;
1274 }
1275
1276 static VkResult
1277 emit_samplers(struct anv_cmd_buffer *cmd_buffer,
1278 gl_shader_stage stage,
1279 struct anv_state *state)
1280 {
1281 struct anv_pipeline *pipeline;
1282
1283 if (stage == MESA_SHADER_COMPUTE)
1284 pipeline = cmd_buffer->state.compute_pipeline;
1285 else
1286 pipeline = cmd_buffer->state.pipeline;
1287
1288 if (!anv_pipeline_has_stage(pipeline, stage)) {
1289 *state = (struct anv_state) { 0, };
1290 return VK_SUCCESS;
1291 }
1292
1293 struct anv_pipeline_bind_map *map = &pipeline->shaders[stage]->bind_map;
1294 if (map->sampler_count == 0) {
1295 *state = (struct anv_state) { 0, };
1296 return VK_SUCCESS;
1297 }
1298
1299 uint32_t size = map->sampler_count * 16;
1300 *state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, 32);
1301
1302 if (state->map == NULL)
1303 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
1304
1305 for (uint32_t s = 0; s < map->sampler_count; s++) {
1306 struct anv_pipeline_binding *binding = &map->sampler_to_descriptor[s];
1307 struct anv_descriptor_set *set =
1308 cmd_buffer->state.descriptors[binding->set];
1309 uint32_t offset = set->layout->binding[binding->binding].descriptor_index;
1310 struct anv_descriptor *desc = &set->descriptors[offset + binding->index];
1311
1312 if (desc->type != VK_DESCRIPTOR_TYPE_SAMPLER &&
1313 desc->type != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
1314 continue;
1315
1316 struct anv_sampler *sampler = desc->sampler;
1317
1318 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
1319 * happens to be zero.
1320 */
1321 if (sampler == NULL)
1322 continue;
1323
1324 memcpy(state->map + (s * 16),
1325 sampler->state, sizeof(sampler->state));
1326 }
1327
1328 anv_state_flush(cmd_buffer->device, *state);
1329
1330 return VK_SUCCESS;
1331 }
1332
1333 static uint32_t
1334 flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer)
1335 {
1336 VkShaderStageFlags dirty = cmd_buffer->state.descriptors_dirty &
1337 cmd_buffer->state.pipeline->active_stages;
1338
1339 VkResult result = VK_SUCCESS;
1340 anv_foreach_stage(s, dirty) {
1341 result = emit_samplers(cmd_buffer, s, &cmd_buffer->state.samplers[s]);
1342 if (result != VK_SUCCESS)
1343 break;
1344 result = emit_binding_table(cmd_buffer, s,
1345 &cmd_buffer->state.binding_tables[s]);
1346 if (result != VK_SUCCESS)
1347 break;
1348 }
1349
1350 if (result != VK_SUCCESS) {
1351 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
1352
1353 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
1354 if (result != VK_SUCCESS)
1355 return 0;
1356
1357 /* Re-emit state base addresses so we get the new surface state base
1358 * address before we start emitting binding tables etc.
1359 */
1360 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
1361
1362 /* Re-emit all active binding tables */
1363 dirty |= cmd_buffer->state.pipeline->active_stages;
1364 anv_foreach_stage(s, dirty) {
1365 result = emit_samplers(cmd_buffer, s, &cmd_buffer->state.samplers[s]);
1366 if (result != VK_SUCCESS) {
1367 anv_batch_set_error(&cmd_buffer->batch, result);
1368 return 0;
1369 }
1370 result = emit_binding_table(cmd_buffer, s,
1371 &cmd_buffer->state.binding_tables[s]);
1372 if (result != VK_SUCCESS) {
1373 anv_batch_set_error(&cmd_buffer->batch, result);
1374 return 0;
1375 }
1376 }
1377 }
1378
1379 cmd_buffer->state.descriptors_dirty &= ~dirty;
1380
1381 return dirty;
1382 }
1383
1384 static void
1385 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer *cmd_buffer,
1386 uint32_t stages)
1387 {
1388 static const uint32_t sampler_state_opcodes[] = {
1389 [MESA_SHADER_VERTEX] = 43,
1390 [MESA_SHADER_TESS_CTRL] = 44, /* HS */
1391 [MESA_SHADER_TESS_EVAL] = 45, /* DS */
1392 [MESA_SHADER_GEOMETRY] = 46,
1393 [MESA_SHADER_FRAGMENT] = 47,
1394 [MESA_SHADER_COMPUTE] = 0,
1395 };
1396
1397 static const uint32_t binding_table_opcodes[] = {
1398 [MESA_SHADER_VERTEX] = 38,
1399 [MESA_SHADER_TESS_CTRL] = 39,
1400 [MESA_SHADER_TESS_EVAL] = 40,
1401 [MESA_SHADER_GEOMETRY] = 41,
1402 [MESA_SHADER_FRAGMENT] = 42,
1403 [MESA_SHADER_COMPUTE] = 0,
1404 };
1405
1406 anv_foreach_stage(s, stages) {
1407 if (cmd_buffer->state.samplers[s].alloc_size > 0) {
1408 anv_batch_emit(&cmd_buffer->batch,
1409 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ssp) {
1410 ssp._3DCommandSubOpcode = sampler_state_opcodes[s];
1411 ssp.PointertoVSSamplerState = cmd_buffer->state.samplers[s].offset;
1412 }
1413 }
1414
1415 /* Always emit binding table pointers if we're asked to, since on SKL
1416 * this is what flushes push constants. */
1417 anv_batch_emit(&cmd_buffer->batch,
1418 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), btp) {
1419 btp._3DCommandSubOpcode = binding_table_opcodes[s];
1420 btp.PointertoVSBindingTable = cmd_buffer->state.binding_tables[s].offset;
1421 }
1422 }
1423 }
1424
1425 static uint32_t
1426 cmd_buffer_flush_push_constants(struct anv_cmd_buffer *cmd_buffer)
1427 {
1428 static const uint32_t push_constant_opcodes[] = {
1429 [MESA_SHADER_VERTEX] = 21,
1430 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
1431 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
1432 [MESA_SHADER_GEOMETRY] = 22,
1433 [MESA_SHADER_FRAGMENT] = 23,
1434 [MESA_SHADER_COMPUTE] = 0,
1435 };
1436
1437 VkShaderStageFlags flushed = 0;
1438
1439 anv_foreach_stage(stage, cmd_buffer->state.push_constants_dirty) {
1440 if (stage == MESA_SHADER_COMPUTE)
1441 continue;
1442
1443 struct anv_state state = anv_cmd_buffer_push_constants(cmd_buffer, stage);
1444
1445 if (state.offset == 0) {
1446 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CONSTANT_VS), c)
1447 c._3DCommandSubOpcode = push_constant_opcodes[stage];
1448 } else {
1449 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CONSTANT_VS), c) {
1450 c._3DCommandSubOpcode = push_constant_opcodes[stage],
1451 c.ConstantBody = (struct GENX(3DSTATE_CONSTANT_BODY)) {
1452 #if GEN_GEN >= 9
1453 .PointerToConstantBuffer2 = { &cmd_buffer->device->dynamic_state_block_pool.bo, state.offset },
1454 .ConstantBuffer2ReadLength = DIV_ROUND_UP(state.alloc_size, 32),
1455 #else
1456 .PointerToConstantBuffer0 = { .offset = state.offset },
1457 .ConstantBuffer0ReadLength = DIV_ROUND_UP(state.alloc_size, 32),
1458 #endif
1459 };
1460 }
1461 }
1462
1463 flushed |= mesa_to_vk_shader_stage(stage);
1464 }
1465
1466 cmd_buffer->state.push_constants_dirty &= ~VK_SHADER_STAGE_ALL_GRAPHICS;
1467
1468 return flushed;
1469 }
1470
1471 void
1472 genX(cmd_buffer_flush_state)(struct anv_cmd_buffer *cmd_buffer)
1473 {
1474 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
1475 uint32_t *p;
1476
1477 uint32_t vb_emit = cmd_buffer->state.vb_dirty & pipeline->vb_used;
1478
1479 assert((pipeline->active_stages & VK_SHADER_STAGE_COMPUTE_BIT) == 0);
1480
1481 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
1482
1483 genX(flush_pipeline_select_3d)(cmd_buffer);
1484
1485 if (vb_emit) {
1486 const uint32_t num_buffers = __builtin_popcount(vb_emit);
1487 const uint32_t num_dwords = 1 + num_buffers * 4;
1488
1489 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
1490 GENX(3DSTATE_VERTEX_BUFFERS));
1491 uint32_t vb, i = 0;
1492 for_each_bit(vb, vb_emit) {
1493 struct anv_buffer *buffer = cmd_buffer->state.vertex_bindings[vb].buffer;
1494 uint32_t offset = cmd_buffer->state.vertex_bindings[vb].offset;
1495
1496 struct GENX(VERTEX_BUFFER_STATE) state = {
1497 .VertexBufferIndex = vb,
1498
1499 #if GEN_GEN >= 8
1500 .MemoryObjectControlState = GENX(MOCS),
1501 #else
1502 .BufferAccessType = pipeline->instancing_enable[vb] ? INSTANCEDATA : VERTEXDATA,
1503 .InstanceDataStepRate = 1,
1504 .VertexBufferMemoryObjectControlState = GENX(MOCS),
1505 #endif
1506
1507 .AddressModifyEnable = true,
1508 .BufferPitch = pipeline->binding_stride[vb],
1509 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
1510
1511 #if GEN_GEN >= 8
1512 .BufferSize = buffer->size - offset
1513 #else
1514 .EndAddress = { buffer->bo, buffer->offset + buffer->size - 1},
1515 #endif
1516 };
1517
1518 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, &p[1 + i * 4], &state);
1519 i++;
1520 }
1521 }
1522
1523 cmd_buffer->state.vb_dirty &= ~vb_emit;
1524
1525 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_PIPELINE) {
1526 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
1527
1528 /* The exact descriptor layout is pulled from the pipeline, so we need
1529 * to re-emit binding tables on every pipeline change.
1530 */
1531 cmd_buffer->state.descriptors_dirty |=
1532 cmd_buffer->state.pipeline->active_stages;
1533
1534 /* If the pipeline changed, we may need to re-allocate push constant
1535 * space in the URB.
1536 */
1537 cmd_buffer_alloc_push_constants(cmd_buffer);
1538 }
1539
1540 #if GEN_GEN <= 7
1541 if (cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_VERTEX_BIT ||
1542 cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_VERTEX_BIT) {
1543 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
1544 *
1545 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
1546 * stall needs to be sent just prior to any 3DSTATE_VS,
1547 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
1548 * 3DSTATE_BINDING_TABLE_POINTER_VS,
1549 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
1550 * PIPE_CONTROL needs to be sent before any combination of VS
1551 * associated 3DSTATE."
1552 */
1553 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1554 pc.DepthStallEnable = true;
1555 pc.PostSyncOperation = WriteImmediateData;
1556 pc.Address =
1557 (struct anv_address) { &cmd_buffer->device->workaround_bo, 0 };
1558 }
1559 }
1560 #endif
1561
1562 /* Render targets live in the same binding table as fragment descriptors */
1563 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_RENDER_TARGETS)
1564 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_FRAGMENT_BIT;
1565
1566 /* We emit the binding tables and sampler tables first, then emit push
1567 * constants and then finally emit binding table and sampler table
1568 * pointers. It has to happen in this order, since emitting the binding
1569 * tables may change the push constants (in case of storage images). After
1570 * emitting push constants, on SKL+ we have to emit the corresponding
1571 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
1572 */
1573 uint32_t dirty = 0;
1574 if (cmd_buffer->state.descriptors_dirty)
1575 dirty = flush_descriptor_sets(cmd_buffer);
1576
1577 if (cmd_buffer->state.push_constants_dirty) {
1578 #if GEN_GEN >= 9
1579 /* On Sky Lake and later, the binding table pointers commands are
1580 * what actually flush the changes to push constant state so we need
1581 * to dirty them so they get re-emitted below.
1582 */
1583 dirty |= cmd_buffer_flush_push_constants(cmd_buffer);
1584 #else
1585 cmd_buffer_flush_push_constants(cmd_buffer);
1586 #endif
1587 }
1588
1589 if (dirty)
1590 cmd_buffer_emit_descriptor_pointers(cmd_buffer, dirty);
1591
1592 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_DYNAMIC_VIEWPORT)
1593 gen8_cmd_buffer_emit_viewport(cmd_buffer);
1594
1595 if (cmd_buffer->state.dirty & (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT |
1596 ANV_CMD_DIRTY_PIPELINE)) {
1597 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer,
1598 pipeline->depth_clamp_enable);
1599 }
1600
1601 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_DYNAMIC_SCISSOR)
1602 gen7_cmd_buffer_emit_scissor(cmd_buffer);
1603
1604 genX(cmd_buffer_flush_dynamic_state)(cmd_buffer);
1605
1606 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
1607 }
1608
1609 static void
1610 emit_vertex_bo(struct anv_cmd_buffer *cmd_buffer,
1611 struct anv_bo *bo, uint32_t offset,
1612 uint32_t size, uint32_t index)
1613 {
1614 uint32_t *p = anv_batch_emitn(&cmd_buffer->batch, 5,
1615 GENX(3DSTATE_VERTEX_BUFFERS));
1616
1617 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, p + 1,
1618 &(struct GENX(VERTEX_BUFFER_STATE)) {
1619 .VertexBufferIndex = index,
1620 .AddressModifyEnable = true,
1621 .BufferPitch = 0,
1622 #if (GEN_GEN >= 8)
1623 .MemoryObjectControlState = GENX(MOCS),
1624 .BufferStartingAddress = { bo, offset },
1625 .BufferSize = size
1626 #else
1627 .VertexBufferMemoryObjectControlState = GENX(MOCS),
1628 .BufferStartingAddress = { bo, offset },
1629 .EndAddress = { bo, offset + size },
1630 #endif
1631 });
1632 }
1633
1634 static void
1635 emit_base_vertex_instance_bo(struct anv_cmd_buffer *cmd_buffer,
1636 struct anv_bo *bo, uint32_t offset)
1637 {
1638 emit_vertex_bo(cmd_buffer, bo, offset, 8, ANV_SVGS_VB_INDEX);
1639 }
1640
1641 static void
1642 emit_base_vertex_instance(struct anv_cmd_buffer *cmd_buffer,
1643 uint32_t base_vertex, uint32_t base_instance)
1644 {
1645 struct anv_state id_state =
1646 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 8, 4);
1647
1648 ((uint32_t *)id_state.map)[0] = base_vertex;
1649 ((uint32_t *)id_state.map)[1] = base_instance;
1650
1651 anv_state_flush(cmd_buffer->device, id_state);
1652
1653 emit_base_vertex_instance_bo(cmd_buffer,
1654 &cmd_buffer->device->dynamic_state_block_pool.bo, id_state.offset);
1655 }
1656
1657 static void
1658 emit_draw_index(struct anv_cmd_buffer *cmd_buffer, uint32_t draw_index)
1659 {
1660 struct anv_state state =
1661 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 4, 4);
1662
1663 ((uint32_t *)state.map)[0] = draw_index;
1664
1665 anv_state_flush(cmd_buffer->device, state);
1666
1667 emit_vertex_bo(cmd_buffer,
1668 &cmd_buffer->device->dynamic_state_block_pool.bo,
1669 state.offset, 4, ANV_DRAWID_VB_INDEX);
1670 }
1671
1672 void genX(CmdDraw)(
1673 VkCommandBuffer commandBuffer,
1674 uint32_t vertexCount,
1675 uint32_t instanceCount,
1676 uint32_t firstVertex,
1677 uint32_t firstInstance)
1678 {
1679 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1680 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
1681 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
1682
1683 if (anv_batch_has_error(&cmd_buffer->batch))
1684 return;
1685
1686 genX(cmd_buffer_flush_state)(cmd_buffer);
1687
1688 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
1689 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
1690 if (vs_prog_data->uses_drawid)
1691 emit_draw_index(cmd_buffer, 0);
1692
1693 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
1694 prim.VertexAccessType = SEQUENTIAL;
1695 prim.PrimitiveTopologyType = pipeline->topology;
1696 prim.VertexCountPerInstance = vertexCount;
1697 prim.StartVertexLocation = firstVertex;
1698 prim.InstanceCount = instanceCount;
1699 prim.StartInstanceLocation = firstInstance;
1700 prim.BaseVertexLocation = 0;
1701 }
1702 }
1703
1704 void genX(CmdDrawIndexed)(
1705 VkCommandBuffer commandBuffer,
1706 uint32_t indexCount,
1707 uint32_t instanceCount,
1708 uint32_t firstIndex,
1709 int32_t vertexOffset,
1710 uint32_t firstInstance)
1711 {
1712 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1713 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
1714 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
1715
1716 if (anv_batch_has_error(&cmd_buffer->batch))
1717 return;
1718
1719 genX(cmd_buffer_flush_state)(cmd_buffer);
1720
1721 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
1722 emit_base_vertex_instance(cmd_buffer, vertexOffset, firstInstance);
1723 if (vs_prog_data->uses_drawid)
1724 emit_draw_index(cmd_buffer, 0);
1725
1726 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
1727 prim.VertexAccessType = RANDOM;
1728 prim.PrimitiveTopologyType = pipeline->topology;
1729 prim.VertexCountPerInstance = indexCount;
1730 prim.StartVertexLocation = firstIndex;
1731 prim.InstanceCount = instanceCount;
1732 prim.StartInstanceLocation = firstInstance;
1733 prim.BaseVertexLocation = vertexOffset;
1734 }
1735 }
1736
1737 /* Auto-Draw / Indirect Registers */
1738 #define GEN7_3DPRIM_END_OFFSET 0x2420
1739 #define GEN7_3DPRIM_START_VERTEX 0x2430
1740 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
1741 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
1742 #define GEN7_3DPRIM_START_INSTANCE 0x243C
1743 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
1744
1745 void genX(CmdDrawIndirect)(
1746 VkCommandBuffer commandBuffer,
1747 VkBuffer _buffer,
1748 VkDeviceSize offset,
1749 uint32_t drawCount,
1750 uint32_t stride)
1751 {
1752 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1753 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1754 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
1755 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
1756 struct anv_bo *bo = buffer->bo;
1757 uint32_t bo_offset = buffer->offset + offset;
1758
1759 if (anv_batch_has_error(&cmd_buffer->batch))
1760 return;
1761
1762 genX(cmd_buffer_flush_state)(cmd_buffer);
1763
1764 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
1765 emit_base_vertex_instance_bo(cmd_buffer, bo, bo_offset + 8);
1766 if (vs_prog_data->uses_drawid)
1767 emit_draw_index(cmd_buffer, 0);
1768
1769 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
1770 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
1771 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
1772 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 12);
1773 emit_lri(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, 0);
1774
1775 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
1776 prim.IndirectParameterEnable = true;
1777 prim.VertexAccessType = SEQUENTIAL;
1778 prim.PrimitiveTopologyType = pipeline->topology;
1779 }
1780 }
1781
1782 void genX(CmdDrawIndexedIndirect)(
1783 VkCommandBuffer commandBuffer,
1784 VkBuffer _buffer,
1785 VkDeviceSize offset,
1786 uint32_t drawCount,
1787 uint32_t stride)
1788 {
1789 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1790 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1791 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
1792 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
1793 struct anv_bo *bo = buffer->bo;
1794 uint32_t bo_offset = buffer->offset + offset;
1795
1796 if (anv_batch_has_error(&cmd_buffer->batch))
1797 return;
1798
1799 genX(cmd_buffer_flush_state)(cmd_buffer);
1800
1801 /* TODO: We need to stomp base vertex to 0 somehow */
1802 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
1803 emit_base_vertex_instance_bo(cmd_buffer, bo, bo_offset + 12);
1804 if (vs_prog_data->uses_drawid)
1805 emit_draw_index(cmd_buffer, 0);
1806
1807 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
1808 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
1809 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
1810 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, bo, bo_offset + 12);
1811 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 16);
1812
1813 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
1814 prim.IndirectParameterEnable = true;
1815 prim.VertexAccessType = RANDOM;
1816 prim.PrimitiveTopologyType = pipeline->topology;
1817 }
1818 }
1819
1820 static VkResult
1821 flush_compute_descriptor_set(struct anv_cmd_buffer *cmd_buffer)
1822 {
1823 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
1824 struct anv_state surfaces = { 0, }, samplers = { 0, };
1825 VkResult result;
1826
1827 result = emit_binding_table(cmd_buffer, MESA_SHADER_COMPUTE, &surfaces);
1828 if (result != VK_SUCCESS) {
1829 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
1830
1831 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
1832 if (result != VK_SUCCESS)
1833 return result;
1834
1835 /* Re-emit state base addresses so we get the new surface state base
1836 * address before we start emitting binding tables etc.
1837 */
1838 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
1839
1840 result = emit_binding_table(cmd_buffer, MESA_SHADER_COMPUTE, &surfaces);
1841 if (result != VK_SUCCESS) {
1842 anv_batch_set_error(&cmd_buffer->batch, result);
1843 return result;
1844 }
1845 }
1846
1847 result = emit_samplers(cmd_buffer, MESA_SHADER_COMPUTE, &samplers);
1848 if (result != VK_SUCCESS) {
1849 anv_batch_set_error(&cmd_buffer->batch, result);
1850 return result;
1851 }
1852
1853 uint32_t iface_desc_data_dw[GENX(INTERFACE_DESCRIPTOR_DATA_length)];
1854 struct GENX(INTERFACE_DESCRIPTOR_DATA) desc = {
1855 .BindingTablePointer = surfaces.offset,
1856 .SamplerStatePointer = samplers.offset,
1857 };
1858 GENX(INTERFACE_DESCRIPTOR_DATA_pack)(NULL, iface_desc_data_dw, &desc);
1859
1860 struct anv_state state =
1861 anv_cmd_buffer_merge_dynamic(cmd_buffer, iface_desc_data_dw,
1862 pipeline->interface_descriptor_data,
1863 GENX(INTERFACE_DESCRIPTOR_DATA_length),
1864 64);
1865
1866 uint32_t size = GENX(INTERFACE_DESCRIPTOR_DATA_length) * sizeof(uint32_t);
1867 anv_batch_emit(&cmd_buffer->batch,
1868 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD), mid) {
1869 mid.InterfaceDescriptorTotalLength = size;
1870 mid.InterfaceDescriptorDataStartAddress = state.offset;
1871 }
1872
1873 return VK_SUCCESS;
1874 }
1875
1876 void
1877 genX(cmd_buffer_flush_compute_state)(struct anv_cmd_buffer *cmd_buffer)
1878 {
1879 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
1880 MAYBE_UNUSED VkResult result;
1881
1882 assert(pipeline->active_stages == VK_SHADER_STAGE_COMPUTE_BIT);
1883
1884 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
1885
1886 genX(flush_pipeline_select_gpgpu)(cmd_buffer);
1887
1888 if (cmd_buffer->state.compute_dirty & ANV_CMD_DIRTY_PIPELINE) {
1889 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
1890 *
1891 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
1892 * the only bits that are changed are scoreboard related: Scoreboard
1893 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
1894 * these scoreboard related states, a MEDIA_STATE_FLUSH is
1895 * sufficient."
1896 */
1897 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
1898 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
1899
1900 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
1901 }
1902
1903 if ((cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_COMPUTE_BIT) ||
1904 (cmd_buffer->state.compute_dirty & ANV_CMD_DIRTY_PIPELINE)) {
1905 /* FIXME: figure out descriptors for gen7 */
1906 result = flush_compute_descriptor_set(cmd_buffer);
1907 if (result != VK_SUCCESS)
1908 return;
1909
1910 cmd_buffer->state.descriptors_dirty &= ~VK_SHADER_STAGE_COMPUTE_BIT;
1911 }
1912
1913 if (cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_COMPUTE_BIT) {
1914 struct anv_state push_state =
1915 anv_cmd_buffer_cs_push_constants(cmd_buffer);
1916
1917 if (push_state.alloc_size) {
1918 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_CURBE_LOAD), curbe) {
1919 curbe.CURBETotalDataLength = push_state.alloc_size;
1920 curbe.CURBEDataStartAddress = push_state.offset;
1921 }
1922 }
1923 }
1924
1925 cmd_buffer->state.compute_dirty = 0;
1926
1927 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
1928 }
1929
1930 #if GEN_GEN == 7
1931
1932 static VkResult
1933 verify_cmd_parser(const struct anv_device *device,
1934 int required_version,
1935 const char *function)
1936 {
1937 if (device->instance->physicalDevice.cmd_parser_version < required_version) {
1938 return vk_errorf(VK_ERROR_FEATURE_NOT_PRESENT,
1939 "cmd parser version %d is required for %s",
1940 required_version, function);
1941 } else {
1942 return VK_SUCCESS;
1943 }
1944 }
1945
1946 #endif
1947
1948 void genX(CmdDispatch)(
1949 VkCommandBuffer commandBuffer,
1950 uint32_t x,
1951 uint32_t y,
1952 uint32_t z)
1953 {
1954 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1955 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
1956 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
1957
1958 if (anv_batch_has_error(&cmd_buffer->batch))
1959 return;
1960
1961 if (prog_data->uses_num_work_groups) {
1962 struct anv_state state =
1963 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 12, 4);
1964 uint32_t *sizes = state.map;
1965 sizes[0] = x;
1966 sizes[1] = y;
1967 sizes[2] = z;
1968 anv_state_flush(cmd_buffer->device, state);
1969 cmd_buffer->state.num_workgroups_offset = state.offset;
1970 cmd_buffer->state.num_workgroups_bo =
1971 &cmd_buffer->device->dynamic_state_block_pool.bo;
1972 }
1973
1974 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
1975
1976 anv_batch_emit(&cmd_buffer->batch, GENX(GPGPU_WALKER), ggw) {
1977 ggw.SIMDSize = prog_data->simd_size / 16;
1978 ggw.ThreadDepthCounterMaximum = 0;
1979 ggw.ThreadHeightCounterMaximum = 0;
1980 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
1981 ggw.ThreadGroupIDXDimension = x;
1982 ggw.ThreadGroupIDYDimension = y;
1983 ggw.ThreadGroupIDZDimension = z;
1984 ggw.RightExecutionMask = pipeline->cs_right_mask;
1985 ggw.BottomExecutionMask = 0xffffffff;
1986 }
1987
1988 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_STATE_FLUSH), msf);
1989 }
1990
1991 #define GPGPU_DISPATCHDIMX 0x2500
1992 #define GPGPU_DISPATCHDIMY 0x2504
1993 #define GPGPU_DISPATCHDIMZ 0x2508
1994
1995 #define MI_PREDICATE_SRC0 0x2400
1996 #define MI_PREDICATE_SRC1 0x2408
1997
1998 void genX(CmdDispatchIndirect)(
1999 VkCommandBuffer commandBuffer,
2000 VkBuffer _buffer,
2001 VkDeviceSize offset)
2002 {
2003 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2004 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
2005 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
2006 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
2007 struct anv_bo *bo = buffer->bo;
2008 uint32_t bo_offset = buffer->offset + offset;
2009 struct anv_batch *batch = &cmd_buffer->batch;
2010
2011 #if GEN_GEN == 7
2012 /* Linux 4.4 added command parser version 5 which allows the GPGPU
2013 * indirect dispatch registers to be written.
2014 */
2015 if (verify_cmd_parser(cmd_buffer->device, 5,
2016 "vkCmdDispatchIndirect") != VK_SUCCESS)
2017 return;
2018 #endif
2019
2020 if (prog_data->uses_num_work_groups) {
2021 cmd_buffer->state.num_workgroups_offset = bo_offset;
2022 cmd_buffer->state.num_workgroups_bo = bo;
2023 }
2024
2025 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
2026
2027 emit_lrm(batch, GPGPU_DISPATCHDIMX, bo, bo_offset);
2028 emit_lrm(batch, GPGPU_DISPATCHDIMY, bo, bo_offset + 4);
2029 emit_lrm(batch, GPGPU_DISPATCHDIMZ, bo, bo_offset + 8);
2030
2031 #if GEN_GEN <= 7
2032 /* Clear upper 32-bits of SRC0 and all 64-bits of SRC1 */
2033 emit_lri(batch, MI_PREDICATE_SRC0 + 4, 0);
2034 emit_lri(batch, MI_PREDICATE_SRC1 + 0, 0);
2035 emit_lri(batch, MI_PREDICATE_SRC1 + 4, 0);
2036
2037 /* Load compute_dispatch_indirect_x_size into SRC0 */
2038 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 0);
2039
2040 /* predicate = (compute_dispatch_indirect_x_size == 0); */
2041 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
2042 mip.LoadOperation = LOAD_LOAD;
2043 mip.CombineOperation = COMBINE_SET;
2044 mip.CompareOperation = COMPARE_SRCS_EQUAL;
2045 }
2046
2047 /* Load compute_dispatch_indirect_y_size into SRC0 */
2048 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 4);
2049
2050 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
2051 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
2052 mip.LoadOperation = LOAD_LOAD;
2053 mip.CombineOperation = COMBINE_OR;
2054 mip.CompareOperation = COMPARE_SRCS_EQUAL;
2055 }
2056
2057 /* Load compute_dispatch_indirect_z_size into SRC0 */
2058 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 8);
2059
2060 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
2061 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
2062 mip.LoadOperation = LOAD_LOAD;
2063 mip.CombineOperation = COMBINE_OR;
2064 mip.CompareOperation = COMPARE_SRCS_EQUAL;
2065 }
2066
2067 /* predicate = !predicate; */
2068 #define COMPARE_FALSE 1
2069 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
2070 mip.LoadOperation = LOAD_LOADINV;
2071 mip.CombineOperation = COMBINE_OR;
2072 mip.CompareOperation = COMPARE_FALSE;
2073 }
2074 #endif
2075
2076 anv_batch_emit(batch, GENX(GPGPU_WALKER), ggw) {
2077 ggw.IndirectParameterEnable = true;
2078 ggw.PredicateEnable = GEN_GEN <= 7;
2079 ggw.SIMDSize = prog_data->simd_size / 16;
2080 ggw.ThreadDepthCounterMaximum = 0;
2081 ggw.ThreadHeightCounterMaximum = 0;
2082 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
2083 ggw.RightExecutionMask = pipeline->cs_right_mask;
2084 ggw.BottomExecutionMask = 0xffffffff;
2085 }
2086
2087 anv_batch_emit(batch, GENX(MEDIA_STATE_FLUSH), msf);
2088 }
2089
2090 static void
2091 genX(flush_pipeline_select)(struct anv_cmd_buffer *cmd_buffer,
2092 uint32_t pipeline)
2093 {
2094 if (cmd_buffer->state.current_pipeline == pipeline)
2095 return;
2096
2097 #if GEN_GEN >= 8 && GEN_GEN < 10
2098 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
2099 *
2100 * Software must clear the COLOR_CALC_STATE Valid field in
2101 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
2102 * with Pipeline Select set to GPGPU.
2103 *
2104 * The internal hardware docs recommend the same workaround for Gen9
2105 * hardware too.
2106 */
2107 if (pipeline == GPGPU)
2108 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CC_STATE_POINTERS), t);
2109 #endif
2110
2111 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
2112 * PIPELINE_SELECT [DevBWR+]":
2113 *
2114 * Project: DEVSNB+
2115 *
2116 * Software must ensure all the write caches are flushed through a
2117 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
2118 * command to invalidate read only caches prior to programming
2119 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
2120 */
2121 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
2122 pc.RenderTargetCacheFlushEnable = true;
2123 pc.DepthCacheFlushEnable = true;
2124 pc.DCFlushEnable = true;
2125 pc.PostSyncOperation = NoWrite;
2126 pc.CommandStreamerStallEnable = true;
2127 }
2128
2129 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
2130 pc.TextureCacheInvalidationEnable = true;
2131 pc.ConstantCacheInvalidationEnable = true;
2132 pc.StateCacheInvalidationEnable = true;
2133 pc.InstructionCacheInvalidateEnable = true;
2134 pc.PostSyncOperation = NoWrite;
2135 }
2136
2137 anv_batch_emit(&cmd_buffer->batch, GENX(PIPELINE_SELECT), ps) {
2138 #if GEN_GEN >= 9
2139 ps.MaskBits = 3;
2140 #endif
2141 ps.PipelineSelection = pipeline;
2142 }
2143
2144 cmd_buffer->state.current_pipeline = pipeline;
2145 }
2146
2147 void
2148 genX(flush_pipeline_select_3d)(struct anv_cmd_buffer *cmd_buffer)
2149 {
2150 genX(flush_pipeline_select)(cmd_buffer, _3D);
2151 }
2152
2153 void
2154 genX(flush_pipeline_select_gpgpu)(struct anv_cmd_buffer *cmd_buffer)
2155 {
2156 genX(flush_pipeline_select)(cmd_buffer, GPGPU);
2157 }
2158
2159 void
2160 genX(cmd_buffer_emit_gen7_depth_flush)(struct anv_cmd_buffer *cmd_buffer)
2161 {
2162 if (GEN_GEN >= 8)
2163 return;
2164
2165 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
2166 *
2167 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
2168 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
2169 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
2170 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
2171 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
2172 * Depth Flush Bit set, followed by another pipelined depth stall
2173 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
2174 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
2175 * via a preceding MI_FLUSH)."
2176 */
2177 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
2178 pipe.DepthStallEnable = true;
2179 }
2180 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
2181 pipe.DepthCacheFlushEnable = true;
2182 }
2183 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
2184 pipe.DepthStallEnable = true;
2185 }
2186 }
2187
2188 static void
2189 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer *cmd_buffer)
2190 {
2191 struct anv_device *device = cmd_buffer->device;
2192 const struct anv_image_view *iview =
2193 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
2194 const struct anv_image *image = iview ? iview->image : NULL;
2195
2196 /* FIXME: Width and Height are wrong */
2197
2198 genX(cmd_buffer_emit_gen7_depth_flush)(cmd_buffer);
2199
2200 uint32_t *dw = anv_batch_emit_dwords(&cmd_buffer->batch,
2201 device->isl_dev.ds.size / 4);
2202 if (dw == NULL)
2203 return;
2204
2205 struct isl_depth_stencil_hiz_emit_info info = {
2206 .mocs = device->default_mocs,
2207 };
2208
2209 if (iview)
2210 info.view = &iview->isl;
2211
2212 if (image && (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
2213 info.depth_surf = &image->depth_surface.isl;
2214
2215 info.depth_address =
2216 anv_batch_emit_reloc(&cmd_buffer->batch,
2217 dw + device->isl_dev.ds.depth_offset / 4,
2218 image->bo,
2219 image->offset + image->depth_surface.offset);
2220
2221 const uint32_t ds =
2222 cmd_buffer->state.subpass->depth_stencil_attachment.attachment;
2223 info.hiz_usage = cmd_buffer->state.attachments[ds].aux_usage;
2224 if (info.hiz_usage == ISL_AUX_USAGE_HIZ) {
2225 info.hiz_surf = &image->aux_surface.isl;
2226
2227 info.hiz_address =
2228 anv_batch_emit_reloc(&cmd_buffer->batch,
2229 dw + device->isl_dev.ds.hiz_offset / 4,
2230 image->bo,
2231 image->offset + image->aux_surface.offset);
2232
2233 info.depth_clear_value = ANV_HZ_FC_VAL;
2234 }
2235 }
2236
2237 if (image && (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT)) {
2238 info.stencil_surf = &image->stencil_surface.isl;
2239
2240 info.stencil_address =
2241 anv_batch_emit_reloc(&cmd_buffer->batch,
2242 dw + device->isl_dev.ds.stencil_offset / 4,
2243 image->bo,
2244 image->offset + image->stencil_surface.offset);
2245 }
2246
2247 isl_emit_depth_stencil_hiz_s(&device->isl_dev, dw, &info);
2248
2249 cmd_buffer->state.hiz_enabled = info.hiz_usage == ISL_AUX_USAGE_HIZ;
2250 }
2251
2252
2253 /**
2254 * @brief Perform any layout transitions required at the beginning and/or end
2255 * of the current subpass for depth buffers.
2256 *
2257 * TODO: Consider preprocessing the attachment reference array at render pass
2258 * create time to determine if no layout transition is needed at the
2259 * beginning and/or end of each subpass.
2260 *
2261 * @param cmd_buffer The command buffer the transition is happening within.
2262 * @param subpass_end If true, marks that the transition is happening at the
2263 * end of the subpass.
2264 */
2265 static void
2266 cmd_buffer_subpass_transition_layouts(struct anv_cmd_buffer * const cmd_buffer,
2267 const bool subpass_end)
2268 {
2269 /* We need a non-NULL command buffer. */
2270 assert(cmd_buffer);
2271
2272 const struct anv_cmd_state * const cmd_state = &cmd_buffer->state;
2273 const struct anv_subpass * const subpass = cmd_state->subpass;
2274
2275 /* This function must be called within a subpass. */
2276 assert(subpass);
2277
2278 /* If there are attachment references, the array shouldn't be NULL.
2279 */
2280 if (subpass->attachment_count > 0)
2281 assert(subpass->attachments);
2282
2283 /* Iterate over the array of attachment references. */
2284 for (const VkAttachmentReference *att_ref = subpass->attachments;
2285 att_ref < subpass->attachments + subpass->attachment_count; att_ref++) {
2286
2287 /* If the attachment is unused, we can't perform a layout transition. */
2288 if (att_ref->attachment == VK_ATTACHMENT_UNUSED)
2289 continue;
2290
2291 /* This attachment index shouldn't go out of bounds. */
2292 assert(att_ref->attachment < cmd_state->pass->attachment_count);
2293
2294 const struct anv_render_pass_attachment * const att_desc =
2295 &cmd_state->pass->attachments[att_ref->attachment];
2296 struct anv_attachment_state * const att_state =
2297 &cmd_buffer->state.attachments[att_ref->attachment];
2298
2299 /* The attachment should not be used in a subpass after its last. */
2300 assert(att_desc->last_subpass_idx >= anv_get_subpass_id(cmd_state));
2301
2302 if (subpass_end && anv_get_subpass_id(cmd_state) <
2303 att_desc->last_subpass_idx) {
2304 /* We're calling this function on a buffer twice in one subpass and
2305 * this is not the last use of the buffer. The layout should not have
2306 * changed from the first call and no transition is necessary.
2307 */
2308 assert(att_ref->layout == att_state->current_layout);
2309 continue;
2310 }
2311
2312 /* Get the appropriate target layout for this attachment. */
2313 const VkImageLayout target_layout = subpass_end ?
2314 att_desc->final_layout : att_ref->layout;
2315
2316 /* The attachment index must be less than the number of attachments
2317 * within the framebuffer.
2318 */
2319 assert(att_ref->attachment < cmd_state->framebuffer->attachment_count);
2320
2321 const struct anv_image * const image =
2322 cmd_state->framebuffer->attachments[att_ref->attachment]->image;
2323
2324 /* Perform the layout transition. */
2325 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
2326 transition_depth_buffer(cmd_buffer, image,
2327 att_state->current_layout, target_layout);
2328 att_state->aux_usage =
2329 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
2330 image->aspects, target_layout);
2331 }
2332
2333 att_state->current_layout = target_layout;
2334 }
2335 }
2336
2337 static void
2338 genX(cmd_buffer_set_subpass)(struct anv_cmd_buffer *cmd_buffer,
2339 struct anv_subpass *subpass)
2340 {
2341 cmd_buffer->state.subpass = subpass;
2342
2343 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
2344
2345 /* Perform transitions to the subpass layout before any writes have
2346 * occurred.
2347 */
2348 cmd_buffer_subpass_transition_layouts(cmd_buffer, false);
2349
2350 cmd_buffer_emit_depth_stencil(cmd_buffer);
2351
2352 anv_cmd_buffer_clear_subpass(cmd_buffer);
2353 }
2354
2355 void genX(CmdBeginRenderPass)(
2356 VkCommandBuffer commandBuffer,
2357 const VkRenderPassBeginInfo* pRenderPassBegin,
2358 VkSubpassContents contents)
2359 {
2360 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2361 ANV_FROM_HANDLE(anv_render_pass, pass, pRenderPassBegin->renderPass);
2362 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, pRenderPassBegin->framebuffer);
2363
2364 cmd_buffer->state.framebuffer = framebuffer;
2365 cmd_buffer->state.pass = pass;
2366 cmd_buffer->state.render_area = pRenderPassBegin->renderArea;
2367 VkResult result =
2368 genX(cmd_buffer_setup_attachments)(cmd_buffer, pass, pRenderPassBegin);
2369
2370 /* If we failed to setup the attachments we should not try to go further */
2371 if (result != VK_SUCCESS) {
2372 assert(anv_batch_has_error(&cmd_buffer->batch));
2373 return;
2374 }
2375
2376 genX(flush_pipeline_select_3d)(cmd_buffer);
2377
2378 genX(cmd_buffer_set_subpass)(cmd_buffer, pass->subpasses);
2379
2380 cmd_buffer->state.pending_pipe_bits |=
2381 cmd_buffer->state.pass->subpass_flushes[0];
2382 }
2383
2384 void genX(CmdNextSubpass)(
2385 VkCommandBuffer commandBuffer,
2386 VkSubpassContents contents)
2387 {
2388 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2389
2390 if (anv_batch_has_error(&cmd_buffer->batch))
2391 return;
2392
2393 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
2394
2395 anv_cmd_buffer_resolve_subpass(cmd_buffer);
2396
2397 /* Perform transitions to the final layout after all writes have occurred.
2398 */
2399 cmd_buffer_subpass_transition_layouts(cmd_buffer, true);
2400
2401 genX(cmd_buffer_set_subpass)(cmd_buffer, cmd_buffer->state.subpass + 1);
2402
2403 uint32_t subpass_id = anv_get_subpass_id(&cmd_buffer->state);
2404 cmd_buffer->state.pending_pipe_bits |=
2405 cmd_buffer->state.pass->subpass_flushes[subpass_id];
2406 }
2407
2408 void genX(CmdEndRenderPass)(
2409 VkCommandBuffer commandBuffer)
2410 {
2411 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2412
2413 if (anv_batch_has_error(&cmd_buffer->batch))
2414 return;
2415
2416 anv_cmd_buffer_resolve_subpass(cmd_buffer);
2417
2418 /* Perform transitions to the final layout after all writes have occurred.
2419 */
2420 cmd_buffer_subpass_transition_layouts(cmd_buffer, true);
2421
2422 cmd_buffer->state.pending_pipe_bits |=
2423 cmd_buffer->state.pass->subpass_flushes[cmd_buffer->state.pass->subpass_count];
2424
2425 cmd_buffer->state.hiz_enabled = false;
2426
2427 #ifndef NDEBUG
2428 anv_dump_add_framebuffer(cmd_buffer, cmd_buffer->state.framebuffer);
2429 #endif
2430
2431 /* Remove references to render pass specific state. This enables us to
2432 * detect whether or not we're in a renderpass.
2433 */
2434 cmd_buffer->state.framebuffer = NULL;
2435 cmd_buffer->state.pass = NULL;
2436 cmd_buffer->state.subpass = NULL;
2437 }