anv: Use an actual binding for gl_NumWorkgroups
[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_count > 0 && devinfo->gen < 9) {
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 assert(image < MAX_GEN8_IMAGES);
2207 struct brw_image_param *image_param =
2208 &cmd_buffer->state.push_constants[stage]->images[image];
2209
2210 *image_param =
2211 desc->image_view->planes[binding->plane].storage_image_param;
2212 }
2213 image++;
2214 break;
2215 }
2216
2217 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2218 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2219 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2220 surface_state = desc->buffer_view->surface_state;
2221 assert(surface_state.alloc_size);
2222 add_surface_reloc(cmd_buffer, surface_state,
2223 desc->buffer_view->address);
2224 break;
2225
2226 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2227 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2228 /* Compute the offset within the buffer */
2229 uint32_t dynamic_offset =
2230 dynamic_offset_for_binding(pipe_state, binding);
2231 uint64_t offset = desc->offset + dynamic_offset;
2232 /* Clamp to the buffer size */
2233 offset = MIN2(offset, desc->buffer->size);
2234 /* Clamp the range to the buffer size */
2235 uint32_t range = MIN2(desc->range, desc->buffer->size - offset);
2236
2237 struct anv_address address =
2238 anv_address_add(desc->buffer->address, offset);
2239
2240 surface_state =
2241 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
2242 enum isl_format format =
2243 anv_isl_format_for_descriptor_type(desc->type);
2244
2245 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
2246 format, address, range, 1);
2247 add_surface_reloc(cmd_buffer, surface_state, address);
2248 break;
2249 }
2250
2251 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2252 surface_state = (binding->write_only)
2253 ? desc->buffer_view->writeonly_storage_surface_state
2254 : desc->buffer_view->storage_surface_state;
2255 assert(surface_state.alloc_size);
2256 add_surface_reloc(cmd_buffer, surface_state,
2257 desc->buffer_view->address);
2258 if (devinfo->gen < 9) {
2259 assert(image < MAX_GEN8_IMAGES);
2260 struct brw_image_param *image_param =
2261 &cmd_buffer->state.push_constants[stage]->images[image];
2262
2263 *image_param = desc->buffer_view->storage_image_param;
2264 }
2265 image++;
2266 break;
2267
2268 default:
2269 assert(!"Invalid descriptor type");
2270 continue;
2271 }
2272
2273 bt_map[s] = surface_state.offset + state_offset;
2274 }
2275 assert(image == map->image_count);
2276
2277 #if GEN_GEN >= 11
2278 /* The PIPE_CONTROL command description says:
2279 *
2280 * "Whenever a Binding Table Index (BTI) used by a Render Taget Message
2281 * points to a different RENDER_SURFACE_STATE, SW must issue a Render
2282 * Target Cache Flush by enabling this bit. When render target flush
2283 * is set due to new association of BTI, PS Scoreboard Stall bit must
2284 * be set in this packet."
2285 *
2286 * FINISHME: Currently we shuffle around the surface states in the binding
2287 * table based on if they are getting used or not. So, we've to do below
2288 * pipe control flush for every binding table upload. Make changes so
2289 * that we do it only when we modify render target surface states.
2290 */
2291 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
2292 pc.RenderTargetCacheFlushEnable = true;
2293 pc.StallAtPixelScoreboard = true;
2294 }
2295 #endif
2296
2297 return VK_SUCCESS;
2298 }
2299
2300 static VkResult
2301 emit_samplers(struct anv_cmd_buffer *cmd_buffer,
2302 gl_shader_stage stage,
2303 struct anv_state *state)
2304 {
2305 struct anv_cmd_pipeline_state *pipe_state =
2306 stage == MESA_SHADER_COMPUTE ? &cmd_buffer->state.compute.base :
2307 &cmd_buffer->state.gfx.base;
2308 struct anv_pipeline *pipeline = pipe_state->pipeline;
2309
2310 if (!anv_pipeline_has_stage(pipeline, stage)) {
2311 *state = (struct anv_state) { 0, };
2312 return VK_SUCCESS;
2313 }
2314
2315 struct anv_pipeline_bind_map *map = &pipeline->shaders[stage]->bind_map;
2316 if (map->sampler_count == 0) {
2317 *state = (struct anv_state) { 0, };
2318 return VK_SUCCESS;
2319 }
2320
2321 uint32_t size = map->sampler_count * 16;
2322 *state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, 32);
2323
2324 if (state->map == NULL)
2325 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2326
2327 for (uint32_t s = 0; s < map->sampler_count; s++) {
2328 struct anv_pipeline_binding *binding = &map->sampler_to_descriptor[s];
2329 const struct anv_descriptor *desc =
2330 anv_descriptor_for_binding(pipe_state, binding);
2331
2332 if (desc->type != VK_DESCRIPTOR_TYPE_SAMPLER &&
2333 desc->type != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
2334 continue;
2335
2336 struct anv_sampler *sampler = desc->sampler;
2337
2338 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
2339 * happens to be zero.
2340 */
2341 if (sampler == NULL)
2342 continue;
2343
2344 memcpy(state->map + (s * 16),
2345 sampler->state[binding->plane], sizeof(sampler->state[0]));
2346 }
2347
2348 return VK_SUCCESS;
2349 }
2350
2351 static uint32_t
2352 flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer)
2353 {
2354 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2355
2356 VkShaderStageFlags dirty = cmd_buffer->state.descriptors_dirty &
2357 pipeline->active_stages;
2358
2359 VkResult result = VK_SUCCESS;
2360 anv_foreach_stage(s, dirty) {
2361 result = emit_samplers(cmd_buffer, s, &cmd_buffer->state.samplers[s]);
2362 if (result != VK_SUCCESS)
2363 break;
2364 result = emit_binding_table(cmd_buffer, s,
2365 &cmd_buffer->state.binding_tables[s]);
2366 if (result != VK_SUCCESS)
2367 break;
2368 }
2369
2370 if (result != VK_SUCCESS) {
2371 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
2372
2373 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
2374 if (result != VK_SUCCESS)
2375 return 0;
2376
2377 /* Re-emit state base addresses so we get the new surface state base
2378 * address before we start emitting binding tables etc.
2379 */
2380 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
2381
2382 /* Re-emit all active binding tables */
2383 dirty |= pipeline->active_stages;
2384 anv_foreach_stage(s, dirty) {
2385 result = emit_samplers(cmd_buffer, s, &cmd_buffer->state.samplers[s]);
2386 if (result != VK_SUCCESS) {
2387 anv_batch_set_error(&cmd_buffer->batch, result);
2388 return 0;
2389 }
2390 result = emit_binding_table(cmd_buffer, s,
2391 &cmd_buffer->state.binding_tables[s]);
2392 if (result != VK_SUCCESS) {
2393 anv_batch_set_error(&cmd_buffer->batch, result);
2394 return 0;
2395 }
2396 }
2397 }
2398
2399 cmd_buffer->state.descriptors_dirty &= ~dirty;
2400
2401 return dirty;
2402 }
2403
2404 static void
2405 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer *cmd_buffer,
2406 uint32_t stages)
2407 {
2408 static const uint32_t sampler_state_opcodes[] = {
2409 [MESA_SHADER_VERTEX] = 43,
2410 [MESA_SHADER_TESS_CTRL] = 44, /* HS */
2411 [MESA_SHADER_TESS_EVAL] = 45, /* DS */
2412 [MESA_SHADER_GEOMETRY] = 46,
2413 [MESA_SHADER_FRAGMENT] = 47,
2414 [MESA_SHADER_COMPUTE] = 0,
2415 };
2416
2417 static const uint32_t binding_table_opcodes[] = {
2418 [MESA_SHADER_VERTEX] = 38,
2419 [MESA_SHADER_TESS_CTRL] = 39,
2420 [MESA_SHADER_TESS_EVAL] = 40,
2421 [MESA_SHADER_GEOMETRY] = 41,
2422 [MESA_SHADER_FRAGMENT] = 42,
2423 [MESA_SHADER_COMPUTE] = 0,
2424 };
2425
2426 anv_foreach_stage(s, stages) {
2427 assert(s < ARRAY_SIZE(binding_table_opcodes));
2428 assert(binding_table_opcodes[s] > 0);
2429
2430 if (cmd_buffer->state.samplers[s].alloc_size > 0) {
2431 anv_batch_emit(&cmd_buffer->batch,
2432 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ssp) {
2433 ssp._3DCommandSubOpcode = sampler_state_opcodes[s];
2434 ssp.PointertoVSSamplerState = cmd_buffer->state.samplers[s].offset;
2435 }
2436 }
2437
2438 /* Always emit binding table pointers if we're asked to, since on SKL
2439 * this is what flushes push constants. */
2440 anv_batch_emit(&cmd_buffer->batch,
2441 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), btp) {
2442 btp._3DCommandSubOpcode = binding_table_opcodes[s];
2443 btp.PointertoVSBindingTable = cmd_buffer->state.binding_tables[s].offset;
2444 }
2445 }
2446 }
2447
2448 static void
2449 cmd_buffer_flush_push_constants(struct anv_cmd_buffer *cmd_buffer,
2450 VkShaderStageFlags dirty_stages)
2451 {
2452 const struct anv_cmd_graphics_state *gfx_state = &cmd_buffer->state.gfx;
2453 const struct anv_pipeline *pipeline = gfx_state->base.pipeline;
2454
2455 static const uint32_t push_constant_opcodes[] = {
2456 [MESA_SHADER_VERTEX] = 21,
2457 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
2458 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
2459 [MESA_SHADER_GEOMETRY] = 22,
2460 [MESA_SHADER_FRAGMENT] = 23,
2461 [MESA_SHADER_COMPUTE] = 0,
2462 };
2463
2464 VkShaderStageFlags flushed = 0;
2465
2466 anv_foreach_stage(stage, dirty_stages) {
2467 assert(stage < ARRAY_SIZE(push_constant_opcodes));
2468 assert(push_constant_opcodes[stage] > 0);
2469
2470 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CONSTANT_VS), c) {
2471 c._3DCommandSubOpcode = push_constant_opcodes[stage];
2472
2473 if (anv_pipeline_has_stage(pipeline, stage)) {
2474 #if GEN_GEN >= 8 || GEN_IS_HASWELL
2475 const struct brw_stage_prog_data *prog_data =
2476 pipeline->shaders[stage]->prog_data;
2477 const struct anv_pipeline_bind_map *bind_map =
2478 &pipeline->shaders[stage]->bind_map;
2479
2480 /* The Skylake PRM contains the following restriction:
2481 *
2482 * "The driver must ensure The following case does not occur
2483 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
2484 * buffer 3 read length equal to zero committed followed by a
2485 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
2486 * zero committed."
2487 *
2488 * To avoid this, we program the buffers in the highest slots.
2489 * This way, slot 0 is only used if slot 3 is also used.
2490 */
2491 int n = 3;
2492
2493 for (int i = 3; i >= 0; i--) {
2494 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
2495 if (range->length == 0)
2496 continue;
2497
2498 const unsigned surface =
2499 prog_data->binding_table.ubo_start + range->block;
2500
2501 assert(surface <= bind_map->surface_count);
2502 const struct anv_pipeline_binding *binding =
2503 &bind_map->surface_to_descriptor[surface];
2504
2505 struct anv_address read_addr;
2506 uint32_t read_len;
2507 if (binding->set == ANV_DESCRIPTOR_SET_SHADER_CONSTANTS) {
2508 struct anv_address constant_data = {
2509 .bo = pipeline->device->dynamic_state_pool.block_pool.bo,
2510 .offset = pipeline->shaders[stage]->constant_data.offset,
2511 };
2512 unsigned constant_data_size =
2513 pipeline->shaders[stage]->constant_data_size;
2514
2515 read_len = MIN2(range->length,
2516 DIV_ROUND_UP(constant_data_size, 32) - range->start);
2517 read_addr = anv_address_add(constant_data,
2518 range->start * 32);
2519 } else {
2520 const struct anv_descriptor *desc =
2521 anv_descriptor_for_binding(&gfx_state->base, binding);
2522
2523 if (desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) {
2524 read_len = MIN2(range->length,
2525 DIV_ROUND_UP(desc->buffer_view->range, 32) - range->start);
2526 read_addr = anv_address_add(desc->buffer_view->address,
2527 range->start * 32);
2528 } else {
2529 assert(desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
2530
2531 uint32_t dynamic_offset =
2532 dynamic_offset_for_binding(&gfx_state->base, binding);
2533 uint32_t buf_offset =
2534 MIN2(desc->offset + dynamic_offset, desc->buffer->size);
2535 uint32_t buf_range =
2536 MIN2(desc->range, desc->buffer->size - buf_offset);
2537
2538 read_len = MIN2(range->length,
2539 DIV_ROUND_UP(buf_range, 32) - range->start);
2540 read_addr = anv_address_add(desc->buffer->address,
2541 buf_offset + range->start * 32);
2542 }
2543 }
2544
2545 if (read_len > 0) {
2546 c.ConstantBody.Buffer[n] = read_addr;
2547 c.ConstantBody.ReadLength[n] = read_len;
2548 n--;
2549 }
2550 }
2551
2552 struct anv_state state =
2553 anv_cmd_buffer_push_constants(cmd_buffer, stage);
2554
2555 if (state.alloc_size > 0) {
2556 c.ConstantBody.Buffer[n] = (struct anv_address) {
2557 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2558 .offset = state.offset,
2559 };
2560 c.ConstantBody.ReadLength[n] =
2561 DIV_ROUND_UP(state.alloc_size, 32);
2562 }
2563 #else
2564 /* For Ivy Bridge, the push constants packets have a different
2565 * rule that would require us to iterate in the other direction
2566 * and possibly mess around with dynamic state base address.
2567 * Don't bother; just emit regular push constants at n = 0.
2568 */
2569 struct anv_state state =
2570 anv_cmd_buffer_push_constants(cmd_buffer, stage);
2571
2572 if (state.alloc_size > 0) {
2573 c.ConstantBody.Buffer[0].offset = state.offset,
2574 c.ConstantBody.ReadLength[0] =
2575 DIV_ROUND_UP(state.alloc_size, 32);
2576 }
2577 #endif
2578 }
2579 }
2580
2581 flushed |= mesa_to_vk_shader_stage(stage);
2582 }
2583
2584 cmd_buffer->state.push_constants_dirty &= ~flushed;
2585 }
2586
2587 void
2588 genX(cmd_buffer_flush_state)(struct anv_cmd_buffer *cmd_buffer)
2589 {
2590 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2591 uint32_t *p;
2592
2593 uint32_t vb_emit = cmd_buffer->state.gfx.vb_dirty & pipeline->vb_used;
2594 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_PIPELINE)
2595 vb_emit |= pipeline->vb_used;
2596
2597 assert((pipeline->active_stages & VK_SHADER_STAGE_COMPUTE_BIT) == 0);
2598
2599 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
2600
2601 genX(flush_pipeline_select_3d)(cmd_buffer);
2602
2603 if (vb_emit) {
2604 const uint32_t num_buffers = __builtin_popcount(vb_emit);
2605 const uint32_t num_dwords = 1 + num_buffers * 4;
2606
2607 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
2608 GENX(3DSTATE_VERTEX_BUFFERS));
2609 uint32_t vb, i = 0;
2610 for_each_bit(vb, vb_emit) {
2611 struct anv_buffer *buffer = cmd_buffer->state.vertex_bindings[vb].buffer;
2612 uint32_t offset = cmd_buffer->state.vertex_bindings[vb].offset;
2613
2614 struct GENX(VERTEX_BUFFER_STATE) state = {
2615 .VertexBufferIndex = vb,
2616
2617 .MOCS = anv_mocs_for_bo(cmd_buffer->device, buffer->address.bo),
2618 #if GEN_GEN <= 7
2619 .BufferAccessType = pipeline->vb[vb].instanced ? INSTANCEDATA : VERTEXDATA,
2620 .InstanceDataStepRate = pipeline->vb[vb].instance_divisor,
2621 #endif
2622
2623 .AddressModifyEnable = true,
2624 .BufferPitch = pipeline->vb[vb].stride,
2625 .BufferStartingAddress = anv_address_add(buffer->address, offset),
2626
2627 #if GEN_GEN >= 8
2628 .BufferSize = buffer->size - offset
2629 #else
2630 .EndAddress = anv_address_add(buffer->address, buffer->size - 1),
2631 #endif
2632 };
2633
2634 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, &p[1 + i * 4], &state);
2635 i++;
2636 }
2637 }
2638
2639 cmd_buffer->state.gfx.vb_dirty &= ~vb_emit;
2640
2641 #if GEN_GEN >= 8
2642 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_XFB_ENABLE) {
2643 /* We don't need any per-buffer dirty tracking because you're not
2644 * allowed to bind different XFB buffers while XFB is enabled.
2645 */
2646 for (unsigned idx = 0; idx < MAX_XFB_BUFFERS; idx++) {
2647 struct anv_xfb_binding *xfb = &cmd_buffer->state.xfb_bindings[idx];
2648 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_SO_BUFFER), sob) {
2649 sob.SOBufferIndex = idx;
2650
2651 if (cmd_buffer->state.xfb_enabled && xfb->buffer) {
2652 sob.SOBufferEnable = true;
2653 sob.MOCS = cmd_buffer->device->default_mocs,
2654 sob.StreamOffsetWriteEnable = false;
2655 sob.SurfaceBaseAddress = anv_address_add(xfb->buffer->address,
2656 xfb->offset);
2657 /* Size is in DWords - 1 */
2658 sob.SurfaceSize = xfb->size / 4 - 1;
2659 }
2660 }
2661 }
2662
2663 /* CNL and later require a CS stall after 3DSTATE_SO_BUFFER */
2664 if (GEN_GEN >= 10)
2665 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
2666 }
2667 #endif
2668
2669 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_PIPELINE) {
2670 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
2671
2672 /* The exact descriptor layout is pulled from the pipeline, so we need
2673 * to re-emit binding tables on every pipeline change.
2674 */
2675 cmd_buffer->state.descriptors_dirty |= pipeline->active_stages;
2676
2677 /* If the pipeline changed, we may need to re-allocate push constant
2678 * space in the URB.
2679 */
2680 cmd_buffer_alloc_push_constants(cmd_buffer);
2681 }
2682
2683 #if GEN_GEN <= 7
2684 if (cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_VERTEX_BIT ||
2685 cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_VERTEX_BIT) {
2686 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
2687 *
2688 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
2689 * stall needs to be sent just prior to any 3DSTATE_VS,
2690 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
2691 * 3DSTATE_BINDING_TABLE_POINTER_VS,
2692 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
2693 * PIPE_CONTROL needs to be sent before any combination of VS
2694 * associated 3DSTATE."
2695 */
2696 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
2697 pc.DepthStallEnable = true;
2698 pc.PostSyncOperation = WriteImmediateData;
2699 pc.Address =
2700 (struct anv_address) { &cmd_buffer->device->workaround_bo, 0 };
2701 }
2702 }
2703 #endif
2704
2705 /* Render targets live in the same binding table as fragment descriptors */
2706 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_RENDER_TARGETS)
2707 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_FRAGMENT_BIT;
2708
2709 /* We emit the binding tables and sampler tables first, then emit push
2710 * constants and then finally emit binding table and sampler table
2711 * pointers. It has to happen in this order, since emitting the binding
2712 * tables may change the push constants (in case of storage images). After
2713 * emitting push constants, on SKL+ we have to emit the corresponding
2714 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
2715 */
2716 uint32_t dirty = 0;
2717 if (cmd_buffer->state.descriptors_dirty)
2718 dirty = flush_descriptor_sets(cmd_buffer);
2719
2720 if (dirty || cmd_buffer->state.push_constants_dirty) {
2721 /* Because we're pushing UBOs, we have to push whenever either
2722 * descriptors or push constants is dirty.
2723 */
2724 dirty |= cmd_buffer->state.push_constants_dirty;
2725 dirty &= ANV_STAGE_MASK & VK_SHADER_STAGE_ALL_GRAPHICS;
2726 cmd_buffer_flush_push_constants(cmd_buffer, dirty);
2727 }
2728
2729 if (dirty)
2730 cmd_buffer_emit_descriptor_pointers(cmd_buffer, dirty);
2731
2732 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_VIEWPORT)
2733 gen8_cmd_buffer_emit_viewport(cmd_buffer);
2734
2735 if (cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT |
2736 ANV_CMD_DIRTY_PIPELINE)) {
2737 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer,
2738 pipeline->depth_clamp_enable);
2739 }
2740
2741 if (cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_DYNAMIC_SCISSOR |
2742 ANV_CMD_DIRTY_RENDER_TARGETS))
2743 gen7_cmd_buffer_emit_scissor(cmd_buffer);
2744
2745 genX(cmd_buffer_flush_dynamic_state)(cmd_buffer);
2746
2747 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
2748 }
2749
2750 static void
2751 emit_vertex_bo(struct anv_cmd_buffer *cmd_buffer,
2752 struct anv_address addr,
2753 uint32_t size, uint32_t index)
2754 {
2755 uint32_t *p = anv_batch_emitn(&cmd_buffer->batch, 5,
2756 GENX(3DSTATE_VERTEX_BUFFERS));
2757
2758 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, p + 1,
2759 &(struct GENX(VERTEX_BUFFER_STATE)) {
2760 .VertexBufferIndex = index,
2761 .AddressModifyEnable = true,
2762 .BufferPitch = 0,
2763 .MOCS = anv_mocs_for_bo(cmd_buffer->device, addr.bo),
2764 #if (GEN_GEN >= 8)
2765 .BufferStartingAddress = addr,
2766 .BufferSize = size
2767 #else
2768 .BufferStartingAddress = addr,
2769 .EndAddress = anv_address_add(addr, size),
2770 #endif
2771 });
2772 }
2773
2774 static void
2775 emit_base_vertex_instance_bo(struct anv_cmd_buffer *cmd_buffer,
2776 struct anv_address addr)
2777 {
2778 emit_vertex_bo(cmd_buffer, addr, 8, ANV_SVGS_VB_INDEX);
2779 }
2780
2781 static void
2782 emit_base_vertex_instance(struct anv_cmd_buffer *cmd_buffer,
2783 uint32_t base_vertex, uint32_t base_instance)
2784 {
2785 struct anv_state id_state =
2786 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 8, 4);
2787
2788 ((uint32_t *)id_state.map)[0] = base_vertex;
2789 ((uint32_t *)id_state.map)[1] = base_instance;
2790
2791 struct anv_address addr = {
2792 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2793 .offset = id_state.offset,
2794 };
2795
2796 emit_base_vertex_instance_bo(cmd_buffer, addr);
2797 }
2798
2799 static void
2800 emit_draw_index(struct anv_cmd_buffer *cmd_buffer, uint32_t draw_index)
2801 {
2802 struct anv_state state =
2803 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 4, 4);
2804
2805 ((uint32_t *)state.map)[0] = draw_index;
2806
2807 struct anv_address addr = {
2808 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2809 .offset = state.offset,
2810 };
2811
2812 emit_vertex_bo(cmd_buffer, addr, 4, ANV_DRAWID_VB_INDEX);
2813 }
2814
2815 void genX(CmdDraw)(
2816 VkCommandBuffer commandBuffer,
2817 uint32_t vertexCount,
2818 uint32_t instanceCount,
2819 uint32_t firstVertex,
2820 uint32_t firstInstance)
2821 {
2822 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2823 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2824 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2825
2826 if (anv_batch_has_error(&cmd_buffer->batch))
2827 return;
2828
2829 genX(cmd_buffer_flush_state)(cmd_buffer);
2830
2831 if (cmd_buffer->state.conditional_render_enabled)
2832 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
2833
2834 if (vs_prog_data->uses_firstvertex ||
2835 vs_prog_data->uses_baseinstance)
2836 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
2837 if (vs_prog_data->uses_drawid)
2838 emit_draw_index(cmd_buffer, 0);
2839
2840 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2841 * different views. We need to multiply instanceCount by the view count.
2842 */
2843 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
2844
2845 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
2846 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
2847 prim.VertexAccessType = SEQUENTIAL;
2848 prim.PrimitiveTopologyType = pipeline->topology;
2849 prim.VertexCountPerInstance = vertexCount;
2850 prim.StartVertexLocation = firstVertex;
2851 prim.InstanceCount = instanceCount;
2852 prim.StartInstanceLocation = firstInstance;
2853 prim.BaseVertexLocation = 0;
2854 }
2855 }
2856
2857 void genX(CmdDrawIndexed)(
2858 VkCommandBuffer commandBuffer,
2859 uint32_t indexCount,
2860 uint32_t instanceCount,
2861 uint32_t firstIndex,
2862 int32_t vertexOffset,
2863 uint32_t firstInstance)
2864 {
2865 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2866 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2867 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2868
2869 if (anv_batch_has_error(&cmd_buffer->batch))
2870 return;
2871
2872 genX(cmd_buffer_flush_state)(cmd_buffer);
2873
2874 if (cmd_buffer->state.conditional_render_enabled)
2875 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
2876
2877 if (vs_prog_data->uses_firstvertex ||
2878 vs_prog_data->uses_baseinstance)
2879 emit_base_vertex_instance(cmd_buffer, vertexOffset, firstInstance);
2880 if (vs_prog_data->uses_drawid)
2881 emit_draw_index(cmd_buffer, 0);
2882
2883 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2884 * different views. We need to multiply instanceCount by the view count.
2885 */
2886 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
2887
2888 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
2889 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
2890 prim.VertexAccessType = RANDOM;
2891 prim.PrimitiveTopologyType = pipeline->topology;
2892 prim.VertexCountPerInstance = indexCount;
2893 prim.StartVertexLocation = firstIndex;
2894 prim.InstanceCount = instanceCount;
2895 prim.StartInstanceLocation = firstInstance;
2896 prim.BaseVertexLocation = vertexOffset;
2897 }
2898 }
2899
2900 /* Auto-Draw / Indirect Registers */
2901 #define GEN7_3DPRIM_END_OFFSET 0x2420
2902 #define GEN7_3DPRIM_START_VERTEX 0x2430
2903 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
2904 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
2905 #define GEN7_3DPRIM_START_INSTANCE 0x243C
2906 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
2907
2908 /* MI_MATH only exists on Haswell+ */
2909 #if GEN_IS_HASWELL || GEN_GEN >= 8
2910
2911 /* Emit dwords to multiply GPR0 by N */
2912 static void
2913 build_alu_multiply_gpr0(uint32_t *dw, unsigned *dw_count, uint32_t N)
2914 {
2915 VK_OUTARRAY_MAKE(out, dw, dw_count);
2916
2917 #define append_alu(opcode, operand1, operand2) \
2918 vk_outarray_append(&out, alu_dw) *alu_dw = mi_alu(opcode, operand1, operand2)
2919
2920 assert(N > 0);
2921 unsigned top_bit = 31 - __builtin_clz(N);
2922 for (int i = top_bit - 1; i >= 0; i--) {
2923 /* We get our initial data in GPR0 and we write the final data out to
2924 * GPR0 but we use GPR1 as our scratch register.
2925 */
2926 unsigned src_reg = i == top_bit - 1 ? MI_ALU_REG0 : MI_ALU_REG1;
2927 unsigned dst_reg = i == 0 ? MI_ALU_REG0 : MI_ALU_REG1;
2928
2929 /* Shift the current value left by 1 */
2930 append_alu(MI_ALU_LOAD, MI_ALU_SRCA, src_reg);
2931 append_alu(MI_ALU_LOAD, MI_ALU_SRCB, src_reg);
2932 append_alu(MI_ALU_ADD, 0, 0);
2933
2934 if (N & (1 << i)) {
2935 /* Store ACCU to R1 and add R0 to R1 */
2936 append_alu(MI_ALU_STORE, MI_ALU_REG1, MI_ALU_ACCU);
2937 append_alu(MI_ALU_LOAD, MI_ALU_SRCA, MI_ALU_REG0);
2938 append_alu(MI_ALU_LOAD, MI_ALU_SRCB, MI_ALU_REG1);
2939 append_alu(MI_ALU_ADD, 0, 0);
2940 }
2941
2942 append_alu(MI_ALU_STORE, dst_reg, MI_ALU_ACCU);
2943 }
2944
2945 #undef append_alu
2946 }
2947
2948 static void
2949 emit_mul_gpr0(struct anv_batch *batch, uint32_t N)
2950 {
2951 uint32_t num_dwords;
2952 build_alu_multiply_gpr0(NULL, &num_dwords, N);
2953
2954 uint32_t *dw = anv_batch_emitn(batch, 1 + num_dwords, GENX(MI_MATH));
2955 build_alu_multiply_gpr0(dw + 1, &num_dwords, N);
2956 }
2957
2958 static void
2959 emit_alu_add(struct anv_batch *batch, unsigned dst_reg,
2960 unsigned reg_a, unsigned reg_b)
2961 {
2962 uint32_t *dw = anv_batch_emitn(batch, 1 + 4, GENX(MI_MATH));
2963 dw[1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, reg_a);
2964 dw[2] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, reg_b);
2965 dw[3] = mi_alu(MI_ALU_ADD, 0, 0);
2966 dw[4] = mi_alu(MI_ALU_STORE, dst_reg, MI_ALU_ACCU);
2967 }
2968
2969 static void
2970 emit_add32_gpr0(struct anv_batch *batch, uint32_t N)
2971 {
2972 emit_lri(batch, CS_GPR(1), N);
2973 emit_alu_add(batch, MI_ALU_REG0, MI_ALU_REG0, MI_ALU_REG1);
2974 }
2975
2976 static void
2977 emit_alu_shl(struct anv_batch *batch, unsigned dst_reg,
2978 unsigned src_reg, unsigned shift)
2979 {
2980 assert(shift > 0);
2981
2982 uint32_t *dw = anv_batch_emitn(batch, 1 + 4 * shift, GENX(MI_MATH));
2983 for (unsigned i = 0; i < shift; i++) {
2984 unsigned add_src = (i == 0) ? src_reg : dst_reg;
2985 dw[1 + (i * 4) + 0] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, add_src);
2986 dw[1 + (i * 4) + 1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, add_src);
2987 dw[1 + (i * 4) + 2] = mi_alu(MI_ALU_ADD, 0, 0);
2988 dw[1 + (i * 4) + 3] = mi_alu(MI_ALU_STORE, dst_reg, MI_ALU_ACCU);
2989 }
2990 }
2991
2992 static void
2993 emit_div32_gpr0(struct anv_batch *batch, uint32_t D)
2994 {
2995 /* Zero out the top of GPR0 */
2996 emit_lri(batch, CS_GPR(0) + 4, 0);
2997
2998 if (D == 0) {
2999 /* This invalid, but we should do something so we set GPR0 to 0. */
3000 emit_lri(batch, CS_GPR(0), 0);
3001 } else if (util_is_power_of_two_or_zero(D)) {
3002 unsigned log2_D = util_logbase2(D);
3003 assert(log2_D < 32);
3004 /* We right-shift by log2(D) by left-shifting by 32 - log2(D) and taking
3005 * the top 32 bits of the result.
3006 */
3007 emit_alu_shl(batch, MI_ALU_REG0, MI_ALU_REG0, 32 - log2_D);
3008 emit_lrr(batch, CS_GPR(0) + 0, CS_GPR(0) + 4);
3009 emit_lri(batch, CS_GPR(0) + 4, 0);
3010 } else {
3011 struct util_fast_udiv_info m = util_compute_fast_udiv_info(D, 32, 32);
3012 assert(m.multiplier <= UINT32_MAX);
3013
3014 if (m.pre_shift) {
3015 /* We right-shift by L by left-shifting by 32 - l and taking the top
3016 * 32 bits of the result.
3017 */
3018 if (m.pre_shift < 32)
3019 emit_alu_shl(batch, MI_ALU_REG0, MI_ALU_REG0, 32 - m.pre_shift);
3020 emit_lrr(batch, CS_GPR(0) + 0, CS_GPR(0) + 4);
3021 emit_lri(batch, CS_GPR(0) + 4, 0);
3022 }
3023
3024 /* Do the 32x32 multiply into gpr0 */
3025 emit_mul_gpr0(batch, m.multiplier);
3026
3027 if (m.increment) {
3028 /* If we need to increment, save off a copy of GPR0 */
3029 emit_lri(batch, CS_GPR(1) + 0, m.multiplier);
3030 emit_lri(batch, CS_GPR(1) + 4, 0);
3031 emit_alu_add(batch, MI_ALU_REG0, MI_ALU_REG0, MI_ALU_REG1);
3032 }
3033
3034 /* Shift by 32 */
3035 emit_lrr(batch, CS_GPR(0) + 0, CS_GPR(0) + 4);
3036 emit_lri(batch, CS_GPR(0) + 4, 0);
3037
3038 if (m.post_shift) {
3039 /* We right-shift by L by left-shifting by 32 - l and taking the top
3040 * 32 bits of the result.
3041 */
3042 if (m.post_shift < 32)
3043 emit_alu_shl(batch, MI_ALU_REG0, MI_ALU_REG0, 32 - m.post_shift);
3044 emit_lrr(batch, CS_GPR(0) + 0, CS_GPR(0) + 4);
3045 emit_lri(batch, CS_GPR(0) + 4, 0);
3046 }
3047 }
3048 }
3049
3050 #endif /* GEN_IS_HASWELL || GEN_GEN >= 8 */
3051
3052 void genX(CmdDrawIndirectByteCountEXT)(
3053 VkCommandBuffer commandBuffer,
3054 uint32_t instanceCount,
3055 uint32_t firstInstance,
3056 VkBuffer counterBuffer,
3057 VkDeviceSize counterBufferOffset,
3058 uint32_t counterOffset,
3059 uint32_t vertexStride)
3060 {
3061 #if GEN_IS_HASWELL || GEN_GEN >= 8
3062 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3063 ANV_FROM_HANDLE(anv_buffer, counter_buffer, counterBuffer);
3064 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
3065 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3066
3067 /* firstVertex is always zero for this draw function */
3068 const uint32_t firstVertex = 0;
3069
3070 if (anv_batch_has_error(&cmd_buffer->batch))
3071 return;
3072
3073 genX(cmd_buffer_flush_state)(cmd_buffer);
3074
3075 if (vs_prog_data->uses_firstvertex ||
3076 vs_prog_data->uses_baseinstance)
3077 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
3078 if (vs_prog_data->uses_drawid)
3079 emit_draw_index(cmd_buffer, 0);
3080
3081 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3082 * different views. We need to multiply instanceCount by the view count.
3083 */
3084 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
3085
3086 emit_lrm(&cmd_buffer->batch, CS_GPR(0),
3087 anv_address_add(counter_buffer->address, counterBufferOffset));
3088 if (counterOffset)
3089 emit_add32_gpr0(&cmd_buffer->batch, -counterOffset);
3090 emit_div32_gpr0(&cmd_buffer->batch, vertexStride);
3091 emit_lrr(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, CS_GPR(0));
3092
3093 emit_lri(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, firstVertex);
3094 emit_lri(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, instanceCount);
3095 emit_lri(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, firstInstance);
3096 emit_lri(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, 0);
3097
3098 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3099 prim.IndirectParameterEnable = true;
3100 prim.VertexAccessType = SEQUENTIAL;
3101 prim.PrimitiveTopologyType = pipeline->topology;
3102 }
3103 #endif /* GEN_IS_HASWELL || GEN_GEN >= 8 */
3104 }
3105
3106 static void
3107 load_indirect_parameters(struct anv_cmd_buffer *cmd_buffer,
3108 struct anv_address addr,
3109 bool indexed)
3110 {
3111 struct anv_batch *batch = &cmd_buffer->batch;
3112
3113 emit_lrm(batch, GEN7_3DPRIM_VERTEX_COUNT, anv_address_add(addr, 0));
3114
3115 unsigned view_count = anv_subpass_view_count(cmd_buffer->state.subpass);
3116 if (view_count > 1) {
3117 #if GEN_IS_HASWELL || GEN_GEN >= 8
3118 emit_lrm(batch, CS_GPR(0), anv_address_add(addr, 4));
3119 emit_mul_gpr0(batch, view_count);
3120 emit_lrr(batch, GEN7_3DPRIM_INSTANCE_COUNT, CS_GPR(0));
3121 #else
3122 anv_finishme("Multiview + indirect draw requires MI_MATH; "
3123 "MI_MATH is not supported on Ivy Bridge");
3124 emit_lrm(batch, GEN7_3DPRIM_INSTANCE_COUNT, anv_address_add(addr, 4));
3125 #endif
3126 } else {
3127 emit_lrm(batch, GEN7_3DPRIM_INSTANCE_COUNT, anv_address_add(addr, 4));
3128 }
3129
3130 emit_lrm(batch, GEN7_3DPRIM_START_VERTEX, anv_address_add(addr, 8));
3131
3132 if (indexed) {
3133 emit_lrm(batch, GEN7_3DPRIM_BASE_VERTEX, anv_address_add(addr, 12));
3134 emit_lrm(batch, GEN7_3DPRIM_START_INSTANCE, anv_address_add(addr, 16));
3135 } else {
3136 emit_lrm(batch, GEN7_3DPRIM_START_INSTANCE, anv_address_add(addr, 12));
3137 emit_lri(batch, GEN7_3DPRIM_BASE_VERTEX, 0);
3138 }
3139 }
3140
3141 void genX(CmdDrawIndirect)(
3142 VkCommandBuffer commandBuffer,
3143 VkBuffer _buffer,
3144 VkDeviceSize offset,
3145 uint32_t drawCount,
3146 uint32_t stride)
3147 {
3148 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3149 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3150 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
3151 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3152
3153 if (anv_batch_has_error(&cmd_buffer->batch))
3154 return;
3155
3156 genX(cmd_buffer_flush_state)(cmd_buffer);
3157
3158 if (cmd_buffer->state.conditional_render_enabled)
3159 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3160
3161 for (uint32_t i = 0; i < drawCount; i++) {
3162 struct anv_address draw = anv_address_add(buffer->address, offset);
3163
3164 if (vs_prog_data->uses_firstvertex ||
3165 vs_prog_data->uses_baseinstance)
3166 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 8));
3167 if (vs_prog_data->uses_drawid)
3168 emit_draw_index(cmd_buffer, i);
3169
3170 load_indirect_parameters(cmd_buffer, draw, false);
3171
3172 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3173 prim.IndirectParameterEnable = true;
3174 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3175 prim.VertexAccessType = SEQUENTIAL;
3176 prim.PrimitiveTopologyType = pipeline->topology;
3177 }
3178
3179 offset += stride;
3180 }
3181 }
3182
3183 void genX(CmdDrawIndexedIndirect)(
3184 VkCommandBuffer commandBuffer,
3185 VkBuffer _buffer,
3186 VkDeviceSize offset,
3187 uint32_t drawCount,
3188 uint32_t stride)
3189 {
3190 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3191 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3192 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
3193 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3194
3195 if (anv_batch_has_error(&cmd_buffer->batch))
3196 return;
3197
3198 genX(cmd_buffer_flush_state)(cmd_buffer);
3199
3200 if (cmd_buffer->state.conditional_render_enabled)
3201 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3202
3203 for (uint32_t i = 0; i < drawCount; i++) {
3204 struct anv_address draw = anv_address_add(buffer->address, offset);
3205
3206 /* TODO: We need to stomp base vertex to 0 somehow */
3207 if (vs_prog_data->uses_firstvertex ||
3208 vs_prog_data->uses_baseinstance)
3209 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 12));
3210 if (vs_prog_data->uses_drawid)
3211 emit_draw_index(cmd_buffer, i);
3212
3213 load_indirect_parameters(cmd_buffer, draw, true);
3214
3215 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3216 prim.IndirectParameterEnable = true;
3217 prim.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3218 prim.VertexAccessType = RANDOM;
3219 prim.PrimitiveTopologyType = pipeline->topology;
3220 }
3221
3222 offset += stride;
3223 }
3224 }
3225
3226 #define TMP_DRAW_COUNT_REG MI_ALU_REG14
3227
3228 static void
3229 prepare_for_draw_count_predicate(struct anv_cmd_buffer *cmd_buffer,
3230 struct anv_address count_address,
3231 const bool conditional_render_enabled)
3232 {
3233 if (conditional_render_enabled) {
3234 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3235 emit_lrm(&cmd_buffer->batch, CS_GPR(TMP_DRAW_COUNT_REG), count_address);
3236 emit_lri(&cmd_buffer->batch, CS_GPR(TMP_DRAW_COUNT_REG) + 4, 0);
3237 #endif
3238 } else {
3239 /* Upload the current draw count from the draw parameters buffer to
3240 * MI_PREDICATE_SRC0.
3241 */
3242 emit_lrm(&cmd_buffer->batch, MI_PREDICATE_SRC0, count_address);
3243 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC0 + 4, 0);
3244
3245 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1 + 4, 0);
3246 }
3247 }
3248
3249 static void
3250 emit_draw_count_predicate(struct anv_cmd_buffer *cmd_buffer,
3251 uint32_t draw_index)
3252 {
3253 /* Upload the index of the current primitive to MI_PREDICATE_SRC1. */
3254 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1, draw_index);
3255
3256 if (draw_index == 0) {
3257 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
3258 mip.LoadOperation = LOAD_LOADINV;
3259 mip.CombineOperation = COMBINE_SET;
3260 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3261 }
3262 } else {
3263 /* While draw_index < draw_count the predicate's result will be
3264 * (draw_index == draw_count) ^ TRUE = TRUE
3265 * When draw_index == draw_count the result is
3266 * (TRUE) ^ TRUE = FALSE
3267 * After this all results will be:
3268 * (FALSE) ^ FALSE = FALSE
3269 */
3270 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
3271 mip.LoadOperation = LOAD_LOAD;
3272 mip.CombineOperation = COMBINE_XOR;
3273 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3274 }
3275 }
3276 }
3277
3278 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3279 static void
3280 emit_draw_count_predicate_with_conditional_render(
3281 struct anv_cmd_buffer *cmd_buffer,
3282 uint32_t draw_index)
3283 {
3284 const int draw_index_reg = MI_ALU_REG0;
3285 const int tmp_result_reg = MI_ALU_REG1;
3286
3287 emit_lri(&cmd_buffer->batch, CS_GPR(draw_index_reg), draw_index);
3288 emit_lri(&cmd_buffer->batch, CS_GPR(draw_index_reg) + 4, 0);
3289
3290 uint32_t *dw;
3291 /* Compute (draw_index < draw_count).
3292 * We do this by subtracting and storing the carry bit.
3293 */
3294 dw = anv_batch_emitn(&cmd_buffer->batch, 9, GENX(MI_MATH));
3295 dw[1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, draw_index_reg);
3296 dw[2] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, TMP_DRAW_COUNT_REG);
3297 dw[3] = mi_alu(MI_ALU_SUB, 0, 0);
3298 dw[4] = mi_alu(MI_ALU_STORE, tmp_result_reg, MI_ALU_CF);
3299 /* & condition */
3300 dw[5] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, tmp_result_reg);
3301 dw[6] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, ANV_PREDICATE_RESULT_REG);
3302 dw[7] = mi_alu(MI_ALU_AND, 0, 0);
3303 dw[8] = mi_alu(MI_ALU_STORE, tmp_result_reg, MI_ALU_ACCU);
3304
3305 #if GEN_GEN >= 8
3306 emit_lrr(&cmd_buffer->batch, MI_PREDICATE_RESULT, CS_GPR(tmp_result_reg));
3307 #else
3308 /* MI_PREDICATE_RESULT is not whitelisted in i915 command parser
3309 * so we emit MI_PREDICATE to set it.
3310 */
3311
3312 emit_lrr(&cmd_buffer->batch, MI_PREDICATE_SRC0, CS_GPR(tmp_result_reg));
3313 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC0 + 4, 0);
3314 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1, 0);
3315 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1 + 4, 0);
3316
3317 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
3318 mip.LoadOperation = LOAD_LOADINV;
3319 mip.CombineOperation = COMBINE_SET;
3320 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3321 }
3322 #endif
3323 }
3324 #endif
3325
3326 void genX(CmdDrawIndirectCountKHR)(
3327 VkCommandBuffer commandBuffer,
3328 VkBuffer _buffer,
3329 VkDeviceSize offset,
3330 VkBuffer _countBuffer,
3331 VkDeviceSize countBufferOffset,
3332 uint32_t maxDrawCount,
3333 uint32_t stride)
3334 {
3335 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3336 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3337 ANV_FROM_HANDLE(anv_buffer, count_buffer, _countBuffer);
3338 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
3339 struct anv_pipeline *pipeline = cmd_state->gfx.base.pipeline;
3340 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3341
3342 if (anv_batch_has_error(&cmd_buffer->batch))
3343 return;
3344
3345 genX(cmd_buffer_flush_state)(cmd_buffer);
3346
3347 struct anv_address count_address =
3348 anv_address_add(count_buffer->address, countBufferOffset);
3349
3350 prepare_for_draw_count_predicate(cmd_buffer, count_address,
3351 cmd_state->conditional_render_enabled);
3352
3353 for (uint32_t i = 0; i < maxDrawCount; i++) {
3354 struct anv_address draw = anv_address_add(buffer->address, offset);
3355
3356 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3357 if (cmd_state->conditional_render_enabled) {
3358 emit_draw_count_predicate_with_conditional_render(cmd_buffer, i);
3359 } else {
3360 emit_draw_count_predicate(cmd_buffer, i);
3361 }
3362 #else
3363 emit_draw_count_predicate(cmd_buffer, i);
3364 #endif
3365
3366 if (vs_prog_data->uses_firstvertex ||
3367 vs_prog_data->uses_baseinstance)
3368 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 8));
3369 if (vs_prog_data->uses_drawid)
3370 emit_draw_index(cmd_buffer, i);
3371
3372 load_indirect_parameters(cmd_buffer, draw, false);
3373
3374 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3375 prim.IndirectParameterEnable = true;
3376 prim.PredicateEnable = true;
3377 prim.VertexAccessType = SEQUENTIAL;
3378 prim.PrimitiveTopologyType = pipeline->topology;
3379 }
3380
3381 offset += stride;
3382 }
3383 }
3384
3385 void genX(CmdDrawIndexedIndirectCountKHR)(
3386 VkCommandBuffer commandBuffer,
3387 VkBuffer _buffer,
3388 VkDeviceSize offset,
3389 VkBuffer _countBuffer,
3390 VkDeviceSize countBufferOffset,
3391 uint32_t maxDrawCount,
3392 uint32_t stride)
3393 {
3394 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3395 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3396 ANV_FROM_HANDLE(anv_buffer, count_buffer, _countBuffer);
3397 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
3398 struct anv_pipeline *pipeline = cmd_state->gfx.base.pipeline;
3399 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
3400
3401 if (anv_batch_has_error(&cmd_buffer->batch))
3402 return;
3403
3404 genX(cmd_buffer_flush_state)(cmd_buffer);
3405
3406 struct anv_address count_address =
3407 anv_address_add(count_buffer->address, countBufferOffset);
3408
3409 prepare_for_draw_count_predicate(cmd_buffer, count_address,
3410 cmd_state->conditional_render_enabled);
3411
3412 for (uint32_t i = 0; i < maxDrawCount; i++) {
3413 struct anv_address draw = anv_address_add(buffer->address, offset);
3414
3415 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3416 if (cmd_state->conditional_render_enabled) {
3417 emit_draw_count_predicate_with_conditional_render(cmd_buffer, i);
3418 } else {
3419 emit_draw_count_predicate(cmd_buffer, i);
3420 }
3421 #else
3422 emit_draw_count_predicate(cmd_buffer, i);
3423 #endif
3424
3425 /* TODO: We need to stomp base vertex to 0 somehow */
3426 if (vs_prog_data->uses_firstvertex ||
3427 vs_prog_data->uses_baseinstance)
3428 emit_base_vertex_instance_bo(cmd_buffer, anv_address_add(draw, 12));
3429 if (vs_prog_data->uses_drawid)
3430 emit_draw_index(cmd_buffer, i);
3431
3432 load_indirect_parameters(cmd_buffer, draw, true);
3433
3434 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
3435 prim.IndirectParameterEnable = true;
3436 prim.PredicateEnable = true;
3437 prim.VertexAccessType = RANDOM;
3438 prim.PrimitiveTopologyType = pipeline->topology;
3439 }
3440
3441 offset += stride;
3442 }
3443 }
3444
3445 void genX(CmdBeginTransformFeedbackEXT)(
3446 VkCommandBuffer commandBuffer,
3447 uint32_t firstCounterBuffer,
3448 uint32_t counterBufferCount,
3449 const VkBuffer* pCounterBuffers,
3450 const VkDeviceSize* pCounterBufferOffsets)
3451 {
3452 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3453
3454 assert(firstCounterBuffer < MAX_XFB_BUFFERS);
3455 assert(counterBufferCount <= MAX_XFB_BUFFERS);
3456 assert(firstCounterBuffer + counterBufferCount <= MAX_XFB_BUFFERS);
3457
3458 /* From the SKL PRM Vol. 2c, SO_WRITE_OFFSET:
3459 *
3460 * "Ssoftware must ensure that no HW stream output operations can be in
3461 * process or otherwise pending at the point that the MI_LOAD/STORE
3462 * commands are processed. This will likely require a pipeline flush."
3463 */
3464 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
3465 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3466
3467 for (uint32_t idx = 0; idx < MAX_XFB_BUFFERS; idx++) {
3468 /* If we have a counter buffer, this is a resume so we need to load the
3469 * value into the streamout offset register. Otherwise, this is a begin
3470 * and we need to reset it to zero.
3471 */
3472 if (pCounterBuffers &&
3473 idx >= firstCounterBuffer &&
3474 idx - firstCounterBuffer < counterBufferCount &&
3475 pCounterBuffers[idx - firstCounterBuffer] != VK_NULL_HANDLE) {
3476 uint32_t cb_idx = idx - firstCounterBuffer;
3477 ANV_FROM_HANDLE(anv_buffer, counter_buffer, pCounterBuffers[cb_idx]);
3478 uint64_t offset = pCounterBufferOffsets ?
3479 pCounterBufferOffsets[cb_idx] : 0;
3480
3481 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
3482 lrm.RegisterAddress = GENX(SO_WRITE_OFFSET0_num) + idx * 4;
3483 lrm.MemoryAddress = anv_address_add(counter_buffer->address,
3484 offset);
3485 }
3486 } else {
3487 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
3488 lri.RegisterOffset = GENX(SO_WRITE_OFFSET0_num) + idx * 4;
3489 lri.DataDWord = 0;
3490 }
3491 }
3492 }
3493
3494 cmd_buffer->state.xfb_enabled = true;
3495 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_XFB_ENABLE;
3496 }
3497
3498 void genX(CmdEndTransformFeedbackEXT)(
3499 VkCommandBuffer commandBuffer,
3500 uint32_t firstCounterBuffer,
3501 uint32_t counterBufferCount,
3502 const VkBuffer* pCounterBuffers,
3503 const VkDeviceSize* pCounterBufferOffsets)
3504 {
3505 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3506
3507 assert(firstCounterBuffer < MAX_XFB_BUFFERS);
3508 assert(counterBufferCount <= MAX_XFB_BUFFERS);
3509 assert(firstCounterBuffer + counterBufferCount <= MAX_XFB_BUFFERS);
3510
3511 /* From the SKL PRM Vol. 2c, SO_WRITE_OFFSET:
3512 *
3513 * "Ssoftware must ensure that no HW stream output operations can be in
3514 * process or otherwise pending at the point that the MI_LOAD/STORE
3515 * commands are processed. This will likely require a pipeline flush."
3516 */
3517 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
3518 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3519
3520 for (uint32_t cb_idx = 0; cb_idx < counterBufferCount; cb_idx++) {
3521 unsigned idx = firstCounterBuffer + cb_idx;
3522
3523 /* If we have a counter buffer, this is a resume so we need to load the
3524 * value into the streamout offset register. Otherwise, this is a begin
3525 * and we need to reset it to zero.
3526 */
3527 if (pCounterBuffers &&
3528 cb_idx < counterBufferCount &&
3529 pCounterBuffers[cb_idx] != VK_NULL_HANDLE) {
3530 ANV_FROM_HANDLE(anv_buffer, counter_buffer, pCounterBuffers[cb_idx]);
3531 uint64_t offset = pCounterBufferOffsets ?
3532 pCounterBufferOffsets[cb_idx] : 0;
3533
3534 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
3535 srm.MemoryAddress = anv_address_add(counter_buffer->address,
3536 offset);
3537 srm.RegisterAddress = GENX(SO_WRITE_OFFSET0_num) + idx * 4;
3538 }
3539 }
3540 }
3541
3542 cmd_buffer->state.xfb_enabled = false;
3543 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_XFB_ENABLE;
3544 }
3545
3546 static VkResult
3547 flush_compute_descriptor_set(struct anv_cmd_buffer *cmd_buffer)
3548 {
3549 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3550 struct anv_state surfaces = { 0, }, samplers = { 0, };
3551 VkResult result;
3552
3553 result = emit_binding_table(cmd_buffer, MESA_SHADER_COMPUTE, &surfaces);
3554 if (result != VK_SUCCESS) {
3555 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
3556
3557 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
3558 if (result != VK_SUCCESS)
3559 return result;
3560
3561 /* Re-emit state base addresses so we get the new surface state base
3562 * address before we start emitting binding tables etc.
3563 */
3564 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
3565
3566 result = emit_binding_table(cmd_buffer, MESA_SHADER_COMPUTE, &surfaces);
3567 if (result != VK_SUCCESS) {
3568 anv_batch_set_error(&cmd_buffer->batch, result);
3569 return result;
3570 }
3571 }
3572
3573 result = emit_samplers(cmd_buffer, MESA_SHADER_COMPUTE, &samplers);
3574 if (result != VK_SUCCESS) {
3575 anv_batch_set_error(&cmd_buffer->batch, result);
3576 return result;
3577 }
3578
3579 uint32_t iface_desc_data_dw[GENX(INTERFACE_DESCRIPTOR_DATA_length)];
3580 struct GENX(INTERFACE_DESCRIPTOR_DATA) desc = {
3581 .BindingTablePointer = surfaces.offset,
3582 .SamplerStatePointer = samplers.offset,
3583 };
3584 GENX(INTERFACE_DESCRIPTOR_DATA_pack)(NULL, iface_desc_data_dw, &desc);
3585
3586 struct anv_state state =
3587 anv_cmd_buffer_merge_dynamic(cmd_buffer, iface_desc_data_dw,
3588 pipeline->interface_descriptor_data,
3589 GENX(INTERFACE_DESCRIPTOR_DATA_length),
3590 64);
3591
3592 uint32_t size = GENX(INTERFACE_DESCRIPTOR_DATA_length) * sizeof(uint32_t);
3593 anv_batch_emit(&cmd_buffer->batch,
3594 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD), mid) {
3595 mid.InterfaceDescriptorTotalLength = size;
3596 mid.InterfaceDescriptorDataStartAddress = state.offset;
3597 }
3598
3599 return VK_SUCCESS;
3600 }
3601
3602 void
3603 genX(cmd_buffer_flush_compute_state)(struct anv_cmd_buffer *cmd_buffer)
3604 {
3605 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3606 MAYBE_UNUSED VkResult result;
3607
3608 assert(pipeline->active_stages == VK_SHADER_STAGE_COMPUTE_BIT);
3609
3610 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
3611
3612 genX(flush_pipeline_select_gpgpu)(cmd_buffer);
3613
3614 if (cmd_buffer->state.compute.pipeline_dirty) {
3615 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
3616 *
3617 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
3618 * the only bits that are changed are scoreboard related: Scoreboard
3619 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
3620 * these scoreboard related states, a MEDIA_STATE_FLUSH is
3621 * sufficient."
3622 */
3623 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
3624 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3625
3626 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
3627 }
3628
3629 if ((cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_COMPUTE_BIT) ||
3630 cmd_buffer->state.compute.pipeline_dirty) {
3631 /* FIXME: figure out descriptors for gen7 */
3632 result = flush_compute_descriptor_set(cmd_buffer);
3633 if (result != VK_SUCCESS)
3634 return;
3635
3636 cmd_buffer->state.descriptors_dirty &= ~VK_SHADER_STAGE_COMPUTE_BIT;
3637 }
3638
3639 if (cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_COMPUTE_BIT) {
3640 struct anv_state push_state =
3641 anv_cmd_buffer_cs_push_constants(cmd_buffer);
3642
3643 if (push_state.alloc_size) {
3644 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_CURBE_LOAD), curbe) {
3645 curbe.CURBETotalDataLength = push_state.alloc_size;
3646 curbe.CURBEDataStartAddress = push_state.offset;
3647 }
3648 }
3649
3650 cmd_buffer->state.push_constants_dirty &= ~VK_SHADER_STAGE_COMPUTE_BIT;
3651 }
3652
3653 cmd_buffer->state.compute.pipeline_dirty = false;
3654
3655 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3656 }
3657
3658 #if GEN_GEN == 7
3659
3660 static VkResult
3661 verify_cmd_parser(const struct anv_device *device,
3662 int required_version,
3663 const char *function)
3664 {
3665 if (device->instance->physicalDevice.cmd_parser_version < required_version) {
3666 return vk_errorf(device->instance, device->instance,
3667 VK_ERROR_FEATURE_NOT_PRESENT,
3668 "cmd parser version %d is required for %s",
3669 required_version, function);
3670 } else {
3671 return VK_SUCCESS;
3672 }
3673 }
3674
3675 #endif
3676
3677 static void
3678 anv_cmd_buffer_push_base_group_id(struct anv_cmd_buffer *cmd_buffer,
3679 uint32_t baseGroupX,
3680 uint32_t baseGroupY,
3681 uint32_t baseGroupZ)
3682 {
3683 if (anv_batch_has_error(&cmd_buffer->batch))
3684 return;
3685
3686 VkResult result =
3687 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, MESA_SHADER_COMPUTE,
3688 base_work_group_id);
3689 if (result != VK_SUCCESS) {
3690 cmd_buffer->batch.status = result;
3691 return;
3692 }
3693
3694 struct anv_push_constants *push =
3695 cmd_buffer->state.push_constants[MESA_SHADER_COMPUTE];
3696 if (push->base_work_group_id[0] != baseGroupX ||
3697 push->base_work_group_id[1] != baseGroupY ||
3698 push->base_work_group_id[2] != baseGroupZ) {
3699 push->base_work_group_id[0] = baseGroupX;
3700 push->base_work_group_id[1] = baseGroupY;
3701 push->base_work_group_id[2] = baseGroupZ;
3702
3703 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_COMPUTE_BIT;
3704 }
3705 }
3706
3707 void genX(CmdDispatch)(
3708 VkCommandBuffer commandBuffer,
3709 uint32_t x,
3710 uint32_t y,
3711 uint32_t z)
3712 {
3713 genX(CmdDispatchBase)(commandBuffer, 0, 0, 0, x, y, z);
3714 }
3715
3716 void genX(CmdDispatchBase)(
3717 VkCommandBuffer commandBuffer,
3718 uint32_t baseGroupX,
3719 uint32_t baseGroupY,
3720 uint32_t baseGroupZ,
3721 uint32_t groupCountX,
3722 uint32_t groupCountY,
3723 uint32_t groupCountZ)
3724 {
3725 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3726 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3727 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
3728
3729 anv_cmd_buffer_push_base_group_id(cmd_buffer, baseGroupX,
3730 baseGroupY, baseGroupZ);
3731
3732 if (anv_batch_has_error(&cmd_buffer->batch))
3733 return;
3734
3735 if (prog_data->uses_num_work_groups) {
3736 struct anv_state state =
3737 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 12, 4);
3738 uint32_t *sizes = state.map;
3739 sizes[0] = groupCountX;
3740 sizes[1] = groupCountY;
3741 sizes[2] = groupCountZ;
3742 cmd_buffer->state.compute.num_workgroups = (struct anv_address) {
3743 .bo = cmd_buffer->device->dynamic_state_pool.block_pool.bo,
3744 .offset = state.offset,
3745 };
3746 }
3747
3748 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
3749
3750 if (cmd_buffer->state.conditional_render_enabled)
3751 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3752
3753 anv_batch_emit(&cmd_buffer->batch, GENX(GPGPU_WALKER), ggw) {
3754 ggw.PredicateEnable = cmd_buffer->state.conditional_render_enabled;
3755 ggw.SIMDSize = prog_data->simd_size / 16;
3756 ggw.ThreadDepthCounterMaximum = 0;
3757 ggw.ThreadHeightCounterMaximum = 0;
3758 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
3759 ggw.ThreadGroupIDXDimension = groupCountX;
3760 ggw.ThreadGroupIDYDimension = groupCountY;
3761 ggw.ThreadGroupIDZDimension = groupCountZ;
3762 ggw.RightExecutionMask = pipeline->cs_right_mask;
3763 ggw.BottomExecutionMask = 0xffffffff;
3764 }
3765
3766 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_STATE_FLUSH), msf);
3767 }
3768
3769 #define GPGPU_DISPATCHDIMX 0x2500
3770 #define GPGPU_DISPATCHDIMY 0x2504
3771 #define GPGPU_DISPATCHDIMZ 0x2508
3772
3773 void genX(CmdDispatchIndirect)(
3774 VkCommandBuffer commandBuffer,
3775 VkBuffer _buffer,
3776 VkDeviceSize offset)
3777 {
3778 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3779 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3780 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3781 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
3782 struct anv_address addr = anv_address_add(buffer->address, offset);
3783 struct anv_batch *batch = &cmd_buffer->batch;
3784
3785 anv_cmd_buffer_push_base_group_id(cmd_buffer, 0, 0, 0);
3786
3787 #if GEN_GEN == 7
3788 /* Linux 4.4 added command parser version 5 which allows the GPGPU
3789 * indirect dispatch registers to be written.
3790 */
3791 if (verify_cmd_parser(cmd_buffer->device, 5,
3792 "vkCmdDispatchIndirect") != VK_SUCCESS)
3793 return;
3794 #endif
3795
3796 if (prog_data->uses_num_work_groups)
3797 cmd_buffer->state.compute.num_workgroups = addr;
3798
3799 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
3800
3801 emit_lrm(batch, GPGPU_DISPATCHDIMX, anv_address_add(addr, 0));
3802 emit_lrm(batch, GPGPU_DISPATCHDIMY, anv_address_add(addr, 4));
3803 emit_lrm(batch, GPGPU_DISPATCHDIMZ, anv_address_add(addr, 8));
3804
3805 #if GEN_GEN <= 7
3806 /* Clear upper 32-bits of SRC0 and all 64-bits of SRC1 */
3807 emit_lri(batch, MI_PREDICATE_SRC0 + 4, 0);
3808 emit_lri(batch, MI_PREDICATE_SRC1 + 0, 0);
3809 emit_lri(batch, MI_PREDICATE_SRC1 + 4, 0);
3810
3811 /* Load compute_dispatch_indirect_x_size into SRC0 */
3812 emit_lrm(batch, MI_PREDICATE_SRC0, anv_address_add(addr, 0));
3813
3814 /* predicate = (compute_dispatch_indirect_x_size == 0); */
3815 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3816 mip.LoadOperation = LOAD_LOAD;
3817 mip.CombineOperation = COMBINE_SET;
3818 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3819 }
3820
3821 /* Load compute_dispatch_indirect_y_size into SRC0 */
3822 emit_lrm(batch, MI_PREDICATE_SRC0, anv_address_add(addr, 4));
3823
3824 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
3825 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3826 mip.LoadOperation = LOAD_LOAD;
3827 mip.CombineOperation = COMBINE_OR;
3828 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3829 }
3830
3831 /* Load compute_dispatch_indirect_z_size into SRC0 */
3832 emit_lrm(batch, MI_PREDICATE_SRC0, anv_address_add(addr, 8));
3833
3834 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
3835 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3836 mip.LoadOperation = LOAD_LOAD;
3837 mip.CombineOperation = COMBINE_OR;
3838 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3839 }
3840
3841 /* predicate = !predicate; */
3842 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3843 mip.LoadOperation = LOAD_LOADINV;
3844 mip.CombineOperation = COMBINE_OR;
3845 mip.CompareOperation = COMPARE_FALSE;
3846 }
3847
3848 #if GEN_IS_HASWELL
3849 if (cmd_buffer->state.conditional_render_enabled) {
3850 emit_lrr(batch, MI_PREDICATE_SRC0, CS_GPR(ANV_PREDICATE_RESULT_REG));
3851 /* predicate &= !(conditional_rendering_predicate == 0); */
3852 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3853 mip.LoadOperation = LOAD_LOADINV;
3854 mip.CombineOperation = COMBINE_AND;
3855 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3856 }
3857 }
3858 #endif
3859
3860 #else /* GEN_GEN > 7 */
3861 if (cmd_buffer->state.conditional_render_enabled)
3862 genX(cmd_emit_conditional_render_predicate)(cmd_buffer);
3863 #endif
3864
3865 anv_batch_emit(batch, GENX(GPGPU_WALKER), ggw) {
3866 ggw.IndirectParameterEnable = true;
3867 ggw.PredicateEnable = GEN_GEN <= 7 ||
3868 cmd_buffer->state.conditional_render_enabled;
3869 ggw.SIMDSize = prog_data->simd_size / 16;
3870 ggw.ThreadDepthCounterMaximum = 0;
3871 ggw.ThreadHeightCounterMaximum = 0;
3872 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
3873 ggw.RightExecutionMask = pipeline->cs_right_mask;
3874 ggw.BottomExecutionMask = 0xffffffff;
3875 }
3876
3877 anv_batch_emit(batch, GENX(MEDIA_STATE_FLUSH), msf);
3878 }
3879
3880 static void
3881 genX(flush_pipeline_select)(struct anv_cmd_buffer *cmd_buffer,
3882 uint32_t pipeline)
3883 {
3884 UNUSED const struct gen_device_info *devinfo = &cmd_buffer->device->info;
3885
3886 if (cmd_buffer->state.current_pipeline == pipeline)
3887 return;
3888
3889 #if GEN_GEN >= 8 && GEN_GEN < 10
3890 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
3891 *
3892 * Software must clear the COLOR_CALC_STATE Valid field in
3893 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
3894 * with Pipeline Select set to GPGPU.
3895 *
3896 * The internal hardware docs recommend the same workaround for Gen9
3897 * hardware too.
3898 */
3899 if (pipeline == GPGPU)
3900 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CC_STATE_POINTERS), t);
3901 #endif
3902
3903 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
3904 * PIPELINE_SELECT [DevBWR+]":
3905 *
3906 * Project: DEVSNB+
3907 *
3908 * Software must ensure all the write caches are flushed through a
3909 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
3910 * command to invalidate read only caches prior to programming
3911 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
3912 */
3913 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
3914 pc.RenderTargetCacheFlushEnable = true;
3915 pc.DepthCacheFlushEnable = true;
3916 pc.DCFlushEnable = true;
3917 pc.PostSyncOperation = NoWrite;
3918 pc.CommandStreamerStallEnable = true;
3919 }
3920
3921 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
3922 pc.TextureCacheInvalidationEnable = true;
3923 pc.ConstantCacheInvalidationEnable = true;
3924 pc.StateCacheInvalidationEnable = true;
3925 pc.InstructionCacheInvalidateEnable = true;
3926 pc.PostSyncOperation = NoWrite;
3927 }
3928
3929 anv_batch_emit(&cmd_buffer->batch, GENX(PIPELINE_SELECT), ps) {
3930 #if GEN_GEN >= 9
3931 ps.MaskBits = 3;
3932 #endif
3933 ps.PipelineSelection = pipeline;
3934 }
3935
3936 #if GEN_GEN == 9
3937 if (devinfo->is_geminilake) {
3938 /* Project: DevGLK
3939 *
3940 * "This chicken bit works around a hardware issue with barrier logic
3941 * encountered when switching between GPGPU and 3D pipelines. To
3942 * workaround the issue, this mode bit should be set after a pipeline
3943 * is selected."
3944 */
3945 uint32_t scec;
3946 anv_pack_struct(&scec, GENX(SLICE_COMMON_ECO_CHICKEN1),
3947 .GLKBarrierMode =
3948 pipeline == GPGPU ? GLK_BARRIER_MODE_GPGPU
3949 : GLK_BARRIER_MODE_3D_HULL,
3950 .GLKBarrierModeMask = 1);
3951 emit_lri(&cmd_buffer->batch, GENX(SLICE_COMMON_ECO_CHICKEN1_num), scec);
3952 }
3953 #endif
3954
3955 cmd_buffer->state.current_pipeline = pipeline;
3956 }
3957
3958 void
3959 genX(flush_pipeline_select_3d)(struct anv_cmd_buffer *cmd_buffer)
3960 {
3961 genX(flush_pipeline_select)(cmd_buffer, _3D);
3962 }
3963
3964 void
3965 genX(flush_pipeline_select_gpgpu)(struct anv_cmd_buffer *cmd_buffer)
3966 {
3967 genX(flush_pipeline_select)(cmd_buffer, GPGPU);
3968 }
3969
3970 void
3971 genX(cmd_buffer_emit_gen7_depth_flush)(struct anv_cmd_buffer *cmd_buffer)
3972 {
3973 if (GEN_GEN >= 8)
3974 return;
3975
3976 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
3977 *
3978 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
3979 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
3980 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
3981 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
3982 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
3983 * Depth Flush Bit set, followed by another pipelined depth stall
3984 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
3985 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
3986 * via a preceding MI_FLUSH)."
3987 */
3988 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
3989 pipe.DepthStallEnable = true;
3990 }
3991 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
3992 pipe.DepthCacheFlushEnable = true;
3993 }
3994 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
3995 pipe.DepthStallEnable = true;
3996 }
3997 }
3998
3999 static void
4000 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer *cmd_buffer)
4001 {
4002 struct anv_device *device = cmd_buffer->device;
4003 const struct anv_image_view *iview =
4004 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
4005 const struct anv_image *image = iview ? iview->image : NULL;
4006
4007 /* FIXME: Width and Height are wrong */
4008
4009 genX(cmd_buffer_emit_gen7_depth_flush)(cmd_buffer);
4010
4011 uint32_t *dw = anv_batch_emit_dwords(&cmd_buffer->batch,
4012 device->isl_dev.ds.size / 4);
4013 if (dw == NULL)
4014 return;
4015
4016 struct isl_depth_stencil_hiz_emit_info info = { };
4017
4018 if (iview)
4019 info.view = &iview->planes[0].isl;
4020
4021 if (image && (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
4022 uint32_t depth_plane =
4023 anv_image_aspect_to_plane(image->aspects, VK_IMAGE_ASPECT_DEPTH_BIT);
4024 const struct anv_surface *surface = &image->planes[depth_plane].surface;
4025
4026 info.depth_surf = &surface->isl;
4027
4028 info.depth_address =
4029 anv_batch_emit_reloc(&cmd_buffer->batch,
4030 dw + device->isl_dev.ds.depth_offset / 4,
4031 image->planes[depth_plane].address.bo,
4032 image->planes[depth_plane].address.offset +
4033 surface->offset);
4034 info.mocs =
4035 anv_mocs_for_bo(device, image->planes[depth_plane].address.bo);
4036
4037 const uint32_t ds =
4038 cmd_buffer->state.subpass->depth_stencil_attachment->attachment;
4039 info.hiz_usage = cmd_buffer->state.attachments[ds].aux_usage;
4040 if (info.hiz_usage == ISL_AUX_USAGE_HIZ) {
4041 info.hiz_surf = &image->planes[depth_plane].aux_surface.isl;
4042
4043 info.hiz_address =
4044 anv_batch_emit_reloc(&cmd_buffer->batch,
4045 dw + device->isl_dev.ds.hiz_offset / 4,
4046 image->planes[depth_plane].address.bo,
4047 image->planes[depth_plane].address.offset +
4048 image->planes[depth_plane].aux_surface.offset);
4049
4050 info.depth_clear_value = ANV_HZ_FC_VAL;
4051 }
4052 }
4053
4054 if (image && (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT)) {
4055 uint32_t stencil_plane =
4056 anv_image_aspect_to_plane(image->aspects, VK_IMAGE_ASPECT_STENCIL_BIT);
4057 const struct anv_surface *surface = &image->planes[stencil_plane].surface;
4058
4059 info.stencil_surf = &surface->isl;
4060
4061 info.stencil_address =
4062 anv_batch_emit_reloc(&cmd_buffer->batch,
4063 dw + device->isl_dev.ds.stencil_offset / 4,
4064 image->planes[stencil_plane].address.bo,
4065 image->planes[stencil_plane].address.offset +
4066 surface->offset);
4067 info.mocs =
4068 anv_mocs_for_bo(device, image->planes[stencil_plane].address.bo);
4069 }
4070
4071 isl_emit_depth_stencil_hiz_s(&device->isl_dev, dw, &info);
4072
4073 cmd_buffer->state.hiz_enabled = info.hiz_usage == ISL_AUX_USAGE_HIZ;
4074 }
4075
4076 /**
4077 * This ANDs the view mask of the current subpass with the pending clear
4078 * views in the attachment to get the mask of views active in the subpass
4079 * that still need to be cleared.
4080 */
4081 static inline uint32_t
4082 get_multiview_subpass_clear_mask(const struct anv_cmd_state *cmd_state,
4083 const struct anv_attachment_state *att_state)
4084 {
4085 return cmd_state->subpass->view_mask & att_state->pending_clear_views;
4086 }
4087
4088 static inline bool
4089 do_first_layer_clear(const struct anv_cmd_state *cmd_state,
4090 const struct anv_attachment_state *att_state)
4091 {
4092 if (!cmd_state->subpass->view_mask)
4093 return true;
4094
4095 uint32_t pending_clear_mask =
4096 get_multiview_subpass_clear_mask(cmd_state, att_state);
4097
4098 return pending_clear_mask & 1;
4099 }
4100
4101 static inline bool
4102 current_subpass_is_last_for_attachment(const struct anv_cmd_state *cmd_state,
4103 uint32_t att_idx)
4104 {
4105 const uint32_t last_subpass_idx =
4106 cmd_state->pass->attachments[att_idx].last_subpass_idx;
4107 const struct anv_subpass *last_subpass =
4108 &cmd_state->pass->subpasses[last_subpass_idx];
4109 return last_subpass == cmd_state->subpass;
4110 }
4111
4112 static void
4113 cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
4114 uint32_t subpass_id)
4115 {
4116 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
4117 struct anv_subpass *subpass = &cmd_state->pass->subpasses[subpass_id];
4118 cmd_state->subpass = subpass;
4119
4120 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
4121
4122 /* Our implementation of VK_KHR_multiview uses instancing to draw the
4123 * different views. If the client asks for instancing, we need to use the
4124 * Instance Data Step Rate to ensure that we repeat the client's
4125 * per-instance data once for each view. Since this bit is in
4126 * VERTEX_BUFFER_STATE on gen7, we need to dirty vertex buffers at the top
4127 * of each subpass.
4128 */
4129 if (GEN_GEN == 7)
4130 cmd_buffer->state.gfx.vb_dirty |= ~0;
4131
4132 /* It is possible to start a render pass with an old pipeline. Because the
4133 * render pass and subpass index are both baked into the pipeline, this is
4134 * highly unlikely. In order to do so, it requires that you have a render
4135 * pass with a single subpass and that you use that render pass twice
4136 * back-to-back and use the same pipeline at the start of the second render
4137 * pass as at the end of the first. In order to avoid unpredictable issues
4138 * with this edge case, we just dirty the pipeline at the start of every
4139 * subpass.
4140 */
4141 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_PIPELINE;
4142
4143 /* Accumulate any subpass flushes that need to happen before the subpass */
4144 cmd_buffer->state.pending_pipe_bits |=
4145 cmd_buffer->state.pass->subpass_flushes[subpass_id];
4146
4147 VkRect2D render_area = cmd_buffer->state.render_area;
4148 struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
4149
4150 bool is_multiview = subpass->view_mask != 0;
4151
4152 for (uint32_t i = 0; i < subpass->attachment_count; ++i) {
4153 const uint32_t a = subpass->attachments[i].attachment;
4154 if (a == VK_ATTACHMENT_UNUSED)
4155 continue;
4156
4157 assert(a < cmd_state->pass->attachment_count);
4158 struct anv_attachment_state *att_state = &cmd_state->attachments[a];
4159
4160 struct anv_image_view *iview = fb->attachments[a];
4161 const struct anv_image *image = iview->image;
4162
4163 /* A resolve is necessary before use as an input attachment if the clear
4164 * color or auxiliary buffer usage isn't supported by the sampler.
4165 */
4166 const bool input_needs_resolve =
4167 (att_state->fast_clear && !att_state->clear_color_is_zero_one) ||
4168 att_state->input_aux_usage != att_state->aux_usage;
4169
4170 VkImageLayout target_layout;
4171 if (iview->aspect_mask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV &&
4172 !input_needs_resolve) {
4173 /* Layout transitions before the final only help to enable sampling
4174 * as an input attachment. If the input attachment supports sampling
4175 * using the auxiliary surface, we can skip such transitions by
4176 * making the target layout one that is CCS-aware.
4177 */
4178 target_layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
4179 } else {
4180 target_layout = subpass->attachments[i].layout;
4181 }
4182
4183 if (image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
4184 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
4185
4186 uint32_t base_layer, layer_count;
4187 if (image->type == VK_IMAGE_TYPE_3D) {
4188 base_layer = 0;
4189 layer_count = anv_minify(iview->image->extent.depth,
4190 iview->planes[0].isl.base_level);
4191 } else {
4192 base_layer = iview->planes[0].isl.base_array_layer;
4193 layer_count = fb->layers;
4194 }
4195
4196 transition_color_buffer(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
4197 iview->planes[0].isl.base_level, 1,
4198 base_layer, layer_count,
4199 att_state->current_layout, target_layout);
4200 } else if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
4201 transition_depth_buffer(cmd_buffer, image,
4202 att_state->current_layout, target_layout);
4203 att_state->aux_usage =
4204 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
4205 VK_IMAGE_ASPECT_DEPTH_BIT, target_layout);
4206 }
4207 att_state->current_layout = target_layout;
4208
4209 if (att_state->pending_clear_aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
4210 assert(att_state->pending_clear_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
4211
4212 /* Multi-planar images are not supported as attachments */
4213 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
4214 assert(image->n_planes == 1);
4215
4216 uint32_t base_clear_layer = iview->planes[0].isl.base_array_layer;
4217 uint32_t clear_layer_count = fb->layers;
4218
4219 if (att_state->fast_clear &&
4220 do_first_layer_clear(cmd_state, att_state)) {
4221 /* We only support fast-clears on the first layer */
4222 assert(iview->planes[0].isl.base_level == 0);
4223 assert(iview->planes[0].isl.base_array_layer == 0);
4224
4225 union isl_color_value clear_color = {};
4226 anv_clear_color_from_att_state(&clear_color, att_state, iview);
4227 if (iview->image->samples == 1) {
4228 anv_image_ccs_op(cmd_buffer, image,
4229 iview->planes[0].isl.format,
4230 VK_IMAGE_ASPECT_COLOR_BIT,
4231 0, 0, 1, ISL_AUX_OP_FAST_CLEAR,
4232 &clear_color,
4233 false);
4234 } else {
4235 anv_image_mcs_op(cmd_buffer, image,
4236 iview->planes[0].isl.format,
4237 VK_IMAGE_ASPECT_COLOR_BIT,
4238 0, 1, ISL_AUX_OP_FAST_CLEAR,
4239 &clear_color,
4240 false);
4241 }
4242 base_clear_layer++;
4243 clear_layer_count--;
4244 if (is_multiview)
4245 att_state->pending_clear_views &= ~1;
4246
4247 if (att_state->clear_color_is_zero) {
4248 /* This image has the auxiliary buffer enabled. We can mark the
4249 * subresource as not needing a resolve because the clear color
4250 * will match what's in every RENDER_SURFACE_STATE object when
4251 * it's being used for sampling.
4252 */
4253 set_image_fast_clear_state(cmd_buffer, iview->image,
4254 VK_IMAGE_ASPECT_COLOR_BIT,
4255 ANV_FAST_CLEAR_DEFAULT_VALUE);
4256 } else {
4257 set_image_fast_clear_state(cmd_buffer, iview->image,
4258 VK_IMAGE_ASPECT_COLOR_BIT,
4259 ANV_FAST_CLEAR_ANY);
4260 }
4261 }
4262
4263 /* From the VkFramebufferCreateInfo spec:
4264 *
4265 * "If the render pass uses multiview, then layers must be one and each
4266 * attachment requires a number of layers that is greater than the
4267 * maximum bit index set in the view mask in the subpasses in which it
4268 * is used."
4269 *
4270 * So if multiview is active we ignore the number of layers in the
4271 * framebuffer and instead we honor the view mask from the subpass.
4272 */
4273 if (is_multiview) {
4274 assert(image->n_planes == 1);
4275 uint32_t pending_clear_mask =
4276 get_multiview_subpass_clear_mask(cmd_state, att_state);
4277
4278 uint32_t layer_idx;
4279 for_each_bit(layer_idx, pending_clear_mask) {
4280 uint32_t layer =
4281 iview->planes[0].isl.base_array_layer + layer_idx;
4282
4283 anv_image_clear_color(cmd_buffer, image,
4284 VK_IMAGE_ASPECT_COLOR_BIT,
4285 att_state->aux_usage,
4286 iview->planes[0].isl.format,
4287 iview->planes[0].isl.swizzle,
4288 iview->planes[0].isl.base_level,
4289 layer, 1,
4290 render_area,
4291 vk_to_isl_color(att_state->clear_value.color));
4292 }
4293
4294 att_state->pending_clear_views &= ~pending_clear_mask;
4295 } else if (clear_layer_count > 0) {
4296 assert(image->n_planes == 1);
4297 anv_image_clear_color(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
4298 att_state->aux_usage,
4299 iview->planes[0].isl.format,
4300 iview->planes[0].isl.swizzle,
4301 iview->planes[0].isl.base_level,
4302 base_clear_layer, clear_layer_count,
4303 render_area,
4304 vk_to_isl_color(att_state->clear_value.color));
4305 }
4306 } else if (att_state->pending_clear_aspects & (VK_IMAGE_ASPECT_DEPTH_BIT |
4307 VK_IMAGE_ASPECT_STENCIL_BIT)) {
4308 if (att_state->fast_clear && !is_multiview) {
4309 /* We currently only support HiZ for single-layer images */
4310 if (att_state->pending_clear_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
4311 assert(iview->image->planes[0].aux_usage == ISL_AUX_USAGE_HIZ);
4312 assert(iview->planes[0].isl.base_level == 0);
4313 assert(iview->planes[0].isl.base_array_layer == 0);
4314 assert(fb->layers == 1);
4315 }
4316
4317 anv_image_hiz_clear(cmd_buffer, image,
4318 att_state->pending_clear_aspects,
4319 iview->planes[0].isl.base_level,
4320 iview->planes[0].isl.base_array_layer,
4321 fb->layers, render_area,
4322 att_state->clear_value.depthStencil.stencil);
4323 } else if (is_multiview) {
4324 uint32_t pending_clear_mask =
4325 get_multiview_subpass_clear_mask(cmd_state, att_state);
4326
4327 uint32_t layer_idx;
4328 for_each_bit(layer_idx, pending_clear_mask) {
4329 uint32_t layer =
4330 iview->planes[0].isl.base_array_layer + layer_idx;
4331
4332 anv_image_clear_depth_stencil(cmd_buffer, image,
4333 att_state->pending_clear_aspects,
4334 att_state->aux_usage,
4335 iview->planes[0].isl.base_level,
4336 layer, 1,
4337 render_area,
4338 att_state->clear_value.depthStencil.depth,
4339 att_state->clear_value.depthStencil.stencil);
4340 }
4341
4342 att_state->pending_clear_views &= ~pending_clear_mask;
4343 } else {
4344 anv_image_clear_depth_stencil(cmd_buffer, image,
4345 att_state->pending_clear_aspects,
4346 att_state->aux_usage,
4347 iview->planes[0].isl.base_level,
4348 iview->planes[0].isl.base_array_layer,
4349 fb->layers, render_area,
4350 att_state->clear_value.depthStencil.depth,
4351 att_state->clear_value.depthStencil.stencil);
4352 }
4353 } else {
4354 assert(att_state->pending_clear_aspects == 0);
4355 }
4356
4357 if (GEN_GEN < 10 &&
4358 (att_state->pending_load_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) &&
4359 image->planes[0].aux_surface.isl.size_B > 0 &&
4360 iview->planes[0].isl.base_level == 0 &&
4361 iview->planes[0].isl.base_array_layer == 0) {
4362 if (att_state->aux_usage != ISL_AUX_USAGE_NONE) {
4363 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->color.state,
4364 image, VK_IMAGE_ASPECT_COLOR_BIT,
4365 false /* copy to ss */);
4366 }
4367
4368 if (need_input_attachment_state(&cmd_state->pass->attachments[a]) &&
4369 att_state->input_aux_usage != ISL_AUX_USAGE_NONE) {
4370 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->input.state,
4371 image, VK_IMAGE_ASPECT_COLOR_BIT,
4372 false /* copy to ss */);
4373 }
4374 }
4375
4376 if (subpass->attachments[i].usage ==
4377 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
4378 /* We assume that if we're starting a subpass, we're going to do some
4379 * rendering so we may end up with compressed data.
4380 */
4381 genX(cmd_buffer_mark_image_written)(cmd_buffer, iview->image,
4382 VK_IMAGE_ASPECT_COLOR_BIT,
4383 att_state->aux_usage,
4384 iview->planes[0].isl.base_level,
4385 iview->planes[0].isl.base_array_layer,
4386 fb->layers);
4387 } else if (subpass->attachments[i].usage ==
4388 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
4389 /* We may be writing depth or stencil so we need to mark the surface.
4390 * Unfortunately, there's no way to know at this point whether the
4391 * depth or stencil tests used will actually write to the surface.
4392 *
4393 * Even though stencil may be plane 1, it always shares a base_level
4394 * with depth.
4395 */
4396 const struct isl_view *ds_view = &iview->planes[0].isl;
4397 if (iview->aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
4398 genX(cmd_buffer_mark_image_written)(cmd_buffer, image,
4399 VK_IMAGE_ASPECT_DEPTH_BIT,
4400 att_state->aux_usage,
4401 ds_view->base_level,
4402 ds_view->base_array_layer,
4403 fb->layers);
4404 }
4405 if (iview->aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
4406 /* Even though stencil may be plane 1, it always shares a
4407 * base_level with depth.
4408 */
4409 genX(cmd_buffer_mark_image_written)(cmd_buffer, image,
4410 VK_IMAGE_ASPECT_STENCIL_BIT,
4411 ISL_AUX_USAGE_NONE,
4412 ds_view->base_level,
4413 ds_view->base_array_layer,
4414 fb->layers);
4415 }
4416 }
4417
4418 /* If multiview is enabled, then we are only done clearing when we no
4419 * longer have pending layers to clear, or when we have processed the
4420 * last subpass that uses this attachment.
4421 */
4422 if (!is_multiview ||
4423 att_state->pending_clear_views == 0 ||
4424 current_subpass_is_last_for_attachment(cmd_state, a)) {
4425 att_state->pending_clear_aspects = 0;
4426 }
4427
4428 att_state->pending_load_aspects = 0;
4429 }
4430
4431 cmd_buffer_emit_depth_stencil(cmd_buffer);
4432 }
4433
4434 static enum blorp_filter
4435 vk_to_blorp_resolve_mode(VkResolveModeFlagBitsKHR vk_mode)
4436 {
4437 switch (vk_mode) {
4438 case VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR:
4439 return BLORP_FILTER_SAMPLE_0;
4440 case VK_RESOLVE_MODE_AVERAGE_BIT_KHR:
4441 return BLORP_FILTER_AVERAGE;
4442 case VK_RESOLVE_MODE_MIN_BIT_KHR:
4443 return BLORP_FILTER_MIN_SAMPLE;
4444 case VK_RESOLVE_MODE_MAX_BIT_KHR:
4445 return BLORP_FILTER_MAX_SAMPLE;
4446 default:
4447 return BLORP_FILTER_NONE;
4448 }
4449 }
4450
4451 static void
4452 cmd_buffer_end_subpass(struct anv_cmd_buffer *cmd_buffer)
4453 {
4454 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
4455 struct anv_subpass *subpass = cmd_state->subpass;
4456 uint32_t subpass_id = anv_get_subpass_id(&cmd_buffer->state);
4457 struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
4458
4459 if (subpass->has_color_resolve) {
4460 /* We are about to do some MSAA resolves. We need to flush so that the
4461 * result of writes to the MSAA color attachments show up in the sampler
4462 * when we blit to the single-sampled resolve target.
4463 */
4464 cmd_buffer->state.pending_pipe_bits |=
4465 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT |
4466 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
4467
4468 for (uint32_t i = 0; i < subpass->color_count; ++i) {
4469 uint32_t src_att = subpass->color_attachments[i].attachment;
4470 uint32_t dst_att = subpass->resolve_attachments[i].attachment;
4471
4472 if (dst_att == VK_ATTACHMENT_UNUSED)
4473 continue;
4474
4475 assert(src_att < cmd_buffer->state.pass->attachment_count);
4476 assert(dst_att < cmd_buffer->state.pass->attachment_count);
4477
4478 if (cmd_buffer->state.attachments[dst_att].pending_clear_aspects) {
4479 /* From the Vulkan 1.0 spec:
4480 *
4481 * If the first use of an attachment in a render pass is as a
4482 * resolve attachment, then the loadOp is effectively ignored
4483 * as the resolve is guaranteed to overwrite all pixels in the
4484 * render area.
4485 */
4486 cmd_buffer->state.attachments[dst_att].pending_clear_aspects = 0;
4487 }
4488
4489 struct anv_image_view *src_iview = fb->attachments[src_att];
4490 struct anv_image_view *dst_iview = fb->attachments[dst_att];
4491
4492 const VkRect2D render_area = cmd_buffer->state.render_area;
4493
4494 enum isl_aux_usage src_aux_usage =
4495 cmd_buffer->state.attachments[src_att].aux_usage;
4496 enum isl_aux_usage dst_aux_usage =
4497 cmd_buffer->state.attachments[dst_att].aux_usage;
4498
4499 assert(src_iview->aspect_mask == VK_IMAGE_ASPECT_COLOR_BIT &&
4500 dst_iview->aspect_mask == VK_IMAGE_ASPECT_COLOR_BIT);
4501
4502 anv_image_msaa_resolve(cmd_buffer,
4503 src_iview->image, src_aux_usage,
4504 src_iview->planes[0].isl.base_level,
4505 src_iview->planes[0].isl.base_array_layer,
4506 dst_iview->image, dst_aux_usage,
4507 dst_iview->planes[0].isl.base_level,
4508 dst_iview->planes[0].isl.base_array_layer,
4509 VK_IMAGE_ASPECT_COLOR_BIT,
4510 render_area.offset.x, render_area.offset.y,
4511 render_area.offset.x, render_area.offset.y,
4512 render_area.extent.width,
4513 render_area.extent.height,
4514 fb->layers, BLORP_FILTER_NONE);
4515 }
4516 }
4517
4518 if (subpass->ds_resolve_attachment) {
4519 /* We are about to do some MSAA resolves. We need to flush so that the
4520 * result of writes to the MSAA depth attachments show up in the sampler
4521 * when we blit to the single-sampled resolve target.
4522 */
4523 cmd_buffer->state.pending_pipe_bits |=
4524 ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT |
4525 ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
4526
4527 uint32_t src_att = subpass->depth_stencil_attachment->attachment;
4528 uint32_t dst_att = subpass->ds_resolve_attachment->attachment;
4529
4530 assert(src_att < cmd_buffer->state.pass->attachment_count);
4531 assert(dst_att < cmd_buffer->state.pass->attachment_count);
4532
4533 if (cmd_buffer->state.attachments[dst_att].pending_clear_aspects) {
4534 /* From the Vulkan 1.0 spec:
4535 *
4536 * If the first use of an attachment in a render pass is as a
4537 * resolve attachment, then the loadOp is effectively ignored
4538 * as the resolve is guaranteed to overwrite all pixels in the
4539 * render area.
4540 */
4541 cmd_buffer->state.attachments[dst_att].pending_clear_aspects = 0;
4542 }
4543
4544 struct anv_image_view *src_iview = fb->attachments[src_att];
4545 struct anv_image_view *dst_iview = fb->attachments[dst_att];
4546
4547 const VkRect2D render_area = cmd_buffer->state.render_area;
4548
4549 if ((src_iview->image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
4550 subpass->depth_resolve_mode != VK_RESOLVE_MODE_NONE_KHR) {
4551
4552 struct anv_attachment_state *src_state =
4553 &cmd_state->attachments[src_att];
4554 struct anv_attachment_state *dst_state =
4555 &cmd_state->attachments[dst_att];
4556
4557 /* MSAA resolves sample from the source attachment. Transition the
4558 * depth attachment first to get rid of any HiZ that we may not be
4559 * able to handle.
4560 */
4561 transition_depth_buffer(cmd_buffer, src_iview->image,
4562 src_state->current_layout,
4563 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
4564 src_state->aux_usage =
4565 anv_layout_to_aux_usage(&cmd_buffer->device->info, src_iview->image,
4566 VK_IMAGE_ASPECT_DEPTH_BIT,
4567 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
4568 src_state->current_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
4569
4570 /* MSAA resolves write to the resolve attachment as if it were any
4571 * other transfer op. Transition the resolve attachment accordingly.
4572 */
4573 VkImageLayout dst_initial_layout = dst_state->current_layout;
4574
4575 /* If our render area is the entire size of the image, we're going to
4576 * blow it all away so we can claim the initial layout is UNDEFINED
4577 * and we'll get a HiZ ambiguate instead of a resolve.
4578 */
4579 if (dst_iview->image->type != VK_IMAGE_TYPE_3D &&
4580 render_area.offset.x == 0 && render_area.offset.y == 0 &&
4581 render_area.extent.width == dst_iview->extent.width &&
4582 render_area.extent.height == dst_iview->extent.height)
4583 dst_initial_layout = VK_IMAGE_LAYOUT_UNDEFINED;
4584
4585 transition_depth_buffer(cmd_buffer, dst_iview->image,
4586 dst_initial_layout,
4587 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
4588 dst_state->aux_usage =
4589 anv_layout_to_aux_usage(&cmd_buffer->device->info, dst_iview->image,
4590 VK_IMAGE_ASPECT_DEPTH_BIT,
4591 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
4592 dst_state->current_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
4593
4594 enum blorp_filter filter =
4595 vk_to_blorp_resolve_mode(subpass->depth_resolve_mode);
4596
4597 anv_image_msaa_resolve(cmd_buffer,
4598 src_iview->image, src_state->aux_usage,
4599 src_iview->planes[0].isl.base_level,
4600 src_iview->planes[0].isl.base_array_layer,
4601 dst_iview->image, dst_state->aux_usage,
4602 dst_iview->planes[0].isl.base_level,
4603 dst_iview->planes[0].isl.base_array_layer,
4604 VK_IMAGE_ASPECT_DEPTH_BIT,
4605 render_area.offset.x, render_area.offset.y,
4606 render_area.offset.x, render_area.offset.y,
4607 render_area.extent.width,
4608 render_area.extent.height,
4609 fb->layers, filter);
4610 }
4611
4612 if ((src_iview->image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
4613 subpass->stencil_resolve_mode != VK_RESOLVE_MODE_NONE_KHR) {
4614
4615 enum isl_aux_usage src_aux_usage = ISL_AUX_USAGE_NONE;
4616 enum isl_aux_usage dst_aux_usage = ISL_AUX_USAGE_NONE;
4617
4618 enum blorp_filter filter =
4619 vk_to_blorp_resolve_mode(subpass->stencil_resolve_mode);
4620
4621 anv_image_msaa_resolve(cmd_buffer,
4622 src_iview->image, src_aux_usage,
4623 src_iview->planes[0].isl.base_level,
4624 src_iview->planes[0].isl.base_array_layer,
4625 dst_iview->image, dst_aux_usage,
4626 dst_iview->planes[0].isl.base_level,
4627 dst_iview->planes[0].isl.base_array_layer,
4628 VK_IMAGE_ASPECT_STENCIL_BIT,
4629 render_area.offset.x, render_area.offset.y,
4630 render_area.offset.x, render_area.offset.y,
4631 render_area.extent.width,
4632 render_area.extent.height,
4633 fb->layers, filter);
4634 }
4635 }
4636
4637 for (uint32_t i = 0; i < subpass->attachment_count; ++i) {
4638 const uint32_t a = subpass->attachments[i].attachment;
4639 if (a == VK_ATTACHMENT_UNUSED)
4640 continue;
4641
4642 if (cmd_state->pass->attachments[a].last_subpass_idx != subpass_id)
4643 continue;
4644
4645 assert(a < cmd_state->pass->attachment_count);
4646 struct anv_attachment_state *att_state = &cmd_state->attachments[a];
4647 struct anv_image_view *iview = fb->attachments[a];
4648 const struct anv_image *image = iview->image;
4649
4650 if ((image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) &&
4651 image->vk_format != iview->vk_format) {
4652 enum anv_fast_clear_type fast_clear_type =
4653 anv_layout_to_fast_clear_type(&cmd_buffer->device->info,
4654 image, VK_IMAGE_ASPECT_COLOR_BIT,
4655 att_state->current_layout);
4656
4657 /* If any clear color was used, flush it down the aux surfaces. If we
4658 * don't do it now using the view's format we might use the clear
4659 * color incorrectly in the following resolves (for example with an
4660 * SRGB view & a UNORM image).
4661 */
4662 if (fast_clear_type != ANV_FAST_CLEAR_NONE) {
4663 anv_perf_warn(cmd_buffer->device->instance, fb,
4664 "Doing a partial resolve to get rid of clear color at the "
4665 "end of a renderpass due to an image/view format mismatch");
4666
4667 uint32_t base_layer, layer_count;
4668 if (image->type == VK_IMAGE_TYPE_3D) {
4669 base_layer = 0;
4670 layer_count = anv_minify(iview->image->extent.depth,
4671 iview->planes[0].isl.base_level);
4672 } else {
4673 base_layer = iview->planes[0].isl.base_array_layer;
4674 layer_count = fb->layers;
4675 }
4676
4677 for (uint32_t a = 0; a < layer_count; a++) {
4678 uint32_t array_layer = base_layer + a;
4679 if (image->samples == 1) {
4680 anv_cmd_predicated_ccs_resolve(cmd_buffer, image,
4681 iview->planes[0].isl.format,
4682 VK_IMAGE_ASPECT_COLOR_BIT,
4683 iview->planes[0].isl.base_level,
4684 array_layer,
4685 ISL_AUX_OP_PARTIAL_RESOLVE,
4686 ANV_FAST_CLEAR_NONE);
4687 } else {
4688 anv_cmd_predicated_mcs_resolve(cmd_buffer, image,
4689 iview->planes[0].isl.format,
4690 VK_IMAGE_ASPECT_COLOR_BIT,
4691 base_layer,
4692 ISL_AUX_OP_PARTIAL_RESOLVE,
4693 ANV_FAST_CLEAR_NONE);
4694 }
4695 }
4696 }
4697 }
4698
4699 /* Transition the image into the final layout for this render pass */
4700 VkImageLayout target_layout =
4701 cmd_state->pass->attachments[a].final_layout;
4702
4703 if (image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
4704 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
4705
4706 uint32_t base_layer, layer_count;
4707 if (image->type == VK_IMAGE_TYPE_3D) {
4708 base_layer = 0;
4709 layer_count = anv_minify(iview->image->extent.depth,
4710 iview->planes[0].isl.base_level);
4711 } else {
4712 base_layer = iview->planes[0].isl.base_array_layer;
4713 layer_count = fb->layers;
4714 }
4715
4716 transition_color_buffer(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
4717 iview->planes[0].isl.base_level, 1,
4718 base_layer, layer_count,
4719 att_state->current_layout, target_layout);
4720 } else if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
4721 transition_depth_buffer(cmd_buffer, image,
4722 att_state->current_layout, target_layout);
4723 }
4724 }
4725
4726 /* Accumulate any subpass flushes that need to happen after the subpass.
4727 * Yes, they do get accumulated twice in the NextSubpass case but since
4728 * genX_CmdNextSubpass just calls end/begin back-to-back, we just end up
4729 * ORing the bits in twice so it's harmless.
4730 */
4731 cmd_buffer->state.pending_pipe_bits |=
4732 cmd_buffer->state.pass->subpass_flushes[subpass_id + 1];
4733 }
4734
4735 void genX(CmdBeginRenderPass)(
4736 VkCommandBuffer commandBuffer,
4737 const VkRenderPassBeginInfo* pRenderPassBegin,
4738 VkSubpassContents contents)
4739 {
4740 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4741 ANV_FROM_HANDLE(anv_render_pass, pass, pRenderPassBegin->renderPass);
4742 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, pRenderPassBegin->framebuffer);
4743
4744 cmd_buffer->state.framebuffer = framebuffer;
4745 cmd_buffer->state.pass = pass;
4746 cmd_buffer->state.render_area = pRenderPassBegin->renderArea;
4747 VkResult result =
4748 genX(cmd_buffer_setup_attachments)(cmd_buffer, pass, pRenderPassBegin);
4749
4750 /* If we failed to setup the attachments we should not try to go further */
4751 if (result != VK_SUCCESS) {
4752 assert(anv_batch_has_error(&cmd_buffer->batch));
4753 return;
4754 }
4755
4756 genX(flush_pipeline_select_3d)(cmd_buffer);
4757
4758 cmd_buffer_begin_subpass(cmd_buffer, 0);
4759 }
4760
4761 void genX(CmdBeginRenderPass2KHR)(
4762 VkCommandBuffer commandBuffer,
4763 const VkRenderPassBeginInfo* pRenderPassBeginInfo,
4764 const VkSubpassBeginInfoKHR* pSubpassBeginInfo)
4765 {
4766 genX(CmdBeginRenderPass)(commandBuffer, pRenderPassBeginInfo,
4767 pSubpassBeginInfo->contents);
4768 }
4769
4770 void genX(CmdNextSubpass)(
4771 VkCommandBuffer commandBuffer,
4772 VkSubpassContents contents)
4773 {
4774 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4775
4776 if (anv_batch_has_error(&cmd_buffer->batch))
4777 return;
4778
4779 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
4780
4781 uint32_t prev_subpass = anv_get_subpass_id(&cmd_buffer->state);
4782 cmd_buffer_end_subpass(cmd_buffer);
4783 cmd_buffer_begin_subpass(cmd_buffer, prev_subpass + 1);
4784 }
4785
4786 void genX(CmdNextSubpass2KHR)(
4787 VkCommandBuffer commandBuffer,
4788 const VkSubpassBeginInfoKHR* pSubpassBeginInfo,
4789 const VkSubpassEndInfoKHR* pSubpassEndInfo)
4790 {
4791 genX(CmdNextSubpass)(commandBuffer, pSubpassBeginInfo->contents);
4792 }
4793
4794 void genX(CmdEndRenderPass)(
4795 VkCommandBuffer commandBuffer)
4796 {
4797 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4798
4799 if (anv_batch_has_error(&cmd_buffer->batch))
4800 return;
4801
4802 cmd_buffer_end_subpass(cmd_buffer);
4803
4804 cmd_buffer->state.hiz_enabled = false;
4805
4806 #ifndef NDEBUG
4807 anv_dump_add_framebuffer(cmd_buffer, cmd_buffer->state.framebuffer);
4808 #endif
4809
4810 /* Remove references to render pass specific state. This enables us to
4811 * detect whether or not we're in a renderpass.
4812 */
4813 cmd_buffer->state.framebuffer = NULL;
4814 cmd_buffer->state.pass = NULL;
4815 cmd_buffer->state.subpass = NULL;
4816 }
4817
4818 void genX(CmdEndRenderPass2KHR)(
4819 VkCommandBuffer commandBuffer,
4820 const VkSubpassEndInfoKHR* pSubpassEndInfo)
4821 {
4822 genX(CmdEndRenderPass)(commandBuffer);
4823 }
4824
4825 void
4826 genX(cmd_emit_conditional_render_predicate)(struct anv_cmd_buffer *cmd_buffer)
4827 {
4828 #if GEN_GEN >= 8 || GEN_IS_HASWELL
4829 emit_lrr(&cmd_buffer->batch, MI_PREDICATE_SRC0, CS_GPR(ANV_PREDICATE_RESULT_REG));
4830 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC0 + 4, 0);
4831 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1, 0);
4832 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1 + 4, 0);
4833
4834 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
4835 mip.LoadOperation = LOAD_LOADINV;
4836 mip.CombineOperation = COMBINE_SET;
4837 mip.CompareOperation = COMPARE_SRCS_EQUAL;
4838 }
4839 #endif
4840 }
4841
4842 #if GEN_GEN >= 8 || GEN_IS_HASWELL
4843 void genX(CmdBeginConditionalRenderingEXT)(
4844 VkCommandBuffer commandBuffer,
4845 const VkConditionalRenderingBeginInfoEXT* pConditionalRenderingBegin)
4846 {
4847 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4848 ANV_FROM_HANDLE(anv_buffer, buffer, pConditionalRenderingBegin->buffer);
4849 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
4850 struct anv_address value_address =
4851 anv_address_add(buffer->address, pConditionalRenderingBegin->offset);
4852
4853 const bool isInverted = pConditionalRenderingBegin->flags &
4854 VK_CONDITIONAL_RENDERING_INVERTED_BIT_EXT;
4855
4856 cmd_state->conditional_render_enabled = true;
4857
4858 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
4859
4860 /* Section 19.4 of the Vulkan 1.1.85 spec says:
4861 *
4862 * If the value of the predicate in buffer memory changes
4863 * while conditional rendering is active, the rendering commands
4864 * may be discarded in an implementation-dependent way.
4865 * Some implementations may latch the value of the predicate
4866 * upon beginning conditional rendering while others
4867 * may read it before every rendering command.
4868 *
4869 * So it's perfectly fine to read a value from the buffer once.
4870 */
4871 emit_lrm(&cmd_buffer->batch, CS_GPR(MI_ALU_REG0), value_address);
4872 /* Zero the top 32-bits of MI_PREDICATE_SRC0 */
4873 emit_lri(&cmd_buffer->batch, CS_GPR(MI_ALU_REG0) + 4, 0);
4874
4875 /* Precompute predicate result, it is necessary to support secondary
4876 * command buffers since it is unknown if conditional rendering is
4877 * inverted when populating them.
4878 */
4879 uint32_t *dw = anv_batch_emitn(&cmd_buffer->batch, 5, GENX(MI_MATH));
4880 dw[1] = mi_alu(MI_ALU_LOAD0, MI_ALU_SRCA, 0);
4881 dw[2] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, MI_ALU_REG0);
4882 dw[3] = mi_alu(MI_ALU_SUB, 0, 0);
4883 dw[4] = mi_alu(isInverted ? MI_ALU_STOREINV : MI_ALU_STORE,
4884 ANV_PREDICATE_RESULT_REG, MI_ALU_CF);
4885 }
4886
4887 void genX(CmdEndConditionalRenderingEXT)(
4888 VkCommandBuffer commandBuffer)
4889 {
4890 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
4891 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
4892
4893 cmd_state->conditional_render_enabled = false;
4894 }
4895 #endif