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