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