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