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