anv: Add a helper to extract clear color from the attachment.
[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_view_relocs(struct anv_cmd_buffer *cmd_buffer,
183 const struct anv_image_view *image_view,
184 const uint32_t plane,
185 struct anv_surface_state state)
186 {
187 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
188 const struct anv_image *image = image_view->image;
189 uint32_t image_plane = image_view->planes[plane].image_plane;
190
191 add_surface_state_reloc(cmd_buffer, state.state,
192 image->planes[image_plane].bo, state.address);
193
194 if (state.aux_address) {
195 VkResult result =
196 anv_reloc_list_add(&cmd_buffer->surface_relocs,
197 &cmd_buffer->pool->alloc,
198 state.state.offset + isl_dev->ss.aux_addr_offset,
199 image->planes[image_plane].bo, state.aux_address);
200 if (result != VK_SUCCESS)
201 anv_batch_set_error(&cmd_buffer->batch, result);
202 }
203 }
204
205 static void
206 color_attachment_compute_aux_usage(struct anv_device * device,
207 struct anv_cmd_state * cmd_state,
208 uint32_t att, VkRect2D render_area,
209 union isl_color_value *fast_clear_color)
210 {
211 struct anv_attachment_state *att_state = &cmd_state->attachments[att];
212 struct anv_image_view *iview = cmd_state->framebuffer->attachments[att];
213
214 assert(iview->n_planes == 1);
215
216 if (iview->planes[0].isl.base_array_layer >=
217 anv_image_aux_layers(iview->image, VK_IMAGE_ASPECT_COLOR_BIT,
218 iview->planes[0].isl.base_level)) {
219 /* There is no aux buffer which corresponds to the level and layer(s)
220 * being accessed.
221 */
222 att_state->aux_usage = ISL_AUX_USAGE_NONE;
223 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
224 att_state->fast_clear = false;
225 return;
226 }
227
228 att_state->aux_usage =
229 anv_layout_to_aux_usage(&device->info, iview->image,
230 VK_IMAGE_ASPECT_COLOR_BIT,
231 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
232
233 /* If we don't have aux, then we should have returned early in the layer
234 * check above. If we got here, we must have something.
235 */
236 assert(att_state->aux_usage != ISL_AUX_USAGE_NONE);
237
238 if (att_state->aux_usage == ISL_AUX_USAGE_CCS_E ||
239 att_state->aux_usage == ISL_AUX_USAGE_MCS) {
240 att_state->input_aux_usage = att_state->aux_usage;
241 } else {
242 /* From the Sky Lake PRM, RENDER_SURFACE_STATE::AuxiliarySurfaceMode:
243 *
244 * "If Number of Multisamples is MULTISAMPLECOUNT_1, AUX_CCS_D
245 * setting is only allowed if Surface Format supported for Fast
246 * Clear. In addition, if the surface is bound to the sampling
247 * engine, Surface Format must be supported for Render Target
248 * Compression for surfaces bound to the sampling engine."
249 *
250 * In other words, we can only sample from a fast-cleared image if it
251 * also supports color compression.
252 */
253 if (isl_format_supports_ccs_e(&device->info, iview->planes[0].isl.format)) {
254 att_state->input_aux_usage = ISL_AUX_USAGE_CCS_D;
255
256 /* While fast-clear resolves and partial resolves are fairly cheap in the
257 * case where you render to most of the pixels, full resolves are not
258 * because they potentially involve reading and writing the entire
259 * framebuffer. If we can't texture with CCS_E, we should leave it off and
260 * limit ourselves to fast clears.
261 */
262 if (cmd_state->pass->attachments[att].first_subpass_layout ==
263 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
264 anv_perf_warn(device->instance, iview->image,
265 "Not temporarily enabling CCS_E.");
266 }
267 } else {
268 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
269 }
270 }
271
272 assert(iview->image->planes[0].aux_surface.isl.usage &
273 (ISL_SURF_USAGE_CCS_BIT | ISL_SURF_USAGE_MCS_BIT));
274
275 union isl_color_value clear_color = {};
276 anv_clear_color_from_att_state(&clear_color, att_state, iview);
277
278 att_state->clear_color_is_zero_one =
279 isl_color_value_is_zero_one(clear_color, iview->planes[0].isl.format);
280 att_state->clear_color_is_zero =
281 isl_color_value_is_zero(clear_color, iview->planes[0].isl.format);
282
283 if (att_state->pending_clear_aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
284 /* Start by getting the fast clear type. We use the first subpass
285 * layout here because we don't want to fast-clear if the first subpass
286 * to use the attachment can't handle fast-clears.
287 */
288 enum anv_fast_clear_type fast_clear_type =
289 anv_layout_to_fast_clear_type(&device->info, iview->image,
290 VK_IMAGE_ASPECT_COLOR_BIT,
291 cmd_state->pass->attachments[att].first_subpass_layout);
292 switch (fast_clear_type) {
293 case ANV_FAST_CLEAR_NONE:
294 att_state->fast_clear = false;
295 break;
296 case ANV_FAST_CLEAR_DEFAULT_VALUE:
297 att_state->fast_clear = att_state->clear_color_is_zero;
298 break;
299 case ANV_FAST_CLEAR_ANY:
300 att_state->fast_clear = true;
301 break;
302 }
303
304 /* Potentially, we could do partial fast-clears but doing so has crazy
305 * alignment restrictions. It's easier to just restrict to full size
306 * fast clears for now.
307 */
308 if (render_area.offset.x != 0 ||
309 render_area.offset.y != 0 ||
310 render_area.extent.width != iview->extent.width ||
311 render_area.extent.height != iview->extent.height)
312 att_state->fast_clear = false;
313
314 /* On Broadwell and earlier, we can only handle 0/1 clear colors */
315 if (GEN_GEN <= 8 && !att_state->clear_color_is_zero_one)
316 att_state->fast_clear = false;
317
318 /* We only allow fast clears to the first slice of an image (level 0,
319 * layer 0) and only for the entire slice. This guarantees us that, at
320 * any given time, there is only one clear color on any given image at
321 * any given time. At the time of our testing (Jan 17, 2018), there
322 * were no known applications which would benefit from fast-clearing
323 * more than just the first slice.
324 */
325 if (att_state->fast_clear &&
326 (iview->planes[0].isl.base_level > 0 ||
327 iview->planes[0].isl.base_array_layer > 0)) {
328 anv_perf_warn(device->instance, iview->image,
329 "Rendering with multi-lod or multi-layer framebuffer "
330 "with LOAD_OP_LOAD and baseMipLevel > 0 or "
331 "baseArrayLayer > 0. Not fast clearing.");
332 att_state->fast_clear = false;
333 } else if (att_state->fast_clear && cmd_state->framebuffer->layers > 1) {
334 anv_perf_warn(device->instance, iview->image,
335 "Rendering to a multi-layer framebuffer with "
336 "LOAD_OP_CLEAR. Only fast-clearing the first slice");
337 }
338
339 if (att_state->fast_clear)
340 *fast_clear_color = clear_color;
341 } else {
342 att_state->fast_clear = false;
343 }
344 }
345
346 static void
347 depth_stencil_attachment_compute_aux_usage(struct anv_device *device,
348 struct anv_cmd_state *cmd_state,
349 uint32_t att, VkRect2D render_area)
350 {
351 struct anv_render_pass_attachment *pass_att =
352 &cmd_state->pass->attachments[att];
353 struct anv_attachment_state *att_state = &cmd_state->attachments[att];
354 struct anv_image_view *iview = cmd_state->framebuffer->attachments[att];
355
356 /* These will be initialized after the first subpass transition. */
357 att_state->aux_usage = ISL_AUX_USAGE_NONE;
358 att_state->input_aux_usage = ISL_AUX_USAGE_NONE;
359
360 if (GEN_GEN == 7) {
361 /* We don't do any HiZ or depth fast-clears on gen7 yet */
362 att_state->fast_clear = false;
363 return;
364 }
365
366 if (!(att_state->pending_clear_aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
367 /* If we're just clearing stencil, we can always HiZ clear */
368 att_state->fast_clear = true;
369 return;
370 }
371
372 /* Default to false for now */
373 att_state->fast_clear = false;
374
375 /* We must have depth in order to have HiZ */
376 if (!(iview->image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT))
377 return;
378
379 const enum isl_aux_usage first_subpass_aux_usage =
380 anv_layout_to_aux_usage(&device->info, iview->image,
381 VK_IMAGE_ASPECT_DEPTH_BIT,
382 pass_att->first_subpass_layout);
383 if (first_subpass_aux_usage != ISL_AUX_USAGE_HIZ)
384 return;
385
386 if (!blorp_can_hiz_clear_depth(GEN_GEN,
387 iview->planes[0].isl.format,
388 iview->image->samples,
389 render_area.offset.x,
390 render_area.offset.y,
391 render_area.offset.x +
392 render_area.extent.width,
393 render_area.offset.y +
394 render_area.extent.height))
395 return;
396
397 if (att_state->clear_value.depthStencil.depth != ANV_HZ_FC_VAL)
398 return;
399
400 if (GEN_GEN == 8 && anv_can_sample_with_hiz(&device->info, iview->image)) {
401 /* Only gen9+ supports returning ANV_HZ_FC_VAL when sampling a
402 * fast-cleared portion of a HiZ buffer. Testing has revealed that Gen8
403 * only supports returning 0.0f. Gens prior to gen8 do not support this
404 * feature at all.
405 */
406 return;
407 }
408
409 /* If we got here, then we can fast clear */
410 att_state->fast_clear = true;
411 }
412
413 static bool
414 need_input_attachment_state(const struct anv_render_pass_attachment *att)
415 {
416 if (!(att->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
417 return false;
418
419 /* We only allocate input attachment states for color surfaces. Compression
420 * is not yet enabled for depth textures and stencil doesn't allow
421 * compression so we can just use the texture surface state from the view.
422 */
423 return vk_format_is_color(att->format);
424 }
425
426 /* Transitions a HiZ-enabled depth buffer from one layout to another. Unless
427 * the initial layout is undefined, the HiZ buffer and depth buffer will
428 * represent the same data at the end of this operation.
429 */
430 static void
431 transition_depth_buffer(struct anv_cmd_buffer *cmd_buffer,
432 const struct anv_image *image,
433 VkImageLayout initial_layout,
434 VkImageLayout final_layout)
435 {
436 const bool hiz_enabled = ISL_AUX_USAGE_HIZ ==
437 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
438 VK_IMAGE_ASPECT_DEPTH_BIT, initial_layout);
439 const bool enable_hiz = ISL_AUX_USAGE_HIZ ==
440 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
441 VK_IMAGE_ASPECT_DEPTH_BIT, final_layout);
442
443 enum isl_aux_op hiz_op;
444 if (hiz_enabled && !enable_hiz) {
445 hiz_op = ISL_AUX_OP_FULL_RESOLVE;
446 } else if (!hiz_enabled && enable_hiz) {
447 hiz_op = ISL_AUX_OP_AMBIGUATE;
448 } else {
449 assert(hiz_enabled == enable_hiz);
450 /* If the same buffer will be used, no resolves are necessary. */
451 hiz_op = ISL_AUX_OP_NONE;
452 }
453
454 if (hiz_op != ISL_AUX_OP_NONE)
455 anv_image_hiz_op(cmd_buffer, image, VK_IMAGE_ASPECT_DEPTH_BIT,
456 0, 0, 1, hiz_op);
457 }
458
459 #define MI_PREDICATE_SRC0 0x2400
460 #define MI_PREDICATE_SRC1 0x2408
461
462 static void
463 set_image_compressed_bit(struct anv_cmd_buffer *cmd_buffer,
464 const struct anv_image *image,
465 VkImageAspectFlagBits aspect,
466 uint32_t level,
467 uint32_t base_layer, uint32_t layer_count,
468 bool compressed)
469 {
470 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
471
472 /* We only have compression tracking for CCS_E */
473 if (image->planes[plane].aux_usage != ISL_AUX_USAGE_CCS_E)
474 return;
475
476 for (uint32_t a = 0; a < layer_count; a++) {
477 uint32_t layer = base_layer + a;
478 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
479 sdi.Address = anv_image_get_compression_state_addr(cmd_buffer->device,
480 image, aspect,
481 level, layer);
482 sdi.ImmediateData = compressed ? UINT32_MAX : 0;
483 }
484 }
485 }
486
487 static void
488 set_image_fast_clear_state(struct anv_cmd_buffer *cmd_buffer,
489 const struct anv_image *image,
490 VkImageAspectFlagBits aspect,
491 enum anv_fast_clear_type fast_clear)
492 {
493 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
494 sdi.Address = anv_image_get_fast_clear_type_addr(cmd_buffer->device,
495 image, aspect);
496 sdi.ImmediateData = fast_clear;
497 }
498
499 /* Whenever we have fast-clear, we consider that slice to be compressed.
500 * This makes building predicates much easier.
501 */
502 if (fast_clear != ANV_FAST_CLEAR_NONE)
503 set_image_compressed_bit(cmd_buffer, image, aspect, 0, 0, 1, true);
504 }
505
506 #if GEN_IS_HASWELL || GEN_GEN >= 8
507 static inline uint32_t
508 mi_alu(uint32_t opcode, uint32_t operand1, uint32_t operand2)
509 {
510 struct GENX(MI_MATH_ALU_INSTRUCTION) instr = {
511 .ALUOpcode = opcode,
512 .Operand1 = operand1,
513 .Operand2 = operand2,
514 };
515
516 uint32_t dw;
517 GENX(MI_MATH_ALU_INSTRUCTION_pack)(NULL, &dw, &instr);
518
519 return dw;
520 }
521 #endif
522
523 #define CS_GPR(n) (0x2600 + (n) * 8)
524
525 /* This is only really practical on haswell and above because it requires
526 * MI math in order to get it correct.
527 */
528 #if GEN_GEN >= 8 || GEN_IS_HASWELL
529 static void
530 anv_cmd_compute_resolve_predicate(struct anv_cmd_buffer *cmd_buffer,
531 const struct anv_image *image,
532 VkImageAspectFlagBits aspect,
533 uint32_t level, uint32_t array_layer,
534 enum isl_aux_op resolve_op,
535 enum anv_fast_clear_type fast_clear_supported)
536 {
537 struct anv_address fast_clear_type_addr =
538 anv_image_get_fast_clear_type_addr(cmd_buffer->device, image, aspect);
539
540 /* Name some registers */
541 const int image_fc_reg = MI_ALU_REG0;
542 const int fc_imm_reg = MI_ALU_REG1;
543 const int pred_reg = MI_ALU_REG2;
544
545 uint32_t *dw;
546
547 if (resolve_op == ISL_AUX_OP_FULL_RESOLVE) {
548 /* In this case, we're doing a full resolve which means we want the
549 * resolve to happen if any compression (including fast-clears) is
550 * present.
551 *
552 * In order to simplify the logic a bit, we make the assumption that,
553 * if the first slice has been fast-cleared, it is also marked as
554 * compressed. See also set_image_fast_clear_state.
555 */
556 struct anv_address compression_state_addr =
557 anv_image_get_compression_state_addr(cmd_buffer->device, image,
558 aspect, level, array_layer);
559 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
560 lrm.RegisterAddress = MI_PREDICATE_SRC0;
561 lrm.MemoryAddress = compression_state_addr;
562 }
563 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
564 sdi.Address = compression_state_addr;
565 sdi.ImmediateData = 0;
566 }
567
568 if (level == 0 && array_layer == 0) {
569 /* If the predicate is true, we want to write 0 to the fast clear type
570 * and, if it's false, leave it alone. We can do this by writing
571 *
572 * clear_type = clear_type & ~predicate;
573 */
574 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
575 lrm.RegisterAddress = CS_GPR(image_fc_reg);
576 lrm.MemoryAddress = fast_clear_type_addr;
577 }
578 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_REG), lrr) {
579 lrr.DestinationRegisterAddress = CS_GPR(pred_reg);
580 lrr.SourceRegisterAddress = MI_PREDICATE_SRC0;
581 }
582
583 dw = anv_batch_emitn(&cmd_buffer->batch, 5, GENX(MI_MATH));
584 dw[1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, image_fc_reg);
585 dw[2] = mi_alu(MI_ALU_LOADINV, MI_ALU_SRCB, pred_reg);
586 dw[3] = mi_alu(MI_ALU_AND, 0, 0);
587 dw[4] = mi_alu(MI_ALU_STORE, image_fc_reg, MI_ALU_ACCU);
588
589 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
590 srm.MemoryAddress = fast_clear_type_addr;
591 srm.RegisterAddress = CS_GPR(image_fc_reg);
592 }
593 }
594 } else if (level == 0 && array_layer == 0) {
595 /* In this case, we are doing a partial resolve to get rid of fast-clear
596 * colors. We don't care about the compression state but we do care
597 * about how much fast clear is allowed by the final layout.
598 */
599 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
600 assert(fast_clear_supported < ANV_FAST_CLEAR_ANY);
601
602 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
603 lrm.RegisterAddress = CS_GPR(image_fc_reg);
604 lrm.MemoryAddress = fast_clear_type_addr;
605 }
606 emit_lri(&cmd_buffer->batch, CS_GPR(image_fc_reg) + 4, 0);
607
608 emit_lri(&cmd_buffer->batch, CS_GPR(fc_imm_reg), fast_clear_supported);
609 emit_lri(&cmd_buffer->batch, CS_GPR(fc_imm_reg) + 4, 0);
610
611 /* We need to compute (fast_clear_supported < image->fast_clear).
612 * We do this by subtracting and storing the carry bit.
613 */
614 dw = anv_batch_emitn(&cmd_buffer->batch, 5, GENX(MI_MATH));
615 dw[1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, fc_imm_reg);
616 dw[2] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, image_fc_reg);
617 dw[3] = mi_alu(MI_ALU_SUB, 0, 0);
618 dw[4] = mi_alu(MI_ALU_STORE, pred_reg, MI_ALU_CF);
619
620 /* Store the predicate */
621 emit_lrr(&cmd_buffer->batch, MI_PREDICATE_SRC0, CS_GPR(pred_reg));
622
623 /* If the predicate is true, we want to write 0 to the fast clear type
624 * and, if it's false, leave it alone. We can do this by writing
625 *
626 * clear_type = clear_type & ~predicate;
627 */
628 dw = anv_batch_emitn(&cmd_buffer->batch, 5, GENX(MI_MATH));
629 dw[1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, image_fc_reg);
630 dw[2] = mi_alu(MI_ALU_LOADINV, MI_ALU_SRCB, pred_reg);
631 dw[3] = mi_alu(MI_ALU_AND, 0, 0);
632 dw[4] = mi_alu(MI_ALU_STORE, image_fc_reg, MI_ALU_ACCU);
633
634 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
635 srm.RegisterAddress = CS_GPR(image_fc_reg);
636 srm.MemoryAddress = fast_clear_type_addr;
637 }
638 } else {
639 /* In this case, we're trying to do a partial resolve on a slice that
640 * doesn't have clear color. There's nothing to do.
641 */
642 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
643 return;
644 }
645
646 /* We use the first half of src0 for the actual predicate. Set the second
647 * half of src0 and all of src1 to 0 as the predicate operation will be
648 * doing an implicit src0 != src1.
649 */
650 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC0 + 4, 0);
651 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1 , 0);
652 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1 + 4, 0);
653
654 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
655 mip.LoadOperation = LOAD_LOADINV;
656 mip.CombineOperation = COMBINE_SET;
657 mip.CompareOperation = COMPARE_SRCS_EQUAL;
658 }
659 }
660 #endif /* GEN_GEN >= 8 || GEN_IS_HASWELL */
661
662 #if GEN_GEN <= 8
663 static void
664 anv_cmd_simple_resolve_predicate(struct anv_cmd_buffer *cmd_buffer,
665 const struct anv_image *image,
666 VkImageAspectFlagBits aspect,
667 uint32_t level, uint32_t array_layer,
668 enum isl_aux_op resolve_op,
669 enum anv_fast_clear_type fast_clear_supported)
670 {
671 struct anv_address fast_clear_type_addr =
672 anv_image_get_fast_clear_type_addr(cmd_buffer->device, image, aspect);
673
674 /* This only works for partial resolves and only when the clear color is
675 * all or nothing. On the upside, this emits less command streamer code
676 * and works on Ivybridge and Bay Trail.
677 */
678 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
679 assert(fast_clear_supported != ANV_FAST_CLEAR_ANY);
680
681 /* We don't support fast clears on anything other than the first slice. */
682 if (level > 0 || array_layer > 0)
683 return;
684
685 /* On gen8, we don't have a concept of default clear colors because we
686 * can't sample from CCS surfaces. It's enough to just load the fast clear
687 * state into the predicate register.
688 */
689 anv_batch_emit(&cmd_buffer->batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
690 lrm.RegisterAddress = MI_PREDICATE_SRC0;
691 lrm.MemoryAddress = fast_clear_type_addr;
692 }
693 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
694 sdi.Address = fast_clear_type_addr;
695 sdi.ImmediateData = 0;
696 }
697
698 /* We use the first half of src0 for the actual predicate. Set the second
699 * half of src0 and all of src1 to 0 as the predicate operation will be
700 * doing an implicit src0 != src1.
701 */
702 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC0 + 4, 0);
703 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1 , 0);
704 emit_lri(&cmd_buffer->batch, MI_PREDICATE_SRC1 + 4, 0);
705
706 anv_batch_emit(&cmd_buffer->batch, GENX(MI_PREDICATE), mip) {
707 mip.LoadOperation = LOAD_LOADINV;
708 mip.CombineOperation = COMBINE_SET;
709 mip.CompareOperation = COMPARE_SRCS_EQUAL;
710 }
711 }
712 #endif /* GEN_GEN <= 8 */
713
714 static void
715 anv_cmd_predicated_ccs_resolve(struct anv_cmd_buffer *cmd_buffer,
716 const struct anv_image *image,
717 VkImageAspectFlagBits aspect,
718 uint32_t level, uint32_t array_layer,
719 enum isl_aux_op resolve_op,
720 enum anv_fast_clear_type fast_clear_supported)
721 {
722 const uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
723
724 #if GEN_GEN >= 9
725 anv_cmd_compute_resolve_predicate(cmd_buffer, image,
726 aspect, level, array_layer,
727 resolve_op, fast_clear_supported);
728 #else /* GEN_GEN <= 8 */
729 anv_cmd_simple_resolve_predicate(cmd_buffer, image,
730 aspect, level, array_layer,
731 resolve_op, fast_clear_supported);
732 #endif
733
734 /* CCS_D only supports full resolves and BLORP will assert on us if we try
735 * to do a partial resolve on a CCS_D surface.
736 */
737 if (resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE &&
738 image->planes[plane].aux_usage == ISL_AUX_USAGE_NONE)
739 resolve_op = ISL_AUX_OP_FULL_RESOLVE;
740
741 anv_image_ccs_op(cmd_buffer, image, aspect, level,
742 array_layer, 1, resolve_op, true);
743 }
744
745 static void
746 anv_cmd_predicated_mcs_resolve(struct anv_cmd_buffer *cmd_buffer,
747 const struct anv_image *image,
748 VkImageAspectFlagBits aspect,
749 uint32_t array_layer,
750 enum isl_aux_op resolve_op,
751 enum anv_fast_clear_type fast_clear_supported)
752 {
753 assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
754 assert(resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
755
756 #if GEN_GEN >= 8 || GEN_IS_HASWELL
757 anv_cmd_compute_resolve_predicate(cmd_buffer, image,
758 aspect, 0, array_layer,
759 resolve_op, fast_clear_supported);
760
761 anv_image_mcs_op(cmd_buffer, image, aspect,
762 array_layer, 1, resolve_op, true);
763 #else
764 unreachable("MCS resolves are unsupported on Ivybridge and Bay Trail");
765 #endif
766 }
767
768 void
769 genX(cmd_buffer_mark_image_written)(struct anv_cmd_buffer *cmd_buffer,
770 const struct anv_image *image,
771 VkImageAspectFlagBits aspect,
772 enum isl_aux_usage aux_usage,
773 uint32_t level,
774 uint32_t base_layer,
775 uint32_t layer_count)
776 {
777 /* The aspect must be exactly one of the image aspects. */
778 assert(_mesa_bitcount(aspect) == 1 && (aspect & image->aspects));
779
780 /* The only compression types with more than just fast-clears are MCS,
781 * CCS_E, and HiZ. With HiZ we just trust the layout and don't actually
782 * track the current fast-clear and compression state. This leaves us
783 * with just MCS and CCS_E.
784 */
785 if (aux_usage != ISL_AUX_USAGE_CCS_E &&
786 aux_usage != ISL_AUX_USAGE_MCS)
787 return;
788
789 set_image_compressed_bit(cmd_buffer, image, aspect,
790 level, base_layer, layer_count, true);
791 }
792
793 static void
794 init_fast_clear_color(struct anv_cmd_buffer *cmd_buffer,
795 const struct anv_image *image,
796 VkImageAspectFlagBits aspect)
797 {
798 assert(cmd_buffer && image);
799 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
800
801 set_image_fast_clear_state(cmd_buffer, image, aspect,
802 ANV_FAST_CLEAR_NONE);
803
804 /* The fast clear value dword(s) will be copied into a surface state object.
805 * Ensure that the restrictions of the fields in the dword(s) are followed.
806 *
807 * CCS buffers on SKL+ can have any value set for the clear colors.
808 */
809 if (image->samples == 1 && GEN_GEN >= 9)
810 return;
811
812 /* Other combinations of auxiliary buffers and platforms require specific
813 * values in the clear value dword(s).
814 */
815 struct anv_address addr =
816 anv_image_get_clear_color_addr(cmd_buffer->device, image, aspect);
817
818 if (GEN_GEN >= 9) {
819 for (unsigned i = 0; i < 4; i++) {
820 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
821 sdi.Address = addr;
822 sdi.Address.offset += i * 4;
823 /* MCS buffers on SKL+ can only have 1/0 clear colors. */
824 assert(image->samples > 1);
825 sdi.ImmediateData = 0;
826 }
827 }
828 } else {
829 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
830 sdi.Address = addr;
831 if (GEN_GEN >= 8 || GEN_IS_HASWELL) {
832 /* Pre-SKL, the dword containing the clear values also contains
833 * other fields, so we need to initialize those fields to match the
834 * values that would be in a color attachment.
835 */
836 sdi.ImmediateData = ISL_CHANNEL_SELECT_RED << 25 |
837 ISL_CHANNEL_SELECT_GREEN << 22 |
838 ISL_CHANNEL_SELECT_BLUE << 19 |
839 ISL_CHANNEL_SELECT_ALPHA << 16;
840 } else if (GEN_GEN == 7) {
841 /* On IVB, the dword containing the clear values also contains
842 * other fields that must be zero or can be zero.
843 */
844 sdi.ImmediateData = 0;
845 }
846 }
847 }
848 }
849
850 /* Copy the fast-clear value dword(s) between a surface state object and an
851 * image's fast clear state buffer.
852 */
853 static void
854 genX(copy_fast_clear_dwords)(struct anv_cmd_buffer *cmd_buffer,
855 struct anv_state surface_state,
856 const struct anv_image *image,
857 VkImageAspectFlagBits aspect,
858 bool copy_from_surface_state)
859 {
860 assert(cmd_buffer && image);
861 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
862
863 struct anv_bo *ss_bo =
864 &cmd_buffer->device->surface_state_pool.block_pool.bo;
865 uint32_t ss_clear_offset = surface_state.offset +
866 cmd_buffer->device->isl_dev.ss.clear_value_offset;
867 const struct anv_address entry_addr =
868 anv_image_get_clear_color_addr(cmd_buffer->device, image, aspect);
869 unsigned copy_size = cmd_buffer->device->isl_dev.ss.clear_value_size;
870
871 if (copy_from_surface_state) {
872 genX(cmd_buffer_mi_memcpy)(cmd_buffer, entry_addr.bo, entry_addr.offset,
873 ss_bo, ss_clear_offset, copy_size);
874 } else {
875 genX(cmd_buffer_mi_memcpy)(cmd_buffer, ss_bo, ss_clear_offset,
876 entry_addr.bo, entry_addr.offset, copy_size);
877
878 /* Updating a surface state object may require that the state cache be
879 * invalidated. From the SKL PRM, Shared Functions -> State -> State
880 * Caching:
881 *
882 * Whenever the RENDER_SURFACE_STATE object in memory pointed to by
883 * the Binding Table Pointer (BTP) and Binding Table Index (BTI) is
884 * modified [...], the L1 state cache must be invalidated to ensure
885 * the new surface or sampler state is fetched from system memory.
886 *
887 * In testing, SKL doesn't actually seem to need this, but HSW does.
888 */
889 cmd_buffer->state.pending_pipe_bits |=
890 ANV_PIPE_STATE_CACHE_INVALIDATE_BIT;
891 }
892 }
893
894 /**
895 * @brief Transitions a color buffer from one layout to another.
896 *
897 * See section 6.1.1. Image Layout Transitions of the Vulkan 1.0.50 spec for
898 * more information.
899 *
900 * @param level_count VK_REMAINING_MIP_LEVELS isn't supported.
901 * @param layer_count VK_REMAINING_ARRAY_LAYERS isn't supported. For 3D images,
902 * this represents the maximum layers to transition at each
903 * specified miplevel.
904 */
905 static void
906 transition_color_buffer(struct anv_cmd_buffer *cmd_buffer,
907 const struct anv_image *image,
908 VkImageAspectFlagBits aspect,
909 const uint32_t base_level, uint32_t level_count,
910 uint32_t base_layer, uint32_t layer_count,
911 VkImageLayout initial_layout,
912 VkImageLayout final_layout)
913 {
914 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
915 /* Validate the inputs. */
916 assert(cmd_buffer);
917 assert(image && image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
918 /* These values aren't supported for simplicity's sake. */
919 assert(level_count != VK_REMAINING_MIP_LEVELS &&
920 layer_count != VK_REMAINING_ARRAY_LAYERS);
921 /* Ensure the subresource range is valid. */
922 uint64_t last_level_num = base_level + level_count;
923 const uint32_t max_depth = anv_minify(image->extent.depth, base_level);
924 UNUSED const uint32_t image_layers = MAX2(image->array_size, max_depth);
925 assert((uint64_t)base_layer + layer_count <= image_layers);
926 assert(last_level_num <= image->levels);
927 /* The spec disallows these final layouts. */
928 assert(final_layout != VK_IMAGE_LAYOUT_UNDEFINED &&
929 final_layout != VK_IMAGE_LAYOUT_PREINITIALIZED);
930
931 /* No work is necessary if the layout stays the same or if this subresource
932 * range lacks auxiliary data.
933 */
934 if (initial_layout == final_layout)
935 return;
936
937 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
938
939 if (image->planes[plane].shadow_surface.isl.size > 0 &&
940 final_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
941 /* This surface is a linear compressed image with a tiled shadow surface
942 * for texturing. The client is about to use it in READ_ONLY_OPTIMAL so
943 * we need to ensure the shadow copy is up-to-date.
944 */
945 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
946 assert(image->planes[plane].surface.isl.tiling == ISL_TILING_LINEAR);
947 assert(image->planes[plane].shadow_surface.isl.tiling != ISL_TILING_LINEAR);
948 assert(isl_format_is_compressed(image->planes[plane].surface.isl.format));
949 assert(plane == 0);
950 anv_image_copy_to_shadow(cmd_buffer, image,
951 base_level, level_count,
952 base_layer, layer_count);
953 }
954
955 if (base_layer >= anv_image_aux_layers(image, aspect, base_level))
956 return;
957
958 assert(image->tiling == VK_IMAGE_TILING_OPTIMAL);
959
960 if (initial_layout == VK_IMAGE_LAYOUT_UNDEFINED ||
961 initial_layout == VK_IMAGE_LAYOUT_PREINITIALIZED) {
962 /* A subresource in the undefined layout may have been aliased and
963 * populated with any arrangement of bits. Therefore, we must initialize
964 * the related aux buffer and clear buffer entry with desirable values.
965 * An initial layout of PREINITIALIZED is the same as UNDEFINED for
966 * images with VK_IMAGE_TILING_OPTIMAL.
967 *
968 * Initialize the relevant clear buffer entries.
969 */
970 if (base_level == 0 && base_layer == 0)
971 init_fast_clear_color(cmd_buffer, image, aspect);
972
973 /* Initialize the aux buffers to enable correct rendering. In order to
974 * ensure that things such as storage images work correctly, aux buffers
975 * need to be initialized to valid data.
976 *
977 * Having an aux buffer with invalid data is a problem for two reasons:
978 *
979 * 1) Having an invalid value in the buffer can confuse the hardware.
980 * For instance, with CCS_E on SKL, a two-bit CCS value of 2 is
981 * invalid and leads to the hardware doing strange things. It
982 * doesn't hang as far as we can tell but rendering corruption can
983 * occur.
984 *
985 * 2) If this transition is into the GENERAL layout and we then use the
986 * image as a storage image, then we must have the aux buffer in the
987 * pass-through state so that, if we then go to texture from the
988 * image, we get the results of our storage image writes and not the
989 * fast clear color or other random data.
990 *
991 * For CCS both of the problems above are real demonstrable issues. In
992 * that case, the only thing we can do is to perform an ambiguate to
993 * transition the aux surface into the pass-through state.
994 *
995 * For MCS, (2) is never an issue because we don't support multisampled
996 * storage images. In theory, issue (1) is a problem with MCS but we've
997 * never seen it in the wild. For 4x and 16x, all bit patters could, in
998 * theory, be interpreted as something but we don't know that all bit
999 * patterns are actually valid. For 2x and 8x, you could easily end up
1000 * with the MCS referring to an invalid plane because not all bits of
1001 * the MCS value are actually used. Even though we've never seen issues
1002 * in the wild, it's best to play it safe and initialize the MCS. We
1003 * can use a fast-clear for MCS because we only ever touch from render
1004 * and texture (no image load store).
1005 */
1006 if (image->samples == 1) {
1007 for (uint32_t l = 0; l < level_count; l++) {
1008 const uint32_t level = base_level + l;
1009
1010 uint32_t aux_layers = anv_image_aux_layers(image, aspect, level);
1011 if (base_layer >= aux_layers)
1012 break; /* We will only get fewer layers as level increases */
1013 uint32_t level_layer_count =
1014 MIN2(layer_count, aux_layers - base_layer);
1015
1016 anv_image_ccs_op(cmd_buffer, image, aspect, level,
1017 base_layer, level_layer_count,
1018 ISL_AUX_OP_AMBIGUATE, false);
1019
1020 if (image->planes[plane].aux_usage == ISL_AUX_USAGE_CCS_E) {
1021 set_image_compressed_bit(cmd_buffer, image, aspect,
1022 level, base_layer, level_layer_count,
1023 false);
1024 }
1025 }
1026 } else {
1027 if (image->samples == 4 || image->samples == 16) {
1028 anv_perf_warn(cmd_buffer->device->instance, image,
1029 "Doing a potentially unnecessary fast-clear to "
1030 "define an MCS buffer.");
1031 }
1032
1033 assert(base_level == 0 && level_count == 1);
1034 anv_image_mcs_op(cmd_buffer, image, aspect,
1035 base_layer, layer_count,
1036 ISL_AUX_OP_FAST_CLEAR, false);
1037 }
1038 return;
1039 }
1040
1041 const enum isl_aux_usage initial_aux_usage =
1042 anv_layout_to_aux_usage(devinfo, image, aspect, initial_layout);
1043 const enum isl_aux_usage final_aux_usage =
1044 anv_layout_to_aux_usage(devinfo, image, aspect, final_layout);
1045
1046 /* The current code assumes that there is no mixing of CCS_E and CCS_D.
1047 * We can handle transitions between CCS_D/E to and from NONE. What we
1048 * don't yet handle is switching between CCS_E and CCS_D within a given
1049 * image. Doing so in a performant way requires more detailed aux state
1050 * tracking such as what is done in i965. For now, just assume that we
1051 * only have one type of compression.
1052 */
1053 assert(initial_aux_usage == ISL_AUX_USAGE_NONE ||
1054 final_aux_usage == ISL_AUX_USAGE_NONE ||
1055 initial_aux_usage == final_aux_usage);
1056
1057 /* If initial aux usage is NONE, there is nothing to resolve */
1058 if (initial_aux_usage == ISL_AUX_USAGE_NONE)
1059 return;
1060
1061 enum isl_aux_op resolve_op = ISL_AUX_OP_NONE;
1062
1063 /* If the initial layout supports more fast clear than the final layout
1064 * then we need at least a partial resolve.
1065 */
1066 const enum anv_fast_clear_type initial_fast_clear =
1067 anv_layout_to_fast_clear_type(devinfo, image, aspect, initial_layout);
1068 const enum anv_fast_clear_type final_fast_clear =
1069 anv_layout_to_fast_clear_type(devinfo, image, aspect, final_layout);
1070 if (final_fast_clear < initial_fast_clear)
1071 resolve_op = ISL_AUX_OP_PARTIAL_RESOLVE;
1072
1073 if (initial_aux_usage == ISL_AUX_USAGE_CCS_E &&
1074 final_aux_usage != ISL_AUX_USAGE_CCS_E)
1075 resolve_op = ISL_AUX_OP_FULL_RESOLVE;
1076
1077 if (resolve_op == ISL_AUX_OP_NONE)
1078 return;
1079
1080 /* Perform a resolve to synchronize data between the main and aux buffer.
1081 * Before we begin, we must satisfy the cache flushing requirement specified
1082 * in the Sky Lake PRM Vol. 7, "MCS Buffer for Render Target(s)":
1083 *
1084 * Any transition from any value in {Clear, Render, Resolve} to a
1085 * different value in {Clear, Render, Resolve} requires end of pipe
1086 * synchronization.
1087 *
1088 * We perform a flush of the write cache before and after the clear and
1089 * resolve operations to meet this requirement.
1090 *
1091 * Unlike other drawing, fast clear operations are not properly
1092 * synchronized. The first PIPE_CONTROL here likely ensures that the
1093 * contents of the previous render or clear hit the render target before we
1094 * resolve and the second likely ensures that the resolve is complete before
1095 * we do any more rendering or clearing.
1096 */
1097 cmd_buffer->state.pending_pipe_bits |=
1098 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT | ANV_PIPE_CS_STALL_BIT;
1099
1100 for (uint32_t l = 0; l < level_count; l++) {
1101 uint32_t level = base_level + l;
1102
1103 uint32_t aux_layers = anv_image_aux_layers(image, aspect, level);
1104 if (base_layer >= aux_layers)
1105 break; /* We will only get fewer layers as level increases */
1106 uint32_t level_layer_count =
1107 MIN2(layer_count, aux_layers - base_layer);
1108
1109 for (uint32_t a = 0; a < level_layer_count; a++) {
1110 uint32_t array_layer = base_layer + a;
1111 if (image->samples == 1) {
1112 anv_cmd_predicated_ccs_resolve(cmd_buffer, image, aspect,
1113 level, array_layer, resolve_op,
1114 final_fast_clear);
1115 } else {
1116 anv_cmd_predicated_mcs_resolve(cmd_buffer, image, aspect,
1117 array_layer, resolve_op,
1118 final_fast_clear);
1119 }
1120 }
1121 }
1122
1123 cmd_buffer->state.pending_pipe_bits |=
1124 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT | ANV_PIPE_CS_STALL_BIT;
1125 }
1126
1127 /**
1128 * Setup anv_cmd_state::attachments for vkCmdBeginRenderPass.
1129 */
1130 static VkResult
1131 genX(cmd_buffer_setup_attachments)(struct anv_cmd_buffer *cmd_buffer,
1132 struct anv_render_pass *pass,
1133 const VkRenderPassBeginInfo *begin)
1134 {
1135 const struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
1136 struct anv_cmd_state *state = &cmd_buffer->state;
1137
1138 vk_free(&cmd_buffer->pool->alloc, state->attachments);
1139
1140 if (pass->attachment_count > 0) {
1141 state->attachments = vk_alloc(&cmd_buffer->pool->alloc,
1142 pass->attachment_count *
1143 sizeof(state->attachments[0]),
1144 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1145 if (state->attachments == NULL) {
1146 /* Propagate VK_ERROR_OUT_OF_HOST_MEMORY to vkEndCommandBuffer */
1147 return anv_batch_set_error(&cmd_buffer->batch,
1148 VK_ERROR_OUT_OF_HOST_MEMORY);
1149 }
1150 } else {
1151 state->attachments = NULL;
1152 }
1153
1154 /* Reserve one for the NULL state. */
1155 unsigned num_states = 1;
1156 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
1157 if (vk_format_is_color(pass->attachments[i].format))
1158 num_states++;
1159
1160 if (need_input_attachment_state(&pass->attachments[i]))
1161 num_states++;
1162 }
1163
1164 const uint32_t ss_stride = align_u32(isl_dev->ss.size, isl_dev->ss.align);
1165 state->render_pass_states =
1166 anv_state_stream_alloc(&cmd_buffer->surface_state_stream,
1167 num_states * ss_stride, isl_dev->ss.align);
1168
1169 struct anv_state next_state = state->render_pass_states;
1170 next_state.alloc_size = isl_dev->ss.size;
1171
1172 state->null_surface_state = next_state;
1173 next_state.offset += ss_stride;
1174 next_state.map += ss_stride;
1175
1176 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
1177 if (vk_format_is_color(pass->attachments[i].format)) {
1178 state->attachments[i].color.state = next_state;
1179 next_state.offset += ss_stride;
1180 next_state.map += ss_stride;
1181 }
1182
1183 if (need_input_attachment_state(&pass->attachments[i])) {
1184 state->attachments[i].input.state = next_state;
1185 next_state.offset += ss_stride;
1186 next_state.map += ss_stride;
1187 }
1188 }
1189 assert(next_state.offset == state->render_pass_states.offset +
1190 state->render_pass_states.alloc_size);
1191
1192 if (begin) {
1193 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, begin->framebuffer);
1194 assert(pass->attachment_count == framebuffer->attachment_count);
1195
1196 isl_null_fill_state(isl_dev, state->null_surface_state.map,
1197 isl_extent3d(framebuffer->width,
1198 framebuffer->height,
1199 framebuffer->layers));
1200
1201 for (uint32_t i = 0; i < pass->attachment_count; ++i) {
1202 struct anv_render_pass_attachment *att = &pass->attachments[i];
1203 VkImageAspectFlags att_aspects = vk_format_aspects(att->format);
1204 VkImageAspectFlags clear_aspects = 0;
1205 VkImageAspectFlags load_aspects = 0;
1206
1207 if (att_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1208 /* color attachment */
1209 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1210 clear_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
1211 } else if (att->load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
1212 load_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
1213 }
1214 } else {
1215 /* depthstencil attachment */
1216 if (att_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
1217 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1218 clear_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1219 } else if (att->load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
1220 load_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1221 }
1222 }
1223 if (att_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
1224 if (att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1225 clear_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1226 } else if (att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
1227 load_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1228 }
1229 }
1230 }
1231
1232 state->attachments[i].current_layout = att->initial_layout;
1233 state->attachments[i].pending_clear_aspects = clear_aspects;
1234 state->attachments[i].pending_load_aspects = load_aspects;
1235 if (clear_aspects)
1236 state->attachments[i].clear_value = begin->pClearValues[i];
1237
1238 struct anv_image_view *iview = framebuffer->attachments[i];
1239 anv_assert(iview->vk_format == att->format);
1240 anv_assert(iview->n_planes == 1);
1241
1242 const uint32_t num_layers = iview->planes[0].isl.array_len;
1243 state->attachments[i].pending_clear_views = (1 << num_layers) - 1;
1244
1245 union isl_color_value clear_color = { .u32 = { 0, } };
1246 if (att_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1247 assert(att_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
1248 color_attachment_compute_aux_usage(cmd_buffer->device,
1249 state, i, begin->renderArea,
1250 &clear_color);
1251
1252 anv_image_fill_surface_state(cmd_buffer->device,
1253 iview->image,
1254 VK_IMAGE_ASPECT_COLOR_BIT,
1255 &iview->planes[0].isl,
1256 ISL_SURF_USAGE_RENDER_TARGET_BIT,
1257 state->attachments[i].aux_usage,
1258 &clear_color,
1259 0,
1260 &state->attachments[i].color,
1261 NULL);
1262
1263 add_image_view_relocs(cmd_buffer, iview, 0,
1264 state->attachments[i].color);
1265 } else {
1266 depth_stencil_attachment_compute_aux_usage(cmd_buffer->device,
1267 state, i,
1268 begin->renderArea);
1269 }
1270
1271 if (need_input_attachment_state(&pass->attachments[i])) {
1272 anv_image_fill_surface_state(cmd_buffer->device,
1273 iview->image,
1274 VK_IMAGE_ASPECT_COLOR_BIT,
1275 &iview->planes[0].isl,
1276 ISL_SURF_USAGE_TEXTURE_BIT,
1277 state->attachments[i].input_aux_usage,
1278 &clear_color,
1279 0,
1280 &state->attachments[i].input,
1281 NULL);
1282
1283 add_image_view_relocs(cmd_buffer, iview, 0,
1284 state->attachments[i].input);
1285 }
1286 }
1287 }
1288
1289 return VK_SUCCESS;
1290 }
1291
1292 VkResult
1293 genX(BeginCommandBuffer)(
1294 VkCommandBuffer commandBuffer,
1295 const VkCommandBufferBeginInfo* pBeginInfo)
1296 {
1297 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1298
1299 /* If this is the first vkBeginCommandBuffer, we must *initialize* the
1300 * command buffer's state. Otherwise, we must *reset* its state. In both
1301 * cases we reset it.
1302 *
1303 * From the Vulkan 1.0 spec:
1304 *
1305 * If a command buffer is in the executable state and the command buffer
1306 * was allocated from a command pool with the
1307 * VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT flag set, then
1308 * vkBeginCommandBuffer implicitly resets the command buffer, behaving
1309 * as if vkResetCommandBuffer had been called with
1310 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT not set. It then puts
1311 * the command buffer in the recording state.
1312 */
1313 anv_cmd_buffer_reset(cmd_buffer);
1314
1315 cmd_buffer->usage_flags = pBeginInfo->flags;
1316
1317 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY ||
1318 !(cmd_buffer->usage_flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT));
1319
1320 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
1321
1322 /* We sometimes store vertex data in the dynamic state buffer for blorp
1323 * operations and our dynamic state stream may re-use data from previous
1324 * command buffers. In order to prevent stale cache data, we flush the VF
1325 * cache. We could do this on every blorp call but that's not really
1326 * needed as all of the data will get written by the CPU prior to the GPU
1327 * executing anything. The chances are fairly high that they will use
1328 * blorp at least once per primary command buffer so it shouldn't be
1329 * wasted.
1330 */
1331 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY)
1332 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1333
1334 /* We send an "Indirect State Pointers Disable" packet at
1335 * EndCommandBuffer, so all push contant packets are ignored during a
1336 * context restore. Documentation says after that command, we need to
1337 * emit push constants again before any rendering operation. So we
1338 * flag them dirty here to make sure they get emitted.
1339 */
1340 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_ALL_GRAPHICS;
1341
1342 VkResult result = VK_SUCCESS;
1343 if (cmd_buffer->usage_flags &
1344 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
1345 assert(pBeginInfo->pInheritanceInfo);
1346 cmd_buffer->state.pass =
1347 anv_render_pass_from_handle(pBeginInfo->pInheritanceInfo->renderPass);
1348 cmd_buffer->state.subpass =
1349 &cmd_buffer->state.pass->subpasses[pBeginInfo->pInheritanceInfo->subpass];
1350
1351 /* This is optional in the inheritance info. */
1352 cmd_buffer->state.framebuffer =
1353 anv_framebuffer_from_handle(pBeginInfo->pInheritanceInfo->framebuffer);
1354
1355 result = genX(cmd_buffer_setup_attachments)(cmd_buffer,
1356 cmd_buffer->state.pass, NULL);
1357
1358 /* Record that HiZ is enabled if we can. */
1359 if (cmd_buffer->state.framebuffer) {
1360 const struct anv_image_view * const iview =
1361 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
1362
1363 if (iview) {
1364 VkImageLayout layout =
1365 cmd_buffer->state.subpass->depth_stencil_attachment.layout;
1366
1367 enum isl_aux_usage aux_usage =
1368 anv_layout_to_aux_usage(&cmd_buffer->device->info, iview->image,
1369 VK_IMAGE_ASPECT_DEPTH_BIT, layout);
1370
1371 cmd_buffer->state.hiz_enabled = aux_usage == ISL_AUX_USAGE_HIZ;
1372 }
1373 }
1374
1375 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
1376 }
1377
1378 return result;
1379 }
1380
1381 /* From the PRM, Volume 2a:
1382 *
1383 * "Indirect State Pointers Disable
1384 *
1385 * At the completion of the post-sync operation associated with this pipe
1386 * control packet, the indirect state pointers in the hardware are
1387 * considered invalid; the indirect pointers are not saved in the context.
1388 * If any new indirect state commands are executed in the command stream
1389 * while the pipe control is pending, the new indirect state commands are
1390 * preserved.
1391 *
1392 * [DevIVB+]: Using Invalidate State Pointer (ISP) only inhibits context
1393 * restoring of Push Constant (3DSTATE_CONSTANT_*) commands. Push Constant
1394 * commands are only considered as Indirect State Pointers. Once ISP is
1395 * issued in a context, SW must initialize by programming push constant
1396 * commands for all the shaders (at least to zero length) before attempting
1397 * any rendering operation for the same context."
1398 *
1399 * 3DSTATE_CONSTANT_* packets are restored during a context restore,
1400 * even though they point to a BO that has been already unreferenced at
1401 * the end of the previous batch buffer. This has been fine so far since
1402 * we are protected by these scratch page (every address not covered by
1403 * a BO should be pointing to the scratch page). But on CNL, it is
1404 * causing a GPU hang during context restore at the 3DSTATE_CONSTANT_*
1405 * instruction.
1406 *
1407 * The flag "Indirect State Pointers Disable" in PIPE_CONTROL tells the
1408 * hardware to ignore previous 3DSTATE_CONSTANT_* packets during a
1409 * context restore, so the mentioned hang doesn't happen. However,
1410 * software must program push constant commands for all stages prior to
1411 * rendering anything. So we flag them dirty in BeginCommandBuffer.
1412 */
1413 static void
1414 emit_isp_disable(struct anv_cmd_buffer *cmd_buffer)
1415 {
1416 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1417 pc.IndirectStatePointersDisable = true;
1418 pc.CommandStreamerStallEnable = true;
1419 }
1420 }
1421
1422 VkResult
1423 genX(EndCommandBuffer)(
1424 VkCommandBuffer commandBuffer)
1425 {
1426 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1427
1428 if (anv_batch_has_error(&cmd_buffer->batch))
1429 return cmd_buffer->batch.status;
1430
1431 /* We want every command buffer to start with the PMA fix in a known state,
1432 * so we disable it at the end of the command buffer.
1433 */
1434 genX(cmd_buffer_enable_pma_fix)(cmd_buffer, false);
1435
1436 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
1437
1438 emit_isp_disable(cmd_buffer);
1439
1440 anv_cmd_buffer_end_batch_buffer(cmd_buffer);
1441
1442 return VK_SUCCESS;
1443 }
1444
1445 void
1446 genX(CmdExecuteCommands)(
1447 VkCommandBuffer commandBuffer,
1448 uint32_t commandBufferCount,
1449 const VkCommandBuffer* pCmdBuffers)
1450 {
1451 ANV_FROM_HANDLE(anv_cmd_buffer, primary, commandBuffer);
1452
1453 assert(primary->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1454
1455 if (anv_batch_has_error(&primary->batch))
1456 return;
1457
1458 /* The secondary command buffers will assume that the PMA fix is disabled
1459 * when they begin executing. Make sure this is true.
1460 */
1461 genX(cmd_buffer_enable_pma_fix)(primary, false);
1462
1463 /* The secondary command buffer doesn't know which textures etc. have been
1464 * flushed prior to their execution. Apply those flushes now.
1465 */
1466 genX(cmd_buffer_apply_pipe_flushes)(primary);
1467
1468 for (uint32_t i = 0; i < commandBufferCount; i++) {
1469 ANV_FROM_HANDLE(anv_cmd_buffer, secondary, pCmdBuffers[i]);
1470
1471 assert(secondary->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY);
1472 assert(!anv_batch_has_error(&secondary->batch));
1473
1474 if (secondary->usage_flags &
1475 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
1476 /* If we're continuing a render pass from the primary, we need to
1477 * copy the surface states for the current subpass into the storage
1478 * we allocated for them in BeginCommandBuffer.
1479 */
1480 struct anv_bo *ss_bo =
1481 &primary->device->surface_state_pool.block_pool.bo;
1482 struct anv_state src_state = primary->state.render_pass_states;
1483 struct anv_state dst_state = secondary->state.render_pass_states;
1484 assert(src_state.alloc_size == dst_state.alloc_size);
1485
1486 genX(cmd_buffer_so_memcpy)(primary, ss_bo, dst_state.offset,
1487 ss_bo, src_state.offset,
1488 src_state.alloc_size);
1489 }
1490
1491 anv_cmd_buffer_add_secondary(primary, secondary);
1492 }
1493
1494 /* The secondary may have selected a different pipeline (3D or compute) and
1495 * may have changed the current L3$ configuration. Reset our tracking
1496 * variables to invalid values to ensure that we re-emit these in the case
1497 * where we do any draws or compute dispatches from the primary after the
1498 * secondary has returned.
1499 */
1500 primary->state.current_pipeline = UINT32_MAX;
1501 primary->state.current_l3_config = NULL;
1502
1503 /* Each of the secondary command buffers will use its own state base
1504 * address. We need to re-emit state base address for the primary after
1505 * all of the secondaries are done.
1506 *
1507 * TODO: Maybe we want to make this a dirty bit to avoid extra state base
1508 * address calls?
1509 */
1510 genX(cmd_buffer_emit_state_base_address)(primary);
1511 }
1512
1513 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
1514 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
1515 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
1516
1517 /**
1518 * Program the hardware to use the specified L3 configuration.
1519 */
1520 void
1521 genX(cmd_buffer_config_l3)(struct anv_cmd_buffer *cmd_buffer,
1522 const struct gen_l3_config *cfg)
1523 {
1524 assert(cfg);
1525 if (cfg == cmd_buffer->state.current_l3_config)
1526 return;
1527
1528 if (unlikely(INTEL_DEBUG & DEBUG_L3)) {
1529 intel_logd("L3 config transition: ");
1530 gen_dump_l3_config(cfg, stderr);
1531 }
1532
1533 const bool has_slm = cfg->n[GEN_L3P_SLM];
1534
1535 /* According to the hardware docs, the L3 partitioning can only be changed
1536 * while the pipeline is completely drained and the caches are flushed,
1537 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
1538 */
1539 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1540 pc.DCFlushEnable = true;
1541 pc.PostSyncOperation = NoWrite;
1542 pc.CommandStreamerStallEnable = true;
1543 }
1544
1545 /* ...followed by a second pipelined PIPE_CONTROL that initiates
1546 * invalidation of the relevant caches. Note that because RO invalidation
1547 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
1548 * command is processed by the CS) we cannot combine it with the previous
1549 * stalling flush as the hardware documentation suggests, because that
1550 * would cause the CS to stall on previous rendering *after* RO
1551 * invalidation and wouldn't prevent the RO caches from being polluted by
1552 * concurrent rendering before the stall completes. This intentionally
1553 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
1554 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
1555 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
1556 * already guarantee that there is no concurrent GPGPU kernel execution
1557 * (see SKL HSD 2132585).
1558 */
1559 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1560 pc.TextureCacheInvalidationEnable = true;
1561 pc.ConstantCacheInvalidationEnable = true;
1562 pc.InstructionCacheInvalidateEnable = true;
1563 pc.StateCacheInvalidationEnable = true;
1564 pc.PostSyncOperation = NoWrite;
1565 }
1566
1567 /* Now send a third stalling flush to make sure that invalidation is
1568 * complete when the L3 configuration registers are modified.
1569 */
1570 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1571 pc.DCFlushEnable = true;
1572 pc.PostSyncOperation = NoWrite;
1573 pc.CommandStreamerStallEnable = true;
1574 }
1575
1576 #if GEN_GEN >= 8
1577
1578 assert(!cfg->n[GEN_L3P_IS] && !cfg->n[GEN_L3P_C] && !cfg->n[GEN_L3P_T]);
1579
1580 uint32_t l3cr;
1581 anv_pack_struct(&l3cr, GENX(L3CNTLREG),
1582 .SLMEnable = has_slm,
1583 .URBAllocation = cfg->n[GEN_L3P_URB],
1584 .ROAllocation = cfg->n[GEN_L3P_RO],
1585 .DCAllocation = cfg->n[GEN_L3P_DC],
1586 .AllAllocation = cfg->n[GEN_L3P_ALL]);
1587
1588 /* Set up the L3 partitioning. */
1589 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG_num), l3cr);
1590
1591 #else
1592
1593 const bool has_dc = cfg->n[GEN_L3P_DC] || cfg->n[GEN_L3P_ALL];
1594 const bool has_is = cfg->n[GEN_L3P_IS] || cfg->n[GEN_L3P_RO] ||
1595 cfg->n[GEN_L3P_ALL];
1596 const bool has_c = cfg->n[GEN_L3P_C] || cfg->n[GEN_L3P_RO] ||
1597 cfg->n[GEN_L3P_ALL];
1598 const bool has_t = cfg->n[GEN_L3P_T] || cfg->n[GEN_L3P_RO] ||
1599 cfg->n[GEN_L3P_ALL];
1600
1601 assert(!cfg->n[GEN_L3P_ALL]);
1602
1603 /* When enabled SLM only uses a portion of the L3 on half of the banks,
1604 * the matching space on the remaining banks has to be allocated to a
1605 * client (URB for all validated configurations) set to the
1606 * lower-bandwidth 2-bank address hashing mode.
1607 */
1608 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
1609 const bool urb_low_bw = has_slm && !devinfo->is_baytrail;
1610 assert(!urb_low_bw || cfg->n[GEN_L3P_URB] == cfg->n[GEN_L3P_SLM]);
1611
1612 /* Minimum number of ways that can be allocated to the URB. */
1613 MAYBE_UNUSED const unsigned n0_urb = devinfo->is_baytrail ? 32 : 0;
1614 assert(cfg->n[GEN_L3P_URB] >= n0_urb);
1615
1616 uint32_t l3sqcr1, l3cr2, l3cr3;
1617 anv_pack_struct(&l3sqcr1, GENX(L3SQCREG1),
1618 .ConvertDC_UC = !has_dc,
1619 .ConvertIS_UC = !has_is,
1620 .ConvertC_UC = !has_c,
1621 .ConvertT_UC = !has_t);
1622 l3sqcr1 |=
1623 GEN_IS_HASWELL ? HSW_L3SQCREG1_SQGHPCI_DEFAULT :
1624 devinfo->is_baytrail ? VLV_L3SQCREG1_SQGHPCI_DEFAULT :
1625 IVB_L3SQCREG1_SQGHPCI_DEFAULT;
1626
1627 anv_pack_struct(&l3cr2, GENX(L3CNTLREG2),
1628 .SLMEnable = has_slm,
1629 .URBLowBandwidth = urb_low_bw,
1630 .URBAllocation = cfg->n[GEN_L3P_URB] - n0_urb,
1631 #if !GEN_IS_HASWELL
1632 .ALLAllocation = cfg->n[GEN_L3P_ALL],
1633 #endif
1634 .ROAllocation = cfg->n[GEN_L3P_RO],
1635 .DCAllocation = cfg->n[GEN_L3P_DC]);
1636
1637 anv_pack_struct(&l3cr3, GENX(L3CNTLREG3),
1638 .ISAllocation = cfg->n[GEN_L3P_IS],
1639 .ISLowBandwidth = 0,
1640 .CAllocation = cfg->n[GEN_L3P_C],
1641 .CLowBandwidth = 0,
1642 .TAllocation = cfg->n[GEN_L3P_T],
1643 .TLowBandwidth = 0);
1644
1645 /* Set up the L3 partitioning. */
1646 emit_lri(&cmd_buffer->batch, GENX(L3SQCREG1_num), l3sqcr1);
1647 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG2_num), l3cr2);
1648 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG3_num), l3cr3);
1649
1650 #if GEN_IS_HASWELL
1651 if (cmd_buffer->device->instance->physicalDevice.cmd_parser_version >= 4) {
1652 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
1653 * them disabled to avoid crashing the system hard.
1654 */
1655 uint32_t scratch1, chicken3;
1656 anv_pack_struct(&scratch1, GENX(SCRATCH1),
1657 .L3AtomicDisable = !has_dc);
1658 anv_pack_struct(&chicken3, GENX(CHICKEN3),
1659 .L3AtomicDisableMask = true,
1660 .L3AtomicDisable = !has_dc);
1661 emit_lri(&cmd_buffer->batch, GENX(SCRATCH1_num), scratch1);
1662 emit_lri(&cmd_buffer->batch, GENX(CHICKEN3_num), chicken3);
1663 }
1664 #endif
1665
1666 #endif
1667
1668 cmd_buffer->state.current_l3_config = cfg;
1669 }
1670
1671 void
1672 genX(cmd_buffer_apply_pipe_flushes)(struct anv_cmd_buffer *cmd_buffer)
1673 {
1674 enum anv_pipe_bits bits = cmd_buffer->state.pending_pipe_bits;
1675
1676 /* Flushes are pipelined while invalidations are handled immediately.
1677 * Therefore, if we're flushing anything then we need to schedule a stall
1678 * before any invalidations can happen.
1679 */
1680 if (bits & ANV_PIPE_FLUSH_BITS)
1681 bits |= ANV_PIPE_NEEDS_CS_STALL_BIT;
1682
1683 /* If we're going to do an invalidate and we have a pending CS stall that
1684 * has yet to be resolved, we do the CS stall now.
1685 */
1686 if ((bits & ANV_PIPE_INVALIDATE_BITS) &&
1687 (bits & ANV_PIPE_NEEDS_CS_STALL_BIT)) {
1688 bits |= ANV_PIPE_CS_STALL_BIT;
1689 bits &= ~ANV_PIPE_NEEDS_CS_STALL_BIT;
1690 }
1691
1692 if (bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT)) {
1693 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
1694 pipe.DepthCacheFlushEnable = bits & ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
1695 pipe.DCFlushEnable = bits & ANV_PIPE_DATA_CACHE_FLUSH_BIT;
1696 pipe.RenderTargetCacheFlushEnable =
1697 bits & ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
1698
1699 pipe.DepthStallEnable = bits & ANV_PIPE_DEPTH_STALL_BIT;
1700 pipe.CommandStreamerStallEnable = bits & ANV_PIPE_CS_STALL_BIT;
1701 pipe.StallAtPixelScoreboard = bits & ANV_PIPE_STALL_AT_SCOREBOARD_BIT;
1702
1703 /*
1704 * According to the Broadwell documentation, any PIPE_CONTROL with the
1705 * "Command Streamer Stall" bit set must also have another bit set,
1706 * with five different options:
1707 *
1708 * - Render Target Cache Flush
1709 * - Depth Cache Flush
1710 * - Stall at Pixel Scoreboard
1711 * - Post-Sync Operation
1712 * - Depth Stall
1713 * - DC Flush Enable
1714 *
1715 * I chose "Stall at Pixel Scoreboard" since that's what we use in
1716 * mesa and it seems to work fine. The choice is fairly arbitrary.
1717 */
1718 if ((bits & ANV_PIPE_CS_STALL_BIT) &&
1719 !(bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_DEPTH_STALL_BIT |
1720 ANV_PIPE_STALL_AT_SCOREBOARD_BIT)))
1721 pipe.StallAtPixelScoreboard = true;
1722 }
1723
1724 bits &= ~(ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT);
1725 }
1726
1727 if (bits & ANV_PIPE_INVALIDATE_BITS) {
1728 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
1729 pipe.StateCacheInvalidationEnable =
1730 bits & ANV_PIPE_STATE_CACHE_INVALIDATE_BIT;
1731 pipe.ConstantCacheInvalidationEnable =
1732 bits & ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT;
1733 pipe.VFCacheInvalidationEnable =
1734 bits & ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
1735 pipe.TextureCacheInvalidationEnable =
1736 bits & ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
1737 pipe.InstructionCacheInvalidateEnable =
1738 bits & ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT;
1739 }
1740
1741 bits &= ~ANV_PIPE_INVALIDATE_BITS;
1742 }
1743
1744 cmd_buffer->state.pending_pipe_bits = bits;
1745 }
1746
1747 void genX(CmdPipelineBarrier)(
1748 VkCommandBuffer commandBuffer,
1749 VkPipelineStageFlags srcStageMask,
1750 VkPipelineStageFlags destStageMask,
1751 VkBool32 byRegion,
1752 uint32_t memoryBarrierCount,
1753 const VkMemoryBarrier* pMemoryBarriers,
1754 uint32_t bufferMemoryBarrierCount,
1755 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1756 uint32_t imageMemoryBarrierCount,
1757 const VkImageMemoryBarrier* pImageMemoryBarriers)
1758 {
1759 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1760
1761 /* XXX: Right now, we're really dumb and just flush whatever categories
1762 * the app asks for. One of these days we may make this a bit better
1763 * but right now that's all the hardware allows for in most areas.
1764 */
1765 VkAccessFlags src_flags = 0;
1766 VkAccessFlags dst_flags = 0;
1767
1768 for (uint32_t i = 0; i < memoryBarrierCount; i++) {
1769 src_flags |= pMemoryBarriers[i].srcAccessMask;
1770 dst_flags |= pMemoryBarriers[i].dstAccessMask;
1771 }
1772
1773 for (uint32_t i = 0; i < bufferMemoryBarrierCount; i++) {
1774 src_flags |= pBufferMemoryBarriers[i].srcAccessMask;
1775 dst_flags |= pBufferMemoryBarriers[i].dstAccessMask;
1776 }
1777
1778 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1779 src_flags |= pImageMemoryBarriers[i].srcAccessMask;
1780 dst_flags |= pImageMemoryBarriers[i].dstAccessMask;
1781 ANV_FROM_HANDLE(anv_image, image, pImageMemoryBarriers[i].image);
1782 const VkImageSubresourceRange *range =
1783 &pImageMemoryBarriers[i].subresourceRange;
1784
1785 if (range->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1786 transition_depth_buffer(cmd_buffer, image,
1787 pImageMemoryBarriers[i].oldLayout,
1788 pImageMemoryBarriers[i].newLayout);
1789 } else if (range->aspectMask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1790 VkImageAspectFlags color_aspects =
1791 anv_image_expand_aspects(image, range->aspectMask);
1792 uint32_t aspect_bit;
1793
1794 uint32_t base_layer, layer_count;
1795 if (image->type == VK_IMAGE_TYPE_3D) {
1796 base_layer = 0;
1797 layer_count = anv_minify(image->extent.depth, range->baseMipLevel);
1798 } else {
1799 base_layer = range->baseArrayLayer;
1800 layer_count = anv_get_layerCount(image, range);
1801 }
1802
1803 anv_foreach_image_aspect_bit(aspect_bit, image, color_aspects) {
1804 transition_color_buffer(cmd_buffer, image, 1UL << aspect_bit,
1805 range->baseMipLevel,
1806 anv_get_levelCount(image, range),
1807 base_layer, layer_count,
1808 pImageMemoryBarriers[i].oldLayout,
1809 pImageMemoryBarriers[i].newLayout);
1810 }
1811 }
1812 }
1813
1814 cmd_buffer->state.pending_pipe_bits |=
1815 anv_pipe_flush_bits_for_access_flags(src_flags) |
1816 anv_pipe_invalidate_bits_for_access_flags(dst_flags);
1817 }
1818
1819 static void
1820 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer *cmd_buffer)
1821 {
1822 VkShaderStageFlags stages =
1823 cmd_buffer->state.gfx.base.pipeline->active_stages;
1824
1825 /* In order to avoid thrash, we assume that vertex and fragment stages
1826 * always exist. In the rare case where one is missing *and* the other
1827 * uses push concstants, this may be suboptimal. However, avoiding stalls
1828 * seems more important.
1829 */
1830 stages |= VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT;
1831
1832 if (stages == cmd_buffer->state.push_constant_stages)
1833 return;
1834
1835 #if GEN_GEN >= 8
1836 const unsigned push_constant_kb = 32;
1837 #elif GEN_IS_HASWELL
1838 const unsigned push_constant_kb = cmd_buffer->device->info.gt == 3 ? 32 : 16;
1839 #else
1840 const unsigned push_constant_kb = 16;
1841 #endif
1842
1843 const unsigned num_stages =
1844 _mesa_bitcount(stages & VK_SHADER_STAGE_ALL_GRAPHICS);
1845 unsigned size_per_stage = push_constant_kb / num_stages;
1846
1847 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
1848 * units of 2KB. Incidentally, these are the same platforms that have
1849 * 32KB worth of push constant space.
1850 */
1851 if (push_constant_kb == 32)
1852 size_per_stage &= ~1u;
1853
1854 uint32_t kb_used = 0;
1855 for (int i = MESA_SHADER_VERTEX; i < MESA_SHADER_FRAGMENT; i++) {
1856 unsigned push_size = (stages & (1 << i)) ? size_per_stage : 0;
1857 anv_batch_emit(&cmd_buffer->batch,
1858 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
1859 alloc._3DCommandSubOpcode = 18 + i;
1860 alloc.ConstantBufferOffset = (push_size > 0) ? kb_used : 0;
1861 alloc.ConstantBufferSize = push_size;
1862 }
1863 kb_used += push_size;
1864 }
1865
1866 anv_batch_emit(&cmd_buffer->batch,
1867 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS), alloc) {
1868 alloc.ConstantBufferOffset = kb_used;
1869 alloc.ConstantBufferSize = push_constant_kb - kb_used;
1870 }
1871
1872 cmd_buffer->state.push_constant_stages = stages;
1873
1874 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
1875 *
1876 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
1877 * the next 3DPRIMITIVE command after programming the
1878 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
1879 *
1880 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
1881 * pipeline setup, we need to dirty push constants.
1882 */
1883 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_ALL_GRAPHICS;
1884 }
1885
1886 static const struct anv_descriptor *
1887 anv_descriptor_for_binding(const struct anv_cmd_pipeline_state *pipe_state,
1888 const struct anv_pipeline_binding *binding)
1889 {
1890 assert(binding->set < MAX_SETS);
1891 const struct anv_descriptor_set *set =
1892 pipe_state->descriptors[binding->set];
1893 const uint32_t offset =
1894 set->layout->binding[binding->binding].descriptor_index;
1895 return &set->descriptors[offset + binding->index];
1896 }
1897
1898 static uint32_t
1899 dynamic_offset_for_binding(const struct anv_cmd_pipeline_state *pipe_state,
1900 const struct anv_pipeline_binding *binding)
1901 {
1902 assert(binding->set < MAX_SETS);
1903 const struct anv_descriptor_set *set =
1904 pipe_state->descriptors[binding->set];
1905
1906 uint32_t dynamic_offset_idx =
1907 pipe_state->layout->set[binding->set].dynamic_offset_start +
1908 set->layout->binding[binding->binding].dynamic_offset_index +
1909 binding->index;
1910
1911 return pipe_state->dynamic_offsets[dynamic_offset_idx];
1912 }
1913
1914 static VkResult
1915 emit_binding_table(struct anv_cmd_buffer *cmd_buffer,
1916 gl_shader_stage stage,
1917 struct anv_state *bt_state)
1918 {
1919 struct anv_subpass *subpass = cmd_buffer->state.subpass;
1920 struct anv_cmd_pipeline_state *pipe_state;
1921 struct anv_pipeline *pipeline;
1922 uint32_t bias, state_offset;
1923
1924 switch (stage) {
1925 case MESA_SHADER_COMPUTE:
1926 pipe_state = &cmd_buffer->state.compute.base;
1927 bias = 1;
1928 break;
1929 default:
1930 pipe_state = &cmd_buffer->state.gfx.base;
1931 bias = 0;
1932 break;
1933 }
1934 pipeline = pipe_state->pipeline;
1935
1936 if (!anv_pipeline_has_stage(pipeline, stage)) {
1937 *bt_state = (struct anv_state) { 0, };
1938 return VK_SUCCESS;
1939 }
1940
1941 struct anv_pipeline_bind_map *map = &pipeline->shaders[stage]->bind_map;
1942 if (bias + map->surface_count == 0) {
1943 *bt_state = (struct anv_state) { 0, };
1944 return VK_SUCCESS;
1945 }
1946
1947 *bt_state = anv_cmd_buffer_alloc_binding_table(cmd_buffer,
1948 bias + map->surface_count,
1949 &state_offset);
1950 uint32_t *bt_map = bt_state->map;
1951
1952 if (bt_state->map == NULL)
1953 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
1954
1955 if (stage == MESA_SHADER_COMPUTE &&
1956 get_cs_prog_data(pipeline)->uses_num_work_groups) {
1957 struct anv_bo *bo = cmd_buffer->state.compute.num_workgroups.bo;
1958 uint32_t bo_offset = cmd_buffer->state.compute.num_workgroups.offset;
1959
1960 struct anv_state surface_state;
1961 surface_state =
1962 anv_cmd_buffer_alloc_surface_state(cmd_buffer);
1963
1964 const enum isl_format format =
1965 anv_isl_format_for_descriptor_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
1966 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
1967 format, bo_offset, 12, 1);
1968
1969 bt_map[0] = surface_state.offset + state_offset;
1970 add_surface_state_reloc(cmd_buffer, surface_state, bo, bo_offset);
1971 }
1972
1973 if (map->surface_count == 0)
1974 goto out;
1975
1976 if (map->image_count > 0) {
1977 VkResult result =
1978 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, stage, images);
1979 if (result != VK_SUCCESS)
1980 return result;
1981
1982 cmd_buffer->state.push_constants_dirty |= 1 << stage;
1983 }
1984
1985 uint32_t image = 0;
1986 for (uint32_t s = 0; s < map->surface_count; s++) {
1987 struct anv_pipeline_binding *binding = &map->surface_to_descriptor[s];
1988
1989 struct anv_state surface_state;
1990
1991 if (binding->set == ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS) {
1992 /* Color attachment binding */
1993 assert(stage == MESA_SHADER_FRAGMENT);
1994 assert(binding->binding == 0);
1995 if (binding->index < subpass->color_count) {
1996 const unsigned att =
1997 subpass->color_attachments[binding->index].attachment;
1998
1999 /* From the Vulkan 1.0.46 spec:
2000 *
2001 * "If any color or depth/stencil attachments are
2002 * VK_ATTACHMENT_UNUSED, then no writes occur for those
2003 * attachments."
2004 */
2005 if (att == VK_ATTACHMENT_UNUSED) {
2006 surface_state = cmd_buffer->state.null_surface_state;
2007 } else {
2008 surface_state = cmd_buffer->state.attachments[att].color.state;
2009 }
2010 } else {
2011 surface_state = cmd_buffer->state.null_surface_state;
2012 }
2013
2014 bt_map[bias + s] = surface_state.offset + state_offset;
2015 continue;
2016 }
2017
2018 const struct anv_descriptor *desc =
2019 anv_descriptor_for_binding(pipe_state, binding);
2020
2021 switch (desc->type) {
2022 case VK_DESCRIPTOR_TYPE_SAMPLER:
2023 /* Nothing for us to do here */
2024 continue;
2025
2026 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
2027 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: {
2028 struct anv_surface_state sstate =
2029 (desc->layout == VK_IMAGE_LAYOUT_GENERAL) ?
2030 desc->image_view->planes[binding->plane].general_sampler_surface_state :
2031 desc->image_view->planes[binding->plane].optimal_sampler_surface_state;
2032 surface_state = sstate.state;
2033 assert(surface_state.alloc_size);
2034 add_image_view_relocs(cmd_buffer, desc->image_view,
2035 binding->plane, sstate);
2036 break;
2037 }
2038 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2039 assert(stage == MESA_SHADER_FRAGMENT);
2040 if ((desc->image_view->aspect_mask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0) {
2041 /* For depth and stencil input attachments, we treat it like any
2042 * old texture that a user may have bound.
2043 */
2044 struct anv_surface_state sstate =
2045 (desc->layout == VK_IMAGE_LAYOUT_GENERAL) ?
2046 desc->image_view->planes[binding->plane].general_sampler_surface_state :
2047 desc->image_view->planes[binding->plane].optimal_sampler_surface_state;
2048 surface_state = sstate.state;
2049 assert(surface_state.alloc_size);
2050 add_image_view_relocs(cmd_buffer, desc->image_view,
2051 binding->plane, sstate);
2052 } else {
2053 /* For color input attachments, we create the surface state at
2054 * vkBeginRenderPass time so that we can include aux and clear
2055 * color information.
2056 */
2057 assert(binding->input_attachment_index < subpass->input_count);
2058 const unsigned subpass_att = binding->input_attachment_index;
2059 const unsigned att = subpass->input_attachments[subpass_att].attachment;
2060 surface_state = cmd_buffer->state.attachments[att].input.state;
2061 }
2062 break;
2063
2064 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
2065 struct anv_surface_state sstate = (binding->write_only)
2066 ? desc->image_view->planes[binding->plane].writeonly_storage_surface_state
2067 : desc->image_view->planes[binding->plane].storage_surface_state;
2068 surface_state = sstate.state;
2069 assert(surface_state.alloc_size);
2070 add_image_view_relocs(cmd_buffer, desc->image_view,
2071 binding->plane, sstate);
2072
2073 struct brw_image_param *image_param =
2074 &cmd_buffer->state.push_constants[stage]->images[image++];
2075
2076 *image_param = desc->image_view->planes[binding->plane].storage_image_param;
2077 image_param->surface_idx = bias + s;
2078 break;
2079 }
2080
2081 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2082 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2083 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2084 surface_state = desc->buffer_view->surface_state;
2085 assert(surface_state.alloc_size);
2086 add_surface_state_reloc(cmd_buffer, surface_state,
2087 desc->buffer_view->bo,
2088 desc->buffer_view->offset);
2089 break;
2090
2091 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2092 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2093 /* Compute the offset within the buffer */
2094 uint32_t dynamic_offset =
2095 dynamic_offset_for_binding(pipe_state, binding);
2096 uint64_t offset = desc->offset + dynamic_offset;
2097 /* Clamp to the buffer size */
2098 offset = MIN2(offset, desc->buffer->size);
2099 /* Clamp the range to the buffer size */
2100 uint32_t range = MIN2(desc->range, desc->buffer->size - offset);
2101
2102 surface_state =
2103 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
2104 enum isl_format format =
2105 anv_isl_format_for_descriptor_type(desc->type);
2106
2107 anv_fill_buffer_surface_state(cmd_buffer->device, surface_state,
2108 format, offset, range, 1);
2109 add_surface_state_reloc(cmd_buffer, surface_state,
2110 desc->buffer->bo,
2111 desc->buffer->offset + offset);
2112 break;
2113 }
2114
2115 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2116 surface_state = (binding->write_only)
2117 ? desc->buffer_view->writeonly_storage_surface_state
2118 : desc->buffer_view->storage_surface_state;
2119 assert(surface_state.alloc_size);
2120 add_surface_state_reloc(cmd_buffer, surface_state,
2121 desc->buffer_view->bo,
2122 desc->buffer_view->offset);
2123
2124 struct brw_image_param *image_param =
2125 &cmd_buffer->state.push_constants[stage]->images[image++];
2126
2127 *image_param = desc->buffer_view->storage_image_param;
2128 image_param->surface_idx = bias + s;
2129 break;
2130
2131 default:
2132 assert(!"Invalid descriptor type");
2133 continue;
2134 }
2135
2136 bt_map[bias + s] = surface_state.offset + state_offset;
2137 }
2138 assert(image == map->image_count);
2139
2140 out:
2141 anv_state_flush(cmd_buffer->device, *bt_state);
2142
2143 #if GEN_GEN >= 11
2144 /* The PIPE_CONTROL command description says:
2145 *
2146 * "Whenever a Binding Table Index (BTI) used by a Render Taget Message
2147 * points to a different RENDER_SURFACE_STATE, SW must issue a Render
2148 * Target Cache Flush by enabling this bit. When render target flush
2149 * is set due to new association of BTI, PS Scoreboard Stall bit must
2150 * be set in this packet."
2151 *
2152 * FINISHME: Currently we shuffle around the surface states in the binding
2153 * table based on if they are getting used or not. So, we've to do below
2154 * pipe control flush for every binding table upload. Make changes so
2155 * that we do it only when we modify render target surface states.
2156 */
2157 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
2158 pc.RenderTargetCacheFlushEnable = true;
2159 pc.StallAtPixelScoreboard = true;
2160 }
2161 #endif
2162
2163 return VK_SUCCESS;
2164 }
2165
2166 static VkResult
2167 emit_samplers(struct anv_cmd_buffer *cmd_buffer,
2168 gl_shader_stage stage,
2169 struct anv_state *state)
2170 {
2171 struct anv_cmd_pipeline_state *pipe_state =
2172 stage == MESA_SHADER_COMPUTE ? &cmd_buffer->state.compute.base :
2173 &cmd_buffer->state.gfx.base;
2174 struct anv_pipeline *pipeline = pipe_state->pipeline;
2175
2176 if (!anv_pipeline_has_stage(pipeline, stage)) {
2177 *state = (struct anv_state) { 0, };
2178 return VK_SUCCESS;
2179 }
2180
2181 struct anv_pipeline_bind_map *map = &pipeline->shaders[stage]->bind_map;
2182 if (map->sampler_count == 0) {
2183 *state = (struct anv_state) { 0, };
2184 return VK_SUCCESS;
2185 }
2186
2187 uint32_t size = map->sampler_count * 16;
2188 *state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, 32);
2189
2190 if (state->map == NULL)
2191 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2192
2193 for (uint32_t s = 0; s < map->sampler_count; s++) {
2194 struct anv_pipeline_binding *binding = &map->sampler_to_descriptor[s];
2195 const struct anv_descriptor *desc =
2196 anv_descriptor_for_binding(pipe_state, binding);
2197
2198 if (desc->type != VK_DESCRIPTOR_TYPE_SAMPLER &&
2199 desc->type != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
2200 continue;
2201
2202 struct anv_sampler *sampler = desc->sampler;
2203
2204 /* This can happen if we have an unfilled slot since TYPE_SAMPLER
2205 * happens to be zero.
2206 */
2207 if (sampler == NULL)
2208 continue;
2209
2210 memcpy(state->map + (s * 16),
2211 sampler->state[binding->plane], sizeof(sampler->state[0]));
2212 }
2213
2214 anv_state_flush(cmd_buffer->device, *state);
2215
2216 return VK_SUCCESS;
2217 }
2218
2219 static uint32_t
2220 flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer)
2221 {
2222 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2223
2224 VkShaderStageFlags dirty = cmd_buffer->state.descriptors_dirty &
2225 pipeline->active_stages;
2226
2227 VkResult result = VK_SUCCESS;
2228 anv_foreach_stage(s, dirty) {
2229 result = emit_samplers(cmd_buffer, s, &cmd_buffer->state.samplers[s]);
2230 if (result != VK_SUCCESS)
2231 break;
2232 result = emit_binding_table(cmd_buffer, s,
2233 &cmd_buffer->state.binding_tables[s]);
2234 if (result != VK_SUCCESS)
2235 break;
2236 }
2237
2238 if (result != VK_SUCCESS) {
2239 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
2240
2241 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
2242 if (result != VK_SUCCESS)
2243 return 0;
2244
2245 /* Re-emit state base addresses so we get the new surface state base
2246 * address before we start emitting binding tables etc.
2247 */
2248 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
2249
2250 /* Re-emit all active binding tables */
2251 dirty |= pipeline->active_stages;
2252 anv_foreach_stage(s, dirty) {
2253 result = emit_samplers(cmd_buffer, s, &cmd_buffer->state.samplers[s]);
2254 if (result != VK_SUCCESS) {
2255 anv_batch_set_error(&cmd_buffer->batch, result);
2256 return 0;
2257 }
2258 result = emit_binding_table(cmd_buffer, s,
2259 &cmd_buffer->state.binding_tables[s]);
2260 if (result != VK_SUCCESS) {
2261 anv_batch_set_error(&cmd_buffer->batch, result);
2262 return 0;
2263 }
2264 }
2265 }
2266
2267 cmd_buffer->state.descriptors_dirty &= ~dirty;
2268
2269 return dirty;
2270 }
2271
2272 static void
2273 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer *cmd_buffer,
2274 uint32_t stages)
2275 {
2276 static const uint32_t sampler_state_opcodes[] = {
2277 [MESA_SHADER_VERTEX] = 43,
2278 [MESA_SHADER_TESS_CTRL] = 44, /* HS */
2279 [MESA_SHADER_TESS_EVAL] = 45, /* DS */
2280 [MESA_SHADER_GEOMETRY] = 46,
2281 [MESA_SHADER_FRAGMENT] = 47,
2282 [MESA_SHADER_COMPUTE] = 0,
2283 };
2284
2285 static const uint32_t binding_table_opcodes[] = {
2286 [MESA_SHADER_VERTEX] = 38,
2287 [MESA_SHADER_TESS_CTRL] = 39,
2288 [MESA_SHADER_TESS_EVAL] = 40,
2289 [MESA_SHADER_GEOMETRY] = 41,
2290 [MESA_SHADER_FRAGMENT] = 42,
2291 [MESA_SHADER_COMPUTE] = 0,
2292 };
2293
2294 anv_foreach_stage(s, stages) {
2295 assert(s < ARRAY_SIZE(binding_table_opcodes));
2296 assert(binding_table_opcodes[s] > 0);
2297
2298 if (cmd_buffer->state.samplers[s].alloc_size > 0) {
2299 anv_batch_emit(&cmd_buffer->batch,
2300 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ssp) {
2301 ssp._3DCommandSubOpcode = sampler_state_opcodes[s];
2302 ssp.PointertoVSSamplerState = cmd_buffer->state.samplers[s].offset;
2303 }
2304 }
2305
2306 /* Always emit binding table pointers if we're asked to, since on SKL
2307 * this is what flushes push constants. */
2308 anv_batch_emit(&cmd_buffer->batch,
2309 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), btp) {
2310 btp._3DCommandSubOpcode = binding_table_opcodes[s];
2311 btp.PointertoVSBindingTable = cmd_buffer->state.binding_tables[s].offset;
2312 }
2313 }
2314 }
2315
2316 static void
2317 cmd_buffer_flush_push_constants(struct anv_cmd_buffer *cmd_buffer,
2318 VkShaderStageFlags dirty_stages)
2319 {
2320 const struct anv_cmd_graphics_state *gfx_state = &cmd_buffer->state.gfx;
2321 const struct anv_pipeline *pipeline = gfx_state->base.pipeline;
2322
2323 static const uint32_t push_constant_opcodes[] = {
2324 [MESA_SHADER_VERTEX] = 21,
2325 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
2326 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
2327 [MESA_SHADER_GEOMETRY] = 22,
2328 [MESA_SHADER_FRAGMENT] = 23,
2329 [MESA_SHADER_COMPUTE] = 0,
2330 };
2331
2332 VkShaderStageFlags flushed = 0;
2333
2334 anv_foreach_stage(stage, dirty_stages) {
2335 assert(stage < ARRAY_SIZE(push_constant_opcodes));
2336 assert(push_constant_opcodes[stage] > 0);
2337
2338 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CONSTANT_VS), c) {
2339 c._3DCommandSubOpcode = push_constant_opcodes[stage];
2340
2341 if (anv_pipeline_has_stage(pipeline, stage)) {
2342 #if GEN_GEN >= 8 || GEN_IS_HASWELL
2343 const struct brw_stage_prog_data *prog_data =
2344 pipeline->shaders[stage]->prog_data;
2345 const struct anv_pipeline_bind_map *bind_map =
2346 &pipeline->shaders[stage]->bind_map;
2347
2348 /* The Skylake PRM contains the following restriction:
2349 *
2350 * "The driver must ensure The following case does not occur
2351 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
2352 * buffer 3 read length equal to zero committed followed by a
2353 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
2354 * zero committed."
2355 *
2356 * To avoid this, we program the buffers in the highest slots.
2357 * This way, slot 0 is only used if slot 3 is also used.
2358 */
2359 int n = 3;
2360
2361 for (int i = 3; i >= 0; i--) {
2362 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
2363 if (range->length == 0)
2364 continue;
2365
2366 const unsigned surface =
2367 prog_data->binding_table.ubo_start + range->block;
2368
2369 assert(surface <= bind_map->surface_count);
2370 const struct anv_pipeline_binding *binding =
2371 &bind_map->surface_to_descriptor[surface];
2372
2373 const struct anv_descriptor *desc =
2374 anv_descriptor_for_binding(&gfx_state->base, binding);
2375
2376 struct anv_address read_addr;
2377 uint32_t read_len;
2378 if (desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) {
2379 read_len = MIN2(range->length,
2380 DIV_ROUND_UP(desc->buffer_view->range, 32) - range->start);
2381 read_addr = (struct anv_address) {
2382 .bo = desc->buffer_view->bo,
2383 .offset = desc->buffer_view->offset +
2384 range->start * 32,
2385 };
2386 } else {
2387 assert(desc->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
2388
2389 uint32_t dynamic_offset =
2390 dynamic_offset_for_binding(&gfx_state->base, binding);
2391 uint32_t buf_offset =
2392 MIN2(desc->offset + dynamic_offset, desc->buffer->size);
2393 uint32_t buf_range =
2394 MIN2(desc->range, desc->buffer->size - buf_offset);
2395
2396 read_len = MIN2(range->length,
2397 DIV_ROUND_UP(buf_range, 32) - range->start);
2398 read_addr = (struct anv_address) {
2399 .bo = desc->buffer->bo,
2400 .offset = desc->buffer->offset + buf_offset +
2401 range->start * 32,
2402 };
2403 }
2404
2405 if (read_len > 0) {
2406 c.ConstantBody.Buffer[n] = read_addr;
2407 c.ConstantBody.ReadLength[n] = read_len;
2408 n--;
2409 }
2410 }
2411
2412 struct anv_state state =
2413 anv_cmd_buffer_push_constants(cmd_buffer, stage);
2414
2415 if (state.alloc_size > 0) {
2416 c.ConstantBody.Buffer[n] = (struct anv_address) {
2417 .bo = &cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2418 .offset = state.offset,
2419 };
2420 c.ConstantBody.ReadLength[n] =
2421 DIV_ROUND_UP(state.alloc_size, 32);
2422 }
2423 #else
2424 /* For Ivy Bridge, the push constants packets have a different
2425 * rule that would require us to iterate in the other direction
2426 * and possibly mess around with dynamic state base address.
2427 * Don't bother; just emit regular push constants at n = 0.
2428 */
2429 struct anv_state state =
2430 anv_cmd_buffer_push_constants(cmd_buffer, stage);
2431
2432 if (state.alloc_size > 0) {
2433 c.ConstantBody.Buffer[0].offset = state.offset,
2434 c.ConstantBody.ReadLength[0] =
2435 DIV_ROUND_UP(state.alloc_size, 32);
2436 }
2437 #endif
2438 }
2439 }
2440
2441 flushed |= mesa_to_vk_shader_stage(stage);
2442 }
2443
2444 cmd_buffer->state.push_constants_dirty &= ~flushed;
2445 }
2446
2447 void
2448 genX(cmd_buffer_flush_state)(struct anv_cmd_buffer *cmd_buffer)
2449 {
2450 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2451 uint32_t *p;
2452
2453 uint32_t vb_emit = cmd_buffer->state.gfx.vb_dirty & pipeline->vb_used;
2454
2455 assert((pipeline->active_stages & VK_SHADER_STAGE_COMPUTE_BIT) == 0);
2456
2457 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
2458
2459 genX(flush_pipeline_select_3d)(cmd_buffer);
2460
2461 if (vb_emit) {
2462 const uint32_t num_buffers = __builtin_popcount(vb_emit);
2463 const uint32_t num_dwords = 1 + num_buffers * 4;
2464
2465 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
2466 GENX(3DSTATE_VERTEX_BUFFERS));
2467 uint32_t vb, i = 0;
2468 for_each_bit(vb, vb_emit) {
2469 struct anv_buffer *buffer = cmd_buffer->state.vertex_bindings[vb].buffer;
2470 uint32_t offset = cmd_buffer->state.vertex_bindings[vb].offset;
2471
2472 struct GENX(VERTEX_BUFFER_STATE) state = {
2473 .VertexBufferIndex = vb,
2474
2475 #if GEN_GEN >= 8
2476 .MemoryObjectControlState = GENX(MOCS),
2477 #else
2478 .BufferAccessType = pipeline->instancing_enable[vb] ? INSTANCEDATA : VERTEXDATA,
2479 /* Our implementation of VK_KHR_multiview uses instancing to draw
2480 * the different views. If the client asks for instancing, we
2481 * need to use the Instance Data Step Rate to ensure that we
2482 * repeat the client's per-instance data once for each view.
2483 */
2484 .InstanceDataStepRate = anv_subpass_view_count(pipeline->subpass),
2485 .VertexBufferMemoryObjectControlState = GENX(MOCS),
2486 #endif
2487
2488 .AddressModifyEnable = true,
2489 .BufferPitch = pipeline->binding_stride[vb],
2490 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
2491
2492 #if GEN_GEN >= 8
2493 .BufferSize = buffer->size - offset
2494 #else
2495 .EndAddress = { buffer->bo, buffer->offset + buffer->size - 1},
2496 #endif
2497 };
2498
2499 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, &p[1 + i * 4], &state);
2500 i++;
2501 }
2502 }
2503
2504 cmd_buffer->state.gfx.vb_dirty &= ~vb_emit;
2505
2506 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_PIPELINE) {
2507 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
2508
2509 /* The exact descriptor layout is pulled from the pipeline, so we need
2510 * to re-emit binding tables on every pipeline change.
2511 */
2512 cmd_buffer->state.descriptors_dirty |= pipeline->active_stages;
2513
2514 /* If the pipeline changed, we may need to re-allocate push constant
2515 * space in the URB.
2516 */
2517 cmd_buffer_alloc_push_constants(cmd_buffer);
2518 }
2519
2520 #if GEN_GEN <= 7
2521 if (cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_VERTEX_BIT ||
2522 cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_VERTEX_BIT) {
2523 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
2524 *
2525 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
2526 * stall needs to be sent just prior to any 3DSTATE_VS,
2527 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
2528 * 3DSTATE_BINDING_TABLE_POINTER_VS,
2529 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
2530 * PIPE_CONTROL needs to be sent before any combination of VS
2531 * associated 3DSTATE."
2532 */
2533 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
2534 pc.DepthStallEnable = true;
2535 pc.PostSyncOperation = WriteImmediateData;
2536 pc.Address =
2537 (struct anv_address) { &cmd_buffer->device->workaround_bo, 0 };
2538 }
2539 }
2540 #endif
2541
2542 /* Render targets live in the same binding table as fragment descriptors */
2543 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_RENDER_TARGETS)
2544 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_FRAGMENT_BIT;
2545
2546 /* We emit the binding tables and sampler tables first, then emit push
2547 * constants and then finally emit binding table and sampler table
2548 * pointers. It has to happen in this order, since emitting the binding
2549 * tables may change the push constants (in case of storage images). After
2550 * emitting push constants, on SKL+ we have to emit the corresponding
2551 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
2552 */
2553 uint32_t dirty = 0;
2554 if (cmd_buffer->state.descriptors_dirty)
2555 dirty = flush_descriptor_sets(cmd_buffer);
2556
2557 if (dirty || cmd_buffer->state.push_constants_dirty) {
2558 /* Because we're pushing UBOs, we have to push whenever either
2559 * descriptors or push constants is dirty.
2560 */
2561 dirty |= cmd_buffer->state.push_constants_dirty;
2562 dirty &= ANV_STAGE_MASK & VK_SHADER_STAGE_ALL_GRAPHICS;
2563 cmd_buffer_flush_push_constants(cmd_buffer, dirty);
2564 }
2565
2566 if (dirty)
2567 cmd_buffer_emit_descriptor_pointers(cmd_buffer, dirty);
2568
2569 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_VIEWPORT)
2570 gen8_cmd_buffer_emit_viewport(cmd_buffer);
2571
2572 if (cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT |
2573 ANV_CMD_DIRTY_PIPELINE)) {
2574 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer,
2575 pipeline->depth_clamp_enable);
2576 }
2577
2578 if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_SCISSOR)
2579 gen7_cmd_buffer_emit_scissor(cmd_buffer);
2580
2581 genX(cmd_buffer_flush_dynamic_state)(cmd_buffer);
2582
2583 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
2584 }
2585
2586 static void
2587 emit_vertex_bo(struct anv_cmd_buffer *cmd_buffer,
2588 struct anv_bo *bo, uint32_t offset,
2589 uint32_t size, uint32_t index)
2590 {
2591 uint32_t *p = anv_batch_emitn(&cmd_buffer->batch, 5,
2592 GENX(3DSTATE_VERTEX_BUFFERS));
2593
2594 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, p + 1,
2595 &(struct GENX(VERTEX_BUFFER_STATE)) {
2596 .VertexBufferIndex = index,
2597 .AddressModifyEnable = true,
2598 .BufferPitch = 0,
2599 #if (GEN_GEN >= 8)
2600 .MemoryObjectControlState = GENX(MOCS),
2601 .BufferStartingAddress = { bo, offset },
2602 .BufferSize = size
2603 #else
2604 .VertexBufferMemoryObjectControlState = GENX(MOCS),
2605 .BufferStartingAddress = { bo, offset },
2606 .EndAddress = { bo, offset + size },
2607 #endif
2608 });
2609 }
2610
2611 static void
2612 emit_base_vertex_instance_bo(struct anv_cmd_buffer *cmd_buffer,
2613 struct anv_bo *bo, uint32_t offset)
2614 {
2615 emit_vertex_bo(cmd_buffer, bo, offset, 8, ANV_SVGS_VB_INDEX);
2616 }
2617
2618 static void
2619 emit_base_vertex_instance(struct anv_cmd_buffer *cmd_buffer,
2620 uint32_t base_vertex, uint32_t base_instance)
2621 {
2622 struct anv_state id_state =
2623 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 8, 4);
2624
2625 ((uint32_t *)id_state.map)[0] = base_vertex;
2626 ((uint32_t *)id_state.map)[1] = base_instance;
2627
2628 anv_state_flush(cmd_buffer->device, id_state);
2629
2630 emit_base_vertex_instance_bo(cmd_buffer,
2631 &cmd_buffer->device->dynamic_state_pool.block_pool.bo, id_state.offset);
2632 }
2633
2634 static void
2635 emit_draw_index(struct anv_cmd_buffer *cmd_buffer, uint32_t draw_index)
2636 {
2637 struct anv_state state =
2638 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 4, 4);
2639
2640 ((uint32_t *)state.map)[0] = draw_index;
2641
2642 anv_state_flush(cmd_buffer->device, state);
2643
2644 emit_vertex_bo(cmd_buffer,
2645 &cmd_buffer->device->dynamic_state_pool.block_pool.bo,
2646 state.offset, 4, ANV_DRAWID_VB_INDEX);
2647 }
2648
2649 void genX(CmdDraw)(
2650 VkCommandBuffer commandBuffer,
2651 uint32_t vertexCount,
2652 uint32_t instanceCount,
2653 uint32_t firstVertex,
2654 uint32_t firstInstance)
2655 {
2656 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2657 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2658 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2659
2660 if (anv_batch_has_error(&cmd_buffer->batch))
2661 return;
2662
2663 genX(cmd_buffer_flush_state)(cmd_buffer);
2664
2665 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
2666 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
2667 if (vs_prog_data->uses_drawid)
2668 emit_draw_index(cmd_buffer, 0);
2669
2670 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2671 * different views. We need to multiply instanceCount by the view count.
2672 */
2673 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
2674
2675 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
2676 prim.VertexAccessType = SEQUENTIAL;
2677 prim.PrimitiveTopologyType = pipeline->topology;
2678 prim.VertexCountPerInstance = vertexCount;
2679 prim.StartVertexLocation = firstVertex;
2680 prim.InstanceCount = instanceCount;
2681 prim.StartInstanceLocation = firstInstance;
2682 prim.BaseVertexLocation = 0;
2683 }
2684 }
2685
2686 void genX(CmdDrawIndexed)(
2687 VkCommandBuffer commandBuffer,
2688 uint32_t indexCount,
2689 uint32_t instanceCount,
2690 uint32_t firstIndex,
2691 int32_t vertexOffset,
2692 uint32_t firstInstance)
2693 {
2694 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2695 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2696 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2697
2698 if (anv_batch_has_error(&cmd_buffer->batch))
2699 return;
2700
2701 genX(cmd_buffer_flush_state)(cmd_buffer);
2702
2703 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
2704 emit_base_vertex_instance(cmd_buffer, vertexOffset, firstInstance);
2705 if (vs_prog_data->uses_drawid)
2706 emit_draw_index(cmd_buffer, 0);
2707
2708 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2709 * different views. We need to multiply instanceCount by the view count.
2710 */
2711 instanceCount *= anv_subpass_view_count(cmd_buffer->state.subpass);
2712
2713 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
2714 prim.VertexAccessType = RANDOM;
2715 prim.PrimitiveTopologyType = pipeline->topology;
2716 prim.VertexCountPerInstance = indexCount;
2717 prim.StartVertexLocation = firstIndex;
2718 prim.InstanceCount = instanceCount;
2719 prim.StartInstanceLocation = firstInstance;
2720 prim.BaseVertexLocation = vertexOffset;
2721 }
2722 }
2723
2724 /* Auto-Draw / Indirect Registers */
2725 #define GEN7_3DPRIM_END_OFFSET 0x2420
2726 #define GEN7_3DPRIM_START_VERTEX 0x2430
2727 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
2728 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
2729 #define GEN7_3DPRIM_START_INSTANCE 0x243C
2730 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
2731
2732 /* MI_MATH only exists on Haswell+ */
2733 #if GEN_IS_HASWELL || GEN_GEN >= 8
2734
2735 /* Emit dwords to multiply GPR0 by N */
2736 static void
2737 build_alu_multiply_gpr0(uint32_t *dw, unsigned *dw_count, uint32_t N)
2738 {
2739 VK_OUTARRAY_MAKE(out, dw, dw_count);
2740
2741 #define append_alu(opcode, operand1, operand2) \
2742 vk_outarray_append(&out, alu_dw) *alu_dw = mi_alu(opcode, operand1, operand2)
2743
2744 assert(N > 0);
2745 unsigned top_bit = 31 - __builtin_clz(N);
2746 for (int i = top_bit - 1; i >= 0; i--) {
2747 /* We get our initial data in GPR0 and we write the final data out to
2748 * GPR0 but we use GPR1 as our scratch register.
2749 */
2750 unsigned src_reg = i == top_bit - 1 ? MI_ALU_REG0 : MI_ALU_REG1;
2751 unsigned dst_reg = i == 0 ? MI_ALU_REG0 : MI_ALU_REG1;
2752
2753 /* Shift the current value left by 1 */
2754 append_alu(MI_ALU_LOAD, MI_ALU_SRCA, src_reg);
2755 append_alu(MI_ALU_LOAD, MI_ALU_SRCB, src_reg);
2756 append_alu(MI_ALU_ADD, 0, 0);
2757
2758 if (N & (1 << i)) {
2759 /* Store ACCU to R1 and add R0 to R1 */
2760 append_alu(MI_ALU_STORE, MI_ALU_REG1, MI_ALU_ACCU);
2761 append_alu(MI_ALU_LOAD, MI_ALU_SRCA, MI_ALU_REG0);
2762 append_alu(MI_ALU_LOAD, MI_ALU_SRCB, MI_ALU_REG1);
2763 append_alu(MI_ALU_ADD, 0, 0);
2764 }
2765
2766 append_alu(MI_ALU_STORE, dst_reg, MI_ALU_ACCU);
2767 }
2768
2769 #undef append_alu
2770 }
2771
2772 static void
2773 emit_mul_gpr0(struct anv_batch *batch, uint32_t N)
2774 {
2775 uint32_t num_dwords;
2776 build_alu_multiply_gpr0(NULL, &num_dwords, N);
2777
2778 uint32_t *dw = anv_batch_emitn(batch, 1 + num_dwords, GENX(MI_MATH));
2779 build_alu_multiply_gpr0(dw + 1, &num_dwords, N);
2780 }
2781
2782 #endif /* GEN_IS_HASWELL || GEN_GEN >= 8 */
2783
2784 static void
2785 load_indirect_parameters(struct anv_cmd_buffer *cmd_buffer,
2786 struct anv_buffer *buffer, uint64_t offset,
2787 bool indexed)
2788 {
2789 struct anv_batch *batch = &cmd_buffer->batch;
2790 struct anv_bo *bo = buffer->bo;
2791 uint32_t bo_offset = buffer->offset + offset;
2792
2793 emit_lrm(batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
2794
2795 unsigned view_count = anv_subpass_view_count(cmd_buffer->state.subpass);
2796 if (view_count > 1) {
2797 #if GEN_IS_HASWELL || GEN_GEN >= 8
2798 emit_lrm(batch, CS_GPR(0), bo, bo_offset + 4);
2799 emit_mul_gpr0(batch, view_count);
2800 emit_lrr(batch, GEN7_3DPRIM_INSTANCE_COUNT, CS_GPR(0));
2801 #else
2802 anv_finishme("Multiview + indirect draw requires MI_MATH; "
2803 "MI_MATH is not supported on Ivy Bridge");
2804 emit_lrm(batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
2805 #endif
2806 } else {
2807 emit_lrm(batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
2808 }
2809
2810 emit_lrm(batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
2811
2812 if (indexed) {
2813 emit_lrm(batch, GEN7_3DPRIM_BASE_VERTEX, bo, bo_offset + 12);
2814 emit_lrm(batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 16);
2815 } else {
2816 emit_lrm(batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 12);
2817 emit_lri(batch, GEN7_3DPRIM_BASE_VERTEX, 0);
2818 }
2819 }
2820
2821 void genX(CmdDrawIndirect)(
2822 VkCommandBuffer commandBuffer,
2823 VkBuffer _buffer,
2824 VkDeviceSize offset,
2825 uint32_t drawCount,
2826 uint32_t stride)
2827 {
2828 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2829 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
2830 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2831 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2832
2833 if (anv_batch_has_error(&cmd_buffer->batch))
2834 return;
2835
2836 genX(cmd_buffer_flush_state)(cmd_buffer);
2837
2838 for (uint32_t i = 0; i < drawCount; i++) {
2839 struct anv_bo *bo = buffer->bo;
2840 uint32_t bo_offset = buffer->offset + offset;
2841
2842 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
2843 emit_base_vertex_instance_bo(cmd_buffer, bo, bo_offset + 8);
2844 if (vs_prog_data->uses_drawid)
2845 emit_draw_index(cmd_buffer, i);
2846
2847 load_indirect_parameters(cmd_buffer, buffer, offset, false);
2848
2849 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
2850 prim.IndirectParameterEnable = true;
2851 prim.VertexAccessType = SEQUENTIAL;
2852 prim.PrimitiveTopologyType = pipeline->topology;
2853 }
2854
2855 offset += stride;
2856 }
2857 }
2858
2859 void genX(CmdDrawIndexedIndirect)(
2860 VkCommandBuffer commandBuffer,
2861 VkBuffer _buffer,
2862 VkDeviceSize offset,
2863 uint32_t drawCount,
2864 uint32_t stride)
2865 {
2866 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
2867 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
2868 struct anv_pipeline *pipeline = cmd_buffer->state.gfx.base.pipeline;
2869 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2870
2871 if (anv_batch_has_error(&cmd_buffer->batch))
2872 return;
2873
2874 genX(cmd_buffer_flush_state)(cmd_buffer);
2875
2876 for (uint32_t i = 0; i < drawCount; i++) {
2877 struct anv_bo *bo = buffer->bo;
2878 uint32_t bo_offset = buffer->offset + offset;
2879
2880 /* TODO: We need to stomp base vertex to 0 somehow */
2881 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
2882 emit_base_vertex_instance_bo(cmd_buffer, bo, bo_offset + 12);
2883 if (vs_prog_data->uses_drawid)
2884 emit_draw_index(cmd_buffer, i);
2885
2886 load_indirect_parameters(cmd_buffer, buffer, offset, true);
2887
2888 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
2889 prim.IndirectParameterEnable = true;
2890 prim.VertexAccessType = RANDOM;
2891 prim.PrimitiveTopologyType = pipeline->topology;
2892 }
2893
2894 offset += stride;
2895 }
2896 }
2897
2898 static VkResult
2899 flush_compute_descriptor_set(struct anv_cmd_buffer *cmd_buffer)
2900 {
2901 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
2902 struct anv_state surfaces = { 0, }, samplers = { 0, };
2903 VkResult result;
2904
2905 result = emit_binding_table(cmd_buffer, MESA_SHADER_COMPUTE, &surfaces);
2906 if (result != VK_SUCCESS) {
2907 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
2908
2909 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
2910 if (result != VK_SUCCESS)
2911 return result;
2912
2913 /* Re-emit state base addresses so we get the new surface state base
2914 * address before we start emitting binding tables etc.
2915 */
2916 genX(cmd_buffer_emit_state_base_address)(cmd_buffer);
2917
2918 result = emit_binding_table(cmd_buffer, MESA_SHADER_COMPUTE, &surfaces);
2919 if (result != VK_SUCCESS) {
2920 anv_batch_set_error(&cmd_buffer->batch, result);
2921 return result;
2922 }
2923 }
2924
2925 result = emit_samplers(cmd_buffer, MESA_SHADER_COMPUTE, &samplers);
2926 if (result != VK_SUCCESS) {
2927 anv_batch_set_error(&cmd_buffer->batch, result);
2928 return result;
2929 }
2930
2931 uint32_t iface_desc_data_dw[GENX(INTERFACE_DESCRIPTOR_DATA_length)];
2932 struct GENX(INTERFACE_DESCRIPTOR_DATA) desc = {
2933 .BindingTablePointer = surfaces.offset,
2934 .SamplerStatePointer = samplers.offset,
2935 };
2936 GENX(INTERFACE_DESCRIPTOR_DATA_pack)(NULL, iface_desc_data_dw, &desc);
2937
2938 struct anv_state state =
2939 anv_cmd_buffer_merge_dynamic(cmd_buffer, iface_desc_data_dw,
2940 pipeline->interface_descriptor_data,
2941 GENX(INTERFACE_DESCRIPTOR_DATA_length),
2942 64);
2943
2944 uint32_t size = GENX(INTERFACE_DESCRIPTOR_DATA_length) * sizeof(uint32_t);
2945 anv_batch_emit(&cmd_buffer->batch,
2946 GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD), mid) {
2947 mid.InterfaceDescriptorTotalLength = size;
2948 mid.InterfaceDescriptorDataStartAddress = state.offset;
2949 }
2950
2951 return VK_SUCCESS;
2952 }
2953
2954 void
2955 genX(cmd_buffer_flush_compute_state)(struct anv_cmd_buffer *cmd_buffer)
2956 {
2957 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
2958 MAYBE_UNUSED VkResult result;
2959
2960 assert(pipeline->active_stages == VK_SHADER_STAGE_COMPUTE_BIT);
2961
2962 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
2963
2964 genX(flush_pipeline_select_gpgpu)(cmd_buffer);
2965
2966 if (cmd_buffer->state.compute.pipeline_dirty) {
2967 /* From the Sky Lake PRM Vol 2a, MEDIA_VFE_STATE:
2968 *
2969 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
2970 * the only bits that are changed are scoreboard related: Scoreboard
2971 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
2972 * these scoreboard related states, a MEDIA_STATE_FLUSH is
2973 * sufficient."
2974 */
2975 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
2976 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
2977
2978 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
2979 }
2980
2981 if ((cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_COMPUTE_BIT) ||
2982 cmd_buffer->state.compute.pipeline_dirty) {
2983 /* FIXME: figure out descriptors for gen7 */
2984 result = flush_compute_descriptor_set(cmd_buffer);
2985 if (result != VK_SUCCESS)
2986 return;
2987
2988 cmd_buffer->state.descriptors_dirty &= ~VK_SHADER_STAGE_COMPUTE_BIT;
2989 }
2990
2991 if (cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_COMPUTE_BIT) {
2992 struct anv_state push_state =
2993 anv_cmd_buffer_cs_push_constants(cmd_buffer);
2994
2995 if (push_state.alloc_size) {
2996 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_CURBE_LOAD), curbe) {
2997 curbe.CURBETotalDataLength = push_state.alloc_size;
2998 curbe.CURBEDataStartAddress = push_state.offset;
2999 }
3000 }
3001 }
3002
3003 cmd_buffer->state.compute.pipeline_dirty = false;
3004
3005 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
3006 }
3007
3008 #if GEN_GEN == 7
3009
3010 static VkResult
3011 verify_cmd_parser(const struct anv_device *device,
3012 int required_version,
3013 const char *function)
3014 {
3015 if (device->instance->physicalDevice.cmd_parser_version < required_version) {
3016 return vk_errorf(device->instance, device->instance,
3017 VK_ERROR_FEATURE_NOT_PRESENT,
3018 "cmd parser version %d is required for %s",
3019 required_version, function);
3020 } else {
3021 return VK_SUCCESS;
3022 }
3023 }
3024
3025 #endif
3026
3027 static void
3028 anv_cmd_buffer_push_base_group_id(struct anv_cmd_buffer *cmd_buffer,
3029 uint32_t baseGroupX,
3030 uint32_t baseGroupY,
3031 uint32_t baseGroupZ)
3032 {
3033 if (anv_batch_has_error(&cmd_buffer->batch))
3034 return;
3035
3036 VkResult result =
3037 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer, MESA_SHADER_COMPUTE,
3038 base_work_group_id);
3039 if (result != VK_SUCCESS) {
3040 cmd_buffer->batch.status = result;
3041 return;
3042 }
3043
3044 struct anv_push_constants *push =
3045 cmd_buffer->state.push_constants[MESA_SHADER_COMPUTE];
3046 if (push->base_work_group_id[0] != baseGroupX ||
3047 push->base_work_group_id[1] != baseGroupY ||
3048 push->base_work_group_id[2] != baseGroupZ) {
3049 push->base_work_group_id[0] = baseGroupX;
3050 push->base_work_group_id[1] = baseGroupY;
3051 push->base_work_group_id[2] = baseGroupZ;
3052
3053 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_COMPUTE_BIT;
3054 }
3055 }
3056
3057 void genX(CmdDispatch)(
3058 VkCommandBuffer commandBuffer,
3059 uint32_t x,
3060 uint32_t y,
3061 uint32_t z)
3062 {
3063 genX(CmdDispatchBase)(commandBuffer, 0, 0, 0, x, y, z);
3064 }
3065
3066 void genX(CmdDispatchBase)(
3067 VkCommandBuffer commandBuffer,
3068 uint32_t baseGroupX,
3069 uint32_t baseGroupY,
3070 uint32_t baseGroupZ,
3071 uint32_t groupCountX,
3072 uint32_t groupCountY,
3073 uint32_t groupCountZ)
3074 {
3075 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3076 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3077 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
3078
3079 anv_cmd_buffer_push_base_group_id(cmd_buffer, baseGroupX,
3080 baseGroupY, baseGroupZ);
3081
3082 if (anv_batch_has_error(&cmd_buffer->batch))
3083 return;
3084
3085 if (prog_data->uses_num_work_groups) {
3086 struct anv_state state =
3087 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 12, 4);
3088 uint32_t *sizes = state.map;
3089 sizes[0] = groupCountX;
3090 sizes[1] = groupCountY;
3091 sizes[2] = groupCountZ;
3092 anv_state_flush(cmd_buffer->device, state);
3093 cmd_buffer->state.compute.num_workgroups = (struct anv_address) {
3094 .bo = &cmd_buffer->device->dynamic_state_pool.block_pool.bo,
3095 .offset = state.offset,
3096 };
3097 }
3098
3099 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
3100
3101 anv_batch_emit(&cmd_buffer->batch, GENX(GPGPU_WALKER), ggw) {
3102 ggw.SIMDSize = prog_data->simd_size / 16;
3103 ggw.ThreadDepthCounterMaximum = 0;
3104 ggw.ThreadHeightCounterMaximum = 0;
3105 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
3106 ggw.ThreadGroupIDXDimension = groupCountX;
3107 ggw.ThreadGroupIDYDimension = groupCountY;
3108 ggw.ThreadGroupIDZDimension = groupCountZ;
3109 ggw.RightExecutionMask = pipeline->cs_right_mask;
3110 ggw.BottomExecutionMask = 0xffffffff;
3111 }
3112
3113 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_STATE_FLUSH), msf);
3114 }
3115
3116 #define GPGPU_DISPATCHDIMX 0x2500
3117 #define GPGPU_DISPATCHDIMY 0x2504
3118 #define GPGPU_DISPATCHDIMZ 0x2508
3119
3120 void genX(CmdDispatchIndirect)(
3121 VkCommandBuffer commandBuffer,
3122 VkBuffer _buffer,
3123 VkDeviceSize offset)
3124 {
3125 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3126 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3127 struct anv_pipeline *pipeline = cmd_buffer->state.compute.base.pipeline;
3128 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
3129 struct anv_bo *bo = buffer->bo;
3130 uint32_t bo_offset = buffer->offset + offset;
3131 struct anv_batch *batch = &cmd_buffer->batch;
3132
3133 anv_cmd_buffer_push_base_group_id(cmd_buffer, 0, 0, 0);
3134
3135 #if GEN_GEN == 7
3136 /* Linux 4.4 added command parser version 5 which allows the GPGPU
3137 * indirect dispatch registers to be written.
3138 */
3139 if (verify_cmd_parser(cmd_buffer->device, 5,
3140 "vkCmdDispatchIndirect") != VK_SUCCESS)
3141 return;
3142 #endif
3143
3144 if (prog_data->uses_num_work_groups) {
3145 cmd_buffer->state.compute.num_workgroups = (struct anv_address) {
3146 .bo = bo,
3147 .offset = bo_offset,
3148 };
3149 }
3150
3151 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
3152
3153 emit_lrm(batch, GPGPU_DISPATCHDIMX, bo, bo_offset);
3154 emit_lrm(batch, GPGPU_DISPATCHDIMY, bo, bo_offset + 4);
3155 emit_lrm(batch, GPGPU_DISPATCHDIMZ, bo, bo_offset + 8);
3156
3157 #if GEN_GEN <= 7
3158 /* Clear upper 32-bits of SRC0 and all 64-bits of SRC1 */
3159 emit_lri(batch, MI_PREDICATE_SRC0 + 4, 0);
3160 emit_lri(batch, MI_PREDICATE_SRC1 + 0, 0);
3161 emit_lri(batch, MI_PREDICATE_SRC1 + 4, 0);
3162
3163 /* Load compute_dispatch_indirect_x_size into SRC0 */
3164 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 0);
3165
3166 /* predicate = (compute_dispatch_indirect_x_size == 0); */
3167 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3168 mip.LoadOperation = LOAD_LOAD;
3169 mip.CombineOperation = COMBINE_SET;
3170 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3171 }
3172
3173 /* Load compute_dispatch_indirect_y_size into SRC0 */
3174 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 4);
3175
3176 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
3177 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3178 mip.LoadOperation = LOAD_LOAD;
3179 mip.CombineOperation = COMBINE_OR;
3180 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3181 }
3182
3183 /* Load compute_dispatch_indirect_z_size into SRC0 */
3184 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 8);
3185
3186 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
3187 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3188 mip.LoadOperation = LOAD_LOAD;
3189 mip.CombineOperation = COMBINE_OR;
3190 mip.CompareOperation = COMPARE_SRCS_EQUAL;
3191 }
3192
3193 /* predicate = !predicate; */
3194 #define COMPARE_FALSE 1
3195 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
3196 mip.LoadOperation = LOAD_LOADINV;
3197 mip.CombineOperation = COMBINE_OR;
3198 mip.CompareOperation = COMPARE_FALSE;
3199 }
3200 #endif
3201
3202 anv_batch_emit(batch, GENX(GPGPU_WALKER), ggw) {
3203 ggw.IndirectParameterEnable = true;
3204 ggw.PredicateEnable = GEN_GEN <= 7;
3205 ggw.SIMDSize = prog_data->simd_size / 16;
3206 ggw.ThreadDepthCounterMaximum = 0;
3207 ggw.ThreadHeightCounterMaximum = 0;
3208 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
3209 ggw.RightExecutionMask = pipeline->cs_right_mask;
3210 ggw.BottomExecutionMask = 0xffffffff;
3211 }
3212
3213 anv_batch_emit(batch, GENX(MEDIA_STATE_FLUSH), msf);
3214 }
3215
3216 static void
3217 genX(flush_pipeline_select)(struct anv_cmd_buffer *cmd_buffer,
3218 uint32_t pipeline)
3219 {
3220 UNUSED const struct gen_device_info *devinfo = &cmd_buffer->device->info;
3221
3222 if (cmd_buffer->state.current_pipeline == pipeline)
3223 return;
3224
3225 #if GEN_GEN >= 8 && GEN_GEN < 10
3226 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
3227 *
3228 * Software must clear the COLOR_CALC_STATE Valid field in
3229 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
3230 * with Pipeline Select set to GPGPU.
3231 *
3232 * The internal hardware docs recommend the same workaround for Gen9
3233 * hardware too.
3234 */
3235 if (pipeline == GPGPU)
3236 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CC_STATE_POINTERS), t);
3237 #endif
3238
3239 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
3240 * PIPELINE_SELECT [DevBWR+]":
3241 *
3242 * Project: DEVSNB+
3243 *
3244 * Software must ensure all the write caches are flushed through a
3245 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
3246 * command to invalidate read only caches prior to programming
3247 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
3248 */
3249 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
3250 pc.RenderTargetCacheFlushEnable = true;
3251 pc.DepthCacheFlushEnable = true;
3252 pc.DCFlushEnable = true;
3253 pc.PostSyncOperation = NoWrite;
3254 pc.CommandStreamerStallEnable = true;
3255 }
3256
3257 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
3258 pc.TextureCacheInvalidationEnable = true;
3259 pc.ConstantCacheInvalidationEnable = true;
3260 pc.StateCacheInvalidationEnable = true;
3261 pc.InstructionCacheInvalidateEnable = true;
3262 pc.PostSyncOperation = NoWrite;
3263 }
3264
3265 anv_batch_emit(&cmd_buffer->batch, GENX(PIPELINE_SELECT), ps) {
3266 #if GEN_GEN >= 9
3267 ps.MaskBits = 3;
3268 #endif
3269 ps.PipelineSelection = pipeline;
3270 }
3271
3272 #if GEN_GEN == 9
3273 if (devinfo->is_geminilake) {
3274 /* Project: DevGLK
3275 *
3276 * "This chicken bit works around a hardware issue with barrier logic
3277 * encountered when switching between GPGPU and 3D pipelines. To
3278 * workaround the issue, this mode bit should be set after a pipeline
3279 * is selected."
3280 */
3281 uint32_t scec;
3282 anv_pack_struct(&scec, GENX(SLICE_COMMON_ECO_CHICKEN1),
3283 .GLKBarrierMode =
3284 pipeline == GPGPU ? GLK_BARRIER_MODE_GPGPU
3285 : GLK_BARRIER_MODE_3D_HULL,
3286 .GLKBarrierModeMask = 1);
3287 emit_lri(&cmd_buffer->batch, GENX(SLICE_COMMON_ECO_CHICKEN1_num), scec);
3288 }
3289 #endif
3290
3291 cmd_buffer->state.current_pipeline = pipeline;
3292 }
3293
3294 void
3295 genX(flush_pipeline_select_3d)(struct anv_cmd_buffer *cmd_buffer)
3296 {
3297 genX(flush_pipeline_select)(cmd_buffer, _3D);
3298 }
3299
3300 void
3301 genX(flush_pipeline_select_gpgpu)(struct anv_cmd_buffer *cmd_buffer)
3302 {
3303 genX(flush_pipeline_select)(cmd_buffer, GPGPU);
3304 }
3305
3306 void
3307 genX(cmd_buffer_emit_gen7_depth_flush)(struct anv_cmd_buffer *cmd_buffer)
3308 {
3309 if (GEN_GEN >= 8)
3310 return;
3311
3312 /* From the Haswell PRM, documentation for 3DSTATE_DEPTH_BUFFER:
3313 *
3314 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e., any
3315 * combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
3316 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
3317 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
3318 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
3319 * Depth Flush Bit set, followed by another pipelined depth stall
3320 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
3321 * guarantee that the pipeline from WM onwards is already flushed (e.g.,
3322 * via a preceding MI_FLUSH)."
3323 */
3324 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
3325 pipe.DepthStallEnable = true;
3326 }
3327 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
3328 pipe.DepthCacheFlushEnable = true;
3329 }
3330 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
3331 pipe.DepthStallEnable = true;
3332 }
3333 }
3334
3335 static void
3336 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer *cmd_buffer)
3337 {
3338 struct anv_device *device = cmd_buffer->device;
3339 const struct anv_image_view *iview =
3340 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
3341 const struct anv_image *image = iview ? iview->image : NULL;
3342
3343 /* FIXME: Width and Height are wrong */
3344
3345 genX(cmd_buffer_emit_gen7_depth_flush)(cmd_buffer);
3346
3347 uint32_t *dw = anv_batch_emit_dwords(&cmd_buffer->batch,
3348 device->isl_dev.ds.size / 4);
3349 if (dw == NULL)
3350 return;
3351
3352 struct isl_depth_stencil_hiz_emit_info info = {
3353 .mocs = device->default_mocs,
3354 };
3355
3356 if (iview)
3357 info.view = &iview->planes[0].isl;
3358
3359 if (image && (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
3360 uint32_t depth_plane =
3361 anv_image_aspect_to_plane(image->aspects, VK_IMAGE_ASPECT_DEPTH_BIT);
3362 const struct anv_surface *surface = &image->planes[depth_plane].surface;
3363
3364 info.depth_surf = &surface->isl;
3365
3366 info.depth_address =
3367 anv_batch_emit_reloc(&cmd_buffer->batch,
3368 dw + device->isl_dev.ds.depth_offset / 4,
3369 image->planes[depth_plane].bo,
3370 image->planes[depth_plane].bo_offset +
3371 surface->offset);
3372
3373 const uint32_t ds =
3374 cmd_buffer->state.subpass->depth_stencil_attachment.attachment;
3375 info.hiz_usage = cmd_buffer->state.attachments[ds].aux_usage;
3376 if (info.hiz_usage == ISL_AUX_USAGE_HIZ) {
3377 info.hiz_surf = &image->planes[depth_plane].aux_surface.isl;
3378
3379 info.hiz_address =
3380 anv_batch_emit_reloc(&cmd_buffer->batch,
3381 dw + device->isl_dev.ds.hiz_offset / 4,
3382 image->planes[depth_plane].bo,
3383 image->planes[depth_plane].bo_offset +
3384 image->planes[depth_plane].aux_surface.offset);
3385
3386 info.depth_clear_value = ANV_HZ_FC_VAL;
3387 }
3388 }
3389
3390 if (image && (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT)) {
3391 uint32_t stencil_plane =
3392 anv_image_aspect_to_plane(image->aspects, VK_IMAGE_ASPECT_STENCIL_BIT);
3393 const struct anv_surface *surface = &image->planes[stencil_plane].surface;
3394
3395 info.stencil_surf = &surface->isl;
3396
3397 info.stencil_address =
3398 anv_batch_emit_reloc(&cmd_buffer->batch,
3399 dw + device->isl_dev.ds.stencil_offset / 4,
3400 image->planes[stencil_plane].bo,
3401 image->planes[stencil_plane].bo_offset + surface->offset);
3402 }
3403
3404 isl_emit_depth_stencil_hiz_s(&device->isl_dev, dw, &info);
3405
3406 cmd_buffer->state.hiz_enabled = info.hiz_usage == ISL_AUX_USAGE_HIZ;
3407 }
3408
3409 /**
3410 * This ANDs the view mask of the current subpass with the pending clear
3411 * views in the attachment to get the mask of views active in the subpass
3412 * that still need to be cleared.
3413 */
3414 static inline uint32_t
3415 get_multiview_subpass_clear_mask(const struct anv_cmd_state *cmd_state,
3416 const struct anv_attachment_state *att_state)
3417 {
3418 return cmd_state->subpass->view_mask & att_state->pending_clear_views;
3419 }
3420
3421 static inline bool
3422 do_first_layer_clear(const struct anv_cmd_state *cmd_state,
3423 const struct anv_attachment_state *att_state)
3424 {
3425 if (!cmd_state->subpass->view_mask)
3426 return true;
3427
3428 uint32_t pending_clear_mask =
3429 get_multiview_subpass_clear_mask(cmd_state, att_state);
3430
3431 return pending_clear_mask & 1;
3432 }
3433
3434 static inline bool
3435 current_subpass_is_last_for_attachment(const struct anv_cmd_state *cmd_state,
3436 uint32_t att_idx)
3437 {
3438 const uint32_t last_subpass_idx =
3439 cmd_state->pass->attachments[att_idx].last_subpass_idx;
3440 const struct anv_subpass *last_subpass =
3441 &cmd_state->pass->subpasses[last_subpass_idx];
3442 return last_subpass == cmd_state->subpass;
3443 }
3444
3445 static void
3446 cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
3447 uint32_t subpass_id)
3448 {
3449 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
3450 struct anv_subpass *subpass = &cmd_state->pass->subpasses[subpass_id];
3451 cmd_state->subpass = subpass;
3452
3453 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
3454
3455 /* Our implementation of VK_KHR_multiview uses instancing to draw the
3456 * different views. If the client asks for instancing, we need to use the
3457 * Instance Data Step Rate to ensure that we repeat the client's
3458 * per-instance data once for each view. Since this bit is in
3459 * VERTEX_BUFFER_STATE on gen7, we need to dirty vertex buffers at the top
3460 * of each subpass.
3461 */
3462 if (GEN_GEN == 7)
3463 cmd_buffer->state.gfx.vb_dirty |= ~0;
3464
3465 /* It is possible to start a render pass with an old pipeline. Because the
3466 * render pass and subpass index are both baked into the pipeline, this is
3467 * highly unlikely. In order to do so, it requires that you have a render
3468 * pass with a single subpass and that you use that render pass twice
3469 * back-to-back and use the same pipeline at the start of the second render
3470 * pass as at the end of the first. In order to avoid unpredictable issues
3471 * with this edge case, we just dirty the pipeline at the start of every
3472 * subpass.
3473 */
3474 cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_PIPELINE;
3475
3476 /* Accumulate any subpass flushes that need to happen before the subpass */
3477 cmd_buffer->state.pending_pipe_bits |=
3478 cmd_buffer->state.pass->subpass_flushes[subpass_id];
3479
3480 VkRect2D render_area = cmd_buffer->state.render_area;
3481 struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
3482
3483 bool is_multiview = subpass->view_mask != 0;
3484
3485 for (uint32_t i = 0; i < subpass->attachment_count; ++i) {
3486 const uint32_t a = subpass->attachments[i].attachment;
3487 if (a == VK_ATTACHMENT_UNUSED)
3488 continue;
3489
3490 assert(a < cmd_state->pass->attachment_count);
3491 struct anv_attachment_state *att_state = &cmd_state->attachments[a];
3492
3493 struct anv_image_view *iview = fb->attachments[a];
3494 const struct anv_image *image = iview->image;
3495
3496 /* A resolve is necessary before use as an input attachment if the clear
3497 * color or auxiliary buffer usage isn't supported by the sampler.
3498 */
3499 const bool input_needs_resolve =
3500 (att_state->fast_clear && !att_state->clear_color_is_zero_one) ||
3501 att_state->input_aux_usage != att_state->aux_usage;
3502
3503 VkImageLayout target_layout;
3504 if (iview->aspect_mask & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV &&
3505 !input_needs_resolve) {
3506 /* Layout transitions before the final only help to enable sampling
3507 * as an input attachment. If the input attachment supports sampling
3508 * using the auxiliary surface, we can skip such transitions by
3509 * making the target layout one that is CCS-aware.
3510 */
3511 target_layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
3512 } else {
3513 target_layout = subpass->attachments[i].layout;
3514 }
3515
3516 if (image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
3517 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
3518
3519 uint32_t base_layer, layer_count;
3520 if (image->type == VK_IMAGE_TYPE_3D) {
3521 base_layer = 0;
3522 layer_count = anv_minify(iview->image->extent.depth,
3523 iview->planes[0].isl.base_level);
3524 } else {
3525 base_layer = iview->planes[0].isl.base_array_layer;
3526 layer_count = fb->layers;
3527 }
3528
3529 transition_color_buffer(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
3530 iview->planes[0].isl.base_level, 1,
3531 base_layer, layer_count,
3532 att_state->current_layout, target_layout);
3533 } else if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
3534 transition_depth_buffer(cmd_buffer, image,
3535 att_state->current_layout, target_layout);
3536 att_state->aux_usage =
3537 anv_layout_to_aux_usage(&cmd_buffer->device->info, image,
3538 VK_IMAGE_ASPECT_DEPTH_BIT, target_layout);
3539 }
3540 att_state->current_layout = target_layout;
3541
3542 if (att_state->pending_clear_aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
3543 assert(att_state->pending_clear_aspects == VK_IMAGE_ASPECT_COLOR_BIT);
3544
3545 /* Multi-planar images are not supported as attachments */
3546 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
3547 assert(image->n_planes == 1);
3548
3549 uint32_t base_clear_layer = iview->planes[0].isl.base_array_layer;
3550 uint32_t clear_layer_count = fb->layers;
3551
3552 if (att_state->fast_clear &&
3553 do_first_layer_clear(cmd_state, att_state)) {
3554 /* We only support fast-clears on the first layer */
3555 assert(iview->planes[0].isl.base_level == 0);
3556 assert(iview->planes[0].isl.base_array_layer == 0);
3557
3558 if (iview->image->samples == 1) {
3559 anv_image_ccs_op(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
3560 0, 0, 1, ISL_AUX_OP_FAST_CLEAR, false);
3561 } else {
3562 anv_image_mcs_op(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
3563 0, 1, ISL_AUX_OP_FAST_CLEAR, false);
3564 }
3565 base_clear_layer++;
3566 clear_layer_count--;
3567 if (is_multiview)
3568 att_state->pending_clear_views &= ~1;
3569
3570 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->color.state,
3571 image, VK_IMAGE_ASPECT_COLOR_BIT,
3572 true /* copy from ss */);
3573
3574 if (att_state->clear_color_is_zero) {
3575 /* This image has the auxiliary buffer enabled. We can mark the
3576 * subresource as not needing a resolve because the clear color
3577 * will match what's in every RENDER_SURFACE_STATE object when
3578 * it's being used for sampling.
3579 */
3580 set_image_fast_clear_state(cmd_buffer, iview->image,
3581 VK_IMAGE_ASPECT_COLOR_BIT,
3582 ANV_FAST_CLEAR_DEFAULT_VALUE);
3583 } else {
3584 set_image_fast_clear_state(cmd_buffer, iview->image,
3585 VK_IMAGE_ASPECT_COLOR_BIT,
3586 ANV_FAST_CLEAR_ANY);
3587 }
3588 }
3589
3590 /* From the VkFramebufferCreateInfo spec:
3591 *
3592 * "If the render pass uses multiview, then layers must be one and each
3593 * attachment requires a number of layers that is greater than the
3594 * maximum bit index set in the view mask in the subpasses in which it
3595 * is used."
3596 *
3597 * So if multiview is active we ignore the number of layers in the
3598 * framebuffer and instead we honor the view mask from the subpass.
3599 */
3600 if (is_multiview) {
3601 assert(image->n_planes == 1);
3602 uint32_t pending_clear_mask =
3603 get_multiview_subpass_clear_mask(cmd_state, att_state);
3604
3605 uint32_t layer_idx;
3606 for_each_bit(layer_idx, pending_clear_mask) {
3607 uint32_t layer =
3608 iview->planes[0].isl.base_array_layer + layer_idx;
3609
3610 anv_image_clear_color(cmd_buffer, image,
3611 VK_IMAGE_ASPECT_COLOR_BIT,
3612 att_state->aux_usage,
3613 iview->planes[0].isl.format,
3614 iview->planes[0].isl.swizzle,
3615 iview->planes[0].isl.base_level,
3616 layer, 1,
3617 render_area,
3618 vk_to_isl_color(att_state->clear_value.color));
3619 }
3620
3621 att_state->pending_clear_views &= ~pending_clear_mask;
3622 } else if (clear_layer_count > 0) {
3623 assert(image->n_planes == 1);
3624 anv_image_clear_color(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
3625 att_state->aux_usage,
3626 iview->planes[0].isl.format,
3627 iview->planes[0].isl.swizzle,
3628 iview->planes[0].isl.base_level,
3629 base_clear_layer, clear_layer_count,
3630 render_area,
3631 vk_to_isl_color(att_state->clear_value.color));
3632 }
3633 } else if (att_state->pending_clear_aspects & (VK_IMAGE_ASPECT_DEPTH_BIT |
3634 VK_IMAGE_ASPECT_STENCIL_BIT)) {
3635 if (att_state->fast_clear && !is_multiview) {
3636 /* We currently only support HiZ for single-layer images */
3637 if (att_state->pending_clear_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
3638 assert(iview->image->planes[0].aux_usage == ISL_AUX_USAGE_HIZ);
3639 assert(iview->planes[0].isl.base_level == 0);
3640 assert(iview->planes[0].isl.base_array_layer == 0);
3641 assert(fb->layers == 1);
3642 }
3643
3644 anv_image_hiz_clear(cmd_buffer, image,
3645 att_state->pending_clear_aspects,
3646 iview->planes[0].isl.base_level,
3647 iview->planes[0].isl.base_array_layer,
3648 fb->layers, render_area,
3649 att_state->clear_value.depthStencil.stencil);
3650 } else if (is_multiview) {
3651 uint32_t pending_clear_mask =
3652 get_multiview_subpass_clear_mask(cmd_state, att_state);
3653
3654 uint32_t layer_idx;
3655 for_each_bit(layer_idx, pending_clear_mask) {
3656 uint32_t layer =
3657 iview->planes[0].isl.base_array_layer + layer_idx;
3658
3659 anv_image_clear_depth_stencil(cmd_buffer, image,
3660 att_state->pending_clear_aspects,
3661 att_state->aux_usage,
3662 iview->planes[0].isl.base_level,
3663 layer, 1,
3664 render_area,
3665 att_state->clear_value.depthStencil.depth,
3666 att_state->clear_value.depthStencil.stencil);
3667 }
3668
3669 att_state->pending_clear_views &= ~pending_clear_mask;
3670 } else {
3671 anv_image_clear_depth_stencil(cmd_buffer, image,
3672 att_state->pending_clear_aspects,
3673 att_state->aux_usage,
3674 iview->planes[0].isl.base_level,
3675 iview->planes[0].isl.base_array_layer,
3676 fb->layers, render_area,
3677 att_state->clear_value.depthStencil.depth,
3678 att_state->clear_value.depthStencil.stencil);
3679 }
3680 } else {
3681 assert(att_state->pending_clear_aspects == 0);
3682 }
3683
3684 if ((att_state->pending_load_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) &&
3685 image->planes[0].aux_surface.isl.size > 0 &&
3686 iview->planes[0].isl.base_level == 0 &&
3687 iview->planes[0].isl.base_array_layer == 0) {
3688 if (att_state->aux_usage != ISL_AUX_USAGE_NONE) {
3689 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->color.state,
3690 image, VK_IMAGE_ASPECT_COLOR_BIT,
3691 false /* copy to ss */);
3692 }
3693
3694 if (need_input_attachment_state(&cmd_state->pass->attachments[a]) &&
3695 att_state->input_aux_usage != ISL_AUX_USAGE_NONE) {
3696 genX(copy_fast_clear_dwords)(cmd_buffer, att_state->input.state,
3697 image, VK_IMAGE_ASPECT_COLOR_BIT,
3698 false /* copy to ss */);
3699 }
3700 }
3701
3702 if (subpass->attachments[i].usage ==
3703 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
3704 /* We assume that if we're starting a subpass, we're going to do some
3705 * rendering so we may end up with compressed data.
3706 */
3707 genX(cmd_buffer_mark_image_written)(cmd_buffer, iview->image,
3708 VK_IMAGE_ASPECT_COLOR_BIT,
3709 att_state->aux_usage,
3710 iview->planes[0].isl.base_level,
3711 iview->planes[0].isl.base_array_layer,
3712 fb->layers);
3713 } else if (subpass->attachments[i].usage ==
3714 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
3715 /* We may be writing depth or stencil so we need to mark the surface.
3716 * Unfortunately, there's no way to know at this point whether the
3717 * depth or stencil tests used will actually write to the surface.
3718 *
3719 * Even though stencil may be plane 1, it always shares a base_level
3720 * with depth.
3721 */
3722 const struct isl_view *ds_view = &iview->planes[0].isl;
3723 if (iview->aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
3724 genX(cmd_buffer_mark_image_written)(cmd_buffer, image,
3725 VK_IMAGE_ASPECT_DEPTH_BIT,
3726 att_state->aux_usage,
3727 ds_view->base_level,
3728 ds_view->base_array_layer,
3729 fb->layers);
3730 }
3731 if (iview->aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
3732 /* Even though stencil may be plane 1, it always shares a
3733 * base_level with depth.
3734 */
3735 genX(cmd_buffer_mark_image_written)(cmd_buffer, image,
3736 VK_IMAGE_ASPECT_STENCIL_BIT,
3737 ISL_AUX_USAGE_NONE,
3738 ds_view->base_level,
3739 ds_view->base_array_layer,
3740 fb->layers);
3741 }
3742 }
3743
3744 /* If multiview is enabled, then we are only done clearing when we no
3745 * longer have pending layers to clear, or when we have processed the
3746 * last subpass that uses this attachment.
3747 */
3748 if (!is_multiview ||
3749 att_state->pending_clear_views == 0 ||
3750 current_subpass_is_last_for_attachment(cmd_state, a)) {
3751 att_state->pending_clear_aspects = 0;
3752 }
3753
3754 att_state->pending_load_aspects = 0;
3755 }
3756
3757 cmd_buffer_emit_depth_stencil(cmd_buffer);
3758 }
3759
3760 static void
3761 cmd_buffer_end_subpass(struct anv_cmd_buffer *cmd_buffer)
3762 {
3763 struct anv_cmd_state *cmd_state = &cmd_buffer->state;
3764 struct anv_subpass *subpass = cmd_state->subpass;
3765 uint32_t subpass_id = anv_get_subpass_id(&cmd_buffer->state);
3766
3767 anv_cmd_buffer_resolve_subpass(cmd_buffer);
3768
3769 struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
3770 for (uint32_t i = 0; i < subpass->attachment_count; ++i) {
3771 const uint32_t a = subpass->attachments[i].attachment;
3772 if (a == VK_ATTACHMENT_UNUSED)
3773 continue;
3774
3775 if (cmd_state->pass->attachments[a].last_subpass_idx != subpass_id)
3776 continue;
3777
3778 assert(a < cmd_state->pass->attachment_count);
3779 struct anv_attachment_state *att_state = &cmd_state->attachments[a];
3780 struct anv_image_view *iview = fb->attachments[a];
3781 const struct anv_image *image = iview->image;
3782
3783 /* Transition the image into the final layout for this render pass */
3784 VkImageLayout target_layout =
3785 cmd_state->pass->attachments[a].final_layout;
3786
3787 if (image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
3788 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
3789
3790 uint32_t base_layer, layer_count;
3791 if (image->type == VK_IMAGE_TYPE_3D) {
3792 base_layer = 0;
3793 layer_count = anv_minify(iview->image->extent.depth,
3794 iview->planes[0].isl.base_level);
3795 } else {
3796 base_layer = iview->planes[0].isl.base_array_layer;
3797 layer_count = fb->layers;
3798 }
3799
3800 transition_color_buffer(cmd_buffer, image, VK_IMAGE_ASPECT_COLOR_BIT,
3801 iview->planes[0].isl.base_level, 1,
3802 base_layer, layer_count,
3803 att_state->current_layout, target_layout);
3804 } else if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
3805 transition_depth_buffer(cmd_buffer, image,
3806 att_state->current_layout, target_layout);
3807 }
3808 }
3809
3810 /* Accumulate any subpass flushes that need to happen after the subpass.
3811 * Yes, they do get accumulated twice in the NextSubpass case but since
3812 * genX_CmdNextSubpass just calls end/begin back-to-back, we just end up
3813 * ORing the bits in twice so it's harmless.
3814 */
3815 cmd_buffer->state.pending_pipe_bits |=
3816 cmd_buffer->state.pass->subpass_flushes[subpass_id + 1];
3817 }
3818
3819 void genX(CmdBeginRenderPass)(
3820 VkCommandBuffer commandBuffer,
3821 const VkRenderPassBeginInfo* pRenderPassBegin,
3822 VkSubpassContents contents)
3823 {
3824 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3825 ANV_FROM_HANDLE(anv_render_pass, pass, pRenderPassBegin->renderPass);
3826 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, pRenderPassBegin->framebuffer);
3827
3828 cmd_buffer->state.framebuffer = framebuffer;
3829 cmd_buffer->state.pass = pass;
3830 cmd_buffer->state.render_area = pRenderPassBegin->renderArea;
3831 VkResult result =
3832 genX(cmd_buffer_setup_attachments)(cmd_buffer, pass, pRenderPassBegin);
3833
3834 /* If we failed to setup the attachments we should not try to go further */
3835 if (result != VK_SUCCESS) {
3836 assert(anv_batch_has_error(&cmd_buffer->batch));
3837 return;
3838 }
3839
3840 genX(flush_pipeline_select_3d)(cmd_buffer);
3841
3842 cmd_buffer_begin_subpass(cmd_buffer, 0);
3843 }
3844
3845 void genX(CmdNextSubpass)(
3846 VkCommandBuffer commandBuffer,
3847 VkSubpassContents contents)
3848 {
3849 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3850
3851 if (anv_batch_has_error(&cmd_buffer->batch))
3852 return;
3853
3854 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
3855
3856 uint32_t prev_subpass = anv_get_subpass_id(&cmd_buffer->state);
3857 cmd_buffer_end_subpass(cmd_buffer);
3858 cmd_buffer_begin_subpass(cmd_buffer, prev_subpass + 1);
3859 }
3860
3861 void genX(CmdEndRenderPass)(
3862 VkCommandBuffer commandBuffer)
3863 {
3864 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
3865
3866 if (anv_batch_has_error(&cmd_buffer->batch))
3867 return;
3868
3869 cmd_buffer_end_subpass(cmd_buffer);
3870
3871 cmd_buffer->state.hiz_enabled = false;
3872
3873 #ifndef NDEBUG
3874 anv_dump_add_framebuffer(cmd_buffer, cmd_buffer->state.framebuffer);
3875 #endif
3876
3877 /* Remove references to render pass specific state. This enables us to
3878 * detect whether or not we're in a renderpass.
3879 */
3880 cmd_buffer->state.framebuffer = NULL;
3881 cmd_buffer->state.pass = NULL;
3882 cmd_buffer->state.subpass = NULL;
3883 }