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