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