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