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