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