anv/cmd_buffer: Always enable CCS_D in render passes
[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
31 #include "common/gen_l3_config.h"
32 #include "genxml/gen_macros.h"
33 #include "genxml/genX_pack.h"
34
35 static void
36 emit_lrm(struct anv_batch *batch,
37 uint32_t reg, struct anv_bo *bo, uint32_t offset)
38 {
39 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
40 lrm.RegisterAddress = reg;
41 lrm.MemoryAddress = (struct anv_address) { bo, offset };
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 /* Emit a render target cache flush.
71 *
72 * This isn't documented anywhere in the PRM. However, it seems to be
73 * necessary prior to changing the surface state base adress. Without
74 * this, we get GPU hangs when using multi-level command buffers which
75 * clear depth, reset state base address, and then go render stuff.
76 */
77 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
78 pc.DCFlushEnable = true;
79 pc.RenderTargetCacheFlushEnable = true;
80 pc.CommandStreamerStallEnable = true;
81 }
82
83 anv_batch_emit(&cmd_buffer->batch, GENX(STATE_BASE_ADDRESS), sba) {
84 sba.GeneralStateBaseAddress = (struct anv_address) { NULL, 0 };
85 sba.GeneralStateMemoryObjectControlState = GENX(MOCS);
86 sba.GeneralStateBaseAddressModifyEnable = true;
87
88 sba.SurfaceStateBaseAddress =
89 anv_cmd_buffer_surface_base_address(cmd_buffer);
90 sba.SurfaceStateMemoryObjectControlState = GENX(MOCS);
91 sba.SurfaceStateBaseAddressModifyEnable = true;
92
93 sba.DynamicStateBaseAddress =
94 (struct anv_address) { &device->dynamic_state_pool.block_pool.bo, 0 };
95 sba.DynamicStateMemoryObjectControlState = GENX(MOCS);
96 sba.DynamicStateBaseAddressModifyEnable = true;
97
98 sba.IndirectObjectBaseAddress = (struct anv_address) { NULL, 0 };
99 sba.IndirectObjectMemoryObjectControlState = GENX(MOCS);
100 sba.IndirectObjectBaseAddressModifyEnable = true;
101
102 sba.InstructionBaseAddress =
103 (struct anv_address) { &device->instruction_state_pool.block_pool.bo, 0 };
104 sba.InstructionMemoryObjectControlState = GENX(MOCS);
105 sba.InstructionBaseAddressModifyEnable = true;
106
107 # if (GEN_GEN >= 8)
108 /* Broadwell requires that we specify a buffer size for a bunch of
109 * these fields. However, since we will be growing the BO's live, we
110 * just set them all to the maximum.
111 */
112 sba.GeneralStateBufferSize = 0xfffff;
113 sba.GeneralStateBufferSizeModifyEnable = true;
114 sba.DynamicStateBufferSize = 0xfffff;
115 sba.DynamicStateBufferSizeModifyEnable = true;
116 sba.IndirectObjectBufferSize = 0xfffff;
117 sba.IndirectObjectBufferSizeModifyEnable = true;
118 sba.InstructionBufferSize = 0xfffff;
119 sba.InstructionBuffersizeModifyEnable = true;
120 # endif
121 }
122
123 /* After re-setting the surface state base address, we have to do some
124 * cache flusing so that the sampler engine will pick up the new
125 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
126 * Shared Function > 3D Sampler > State > State Caching (page 96):
127 *
128 * Coherency with system memory in the state cache, like the texture
129 * cache is handled partially by software. It is expected that the
130 * command stream or shader will issue Cache Flush operation or
131 * Cache_Flush sampler message to ensure that the L1 cache remains
132 * coherent with system memory.
133 *
134 * [...]
135 *
136 * Whenever the value of the Dynamic_State_Base_Addr,
137 * Surface_State_Base_Addr are altered, the L1 state cache must be
138 * invalidated to ensure the new surface or sampler state is fetched
139 * from system memory.
140 *
141 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
142 * which, according the PIPE_CONTROL instruction documentation in the
143 * Broadwell PRM:
144 *
145 * Setting this bit is independent of any other bit in this packet.
146 * This bit controls the invalidation of the L1 and L2 state caches
147 * at the top of the pipe i.e. at the parsing time.
148 *
149 * Unfortunately, experimentation seems to indicate that state cache
150 * invalidation through a PIPE_CONTROL does nothing whatsoever in
151 * regards to surface state and binding tables. In stead, it seems that
152 * invalidating the texture cache is what is actually needed.
153 *
154 * XXX: As far as we have been able to determine through
155 * experimentation, shows that flush the texture cache appears to be
156 * sufficient. The theory here is that all of the sampling/rendering
157 * units cache the binding table in the texture cache. However, we have
158 * yet to be able to actually confirm this.
159 */
160 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
161 pc.TextureCacheInvalidationEnable = true;
162 pc.ConstantCacheInvalidationEnable = true;
163 pc.StateCacheInvalidationEnable = true;
164 }
165 }
166
167 static void
168 add_surface_state_reloc(struct anv_cmd_buffer *cmd_buffer,
169 struct anv_state state,
170 struct anv_bo *bo, uint32_t offset)
171 {
172 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
173
174 VkResult result =
175 anv_reloc_list_add(&cmd_buffer->surface_relocs, &cmd_buffer->pool->alloc,
176 state.offset + isl_dev->ss.addr_offset, bo, offset);
177 if (result != VK_SUCCESS)
178 anv_batch_set_error(&cmd_buffer->batch, result);
179 }
180
181 static void
182 add_image_relocs(struct anv_cmd_buffer * const cmd_buffer,
183 const struct anv_image * const image,
184 const VkImageAspectFlags aspect_mask,
185 const enum isl_aux_usage aux_usage,
186 const struct anv_state state)
187 {
188 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
189 const uint32_t surf_offset = image->offset +
190 anv_image_get_surface_for_aspect_mask(image, aspect_mask)->offset;
191
192 add_surface_state_reloc(cmd_buffer, state, image->bo, surf_offset);
193
194 if (aux_usage != ISL_AUX_USAGE_NONE) {
195 uint32_t aux_offset = image->offset + image->aux_surface.offset;
196
197 /* On gen7 and prior, the bottom 12 bits of the MCS base address are
198 * used to store other information. This should be ok, however, because
199 * surface buffer addresses are always 4K page alinged.
200 */
201 assert((aux_offset & 0xfff) == 0);
202 uint32_t *aux_addr_dw = state.map + isl_dev->ss.aux_addr_offset;
203 aux_offset += *aux_addr_dw & 0xfff;
204
205 VkResult result =
206 anv_reloc_list_add(&cmd_buffer->surface_relocs,
207 &cmd_buffer->pool->alloc,
208 state.offset + isl_dev->ss.aux_addr_offset,
209 image->bo, aux_offset);
210 if (result != VK_SUCCESS)
211 anv_batch_set_error(&cmd_buffer->batch, result);
212 }
213 }
214
215 static bool
216 color_is_zero_one(VkClearColorValue value, enum isl_format format)
217 {
218 if (isl_format_has_int_channel(format)) {
219 for (unsigned i = 0; i < 4; i++) {
220 if (value.int32[i] != 0 && value.int32[i] != 1)
221 return false;
222 }
223 } else {
224 for (unsigned i = 0; i < 4; i++) {
225 if (value.float32[i] != 0.0f && value.float32[i] != 1.0f)
226 return false;
227 }
228 }
229
230 return true;
231 }
232
233 static void
234 color_attachment_compute_aux_usage(struct anv_device * device,
235 struct anv_cmd_state * cmd_state,
236 uint32_t att, VkRect2D render_area,
237 union isl_color_value *fast_clear_color)
238 {
239 struct anv_attachment_state *att_state = &cmd_state->attachments[att];
240 struct anv_image_view *iview = cmd_state->framebuffer->attachments[att];
241
242 if (iview->isl.base_array_layer >=
243 anv_image_aux_layers(iview->image, iview->isl.base_level)) {
244 /* There is no aux buffer which corresponds to the level and layer(s)
245 * being accessed.
246 */
247 att_state->aux_usage = ISL_AUX_USAGE_NONE;
248 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
249 att_state->fast_clear = false;
250 return;
251 } else if (iview->image->aux_usage == ISL_AUX_USAGE_MCS) {
252 att_state->aux_usage = ISL_AUX_USAGE_MCS;
253 att_state->input_aux_usage = ISL_AUX_USAGE_MCS;
254 att_state->fast_clear = false;
255 return;
256 }
257
258 assert(iview->image->aux_surface.isl.usage & ISL_SURF_USAGE_CCS_BIT);
259
260 att_state->clear_color_is_zero_one =
261 color_is_zero_one(att_state->clear_value.color, iview->isl.format);
262 att_state->clear_color_is_zero =
263 att_state->clear_value.color.uint32[0] == 0 &&
264 att_state->clear_value.color.uint32[1] == 0 &&
265 att_state->clear_value.color.uint32[2] == 0 &&
266 att_state->clear_value.color.uint32[3] == 0;
267
268 if (att_state->pending_clear_aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
269 /* Start off assuming fast clears are possible */
270 att_state->fast_clear = true;
271
272 /* Potentially, we could do partial fast-clears but doing so has crazy
273 * alignment restrictions. It's easier to just restrict to full size
274 * fast clears for now.
275 */
276 if (render_area.offset.x != 0 ||
277 render_area.offset.y != 0 ||
278 render_area.extent.width != iview->extent.width ||
279 render_area.extent.height != iview->extent.height)
280 att_state->fast_clear = false;
281
282 /* On Broadwell and earlier, we can only handle 0/1 clear colors */
283 if (GEN_GEN <= 8 && !att_state->clear_color_is_zero_one)
284 att_state->fast_clear = false;
285
286 /* We allow fast clears when all aux layers of the miplevel are targeted.
287 * See add_fast_clear_state_buffer() for more information. Also, because
288 * we only either do a fast clear or a normal clear and not both, this
289 * complies with the gen7 restriction of not fast-clearing multiple
290 * layers.
291 */
292 if (cmd_state->framebuffer->layers !=
293 anv_image_aux_layers(iview->image, iview->isl.base_level)) {
294 att_state->fast_clear = false;
295 if (GEN_GEN == 7) {
296 anv_perf_warn("Not fast-clearing the first layer in "
297 "a multi-layer fast clear.");
298 }
299 }
300
301 /* We only allow fast clears in the GENERAL layout if the auxiliary
302 * buffer is always enabled and the fast-clear value is all 0's. See
303 * add_fast_clear_state_buffer() for more information.
304 */
305 if (cmd_state->pass->attachments[att].first_subpass_layout ==
306 VK_IMAGE_LAYOUT_GENERAL &&
307 (!att_state->clear_color_is_zero ||
308 iview->image->aux_usage == ISL_AUX_USAGE_NONE)) {
309 att_state->fast_clear = false;
310 }
311
312 if (att_state->fast_clear) {
313 memcpy(fast_clear_color->u32, att_state->clear_value.color.uint32,
314 sizeof(fast_clear_color->u32));
315 }
316 } else {
317 att_state->fast_clear = false;
318 }
319
320 /**
321 * TODO: Consider using a heuristic to determine if temporarily enabling
322 * CCS_E for this image view would be beneficial.
323 *
324 * While fast-clear resolves and partial resolves are fairly cheap in the
325 * case where you render to most of the pixels, full resolves are not
326 * because they potentially involve reading and writing the entire
327 * framebuffer. If we can't texture with CCS_E, we should leave it off and
328 * limit ourselves to fast clears.
329 */
330 if (iview->image->aux_usage == ISL_AUX_USAGE_CCS_E) {
331 att_state->aux_usage = ISL_AUX_USAGE_CCS_E;
332 att_state->input_aux_usage = ISL_AUX_USAGE_CCS_E;
333 } else {
334 att_state->aux_usage = ISL_AUX_USAGE_CCS_D;
335 /* From the Sky Lake PRM, RENDER_SURFACE_STATE::AuxiliarySurfaceMode:
336 *
337 * "If Number of Multisamples is MULTISAMPLECOUNT_1, AUX_CCS_D
338 * setting is only allowed if Surface Format supported for Fast
339 * Clear. In addition, if the surface is bound to the sampling
340 * engine, Surface Format must be supported for Render Target
341 * Compression for surfaces bound to the sampling engine."
342 *
343 * In other words, we can only sample from a fast-cleared image if it
344 * also supports color compression.
345 */
346 if (isl_format_supports_ccs_e(&device->info, iview->isl.format))
347 att_state->input_aux_usage = ISL_AUX_USAGE_CCS_D;
348 else
349 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
350 }
351 }
352
353 static bool
354 need_input_attachment_state(const struct anv_render_pass_attachment *att)
355 {
356 if (!(att->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
357 return false;
358
359 /* We only allocate input attachment states for color surfaces. Compression
360 * is not yet enabled for depth textures and stencil doesn't allow
361 * compression so we can just use the texture surface state from the view.
362 */
363 return vk_format_is_color(att->format);
364 }
365
366 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
367 * the initial layout is undefined, the HiZ buffer and depth buffer will
368 * represent the same data at the end of this operation.
369 */
370 static void
371 transition_depth_buffer(struct anv_cmd_buffer *cmd_buffer,
372 const struct anv_image *image,
373 VkImageLayout initial_layout,
374 VkImageLayout final_layout)
375 {
376 assert(image);
377
378 /* A transition is a no-op if HiZ is not enabled, or if the initial and
379 * final layouts are equal.
380 *
381 * The undefined layout indicates that the user doesn't care about the data
382 * that's currently in the buffer. Therefore, a data-preserving resolve
383 * operation is not needed.
384 */
385 if (image->aux_usage != ISL_AUX_USAGE_HIZ || initial_layout == final_layout)
386 return;
387
388 const bool hiz_enabled = ISL_AUX_USAGE_HIZ ==
389 anv_layout_to_aux_usage(&cmd_buffer->device->info, image, image->aspects,
390 initial_layout);
391 const bool enable_hiz = ISL_AUX_USAGE_HIZ ==
392 anv_layout_to_aux_usage(&cmd_buffer->device->info, image, image->aspects,
393 final_layout);
394
395 enum blorp_hiz_op hiz_op;
396 if (hiz_enabled && !enable_hiz) {
397 hiz_op = BLORP_HIZ_OP_DEPTH_RESOLVE;
398 } else if (!hiz_enabled && enable_hiz) {
399 hiz_op = BLORP_HIZ_OP_HIZ_RESOLVE;
400 } else {
401 assert(hiz_enabled == enable_hiz);
402 /* If the same buffer will be used, no resolves are necessary. */
403 hiz_op = BLORP_HIZ_OP_NONE;
404 }
405
406 if (hiz_op != BLORP_HIZ_OP_NONE)
407 anv_gen8_hiz_op_resolve(cmd_buffer, image, hiz_op);
408 }
409
410 static inline uint32_t
411 get_fast_clear_state_entry_offset(const struct anv_device *device,
412 const struct anv_image *image,
413 unsigned level)
414 {
415 assert(device && image);
416 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
417 assert(level < anv_image_aux_levels(image));
418 const uint32_t offset = image->offset + image->aux_surface.offset +
419 image->aux_surface.isl.size +
420 anv_fast_clear_state_entry_size(device) * level;
421 assert(offset < image->offset + image->size);
422 return offset;
423 }
424
425 static void
426 init_fast_clear_state_entry(struct anv_cmd_buffer *cmd_buffer,
427 const struct anv_image *image,
428 unsigned level)
429 {
430 assert(cmd_buffer && image);
431 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
432 assert(level < anv_image_aux_levels(image));
433
434 /* The fast clear value dword(s) will be copied into a surface state object.
435 * Ensure that the restrictions of the fields in the dword(s) are followed.
436 *
437 * CCS buffers on SKL+ can have any value set for the clear colors.
438 */
439 if (image->samples == 1 && GEN_GEN >= 9)
440 return;
441
442 /* Other combinations of auxiliary buffers and platforms require specific
443 * values in the clear value dword(s).
444 */
445 unsigned i = 0;
446 for (; i < cmd_buffer->device->isl_dev.ss.clear_value_size; i += 4) {
447 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
448 const uint32_t entry_offset =
449 get_fast_clear_state_entry_offset(cmd_buffer->device, image, level);
450 sdi.Address = (struct anv_address) { image->bo, entry_offset + i };
451
452 if (GEN_GEN >= 9) {
453 /* MCS buffers on SKL+ can only have 1/0 clear colors. */
454 assert(image->aux_usage == ISL_AUX_USAGE_MCS);
455 sdi.ImmediateData = 0;
456 } else if (GEN_VERSIONx10 >= 75) {
457 /* Pre-SKL, the dword containing the clear values also contains
458 * other fields, so we need to initialize those fields to match the
459 * values that would be in a color attachment.
460 */
461 assert(i == 0);
462 sdi.ImmediateData = ISL_CHANNEL_SELECT_RED << 25 |
463 ISL_CHANNEL_SELECT_GREEN << 22 |
464 ISL_CHANNEL_SELECT_BLUE << 19 |
465 ISL_CHANNEL_SELECT_ALPHA << 16;
466 } else if (GEN_VERSIONx10 == 70) {
467 /* On IVB, the dword containing the clear values also contains
468 * other fields that must be zero or can be zero.
469 */
470 assert(i == 0);
471 sdi.ImmediateData = 0;
472 }
473 }
474 }
475 }
476
477 /* Copy the fast-clear value dword(s) between a surface state object and an
478 * image's fast clear state buffer.
479 */
480 static void
481 genX(copy_fast_clear_dwords)(struct anv_cmd_buffer *cmd_buffer,
482 struct anv_state surface_state,
483 const struct anv_image *image,
484 unsigned level,
485 bool copy_from_surface_state)
486 {
487 assert(cmd_buffer && image);
488 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
489 assert(level < anv_image_aux_levels(image));
490
491 struct anv_bo *ss_bo =
492 &cmd_buffer->device->surface_state_pool.block_pool.bo;
493 uint32_t ss_clear_offset = surface_state.offset +
494 cmd_buffer->device->isl_dev.ss.clear_value_offset;
495 uint32_t entry_offset =
496 get_fast_clear_state_entry_offset(cmd_buffer->device, image, level);
497 unsigned copy_size = cmd_buffer->device->isl_dev.ss.clear_value_size;
498
499 if (copy_from_surface_state) {
500 genX(cmd_buffer_mi_memcpy)(cmd_buffer, image->bo, entry_offset,
501 ss_bo, ss_clear_offset, copy_size);
502 } else {
503 genX(cmd_buffer_mi_memcpy)(cmd_buffer, ss_bo, ss_clear_offset,
504 image->bo, entry_offset, copy_size);
505
506 /* Updating a surface state object may require that the state cache be
507 * invalidated. From the SKL PRM, Shared Functions -> State -> State
508 * Caching:
509 *
510 * Whenever the RENDER_SURFACE_STATE object in memory pointed to by
511 * the Binding Table Pointer (BTP) and Binding Table Index (BTI) is
512 * modified [...], the L1 state cache must be invalidated to ensure
513 * the new surface or sampler state is fetched from system memory.
514 *
515 * In testing, SKL doesn't actually seem to need this, but HSW does.
516 */
517 cmd_buffer->state.pending_pipe_bits |=
518 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT;
519 }
520 }
521
522 static void
523 transition_color_buffer(struct anv_cmd_buffer *cmd_buffer,
524 const struct anv_image *image,
525 const uint32_t base_level, uint32_t level_count,
526 uint32_t base_layer, uint32_t layer_count,
527 VkImageLayout initial_layout,
528 VkImageLayout final_layout)
529 {
530 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
531
532 if (image->aux_surface.isl.size == 0 ||
533 base_level >= anv_image_aux_levels(image))
534 return;
535
536 if (initial_layout != VK_IMAGE_LAYOUT_UNDEFINED &&
537 initial_layout != VK_IMAGE_LAYOUT_PREINITIALIZED)
538 return;
539
540 /* A transition of a 3D subresource works on all slices at a time. */
541 if (image->type == VK_IMAGE_TYPE_3D) {
542 base_layer = 0;
543 layer_count = anv_minify(image->extent.depth, base_level);
544 }
545
546 /* We're interested in the subresource range subset that has aux data. */
547 level_count = MIN2(level_count, anv_image_aux_levels(image) - base_level);
548
549 /* We're transitioning from an undefined layout. We must ensure that the
550 * clear values buffer is filled with valid data.
551 */
552 for (unsigned l = 0; l < level_count; l++)
553 init_fast_clear_state_entry(cmd_buffer, image, base_level + l);
554
555 if (image->aux_usage == ISL_AUX_USAGE_CCS_E ||
556 image->aux_usage == ISL_AUX_USAGE_MCS) {
557 /* We're transitioning from an undefined layout so it doesn't really
558 * matter what data ends up in the color buffer. We do, however, need to
559 * ensure that the auxiliary surface is not in an undefined state. This
560 * state is possible for CCS buffers SKL+ and MCS buffers with certain
561 * sample counts that require certain bits to be reserved (2x and 8x).
562 * One easy way to get to a valid state is to fast-clear the specified
563 * range.
564 *
565 * Even for MCS buffers that have sample counts that don't require
566 * certain bits to be reserved (4x and 8x), we're unsure if the hardware
567 * will be okay with the sample mappings given by the undefined buffer.
568 * We don't have any data to show that this is a problem, but we want to
569 * avoid causing difficult-to-debug problems.
570 */
571 if (image->samples == 4 || image->samples == 16) {
572 anv_perf_warn("Doing a potentially unnecessary fast-clear to define "
573 "an MCS buffer.");
574 }
575
576 anv_image_fast_clear(cmd_buffer, image, base_level, level_count,
577 base_layer, layer_count);
578 }
579 }
580
581 /**
582 * Setup anv_cmd_state::attachments for vkCmdBeginRenderPass.
583 */
584 static VkResult
585 genX(cmd_buffer_setup_attachments)(struct anv_cmd_buffer *cmd_buffer,
586 struct anv_render_pass *pass,
587 const VkRenderPassBeginInfo *begin)
588 {
589 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
590 struct anv_cmd_state *state = &cmd_buffer->state;
591
592 vk_free(&cmd_buffer->pool->alloc, state->attachments);
593
594 if (pass->attachment_count > 0) {
595 state->attachments = vk_alloc(&cmd_buffer->pool->alloc,
596 pass->attachment_count *
597 sizeof(state->attachments[0]),
598 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
599 if (state->attachments == NULL) {
600 /* Propagate VK_ERROR_OUT_OF_HOST_MEMORY to vkEndCommandBuffer */
601 return anv_batch_set_error(&cmd_buffer->batch,
602 VK_ERROR_OUT_OF_HOST_MEMORY);
603 }
604 } else {
605 state->attachments = NULL;
606 }
607
608 /* Reserve one for the NULL state. */
609 unsigned num_states = 1;
610 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
611 if (vk_format_is_color(pass->attachments[i].format))
612 num_states++;
613
614 if (need_input_attachment_state(&pass->attachments[i]))
615 num_states++;
616 }
617
618 const uint32_t ss_stride = align_u32(isl_dev->ss.size, isl_dev->ss.align);
619 state->render_pass_states =
620 anv_state_stream_alloc(&cmd_buffer->surface_state_stream,
621 num_states * ss_stride, isl_dev->ss.align);
622
623 struct anv_state next_state = state->render_pass_states;
624 next_state.alloc_size = isl_dev->ss.size;
625
626 state->null_surface_state = next_state;
627 next_state.offset += ss_stride;
628 next_state.map += ss_stride;
629
630 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
631 if (vk_format_is_color(pass->attachments[i].format)) {
632 state->attachments[i].color_rt_state = next_state;
633 next_state.offset += ss_stride;
634 next_state.map += ss_stride;
635 }
636
637 if (need_input_attachment_state(&pass->attachments[i])) {
638 state->attachments[i].input_att_state = next_state;
639 next_state.offset += ss_stride;
640 next_state.map += ss_stride;
641 }
642 }
643 assert(next_state.offset == state->render_pass_states.offset +
644 state->render_pass_states.alloc_size);
645
646 if (begin) {
647 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, begin->framebuffer);
648 assert(pass->attachment_count == framebuffer->attachment_count);
649
650 struct GENX(RENDER_SURFACE_STATE) null_ss = {
651 .SurfaceType = SURFTYPE_NULL,
652 .SurfaceArray = framebuffer->layers > 0,
653 .SurfaceFormat = ISL_FORMAT_R8G8B8A8_UNORM,
654 #if GEN_GEN >= 8
655 .TileMode = YMAJOR,
656 #else
657 .TiledSurface = true,
658 #endif
659 .Width = framebuffer->width - 1,
660 .Height = framebuffer->height - 1,
661 .Depth = framebuffer->layers - 1,
662 .RenderTargetViewExtent = framebuffer->layers - 1,
663 };
664 GENX(RENDER_SURFACE_STATE_pack)(NULL, state->null_surface_state.map,
665 &null_ss);
666
667 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
668 struct anv_render_pass_attachment *att = &pass->attachments[i];
669 VkImageAspectFlags att_aspects = vk_format_aspects(att->format);
670 VkImageAspectFlags clear_aspects = 0;
671
672 if (att_aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
673 /* color attachment */
674 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
675 clear_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
676 }
677 } else {
678 /* depthstencil attachment */
679 if ((att_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
680 att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
681 clear_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
682 }
683 if ((att_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
684 att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
685 clear_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
686 }
687 }
688
689 state->attachments[i].current_layout = att->initial_layout;
690 state->attachments[i].pending_clear_aspects = clear_aspects;
691 if (clear_aspects)
692 state->attachments[i].clear_value = begin->pClearValues[i];
693
694 struct anv_image_view *iview = framebuffer->attachments[i];
695 anv_assert(iview->vk_format == att->format);
696
697 union isl_color_value clear_color = { .u32 = { 0, } };
698 if (att_aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
699 color_attachment_compute_aux_usage(cmd_buffer->device,
700 state, i, begin->renderArea,
701 &clear_color);
702
703 struct isl_view view = iview->isl;
704 view.usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
705 view.swizzle = anv_swizzle_for_render(view.swizzle);
706 isl_surf_fill_state(isl_dev,
707 state->attachments[i].color_rt_state.map,
708 .surf = &iview->image->color_surface.isl,
709 .view = &view,
710 .aux_surf = &iview->image->aux_surface.isl,
711 .aux_usage = state->attachments[i].aux_usage,
712 .clear_color = clear_color,
713 .mocs = cmd_buffer->device->default_mocs);
714
715 add_image_relocs(cmd_buffer, iview->image, iview->aspect_mask,
716 state->attachments[i].aux_usage,
717 state->attachments[i].color_rt_state);
718 } else {
719 /* This field will be initialized after the first subpass
720 * transition.
721 */
722 state->attachments[i].aux_usage = ISL_AUX_USAGE_NONE;
723
724 state->attachments[i].input_aux_usage = ISL_AUX_USAGE_NONE;
725 }
726
727 if (need_input_attachment_state(&pass->attachments[i])) {
728 struct isl_view view = iview->isl;
729 view.usage |= ISL_SURF_USAGE_TEXTURE_BIT;
730 isl_surf_fill_state(isl_dev,
731 state->attachments[i].input_att_state.map,
732 .surf = &iview->image->color_surface.isl,
733 .view = &view,
734 .aux_surf = &iview->image->aux_surface.isl,
735 .aux_usage = state->attachments[i].input_aux_usage,
736 .clear_color = clear_color,
737 .mocs = cmd_buffer->device->default_mocs);
738
739 add_image_relocs(cmd_buffer, iview->image, iview->aspect_mask,
740 state->attachments[i].input_aux_usage,
741 state->attachments[i].input_att_state);
742 }
743 }
744
745 anv_state_flush(cmd_buffer->device, state->render_pass_states);
746 }
747
748 return VK_SUCCESS;
749 }
750
751 VkResult
752 genX(BeginCommandBuffer)(
753 VkCommandBuffer commandBuffer,
754 const VkCommandBufferBeginInfo* pBeginInfo)
755 {
756 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
757
758 /* If this is the first vkBeginCommandBuffer, we must *initialize* the
759 * command buffer's state. Otherwise, we must *reset* its state. In both
760 * cases we reset it.
761 *
762 * From the Vulkan 1.0 spec:
763 *
764 * If a command buffer is in the executable state and the command buffer
765 * was allocated from a command pool with the
766 * VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT flag set, then
767 * vkBeginCommandBuffer implicitly resets the command buffer, behaving
768 * as if vkResetCommandBuffer had been called with
769 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT not set. It then puts
770 * the command buffer in the recording state.
771 */
772 anv_cmd_buffer_reset(cmd_buffer);
773
774 cmd_buffer->usage_flags = pBeginInfo->flags;
775
776 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY ||
777 !(cmd_buffer->usage_flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT));
778
779 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
780
781 /* We sometimes store vertex data in the dynamic state buffer for blorp
782 * operations and our dynamic state stream may re-use data from previous
783 * command buffers. In order to prevent stale cache data, we flush the VF
784 * cache. We could do this on every blorp call but that's not really
785 * needed as all of the data will get written by the CPU prior to the GPU
786 * executing anything. The chances are fairly high that they will use
787 * blorp at least once per primary command buffer so it shouldn't be
788 * wasted.
789 */
790 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY)
791 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
792
793 VkResult result = VK_SUCCESS;
794 if (cmd_buffer->usage_flags &
795 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
796 cmd_buffer->state.pass =
797 anv_render_pass_from_handle(pBeginInfo->pInheritanceInfo->renderPass);
798 cmd_buffer->state.subpass =
799 &cmd_buffer->state.pass->subpasses[pBeginInfo->pInheritanceInfo->subpass];
800 cmd_buffer->state.framebuffer = NULL;
801
802 result = genX(cmd_buffer_setup_attachments)(cmd_buffer,
803 cmd_buffer->state.pass, NULL);
804
805 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
806 }
807
808 return result;
809 }
810
811 VkResult
812 genX(EndCommandBuffer)(
813 VkCommandBuffer commandBuffer)
814 {
815 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
816
817 if (anv_batch_has_error(&cmd_buffer->batch))
818 return cmd_buffer->batch.status;
819
820 /* We want every command buffer to start with the PMA fix in a known state,
821 * so we disable it at the end of the command buffer.
822 */
823 genX(cmd_buffer_enable_pma_fix)(cmd_buffer, false);
824
825 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
826
827 anv_cmd_buffer_end_batch_buffer(cmd_buffer);
828
829 return VK_SUCCESS;
830 }
831
832 void
833 genX(CmdExecuteCommands)(
834 VkCommandBuffer commandBuffer,
835 uint32_t commandBufferCount,
836 const VkCommandBuffer* pCmdBuffers)
837 {
838 ANV_FROM_HANDLE(anv_cmd_buffer, primary, commandBuffer);
839
840 assert(primary->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
841
842 if (anv_batch_has_error(&primary->batch))
843 return;
844
845 /* The secondary command buffers will assume that the PMA fix is disabled
846 * when they begin executing. Make sure this is true.
847 */
848 genX(cmd_buffer_enable_pma_fix)(primary, false);
849
850 /* The secondary command buffer doesn't know which textures etc. have been
851 * flushed prior to their execution. Apply those flushes now.
852 */
853 genX(cmd_buffer_apply_pipe_flushes)(primary);
854
855 for (uint32_t i = 0; i < commandBufferCount; i++) {
856 ANV_FROM_HANDLE(anv_cmd_buffer, secondary, pCmdBuffers[i]);
857
858 assert(secondary->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY);
859 assert(!anv_batch_has_error(&secondary->batch));
860
861 if (secondary->usage_flags &
862 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
863 /* If we're continuing a render pass from the primary, we need to
864 * copy the surface states for the current subpass into the storage
865 * we allocated for them in BeginCommandBuffer.
866 */
867 struct anv_bo *ss_bo =
868 &primary->device->surface_state_pool.block_pool.bo;
869 struct anv_state src_state = primary->state.render_pass_states;
870 struct anv_state dst_state = secondary->state.render_pass_states;
871 assert(src_state.alloc_size == dst_state.alloc_size);
872
873 genX(cmd_buffer_so_memcpy)(primary, ss_bo, dst_state.offset,
874 ss_bo, src_state.offset,
875 src_state.alloc_size);
876 }
877
878 anv_cmd_buffer_add_secondary(primary, secondary);
879 }
880
881 /* Each of the secondary command buffers will use its own state base
882 * address. We need to re-emit state base address for the primary after
883 * all of the secondaries are done.
884 *
885 * TODO: Maybe we want to make this a dirty bit to avoid extra state base
886 * address calls?
887 */
888 genX(cmd_buffer_emit_state_base_address)(primary);
889 }
890
891 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
892 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
893 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
894
895 /**
896 * Program the hardware to use the specified L3 configuration.
897 */
898 void
899 genX(cmd_buffer_config_l3)(struct anv_cmd_buffer *cmd_buffer,
900 const struct gen_l3_config *cfg)
901 {
902 assert(cfg);
903 if (cfg == cmd_buffer->state.current_l3_config)
904 return;
905
906 if (unlikely(INTEL_DEBUG & DEBUG_L3)) {
907 fprintf(stderr, "L3 config transition: ");
908 gen_dump_l3_config(cfg, stderr);
909 }
910
911 const bool has_slm = cfg->n[GEN_L3P_SLM];
912
913 /* According to the hardware docs, the L3 partitioning can only be changed
914 * while the pipeline is completely drained and the caches are flushed,
915 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
916 */
917 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
918 pc.DCFlushEnable = true;
919 pc.PostSyncOperation = NoWrite;
920 pc.CommandStreamerStallEnable = true;
921 }
922
923 /* ...followed by a second pipelined PIPE_CONTROL that initiates
924 * invalidation of the relevant caches. Note that because RO invalidation
925 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
926 * command is processed by the CS) we cannot combine it with the previous
927 * stalling flush as the hardware documentation suggests, because that
928 * would cause the CS to stall on previous rendering *after* RO
929 * invalidation and wouldn't prevent the RO caches from being polluted by
930 * concurrent rendering before the stall completes. This intentionally
931 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
932 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
933 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
934 * already guarantee that there is no concurrent GPGPU kernel execution
935 * (see SKL HSD 2132585).
936 */
937 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
938 pc.TextureCacheInvalidationEnable = true;
939 pc.ConstantCacheInvalidationEnable = true;
940 pc.InstructionCacheInvalidateEnable = true;
941 pc.StateCacheInvalidationEnable = true;
942 pc.PostSyncOperation = NoWrite;
943 }
944
945 /* Now send a third stalling flush to make sure that invalidation is
946 * complete when the L3 configuration registers are modified.
947 */
948 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
949 pc.DCFlushEnable = true;
950 pc.PostSyncOperation = NoWrite;
951 pc.CommandStreamerStallEnable = true;
952 }
953
954 #if GEN_GEN >= 8
955
956 assert(!cfg->n[GEN_L3P_IS] && !cfg->n[GEN_L3P_C] && !cfg->n[GEN_L3P_T]);
957
958 uint32_t l3cr;
959 anv_pack_struct(&l3cr, GENX(L3CNTLREG),
960 .SLMEnable = has_slm,
961 .URBAllocation = cfg->n[GEN_L3P_URB],
962 .ROAllocation = cfg->n[GEN_L3P_RO],
963 .DCAllocation = cfg->n[GEN_L3P_DC],
964 .AllAllocation = cfg->n[GEN_L3P_ALL]);
965
966 /* Set up the L3 partitioning. */
967 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG_num), l3cr);
968
969 #else
970
971 const bool has_dc = cfg->n[GEN_L3P_DC] || cfg->n[GEN_L3P_ALL];
972 const bool has_is = cfg->n[GEN_L3P_IS] || cfg->n[GEN_L3P_RO] ||
973 cfg->n[GEN_L3P_ALL];
974 const bool has_c = cfg->n[GEN_L3P_C] || cfg->n[GEN_L3P_RO] ||
975 cfg->n[GEN_L3P_ALL];
976 const bool has_t = cfg->n[GEN_L3P_T] || cfg->n[GEN_L3P_RO] ||
977 cfg->n[GEN_L3P_ALL];
978
979 assert(!cfg->n[GEN_L3P_ALL]);
980
981 /* When enabled SLM only uses a portion of the L3 on half of the banks,
982 * the matching space on the remaining banks has to be allocated to a
983 * client (URB for all validated configurations) set to the
984 * lower-bandwidth 2-bank address hashing mode.
985 */
986 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
987 const bool urb_low_bw = has_slm && !devinfo->is_baytrail;
988 assert(!urb_low_bw || cfg->n[GEN_L3P_URB] == cfg->n[GEN_L3P_SLM]);
989
990 /* Minimum number of ways that can be allocated to the URB. */
991 MAYBE_UNUSED const unsigned n0_urb = devinfo->is_baytrail ? 32 : 0;
992 assert(cfg->n[GEN_L3P_URB] >= n0_urb);
993
994 uint32_t l3sqcr1, l3cr2, l3cr3;
995 anv_pack_struct(&l3sqcr1, GENX(L3SQCREG1),
996 .ConvertDC_UC = !has_dc,
997 .ConvertIS_UC = !has_is,
998 .ConvertC_UC = !has_c,
999 .ConvertT_UC = !has_t);
1000 l3sqcr1 |=
1001 GEN_IS_HASWELL ? HSW_L3SQCREG1_SQGHPCI_DEFAULT :
1002 devinfo->is_baytrail ? VLV_L3SQCREG1_SQGHPCI_DEFAULT :
1003 IVB_L3SQCREG1_SQGHPCI_DEFAULT;
1004
1005 anv_pack_struct(&l3cr2, GENX(L3CNTLREG2),
1006 .SLMEnable = has_slm,
1007 .URBLowBandwidth = urb_low_bw,
1008 .URBAllocation = cfg->n[GEN_L3P_URB] - n0_urb,
1009 #if !GEN_IS_HASWELL
1010 .ALLAllocation = cfg->n[GEN_L3P_ALL],
1011 #endif
1012 .ROAllocation = cfg->n[GEN_L3P_RO],
1013 .DCAllocation = cfg->n[GEN_L3P_DC]);
1014
1015 anv_pack_struct(&l3cr3, GENX(L3CNTLREG3),
1016 .ISAllocation = cfg->n[GEN_L3P_IS],
1017 .ISLowBandwidth = 0,
1018 .CAllocation = cfg->n[GEN_L3P_C],
1019 .CLowBandwidth = 0,
1020 .TAllocation = cfg->n[GEN_L3P_T],
1021 .TLowBandwidth = 0);
1022
1023 /* Set up the L3 partitioning. */
1024 emit_lri(&cmd_buffer->batch, GENX(L3SQCREG1_num), l3sqcr1);
1025 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG2_num), l3cr2);
1026 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG3_num), l3cr3);
1027
1028 #if GEN_IS_HASWELL
1029 if (cmd_buffer->device->instance->physicalDevice.cmd_parser_version >= 4) {
1030 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
1031 * them disabled to avoid crashing the system hard.
1032 */
1033 uint32_t scratch1, chicken3;
1034 anv_pack_struct(&scratch1, GENX(SCRATCH1),
1035 .L3AtomicDisable = !has_dc);
1036 anv_pack_struct(&chicken3, GENX(CHICKEN3),
1037 .L3AtomicDisableMask = true,
1038 .L3AtomicDisable = !has_dc);
1039 emit_lri(&cmd_buffer->batch, GENX(SCRATCH1_num), scratch1);
1040 emit_lri(&cmd_buffer->batch, GENX(CHICKEN3_num), chicken3);
1041 }
1042 #endif
1043
1044 #endif
1045
1046 cmd_buffer->state.current_l3_config = cfg;
1047 }
1048
1049 void
1050 genX(cmd_buffer_apply_pipe_flushes)(struct anv_cmd_buffer *cmd_buffer)
1051 {
1052 enum anv_pipe_bits bits = cmd_buffer->state.pending_pipe_bits;
1053
1054 /* Flushes are pipelined while invalidations are handled immediately.
1055 * Therefore, if we're flushing anything then we need to schedule a stall
1056 * before any invalidations can happen.
1057 */
1058 if (bits & ANV_PIPE_FLUSH_BITS)
1059 bits |= ANV_PIPE_NEEDS_CS_STALL_BIT;
1060
1061 /* If we're going to do an invalidate and we have a pending CS stall that
1062 * has yet to be resolved, we do the CS stall now.
1063 */
1064 if ((bits & ANV_PIPE_INVALIDATE_BITS) &&
1065 (bits & ANV_PIPE_NEEDS_CS_STALL_BIT)) {
1066 bits |= ANV_PIPE_CS_STALL_BIT;
1067 bits &= ~ANV_PIPE_NEEDS_CS_STALL_BIT;
1068 }
1069
1070 if (bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT)) {
1071 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
1072 pipe.DepthCacheFlushEnable = bits & ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
1073 pipe.DCFlushEnable = bits & ANV_PIPE_DATA_CACHE_FLUSH_BIT;
1074 pipe.RenderTargetCacheFlushEnable =
1075 bits & ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
1076
1077 pipe.DepthStallEnable = bits & ANV_PIPE_DEPTH_STALL_BIT;
1078 pipe.CommandStreamerStallEnable = bits & ANV_PIPE_CS_STALL_BIT;
1079 pipe.StallAtPixelScoreboard = bits & ANV_PIPE_STALL_AT_SCOREBOARD_BIT;
1080
1081 /*
1082 * According to the Broadwell documentation, any PIPE_CONTROL with the
1083 * "Command Streamer Stall" bit set must also have another bit set,
1084 * with five different options:
1085 *
1086 * - Render Target Cache Flush
1087 * - Depth Cache Flush
1088 * - Stall at Pixel Scoreboard
1089 * - Post-Sync Operation
1090 * - Depth Stall
1091 * - DC Flush Enable
1092 *
1093 * I chose "Stall at Pixel Scoreboard" since that's what we use in
1094 * mesa and it seems to work fine. The choice is fairly arbitrary.
1095 */
1096 if ((bits & ANV_PIPE_CS_STALL_BIT) &&
1097 !(bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_DEPTH_STALL_BIT |
1098 ANV_PIPE_STALL_AT_SCOREBOARD_BIT)))
1099 pipe.StallAtPixelScoreboard = true;
1100 }
1101
1102 bits &= ~(ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT);
1103 }
1104
1105 if (bits & ANV_PIPE_INVALIDATE_BITS) {
1106 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
1107 pipe.StateCacheInvalidationEnable =
1108 bits & ANV_PIPE_STATE_CACHE_INVALIDATE_BIT;
1109 pipe.ConstantCacheInvalidationEnable =
1110 bits & ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT;
1111 pipe.VFCacheInvalidationEnable =
1112 bits & ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1113 pipe.TextureCacheInvalidationEnable =
1114 bits & ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
1115 pipe.InstructionCacheInvalidateEnable =
1116 bits & ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT;
1117 }
1118
1119 bits &= ~ANV_PIPE_INVALIDATE_BITS;
1120 }
1121
1122 cmd_buffer->state.pending_pipe_bits = bits;
1123 }
1124
1125 void genX(CmdPipelineBarrier)(
1126 VkCommandBuffer commandBuffer,
1127 VkPipelineStageFlags srcStageMask,
1128 VkPipelineStageFlags destStageMask,
1129 VkBool32 byRegion,
1130 uint32_t memoryBarrierCount,
1131 const VkMemoryBarrier* pMemoryBarriers,
1132 uint32_t bufferMemoryBarrierCount,
1133 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1134 uint32_t imageMemoryBarrierCount,
1135 const VkImageMemoryBarrier* pImageMemoryBarriers)
1136 {
1137 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1138
1139 /* XXX: Right now, we're really dumb and just flush whatever categories
1140 * the app asks for. One of these days we may make this a bit better
1141 * but right now that's all the hardware allows for in most areas.
1142 */
1143 VkAccessFlags src_flags = 0;
1144 VkAccessFlags dst_flags = 0;
1145
1146 for (uint32_t i = 0; i < memoryBarrierCount; i++) {
1147 src_flags |= pMemoryBarriers[i].srcAccessMask;
1148 dst_flags |= pMemoryBarriers[i].dstAccessMask;
1149 }
1150
1151 for (uint32_t i = 0; i < bufferMemoryBarrierCount; i++) {
1152 src_flags |= pBufferMemoryBarriers[i].srcAccessMask;
1153 dst_flags |= pBufferMemoryBarriers[i].dstAccessMask;
1154 }
1155
1156 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1157 src_flags |= pImageMemoryBarriers[i].srcAccessMask;
1158 dst_flags |= pImageMemoryBarriers[i].dstAccessMask;
1159 ANV_FROM_HANDLE(anv_image, image, pImageMemoryBarriers[i].image);
1160 const VkImageSubresourceRange *range =
1161 &pImageMemoryBarriers[i].subresourceRange;
1162
1163 if (range->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1164 transition_depth_buffer(cmd_buffer, image,
1165 pImageMemoryBarriers[i].oldLayout,
1166 pImageMemoryBarriers[i].newLayout);
1167 } else if (range->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
1168 transition_color_buffer(cmd_buffer, image,
1169 range->baseMipLevel,
1170 anv_get_levelCount(image, range),
1171 range->baseArrayLayer,
1172 anv_get_layerCount(image, range),
1173 pImageMemoryBarriers[i].oldLayout,
1174 pImageMemoryBarriers[i].newLayout);
1175 }
1176 }
1177
1178 cmd_buffer->state.pending_pipe_bits |=
1179 anv_pipe_flush_bits_for_access_flags(src_flags) |
1180 anv_pipe_invalidate_bits_for_access_flags(dst_flags);
1181 }
1182
1183 static void
1184 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer *cmd_buffer)
1185 {
1186 VkShaderStageFlags stages = cmd_buffer->state.pipeline->active_stages;
1187
1188 /* In order to avoid thrash, we assume that vertex and fragment stages
1189 * always exist. In the rare case where one is missing *and* the other
1190 * uses push concstants, this may be suboptimal. However, avoiding stalls
1191 * seems more important.
1192 */
1193 stages |= VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT;
1194
1195 if (stages == cmd_buffer->state.push_constant_stages)
1196 return;
1197
1198 #if GEN_GEN >= 8
1199 const unsigned push_constant_kb = 32;
1200 #elif GEN_IS_HASWELL
1201 const unsigned push_constant_kb = cmd_buffer->device->info.gt == 3 ? 32 : 16;
1202 #else
1203 const unsigned push_constant_kb = 16;
1204 #endif
1205
1206 const unsigned num_stages =
1207 _mesa_bitcount(stages & VK_SHADER_STAGE_ALL_GRAPHICS);
1208 unsigned size_per_stage = push_constant_kb / num_stages;
1209
1210 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
1211 * units of 2KB. Incidentally, these are the same platforms that have
1212 * 32KB worth of push constant space.
1213 */
1214 if (push_constant_kb == 32)
1215 size_per_stage &= ~1u;
1216
1217 uint32_t kb_used = 0;
1218 for (int i = MESA_SHADER_VERTEX; i < MESA_SHADER_FRAGMENT; i++) {
1219 unsigned push_size = (stages & (1 << i)) ? size_per_stage : 0;
1220 anv_batch_emit(&cmd_buffer->batch,
1221 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
1222 alloc._3DCommandSubOpcode = 18 + i;
1223 alloc.ConstantBufferOffset = (push_size > 0) ? kb_used : 0;
1224 alloc.ConstantBufferSize = push_size;
1225 }
1226 kb_used += push_size;
1227 }
1228
1229 anv_batch_emit(&cmd_buffer->batch,
1230 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS), alloc) {
1231 alloc.ConstantBufferOffset = kb_used;
1232 alloc.ConstantBufferSize = push_constant_kb - kb_used;
1233 }
1234
1235 cmd_buffer->state.push_constant_stages = stages;
1236
1237 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
1238 *
1239 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
1240 * the next 3DPRIMITIVE command after programming the
1241 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
1242 *
1243 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
1244 * pipeline setup, we need to dirty push constants.
1245 */
1246 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_ALL_GRAPHICS;
1247 }
1248
1249 static VkResult
1250 emit_binding_table(struct anv_cmd_buffer *cmd_buffer,
1251 gl_shader_stage stage,
1252 struct anv_state *bt_state)
1253 {
1254 struct anv_subpass *subpass = cmd_buffer->state.subpass;
1255 struct anv_pipeline *pipeline;
1256 uint32_t bias, state_offset;
1257
1258 switch (stage) {
1259 case MESA_SHADER_COMPUTE:
1260 pipeline = cmd_buffer->state.compute_pipeline;
1261 bias = 1;
1262 break;
1263 default:
1264 pipeline = cmd_buffer->state.pipeline;
1265 bias = 0;
1266 break;
1267 }
1268
1269 if (!anv_pipeline_has_stage(pipeline, stage)) {
1270 *bt_state = (struct anv_state) { 0, };
1271 return VK_SUCCESS;
1272 }
1273
1274 struct anv_pipeline_bind_map *map = &pipeline->shaders[stage]->bind_map;
1275 if (bias + map->surface_count == 0) {
1276 *bt_state = (struct anv_state) { 0, };
1277 return VK_SUCCESS;
1278 }
1279
1280 *bt_state = anv_cmd_buffer_alloc_binding_table(cmd_buffer,
1281 bias + map->surface_count,
1282 &state_offset);
1283 uint32_t *bt_map = bt_state->map;
1284
1285 if (bt_state->map == NULL)
1286 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
1287
1288 if (stage == MESA_SHADER_COMPUTE &&
1289 get_cs_prog_data(cmd_buffer->state.compute_pipeline)->uses_num_work_groups) {
1290 struct anv_bo *bo = cmd_buffer->state.num_workgroups_bo;
1291 uint32_t bo_offset = cmd_buffer->state.num_workgroups_offset;
1292
1293 struct anv_state surface_state;
1294 surface_state =
1295 anv_cmd_buffer_alloc_surface_state(cmd_buffer);
1296
1297 const enum isl_format format =
1298 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
1299 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
1300 format, bo_offset, 12, 1);
1301
1302 bt_map[0] = surface_state.offset + state_offset;
1303 add_surface_state_reloc(cmd_buffer, surface_state, bo, bo_offset);
1304 }
1305
1306 if (map->surface_count == 0)
1307 goto out;
1308
1309 if (map->image_count > 0) {
1310 VkResult result =
1311 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, stage, images);
1312 if (result != VK_SUCCESS)
1313 return result;
1314
1315 cmd_buffer->state.push_constants_dirty |= 1 << stage;
1316 }
1317
1318 uint32_t image = 0;
1319 for (uint32_t s = 0; s < map->surface_count; s++) {
1320 struct anv_pipeline_binding *binding = &map->surface_to_descriptor[s];
1321
1322 struct anv_state surface_state;
1323
1324 if (binding->set == ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS) {
1325 /* Color attachment binding */
1326 assert(stage == MESA_SHADER_FRAGMENT);
1327 assert(binding->binding == 0);
1328 if (binding->index < subpass->color_count) {
1329 const unsigned att =
1330 subpass->color_attachments[binding->index].attachment;
1331
1332 /* From the Vulkan 1.0.46 spec:
1333 *
1334 * "If any color or depth/stencil attachments are
1335 * VK_ATTACHMENT_UNUSED, then no writes occur for those
1336 * attachments."
1337 */
1338 if (att == VK_ATTACHMENT_UNUSED) {
1339 surface_state = cmd_buffer->state.null_surface_state;
1340 } else {
1341 surface_state = cmd_buffer->state.attachments[att].color_rt_state;
1342 }
1343 } else {
1344 surface_state = cmd_buffer->state.null_surface_state;
1345 }
1346
1347 bt_map[bias + s] = surface_state.offset + state_offset;
1348 continue;
1349 }
1350
1351 struct anv_descriptor_set *set =
1352 cmd_buffer->state.descriptors[binding->set];
1353 uint32_t offset = set->layout->binding[binding->binding].descriptor_index;
1354 struct anv_descriptor *desc = &set->descriptors[offset + binding->index];
1355
1356 switch (desc->type) {
1357 case VK_DESCRIPTOR_TYPE_SAMPLER:
1358 /* Nothing for us to do here */
1359 continue;
1360
1361 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1362 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1363 surface_state = desc->aux_usage == ISL_AUX_USAGE_NONE ?
1364 desc->image_view->no_aux_sampler_surface_state :
1365 desc->image_view->sampler_surface_state;
1366 assert(surface_state.alloc_size);
1367 add_image_relocs(cmd_buffer, desc->image_view->image,
1368 desc->image_view->aspect_mask,
1369 desc->aux_usage, surface_state);
1370 break;
1371 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1372 assert(stage == MESA_SHADER_FRAGMENT);
1373 if (desc->image_view->aspect_mask != VK_IMAGE_ASPECT_COLOR_BIT) {
1374 /* For depth and stencil input attachments, we treat it like any
1375 * old texture that a user may have bound.
1376 */
1377 surface_state = desc->aux_usage == ISL_AUX_USAGE_NONE ?
1378 desc->image_view->no_aux_sampler_surface_state :
1379 desc->image_view->sampler_surface_state;
1380 assert(surface_state.alloc_size);
1381 add_image_relocs(cmd_buffer, desc->image_view->image,
1382 desc->image_view->aspect_mask,
1383 desc->aux_usage, surface_state);
1384 } else {
1385 /* For color input attachments, we create the surface state at
1386 * vkBeginRenderPass time so that we can include aux and clear
1387 * color information.
1388 */
1389 assert(binding->input_attachment_index < subpass->input_count);
1390 const unsigned subpass_att = binding->input_attachment_index;
1391 const unsigned att = subpass->input_attachments[subpass_att].attachment;
1392 surface_state = cmd_buffer->state.attachments[att].input_att_state;
1393 }
1394 break;
1395
1396 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1397 surface_state = (binding->write_only)
1398 ? desc->image_view->writeonly_storage_surface_state
1399 : desc->image_view->storage_surface_state;
1400 assert(surface_state.alloc_size);
1401 add_image_relocs(cmd_buffer, desc->image_view->image,
1402 desc->image_view->aspect_mask,
1403 desc->image_view->image->aux_usage, surface_state);
1404
1405 struct brw_image_param *image_param =
1406 &cmd_buffer->state.push_constants[stage]->images[image++];
1407
1408 *image_param = desc->image_view->storage_image_param;
1409 image_param->surface_idx = bias + s;
1410 break;
1411 }
1412
1413 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1414 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1415 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1416 surface_state = desc->buffer_view->surface_state;
1417 assert(surface_state.alloc_size);
1418 add_surface_state_reloc(cmd_buffer, surface_state,
1419 desc->buffer_view->bo,
1420 desc->buffer_view->offset);
1421 break;
1422
1423 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1424 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1425 uint32_t dynamic_offset_idx =
1426 pipeline->layout->set[binding->set].dynamic_offset_start +
1427 set->layout->binding[binding->binding].dynamic_offset_index +
1428 binding->index;
1429
1430 /* Compute the offset within the buffer */
1431 uint64_t offset = desc->offset +
1432 cmd_buffer->state.dynamic_offsets[dynamic_offset_idx];
1433 /* Clamp to the buffer size */
1434 offset = MIN2(offset, desc->buffer->size);
1435 /* Clamp the range to the buffer size */
1436 uint32_t range = MIN2(desc->range, desc->buffer->size - offset);
1437
1438 surface_state =
1439 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
1440 enum isl_format format =
1441 anv_isl_format_for_descriptor_type(desc->type);
1442
1443 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
1444 format, offset, range, 1);
1445 add_surface_state_reloc(cmd_buffer, surface_state,
1446 desc->buffer->bo,
1447 desc->buffer->offset + offset);
1448 break;
1449 }
1450
1451 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1452 surface_state = (binding->write_only)
1453 ? desc->buffer_view->writeonly_storage_surface_state
1454 : desc->buffer_view->storage_surface_state;
1455 assert(surface_state.alloc_size);
1456 add_surface_state_reloc(cmd_buffer, surface_state,
1457 desc->buffer_view->bo,
1458 desc->buffer_view->offset);
1459
1460 struct brw_image_param *image_param =
1461 &cmd_buffer->state.push_constants[stage]->images[image++];
1462
1463 *image_param = desc->buffer_view->storage_image_param;
1464 image_param->surface_idx = bias + s;
1465 break;
1466
1467 default:
1468 assert(!"Invalid descriptor type");
1469 continue;
1470 }
1471
1472 bt_map[bias + s] = surface_state.offset + state_offset;
1473 }
1474 assert(image == map->image_count);
1475
1476 out:
1477 anv_state_flush(cmd_buffer->device, *bt_state);
1478
1479 return VK_SUCCESS;
1480 }
1481
1482 static VkResult
1483 emit_samplers(struct anv_cmd_buffer *cmd_buffer,
1484 gl_shader_stage stage,
1485 struct anv_state *state)
1486 {
1487 struct anv_pipeline *pipeline;
1488
1489 if (stage == MESA_SHADER_COMPUTE)
1490 pipeline = cmd_buffer->state.compute_pipeline;
1491 else
1492 pipeline = cmd_buffer->state.pipeline;
1493
1494 if (!anv_pipeline_has_stage(pipeline, stage)) {
1495 *state = (struct anv_state) { 0, };
1496 return VK_SUCCESS;
1497 }
1498
1499 struct anv_pipeline_bind_map *map = &pipeline->shaders[stage]->bind_map;
1500 if (map->sampler_count == 0) {
1501 *state = (struct anv_state) { 0, };
1502 return VK_SUCCESS;
1503 }
1504
1505 uint32_t size = map->sampler_count * 16;
1506 *state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, 32);
1507
1508 if (state->map == NULL)
1509 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
1510
1511 for (uint32_t s = 0; s < map->sampler_count; s++) {
1512 struct anv_pipeline_binding *binding = &map->sampler_to_descriptor[s];
1513 struct anv_descriptor_set *set =
1514 cmd_buffer->state.descriptors[binding->set];
1515 uint32_t offset = set->layout->binding[binding->binding].descriptor_index;
1516 struct anv_descriptor *desc = &set->descriptors[offset + binding->index];
1517
1518 if (desc->type != VK_DESCRIPTOR_TYPE_SAMPLER &&
1519 desc->type != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
1520 continue;
1521
1522 struct anv_sampler *sampler = desc->sampler;
1523
1524 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
1525 * happens to be zero.
1526 */
1527 if (sampler == NULL)
1528 continue;
1529
1530 memcpy(state->map + (s * 16),
1531 sampler->state, sizeof(sampler->state));
1532 }
1533
1534 anv_state_flush(cmd_buffer->device, *state);
1535
1536 return VK_SUCCESS;
1537 }
1538
1539 static uint32_t
1540 flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer)
1541 {
1542 VkShaderStageFlags dirty = cmd_buffer->state.descriptors_dirty &
1543 cmd_buffer->state.pipeline->active_stages;
1544
1545 VkResult result = VK_SUCCESS;
1546 anv_foreach_stage(s, dirty) {
1547 result = emit_samplers(cmd_buffer, s, &cmd_buffer->state.samplers[s]);
1548 if (result != VK_SUCCESS)
1549 break;
1550 result = emit_binding_table(cmd_buffer, s,
1551 &cmd_buffer->state.binding_tables[s]);
1552 if (result != VK_SUCCESS)
1553 break;
1554 }
1555
1556 if (result != VK_SUCCESS) {
1557 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
1558
1559 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
1560 if (result != VK_SUCCESS)
1561 return 0;
1562
1563 /* Re-emit state base addresses so we get the new surface state base
1564 * address before we start emitting binding tables etc.
1565 */
1566 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
1567
1568 /* Re-emit all active binding tables */
1569 dirty |= cmd_buffer->state.pipeline->active_stages;
1570 anv_foreach_stage(s, dirty) {
1571 result = emit_samplers(cmd_buffer, s, &cmd_buffer->state.samplers[s]);
1572 if (result != VK_SUCCESS) {
1573 anv_batch_set_error(&cmd_buffer->batch, result);
1574 return 0;
1575 }
1576 result = emit_binding_table(cmd_buffer, s,
1577 &cmd_buffer->state.binding_tables[s]);
1578 if (result != VK_SUCCESS) {
1579 anv_batch_set_error(&cmd_buffer->batch, result);
1580 return 0;
1581 }
1582 }
1583 }
1584
1585 cmd_buffer->state.descriptors_dirty &= ~dirty;
1586
1587 return dirty;
1588 }
1589
1590 static void
1591 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer *cmd_buffer,
1592 uint32_t stages)
1593 {
1594 static const uint32_t sampler_state_opcodes[] = {
1595 [MESA_SHADER_VERTEX] = 43,
1596 [MESA_SHADER_TESS_CTRL] = 44, /* HS */
1597 [MESA_SHADER_TESS_EVAL] = 45, /* DS */
1598 [MESA_SHADER_GEOMETRY] = 46,
1599 [MESA_SHADER_FRAGMENT] = 47,
1600 [MESA_SHADER_COMPUTE] = 0,
1601 };
1602
1603 static const uint32_t binding_table_opcodes[] = {
1604 [MESA_SHADER_VERTEX] = 38,
1605 [MESA_SHADER_TESS_CTRL] = 39,
1606 [MESA_SHADER_TESS_EVAL] = 40,
1607 [MESA_SHADER_GEOMETRY] = 41,
1608 [MESA_SHADER_FRAGMENT] = 42,
1609 [MESA_SHADER_COMPUTE] = 0,
1610 };
1611
1612 anv_foreach_stage(s, stages) {
1613 if (cmd_buffer->state.samplers[s].alloc_size > 0) {
1614 anv_batch_emit(&cmd_buffer->batch,
1615 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ssp) {
1616 ssp._3DCommandSubOpcode = sampler_state_opcodes[s];
1617 ssp.PointertoVSSamplerState = cmd_buffer->state.samplers[s].offset;
1618 }
1619 }
1620
1621 /* Always emit binding table pointers if we're asked to, since on SKL
1622 * this is what flushes push constants. */
1623 anv_batch_emit(&cmd_buffer->batch,
1624 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), btp) {
1625 btp._3DCommandSubOpcode = binding_table_opcodes[s];
1626 btp.PointertoVSBindingTable = cmd_buffer->state.binding_tables[s].offset;
1627 }
1628 }
1629 }
1630
1631 static uint32_t
1632 cmd_buffer_flush_push_constants(struct anv_cmd_buffer *cmd_buffer)
1633 {
1634 static const uint32_t push_constant_opcodes[] = {
1635 [MESA_SHADER_VERTEX] = 21,
1636 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
1637 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
1638 [MESA_SHADER_GEOMETRY] = 22,
1639 [MESA_SHADER_FRAGMENT] = 23,
1640 [MESA_SHADER_COMPUTE] = 0,
1641 };
1642
1643 VkShaderStageFlags flushed = 0;
1644
1645 anv_foreach_stage(stage, cmd_buffer->state.push_constants_dirty) {
1646 if (stage == MESA_SHADER_COMPUTE)
1647 continue;
1648
1649 struct anv_state state = anv_cmd_buffer_push_constants(cmd_buffer, stage);
1650
1651 if (state.offset == 0) {
1652 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CONSTANT_VS), c)
1653 c._3DCommandSubOpcode = push_constant_opcodes[stage];
1654 } else {
1655 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CONSTANT_VS), c) {
1656 c._3DCommandSubOpcode = push_constant_opcodes[stage],
1657 c.ConstantBody = (struct GENX(3DSTATE_CONSTANT_BODY)) {
1658 #if GEN_GEN >= 9
1659 .Buffer[2] = { &cmd_buffer->device->dynamic_state_pool.block_pool.bo, state.offset },
1660 .ReadLength[2] = DIV_ROUND_UP(state.alloc_size, 32),
1661 #else
1662 .Buffer[0] = { .offset = state.offset },
1663 .ReadLength[0] = DIV_ROUND_UP(state.alloc_size, 32),
1664 #endif
1665 };
1666 }
1667 }
1668
1669 flushed |= mesa_to_vk_shader_stage(stage);
1670 }
1671
1672 cmd_buffer->state.push_constants_dirty &= ~VK_SHADER_STAGE_ALL_GRAPHICS;
1673
1674 return flushed;
1675 }
1676
1677 void
1678 genX(cmd_buffer_flush_state)(struct anv_cmd_buffer *cmd_buffer)
1679 {
1680 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
1681 uint32_t *p;
1682
1683 uint32_t vb_emit = cmd_buffer->state.vb_dirty & pipeline->vb_used;
1684
1685 assert((pipeline->active_stages & VK_SHADER_STAGE_COMPUTE_BIT) == 0);
1686
1687 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
1688
1689 genX(flush_pipeline_select_3d)(cmd_buffer);
1690
1691 if (vb_emit) {
1692 const uint32_t num_buffers = __builtin_popcount(vb_emit);
1693 const uint32_t num_dwords = 1 + num_buffers * 4;
1694
1695 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
1696 GENX(3DSTATE_VERTEX_BUFFERS));
1697 uint32_t vb, i = 0;
1698 for_each_bit(vb, vb_emit) {
1699 struct anv_buffer *buffer = cmd_buffer->state.vertex_bindings[vb].buffer;
1700 uint32_t offset = cmd_buffer->state.vertex_bindings[vb].offset;
1701
1702 struct GENX(VERTEX_BUFFER_STATE) state = {
1703 .VertexBufferIndex = vb,
1704
1705 #if GEN_GEN >= 8
1706 .MemoryObjectControlState = GENX(MOCS),
1707 #else
1708 .BufferAccessType = pipeline->instancing_enable[vb] ? INSTANCEDATA : VERTEXDATA,
1709 /* Our implementation of VK_KHR_multiview uses instancing to draw
1710 * the different views. If the client asks for instancing, we
1711 * need to use the Instance Data Step Rate to ensure that we
1712 * repeat the client's per-instance data once for each view.
1713 */
1714 .InstanceDataStepRate = anv_subpass_view_count(pipeline->subpass),
1715 .VertexBufferMemoryObjectControlState = GENX(MOCS),
1716 #endif
1717
1718 .AddressModifyEnable = true,
1719 .BufferPitch = pipeline->binding_stride[vb],
1720 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
1721
1722 #if GEN_GEN >= 8
1723 .BufferSize = buffer->size - offset
1724 #else
1725 .EndAddress = { buffer->bo, buffer->offset + buffer->size - 1},
1726 #endif
1727 };
1728
1729 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, &p[1 + i * 4], &state);
1730 i++;
1731 }
1732 }
1733
1734 cmd_buffer->state.vb_dirty &= ~vb_emit;
1735
1736 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_PIPELINE) {
1737 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
1738
1739 /* The exact descriptor layout is pulled from the pipeline, so we need
1740 * to re-emit binding tables on every pipeline change.
1741 */
1742 cmd_buffer->state.descriptors_dirty |=
1743 cmd_buffer->state.pipeline->active_stages;
1744
1745 /* If the pipeline changed, we may need to re-allocate push constant
1746 * space in the URB.
1747 */
1748 cmd_buffer_alloc_push_constants(cmd_buffer);
1749 }
1750
1751 #if GEN_GEN <= 7
1752 if (cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_VERTEX_BIT ||
1753 cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_VERTEX_BIT) {
1754 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
1755 *
1756 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
1757 * stall needs to be sent just prior to any 3DSTATE_VS,
1758 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
1759 * 3DSTATE_BINDING_TABLE_POINTER_VS,
1760 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
1761 * PIPE_CONTROL needs to be sent before any combination of VS
1762 * associated 3DSTATE."
1763 */
1764 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1765 pc.DepthStallEnable = true;
1766 pc.PostSyncOperation = WriteImmediateData;
1767 pc.Address =
1768 (struct anv_address) { &cmd_buffer->device->workaround_bo, 0 };
1769 }
1770 }
1771 #endif
1772
1773 /* Render targets live in the same binding table as fragment descriptors */
1774 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_RENDER_TARGETS)
1775 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_FRAGMENT_BIT;
1776
1777 /* We emit the binding tables and sampler tables first, then emit push
1778 * constants and then finally emit binding table and sampler table
1779 * pointers. It has to happen in this order, since emitting the binding
1780 * tables may change the push constants (in case of storage images). After
1781 * emitting push constants, on SKL+ we have to emit the corresponding
1782 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
1783 */
1784 uint32_t dirty = 0;
1785 if (cmd_buffer->state.descriptors_dirty)
1786 dirty = flush_descriptor_sets(cmd_buffer);
1787
1788 if (cmd_buffer->state.push_constants_dirty) {
1789 #if GEN_GEN >= 9
1790 /* On Sky Lake and later, the binding table pointers commands are
1791 * what actually flush the changes to push constant state so we need
1792 * to dirty them so they get re-emitted below.
1793 */
1794 dirty |= cmd_buffer_flush_push_constants(cmd_buffer);
1795 #else
1796 cmd_buffer_flush_push_constants(cmd_buffer);
1797 #endif
1798 }
1799
1800 if (dirty)
1801 cmd_buffer_emit_descriptor_pointers(cmd_buffer, dirty);
1802
1803 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_DYNAMIC_VIEWPORT)
1804 gen8_cmd_buffer_emit_viewport(cmd_buffer);
1805
1806 if (cmd_buffer->state.dirty & (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT |
1807 ANV_CMD_DIRTY_PIPELINE)) {
1808 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer,
1809 pipeline->depth_clamp_enable);
1810 }
1811
1812 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_DYNAMIC_SCISSOR)
1813 gen7_cmd_buffer_emit_scissor(cmd_buffer);
1814
1815 genX(cmd_buffer_flush_dynamic_state)(cmd_buffer);
1816
1817 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
1818 }
1819
1820 static void
1821 emit_vertex_bo(struct anv_cmd_buffer *cmd_buffer,
1822 struct anv_bo *bo, uint32_t offset,
1823 uint32_t size, uint32_t index)
1824 {
1825 uint32_t *p = anv_batch_emitn(&cmd_buffer->batch, 5,
1826 GENX(3DSTATE_VERTEX_BUFFERS));
1827
1828 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, p + 1,
1829 &(struct GENX(VERTEX_BUFFER_STATE)) {
1830 .VertexBufferIndex = index,
1831 .AddressModifyEnable = true,
1832 .BufferPitch = 0,
1833 #if (GEN_GEN >= 8)
1834 .MemoryObjectControlState = GENX(MOCS),
1835 .BufferStartingAddress = { bo, offset },
1836 .BufferSize = size
1837 #else
1838 .VertexBufferMemoryObjectControlState = GENX(MOCS),
1839 .BufferStartingAddress = { bo, offset },
1840 .EndAddress = { bo, offset + size },
1841 #endif
1842 });
1843 }
1844
1845 static void
1846 emit_base_vertex_instance_bo(struct anv_cmd_buffer *cmd_buffer,
1847 struct anv_bo *bo, uint32_t offset)
1848 {
1849 emit_vertex_bo(cmd_buffer, bo, offset, 8, ANV_SVGS_VB_INDEX);
1850 }
1851
1852 static void
1853 emit_base_vertex_instance(struct anv_cmd_buffer *cmd_buffer,
1854 uint32_t base_vertex, uint32_t base_instance)
1855 {
1856 struct anv_state id_state =
1857 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 8, 4);
1858
1859 ((uint32_t *)id_state.map)[0] = base_vertex;
1860 ((uint32_t *)id_state.map)[1] = base_instance;
1861
1862 anv_state_flush(cmd_buffer->device, id_state);
1863
1864 emit_base_vertex_instance_bo(cmd_buffer,
1865 &cmd_buffer->device->dynamic_state_pool.block_pool.bo, id_state.offset);
1866 }
1867
1868 static void
1869 emit_draw_index(struct anv_cmd_buffer *cmd_buffer, uint32_t draw_index)
1870 {
1871 struct anv_state state =
1872 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 4, 4);
1873
1874 ((uint32_t *)state.map)[0] = draw_index;
1875
1876 anv_state_flush(cmd_buffer->device, state);
1877
1878 emit_vertex_bo(cmd_buffer,
1879 &cmd_buffer->device->dynamic_state_pool.block_pool.bo,
1880 state.offset, 4, ANV_DRAWID_VB_INDEX);
1881 }
1882
1883 void genX(CmdDraw)(
1884 VkCommandBuffer commandBuffer,
1885 uint32_t vertexCount,
1886 uint32_t instanceCount,
1887 uint32_t firstVertex,
1888 uint32_t firstInstance)
1889 {
1890 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1891 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
1892 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
1893
1894 if (anv_batch_has_error(&cmd_buffer->batch))
1895 return;
1896
1897 genX(cmd_buffer_flush_state)(cmd_buffer);
1898
1899 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
1900 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
1901 if (vs_prog_data->uses_drawid)
1902 emit_draw_index(cmd_buffer, 0);
1903
1904 /* Our implementation of VK_KHR_multiview uses instancing to draw the
1905 * different views. We need to multiply instanceCount by the view count.
1906 */
1907 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
1908
1909 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
1910 prim.VertexAccessType = SEQUENTIAL;
1911 prim.PrimitiveTopologyType = pipeline->topology;
1912 prim.VertexCountPerInstance = vertexCount;
1913 prim.StartVertexLocation = firstVertex;
1914 prim.InstanceCount = instanceCount;
1915 prim.StartInstanceLocation = firstInstance;
1916 prim.BaseVertexLocation = 0;
1917 }
1918 }
1919
1920 void genX(CmdDrawIndexed)(
1921 VkCommandBuffer commandBuffer,
1922 uint32_t indexCount,
1923 uint32_t instanceCount,
1924 uint32_t firstIndex,
1925 int32_t vertexOffset,
1926 uint32_t firstInstance)
1927 {
1928 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1929 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
1930 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
1931
1932 if (anv_batch_has_error(&cmd_buffer->batch))
1933 return;
1934
1935 genX(cmd_buffer_flush_state)(cmd_buffer);
1936
1937 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
1938 emit_base_vertex_instance(cmd_buffer, vertexOffset, firstInstance);
1939 if (vs_prog_data->uses_drawid)
1940 emit_draw_index(cmd_buffer, 0);
1941
1942 /* Our implementation of VK_KHR_multiview uses instancing to draw the
1943 * different views. We need to multiply instanceCount by the view count.
1944 */
1945 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
1946
1947 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
1948 prim.VertexAccessType = RANDOM;
1949 prim.PrimitiveTopologyType = pipeline->topology;
1950 prim.VertexCountPerInstance = indexCount;
1951 prim.StartVertexLocation = firstIndex;
1952 prim.InstanceCount = instanceCount;
1953 prim.StartInstanceLocation = firstInstance;
1954 prim.BaseVertexLocation = vertexOffset;
1955 }
1956 }
1957
1958 /* Auto-Draw / Indirect Registers */
1959 #define GEN7_3DPRIM_END_OFFSET 0x2420
1960 #define GEN7_3DPRIM_START_VERTEX 0x2430
1961 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
1962 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
1963 #define GEN7_3DPRIM_START_INSTANCE 0x243C
1964 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
1965
1966 /* MI_MATH only exists on Haswell+ */
1967 #if GEN_IS_HASWELL || GEN_GEN >= 8
1968
1969 static uint32_t
1970 mi_alu(uint32_t opcode, uint32_t op1, uint32_t op2)
1971 {
1972 struct GENX(MI_MATH_ALU_INSTRUCTION) instr = {
1973 .ALUOpcode = opcode,
1974 .Operand1 = op1,
1975 .Operand2 = op2,
1976 };
1977
1978 uint32_t dw;
1979 GENX(MI_MATH_ALU_INSTRUCTION_pack)(NULL, &dw, &instr);
1980
1981 return dw;
1982 }
1983
1984 #define CS_GPR(n) (0x2600 + (n) * 8)
1985
1986 /* Emit dwords to multiply GPR0 by N */
1987 static void
1988 build_alu_multiply_gpr0(uint32_t *dw, unsigned *dw_count, uint32_t N)
1989 {
1990 VK_OUTARRAY_MAKE(out, dw, dw_count);
1991
1992 #define append_alu(opcode, operand1, operand2) \
1993 vk_outarray_append(&out, alu_dw) *alu_dw = mi_alu(opcode, operand1, operand2)
1994
1995 assert(N > 0);
1996 unsigned top_bit = 31 - __builtin_clz(N);
1997 for (int i = top_bit - 1; i >= 0; i--) {
1998 /* We get our initial data in GPR0 and we write the final data out to
1999 * GPR0 but we use GPR1 as our scratch register.
2000 */
2001 unsigned src_reg = i == top_bit - 1 ? MI_ALU_REG0 : MI_ALU_REG1;
2002 unsigned dst_reg = i == 0 ? MI_ALU_REG0 : MI_ALU_REG1;
2003
2004 /* Shift the current value left by 1 */
2005 append_alu(MI_ALU_LOAD, MI_ALU_SRCA, src_reg);
2006 append_alu(MI_ALU_LOAD, MI_ALU_SRCB, src_reg);
2007 append_alu(MI_ALU_ADD, 0, 0);
2008
2009 if (N & (1 << i)) {
2010 /* Store ACCU to R1 and add R0 to R1 */
2011 append_alu(MI_ALU_STORE, MI_ALU_REG1, MI_ALU_ACCU);
2012 append_alu(MI_ALU_LOAD, MI_ALU_SRCA, MI_ALU_REG0);
2013 append_alu(MI_ALU_LOAD, MI_ALU_SRCB, MI_ALU_REG1);
2014 append_alu(MI_ALU_ADD, 0, 0);
2015 }
2016
2017 append_alu(MI_ALU_STORE, dst_reg, MI_ALU_ACCU);
2018 }
2019
2020 #undef append_alu
2021 }
2022
2023 static void
2024 emit_mul_gpr0(struct anv_batch *batch, uint32_t N)
2025 {
2026 uint32_t num_dwords;
2027 build_alu_multiply_gpr0(NULL, &num_dwords, N);
2028
2029 uint32_t *dw = anv_batch_emitn(batch, 1 + num_dwords, GENX(MI_MATH));
2030 build_alu_multiply_gpr0(dw + 1, &num_dwords, N);
2031 }
2032
2033 #endif /* GEN_IS_HASWELL || GEN_GEN >= 8 */
2034
2035 static void
2036 load_indirect_parameters(struct anv_cmd_buffer *cmd_buffer,
2037 struct anv_buffer *buffer, uint64_t offset,
2038 bool indexed)
2039 {
2040 struct anv_batch *batch = &cmd_buffer->batch;
2041 struct anv_bo *bo = buffer->bo;
2042 uint32_t bo_offset = buffer->offset + offset;
2043
2044 emit_lrm(batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
2045
2046 unsigned view_count = anv_subpass_view_count(cmd_buffer->state.subpass);
2047 if (view_count > 1) {
2048 #if GEN_IS_HASWELL || GEN_GEN >= 8
2049 emit_lrm(batch, CS_GPR(0), bo, bo_offset + 4);
2050 emit_mul_gpr0(batch, view_count);
2051 emit_lrr(batch, GEN7_3DPRIM_INSTANCE_COUNT, CS_GPR(0));
2052 #else
2053 anv_finishme("Multiview + indirect draw requires MI_MATH\n"
2054 "MI_MATH is not supported on Ivy Bridge");
2055 emit_lrm(batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
2056 #endif
2057 } else {
2058 emit_lrm(batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
2059 }
2060
2061 emit_lrm(batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
2062
2063 if (indexed) {
2064 emit_lrm(batch, GEN7_3DPRIM_BASE_VERTEX, bo, bo_offset + 12);
2065 emit_lrm(batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 16);
2066 } else {
2067 emit_lrm(batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 12);
2068 emit_lri(batch, GEN7_3DPRIM_BASE_VERTEX, 0);
2069 }
2070 }
2071
2072 void genX(CmdDrawIndirect)(
2073 VkCommandBuffer commandBuffer,
2074 VkBuffer _buffer,
2075 VkDeviceSize offset,
2076 uint32_t drawCount,
2077 uint32_t stride)
2078 {
2079 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2080 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
2081 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
2082 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2083
2084 if (anv_batch_has_error(&cmd_buffer->batch))
2085 return;
2086
2087 genX(cmd_buffer_flush_state)(cmd_buffer);
2088
2089 for (uint32_t i = 0; i < drawCount; i++) {
2090 struct anv_bo *bo = buffer->bo;
2091 uint32_t bo_offset = buffer->offset + offset;
2092
2093 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
2094 emit_base_vertex_instance_bo(cmd_buffer, bo, bo_offset + 8);
2095 if (vs_prog_data->uses_drawid)
2096 emit_draw_index(cmd_buffer, i);
2097
2098 load_indirect_parameters(cmd_buffer, buffer, offset, false);
2099
2100 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
2101 prim.IndirectParameterEnable = true;
2102 prim.VertexAccessType = SEQUENTIAL;
2103 prim.PrimitiveTopologyType = pipeline->topology;
2104 }
2105
2106 offset += stride;
2107 }
2108 }
2109
2110 void genX(CmdDrawIndexedIndirect)(
2111 VkCommandBuffer commandBuffer,
2112 VkBuffer _buffer,
2113 VkDeviceSize offset,
2114 uint32_t drawCount,
2115 uint32_t stride)
2116 {
2117 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2118 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
2119 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
2120 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2121
2122 if (anv_batch_has_error(&cmd_buffer->batch))
2123 return;
2124
2125 genX(cmd_buffer_flush_state)(cmd_buffer);
2126
2127 for (uint32_t i = 0; i < drawCount; i++) {
2128 struct anv_bo *bo = buffer->bo;
2129 uint32_t bo_offset = buffer->offset + offset;
2130
2131 /* TODO: We need to stomp base vertex to 0 somehow */
2132 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
2133 emit_base_vertex_instance_bo(cmd_buffer, bo, bo_offset + 12);
2134 if (vs_prog_data->uses_drawid)
2135 emit_draw_index(cmd_buffer, i);
2136
2137 load_indirect_parameters(cmd_buffer, buffer, offset, true);
2138
2139 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
2140 prim.IndirectParameterEnable = true;
2141 prim.VertexAccessType = RANDOM;
2142 prim.PrimitiveTopologyType = pipeline->topology;
2143 }
2144
2145 offset += stride;
2146 }
2147 }
2148
2149 static VkResult
2150 flush_compute_descriptor_set(struct anv_cmd_buffer *cmd_buffer)
2151 {
2152 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
2153 struct anv_state surfaces = { 0, }, samplers = { 0, };
2154 VkResult result;
2155
2156 result = emit_binding_table(cmd_buffer, MESA_SHADER_COMPUTE, &surfaces);
2157 if (result != VK_SUCCESS) {
2158 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
2159
2160 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
2161 if (result != VK_SUCCESS)
2162 return result;
2163
2164 /* Re-emit state base addresses so we get the new surface state base
2165 * address before we start emitting binding tables etc.
2166 */
2167 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
2168
2169 result = emit_binding_table(cmd_buffer, MESA_SHADER_COMPUTE, &surfaces);
2170 if (result != VK_SUCCESS) {
2171 anv_batch_set_error(&cmd_buffer->batch, result);
2172 return result;
2173 }
2174 }
2175
2176 result = emit_samplers(cmd_buffer, MESA_SHADER_COMPUTE, &samplers);
2177 if (result != VK_SUCCESS) {
2178 anv_batch_set_error(&cmd_buffer->batch, result);
2179 return result;
2180 }
2181
2182 uint32_t iface_desc_data_dw[GENX(INTERFACE_DESCRIPTOR_DATA_length)];
2183 struct GENX(INTERFACE_DESCRIPTOR_DATA) desc = {
2184 .BindingTablePointer = surfaces.offset,
2185 .SamplerStatePointer = samplers.offset,
2186 };
2187 GENX(INTERFACE_DESCRIPTOR_DATA_pack)(NULL, iface_desc_data_dw, &desc);
2188
2189 struct anv_state state =
2190 anv_cmd_buffer_merge_dynamic(cmd_buffer, iface_desc_data_dw,
2191 pipeline->interface_descriptor_data,
2192 GENX(INTERFACE_DESCRIPTOR_DATA_length),
2193 64);
2194
2195 uint32_t size = GENX(INTERFACE_DESCRIPTOR_DATA_length) * sizeof(uint32_t);
2196 anv_batch_emit(&cmd_buffer->batch,
2197 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD), mid) {
2198 mid.InterfaceDescriptorTotalLength = size;
2199 mid.InterfaceDescriptorDataStartAddress = state.offset;
2200 }
2201
2202 return VK_SUCCESS;
2203 }
2204
2205 void
2206 genX(cmd_buffer_flush_compute_state)(struct anv_cmd_buffer *cmd_buffer)
2207 {
2208 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
2209 MAYBE_UNUSED VkResult result;
2210
2211 assert(pipeline->active_stages == VK_SHADER_STAGE_COMPUTE_BIT);
2212
2213 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
2214
2215 genX(flush_pipeline_select_gpgpu)(cmd_buffer);
2216
2217 if (cmd_buffer->state.compute_dirty & ANV_CMD_DIRTY_PIPELINE) {
2218 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
2219 *
2220 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
2221 * the only bits that are changed are scoreboard related: Scoreboard
2222 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
2223 * these scoreboard related states, a MEDIA_STATE_FLUSH is
2224 * sufficient."
2225 */
2226 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
2227 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
2228
2229 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
2230 }
2231
2232 if ((cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_COMPUTE_BIT) ||
2233 (cmd_buffer->state.compute_dirty & ANV_CMD_DIRTY_PIPELINE)) {
2234 /* FIXME: figure out descriptors for gen7 */
2235 result = flush_compute_descriptor_set(cmd_buffer);
2236 if (result != VK_SUCCESS)
2237 return;
2238
2239 cmd_buffer->state.descriptors_dirty &= ~VK_SHADER_STAGE_COMPUTE_BIT;
2240 }
2241
2242 if (cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_COMPUTE_BIT) {
2243 struct anv_state push_state =
2244 anv_cmd_buffer_cs_push_constants(cmd_buffer);
2245
2246 if (push_state.alloc_size) {
2247 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_CURBE_LOAD), curbe) {
2248 curbe.CURBETotalDataLength = push_state.alloc_size;
2249 curbe.CURBEDataStartAddress = push_state.offset;
2250 }
2251 }
2252 }
2253
2254 cmd_buffer->state.compute_dirty = 0;
2255
2256 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
2257 }
2258
2259 #if GEN_GEN == 7
2260
2261 static VkResult
2262 verify_cmd_parser(const struct anv_device *device,
2263 int required_version,
2264 const char *function)
2265 {
2266 if (device->instance->physicalDevice.cmd_parser_version < required_version) {
2267 return vk_errorf(VK_ERROR_FEATURE_NOT_PRESENT,
2268 "cmd parser version %d is required for %s",
2269 required_version, function);
2270 } else {
2271 return VK_SUCCESS;
2272 }
2273 }
2274
2275 #endif
2276
2277 void genX(CmdDispatch)(
2278 VkCommandBuffer commandBuffer,
2279 uint32_t x,
2280 uint32_t y,
2281 uint32_t z)
2282 {
2283 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2284 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
2285 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
2286
2287 if (anv_batch_has_error(&cmd_buffer->batch))
2288 return;
2289
2290 if (prog_data->uses_num_work_groups) {
2291 struct anv_state state =
2292 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 12, 4);
2293 uint32_t *sizes = state.map;
2294 sizes[0] = x;
2295 sizes[1] = y;
2296 sizes[2] = z;
2297 anv_state_flush(cmd_buffer->device, state);
2298 cmd_buffer->state.num_workgroups_offset = state.offset;
2299 cmd_buffer->state.num_workgroups_bo =
2300 &cmd_buffer->device->dynamic_state_pool.block_pool.bo;
2301 }
2302
2303 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
2304
2305 anv_batch_emit(&cmd_buffer->batch, GENX(GPGPU_WALKER), ggw) {
2306 ggw.SIMDSize = prog_data->simd_size / 16;
2307 ggw.ThreadDepthCounterMaximum = 0;
2308 ggw.ThreadHeightCounterMaximum = 0;
2309 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
2310 ggw.ThreadGroupIDXDimension = x;
2311 ggw.ThreadGroupIDYDimension = y;
2312 ggw.ThreadGroupIDZDimension = z;
2313 ggw.RightExecutionMask = pipeline->cs_right_mask;
2314 ggw.BottomExecutionMask = 0xffffffff;
2315 }
2316
2317 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_STATE_FLUSH), msf);
2318 }
2319
2320 #define GPGPU_DISPATCHDIMX 0x2500
2321 #define GPGPU_DISPATCHDIMY 0x2504
2322 #define GPGPU_DISPATCHDIMZ 0x2508
2323
2324 #define MI_PREDICATE_SRC0 0x2400
2325 #define MI_PREDICATE_SRC1 0x2408
2326
2327 void genX(CmdDispatchIndirect)(
2328 VkCommandBuffer commandBuffer,
2329 VkBuffer _buffer,
2330 VkDeviceSize offset)
2331 {
2332 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2333 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
2334 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
2335 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
2336 struct anv_bo *bo = buffer->bo;
2337 uint32_t bo_offset = buffer->offset + offset;
2338 struct anv_batch *batch = &cmd_buffer->batch;
2339
2340 #if GEN_GEN == 7
2341 /* Linux 4.4 added command parser version 5 which allows the GPGPU
2342 * indirect dispatch registers to be written.
2343 */
2344 if (verify_cmd_parser(cmd_buffer->device, 5,
2345 "vkCmdDispatchIndirect") != VK_SUCCESS)
2346 return;
2347 #endif
2348
2349 if (prog_data->uses_num_work_groups) {
2350 cmd_buffer->state.num_workgroups_offset = bo_offset;
2351 cmd_buffer->state.num_workgroups_bo = bo;
2352 }
2353
2354 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
2355
2356 emit_lrm(batch, GPGPU_DISPATCHDIMX, bo, bo_offset);
2357 emit_lrm(batch, GPGPU_DISPATCHDIMY, bo, bo_offset + 4);
2358 emit_lrm(batch, GPGPU_DISPATCHDIMZ, bo, bo_offset + 8);
2359
2360 #if GEN_GEN <= 7
2361 /* Clear upper 32-bits of SRC0 and all 64-bits of SRC1 */
2362 emit_lri(batch, MI_PREDICATE_SRC0 + 4, 0);
2363 emit_lri(batch, MI_PREDICATE_SRC1 + 0, 0);
2364 emit_lri(batch, MI_PREDICATE_SRC1 + 4, 0);
2365
2366 /* Load compute_dispatch_indirect_x_size into SRC0 */
2367 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 0);
2368
2369 /* predicate = (compute_dispatch_indirect_x_size == 0); */
2370 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
2371 mip.LoadOperation = LOAD_LOAD;
2372 mip.CombineOperation = COMBINE_SET;
2373 mip.CompareOperation = COMPARE_SRCS_EQUAL;
2374 }
2375
2376 /* Load compute_dispatch_indirect_y_size into SRC0 */
2377 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 4);
2378
2379 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
2380 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
2381 mip.LoadOperation = LOAD_LOAD;
2382 mip.CombineOperation = COMBINE_OR;
2383 mip.CompareOperation = COMPARE_SRCS_EQUAL;
2384 }
2385
2386 /* Load compute_dispatch_indirect_z_size into SRC0 */
2387 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 8);
2388
2389 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
2390 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
2391 mip.LoadOperation = LOAD_LOAD;
2392 mip.CombineOperation = COMBINE_OR;
2393 mip.CompareOperation = COMPARE_SRCS_EQUAL;
2394 }
2395
2396 /* predicate = !predicate; */
2397 #define COMPARE_FALSE 1
2398 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
2399 mip.LoadOperation = LOAD_LOADINV;
2400 mip.CombineOperation = COMBINE_OR;
2401 mip.CompareOperation = COMPARE_FALSE;
2402 }
2403 #endif
2404
2405 anv_batch_emit(batch, GENX(GPGPU_WALKER), ggw) {
2406 ggw.IndirectParameterEnable = true;
2407 ggw.PredicateEnable = GEN_GEN <= 7;
2408 ggw.SIMDSize = prog_data->simd_size / 16;
2409 ggw.ThreadDepthCounterMaximum = 0;
2410 ggw.ThreadHeightCounterMaximum = 0;
2411 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
2412 ggw.RightExecutionMask = pipeline->cs_right_mask;
2413 ggw.BottomExecutionMask = 0xffffffff;
2414 }
2415
2416 anv_batch_emit(batch, GENX(MEDIA_STATE_FLUSH), msf);
2417 }
2418
2419 static void
2420 genX(flush_pipeline_select)(struct anv_cmd_buffer *cmd_buffer,
2421 uint32_t pipeline)
2422 {
2423 if (cmd_buffer->state.current_pipeline == pipeline)
2424 return;
2425
2426 #if GEN_GEN >= 8 && GEN_GEN < 10
2427 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
2428 *
2429 * Software must clear the COLOR_CALC_STATE Valid field in
2430 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
2431 * with Pipeline Select set to GPGPU.
2432 *
2433 * The internal hardware docs recommend the same workaround for Gen9
2434 * hardware too.
2435 */
2436 if (pipeline == GPGPU)
2437 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CC_STATE_POINTERS), t);
2438 #endif
2439
2440 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
2441 * PIPELINE_SELECT [DevBWR+]":
2442 *
2443 * Project: DEVSNB+
2444 *
2445 * Software must ensure all the write caches are flushed through a
2446 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
2447 * command to invalidate read only caches prior to programming
2448 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
2449 */
2450 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
2451 pc.RenderTargetCacheFlushEnable = true;
2452 pc.DepthCacheFlushEnable = true;
2453 pc.DCFlushEnable = true;
2454 pc.PostSyncOperation = NoWrite;
2455 pc.CommandStreamerStallEnable = true;
2456 }
2457
2458 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
2459 pc.TextureCacheInvalidationEnable = true;
2460 pc.ConstantCacheInvalidationEnable = true;
2461 pc.StateCacheInvalidationEnable = true;
2462 pc.InstructionCacheInvalidateEnable = true;
2463 pc.PostSyncOperation = NoWrite;
2464 }
2465
2466 anv_batch_emit(&cmd_buffer->batch, GENX(PIPELINE_SELECT), ps) {
2467 #if GEN_GEN >= 9
2468 ps.MaskBits = 3;
2469 #endif
2470 ps.PipelineSelection = pipeline;
2471 }
2472
2473 cmd_buffer->state.current_pipeline = pipeline;
2474 }
2475
2476 void
2477 genX(flush_pipeline_select_3d)(struct anv_cmd_buffer *cmd_buffer)
2478 {
2479 genX(flush_pipeline_select)(cmd_buffer, _3D);
2480 }
2481
2482 void
2483 genX(flush_pipeline_select_gpgpu)(struct anv_cmd_buffer *cmd_buffer)
2484 {
2485 genX(flush_pipeline_select)(cmd_buffer, GPGPU);
2486 }
2487
2488 void
2489 genX(cmd_buffer_emit_gen7_depth_flush)(struct anv_cmd_buffer *cmd_buffer)
2490 {
2491 if (GEN_GEN >= 8)
2492 return;
2493
2494 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
2495 *
2496 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
2497 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
2498 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
2499 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
2500 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
2501 * Depth Flush Bit set, followed by another pipelined depth stall
2502 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
2503 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
2504 * via a preceding MI_FLUSH)."
2505 */
2506 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
2507 pipe.DepthStallEnable = true;
2508 }
2509 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
2510 pipe.DepthCacheFlushEnable = true;
2511 }
2512 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
2513 pipe.DepthStallEnable = true;
2514 }
2515 }
2516
2517 static void
2518 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer *cmd_buffer)
2519 {
2520 struct anv_device *device = cmd_buffer->device;
2521 const struct anv_image_view *iview =
2522 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
2523 const struct anv_image *image = iview ? iview->image : NULL;
2524
2525 /* FIXME: Width and Height are wrong */
2526
2527 genX(cmd_buffer_emit_gen7_depth_flush)(cmd_buffer);
2528
2529 uint32_t *dw = anv_batch_emit_dwords(&cmd_buffer->batch,
2530 device->isl_dev.ds.size / 4);
2531 if (dw == NULL)
2532 return;
2533
2534 struct isl_depth_stencil_hiz_emit_info info = {
2535 .mocs = device->default_mocs,
2536 };
2537
2538 if (iview)
2539 info.view = &iview->isl;
2540
2541 if (image && (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
2542 info.depth_surf = &image->depth_surface.isl;
2543
2544 info.depth_address =
2545 anv_batch_emit_reloc(&cmd_buffer->batch,
2546 dw + device->isl_dev.ds.depth_offset / 4,
2547 image->bo,
2548 image->offset + image->depth_surface.offset);
2549
2550 const uint32_t ds =
2551 cmd_buffer->state.subpass->depth_stencil_attachment.attachment;
2552 info.hiz_usage = cmd_buffer->state.attachments[ds].aux_usage;
2553 if (info.hiz_usage == ISL_AUX_USAGE_HIZ) {
2554 info.hiz_surf = &image->aux_surface.isl;
2555
2556 info.hiz_address =
2557 anv_batch_emit_reloc(&cmd_buffer->batch,
2558 dw + device->isl_dev.ds.hiz_offset / 4,
2559 image->bo,
2560 image->offset + image->aux_surface.offset);
2561
2562 info.depth_clear_value = ANV_HZ_FC_VAL;
2563 }
2564 }
2565
2566 if (image && (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT)) {
2567 info.stencil_surf = &image->stencil_surface.isl;
2568
2569 info.stencil_address =
2570 anv_batch_emit_reloc(&cmd_buffer->batch,
2571 dw + device->isl_dev.ds.stencil_offset / 4,
2572 image->bo,
2573 image->offset + image->stencil_surface.offset);
2574 }
2575
2576 isl_emit_depth_stencil_hiz_s(&device->isl_dev, dw, &info);
2577
2578 cmd_buffer->state.hiz_enabled = info.hiz_usage == ISL_AUX_USAGE_HIZ;
2579 }
2580
2581
2582 /**
2583 * @brief Perform any layout transitions required at the beginning and/or end
2584 * of the current subpass for depth buffers.
2585 *
2586 * TODO: Consider preprocessing the attachment reference array at render pass
2587 * create time to determine if no layout transition is needed at the
2588 * beginning and/or end of each subpass.
2589 *
2590 * @param cmd_buffer The command buffer the transition is happening within.
2591 * @param subpass_end If true, marks that the transition is happening at the
2592 * end of the subpass.
2593 */
2594 static void
2595 cmd_buffer_subpass_transition_layouts(struct anv_cmd_buffer * const cmd_buffer,
2596 const bool subpass_end)
2597 {
2598 /* We need a non-NULL command buffer. */
2599 assert(cmd_buffer);
2600
2601 const struct anv_cmd_state * const cmd_state = &cmd_buffer->state;
2602 const struct anv_subpass * const subpass = cmd_state->subpass;
2603
2604 /* This function must be called within a subpass. */
2605 assert(subpass);
2606
2607 /* If there are attachment references, the array shouldn't be NULL.
2608 */
2609 if (subpass->attachment_count > 0)
2610 assert(subpass->attachments);
2611
2612 /* Iterate over the array of attachment references. */
2613 for (const VkAttachmentReference *att_ref = subpass->attachments;
2614 att_ref < subpass->attachments + subpass->attachment_count; att_ref++) {
2615
2616 /* If the attachment is unused, we can't perform a layout transition. */
2617 if (att_ref->attachment == VK_ATTACHMENT_UNUSED)
2618 continue;
2619
2620 /* This attachment index shouldn't go out of bounds. */
2621 assert(att_ref->attachment < cmd_state->pass->attachment_count);
2622
2623 const struct anv_render_pass_attachment * const att_desc =
2624 &cmd_state->pass->attachments[att_ref->attachment];
2625 struct anv_attachment_state * const att_state =
2626 &cmd_buffer->state.attachments[att_ref->attachment];
2627
2628 /* The attachment should not be used in a subpass after its last. */
2629 assert(att_desc->last_subpass_idx >= anv_get_subpass_id(cmd_state));
2630
2631 if (subpass_end && anv_get_subpass_id(cmd_state) <
2632 att_desc->last_subpass_idx) {
2633 /* We're calling this function on a buffer twice in one subpass and
2634 * this is not the last use of the buffer. The layout should not have
2635 * changed from the first call and no transition is necessary.
2636 */
2637 assert(att_ref->layout == att_state->current_layout);
2638 continue;
2639 }
2640
2641 /* Get the appropriate target layout for this attachment. */
2642 const VkImageLayout target_layout = subpass_end ?
2643 att_desc->final_layout : att_ref->layout;
2644
2645 /* The attachment index must be less than the number of attachments
2646 * within the framebuffer.
2647 */
2648 assert(att_ref->attachment < cmd_state->framebuffer->attachment_count);
2649
2650 const struct anv_image_view * const iview =
2651 cmd_state->framebuffer->attachments[att_ref->attachment];
2652 const struct anv_image * const image = iview->image;
2653
2654 /* Perform the layout transition. */
2655 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
2656 transition_depth_buffer(cmd_buffer, image,
2657 att_state->current_layout, target_layout);
2658 att_state->aux_usage =
2659 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
2660 image->aspects, target_layout);
2661 } else if (image->aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
2662 transition_color_buffer(cmd_buffer, image,
2663 iview->isl.base_level, 1,
2664 iview->isl.base_array_layer,
2665 iview->isl.array_len,
2666 att_state->current_layout, target_layout);
2667 }
2668
2669 att_state->current_layout = target_layout;
2670 }
2671 }
2672
2673 /* Update the clear value dword(s) in surface state objects or the fast clear
2674 * state buffer entry for the color attachments used in this subpass.
2675 */
2676 static void
2677 cmd_buffer_subpass_sync_fast_clear_values(struct anv_cmd_buffer *cmd_buffer)
2678 {
2679 assert(cmd_buffer && cmd_buffer->state.subpass);
2680
2681 const struct anv_cmd_state *state = &cmd_buffer->state;
2682
2683 /* Iterate through every color attachment used in this subpass. */
2684 for (uint32_t i = 0; i < state->subpass->color_count; ++i) {
2685
2686 /* The attachment should be one of the attachments described in the
2687 * render pass and used in the subpass.
2688 */
2689 const uint32_t a = state->subpass->color_attachments[i].attachment;
2690 assert(a < state->pass->attachment_count);
2691 if (a == VK_ATTACHMENT_UNUSED)
2692 continue;
2693
2694 /* Store some information regarding this attachment. */
2695 const struct anv_attachment_state *att_state = &state->attachments[a];
2696 const struct anv_image_view *iview = state->framebuffer->attachments[a];
2697 const struct anv_render_pass_attachment *rp_att =
2698 &state->pass->attachments[a];
2699
2700 if (att_state->aux_usage == ISL_AUX_USAGE_NONE)
2701 continue;
2702
2703 /* The fast clear state entry must be updated if a fast clear is going to
2704 * happen. The surface state must be updated if the clear value from a
2705 * prior fast clear may be needed.
2706 */
2707 if (att_state->pending_clear_aspects && att_state->fast_clear) {
2708 /* Update the fast clear state entry. */
2709 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->color_rt_state,
2710 iview->image, iview->isl.base_level,
2711 true /* copy from ss */);
2712 } else if (rp_att->load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
2713 /* The attachment may have been fast-cleared in a previous render
2714 * pass and the value is needed now. Update the surface state(s).
2715 *
2716 * TODO: Do this only once per render pass instead of every subpass.
2717 */
2718 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->color_rt_state,
2719 iview->image, iview->isl.base_level,
2720 false /* copy to ss */);
2721
2722 if (need_input_attachment_state(rp_att) &&
2723 att_state->input_aux_usage != ISL_AUX_USAGE_NONE) {
2724 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->input_att_state,
2725 iview->image, iview->isl.base_level,
2726 false /* copy to ss */);
2727 }
2728 }
2729 }
2730 }
2731
2732
2733 static void
2734 genX(cmd_buffer_set_subpass)(struct anv_cmd_buffer *cmd_buffer,
2735 struct anv_subpass *subpass)
2736 {
2737 cmd_buffer->state.subpass = subpass;
2738
2739 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
2740
2741 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2742 * different views. If the client asks for instancing, we need to use the
2743 * Instance Data Step Rate to ensure that we repeat the client's
2744 * per-instance data once for each view. Since this bit is in
2745 * VERTEX_BUFFER_STATE on gen7, we need to dirty vertex buffers at the top
2746 * of each subpass.
2747 */
2748 if (GEN_GEN == 7)
2749 cmd_buffer->state.vb_dirty |= ~0;
2750
2751 /* Perform transitions to the subpass layout before any writes have
2752 * occurred.
2753 */
2754 cmd_buffer_subpass_transition_layouts(cmd_buffer, false);
2755
2756 /* Update clear values *after* performing automatic layout transitions.
2757 * This ensures that transitions from the UNDEFINED layout have had a chance
2758 * to populate the clear value buffer with the correct values for the
2759 * LOAD_OP_LOAD loadOp and that the fast-clears will update the buffer
2760 * without the aforementioned layout transition overwriting the fast-clear
2761 * value.
2762 */
2763 cmd_buffer_subpass_sync_fast_clear_values(cmd_buffer);
2764
2765 cmd_buffer_emit_depth_stencil(cmd_buffer);
2766
2767 anv_cmd_buffer_clear_subpass(cmd_buffer);
2768 }
2769
2770 void genX(CmdBeginRenderPass)(
2771 VkCommandBuffer commandBuffer,
2772 const VkRenderPassBeginInfo* pRenderPassBegin,
2773 VkSubpassContents contents)
2774 {
2775 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2776 ANV_FROM_HANDLE(anv_render_pass, pass, pRenderPassBegin->renderPass);
2777 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, pRenderPassBegin->framebuffer);
2778
2779 cmd_buffer->state.framebuffer = framebuffer;
2780 cmd_buffer->state.pass = pass;
2781 cmd_buffer->state.render_area = pRenderPassBegin->renderArea;
2782 VkResult result =
2783 genX(cmd_buffer_setup_attachments)(cmd_buffer, pass, pRenderPassBegin);
2784
2785 /* If we failed to setup the attachments we should not try to go further */
2786 if (result != VK_SUCCESS) {
2787 assert(anv_batch_has_error(&cmd_buffer->batch));
2788 return;
2789 }
2790
2791 genX(flush_pipeline_select_3d)(cmd_buffer);
2792
2793 genX(cmd_buffer_set_subpass)(cmd_buffer, pass->subpasses);
2794
2795 cmd_buffer->state.pending_pipe_bits |=
2796 cmd_buffer->state.pass->subpass_flushes[0];
2797 }
2798
2799 void genX(CmdNextSubpass)(
2800 VkCommandBuffer commandBuffer,
2801 VkSubpassContents contents)
2802 {
2803 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2804
2805 if (anv_batch_has_error(&cmd_buffer->batch))
2806 return;
2807
2808 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
2809
2810 anv_cmd_buffer_resolve_subpass(cmd_buffer);
2811
2812 /* Perform transitions to the final layout after all writes have occurred.
2813 */
2814 cmd_buffer_subpass_transition_layouts(cmd_buffer, true);
2815
2816 genX(cmd_buffer_set_subpass)(cmd_buffer, cmd_buffer->state.subpass + 1);
2817
2818 uint32_t subpass_id = anv_get_subpass_id(&cmd_buffer->state);
2819 cmd_buffer->state.pending_pipe_bits |=
2820 cmd_buffer->state.pass->subpass_flushes[subpass_id];
2821 }
2822
2823 void genX(CmdEndRenderPass)(
2824 VkCommandBuffer commandBuffer)
2825 {
2826 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2827
2828 if (anv_batch_has_error(&cmd_buffer->batch))
2829 return;
2830
2831 anv_cmd_buffer_resolve_subpass(cmd_buffer);
2832
2833 /* Perform transitions to the final layout after all writes have occurred.
2834 */
2835 cmd_buffer_subpass_transition_layouts(cmd_buffer, true);
2836
2837 cmd_buffer->state.pending_pipe_bits |=
2838 cmd_buffer->state.pass->subpass_flushes[cmd_buffer->state.pass->subpass_count];
2839
2840 cmd_buffer->state.hiz_enabled = false;
2841
2842 #ifndef NDEBUG
2843 anv_dump_add_framebuffer(cmd_buffer, cmd_buffer->state.framebuffer);
2844 #endif
2845
2846 /* Remove references to render pass specific state. This enables us to
2847 * detect whether or not we're in a renderpass.
2848 */
2849 cmd_buffer->state.framebuffer = NULL;
2850 cmd_buffer->state.pass = NULL;
2851 cmd_buffer->state.subpass = NULL;
2852 }