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