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