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