anv: Add support for HiZ+CCS
[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 #include "vk_util.h"
30 #include "util/fast_idiv_by_const.h"
31
32 #include "common/gen_aux_map.h"
33 #include "common/gen_l3_config.h"
34 #include "genxml/gen_macros.h"
35 #include "genxml/genX_pack.h"
36
37 /* We reserve GPR 14 and 15 for conditional rendering */
38 #define GEN_MI_BUILDER_NUM_ALLOC_GPRS 14
39 #define __gen_get_batch_dwords anv_batch_emit_dwords
40 #define __gen_address_offset anv_address_add
41 #include "common/gen_mi_builder.h"
42
43 static void genX(flush_pipeline_select)(struct anv_cmd_buffer *cmd_buffer,
44 uint32_t pipeline);
45
46 static void
47 emit_lri(struct anv_batch *batch, uint32_t reg, uint32_t imm)
48 {
49 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
50 lri.RegisterOffset = reg;
51 lri.DataDWord = imm;
52 }
53 }
54
55 void
56 genX(cmd_buffer_emit_state_base_address)(struct anv_cmd_buffer *cmd_buffer)
57 {
58 struct anv_device *device = cmd_buffer->device;
59 UNUSED const struct gen_device_info *devinfo = &device->info;
60 uint32_t mocs = device->isl_dev.mocs.internal;
61
62 /* If we are emitting a new state base address we probably need to re-emit
63 * binding tables.
64 */
65 cmd_buffer->state.descriptors_dirty |= ~0;
66
67 /* Emit a render target cache flush.
68 *
69 * This isn't documented anywhere in the PRM. However, it seems to be
70 * necessary prior to changing the surface state base adress. Without
71 * this, we get GPU hangs when using multi-level command buffers which
72 * clear depth, reset state base address, and then go render stuff.
73 */
74 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
75 pc.DCFlushEnable = true;
76 pc.RenderTargetCacheFlushEnable = true;
77 pc.CommandStreamerStallEnable = true;
78 #if GEN_GEN >= 12
79 pc.TileCacheFlushEnable = true;
80 #endif
81 #if GEN_GEN == 12
82 /* GEN:BUG:1606662791:
83 *
84 * Software must program PIPE_CONTROL command with "HDC Pipeline
85 * Flush" prior to programming of the below two non-pipeline state :
86 * * STATE_BASE_ADDRESS
87 * * 3DSTATE_BINDING_TABLE_POOL_ALLOC
88 */
89 if (devinfo->revision == 0 /* A0 */)
90 pc.HDCPipelineFlushEnable = true;
91 #endif
92 }
93
94 #if GEN_GEN == 12
95 /* GEN:BUG:1607854226:
96 *
97 * Workaround the non pipelined state not applying in MEDIA/GPGPU pipeline
98 * mode by putting the pipeline temporarily in 3D mode.
99 */
100 uint32_t gen12_wa_pipeline = cmd_buffer->state.current_pipeline;
101 genX(flush_pipeline_select_3d)(cmd_buffer);
102 #endif
103
104 anv_batch_emit(&cmd_buffer->batch, GENX(STATE_BASE_ADDRESS), sba) {
105 sba.GeneralStateBaseAddress = (struct anv_address) { NULL, 0 };
106 sba.GeneralStateMOCS = mocs;
107 sba.GeneralStateBaseAddressModifyEnable = true;
108
109 sba.StatelessDataPortAccessMOCS = mocs;
110
111 sba.SurfaceStateBaseAddress =
112 anv_cmd_buffer_surface_base_address(cmd_buffer);
113 sba.SurfaceStateMOCS = mocs;
114 sba.SurfaceStateBaseAddressModifyEnable = true;
115
116 sba.DynamicStateBaseAddress =
117 (struct anv_address) { device->dynamic_state_pool.block_pool.bo, 0 };
118 sba.DynamicStateMOCS = mocs;
119 sba.DynamicStateBaseAddressModifyEnable = true;
120
121 sba.IndirectObjectBaseAddress = (struct anv_address) { NULL, 0 };
122 sba.IndirectObjectMOCS = mocs;
123 sba.IndirectObjectBaseAddressModifyEnable = true;
124
125 sba.InstructionBaseAddress =
126 (struct anv_address) { device->instruction_state_pool.block_pool.bo, 0 };
127 sba.InstructionMOCS = mocs;
128 sba.InstructionBaseAddressModifyEnable = true;
129
130 # if (GEN_GEN >= 8)
131 /* Broadwell requires that we specify a buffer size for a bunch of
132 * these fields. However, since we will be growing the BO's live, we
133 * just set them all to the maximum.
134 */
135 sba.GeneralStateBufferSize = 0xfffff;
136 sba.IndirectObjectBufferSize = 0xfffff;
137 if (device->physical->use_softpin) {
138 /* With softpin, we use fixed addresses so we actually know how big
139 * our base addresses are.
140 */
141 sba.DynamicStateBufferSize = DYNAMIC_STATE_POOL_SIZE / 4096;
142 sba.InstructionBufferSize = INSTRUCTION_STATE_POOL_SIZE / 4096;
143 } else {
144 sba.DynamicStateBufferSize = 0xfffff;
145 sba.InstructionBufferSize = 0xfffff;
146 }
147 sba.GeneralStateBufferSizeModifyEnable = true;
148 sba.IndirectObjectBufferSizeModifyEnable = true;
149 sba.DynamicStateBufferSizeModifyEnable = true;
150 sba.InstructionBuffersizeModifyEnable = true;
151 # else
152 /* On gen7, we have upper bounds instead. According to the docs,
153 * setting an upper bound of zero means that no bounds checking is
154 * performed so, in theory, we should be able to leave them zero.
155 * However, border color is broken and the GPU bounds-checks anyway.
156 * To avoid this and other potential problems, we may as well set it
157 * for everything.
158 */
159 sba.GeneralStateAccessUpperBound =
160 (struct anv_address) { .bo = NULL, .offset = 0xfffff000 };
161 sba.GeneralStateAccessUpperBoundModifyEnable = true;
162 sba.DynamicStateAccessUpperBound =
163 (struct anv_address) { .bo = NULL, .offset = 0xfffff000 };
164 sba.DynamicStateAccessUpperBoundModifyEnable = true;
165 sba.InstructionAccessUpperBound =
166 (struct anv_address) { .bo = NULL, .offset = 0xfffff000 };
167 sba.InstructionAccessUpperBoundModifyEnable = true;
168 # endif
169 # if (GEN_GEN >= 9)
170 if (cmd_buffer->device->physical->use_softpin) {
171 sba.BindlessSurfaceStateBaseAddress = (struct anv_address) {
172 .bo = device->surface_state_pool.block_pool.bo,
173 .offset = 0,
174 };
175 sba.BindlessSurfaceStateSize = (1 << 20) - 1;
176 } else {
177 sba.BindlessSurfaceStateBaseAddress = ANV_NULL_ADDRESS;
178 sba.BindlessSurfaceStateSize = 0;
179 }
180 sba.BindlessSurfaceStateMOCS = mocs;
181 sba.BindlessSurfaceStateBaseAddressModifyEnable = true;
182 # endif
183 # if (GEN_GEN >= 10)
184 sba.BindlessSamplerStateBaseAddress = (struct anv_address) { NULL, 0 };
185 sba.BindlessSamplerStateMOCS = mocs;
186 sba.BindlessSamplerStateBaseAddressModifyEnable = true;
187 sba.BindlessSamplerStateBufferSize = 0;
188 # endif
189 }
190
191 #if GEN_GEN == 12
192 /* GEN:BUG:1607854226:
193 *
194 * Put the pipeline back into its current mode.
195 */
196 if (gen12_wa_pipeline != UINT32_MAX)
197 genX(flush_pipeline_select)(cmd_buffer, gen12_wa_pipeline);
198 #endif
199
200 /* After re-setting the surface state base address, we have to do some
201 * cache flusing so that the sampler engine will pick up the new
202 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
203 * Shared Function > 3D Sampler > State > State Caching (page 96):
204 *
205 * Coherency with system memory in the state cache, like the texture
206 * cache is handled partially by software. It is expected that the
207 * command stream or shader will issue Cache Flush operation or
208 * Cache_Flush sampler message to ensure that the L1 cache remains
209 * coherent with system memory.
210 *
211 * [...]
212 *
213 * Whenever the value of the Dynamic_State_Base_Addr,
214 * Surface_State_Base_Addr are altered, the L1 state cache must be
215 * invalidated to ensure the new surface or sampler state is fetched
216 * from system memory.
217 *
218 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
219 * which, according the PIPE_CONTROL instruction documentation in the
220 * Broadwell PRM:
221 *
222 * Setting this bit is independent of any other bit in this packet.
223 * This bit controls the invalidation of the L1 and L2 state caches
224 * at the top of the pipe i.e. at the parsing time.
225 *
226 * Unfortunately, experimentation seems to indicate that state cache
227 * invalidation through a PIPE_CONTROL does nothing whatsoever in
228 * regards to surface state and binding tables. In stead, it seems that
229 * invalidating the texture cache is what is actually needed.
230 *
231 * XXX: As far as we have been able to determine through
232 * experimentation, shows that flush the texture cache appears to be
233 * sufficient. The theory here is that all of the sampling/rendering
234 * units cache the binding table in the texture cache. However, we have
235 * yet to be able to actually confirm this.
236 */
237 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
238 pc.TextureCacheInvalidationEnable = true;
239 pc.ConstantCacheInvalidationEnable = true;
240 pc.StateCacheInvalidationEnable = true;
241 }
242 }
243
244 static void
245 add_surface_reloc(struct anv_cmd_buffer *cmd_buffer,
246 struct anv_state state, struct anv_address addr)
247 {
248 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
249
250 VkResult result =
251 anv_reloc_list_add(&cmd_buffer->surface_relocs, &cmd_buffer->pool->alloc,
252 state.offset + isl_dev->ss.addr_offset,
253 addr.bo, addr.offset, NULL);
254 if (result != VK_SUCCESS)
255 anv_batch_set_error(&cmd_buffer->batch, result);
256 }
257
258 static void
259 add_surface_state_relocs(struct anv_cmd_buffer *cmd_buffer,
260 struct anv_surface_state state)
261 {
262 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
263
264 assert(!anv_address_is_null(state.address));
265 add_surface_reloc(cmd_buffer, state.state, state.address);
266
267 if (!anv_address_is_null(state.aux_address)) {
268 VkResult result =
269 anv_reloc_list_add(&cmd_buffer->surface_relocs,
270 &cmd_buffer->pool->alloc,
271 state.state.offset + isl_dev->ss.aux_addr_offset,
272 state.aux_address.bo,
273 state.aux_address.offset,
274 NULL);
275 if (result != VK_SUCCESS)
276 anv_batch_set_error(&cmd_buffer->batch, result);
277 }
278
279 if (!anv_address_is_null(state.clear_address)) {
280 VkResult result =
281 anv_reloc_list_add(&cmd_buffer->surface_relocs,
282 &cmd_buffer->pool->alloc,
283 state.state.offset +
284 isl_dev->ss.clear_color_state_offset,
285 state.clear_address.bo,
286 state.clear_address.offset,
287 NULL);
288 if (result != VK_SUCCESS)
289 anv_batch_set_error(&cmd_buffer->batch, result);
290 }
291 }
292
293 static void
294 color_attachment_compute_aux_usage(struct anv_device * device,
295 struct anv_cmd_state * cmd_state,
296 uint32_t att, VkRect2D render_area,
297 union isl_color_value *fast_clear_color)
298 {
299 struct anv_attachment_state *att_state = &cmd_state->attachments[att];
300 struct anv_image_view *iview = cmd_state->attachments[att].image_view;
301
302 assert(iview->n_planes == 1);
303
304 if (iview->planes[0].isl.base_array_layer >=
305 anv_image_aux_layers(iview->image, VK_IMAGE_ASPECT_COLOR_BIT,
306 iview->planes[0].isl.base_level)) {
307 /* There is no aux buffer which corresponds to the level and layer(s)
308 * being accessed.
309 */
310 att_state->aux_usage = ISL_AUX_USAGE_NONE;
311 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
312 att_state->fast_clear = false;
313 return;
314 }
315
316 att_state->aux_usage =
317 anv_layout_to_aux_usage(&device->info, iview->image,
318 VK_IMAGE_ASPECT_COLOR_BIT,
319 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
320 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
321
322 /* If we don't have aux, then we should have returned early in the layer
323 * check above. If we got here, we must have something.
324 */
325 assert(att_state->aux_usage != ISL_AUX_USAGE_NONE);
326
327 if (att_state->aux_usage == ISL_AUX_USAGE_CCS_E ||
328 att_state->aux_usage == ISL_AUX_USAGE_MCS) {
329 att_state->input_aux_usage = att_state->aux_usage;
330 } else {
331 /* From the Sky Lake PRM, RENDER_SURFACE_STATE::AuxiliarySurfaceMode:
332 *
333 * "If Number of Multisamples is MULTISAMPLECOUNT_1, AUX_CCS_D
334 * setting is only allowed if Surface Format supported for Fast
335 * Clear. In addition, if the surface is bound to the sampling
336 * engine, Surface Format must be supported for Render Target
337 * Compression for surfaces bound to the sampling engine."
338 *
339 * In other words, we can only sample from a fast-cleared image if it
340 * also supports color compression.
341 */
342 if (isl_format_supports_ccs_e(&device->info, iview->planes[0].isl.format) &&
343 isl_format_supports_ccs_d(&device->info, iview->planes[0].isl.format)) {
344 att_state->input_aux_usage = ISL_AUX_USAGE_CCS_D;
345
346 /* While fast-clear resolves and partial resolves are fairly cheap in the
347 * case where you render to most of the pixels, full resolves are not
348 * because they potentially involve reading and writing the entire
349 * framebuffer. If we can't texture with CCS_E, we should leave it off and
350 * limit ourselves to fast clears.
351 */
352 if (cmd_state->pass->attachments[att].first_subpass_layout ==
353 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
354 anv_perf_warn(device, iview->image,
355 "Not temporarily enabling CCS_E.");
356 }
357 } else {
358 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
359 }
360 }
361
362 assert(iview->image->planes[0].aux_surface.isl.usage &
363 (ISL_SURF_USAGE_CCS_BIT | ISL_SURF_USAGE_MCS_BIT));
364
365 union isl_color_value clear_color = {};
366 anv_clear_color_from_att_state(&clear_color, att_state, iview);
367
368 att_state->clear_color_is_zero_one =
369 isl_color_value_is_zero_one(clear_color, iview->planes[0].isl.format);
370 att_state->clear_color_is_zero =
371 isl_color_value_is_zero(clear_color, iview->planes[0].isl.format);
372
373 if (att_state->pending_clear_aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
374 /* Start by getting the fast clear type. We use the first subpass
375 * layout here because we don't want to fast-clear if the first subpass
376 * to use the attachment can't handle fast-clears.
377 */
378 enum anv_fast_clear_type fast_clear_type =
379 anv_layout_to_fast_clear_type(&device->info, iview->image,
380 VK_IMAGE_ASPECT_COLOR_BIT,
381 cmd_state->pass->attachments[att].first_subpass_layout);
382 switch (fast_clear_type) {
383 case ANV_FAST_CLEAR_NONE:
384 att_state->fast_clear = false;
385 break;
386 case ANV_FAST_CLEAR_DEFAULT_VALUE:
387 att_state->fast_clear = att_state->clear_color_is_zero;
388 break;
389 case ANV_FAST_CLEAR_ANY:
390 att_state->fast_clear = true;
391 break;
392 }
393
394 /* Potentially, we could do partial fast-clears but doing so has crazy
395 * alignment restrictions. It's easier to just restrict to full size
396 * fast clears for now.
397 */
398 if (render_area.offset.x != 0 ||
399 render_area.offset.y != 0 ||
400 render_area.extent.width != iview->extent.width ||
401 render_area.extent.height != iview->extent.height)
402 att_state->fast_clear = false;
403
404 /* On Broadwell and earlier, we can only handle 0/1 clear colors */
405 if (GEN_GEN <= 8 && !att_state->clear_color_is_zero_one)
406 att_state->fast_clear = false;
407
408 /* We only allow fast clears to the first slice of an image (level 0,
409 * layer 0) and only for the entire slice. This guarantees us that, at
410 * any given time, there is only one clear color on any given image at
411 * any given time. At the time of our testing (Jan 17, 2018), there
412 * were no known applications which would benefit from fast-clearing
413 * more than just the first slice.
414 */
415 if (att_state->fast_clear &&
416 (iview->planes[0].isl.base_level > 0 ||
417 iview->planes[0].isl.base_array_layer > 0)) {
418 anv_perf_warn(device, iview->image,
419 "Rendering with multi-lod or multi-layer framebuffer "
420 "with LOAD_OP_LOAD and baseMipLevel > 0 or "
421 "baseArrayLayer > 0. Not fast clearing.");
422 att_state->fast_clear = false;
423 } else if (att_state->fast_clear && cmd_state->framebuffer->layers > 1) {
424 anv_perf_warn(device, iview->image,
425 "Rendering to a multi-layer framebuffer with "
426 "LOAD_OP_CLEAR. Only fast-clearing the first slice");
427 }
428
429 if (att_state->fast_clear)
430 *fast_clear_color = clear_color;
431 } else {
432 att_state->fast_clear = false;
433 }
434 }
435
436 static void
437 depth_stencil_attachment_compute_aux_usage(struct anv_device *device,
438 struct anv_cmd_state *cmd_state,
439 uint32_t att, VkRect2D render_area)
440 {
441 struct anv_render_pass_attachment *pass_att =
442 &cmd_state->pass->attachments[att];
443 struct anv_attachment_state *att_state = &cmd_state->attachments[att];
444 struct anv_image_view *iview = cmd_state->attachments[att].image_view;
445
446 /* These will be initialized after the first subpass transition. */
447 att_state->aux_usage = ISL_AUX_USAGE_NONE;
448 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
449
450 /* This is unused for depth/stencil but valgrind complains if it
451 * isn't initialized
452 */
453 att_state->clear_color_is_zero_one = false;
454
455 if (GEN_GEN == 7) {
456 /* We don't do any HiZ or depth fast-clears on gen7 yet */
457 att_state->fast_clear = false;
458 return;
459 }
460
461 if (!(att_state->pending_clear_aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
462 /* If we're just clearing stencil, we can always HiZ clear */
463 att_state->fast_clear = true;
464 return;
465 }
466
467 /* Default to false for now */
468 att_state->fast_clear = false;
469
470 /* We must have depth in order to have HiZ */
471 if (!(iview->image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT))
472 return;
473
474 const enum isl_aux_usage first_subpass_aux_usage =
475 anv_layout_to_aux_usage(&device->info, iview->image,
476 VK_IMAGE_ASPECT_DEPTH_BIT,
477 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
478 pass_att->first_subpass_layout);
479 if (!blorp_can_hiz_clear_depth(&device->info,
480 &iview->image->planes[0].surface.isl,
481 first_subpass_aux_usage,
482 iview->planes[0].isl.base_level,
483 iview->planes[0].isl.base_array_layer,
484 render_area.offset.x,
485 render_area.offset.y,
486 render_area.offset.x +
487 render_area.extent.width,
488 render_area.offset.y +
489 render_area.extent.height))
490 return;
491
492 if (att_state->clear_value.depthStencil.depth != ANV_HZ_FC_VAL)
493 return;
494
495 if (GEN_GEN == 8 && anv_can_sample_with_hiz(&device->info, iview->image)) {
496 /* Only gen9+ supports returning ANV_HZ_FC_VAL when sampling a
497 * fast-cleared portion of a HiZ buffer. Testing has revealed that Gen8
498 * only supports returning 0.0f. Gens prior to gen8 do not support this
499 * feature at all.
500 */
501 return;
502 }
503
504 /* If we got here, then we can fast clear */
505 att_state->fast_clear = true;
506 }
507
508 static bool
509 need_input_attachment_state(const struct anv_render_pass_attachment *att)
510 {
511 if (!(att->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
512 return false;
513
514 /* We only allocate input attachment states for color surfaces. Compression
515 * is not yet enabled for depth textures and stencil doesn't allow
516 * compression so we can just use the texture surface state from the view.
517 */
518 return vk_format_is_color(att->format);
519 }
520
521 #define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
522
523 #if GEN_GEN == 12
524 static void
525 anv_image_init_aux_tt(struct anv_cmd_buffer *cmd_buffer,
526 const struct anv_image *image,
527 VkImageAspectFlagBits aspect,
528 uint32_t base_level, uint32_t level_count,
529 uint32_t base_layer, uint32_t layer_count)
530 {
531 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
532
533 uint64_t base_address =
534 anv_address_physical(image->planes[plane].address);
535
536 const struct isl_surf *isl_surf = &image->planes[plane].surface.isl;
537 uint64_t format_bits = gen_aux_map_format_bits_for_isl_surf(isl_surf);
538
539 /* We're about to live-update the AUX-TT. We really don't want anyone else
540 * trying to read it while we're doing this. We could probably get away
541 * with not having this stall in some cases if we were really careful but
542 * it's better to play it safe. Full stall the GPU.
543 */
544 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_END_OF_PIPE_SYNC_BIT;
545 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
546
547 struct gen_mi_builder b;
548 gen_mi_builder_init(&b, &cmd_buffer->batch);
549
550 for (uint32_t a = 0; a < layer_count; a++) {
551 const uint32_t layer = base_layer + a;
552
553 uint64_t start_offset_B = UINT64_MAX, end_offset_B = 0;
554 for (uint32_t l = 0; l < level_count; l++) {
555 const uint32_t level = base_level + l;
556
557 uint32_t logical_array_layer, logical_z_offset_px;
558 if (image->type == VK_IMAGE_TYPE_3D) {
559 logical_array_layer = 0;
560
561 /* If the given miplevel does not have this layer, then any higher
562 * miplevels won't either because miplevels only get smaller the
563 * higher the LOD.
564 */
565 assert(layer < image->extent.depth);
566 if (layer >= anv_minify(image->extent.depth, level))
567 break;
568 logical_z_offset_px = layer;
569 } else {
570 assert(layer < image->array_size);
571 logical_array_layer = layer;
572 logical_z_offset_px = 0;
573 }
574
575 uint32_t slice_start_offset_B, slice_end_offset_B;
576 isl_surf_get_image_range_B_tile(isl_surf, level,
577 logical_array_layer,
578 logical_z_offset_px,
579 &slice_start_offset_B,
580 &slice_end_offset_B);
581
582 start_offset_B = MIN2(start_offset_B, slice_start_offset_B);
583 end_offset_B = MAX2(end_offset_B, slice_end_offset_B);
584 }
585
586 /* Aux operates 64K at a time */
587 start_offset_B = align_down_u64(start_offset_B, 64 * 1024);
588 end_offset_B = align_u64(end_offset_B, 64 * 1024);
589
590 for (uint64_t offset = start_offset_B;
591 offset < end_offset_B; offset += 64 * 1024) {
592 uint64_t address = base_address + offset;
593
594 uint64_t aux_entry_addr64, *aux_entry_map;
595 aux_entry_map = gen_aux_map_get_entry(cmd_buffer->device->aux_map_ctx,
596 address, &aux_entry_addr64);
597
598 assert(cmd_buffer->device->physical->use_softpin);
599 struct anv_address aux_entry_address = {
600 .bo = NULL,
601 .offset = aux_entry_addr64,
602 };
603
604 const uint64_t old_aux_entry = READ_ONCE(*aux_entry_map);
605 uint64_t new_aux_entry =
606 (old_aux_entry & GEN_AUX_MAP_ADDRESS_MASK) | format_bits;
607
608 if (isl_aux_usage_has_ccs(image->planes[plane].aux_usage))
609 new_aux_entry |= GEN_AUX_MAP_ENTRY_VALID_BIT;
610
611 gen_mi_store(&b, gen_mi_mem64(aux_entry_address),
612 gen_mi_imm(new_aux_entry));
613 }
614 }
615
616 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_AUX_TABLE_INVALIDATE_BIT;
617 }
618 #endif /* GEN_GEN == 12 */
619
620 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
621 * the initial layout is undefined, the HiZ buffer and depth buffer will
622 * represent the same data at the end of this operation.
623 */
624 static void
625 transition_depth_buffer(struct anv_cmd_buffer *cmd_buffer,
626 const struct anv_image *image,
627 uint32_t base_layer, uint32_t layer_count,
628 VkImageLayout initial_layout,
629 VkImageLayout final_layout)
630 {
631 uint32_t depth_plane =
632 anv_image_aspect_to_plane(image->aspects, VK_IMAGE_ASPECT_DEPTH_BIT);
633 if (image->planes[depth_plane].aux_usage == ISL_AUX_USAGE_NONE)
634 return;
635
636 #if GEN_GEN == 12
637 if ((initial_layout == VK_IMAGE_LAYOUT_UNDEFINED ||
638 initial_layout == VK_IMAGE_LAYOUT_PREINITIALIZED) &&
639 cmd_buffer->device->physical->has_implicit_ccs &&
640 cmd_buffer->device->info.has_aux_map) {
641 anv_image_init_aux_tt(cmd_buffer, image, VK_IMAGE_ASPECT_DEPTH_BIT,
642 0, 1, 0, 1);
643 }
644 #endif
645
646 const enum isl_aux_state initial_state =
647 anv_layout_to_aux_state(&cmd_buffer->device->info, image,
648 VK_IMAGE_ASPECT_DEPTH_BIT,
649 initial_layout);
650 const enum isl_aux_state final_state =
651 anv_layout_to_aux_state(&cmd_buffer->device->info, image,
652 VK_IMAGE_ASPECT_DEPTH_BIT,
653 final_layout);
654
655 const bool initial_depth_valid =
656 isl_aux_state_has_valid_primary(initial_state);
657 const bool initial_hiz_valid =
658 isl_aux_state_has_valid_aux(initial_state);
659 const bool final_needs_depth =
660 isl_aux_state_has_valid_primary(final_state);
661 const bool final_needs_hiz =
662 isl_aux_state_has_valid_aux(final_state);
663
664 /* Getting into the pass-through state for Depth is tricky and involves
665 * both a resolve and an ambiguate. We don't handle that state right now
666 * as anv_layout_to_aux_state never returns it.
667 */
668 assert(final_state != ISL_AUX_STATE_PASS_THROUGH);
669
670 if (final_needs_depth && !initial_depth_valid) {
671 assert(initial_hiz_valid);
672 anv_image_hiz_op(cmd_buffer, image, VK_IMAGE_ASPECT_DEPTH_BIT,
673 0, base_layer, layer_count, ISL_AUX_OP_FULL_RESOLVE);
674 } else if (final_needs_hiz && !initial_hiz_valid) {
675 assert(initial_depth_valid);
676 anv_image_hiz_op(cmd_buffer, image, VK_IMAGE_ASPECT_DEPTH_BIT,
677 0, base_layer, layer_count, ISL_AUX_OP_AMBIGUATE);
678 }
679 }
680
681 static inline bool
682 vk_image_layout_stencil_write_optimal(VkImageLayout layout)
683 {
684 return layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
685 layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
686 layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL_KHR;
687 }
688
689 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
690 * the initial layout is undefined, the HiZ buffer and depth buffer will
691 * represent the same data at the end of this operation.
692 */
693 static void
694 transition_stencil_buffer(struct anv_cmd_buffer *cmd_buffer,
695 const struct anv_image *image,
696 uint32_t base_level, uint32_t level_count,
697 uint32_t base_layer, uint32_t layer_count,
698 VkImageLayout initial_layout,
699 VkImageLayout final_layout)
700 {
701 #if GEN_GEN == 7
702 uint32_t plane = anv_image_aspect_to_plane(image->aspects,
703 VK_IMAGE_ASPECT_STENCIL_BIT);
704
705 /* On gen7, we have to store a texturable version of the stencil buffer in
706 * a shadow whenever VK_IMAGE_USAGE_SAMPLED_BIT is set and copy back and
707 * forth at strategic points. Stencil writes are only allowed in following
708 * layouts:
709 *
710 * - VK_IMAGE_LAYOUT_GENERAL
711 * - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
712 * - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
713 * - VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
714 * - VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL_KHR
715 *
716 * For general, we have no nice opportunity to transition so we do the copy
717 * to the shadow unconditionally at the end of the subpass. For transfer
718 * destinations, we can update it as part of the transfer op. For the other
719 * layouts, we delay the copy until a transition into some other layout.
720 */
721 if (image->planes[plane].shadow_surface.isl.size_B > 0 &&
722 vk_image_layout_stencil_write_optimal(initial_layout) &&
723 !vk_image_layout_stencil_write_optimal(final_layout)) {
724 anv_image_copy_to_shadow(cmd_buffer, image,
725 VK_IMAGE_ASPECT_STENCIL_BIT,
726 base_level, level_count,
727 base_layer, layer_count);
728 }
729 #endif /* GEN_GEN == 7 */
730 }
731
732 #define MI_PREDICATE_SRC0 0x2400
733 #define MI_PREDICATE_SRC1 0x2408
734 #define MI_PREDICATE_RESULT 0x2418
735
736 static void
737 set_image_compressed_bit(struct anv_cmd_buffer *cmd_buffer,
738 const struct anv_image *image,
739 VkImageAspectFlagBits aspect,
740 uint32_t level,
741 uint32_t base_layer, uint32_t layer_count,
742 bool compressed)
743 {
744 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
745
746 /* We only have compression tracking for CCS_E */
747 if (image->planes[plane].aux_usage != ISL_AUX_USAGE_CCS_E)
748 return;
749
750 for (uint32_t a = 0; a < layer_count; a++) {
751 uint32_t layer = base_layer + a;
752 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
753 sdi.Address = anv_image_get_compression_state_addr(cmd_buffer->device,
754 image, aspect,
755 level, layer);
756 sdi.ImmediateData = compressed ? UINT32_MAX : 0;
757 }
758 }
759 }
760
761 static void
762 set_image_fast_clear_state(struct anv_cmd_buffer *cmd_buffer,
763 const struct anv_image *image,
764 VkImageAspectFlagBits aspect,
765 enum anv_fast_clear_type fast_clear)
766 {
767 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
768 sdi.Address = anv_image_get_fast_clear_type_addr(cmd_buffer->device,
769 image, aspect);
770 sdi.ImmediateData = fast_clear;
771 }
772
773 /* Whenever we have fast-clear, we consider that slice to be compressed.
774 * This makes building predicates much easier.
775 */
776 if (fast_clear != ANV_FAST_CLEAR_NONE)
777 set_image_compressed_bit(cmd_buffer, image, aspect, 0, 0, 1, true);
778 }
779
780 /* This is only really practical on haswell and above because it requires
781 * MI math in order to get it correct.
782 */
783 #if GEN_GEN >= 8 || GEN_IS_HASWELL
784 static void
785 anv_cmd_compute_resolve_predicate(struct anv_cmd_buffer *cmd_buffer,
786 const struct anv_image *image,
787 VkImageAspectFlagBits aspect,
788 uint32_t level, uint32_t array_layer,
789 enum isl_aux_op resolve_op,
790 enum anv_fast_clear_type fast_clear_supported)
791 {
792 struct gen_mi_builder b;
793 gen_mi_builder_init(&b, &cmd_buffer->batch);
794
795 const struct gen_mi_value fast_clear_type =
796 gen_mi_mem32(anv_image_get_fast_clear_type_addr(cmd_buffer->device,
797 image, aspect));
798
799 if (resolve_op == ISL_AUX_OP_FULL_RESOLVE) {
800 /* In this case, we're doing a full resolve which means we want the
801 * resolve to happen if any compression (including fast-clears) is
802 * present.
803 *
804 * In order to simplify the logic a bit, we make the assumption that,
805 * if the first slice has been fast-cleared, it is also marked as
806 * compressed. See also set_image_fast_clear_state.
807 */
808 const struct gen_mi_value compression_state =
809 gen_mi_mem32(anv_image_get_compression_state_addr(cmd_buffer->device,
810 image, aspect,
811 level, array_layer));
812 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0),
813 compression_state);
814 gen_mi_store(&b, compression_state, gen_mi_imm(0));
815
816 if (level == 0 && array_layer == 0) {
817 /* If the predicate is true, we want to write 0 to the fast clear type
818 * and, if it's false, leave it alone. We can do this by writing
819 *
820 * clear_type = clear_type & ~predicate;
821 */
822 struct gen_mi_value new_fast_clear_type =
823 gen_mi_iand(&b, fast_clear_type,
824 gen_mi_inot(&b, gen_mi_reg64(MI_PREDICATE_SRC0)));
825 gen_mi_store(&b, fast_clear_type, new_fast_clear_type);
826 }
827 } else if (level == 0 && array_layer == 0) {
828 /* In this case, we are doing a partial resolve to get rid of fast-clear
829 * colors. We don't care about the compression state but we do care
830 * about how much fast clear is allowed by the final layout.
831 */
832 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
833 assert(fast_clear_supported < ANV_FAST_CLEAR_ANY);
834
835 /* We need to compute (fast_clear_supported < image->fast_clear) */
836 struct gen_mi_value pred =
837 gen_mi_ult(&b, gen_mi_imm(fast_clear_supported), fast_clear_type);
838 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0),
839 gen_mi_value_ref(&b, pred));
840
841 /* If the predicate is true, we want to write 0 to the fast clear type
842 * and, if it's false, leave it alone. We can do this by writing
843 *
844 * clear_type = clear_type & ~predicate;
845 */
846 struct gen_mi_value new_fast_clear_type =
847 gen_mi_iand(&b, fast_clear_type, gen_mi_inot(&b, pred));
848 gen_mi_store(&b, fast_clear_type, new_fast_clear_type);
849 } else {
850 /* In this case, we're trying to do a partial resolve on a slice that
851 * doesn't have clear color. There's nothing to do.
852 */
853 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
854 return;
855 }
856
857 /* Set src1 to 0 and use a != condition */
858 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
859
860 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
861 mip.LoadOperation = LOAD_LOADINV;
862 mip.CombineOperation = COMBINE_SET;
863 mip.CompareOperation = COMPARE_SRCS_EQUAL;
864 }
865 }
866 #endif /* GEN_GEN >= 8 || GEN_IS_HASWELL */
867
868 #if GEN_GEN <= 8
869 static void
870 anv_cmd_simple_resolve_predicate(struct anv_cmd_buffer *cmd_buffer,
871 const struct anv_image *image,
872 VkImageAspectFlagBits aspect,
873 uint32_t level, uint32_t array_layer,
874 enum isl_aux_op resolve_op,
875 enum anv_fast_clear_type fast_clear_supported)
876 {
877 struct gen_mi_builder b;
878 gen_mi_builder_init(&b, &cmd_buffer->batch);
879
880 struct gen_mi_value fast_clear_type_mem =
881 gen_mi_mem32(anv_image_get_fast_clear_type_addr(cmd_buffer->device,
882 image, aspect));
883
884 /* This only works for partial resolves and only when the clear color is
885 * all or nothing. On the upside, this emits less command streamer code
886 * and works on Ivybridge and Bay Trail.
887 */
888 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
889 assert(fast_clear_supported != ANV_FAST_CLEAR_ANY);
890
891 /* We don't support fast clears on anything other than the first slice. */
892 if (level > 0 || array_layer > 0)
893 return;
894
895 /* On gen8, we don't have a concept of default clear colors because we
896 * can't sample from CCS surfaces. It's enough to just load the fast clear
897 * state into the predicate register.
898 */
899 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0), fast_clear_type_mem);
900 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
901 gen_mi_store(&b, fast_clear_type_mem, gen_mi_imm(0));
902
903 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
904 mip.LoadOperation = LOAD_LOADINV;
905 mip.CombineOperation = COMBINE_SET;
906 mip.CompareOperation = COMPARE_SRCS_EQUAL;
907 }
908 }
909 #endif /* GEN_GEN <= 8 */
910
911 static void
912 anv_cmd_predicated_ccs_resolve(struct anv_cmd_buffer *cmd_buffer,
913 const struct anv_image *image,
914 enum isl_format format,
915 struct isl_swizzle swizzle,
916 VkImageAspectFlagBits aspect,
917 uint32_t level, uint32_t array_layer,
918 enum isl_aux_op resolve_op,
919 enum anv_fast_clear_type fast_clear_supported)
920 {
921 const uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
922
923 #if GEN_GEN >= 9
924 anv_cmd_compute_resolve_predicate(cmd_buffer, image,
925 aspect, level, array_layer,
926 resolve_op, fast_clear_supported);
927 #else /* GEN_GEN <= 8 */
928 anv_cmd_simple_resolve_predicate(cmd_buffer, image,
929 aspect, level, array_layer,
930 resolve_op, fast_clear_supported);
931 #endif
932
933 /* CCS_D only supports full resolves and BLORP will assert on us if we try
934 * to do a partial resolve on a CCS_D surface.
935 */
936 if (resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE &&
937 image->planes[plane].aux_usage == ISL_AUX_USAGE_CCS_D)
938 resolve_op = ISL_AUX_OP_FULL_RESOLVE;
939
940 anv_image_ccs_op(cmd_buffer, image, format, swizzle, aspect,
941 level, array_layer, 1, resolve_op, NULL, true);
942 }
943
944 static void
945 anv_cmd_predicated_mcs_resolve(struct anv_cmd_buffer *cmd_buffer,
946 const struct anv_image *image,
947 enum isl_format format,
948 struct isl_swizzle swizzle,
949 VkImageAspectFlagBits aspect,
950 uint32_t array_layer,
951 enum isl_aux_op resolve_op,
952 enum anv_fast_clear_type fast_clear_supported)
953 {
954 assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
955 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
956
957 #if GEN_GEN >= 8 || GEN_IS_HASWELL
958 anv_cmd_compute_resolve_predicate(cmd_buffer, image,
959 aspect, 0, array_layer,
960 resolve_op, fast_clear_supported);
961
962 anv_image_mcs_op(cmd_buffer, image, format, swizzle, aspect,
963 array_layer, 1, resolve_op, NULL, true);
964 #else
965 unreachable("MCS resolves are unsupported on Ivybridge and Bay Trail");
966 #endif
967 }
968
969 void
970 genX(cmd_buffer_mark_image_written)(struct anv_cmd_buffer *cmd_buffer,
971 const struct anv_image *image,
972 VkImageAspectFlagBits aspect,
973 enum isl_aux_usage aux_usage,
974 uint32_t level,
975 uint32_t base_layer,
976 uint32_t layer_count)
977 {
978 /* The aspect must be exactly one of the image aspects. */
979 assert(util_bitcount(aspect) == 1 && (aspect & image->aspects));
980
981 /* The only compression types with more than just fast-clears are MCS,
982 * CCS_E, and HiZ. With HiZ we just trust the layout and don't actually
983 * track the current fast-clear and compression state. This leaves us
984 * with just MCS and CCS_E.
985 */
986 if (aux_usage != ISL_AUX_USAGE_CCS_E &&
987 aux_usage != ISL_AUX_USAGE_MCS)
988 return;
989
990 set_image_compressed_bit(cmd_buffer, image, aspect,
991 level, base_layer, layer_count, true);
992 }
993
994 static void
995 init_fast_clear_color(struct anv_cmd_buffer *cmd_buffer,
996 const struct anv_image *image,
997 VkImageAspectFlagBits aspect)
998 {
999 assert(cmd_buffer && image);
1000 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
1001
1002 set_image_fast_clear_state(cmd_buffer, image, aspect,
1003 ANV_FAST_CLEAR_NONE);
1004
1005 /* Initialize the struct fields that are accessed for fast-clears so that
1006 * the HW restrictions on the field values are satisfied.
1007 */
1008 struct anv_address addr =
1009 anv_image_get_clear_color_addr(cmd_buffer->device, image, aspect);
1010
1011 if (GEN_GEN >= 9) {
1012 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
1013 const unsigned num_dwords = GEN_GEN >= 10 ?
1014 isl_dev->ss.clear_color_state_size / 4 :
1015 isl_dev->ss.clear_value_size / 4;
1016 for (unsigned i = 0; i < num_dwords; i++) {
1017 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
1018 sdi.Address = addr;
1019 sdi.Address.offset += i * 4;
1020 sdi.ImmediateData = 0;
1021 }
1022 }
1023 } else {
1024 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
1025 sdi.Address = addr;
1026 if (GEN_GEN >= 8 || GEN_IS_HASWELL) {
1027 /* Pre-SKL, the dword containing the clear values also contains
1028 * other fields, so we need to initialize those fields to match the
1029 * values that would be in a color attachment.
1030 */
1031 sdi.ImmediateData = ISL_CHANNEL_SELECT_RED << 25 |
1032 ISL_CHANNEL_SELECT_GREEN << 22 |
1033 ISL_CHANNEL_SELECT_BLUE << 19 |
1034 ISL_CHANNEL_SELECT_ALPHA << 16;
1035 } else if (GEN_GEN == 7) {
1036 /* On IVB, the dword containing the clear values also contains
1037 * other fields that must be zero or can be zero.
1038 */
1039 sdi.ImmediateData = 0;
1040 }
1041 }
1042 }
1043 }
1044
1045 /* Copy the fast-clear value dword(s) between a surface state object and an
1046 * image's fast clear state buffer.
1047 */
1048 static void
1049 genX(copy_fast_clear_dwords)(struct anv_cmd_buffer *cmd_buffer,
1050 struct anv_state surface_state,
1051 const struct anv_image *image,
1052 VkImageAspectFlagBits aspect,
1053 bool copy_from_surface_state)
1054 {
1055 assert(cmd_buffer && image);
1056 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
1057
1058 struct anv_address ss_clear_addr = {
1059 .bo = cmd_buffer->device->surface_state_pool.block_pool.bo,
1060 .offset = surface_state.offset +
1061 cmd_buffer->device->isl_dev.ss.clear_value_offset,
1062 };
1063 const struct anv_address entry_addr =
1064 anv_image_get_clear_color_addr(cmd_buffer->device, image, aspect);
1065 unsigned copy_size = cmd_buffer->device->isl_dev.ss.clear_value_size;
1066
1067 #if GEN_GEN == 7
1068 /* On gen7, the combination of commands used here(MI_LOAD_REGISTER_MEM
1069 * and MI_STORE_REGISTER_MEM) can cause GPU hangs if any rendering is
1070 * in-flight when they are issued even if the memory touched is not
1071 * currently active for rendering. The weird bit is that it is not the
1072 * MI_LOAD/STORE_REGISTER_MEM commands which hang but rather the in-flight
1073 * rendering hangs such that the next stalling command after the
1074 * MI_LOAD/STORE_REGISTER_MEM commands will catch the hang.
1075 *
1076 * It is unclear exactly why this hang occurs. Both MI commands come with
1077 * warnings about the 3D pipeline but that doesn't seem to fully explain
1078 * it. My (Jason's) best theory is that it has something to do with the
1079 * fact that we're using a GPU state register as our temporary and that
1080 * something with reading/writing it is causing problems.
1081 *
1082 * In order to work around this issue, we emit a PIPE_CONTROL with the
1083 * command streamer stall bit set.
1084 */
1085 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
1086 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
1087 #endif
1088
1089 struct gen_mi_builder b;
1090 gen_mi_builder_init(&b, &cmd_buffer->batch);
1091
1092 if (copy_from_surface_state) {
1093 gen_mi_memcpy(&b, entry_addr, ss_clear_addr, copy_size);
1094 } else {
1095 gen_mi_memcpy(&b, ss_clear_addr, entry_addr, copy_size);
1096
1097 /* Updating a surface state object may require that the state cache be
1098 * invalidated. From the SKL PRM, Shared Functions -> State -> State
1099 * Caching:
1100 *
1101 * Whenever the RENDER_SURFACE_STATE object in memory pointed to by
1102 * the Binding Table Pointer (BTP) and Binding Table Index (BTI) is
1103 * modified [...], the L1 state cache must be invalidated to ensure
1104 * the new surface or sampler state is fetched from system memory.
1105 *
1106 * In testing, SKL doesn't actually seem to need this, but HSW does.
1107 */
1108 cmd_buffer->state.pending_pipe_bits |=
1109 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT;
1110 }
1111 }
1112
1113 /**
1114 * @brief Transitions a color buffer from one layout to another.
1115 *
1116 * See section 6.1.1. Image Layout Transitions of the Vulkan 1.0.50 spec for
1117 * more information.
1118 *
1119 * @param level_count VK_REMAINING_MIP_LEVELS isn't supported.
1120 * @param layer_count VK_REMAINING_ARRAY_LAYERS isn't supported. For 3D images,
1121 * this represents the maximum layers to transition at each
1122 * specified miplevel.
1123 */
1124 static void
1125 transition_color_buffer(struct anv_cmd_buffer *cmd_buffer,
1126 const struct anv_image *image,
1127 VkImageAspectFlagBits aspect,
1128 const uint32_t base_level, uint32_t level_count,
1129 uint32_t base_layer, uint32_t layer_count,
1130 VkImageLayout initial_layout,
1131 VkImageLayout final_layout)
1132 {
1133 struct anv_device *device = cmd_buffer->device;
1134 const struct gen_device_info *devinfo = &device->info;
1135 /* Validate the inputs. */
1136 assert(cmd_buffer);
1137 assert(image && image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
1138 /* These values aren't supported for simplicity's sake. */
1139 assert(level_count != VK_REMAINING_MIP_LEVELS &&
1140 layer_count != VK_REMAINING_ARRAY_LAYERS);
1141 /* Ensure the subresource range is valid. */
1142 UNUSED uint64_t last_level_num = base_level + level_count;
1143 const uint32_t max_depth = anv_minify(image->extent.depth, base_level);
1144 UNUSED const uint32_t image_layers = MAX2(image->array_size, max_depth);
1145 assert((uint64_t)base_layer + layer_count <= image_layers);
1146 assert(last_level_num <= image->levels);
1147 /* The spec disallows these final layouts. */
1148 assert(final_layout != VK_IMAGE_LAYOUT_UNDEFINED &&
1149 final_layout != VK_IMAGE_LAYOUT_PREINITIALIZED);
1150
1151 /* No work is necessary if the layout stays the same or if this subresource
1152 * range lacks auxiliary data.
1153 */
1154 if (initial_layout == final_layout)
1155 return;
1156
1157 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
1158
1159 if (image->planes[plane].shadow_surface.isl.size_B > 0 &&
1160 final_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
1161 /* This surface is a linear compressed image with a tiled shadow surface
1162 * for texturing. The client is about to use it in READ_ONLY_OPTIMAL so
1163 * we need to ensure the shadow copy is up-to-date.
1164 */
1165 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
1166 assert(image->planes[plane].surface.isl.tiling == ISL_TILING_LINEAR);
1167 assert(image->planes[plane].shadow_surface.isl.tiling != ISL_TILING_LINEAR);
1168 assert(isl_format_is_compressed(image->planes[plane].surface.isl.format));
1169 assert(plane == 0);
1170 anv_image_copy_to_shadow(cmd_buffer, image,
1171 VK_IMAGE_ASPECT_COLOR_BIT,
1172 base_level, level_count,
1173 base_layer, layer_count);
1174 }
1175
1176 if (base_layer >= anv_image_aux_layers(image, aspect, base_level))
1177 return;
1178
1179 assert(image->planes[plane].surface.isl.tiling != ISL_TILING_LINEAR);
1180
1181 if (initial_layout == VK_IMAGE_LAYOUT_UNDEFINED ||
1182 initial_layout == VK_IMAGE_LAYOUT_PREINITIALIZED) {
1183 #if GEN_GEN == 12
1184 if (device->physical->has_implicit_ccs && devinfo->has_aux_map) {
1185 anv_image_init_aux_tt(cmd_buffer, image, aspect,
1186 base_level, level_count,
1187 base_layer, layer_count);
1188 }
1189 #else
1190 assert(!(device->physical->has_implicit_ccs && devinfo->has_aux_map));
1191 #endif
1192
1193 /* A subresource in the undefined layout may have been aliased and
1194 * populated with any arrangement of bits. Therefore, we must initialize
1195 * the related aux buffer and clear buffer entry with desirable values.
1196 * An initial layout of PREINITIALIZED is the same as UNDEFINED for
1197 * images with VK_IMAGE_TILING_OPTIMAL.
1198 *
1199 * Initialize the relevant clear buffer entries.
1200 */
1201 if (base_level == 0 && base_layer == 0)
1202 init_fast_clear_color(cmd_buffer, image, aspect);
1203
1204 /* Initialize the aux buffers to enable correct rendering. In order to
1205 * ensure that things such as storage images work correctly, aux buffers
1206 * need to be initialized to valid data.
1207 *
1208 * Having an aux buffer with invalid data is a problem for two reasons:
1209 *
1210 * 1) Having an invalid value in the buffer can confuse the hardware.
1211 * For instance, with CCS_E on SKL, a two-bit CCS value of 2 is
1212 * invalid and leads to the hardware doing strange things. It
1213 * doesn't hang as far as we can tell but rendering corruption can
1214 * occur.
1215 *
1216 * 2) If this transition is into the GENERAL layout and we then use the
1217 * image as a storage image, then we must have the aux buffer in the
1218 * pass-through state so that, if we then go to texture from the
1219 * image, we get the results of our storage image writes and not the
1220 * fast clear color or other random data.
1221 *
1222 * For CCS both of the problems above are real demonstrable issues. In
1223 * that case, the only thing we can do is to perform an ambiguate to
1224 * transition the aux surface into the pass-through state.
1225 *
1226 * For MCS, (2) is never an issue because we don't support multisampled
1227 * storage images. In theory, issue (1) is a problem with MCS but we've
1228 * never seen it in the wild. For 4x and 16x, all bit patters could, in
1229 * theory, be interpreted as something but we don't know that all bit
1230 * patterns are actually valid. For 2x and 8x, you could easily end up
1231 * with the MCS referring to an invalid plane because not all bits of
1232 * the MCS value are actually used. Even though we've never seen issues
1233 * in the wild, it's best to play it safe and initialize the MCS. We
1234 * can use a fast-clear for MCS because we only ever touch from render
1235 * and texture (no image load store).
1236 */
1237 if (image->samples == 1) {
1238 for (uint32_t l = 0; l < level_count; l++) {
1239 const uint32_t level = base_level + l;
1240
1241 uint32_t aux_layers = anv_image_aux_layers(image, aspect, level);
1242 if (base_layer >= aux_layers)
1243 break; /* We will only get fewer layers as level increases */
1244 uint32_t level_layer_count =
1245 MIN2(layer_count, aux_layers - base_layer);
1246
1247 anv_image_ccs_op(cmd_buffer, image,
1248 image->planes[plane].surface.isl.format,
1249 ISL_SWIZZLE_IDENTITY,
1250 aspect, level, base_layer, level_layer_count,
1251 ISL_AUX_OP_AMBIGUATE, NULL, false);
1252
1253 if (image->planes[plane].aux_usage == ISL_AUX_USAGE_CCS_E) {
1254 set_image_compressed_bit(cmd_buffer, image, aspect,
1255 level, base_layer, level_layer_count,
1256 false);
1257 }
1258 }
1259 } else {
1260 if (image->samples == 4 || image->samples == 16) {
1261 anv_perf_warn(cmd_buffer->device, image,
1262 "Doing a potentially unnecessary fast-clear to "
1263 "define an MCS buffer.");
1264 }
1265
1266 assert(base_level == 0 && level_count == 1);
1267 anv_image_mcs_op(cmd_buffer, image,
1268 image->planes[plane].surface.isl.format,
1269 ISL_SWIZZLE_IDENTITY,
1270 aspect, base_layer, layer_count,
1271 ISL_AUX_OP_FAST_CLEAR, NULL, false);
1272 }
1273 return;
1274 }
1275
1276 const enum isl_aux_usage initial_aux_usage =
1277 anv_layout_to_aux_usage(devinfo, image, aspect, 0, initial_layout);
1278 const enum isl_aux_usage final_aux_usage =
1279 anv_layout_to_aux_usage(devinfo, image, aspect, 0, final_layout);
1280
1281 /* The current code assumes that there is no mixing of CCS_E and CCS_D.
1282 * We can handle transitions between CCS_D/E to and from NONE. What we
1283 * don't yet handle is switching between CCS_E and CCS_D within a given
1284 * image. Doing so in a performant way requires more detailed aux state
1285 * tracking such as what is done in i965. For now, just assume that we
1286 * only have one type of compression.
1287 */
1288 assert(initial_aux_usage == ISL_AUX_USAGE_NONE ||
1289 final_aux_usage == ISL_AUX_USAGE_NONE ||
1290 initial_aux_usage == final_aux_usage);
1291
1292 /* If initial aux usage is NONE, there is nothing to resolve */
1293 if (initial_aux_usage == ISL_AUX_USAGE_NONE)
1294 return;
1295
1296 enum isl_aux_op resolve_op = ISL_AUX_OP_NONE;
1297
1298 /* If the initial layout supports more fast clear than the final layout
1299 * then we need at least a partial resolve.
1300 */
1301 const enum anv_fast_clear_type initial_fast_clear =
1302 anv_layout_to_fast_clear_type(devinfo, image, aspect, initial_layout);
1303 const enum anv_fast_clear_type final_fast_clear =
1304 anv_layout_to_fast_clear_type(devinfo, image, aspect, final_layout);
1305 if (final_fast_clear < initial_fast_clear)
1306 resolve_op = ISL_AUX_OP_PARTIAL_RESOLVE;
1307
1308 if (initial_aux_usage == ISL_AUX_USAGE_CCS_E &&
1309 final_aux_usage != ISL_AUX_USAGE_CCS_E)
1310 resolve_op = ISL_AUX_OP_FULL_RESOLVE;
1311
1312 if (resolve_op == ISL_AUX_OP_NONE)
1313 return;
1314
1315 /* Perform a resolve to synchronize data between the main and aux buffer.
1316 * Before we begin, we must satisfy the cache flushing requirement specified
1317 * in the Sky Lake PRM Vol. 7, "MCS Buffer for Render Target(s)":
1318 *
1319 * Any transition from any value in {Clear, Render, Resolve} to a
1320 * different value in {Clear, Render, Resolve} requires end of pipe
1321 * synchronization.
1322 *
1323 * We perform a flush of the write cache before and after the clear and
1324 * resolve operations to meet this requirement.
1325 *
1326 * Unlike other drawing, fast clear operations are not properly
1327 * synchronized. The first PIPE_CONTROL here likely ensures that the
1328 * contents of the previous render or clear hit the render target before we
1329 * resolve and the second likely ensures that the resolve is complete before
1330 * we do any more rendering or clearing.
1331 */
1332 cmd_buffer->state.pending_pipe_bits |=
1333 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT | ANV_PIPE_END_OF_PIPE_SYNC_BIT;
1334
1335 for (uint32_t l = 0; l < level_count; l++) {
1336 uint32_t level = base_level + l;
1337
1338 uint32_t aux_layers = anv_image_aux_layers(image, aspect, level);
1339 if (base_layer >= aux_layers)
1340 break; /* We will only get fewer layers as level increases */
1341 uint32_t level_layer_count =
1342 MIN2(layer_count, aux_layers - base_layer);
1343
1344 for (uint32_t a = 0; a < level_layer_count; a++) {
1345 uint32_t array_layer = base_layer + a;
1346 if (image->samples == 1) {
1347 anv_cmd_predicated_ccs_resolve(cmd_buffer, image,
1348 image->planes[plane].surface.isl.format,
1349 ISL_SWIZZLE_IDENTITY,
1350 aspect, level, array_layer, resolve_op,
1351 final_fast_clear);
1352 } else {
1353 /* We only support fast-clear on the first layer so partial
1354 * resolves should not be used on other layers as they will use
1355 * the clear color stored in memory that is only valid for layer0.
1356 */
1357 if (resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE &&
1358 array_layer != 0)
1359 continue;
1360
1361 anv_cmd_predicated_mcs_resolve(cmd_buffer, image,
1362 image->planes[plane].surface.isl.format,
1363 ISL_SWIZZLE_IDENTITY,
1364 aspect, array_layer, resolve_op,
1365 final_fast_clear);
1366 }
1367 }
1368 }
1369
1370 cmd_buffer->state.pending_pipe_bits |=
1371 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT | ANV_PIPE_END_OF_PIPE_SYNC_BIT;
1372 }
1373
1374 /**
1375 * Setup anv_cmd_state::attachments for vkCmdBeginRenderPass.
1376 */
1377 static VkResult
1378 genX(cmd_buffer_setup_attachments)(struct anv_cmd_buffer *cmd_buffer,
1379 struct anv_render_pass *pass,
1380 const VkRenderPassBeginInfo *begin)
1381 {
1382 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
1383 struct anv_cmd_state *state = &cmd_buffer->state;
1384 struct anv_framebuffer *framebuffer = cmd_buffer->state.framebuffer;
1385
1386 vk_free(&cmd_buffer->pool->alloc, state->attachments);
1387
1388 if (pass->attachment_count > 0) {
1389 state->attachments = vk_alloc(&cmd_buffer->pool->alloc,
1390 pass->attachment_count *
1391 sizeof(state->attachments[0]),
1392 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1393 if (state->attachments == NULL) {
1394 /* Propagate VK_ERROR_OUT_OF_HOST_MEMORY to vkEndCommandBuffer */
1395 return anv_batch_set_error(&cmd_buffer->batch,
1396 VK_ERROR_OUT_OF_HOST_MEMORY);
1397 }
1398 } else {
1399 state->attachments = NULL;
1400 }
1401
1402 /* Reserve one for the NULL state. */
1403 unsigned num_states = 1;
1404 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
1405 if (vk_format_is_color(pass->attachments[i].format))
1406 num_states++;
1407
1408 if (need_input_attachment_state(&pass->attachments[i]))
1409 num_states++;
1410 }
1411
1412 const uint32_t ss_stride = align_u32(isl_dev->ss.size, isl_dev->ss.align);
1413 state->render_pass_states =
1414 anv_state_stream_alloc(&cmd_buffer->surface_state_stream,
1415 num_states * ss_stride, isl_dev->ss.align);
1416
1417 struct anv_state next_state = state->render_pass_states;
1418 next_state.alloc_size = isl_dev->ss.size;
1419
1420 state->null_surface_state = next_state;
1421 next_state.offset += ss_stride;
1422 next_state.map += ss_stride;
1423
1424 const VkRenderPassAttachmentBeginInfoKHR *begin_attachment =
1425 vk_find_struct_const(begin, RENDER_PASS_ATTACHMENT_BEGIN_INFO_KHR);
1426
1427 if (begin && !begin_attachment)
1428 assert(pass->attachment_count == framebuffer->attachment_count);
1429
1430 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
1431 if (vk_format_is_color(pass->attachments[i].format)) {
1432 state->attachments[i].color.state = next_state;
1433 next_state.offset += ss_stride;
1434 next_state.map += ss_stride;
1435 }
1436
1437 if (need_input_attachment_state(&pass->attachments[i])) {
1438 state->attachments[i].input.state = next_state;
1439 next_state.offset += ss_stride;
1440 next_state.map += ss_stride;
1441 }
1442
1443 if (begin_attachment && begin_attachment->attachmentCount != 0) {
1444 assert(begin_attachment->attachmentCount == pass->attachment_count);
1445 ANV_FROM_HANDLE(anv_image_view, iview, begin_attachment->pAttachments[i]);
1446 cmd_buffer->state.attachments[i].image_view = iview;
1447 } else if (framebuffer && i < framebuffer->attachment_count) {
1448 cmd_buffer->state.attachments[i].image_view = framebuffer->attachments[i];
1449 }
1450 }
1451 assert(next_state.offset == state->render_pass_states.offset +
1452 state->render_pass_states.alloc_size);
1453
1454 if (begin) {
1455 isl_null_fill_state(isl_dev, state->null_surface_state.map,
1456 isl_extent3d(framebuffer->width,
1457 framebuffer->height,
1458 framebuffer->layers));
1459
1460 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
1461 struct anv_render_pass_attachment *att = &pass->attachments[i];
1462 VkImageAspectFlags att_aspects = vk_format_aspects(att->format);
1463 VkImageAspectFlags clear_aspects = 0;
1464 VkImageAspectFlags load_aspects = 0;
1465
1466 if (att_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1467 /* color attachment */
1468 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1469 clear_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
1470 } else if (att->load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
1471 load_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
1472 }
1473 } else {
1474 /* depthstencil attachment */
1475 if (att_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
1476 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1477 clear_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1478 } else if (att->load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
1479 load_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1480 }
1481 }
1482 if (att_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
1483 if (att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1484 clear_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1485 } else if (att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
1486 load_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1487 }
1488 }
1489 }
1490
1491 state->attachments[i].current_layout = att->initial_layout;
1492 state->attachments[i].current_stencil_layout = att->stencil_initial_layout;
1493 state->attachments[i].pending_clear_aspects = clear_aspects;
1494 state->attachments[i].pending_load_aspects = load_aspects;
1495 if (clear_aspects)
1496 state->attachments[i].clear_value = begin->pClearValues[i];
1497
1498 struct anv_image_view *iview = cmd_buffer->state.attachments[i].image_view;
1499 anv_assert(iview->vk_format == att->format);
1500
1501 const uint32_t num_layers = iview->planes[0].isl.array_len;
1502 state->attachments[i].pending_clear_views = (1 << num_layers) - 1;
1503
1504 union isl_color_value clear_color = { .u32 = { 0, } };
1505 if (att_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1506 anv_assert(iview->n_planes == 1);
1507 assert(att_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
1508 color_attachment_compute_aux_usage(cmd_buffer->device,
1509 state, i, begin->renderArea,
1510 &clear_color);
1511
1512 anv_image_fill_surface_state(cmd_buffer->device,
1513 iview->image,
1514 VK_IMAGE_ASPECT_COLOR_BIT,
1515 &iview->planes[0].isl,
1516 ISL_SURF_USAGE_RENDER_TARGET_BIT,
1517 state->attachments[i].aux_usage,
1518 &clear_color,
1519 0,
1520 &state->attachments[i].color,
1521 NULL);
1522
1523 add_surface_state_relocs(cmd_buffer, state->attachments[i].color);
1524 } else {
1525 depth_stencil_attachment_compute_aux_usage(cmd_buffer->device,
1526 state, i,
1527 begin->renderArea);
1528 }
1529
1530 if (need_input_attachment_state(&pass->attachments[i])) {
1531 anv_image_fill_surface_state(cmd_buffer->device,
1532 iview->image,
1533 VK_IMAGE_ASPECT_COLOR_BIT,
1534 &iview->planes[0].isl,
1535 ISL_SURF_USAGE_TEXTURE_BIT,
1536 state->attachments[i].input_aux_usage,
1537 &clear_color,
1538 0,
1539 &state->attachments[i].input,
1540 NULL);
1541
1542 add_surface_state_relocs(cmd_buffer, state->attachments[i].input);
1543 }
1544 }
1545 }
1546
1547 return VK_SUCCESS;
1548 }
1549
1550 VkResult
1551 genX(BeginCommandBuffer)(
1552 VkCommandBuffer commandBuffer,
1553 const VkCommandBufferBeginInfo* pBeginInfo)
1554 {
1555 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1556
1557 /* If this is the first vkBeginCommandBuffer, we must *initialize* the
1558 * command buffer's state. Otherwise, we must *reset* its state. In both
1559 * cases we reset it.
1560 *
1561 * From the Vulkan 1.0 spec:
1562 *
1563 * If a command buffer is in the executable state and the command buffer
1564 * was allocated from a command pool with the
1565 * VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT flag set, then
1566 * vkBeginCommandBuffer implicitly resets the command buffer, behaving
1567 * as if vkResetCommandBuffer had been called with
1568 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT not set. It then puts
1569 * the command buffer in the recording state.
1570 */
1571 anv_cmd_buffer_reset(cmd_buffer);
1572
1573 cmd_buffer->usage_flags = pBeginInfo->flags;
1574
1575 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY ||
1576 !(cmd_buffer->usage_flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT));
1577
1578 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
1579
1580 /* We sometimes store vertex data in the dynamic state buffer for blorp
1581 * operations and our dynamic state stream may re-use data from previous
1582 * command buffers. In order to prevent stale cache data, we flush the VF
1583 * cache. We could do this on every blorp call but that's not really
1584 * needed as all of the data will get written by the CPU prior to the GPU
1585 * executing anything. The chances are fairly high that they will use
1586 * blorp at least once per primary command buffer so it shouldn't be
1587 * wasted.
1588 *
1589 * There is also a workaround on gen8 which requires us to invalidate the
1590 * VF cache occasionally. It's easier if we can assume we start with a
1591 * fresh cache (See also genX(cmd_buffer_set_binding_for_gen8_vb_flush).)
1592 */
1593 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1594
1595 /* Re-emit the aux table register in every command buffer. This way we're
1596 * ensured that we have the table even if this command buffer doesn't
1597 * initialize any images.
1598 */
1599 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_AUX_TABLE_INVALIDATE_BIT;
1600
1601 /* We send an "Indirect State Pointers Disable" packet at
1602 * EndCommandBuffer, so all push contant packets are ignored during a
1603 * context restore. Documentation says after that command, we need to
1604 * emit push constants again before any rendering operation. So we
1605 * flag them dirty here to make sure they get emitted.
1606 */
1607 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_ALL_GRAPHICS;
1608
1609 VkResult result = VK_SUCCESS;
1610 if (cmd_buffer->usage_flags &
1611 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
1612 assert(pBeginInfo->pInheritanceInfo);
1613 cmd_buffer->state.pass =
1614 anv_render_pass_from_handle(pBeginInfo->pInheritanceInfo->renderPass);
1615 cmd_buffer->state.subpass =
1616 &cmd_buffer->state.pass->subpasses[pBeginInfo->pInheritanceInfo->subpass];
1617
1618 /* This is optional in the inheritance info. */
1619 cmd_buffer->state.framebuffer =
1620 anv_framebuffer_from_handle(pBeginInfo->pInheritanceInfo->framebuffer);
1621
1622 result = genX(cmd_buffer_setup_attachments)(cmd_buffer,
1623 cmd_buffer->state.pass, NULL);
1624
1625 /* Record that HiZ is enabled if we can. */
1626 if (cmd_buffer->state.framebuffer) {
1627 const struct anv_image_view * const iview =
1628 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
1629
1630 if (iview) {
1631 VkImageLayout layout =
1632 cmd_buffer->state.subpass->depth_stencil_attachment->layout;
1633
1634 enum isl_aux_usage aux_usage =
1635 anv_layout_to_aux_usage(&cmd_buffer->device->info, iview->image,
1636 VK_IMAGE_ASPECT_DEPTH_BIT,
1637 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
1638 layout);
1639
1640 cmd_buffer->state.hiz_enabled = isl_aux_usage_has_hiz(aux_usage);
1641 }
1642 }
1643
1644 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
1645 }
1646
1647 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1648 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
1649 const VkCommandBufferInheritanceConditionalRenderingInfoEXT *conditional_rendering_info =
1650 vk_find_struct_const(pBeginInfo->pInheritanceInfo->pNext, COMMAND_BUFFER_INHERITANCE_CONDITIONAL_RENDERING_INFO_EXT);
1651
1652 /* If secondary buffer supports conditional rendering
1653 * we should emit commands as if conditional rendering is enabled.
1654 */
1655 cmd_buffer->state.conditional_render_enabled =
1656 conditional_rendering_info && conditional_rendering_info->conditionalRenderingEnable;
1657 }
1658 #endif
1659
1660 return result;
1661 }
1662
1663 /* From the PRM, Volume 2a:
1664 *
1665 * "Indirect State Pointers Disable
1666 *
1667 * At the completion of the post-sync operation associated with this pipe
1668 * control packet, the indirect state pointers in the hardware are
1669 * considered invalid; the indirect pointers are not saved in the context.
1670 * If any new indirect state commands are executed in the command stream
1671 * while the pipe control is pending, the new indirect state commands are
1672 * preserved.
1673 *
1674 * [DevIVB+]: Using Invalidate State Pointer (ISP) only inhibits context
1675 * restoring of Push Constant (3DSTATE_CONSTANT_*) commands. Push Constant
1676 * commands are only considered as Indirect State Pointers. Once ISP is
1677 * issued in a context, SW must initialize by programming push constant
1678 * commands for all the shaders (at least to zero length) before attempting
1679 * any rendering operation for the same context."
1680 *
1681 * 3DSTATE_CONSTANT_* packets are restored during a context restore,
1682 * even though they point to a BO that has been already unreferenced at
1683 * the end of the previous batch buffer. This has been fine so far since
1684 * we are protected by these scratch page (every address not covered by
1685 * a BO should be pointing to the scratch page). But on CNL, it is
1686 * causing a GPU hang during context restore at the 3DSTATE_CONSTANT_*
1687 * instruction.
1688 *
1689 * The flag "Indirect State Pointers Disable" in PIPE_CONTROL tells the
1690 * hardware to ignore previous 3DSTATE_CONSTANT_* packets during a
1691 * context restore, so the mentioned hang doesn't happen. However,
1692 * software must program push constant commands for all stages prior to
1693 * rendering anything. So we flag them dirty in BeginCommandBuffer.
1694 *
1695 * Finally, we also make sure to stall at pixel scoreboard to make sure the
1696 * constants have been loaded into the EUs prior to disable the push constants
1697 * so that it doesn't hang a previous 3DPRIMITIVE.
1698 */
1699 static void
1700 emit_isp_disable(struct anv_cmd_buffer *cmd_buffer)
1701 {
1702 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1703 pc.StallAtPixelScoreboard = true;
1704 pc.CommandStreamerStallEnable = true;
1705 }
1706 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1707 pc.IndirectStatePointersDisable = true;
1708 pc.CommandStreamerStallEnable = true;
1709 }
1710 }
1711
1712 VkResult
1713 genX(EndCommandBuffer)(
1714 VkCommandBuffer commandBuffer)
1715 {
1716 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1717
1718 if (anv_batch_has_error(&cmd_buffer->batch))
1719 return cmd_buffer->batch.status;
1720
1721 /* We want every command buffer to start with the PMA fix in a known state,
1722 * so we disable it at the end of the command buffer.
1723 */
1724 genX(cmd_buffer_enable_pma_fix)(cmd_buffer, false);
1725
1726 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
1727
1728 emit_isp_disable(cmd_buffer);
1729
1730 anv_cmd_buffer_end_batch_buffer(cmd_buffer);
1731
1732 return VK_SUCCESS;
1733 }
1734
1735 void
1736 genX(CmdExecuteCommands)(
1737 VkCommandBuffer commandBuffer,
1738 uint32_t commandBufferCount,
1739 const VkCommandBuffer* pCmdBuffers)
1740 {
1741 ANV_FROM_HANDLE(anv_cmd_buffer, primary, commandBuffer);
1742
1743 assert(primary->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1744
1745 if (anv_batch_has_error(&primary->batch))
1746 return;
1747
1748 /* The secondary command buffers will assume that the PMA fix is disabled
1749 * when they begin executing. Make sure this is true.
1750 */
1751 genX(cmd_buffer_enable_pma_fix)(primary, false);
1752
1753 /* The secondary command buffer doesn't know which textures etc. have been
1754 * flushed prior to their execution. Apply those flushes now.
1755 */
1756 genX(cmd_buffer_apply_pipe_flushes)(primary);
1757
1758 for (uint32_t i = 0; i < commandBufferCount; i++) {
1759 ANV_FROM_HANDLE(anv_cmd_buffer, secondary, pCmdBuffers[i]);
1760
1761 assert(secondary->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY);
1762 assert(!anv_batch_has_error(&secondary->batch));
1763
1764 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1765 if (secondary->state.conditional_render_enabled) {
1766 if (!primary->state.conditional_render_enabled) {
1767 /* Secondary buffer is constructed as if it will be executed
1768 * with conditional rendering, we should satisfy this dependency
1769 * regardless of conditional rendering being enabled in primary.
1770 */
1771 struct gen_mi_builder b;
1772 gen_mi_builder_init(&b, &primary->batch);
1773 gen_mi_store(&b, gen_mi_reg64(ANV_PREDICATE_RESULT_REG),
1774 gen_mi_imm(UINT64_MAX));
1775 }
1776 }
1777 #endif
1778
1779 if (secondary->usage_flags &
1780 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
1781 /* If we're continuing a render pass from the primary, we need to
1782 * copy the surface states for the current subpass into the storage
1783 * we allocated for them in BeginCommandBuffer.
1784 */
1785 struct anv_bo *ss_bo =
1786 primary->device->surface_state_pool.block_pool.bo;
1787 struct anv_state src_state = primary->state.render_pass_states;
1788 struct anv_state dst_state = secondary->state.render_pass_states;
1789 assert(src_state.alloc_size == dst_state.alloc_size);
1790
1791 genX(cmd_buffer_so_memcpy)(primary,
1792 (struct anv_address) {
1793 .bo = ss_bo,
1794 .offset = dst_state.offset,
1795 },
1796 (struct anv_address) {
1797 .bo = ss_bo,
1798 .offset = src_state.offset,
1799 },
1800 src_state.alloc_size);
1801 }
1802
1803 anv_cmd_buffer_add_secondary(primary, secondary);
1804 }
1805
1806 /* The secondary isn't counted in our VF cache tracking so we need to
1807 * invalidate the whole thing.
1808 */
1809 if (GEN_GEN >= 8 && GEN_GEN <= 9) {
1810 primary->state.pending_pipe_bits |=
1811 ANV_PIPE_CS_STALL_BIT | ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1812 }
1813
1814 /* The secondary may have selected a different pipeline (3D or compute) and
1815 * may have changed the current L3$ configuration. Reset our tracking
1816 * variables to invalid values to ensure that we re-emit these in the case
1817 * where we do any draws or compute dispatches from the primary after the
1818 * secondary has returned.
1819 */
1820 primary->state.current_pipeline = UINT32_MAX;
1821 primary->state.current_l3_config = NULL;
1822 primary->state.current_hash_scale = 0;
1823
1824 /* Each of the secondary command buffers will use its own state base
1825 * address. We need to re-emit state base address for the primary after
1826 * all of the secondaries are done.
1827 *
1828 * TODO: Maybe we want to make this a dirty bit to avoid extra state base
1829 * address calls?
1830 */
1831 genX(cmd_buffer_emit_state_base_address)(primary);
1832 }
1833
1834 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
1835 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
1836 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
1837
1838 /**
1839 * Program the hardware to use the specified L3 configuration.
1840 */
1841 void
1842 genX(cmd_buffer_config_l3)(struct anv_cmd_buffer *cmd_buffer,
1843 const struct gen_l3_config *cfg)
1844 {
1845 assert(cfg);
1846 if (cfg == cmd_buffer->state.current_l3_config)
1847 return;
1848
1849 if (unlikely(INTEL_DEBUG & DEBUG_L3)) {
1850 intel_logd("L3 config transition: ");
1851 gen_dump_l3_config(cfg, stderr);
1852 }
1853
1854 UNUSED const bool has_slm = cfg->n[GEN_L3P_SLM];
1855
1856 /* According to the hardware docs, the L3 partitioning can only be changed
1857 * while the pipeline is completely drained and the caches are flushed,
1858 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
1859 */
1860 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1861 pc.DCFlushEnable = true;
1862 pc.PostSyncOperation = NoWrite;
1863 pc.CommandStreamerStallEnable = true;
1864 }
1865
1866 /* ...followed by a second pipelined PIPE_CONTROL that initiates
1867 * invalidation of the relevant caches. Note that because RO invalidation
1868 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
1869 * command is processed by the CS) we cannot combine it with the previous
1870 * stalling flush as the hardware documentation suggests, because that
1871 * would cause the CS to stall on previous rendering *after* RO
1872 * invalidation and wouldn't prevent the RO caches from being polluted by
1873 * concurrent rendering before the stall completes. This intentionally
1874 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
1875 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
1876 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
1877 * already guarantee that there is no concurrent GPGPU kernel execution
1878 * (see SKL HSD 2132585).
1879 */
1880 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1881 pc.TextureCacheInvalidationEnable = true;
1882 pc.ConstantCacheInvalidationEnable = true;
1883 pc.InstructionCacheInvalidateEnable = true;
1884 pc.StateCacheInvalidationEnable = true;
1885 pc.PostSyncOperation = NoWrite;
1886 }
1887
1888 /* Now send a third stalling flush to make sure that invalidation is
1889 * complete when the L3 configuration registers are modified.
1890 */
1891 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1892 pc.DCFlushEnable = true;
1893 pc.PostSyncOperation = NoWrite;
1894 pc.CommandStreamerStallEnable = true;
1895 }
1896
1897 #if GEN_GEN >= 8
1898
1899 assert(!cfg->n[GEN_L3P_IS] && !cfg->n[GEN_L3P_C] && !cfg->n[GEN_L3P_T]);
1900
1901 #if GEN_GEN >= 12
1902 #define L3_ALLOCATION_REG GENX(L3ALLOC)
1903 #define L3_ALLOCATION_REG_num GENX(L3ALLOC_num)
1904 #else
1905 #define L3_ALLOCATION_REG GENX(L3CNTLREG)
1906 #define L3_ALLOCATION_REG_num GENX(L3CNTLREG_num)
1907 #endif
1908
1909 uint32_t l3cr;
1910 anv_pack_struct(&l3cr, L3_ALLOCATION_REG,
1911 #if GEN_GEN < 11
1912 .SLMEnable = has_slm,
1913 #endif
1914 #if GEN_GEN == 11
1915 /* WA_1406697149: Bit 9 "Error Detection Behavior Control" must be set
1916 * in L3CNTLREG register. The default setting of the bit is not the
1917 * desirable behavior.
1918 */
1919 .ErrorDetectionBehaviorControl = true,
1920 .UseFullWays = true,
1921 #endif
1922 .URBAllocation = cfg->n[GEN_L3P_URB],
1923 .ROAllocation = cfg->n[GEN_L3P_RO],
1924 .DCAllocation = cfg->n[GEN_L3P_DC],
1925 .AllAllocation = cfg->n[GEN_L3P_ALL]);
1926
1927 /* Set up the L3 partitioning. */
1928 emit_lri(&cmd_buffer->batch, L3_ALLOCATION_REG_num, l3cr);
1929
1930 #else
1931
1932 const bool has_dc = cfg->n[GEN_L3P_DC] || cfg->n[GEN_L3P_ALL];
1933 const bool has_is = cfg->n[GEN_L3P_IS] || cfg->n[GEN_L3P_RO] ||
1934 cfg->n[GEN_L3P_ALL];
1935 const bool has_c = cfg->n[GEN_L3P_C] || cfg->n[GEN_L3P_RO] ||
1936 cfg->n[GEN_L3P_ALL];
1937 const bool has_t = cfg->n[GEN_L3P_T] || cfg->n[GEN_L3P_RO] ||
1938 cfg->n[GEN_L3P_ALL];
1939
1940 assert(!cfg->n[GEN_L3P_ALL]);
1941
1942 /* When enabled SLM only uses a portion of the L3 on half of the banks,
1943 * the matching space on the remaining banks has to be allocated to a
1944 * client (URB for all validated configurations) set to the
1945 * lower-bandwidth 2-bank address hashing mode.
1946 */
1947 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
1948 const bool urb_low_bw = has_slm && !devinfo->is_baytrail;
1949 assert(!urb_low_bw || cfg->n[GEN_L3P_URB] == cfg->n[GEN_L3P_SLM]);
1950
1951 /* Minimum number of ways that can be allocated to the URB. */
1952 const unsigned n0_urb = devinfo->is_baytrail ? 32 : 0;
1953 assert(cfg->n[GEN_L3P_URB] >= n0_urb);
1954
1955 uint32_t l3sqcr1, l3cr2, l3cr3;
1956 anv_pack_struct(&l3sqcr1, GENX(L3SQCREG1),
1957 .ConvertDC_UC = !has_dc,
1958 .ConvertIS_UC = !has_is,
1959 .ConvertC_UC = !has_c,
1960 .ConvertT_UC = !has_t);
1961 l3sqcr1 |=
1962 GEN_IS_HASWELL ? HSW_L3SQCREG1_SQGHPCI_DEFAULT :
1963 devinfo->is_baytrail ? VLV_L3SQCREG1_SQGHPCI_DEFAULT :
1964 IVB_L3SQCREG1_SQGHPCI_DEFAULT;
1965
1966 anv_pack_struct(&l3cr2, GENX(L3CNTLREG2),
1967 .SLMEnable = has_slm,
1968 .URBLowBandwidth = urb_low_bw,
1969 .URBAllocation = cfg->n[GEN_L3P_URB] - n0_urb,
1970 #if !GEN_IS_HASWELL
1971 .ALLAllocation = cfg->n[GEN_L3P_ALL],
1972 #endif
1973 .ROAllocation = cfg->n[GEN_L3P_RO],
1974 .DCAllocation = cfg->n[GEN_L3P_DC]);
1975
1976 anv_pack_struct(&l3cr3, GENX(L3CNTLREG3),
1977 .ISAllocation = cfg->n[GEN_L3P_IS],
1978 .ISLowBandwidth = 0,
1979 .CAllocation = cfg->n[GEN_L3P_C],
1980 .CLowBandwidth = 0,
1981 .TAllocation = cfg->n[GEN_L3P_T],
1982 .TLowBandwidth = 0);
1983
1984 /* Set up the L3 partitioning. */
1985 emit_lri(&cmd_buffer->batch, GENX(L3SQCREG1_num), l3sqcr1);
1986 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG2_num), l3cr2);
1987 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG3_num), l3cr3);
1988
1989 #if GEN_IS_HASWELL
1990 if (cmd_buffer->device->physical->cmd_parser_version >= 4) {
1991 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
1992 * them disabled to avoid crashing the system hard.
1993 */
1994 uint32_t scratch1, chicken3;
1995 anv_pack_struct(&scratch1, GENX(SCRATCH1),
1996 .L3AtomicDisable = !has_dc);
1997 anv_pack_struct(&chicken3, GENX(CHICKEN3),
1998 .L3AtomicDisableMask = true,
1999 .L3AtomicDisable = !has_dc);
2000 emit_lri(&cmd_buffer->batch, GENX(SCRATCH1_num), scratch1);
2001 emit_lri(&cmd_buffer->batch, GENX(CHICKEN3_num), chicken3);
2002 }
2003 #endif
2004
2005 #endif
2006
2007 cmd_buffer->state.current_l3_config = cfg;
2008 }
2009
2010 void
2011 genX(cmd_buffer_apply_pipe_flushes)(struct anv_cmd_buffer *cmd_buffer)
2012 {
2013 UNUSED const struct gen_device_info *devinfo = &cmd_buffer->device->info;
2014 enum anv_pipe_bits bits = cmd_buffer->state.pending_pipe_bits;
2015
2016 if (cmd_buffer->device->physical->always_flush_cache)
2017 bits |= ANV_PIPE_FLUSH_BITS | ANV_PIPE_INVALIDATE_BITS;
2018
2019 /*
2020 * From Sandybridge PRM, volume 2, "1.7.2 End-of-Pipe Synchronization":
2021 *
2022 * Write synchronization is a special case of end-of-pipe
2023 * synchronization that requires that the render cache and/or depth
2024 * related caches are flushed to memory, where the data will become
2025 * globally visible. This type of synchronization is required prior to
2026 * SW (CPU) actually reading the result data from memory, or initiating
2027 * an operation that will use as a read surface (such as a texture
2028 * surface) a previous render target and/or depth/stencil buffer
2029 *
2030 *
2031 * From Haswell PRM, volume 2, part 1, "End-of-Pipe Synchronization":
2032 *
2033 * Exercising the write cache flush bits (Render Target Cache Flush
2034 * Enable, Depth Cache Flush Enable, DC Flush) in PIPE_CONTROL only
2035 * ensures the write caches are flushed and doesn't guarantee the data
2036 * is globally visible.
2037 *
2038 * SW can track the completion of the end-of-pipe-synchronization by
2039 * using "Notify Enable" and "PostSync Operation - Write Immediate
2040 * Data" in the PIPE_CONTROL command.
2041 *
2042 * In other words, flushes are pipelined while invalidations are handled
2043 * immediately. Therefore, if we're flushing anything then we need to
2044 * schedule an end-of-pipe sync before any invalidations can happen.
2045 */
2046 if (bits & ANV_PIPE_FLUSH_BITS)
2047 bits |= ANV_PIPE_NEEDS_END_OF_PIPE_SYNC_BIT;
2048
2049
2050 /* HSD 1209978178: docs say that before programming the aux table:
2051 *
2052 * "Driver must ensure that the engine is IDLE but ensure it doesn't
2053 * add extra flushes in the case it knows that the engine is already
2054 * IDLE."
2055 */
2056 if (GEN_GEN == 12 && (bits & ANV_PIPE_AUX_TABLE_INVALIDATE_BIT))
2057 bits |= ANV_PIPE_NEEDS_END_OF_PIPE_SYNC_BIT;
2058
2059 /* If we're going to do an invalidate and we have a pending end-of-pipe
2060 * sync that has yet to be resolved, we do the end-of-pipe sync now.
2061 */
2062 if ((bits & ANV_PIPE_INVALIDATE_BITS) &&
2063 (bits & ANV_PIPE_NEEDS_END_OF_PIPE_SYNC_BIT)) {
2064 bits |= ANV_PIPE_END_OF_PIPE_SYNC_BIT;
2065 bits &= ~ANV_PIPE_NEEDS_END_OF_PIPE_SYNC_BIT;
2066 }
2067
2068 if (GEN_GEN >= 12 &&
2069 ((bits & ANV_PIPE_DEPTH_CACHE_FLUSH_BIT) ||
2070 (bits & ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT))) {
2071 /* From the PIPE_CONTROL instruction table, bit 28 (Tile Cache Flush
2072 * Enable):
2073 *
2074 * Unified Cache (Tile Cache Disabled):
2075 *
2076 * When the Color and Depth (Z) streams are enabled to be cached in
2077 * the DC space of L2, Software must use "Render Target Cache Flush
2078 * Enable" and "Depth Cache Flush Enable" along with "Tile Cache
2079 * Flush" for getting the color and depth (Z) write data to be
2080 * globally observable. In this mode of operation it is not required
2081 * to set "CS Stall" upon setting "Tile Cache Flush" bit.
2082 */
2083 bits |= ANV_PIPE_TILE_CACHE_FLUSH_BIT;
2084 }
2085
2086 /* GEN:BUG:1409226450, Wait for EU to be idle before pipe control which
2087 * invalidates the instruction cache
2088 */
2089 if (GEN_GEN == 12 && (bits & ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT))
2090 bits |= ANV_PIPE_CS_STALL_BIT | ANV_PIPE_STALL_AT_SCOREBOARD_BIT;
2091
2092 if ((GEN_GEN >= 8 && GEN_GEN <= 9) &&
2093 (bits & ANV_PIPE_CS_STALL_BIT) &&
2094 (bits & ANV_PIPE_VF_CACHE_INVALIDATE_BIT)) {
2095 /* If we are doing a VF cache invalidate AND a CS stall (it must be
2096 * both) then we can reset our vertex cache tracking.
2097 */
2098 memset(cmd_buffer->state.gfx.vb_dirty_ranges, 0,
2099 sizeof(cmd_buffer->state.gfx.vb_dirty_ranges));
2100 memset(&cmd_buffer->state.gfx.ib_dirty_range, 0,
2101 sizeof(cmd_buffer->state.gfx.ib_dirty_range));
2102 }
2103
2104 /* Project: SKL / Argument: LRI Post Sync Operation [23]
2105 *
2106 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
2107 * programmed prior to programming a PIPECONTROL command with "LRI
2108 * Post Sync Operation" in GPGPU mode of operation (i.e when
2109 * PIPELINE_SELECT command is set to GPGPU mode of operation)."
2110 *
2111 * The same text exists a few rows below for Post Sync Op.
2112 *
2113 * On Gen12 this is GEN:BUG:1607156449.
2114 */
2115 if (bits & ANV_PIPE_POST_SYNC_BIT) {
2116 if ((GEN_GEN == 9 || (GEN_GEN == 12 && devinfo->revision == 0 /* A0 */)) &&
2117 cmd_buffer->state.current_pipeline == GPGPU)
2118 bits |= ANV_PIPE_CS_STALL_BIT;
2119 bits &= ~ANV_PIPE_POST_SYNC_BIT;
2120 }
2121
2122 if (bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT |
2123 ANV_PIPE_END_OF_PIPE_SYNC_BIT)) {
2124 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
2125 #if GEN_GEN >= 12
2126 pipe.TileCacheFlushEnable = bits & ANV_PIPE_TILE_CACHE_FLUSH_BIT;
2127 #endif
2128 pipe.DepthCacheFlushEnable = bits & ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
2129 pipe.DCFlushEnable = bits & ANV_PIPE_DATA_CACHE_FLUSH_BIT;
2130 pipe.RenderTargetCacheFlushEnable =
2131 bits & ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
2132
2133 /* GEN:BUG:1409600907: "PIPE_CONTROL with Depth Stall Enable bit must
2134 * be set with any PIPE_CONTROL with Depth Flush Enable bit set.
2135 */
2136 #if GEN_GEN >= 12
2137 pipe.DepthStallEnable =
2138 pipe.DepthCacheFlushEnable || (bits & ANV_PIPE_DEPTH_STALL_BIT);
2139 #else
2140 pipe.DepthStallEnable = bits & ANV_PIPE_DEPTH_STALL_BIT;
2141 #endif
2142
2143 pipe.CommandStreamerStallEnable = bits & ANV_PIPE_CS_STALL_BIT;
2144 pipe.StallAtPixelScoreboard = bits & ANV_PIPE_STALL_AT_SCOREBOARD_BIT;
2145
2146 /* From Sandybridge PRM, volume 2, "1.7.3.1 Writing a Value to Memory":
2147 *
2148 * "The most common action to perform upon reaching a
2149 * synchronization point is to write a value out to memory. An
2150 * immediate value (included with the synchronization command) may
2151 * be written."
2152 *
2153 *
2154 * From Broadwell PRM, volume 7, "End-of-Pipe Synchronization":
2155 *
2156 * "In case the data flushed out by the render engine is to be
2157 * read back in to the render engine in coherent manner, then the
2158 * render engine has to wait for the fence completion before
2159 * accessing the flushed data. This can be achieved by following
2160 * means on various products: PIPE_CONTROL command with CS Stall
2161 * and the required write caches flushed with Post-Sync-Operation
2162 * as Write Immediate Data.
2163 *
2164 * Example:
2165 * - Workload-1 (3D/GPGPU/MEDIA)
2166 * - PIPE_CONTROL (CS Stall, Post-Sync-Operation Write
2167 * Immediate Data, Required Write Cache Flush bits set)
2168 * - Workload-2 (Can use the data produce or output by
2169 * Workload-1)
2170 */
2171 if (bits & ANV_PIPE_END_OF_PIPE_SYNC_BIT) {
2172 pipe.CommandStreamerStallEnable = true;
2173 pipe.PostSyncOperation = WriteImmediateData;
2174 pipe.Address = (struct anv_address) {
2175 .bo = cmd_buffer->device->workaround_bo,
2176 .offset = 0
2177 };
2178 }
2179
2180 /*
2181 * According to the Broadwell documentation, any PIPE_CONTROL with the
2182 * "Command Streamer Stall" bit set must also have another bit set,
2183 * with five different options:
2184 *
2185 * - Render Target Cache Flush
2186 * - Depth Cache Flush
2187 * - Stall at Pixel Scoreboard
2188 * - Post-Sync Operation
2189 * - Depth Stall
2190 * - DC Flush Enable
2191 *
2192 * I chose "Stall at Pixel Scoreboard" since that's what we use in
2193 * mesa and it seems to work fine. The choice is fairly arbitrary.
2194 */
2195 if (pipe.CommandStreamerStallEnable &&
2196 !pipe.RenderTargetCacheFlushEnable &&
2197 !pipe.DepthCacheFlushEnable &&
2198 !pipe.StallAtPixelScoreboard &&
2199 !pipe.PostSyncOperation &&
2200 !pipe.DepthStallEnable &&
2201 !pipe.DCFlushEnable)
2202 pipe.StallAtPixelScoreboard = true;
2203 }
2204
2205 /* If a render target flush was emitted, then we can toggle off the bit
2206 * saying that render target writes are ongoing.
2207 */
2208 if (bits & ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT)
2209 bits &= ~(ANV_PIPE_RENDER_TARGET_BUFFER_WRITES);
2210
2211 if (GEN_IS_HASWELL) {
2212 /* Haswell needs addition work-arounds:
2213 *
2214 * From Haswell PRM, volume 2, part 1, "End-of-Pipe Synchronization":
2215 *
2216 * Option 1:
2217 * PIPE_CONTROL command with the CS Stall and the required write
2218 * caches flushed with Post-SyncOperation as Write Immediate Data
2219 * followed by eight dummy MI_STORE_DATA_IMM (write to scratch
2220 * spce) commands.
2221 *
2222 * Example:
2223 * - Workload-1
2224 * - PIPE_CONTROL (CS Stall, Post-Sync-Operation Write
2225 * Immediate Data, Required Write Cache Flush bits set)
2226 * - MI_STORE_DATA_IMM (8 times) (Dummy data, Scratch Address)
2227 * - Workload-2 (Can use the data produce or output by
2228 * Workload-1)
2229 *
2230 * Unfortunately, both the PRMs and the internal docs are a bit
2231 * out-of-date in this regard. What the windows driver does (and
2232 * this appears to actually work) is to emit a register read from the
2233 * memory address written by the pipe control above.
2234 *
2235 * What register we load into doesn't matter. We choose an indirect
2236 * rendering register because we know it always exists and it's one
2237 * of the first registers the command parser allows us to write. If
2238 * you don't have command parser support in your kernel (pre-4.2),
2239 * this will get turned into MI_NOOP and you won't get the
2240 * workaround. Unfortunately, there's just not much we can do in
2241 * that case. This register is perfectly safe to write since we
2242 * always re-load all of the indirect draw registers right before
2243 * 3DPRIMITIVE when needed anyway.
2244 */
2245 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
2246 lrm.RegisterAddress = 0x243C; /* GEN7_3DPRIM_START_INSTANCE */
2247 lrm.MemoryAddress = (struct anv_address) {
2248 .bo = cmd_buffer->device->workaround_bo,
2249 .offset = 0
2250 };
2251 }
2252 }
2253
2254 bits &= ~(ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT |
2255 ANV_PIPE_END_OF_PIPE_SYNC_BIT);
2256 }
2257
2258 if (bits & ANV_PIPE_INVALIDATE_BITS) {
2259 /* From the SKL PRM, Vol. 2a, "PIPE_CONTROL",
2260 *
2261 * "If the VF Cache Invalidation Enable is set to a 1 in a
2262 * PIPE_CONTROL, a separate Null PIPE_CONTROL, all bitfields sets to
2263 * 0, with the VF Cache Invalidation Enable set to 0 needs to be sent
2264 * prior to the PIPE_CONTROL with VF Cache Invalidation Enable set to
2265 * a 1."
2266 *
2267 * This appears to hang Broadwell, so we restrict it to just gen9.
2268 */
2269 if (GEN_GEN == 9 && (bits & ANV_PIPE_VF_CACHE_INVALIDATE_BIT))
2270 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe);
2271
2272 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
2273 pipe.StateCacheInvalidationEnable =
2274 bits & ANV_PIPE_STATE_CACHE_INVALIDATE_BIT;
2275 pipe.ConstantCacheInvalidationEnable =
2276 bits & ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT;
2277 pipe.VFCacheInvalidationEnable =
2278 bits & ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
2279 pipe.TextureCacheInvalidationEnable =
2280 bits & ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
2281 pipe.InstructionCacheInvalidateEnable =
2282 bits & ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT;
2283
2284 /* From the SKL PRM, Vol. 2a, "PIPE_CONTROL",
2285 *
2286 * "When VF Cache Invalidate is set “Post Sync Operation” must be
2287 * enabled to “Write Immediate Data” or “Write PS Depth Count” or
2288 * “Write Timestamp”.
2289 */
2290 if (GEN_GEN == 9 && pipe.VFCacheInvalidationEnable) {
2291 pipe.PostSyncOperation = WriteImmediateData;
2292 pipe.Address =
2293 (struct anv_address) { cmd_buffer->device->workaround_bo, 0 };
2294 }
2295 }
2296
2297 #if GEN_GEN == 12
2298 if ((bits & ANV_PIPE_AUX_TABLE_INVALIDATE_BIT) &&
2299 cmd_buffer->device->info.has_aux_map) {
2300 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
2301 lri.RegisterOffset = GENX(GFX_CCS_AUX_INV_num);
2302 lri.DataDWord = 1;
2303 }
2304 }
2305 #endif
2306
2307 bits &= ~ANV_PIPE_INVALIDATE_BITS;
2308 }
2309
2310 cmd_buffer->state.pending_pipe_bits = bits;
2311 }
2312
2313 void genX(CmdPipelineBarrier)(
2314 VkCommandBuffer commandBuffer,
2315 VkPipelineStageFlags srcStageMask,
2316 VkPipelineStageFlags destStageMask,
2317 VkBool32 byRegion,
2318 uint32_t memoryBarrierCount,
2319 const VkMemoryBarrier* pMemoryBarriers,
2320 uint32_t bufferMemoryBarrierCount,
2321 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
2322 uint32_t imageMemoryBarrierCount,
2323 const VkImageMemoryBarrier* pImageMemoryBarriers)
2324 {
2325 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2326
2327 /* XXX: Right now, we're really dumb and just flush whatever categories
2328 * the app asks for. One of these days we may make this a bit better
2329 * but right now that's all the hardware allows for in most areas.
2330 */
2331 VkAccessFlags src_flags = 0;
2332 VkAccessFlags dst_flags = 0;
2333
2334 for (uint32_t i = 0; i < memoryBarrierCount; i++) {
2335 src_flags |= pMemoryBarriers[i].srcAccessMask;
2336 dst_flags |= pMemoryBarriers[i].dstAccessMask;
2337 }
2338
2339 for (uint32_t i = 0; i < bufferMemoryBarrierCount; i++) {
2340 src_flags |= pBufferMemoryBarriers[i].srcAccessMask;
2341 dst_flags |= pBufferMemoryBarriers[i].dstAccessMask;
2342 }
2343
2344 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
2345 src_flags |= pImageMemoryBarriers[i].srcAccessMask;
2346 dst_flags |= pImageMemoryBarriers[i].dstAccessMask;
2347 ANV_FROM_HANDLE(anv_image, image, pImageMemoryBarriers[i].image);
2348 const VkImageSubresourceRange *range =
2349 &pImageMemoryBarriers[i].subresourceRange;
2350
2351 uint32_t base_layer, layer_count;
2352 if (image->type == VK_IMAGE_TYPE_3D) {
2353 base_layer = 0;
2354 layer_count = anv_minify(image->extent.depth, range->baseMipLevel);
2355 } else {
2356 base_layer = range->baseArrayLayer;
2357 layer_count = anv_get_layerCount(image, range);
2358 }
2359
2360 if (range->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
2361 transition_depth_buffer(cmd_buffer, image,
2362 base_layer, layer_count,
2363 pImageMemoryBarriers[i].oldLayout,
2364 pImageMemoryBarriers[i].newLayout);
2365 }
2366
2367 if (range->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
2368 transition_stencil_buffer(cmd_buffer, image,
2369 range->baseMipLevel,
2370 anv_get_levelCount(image, range),
2371 base_layer, layer_count,
2372 pImageMemoryBarriers[i].oldLayout,
2373 pImageMemoryBarriers[i].newLayout);
2374 }
2375
2376 if (range->aspectMask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
2377 VkImageAspectFlags color_aspects =
2378 anv_image_expand_aspects(image, range->aspectMask);
2379 uint32_t aspect_bit;
2380 anv_foreach_image_aspect_bit(aspect_bit, image, color_aspects) {
2381 transition_color_buffer(cmd_buffer, image, 1UL << aspect_bit,
2382 range->baseMipLevel,
2383 anv_get_levelCount(image, range),
2384 base_layer, layer_count,
2385 pImageMemoryBarriers[i].oldLayout,
2386 pImageMemoryBarriers[i].newLayout);
2387 }
2388 }
2389 }
2390
2391 cmd_buffer->state.pending_pipe_bits |=
2392 anv_pipe_flush_bits_for_access_flags(src_flags) |
2393 anv_pipe_invalidate_bits_for_access_flags(dst_flags);
2394 }
2395
2396 static void
2397 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer *cmd_buffer)
2398 {
2399 VkShaderStageFlags stages =
2400 cmd_buffer->state.gfx.pipeline->active_stages;
2401
2402 /* In order to avoid thrash, we assume that vertex and fragment stages
2403 * always exist. In the rare case where one is missing *and* the other
2404 * uses push concstants, this may be suboptimal. However, avoiding stalls
2405 * seems more important.
2406 */
2407 stages |= VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT;
2408
2409 if (stages == cmd_buffer->state.push_constant_stages)
2410 return;
2411
2412 #if GEN_GEN >= 8
2413 const unsigned push_constant_kb = 32;
2414 #elif GEN_IS_HASWELL
2415 const unsigned push_constant_kb = cmd_buffer->device->info.gt == 3 ? 32 : 16;
2416 #else
2417 const unsigned push_constant_kb = 16;
2418 #endif
2419
2420 const unsigned num_stages =
2421 util_bitcount(stages & VK_SHADER_STAGE_ALL_GRAPHICS);
2422 unsigned size_per_stage = push_constant_kb / num_stages;
2423
2424 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
2425 * units of 2KB. Incidentally, these are the same platforms that have
2426 * 32KB worth of push constant space.
2427 */
2428 if (push_constant_kb == 32)
2429 size_per_stage &= ~1u;
2430
2431 uint32_t kb_used = 0;
2432 for (int i = MESA_SHADER_VERTEX; i < MESA_SHADER_FRAGMENT; i++) {
2433 unsigned push_size = (stages & (1 << i)) ? size_per_stage : 0;
2434 anv_batch_emit(&cmd_buffer->batch,
2435 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
2436 alloc._3DCommandSubOpcode = 18 + i;
2437 alloc.ConstantBufferOffset = (push_size > 0) ? kb_used : 0;
2438 alloc.ConstantBufferSize = push_size;
2439 }
2440 kb_used += push_size;
2441 }
2442
2443 anv_batch_emit(&cmd_buffer->batch,
2444 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS), alloc) {
2445 alloc.ConstantBufferOffset = kb_used;
2446 alloc.ConstantBufferSize = push_constant_kb - kb_used;
2447 }
2448
2449 cmd_buffer->state.push_constant_stages = stages;
2450
2451 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
2452 *
2453 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
2454 * the next 3DPRIMITIVE command after programming the
2455 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
2456 *
2457 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
2458 * pipeline setup, we need to dirty push constants.
2459 */
2460 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_ALL_GRAPHICS;
2461 }
2462
2463 static struct anv_address
2464 anv_descriptor_set_address(struct anv_cmd_buffer *cmd_buffer,
2465 struct anv_descriptor_set *set)
2466 {
2467 if (set->pool) {
2468 /* This is a normal descriptor set */
2469 return (struct anv_address) {
2470 .bo = set->pool->bo,
2471 .offset = set->desc_mem.offset,
2472 };
2473 } else {
2474 /* This is a push descriptor set. We have to flag it as used on the GPU
2475 * so that the next time we push descriptors, we grab a new memory.
2476 */
2477 struct anv_push_descriptor_set *push_set =
2478 (struct anv_push_descriptor_set *)set;
2479 push_set->set_used_on_gpu = true;
2480
2481 return (struct anv_address) {
2482 .bo = cmd_buffer->dynamic_state_stream.state_pool->block_pool.bo,
2483 .offset = set->desc_mem.offset,
2484 };
2485 }
2486 }
2487
2488 static VkResult
2489 emit_binding_table(struct anv_cmd_buffer *cmd_buffer,
2490 struct anv_cmd_pipeline_state *pipe_state,
2491 struct anv_shader_bin *shader,
2492 struct anv_state *bt_state)
2493 {
2494 struct anv_subpass *subpass = cmd_buffer->state.subpass;
2495 uint32_t state_offset;
2496
2497 struct anv_pipeline_bind_map *map = &shader->bind_map;
2498 if (map->surface_count == 0) {
2499 *bt_state = (struct anv_state) { 0, };
2500 return VK_SUCCESS;
2501 }
2502
2503 *bt_state = anv_cmd_buffer_alloc_binding_table(cmd_buffer,
2504 map->surface_count,
2505 &state_offset);
2506 uint32_t *bt_map = bt_state->map;
2507
2508 if (bt_state->map == NULL)
2509 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2510
2511 /* We only need to emit relocs if we're not using softpin. If we are using
2512 * softpin then we always keep all user-allocated memory objects resident.
2513 */
2514 const bool need_client_mem_relocs =
2515 !cmd_buffer->device->physical->use_softpin;
2516
2517 for (uint32_t s = 0; s < map->surface_count; s++) {
2518 struct anv_pipeline_binding *binding = &map->surface_to_descriptor[s];
2519
2520 struct anv_state surface_state;
2521
2522 switch (binding->set) {
2523 case ANV_DESCRIPTOR_SET_NULL:
2524 bt_map[s] = 0;
2525 break;
2526
2527 case ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS:
2528 /* Color attachment binding */
2529 assert(shader->stage == MESA_SHADER_FRAGMENT);
2530 if (binding->index < subpass->color_count) {
2531 const unsigned att =
2532 subpass->color_attachments[binding->index].attachment;
2533
2534 /* From the Vulkan 1.0.46 spec:
2535 *
2536 * "If any color or depth/stencil attachments are
2537 * VK_ATTACHMENT_UNUSED, then no writes occur for those
2538 * attachments."
2539 */
2540 if (att == VK_ATTACHMENT_UNUSED) {
2541 surface_state = cmd_buffer->state.null_surface_state;
2542 } else {
2543 surface_state = cmd_buffer->state.attachments[att].color.state;
2544 }
2545 } else {
2546 surface_state = cmd_buffer->state.null_surface_state;
2547 }
2548
2549 bt_map[s] = surface_state.offset + state_offset;
2550 break;
2551
2552 case ANV_DESCRIPTOR_SET_SHADER_CONSTANTS: {
2553 struct anv_state surface_state =
2554 anv_cmd_buffer_alloc_surface_state(cmd_buffer);
2555
2556 struct anv_address constant_data = {
2557 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2558 .offset = shader->constant_data.offset,
2559 };
2560 unsigned constant_data_size = shader->constant_data_size;
2561
2562 const enum isl_format format =
2563 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
2564 anv_fill_buffer_surface_state(cmd_buffer->device,
2565 surface_state, format,
2566 constant_data, constant_data_size, 1);
2567
2568 bt_map[s] = surface_state.offset + state_offset;
2569 add_surface_reloc(cmd_buffer, surface_state, constant_data);
2570 break;
2571 }
2572
2573 case ANV_DESCRIPTOR_SET_NUM_WORK_GROUPS: {
2574 /* This is always the first binding for compute shaders */
2575 assert(shader->stage == MESA_SHADER_COMPUTE && s == 0);
2576
2577 struct anv_state surface_state =
2578 anv_cmd_buffer_alloc_surface_state(cmd_buffer);
2579
2580 const enum isl_format format =
2581 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
2582 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
2583 format,
2584 cmd_buffer->state.compute.num_workgroups,
2585 12, 1);
2586 bt_map[s] = surface_state.offset + state_offset;
2587 if (need_client_mem_relocs) {
2588 add_surface_reloc(cmd_buffer, surface_state,
2589 cmd_buffer->state.compute.num_workgroups);
2590 }
2591 break;
2592 }
2593
2594 case ANV_DESCRIPTOR_SET_DESCRIPTORS: {
2595 /* This is a descriptor set buffer so the set index is actually
2596 * given by binding->binding. (Yes, that's confusing.)
2597 */
2598 struct anv_descriptor_set *set =
2599 pipe_state->descriptors[binding->index];
2600 assert(set->desc_mem.alloc_size);
2601 assert(set->desc_surface_state.alloc_size);
2602 bt_map[s] = set->desc_surface_state.offset + state_offset;
2603 add_surface_reloc(cmd_buffer, set->desc_surface_state,
2604 anv_descriptor_set_address(cmd_buffer, set));
2605 break;
2606 }
2607
2608 default: {
2609 assert(binding->set < MAX_SETS);
2610 const struct anv_descriptor *desc =
2611 &pipe_state->descriptors[binding->set]->descriptors[binding->index];
2612
2613 switch (desc->type) {
2614 case VK_DESCRIPTOR_TYPE_SAMPLER:
2615 /* Nothing for us to do here */
2616 continue;
2617
2618 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
2619 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: {
2620 struct anv_surface_state sstate =
2621 (desc->layout == VK_IMAGE_LAYOUT_GENERAL) ?
2622 desc->image_view->planes[binding->plane].general_sampler_surface_state :
2623 desc->image_view->planes[binding->plane].optimal_sampler_surface_state;
2624 surface_state = sstate.state;
2625 assert(surface_state.alloc_size);
2626 if (need_client_mem_relocs)
2627 add_surface_state_relocs(cmd_buffer, sstate);
2628 break;
2629 }
2630 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2631 assert(shader->stage == MESA_SHADER_FRAGMENT);
2632 if ((desc->image_view->aspect_mask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0) {
2633 /* For depth and stencil input attachments, we treat it like any
2634 * old texture that a user may have bound.
2635 */
2636 assert(desc->image_view->n_planes == 1);
2637 struct anv_surface_state sstate =
2638 (desc->layout == VK_IMAGE_LAYOUT_GENERAL) ?
2639 desc->image_view->planes[0].general_sampler_surface_state :
2640 desc->image_view->planes[0].optimal_sampler_surface_state;
2641 surface_state = sstate.state;
2642 assert(surface_state.alloc_size);
2643 if (need_client_mem_relocs)
2644 add_surface_state_relocs(cmd_buffer, sstate);
2645 } else {
2646 /* For color input attachments, we create the surface state at
2647 * vkBeginRenderPass time so that we can include aux and clear
2648 * color information.
2649 */
2650 assert(binding->input_attachment_index < subpass->input_count);
2651 const unsigned subpass_att = binding->input_attachment_index;
2652 const unsigned att = subpass->input_attachments[subpass_att].attachment;
2653 surface_state = cmd_buffer->state.attachments[att].input.state;
2654 }
2655 break;
2656
2657 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
2658 struct anv_surface_state sstate = (binding->write_only)
2659 ? desc->image_view->planes[binding->plane].writeonly_storage_surface_state
2660 : desc->image_view->planes[binding->plane].storage_surface_state;
2661 surface_state = sstate.state;
2662 assert(surface_state.alloc_size);
2663 if (need_client_mem_relocs)
2664 add_surface_state_relocs(cmd_buffer, sstate);
2665 break;
2666 }
2667
2668 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2669 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2670 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2671 surface_state = desc->buffer_view->surface_state;
2672 assert(surface_state.alloc_size);
2673 if (need_client_mem_relocs) {
2674 add_surface_reloc(cmd_buffer, surface_state,
2675 desc->buffer_view->address);
2676 }
2677 break;
2678
2679 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2680 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2681 /* Compute the offset within the buffer */
2682 struct anv_push_constants *push =
2683 &cmd_buffer->state.push_constants[shader->stage];
2684
2685 uint32_t dynamic_offset =
2686 push->dynamic_offsets[binding->dynamic_offset_index];
2687 uint64_t offset = desc->offset + dynamic_offset;
2688 /* Clamp to the buffer size */
2689 offset = MIN2(offset, desc->buffer->size);
2690 /* Clamp the range to the buffer size */
2691 uint32_t range = MIN2(desc->range, desc->buffer->size - offset);
2692
2693 /* Align the range for consistency */
2694 if (desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC)
2695 range = align_u32(range, ANV_UBO_BOUNDS_CHECK_ALIGNMENT);
2696
2697 struct anv_address address =
2698 anv_address_add(desc->buffer->address, offset);
2699
2700 surface_state =
2701 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
2702 enum isl_format format =
2703 anv_isl_format_for_descriptor_type(desc->type);
2704
2705 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
2706 format, address, range, 1);
2707 if (need_client_mem_relocs)
2708 add_surface_reloc(cmd_buffer, surface_state, address);
2709 break;
2710 }
2711
2712 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2713 surface_state = (binding->write_only)
2714 ? desc->buffer_view->writeonly_storage_surface_state
2715 : desc->buffer_view->storage_surface_state;
2716 assert(surface_state.alloc_size);
2717 if (need_client_mem_relocs) {
2718 add_surface_reloc(cmd_buffer, surface_state,
2719 desc->buffer_view->address);
2720 }
2721 break;
2722
2723 default:
2724 assert(!"Invalid descriptor type");
2725 continue;
2726 }
2727 bt_map[s] = surface_state.offset + state_offset;
2728 break;
2729 }
2730 }
2731 }
2732
2733 return VK_SUCCESS;
2734 }
2735
2736 static VkResult
2737 emit_samplers(struct anv_cmd_buffer *cmd_buffer,
2738 struct anv_cmd_pipeline_state *pipe_state,
2739 struct anv_shader_bin *shader,
2740 struct anv_state *state)
2741 {
2742 struct anv_pipeline_bind_map *map = &shader->bind_map;
2743 if (map->sampler_count == 0) {
2744 *state = (struct anv_state) { 0, };
2745 return VK_SUCCESS;
2746 }
2747
2748 uint32_t size = map->sampler_count * 16;
2749 *state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, 32);
2750
2751 if (state->map == NULL)
2752 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2753
2754 for (uint32_t s = 0; s < map->sampler_count; s++) {
2755 struct anv_pipeline_binding *binding = &map->sampler_to_descriptor[s];
2756 const struct anv_descriptor *desc =
2757 &pipe_state->descriptors[binding->set]->descriptors[binding->index];
2758
2759 if (desc->type != VK_DESCRIPTOR_TYPE_SAMPLER &&
2760 desc->type != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
2761 continue;
2762
2763 struct anv_sampler *sampler = desc->sampler;
2764
2765 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
2766 * happens to be zero.
2767 */
2768 if (sampler == NULL)
2769 continue;
2770
2771 memcpy(state->map + (s * 16),
2772 sampler->state[binding->plane], sizeof(sampler->state[0]));
2773 }
2774
2775 return VK_SUCCESS;
2776 }
2777
2778 static uint32_t
2779 flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer,
2780 struct anv_cmd_pipeline_state *pipe_state,
2781 struct anv_shader_bin **shaders,
2782 uint32_t num_shaders)
2783 {
2784 const VkShaderStageFlags dirty = cmd_buffer->state.descriptors_dirty;
2785 VkShaderStageFlags flushed = 0;
2786
2787 VkResult result = VK_SUCCESS;
2788 for (uint32_t i = 0; i < num_shaders; i++) {
2789 if (!shaders[i])
2790 continue;
2791
2792 gl_shader_stage stage = shaders[i]->stage;
2793 VkShaderStageFlags vk_stage = mesa_to_vk_shader_stage(stage);
2794 if ((vk_stage & dirty) == 0)
2795 continue;
2796
2797 result = emit_samplers(cmd_buffer, pipe_state, shaders[i],
2798 &cmd_buffer->state.samplers[stage]);
2799 if (result != VK_SUCCESS)
2800 break;
2801 result = emit_binding_table(cmd_buffer, pipe_state, shaders[i],
2802 &cmd_buffer->state.binding_tables[stage]);
2803 if (result != VK_SUCCESS)
2804 break;
2805
2806 flushed |= vk_stage;
2807 }
2808
2809 if (result != VK_SUCCESS) {
2810 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
2811
2812 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
2813 if (result != VK_SUCCESS)
2814 return 0;
2815
2816 /* Re-emit state base addresses so we get the new surface state base
2817 * address before we start emitting binding tables etc.
2818 */
2819 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
2820
2821 /* Re-emit all active binding tables */
2822 flushed = 0;
2823
2824 for (uint32_t i = 0; i < num_shaders; i++) {
2825 if (!shaders[i])
2826 continue;
2827
2828 gl_shader_stage stage = shaders[i]->stage;
2829
2830 result = emit_samplers(cmd_buffer, pipe_state, shaders[i],
2831 &cmd_buffer->state.samplers[stage]);
2832 if (result != VK_SUCCESS) {
2833 anv_batch_set_error(&cmd_buffer->batch, result);
2834 return 0;
2835 }
2836 result = emit_binding_table(cmd_buffer, pipe_state, shaders[i],
2837 &cmd_buffer->state.binding_tables[stage]);
2838 if (result != VK_SUCCESS) {
2839 anv_batch_set_error(&cmd_buffer->batch, result);
2840 return 0;
2841 }
2842
2843 flushed |= mesa_to_vk_shader_stage(stage);
2844 }
2845 }
2846
2847 cmd_buffer->state.descriptors_dirty &= ~flushed;
2848
2849 return flushed;
2850 }
2851
2852 static void
2853 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer *cmd_buffer,
2854 uint32_t stages)
2855 {
2856 static const uint32_t sampler_state_opcodes[] = {
2857 [MESA_SHADER_VERTEX] = 43,
2858 [MESA_SHADER_TESS_CTRL] = 44, /* HS */
2859 [MESA_SHADER_TESS_EVAL] = 45, /* DS */
2860 [MESA_SHADER_GEOMETRY] = 46,
2861 [MESA_SHADER_FRAGMENT] = 47,
2862 [MESA_SHADER_COMPUTE] = 0,
2863 };
2864
2865 static const uint32_t binding_table_opcodes[] = {
2866 [MESA_SHADER_VERTEX] = 38,
2867 [MESA_SHADER_TESS_CTRL] = 39,
2868 [MESA_SHADER_TESS_EVAL] = 40,
2869 [MESA_SHADER_GEOMETRY] = 41,
2870 [MESA_SHADER_FRAGMENT] = 42,
2871 [MESA_SHADER_COMPUTE] = 0,
2872 };
2873
2874 anv_foreach_stage(s, stages) {
2875 assert(s < ARRAY_SIZE(binding_table_opcodes));
2876 assert(binding_table_opcodes[s] > 0);
2877
2878 if (cmd_buffer->state.samplers[s].alloc_size > 0) {
2879 anv_batch_emit(&cmd_buffer->batch,
2880 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ssp) {
2881 ssp._3DCommandSubOpcode = sampler_state_opcodes[s];
2882 ssp.PointertoVSSamplerState = cmd_buffer->state.samplers[s].offset;
2883 }
2884 }
2885
2886 /* Always emit binding table pointers if we're asked to, since on SKL
2887 * this is what flushes push constants. */
2888 anv_batch_emit(&cmd_buffer->batch,
2889 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), btp) {
2890 btp._3DCommandSubOpcode = binding_table_opcodes[s];
2891 btp.PointertoVSBindingTable = cmd_buffer->state.binding_tables[s].offset;
2892 }
2893 }
2894 }
2895
2896 static struct anv_address
2897 get_push_range_address(struct anv_cmd_buffer *cmd_buffer,
2898 gl_shader_stage stage,
2899 const struct anv_push_range *range)
2900 {
2901 const struct anv_cmd_graphics_state *gfx_state = &cmd_buffer->state.gfx;
2902 switch (range->set) {
2903 case ANV_DESCRIPTOR_SET_DESCRIPTORS: {
2904 /* This is a descriptor set buffer so the set index is
2905 * actually given by binding->binding. (Yes, that's
2906 * confusing.)
2907 */
2908 struct anv_descriptor_set *set =
2909 gfx_state->base.descriptors[range->index];
2910 return anv_descriptor_set_address(cmd_buffer, set);
2911 }
2912
2913 case ANV_DESCRIPTOR_SET_PUSH_CONSTANTS: {
2914 struct anv_state state =
2915 anv_cmd_buffer_push_constants(cmd_buffer, stage);
2916 return (struct anv_address) {
2917 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2918 .offset = state.offset,
2919 };
2920 }
2921
2922 default: {
2923 assert(range->set < MAX_SETS);
2924 struct anv_descriptor_set *set =
2925 gfx_state->base.descriptors[range->set];
2926 const struct anv_descriptor *desc =
2927 &set->descriptors[range->index];
2928
2929 if (desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) {
2930 return desc->buffer_view->address;
2931 } else {
2932 assert(desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
2933 struct anv_push_constants *push =
2934 &cmd_buffer->state.push_constants[stage];
2935 uint32_t dynamic_offset =
2936 push->dynamic_offsets[range->dynamic_offset_index];
2937 return anv_address_add(desc->buffer->address,
2938 desc->offset + dynamic_offset);
2939 }
2940 }
2941 }
2942 }
2943
2944
2945 /** Returns the size in bytes of the bound buffer
2946 *
2947 * The range is relative to the start of the buffer, not the start of the
2948 * range. The returned range may be smaller than
2949 *
2950 * (range->start + range->length) * 32;
2951 */
2952 static uint32_t
2953 get_push_range_bound_size(struct anv_cmd_buffer *cmd_buffer,
2954 gl_shader_stage stage,
2955 const struct anv_push_range *range)
2956 {
2957 assert(stage != MESA_SHADER_COMPUTE);
2958 const struct anv_cmd_graphics_state *gfx_state = &cmd_buffer->state.gfx;
2959 switch (range->set) {
2960 case ANV_DESCRIPTOR_SET_DESCRIPTORS: {
2961 struct anv_descriptor_set *set =
2962 gfx_state->base.descriptors[range->index];
2963 assert(range->start * 32 < set->desc_mem.alloc_size);
2964 assert((range->start + range->length) * 32 <= set->desc_mem.alloc_size);
2965 return set->desc_mem.alloc_size;
2966 }
2967
2968 case ANV_DESCRIPTOR_SET_PUSH_CONSTANTS:
2969 return (range->start + range->length) * 32;
2970
2971 default: {
2972 assert(range->set < MAX_SETS);
2973 struct anv_descriptor_set *set =
2974 gfx_state->base.descriptors[range->set];
2975 const struct anv_descriptor *desc =
2976 &set->descriptors[range->index];
2977
2978 if (desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) {
2979 return desc->buffer_view->range;
2980 } else {
2981 assert(desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
2982 /* Compute the offset within the buffer */
2983 struct anv_push_constants *push =
2984 &cmd_buffer->state.push_constants[stage];
2985 uint32_t dynamic_offset =
2986 push->dynamic_offsets[range->dynamic_offset_index];
2987 uint64_t offset = desc->offset + dynamic_offset;
2988 /* Clamp to the buffer size */
2989 offset = MIN2(offset, desc->buffer->size);
2990 /* Clamp the range to the buffer size */
2991 uint32_t bound_range = MIN2(desc->range, desc->buffer->size - offset);
2992
2993 /* Align the range for consistency */
2994 bound_range = align_u32(bound_range, ANV_UBO_BOUNDS_CHECK_ALIGNMENT);
2995
2996 return bound_range;
2997 }
2998 }
2999 }
3000 }
3001
3002 static void
3003 cmd_buffer_emit_push_constant(struct anv_cmd_buffer *cmd_buffer,
3004 gl_shader_stage stage,
3005 struct anv_address *buffers,
3006 unsigned buffer_count)
3007 {
3008 const struct anv_cmd_graphics_state *gfx_state = &cmd_buffer->state.gfx;
3009 const struct anv_graphics_pipeline *pipeline = gfx_state->pipeline;
3010
3011 static const uint32_t push_constant_opcodes[] = {
3012 [MESA_SHADER_VERTEX] = 21,
3013 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
3014 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
3015 [MESA_SHADER_GEOMETRY] = 22,
3016 [MESA_SHADER_FRAGMENT] = 23,
3017 [MESA_SHADER_COMPUTE] = 0,
3018 };
3019
3020 assert(stage < ARRAY_SIZE(push_constant_opcodes));
3021 assert(push_constant_opcodes[stage] > 0);
3022
3023 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CONSTANT_VS), c) {
3024 c._3DCommandSubOpcode = push_constant_opcodes[stage];
3025
3026 if (anv_pipeline_has_stage(pipeline, stage)) {
3027 const struct anv_pipeline_bind_map *bind_map =
3028 &pipeline->shaders[stage]->bind_map;
3029
3030 #if GEN_GEN >= 12
3031 c.MOCS = cmd_buffer->device->isl_dev.mocs.internal;
3032 #endif
3033
3034 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3035 /* The Skylake PRM contains the following restriction:
3036 *
3037 * "The driver must ensure The following case does not occur
3038 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
3039 * buffer 3 read length equal to zero committed followed by a
3040 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
3041 * zero committed."
3042 *
3043 * To avoid this, we program the buffers in the highest slots.
3044 * This way, slot 0 is only used if slot 3 is also used.
3045 */
3046 assert(buffer_count <= 4);
3047 const unsigned shift = 4 - buffer_count;
3048 for (unsigned i = 0; i < buffer_count; i++) {
3049 const struct anv_push_range *range = &bind_map->push_ranges[i];
3050
3051 /* At this point we only have non-empty ranges */
3052 assert(range->length > 0);
3053
3054 /* For Ivy Bridge, make sure we only set the first range (actual
3055 * push constants)
3056 */
3057 assert((GEN_GEN >= 8 || GEN_IS_HASWELL) || i == 0);
3058
3059 c.ConstantBody.ReadLength[i + shift] = range->length;
3060 c.ConstantBody.Buffer[i + shift] =
3061 anv_address_add(buffers[i], range->start * 32);
3062 }
3063 #else
3064 /* For Ivy Bridge, push constants are relative to dynamic state
3065 * base address and we only ever push actual push constants.
3066 */
3067 if (bind_map->push_ranges[0].length > 0) {
3068 assert(buffer_count == 1);
3069 assert(bind_map->push_ranges[0].set ==
3070 ANV_DESCRIPTOR_SET_PUSH_CONSTANTS);
3071 assert(buffers[0].bo ==
3072 cmd_buffer->device->dynamic_state_pool.block_pool.bo);
3073 c.ConstantBody.ReadLength[0] = bind_map->push_ranges[0].length;
3074 c.ConstantBody.Buffer[0].bo = NULL;
3075 c.ConstantBody.Buffer[0].offset = buffers[0].offset;
3076 }
3077 assert(bind_map->push_ranges[1].length == 0);
3078 assert(bind_map->push_ranges[2].length == 0);
3079 assert(bind_map->push_ranges[3].length == 0);
3080 #endif
3081 }
3082 }
3083 }
3084
3085 #if GEN_GEN >= 12
3086 static void
3087 cmd_buffer_emit_push_constant_all(struct anv_cmd_buffer *cmd_buffer,
3088 uint32_t shader_mask,
3089 struct anv_address *buffers,
3090 uint32_t buffer_count)
3091 {
3092 if (buffer_count == 0) {
3093 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CONSTANT_ALL), c) {
3094 c.ShaderUpdateEnable = shader_mask;
3095 c.MOCS = cmd_buffer->device->isl_dev.mocs.internal;
3096 }
3097 return;
3098 }
3099
3100 const struct anv_cmd_graphics_state *gfx_state = &cmd_buffer->state.gfx;
3101 const struct anv_graphics_pipeline *pipeline = gfx_state->pipeline;
3102
3103 static const uint32_t push_constant_opcodes[] = {
3104 [MESA_SHADER_VERTEX] = 21,
3105 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
3106 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
3107 [MESA_SHADER_GEOMETRY] = 22,
3108 [MESA_SHADER_FRAGMENT] = 23,
3109 [MESA_SHADER_COMPUTE] = 0,
3110 };
3111
3112 gl_shader_stage stage = vk_to_mesa_shader_stage(shader_mask);
3113 assert(stage < ARRAY_SIZE(push_constant_opcodes));
3114 assert(push_constant_opcodes[stage] > 0);
3115
3116 const struct anv_pipeline_bind_map *bind_map =
3117 &pipeline->shaders[stage]->bind_map;
3118
3119 uint32_t *dw;
3120 const uint32_t buffer_mask = (1 << buffer_count) - 1;
3121 const uint32_t num_dwords = 2 + 2 * buffer_count;
3122
3123 dw = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
3124 GENX(3DSTATE_CONSTANT_ALL),
3125 .ShaderUpdateEnable = shader_mask,
3126 .PointerBufferMask = buffer_mask,
3127 .MOCS = cmd_buffer->device->isl_dev.mocs.internal);
3128
3129 for (int i = 0; i < buffer_count; i++) {
3130 const struct anv_push_range *range = &bind_map->push_ranges[i];
3131 GENX(3DSTATE_CONSTANT_ALL_DATA_pack)(
3132 &cmd_buffer->batch, dw + 2 + i * 2,
3133 &(struct GENX(3DSTATE_CONSTANT_ALL_DATA)) {
3134 .PointerToConstantBuffer =
3135 anv_address_add(buffers[i], range->start * 32),
3136 .ConstantBufferReadLength = range->length,
3137 });
3138 }
3139 }
3140 #endif
3141
3142 static void
3143 cmd_buffer_flush_push_constants(struct anv_cmd_buffer *cmd_buffer,
3144 VkShaderStageFlags dirty_stages)
3145 {
3146 VkShaderStageFlags flushed = 0;
3147 const struct anv_cmd_graphics_state *gfx_state = &cmd_buffer->state.gfx;
3148 const struct anv_graphics_pipeline *pipeline = gfx_state->pipeline;
3149
3150 #if GEN_GEN >= 12
3151 uint32_t nobuffer_stages = 0;
3152 #endif
3153
3154 anv_foreach_stage(stage, dirty_stages) {
3155 unsigned buffer_count = 0;
3156 flushed |= mesa_to_vk_shader_stage(stage);
3157 UNUSED uint32_t max_push_range = 0;
3158
3159 struct anv_address buffers[4] = {};
3160 if (anv_pipeline_has_stage(pipeline, stage)) {
3161 const struct anv_pipeline_bind_map *bind_map =
3162 &pipeline->shaders[stage]->bind_map;
3163 struct anv_push_constants *push =
3164 &cmd_buffer->state.push_constants[stage];
3165
3166 if (cmd_buffer->device->robust_buffer_access) {
3167 push->push_reg_mask = 0;
3168 /* Start of the current range in the shader, relative to the start
3169 * of push constants in the shader.
3170 */
3171 unsigned range_start_reg = 0;
3172 for (unsigned i = 0; i < 4; i++) {
3173 const struct anv_push_range *range = &bind_map->push_ranges[i];
3174 if (range->length == 0)
3175 continue;
3176
3177 unsigned bound_size =
3178 get_push_range_bound_size(cmd_buffer, stage, range);
3179 if (bound_size >= range->start * 32) {
3180 unsigned bound_regs =
3181 MIN2(DIV_ROUND_UP(bound_size, 32) - range->start,
3182 range->length);
3183 assert(range_start_reg + bound_regs <= 64);
3184 push->push_reg_mask |= BITFIELD64_RANGE(range_start_reg,
3185 bound_regs);
3186 }
3187
3188 cmd_buffer->state.push_constants_dirty |=
3189 mesa_to_vk_shader_stage(stage);
3190
3191 range_start_reg += range->length;
3192 }
3193 }
3194
3195 /* We have to gather buffer addresses as a second step because the
3196 * loop above puts data into the push constant area and the call to
3197 * get_push_range_address is what locks our push constants and copies
3198 * them into the actual GPU buffer. If we did the two loops at the
3199 * same time, we'd risk only having some of the sizes in the push
3200 * constant buffer when we did the copy.
3201 */
3202 for (unsigned i = 0; i < 4; i++) {
3203 const struct anv_push_range *range = &bind_map->push_ranges[i];
3204 if (range->length == 0)
3205 break;
3206
3207 buffers[i] = get_push_range_address(cmd_buffer, stage, range);
3208 max_push_range = MAX2(max_push_range, range->length);
3209 buffer_count++;
3210 }
3211
3212 /* We have at most 4 buffers but they should be tightly packed */
3213 for (unsigned i = buffer_count; i < 4; i++)
3214 assert(bind_map->push_ranges[i].length == 0);
3215 }
3216
3217 #if GEN_GEN >= 12
3218 /* If this stage doesn't have any push constants, emit it later in a
3219 * single CONSTANT_ALL packet.
3220 */
3221 if (buffer_count == 0) {
3222 nobuffer_stages |= 1 << stage;
3223 continue;
3224 }
3225
3226 /* The Constant Buffer Read Length field from 3DSTATE_CONSTANT_ALL
3227 * contains only 5 bits, so we can only use it for buffers smaller than
3228 * 32.
3229 */
3230 if (max_push_range < 32) {
3231 cmd_buffer_emit_push_constant_all(cmd_buffer, 1 << stage,
3232 buffers, buffer_count);
3233 continue;
3234 }
3235 #endif
3236
3237 cmd_buffer_emit_push_constant(cmd_buffer, stage, buffers, buffer_count);
3238 }
3239
3240 #if GEN_GEN >= 12
3241 if (nobuffer_stages)
3242 cmd_buffer_emit_push_constant_all(cmd_buffer, nobuffer_stages, NULL, 0);
3243 #endif
3244
3245 cmd_buffer->state.push_constants_dirty &= ~flushed;
3246 }
3247
3248 void
3249 genX(cmd_buffer_flush_state)(struct anv_cmd_buffer *cmd_buffer)
3250 {
3251 struct anv_graphics_pipeline *pipeline = cmd_buffer->state.gfx.pipeline;
3252 uint32_t *p;
3253
3254 assert((pipeline->active_stages & VK_SHADER_STAGE_COMPUTE_BIT) == 0);
3255
3256 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->base.l3_config);
3257
3258 genX(cmd_buffer_emit_hashing_mode)(cmd_buffer, UINT_MAX, UINT_MAX, 1);
3259
3260 genX(flush_pipeline_select_3d)(cmd_buffer);
3261
3262 /* Apply any pending pipeline flushes we may have. We want to apply them
3263 * now because, if any of those flushes are for things like push constants,
3264 * the GPU will read the state at weird times.
3265 */
3266 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3267
3268 uint32_t vb_emit = cmd_buffer->state.gfx.vb_dirty & pipeline->vb_used;
3269 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_PIPELINE)
3270 vb_emit |= pipeline->vb_used;
3271
3272 if (vb_emit) {
3273 const uint32_t num_buffers = __builtin_popcount(vb_emit);
3274 const uint32_t num_dwords = 1 + num_buffers * 4;
3275
3276 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
3277 GENX(3DSTATE_VERTEX_BUFFERS));
3278 uint32_t vb, i = 0;
3279 for_each_bit(vb, vb_emit) {
3280 struct anv_buffer *buffer = cmd_buffer->state.vertex_bindings[vb].buffer;
3281 uint32_t offset = cmd_buffer->state.vertex_bindings[vb].offset;
3282
3283 struct GENX(VERTEX_BUFFER_STATE) state = {
3284 .VertexBufferIndex = vb,
3285
3286 .MOCS = anv_mocs_for_bo(cmd_buffer->device, buffer->address.bo),
3287 #if GEN_GEN <= 7
3288 .BufferAccessType = pipeline->vb[vb].instanced ? INSTANCEDATA : VERTEXDATA,
3289 .InstanceDataStepRate = pipeline->vb[vb].instance_divisor,
3290 #endif
3291
3292 .AddressModifyEnable = true,
3293 .BufferPitch = pipeline->vb[vb].stride,
3294 .BufferStartingAddress = anv_address_add(buffer->address, offset),
3295
3296 #if GEN_GEN >= 8
3297 .BufferSize = buffer->size - offset
3298 #else
3299 .EndAddress = anv_address_add(buffer->address, buffer->size - 1),
3300 #endif
3301 };
3302
3303 #if GEN_GEN >= 8 && GEN_GEN <= 9
3304 genX(cmd_buffer_set_binding_for_gen8_vb_flush)(cmd_buffer, vb,
3305 state.BufferStartingAddress,
3306 state.BufferSize);
3307 #endif
3308
3309 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, &p[1 + i * 4], &state);
3310 i++;
3311 }
3312 }
3313
3314 cmd_buffer->state.gfx.vb_dirty &= ~vb_emit;
3315
3316 #if GEN_GEN >= 8
3317 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_XFB_ENABLE) {
3318 /* We don't need any per-buffer dirty tracking because you're not
3319 * allowed to bind different XFB buffers while XFB is enabled.
3320 */
3321 for (unsigned idx = 0; idx < MAX_XFB_BUFFERS; idx++) {
3322 struct anv_xfb_binding *xfb = &cmd_buffer->state.xfb_bindings[idx];
3323 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_SO_BUFFER), sob) {
3324 #if GEN_GEN < 12
3325 sob.SOBufferIndex = idx;
3326 #else
3327 sob._3DCommandOpcode = 0;
3328 sob._3DCommandSubOpcode = SO_BUFFER_INDEX_0_CMD + idx;
3329 #endif
3330
3331 if (cmd_buffer->state.xfb_enabled && xfb->buffer && xfb->size != 0) {
3332 sob.SOBufferEnable = true;
3333 sob.MOCS = cmd_buffer->device->isl_dev.mocs.internal,
3334 sob.StreamOffsetWriteEnable = false;
3335 sob.SurfaceBaseAddress = anv_address_add(xfb->buffer->address,
3336 xfb->offset);
3337 /* Size is in DWords - 1 */
3338 sob.SurfaceSize = xfb->size / 4 - 1;
3339 }
3340 }
3341 }
3342
3343 /* CNL and later require a CS stall after 3DSTATE_SO_BUFFER */
3344 if (GEN_GEN >= 10)
3345 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
3346 }
3347 #endif
3348
3349 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_PIPELINE) {
3350 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->base.batch);
3351
3352 /* If the pipeline changed, we may need to re-allocate push constant
3353 * space in the URB.
3354 */
3355 cmd_buffer_alloc_push_constants(cmd_buffer);
3356 }
3357
3358 #if GEN_GEN <= 7
3359 if (cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_VERTEX_BIT ||
3360 cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_VERTEX_BIT) {
3361 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
3362 *
3363 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
3364 * stall needs to be sent just prior to any 3DSTATE_VS,
3365 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
3366 * 3DSTATE_BINDING_TABLE_POINTER_VS,
3367 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
3368 * PIPE_CONTROL needs to be sent before any combination of VS
3369 * associated 3DSTATE."
3370 */
3371 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
3372 pc.DepthStallEnable = true;
3373 pc.PostSyncOperation = WriteImmediateData;
3374 pc.Address =
3375 (struct anv_address) { cmd_buffer->device->workaround_bo, 0 };
3376 }
3377 }
3378 #endif
3379
3380 /* Render targets live in the same binding table as fragment descriptors */
3381 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_RENDER_TARGETS)
3382 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_FRAGMENT_BIT;
3383
3384 /* We emit the binding tables and sampler tables first, then emit push
3385 * constants and then finally emit binding table and sampler table
3386 * pointers. It has to happen in this order, since emitting the binding
3387 * tables may change the push constants (in case of storage images). After
3388 * emitting push constants, on SKL+ we have to emit the corresponding
3389 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
3390 */
3391 uint32_t dirty = 0;
3392 if (cmd_buffer->state.descriptors_dirty) {
3393 dirty = flush_descriptor_sets(cmd_buffer,
3394 &cmd_buffer->state.gfx.base,
3395 pipeline->shaders,
3396 ARRAY_SIZE(pipeline->shaders));
3397 }
3398
3399 if (dirty || cmd_buffer->state.push_constants_dirty) {
3400 /* Because we're pushing UBOs, we have to push whenever either
3401 * descriptors or push constants is dirty.
3402 */
3403 dirty |= cmd_buffer->state.push_constants_dirty;
3404 dirty &= ANV_STAGE_MASK & VK_SHADER_STAGE_ALL_GRAPHICS;
3405 cmd_buffer_flush_push_constants(cmd_buffer, dirty);
3406 }
3407
3408 if (dirty)
3409 cmd_buffer_emit_descriptor_pointers(cmd_buffer, dirty);
3410
3411 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_VIEWPORT)
3412 gen8_cmd_buffer_emit_viewport(cmd_buffer);
3413
3414 if (cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT |
3415 ANV_CMD_DIRTY_PIPELINE)) {
3416 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer,
3417 pipeline->depth_clamp_enable);
3418 }
3419
3420 if (cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_DYNAMIC_SCISSOR |
3421 ANV_CMD_DIRTY_RENDER_TARGETS))
3422 gen7_cmd_buffer_emit_scissor(cmd_buffer);
3423
3424 genX(cmd_buffer_flush_dynamic_state)(cmd_buffer);
3425 }
3426
3427 static void
3428 emit_vertex_bo(struct anv_cmd_buffer *cmd_buffer,
3429 struct anv_address addr,
3430 uint32_t size, uint32_t index)
3431 {
3432 uint32_t *p = anv_batch_emitn(&cmd_buffer->batch, 5,
3433 GENX(3DSTATE_VERTEX_BUFFERS));
3434
3435 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, p + 1,
3436 &(struct GENX(VERTEX_BUFFER_STATE)) {
3437 .VertexBufferIndex = index,
3438 .AddressModifyEnable = true,
3439 .BufferPitch = 0,
3440 .MOCS = addr.bo ? anv_mocs_for_bo(cmd_buffer->device, addr.bo) : 0,
3441 .NullVertexBuffer = size == 0,
3442 #if (GEN_GEN >= 8)
3443 .BufferStartingAddress = addr,
3444 .BufferSize = size
3445 #else
3446 .BufferStartingAddress = addr,
3447 .EndAddress = anv_address_add(addr, size),
3448 #endif
3449 });
3450
3451 genX(cmd_buffer_set_binding_for_gen8_vb_flush)(cmd_buffer,
3452 index, addr, size);
3453 }
3454
3455 static void
3456 emit_base_vertex_instance_bo(struct anv_cmd_buffer *cmd_buffer,
3457 struct anv_address addr)
3458 {
3459 emit_vertex_bo(cmd_buffer, addr, addr.bo ? 8 : 0, ANV_SVGS_VB_INDEX);
3460 }
3461
3462 static void
3463 emit_base_vertex_instance(struct anv_cmd_buffer *cmd_buffer,
3464 uint32_t base_vertex, uint32_t base_instance)
3465 {
3466 if (base_vertex == 0 && base_instance == 0) {
3467 emit_base_vertex_instance_bo(cmd_buffer, ANV_NULL_ADDRESS);
3468 } else {
3469 struct anv_state id_state =
3470 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 8, 4);
3471
3472 ((uint32_t *)id_state.map)[0] = base_vertex;
3473 ((uint32_t *)id_state.map)[1] = base_instance;
3474
3475 struct anv_address addr = {
3476 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
3477 .offset = id_state.offset,
3478 };
3479
3480 emit_base_vertex_instance_bo(cmd_buffer, addr);
3481 }
3482 }
3483
3484 static void
3485 emit_draw_index(struct anv_cmd_buffer *cmd_buffer, uint32_t draw_index)
3486 {
3487 struct anv_state state =
3488 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 4, 4);
3489
3490 ((uint32_t *)state.map)[0] = draw_index;
3491
3492 struct anv_address addr = {
3493 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
3494 .offset = state.offset,
3495 };
3496
3497 emit_vertex_bo(cmd_buffer, addr, 4, ANV_DRAWID_VB_INDEX);
3498 }
3499
3500 static void
3501 update_dirty_vbs_for_gen8_vb_flush(struct anv_cmd_buffer *cmd_buffer,
3502 uint32_t access_type)
3503 {
3504 struct anv_graphics_pipeline *pipeline = cmd_buffer->state.gfx.pipeline;
3505 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3506
3507 uint64_t vb_used = pipeline->vb_used;
3508 if (vs_prog_data->uses_firstvertex ||
3509 vs_prog_data->uses_baseinstance)
3510 vb_used |= 1ull << ANV_SVGS_VB_INDEX;
3511 if (vs_prog_data->uses_drawid)
3512 vb_used |= 1ull << ANV_DRAWID_VB_INDEX;
3513
3514 genX(cmd_buffer_update_dirty_vbs_for_gen8_vb_flush)(cmd_buffer,
3515 access_type == RANDOM,
3516 vb_used);
3517 }
3518
3519 void genX(CmdDraw)(
3520 VkCommandBuffer commandBuffer,
3521 uint32_t vertexCount,
3522 uint32_t instanceCount,
3523 uint32_t firstVertex,
3524 uint32_t firstInstance)
3525 {
3526 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3527 struct anv_graphics_pipeline *pipeline = cmd_buffer->state.gfx.pipeline;
3528 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3529
3530 if (anv_batch_has_error(&cmd_buffer->batch))
3531 return;
3532
3533 genX(cmd_buffer_flush_state)(cmd_buffer);
3534
3535 if (cmd_buffer->state.conditional_render_enabled)
3536 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3537
3538 if (vs_prog_data->uses_firstvertex ||
3539 vs_prog_data->uses_baseinstance)
3540 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
3541 if (vs_prog_data->uses_drawid)
3542 emit_draw_index(cmd_buffer, 0);
3543
3544 /* Emitting draw index or vertex index BOs may result in needing
3545 * additional VF cache flushes.
3546 */
3547 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3548
3549 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3550 * different views. We need to multiply instanceCount by the view count.
3551 */
3552 if (!pipeline->use_primitive_replication)
3553 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
3554
3555 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3556 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3557 prim.VertexAccessType = SEQUENTIAL;
3558 prim.PrimitiveTopologyType = pipeline->topology;
3559 prim.VertexCountPerInstance = vertexCount;
3560 prim.StartVertexLocation = firstVertex;
3561 prim.InstanceCount = instanceCount;
3562 prim.StartInstanceLocation = firstInstance;
3563 prim.BaseVertexLocation = 0;
3564 }
3565
3566 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer, SEQUENTIAL);
3567 }
3568
3569 void genX(CmdDrawIndexed)(
3570 VkCommandBuffer commandBuffer,
3571 uint32_t indexCount,
3572 uint32_t instanceCount,
3573 uint32_t firstIndex,
3574 int32_t vertexOffset,
3575 uint32_t firstInstance)
3576 {
3577 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3578 struct anv_graphics_pipeline *pipeline = cmd_buffer->state.gfx.pipeline;
3579 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3580
3581 if (anv_batch_has_error(&cmd_buffer->batch))
3582 return;
3583
3584 genX(cmd_buffer_flush_state)(cmd_buffer);
3585
3586 if (cmd_buffer->state.conditional_render_enabled)
3587 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3588
3589 if (vs_prog_data->uses_firstvertex ||
3590 vs_prog_data->uses_baseinstance)
3591 emit_base_vertex_instance(cmd_buffer, vertexOffset, firstInstance);
3592 if (vs_prog_data->uses_drawid)
3593 emit_draw_index(cmd_buffer, 0);
3594
3595 /* Emitting draw index or vertex index BOs may result in needing
3596 * additional VF cache flushes.
3597 */
3598 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3599
3600 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3601 * different views. We need to multiply instanceCount by the view count.
3602 */
3603 if (!pipeline->use_primitive_replication)
3604 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
3605
3606 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3607 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3608 prim.VertexAccessType = RANDOM;
3609 prim.PrimitiveTopologyType = pipeline->topology;
3610 prim.VertexCountPerInstance = indexCount;
3611 prim.StartVertexLocation = firstIndex;
3612 prim.InstanceCount = instanceCount;
3613 prim.StartInstanceLocation = firstInstance;
3614 prim.BaseVertexLocation = vertexOffset;
3615 }
3616
3617 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer, RANDOM);
3618 }
3619
3620 /* Auto-Draw / Indirect Registers */
3621 #define GEN7_3DPRIM_END_OFFSET 0x2420
3622 #define GEN7_3DPRIM_START_VERTEX 0x2430
3623 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
3624 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
3625 #define GEN7_3DPRIM_START_INSTANCE 0x243C
3626 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
3627
3628 void genX(CmdDrawIndirectByteCountEXT)(
3629 VkCommandBuffer commandBuffer,
3630 uint32_t instanceCount,
3631 uint32_t firstInstance,
3632 VkBuffer counterBuffer,
3633 VkDeviceSize counterBufferOffset,
3634 uint32_t counterOffset,
3635 uint32_t vertexStride)
3636 {
3637 #if GEN_IS_HASWELL || GEN_GEN >= 8
3638 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3639 ANV_FROM_HANDLE(anv_buffer, counter_buffer, counterBuffer);
3640 struct anv_graphics_pipeline *pipeline = cmd_buffer->state.gfx.pipeline;
3641 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3642
3643 /* firstVertex is always zero for this draw function */
3644 const uint32_t firstVertex = 0;
3645
3646 if (anv_batch_has_error(&cmd_buffer->batch))
3647 return;
3648
3649 genX(cmd_buffer_flush_state)(cmd_buffer);
3650
3651 if (vs_prog_data->uses_firstvertex ||
3652 vs_prog_data->uses_baseinstance)
3653 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
3654 if (vs_prog_data->uses_drawid)
3655 emit_draw_index(cmd_buffer, 0);
3656
3657 /* Emitting draw index or vertex index BOs may result in needing
3658 * additional VF cache flushes.
3659 */
3660 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3661
3662 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3663 * different views. We need to multiply instanceCount by the view count.
3664 */
3665 if (!pipeline->use_primitive_replication)
3666 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
3667
3668 struct gen_mi_builder b;
3669 gen_mi_builder_init(&b, &cmd_buffer->batch);
3670 struct gen_mi_value count =
3671 gen_mi_mem32(anv_address_add(counter_buffer->address,
3672 counterBufferOffset));
3673 if (counterOffset)
3674 count = gen_mi_isub(&b, count, gen_mi_imm(counterOffset));
3675 count = gen_mi_udiv32_imm(&b, count, vertexStride);
3676 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_VERTEX_COUNT), count);
3677
3678 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_VERTEX),
3679 gen_mi_imm(firstVertex));
3680 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_INSTANCE_COUNT),
3681 gen_mi_imm(instanceCount));
3682 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE),
3683 gen_mi_imm(firstInstance));
3684 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX), gen_mi_imm(0));
3685
3686 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3687 prim.IndirectParameterEnable = true;
3688 prim.VertexAccessType = SEQUENTIAL;
3689 prim.PrimitiveTopologyType = pipeline->topology;
3690 }
3691
3692 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer, SEQUENTIAL);
3693 #endif /* GEN_IS_HASWELL || GEN_GEN >= 8 */
3694 }
3695
3696 static void
3697 load_indirect_parameters(struct anv_cmd_buffer *cmd_buffer,
3698 struct anv_address addr,
3699 bool indexed)
3700 {
3701 struct gen_mi_builder b;
3702 gen_mi_builder_init(&b, &cmd_buffer->batch);
3703
3704 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_VERTEX_COUNT),
3705 gen_mi_mem32(anv_address_add(addr, 0)));
3706
3707 struct gen_mi_value instance_count = gen_mi_mem32(anv_address_add(addr, 4));
3708 unsigned view_count = anv_subpass_view_count(cmd_buffer->state.subpass);
3709 if (view_count > 1) {
3710 #if GEN_IS_HASWELL || GEN_GEN >= 8
3711 instance_count = gen_mi_imul_imm(&b, instance_count, view_count);
3712 #else
3713 anv_finishme("Multiview + indirect draw requires MI_MATH; "
3714 "MI_MATH is not supported on Ivy Bridge");
3715 #endif
3716 }
3717 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_INSTANCE_COUNT), instance_count);
3718
3719 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_VERTEX),
3720 gen_mi_mem32(anv_address_add(addr, 8)));
3721
3722 if (indexed) {
3723 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX),
3724 gen_mi_mem32(anv_address_add(addr, 12)));
3725 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE),
3726 gen_mi_mem32(anv_address_add(addr, 16)));
3727 } else {
3728 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE),
3729 gen_mi_mem32(anv_address_add(addr, 12)));
3730 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX), gen_mi_imm(0));
3731 }
3732 }
3733
3734 void genX(CmdDrawIndirect)(
3735 VkCommandBuffer commandBuffer,
3736 VkBuffer _buffer,
3737 VkDeviceSize offset,
3738 uint32_t drawCount,
3739 uint32_t stride)
3740 {
3741 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3742 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3743 struct anv_graphics_pipeline *pipeline = cmd_buffer->state.gfx.pipeline;
3744 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3745
3746 if (anv_batch_has_error(&cmd_buffer->batch))
3747 return;
3748
3749 genX(cmd_buffer_flush_state)(cmd_buffer);
3750
3751 if (cmd_buffer->state.conditional_render_enabled)
3752 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3753
3754 for (uint32_t i = 0; i < drawCount; i++) {
3755 struct anv_address draw = anv_address_add(buffer->address, offset);
3756
3757 if (vs_prog_data->uses_firstvertex ||
3758 vs_prog_data->uses_baseinstance)
3759 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 8));
3760 if (vs_prog_data->uses_drawid)
3761 emit_draw_index(cmd_buffer, i);
3762
3763 /* Emitting draw index or vertex index BOs may result in needing
3764 * additional VF cache flushes.
3765 */
3766 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3767
3768 load_indirect_parameters(cmd_buffer, draw, false);
3769
3770 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3771 prim.IndirectParameterEnable = true;
3772 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3773 prim.VertexAccessType = SEQUENTIAL;
3774 prim.PrimitiveTopologyType = pipeline->topology;
3775 }
3776
3777 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer, SEQUENTIAL);
3778
3779 offset += stride;
3780 }
3781 }
3782
3783 void genX(CmdDrawIndexedIndirect)(
3784 VkCommandBuffer commandBuffer,
3785 VkBuffer _buffer,
3786 VkDeviceSize offset,
3787 uint32_t drawCount,
3788 uint32_t stride)
3789 {
3790 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3791 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3792 struct anv_graphics_pipeline *pipeline = cmd_buffer->state.gfx.pipeline;
3793 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3794
3795 if (anv_batch_has_error(&cmd_buffer->batch))
3796 return;
3797
3798 genX(cmd_buffer_flush_state)(cmd_buffer);
3799
3800 if (cmd_buffer->state.conditional_render_enabled)
3801 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3802
3803 for (uint32_t i = 0; i < drawCount; i++) {
3804 struct anv_address draw = anv_address_add(buffer->address, offset);
3805
3806 /* TODO: We need to stomp base vertex to 0 somehow */
3807 if (vs_prog_data->uses_firstvertex ||
3808 vs_prog_data->uses_baseinstance)
3809 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 12));
3810 if (vs_prog_data->uses_drawid)
3811 emit_draw_index(cmd_buffer, i);
3812
3813 /* Emitting draw index or vertex index BOs may result in needing
3814 * additional VF cache flushes.
3815 */
3816 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3817
3818 load_indirect_parameters(cmd_buffer, draw, true);
3819
3820 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3821 prim.IndirectParameterEnable = true;
3822 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3823 prim.VertexAccessType = RANDOM;
3824 prim.PrimitiveTopologyType = pipeline->topology;
3825 }
3826
3827 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer, RANDOM);
3828
3829 offset += stride;
3830 }
3831 }
3832
3833 #define TMP_DRAW_COUNT_REG 0x2670 /* MI_ALU_REG14 */
3834
3835 static void
3836 prepare_for_draw_count_predicate(struct anv_cmd_buffer *cmd_buffer,
3837 struct anv_address count_address,
3838 const bool conditional_render_enabled)
3839 {
3840 struct gen_mi_builder b;
3841 gen_mi_builder_init(&b, &cmd_buffer->batch);
3842
3843 if (conditional_render_enabled) {
3844 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3845 gen_mi_store(&b, gen_mi_reg64(TMP_DRAW_COUNT_REG),
3846 gen_mi_mem32(count_address));
3847 #endif
3848 } else {
3849 /* Upload the current draw count from the draw parameters buffer to
3850 * MI_PREDICATE_SRC0.
3851 */
3852 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0),
3853 gen_mi_mem32(count_address));
3854
3855 gen_mi_store(&b, gen_mi_reg32(MI_PREDICATE_SRC1 + 4), gen_mi_imm(0));
3856 }
3857 }
3858
3859 static void
3860 emit_draw_count_predicate(struct anv_cmd_buffer *cmd_buffer,
3861 uint32_t draw_index)
3862 {
3863 struct gen_mi_builder b;
3864 gen_mi_builder_init(&b, &cmd_buffer->batch);
3865
3866 /* Upload the index of the current primitive to MI_PREDICATE_SRC1. */
3867 gen_mi_store(&b, gen_mi_reg32(MI_PREDICATE_SRC1), gen_mi_imm(draw_index));
3868
3869 if (draw_index == 0) {
3870 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
3871 mip.LoadOperation = LOAD_LOADINV;
3872 mip.CombineOperation = COMBINE_SET;
3873 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3874 }
3875 } else {
3876 /* While draw_index < draw_count the predicate's result will be
3877 * (draw_index == draw_count) ^ TRUE = TRUE
3878 * When draw_index == draw_count the result is
3879 * (TRUE) ^ TRUE = FALSE
3880 * After this all results will be:
3881 * (FALSE) ^ FALSE = FALSE
3882 */
3883 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
3884 mip.LoadOperation = LOAD_LOAD;
3885 mip.CombineOperation = COMBINE_XOR;
3886 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3887 }
3888 }
3889 }
3890
3891 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3892 static void
3893 emit_draw_count_predicate_with_conditional_render(
3894 struct anv_cmd_buffer *cmd_buffer,
3895 uint32_t draw_index)
3896 {
3897 struct gen_mi_builder b;
3898 gen_mi_builder_init(&b, &cmd_buffer->batch);
3899
3900 struct gen_mi_value pred = gen_mi_ult(&b, gen_mi_imm(draw_index),
3901 gen_mi_reg64(TMP_DRAW_COUNT_REG));
3902 pred = gen_mi_iand(&b, pred, gen_mi_reg64(ANV_PREDICATE_RESULT_REG));
3903
3904 #if GEN_GEN >= 8
3905 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_RESULT), pred);
3906 #else
3907 /* MI_PREDICATE_RESULT is not whitelisted in i915 command parser
3908 * so we emit MI_PREDICATE to set it.
3909 */
3910
3911 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0), pred);
3912 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
3913
3914 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
3915 mip.LoadOperation = LOAD_LOADINV;
3916 mip.CombineOperation = COMBINE_SET;
3917 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3918 }
3919 #endif
3920 }
3921 #endif
3922
3923 void genX(CmdDrawIndirectCount)(
3924 VkCommandBuffer commandBuffer,
3925 VkBuffer _buffer,
3926 VkDeviceSize offset,
3927 VkBuffer _countBuffer,
3928 VkDeviceSize countBufferOffset,
3929 uint32_t maxDrawCount,
3930 uint32_t stride)
3931 {
3932 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3933 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3934 ANV_FROM_HANDLE(anv_buffer, count_buffer, _countBuffer);
3935 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
3936 struct anv_graphics_pipeline *pipeline = cmd_state->gfx.pipeline;
3937 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3938
3939 if (anv_batch_has_error(&cmd_buffer->batch))
3940 return;
3941
3942 genX(cmd_buffer_flush_state)(cmd_buffer);
3943
3944 struct anv_address count_address =
3945 anv_address_add(count_buffer->address, countBufferOffset);
3946
3947 prepare_for_draw_count_predicate(cmd_buffer, count_address,
3948 cmd_state->conditional_render_enabled);
3949
3950 for (uint32_t i = 0; i < maxDrawCount; i++) {
3951 struct anv_address draw = anv_address_add(buffer->address, offset);
3952
3953 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3954 if (cmd_state->conditional_render_enabled) {
3955 emit_draw_count_predicate_with_conditional_render(cmd_buffer, i);
3956 } else {
3957 emit_draw_count_predicate(cmd_buffer, i);
3958 }
3959 #else
3960 emit_draw_count_predicate(cmd_buffer, i);
3961 #endif
3962
3963 if (vs_prog_data->uses_firstvertex ||
3964 vs_prog_data->uses_baseinstance)
3965 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 8));
3966 if (vs_prog_data->uses_drawid)
3967 emit_draw_index(cmd_buffer, i);
3968
3969 /* Emitting draw index or vertex index BOs may result in needing
3970 * additional VF cache flushes.
3971 */
3972 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3973
3974 load_indirect_parameters(cmd_buffer, draw, false);
3975
3976 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3977 prim.IndirectParameterEnable = true;
3978 prim.PredicateEnable = true;
3979 prim.VertexAccessType = SEQUENTIAL;
3980 prim.PrimitiveTopologyType = pipeline->topology;
3981 }
3982
3983 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer, SEQUENTIAL);
3984
3985 offset += stride;
3986 }
3987 }
3988
3989 void genX(CmdDrawIndexedIndirectCount)(
3990 VkCommandBuffer commandBuffer,
3991 VkBuffer _buffer,
3992 VkDeviceSize offset,
3993 VkBuffer _countBuffer,
3994 VkDeviceSize countBufferOffset,
3995 uint32_t maxDrawCount,
3996 uint32_t stride)
3997 {
3998 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3999 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
4000 ANV_FROM_HANDLE(anv_buffer, count_buffer, _countBuffer);
4001 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
4002 struct anv_graphics_pipeline *pipeline = cmd_state->gfx.pipeline;
4003 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
4004
4005 if (anv_batch_has_error(&cmd_buffer->batch))
4006 return;
4007
4008 genX(cmd_buffer_flush_state)(cmd_buffer);
4009
4010 struct anv_address count_address =
4011 anv_address_add(count_buffer->address, countBufferOffset);
4012
4013 prepare_for_draw_count_predicate(cmd_buffer, count_address,
4014 cmd_state->conditional_render_enabled);
4015
4016 for (uint32_t i = 0; i < maxDrawCount; i++) {
4017 struct anv_address draw = anv_address_add(buffer->address, offset);
4018
4019 #if GEN_GEN >= 8 || GEN_IS_HASWELL
4020 if (cmd_state->conditional_render_enabled) {
4021 emit_draw_count_predicate_with_conditional_render(cmd_buffer, i);
4022 } else {
4023 emit_draw_count_predicate(cmd_buffer, i);
4024 }
4025 #else
4026 emit_draw_count_predicate(cmd_buffer, i);
4027 #endif
4028
4029 /* TODO: We need to stomp base vertex to 0 somehow */
4030 if (vs_prog_data->uses_firstvertex ||
4031 vs_prog_data->uses_baseinstance)
4032 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 12));
4033 if (vs_prog_data->uses_drawid)
4034 emit_draw_index(cmd_buffer, i);
4035
4036 /* Emitting draw index or vertex index BOs may result in needing
4037 * additional VF cache flushes.
4038 */
4039 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
4040
4041 load_indirect_parameters(cmd_buffer, draw, true);
4042
4043 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
4044 prim.IndirectParameterEnable = true;
4045 prim.PredicateEnable = true;
4046 prim.VertexAccessType = RANDOM;
4047 prim.PrimitiveTopologyType = pipeline->topology;
4048 }
4049
4050 update_dirty_vbs_for_gen8_vb_flush(cmd_buffer, RANDOM);
4051
4052 offset += stride;
4053 }
4054 }
4055
4056 void genX(CmdBeginTransformFeedbackEXT)(
4057 VkCommandBuffer commandBuffer,
4058 uint32_t firstCounterBuffer,
4059 uint32_t counterBufferCount,
4060 const VkBuffer* pCounterBuffers,
4061 const VkDeviceSize* pCounterBufferOffsets)
4062 {
4063 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4064
4065 assert(firstCounterBuffer < MAX_XFB_BUFFERS);
4066 assert(counterBufferCount <= MAX_XFB_BUFFERS);
4067 assert(firstCounterBuffer + counterBufferCount <= MAX_XFB_BUFFERS);
4068
4069 /* From the SKL PRM Vol. 2c, SO_WRITE_OFFSET:
4070 *
4071 * "Ssoftware must ensure that no HW stream output operations can be in
4072 * process or otherwise pending at the point that the MI_LOAD/STORE
4073 * commands are processed. This will likely require a pipeline flush."
4074 */
4075 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
4076 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
4077
4078 for (uint32_t idx = 0; idx < MAX_XFB_BUFFERS; idx++) {
4079 /* If we have a counter buffer, this is a resume so we need to load the
4080 * value into the streamout offset register. Otherwise, this is a begin
4081 * and we need to reset it to zero.
4082 */
4083 if (pCounterBuffers &&
4084 idx >= firstCounterBuffer &&
4085 idx - firstCounterBuffer < counterBufferCount &&
4086 pCounterBuffers[idx - firstCounterBuffer] != VK_NULL_HANDLE) {
4087 uint32_t cb_idx = idx - firstCounterBuffer;
4088 ANV_FROM_HANDLE(anv_buffer, counter_buffer, pCounterBuffers[cb_idx]);
4089 uint64_t offset = pCounterBufferOffsets ?
4090 pCounterBufferOffsets[cb_idx] : 0;
4091
4092 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4093 lrm.RegisterAddress = GENX(SO_WRITE_OFFSET0_num) + idx * 4;
4094 lrm.MemoryAddress = anv_address_add(counter_buffer->address,
4095 offset);
4096 }
4097 } else {
4098 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
4099 lri.RegisterOffset = GENX(SO_WRITE_OFFSET0_num) + idx * 4;
4100 lri.DataDWord = 0;
4101 }
4102 }
4103 }
4104
4105 cmd_buffer->state.xfb_enabled = true;
4106 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_XFB_ENABLE;
4107 }
4108
4109 void genX(CmdEndTransformFeedbackEXT)(
4110 VkCommandBuffer commandBuffer,
4111 uint32_t firstCounterBuffer,
4112 uint32_t counterBufferCount,
4113 const VkBuffer* pCounterBuffers,
4114 const VkDeviceSize* pCounterBufferOffsets)
4115 {
4116 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4117
4118 assert(firstCounterBuffer < MAX_XFB_BUFFERS);
4119 assert(counterBufferCount <= MAX_XFB_BUFFERS);
4120 assert(firstCounterBuffer + counterBufferCount <= MAX_XFB_BUFFERS);
4121
4122 /* From the SKL PRM Vol. 2c, SO_WRITE_OFFSET:
4123 *
4124 * "Ssoftware must ensure that no HW stream output operations can be in
4125 * process or otherwise pending at the point that the MI_LOAD/STORE
4126 * commands are processed. This will likely require a pipeline flush."
4127 */
4128 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
4129 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
4130
4131 for (uint32_t cb_idx = 0; cb_idx < counterBufferCount; cb_idx++) {
4132 unsigned idx = firstCounterBuffer + cb_idx;
4133
4134 /* If we have a counter buffer, this is a resume so we need to load the
4135 * value into the streamout offset register. Otherwise, this is a begin
4136 * and we need to reset it to zero.
4137 */
4138 if (pCounterBuffers &&
4139 cb_idx < counterBufferCount &&
4140 pCounterBuffers[cb_idx] != VK_NULL_HANDLE) {
4141 ANV_FROM_HANDLE(anv_buffer, counter_buffer, pCounterBuffers[cb_idx]);
4142 uint64_t offset = pCounterBufferOffsets ?
4143 pCounterBufferOffsets[cb_idx] : 0;
4144
4145 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
4146 srm.MemoryAddress = anv_address_add(counter_buffer->address,
4147 offset);
4148 srm.RegisterAddress = GENX(SO_WRITE_OFFSET0_num) + idx * 4;
4149 }
4150 }
4151 }
4152
4153 cmd_buffer->state.xfb_enabled = false;
4154 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_XFB_ENABLE;
4155 }
4156
4157 void
4158 genX(cmd_buffer_flush_compute_state)(struct anv_cmd_buffer *cmd_buffer)
4159 {
4160 struct anv_compute_pipeline *pipeline = cmd_buffer->state.compute.pipeline;
4161
4162 assert(pipeline->cs);
4163
4164 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->base.l3_config);
4165
4166 genX(flush_pipeline_select_gpgpu)(cmd_buffer);
4167
4168 /* Apply any pending pipeline flushes we may have. We want to apply them
4169 * now because, if any of those flushes are for things like push constants,
4170 * the GPU will read the state at weird times.
4171 */
4172 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
4173
4174 if (cmd_buffer->state.compute.pipeline_dirty) {
4175 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
4176 *
4177 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
4178 * the only bits that are changed are scoreboard related: Scoreboard
4179 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
4180 * these scoreboard related states, a MEDIA_STATE_FLUSH is
4181 * sufficient."
4182 */
4183 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
4184 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
4185
4186 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->base.batch);
4187
4188 /* The workgroup size of the pipeline affects our push constant layout
4189 * so flag push constants as dirty if we change the pipeline.
4190 */
4191 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_COMPUTE_BIT;
4192 }
4193
4194 if ((cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_COMPUTE_BIT) ||
4195 cmd_buffer->state.compute.pipeline_dirty) {
4196 flush_descriptor_sets(cmd_buffer,
4197 &cmd_buffer->state.compute.base,
4198 &pipeline->cs, 1);
4199
4200 uint32_t iface_desc_data_dw[GENX(INTERFACE_DESCRIPTOR_DATA_length)];
4201 struct GENX(INTERFACE_DESCRIPTOR_DATA) desc = {
4202 .BindingTablePointer =
4203 cmd_buffer->state.binding_tables[MESA_SHADER_COMPUTE].offset,
4204 .SamplerStatePointer =
4205 cmd_buffer->state.samplers[MESA_SHADER_COMPUTE].offset,
4206 };
4207 GENX(INTERFACE_DESCRIPTOR_DATA_pack)(NULL, iface_desc_data_dw, &desc);
4208
4209 struct anv_state state =
4210 anv_cmd_buffer_merge_dynamic(cmd_buffer, iface_desc_data_dw,
4211 pipeline->interface_descriptor_data,
4212 GENX(INTERFACE_DESCRIPTOR_DATA_length),
4213 64);
4214
4215 uint32_t size = GENX(INTERFACE_DESCRIPTOR_DATA_length) * sizeof(uint32_t);
4216 anv_batch_emit(&cmd_buffer->batch,
4217 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD), mid) {
4218 mid.InterfaceDescriptorTotalLength = size;
4219 mid.InterfaceDescriptorDataStartAddress = state.offset;
4220 }
4221 }
4222
4223 if (cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_COMPUTE_BIT) {
4224 struct anv_state push_state =
4225 anv_cmd_buffer_cs_push_constants(cmd_buffer);
4226
4227 if (push_state.alloc_size) {
4228 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_CURBE_LOAD), curbe) {
4229 curbe.CURBETotalDataLength = push_state.alloc_size;
4230 curbe.CURBEDataStartAddress = push_state.offset;
4231 }
4232 }
4233
4234 cmd_buffer->state.push_constants_dirty &= ~VK_SHADER_STAGE_COMPUTE_BIT;
4235 }
4236
4237 cmd_buffer->state.compute.pipeline_dirty = false;
4238
4239 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
4240 }
4241
4242 #if GEN_GEN == 7
4243
4244 static VkResult
4245 verify_cmd_parser(const struct anv_device *device,
4246 int required_version,
4247 const char *function)
4248 {
4249 if (device->physical->cmd_parser_version < required_version) {
4250 return vk_errorf(device, device->physical,
4251 VK_ERROR_FEATURE_NOT_PRESENT,
4252 "cmd parser version %d is required for %s",
4253 required_version, function);
4254 } else {
4255 return VK_SUCCESS;
4256 }
4257 }
4258
4259 #endif
4260
4261 static void
4262 anv_cmd_buffer_push_base_group_id(struct anv_cmd_buffer *cmd_buffer,
4263 uint32_t baseGroupX,
4264 uint32_t baseGroupY,
4265 uint32_t baseGroupZ)
4266 {
4267 if (anv_batch_has_error(&cmd_buffer->batch))
4268 return;
4269
4270 struct anv_push_constants *push =
4271 &cmd_buffer->state.push_constants[MESA_SHADER_COMPUTE];
4272 if (push->cs.base_work_group_id[0] != baseGroupX ||
4273 push->cs.base_work_group_id[1] != baseGroupY ||
4274 push->cs.base_work_group_id[2] != baseGroupZ) {
4275 push->cs.base_work_group_id[0] = baseGroupX;
4276 push->cs.base_work_group_id[1] = baseGroupY;
4277 push->cs.base_work_group_id[2] = baseGroupZ;
4278
4279 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_COMPUTE_BIT;
4280 }
4281 }
4282
4283 void genX(CmdDispatch)(
4284 VkCommandBuffer commandBuffer,
4285 uint32_t x,
4286 uint32_t y,
4287 uint32_t z)
4288 {
4289 genX(CmdDispatchBase)(commandBuffer, 0, 0, 0, x, y, z);
4290 }
4291
4292 void genX(CmdDispatchBase)(
4293 VkCommandBuffer commandBuffer,
4294 uint32_t baseGroupX,
4295 uint32_t baseGroupY,
4296 uint32_t baseGroupZ,
4297 uint32_t groupCountX,
4298 uint32_t groupCountY,
4299 uint32_t groupCountZ)
4300 {
4301 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4302 struct anv_compute_pipeline *pipeline = cmd_buffer->state.compute.pipeline;
4303 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
4304
4305 anv_cmd_buffer_push_base_group_id(cmd_buffer, baseGroupX,
4306 baseGroupY, baseGroupZ);
4307
4308 if (anv_batch_has_error(&cmd_buffer->batch))
4309 return;
4310
4311 if (prog_data->uses_num_work_groups) {
4312 struct anv_state state =
4313 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 12, 4);
4314 uint32_t *sizes = state.map;
4315 sizes[0] = groupCountX;
4316 sizes[1] = groupCountY;
4317 sizes[2] = groupCountZ;
4318 cmd_buffer->state.compute.num_workgroups = (struct anv_address) {
4319 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
4320 .offset = state.offset,
4321 };
4322
4323 /* The num_workgroups buffer goes in the binding table */
4324 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_COMPUTE_BIT;
4325 }
4326
4327 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
4328
4329 if (cmd_buffer->state.conditional_render_enabled)
4330 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
4331
4332 anv_batch_emit(&cmd_buffer->batch, GENX(GPGPU_WALKER), ggw) {
4333 ggw.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
4334 ggw.SIMDSize = prog_data->simd_size / 16;
4335 ggw.ThreadDepthCounterMaximum = 0;
4336 ggw.ThreadHeightCounterMaximum = 0;
4337 ggw.ThreadWidthCounterMaximum = anv_cs_threads(pipeline) - 1;
4338 ggw.ThreadGroupIDXDimension = groupCountX;
4339 ggw.ThreadGroupIDYDimension = groupCountY;
4340 ggw.ThreadGroupIDZDimension = groupCountZ;
4341 ggw.RightExecutionMask = pipeline->cs_right_mask;
4342 ggw.BottomExecutionMask = 0xffffffff;
4343 }
4344
4345 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_STATE_FLUSH), msf);
4346 }
4347
4348 #define GPGPU_DISPATCHDIMX 0x2500
4349 #define GPGPU_DISPATCHDIMY 0x2504
4350 #define GPGPU_DISPATCHDIMZ 0x2508
4351
4352 void genX(CmdDispatchIndirect)(
4353 VkCommandBuffer commandBuffer,
4354 VkBuffer _buffer,
4355 VkDeviceSize offset)
4356 {
4357 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4358 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
4359 struct anv_compute_pipeline *pipeline = cmd_buffer->state.compute.pipeline;
4360 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
4361 struct anv_address addr = anv_address_add(buffer->address, offset);
4362 struct anv_batch *batch = &cmd_buffer->batch;
4363
4364 anv_cmd_buffer_push_base_group_id(cmd_buffer, 0, 0, 0);
4365
4366 #if GEN_GEN == 7
4367 /* Linux 4.4 added command parser version 5 which allows the GPGPU
4368 * indirect dispatch registers to be written.
4369 */
4370 if (verify_cmd_parser(cmd_buffer->device, 5,
4371 "vkCmdDispatchIndirect") != VK_SUCCESS)
4372 return;
4373 #endif
4374
4375 if (prog_data->uses_num_work_groups) {
4376 cmd_buffer->state.compute.num_workgroups = addr;
4377
4378 /* The num_workgroups buffer goes in the binding table */
4379 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_COMPUTE_BIT;
4380 }
4381
4382 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
4383
4384 struct gen_mi_builder b;
4385 gen_mi_builder_init(&b, &cmd_buffer->batch);
4386
4387 struct gen_mi_value size_x = gen_mi_mem32(anv_address_add(addr, 0));
4388 struct gen_mi_value size_y = gen_mi_mem32(anv_address_add(addr, 4));
4389 struct gen_mi_value size_z = gen_mi_mem32(anv_address_add(addr, 8));
4390
4391 gen_mi_store(&b, gen_mi_reg32(GPGPU_DISPATCHDIMX), size_x);
4392 gen_mi_store(&b, gen_mi_reg32(GPGPU_DISPATCHDIMY), size_y);
4393 gen_mi_store(&b, gen_mi_reg32(GPGPU_DISPATCHDIMZ), size_z);
4394
4395 #if GEN_GEN <= 7
4396 /* predicate = (compute_dispatch_indirect_x_size == 0); */
4397 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0), size_x);
4398 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
4399 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
4400 mip.LoadOperation = LOAD_LOAD;
4401 mip.CombineOperation = COMBINE_SET;
4402 mip.CompareOperation = COMPARE_SRCS_EQUAL;
4403 }
4404
4405 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
4406 gen_mi_store(&b, gen_mi_reg32(MI_PREDICATE_SRC0), size_y);
4407 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
4408 mip.LoadOperation = LOAD_LOAD;
4409 mip.CombineOperation = COMBINE_OR;
4410 mip.CompareOperation = COMPARE_SRCS_EQUAL;
4411 }
4412
4413 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
4414 gen_mi_store(&b, gen_mi_reg32(MI_PREDICATE_SRC0), size_z);
4415 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
4416 mip.LoadOperation = LOAD_LOAD;
4417 mip.CombineOperation = COMBINE_OR;
4418 mip.CompareOperation = COMPARE_SRCS_EQUAL;
4419 }
4420
4421 /* predicate = !predicate; */
4422 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
4423 mip.LoadOperation = LOAD_LOADINV;
4424 mip.CombineOperation = COMBINE_OR;
4425 mip.CompareOperation = COMPARE_FALSE;
4426 }
4427
4428 #if GEN_IS_HASWELL
4429 if (cmd_buffer->state.conditional_render_enabled) {
4430 /* predicate &= !(conditional_rendering_predicate == 0); */
4431 gen_mi_store(&b, gen_mi_reg32(MI_PREDICATE_SRC0),
4432 gen_mi_reg32(ANV_PREDICATE_RESULT_REG));
4433 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
4434 mip.LoadOperation = LOAD_LOADINV;
4435 mip.CombineOperation = COMBINE_AND;
4436 mip.CompareOperation = COMPARE_SRCS_EQUAL;
4437 }
4438 }
4439 #endif
4440
4441 #else /* GEN_GEN > 7 */
4442 if (cmd_buffer->state.conditional_render_enabled)
4443 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
4444 #endif
4445
4446 anv_batch_emit(batch, GENX(GPGPU_WALKER), ggw) {
4447 ggw.IndirectParameterEnable = true;
4448 ggw.PredicateEnable = GEN_GEN <= 7 ||
4449 cmd_buffer->state.conditional_render_enabled;
4450 ggw.SIMDSize = prog_data->simd_size / 16;
4451 ggw.ThreadDepthCounterMaximum = 0;
4452 ggw.ThreadHeightCounterMaximum = 0;
4453 ggw.ThreadWidthCounterMaximum = anv_cs_threads(pipeline) - 1;
4454 ggw.RightExecutionMask = pipeline->cs_right_mask;
4455 ggw.BottomExecutionMask = 0xffffffff;
4456 }
4457
4458 anv_batch_emit(batch, GENX(MEDIA_STATE_FLUSH), msf);
4459 }
4460
4461 static void
4462 genX(flush_pipeline_select)(struct anv_cmd_buffer *cmd_buffer,
4463 uint32_t pipeline)
4464 {
4465 UNUSED const struct gen_device_info *devinfo = &cmd_buffer->device->info;
4466
4467 if (cmd_buffer->state.current_pipeline == pipeline)
4468 return;
4469
4470 #if GEN_GEN >= 8 && GEN_GEN < 10
4471 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
4472 *
4473 * Software must clear the COLOR_CALC_STATE Valid field in
4474 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
4475 * with Pipeline Select set to GPGPU.
4476 *
4477 * The internal hardware docs recommend the same workaround for Gen9
4478 * hardware too.
4479 */
4480 if (pipeline == GPGPU)
4481 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CC_STATE_POINTERS), t);
4482 #endif
4483
4484 #if GEN_GEN == 9
4485 if (pipeline == _3D) {
4486 /* There is a mid-object preemption workaround which requires you to
4487 * re-emit MEDIA_VFE_STATE after switching from GPGPU to 3D. However,
4488 * even without preemption, we have issues with geometry flickering when
4489 * GPGPU and 3D are back-to-back and this seems to fix it. We don't
4490 * really know why.
4491 */
4492 const uint32_t subslices =
4493 MAX2(cmd_buffer->device->physical->subslice_total, 1);
4494 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_VFE_STATE), vfe) {
4495 vfe.MaximumNumberofThreads =
4496 devinfo->max_cs_threads * subslices - 1;
4497 vfe.NumberofURBEntries = 2;
4498 vfe.URBEntryAllocationSize = 2;
4499 }
4500
4501 /* We just emitted a dummy MEDIA_VFE_STATE so now that packet is
4502 * invalid. Set the compute pipeline to dirty to force a re-emit of the
4503 * pipeline in case we get back-to-back dispatch calls with the same
4504 * pipeline and a PIPELINE_SELECT in between.
4505 */
4506 cmd_buffer->state.compute.pipeline_dirty = true;
4507 }
4508 #endif
4509
4510 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
4511 * PIPELINE_SELECT [DevBWR+]":
4512 *
4513 * Project: DEVSNB+
4514 *
4515 * Software must ensure all the write caches are flushed through a
4516 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
4517 * command to invalidate read only caches prior to programming
4518 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
4519 */
4520 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
4521 pc.RenderTargetCacheFlushEnable = true;
4522 pc.DepthCacheFlushEnable = true;
4523 pc.DCFlushEnable = true;
4524 pc.PostSyncOperation = NoWrite;
4525 pc.CommandStreamerStallEnable = true;
4526 #if GEN_GEN >= 12
4527 pc.TileCacheFlushEnable = true;
4528
4529 /* GEN:BUG:1409600907: "PIPE_CONTROL with Depth Stall Enable bit must be
4530 * set with any PIPE_CONTROL with Depth Flush Enable bit set.
4531 */
4532 pc.DepthStallEnable = true;
4533 #endif
4534 }
4535
4536 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
4537 pc.TextureCacheInvalidationEnable = true;
4538 pc.ConstantCacheInvalidationEnable = true;
4539 pc.StateCacheInvalidationEnable = true;
4540 pc.InstructionCacheInvalidateEnable = true;
4541 pc.PostSyncOperation = NoWrite;
4542 #if GEN_GEN >= 12
4543 pc.TileCacheFlushEnable = true;
4544 #endif
4545 }
4546
4547 anv_batch_emit(&cmd_buffer->batch, GENX(PIPELINE_SELECT), ps) {
4548 #if GEN_GEN >= 9
4549 ps.MaskBits = 3;
4550 #endif
4551 ps.PipelineSelection = pipeline;
4552 }
4553
4554 #if GEN_GEN == 9
4555 if (devinfo->is_geminilake) {
4556 /* Project: DevGLK
4557 *
4558 * "This chicken bit works around a hardware issue with barrier logic
4559 * encountered when switching between GPGPU and 3D pipelines. To
4560 * workaround the issue, this mode bit should be set after a pipeline
4561 * is selected."
4562 */
4563 uint32_t scec;
4564 anv_pack_struct(&scec, GENX(SLICE_COMMON_ECO_CHICKEN1),
4565 .GLKBarrierMode =
4566 pipeline == GPGPU ? GLK_BARRIER_MODE_GPGPU
4567 : GLK_BARRIER_MODE_3D_HULL,
4568 .GLKBarrierModeMask = 1);
4569 emit_lri(&cmd_buffer->batch, GENX(SLICE_COMMON_ECO_CHICKEN1_num), scec);
4570 }
4571 #endif
4572
4573 cmd_buffer->state.current_pipeline = pipeline;
4574 }
4575
4576 void
4577 genX(flush_pipeline_select_3d)(struct anv_cmd_buffer *cmd_buffer)
4578 {
4579 genX(flush_pipeline_select)(cmd_buffer, _3D);
4580 }
4581
4582 void
4583 genX(flush_pipeline_select_gpgpu)(struct anv_cmd_buffer *cmd_buffer)
4584 {
4585 genX(flush_pipeline_select)(cmd_buffer, GPGPU);
4586 }
4587
4588 void
4589 genX(cmd_buffer_emit_gen7_depth_flush)(struct anv_cmd_buffer *cmd_buffer)
4590 {
4591 if (GEN_GEN >= 8)
4592 return;
4593
4594 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
4595 *
4596 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
4597 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
4598 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
4599 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
4600 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
4601 * Depth Flush Bit set, followed by another pipelined depth stall
4602 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
4603 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
4604 * via a preceding MI_FLUSH)."
4605 */
4606 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
4607 pipe.DepthStallEnable = true;
4608 }
4609 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
4610 pipe.DepthCacheFlushEnable = true;
4611 #if GEN_GEN >= 12
4612 pipe.TileCacheFlushEnable = true;
4613 #endif
4614 }
4615 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
4616 pipe.DepthStallEnable = true;
4617 }
4618 }
4619
4620 /* From the Skylake PRM, 3DSTATE_VERTEX_BUFFERS:
4621 *
4622 * "The VF cache needs to be invalidated before binding and then using
4623 * Vertex Buffers that overlap with any previously bound Vertex Buffer
4624 * (at a 64B granularity) since the last invalidation. A VF cache
4625 * invalidate is performed by setting the "VF Cache Invalidation Enable"
4626 * bit in PIPE_CONTROL."
4627 *
4628 * This is implemented by carefully tracking all vertex and index buffer
4629 * bindings and flushing if the cache ever ends up with a range in the cache
4630 * that would exceed 4 GiB. This is implemented in three parts:
4631 *
4632 * 1. genX(cmd_buffer_set_binding_for_gen8_vb_flush)() which must be called
4633 * every time a 3DSTATE_VERTEX_BUFFER packet is emitted and informs the
4634 * tracking code of the new binding. If this new binding would cause
4635 * the cache to have a too-large range on the next draw call, a pipeline
4636 * stall and VF cache invalidate are added to pending_pipeline_bits.
4637 *
4638 * 2. genX(cmd_buffer_apply_pipe_flushes)() resets the cache tracking to
4639 * empty whenever we emit a VF invalidate.
4640 *
4641 * 3. genX(cmd_buffer_update_dirty_vbs_for_gen8_vb_flush)() must be called
4642 * after every 3DPRIMITIVE and copies the bound range into the dirty
4643 * range for each used buffer. This has to be a separate step because
4644 * we don't always re-bind all buffers and so 1. can't know which
4645 * buffers are actually bound.
4646 */
4647 void
4648 genX(cmd_buffer_set_binding_for_gen8_vb_flush)(struct anv_cmd_buffer *cmd_buffer,
4649 int vb_index,
4650 struct anv_address vb_address,
4651 uint32_t vb_size)
4652 {
4653 if (GEN_GEN < 8 || GEN_GEN > 9 ||
4654 !cmd_buffer->device->physical->use_softpin)
4655 return;
4656
4657 struct anv_vb_cache_range *bound, *dirty;
4658 if (vb_index == -1) {
4659 bound = &cmd_buffer->state.gfx.ib_bound_range;
4660 dirty = &cmd_buffer->state.gfx.ib_dirty_range;
4661 } else {
4662 assert(vb_index >= 0);
4663 assert(vb_index < ARRAY_SIZE(cmd_buffer->state.gfx.vb_bound_ranges));
4664 assert(vb_index < ARRAY_SIZE(cmd_buffer->state.gfx.vb_dirty_ranges));
4665 bound = &cmd_buffer->state.gfx.vb_bound_ranges[vb_index];
4666 dirty = &cmd_buffer->state.gfx.vb_dirty_ranges[vb_index];
4667 }
4668
4669 if (vb_size == 0) {
4670 bound->start = 0;
4671 bound->end = 0;
4672 return;
4673 }
4674
4675 assert(vb_address.bo && (vb_address.bo->flags & EXEC_OBJECT_PINNED));
4676 bound->start = gen_48b_address(anv_address_physical(vb_address));
4677 bound->end = bound->start + vb_size;
4678 assert(bound->end > bound->start); /* No overflow */
4679
4680 /* Align everything to a cache line */
4681 bound->start &= ~(64ull - 1ull);
4682 bound->end = align_u64(bound->end, 64);
4683
4684 /* Compute the dirty range */
4685 dirty->start = MIN2(dirty->start, bound->start);
4686 dirty->end = MAX2(dirty->end, bound->end);
4687
4688 /* If our range is larger than 32 bits, we have to flush */
4689 assert(bound->end - bound->start <= (1ull << 32));
4690 if (dirty->end - dirty->start > (1ull << 32)) {
4691 cmd_buffer->state.pending_pipe_bits |=
4692 ANV_PIPE_CS_STALL_BIT | ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
4693 }
4694 }
4695
4696 void
4697 genX(cmd_buffer_update_dirty_vbs_for_gen8_vb_flush)(struct anv_cmd_buffer *cmd_buffer,
4698 uint32_t access_type,
4699 uint64_t vb_used)
4700 {
4701 if (GEN_GEN < 8 || GEN_GEN > 9 ||
4702 !cmd_buffer->device->physical->use_softpin)
4703 return;
4704
4705 if (access_type == RANDOM) {
4706 /* We have an index buffer */
4707 struct anv_vb_cache_range *bound = &cmd_buffer->state.gfx.ib_bound_range;
4708 struct anv_vb_cache_range *dirty = &cmd_buffer->state.gfx.ib_dirty_range;
4709
4710 if (bound->end > bound->start) {
4711 dirty->start = MIN2(dirty->start, bound->start);
4712 dirty->end = MAX2(dirty->end, bound->end);
4713 }
4714 }
4715
4716 uint64_t mask = vb_used;
4717 while (mask) {
4718 int i = u_bit_scan64(&mask);
4719 assert(i >= 0);
4720 assert(i < ARRAY_SIZE(cmd_buffer->state.gfx.vb_bound_ranges));
4721 assert(i < ARRAY_SIZE(cmd_buffer->state.gfx.vb_dirty_ranges));
4722
4723 struct anv_vb_cache_range *bound, *dirty;
4724 bound = &cmd_buffer->state.gfx.vb_bound_ranges[i];
4725 dirty = &cmd_buffer->state.gfx.vb_dirty_ranges[i];
4726
4727 if (bound->end > bound->start) {
4728 dirty->start = MIN2(dirty->start, bound->start);
4729 dirty->end = MAX2(dirty->end, bound->end);
4730 }
4731 }
4732 }
4733
4734 /**
4735 * Update the pixel hashing modes that determine the balancing of PS threads
4736 * across subslices and slices.
4737 *
4738 * \param width Width bound of the rendering area (already scaled down if \p
4739 * scale is greater than 1).
4740 * \param height Height bound of the rendering area (already scaled down if \p
4741 * scale is greater than 1).
4742 * \param scale The number of framebuffer samples that could potentially be
4743 * affected by an individual channel of the PS thread. This is
4744 * typically one for single-sampled rendering, but for operations
4745 * like CCS resolves and fast clears a single PS invocation may
4746 * update a huge number of pixels, in which case a finer
4747 * balancing is desirable in order to maximally utilize the
4748 * bandwidth available. UINT_MAX can be used as shorthand for
4749 * "finest hashing mode available".
4750 */
4751 void
4752 genX(cmd_buffer_emit_hashing_mode)(struct anv_cmd_buffer *cmd_buffer,
4753 unsigned width, unsigned height,
4754 unsigned scale)
4755 {
4756 #if GEN_GEN == 9
4757 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
4758 const unsigned slice_hashing[] = {
4759 /* Because all Gen9 platforms with more than one slice require
4760 * three-way subslice hashing, a single "normal" 16x16 slice hashing
4761 * block is guaranteed to suffer from substantial imbalance, with one
4762 * subslice receiving twice as much work as the other two in the
4763 * slice.
4764 *
4765 * The performance impact of that would be particularly severe when
4766 * three-way hashing is also in use for slice balancing (which is the
4767 * case for all Gen9 GT4 platforms), because one of the slices
4768 * receives one every three 16x16 blocks in either direction, which
4769 * is roughly the periodicity of the underlying subslice imbalance
4770 * pattern ("roughly" because in reality the hardware's
4771 * implementation of three-way hashing doesn't do exact modulo 3
4772 * arithmetic, which somewhat decreases the magnitude of this effect
4773 * in practice). This leads to a systematic subslice imbalance
4774 * within that slice regardless of the size of the primitive. The
4775 * 32x32 hashing mode guarantees that the subslice imbalance within a
4776 * single slice hashing block is minimal, largely eliminating this
4777 * effect.
4778 */
4779 _32x32,
4780 /* Finest slice hashing mode available. */
4781 NORMAL
4782 };
4783 const unsigned subslice_hashing[] = {
4784 /* 16x16 would provide a slight cache locality benefit especially
4785 * visible in the sampler L1 cache efficiency of low-bandwidth
4786 * non-LLC platforms, but it comes at the cost of greater subslice
4787 * imbalance for primitives of dimensions approximately intermediate
4788 * between 16x4 and 16x16.
4789 */
4790 _16x4,
4791 /* Finest subslice hashing mode available. */
4792 _8x4
4793 };
4794 /* Dimensions of the smallest hashing block of a given hashing mode. If
4795 * the rendering area is smaller than this there can't possibly be any
4796 * benefit from switching to this mode, so we optimize out the
4797 * transition.
4798 */
4799 const unsigned min_size[][2] = {
4800 { 16, 4 },
4801 { 8, 4 }
4802 };
4803 const unsigned idx = scale > 1;
4804
4805 if (cmd_buffer->state.current_hash_scale != scale &&
4806 (width > min_size[idx][0] || height > min_size[idx][1])) {
4807 uint32_t gt_mode;
4808
4809 anv_pack_struct(&gt_mode, GENX(GT_MODE),
4810 .SliceHashing = (devinfo->num_slices > 1 ? slice_hashing[idx] : 0),
4811 .SliceHashingMask = (devinfo->num_slices > 1 ? -1 : 0),
4812 .SubsliceHashing = subslice_hashing[idx],
4813 .SubsliceHashingMask = -1);
4814
4815 cmd_buffer->state.pending_pipe_bits |=
4816 ANV_PIPE_CS_STALL_BIT | ANV_PIPE_STALL_AT_SCOREBOARD_BIT;
4817 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
4818
4819 emit_lri(&cmd_buffer->batch, GENX(GT_MODE_num), gt_mode);
4820
4821 cmd_buffer->state.current_hash_scale = scale;
4822 }
4823 #endif
4824 }
4825
4826 static void
4827 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer *cmd_buffer)
4828 {
4829 struct anv_device *device = cmd_buffer->device;
4830 const struct anv_image_view *iview =
4831 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
4832 const struct anv_image *image = iview ? iview->image : NULL;
4833
4834 /* FIXME: Width and Height are wrong */
4835
4836 genX(cmd_buffer_emit_gen7_depth_flush)(cmd_buffer);
4837
4838 uint32_t *dw = anv_batch_emit_dwords(&cmd_buffer->batch,
4839 device->isl_dev.ds.size / 4);
4840 if (dw == NULL)
4841 return;
4842
4843 struct isl_depth_stencil_hiz_emit_info info = { };
4844
4845 if (iview)
4846 info.view = &iview->planes[0].isl;
4847
4848 if (image && (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
4849 uint32_t depth_plane =
4850 anv_image_aspect_to_plane(image->aspects, VK_IMAGE_ASPECT_DEPTH_BIT);
4851 const struct anv_surface *surface = &image->planes[depth_plane].surface;
4852
4853 info.depth_surf = &surface->isl;
4854
4855 info.depth_address =
4856 anv_batch_emit_reloc(&cmd_buffer->batch,
4857 dw + device->isl_dev.ds.depth_offset / 4,
4858 image->planes[depth_plane].address.bo,
4859 image->planes[depth_plane].address.offset +
4860 surface->offset);
4861 info.mocs =
4862 anv_mocs_for_bo(device, image->planes[depth_plane].address.bo);
4863
4864 const uint32_t ds =
4865 cmd_buffer->state.subpass->depth_stencil_attachment->attachment;
4866 info.hiz_usage = cmd_buffer->state.attachments[ds].aux_usage;
4867 if (info.hiz_usage != ISL_AUX_USAGE_NONE) {
4868 assert(isl_aux_usage_has_hiz(info.hiz_usage));
4869 info.hiz_surf = &image->planes[depth_plane].aux_surface.isl;
4870
4871 info.hiz_address =
4872 anv_batch_emit_reloc(&cmd_buffer->batch,
4873 dw + device->isl_dev.ds.hiz_offset / 4,
4874 image->planes[depth_plane].address.bo,
4875 image->planes[depth_plane].address.offset +
4876 image->planes[depth_plane].aux_surface.offset);
4877
4878 info.depth_clear_value = ANV_HZ_FC_VAL;
4879 }
4880 }
4881
4882 if (image && (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT)) {
4883 uint32_t stencil_plane =
4884 anv_image_aspect_to_plane(image->aspects, VK_IMAGE_ASPECT_STENCIL_BIT);
4885 const struct anv_surface *surface = &image->planes[stencil_plane].surface;
4886
4887 info.stencil_surf = &surface->isl;
4888
4889 info.stencil_address =
4890 anv_batch_emit_reloc(&cmd_buffer->batch,
4891 dw + device->isl_dev.ds.stencil_offset / 4,
4892 image->planes[stencil_plane].address.bo,
4893 image->planes[stencil_plane].address.offset +
4894 surface->offset);
4895 info.mocs =
4896 anv_mocs_for_bo(device, image->planes[stencil_plane].address.bo);
4897 }
4898
4899 isl_emit_depth_stencil_hiz_s(&device->isl_dev, dw, &info);
4900
4901 if (GEN_GEN >= 12) {
4902 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_POST_SYNC_BIT;
4903 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
4904
4905 /* GEN:BUG:1408224581
4906 *
4907 * Workaround: Gen12LP Astep only An additional pipe control with
4908 * post-sync = store dword operation would be required.( w/a is to
4909 * have an additional pipe control after the stencil state whenever
4910 * the surface state bits of this state is changing).
4911 */
4912 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
4913 pc.PostSyncOperation = WriteImmediateData;
4914 pc.Address =
4915 (struct anv_address) { cmd_buffer->device->workaround_bo, 0 };
4916 }
4917 }
4918 cmd_buffer->state.hiz_enabled = isl_aux_usage_has_hiz(info.hiz_usage);
4919 }
4920
4921 /**
4922 * This ANDs the view mask of the current subpass with the pending clear
4923 * views in the attachment to get the mask of views active in the subpass
4924 * that still need to be cleared.
4925 */
4926 static inline uint32_t
4927 get_multiview_subpass_clear_mask(const struct anv_cmd_state *cmd_state,
4928 const struct anv_attachment_state *att_state)
4929 {
4930 return cmd_state->subpass->view_mask & att_state->pending_clear_views;
4931 }
4932
4933 static inline bool
4934 do_first_layer_clear(const struct anv_cmd_state *cmd_state,
4935 const struct anv_attachment_state *att_state)
4936 {
4937 if (!cmd_state->subpass->view_mask)
4938 return true;
4939
4940 uint32_t pending_clear_mask =
4941 get_multiview_subpass_clear_mask(cmd_state, att_state);
4942
4943 return pending_clear_mask & 1;
4944 }
4945
4946 static inline bool
4947 current_subpass_is_last_for_attachment(const struct anv_cmd_state *cmd_state,
4948 uint32_t att_idx)
4949 {
4950 const uint32_t last_subpass_idx =
4951 cmd_state->pass->attachments[att_idx].last_subpass_idx;
4952 const struct anv_subpass *last_subpass =
4953 &cmd_state->pass->subpasses[last_subpass_idx];
4954 return last_subpass == cmd_state->subpass;
4955 }
4956
4957 static void
4958 cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
4959 uint32_t subpass_id)
4960 {
4961 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
4962 struct anv_subpass *subpass = &cmd_state->pass->subpasses[subpass_id];
4963 cmd_state->subpass = subpass;
4964
4965 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
4966
4967 /* Our implementation of VK_KHR_multiview uses instancing to draw the
4968 * different views. If the client asks for instancing, we need to use the
4969 * Instance Data Step Rate to ensure that we repeat the client's
4970 * per-instance data once for each view. Since this bit is in
4971 * VERTEX_BUFFER_STATE on gen7, we need to dirty vertex buffers at the top
4972 * of each subpass.
4973 */
4974 if (GEN_GEN == 7)
4975 cmd_buffer->state.gfx.vb_dirty |= ~0;
4976
4977 /* It is possible to start a render pass with an old pipeline. Because the
4978 * render pass and subpass index are both baked into the pipeline, this is
4979 * highly unlikely. In order to do so, it requires that you have a render
4980 * pass with a single subpass and that you use that render pass twice
4981 * back-to-back and use the same pipeline at the start of the second render
4982 * pass as at the end of the first. In order to avoid unpredictable issues
4983 * with this edge case, we just dirty the pipeline at the start of every
4984 * subpass.
4985 */
4986 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_PIPELINE;
4987
4988 /* Accumulate any subpass flushes that need to happen before the subpass */
4989 cmd_buffer->state.pending_pipe_bits |=
4990 cmd_buffer->state.pass->subpass_flushes[subpass_id];
4991
4992 VkRect2D render_area = cmd_buffer->state.render_area;
4993 struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
4994
4995 bool is_multiview = subpass->view_mask != 0;
4996
4997 for (uint32_t i = 0; i < subpass->attachment_count; ++i) {
4998 const uint32_t a = subpass->attachments[i].attachment;
4999 if (a == VK_ATTACHMENT_UNUSED)
5000 continue;
5001
5002 assert(a < cmd_state->pass->attachment_count);
5003 struct anv_attachment_state *att_state = &cmd_state->attachments[a];
5004
5005 struct anv_image_view *iview = cmd_state->attachments[a].image_view;
5006 const struct anv_image *image = iview->image;
5007
5008 /* A resolve is necessary before use as an input attachment if the clear
5009 * color or auxiliary buffer usage isn't supported by the sampler.
5010 */
5011 const bool input_needs_resolve =
5012 (att_state->fast_clear && !att_state->clear_color_is_zero_one) ||
5013 att_state->input_aux_usage != att_state->aux_usage;
5014
5015 VkImageLayout target_layout;
5016 if (iview->aspect_mask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV &&
5017 !input_needs_resolve) {
5018 /* Layout transitions before the final only help to enable sampling
5019 * as an input attachment. If the input attachment supports sampling
5020 * using the auxiliary surface, we can skip such transitions by
5021 * making the target layout one that is CCS-aware.
5022 */
5023 target_layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5024 } else {
5025 target_layout = subpass->attachments[i].layout;
5026 }
5027
5028 VkImageLayout target_stencil_layout =
5029 subpass->attachments[i].stencil_layout;
5030
5031 uint32_t base_layer, layer_count;
5032 if (image->type == VK_IMAGE_TYPE_3D) {
5033 base_layer = 0;
5034 layer_count = anv_minify(iview->image->extent.depth,
5035 iview->planes[0].isl.base_level);
5036 } else {
5037 base_layer = iview->planes[0].isl.base_array_layer;
5038 layer_count = fb->layers;
5039 }
5040
5041 if (image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
5042 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
5043 transition_color_buffer(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
5044 iview->planes[0].isl.base_level, 1,
5045 base_layer, layer_count,
5046 att_state->current_layout, target_layout);
5047 }
5048
5049 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
5050 transition_depth_buffer(cmd_buffer, image,
5051 base_layer, layer_count,
5052 att_state->current_layout, target_layout);
5053 att_state->aux_usage =
5054 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
5055 VK_IMAGE_ASPECT_DEPTH_BIT,
5056 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
5057 target_layout);
5058 }
5059
5060 if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
5061 transition_stencil_buffer(cmd_buffer, image,
5062 iview->planes[0].isl.base_level, 1,
5063 base_layer, layer_count,
5064 att_state->current_stencil_layout,
5065 target_stencil_layout);
5066 }
5067 att_state->current_layout = target_layout;
5068 att_state->current_stencil_layout = target_stencil_layout;
5069
5070 if (att_state->pending_clear_aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
5071 assert(att_state->pending_clear_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
5072
5073 /* Multi-planar images are not supported as attachments */
5074 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
5075 assert(image->n_planes == 1);
5076
5077 uint32_t base_clear_layer = iview->planes[0].isl.base_array_layer;
5078 uint32_t clear_layer_count = fb->layers;
5079
5080 if (att_state->fast_clear &&
5081 do_first_layer_clear(cmd_state, att_state)) {
5082 /* We only support fast-clears on the first layer */
5083 assert(iview->planes[0].isl.base_level == 0);
5084 assert(iview->planes[0].isl.base_array_layer == 0);
5085
5086 union isl_color_value clear_color = {};
5087 anv_clear_color_from_att_state(&clear_color, att_state, iview);
5088 if (iview->image->samples == 1) {
5089 anv_image_ccs_op(cmd_buffer, image,
5090 iview->planes[0].isl.format,
5091 iview->planes[0].isl.swizzle,
5092 VK_IMAGE_ASPECT_COLOR_BIT,
5093 0, 0, 1, ISL_AUX_OP_FAST_CLEAR,
5094 &clear_color,
5095 false);
5096 } else {
5097 anv_image_mcs_op(cmd_buffer, image,
5098 iview->planes[0].isl.format,
5099 iview->planes[0].isl.swizzle,
5100 VK_IMAGE_ASPECT_COLOR_BIT,
5101 0, 1, ISL_AUX_OP_FAST_CLEAR,
5102 &clear_color,
5103 false);
5104 }
5105 base_clear_layer++;
5106 clear_layer_count--;
5107 if (is_multiview)
5108 att_state->pending_clear_views &= ~1;
5109
5110 if (att_state->clear_color_is_zero) {
5111 /* This image has the auxiliary buffer enabled. We can mark the
5112 * subresource as not needing a resolve because the clear color
5113 * will match what's in every RENDER_SURFACE_STATE object when
5114 * it's being used for sampling.
5115 */
5116 set_image_fast_clear_state(cmd_buffer, iview->image,
5117 VK_IMAGE_ASPECT_COLOR_BIT,
5118 ANV_FAST_CLEAR_DEFAULT_VALUE);
5119 } else {
5120 set_image_fast_clear_state(cmd_buffer, iview->image,
5121 VK_IMAGE_ASPECT_COLOR_BIT,
5122 ANV_FAST_CLEAR_ANY);
5123 }
5124 }
5125
5126 /* From the VkFramebufferCreateInfo spec:
5127 *
5128 * "If the render pass uses multiview, then layers must be one and each
5129 * attachment requires a number of layers that is greater than the
5130 * maximum bit index set in the view mask in the subpasses in which it
5131 * is used."
5132 *
5133 * So if multiview is active we ignore the number of layers in the
5134 * framebuffer and instead we honor the view mask from the subpass.
5135 */
5136 if (is_multiview) {
5137 assert(image->n_planes == 1);
5138 uint32_t pending_clear_mask =
5139 get_multiview_subpass_clear_mask(cmd_state, att_state);
5140
5141 uint32_t layer_idx;
5142 for_each_bit(layer_idx, pending_clear_mask) {
5143 uint32_t layer =
5144 iview->planes[0].isl.base_array_layer + layer_idx;
5145
5146 anv_image_clear_color(cmd_buffer, image,
5147 VK_IMAGE_ASPECT_COLOR_BIT,
5148 att_state->aux_usage,
5149 iview->planes[0].isl.format,
5150 iview->planes[0].isl.swizzle,
5151 iview->planes[0].isl.base_level,
5152 layer, 1,
5153 render_area,
5154 vk_to_isl_color(att_state->clear_value.color));
5155 }
5156
5157 att_state->pending_clear_views &= ~pending_clear_mask;
5158 } else if (clear_layer_count > 0) {
5159 assert(image->n_planes == 1);
5160 anv_image_clear_color(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
5161 att_state->aux_usage,
5162 iview->planes[0].isl.format,
5163 iview->planes[0].isl.swizzle,
5164 iview->planes[0].isl.base_level,
5165 base_clear_layer, clear_layer_count,
5166 render_area,
5167 vk_to_isl_color(att_state->clear_value.color));
5168 }
5169 } else if (att_state->pending_clear_aspects & (VK_IMAGE_ASPECT_DEPTH_BIT |
5170 VK_IMAGE_ASPECT_STENCIL_BIT)) {
5171 if (att_state->fast_clear && !is_multiview) {
5172 /* We currently only support HiZ for single-LOD images */
5173 if (att_state->pending_clear_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
5174 assert(isl_aux_usage_has_hiz(iview->image->planes[0].aux_usage));
5175 assert(iview->planes[0].isl.base_level == 0);
5176 }
5177
5178 anv_image_hiz_clear(cmd_buffer, image,
5179 att_state->pending_clear_aspects,
5180 iview->planes[0].isl.base_level,
5181 iview->planes[0].isl.base_array_layer,
5182 fb->layers, render_area,
5183 att_state->clear_value.depthStencil.stencil);
5184 } else if (is_multiview) {
5185 uint32_t pending_clear_mask =
5186 get_multiview_subpass_clear_mask(cmd_state, att_state);
5187
5188 uint32_t layer_idx;
5189 for_each_bit(layer_idx, pending_clear_mask) {
5190 uint32_t layer =
5191 iview->planes[0].isl.base_array_layer + layer_idx;
5192
5193 anv_image_clear_depth_stencil(cmd_buffer, image,
5194 att_state->pending_clear_aspects,
5195 att_state->aux_usage,
5196 iview->planes[0].isl.base_level,
5197 layer, 1,
5198 render_area,
5199 att_state->clear_value.depthStencil.depth,
5200 att_state->clear_value.depthStencil.stencil);
5201 }
5202
5203 att_state->pending_clear_views &= ~pending_clear_mask;
5204 } else {
5205 anv_image_clear_depth_stencil(cmd_buffer, image,
5206 att_state->pending_clear_aspects,
5207 att_state->aux_usage,
5208 iview->planes[0].isl.base_level,
5209 iview->planes[0].isl.base_array_layer,
5210 fb->layers, render_area,
5211 att_state->clear_value.depthStencil.depth,
5212 att_state->clear_value.depthStencil.stencil);
5213 }
5214 } else {
5215 assert(att_state->pending_clear_aspects == 0);
5216 }
5217
5218 if (GEN_GEN < 10 &&
5219 (att_state->pending_load_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) &&
5220 image->planes[0].aux_usage != ISL_AUX_USAGE_NONE &&
5221 iview->planes[0].isl.base_level == 0 &&
5222 iview->planes[0].isl.base_array_layer == 0) {
5223 if (att_state->aux_usage != ISL_AUX_USAGE_NONE) {
5224 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->color.state,
5225 image, VK_IMAGE_ASPECT_COLOR_BIT,
5226 false /* copy to ss */);
5227 }
5228
5229 if (need_input_attachment_state(&cmd_state->pass->attachments[a]) &&
5230 att_state->input_aux_usage != ISL_AUX_USAGE_NONE) {
5231 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->input.state,
5232 image, VK_IMAGE_ASPECT_COLOR_BIT,
5233 false /* copy to ss */);
5234 }
5235 }
5236
5237 if (subpass->attachments[i].usage ==
5238 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
5239 /* We assume that if we're starting a subpass, we're going to do some
5240 * rendering so we may end up with compressed data.
5241 */
5242 genX(cmd_buffer_mark_image_written)(cmd_buffer, iview->image,
5243 VK_IMAGE_ASPECT_COLOR_BIT,
5244 att_state->aux_usage,
5245 iview->planes[0].isl.base_level,
5246 iview->planes[0].isl.base_array_layer,
5247 fb->layers);
5248 } else if (subpass->attachments[i].usage ==
5249 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
5250 /* We may be writing depth or stencil so we need to mark the surface.
5251 * Unfortunately, there's no way to know at this point whether the
5252 * depth or stencil tests used will actually write to the surface.
5253 *
5254 * Even though stencil may be plane 1, it always shares a base_level
5255 * with depth.
5256 */
5257 const struct isl_view *ds_view = &iview->planes[0].isl;
5258 if (iview->aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
5259 genX(cmd_buffer_mark_image_written)(cmd_buffer, image,
5260 VK_IMAGE_ASPECT_DEPTH_BIT,
5261 att_state->aux_usage,
5262 ds_view->base_level,
5263 ds_view->base_array_layer,
5264 fb->layers);
5265 }
5266 if (iview->aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
5267 /* Even though stencil may be plane 1, it always shares a
5268 * base_level with depth.
5269 */
5270 genX(cmd_buffer_mark_image_written)(cmd_buffer, image,
5271 VK_IMAGE_ASPECT_STENCIL_BIT,
5272 ISL_AUX_USAGE_NONE,
5273 ds_view->base_level,
5274 ds_view->base_array_layer,
5275 fb->layers);
5276 }
5277 }
5278
5279 /* If multiview is enabled, then we are only done clearing when we no
5280 * longer have pending layers to clear, or when we have processed the
5281 * last subpass that uses this attachment.
5282 */
5283 if (!is_multiview ||
5284 att_state->pending_clear_views == 0 ||
5285 current_subpass_is_last_for_attachment(cmd_state, a)) {
5286 att_state->pending_clear_aspects = 0;
5287 }
5288
5289 att_state->pending_load_aspects = 0;
5290 }
5291
5292 #if GEN_GEN >= 11
5293 /* The PIPE_CONTROL command description says:
5294 *
5295 * "Whenever a Binding Table Index (BTI) used by a Render Taget Message
5296 * points to a different RENDER_SURFACE_STATE, SW must issue a Render
5297 * Target Cache Flush by enabling this bit. When render target flush
5298 * is set due to new association of BTI, PS Scoreboard Stall bit must
5299 * be set in this packet."
5300 */
5301 cmd_buffer->state.pending_pipe_bits |=
5302 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT |
5303 ANV_PIPE_STALL_AT_SCOREBOARD_BIT;
5304 #endif
5305
5306 #if GEN_GEN == 12
5307 /* GEN:BUG:14010455700
5308 *
5309 * ISL will change some CHICKEN registers depending on the depth surface
5310 * format, along with emitting the depth and stencil packets. In that case,
5311 * we want to do a depth flush and stall, so the pipeline is not using these
5312 * settings while we change the registers.
5313 */
5314 cmd_buffer->state.pending_pipe_bits |=
5315 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT |
5316 ANV_PIPE_DEPTH_STALL_BIT |
5317 ANV_PIPE_END_OF_PIPE_SYNC_BIT;
5318 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
5319 #endif
5320
5321 cmd_buffer_emit_depth_stencil(cmd_buffer);
5322 }
5323
5324 static enum blorp_filter
5325 vk_to_blorp_resolve_mode(VkResolveModeFlagBitsKHR vk_mode)
5326 {
5327 switch (vk_mode) {
5328 case VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR:
5329 return BLORP_FILTER_SAMPLE_0;
5330 case VK_RESOLVE_MODE_AVERAGE_BIT_KHR:
5331 return BLORP_FILTER_AVERAGE;
5332 case VK_RESOLVE_MODE_MIN_BIT_KHR:
5333 return BLORP_FILTER_MIN_SAMPLE;
5334 case VK_RESOLVE_MODE_MAX_BIT_KHR:
5335 return BLORP_FILTER_MAX_SAMPLE;
5336 default:
5337 return BLORP_FILTER_NONE;
5338 }
5339 }
5340
5341 static void
5342 cmd_buffer_end_subpass(struct anv_cmd_buffer *cmd_buffer)
5343 {
5344 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
5345 struct anv_subpass *subpass = cmd_state->subpass;
5346 uint32_t subpass_id = anv_get_subpass_id(&cmd_buffer->state);
5347 struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
5348
5349 if (subpass->has_color_resolve) {
5350 /* We are about to do some MSAA resolves. We need to flush so that the
5351 * result of writes to the MSAA color attachments show up in the sampler
5352 * when we blit to the single-sampled resolve target.
5353 */
5354 cmd_buffer->state.pending_pipe_bits |=
5355 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT |
5356 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
5357
5358 for (uint32_t i = 0; i < subpass->color_count; ++i) {
5359 uint32_t src_att = subpass->color_attachments[i].attachment;
5360 uint32_t dst_att = subpass->resolve_attachments[i].attachment;
5361
5362 if (dst_att == VK_ATTACHMENT_UNUSED)
5363 continue;
5364
5365 assert(src_att < cmd_buffer->state.pass->attachment_count);
5366 assert(dst_att < cmd_buffer->state.pass->attachment_count);
5367
5368 if (cmd_buffer->state.attachments[dst_att].pending_clear_aspects) {
5369 /* From the Vulkan 1.0 spec:
5370 *
5371 * If the first use of an attachment in a render pass is as a
5372 * resolve attachment, then the loadOp is effectively ignored
5373 * as the resolve is guaranteed to overwrite all pixels in the
5374 * render area.
5375 */
5376 cmd_buffer->state.attachments[dst_att].pending_clear_aspects = 0;
5377 }
5378
5379 struct anv_image_view *src_iview = cmd_state->attachments[src_att].image_view;
5380 struct anv_image_view *dst_iview = cmd_state->attachments[dst_att].image_view;
5381
5382 const VkRect2D render_area = cmd_buffer->state.render_area;
5383
5384 enum isl_aux_usage src_aux_usage =
5385 cmd_buffer->state.attachments[src_att].aux_usage;
5386 enum isl_aux_usage dst_aux_usage =
5387 cmd_buffer->state.attachments[dst_att].aux_usage;
5388
5389 assert(src_iview->aspect_mask == VK_IMAGE_ASPECT_COLOR_BIT &&
5390 dst_iview->aspect_mask == VK_IMAGE_ASPECT_COLOR_BIT);
5391
5392 anv_image_msaa_resolve(cmd_buffer,
5393 src_iview->image, src_aux_usage,
5394 src_iview->planes[0].isl.base_level,
5395 src_iview->planes[0].isl.base_array_layer,
5396 dst_iview->image, dst_aux_usage,
5397 dst_iview->planes[0].isl.base_level,
5398 dst_iview->planes[0].isl.base_array_layer,
5399 VK_IMAGE_ASPECT_COLOR_BIT,
5400 render_area.offset.x, render_area.offset.y,
5401 render_area.offset.x, render_area.offset.y,
5402 render_area.extent.width,
5403 render_area.extent.height,
5404 fb->layers, BLORP_FILTER_NONE);
5405 }
5406 }
5407
5408 if (subpass->ds_resolve_attachment) {
5409 /* We are about to do some MSAA resolves. We need to flush so that the
5410 * result of writes to the MSAA depth attachments show up in the sampler
5411 * when we blit to the single-sampled resolve target.
5412 */
5413 cmd_buffer->state.pending_pipe_bits |=
5414 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT |
5415 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
5416
5417 uint32_t src_att = subpass->depth_stencil_attachment->attachment;
5418 uint32_t dst_att = subpass->ds_resolve_attachment->attachment;
5419
5420 assert(src_att < cmd_buffer->state.pass->attachment_count);
5421 assert(dst_att < cmd_buffer->state.pass->attachment_count);
5422
5423 if (cmd_buffer->state.attachments[dst_att].pending_clear_aspects) {
5424 /* From the Vulkan 1.0 spec:
5425 *
5426 * If the first use of an attachment in a render pass is as a
5427 * resolve attachment, then the loadOp is effectively ignored
5428 * as the resolve is guaranteed to overwrite all pixels in the
5429 * render area.
5430 */
5431 cmd_buffer->state.attachments[dst_att].pending_clear_aspects = 0;
5432 }
5433
5434 struct anv_image_view *src_iview = cmd_state->attachments[src_att].image_view;
5435 struct anv_image_view *dst_iview = cmd_state->attachments[dst_att].image_view;
5436
5437 const VkRect2D render_area = cmd_buffer->state.render_area;
5438
5439 struct anv_attachment_state *src_state =
5440 &cmd_state->attachments[src_att];
5441 struct anv_attachment_state *dst_state =
5442 &cmd_state->attachments[dst_att];
5443
5444 if ((src_iview->image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
5445 subpass->depth_resolve_mode != VK_RESOLVE_MODE_NONE_KHR) {
5446
5447 /* MSAA resolves sample from the source attachment. Transition the
5448 * depth attachment first to get rid of any HiZ that we may not be
5449 * able to handle.
5450 */
5451 transition_depth_buffer(cmd_buffer, src_iview->image,
5452 src_iview->planes[0].isl.base_array_layer,
5453 fb->layers,
5454 src_state->current_layout,
5455 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
5456 src_state->aux_usage =
5457 anv_layout_to_aux_usage(&cmd_buffer->device->info, src_iview->image,
5458 VK_IMAGE_ASPECT_DEPTH_BIT,
5459 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
5460 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
5461 src_state->current_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
5462
5463 /* MSAA resolves write to the resolve attachment as if it were any
5464 * other transfer op. Transition the resolve attachment accordingly.
5465 */
5466 VkImageLayout dst_initial_layout = dst_state->current_layout;
5467
5468 /* If our render area is the entire size of the image, we're going to
5469 * blow it all away so we can claim the initial layout is UNDEFINED
5470 * and we'll get a HiZ ambiguate instead of a resolve.
5471 */
5472 if (dst_iview->image->type != VK_IMAGE_TYPE_3D &&
5473 render_area.offset.x == 0 && render_area.offset.y == 0 &&
5474 render_area.extent.width == dst_iview->extent.width &&
5475 render_area.extent.height == dst_iview->extent.height)
5476 dst_initial_layout = VK_IMAGE_LAYOUT_UNDEFINED;
5477
5478 transition_depth_buffer(cmd_buffer, dst_iview->image,
5479 dst_iview->planes[0].isl.base_array_layer,
5480 fb->layers,
5481 dst_initial_layout,
5482 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
5483 dst_state->aux_usage =
5484 anv_layout_to_aux_usage(&cmd_buffer->device->info, dst_iview->image,
5485 VK_IMAGE_ASPECT_DEPTH_BIT,
5486 VK_IMAGE_USAGE_TRANSFER_DST_BIT,
5487 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
5488 dst_state->current_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
5489
5490 enum blorp_filter filter =
5491 vk_to_blorp_resolve_mode(subpass->depth_resolve_mode);
5492
5493 anv_image_msaa_resolve(cmd_buffer,
5494 src_iview->image, src_state->aux_usage,
5495 src_iview->planes[0].isl.base_level,
5496 src_iview->planes[0].isl.base_array_layer,
5497 dst_iview->image, dst_state->aux_usage,
5498 dst_iview->planes[0].isl.base_level,
5499 dst_iview->planes[0].isl.base_array_layer,
5500 VK_IMAGE_ASPECT_DEPTH_BIT,
5501 render_area.offset.x, render_area.offset.y,
5502 render_area.offset.x, render_area.offset.y,
5503 render_area.extent.width,
5504 render_area.extent.height,
5505 fb->layers, filter);
5506 }
5507
5508 if ((src_iview->image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
5509 subpass->stencil_resolve_mode != VK_RESOLVE_MODE_NONE_KHR) {
5510
5511 src_state->current_stencil_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
5512 dst_state->current_stencil_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
5513
5514 enum isl_aux_usage src_aux_usage = ISL_AUX_USAGE_NONE;
5515 enum isl_aux_usage dst_aux_usage = ISL_AUX_USAGE_NONE;
5516
5517 enum blorp_filter filter =
5518 vk_to_blorp_resolve_mode(subpass->stencil_resolve_mode);
5519
5520 anv_image_msaa_resolve(cmd_buffer,
5521 src_iview->image, src_aux_usage,
5522 src_iview->planes[0].isl.base_level,
5523 src_iview->planes[0].isl.base_array_layer,
5524 dst_iview->image, dst_aux_usage,
5525 dst_iview->planes[0].isl.base_level,
5526 dst_iview->planes[0].isl.base_array_layer,
5527 VK_IMAGE_ASPECT_STENCIL_BIT,
5528 render_area.offset.x, render_area.offset.y,
5529 render_area.offset.x, render_area.offset.y,
5530 render_area.extent.width,
5531 render_area.extent.height,
5532 fb->layers, filter);
5533 }
5534 }
5535
5536 #if GEN_GEN == 7
5537 /* On gen7, we have to store a texturable version of the stencil buffer in
5538 * a shadow whenever VK_IMAGE_USAGE_SAMPLED_BIT is set and copy back and
5539 * forth at strategic points. Stencil writes are only allowed in following
5540 * layouts:
5541 *
5542 * - VK_IMAGE_LAYOUT_GENERAL
5543 * - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
5544 * - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
5545 * - VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
5546 * - VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL_KHR
5547 *
5548 * For general, we have no nice opportunity to transition so we do the copy
5549 * to the shadow unconditionally at the end of the subpass. For transfer
5550 * destinations, we can update it as part of the transfer op. For the other
5551 * layouts, we delay the copy until a transition into some other layout.
5552 */
5553 if (subpass->depth_stencil_attachment) {
5554 uint32_t a = subpass->depth_stencil_attachment->attachment;
5555 assert(a != VK_ATTACHMENT_UNUSED);
5556
5557 struct anv_attachment_state *att_state = &cmd_state->attachments[a];
5558 struct anv_image_view *iview = cmd_state->attachments[a].image_view;;
5559 const struct anv_image *image = iview->image;
5560
5561 if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
5562 uint32_t plane = anv_image_aspect_to_plane(image->aspects,
5563 VK_IMAGE_ASPECT_STENCIL_BIT);
5564
5565 if (image->planes[plane].shadow_surface.isl.size_B > 0 &&
5566 att_state->current_stencil_layout == VK_IMAGE_LAYOUT_GENERAL) {
5567 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
5568 anv_image_copy_to_shadow(cmd_buffer, image,
5569 VK_IMAGE_ASPECT_STENCIL_BIT,
5570 iview->planes[plane].isl.base_level, 1,
5571 iview->planes[plane].isl.base_array_layer,
5572 fb->layers);
5573 }
5574 }
5575 }
5576 #endif /* GEN_GEN == 7 */
5577
5578 for (uint32_t i = 0; i < subpass->attachment_count; ++i) {
5579 const uint32_t a = subpass->attachments[i].attachment;
5580 if (a == VK_ATTACHMENT_UNUSED)
5581 continue;
5582
5583 if (cmd_state->pass->attachments[a].last_subpass_idx != subpass_id)
5584 continue;
5585
5586 assert(a < cmd_state->pass->attachment_count);
5587 struct anv_attachment_state *att_state = &cmd_state->attachments[a];
5588 struct anv_image_view *iview = cmd_state->attachments[a].image_view;
5589 const struct anv_image *image = iview->image;
5590
5591 if ((image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) &&
5592 image->vk_format != iview->vk_format) {
5593 enum anv_fast_clear_type fast_clear_type =
5594 anv_layout_to_fast_clear_type(&cmd_buffer->device->info,
5595 image, VK_IMAGE_ASPECT_COLOR_BIT,
5596 att_state->current_layout);
5597
5598 /* If any clear color was used, flush it down the aux surfaces. If we
5599 * don't do it now using the view's format we might use the clear
5600 * color incorrectly in the following resolves (for example with an
5601 * SRGB view & a UNORM image).
5602 */
5603 if (fast_clear_type != ANV_FAST_CLEAR_NONE) {
5604 anv_perf_warn(cmd_buffer->device, iview,
5605 "Doing a partial resolve to get rid of clear color at the "
5606 "end of a renderpass due to an image/view format mismatch");
5607
5608 uint32_t base_layer, layer_count;
5609 if (image->type == VK_IMAGE_TYPE_3D) {
5610 base_layer = 0;
5611 layer_count = anv_minify(iview->image->extent.depth,
5612 iview->planes[0].isl.base_level);
5613 } else {
5614 base_layer = iview->planes[0].isl.base_array_layer;
5615 layer_count = fb->layers;
5616 }
5617
5618 for (uint32_t a = 0; a < layer_count; a++) {
5619 uint32_t array_layer = base_layer + a;
5620 if (image->samples == 1) {
5621 anv_cmd_predicated_ccs_resolve(cmd_buffer, image,
5622 iview->planes[0].isl.format,
5623 iview->planes[0].isl.swizzle,
5624 VK_IMAGE_ASPECT_COLOR_BIT,
5625 iview->planes[0].isl.base_level,
5626 array_layer,
5627 ISL_AUX_OP_PARTIAL_RESOLVE,
5628 ANV_FAST_CLEAR_NONE);
5629 } else {
5630 anv_cmd_predicated_mcs_resolve(cmd_buffer, image,
5631 iview->planes[0].isl.format,
5632 iview->planes[0].isl.swizzle,
5633 VK_IMAGE_ASPECT_COLOR_BIT,
5634 base_layer,
5635 ISL_AUX_OP_PARTIAL_RESOLVE,
5636 ANV_FAST_CLEAR_NONE);
5637 }
5638 }
5639 }
5640 }
5641
5642 /* Transition the image into the final layout for this render pass */
5643 VkImageLayout target_layout =
5644 cmd_state->pass->attachments[a].final_layout;
5645 VkImageLayout target_stencil_layout =
5646 cmd_state->pass->attachments[a].stencil_final_layout;
5647
5648 uint32_t base_layer, layer_count;
5649 if (image->type == VK_IMAGE_TYPE_3D) {
5650 base_layer = 0;
5651 layer_count = anv_minify(iview->image->extent.depth,
5652 iview->planes[0].isl.base_level);
5653 } else {
5654 base_layer = iview->planes[0].isl.base_array_layer;
5655 layer_count = fb->layers;
5656 }
5657
5658 if (image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
5659 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
5660 transition_color_buffer(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
5661 iview->planes[0].isl.base_level, 1,
5662 base_layer, layer_count,
5663 att_state->current_layout, target_layout);
5664 }
5665
5666 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
5667 transition_depth_buffer(cmd_buffer, image,
5668 base_layer, layer_count,
5669 att_state->current_layout, target_layout);
5670 }
5671
5672 if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
5673 transition_stencil_buffer(cmd_buffer, image,
5674 iview->planes[0].isl.base_level, 1,
5675 base_layer, layer_count,
5676 att_state->current_stencil_layout,
5677 target_stencil_layout);
5678 }
5679 }
5680
5681 /* Accumulate any subpass flushes that need to happen after the subpass.
5682 * Yes, they do get accumulated twice in the NextSubpass case but since
5683 * genX_CmdNextSubpass just calls end/begin back-to-back, we just end up
5684 * ORing the bits in twice so it's harmless.
5685 */
5686 cmd_buffer->state.pending_pipe_bits |=
5687 cmd_buffer->state.pass->subpass_flushes[subpass_id + 1];
5688 }
5689
5690 void genX(CmdBeginRenderPass)(
5691 VkCommandBuffer commandBuffer,
5692 const VkRenderPassBeginInfo* pRenderPassBegin,
5693 VkSubpassContents contents)
5694 {
5695 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5696 ANV_FROM_HANDLE(anv_render_pass, pass, pRenderPassBegin->renderPass);
5697 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, pRenderPassBegin->framebuffer);
5698
5699 cmd_buffer->state.framebuffer = framebuffer;
5700 cmd_buffer->state.pass = pass;
5701 cmd_buffer->state.render_area = pRenderPassBegin->renderArea;
5702 VkResult result =
5703 genX(cmd_buffer_setup_attachments)(cmd_buffer, pass, pRenderPassBegin);
5704
5705 /* If we failed to setup the attachments we should not try to go further */
5706 if (result != VK_SUCCESS) {
5707 assert(anv_batch_has_error(&cmd_buffer->batch));
5708 return;
5709 }
5710
5711 genX(flush_pipeline_select_3d)(cmd_buffer);
5712
5713 cmd_buffer_begin_subpass(cmd_buffer, 0);
5714 }
5715
5716 void genX(CmdBeginRenderPass2)(
5717 VkCommandBuffer commandBuffer,
5718 const VkRenderPassBeginInfo* pRenderPassBeginInfo,
5719 const VkSubpassBeginInfoKHR* pSubpassBeginInfo)
5720 {
5721 genX(CmdBeginRenderPass)(commandBuffer, pRenderPassBeginInfo,
5722 pSubpassBeginInfo->contents);
5723 }
5724
5725 void genX(CmdNextSubpass)(
5726 VkCommandBuffer commandBuffer,
5727 VkSubpassContents contents)
5728 {
5729 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5730
5731 if (anv_batch_has_error(&cmd_buffer->batch))
5732 return;
5733
5734 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
5735
5736 uint32_t prev_subpass = anv_get_subpass_id(&cmd_buffer->state);
5737 cmd_buffer_end_subpass(cmd_buffer);
5738 cmd_buffer_begin_subpass(cmd_buffer, prev_subpass + 1);
5739 }
5740
5741 void genX(CmdNextSubpass2)(
5742 VkCommandBuffer commandBuffer,
5743 const VkSubpassBeginInfoKHR* pSubpassBeginInfo,
5744 const VkSubpassEndInfoKHR* pSubpassEndInfo)
5745 {
5746 genX(CmdNextSubpass)(commandBuffer, pSubpassBeginInfo->contents);
5747 }
5748
5749 void genX(CmdEndRenderPass)(
5750 VkCommandBuffer commandBuffer)
5751 {
5752 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5753
5754 if (anv_batch_has_error(&cmd_buffer->batch))
5755 return;
5756
5757 cmd_buffer_end_subpass(cmd_buffer);
5758
5759 cmd_buffer->state.hiz_enabled = false;
5760
5761 #ifndef NDEBUG
5762 anv_dump_add_attachments(cmd_buffer);
5763 #endif
5764
5765 /* Remove references to render pass specific state. This enables us to
5766 * detect whether or not we're in a renderpass.
5767 */
5768 cmd_buffer->state.framebuffer = NULL;
5769 cmd_buffer->state.pass = NULL;
5770 cmd_buffer->state.subpass = NULL;
5771 }
5772
5773 void genX(CmdEndRenderPass2)(
5774 VkCommandBuffer commandBuffer,
5775 const VkSubpassEndInfoKHR* pSubpassEndInfo)
5776 {
5777 genX(CmdEndRenderPass)(commandBuffer);
5778 }
5779
5780 void
5781 genX(cmd_emit_conditional_render_predicate)(struct anv_cmd_buffer *cmd_buffer)
5782 {
5783 #if GEN_GEN >= 8 || GEN_IS_HASWELL
5784 struct gen_mi_builder b;
5785 gen_mi_builder_init(&b, &cmd_buffer->batch);
5786
5787 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0),
5788 gen_mi_reg32(ANV_PREDICATE_RESULT_REG));
5789 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
5790
5791 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
5792 mip.LoadOperation = LOAD_LOADINV;
5793 mip.CombineOperation = COMBINE_SET;
5794 mip.CompareOperation = COMPARE_SRCS_EQUAL;
5795 }
5796 #endif
5797 }
5798
5799 #if GEN_GEN >= 8 || GEN_IS_HASWELL
5800 void genX(CmdBeginConditionalRenderingEXT)(
5801 VkCommandBuffer commandBuffer,
5802 const VkConditionalRenderingBeginInfoEXT* pConditionalRenderingBegin)
5803 {
5804 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5805 ANV_FROM_HANDLE(anv_buffer, buffer, pConditionalRenderingBegin->buffer);
5806 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
5807 struct anv_address value_address =
5808 anv_address_add(buffer->address, pConditionalRenderingBegin->offset);
5809
5810 const bool isInverted = pConditionalRenderingBegin->flags &
5811 VK_CONDITIONAL_RENDERING_INVERTED_BIT_EXT;
5812
5813 cmd_state->conditional_render_enabled = true;
5814
5815 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
5816
5817 struct gen_mi_builder b;
5818 gen_mi_builder_init(&b, &cmd_buffer->batch);
5819
5820 /* Section 19.4 of the Vulkan 1.1.85 spec says:
5821 *
5822 * If the value of the predicate in buffer memory changes
5823 * while conditional rendering is active, the rendering commands
5824 * may be discarded in an implementation-dependent way.
5825 * Some implementations may latch the value of the predicate
5826 * upon beginning conditional rendering while others
5827 * may read it before every rendering command.
5828 *
5829 * So it's perfectly fine to read a value from the buffer once.
5830 */
5831 struct gen_mi_value value = gen_mi_mem32(value_address);
5832
5833 /* Precompute predicate result, it is necessary to support secondary
5834 * command buffers since it is unknown if conditional rendering is
5835 * inverted when populating them.
5836 */
5837 gen_mi_store(&b, gen_mi_reg64(ANV_PREDICATE_RESULT_REG),
5838 isInverted ? gen_mi_uge(&b, gen_mi_imm(0), value) :
5839 gen_mi_ult(&b, gen_mi_imm(0), value));
5840 }
5841
5842 void genX(CmdEndConditionalRenderingEXT)(
5843 VkCommandBuffer commandBuffer)
5844 {
5845 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5846 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
5847
5848 cmd_state->conditional_render_enabled = false;
5849 }
5850 #endif
5851
5852 /* Set of stage bits for which are pipelined, i.e. they get queued by the
5853 * command streamer for later execution.
5854 */
5855 #define ANV_PIPELINE_STAGE_PIPELINED_BITS \
5856 (VK_PIPELINE_STAGE_VERTEX_INPUT_BIT | \
5857 VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | \
5858 VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT | \
5859 VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT | \
5860 VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT | \
5861 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | \
5862 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | \
5863 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | \
5864 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | \
5865 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT | \
5866 VK_PIPELINE_STAGE_TRANSFER_BIT | \
5867 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT | \
5868 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT | \
5869 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT)
5870
5871 void genX(CmdSetEvent)(
5872 VkCommandBuffer commandBuffer,
5873 VkEvent _event,
5874 VkPipelineStageFlags stageMask)
5875 {
5876 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5877 ANV_FROM_HANDLE(anv_event, event, _event);
5878
5879 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_POST_SYNC_BIT;
5880 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
5881
5882 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
5883 if (stageMask & ANV_PIPELINE_STAGE_PIPELINED_BITS) {
5884 pc.StallAtPixelScoreboard = true;
5885 pc.CommandStreamerStallEnable = true;
5886 }
5887
5888 pc.DestinationAddressType = DAT_PPGTT,
5889 pc.PostSyncOperation = WriteImmediateData,
5890 pc.Address = (struct anv_address) {
5891 cmd_buffer->device->dynamic_state_pool.block_pool.bo,
5892 event->state.offset
5893 };
5894 pc.ImmediateData = VK_EVENT_SET;
5895 }
5896 }
5897
5898 void genX(CmdResetEvent)(
5899 VkCommandBuffer commandBuffer,
5900 VkEvent _event,
5901 VkPipelineStageFlags stageMask)
5902 {
5903 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5904 ANV_FROM_HANDLE(anv_event, event, _event);
5905
5906 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_POST_SYNC_BIT;
5907 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
5908
5909 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
5910 if (stageMask & ANV_PIPELINE_STAGE_PIPELINED_BITS) {
5911 pc.StallAtPixelScoreboard = true;
5912 pc.CommandStreamerStallEnable = true;
5913 }
5914
5915 pc.DestinationAddressType = DAT_PPGTT;
5916 pc.PostSyncOperation = WriteImmediateData;
5917 pc.Address = (struct anv_address) {
5918 cmd_buffer->device->dynamic_state_pool.block_pool.bo,
5919 event->state.offset
5920 };
5921 pc.ImmediateData = VK_EVENT_RESET;
5922 }
5923 }
5924
5925 void genX(CmdWaitEvents)(
5926 VkCommandBuffer commandBuffer,
5927 uint32_t eventCount,
5928 const VkEvent* pEvents,
5929 VkPipelineStageFlags srcStageMask,
5930 VkPipelineStageFlags destStageMask,
5931 uint32_t memoryBarrierCount,
5932 const VkMemoryBarrier* pMemoryBarriers,
5933 uint32_t bufferMemoryBarrierCount,
5934 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
5935 uint32_t imageMemoryBarrierCount,
5936 const VkImageMemoryBarrier* pImageMemoryBarriers)
5937 {
5938 #if GEN_GEN >= 8
5939 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5940
5941 for (uint32_t i = 0; i < eventCount; i++) {
5942 ANV_FROM_HANDLE(anv_event, event, pEvents[i]);
5943
5944 anv_batch_emit(&cmd_buffer->batch, GENX(MI_SEMAPHORE_WAIT), sem) {
5945 sem.WaitMode = PollingMode,
5946 sem.CompareOperation = COMPARE_SAD_EQUAL_SDD,
5947 sem.SemaphoreDataDword = VK_EVENT_SET,
5948 sem.SemaphoreAddress = (struct anv_address) {
5949 cmd_buffer->device->dynamic_state_pool.block_pool.bo,
5950 event->state.offset
5951 };
5952 }
5953 }
5954 #else
5955 anv_finishme("Implement events on gen7");
5956 #endif
5957
5958 genX(CmdPipelineBarrier)(commandBuffer, srcStageMask, destStageMask,
5959 false, /* byRegion */
5960 memoryBarrierCount, pMemoryBarriers,
5961 bufferMemoryBarrierCount, pBufferMemoryBarriers,
5962 imageMemoryBarrierCount, pImageMemoryBarriers);
5963 }
5964
5965 VkResult genX(CmdSetPerformanceOverrideINTEL)(
5966 VkCommandBuffer commandBuffer,
5967 const VkPerformanceOverrideInfoINTEL* pOverrideInfo)
5968 {
5969 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
5970
5971 switch (pOverrideInfo->type) {
5972 case VK_PERFORMANCE_OVERRIDE_TYPE_NULL_HARDWARE_INTEL: {
5973 uint32_t dw;
5974
5975 #if GEN_GEN >= 9
5976 anv_pack_struct(&dw, GENX(CS_DEBUG_MODE2),
5977 ._3DRenderingInstructionDisable = pOverrideInfo->enable,
5978 .MediaInstructionDisable = pOverrideInfo->enable,
5979 ._3DRenderingInstructionDisableMask = true,
5980 .MediaInstructionDisableMask = true);
5981 emit_lri(&cmd_buffer->batch, GENX(CS_DEBUG_MODE2_num), dw);
5982 #else
5983 anv_pack_struct(&dw, GENX(INSTPM),
5984 ._3DRenderingInstructionDisable = pOverrideInfo->enable,
5985 .MediaInstructionDisable = pOverrideInfo->enable,
5986 ._3DRenderingInstructionDisableMask = true,
5987 .MediaInstructionDisableMask = true);
5988 emit_lri(&cmd_buffer->batch, GENX(INSTPM_num), dw);
5989 #endif
5990 break;
5991 }
5992
5993 case VK_PERFORMANCE_OVERRIDE_TYPE_FLUSH_GPU_CACHES_INTEL:
5994 if (pOverrideInfo->enable) {
5995 /* FLUSH ALL THE THINGS! As requested by the MDAPI team. */
5996 cmd_buffer->state.pending_pipe_bits |=
5997 ANV_PIPE_FLUSH_BITS |
5998 ANV_PIPE_INVALIDATE_BITS;
5999 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
6000 }
6001 break;
6002
6003 default:
6004 unreachable("Invalid override");
6005 }
6006
6007 return VK_SUCCESS;
6008 }
6009
6010 VkResult genX(CmdSetPerformanceStreamMarkerINTEL)(
6011 VkCommandBuffer commandBuffer,
6012 const VkPerformanceStreamMarkerInfoINTEL* pMarkerInfo)
6013 {
6014 /* TODO: Waiting on the register to write, might depend on generation. */
6015
6016 return VK_SUCCESS;
6017 }