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