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