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