anv: Use gen_mi_builder for indirect dispatch
[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_l3_config.h"
33 #include "genxml/gen_macros.h"
34 #include "genxml/genX_pack.h"
35
36 /* We reserve GPR 14 and 15 for conditional rendering */
37 #define GEN_MI_BUILDER_NUM_ALLOC_GPRS 14
38 #define __gen_get_batch_dwords anv_batch_emit_dwords
39 #define __gen_address_offset anv_address_add
40 #include "common/gen_mi_builder.h"
41
42 static void
43 emit_lrm(struct anv_batch *batch, uint32_t reg, struct anv_address addr)
44 {
45 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
46 lrm.RegisterAddress = reg;
47 lrm.MemoryAddress = addr;
48 }
49 }
50
51 static void
52 emit_lri(struct anv_batch *batch, uint32_t reg, uint32_t imm)
53 {
54 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
55 lri.RegisterOffset = reg;
56 lri.DataDWord = imm;
57 }
58 }
59
60 #if GEN_IS_HASWELL || GEN_GEN >= 8
61 static void
62 emit_lrr(struct anv_batch *batch, uint32_t dst, uint32_t src)
63 {
64 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_REG), lrr) {
65 lrr.SourceRegisterAddress = src;
66 lrr.DestinationRegisterAddress = dst;
67 }
68 }
69 #endif
70
71 void
72 genX(cmd_buffer_emit_state_base_address)(struct anv_cmd_buffer *cmd_buffer)
73 {
74 struct anv_device *device = cmd_buffer->device;
75
76 /* If we are emitting a new state base address we probably need to re-emit
77 * binding tables.
78 */
79 cmd_buffer->state.descriptors_dirty |= ~0;
80
81 /* Emit a render target cache flush.
82 *
83 * This isn't documented anywhere in the PRM. However, it seems to be
84 * necessary prior to changing the surface state base adress. Without
85 * this, we get GPU hangs when using multi-level command buffers which
86 * clear depth, reset state base address, and then go render stuff.
87 */
88 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
89 pc.DCFlushEnable = true;
90 pc.RenderTargetCacheFlushEnable = true;
91 pc.CommandStreamerStallEnable = true;
92 }
93
94 anv_batch_emit(&cmd_buffer->batch, GENX(STATE_BASE_ADDRESS), sba) {
95 sba.GeneralStateBaseAddress = (struct anv_address) { NULL, 0 };
96 sba.GeneralStateMOCS = GENX(MOCS);
97 sba.GeneralStateBaseAddressModifyEnable = true;
98
99 sba.SurfaceStateBaseAddress =
100 anv_cmd_buffer_surface_base_address(cmd_buffer);
101 sba.SurfaceStateMOCS = GENX(MOCS);
102 sba.SurfaceStateBaseAddressModifyEnable = true;
103
104 sba.DynamicStateBaseAddress =
105 (struct anv_address) { device->dynamic_state_pool.block_pool.bo, 0 };
106 sba.DynamicStateMOCS = GENX(MOCS);
107 sba.DynamicStateBaseAddressModifyEnable = true;
108
109 sba.IndirectObjectBaseAddress = (struct anv_address) { NULL, 0 };
110 sba.IndirectObjectMOCS = GENX(MOCS);
111 sba.IndirectObjectBaseAddressModifyEnable = true;
112
113 sba.InstructionBaseAddress =
114 (struct anv_address) { device->instruction_state_pool.block_pool.bo, 0 };
115 sba.InstructionMOCS = GENX(MOCS);
116 sba.InstructionBaseAddressModifyEnable = true;
117
118 # if (GEN_GEN >= 8)
119 /* Broadwell requires that we specify a buffer size for a bunch of
120 * these fields. However, since we will be growing the BO's live, we
121 * just set them all to the maximum.
122 */
123 sba.GeneralStateBufferSize = 0xfffff;
124 sba.GeneralStateBufferSizeModifyEnable = true;
125 sba.DynamicStateBufferSize = 0xfffff;
126 sba.DynamicStateBufferSizeModifyEnable = true;
127 sba.IndirectObjectBufferSize = 0xfffff;
128 sba.IndirectObjectBufferSizeModifyEnable = true;
129 sba.InstructionBufferSize = 0xfffff;
130 sba.InstructionBuffersizeModifyEnable = true;
131 # endif
132 # if (GEN_GEN >= 9)
133 sba.BindlessSurfaceStateBaseAddress = (struct anv_address) { NULL, 0 };
134 sba.BindlessSurfaceStateMOCS = GENX(MOCS);
135 sba.BindlessSurfaceStateBaseAddressModifyEnable = true;
136 sba.BindlessSurfaceStateSize = 0;
137 # endif
138 # if (GEN_GEN >= 10)
139 sba.BindlessSamplerStateBaseAddress = (struct anv_address) { NULL, 0 };
140 sba.BindlessSamplerStateMOCS = GENX(MOCS);
141 sba.BindlessSamplerStateBaseAddressModifyEnable = true;
142 sba.BindlessSamplerStateBufferSize = 0;
143 # endif
144 }
145
146 /* After re-setting the surface state base address, we have to do some
147 * cache flusing so that the sampler engine will pick up the new
148 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
149 * Shared Function > 3D Sampler > State > State Caching (page 96):
150 *
151 * Coherency with system memory in the state cache, like the texture
152 * cache is handled partially by software. It is expected that the
153 * command stream or shader will issue Cache Flush operation or
154 * Cache_Flush sampler message to ensure that the L1 cache remains
155 * coherent with system memory.
156 *
157 * [...]
158 *
159 * Whenever the value of the Dynamic_State_Base_Addr,
160 * Surface_State_Base_Addr are altered, the L1 state cache must be
161 * invalidated to ensure the new surface or sampler state is fetched
162 * from system memory.
163 *
164 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
165 * which, according the PIPE_CONTROL instruction documentation in the
166 * Broadwell PRM:
167 *
168 * Setting this bit is independent of any other bit in this packet.
169 * This bit controls the invalidation of the L1 and L2 state caches
170 * at the top of the pipe i.e. at the parsing time.
171 *
172 * Unfortunately, experimentation seems to indicate that state cache
173 * invalidation through a PIPE_CONTROL does nothing whatsoever in
174 * regards to surface state and binding tables. In stead, it seems that
175 * invalidating the texture cache is what is actually needed.
176 *
177 * XXX: As far as we have been able to determine through
178 * experimentation, shows that flush the texture cache appears to be
179 * sufficient. The theory here is that all of the sampling/rendering
180 * units cache the binding table in the texture cache. However, we have
181 * yet to be able to actually confirm this.
182 */
183 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
184 pc.TextureCacheInvalidationEnable = true;
185 pc.ConstantCacheInvalidationEnable = true;
186 pc.StateCacheInvalidationEnable = true;
187 }
188 }
189
190 static void
191 add_surface_reloc(struct anv_cmd_buffer *cmd_buffer,
192 struct anv_state state, struct anv_address addr)
193 {
194 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
195
196 VkResult result =
197 anv_reloc_list_add(&cmd_buffer->surface_relocs, &cmd_buffer->pool->alloc,
198 state.offset + isl_dev->ss.addr_offset,
199 addr.bo, addr.offset);
200 if (result != VK_SUCCESS)
201 anv_batch_set_error(&cmd_buffer->batch, result);
202 }
203
204 static void
205 add_surface_state_relocs(struct anv_cmd_buffer *cmd_buffer,
206 struct anv_surface_state state)
207 {
208 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
209
210 assert(!anv_address_is_null(state.address));
211 add_surface_reloc(cmd_buffer, state.state, state.address);
212
213 if (!anv_address_is_null(state.aux_address)) {
214 VkResult result =
215 anv_reloc_list_add(&cmd_buffer->surface_relocs,
216 &cmd_buffer->pool->alloc,
217 state.state.offset + isl_dev->ss.aux_addr_offset,
218 state.aux_address.bo, state.aux_address.offset);
219 if (result != VK_SUCCESS)
220 anv_batch_set_error(&cmd_buffer->batch, result);
221 }
222
223 if (!anv_address_is_null(state.clear_address)) {
224 VkResult result =
225 anv_reloc_list_add(&cmd_buffer->surface_relocs,
226 &cmd_buffer->pool->alloc,
227 state.state.offset +
228 isl_dev->ss.clear_color_state_offset,
229 state.clear_address.bo, state.clear_address.offset);
230 if (result != VK_SUCCESS)
231 anv_batch_set_error(&cmd_buffer->batch, result);
232 }
233 }
234
235 static void
236 color_attachment_compute_aux_usage(struct anv_device * device,
237 struct anv_cmd_state * cmd_state,
238 uint32_t att, VkRect2D render_area,
239 union isl_color_value *fast_clear_color)
240 {
241 struct anv_attachment_state *att_state = &cmd_state->attachments[att];
242 struct anv_image_view *iview = cmd_state->framebuffer->attachments[att];
243
244 assert(iview->n_planes == 1);
245
246 if (iview->planes[0].isl.base_array_layer >=
247 anv_image_aux_layers(iview->image, VK_IMAGE_ASPECT_COLOR_BIT,
248 iview->planes[0].isl.base_level)) {
249 /* There is no aux buffer which corresponds to the level and layer(s)
250 * being accessed.
251 */
252 att_state->aux_usage = ISL_AUX_USAGE_NONE;
253 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
254 att_state->fast_clear = false;
255 return;
256 }
257
258 att_state->aux_usage =
259 anv_layout_to_aux_usage(&device->info, iview->image,
260 VK_IMAGE_ASPECT_COLOR_BIT,
261 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
262
263 /* If we don't have aux, then we should have returned early in the layer
264 * check above. If we got here, we must have something.
265 */
266 assert(att_state->aux_usage != ISL_AUX_USAGE_NONE);
267
268 if (att_state->aux_usage == ISL_AUX_USAGE_CCS_E ||
269 att_state->aux_usage == ISL_AUX_USAGE_MCS) {
270 att_state->input_aux_usage = att_state->aux_usage;
271 } else {
272 /* From the Sky Lake PRM, RENDER_SURFACE_STATE::AuxiliarySurfaceMode:
273 *
274 * "If Number of Multisamples is MULTISAMPLECOUNT_1, AUX_CCS_D
275 * setting is only allowed if Surface Format supported for Fast
276 * Clear. In addition, if the surface is bound to the sampling
277 * engine, Surface Format must be supported for Render Target
278 * Compression for surfaces bound to the sampling engine."
279 *
280 * In other words, we can only sample from a fast-cleared image if it
281 * also supports color compression.
282 */
283 if (isl_format_supports_ccs_e(&device->info, iview->planes[0].isl.format)) {
284 att_state->input_aux_usage = ISL_AUX_USAGE_CCS_D;
285
286 /* While fast-clear resolves and partial resolves are fairly cheap in the
287 * case where you render to most of the pixels, full resolves are not
288 * because they potentially involve reading and writing the entire
289 * framebuffer. If we can't texture with CCS_E, we should leave it off and
290 * limit ourselves to fast clears.
291 */
292 if (cmd_state->pass->attachments[att].first_subpass_layout ==
293 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
294 anv_perf_warn(device->instance, iview->image,
295 "Not temporarily enabling CCS_E.");
296 }
297 } else {
298 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
299 }
300 }
301
302 assert(iview->image->planes[0].aux_surface.isl.usage &
303 (ISL_SURF_USAGE_CCS_BIT | ISL_SURF_USAGE_MCS_BIT));
304
305 union isl_color_value clear_color = {};
306 anv_clear_color_from_att_state(&clear_color, att_state, iview);
307
308 att_state->clear_color_is_zero_one =
309 isl_color_value_is_zero_one(clear_color, iview->planes[0].isl.format);
310 att_state->clear_color_is_zero =
311 isl_color_value_is_zero(clear_color, iview->planes[0].isl.format);
312
313 if (att_state->pending_clear_aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
314 /* Start by getting the fast clear type. We use the first subpass
315 * layout here because we don't want to fast-clear if the first subpass
316 * to use the attachment can't handle fast-clears.
317 */
318 enum anv_fast_clear_type fast_clear_type =
319 anv_layout_to_fast_clear_type(&device->info, iview->image,
320 VK_IMAGE_ASPECT_COLOR_BIT,
321 cmd_state->pass->attachments[att].first_subpass_layout);
322 switch (fast_clear_type) {
323 case ANV_FAST_CLEAR_NONE:
324 att_state->fast_clear = false;
325 break;
326 case ANV_FAST_CLEAR_DEFAULT_VALUE:
327 att_state->fast_clear = att_state->clear_color_is_zero;
328 break;
329 case ANV_FAST_CLEAR_ANY:
330 att_state->fast_clear = true;
331 break;
332 }
333
334 /* Potentially, we could do partial fast-clears but doing so has crazy
335 * alignment restrictions. It's easier to just restrict to full size
336 * fast clears for now.
337 */
338 if (render_area.offset.x != 0 ||
339 render_area.offset.y != 0 ||
340 render_area.extent.width != iview->extent.width ||
341 render_area.extent.height != iview->extent.height)
342 att_state->fast_clear = false;
343
344 /* On Broadwell and earlier, we can only handle 0/1 clear colors */
345 if (GEN_GEN <= 8 && !att_state->clear_color_is_zero_one)
346 att_state->fast_clear = false;
347
348 /* We only allow fast clears to the first slice of an image (level 0,
349 * layer 0) and only for the entire slice. This guarantees us that, at
350 * any given time, there is only one clear color on any given image at
351 * any given time. At the time of our testing (Jan 17, 2018), there
352 * were no known applications which would benefit from fast-clearing
353 * more than just the first slice.
354 */
355 if (att_state->fast_clear &&
356 (iview->planes[0].isl.base_level > 0 ||
357 iview->planes[0].isl.base_array_layer > 0)) {
358 anv_perf_warn(device->instance, iview->image,
359 "Rendering with multi-lod or multi-layer framebuffer "
360 "with LOAD_OP_LOAD and baseMipLevel > 0 or "
361 "baseArrayLayer > 0. Not fast clearing.");
362 att_state->fast_clear = false;
363 } else if (att_state->fast_clear && cmd_state->framebuffer->layers > 1) {
364 anv_perf_warn(device->instance, iview->image,
365 "Rendering to a multi-layer framebuffer with "
366 "LOAD_OP_CLEAR. Only fast-clearing the first slice");
367 }
368
369 if (att_state->fast_clear)
370 *fast_clear_color = clear_color;
371 } else {
372 att_state->fast_clear = false;
373 }
374 }
375
376 static void
377 depth_stencil_attachment_compute_aux_usage(struct anv_device *device,
378 struct anv_cmd_state *cmd_state,
379 uint32_t att, VkRect2D render_area)
380 {
381 struct anv_render_pass_attachment *pass_att =
382 &cmd_state->pass->attachments[att];
383 struct anv_attachment_state *att_state = &cmd_state->attachments[att];
384 struct anv_image_view *iview = cmd_state->framebuffer->attachments[att];
385
386 /* These will be initialized after the first subpass transition. */
387 att_state->aux_usage = ISL_AUX_USAGE_NONE;
388 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
389
390 if (GEN_GEN == 7) {
391 /* We don't do any HiZ or depth fast-clears on gen7 yet */
392 att_state->fast_clear = false;
393 return;
394 }
395
396 if (!(att_state->pending_clear_aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
397 /* If we're just clearing stencil, we can always HiZ clear */
398 att_state->fast_clear = true;
399 return;
400 }
401
402 /* Default to false for now */
403 att_state->fast_clear = false;
404
405 /* We must have depth in order to have HiZ */
406 if (!(iview->image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT))
407 return;
408
409 const enum isl_aux_usage first_subpass_aux_usage =
410 anv_layout_to_aux_usage(&device->info, iview->image,
411 VK_IMAGE_ASPECT_DEPTH_BIT,
412 pass_att->first_subpass_layout);
413 if (first_subpass_aux_usage != ISL_AUX_USAGE_HIZ)
414 return;
415
416 if (!blorp_can_hiz_clear_depth(GEN_GEN,
417 iview->planes[0].isl.format,
418 iview->image->samples,
419 render_area.offset.x,
420 render_area.offset.y,
421 render_area.offset.x +
422 render_area.extent.width,
423 render_area.offset.y +
424 render_area.extent.height))
425 return;
426
427 if (att_state->clear_value.depthStencil.depth != ANV_HZ_FC_VAL)
428 return;
429
430 if (GEN_GEN == 8 && anv_can_sample_with_hiz(&device->info, iview->image)) {
431 /* Only gen9+ supports returning ANV_HZ_FC_VAL when sampling a
432 * fast-cleared portion of a HiZ buffer. Testing has revealed that Gen8
433 * only supports returning 0.0f. Gens prior to gen8 do not support this
434 * feature at all.
435 */
436 return;
437 }
438
439 /* If we got here, then we can fast clear */
440 att_state->fast_clear = true;
441 }
442
443 static bool
444 need_input_attachment_state(const struct anv_render_pass_attachment *att)
445 {
446 if (!(att->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
447 return false;
448
449 /* We only allocate input attachment states for color surfaces. Compression
450 * is not yet enabled for depth textures and stencil doesn't allow
451 * compression so we can just use the texture surface state from the view.
452 */
453 return vk_format_is_color(att->format);
454 }
455
456 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
457 * the initial layout is undefined, the HiZ buffer and depth buffer will
458 * represent the same data at the end of this operation.
459 */
460 static void
461 transition_depth_buffer(struct anv_cmd_buffer *cmd_buffer,
462 const struct anv_image *image,
463 VkImageLayout initial_layout,
464 VkImageLayout final_layout)
465 {
466 const bool hiz_enabled = ISL_AUX_USAGE_HIZ ==
467 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
468 VK_IMAGE_ASPECT_DEPTH_BIT, initial_layout);
469 const bool enable_hiz = ISL_AUX_USAGE_HIZ ==
470 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
471 VK_IMAGE_ASPECT_DEPTH_BIT, final_layout);
472
473 enum isl_aux_op hiz_op;
474 if (hiz_enabled && !enable_hiz) {
475 hiz_op = ISL_AUX_OP_FULL_RESOLVE;
476 } else if (!hiz_enabled && enable_hiz) {
477 hiz_op = ISL_AUX_OP_AMBIGUATE;
478 } else {
479 assert(hiz_enabled == enable_hiz);
480 /* If the same buffer will be used, no resolves are necessary. */
481 hiz_op = ISL_AUX_OP_NONE;
482 }
483
484 if (hiz_op != ISL_AUX_OP_NONE)
485 anv_image_hiz_op(cmd_buffer, image, VK_IMAGE_ASPECT_DEPTH_BIT,
486 0, 0, 1, hiz_op);
487 }
488
489 #define MI_PREDICATE_SRC0 0x2400
490 #define MI_PREDICATE_SRC1 0x2408
491 #define MI_PREDICATE_RESULT 0x2418
492
493 static void
494 set_image_compressed_bit(struct anv_cmd_buffer *cmd_buffer,
495 const struct anv_image *image,
496 VkImageAspectFlagBits aspect,
497 uint32_t level,
498 uint32_t base_layer, uint32_t layer_count,
499 bool compressed)
500 {
501 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
502
503 /* We only have compression tracking for CCS_E */
504 if (image->planes[plane].aux_usage != ISL_AUX_USAGE_CCS_E)
505 return;
506
507 for (uint32_t a = 0; a < layer_count; a++) {
508 uint32_t layer = base_layer + a;
509 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
510 sdi.Address = anv_image_get_compression_state_addr(cmd_buffer->device,
511 image, aspect,
512 level, layer);
513 sdi.ImmediateData = compressed ? UINT32_MAX : 0;
514 }
515 }
516 }
517
518 static void
519 set_image_fast_clear_state(struct anv_cmd_buffer *cmd_buffer,
520 const struct anv_image *image,
521 VkImageAspectFlagBits aspect,
522 enum anv_fast_clear_type fast_clear)
523 {
524 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
525 sdi.Address = anv_image_get_fast_clear_type_addr(cmd_buffer->device,
526 image, aspect);
527 sdi.ImmediateData = fast_clear;
528 }
529
530 /* Whenever we have fast-clear, we consider that slice to be compressed.
531 * This makes building predicates much easier.
532 */
533 if (fast_clear != ANV_FAST_CLEAR_NONE)
534 set_image_compressed_bit(cmd_buffer, image, aspect, 0, 0, 1, true);
535 }
536
537 #if GEN_IS_HASWELL || GEN_GEN >= 8
538 static inline uint32_t
539 mi_alu(uint32_t opcode, uint32_t operand1, uint32_t operand2)
540 {
541 struct GENX(MI_MATH_ALU_INSTRUCTION) instr = {
542 .ALUOpcode = opcode,
543 .Operand1 = operand1,
544 .Operand2 = operand2,
545 };
546
547 uint32_t dw;
548 GENX(MI_MATH_ALU_INSTRUCTION_pack)(NULL, &dw, &instr);
549
550 return dw;
551 }
552 #endif
553
554 #define CS_GPR(n) (0x2600 + (n) * 8)
555
556 /* This is only really practical on haswell and above because it requires
557 * MI math in order to get it correct.
558 */
559 #if GEN_GEN >= 8 || GEN_IS_HASWELL
560 static void
561 anv_cmd_compute_resolve_predicate(struct anv_cmd_buffer *cmd_buffer,
562 const struct anv_image *image,
563 VkImageAspectFlagBits aspect,
564 uint32_t level, uint32_t array_layer,
565 enum isl_aux_op resolve_op,
566 enum anv_fast_clear_type fast_clear_supported)
567 {
568 struct gen_mi_builder b;
569 gen_mi_builder_init(&b, &cmd_buffer->batch);
570
571 const struct gen_mi_value fast_clear_type =
572 gen_mi_mem32(anv_image_get_fast_clear_type_addr(cmd_buffer->device,
573 image, aspect));
574
575 if (resolve_op == ISL_AUX_OP_FULL_RESOLVE) {
576 /* In this case, we're doing a full resolve which means we want the
577 * resolve to happen if any compression (including fast-clears) is
578 * present.
579 *
580 * In order to simplify the logic a bit, we make the assumption that,
581 * if the first slice has been fast-cleared, it is also marked as
582 * compressed. See also set_image_fast_clear_state.
583 */
584 const struct gen_mi_value compression_state =
585 gen_mi_mem32(anv_image_get_compression_state_addr(cmd_buffer->device,
586 image, aspect,
587 level, array_layer));
588 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0),
589 compression_state);
590 gen_mi_store(&b, compression_state, gen_mi_imm(0));
591
592 if (level == 0 && array_layer == 0) {
593 /* If the predicate is true, we want to write 0 to the fast clear type
594 * and, if it's false, leave it alone. We can do this by writing
595 *
596 * clear_type = clear_type & ~predicate;
597 */
598 struct gen_mi_value new_fast_clear_type =
599 gen_mi_iand(&b, fast_clear_type,
600 gen_mi_inot(&b, gen_mi_reg64(MI_PREDICATE_SRC0)));
601 gen_mi_store(&b, fast_clear_type, new_fast_clear_type);
602 }
603 } else if (level == 0 && array_layer == 0) {
604 /* In this case, we are doing a partial resolve to get rid of fast-clear
605 * colors. We don't care about the compression state but we do care
606 * about how much fast clear is allowed by the final layout.
607 */
608 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
609 assert(fast_clear_supported < ANV_FAST_CLEAR_ANY);
610
611 /* We need to compute (fast_clear_supported < image->fast_clear) */
612 struct gen_mi_value pred =
613 gen_mi_ult(&b, gen_mi_imm(fast_clear_supported), fast_clear_type);
614 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0),
615 gen_mi_value_ref(&b, pred));
616
617 /* If the predicate is true, we want to write 0 to the fast clear type
618 * and, if it's false, leave it alone. We can do this by writing
619 *
620 * clear_type = clear_type & ~predicate;
621 */
622 struct gen_mi_value new_fast_clear_type =
623 gen_mi_iand(&b, fast_clear_type, gen_mi_inot(&b, pred));
624 gen_mi_store(&b, fast_clear_type, new_fast_clear_type);
625 } else {
626 /* In this case, we're trying to do a partial resolve on a slice that
627 * doesn't have clear color. There's nothing to do.
628 */
629 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
630 return;
631 }
632
633 /* Set src1 to 0 and use a != condition */
634 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
635
636 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
637 mip.LoadOperation = LOAD_LOADINV;
638 mip.CombineOperation = COMBINE_SET;
639 mip.CompareOperation = COMPARE_SRCS_EQUAL;
640 }
641 }
642 #endif /* GEN_GEN >= 8 || GEN_IS_HASWELL */
643
644 #if GEN_GEN <= 8
645 static void
646 anv_cmd_simple_resolve_predicate(struct anv_cmd_buffer *cmd_buffer,
647 const struct anv_image *image,
648 VkImageAspectFlagBits aspect,
649 uint32_t level, uint32_t array_layer,
650 enum isl_aux_op resolve_op,
651 enum anv_fast_clear_type fast_clear_supported)
652 {
653 struct gen_mi_builder b;
654 gen_mi_builder_init(&b, &cmd_buffer->batch);
655
656 struct gen_mi_value fast_clear_type_mem =
657 gen_mi_mem32(anv_image_get_fast_clear_type_addr(cmd_buffer->device,
658 image, aspect));
659
660 /* This only works for partial resolves and only when the clear color is
661 * all or nothing. On the upside, this emits less command streamer code
662 * and works on Ivybridge and Bay Trail.
663 */
664 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
665 assert(fast_clear_supported != ANV_FAST_CLEAR_ANY);
666
667 /* We don't support fast clears on anything other than the first slice. */
668 if (level > 0 || array_layer > 0)
669 return;
670
671 /* On gen8, we don't have a concept of default clear colors because we
672 * can't sample from CCS surfaces. It's enough to just load the fast clear
673 * state into the predicate register.
674 */
675 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0), fast_clear_type_mem);
676 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
677 gen_mi_store(&b, fast_clear_type_mem, gen_mi_imm(0));
678
679 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
680 mip.LoadOperation = LOAD_LOADINV;
681 mip.CombineOperation = COMBINE_SET;
682 mip.CompareOperation = COMPARE_SRCS_EQUAL;
683 }
684 }
685 #endif /* GEN_GEN <= 8 */
686
687 static void
688 anv_cmd_predicated_ccs_resolve(struct anv_cmd_buffer *cmd_buffer,
689 const struct anv_image *image,
690 enum isl_format format,
691 VkImageAspectFlagBits aspect,
692 uint32_t level, uint32_t array_layer,
693 enum isl_aux_op resolve_op,
694 enum anv_fast_clear_type fast_clear_supported)
695 {
696 const uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
697
698 #if GEN_GEN >= 9
699 anv_cmd_compute_resolve_predicate(cmd_buffer, image,
700 aspect, level, array_layer,
701 resolve_op, fast_clear_supported);
702 #else /* GEN_GEN <= 8 */
703 anv_cmd_simple_resolve_predicate(cmd_buffer, image,
704 aspect, level, array_layer,
705 resolve_op, fast_clear_supported);
706 #endif
707
708 /* CCS_D only supports full resolves and BLORP will assert on us if we try
709 * to do a partial resolve on a CCS_D surface.
710 */
711 if (resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE &&
712 image->planes[plane].aux_usage == ISL_AUX_USAGE_NONE)
713 resolve_op = ISL_AUX_OP_FULL_RESOLVE;
714
715 anv_image_ccs_op(cmd_buffer, image, format, aspect, level,
716 array_layer, 1, resolve_op, NULL, true);
717 }
718
719 static void
720 anv_cmd_predicated_mcs_resolve(struct anv_cmd_buffer *cmd_buffer,
721 const struct anv_image *image,
722 enum isl_format format,
723 VkImageAspectFlagBits aspect,
724 uint32_t array_layer,
725 enum isl_aux_op resolve_op,
726 enum anv_fast_clear_type fast_clear_supported)
727 {
728 assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
729 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
730
731 #if GEN_GEN >= 8 || GEN_IS_HASWELL
732 anv_cmd_compute_resolve_predicate(cmd_buffer, image,
733 aspect, 0, array_layer,
734 resolve_op, fast_clear_supported);
735
736 anv_image_mcs_op(cmd_buffer, image, format, aspect,
737 array_layer, 1, resolve_op, NULL, true);
738 #else
739 unreachable("MCS resolves are unsupported on Ivybridge and Bay Trail");
740 #endif
741 }
742
743 void
744 genX(cmd_buffer_mark_image_written)(struct anv_cmd_buffer *cmd_buffer,
745 const struct anv_image *image,
746 VkImageAspectFlagBits aspect,
747 enum isl_aux_usage aux_usage,
748 uint32_t level,
749 uint32_t base_layer,
750 uint32_t layer_count)
751 {
752 /* The aspect must be exactly one of the image aspects. */
753 assert(util_bitcount(aspect) == 1 && (aspect & image->aspects));
754
755 /* The only compression types with more than just fast-clears are MCS,
756 * CCS_E, and HiZ. With HiZ we just trust the layout and don't actually
757 * track the current fast-clear and compression state. This leaves us
758 * with just MCS and CCS_E.
759 */
760 if (aux_usage != ISL_AUX_USAGE_CCS_E &&
761 aux_usage != ISL_AUX_USAGE_MCS)
762 return;
763
764 set_image_compressed_bit(cmd_buffer, image, aspect,
765 level, base_layer, layer_count, true);
766 }
767
768 static void
769 init_fast_clear_color(struct anv_cmd_buffer *cmd_buffer,
770 const struct anv_image *image,
771 VkImageAspectFlagBits aspect)
772 {
773 assert(cmd_buffer && image);
774 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
775
776 set_image_fast_clear_state(cmd_buffer, image, aspect,
777 ANV_FAST_CLEAR_NONE);
778
779 /* The fast clear value dword(s) will be copied into a surface state object.
780 * Ensure that the restrictions of the fields in the dword(s) are followed.
781 *
782 * CCS buffers on SKL+ can have any value set for the clear colors.
783 */
784 if (image->samples == 1 && GEN_GEN >= 9)
785 return;
786
787 /* Other combinations of auxiliary buffers and platforms require specific
788 * values in the clear value dword(s).
789 */
790 struct anv_address addr =
791 anv_image_get_clear_color_addr(cmd_buffer->device, image, aspect);
792
793 if (GEN_GEN >= 9) {
794 for (unsigned i = 0; i < 4; i++) {
795 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
796 sdi.Address = addr;
797 sdi.Address.offset += i * 4;
798 /* MCS buffers on SKL+ can only have 1/0 clear colors. */
799 assert(image->samples > 1);
800 sdi.ImmediateData = 0;
801 }
802 }
803 } else {
804 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
805 sdi.Address = addr;
806 if (GEN_GEN >= 8 || GEN_IS_HASWELL) {
807 /* Pre-SKL, the dword containing the clear values also contains
808 * other fields, so we need to initialize those fields to match the
809 * values that would be in a color attachment.
810 */
811 sdi.ImmediateData = ISL_CHANNEL_SELECT_RED << 25 |
812 ISL_CHANNEL_SELECT_GREEN << 22 |
813 ISL_CHANNEL_SELECT_BLUE << 19 |
814 ISL_CHANNEL_SELECT_ALPHA << 16;
815 } else if (GEN_GEN == 7) {
816 /* On IVB, the dword containing the clear values also contains
817 * other fields that must be zero or can be zero.
818 */
819 sdi.ImmediateData = 0;
820 }
821 }
822 }
823 }
824
825 /* Copy the fast-clear value dword(s) between a surface state object and an
826 * image's fast clear state buffer.
827 */
828 static void
829 genX(copy_fast_clear_dwords)(struct anv_cmd_buffer *cmd_buffer,
830 struct anv_state surface_state,
831 const struct anv_image *image,
832 VkImageAspectFlagBits aspect,
833 bool copy_from_surface_state)
834 {
835 assert(cmd_buffer && image);
836 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
837
838 struct anv_address ss_clear_addr = {
839 .bo = cmd_buffer->device->surface_state_pool.block_pool.bo,
840 .offset = surface_state.offset +
841 cmd_buffer->device->isl_dev.ss.clear_value_offset,
842 };
843 const struct anv_address entry_addr =
844 anv_image_get_clear_color_addr(cmd_buffer->device, image, aspect);
845 unsigned copy_size = cmd_buffer->device->isl_dev.ss.clear_value_size;
846
847 if (copy_from_surface_state) {
848 genX(cmd_buffer_mi_memcpy)(cmd_buffer, entry_addr,
849 ss_clear_addr, copy_size);
850 } else {
851 genX(cmd_buffer_mi_memcpy)(cmd_buffer, ss_clear_addr,
852 entry_addr, copy_size);
853
854 /* Updating a surface state object may require that the state cache be
855 * invalidated. From the SKL PRM, Shared Functions -> State -> State
856 * Caching:
857 *
858 * Whenever the RENDER_SURFACE_STATE object in memory pointed to by
859 * the Binding Table Pointer (BTP) and Binding Table Index (BTI) is
860 * modified [...], the L1 state cache must be invalidated to ensure
861 * the new surface or sampler state is fetched from system memory.
862 *
863 * In testing, SKL doesn't actually seem to need this, but HSW does.
864 */
865 cmd_buffer->state.pending_pipe_bits |=
866 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT;
867 }
868 }
869
870 /**
871 * @brief Transitions a color buffer from one layout to another.
872 *
873 * See section 6.1.1. Image Layout Transitions of the Vulkan 1.0.50 spec for
874 * more information.
875 *
876 * @param level_count VK_REMAINING_MIP_LEVELS isn't supported.
877 * @param layer_count VK_REMAINING_ARRAY_LAYERS isn't supported. For 3D images,
878 * this represents the maximum layers to transition at each
879 * specified miplevel.
880 */
881 static void
882 transition_color_buffer(struct anv_cmd_buffer *cmd_buffer,
883 const struct anv_image *image,
884 VkImageAspectFlagBits aspect,
885 const uint32_t base_level, uint32_t level_count,
886 uint32_t base_layer, uint32_t layer_count,
887 VkImageLayout initial_layout,
888 VkImageLayout final_layout)
889 {
890 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
891 /* Validate the inputs. */
892 assert(cmd_buffer);
893 assert(image && image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
894 /* These values aren't supported for simplicity's sake. */
895 assert(level_count != VK_REMAINING_MIP_LEVELS &&
896 layer_count != VK_REMAINING_ARRAY_LAYERS);
897 /* Ensure the subresource range is valid. */
898 UNUSED uint64_t last_level_num = base_level + level_count;
899 const uint32_t max_depth = anv_minify(image->extent.depth, base_level);
900 UNUSED const uint32_t image_layers = MAX2(image->array_size, max_depth);
901 assert((uint64_t)base_layer + layer_count <= image_layers);
902 assert(last_level_num <= image->levels);
903 /* The spec disallows these final layouts. */
904 assert(final_layout != VK_IMAGE_LAYOUT_UNDEFINED &&
905 final_layout != VK_IMAGE_LAYOUT_PREINITIALIZED);
906
907 /* No work is necessary if the layout stays the same or if this subresource
908 * range lacks auxiliary data.
909 */
910 if (initial_layout == final_layout)
911 return;
912
913 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
914
915 if (image->planes[plane].shadow_surface.isl.size_B > 0 &&
916 final_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
917 /* This surface is a linear compressed image with a tiled shadow surface
918 * for texturing. The client is about to use it in READ_ONLY_OPTIMAL so
919 * we need to ensure the shadow copy is up-to-date.
920 */
921 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
922 assert(image->planes[plane].surface.isl.tiling == ISL_TILING_LINEAR);
923 assert(image->planes[plane].shadow_surface.isl.tiling != ISL_TILING_LINEAR);
924 assert(isl_format_is_compressed(image->planes[plane].surface.isl.format));
925 assert(plane == 0);
926 anv_image_copy_to_shadow(cmd_buffer, image,
927 base_level, level_count,
928 base_layer, layer_count);
929 }
930
931 if (base_layer >= anv_image_aux_layers(image, aspect, base_level))
932 return;
933
934 assert(image->tiling == VK_IMAGE_TILING_OPTIMAL);
935
936 if (initial_layout == VK_IMAGE_LAYOUT_UNDEFINED ||
937 initial_layout == VK_IMAGE_LAYOUT_PREINITIALIZED) {
938 /* A subresource in the undefined layout may have been aliased and
939 * populated with any arrangement of bits. Therefore, we must initialize
940 * the related aux buffer and clear buffer entry with desirable values.
941 * An initial layout of PREINITIALIZED is the same as UNDEFINED for
942 * images with VK_IMAGE_TILING_OPTIMAL.
943 *
944 * Initialize the relevant clear buffer entries.
945 */
946 if (base_level == 0 && base_layer == 0)
947 init_fast_clear_color(cmd_buffer, image, aspect);
948
949 /* Initialize the aux buffers to enable correct rendering. In order to
950 * ensure that things such as storage images work correctly, aux buffers
951 * need to be initialized to valid data.
952 *
953 * Having an aux buffer with invalid data is a problem for two reasons:
954 *
955 * 1) Having an invalid value in the buffer can confuse the hardware.
956 * For instance, with CCS_E on SKL, a two-bit CCS value of 2 is
957 * invalid and leads to the hardware doing strange things. It
958 * doesn't hang as far as we can tell but rendering corruption can
959 * occur.
960 *
961 * 2) If this transition is into the GENERAL layout and we then use the
962 * image as a storage image, then we must have the aux buffer in the
963 * pass-through state so that, if we then go to texture from the
964 * image, we get the results of our storage image writes and not the
965 * fast clear color or other random data.
966 *
967 * For CCS both of the problems above are real demonstrable issues. In
968 * that case, the only thing we can do is to perform an ambiguate to
969 * transition the aux surface into the pass-through state.
970 *
971 * For MCS, (2) is never an issue because we don't support multisampled
972 * storage images. In theory, issue (1) is a problem with MCS but we've
973 * never seen it in the wild. For 4x and 16x, all bit patters could, in
974 * theory, be interpreted as something but we don't know that all bit
975 * patterns are actually valid. For 2x and 8x, you could easily end up
976 * with the MCS referring to an invalid plane because not all bits of
977 * the MCS value are actually used. Even though we've never seen issues
978 * in the wild, it's best to play it safe and initialize the MCS. We
979 * can use a fast-clear for MCS because we only ever touch from render
980 * and texture (no image load store).
981 */
982 if (image->samples == 1) {
983 for (uint32_t l = 0; l < level_count; l++) {
984 const uint32_t level = base_level + l;
985
986 uint32_t aux_layers = anv_image_aux_layers(image, aspect, level);
987 if (base_layer >= aux_layers)
988 break; /* We will only get fewer layers as level increases */
989 uint32_t level_layer_count =
990 MIN2(layer_count, aux_layers - base_layer);
991
992 anv_image_ccs_op(cmd_buffer, image,
993 image->planes[plane].surface.isl.format,
994 aspect, level, base_layer, level_layer_count,
995 ISL_AUX_OP_AMBIGUATE, NULL, false);
996
997 if (image->planes[plane].aux_usage == ISL_AUX_USAGE_CCS_E) {
998 set_image_compressed_bit(cmd_buffer, image, aspect,
999 level, base_layer, level_layer_count,
1000 false);
1001 }
1002 }
1003 } else {
1004 if (image->samples == 4 || image->samples == 16) {
1005 anv_perf_warn(cmd_buffer->device->instance, image,
1006 "Doing a potentially unnecessary fast-clear to "
1007 "define an MCS buffer.");
1008 }
1009
1010 assert(base_level == 0 && level_count == 1);
1011 anv_image_mcs_op(cmd_buffer, image,
1012 image->planes[plane].surface.isl.format,
1013 aspect, base_layer, layer_count,
1014 ISL_AUX_OP_FAST_CLEAR, NULL, false);
1015 }
1016 return;
1017 }
1018
1019 const enum isl_aux_usage initial_aux_usage =
1020 anv_layout_to_aux_usage(devinfo, image, aspect, initial_layout);
1021 const enum isl_aux_usage final_aux_usage =
1022 anv_layout_to_aux_usage(devinfo, image, aspect, final_layout);
1023
1024 /* The current code assumes that there is no mixing of CCS_E and CCS_D.
1025 * We can handle transitions between CCS_D/E to and from NONE. What we
1026 * don't yet handle is switching between CCS_E and CCS_D within a given
1027 * image. Doing so in a performant way requires more detailed aux state
1028 * tracking such as what is done in i965. For now, just assume that we
1029 * only have one type of compression.
1030 */
1031 assert(initial_aux_usage == ISL_AUX_USAGE_NONE ||
1032 final_aux_usage == ISL_AUX_USAGE_NONE ||
1033 initial_aux_usage == final_aux_usage);
1034
1035 /* If initial aux usage is NONE, there is nothing to resolve */
1036 if (initial_aux_usage == ISL_AUX_USAGE_NONE)
1037 return;
1038
1039 enum isl_aux_op resolve_op = ISL_AUX_OP_NONE;
1040
1041 /* If the initial layout supports more fast clear than the final layout
1042 * then we need at least a partial resolve.
1043 */
1044 const enum anv_fast_clear_type initial_fast_clear =
1045 anv_layout_to_fast_clear_type(devinfo, image, aspect, initial_layout);
1046 const enum anv_fast_clear_type final_fast_clear =
1047 anv_layout_to_fast_clear_type(devinfo, image, aspect, final_layout);
1048 if (final_fast_clear < initial_fast_clear)
1049 resolve_op = ISL_AUX_OP_PARTIAL_RESOLVE;
1050
1051 if (initial_aux_usage == ISL_AUX_USAGE_CCS_E &&
1052 final_aux_usage != ISL_AUX_USAGE_CCS_E)
1053 resolve_op = ISL_AUX_OP_FULL_RESOLVE;
1054
1055 if (resolve_op == ISL_AUX_OP_NONE)
1056 return;
1057
1058 /* Perform a resolve to synchronize data between the main and aux buffer.
1059 * Before we begin, we must satisfy the cache flushing requirement specified
1060 * in the Sky Lake PRM Vol. 7, "MCS Buffer for Render Target(s)":
1061 *
1062 * Any transition from any value in {Clear, Render, Resolve} to a
1063 * different value in {Clear, Render, Resolve} requires end of pipe
1064 * synchronization.
1065 *
1066 * We perform a flush of the write cache before and after the clear and
1067 * resolve operations to meet this requirement.
1068 *
1069 * Unlike other drawing, fast clear operations are not properly
1070 * synchronized. The first PIPE_CONTROL here likely ensures that the
1071 * contents of the previous render or clear hit the render target before we
1072 * resolve and the second likely ensures that the resolve is complete before
1073 * we do any more rendering or clearing.
1074 */
1075 cmd_buffer->state.pending_pipe_bits |=
1076 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT | ANV_PIPE_CS_STALL_BIT;
1077
1078 for (uint32_t l = 0; l < level_count; l++) {
1079 uint32_t level = base_level + l;
1080
1081 uint32_t aux_layers = anv_image_aux_layers(image, aspect, level);
1082 if (base_layer >= aux_layers)
1083 break; /* We will only get fewer layers as level increases */
1084 uint32_t level_layer_count =
1085 MIN2(layer_count, aux_layers - base_layer);
1086
1087 for (uint32_t a = 0; a < level_layer_count; a++) {
1088 uint32_t array_layer = base_layer + a;
1089 if (image->samples == 1) {
1090 anv_cmd_predicated_ccs_resolve(cmd_buffer, image,
1091 image->planes[plane].surface.isl.format,
1092 aspect, level, array_layer, resolve_op,
1093 final_fast_clear);
1094 } else {
1095 /* We only support fast-clear on the first layer so partial
1096 * resolves should not be used on other layers as they will use
1097 * the clear color stored in memory that is only valid for layer0.
1098 */
1099 if (resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE &&
1100 array_layer != 0)
1101 continue;
1102
1103 anv_cmd_predicated_mcs_resolve(cmd_buffer, image,
1104 image->planes[plane].surface.isl.format,
1105 aspect, array_layer, resolve_op,
1106 final_fast_clear);
1107 }
1108 }
1109 }
1110
1111 cmd_buffer->state.pending_pipe_bits |=
1112 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT | ANV_PIPE_CS_STALL_BIT;
1113 }
1114
1115 /**
1116 * Setup anv_cmd_state::attachments for vkCmdBeginRenderPass.
1117 */
1118 static VkResult
1119 genX(cmd_buffer_setup_attachments)(struct anv_cmd_buffer *cmd_buffer,
1120 struct anv_render_pass *pass,
1121 const VkRenderPassBeginInfo *begin)
1122 {
1123 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
1124 struct anv_cmd_state *state = &cmd_buffer->state;
1125
1126 vk_free(&cmd_buffer->pool->alloc, state->attachments);
1127
1128 if (pass->attachment_count > 0) {
1129 state->attachments = vk_alloc(&cmd_buffer->pool->alloc,
1130 pass->attachment_count *
1131 sizeof(state->attachments[0]),
1132 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1133 if (state->attachments == NULL) {
1134 /* Propagate VK_ERROR_OUT_OF_HOST_MEMORY to vkEndCommandBuffer */
1135 return anv_batch_set_error(&cmd_buffer->batch,
1136 VK_ERROR_OUT_OF_HOST_MEMORY);
1137 }
1138 } else {
1139 state->attachments = NULL;
1140 }
1141
1142 /* Reserve one for the NULL state. */
1143 unsigned num_states = 1;
1144 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
1145 if (vk_format_is_color(pass->attachments[i].format))
1146 num_states++;
1147
1148 if (need_input_attachment_state(&pass->attachments[i]))
1149 num_states++;
1150 }
1151
1152 const uint32_t ss_stride = align_u32(isl_dev->ss.size, isl_dev->ss.align);
1153 state->render_pass_states =
1154 anv_state_stream_alloc(&cmd_buffer->surface_state_stream,
1155 num_states * ss_stride, isl_dev->ss.align);
1156
1157 struct anv_state next_state = state->render_pass_states;
1158 next_state.alloc_size = isl_dev->ss.size;
1159
1160 state->null_surface_state = next_state;
1161 next_state.offset += ss_stride;
1162 next_state.map += ss_stride;
1163
1164 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
1165 if (vk_format_is_color(pass->attachments[i].format)) {
1166 state->attachments[i].color.state = next_state;
1167 next_state.offset += ss_stride;
1168 next_state.map += ss_stride;
1169 }
1170
1171 if (need_input_attachment_state(&pass->attachments[i])) {
1172 state->attachments[i].input.state = next_state;
1173 next_state.offset += ss_stride;
1174 next_state.map += ss_stride;
1175 }
1176 }
1177 assert(next_state.offset == state->render_pass_states.offset +
1178 state->render_pass_states.alloc_size);
1179
1180 if (begin) {
1181 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, begin->framebuffer);
1182 assert(pass->attachment_count == framebuffer->attachment_count);
1183
1184 isl_null_fill_state(isl_dev, state->null_surface_state.map,
1185 isl_extent3d(framebuffer->width,
1186 framebuffer->height,
1187 framebuffer->layers));
1188
1189 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
1190 struct anv_render_pass_attachment *att = &pass->attachments[i];
1191 VkImageAspectFlags att_aspects = vk_format_aspects(att->format);
1192 VkImageAspectFlags clear_aspects = 0;
1193 VkImageAspectFlags load_aspects = 0;
1194
1195 if (att_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1196 /* color attachment */
1197 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1198 clear_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
1199 } else if (att->load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
1200 load_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
1201 }
1202 } else {
1203 /* depthstencil attachment */
1204 if (att_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
1205 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1206 clear_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1207 } else if (att->load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
1208 load_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1209 }
1210 }
1211 if (att_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
1212 if (att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1213 clear_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1214 } else if (att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
1215 load_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1216 }
1217 }
1218 }
1219
1220 state->attachments[i].current_layout = att->initial_layout;
1221 state->attachments[i].pending_clear_aspects = clear_aspects;
1222 state->attachments[i].pending_load_aspects = load_aspects;
1223 if (clear_aspects)
1224 state->attachments[i].clear_value = begin->pClearValues[i];
1225
1226 struct anv_image_view *iview = framebuffer->attachments[i];
1227 anv_assert(iview->vk_format == att->format);
1228
1229 const uint32_t num_layers = iview->planes[0].isl.array_len;
1230 state->attachments[i].pending_clear_views = (1 << num_layers) - 1;
1231
1232 union isl_color_value clear_color = { .u32 = { 0, } };
1233 if (att_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1234 anv_assert(iview->n_planes == 1);
1235 assert(att_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
1236 color_attachment_compute_aux_usage(cmd_buffer->device,
1237 state, i, begin->renderArea,
1238 &clear_color);
1239
1240 anv_image_fill_surface_state(cmd_buffer->device,
1241 iview->image,
1242 VK_IMAGE_ASPECT_COLOR_BIT,
1243 &iview->planes[0].isl,
1244 ISL_SURF_USAGE_RENDER_TARGET_BIT,
1245 state->attachments[i].aux_usage,
1246 &clear_color,
1247 0,
1248 &state->attachments[i].color,
1249 NULL);
1250
1251 add_surface_state_relocs(cmd_buffer, state->attachments[i].color);
1252 } else {
1253 depth_stencil_attachment_compute_aux_usage(cmd_buffer->device,
1254 state, i,
1255 begin->renderArea);
1256 }
1257
1258 if (need_input_attachment_state(&pass->attachments[i])) {
1259 anv_image_fill_surface_state(cmd_buffer->device,
1260 iview->image,
1261 VK_IMAGE_ASPECT_COLOR_BIT,
1262 &iview->planes[0].isl,
1263 ISL_SURF_USAGE_TEXTURE_BIT,
1264 state->attachments[i].input_aux_usage,
1265 &clear_color,
1266 0,
1267 &state->attachments[i].input,
1268 NULL);
1269
1270 add_surface_state_relocs(cmd_buffer, state->attachments[i].input);
1271 }
1272 }
1273 }
1274
1275 return VK_SUCCESS;
1276 }
1277
1278 VkResult
1279 genX(BeginCommandBuffer)(
1280 VkCommandBuffer commandBuffer,
1281 const VkCommandBufferBeginInfo* pBeginInfo)
1282 {
1283 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1284
1285 /* If this is the first vkBeginCommandBuffer, we must *initialize* the
1286 * command buffer's state. Otherwise, we must *reset* its state. In both
1287 * cases we reset it.
1288 *
1289 * From the Vulkan 1.0 spec:
1290 *
1291 * If a command buffer is in the executable state and the command buffer
1292 * was allocated from a command pool with the
1293 * VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT flag set, then
1294 * vkBeginCommandBuffer implicitly resets the command buffer, behaving
1295 * as if vkResetCommandBuffer had been called with
1296 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT not set. It then puts
1297 * the command buffer in the recording state.
1298 */
1299 anv_cmd_buffer_reset(cmd_buffer);
1300
1301 cmd_buffer->usage_flags = pBeginInfo->flags;
1302
1303 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY ||
1304 !(cmd_buffer->usage_flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT));
1305
1306 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
1307
1308 /* We sometimes store vertex data in the dynamic state buffer for blorp
1309 * operations and our dynamic state stream may re-use data from previous
1310 * command buffers. In order to prevent stale cache data, we flush the VF
1311 * cache. We could do this on every blorp call but that's not really
1312 * needed as all of the data will get written by the CPU prior to the GPU
1313 * executing anything. The chances are fairly high that they will use
1314 * blorp at least once per primary command buffer so it shouldn't be
1315 * wasted.
1316 */
1317 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY)
1318 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1319
1320 /* We send an "Indirect State Pointers Disable" packet at
1321 * EndCommandBuffer, so all push contant packets are ignored during a
1322 * context restore. Documentation says after that command, we need to
1323 * emit push constants again before any rendering operation. So we
1324 * flag them dirty here to make sure they get emitted.
1325 */
1326 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_ALL_GRAPHICS;
1327
1328 VkResult result = VK_SUCCESS;
1329 if (cmd_buffer->usage_flags &
1330 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
1331 assert(pBeginInfo->pInheritanceInfo);
1332 cmd_buffer->state.pass =
1333 anv_render_pass_from_handle(pBeginInfo->pInheritanceInfo->renderPass);
1334 cmd_buffer->state.subpass =
1335 &cmd_buffer->state.pass->subpasses[pBeginInfo->pInheritanceInfo->subpass];
1336
1337 /* This is optional in the inheritance info. */
1338 cmd_buffer->state.framebuffer =
1339 anv_framebuffer_from_handle(pBeginInfo->pInheritanceInfo->framebuffer);
1340
1341 result = genX(cmd_buffer_setup_attachments)(cmd_buffer,
1342 cmd_buffer->state.pass, NULL);
1343
1344 /* Record that HiZ is enabled if we can. */
1345 if (cmd_buffer->state.framebuffer) {
1346 const struct anv_image_view * const iview =
1347 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
1348
1349 if (iview) {
1350 VkImageLayout layout =
1351 cmd_buffer->state.subpass->depth_stencil_attachment->layout;
1352
1353 enum isl_aux_usage aux_usage =
1354 anv_layout_to_aux_usage(&cmd_buffer->device->info, iview->image,
1355 VK_IMAGE_ASPECT_DEPTH_BIT, layout);
1356
1357 cmd_buffer->state.hiz_enabled = aux_usage == ISL_AUX_USAGE_HIZ;
1358 }
1359 }
1360
1361 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
1362 }
1363
1364 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1365 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
1366 const VkCommandBufferInheritanceConditionalRenderingInfoEXT *conditional_rendering_info =
1367 vk_find_struct_const(pBeginInfo->pInheritanceInfo->pNext, COMMAND_BUFFER_INHERITANCE_CONDITIONAL_RENDERING_INFO_EXT);
1368
1369 /* If secondary buffer supports conditional rendering
1370 * we should emit commands as if conditional rendering is enabled.
1371 */
1372 cmd_buffer->state.conditional_render_enabled =
1373 conditional_rendering_info && conditional_rendering_info->conditionalRenderingEnable;
1374 }
1375 #endif
1376
1377 return result;
1378 }
1379
1380 /* From the PRM, Volume 2a:
1381 *
1382 * "Indirect State Pointers Disable
1383 *
1384 * At the completion of the post-sync operation associated with this pipe
1385 * control packet, the indirect state pointers in the hardware are
1386 * considered invalid; the indirect pointers are not saved in the context.
1387 * If any new indirect state commands are executed in the command stream
1388 * while the pipe control is pending, the new indirect state commands are
1389 * preserved.
1390 *
1391 * [DevIVB+]: Using Invalidate State Pointer (ISP) only inhibits context
1392 * restoring of Push Constant (3DSTATE_CONSTANT_*) commands. Push Constant
1393 * commands are only considered as Indirect State Pointers. Once ISP is
1394 * issued in a context, SW must initialize by programming push constant
1395 * commands for all the shaders (at least to zero length) before attempting
1396 * any rendering operation for the same context."
1397 *
1398 * 3DSTATE_CONSTANT_* packets are restored during a context restore,
1399 * even though they point to a BO that has been already unreferenced at
1400 * the end of the previous batch buffer. This has been fine so far since
1401 * we are protected by these scratch page (every address not covered by
1402 * a BO should be pointing to the scratch page). But on CNL, it is
1403 * causing a GPU hang during context restore at the 3DSTATE_CONSTANT_*
1404 * instruction.
1405 *
1406 * The flag "Indirect State Pointers Disable" in PIPE_CONTROL tells the
1407 * hardware to ignore previous 3DSTATE_CONSTANT_* packets during a
1408 * context restore, so the mentioned hang doesn't happen. However,
1409 * software must program push constant commands for all stages prior to
1410 * rendering anything. So we flag them dirty in BeginCommandBuffer.
1411 *
1412 * Finally, we also make sure to stall at pixel scoreboard to make sure the
1413 * constants have been loaded into the EUs prior to disable the push constants
1414 * so that it doesn't hang a previous 3DPRIMITIVE.
1415 */
1416 static void
1417 emit_isp_disable(struct anv_cmd_buffer *cmd_buffer)
1418 {
1419 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1420 pc.StallAtPixelScoreboard = true;
1421 pc.CommandStreamerStallEnable = true;
1422 }
1423 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1424 pc.IndirectStatePointersDisable = true;
1425 pc.CommandStreamerStallEnable = true;
1426 }
1427 }
1428
1429 VkResult
1430 genX(EndCommandBuffer)(
1431 VkCommandBuffer commandBuffer)
1432 {
1433 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1434
1435 if (anv_batch_has_error(&cmd_buffer->batch))
1436 return cmd_buffer->batch.status;
1437
1438 /* We want every command buffer to start with the PMA fix in a known state,
1439 * so we disable it at the end of the command buffer.
1440 */
1441 genX(cmd_buffer_enable_pma_fix)(cmd_buffer, false);
1442
1443 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
1444
1445 emit_isp_disable(cmd_buffer);
1446
1447 anv_cmd_buffer_end_batch_buffer(cmd_buffer);
1448
1449 return VK_SUCCESS;
1450 }
1451
1452 void
1453 genX(CmdExecuteCommands)(
1454 VkCommandBuffer commandBuffer,
1455 uint32_t commandBufferCount,
1456 const VkCommandBuffer* pCmdBuffers)
1457 {
1458 ANV_FROM_HANDLE(anv_cmd_buffer, primary, commandBuffer);
1459
1460 assert(primary->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1461
1462 if (anv_batch_has_error(&primary->batch))
1463 return;
1464
1465 /* The secondary command buffers will assume that the PMA fix is disabled
1466 * when they begin executing. Make sure this is true.
1467 */
1468 genX(cmd_buffer_enable_pma_fix)(primary, false);
1469
1470 /* The secondary command buffer doesn't know which textures etc. have been
1471 * flushed prior to their execution. Apply those flushes now.
1472 */
1473 genX(cmd_buffer_apply_pipe_flushes)(primary);
1474
1475 for (uint32_t i = 0; i < commandBufferCount; i++) {
1476 ANV_FROM_HANDLE(anv_cmd_buffer, secondary, pCmdBuffers[i]);
1477
1478 assert(secondary->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY);
1479 assert(!anv_batch_has_error(&secondary->batch));
1480
1481 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1482 if (secondary->state.conditional_render_enabled) {
1483 if (!primary->state.conditional_render_enabled) {
1484 /* Secondary buffer is constructed as if it will be executed
1485 * with conditional rendering, we should satisfy this dependency
1486 * regardless of conditional rendering being enabled in primary.
1487 */
1488 emit_lri(&primary->batch, CS_GPR(ANV_PREDICATE_RESULT_REG), UINT32_MAX);
1489 emit_lri(&primary->batch, CS_GPR(ANV_PREDICATE_RESULT_REG) + 4, UINT32_MAX);
1490 }
1491 }
1492 #endif
1493
1494 if (secondary->usage_flags &
1495 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
1496 /* If we're continuing a render pass from the primary, we need to
1497 * copy the surface states for the current subpass into the storage
1498 * we allocated for them in BeginCommandBuffer.
1499 */
1500 struct anv_bo *ss_bo =
1501 primary->device->surface_state_pool.block_pool.bo;
1502 struct anv_state src_state = primary->state.render_pass_states;
1503 struct anv_state dst_state = secondary->state.render_pass_states;
1504 assert(src_state.alloc_size == dst_state.alloc_size);
1505
1506 genX(cmd_buffer_so_memcpy)(primary,
1507 (struct anv_address) {
1508 .bo = ss_bo,
1509 .offset = dst_state.offset,
1510 },
1511 (struct anv_address) {
1512 .bo = ss_bo,
1513 .offset = src_state.offset,
1514 },
1515 src_state.alloc_size);
1516 }
1517
1518 anv_cmd_buffer_add_secondary(primary, secondary);
1519 }
1520
1521 /* The secondary may have selected a different pipeline (3D or compute) and
1522 * may have changed the current L3$ configuration. Reset our tracking
1523 * variables to invalid values to ensure that we re-emit these in the case
1524 * where we do any draws or compute dispatches from the primary after the
1525 * secondary has returned.
1526 */
1527 primary->state.current_pipeline = UINT32_MAX;
1528 primary->state.current_l3_config = NULL;
1529
1530 /* Each of the secondary command buffers will use its own state base
1531 * address. We need to re-emit state base address for the primary after
1532 * all of the secondaries are done.
1533 *
1534 * TODO: Maybe we want to make this a dirty bit to avoid extra state base
1535 * address calls?
1536 */
1537 genX(cmd_buffer_emit_state_base_address)(primary);
1538 }
1539
1540 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
1541 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
1542 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
1543
1544 /**
1545 * Program the hardware to use the specified L3 configuration.
1546 */
1547 void
1548 genX(cmd_buffer_config_l3)(struct anv_cmd_buffer *cmd_buffer,
1549 const struct gen_l3_config *cfg)
1550 {
1551 assert(cfg);
1552 if (cfg == cmd_buffer->state.current_l3_config)
1553 return;
1554
1555 if (unlikely(INTEL_DEBUG & DEBUG_L3)) {
1556 intel_logd("L3 config transition: ");
1557 gen_dump_l3_config(cfg, stderr);
1558 }
1559
1560 const bool has_slm = cfg->n[GEN_L3P_SLM];
1561
1562 /* According to the hardware docs, the L3 partitioning can only be changed
1563 * while the pipeline is completely drained and the caches are flushed,
1564 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
1565 */
1566 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1567 pc.DCFlushEnable = true;
1568 pc.PostSyncOperation = NoWrite;
1569 pc.CommandStreamerStallEnable = true;
1570 }
1571
1572 /* ...followed by a second pipelined PIPE_CONTROL that initiates
1573 * invalidation of the relevant caches. Note that because RO invalidation
1574 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
1575 * command is processed by the CS) we cannot combine it with the previous
1576 * stalling flush as the hardware documentation suggests, because that
1577 * would cause the CS to stall on previous rendering *after* RO
1578 * invalidation and wouldn't prevent the RO caches from being polluted by
1579 * concurrent rendering before the stall completes. This intentionally
1580 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
1581 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
1582 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
1583 * already guarantee that there is no concurrent GPGPU kernel execution
1584 * (see SKL HSD 2132585).
1585 */
1586 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1587 pc.TextureCacheInvalidationEnable = true;
1588 pc.ConstantCacheInvalidationEnable = true;
1589 pc.InstructionCacheInvalidateEnable = true;
1590 pc.StateCacheInvalidationEnable = true;
1591 pc.PostSyncOperation = NoWrite;
1592 }
1593
1594 /* Now send a third stalling flush to make sure that invalidation is
1595 * complete when the L3 configuration registers are modified.
1596 */
1597 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1598 pc.DCFlushEnable = true;
1599 pc.PostSyncOperation = NoWrite;
1600 pc.CommandStreamerStallEnable = true;
1601 }
1602
1603 #if GEN_GEN >= 8
1604
1605 assert(!cfg->n[GEN_L3P_IS] && !cfg->n[GEN_L3P_C] && !cfg->n[GEN_L3P_T]);
1606
1607 uint32_t l3cr;
1608 anv_pack_struct(&l3cr, GENX(L3CNTLREG),
1609 .SLMEnable = has_slm,
1610 #if GEN_GEN == 11
1611 /* WA_1406697149: Bit 9 "Error Detection Behavior Control" must be set
1612 * in L3CNTLREG register. The default setting of the bit is not the
1613 * desirable behavior.
1614 */
1615 .ErrorDetectionBehaviorControl = true,
1616 .UseFullWays = true,
1617 #endif
1618 .URBAllocation = cfg->n[GEN_L3P_URB],
1619 .ROAllocation = cfg->n[GEN_L3P_RO],
1620 .DCAllocation = cfg->n[GEN_L3P_DC],
1621 .AllAllocation = cfg->n[GEN_L3P_ALL]);
1622
1623 /* Set up the L3 partitioning. */
1624 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG_num), l3cr);
1625
1626 #else
1627
1628 const bool has_dc = cfg->n[GEN_L3P_DC] || cfg->n[GEN_L3P_ALL];
1629 const bool has_is = cfg->n[GEN_L3P_IS] || cfg->n[GEN_L3P_RO] ||
1630 cfg->n[GEN_L3P_ALL];
1631 const bool has_c = cfg->n[GEN_L3P_C] || cfg->n[GEN_L3P_RO] ||
1632 cfg->n[GEN_L3P_ALL];
1633 const bool has_t = cfg->n[GEN_L3P_T] || cfg->n[GEN_L3P_RO] ||
1634 cfg->n[GEN_L3P_ALL];
1635
1636 assert(!cfg->n[GEN_L3P_ALL]);
1637
1638 /* When enabled SLM only uses a portion of the L3 on half of the banks,
1639 * the matching space on the remaining banks has to be allocated to a
1640 * client (URB for all validated configurations) set to the
1641 * lower-bandwidth 2-bank address hashing mode.
1642 */
1643 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
1644 const bool urb_low_bw = has_slm && !devinfo->is_baytrail;
1645 assert(!urb_low_bw || cfg->n[GEN_L3P_URB] == cfg->n[GEN_L3P_SLM]);
1646
1647 /* Minimum number of ways that can be allocated to the URB. */
1648 MAYBE_UNUSED const unsigned n0_urb = devinfo->is_baytrail ? 32 : 0;
1649 assert(cfg->n[GEN_L3P_URB] >= n0_urb);
1650
1651 uint32_t l3sqcr1, l3cr2, l3cr3;
1652 anv_pack_struct(&l3sqcr1, GENX(L3SQCREG1),
1653 .ConvertDC_UC = !has_dc,
1654 .ConvertIS_UC = !has_is,
1655 .ConvertC_UC = !has_c,
1656 .ConvertT_UC = !has_t);
1657 l3sqcr1 |=
1658 GEN_IS_HASWELL ? HSW_L3SQCREG1_SQGHPCI_DEFAULT :
1659 devinfo->is_baytrail ? VLV_L3SQCREG1_SQGHPCI_DEFAULT :
1660 IVB_L3SQCREG1_SQGHPCI_DEFAULT;
1661
1662 anv_pack_struct(&l3cr2, GENX(L3CNTLREG2),
1663 .SLMEnable = has_slm,
1664 .URBLowBandwidth = urb_low_bw,
1665 .URBAllocation = cfg->n[GEN_L3P_URB] - n0_urb,
1666 #if !GEN_IS_HASWELL
1667 .ALLAllocation = cfg->n[GEN_L3P_ALL],
1668 #endif
1669 .ROAllocation = cfg->n[GEN_L3P_RO],
1670 .DCAllocation = cfg->n[GEN_L3P_DC]);
1671
1672 anv_pack_struct(&l3cr3, GENX(L3CNTLREG3),
1673 .ISAllocation = cfg->n[GEN_L3P_IS],
1674 .ISLowBandwidth = 0,
1675 .CAllocation = cfg->n[GEN_L3P_C],
1676 .CLowBandwidth = 0,
1677 .TAllocation = cfg->n[GEN_L3P_T],
1678 .TLowBandwidth = 0);
1679
1680 /* Set up the L3 partitioning. */
1681 emit_lri(&cmd_buffer->batch, GENX(L3SQCREG1_num), l3sqcr1);
1682 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG2_num), l3cr2);
1683 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG3_num), l3cr3);
1684
1685 #if GEN_IS_HASWELL
1686 if (cmd_buffer->device->instance->physicalDevice.cmd_parser_version >= 4) {
1687 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
1688 * them disabled to avoid crashing the system hard.
1689 */
1690 uint32_t scratch1, chicken3;
1691 anv_pack_struct(&scratch1, GENX(SCRATCH1),
1692 .L3AtomicDisable = !has_dc);
1693 anv_pack_struct(&chicken3, GENX(CHICKEN3),
1694 .L3AtomicDisableMask = true,
1695 .L3AtomicDisable = !has_dc);
1696 emit_lri(&cmd_buffer->batch, GENX(SCRATCH1_num), scratch1);
1697 emit_lri(&cmd_buffer->batch, GENX(CHICKEN3_num), chicken3);
1698 }
1699 #endif
1700
1701 #endif
1702
1703 cmd_buffer->state.current_l3_config = cfg;
1704 }
1705
1706 void
1707 genX(cmd_buffer_apply_pipe_flushes)(struct anv_cmd_buffer *cmd_buffer)
1708 {
1709 enum anv_pipe_bits bits = cmd_buffer->state.pending_pipe_bits;
1710
1711 /* Flushes are pipelined while invalidations are handled immediately.
1712 * Therefore, if we're flushing anything then we need to schedule a stall
1713 * before any invalidations can happen.
1714 */
1715 if (bits & ANV_PIPE_FLUSH_BITS)
1716 bits |= ANV_PIPE_NEEDS_CS_STALL_BIT;
1717
1718 /* If we're going to do an invalidate and we have a pending CS stall that
1719 * has yet to be resolved, we do the CS stall now.
1720 */
1721 if ((bits & ANV_PIPE_INVALIDATE_BITS) &&
1722 (bits & ANV_PIPE_NEEDS_CS_STALL_BIT)) {
1723 bits |= ANV_PIPE_CS_STALL_BIT;
1724 bits &= ~ANV_PIPE_NEEDS_CS_STALL_BIT;
1725 }
1726
1727 if (bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT)) {
1728 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
1729 pipe.DepthCacheFlushEnable = bits & ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
1730 pipe.DCFlushEnable = bits & ANV_PIPE_DATA_CACHE_FLUSH_BIT;
1731 pipe.RenderTargetCacheFlushEnable =
1732 bits & ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
1733
1734 pipe.DepthStallEnable = bits & ANV_PIPE_DEPTH_STALL_BIT;
1735 pipe.CommandStreamerStallEnable = bits & ANV_PIPE_CS_STALL_BIT;
1736 pipe.StallAtPixelScoreboard = bits & ANV_PIPE_STALL_AT_SCOREBOARD_BIT;
1737
1738 /*
1739 * According to the Broadwell documentation, any PIPE_CONTROL with the
1740 * "Command Streamer Stall" bit set must also have another bit set,
1741 * with five different options:
1742 *
1743 * - Render Target Cache Flush
1744 * - Depth Cache Flush
1745 * - Stall at Pixel Scoreboard
1746 * - Post-Sync Operation
1747 * - Depth Stall
1748 * - DC Flush Enable
1749 *
1750 * I chose "Stall at Pixel Scoreboard" since that's what we use in
1751 * mesa and it seems to work fine. The choice is fairly arbitrary.
1752 */
1753 if ((bits & ANV_PIPE_CS_STALL_BIT) &&
1754 !(bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_DEPTH_STALL_BIT |
1755 ANV_PIPE_STALL_AT_SCOREBOARD_BIT)))
1756 pipe.StallAtPixelScoreboard = true;
1757 }
1758
1759 /* If a render target flush was emitted, then we can toggle off the bit
1760 * saying that render target writes are ongoing.
1761 */
1762 if (bits & ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT)
1763 bits &= ~(ANV_PIPE_RENDER_TARGET_BUFFER_WRITES);
1764
1765 bits &= ~(ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT);
1766 }
1767
1768 if (bits & ANV_PIPE_INVALIDATE_BITS) {
1769 /* From the SKL PRM, Vol. 2a, "PIPE_CONTROL",
1770 *
1771 * "If the VF Cache Invalidation Enable is set to a 1 in a
1772 * PIPE_CONTROL, a separate Null PIPE_CONTROL, all bitfields sets to
1773 * 0, with the VF Cache Invalidation Enable set to 0 needs to be sent
1774 * prior to the PIPE_CONTROL with VF Cache Invalidation Enable set to
1775 * a 1."
1776 *
1777 * This appears to hang Broadwell, so we restrict it to just gen9.
1778 */
1779 if (GEN_GEN == 9 && (bits & ANV_PIPE_VF_CACHE_INVALIDATE_BIT))
1780 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe);
1781
1782 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
1783 pipe.StateCacheInvalidationEnable =
1784 bits & ANV_PIPE_STATE_CACHE_INVALIDATE_BIT;
1785 pipe.ConstantCacheInvalidationEnable =
1786 bits & ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT;
1787 pipe.VFCacheInvalidationEnable =
1788 bits & ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1789 pipe.TextureCacheInvalidationEnable =
1790 bits & ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
1791 pipe.InstructionCacheInvalidateEnable =
1792 bits & ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT;
1793
1794 /* From the SKL PRM, Vol. 2a, "PIPE_CONTROL",
1795 *
1796 * "When VF Cache Invalidate is set “Post Sync Operation” must be
1797 * enabled to “Write Immediate Data” or “Write PS Depth Count” or
1798 * “Write Timestamp”.
1799 */
1800 if (GEN_GEN == 9 && pipe.VFCacheInvalidationEnable) {
1801 pipe.PostSyncOperation = WriteImmediateData;
1802 pipe.Address =
1803 (struct anv_address) { &cmd_buffer->device->workaround_bo, 0 };
1804 }
1805 }
1806
1807 bits &= ~ANV_PIPE_INVALIDATE_BITS;
1808 }
1809
1810 cmd_buffer->state.pending_pipe_bits = bits;
1811 }
1812
1813 void genX(CmdPipelineBarrier)(
1814 VkCommandBuffer commandBuffer,
1815 VkPipelineStageFlags srcStageMask,
1816 VkPipelineStageFlags destStageMask,
1817 VkBool32 byRegion,
1818 uint32_t memoryBarrierCount,
1819 const VkMemoryBarrier* pMemoryBarriers,
1820 uint32_t bufferMemoryBarrierCount,
1821 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1822 uint32_t imageMemoryBarrierCount,
1823 const VkImageMemoryBarrier* pImageMemoryBarriers)
1824 {
1825 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1826
1827 /* XXX: Right now, we're really dumb and just flush whatever categories
1828 * the app asks for. One of these days we may make this a bit better
1829 * but right now that's all the hardware allows for in most areas.
1830 */
1831 VkAccessFlags src_flags = 0;
1832 VkAccessFlags dst_flags = 0;
1833
1834 for (uint32_t i = 0; i < memoryBarrierCount; i++) {
1835 src_flags |= pMemoryBarriers[i].srcAccessMask;
1836 dst_flags |= pMemoryBarriers[i].dstAccessMask;
1837 }
1838
1839 for (uint32_t i = 0; i < bufferMemoryBarrierCount; i++) {
1840 src_flags |= pBufferMemoryBarriers[i].srcAccessMask;
1841 dst_flags |= pBufferMemoryBarriers[i].dstAccessMask;
1842 }
1843
1844 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1845 src_flags |= pImageMemoryBarriers[i].srcAccessMask;
1846 dst_flags |= pImageMemoryBarriers[i].dstAccessMask;
1847 ANV_FROM_HANDLE(anv_image, image, pImageMemoryBarriers[i].image);
1848 const VkImageSubresourceRange *range =
1849 &pImageMemoryBarriers[i].subresourceRange;
1850
1851 if (range->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1852 transition_depth_buffer(cmd_buffer, image,
1853 pImageMemoryBarriers[i].oldLayout,
1854 pImageMemoryBarriers[i].newLayout);
1855 } else if (range->aspectMask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1856 VkImageAspectFlags color_aspects =
1857 anv_image_expand_aspects(image, range->aspectMask);
1858 uint32_t aspect_bit;
1859
1860 uint32_t base_layer, layer_count;
1861 if (image->type == VK_IMAGE_TYPE_3D) {
1862 base_layer = 0;
1863 layer_count = anv_minify(image->extent.depth, range->baseMipLevel);
1864 } else {
1865 base_layer = range->baseArrayLayer;
1866 layer_count = anv_get_layerCount(image, range);
1867 }
1868
1869 anv_foreach_image_aspect_bit(aspect_bit, image, color_aspects) {
1870 transition_color_buffer(cmd_buffer, image, 1UL << aspect_bit,
1871 range->baseMipLevel,
1872 anv_get_levelCount(image, range),
1873 base_layer, layer_count,
1874 pImageMemoryBarriers[i].oldLayout,
1875 pImageMemoryBarriers[i].newLayout);
1876 }
1877 }
1878 }
1879
1880 cmd_buffer->state.pending_pipe_bits |=
1881 anv_pipe_flush_bits_for_access_flags(src_flags) |
1882 anv_pipe_invalidate_bits_for_access_flags(dst_flags);
1883 }
1884
1885 static void
1886 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer *cmd_buffer)
1887 {
1888 VkShaderStageFlags stages =
1889 cmd_buffer->state.gfx.base.pipeline->active_stages;
1890
1891 /* In order to avoid thrash, we assume that vertex and fragment stages
1892 * always exist. In the rare case where one is missing *and* the other
1893 * uses push concstants, this may be suboptimal. However, avoiding stalls
1894 * seems more important.
1895 */
1896 stages |= VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT;
1897
1898 if (stages == cmd_buffer->state.push_constant_stages)
1899 return;
1900
1901 #if GEN_GEN >= 8
1902 const unsigned push_constant_kb = 32;
1903 #elif GEN_IS_HASWELL
1904 const unsigned push_constant_kb = cmd_buffer->device->info.gt == 3 ? 32 : 16;
1905 #else
1906 const unsigned push_constant_kb = 16;
1907 #endif
1908
1909 const unsigned num_stages =
1910 util_bitcount(stages & VK_SHADER_STAGE_ALL_GRAPHICS);
1911 unsigned size_per_stage = push_constant_kb / num_stages;
1912
1913 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
1914 * units of 2KB. Incidentally, these are the same platforms that have
1915 * 32KB worth of push constant space.
1916 */
1917 if (push_constant_kb == 32)
1918 size_per_stage &= ~1u;
1919
1920 uint32_t kb_used = 0;
1921 for (int i = MESA_SHADER_VERTEX; i < MESA_SHADER_FRAGMENT; i++) {
1922 unsigned push_size = (stages & (1 << i)) ? size_per_stage : 0;
1923 anv_batch_emit(&cmd_buffer->batch,
1924 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
1925 alloc._3DCommandSubOpcode = 18 + i;
1926 alloc.ConstantBufferOffset = (push_size > 0) ? kb_used : 0;
1927 alloc.ConstantBufferSize = push_size;
1928 }
1929 kb_used += push_size;
1930 }
1931
1932 anv_batch_emit(&cmd_buffer->batch,
1933 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS), alloc) {
1934 alloc.ConstantBufferOffset = kb_used;
1935 alloc.ConstantBufferSize = push_constant_kb - kb_used;
1936 }
1937
1938 cmd_buffer->state.push_constant_stages = stages;
1939
1940 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
1941 *
1942 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
1943 * the next 3DPRIMITIVE command after programming the
1944 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
1945 *
1946 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
1947 * pipeline setup, we need to dirty push constants.
1948 */
1949 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_ALL_GRAPHICS;
1950 }
1951
1952 static const struct anv_descriptor *
1953 anv_descriptor_for_binding(const struct anv_cmd_pipeline_state *pipe_state,
1954 const struct anv_pipeline_binding *binding)
1955 {
1956 assert(binding->set < MAX_SETS);
1957 const struct anv_descriptor_set *set =
1958 pipe_state->descriptors[binding->set];
1959 const uint32_t offset =
1960 set->layout->binding[binding->binding].descriptor_index;
1961 return &set->descriptors[offset + binding->index];
1962 }
1963
1964 static uint32_t
1965 dynamic_offset_for_binding(const struct anv_cmd_pipeline_state *pipe_state,
1966 const struct anv_pipeline_binding *binding)
1967 {
1968 assert(binding->set < MAX_SETS);
1969 const struct anv_descriptor_set *set =
1970 pipe_state->descriptors[binding->set];
1971
1972 uint32_t dynamic_offset_idx =
1973 pipe_state->layout->set[binding->set].dynamic_offset_start +
1974 set->layout->binding[binding->binding].dynamic_offset_index +
1975 binding->index;
1976
1977 return pipe_state->dynamic_offsets[dynamic_offset_idx];
1978 }
1979
1980 static struct anv_address
1981 anv_descriptor_set_address(struct anv_cmd_buffer *cmd_buffer,
1982 struct anv_descriptor_set *set)
1983 {
1984 if (set->pool) {
1985 /* This is a normal descriptor set */
1986 return (struct anv_address) {
1987 .bo = &set->pool->bo,
1988 .offset = set->desc_mem.offset,
1989 };
1990 } else {
1991 /* This is a push descriptor set. We have to flag it as used on the GPU
1992 * so that the next time we push descriptors, we grab a new memory.
1993 */
1994 struct anv_push_descriptor_set *push_set =
1995 (struct anv_push_descriptor_set *)set;
1996 push_set->set_used_on_gpu = true;
1997
1998 return (struct anv_address) {
1999 .bo = cmd_buffer->dynamic_state_stream.state_pool->block_pool.bo,
2000 .offset = set->desc_mem.offset,
2001 };
2002 }
2003 }
2004
2005 static VkResult
2006 emit_binding_table(struct anv_cmd_buffer *cmd_buffer,
2007 gl_shader_stage stage,
2008 struct anv_state *bt_state)
2009 {
2010 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
2011 struct anv_subpass *subpass = cmd_buffer->state.subpass;
2012 struct anv_cmd_pipeline_state *pipe_state;
2013 struct anv_pipeline *pipeline;
2014 uint32_t state_offset;
2015
2016 switch (stage) {
2017 case MESA_SHADER_COMPUTE:
2018 pipe_state = &cmd_buffer->state.compute.base;
2019 break;
2020 default:
2021 pipe_state = &cmd_buffer->state.gfx.base;
2022 break;
2023 }
2024 pipeline = pipe_state->pipeline;
2025
2026 if (!anv_pipeline_has_stage(pipeline, stage)) {
2027 *bt_state = (struct anv_state) { 0, };
2028 return VK_SUCCESS;
2029 }
2030
2031 struct anv_pipeline_bind_map *map = &pipeline->shaders[stage]->bind_map;
2032 if (map->surface_count == 0) {
2033 *bt_state = (struct anv_state) { 0, };
2034 return VK_SUCCESS;
2035 }
2036
2037 *bt_state = anv_cmd_buffer_alloc_binding_table(cmd_buffer,
2038 map->surface_count,
2039 &state_offset);
2040 uint32_t *bt_map = bt_state->map;
2041
2042 if (bt_state->map == NULL)
2043 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2044
2045 /* We only use push constant space for images before gen9 */
2046 if (map->image_param_count > 0) {
2047 VkResult result =
2048 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, stage, images);
2049 if (result != VK_SUCCESS)
2050 return result;
2051
2052 cmd_buffer->state.push_constants_dirty |= 1 << stage;
2053 }
2054
2055 uint32_t image = 0;
2056 for (uint32_t s = 0; s < map->surface_count; s++) {
2057 struct anv_pipeline_binding *binding = &map->surface_to_descriptor[s];
2058
2059 struct anv_state surface_state;
2060
2061 if (binding->set == ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS) {
2062 /* Color attachment binding */
2063 assert(stage == MESA_SHADER_FRAGMENT);
2064 assert(binding->binding == 0);
2065 if (binding->index < subpass->color_count) {
2066 const unsigned att =
2067 subpass->color_attachments[binding->index].attachment;
2068
2069 /* From the Vulkan 1.0.46 spec:
2070 *
2071 * "If any color or depth/stencil attachments are
2072 * VK_ATTACHMENT_UNUSED, then no writes occur for those
2073 * attachments."
2074 */
2075 if (att == VK_ATTACHMENT_UNUSED) {
2076 surface_state = cmd_buffer->state.null_surface_state;
2077 } else {
2078 surface_state = cmd_buffer->state.attachments[att].color.state;
2079 }
2080 } else {
2081 surface_state = cmd_buffer->state.null_surface_state;
2082 }
2083
2084 bt_map[s] = surface_state.offset + state_offset;
2085 continue;
2086 } else if (binding->set == ANV_DESCRIPTOR_SET_SHADER_CONSTANTS) {
2087 struct anv_state surface_state =
2088 anv_cmd_buffer_alloc_surface_state(cmd_buffer);
2089
2090 struct anv_address constant_data = {
2091 .bo = pipeline->device->dynamic_state_pool.block_pool.bo,
2092 .offset = pipeline->shaders[stage]->constant_data.offset,
2093 };
2094 unsigned constant_data_size =
2095 pipeline->shaders[stage]->constant_data_size;
2096
2097 const enum isl_format format =
2098 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
2099 anv_fill_buffer_surface_state(cmd_buffer->device,
2100 surface_state, format,
2101 constant_data, constant_data_size, 1);
2102
2103 bt_map[s] = surface_state.offset + state_offset;
2104 add_surface_reloc(cmd_buffer, surface_state, constant_data);
2105 continue;
2106 } else if (binding->set == ANV_DESCRIPTOR_SET_NUM_WORK_GROUPS) {
2107 /* This is always the first binding for compute shaders */
2108 assert(stage == MESA_SHADER_COMPUTE && s == 0);
2109 if (!get_cs_prog_data(pipeline)->uses_num_work_groups)
2110 continue;
2111
2112 struct anv_state surface_state =
2113 anv_cmd_buffer_alloc_surface_state(cmd_buffer);
2114
2115 const enum isl_format format =
2116 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
2117 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
2118 format,
2119 cmd_buffer->state.compute.num_workgroups,
2120 12, 1);
2121 bt_map[s] = surface_state.offset + state_offset;
2122 add_surface_reloc(cmd_buffer, surface_state,
2123 cmd_buffer->state.compute.num_workgroups);
2124 continue;
2125 } else if (binding->set == ANV_DESCRIPTOR_SET_DESCRIPTORS) {
2126 /* This is a descriptor set buffer so the set index is actually
2127 * given by binding->binding. (Yes, that's confusing.)
2128 */
2129 struct anv_descriptor_set *set =
2130 pipe_state->descriptors[binding->binding];
2131 assert(set->desc_mem.alloc_size);
2132 assert(set->desc_surface_state.alloc_size);
2133 bt_map[s] = set->desc_surface_state.offset + state_offset;
2134 add_surface_reloc(cmd_buffer, set->desc_surface_state,
2135 anv_descriptor_set_address(cmd_buffer, set));
2136 continue;
2137 }
2138
2139 const struct anv_descriptor *desc =
2140 anv_descriptor_for_binding(pipe_state, binding);
2141
2142 switch (desc->type) {
2143 case VK_DESCRIPTOR_TYPE_SAMPLER:
2144 /* Nothing for us to do here */
2145 continue;
2146
2147 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
2148 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: {
2149 struct anv_surface_state sstate =
2150 (desc->layout == VK_IMAGE_LAYOUT_GENERAL) ?
2151 desc->image_view->planes[binding->plane].general_sampler_surface_state :
2152 desc->image_view->planes[binding->plane].optimal_sampler_surface_state;
2153 surface_state = sstate.state;
2154 assert(surface_state.alloc_size);
2155 add_surface_state_relocs(cmd_buffer, sstate);
2156 break;
2157 }
2158 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2159 assert(stage == MESA_SHADER_FRAGMENT);
2160 if ((desc->image_view->aspect_mask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0) {
2161 /* For depth and stencil input attachments, we treat it like any
2162 * old texture that a user may have bound.
2163 */
2164 struct anv_surface_state sstate =
2165 (desc->layout == VK_IMAGE_LAYOUT_GENERAL) ?
2166 desc->image_view->planes[binding->plane].general_sampler_surface_state :
2167 desc->image_view->planes[binding->plane].optimal_sampler_surface_state;
2168 surface_state = sstate.state;
2169 assert(surface_state.alloc_size);
2170 add_surface_state_relocs(cmd_buffer, sstate);
2171 } else {
2172 /* For color input attachments, we create the surface state at
2173 * vkBeginRenderPass time so that we can include aux and clear
2174 * color information.
2175 */
2176 assert(binding->input_attachment_index < subpass->input_count);
2177 const unsigned subpass_att = binding->input_attachment_index;
2178 const unsigned att = subpass->input_attachments[subpass_att].attachment;
2179 surface_state = cmd_buffer->state.attachments[att].input.state;
2180 }
2181 break;
2182
2183 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
2184 struct anv_surface_state sstate = (binding->write_only)
2185 ? desc->image_view->planes[binding->plane].writeonly_storage_surface_state
2186 : desc->image_view->planes[binding->plane].storage_surface_state;
2187 surface_state = sstate.state;
2188 assert(surface_state.alloc_size);
2189 add_surface_state_relocs(cmd_buffer, sstate);
2190 if (devinfo->gen < 9) {
2191 /* We only need the image params on gen8 and earlier. No image
2192 * workarounds that require tiling information are required on
2193 * SKL and above.
2194 */
2195 assert(image < MAX_GEN8_IMAGES);
2196 struct brw_image_param *image_param =
2197 &cmd_buffer->state.push_constants[stage]->images[image++];
2198
2199 *image_param =
2200 desc->image_view->planes[binding->plane].storage_image_param;
2201 }
2202 break;
2203 }
2204
2205 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2206 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2207 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2208 surface_state = desc->buffer_view->surface_state;
2209 assert(surface_state.alloc_size);
2210 add_surface_reloc(cmd_buffer, surface_state,
2211 desc->buffer_view->address);
2212 break;
2213
2214 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2215 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2216 /* Compute the offset within the buffer */
2217 uint32_t dynamic_offset =
2218 dynamic_offset_for_binding(pipe_state, binding);
2219 uint64_t offset = desc->offset + dynamic_offset;
2220 /* Clamp to the buffer size */
2221 offset = MIN2(offset, desc->buffer->size);
2222 /* Clamp the range to the buffer size */
2223 uint32_t range = MIN2(desc->range, desc->buffer->size - offset);
2224
2225 struct anv_address address =
2226 anv_address_add(desc->buffer->address, offset);
2227
2228 surface_state =
2229 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
2230 enum isl_format format =
2231 anv_isl_format_for_descriptor_type(desc->type);
2232
2233 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
2234 format, address, range, 1);
2235 add_surface_reloc(cmd_buffer, surface_state, address);
2236 break;
2237 }
2238
2239 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2240 surface_state = (binding->write_only)
2241 ? desc->buffer_view->writeonly_storage_surface_state
2242 : desc->buffer_view->storage_surface_state;
2243 assert(surface_state.alloc_size);
2244 add_surface_reloc(cmd_buffer, surface_state,
2245 desc->buffer_view->address);
2246 if (devinfo->gen < 9) {
2247 assert(image < MAX_GEN8_IMAGES);
2248 struct brw_image_param *image_param =
2249 &cmd_buffer->state.push_constants[stage]->images[image++];
2250
2251 *image_param = desc->buffer_view->storage_image_param;
2252 }
2253 break;
2254
2255 default:
2256 assert(!"Invalid descriptor type");
2257 continue;
2258 }
2259
2260 bt_map[s] = surface_state.offset + state_offset;
2261 }
2262 assert(image == map->image_param_count);
2263
2264 #if GEN_GEN >= 11
2265 /* The PIPE_CONTROL command description says:
2266 *
2267 * "Whenever a Binding Table Index (BTI) used by a Render Taget Message
2268 * points to a different RENDER_SURFACE_STATE, SW must issue a Render
2269 * Target Cache Flush by enabling this bit. When render target flush
2270 * is set due to new association of BTI, PS Scoreboard Stall bit must
2271 * be set in this packet."
2272 *
2273 * FINISHME: Currently we shuffle around the surface states in the binding
2274 * table based on if they are getting used or not. So, we've to do below
2275 * pipe control flush for every binding table upload. Make changes so
2276 * that we do it only when we modify render target surface states.
2277 */
2278 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
2279 pc.RenderTargetCacheFlushEnable = true;
2280 pc.StallAtPixelScoreboard = true;
2281 }
2282 #endif
2283
2284 return VK_SUCCESS;
2285 }
2286
2287 static VkResult
2288 emit_samplers(struct anv_cmd_buffer *cmd_buffer,
2289 gl_shader_stage stage,
2290 struct anv_state *state)
2291 {
2292 struct anv_cmd_pipeline_state *pipe_state =
2293 stage == MESA_SHADER_COMPUTE ? &cmd_buffer->state.compute.base :
2294 &cmd_buffer->state.gfx.base;
2295 struct anv_pipeline *pipeline = pipe_state->pipeline;
2296
2297 if (!anv_pipeline_has_stage(pipeline, stage)) {
2298 *state = (struct anv_state) { 0, };
2299 return VK_SUCCESS;
2300 }
2301
2302 struct anv_pipeline_bind_map *map = &pipeline->shaders[stage]->bind_map;
2303 if (map->sampler_count == 0) {
2304 *state = (struct anv_state) { 0, };
2305 return VK_SUCCESS;
2306 }
2307
2308 uint32_t size = map->sampler_count * 16;
2309 *state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, 32);
2310
2311 if (state->map == NULL)
2312 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2313
2314 for (uint32_t s = 0; s < map->sampler_count; s++) {
2315 struct anv_pipeline_binding *binding = &map->sampler_to_descriptor[s];
2316 const struct anv_descriptor *desc =
2317 anv_descriptor_for_binding(pipe_state, binding);
2318
2319 if (desc->type != VK_DESCRIPTOR_TYPE_SAMPLER &&
2320 desc->type != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
2321 continue;
2322
2323 struct anv_sampler *sampler = desc->sampler;
2324
2325 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
2326 * happens to be zero.
2327 */
2328 if (sampler == NULL)
2329 continue;
2330
2331 memcpy(state->map + (s * 16),
2332 sampler->state[binding->plane], sizeof(sampler->state[0]));
2333 }
2334
2335 return VK_SUCCESS;
2336 }
2337
2338 static uint32_t
2339 flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer)
2340 {
2341 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2342
2343 VkShaderStageFlags dirty = cmd_buffer->state.descriptors_dirty &
2344 pipeline->active_stages;
2345
2346 VkResult result = VK_SUCCESS;
2347 anv_foreach_stage(s, dirty) {
2348 result = emit_samplers(cmd_buffer, s, &cmd_buffer->state.samplers[s]);
2349 if (result != VK_SUCCESS)
2350 break;
2351 result = emit_binding_table(cmd_buffer, s,
2352 &cmd_buffer->state.binding_tables[s]);
2353 if (result != VK_SUCCESS)
2354 break;
2355 }
2356
2357 if (result != VK_SUCCESS) {
2358 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
2359
2360 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
2361 if (result != VK_SUCCESS)
2362 return 0;
2363
2364 /* Re-emit state base addresses so we get the new surface state base
2365 * address before we start emitting binding tables etc.
2366 */
2367 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
2368
2369 /* Re-emit all active binding tables */
2370 dirty |= pipeline->active_stages;
2371 anv_foreach_stage(s, dirty) {
2372 result = emit_samplers(cmd_buffer, s, &cmd_buffer->state.samplers[s]);
2373 if (result != VK_SUCCESS) {
2374 anv_batch_set_error(&cmd_buffer->batch, result);
2375 return 0;
2376 }
2377 result = emit_binding_table(cmd_buffer, s,
2378 &cmd_buffer->state.binding_tables[s]);
2379 if (result != VK_SUCCESS) {
2380 anv_batch_set_error(&cmd_buffer->batch, result);
2381 return 0;
2382 }
2383 }
2384 }
2385
2386 cmd_buffer->state.descriptors_dirty &= ~dirty;
2387
2388 return dirty;
2389 }
2390
2391 static void
2392 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer *cmd_buffer,
2393 uint32_t stages)
2394 {
2395 static const uint32_t sampler_state_opcodes[] = {
2396 [MESA_SHADER_VERTEX] = 43,
2397 [MESA_SHADER_TESS_CTRL] = 44, /* HS */
2398 [MESA_SHADER_TESS_EVAL] = 45, /* DS */
2399 [MESA_SHADER_GEOMETRY] = 46,
2400 [MESA_SHADER_FRAGMENT] = 47,
2401 [MESA_SHADER_COMPUTE] = 0,
2402 };
2403
2404 static const uint32_t binding_table_opcodes[] = {
2405 [MESA_SHADER_VERTEX] = 38,
2406 [MESA_SHADER_TESS_CTRL] = 39,
2407 [MESA_SHADER_TESS_EVAL] = 40,
2408 [MESA_SHADER_GEOMETRY] = 41,
2409 [MESA_SHADER_FRAGMENT] = 42,
2410 [MESA_SHADER_COMPUTE] = 0,
2411 };
2412
2413 anv_foreach_stage(s, stages) {
2414 assert(s < ARRAY_SIZE(binding_table_opcodes));
2415 assert(binding_table_opcodes[s] > 0);
2416
2417 if (cmd_buffer->state.samplers[s].alloc_size > 0) {
2418 anv_batch_emit(&cmd_buffer->batch,
2419 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ssp) {
2420 ssp._3DCommandSubOpcode = sampler_state_opcodes[s];
2421 ssp.PointertoVSSamplerState = cmd_buffer->state.samplers[s].offset;
2422 }
2423 }
2424
2425 /* Always emit binding table pointers if we're asked to, since on SKL
2426 * this is what flushes push constants. */
2427 anv_batch_emit(&cmd_buffer->batch,
2428 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), btp) {
2429 btp._3DCommandSubOpcode = binding_table_opcodes[s];
2430 btp.PointertoVSBindingTable = cmd_buffer->state.binding_tables[s].offset;
2431 }
2432 }
2433 }
2434
2435 static void
2436 cmd_buffer_flush_push_constants(struct anv_cmd_buffer *cmd_buffer,
2437 VkShaderStageFlags dirty_stages)
2438 {
2439 const struct anv_cmd_graphics_state *gfx_state = &cmd_buffer->state.gfx;
2440 const struct anv_pipeline *pipeline = gfx_state->base.pipeline;
2441
2442 static const uint32_t push_constant_opcodes[] = {
2443 [MESA_SHADER_VERTEX] = 21,
2444 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
2445 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
2446 [MESA_SHADER_GEOMETRY] = 22,
2447 [MESA_SHADER_FRAGMENT] = 23,
2448 [MESA_SHADER_COMPUTE] = 0,
2449 };
2450
2451 VkShaderStageFlags flushed = 0;
2452
2453 anv_foreach_stage(stage, dirty_stages) {
2454 assert(stage < ARRAY_SIZE(push_constant_opcodes));
2455 assert(push_constant_opcodes[stage] > 0);
2456
2457 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CONSTANT_VS), c) {
2458 c._3DCommandSubOpcode = push_constant_opcodes[stage];
2459
2460 if (anv_pipeline_has_stage(pipeline, stage)) {
2461 #if GEN_GEN >= 8 || GEN_IS_HASWELL
2462 const struct brw_stage_prog_data *prog_data =
2463 pipeline->shaders[stage]->prog_data;
2464 const struct anv_pipeline_bind_map *bind_map =
2465 &pipeline->shaders[stage]->bind_map;
2466
2467 /* The Skylake PRM contains the following restriction:
2468 *
2469 * "The driver must ensure The following case does not occur
2470 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
2471 * buffer 3 read length equal to zero committed followed by a
2472 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
2473 * zero committed."
2474 *
2475 * To avoid this, we program the buffers in the highest slots.
2476 * This way, slot 0 is only used if slot 3 is also used.
2477 */
2478 int n = 3;
2479
2480 for (int i = 3; i >= 0; i--) {
2481 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
2482 if (range->length == 0)
2483 continue;
2484
2485 const unsigned surface =
2486 prog_data->binding_table.ubo_start + range->block;
2487
2488 assert(surface <= bind_map->surface_count);
2489 const struct anv_pipeline_binding *binding =
2490 &bind_map->surface_to_descriptor[surface];
2491
2492 struct anv_address read_addr;
2493 uint32_t read_len;
2494 if (binding->set == ANV_DESCRIPTOR_SET_SHADER_CONSTANTS) {
2495 struct anv_address constant_data = {
2496 .bo = pipeline->device->dynamic_state_pool.block_pool.bo,
2497 .offset = pipeline->shaders[stage]->constant_data.offset,
2498 };
2499 unsigned constant_data_size =
2500 pipeline->shaders[stage]->constant_data_size;
2501
2502 read_len = MIN2(range->length,
2503 DIV_ROUND_UP(constant_data_size, 32) - range->start);
2504 read_addr = anv_address_add(constant_data,
2505 range->start * 32);
2506 } else if (binding->set == ANV_DESCRIPTOR_SET_DESCRIPTORS) {
2507 /* This is a descriptor set buffer so the set index is
2508 * actually given by binding->binding. (Yes, that's
2509 * confusing.)
2510 */
2511 struct anv_descriptor_set *set =
2512 gfx_state->base.descriptors[binding->binding];
2513 struct anv_address desc_buffer_addr =
2514 anv_descriptor_set_address(cmd_buffer, set);
2515 const unsigned desc_buffer_size = set->desc_mem.alloc_size;
2516
2517 read_len = MIN2(range->length,
2518 DIV_ROUND_UP(desc_buffer_size, 32) - range->start);
2519 read_addr = anv_address_add(desc_buffer_addr,
2520 range->start * 32);
2521 } else {
2522 const struct anv_descriptor *desc =
2523 anv_descriptor_for_binding(&gfx_state->base, binding);
2524
2525 if (desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) {
2526 read_len = MIN2(range->length,
2527 DIV_ROUND_UP(desc->buffer_view->range, 32) - range->start);
2528 read_addr = anv_address_add(desc->buffer_view->address,
2529 range->start * 32);
2530 } else {
2531 assert(desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
2532
2533 uint32_t dynamic_offset =
2534 dynamic_offset_for_binding(&gfx_state->base, binding);
2535 uint32_t buf_offset =
2536 MIN2(desc->offset + dynamic_offset, desc->buffer->size);
2537 uint32_t buf_range =
2538 MIN2(desc->range, desc->buffer->size - buf_offset);
2539
2540 read_len = MIN2(range->length,
2541 DIV_ROUND_UP(buf_range, 32) - range->start);
2542 read_addr = anv_address_add(desc->buffer->address,
2543 buf_offset + range->start * 32);
2544 }
2545 }
2546
2547 if (read_len > 0) {
2548 c.ConstantBody.Buffer[n] = read_addr;
2549 c.ConstantBody.ReadLength[n] = read_len;
2550 n--;
2551 }
2552 }
2553
2554 struct anv_state state =
2555 anv_cmd_buffer_push_constants(cmd_buffer, stage);
2556
2557 if (state.alloc_size > 0) {
2558 c.ConstantBody.Buffer[n] = (struct anv_address) {
2559 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2560 .offset = state.offset,
2561 };
2562 c.ConstantBody.ReadLength[n] =
2563 DIV_ROUND_UP(state.alloc_size, 32);
2564 }
2565 #else
2566 /* For Ivy Bridge, the push constants packets have a different
2567 * rule that would require us to iterate in the other direction
2568 * and possibly mess around with dynamic state base address.
2569 * Don't bother; just emit regular push constants at n = 0.
2570 */
2571 struct anv_state state =
2572 anv_cmd_buffer_push_constants(cmd_buffer, stage);
2573
2574 if (state.alloc_size > 0) {
2575 c.ConstantBody.Buffer[0].offset = state.offset,
2576 c.ConstantBody.ReadLength[0] =
2577 DIV_ROUND_UP(state.alloc_size, 32);
2578 }
2579 #endif
2580 }
2581 }
2582
2583 flushed |= mesa_to_vk_shader_stage(stage);
2584 }
2585
2586 cmd_buffer->state.push_constants_dirty &= ~flushed;
2587 }
2588
2589 void
2590 genX(cmd_buffer_flush_state)(struct anv_cmd_buffer *cmd_buffer)
2591 {
2592 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2593 uint32_t *p;
2594
2595 uint32_t vb_emit = cmd_buffer->state.gfx.vb_dirty & pipeline->vb_used;
2596 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_PIPELINE)
2597 vb_emit |= pipeline->vb_used;
2598
2599 assert((pipeline->active_stages & VK_SHADER_STAGE_COMPUTE_BIT) == 0);
2600
2601 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
2602
2603 genX(flush_pipeline_select_3d)(cmd_buffer);
2604
2605 if (vb_emit) {
2606 const uint32_t num_buffers = __builtin_popcount(vb_emit);
2607 const uint32_t num_dwords = 1 + num_buffers * 4;
2608
2609 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
2610 GENX(3DSTATE_VERTEX_BUFFERS));
2611 uint32_t vb, i = 0;
2612 for_each_bit(vb, vb_emit) {
2613 struct anv_buffer *buffer = cmd_buffer->state.vertex_bindings[vb].buffer;
2614 uint32_t offset = cmd_buffer->state.vertex_bindings[vb].offset;
2615
2616 struct GENX(VERTEX_BUFFER_STATE) state = {
2617 .VertexBufferIndex = vb,
2618
2619 .MOCS = anv_mocs_for_bo(cmd_buffer->device, buffer->address.bo),
2620 #if GEN_GEN <= 7
2621 .BufferAccessType = pipeline->vb[vb].instanced ? INSTANCEDATA : VERTEXDATA,
2622 .InstanceDataStepRate = pipeline->vb[vb].instance_divisor,
2623 #endif
2624
2625 .AddressModifyEnable = true,
2626 .BufferPitch = pipeline->vb[vb].stride,
2627 .BufferStartingAddress = anv_address_add(buffer->address, offset),
2628
2629 #if GEN_GEN >= 8
2630 .BufferSize = buffer->size - offset
2631 #else
2632 .EndAddress = anv_address_add(buffer->address, buffer->size - 1),
2633 #endif
2634 };
2635
2636 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, &p[1 + i * 4], &state);
2637 i++;
2638 }
2639 }
2640
2641 cmd_buffer->state.gfx.vb_dirty &= ~vb_emit;
2642
2643 #if GEN_GEN >= 8
2644 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_XFB_ENABLE) {
2645 /* We don't need any per-buffer dirty tracking because you're not
2646 * allowed to bind different XFB buffers while XFB is enabled.
2647 */
2648 for (unsigned idx = 0; idx < MAX_XFB_BUFFERS; idx++) {
2649 struct anv_xfb_binding *xfb = &cmd_buffer->state.xfb_bindings[idx];
2650 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_SO_BUFFER), sob) {
2651 sob.SOBufferIndex = idx;
2652
2653 if (cmd_buffer->state.xfb_enabled && xfb->buffer && xfb->size != 0) {
2654 sob.SOBufferEnable = true;
2655 sob.MOCS = cmd_buffer->device->default_mocs,
2656 sob.StreamOffsetWriteEnable = false;
2657 sob.SurfaceBaseAddress = anv_address_add(xfb->buffer->address,
2658 xfb->offset);
2659 /* Size is in DWords - 1 */
2660 sob.SurfaceSize = xfb->size / 4 - 1;
2661 }
2662 }
2663 }
2664
2665 /* CNL and later require a CS stall after 3DSTATE_SO_BUFFER */
2666 if (GEN_GEN >= 10)
2667 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
2668 }
2669 #endif
2670
2671 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_PIPELINE) {
2672 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
2673
2674 /* The exact descriptor layout is pulled from the pipeline, so we need
2675 * to re-emit binding tables on every pipeline change.
2676 */
2677 cmd_buffer->state.descriptors_dirty |= pipeline->active_stages;
2678
2679 /* If the pipeline changed, we may need to re-allocate push constant
2680 * space in the URB.
2681 */
2682 cmd_buffer_alloc_push_constants(cmd_buffer);
2683 }
2684
2685 #if GEN_GEN <= 7
2686 if (cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_VERTEX_BIT ||
2687 cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_VERTEX_BIT) {
2688 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
2689 *
2690 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
2691 * stall needs to be sent just prior to any 3DSTATE_VS,
2692 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
2693 * 3DSTATE_BINDING_TABLE_POINTER_VS,
2694 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
2695 * PIPE_CONTROL needs to be sent before any combination of VS
2696 * associated 3DSTATE."
2697 */
2698 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
2699 pc.DepthStallEnable = true;
2700 pc.PostSyncOperation = WriteImmediateData;
2701 pc.Address =
2702 (struct anv_address) { &cmd_buffer->device->workaround_bo, 0 };
2703 }
2704 }
2705 #endif
2706
2707 /* Render targets live in the same binding table as fragment descriptors */
2708 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_RENDER_TARGETS)
2709 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_FRAGMENT_BIT;
2710
2711 /* We emit the binding tables and sampler tables first, then emit push
2712 * constants and then finally emit binding table and sampler table
2713 * pointers. It has to happen in this order, since emitting the binding
2714 * tables may change the push constants (in case of storage images). After
2715 * emitting push constants, on SKL+ we have to emit the corresponding
2716 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
2717 */
2718 uint32_t dirty = 0;
2719 if (cmd_buffer->state.descriptors_dirty)
2720 dirty = flush_descriptor_sets(cmd_buffer);
2721
2722 if (dirty || cmd_buffer->state.push_constants_dirty) {
2723 /* Because we're pushing UBOs, we have to push whenever either
2724 * descriptors or push constants is dirty.
2725 */
2726 dirty |= cmd_buffer->state.push_constants_dirty;
2727 dirty &= ANV_STAGE_MASK & VK_SHADER_STAGE_ALL_GRAPHICS;
2728 cmd_buffer_flush_push_constants(cmd_buffer, dirty);
2729 }
2730
2731 if (dirty)
2732 cmd_buffer_emit_descriptor_pointers(cmd_buffer, dirty);
2733
2734 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_VIEWPORT)
2735 gen8_cmd_buffer_emit_viewport(cmd_buffer);
2736
2737 if (cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT |
2738 ANV_CMD_DIRTY_PIPELINE)) {
2739 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer,
2740 pipeline->depth_clamp_enable);
2741 }
2742
2743 if (cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_DYNAMIC_SCISSOR |
2744 ANV_CMD_DIRTY_RENDER_TARGETS))
2745 gen7_cmd_buffer_emit_scissor(cmd_buffer);
2746
2747 genX(cmd_buffer_flush_dynamic_state)(cmd_buffer);
2748
2749 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
2750 }
2751
2752 static void
2753 emit_vertex_bo(struct anv_cmd_buffer *cmd_buffer,
2754 struct anv_address addr,
2755 uint32_t size, uint32_t index)
2756 {
2757 uint32_t *p = anv_batch_emitn(&cmd_buffer->batch, 5,
2758 GENX(3DSTATE_VERTEX_BUFFERS));
2759
2760 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, p + 1,
2761 &(struct GENX(VERTEX_BUFFER_STATE)) {
2762 .VertexBufferIndex = index,
2763 .AddressModifyEnable = true,
2764 .BufferPitch = 0,
2765 .MOCS = anv_mocs_for_bo(cmd_buffer->device, addr.bo),
2766 #if (GEN_GEN >= 8)
2767 .BufferStartingAddress = addr,
2768 .BufferSize = size
2769 #else
2770 .BufferStartingAddress = addr,
2771 .EndAddress = anv_address_add(addr, size),
2772 #endif
2773 });
2774 }
2775
2776 static void
2777 emit_base_vertex_instance_bo(struct anv_cmd_buffer *cmd_buffer,
2778 struct anv_address addr)
2779 {
2780 emit_vertex_bo(cmd_buffer, addr, 8, ANV_SVGS_VB_INDEX);
2781 }
2782
2783 static void
2784 emit_base_vertex_instance(struct anv_cmd_buffer *cmd_buffer,
2785 uint32_t base_vertex, uint32_t base_instance)
2786 {
2787 struct anv_state id_state =
2788 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 8, 4);
2789
2790 ((uint32_t *)id_state.map)[0] = base_vertex;
2791 ((uint32_t *)id_state.map)[1] = base_instance;
2792
2793 struct anv_address addr = {
2794 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2795 .offset = id_state.offset,
2796 };
2797
2798 emit_base_vertex_instance_bo(cmd_buffer, addr);
2799 }
2800
2801 static void
2802 emit_draw_index(struct anv_cmd_buffer *cmd_buffer, uint32_t draw_index)
2803 {
2804 struct anv_state state =
2805 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 4, 4);
2806
2807 ((uint32_t *)state.map)[0] = draw_index;
2808
2809 struct anv_address addr = {
2810 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2811 .offset = state.offset,
2812 };
2813
2814 emit_vertex_bo(cmd_buffer, addr, 4, ANV_DRAWID_VB_INDEX);
2815 }
2816
2817 void genX(CmdDraw)(
2818 VkCommandBuffer commandBuffer,
2819 uint32_t vertexCount,
2820 uint32_t instanceCount,
2821 uint32_t firstVertex,
2822 uint32_t firstInstance)
2823 {
2824 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2825 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2826 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2827
2828 if (anv_batch_has_error(&cmd_buffer->batch))
2829 return;
2830
2831 genX(cmd_buffer_flush_state)(cmd_buffer);
2832
2833 if (cmd_buffer->state.conditional_render_enabled)
2834 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
2835
2836 if (vs_prog_data->uses_firstvertex ||
2837 vs_prog_data->uses_baseinstance)
2838 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
2839 if (vs_prog_data->uses_drawid)
2840 emit_draw_index(cmd_buffer, 0);
2841
2842 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2843 * different views. We need to multiply instanceCount by the view count.
2844 */
2845 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
2846
2847 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
2848 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
2849 prim.VertexAccessType = SEQUENTIAL;
2850 prim.PrimitiveTopologyType = pipeline->topology;
2851 prim.VertexCountPerInstance = vertexCount;
2852 prim.StartVertexLocation = firstVertex;
2853 prim.InstanceCount = instanceCount;
2854 prim.StartInstanceLocation = firstInstance;
2855 prim.BaseVertexLocation = 0;
2856 }
2857 }
2858
2859 void genX(CmdDrawIndexed)(
2860 VkCommandBuffer commandBuffer,
2861 uint32_t indexCount,
2862 uint32_t instanceCount,
2863 uint32_t firstIndex,
2864 int32_t vertexOffset,
2865 uint32_t firstInstance)
2866 {
2867 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2868 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2869 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2870
2871 if (anv_batch_has_error(&cmd_buffer->batch))
2872 return;
2873
2874 genX(cmd_buffer_flush_state)(cmd_buffer);
2875
2876 if (cmd_buffer->state.conditional_render_enabled)
2877 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
2878
2879 if (vs_prog_data->uses_firstvertex ||
2880 vs_prog_data->uses_baseinstance)
2881 emit_base_vertex_instance(cmd_buffer, vertexOffset, firstInstance);
2882 if (vs_prog_data->uses_drawid)
2883 emit_draw_index(cmd_buffer, 0);
2884
2885 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2886 * different views. We need to multiply instanceCount by the view count.
2887 */
2888 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
2889
2890 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
2891 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
2892 prim.VertexAccessType = RANDOM;
2893 prim.PrimitiveTopologyType = pipeline->topology;
2894 prim.VertexCountPerInstance = indexCount;
2895 prim.StartVertexLocation = firstIndex;
2896 prim.InstanceCount = instanceCount;
2897 prim.StartInstanceLocation = firstInstance;
2898 prim.BaseVertexLocation = vertexOffset;
2899 }
2900 }
2901
2902 /* Auto-Draw / Indirect Registers */
2903 #define GEN7_3DPRIM_END_OFFSET 0x2420
2904 #define GEN7_3DPRIM_START_VERTEX 0x2430
2905 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
2906 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
2907 #define GEN7_3DPRIM_START_INSTANCE 0x243C
2908 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
2909
2910 void genX(CmdDrawIndirectByteCountEXT)(
2911 VkCommandBuffer commandBuffer,
2912 uint32_t instanceCount,
2913 uint32_t firstInstance,
2914 VkBuffer counterBuffer,
2915 VkDeviceSize counterBufferOffset,
2916 uint32_t counterOffset,
2917 uint32_t vertexStride)
2918 {
2919 #if GEN_IS_HASWELL || GEN_GEN >= 8
2920 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2921 ANV_FROM_HANDLE(anv_buffer, counter_buffer, counterBuffer);
2922 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2923 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2924
2925 /* firstVertex is always zero for this draw function */
2926 const uint32_t firstVertex = 0;
2927
2928 if (anv_batch_has_error(&cmd_buffer->batch))
2929 return;
2930
2931 genX(cmd_buffer_flush_state)(cmd_buffer);
2932
2933 if (vs_prog_data->uses_firstvertex ||
2934 vs_prog_data->uses_baseinstance)
2935 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
2936 if (vs_prog_data->uses_drawid)
2937 emit_draw_index(cmd_buffer, 0);
2938
2939 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2940 * different views. We need to multiply instanceCount by the view count.
2941 */
2942 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
2943
2944 struct gen_mi_builder b;
2945 gen_mi_builder_init(&b, &cmd_buffer->batch);
2946 struct gen_mi_value count =
2947 gen_mi_mem32(anv_address_add(counter_buffer->address,
2948 counterBufferOffset));
2949 if (counterOffset)
2950 count = gen_mi_iadd(&b, count, gen_mi_imm(-counterOffset));
2951 count = gen_mi_udiv32_imm(&b, count, vertexStride);
2952 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_VERTEX_COUNT), count);
2953
2954 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_VERTEX),
2955 gen_mi_imm(firstVertex));
2956 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_INSTANCE_COUNT),
2957 gen_mi_imm(instanceCount));
2958 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE),
2959 gen_mi_imm(firstInstance));
2960 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX), gen_mi_imm(0));
2961
2962 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
2963 prim.IndirectParameterEnable = true;
2964 prim.VertexAccessType = SEQUENTIAL;
2965 prim.PrimitiveTopologyType = pipeline->topology;
2966 }
2967 #endif /* GEN_IS_HASWELL || GEN_GEN >= 8 */
2968 }
2969
2970 static void
2971 load_indirect_parameters(struct anv_cmd_buffer *cmd_buffer,
2972 struct anv_address addr,
2973 bool indexed)
2974 {
2975 struct gen_mi_builder b;
2976 gen_mi_builder_init(&b, &cmd_buffer->batch);
2977
2978 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_VERTEX_COUNT),
2979 gen_mi_mem32(anv_address_add(addr, 0)));
2980
2981 struct gen_mi_value instance_count = gen_mi_mem32(anv_address_add(addr, 4));
2982 unsigned view_count = anv_subpass_view_count(cmd_buffer->state.subpass);
2983 if (view_count > 1) {
2984 #if GEN_IS_HASWELL || GEN_GEN >= 8
2985 instance_count = gen_mi_imul_imm(&b, instance_count, view_count);
2986 #else
2987 anv_finishme("Multiview + indirect draw requires MI_MATH; "
2988 "MI_MATH is not supported on Ivy Bridge");
2989 #endif
2990 }
2991 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_INSTANCE_COUNT), instance_count);
2992
2993 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_VERTEX),
2994 gen_mi_mem32(anv_address_add(addr, 8)));
2995
2996 if (indexed) {
2997 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX),
2998 gen_mi_mem32(anv_address_add(addr, 12)));
2999 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE),
3000 gen_mi_mem32(anv_address_add(addr, 16)));
3001 } else {
3002 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_START_INSTANCE),
3003 gen_mi_mem32(anv_address_add(addr, 12)));
3004 gen_mi_store(&b, gen_mi_reg32(GEN7_3DPRIM_BASE_VERTEX), gen_mi_imm(0));
3005 }
3006 }
3007
3008 void genX(CmdDrawIndirect)(
3009 VkCommandBuffer commandBuffer,
3010 VkBuffer _buffer,
3011 VkDeviceSize offset,
3012 uint32_t drawCount,
3013 uint32_t stride)
3014 {
3015 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3016 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3017 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
3018 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3019
3020 if (anv_batch_has_error(&cmd_buffer->batch))
3021 return;
3022
3023 genX(cmd_buffer_flush_state)(cmd_buffer);
3024
3025 if (cmd_buffer->state.conditional_render_enabled)
3026 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3027
3028 for (uint32_t i = 0; i < drawCount; i++) {
3029 struct anv_address draw = anv_address_add(buffer->address, offset);
3030
3031 if (vs_prog_data->uses_firstvertex ||
3032 vs_prog_data->uses_baseinstance)
3033 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 8));
3034 if (vs_prog_data->uses_drawid)
3035 emit_draw_index(cmd_buffer, i);
3036
3037 load_indirect_parameters(cmd_buffer, draw, false);
3038
3039 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3040 prim.IndirectParameterEnable = true;
3041 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3042 prim.VertexAccessType = SEQUENTIAL;
3043 prim.PrimitiveTopologyType = pipeline->topology;
3044 }
3045
3046 offset += stride;
3047 }
3048 }
3049
3050 void genX(CmdDrawIndexedIndirect)(
3051 VkCommandBuffer commandBuffer,
3052 VkBuffer _buffer,
3053 VkDeviceSize offset,
3054 uint32_t drawCount,
3055 uint32_t stride)
3056 {
3057 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3058 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3059 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
3060 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3061
3062 if (anv_batch_has_error(&cmd_buffer->batch))
3063 return;
3064
3065 genX(cmd_buffer_flush_state)(cmd_buffer);
3066
3067 if (cmd_buffer->state.conditional_render_enabled)
3068 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3069
3070 for (uint32_t i = 0; i < drawCount; i++) {
3071 struct anv_address draw = anv_address_add(buffer->address, offset);
3072
3073 /* TODO: We need to stomp base vertex to 0 somehow */
3074 if (vs_prog_data->uses_firstvertex ||
3075 vs_prog_data->uses_baseinstance)
3076 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 12));
3077 if (vs_prog_data->uses_drawid)
3078 emit_draw_index(cmd_buffer, i);
3079
3080 load_indirect_parameters(cmd_buffer, draw, true);
3081
3082 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3083 prim.IndirectParameterEnable = true;
3084 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3085 prim.VertexAccessType = RANDOM;
3086 prim.PrimitiveTopologyType = pipeline->topology;
3087 }
3088
3089 offset += stride;
3090 }
3091 }
3092
3093 #define TMP_DRAW_COUNT_REG MI_ALU_REG14
3094
3095 static void
3096 prepare_for_draw_count_predicate(struct anv_cmd_buffer *cmd_buffer,
3097 struct anv_address count_address,
3098 const bool conditional_render_enabled)
3099 {
3100 if (conditional_render_enabled) {
3101 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3102 emit_lrm(&cmd_buffer->batch, CS_GPR(TMP_DRAW_COUNT_REG), count_address);
3103 emit_lri(&cmd_buffer->batch, CS_GPR(TMP_DRAW_COUNT_REG) + 4, 0);
3104 #endif
3105 } else {
3106 /* Upload the current draw count from the draw parameters buffer to
3107 * MI_PREDICATE_SRC0.
3108 */
3109 emit_lrm(&cmd_buffer->batch, MI_PREDICATE_SRC0, count_address);
3110 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC0 + 4, 0);
3111
3112 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1 + 4, 0);
3113 }
3114 }
3115
3116 static void
3117 emit_draw_count_predicate(struct anv_cmd_buffer *cmd_buffer,
3118 uint32_t draw_index)
3119 {
3120 /* Upload the index of the current primitive to MI_PREDICATE_SRC1. */
3121 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1, draw_index);
3122
3123 if (draw_index == 0) {
3124 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
3125 mip.LoadOperation = LOAD_LOADINV;
3126 mip.CombineOperation = COMBINE_SET;
3127 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3128 }
3129 } else {
3130 /* While draw_index < draw_count the predicate's result will be
3131 * (draw_index == draw_count) ^ TRUE = TRUE
3132 * When draw_index == draw_count the result is
3133 * (TRUE) ^ TRUE = FALSE
3134 * After this all results will be:
3135 * (FALSE) ^ FALSE = FALSE
3136 */
3137 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
3138 mip.LoadOperation = LOAD_LOAD;
3139 mip.CombineOperation = COMBINE_XOR;
3140 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3141 }
3142 }
3143 }
3144
3145 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3146 static void
3147 emit_draw_count_predicate_with_conditional_render(
3148 struct anv_cmd_buffer *cmd_buffer,
3149 uint32_t draw_index)
3150 {
3151 const int draw_index_reg = MI_ALU_REG0;
3152 const int tmp_result_reg = MI_ALU_REG1;
3153
3154 emit_lri(&cmd_buffer->batch, CS_GPR(draw_index_reg), draw_index);
3155 emit_lri(&cmd_buffer->batch, CS_GPR(draw_index_reg) + 4, 0);
3156
3157 uint32_t *dw;
3158 /* Compute (draw_index < draw_count).
3159 * We do this by subtracting and storing the carry bit.
3160 */
3161 dw = anv_batch_emitn(&cmd_buffer->batch, 9, GENX(MI_MATH));
3162 dw[1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, draw_index_reg);
3163 dw[2] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, TMP_DRAW_COUNT_REG);
3164 dw[3] = mi_alu(MI_ALU_SUB, 0, 0);
3165 dw[4] = mi_alu(MI_ALU_STORE, tmp_result_reg, MI_ALU_CF);
3166 /* & condition */
3167 dw[5] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, tmp_result_reg);
3168 dw[6] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, ANV_PREDICATE_RESULT_REG);
3169 dw[7] = mi_alu(MI_ALU_AND, 0, 0);
3170 dw[8] = mi_alu(MI_ALU_STORE, tmp_result_reg, MI_ALU_ACCU);
3171
3172 #if GEN_GEN >= 8
3173 emit_lrr(&cmd_buffer->batch, MI_PREDICATE_RESULT, CS_GPR(tmp_result_reg));
3174 #else
3175 /* MI_PREDICATE_RESULT is not whitelisted in i915 command parser
3176 * so we emit MI_PREDICATE to set it.
3177 */
3178
3179 emit_lrr(&cmd_buffer->batch, MI_PREDICATE_SRC0, CS_GPR(tmp_result_reg));
3180 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC0 + 4, 0);
3181 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1, 0);
3182 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1 + 4, 0);
3183
3184 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
3185 mip.LoadOperation = LOAD_LOADINV;
3186 mip.CombineOperation = COMBINE_SET;
3187 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3188 }
3189 #endif
3190 }
3191 #endif
3192
3193 void genX(CmdDrawIndirectCountKHR)(
3194 VkCommandBuffer commandBuffer,
3195 VkBuffer _buffer,
3196 VkDeviceSize offset,
3197 VkBuffer _countBuffer,
3198 VkDeviceSize countBufferOffset,
3199 uint32_t maxDrawCount,
3200 uint32_t stride)
3201 {
3202 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3203 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3204 ANV_FROM_HANDLE(anv_buffer, count_buffer, _countBuffer);
3205 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
3206 struct anv_pipeline *pipeline = cmd_state->gfx.base.pipeline;
3207 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3208
3209 if (anv_batch_has_error(&cmd_buffer->batch))
3210 return;
3211
3212 genX(cmd_buffer_flush_state)(cmd_buffer);
3213
3214 struct anv_address count_address =
3215 anv_address_add(count_buffer->address, countBufferOffset);
3216
3217 prepare_for_draw_count_predicate(cmd_buffer, count_address,
3218 cmd_state->conditional_render_enabled);
3219
3220 for (uint32_t i = 0; i < maxDrawCount; i++) {
3221 struct anv_address draw = anv_address_add(buffer->address, offset);
3222
3223 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3224 if (cmd_state->conditional_render_enabled) {
3225 emit_draw_count_predicate_with_conditional_render(cmd_buffer, i);
3226 } else {
3227 emit_draw_count_predicate(cmd_buffer, i);
3228 }
3229 #else
3230 emit_draw_count_predicate(cmd_buffer, i);
3231 #endif
3232
3233 if (vs_prog_data->uses_firstvertex ||
3234 vs_prog_data->uses_baseinstance)
3235 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 8));
3236 if (vs_prog_data->uses_drawid)
3237 emit_draw_index(cmd_buffer, i);
3238
3239 load_indirect_parameters(cmd_buffer, draw, false);
3240
3241 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3242 prim.IndirectParameterEnable = true;
3243 prim.PredicateEnable = true;
3244 prim.VertexAccessType = SEQUENTIAL;
3245 prim.PrimitiveTopologyType = pipeline->topology;
3246 }
3247
3248 offset += stride;
3249 }
3250 }
3251
3252 void genX(CmdDrawIndexedIndirectCountKHR)(
3253 VkCommandBuffer commandBuffer,
3254 VkBuffer _buffer,
3255 VkDeviceSize offset,
3256 VkBuffer _countBuffer,
3257 VkDeviceSize countBufferOffset,
3258 uint32_t maxDrawCount,
3259 uint32_t stride)
3260 {
3261 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3262 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3263 ANV_FROM_HANDLE(anv_buffer, count_buffer, _countBuffer);
3264 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
3265 struct anv_pipeline *pipeline = cmd_state->gfx.base.pipeline;
3266 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3267
3268 if (anv_batch_has_error(&cmd_buffer->batch))
3269 return;
3270
3271 genX(cmd_buffer_flush_state)(cmd_buffer);
3272
3273 struct anv_address count_address =
3274 anv_address_add(count_buffer->address, countBufferOffset);
3275
3276 prepare_for_draw_count_predicate(cmd_buffer, count_address,
3277 cmd_state->conditional_render_enabled);
3278
3279 for (uint32_t i = 0; i < maxDrawCount; i++) {
3280 struct anv_address draw = anv_address_add(buffer->address, offset);
3281
3282 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3283 if (cmd_state->conditional_render_enabled) {
3284 emit_draw_count_predicate_with_conditional_render(cmd_buffer, i);
3285 } else {
3286 emit_draw_count_predicate(cmd_buffer, i);
3287 }
3288 #else
3289 emit_draw_count_predicate(cmd_buffer, i);
3290 #endif
3291
3292 /* TODO: We need to stomp base vertex to 0 somehow */
3293 if (vs_prog_data->uses_firstvertex ||
3294 vs_prog_data->uses_baseinstance)
3295 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 12));
3296 if (vs_prog_data->uses_drawid)
3297 emit_draw_index(cmd_buffer, i);
3298
3299 load_indirect_parameters(cmd_buffer, draw, true);
3300
3301 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3302 prim.IndirectParameterEnable = true;
3303 prim.PredicateEnable = true;
3304 prim.VertexAccessType = RANDOM;
3305 prim.PrimitiveTopologyType = pipeline->topology;
3306 }
3307
3308 offset += stride;
3309 }
3310 }
3311
3312 void genX(CmdBeginTransformFeedbackEXT)(
3313 VkCommandBuffer commandBuffer,
3314 uint32_t firstCounterBuffer,
3315 uint32_t counterBufferCount,
3316 const VkBuffer* pCounterBuffers,
3317 const VkDeviceSize* pCounterBufferOffsets)
3318 {
3319 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3320
3321 assert(firstCounterBuffer < MAX_XFB_BUFFERS);
3322 assert(counterBufferCount <= MAX_XFB_BUFFERS);
3323 assert(firstCounterBuffer + counterBufferCount <= MAX_XFB_BUFFERS);
3324
3325 /* From the SKL PRM Vol. 2c, SO_WRITE_OFFSET:
3326 *
3327 * "Ssoftware must ensure that no HW stream output operations can be in
3328 * process or otherwise pending at the point that the MI_LOAD/STORE
3329 * commands are processed. This will likely require a pipeline flush."
3330 */
3331 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
3332 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3333
3334 for (uint32_t idx = 0; idx < MAX_XFB_BUFFERS; idx++) {
3335 /* If we have a counter buffer, this is a resume so we need to load the
3336 * value into the streamout offset register. Otherwise, this is a begin
3337 * and we need to reset it to zero.
3338 */
3339 if (pCounterBuffers &&
3340 idx >= firstCounterBuffer &&
3341 idx - firstCounterBuffer < counterBufferCount &&
3342 pCounterBuffers[idx - firstCounterBuffer] != VK_NULL_HANDLE) {
3343 uint32_t cb_idx = idx - firstCounterBuffer;
3344 ANV_FROM_HANDLE(anv_buffer, counter_buffer, pCounterBuffers[cb_idx]);
3345 uint64_t offset = pCounterBufferOffsets ?
3346 pCounterBufferOffsets[cb_idx] : 0;
3347
3348 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
3349 lrm.RegisterAddress = GENX(SO_WRITE_OFFSET0_num) + idx * 4;
3350 lrm.MemoryAddress = anv_address_add(counter_buffer->address,
3351 offset);
3352 }
3353 } else {
3354 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
3355 lri.RegisterOffset = GENX(SO_WRITE_OFFSET0_num) + idx * 4;
3356 lri.DataDWord = 0;
3357 }
3358 }
3359 }
3360
3361 cmd_buffer->state.xfb_enabled = true;
3362 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_XFB_ENABLE;
3363 }
3364
3365 void genX(CmdEndTransformFeedbackEXT)(
3366 VkCommandBuffer commandBuffer,
3367 uint32_t firstCounterBuffer,
3368 uint32_t counterBufferCount,
3369 const VkBuffer* pCounterBuffers,
3370 const VkDeviceSize* pCounterBufferOffsets)
3371 {
3372 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3373
3374 assert(firstCounterBuffer < MAX_XFB_BUFFERS);
3375 assert(counterBufferCount <= MAX_XFB_BUFFERS);
3376 assert(firstCounterBuffer + counterBufferCount <= MAX_XFB_BUFFERS);
3377
3378 /* From the SKL PRM Vol. 2c, SO_WRITE_OFFSET:
3379 *
3380 * "Ssoftware must ensure that no HW stream output operations can be in
3381 * process or otherwise pending at the point that the MI_LOAD/STORE
3382 * commands are processed. This will likely require a pipeline flush."
3383 */
3384 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
3385 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3386
3387 for (uint32_t cb_idx = 0; cb_idx < counterBufferCount; cb_idx++) {
3388 unsigned idx = firstCounterBuffer + cb_idx;
3389
3390 /* If we have a counter buffer, this is a resume so we need to load the
3391 * value into the streamout offset register. Otherwise, this is a begin
3392 * and we need to reset it to zero.
3393 */
3394 if (pCounterBuffers &&
3395 cb_idx < counterBufferCount &&
3396 pCounterBuffers[cb_idx] != VK_NULL_HANDLE) {
3397 ANV_FROM_HANDLE(anv_buffer, counter_buffer, pCounterBuffers[cb_idx]);
3398 uint64_t offset = pCounterBufferOffsets ?
3399 pCounterBufferOffsets[cb_idx] : 0;
3400
3401 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
3402 srm.MemoryAddress = anv_address_add(counter_buffer->address,
3403 offset);
3404 srm.RegisterAddress = GENX(SO_WRITE_OFFSET0_num) + idx * 4;
3405 }
3406 }
3407 }
3408
3409 cmd_buffer->state.xfb_enabled = false;
3410 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_XFB_ENABLE;
3411 }
3412
3413 static VkResult
3414 flush_compute_descriptor_set(struct anv_cmd_buffer *cmd_buffer)
3415 {
3416 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3417 struct anv_state surfaces = { 0, }, samplers = { 0, };
3418 VkResult result;
3419
3420 result = emit_binding_table(cmd_buffer, MESA_SHADER_COMPUTE, &surfaces);
3421 if (result != VK_SUCCESS) {
3422 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
3423
3424 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
3425 if (result != VK_SUCCESS)
3426 return result;
3427
3428 /* Re-emit state base addresses so we get the new surface state base
3429 * address before we start emitting binding tables etc.
3430 */
3431 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
3432
3433 result = emit_binding_table(cmd_buffer, MESA_SHADER_COMPUTE, &surfaces);
3434 if (result != VK_SUCCESS) {
3435 anv_batch_set_error(&cmd_buffer->batch, result);
3436 return result;
3437 }
3438 }
3439
3440 result = emit_samplers(cmd_buffer, MESA_SHADER_COMPUTE, &samplers);
3441 if (result != VK_SUCCESS) {
3442 anv_batch_set_error(&cmd_buffer->batch, result);
3443 return result;
3444 }
3445
3446 uint32_t iface_desc_data_dw[GENX(INTERFACE_DESCRIPTOR_DATA_length)];
3447 struct GENX(INTERFACE_DESCRIPTOR_DATA) desc = {
3448 .BindingTablePointer = surfaces.offset,
3449 .SamplerStatePointer = samplers.offset,
3450 };
3451 GENX(INTERFACE_DESCRIPTOR_DATA_pack)(NULL, iface_desc_data_dw, &desc);
3452
3453 struct anv_state state =
3454 anv_cmd_buffer_merge_dynamic(cmd_buffer, iface_desc_data_dw,
3455 pipeline->interface_descriptor_data,
3456 GENX(INTERFACE_DESCRIPTOR_DATA_length),
3457 64);
3458
3459 uint32_t size = GENX(INTERFACE_DESCRIPTOR_DATA_length) * sizeof(uint32_t);
3460 anv_batch_emit(&cmd_buffer->batch,
3461 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD), mid) {
3462 mid.InterfaceDescriptorTotalLength = size;
3463 mid.InterfaceDescriptorDataStartAddress = state.offset;
3464 }
3465
3466 return VK_SUCCESS;
3467 }
3468
3469 void
3470 genX(cmd_buffer_flush_compute_state)(struct anv_cmd_buffer *cmd_buffer)
3471 {
3472 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3473 MAYBE_UNUSED VkResult result;
3474
3475 assert(pipeline->active_stages == VK_SHADER_STAGE_COMPUTE_BIT);
3476
3477 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
3478
3479 genX(flush_pipeline_select_gpgpu)(cmd_buffer);
3480
3481 if (cmd_buffer->state.compute.pipeline_dirty) {
3482 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
3483 *
3484 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
3485 * the only bits that are changed are scoreboard related: Scoreboard
3486 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
3487 * these scoreboard related states, a MEDIA_STATE_FLUSH is
3488 * sufficient."
3489 */
3490 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
3491 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3492
3493 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
3494 }
3495
3496 if ((cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_COMPUTE_BIT) ||
3497 cmd_buffer->state.compute.pipeline_dirty) {
3498 /* FIXME: figure out descriptors for gen7 */
3499 result = flush_compute_descriptor_set(cmd_buffer);
3500 if (result != VK_SUCCESS)
3501 return;
3502
3503 cmd_buffer->state.descriptors_dirty &= ~VK_SHADER_STAGE_COMPUTE_BIT;
3504 }
3505
3506 if (cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_COMPUTE_BIT) {
3507 struct anv_state push_state =
3508 anv_cmd_buffer_cs_push_constants(cmd_buffer);
3509
3510 if (push_state.alloc_size) {
3511 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_CURBE_LOAD), curbe) {
3512 curbe.CURBETotalDataLength = push_state.alloc_size;
3513 curbe.CURBEDataStartAddress = push_state.offset;
3514 }
3515 }
3516
3517 cmd_buffer->state.push_constants_dirty &= ~VK_SHADER_STAGE_COMPUTE_BIT;
3518 }
3519
3520 cmd_buffer->state.compute.pipeline_dirty = false;
3521
3522 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3523 }
3524
3525 #if GEN_GEN == 7
3526
3527 static VkResult
3528 verify_cmd_parser(const struct anv_device *device,
3529 int required_version,
3530 const char *function)
3531 {
3532 if (device->instance->physicalDevice.cmd_parser_version < required_version) {
3533 return vk_errorf(device->instance, device->instance,
3534 VK_ERROR_FEATURE_NOT_PRESENT,
3535 "cmd parser version %d is required for %s",
3536 required_version, function);
3537 } else {
3538 return VK_SUCCESS;
3539 }
3540 }
3541
3542 #endif
3543
3544 static void
3545 anv_cmd_buffer_push_base_group_id(struct anv_cmd_buffer *cmd_buffer,
3546 uint32_t baseGroupX,
3547 uint32_t baseGroupY,
3548 uint32_t baseGroupZ)
3549 {
3550 if (anv_batch_has_error(&cmd_buffer->batch))
3551 return;
3552
3553 VkResult result =
3554 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, MESA_SHADER_COMPUTE,
3555 base_work_group_id);
3556 if (result != VK_SUCCESS) {
3557 cmd_buffer->batch.status = result;
3558 return;
3559 }
3560
3561 struct anv_push_constants *push =
3562 cmd_buffer->state.push_constants[MESA_SHADER_COMPUTE];
3563 if (push->base_work_group_id[0] != baseGroupX ||
3564 push->base_work_group_id[1] != baseGroupY ||
3565 push->base_work_group_id[2] != baseGroupZ) {
3566 push->base_work_group_id[0] = baseGroupX;
3567 push->base_work_group_id[1] = baseGroupY;
3568 push->base_work_group_id[2] = baseGroupZ;
3569
3570 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_COMPUTE_BIT;
3571 }
3572 }
3573
3574 void genX(CmdDispatch)(
3575 VkCommandBuffer commandBuffer,
3576 uint32_t x,
3577 uint32_t y,
3578 uint32_t z)
3579 {
3580 genX(CmdDispatchBase)(commandBuffer, 0, 0, 0, x, y, z);
3581 }
3582
3583 void genX(CmdDispatchBase)(
3584 VkCommandBuffer commandBuffer,
3585 uint32_t baseGroupX,
3586 uint32_t baseGroupY,
3587 uint32_t baseGroupZ,
3588 uint32_t groupCountX,
3589 uint32_t groupCountY,
3590 uint32_t groupCountZ)
3591 {
3592 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3593 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3594 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
3595
3596 anv_cmd_buffer_push_base_group_id(cmd_buffer, baseGroupX,
3597 baseGroupY, baseGroupZ);
3598
3599 if (anv_batch_has_error(&cmd_buffer->batch))
3600 return;
3601
3602 if (prog_data->uses_num_work_groups) {
3603 struct anv_state state =
3604 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 12, 4);
3605 uint32_t *sizes = state.map;
3606 sizes[0] = groupCountX;
3607 sizes[1] = groupCountY;
3608 sizes[2] = groupCountZ;
3609 cmd_buffer->state.compute.num_workgroups = (struct anv_address) {
3610 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
3611 .offset = state.offset,
3612 };
3613 }
3614
3615 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
3616
3617 if (cmd_buffer->state.conditional_render_enabled)
3618 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3619
3620 anv_batch_emit(&cmd_buffer->batch, GENX(GPGPU_WALKER), ggw) {
3621 ggw.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3622 ggw.SIMDSize = prog_data->simd_size / 16;
3623 ggw.ThreadDepthCounterMaximum = 0;
3624 ggw.ThreadHeightCounterMaximum = 0;
3625 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
3626 ggw.ThreadGroupIDXDimension = groupCountX;
3627 ggw.ThreadGroupIDYDimension = groupCountY;
3628 ggw.ThreadGroupIDZDimension = groupCountZ;
3629 ggw.RightExecutionMask = pipeline->cs_right_mask;
3630 ggw.BottomExecutionMask = 0xffffffff;
3631 }
3632
3633 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_STATE_FLUSH), msf);
3634 }
3635
3636 #define GPGPU_DISPATCHDIMX 0x2500
3637 #define GPGPU_DISPATCHDIMY 0x2504
3638 #define GPGPU_DISPATCHDIMZ 0x2508
3639
3640 void genX(CmdDispatchIndirect)(
3641 VkCommandBuffer commandBuffer,
3642 VkBuffer _buffer,
3643 VkDeviceSize offset)
3644 {
3645 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3646 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3647 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3648 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
3649 struct anv_address addr = anv_address_add(buffer->address, offset);
3650 struct anv_batch *batch = &cmd_buffer->batch;
3651
3652 anv_cmd_buffer_push_base_group_id(cmd_buffer, 0, 0, 0);
3653
3654 #if GEN_GEN == 7
3655 /* Linux 4.4 added command parser version 5 which allows the GPGPU
3656 * indirect dispatch registers to be written.
3657 */
3658 if (verify_cmd_parser(cmd_buffer->device, 5,
3659 "vkCmdDispatchIndirect") != VK_SUCCESS)
3660 return;
3661 #endif
3662
3663 if (prog_data->uses_num_work_groups)
3664 cmd_buffer->state.compute.num_workgroups = addr;
3665
3666 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
3667
3668 struct gen_mi_builder b;
3669 gen_mi_builder_init(&b, &cmd_buffer->batch);
3670
3671 struct gen_mi_value size_x = gen_mi_mem32(anv_address_add(addr, 0));
3672 struct gen_mi_value size_y = gen_mi_mem32(anv_address_add(addr, 4));
3673 struct gen_mi_value size_z = gen_mi_mem32(anv_address_add(addr, 8));
3674
3675 gen_mi_store(&b, gen_mi_reg32(GPGPU_DISPATCHDIMX), size_x);
3676 gen_mi_store(&b, gen_mi_reg32(GPGPU_DISPATCHDIMY), size_y);
3677 gen_mi_store(&b, gen_mi_reg32(GPGPU_DISPATCHDIMZ), size_z);
3678
3679 #if GEN_GEN <= 7
3680 /* predicate = (compute_dispatch_indirect_x_size == 0); */
3681 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC0), size_x);
3682 gen_mi_store(&b, gen_mi_reg64(MI_PREDICATE_SRC1), gen_mi_imm(0));
3683 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3684 mip.LoadOperation = LOAD_LOAD;
3685 mip.CombineOperation = COMBINE_SET;
3686 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3687 }
3688
3689 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
3690 gen_mi_store(&b, gen_mi_reg32(MI_PREDICATE_SRC0), size_y);
3691 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3692 mip.LoadOperation = LOAD_LOAD;
3693 mip.CombineOperation = COMBINE_OR;
3694 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3695 }
3696
3697 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
3698 gen_mi_store(&b, gen_mi_reg32(MI_PREDICATE_SRC0), size_z);
3699 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3700 mip.LoadOperation = LOAD_LOAD;
3701 mip.CombineOperation = COMBINE_OR;
3702 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3703 }
3704
3705 /* predicate = !predicate; */
3706 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3707 mip.LoadOperation = LOAD_LOADINV;
3708 mip.CombineOperation = COMBINE_OR;
3709 mip.CompareOperation = COMPARE_FALSE;
3710 }
3711
3712 #if GEN_IS_HASWELL
3713 if (cmd_buffer->state.conditional_render_enabled) {
3714 emit_lrr(batch, MI_PREDICATE_SRC0, CS_GPR(ANV_PREDICATE_RESULT_REG));
3715 /* predicate &= !(conditional_rendering_predicate == 0); */
3716 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3717 mip.LoadOperation = LOAD_LOADINV;
3718 mip.CombineOperation = COMBINE_AND;
3719 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3720 }
3721 }
3722 #endif
3723
3724 #else /* GEN_GEN > 7 */
3725 if (cmd_buffer->state.conditional_render_enabled)
3726 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3727 #endif
3728
3729 anv_batch_emit(batch, GENX(GPGPU_WALKER), ggw) {
3730 ggw.IndirectParameterEnable = true;
3731 ggw.PredicateEnable = GEN_GEN <= 7 ||
3732 cmd_buffer->state.conditional_render_enabled;
3733 ggw.SIMDSize = prog_data->simd_size / 16;
3734 ggw.ThreadDepthCounterMaximum = 0;
3735 ggw.ThreadHeightCounterMaximum = 0;
3736 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
3737 ggw.RightExecutionMask = pipeline->cs_right_mask;
3738 ggw.BottomExecutionMask = 0xffffffff;
3739 }
3740
3741 anv_batch_emit(batch, GENX(MEDIA_STATE_FLUSH), msf);
3742 }
3743
3744 static void
3745 genX(flush_pipeline_select)(struct anv_cmd_buffer *cmd_buffer,
3746 uint32_t pipeline)
3747 {
3748 UNUSED const struct gen_device_info *devinfo = &cmd_buffer->device->info;
3749
3750 if (cmd_buffer->state.current_pipeline == pipeline)
3751 return;
3752
3753 #if GEN_GEN >= 8 && GEN_GEN < 10
3754 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
3755 *
3756 * Software must clear the COLOR_CALC_STATE Valid field in
3757 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
3758 * with Pipeline Select set to GPGPU.
3759 *
3760 * The internal hardware docs recommend the same workaround for Gen9
3761 * hardware too.
3762 */
3763 if (pipeline == GPGPU)
3764 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CC_STATE_POINTERS), t);
3765 #endif
3766
3767 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
3768 * PIPELINE_SELECT [DevBWR+]":
3769 *
3770 * Project: DEVSNB+
3771 *
3772 * Software must ensure all the write caches are flushed through a
3773 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
3774 * command to invalidate read only caches prior to programming
3775 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
3776 */
3777 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
3778 pc.RenderTargetCacheFlushEnable = true;
3779 pc.DepthCacheFlushEnable = true;
3780 pc.DCFlushEnable = true;
3781 pc.PostSyncOperation = NoWrite;
3782 pc.CommandStreamerStallEnable = true;
3783 }
3784
3785 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
3786 pc.TextureCacheInvalidationEnable = true;
3787 pc.ConstantCacheInvalidationEnable = true;
3788 pc.StateCacheInvalidationEnable = true;
3789 pc.InstructionCacheInvalidateEnable = true;
3790 pc.PostSyncOperation = NoWrite;
3791 }
3792
3793 anv_batch_emit(&cmd_buffer->batch, GENX(PIPELINE_SELECT), ps) {
3794 #if GEN_GEN >= 9
3795 ps.MaskBits = 3;
3796 #endif
3797 ps.PipelineSelection = pipeline;
3798 }
3799
3800 #if GEN_GEN == 9
3801 if (devinfo->is_geminilake) {
3802 /* Project: DevGLK
3803 *
3804 * "This chicken bit works around a hardware issue with barrier logic
3805 * encountered when switching between GPGPU and 3D pipelines. To
3806 * workaround the issue, this mode bit should be set after a pipeline
3807 * is selected."
3808 */
3809 uint32_t scec;
3810 anv_pack_struct(&scec, GENX(SLICE_COMMON_ECO_CHICKEN1),
3811 .GLKBarrierMode =
3812 pipeline == GPGPU ? GLK_BARRIER_MODE_GPGPU
3813 : GLK_BARRIER_MODE_3D_HULL,
3814 .GLKBarrierModeMask = 1);
3815 emit_lri(&cmd_buffer->batch, GENX(SLICE_COMMON_ECO_CHICKEN1_num), scec);
3816 }
3817 #endif
3818
3819 cmd_buffer->state.current_pipeline = pipeline;
3820 }
3821
3822 void
3823 genX(flush_pipeline_select_3d)(struct anv_cmd_buffer *cmd_buffer)
3824 {
3825 genX(flush_pipeline_select)(cmd_buffer, _3D);
3826 }
3827
3828 void
3829 genX(flush_pipeline_select_gpgpu)(struct anv_cmd_buffer *cmd_buffer)
3830 {
3831 genX(flush_pipeline_select)(cmd_buffer, GPGPU);
3832 }
3833
3834 void
3835 genX(cmd_buffer_emit_gen7_depth_flush)(struct anv_cmd_buffer *cmd_buffer)
3836 {
3837 if (GEN_GEN >= 8)
3838 return;
3839
3840 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
3841 *
3842 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
3843 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
3844 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
3845 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
3846 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
3847 * Depth Flush Bit set, followed by another pipelined depth stall
3848 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
3849 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
3850 * via a preceding MI_FLUSH)."
3851 */
3852 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
3853 pipe.DepthStallEnable = true;
3854 }
3855 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
3856 pipe.DepthCacheFlushEnable = true;
3857 }
3858 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
3859 pipe.DepthStallEnable = true;
3860 }
3861 }
3862
3863 static void
3864 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer *cmd_buffer)
3865 {
3866 struct anv_device *device = cmd_buffer->device;
3867 const struct anv_image_view *iview =
3868 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
3869 const struct anv_image *image = iview ? iview->image : NULL;
3870
3871 /* FIXME: Width and Height are wrong */
3872
3873 genX(cmd_buffer_emit_gen7_depth_flush)(cmd_buffer);
3874
3875 uint32_t *dw = anv_batch_emit_dwords(&cmd_buffer->batch,
3876 device->isl_dev.ds.size / 4);
3877 if (dw == NULL)
3878 return;
3879
3880 struct isl_depth_stencil_hiz_emit_info info = { };
3881
3882 if (iview)
3883 info.view = &iview->planes[0].isl;
3884
3885 if (image && (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
3886 uint32_t depth_plane =
3887 anv_image_aspect_to_plane(image->aspects, VK_IMAGE_ASPECT_DEPTH_BIT);
3888 const struct anv_surface *surface = &image->planes[depth_plane].surface;
3889
3890 info.depth_surf = &surface->isl;
3891
3892 info.depth_address =
3893 anv_batch_emit_reloc(&cmd_buffer->batch,
3894 dw + device->isl_dev.ds.depth_offset / 4,
3895 image->planes[depth_plane].address.bo,
3896 image->planes[depth_plane].address.offset +
3897 surface->offset);
3898 info.mocs =
3899 anv_mocs_for_bo(device, image->planes[depth_plane].address.bo);
3900
3901 const uint32_t ds =
3902 cmd_buffer->state.subpass->depth_stencil_attachment->attachment;
3903 info.hiz_usage = cmd_buffer->state.attachments[ds].aux_usage;
3904 if (info.hiz_usage == ISL_AUX_USAGE_HIZ) {
3905 info.hiz_surf = &image->planes[depth_plane].aux_surface.isl;
3906
3907 info.hiz_address =
3908 anv_batch_emit_reloc(&cmd_buffer->batch,
3909 dw + device->isl_dev.ds.hiz_offset / 4,
3910 image->planes[depth_plane].address.bo,
3911 image->planes[depth_plane].address.offset +
3912 image->planes[depth_plane].aux_surface.offset);
3913
3914 info.depth_clear_value = ANV_HZ_FC_VAL;
3915 }
3916 }
3917
3918 if (image && (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT)) {
3919 uint32_t stencil_plane =
3920 anv_image_aspect_to_plane(image->aspects, VK_IMAGE_ASPECT_STENCIL_BIT);
3921 const struct anv_surface *surface = &image->planes[stencil_plane].surface;
3922
3923 info.stencil_surf = &surface->isl;
3924
3925 info.stencil_address =
3926 anv_batch_emit_reloc(&cmd_buffer->batch,
3927 dw + device->isl_dev.ds.stencil_offset / 4,
3928 image->planes[stencil_plane].address.bo,
3929 image->planes[stencil_plane].address.offset +
3930 surface->offset);
3931 info.mocs =
3932 anv_mocs_for_bo(device, image->planes[stencil_plane].address.bo);
3933 }
3934
3935 isl_emit_depth_stencil_hiz_s(&device->isl_dev, dw, &info);
3936
3937 cmd_buffer->state.hiz_enabled = info.hiz_usage == ISL_AUX_USAGE_HIZ;
3938 }
3939
3940 /**
3941 * This ANDs the view mask of the current subpass with the pending clear
3942 * views in the attachment to get the mask of views active in the subpass
3943 * that still need to be cleared.
3944 */
3945 static inline uint32_t
3946 get_multiview_subpass_clear_mask(const struct anv_cmd_state *cmd_state,
3947 const struct anv_attachment_state *att_state)
3948 {
3949 return cmd_state->subpass->view_mask & att_state->pending_clear_views;
3950 }
3951
3952 static inline bool
3953 do_first_layer_clear(const struct anv_cmd_state *cmd_state,
3954 const struct anv_attachment_state *att_state)
3955 {
3956 if (!cmd_state->subpass->view_mask)
3957 return true;
3958
3959 uint32_t pending_clear_mask =
3960 get_multiview_subpass_clear_mask(cmd_state, att_state);
3961
3962 return pending_clear_mask & 1;
3963 }
3964
3965 static inline bool
3966 current_subpass_is_last_for_attachment(const struct anv_cmd_state *cmd_state,
3967 uint32_t att_idx)
3968 {
3969 const uint32_t last_subpass_idx =
3970 cmd_state->pass->attachments[att_idx].last_subpass_idx;
3971 const struct anv_subpass *last_subpass =
3972 &cmd_state->pass->subpasses[last_subpass_idx];
3973 return last_subpass == cmd_state->subpass;
3974 }
3975
3976 static void
3977 cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
3978 uint32_t subpass_id)
3979 {
3980 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
3981 struct anv_subpass *subpass = &cmd_state->pass->subpasses[subpass_id];
3982 cmd_state->subpass = subpass;
3983
3984 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
3985
3986 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3987 * different views. If the client asks for instancing, we need to use the
3988 * Instance Data Step Rate to ensure that we repeat the client's
3989 * per-instance data once for each view. Since this bit is in
3990 * VERTEX_BUFFER_STATE on gen7, we need to dirty vertex buffers at the top
3991 * of each subpass.
3992 */
3993 if (GEN_GEN == 7)
3994 cmd_buffer->state.gfx.vb_dirty |= ~0;
3995
3996 /* It is possible to start a render pass with an old pipeline. Because the
3997 * render pass and subpass index are both baked into the pipeline, this is
3998 * highly unlikely. In order to do so, it requires that you have a render
3999 * pass with a single subpass and that you use that render pass twice
4000 * back-to-back and use the same pipeline at the start of the second render
4001 * pass as at the end of the first. In order to avoid unpredictable issues
4002 * with this edge case, we just dirty the pipeline at the start of every
4003 * subpass.
4004 */
4005 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_PIPELINE;
4006
4007 /* Accumulate any subpass flushes that need to happen before the subpass */
4008 cmd_buffer->state.pending_pipe_bits |=
4009 cmd_buffer->state.pass->subpass_flushes[subpass_id];
4010
4011 VkRect2D render_area = cmd_buffer->state.render_area;
4012 struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
4013
4014 bool is_multiview = subpass->view_mask != 0;
4015
4016 for (uint32_t i = 0; i < subpass->attachment_count; ++i) {
4017 const uint32_t a = subpass->attachments[i].attachment;
4018 if (a == VK_ATTACHMENT_UNUSED)
4019 continue;
4020
4021 assert(a < cmd_state->pass->attachment_count);
4022 struct anv_attachment_state *att_state = &cmd_state->attachments[a];
4023
4024 struct anv_image_view *iview = fb->attachments[a];
4025 const struct anv_image *image = iview->image;
4026
4027 /* A resolve is necessary before use as an input attachment if the clear
4028 * color or auxiliary buffer usage isn't supported by the sampler.
4029 */
4030 const bool input_needs_resolve =
4031 (att_state->fast_clear && !att_state->clear_color_is_zero_one) ||
4032 att_state->input_aux_usage != att_state->aux_usage;
4033
4034 VkImageLayout target_layout;
4035 if (iview->aspect_mask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV &&
4036 !input_needs_resolve) {
4037 /* Layout transitions before the final only help to enable sampling
4038 * as an input attachment. If the input attachment supports sampling
4039 * using the auxiliary surface, we can skip such transitions by
4040 * making the target layout one that is CCS-aware.
4041 */
4042 target_layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
4043 } else {
4044 target_layout = subpass->attachments[i].layout;
4045 }
4046
4047 if (image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
4048 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
4049
4050 uint32_t base_layer, layer_count;
4051 if (image->type == VK_IMAGE_TYPE_3D) {
4052 base_layer = 0;
4053 layer_count = anv_minify(iview->image->extent.depth,
4054 iview->planes[0].isl.base_level);
4055 } else {
4056 base_layer = iview->planes[0].isl.base_array_layer;
4057 layer_count = fb->layers;
4058 }
4059
4060 transition_color_buffer(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
4061 iview->planes[0].isl.base_level, 1,
4062 base_layer, layer_count,
4063 att_state->current_layout, target_layout);
4064 } else if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
4065 transition_depth_buffer(cmd_buffer, image,
4066 att_state->current_layout, target_layout);
4067 att_state->aux_usage =
4068 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
4069 VK_IMAGE_ASPECT_DEPTH_BIT, target_layout);
4070 }
4071 att_state->current_layout = target_layout;
4072
4073 if (att_state->pending_clear_aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
4074 assert(att_state->pending_clear_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
4075
4076 /* Multi-planar images are not supported as attachments */
4077 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
4078 assert(image->n_planes == 1);
4079
4080 uint32_t base_clear_layer = iview->planes[0].isl.base_array_layer;
4081 uint32_t clear_layer_count = fb->layers;
4082
4083 if (att_state->fast_clear &&
4084 do_first_layer_clear(cmd_state, att_state)) {
4085 /* We only support fast-clears on the first layer */
4086 assert(iview->planes[0].isl.base_level == 0);
4087 assert(iview->planes[0].isl.base_array_layer == 0);
4088
4089 union isl_color_value clear_color = {};
4090 anv_clear_color_from_att_state(&clear_color, att_state, iview);
4091 if (iview->image->samples == 1) {
4092 anv_image_ccs_op(cmd_buffer, image,
4093 iview->planes[0].isl.format,
4094 VK_IMAGE_ASPECT_COLOR_BIT,
4095 0, 0, 1, ISL_AUX_OP_FAST_CLEAR,
4096 &clear_color,
4097 false);
4098 } else {
4099 anv_image_mcs_op(cmd_buffer, image,
4100 iview->planes[0].isl.format,
4101 VK_IMAGE_ASPECT_COLOR_BIT,
4102 0, 1, ISL_AUX_OP_FAST_CLEAR,
4103 &clear_color,
4104 false);
4105 }
4106 base_clear_layer++;
4107 clear_layer_count--;
4108 if (is_multiview)
4109 att_state->pending_clear_views &= ~1;
4110
4111 if (att_state->clear_color_is_zero) {
4112 /* This image has the auxiliary buffer enabled. We can mark the
4113 * subresource as not needing a resolve because the clear color
4114 * will match what's in every RENDER_SURFACE_STATE object when
4115 * it's being used for sampling.
4116 */
4117 set_image_fast_clear_state(cmd_buffer, iview->image,
4118 VK_IMAGE_ASPECT_COLOR_BIT,
4119 ANV_FAST_CLEAR_DEFAULT_VALUE);
4120 } else {
4121 set_image_fast_clear_state(cmd_buffer, iview->image,
4122 VK_IMAGE_ASPECT_COLOR_BIT,
4123 ANV_FAST_CLEAR_ANY);
4124 }
4125 }
4126
4127 /* From the VkFramebufferCreateInfo spec:
4128 *
4129 * "If the render pass uses multiview, then layers must be one and each
4130 * attachment requires a number of layers that is greater than the
4131 * maximum bit index set in the view mask in the subpasses in which it
4132 * is used."
4133 *
4134 * So if multiview is active we ignore the number of layers in the
4135 * framebuffer and instead we honor the view mask from the subpass.
4136 */
4137 if (is_multiview) {
4138 assert(image->n_planes == 1);
4139 uint32_t pending_clear_mask =
4140 get_multiview_subpass_clear_mask(cmd_state, att_state);
4141
4142 uint32_t layer_idx;
4143 for_each_bit(layer_idx, pending_clear_mask) {
4144 uint32_t layer =
4145 iview->planes[0].isl.base_array_layer + layer_idx;
4146
4147 anv_image_clear_color(cmd_buffer, image,
4148 VK_IMAGE_ASPECT_COLOR_BIT,
4149 att_state->aux_usage,
4150 iview->planes[0].isl.format,
4151 iview->planes[0].isl.swizzle,
4152 iview->planes[0].isl.base_level,
4153 layer, 1,
4154 render_area,
4155 vk_to_isl_color(att_state->clear_value.color));
4156 }
4157
4158 att_state->pending_clear_views &= ~pending_clear_mask;
4159 } else if (clear_layer_count > 0) {
4160 assert(image->n_planes == 1);
4161 anv_image_clear_color(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
4162 att_state->aux_usage,
4163 iview->planes[0].isl.format,
4164 iview->planes[0].isl.swizzle,
4165 iview->planes[0].isl.base_level,
4166 base_clear_layer, clear_layer_count,
4167 render_area,
4168 vk_to_isl_color(att_state->clear_value.color));
4169 }
4170 } else if (att_state->pending_clear_aspects & (VK_IMAGE_ASPECT_DEPTH_BIT |
4171 VK_IMAGE_ASPECT_STENCIL_BIT)) {
4172 if (att_state->fast_clear && !is_multiview) {
4173 /* We currently only support HiZ for single-layer images */
4174 if (att_state->pending_clear_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
4175 assert(iview->image->planes[0].aux_usage == ISL_AUX_USAGE_HIZ);
4176 assert(iview->planes[0].isl.base_level == 0);
4177 assert(iview->planes[0].isl.base_array_layer == 0);
4178 assert(fb->layers == 1);
4179 }
4180
4181 anv_image_hiz_clear(cmd_buffer, image,
4182 att_state->pending_clear_aspects,
4183 iview->planes[0].isl.base_level,
4184 iview->planes[0].isl.base_array_layer,
4185 fb->layers, render_area,
4186 att_state->clear_value.depthStencil.stencil);
4187 } else if (is_multiview) {
4188 uint32_t pending_clear_mask =
4189 get_multiview_subpass_clear_mask(cmd_state, att_state);
4190
4191 uint32_t layer_idx;
4192 for_each_bit(layer_idx, pending_clear_mask) {
4193 uint32_t layer =
4194 iview->planes[0].isl.base_array_layer + layer_idx;
4195
4196 anv_image_clear_depth_stencil(cmd_buffer, image,
4197 att_state->pending_clear_aspects,
4198 att_state->aux_usage,
4199 iview->planes[0].isl.base_level,
4200 layer, 1,
4201 render_area,
4202 att_state->clear_value.depthStencil.depth,
4203 att_state->clear_value.depthStencil.stencil);
4204 }
4205
4206 att_state->pending_clear_views &= ~pending_clear_mask;
4207 } else {
4208 anv_image_clear_depth_stencil(cmd_buffer, image,
4209 att_state->pending_clear_aspects,
4210 att_state->aux_usage,
4211 iview->planes[0].isl.base_level,
4212 iview->planes[0].isl.base_array_layer,
4213 fb->layers, render_area,
4214 att_state->clear_value.depthStencil.depth,
4215 att_state->clear_value.depthStencil.stencil);
4216 }
4217 } else {
4218 assert(att_state->pending_clear_aspects == 0);
4219 }
4220
4221 if (GEN_GEN < 10 &&
4222 (att_state->pending_load_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) &&
4223 image->planes[0].aux_surface.isl.size_B > 0 &&
4224 iview->planes[0].isl.base_level == 0 &&
4225 iview->planes[0].isl.base_array_layer == 0) {
4226 if (att_state->aux_usage != ISL_AUX_USAGE_NONE) {
4227 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->color.state,
4228 image, VK_IMAGE_ASPECT_COLOR_BIT,
4229 false /* copy to ss */);
4230 }
4231
4232 if (need_input_attachment_state(&cmd_state->pass->attachments[a]) &&
4233 att_state->input_aux_usage != ISL_AUX_USAGE_NONE) {
4234 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->input.state,
4235 image, VK_IMAGE_ASPECT_COLOR_BIT,
4236 false /* copy to ss */);
4237 }
4238 }
4239
4240 if (subpass->attachments[i].usage ==
4241 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
4242 /* We assume that if we're starting a subpass, we're going to do some
4243 * rendering so we may end up with compressed data.
4244 */
4245 genX(cmd_buffer_mark_image_written)(cmd_buffer, iview->image,
4246 VK_IMAGE_ASPECT_COLOR_BIT,
4247 att_state->aux_usage,
4248 iview->planes[0].isl.base_level,
4249 iview->planes[0].isl.base_array_layer,
4250 fb->layers);
4251 } else if (subpass->attachments[i].usage ==
4252 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
4253 /* We may be writing depth or stencil so we need to mark the surface.
4254 * Unfortunately, there's no way to know at this point whether the
4255 * depth or stencil tests used will actually write to the surface.
4256 *
4257 * Even though stencil may be plane 1, it always shares a base_level
4258 * with depth.
4259 */
4260 const struct isl_view *ds_view = &iview->planes[0].isl;
4261 if (iview->aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
4262 genX(cmd_buffer_mark_image_written)(cmd_buffer, image,
4263 VK_IMAGE_ASPECT_DEPTH_BIT,
4264 att_state->aux_usage,
4265 ds_view->base_level,
4266 ds_view->base_array_layer,
4267 fb->layers);
4268 }
4269 if (iview->aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
4270 /* Even though stencil may be plane 1, it always shares a
4271 * base_level with depth.
4272 */
4273 genX(cmd_buffer_mark_image_written)(cmd_buffer, image,
4274 VK_IMAGE_ASPECT_STENCIL_BIT,
4275 ISL_AUX_USAGE_NONE,
4276 ds_view->base_level,
4277 ds_view->base_array_layer,
4278 fb->layers);
4279 }
4280 }
4281
4282 /* If multiview is enabled, then we are only done clearing when we no
4283 * longer have pending layers to clear, or when we have processed the
4284 * last subpass that uses this attachment.
4285 */
4286 if (!is_multiview ||
4287 att_state->pending_clear_views == 0 ||
4288 current_subpass_is_last_for_attachment(cmd_state, a)) {
4289 att_state->pending_clear_aspects = 0;
4290 }
4291
4292 att_state->pending_load_aspects = 0;
4293 }
4294
4295 cmd_buffer_emit_depth_stencil(cmd_buffer);
4296 }
4297
4298 static enum blorp_filter
4299 vk_to_blorp_resolve_mode(VkResolveModeFlagBitsKHR vk_mode)
4300 {
4301 switch (vk_mode) {
4302 case VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR:
4303 return BLORP_FILTER_SAMPLE_0;
4304 case VK_RESOLVE_MODE_AVERAGE_BIT_KHR:
4305 return BLORP_FILTER_AVERAGE;
4306 case VK_RESOLVE_MODE_MIN_BIT_KHR:
4307 return BLORP_FILTER_MIN_SAMPLE;
4308 case VK_RESOLVE_MODE_MAX_BIT_KHR:
4309 return BLORP_FILTER_MAX_SAMPLE;
4310 default:
4311 return BLORP_FILTER_NONE;
4312 }
4313 }
4314
4315 static void
4316 cmd_buffer_end_subpass(struct anv_cmd_buffer *cmd_buffer)
4317 {
4318 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
4319 struct anv_subpass *subpass = cmd_state->subpass;
4320 uint32_t subpass_id = anv_get_subpass_id(&cmd_buffer->state);
4321 struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
4322
4323 if (subpass->has_color_resolve) {
4324 /* We are about to do some MSAA resolves. We need to flush so that the
4325 * result of writes to the MSAA color attachments show up in the sampler
4326 * when we blit to the single-sampled resolve target.
4327 */
4328 cmd_buffer->state.pending_pipe_bits |=
4329 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT |
4330 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
4331
4332 for (uint32_t i = 0; i < subpass->color_count; ++i) {
4333 uint32_t src_att = subpass->color_attachments[i].attachment;
4334 uint32_t dst_att = subpass->resolve_attachments[i].attachment;
4335
4336 if (dst_att == VK_ATTACHMENT_UNUSED)
4337 continue;
4338
4339 assert(src_att < cmd_buffer->state.pass->attachment_count);
4340 assert(dst_att < cmd_buffer->state.pass->attachment_count);
4341
4342 if (cmd_buffer->state.attachments[dst_att].pending_clear_aspects) {
4343 /* From the Vulkan 1.0 spec:
4344 *
4345 * If the first use of an attachment in a render pass is as a
4346 * resolve attachment, then the loadOp is effectively ignored
4347 * as the resolve is guaranteed to overwrite all pixels in the
4348 * render area.
4349 */
4350 cmd_buffer->state.attachments[dst_att].pending_clear_aspects = 0;
4351 }
4352
4353 struct anv_image_view *src_iview = fb->attachments[src_att];
4354 struct anv_image_view *dst_iview = fb->attachments[dst_att];
4355
4356 const VkRect2D render_area = cmd_buffer->state.render_area;
4357
4358 enum isl_aux_usage src_aux_usage =
4359 cmd_buffer->state.attachments[src_att].aux_usage;
4360 enum isl_aux_usage dst_aux_usage =
4361 cmd_buffer->state.attachments[dst_att].aux_usage;
4362
4363 assert(src_iview->aspect_mask == VK_IMAGE_ASPECT_COLOR_BIT &&
4364 dst_iview->aspect_mask == VK_IMAGE_ASPECT_COLOR_BIT);
4365
4366 anv_image_msaa_resolve(cmd_buffer,
4367 src_iview->image, src_aux_usage,
4368 src_iview->planes[0].isl.base_level,
4369 src_iview->planes[0].isl.base_array_layer,
4370 dst_iview->image, dst_aux_usage,
4371 dst_iview->planes[0].isl.base_level,
4372 dst_iview->planes[0].isl.base_array_layer,
4373 VK_IMAGE_ASPECT_COLOR_BIT,
4374 render_area.offset.x, render_area.offset.y,
4375 render_area.offset.x, render_area.offset.y,
4376 render_area.extent.width,
4377 render_area.extent.height,
4378 fb->layers, BLORP_FILTER_NONE);
4379 }
4380 }
4381
4382 if (subpass->ds_resolve_attachment) {
4383 /* We are about to do some MSAA resolves. We need to flush so that the
4384 * result of writes to the MSAA depth attachments show up in the sampler
4385 * when we blit to the single-sampled resolve target.
4386 */
4387 cmd_buffer->state.pending_pipe_bits |=
4388 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT |
4389 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
4390
4391 uint32_t src_att = subpass->depth_stencil_attachment->attachment;
4392 uint32_t dst_att = subpass->ds_resolve_attachment->attachment;
4393
4394 assert(src_att < cmd_buffer->state.pass->attachment_count);
4395 assert(dst_att < cmd_buffer->state.pass->attachment_count);
4396
4397 if (cmd_buffer->state.attachments[dst_att].pending_clear_aspects) {
4398 /* From the Vulkan 1.0 spec:
4399 *
4400 * If the first use of an attachment in a render pass is as a
4401 * resolve attachment, then the loadOp is effectively ignored
4402 * as the resolve is guaranteed to overwrite all pixels in the
4403 * render area.
4404 */
4405 cmd_buffer->state.attachments[dst_att].pending_clear_aspects = 0;
4406 }
4407
4408 struct anv_image_view *src_iview = fb->attachments[src_att];
4409 struct anv_image_view *dst_iview = fb->attachments[dst_att];
4410
4411 const VkRect2D render_area = cmd_buffer->state.render_area;
4412
4413 if ((src_iview->image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
4414 subpass->depth_resolve_mode != VK_RESOLVE_MODE_NONE_KHR) {
4415
4416 struct anv_attachment_state *src_state =
4417 &cmd_state->attachments[src_att];
4418 struct anv_attachment_state *dst_state =
4419 &cmd_state->attachments[dst_att];
4420
4421 /* MSAA resolves sample from the source attachment. Transition the
4422 * depth attachment first to get rid of any HiZ that we may not be
4423 * able to handle.
4424 */
4425 transition_depth_buffer(cmd_buffer, src_iview->image,
4426 src_state->current_layout,
4427 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
4428 src_state->aux_usage =
4429 anv_layout_to_aux_usage(&cmd_buffer->device->info, src_iview->image,
4430 VK_IMAGE_ASPECT_DEPTH_BIT,
4431 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
4432 src_state->current_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
4433
4434 /* MSAA resolves write to the resolve attachment as if it were any
4435 * other transfer op. Transition the resolve attachment accordingly.
4436 */
4437 VkImageLayout dst_initial_layout = dst_state->current_layout;
4438
4439 /* If our render area is the entire size of the image, we're going to
4440 * blow it all away so we can claim the initial layout is UNDEFINED
4441 * and we'll get a HiZ ambiguate instead of a resolve.
4442 */
4443 if (dst_iview->image->type != VK_IMAGE_TYPE_3D &&
4444 render_area.offset.x == 0 && render_area.offset.y == 0 &&
4445 render_area.extent.width == dst_iview->extent.width &&
4446 render_area.extent.height == dst_iview->extent.height)
4447 dst_initial_layout = VK_IMAGE_LAYOUT_UNDEFINED;
4448
4449 transition_depth_buffer(cmd_buffer, dst_iview->image,
4450 dst_initial_layout,
4451 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
4452 dst_state->aux_usage =
4453 anv_layout_to_aux_usage(&cmd_buffer->device->info, dst_iview->image,
4454 VK_IMAGE_ASPECT_DEPTH_BIT,
4455 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
4456 dst_state->current_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
4457
4458 enum blorp_filter filter =
4459 vk_to_blorp_resolve_mode(subpass->depth_resolve_mode);
4460
4461 anv_image_msaa_resolve(cmd_buffer,
4462 src_iview->image, src_state->aux_usage,
4463 src_iview->planes[0].isl.base_level,
4464 src_iview->planes[0].isl.base_array_layer,
4465 dst_iview->image, dst_state->aux_usage,
4466 dst_iview->planes[0].isl.base_level,
4467 dst_iview->planes[0].isl.base_array_layer,
4468 VK_IMAGE_ASPECT_DEPTH_BIT,
4469 render_area.offset.x, render_area.offset.y,
4470 render_area.offset.x, render_area.offset.y,
4471 render_area.extent.width,
4472 render_area.extent.height,
4473 fb->layers, filter);
4474 }
4475
4476 if ((src_iview->image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
4477 subpass->stencil_resolve_mode != VK_RESOLVE_MODE_NONE_KHR) {
4478
4479 enum isl_aux_usage src_aux_usage = ISL_AUX_USAGE_NONE;
4480 enum isl_aux_usage dst_aux_usage = ISL_AUX_USAGE_NONE;
4481
4482 enum blorp_filter filter =
4483 vk_to_blorp_resolve_mode(subpass->stencil_resolve_mode);
4484
4485 anv_image_msaa_resolve(cmd_buffer,
4486 src_iview->image, src_aux_usage,
4487 src_iview->planes[0].isl.base_level,
4488 src_iview->planes[0].isl.base_array_layer,
4489 dst_iview->image, dst_aux_usage,
4490 dst_iview->planes[0].isl.base_level,
4491 dst_iview->planes[0].isl.base_array_layer,
4492 VK_IMAGE_ASPECT_STENCIL_BIT,
4493 render_area.offset.x, render_area.offset.y,
4494 render_area.offset.x, render_area.offset.y,
4495 render_area.extent.width,
4496 render_area.extent.height,
4497 fb->layers, filter);
4498 }
4499 }
4500
4501 for (uint32_t i = 0; i < subpass->attachment_count; ++i) {
4502 const uint32_t a = subpass->attachments[i].attachment;
4503 if (a == VK_ATTACHMENT_UNUSED)
4504 continue;
4505
4506 if (cmd_state->pass->attachments[a].last_subpass_idx != subpass_id)
4507 continue;
4508
4509 assert(a < cmd_state->pass->attachment_count);
4510 struct anv_attachment_state *att_state = &cmd_state->attachments[a];
4511 struct anv_image_view *iview = fb->attachments[a];
4512 const struct anv_image *image = iview->image;
4513
4514 if ((image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) &&
4515 image->vk_format != iview->vk_format) {
4516 enum anv_fast_clear_type fast_clear_type =
4517 anv_layout_to_fast_clear_type(&cmd_buffer->device->info,
4518 image, VK_IMAGE_ASPECT_COLOR_BIT,
4519 att_state->current_layout);
4520
4521 /* If any clear color was used, flush it down the aux surfaces. If we
4522 * don't do it now using the view's format we might use the clear
4523 * color incorrectly in the following resolves (for example with an
4524 * SRGB view & a UNORM image).
4525 */
4526 if (fast_clear_type != ANV_FAST_CLEAR_NONE) {
4527 anv_perf_warn(cmd_buffer->device->instance, fb,
4528 "Doing a partial resolve to get rid of clear color at the "
4529 "end of a renderpass due to an image/view format mismatch");
4530
4531 uint32_t base_layer, layer_count;
4532 if (image->type == VK_IMAGE_TYPE_3D) {
4533 base_layer = 0;
4534 layer_count = anv_minify(iview->image->extent.depth,
4535 iview->planes[0].isl.base_level);
4536 } else {
4537 base_layer = iview->planes[0].isl.base_array_layer;
4538 layer_count = fb->layers;
4539 }
4540
4541 for (uint32_t a = 0; a < layer_count; a++) {
4542 uint32_t array_layer = base_layer + a;
4543 if (image->samples == 1) {
4544 anv_cmd_predicated_ccs_resolve(cmd_buffer, image,
4545 iview->planes[0].isl.format,
4546 VK_IMAGE_ASPECT_COLOR_BIT,
4547 iview->planes[0].isl.base_level,
4548 array_layer,
4549 ISL_AUX_OP_PARTIAL_RESOLVE,
4550 ANV_FAST_CLEAR_NONE);
4551 } else {
4552 anv_cmd_predicated_mcs_resolve(cmd_buffer, image,
4553 iview->planes[0].isl.format,
4554 VK_IMAGE_ASPECT_COLOR_BIT,
4555 base_layer,
4556 ISL_AUX_OP_PARTIAL_RESOLVE,
4557 ANV_FAST_CLEAR_NONE);
4558 }
4559 }
4560 }
4561 }
4562
4563 /* Transition the image into the final layout for this render pass */
4564 VkImageLayout target_layout =
4565 cmd_state->pass->attachments[a].final_layout;
4566
4567 if (image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
4568 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
4569
4570 uint32_t base_layer, layer_count;
4571 if (image->type == VK_IMAGE_TYPE_3D) {
4572 base_layer = 0;
4573 layer_count = anv_minify(iview->image->extent.depth,
4574 iview->planes[0].isl.base_level);
4575 } else {
4576 base_layer = iview->planes[0].isl.base_array_layer;
4577 layer_count = fb->layers;
4578 }
4579
4580 transition_color_buffer(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
4581 iview->planes[0].isl.base_level, 1,
4582 base_layer, layer_count,
4583 att_state->current_layout, target_layout);
4584 } else if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
4585 transition_depth_buffer(cmd_buffer, image,
4586 att_state->current_layout, target_layout);
4587 }
4588 }
4589
4590 /* Accumulate any subpass flushes that need to happen after the subpass.
4591 * Yes, they do get accumulated twice in the NextSubpass case but since
4592 * genX_CmdNextSubpass just calls end/begin back-to-back, we just end up
4593 * ORing the bits in twice so it's harmless.
4594 */
4595 cmd_buffer->state.pending_pipe_bits |=
4596 cmd_buffer->state.pass->subpass_flushes[subpass_id + 1];
4597 }
4598
4599 void genX(CmdBeginRenderPass)(
4600 VkCommandBuffer commandBuffer,
4601 const VkRenderPassBeginInfo* pRenderPassBegin,
4602 VkSubpassContents contents)
4603 {
4604 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4605 ANV_FROM_HANDLE(anv_render_pass, pass, pRenderPassBegin->renderPass);
4606 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, pRenderPassBegin->framebuffer);
4607
4608 cmd_buffer->state.framebuffer = framebuffer;
4609 cmd_buffer->state.pass = pass;
4610 cmd_buffer->state.render_area = pRenderPassBegin->renderArea;
4611 VkResult result =
4612 genX(cmd_buffer_setup_attachments)(cmd_buffer, pass, pRenderPassBegin);
4613
4614 /* If we failed to setup the attachments we should not try to go further */
4615 if (result != VK_SUCCESS) {
4616 assert(anv_batch_has_error(&cmd_buffer->batch));
4617 return;
4618 }
4619
4620 genX(flush_pipeline_select_3d)(cmd_buffer);
4621
4622 cmd_buffer_begin_subpass(cmd_buffer, 0);
4623 }
4624
4625 void genX(CmdBeginRenderPass2KHR)(
4626 VkCommandBuffer commandBuffer,
4627 const VkRenderPassBeginInfo* pRenderPassBeginInfo,
4628 const VkSubpassBeginInfoKHR* pSubpassBeginInfo)
4629 {
4630 genX(CmdBeginRenderPass)(commandBuffer, pRenderPassBeginInfo,
4631 pSubpassBeginInfo->contents);
4632 }
4633
4634 void genX(CmdNextSubpass)(
4635 VkCommandBuffer commandBuffer,
4636 VkSubpassContents contents)
4637 {
4638 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4639
4640 if (anv_batch_has_error(&cmd_buffer->batch))
4641 return;
4642
4643 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
4644
4645 uint32_t prev_subpass = anv_get_subpass_id(&cmd_buffer->state);
4646 cmd_buffer_end_subpass(cmd_buffer);
4647 cmd_buffer_begin_subpass(cmd_buffer, prev_subpass + 1);
4648 }
4649
4650 void genX(CmdNextSubpass2KHR)(
4651 VkCommandBuffer commandBuffer,
4652 const VkSubpassBeginInfoKHR* pSubpassBeginInfo,
4653 const VkSubpassEndInfoKHR* pSubpassEndInfo)
4654 {
4655 genX(CmdNextSubpass)(commandBuffer, pSubpassBeginInfo->contents);
4656 }
4657
4658 void genX(CmdEndRenderPass)(
4659 VkCommandBuffer commandBuffer)
4660 {
4661 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4662
4663 if (anv_batch_has_error(&cmd_buffer->batch))
4664 return;
4665
4666 cmd_buffer_end_subpass(cmd_buffer);
4667
4668 cmd_buffer->state.hiz_enabled = false;
4669
4670 #ifndef NDEBUG
4671 anv_dump_add_framebuffer(cmd_buffer, cmd_buffer->state.framebuffer);
4672 #endif
4673
4674 /* Remove references to render pass specific state. This enables us to
4675 * detect whether or not we're in a renderpass.
4676 */
4677 cmd_buffer->state.framebuffer = NULL;
4678 cmd_buffer->state.pass = NULL;
4679 cmd_buffer->state.subpass = NULL;
4680 }
4681
4682 void genX(CmdEndRenderPass2KHR)(
4683 VkCommandBuffer commandBuffer,
4684 const VkSubpassEndInfoKHR* pSubpassEndInfo)
4685 {
4686 genX(CmdEndRenderPass)(commandBuffer);
4687 }
4688
4689 void
4690 genX(cmd_emit_conditional_render_predicate)(struct anv_cmd_buffer *cmd_buffer)
4691 {
4692 #if GEN_GEN >= 8 || GEN_IS_HASWELL
4693 emit_lrr(&cmd_buffer->batch, MI_PREDICATE_SRC0, CS_GPR(ANV_PREDICATE_RESULT_REG));
4694 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC0 + 4, 0);
4695 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1, 0);
4696 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1 + 4, 0);
4697
4698 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
4699 mip.LoadOperation = LOAD_LOADINV;
4700 mip.CombineOperation = COMBINE_SET;
4701 mip.CompareOperation = COMPARE_SRCS_EQUAL;
4702 }
4703 #endif
4704 }
4705
4706 #if GEN_GEN >= 8 || GEN_IS_HASWELL
4707 void genX(CmdBeginConditionalRenderingEXT)(
4708 VkCommandBuffer commandBuffer,
4709 const VkConditionalRenderingBeginInfoEXT* pConditionalRenderingBegin)
4710 {
4711 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4712 ANV_FROM_HANDLE(anv_buffer, buffer, pConditionalRenderingBegin->buffer);
4713 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
4714 struct anv_address value_address =
4715 anv_address_add(buffer->address, pConditionalRenderingBegin->offset);
4716
4717 const bool isInverted = pConditionalRenderingBegin->flags &
4718 VK_CONDITIONAL_RENDERING_INVERTED_BIT_EXT;
4719
4720 cmd_state->conditional_render_enabled = true;
4721
4722 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
4723
4724 /* Section 19.4 of the Vulkan 1.1.85 spec says:
4725 *
4726 * If the value of the predicate in buffer memory changes
4727 * while conditional rendering is active, the rendering commands
4728 * may be discarded in an implementation-dependent way.
4729 * Some implementations may latch the value of the predicate
4730 * upon beginning conditional rendering while others
4731 * may read it before every rendering command.
4732 *
4733 * So it's perfectly fine to read a value from the buffer once.
4734 */
4735 emit_lrm(&cmd_buffer->batch, CS_GPR(MI_ALU_REG0), value_address);
4736 /* Zero the top 32-bits of MI_PREDICATE_SRC0 */
4737 emit_lri(&cmd_buffer->batch, CS_GPR(MI_ALU_REG0) + 4, 0);
4738
4739 /* Precompute predicate result, it is necessary to support secondary
4740 * command buffers since it is unknown if conditional rendering is
4741 * inverted when populating them.
4742 */
4743 uint32_t *dw = anv_batch_emitn(&cmd_buffer->batch, 5, GENX(MI_MATH));
4744 dw[1] = mi_alu(MI_ALU_LOAD0, MI_ALU_SRCA, 0);
4745 dw[2] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, MI_ALU_REG0);
4746 dw[3] = mi_alu(MI_ALU_SUB, 0, 0);
4747 dw[4] = mi_alu(isInverted ? MI_ALU_STOREINV : MI_ALU_STORE,
4748 ANV_PREDICATE_RESULT_REG, MI_ALU_CF);
4749 }
4750
4751 void genX(CmdEndConditionalRenderingEXT)(
4752 VkCommandBuffer commandBuffer)
4753 {
4754 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4755 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
4756
4757 cmd_state->conditional_render_enabled = false;
4758 }
4759 #endif