anv/cmd_buffer: Set the L3 atomic disable mask bit in CHICKEN3 on HSW
[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
29 #include "common/gen_l3_config.h"
30 #include "genxml/gen_macros.h"
31 #include "genxml/genX_pack.h"
32
33 static void
34 emit_lrm(struct anv_batch *batch,
35 uint32_t reg, struct anv_bo *bo, uint32_t offset)
36 {
37 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
38 lrm.RegisterAddress = reg;
39 lrm.MemoryAddress = (struct anv_address) { bo, offset };
40 }
41 }
42
43 static void
44 emit_lri(struct anv_batch *batch, uint32_t reg, uint32_t imm)
45 {
46 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
47 lri.RegisterOffset = reg;
48 lri.DataDWord = imm;
49 }
50 }
51
52 void
53 genX(cmd_buffer_emit_state_base_address)(struct anv_cmd_buffer *cmd_buffer)
54 {
55 struct anv_device *device = cmd_buffer->device;
56
57 /* XXX: Do we need this on more than just BDW? */
58 #if (GEN_GEN >= 8)
59 /* Emit a render target cache flush.
60 *
61 * This isn't documented anywhere in the PRM. However, it seems to be
62 * necessary prior to changing the surface state base adress. Without
63 * this, we get GPU hangs when using multi-level command buffers which
64 * clear depth, reset state base address, and then go render stuff.
65 */
66 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
67 pc.RenderTargetCacheFlushEnable = true;
68 }
69 #endif
70
71 anv_batch_emit(&cmd_buffer->batch, GENX(STATE_BASE_ADDRESS), sba) {
72 sba.GeneralStateBaseAddress = (struct anv_address) { NULL, 0 };
73 sba.GeneralStateMemoryObjectControlState = GENX(MOCS);
74 sba.GeneralStateBaseAddressModifyEnable = true;
75
76 sba.SurfaceStateBaseAddress =
77 anv_cmd_buffer_surface_base_address(cmd_buffer);
78 sba.SurfaceStateMemoryObjectControlState = GENX(MOCS);
79 sba.SurfaceStateBaseAddressModifyEnable = true;
80
81 sba.DynamicStateBaseAddress =
82 (struct anv_address) { &device->dynamic_state_block_pool.bo, 0 };
83 sba.DynamicStateMemoryObjectControlState = GENX(MOCS),
84 sba.DynamicStateBaseAddressModifyEnable = true,
85
86 sba.IndirectObjectBaseAddress = (struct anv_address) { NULL, 0 };
87 sba.IndirectObjectMemoryObjectControlState = GENX(MOCS);
88 sba.IndirectObjectBaseAddressModifyEnable = true;
89
90 sba.InstructionBaseAddress =
91 (struct anv_address) { &device->instruction_block_pool.bo, 0 };
92 sba.InstructionMemoryObjectControlState = GENX(MOCS);
93 sba.InstructionBaseAddressModifyEnable = true;
94
95 # if (GEN_GEN >= 8)
96 /* Broadwell requires that we specify a buffer size for a bunch of
97 * these fields. However, since we will be growing the BO's live, we
98 * just set them all to the maximum.
99 */
100 sba.GeneralStateBufferSize = 0xfffff;
101 sba.GeneralStateBufferSizeModifyEnable = true;
102 sba.DynamicStateBufferSize = 0xfffff;
103 sba.DynamicStateBufferSizeModifyEnable = true;
104 sba.IndirectObjectBufferSize = 0xfffff;
105 sba.IndirectObjectBufferSizeModifyEnable = true;
106 sba.InstructionBufferSize = 0xfffff;
107 sba.InstructionBuffersizeModifyEnable = true;
108 # endif
109 }
110
111 /* After re-setting the surface state base address, we have to do some
112 * cache flusing so that the sampler engine will pick up the new
113 * SURFACE_STATE objects and binding tables. From the Broadwell PRM,
114 * Shared Function > 3D Sampler > State > State Caching (page 96):
115 *
116 * Coherency with system memory in the state cache, like the texture
117 * cache is handled partially by software. It is expected that the
118 * command stream or shader will issue Cache Flush operation or
119 * Cache_Flush sampler message to ensure that the L1 cache remains
120 * coherent with system memory.
121 *
122 * [...]
123 *
124 * Whenever the value of the Dynamic_State_Base_Addr,
125 * Surface_State_Base_Addr are altered, the L1 state cache must be
126 * invalidated to ensure the new surface or sampler state is fetched
127 * from system memory.
128 *
129 * The PIPE_CONTROL command has a "State Cache Invalidation Enable" bit
130 * which, according the PIPE_CONTROL instruction documentation in the
131 * Broadwell PRM:
132 *
133 * Setting this bit is independent of any other bit in this packet.
134 * This bit controls the invalidation of the L1 and L2 state caches
135 * at the top of the pipe i.e. at the parsing time.
136 *
137 * Unfortunately, experimentation seems to indicate that state cache
138 * invalidation through a PIPE_CONTROL does nothing whatsoever in
139 * regards to surface state and binding tables. In stead, it seems that
140 * invalidating the texture cache is what is actually needed.
141 *
142 * XXX: As far as we have been able to determine through
143 * experimentation, shows that flush the texture cache appears to be
144 * sufficient. The theory here is that all of the sampling/rendering
145 * units cache the binding table in the texture cache. However, we have
146 * yet to be able to actually confirm this.
147 */
148 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
149 pc.TextureCacheInvalidationEnable = true;
150 }
151 }
152
153 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT 0x00730000
154 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT 0x00d30000
155 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT 0x00610000
156
157 /**
158 * Program the hardware to use the specified L3 configuration.
159 */
160 void
161 genX(cmd_buffer_config_l3)(struct anv_cmd_buffer *cmd_buffer,
162 const struct gen_l3_config *cfg)
163 {
164 assert(cfg);
165 if (cfg == cmd_buffer->state.current_l3_config)
166 return;
167
168 if (unlikely(INTEL_DEBUG & DEBUG_L3)) {
169 fprintf(stderr, "L3 config transition: ");
170 gen_dump_l3_config(cfg, stderr);
171 }
172
173 const bool has_slm = cfg->n[GEN_L3P_SLM];
174
175 /* According to the hardware docs, the L3 partitioning can only be changed
176 * while the pipeline is completely drained and the caches are flushed,
177 * which involves a first PIPE_CONTROL flush which stalls the pipeline...
178 */
179 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
180 pc.DCFlushEnable = true;
181 pc.PostSyncOperation = NoWrite;
182 pc.CommandStreamerStallEnable = true;
183 }
184
185 /* ...followed by a second pipelined PIPE_CONTROL that initiates
186 * invalidation of the relevant caches. Note that because RO invalidation
187 * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
188 * command is processed by the CS) we cannot combine it with the previous
189 * stalling flush as the hardware documentation suggests, because that
190 * would cause the CS to stall on previous rendering *after* RO
191 * invalidation and wouldn't prevent the RO caches from being polluted by
192 * concurrent rendering before the stall completes. This intentionally
193 * doesn't implement the SKL+ hardware workaround suggesting to enable CS
194 * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
195 * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
196 * already guarantee that there is no concurrent GPGPU kernel execution
197 * (see SKL HSD 2132585).
198 */
199 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
200 pc.TextureCacheInvalidationEnable = true;
201 pc.ConstantCacheInvalidationEnable = true;
202 pc.InstructionCacheInvalidateEnable = true;
203 pc.StateCacheInvalidationEnable = true;
204 pc.PostSyncOperation = NoWrite;
205 }
206
207 /* Now send a third stalling flush to make sure that invalidation is
208 * complete when the L3 configuration registers are modified.
209 */
210 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
211 pc.DCFlushEnable = true;
212 pc.PostSyncOperation = NoWrite;
213 pc.CommandStreamerStallEnable = true;
214 }
215
216 #if GEN_GEN >= 8
217
218 assert(!cfg->n[GEN_L3P_IS] && !cfg->n[GEN_L3P_C] && !cfg->n[GEN_L3P_T]);
219
220 uint32_t l3cr;
221 anv_pack_struct(&l3cr, GENX(L3CNTLREG),
222 .SLMEnable = has_slm,
223 .URBAllocation = cfg->n[GEN_L3P_URB],
224 .ROAllocation = cfg->n[GEN_L3P_RO],
225 .DCAllocation = cfg->n[GEN_L3P_DC],
226 .AllAllocation = cfg->n[GEN_L3P_ALL]);
227
228 /* Set up the L3 partitioning. */
229 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG_num), l3cr);
230
231 #else
232
233 const bool has_dc = cfg->n[GEN_L3P_DC] || cfg->n[GEN_L3P_ALL];
234 const bool has_is = cfg->n[GEN_L3P_IS] || cfg->n[GEN_L3P_RO] ||
235 cfg->n[GEN_L3P_ALL];
236 const bool has_c = cfg->n[GEN_L3P_C] || cfg->n[GEN_L3P_RO] ||
237 cfg->n[GEN_L3P_ALL];
238 const bool has_t = cfg->n[GEN_L3P_T] || cfg->n[GEN_L3P_RO] ||
239 cfg->n[GEN_L3P_ALL];
240
241 assert(!cfg->n[GEN_L3P_ALL]);
242
243 /* When enabled SLM only uses a portion of the L3 on half of the banks,
244 * the matching space on the remaining banks has to be allocated to a
245 * client (URB for all validated configurations) set to the
246 * lower-bandwidth 2-bank address hashing mode.
247 */
248 const struct gen_device_info *devinfo = &cmd_buffer->device->info;
249 const bool urb_low_bw = has_slm && !devinfo->is_baytrail;
250 assert(!urb_low_bw || cfg->n[GEN_L3P_URB] == cfg->n[GEN_L3P_SLM]);
251
252 /* Minimum number of ways that can be allocated to the URB. */
253 const unsigned n0_urb = (devinfo->is_baytrail ? 32 : 0);
254 assert(cfg->n[GEN_L3P_URB] >= n0_urb);
255
256 uint32_t l3sqcr1, l3cr2, l3cr3;
257 anv_pack_struct(&l3sqcr1, GENX(L3SQCREG1),
258 .ConvertDC_UC = !has_dc,
259 .ConvertIS_UC = !has_is,
260 .ConvertC_UC = !has_c,
261 .ConvertT_UC = !has_t);
262 l3sqcr1 |=
263 GEN_IS_HASWELL ? HSW_L3SQCREG1_SQGHPCI_DEFAULT :
264 devinfo->is_baytrail ? VLV_L3SQCREG1_SQGHPCI_DEFAULT :
265 IVB_L3SQCREG1_SQGHPCI_DEFAULT;
266
267 anv_pack_struct(&l3cr2, GENX(L3CNTLREG2),
268 .SLMEnable = has_slm,
269 .URBLowBandwidth = urb_low_bw,
270 .URBAllocation = cfg->n[GEN_L3P_URB],
271 #if !GEN_IS_HASWELL
272 .ALLAllocation = cfg->n[GEN_L3P_ALL],
273 #endif
274 .ROAllocation = cfg->n[GEN_L3P_RO],
275 .DCAllocation = cfg->n[GEN_L3P_DC]);
276
277 anv_pack_struct(&l3cr3, GENX(L3CNTLREG3),
278 .ISAllocation = cfg->n[GEN_L3P_IS],
279 .ISLowBandwidth = 0,
280 .CAllocation = cfg->n[GEN_L3P_C],
281 .CLowBandwidth = 0,
282 .TAllocation = cfg->n[GEN_L3P_T],
283 .TLowBandwidth = 0);
284
285 /* Set up the L3 partitioning. */
286 emit_lri(&cmd_buffer->batch, GENX(L3SQCREG1_num), l3sqcr1);
287 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG2_num), l3cr2);
288 emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG3_num), l3cr3);
289
290 #if GEN_IS_HASWELL
291 if (cmd_buffer->device->instance->physicalDevice.cmd_parser_version >= 4) {
292 /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
293 * them disabled to avoid crashing the system hard.
294 */
295 uint32_t scratch1, chicken3;
296 anv_pack_struct(&scratch1, GENX(SCRATCH1),
297 .L3AtomicDisable = !has_dc);
298 anv_pack_struct(&chicken3, GENX(CHICKEN3),
299 .L3AtomicDisableMask = true,
300 .L3AtomicDisable = !has_dc);
301 emit_lri(&cmd_buffer->batch, GENX(SCRATCH1_num), scratch1);
302 emit_lri(&cmd_buffer->batch, GENX(CHICKEN3_num), chicken3);
303 }
304 #endif
305
306 #endif
307
308 cmd_buffer->state.current_l3_config = cfg;
309 }
310
311 void
312 genX(cmd_buffer_apply_pipe_flushes)(struct anv_cmd_buffer *cmd_buffer)
313 {
314 enum anv_pipe_bits bits = cmd_buffer->state.pending_pipe_bits;
315
316 /* Flushes are pipelined while invalidations are handled immediately.
317 * Therefore, if we're flushing anything then we need to schedule a stall
318 * before any invalidations can happen.
319 */
320 if (bits & ANV_PIPE_FLUSH_BITS)
321 bits |= ANV_PIPE_NEEDS_CS_STALL_BIT;
322
323 /* If we're going to do an invalidate and we have a pending CS stall that
324 * has yet to be resolved, we do the CS stall now.
325 */
326 if ((bits & ANV_PIPE_INVALIDATE_BITS) &&
327 (bits & ANV_PIPE_NEEDS_CS_STALL_BIT)) {
328 bits |= ANV_PIPE_CS_STALL_BIT;
329 bits &= ~ANV_PIPE_NEEDS_CS_STALL_BIT;
330 }
331
332 if (bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT)) {
333 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
334 pipe.DepthCacheFlushEnable = bits & ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
335 pipe.DCFlushEnable = bits & ANV_PIPE_DATA_CACHE_FLUSH_BIT;
336 pipe.RenderTargetCacheFlushEnable =
337 bits & ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
338
339 pipe.DepthStallEnable = bits & ANV_PIPE_DEPTH_STALL_BIT;
340 pipe.CommandStreamerStallEnable = bits & ANV_PIPE_CS_STALL_BIT;
341 pipe.StallAtPixelScoreboard = bits & ANV_PIPE_STALL_AT_SCOREBOARD_BIT;
342
343 /*
344 * According to the Broadwell documentation, any PIPE_CONTROL with the
345 * "Command Streamer Stall" bit set must also have another bit set,
346 * with five different options:
347 *
348 * - Render Target Cache Flush
349 * - Depth Cache Flush
350 * - Stall at Pixel Scoreboard
351 * - Post-Sync Operation
352 * - Depth Stall
353 * - DC Flush Enable
354 *
355 * I chose "Stall at Pixel Scoreboard" since that's what we use in
356 * mesa and it seems to work fine. The choice is fairly arbitrary.
357 */
358 if ((bits & ANV_PIPE_CS_STALL_BIT) &&
359 !(bits & (ANV_PIPE_FLUSH_BITS | ANV_PIPE_DEPTH_STALL_BIT |
360 ANV_PIPE_STALL_AT_SCOREBOARD_BIT)))
361 pipe.StallAtPixelScoreboard = true;
362 }
363
364 bits &= ~(ANV_PIPE_FLUSH_BITS | ANV_PIPE_CS_STALL_BIT);
365 }
366
367 if (bits & ANV_PIPE_INVALIDATE_BITS) {
368 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pipe) {
369 pipe.StateCacheInvalidationEnable =
370 bits & ANV_PIPE_STATE_CACHE_INVALIDATE_BIT;
371 pipe.ConstantCacheInvalidationEnable =
372 bits & ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT;
373 pipe.VFCacheInvalidationEnable =
374 bits & ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
375 pipe.TextureCacheInvalidationEnable =
376 bits & ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
377 pipe.InstructionCacheInvalidateEnable =
378 bits & ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT;
379 }
380
381 bits &= ~ANV_PIPE_INVALIDATE_BITS;
382 }
383
384 cmd_buffer->state.pending_pipe_bits = bits;
385 }
386
387 void genX(CmdPipelineBarrier)(
388 VkCommandBuffer commandBuffer,
389 VkPipelineStageFlags srcStageMask,
390 VkPipelineStageFlags destStageMask,
391 VkBool32 byRegion,
392 uint32_t memoryBarrierCount,
393 const VkMemoryBarrier* pMemoryBarriers,
394 uint32_t bufferMemoryBarrierCount,
395 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
396 uint32_t imageMemoryBarrierCount,
397 const VkImageMemoryBarrier* pImageMemoryBarriers)
398 {
399 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
400 uint32_t b;
401
402 /* XXX: Right now, we're really dumb and just flush whatever categories
403 * the app asks for. One of these days we may make this a bit better
404 * but right now that's all the hardware allows for in most areas.
405 */
406 VkAccessFlags src_flags = 0;
407 VkAccessFlags dst_flags = 0;
408
409 for (uint32_t i = 0; i < memoryBarrierCount; i++) {
410 src_flags |= pMemoryBarriers[i].srcAccessMask;
411 dst_flags |= pMemoryBarriers[i].dstAccessMask;
412 }
413
414 for (uint32_t i = 0; i < bufferMemoryBarrierCount; i++) {
415 src_flags |= pBufferMemoryBarriers[i].srcAccessMask;
416 dst_flags |= pBufferMemoryBarriers[i].dstAccessMask;
417 }
418
419 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
420 src_flags |= pImageMemoryBarriers[i].srcAccessMask;
421 dst_flags |= pImageMemoryBarriers[i].dstAccessMask;
422 }
423
424 enum anv_pipe_bits pipe_bits = 0;
425
426 for_each_bit(b, src_flags) {
427 switch ((VkAccessFlagBits)(1 << b)) {
428 case VK_ACCESS_SHADER_WRITE_BIT:
429 pipe_bits |= ANV_PIPE_DATA_CACHE_FLUSH_BIT;
430 break;
431 case VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT:
432 pipe_bits |= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
433 break;
434 case VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT:
435 pipe_bits |= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
436 break;
437 case VK_ACCESS_TRANSFER_WRITE_BIT:
438 pipe_bits |= ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
439 pipe_bits |= ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
440 break;
441 default:
442 break; /* Nothing to do */
443 }
444 }
445
446 for_each_bit(b, dst_flags) {
447 switch ((VkAccessFlagBits)(1 << b)) {
448 case VK_ACCESS_INDIRECT_COMMAND_READ_BIT:
449 case VK_ACCESS_INDEX_READ_BIT:
450 case VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT:
451 pipe_bits |= ANV_PIPE_VF_CACHE_INVALIDATE_BIT;
452 break;
453 case VK_ACCESS_UNIFORM_READ_BIT:
454 pipe_bits |= ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT;
455 pipe_bits |= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
456 break;
457 case VK_ACCESS_SHADER_READ_BIT:
458 case VK_ACCESS_COLOR_ATTACHMENT_READ_BIT:
459 case VK_ACCESS_TRANSFER_READ_BIT:
460 pipe_bits |= ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
461 break;
462 default:
463 break; /* Nothing to do */
464 }
465 }
466
467 cmd_buffer->state.pending_pipe_bits |= pipe_bits;
468 }
469
470 static void
471 cmd_buffer_alloc_push_constants(struct anv_cmd_buffer *cmd_buffer)
472 {
473 VkShaderStageFlags stages = cmd_buffer->state.pipeline->active_stages;
474
475 /* In order to avoid thrash, we assume that vertex and fragment stages
476 * always exist. In the rare case where one is missing *and* the other
477 * uses push concstants, this may be suboptimal. However, avoiding stalls
478 * seems more important.
479 */
480 stages |= VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT;
481
482 if (stages == cmd_buffer->state.push_constant_stages)
483 return;
484
485 #if GEN_GEN >= 8
486 const unsigned push_constant_kb = 32;
487 #elif GEN_IS_HASWELL
488 const unsigned push_constant_kb = cmd_buffer->device->info.gt == 3 ? 32 : 16;
489 #else
490 const unsigned push_constant_kb = 16;
491 #endif
492
493 const unsigned num_stages =
494 _mesa_bitcount(stages & VK_SHADER_STAGE_ALL_GRAPHICS);
495 unsigned size_per_stage = push_constant_kb / num_stages;
496
497 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
498 * units of 2KB. Incidentally, these are the same platforms that have
499 * 32KB worth of push constant space.
500 */
501 if (push_constant_kb == 32)
502 size_per_stage &= ~1u;
503
504 uint32_t kb_used = 0;
505 for (int i = MESA_SHADER_VERTEX; i < MESA_SHADER_FRAGMENT; i++) {
506 unsigned push_size = (stages & (1 << i)) ? size_per_stage : 0;
507 anv_batch_emit(&cmd_buffer->batch,
508 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
509 alloc._3DCommandSubOpcode = 18 + i;
510 alloc.ConstantBufferOffset = (push_size > 0) ? kb_used : 0;
511 alloc.ConstantBufferSize = push_size;
512 }
513 kb_used += push_size;
514 }
515
516 anv_batch_emit(&cmd_buffer->batch,
517 GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS), alloc) {
518 alloc.ConstantBufferOffset = kb_used;
519 alloc.ConstantBufferSize = push_constant_kb - kb_used;
520 }
521
522 cmd_buffer->state.push_constant_stages = stages;
523
524 /* From the BDW PRM for 3DSTATE_PUSH_CONSTANT_ALLOC_VS:
525 *
526 * "The 3DSTATE_CONSTANT_VS must be reprogrammed prior to
527 * the next 3DPRIMITIVE command after programming the
528 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS"
529 *
530 * Since 3DSTATE_PUSH_CONSTANT_ALLOC_VS is programmed as part of
531 * pipeline setup, we need to dirty push constants.
532 */
533 cmd_buffer->state.push_constants_dirty |= VK_SHADER_STAGE_ALL_GRAPHICS;
534 }
535
536 static void
537 cmd_buffer_emit_descriptor_pointers(struct anv_cmd_buffer *cmd_buffer,
538 uint32_t stages)
539 {
540 static const uint32_t sampler_state_opcodes[] = {
541 [MESA_SHADER_VERTEX] = 43,
542 [MESA_SHADER_TESS_CTRL] = 44, /* HS */
543 [MESA_SHADER_TESS_EVAL] = 45, /* DS */
544 [MESA_SHADER_GEOMETRY] = 46,
545 [MESA_SHADER_FRAGMENT] = 47,
546 [MESA_SHADER_COMPUTE] = 0,
547 };
548
549 static const uint32_t binding_table_opcodes[] = {
550 [MESA_SHADER_VERTEX] = 38,
551 [MESA_SHADER_TESS_CTRL] = 39,
552 [MESA_SHADER_TESS_EVAL] = 40,
553 [MESA_SHADER_GEOMETRY] = 41,
554 [MESA_SHADER_FRAGMENT] = 42,
555 [MESA_SHADER_COMPUTE] = 0,
556 };
557
558 anv_foreach_stage(s, stages) {
559 if (cmd_buffer->state.samplers[s].alloc_size > 0) {
560 anv_batch_emit(&cmd_buffer->batch,
561 GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ssp) {
562 ssp._3DCommandSubOpcode = sampler_state_opcodes[s];
563 ssp.PointertoVSSamplerState = cmd_buffer->state.samplers[s].offset;
564 }
565 }
566
567 /* Always emit binding table pointers if we're asked to, since on SKL
568 * this is what flushes push constants. */
569 anv_batch_emit(&cmd_buffer->batch,
570 GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), btp) {
571 btp._3DCommandSubOpcode = binding_table_opcodes[s];
572 btp.PointertoVSBindingTable = cmd_buffer->state.binding_tables[s].offset;
573 }
574 }
575 }
576
577 static uint32_t
578 cmd_buffer_flush_push_constants(struct anv_cmd_buffer *cmd_buffer)
579 {
580 static const uint32_t push_constant_opcodes[] = {
581 [MESA_SHADER_VERTEX] = 21,
582 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
583 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
584 [MESA_SHADER_GEOMETRY] = 22,
585 [MESA_SHADER_FRAGMENT] = 23,
586 [MESA_SHADER_COMPUTE] = 0,
587 };
588
589 VkShaderStageFlags flushed = 0;
590
591 anv_foreach_stage(stage, cmd_buffer->state.push_constants_dirty) {
592 if (stage == MESA_SHADER_COMPUTE)
593 continue;
594
595 struct anv_state state = anv_cmd_buffer_push_constants(cmd_buffer, stage);
596
597 if (state.offset == 0) {
598 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CONSTANT_VS), c)
599 c._3DCommandSubOpcode = push_constant_opcodes[stage];
600 } else {
601 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CONSTANT_VS), c) {
602 c._3DCommandSubOpcode = push_constant_opcodes[stage],
603 c.ConstantBody = (struct GENX(3DSTATE_CONSTANT_BODY)) {
604 #if GEN_GEN >= 9
605 .PointerToConstantBuffer2 = { &cmd_buffer->device->dynamic_state_block_pool.bo, state.offset },
606 .ConstantBuffer2ReadLength = DIV_ROUND_UP(state.alloc_size, 32),
607 #else
608 .PointerToConstantBuffer0 = { .offset = state.offset },
609 .ConstantBuffer0ReadLength = DIV_ROUND_UP(state.alloc_size, 32),
610 #endif
611 };
612 }
613 }
614
615 flushed |= mesa_to_vk_shader_stage(stage);
616 }
617
618 cmd_buffer->state.push_constants_dirty &= ~VK_SHADER_STAGE_ALL_GRAPHICS;
619
620 return flushed;
621 }
622
623 void
624 genX(cmd_buffer_flush_state)(struct anv_cmd_buffer *cmd_buffer)
625 {
626 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
627 uint32_t *p;
628
629 uint32_t vb_emit = cmd_buffer->state.vb_dirty & pipeline->vb_used;
630
631 assert((pipeline->active_stages & VK_SHADER_STAGE_COMPUTE_BIT) == 0);
632
633 genX(cmd_buffer_config_l3)(cmd_buffer, pipeline->urb.l3_config);
634
635 genX(flush_pipeline_select_3d)(cmd_buffer);
636
637 if (vb_emit) {
638 const uint32_t num_buffers = __builtin_popcount(vb_emit);
639 const uint32_t num_dwords = 1 + num_buffers * 4;
640
641 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
642 GENX(3DSTATE_VERTEX_BUFFERS));
643 uint32_t vb, i = 0;
644 for_each_bit(vb, vb_emit) {
645 struct anv_buffer *buffer = cmd_buffer->state.vertex_bindings[vb].buffer;
646 uint32_t offset = cmd_buffer->state.vertex_bindings[vb].offset;
647
648 struct GENX(VERTEX_BUFFER_STATE) state = {
649 .VertexBufferIndex = vb,
650
651 #if GEN_GEN >= 8
652 .MemoryObjectControlState = GENX(MOCS),
653 #else
654 .BufferAccessType = pipeline->instancing_enable[vb] ? INSTANCEDATA : VERTEXDATA,
655 .InstanceDataStepRate = 1,
656 .VertexBufferMemoryObjectControlState = GENX(MOCS),
657 #endif
658
659 .AddressModifyEnable = true,
660 .BufferPitch = pipeline->binding_stride[vb],
661 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
662
663 #if GEN_GEN >= 8
664 .BufferSize = buffer->size - offset
665 #else
666 .EndAddress = { buffer->bo, buffer->offset + buffer->size - 1},
667 #endif
668 };
669
670 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, &p[1 + i * 4], &state);
671 i++;
672 }
673 }
674
675 cmd_buffer->state.vb_dirty &= ~vb_emit;
676
677 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_PIPELINE) {
678 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
679
680 /* The exact descriptor layout is pulled from the pipeline, so we need
681 * to re-emit binding tables on every pipeline change.
682 */
683 cmd_buffer->state.descriptors_dirty |=
684 cmd_buffer->state.pipeline->active_stages;
685
686 /* If the pipeline changed, we may need to re-allocate push constant
687 * space in the URB.
688 */
689 cmd_buffer_alloc_push_constants(cmd_buffer);
690 }
691
692 #if GEN_GEN <= 7
693 if (cmd_buffer->state.descriptors_dirty & VK_SHADER_STAGE_VERTEX_BIT ||
694 cmd_buffer->state.push_constants_dirty & VK_SHADER_STAGE_VERTEX_BIT) {
695 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
696 *
697 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
698 * stall needs to be sent just prior to any 3DSTATE_VS,
699 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
700 * 3DSTATE_BINDING_TABLE_POINTER_VS,
701 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one
702 * PIPE_CONTROL needs to be sent before any combination of VS
703 * associated 3DSTATE."
704 */
705 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
706 pc.DepthStallEnable = true;
707 pc.PostSyncOperation = WriteImmediateData;
708 pc.Address =
709 (struct anv_address) { &cmd_buffer->device->workaround_bo, 0 };
710 }
711 }
712 #endif
713
714 /* We emit the binding tables and sampler tables first, then emit push
715 * constants and then finally emit binding table and sampler table
716 * pointers. It has to happen in this order, since emitting the binding
717 * tables may change the push constants (in case of storage images). After
718 * emitting push constants, on SKL+ we have to emit the corresponding
719 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
720 */
721 uint32_t dirty = 0;
722 if (cmd_buffer->state.descriptors_dirty)
723 dirty = anv_cmd_buffer_flush_descriptor_sets(cmd_buffer);
724
725 if (cmd_buffer->state.push_constants_dirty) {
726 #if GEN_GEN >= 9
727 /* On Sky Lake and later, the binding table pointers commands are
728 * what actually flush the changes to push constant state so we need
729 * to dirty them so they get re-emitted below.
730 */
731 dirty |= cmd_buffer_flush_push_constants(cmd_buffer);
732 #else
733 cmd_buffer_flush_push_constants(cmd_buffer);
734 #endif
735 }
736
737 if (dirty)
738 cmd_buffer_emit_descriptor_pointers(cmd_buffer, dirty);
739
740 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_DYNAMIC_VIEWPORT)
741 gen8_cmd_buffer_emit_viewport(cmd_buffer);
742
743 if (cmd_buffer->state.dirty & (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT |
744 ANV_CMD_DIRTY_PIPELINE)) {
745 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer,
746 pipeline->depth_clamp_enable);
747 }
748
749 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_DYNAMIC_SCISSOR)
750 gen7_cmd_buffer_emit_scissor(cmd_buffer);
751
752 genX(cmd_buffer_flush_dynamic_state)(cmd_buffer);
753
754 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
755 }
756
757 static void
758 emit_base_vertex_instance_bo(struct anv_cmd_buffer *cmd_buffer,
759 struct anv_bo *bo, uint32_t offset)
760 {
761 uint32_t *p = anv_batch_emitn(&cmd_buffer->batch, 5,
762 GENX(3DSTATE_VERTEX_BUFFERS));
763
764 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, p + 1,
765 &(struct GENX(VERTEX_BUFFER_STATE)) {
766 .VertexBufferIndex = 32, /* Reserved for this */
767 .AddressModifyEnable = true,
768 .BufferPitch = 0,
769 #if (GEN_GEN >= 8)
770 .MemoryObjectControlState = GENX(MOCS),
771 .BufferStartingAddress = { bo, offset },
772 .BufferSize = 8
773 #else
774 .VertexBufferMemoryObjectControlState = GENX(MOCS),
775 .BufferStartingAddress = { bo, offset },
776 .EndAddress = { bo, offset + 8 },
777 #endif
778 });
779 }
780
781 static void
782 emit_base_vertex_instance(struct anv_cmd_buffer *cmd_buffer,
783 uint32_t base_vertex, uint32_t base_instance)
784 {
785 struct anv_state id_state =
786 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 8, 4);
787
788 ((uint32_t *)id_state.map)[0] = base_vertex;
789 ((uint32_t *)id_state.map)[1] = base_instance;
790
791 if (!cmd_buffer->device->info.has_llc)
792 anv_state_clflush(id_state);
793
794 emit_base_vertex_instance_bo(cmd_buffer,
795 &cmd_buffer->device->dynamic_state_block_pool.bo, id_state.offset);
796 }
797
798 void genX(CmdDraw)(
799 VkCommandBuffer commandBuffer,
800 uint32_t vertexCount,
801 uint32_t instanceCount,
802 uint32_t firstVertex,
803 uint32_t firstInstance)
804 {
805 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
806 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
807 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
808
809 genX(cmd_buffer_flush_state)(cmd_buffer);
810
811 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
812 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
813
814 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
815 prim.VertexAccessType = SEQUENTIAL;
816 prim.PrimitiveTopologyType = pipeline->topology;
817 prim.VertexCountPerInstance = vertexCount;
818 prim.StartVertexLocation = firstVertex;
819 prim.InstanceCount = instanceCount;
820 prim.StartInstanceLocation = firstInstance;
821 prim.BaseVertexLocation = 0;
822 }
823 }
824
825 void genX(CmdDrawIndexed)(
826 VkCommandBuffer commandBuffer,
827 uint32_t indexCount,
828 uint32_t instanceCount,
829 uint32_t firstIndex,
830 int32_t vertexOffset,
831 uint32_t firstInstance)
832 {
833 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
834 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
835 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
836
837 genX(cmd_buffer_flush_state)(cmd_buffer);
838
839 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
840 emit_base_vertex_instance(cmd_buffer, vertexOffset, firstInstance);
841
842 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
843 prim.VertexAccessType = RANDOM;
844 prim.PrimitiveTopologyType = pipeline->topology;
845 prim.VertexCountPerInstance = indexCount;
846 prim.StartVertexLocation = firstIndex;
847 prim.InstanceCount = instanceCount;
848 prim.StartInstanceLocation = firstInstance;
849 prim.BaseVertexLocation = vertexOffset;
850 }
851 }
852
853 /* Auto-Draw / Indirect Registers */
854 #define GEN7_3DPRIM_END_OFFSET 0x2420
855 #define GEN7_3DPRIM_START_VERTEX 0x2430
856 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
857 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
858 #define GEN7_3DPRIM_START_INSTANCE 0x243C
859 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
860
861 void genX(CmdDrawIndirect)(
862 VkCommandBuffer commandBuffer,
863 VkBuffer _buffer,
864 VkDeviceSize offset,
865 uint32_t drawCount,
866 uint32_t stride)
867 {
868 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
869 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
870 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
871 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
872 struct anv_bo *bo = buffer->bo;
873 uint32_t bo_offset = buffer->offset + offset;
874
875 genX(cmd_buffer_flush_state)(cmd_buffer);
876
877 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
878 emit_base_vertex_instance_bo(cmd_buffer, bo, bo_offset + 8);
879
880 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
881 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
882 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
883 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 12);
884 emit_lri(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, 0);
885
886 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
887 prim.IndirectParameterEnable = true;
888 prim.VertexAccessType = SEQUENTIAL;
889 prim.PrimitiveTopologyType = pipeline->topology;
890 }
891 }
892
893 void genX(CmdDrawIndexedIndirect)(
894 VkCommandBuffer commandBuffer,
895 VkBuffer _buffer,
896 VkDeviceSize offset,
897 uint32_t drawCount,
898 uint32_t stride)
899 {
900 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
901 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
902 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
903 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
904 struct anv_bo *bo = buffer->bo;
905 uint32_t bo_offset = buffer->offset + offset;
906
907 genX(cmd_buffer_flush_state)(cmd_buffer);
908
909 /* TODO: We need to stomp base vertex to 0 somehow */
910 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
911 emit_base_vertex_instance_bo(cmd_buffer, bo, bo_offset + 12);
912
913 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
914 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
915 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
916 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, bo, bo_offset + 12);
917 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 16);
918
919 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
920 prim.IndirectParameterEnable = true;
921 prim.VertexAccessType = RANDOM;
922 prim.PrimitiveTopologyType = pipeline->topology;
923 }
924 }
925
926 #if GEN_GEN == 7
927
928 static bool
929 verify_cmd_parser(const struct anv_device *device,
930 int required_version,
931 const char *function)
932 {
933 if (device->instance->physicalDevice.cmd_parser_version < required_version) {
934 vk_errorf(VK_ERROR_FEATURE_NOT_PRESENT,
935 "cmd parser version %d is required for %s",
936 required_version, function);
937 return false;
938 } else {
939 return true;
940 }
941 }
942
943 #endif
944
945 void genX(CmdDispatch)(
946 VkCommandBuffer commandBuffer,
947 uint32_t x,
948 uint32_t y,
949 uint32_t z)
950 {
951 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
952 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
953 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
954
955 if (prog_data->uses_num_work_groups) {
956 struct anv_state state =
957 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 12, 4);
958 uint32_t *sizes = state.map;
959 sizes[0] = x;
960 sizes[1] = y;
961 sizes[2] = z;
962 if (!cmd_buffer->device->info.has_llc)
963 anv_state_clflush(state);
964 cmd_buffer->state.num_workgroups_offset = state.offset;
965 cmd_buffer->state.num_workgroups_bo =
966 &cmd_buffer->device->dynamic_state_block_pool.bo;
967 }
968
969 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
970
971 anv_batch_emit(&cmd_buffer->batch, GENX(GPGPU_WALKER), ggw) {
972 ggw.SIMDSize = prog_data->simd_size / 16;
973 ggw.ThreadDepthCounterMaximum = 0;
974 ggw.ThreadHeightCounterMaximum = 0;
975 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
976 ggw.ThreadGroupIDXDimension = x;
977 ggw.ThreadGroupIDYDimension = y;
978 ggw.ThreadGroupIDZDimension = z;
979 ggw.RightExecutionMask = pipeline->cs_right_mask;
980 ggw.BottomExecutionMask = 0xffffffff;
981 }
982
983 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_STATE_FLUSH), msf);
984 }
985
986 #define GPGPU_DISPATCHDIMX 0x2500
987 #define GPGPU_DISPATCHDIMY 0x2504
988 #define GPGPU_DISPATCHDIMZ 0x2508
989
990 #define MI_PREDICATE_SRC0 0x2400
991 #define MI_PREDICATE_SRC1 0x2408
992
993 void genX(CmdDispatchIndirect)(
994 VkCommandBuffer commandBuffer,
995 VkBuffer _buffer,
996 VkDeviceSize offset)
997 {
998 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
999 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1000 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
1001 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
1002 struct anv_bo *bo = buffer->bo;
1003 uint32_t bo_offset = buffer->offset + offset;
1004 struct anv_batch *batch = &cmd_buffer->batch;
1005
1006 #if GEN_GEN == 7
1007 /* Linux 4.4 added command parser version 5 which allows the GPGPU
1008 * indirect dispatch registers to be written.
1009 */
1010 if (!verify_cmd_parser(cmd_buffer->device, 5, "vkCmdDispatchIndirect"))
1011 return;
1012 #endif
1013
1014 if (prog_data->uses_num_work_groups) {
1015 cmd_buffer->state.num_workgroups_offset = bo_offset;
1016 cmd_buffer->state.num_workgroups_bo = bo;
1017 }
1018
1019 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
1020
1021 emit_lrm(batch, GPGPU_DISPATCHDIMX, bo, bo_offset);
1022 emit_lrm(batch, GPGPU_DISPATCHDIMY, bo, bo_offset + 4);
1023 emit_lrm(batch, GPGPU_DISPATCHDIMZ, bo, bo_offset + 8);
1024
1025 #if GEN_GEN <= 7
1026 /* Clear upper 32-bits of SRC0 and all 64-bits of SRC1 */
1027 emit_lri(batch, MI_PREDICATE_SRC0 + 4, 0);
1028 emit_lri(batch, MI_PREDICATE_SRC1 + 0, 0);
1029 emit_lri(batch, MI_PREDICATE_SRC1 + 4, 0);
1030
1031 /* Load compute_dispatch_indirect_x_size into SRC0 */
1032 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 0);
1033
1034 /* predicate = (compute_dispatch_indirect_x_size == 0); */
1035 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
1036 mip.LoadOperation = LOAD_LOAD;
1037 mip.CombineOperation = COMBINE_SET;
1038 mip.CompareOperation = COMPARE_SRCS_EQUAL;
1039 }
1040
1041 /* Load compute_dispatch_indirect_y_size into SRC0 */
1042 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 4);
1043
1044 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
1045 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
1046 mip.LoadOperation = LOAD_LOAD;
1047 mip.CombineOperation = COMBINE_OR;
1048 mip.CompareOperation = COMPARE_SRCS_EQUAL;
1049 }
1050
1051 /* Load compute_dispatch_indirect_z_size into SRC0 */
1052 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 8);
1053
1054 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
1055 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
1056 mip.LoadOperation = LOAD_LOAD;
1057 mip.CombineOperation = COMBINE_OR;
1058 mip.CompareOperation = COMPARE_SRCS_EQUAL;
1059 }
1060
1061 /* predicate = !predicate; */
1062 #define COMPARE_FALSE 1
1063 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
1064 mip.LoadOperation = LOAD_LOADINV;
1065 mip.CombineOperation = COMBINE_OR;
1066 mip.CompareOperation = COMPARE_FALSE;
1067 }
1068 #endif
1069
1070 anv_batch_emit(batch, GENX(GPGPU_WALKER), ggw) {
1071 ggw.IndirectParameterEnable = true;
1072 ggw.PredicateEnable = GEN_GEN <= 7;
1073 ggw.SIMDSize = prog_data->simd_size / 16;
1074 ggw.ThreadDepthCounterMaximum = 0;
1075 ggw.ThreadHeightCounterMaximum = 0;
1076 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
1077 ggw.RightExecutionMask = pipeline->cs_right_mask;
1078 ggw.BottomExecutionMask = 0xffffffff;
1079 }
1080
1081 anv_batch_emit(batch, GENX(MEDIA_STATE_FLUSH), msf);
1082 }
1083
1084 static void
1085 flush_pipeline_before_pipeline_select(struct anv_cmd_buffer *cmd_buffer,
1086 uint32_t pipeline)
1087 {
1088 #if GEN_GEN >= 8 && GEN_GEN < 10
1089 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
1090 *
1091 * Software must clear the COLOR_CALC_STATE Valid field in
1092 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
1093 * with Pipeline Select set to GPGPU.
1094 *
1095 * The internal hardware docs recommend the same workaround for Gen9
1096 * hardware too.
1097 */
1098 if (pipeline == GPGPU)
1099 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CC_STATE_POINTERS), t);
1100 #elif GEN_GEN <= 7
1101 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
1102 * PIPELINE_SELECT [DevBWR+]":
1103 *
1104 * Project: DEVSNB+
1105 *
1106 * Software must ensure all the write caches are flushed through a
1107 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
1108 * command to invalidate read only caches prior to programming
1109 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
1110 */
1111 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1112 pc.RenderTargetCacheFlushEnable = true;
1113 pc.DepthCacheFlushEnable = true;
1114 pc.DCFlushEnable = true;
1115 pc.PostSyncOperation = NoWrite;
1116 pc.CommandStreamerStallEnable = true;
1117 }
1118
1119 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1120 pc.TextureCacheInvalidationEnable = true;
1121 pc.ConstantCacheInvalidationEnable = true;
1122 pc.StateCacheInvalidationEnable = true;
1123 pc.InstructionCacheInvalidateEnable = true;
1124 pc.PostSyncOperation = NoWrite;
1125 }
1126 #endif
1127 }
1128
1129 void
1130 genX(flush_pipeline_select_3d)(struct anv_cmd_buffer *cmd_buffer)
1131 {
1132 if (cmd_buffer->state.current_pipeline != _3D) {
1133 flush_pipeline_before_pipeline_select(cmd_buffer, _3D);
1134
1135 anv_batch_emit(&cmd_buffer->batch, GENX(PIPELINE_SELECT), ps) {
1136 #if GEN_GEN >= 9
1137 ps.MaskBits = 3;
1138 #endif
1139 ps.PipelineSelection = _3D;
1140 }
1141
1142 cmd_buffer->state.current_pipeline = _3D;
1143 }
1144 }
1145
1146 void
1147 genX(flush_pipeline_select_gpgpu)(struct anv_cmd_buffer *cmd_buffer)
1148 {
1149 if (cmd_buffer->state.current_pipeline != GPGPU) {
1150 flush_pipeline_before_pipeline_select(cmd_buffer, GPGPU);
1151
1152 anv_batch_emit(&cmd_buffer->batch, GENX(PIPELINE_SELECT), ps) {
1153 #if GEN_GEN >= 9
1154 ps.MaskBits = 3;
1155 #endif
1156 ps.PipelineSelection = GPGPU;
1157 }
1158
1159 cmd_buffer->state.current_pipeline = GPGPU;
1160 }
1161 }
1162
1163 struct anv_state
1164 genX(cmd_buffer_alloc_null_surface_state)(struct anv_cmd_buffer *cmd_buffer,
1165 struct anv_framebuffer *fb)
1166 {
1167 struct anv_state state =
1168 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
1169
1170 struct GENX(RENDER_SURFACE_STATE) null_ss = {
1171 .SurfaceType = SURFTYPE_NULL,
1172 .SurfaceArray = fb->layers > 0,
1173 .SurfaceFormat = ISL_FORMAT_R8G8B8A8_UNORM,
1174 #if GEN_GEN >= 8
1175 .TileMode = YMAJOR,
1176 #else
1177 .TiledSurface = true,
1178 #endif
1179 .Width = fb->width - 1,
1180 .Height = fb->height - 1,
1181 .Depth = fb->layers - 1,
1182 .RenderTargetViewExtent = fb->layers - 1,
1183 };
1184
1185 GENX(RENDER_SURFACE_STATE_pack)(NULL, state.map, &null_ss);
1186
1187 if (!cmd_buffer->device->info.has_llc)
1188 anv_state_clflush(state);
1189
1190 return state;
1191 }
1192
1193 static void
1194 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer *cmd_buffer)
1195 {
1196 struct anv_device *device = cmd_buffer->device;
1197 const struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
1198 const struct anv_image_view *iview =
1199 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
1200 const struct anv_image *image = iview ? iview->image : NULL;
1201 const bool has_depth = image && (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
1202 const bool has_stencil =
1203 image && (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
1204
1205 /* FIXME: Implement the PMA stall W/A */
1206 /* FIXME: Width and Height are wrong */
1207
1208 /* Emit 3DSTATE_DEPTH_BUFFER */
1209 if (has_depth) {
1210 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_DEPTH_BUFFER), db) {
1211 db.SurfaceType = SURFTYPE_2D;
1212 db.DepthWriteEnable = true;
1213 db.StencilWriteEnable = has_stencil;
1214 db.HierarchicalDepthBufferEnable = false;
1215
1216 db.SurfaceFormat = isl_surf_get_depth_format(&device->isl_dev,
1217 &image->depth_surface.isl);
1218
1219 db.SurfaceBaseAddress = (struct anv_address) {
1220 .bo = image->bo,
1221 .offset = image->offset + image->depth_surface.offset,
1222 };
1223 db.DepthBufferObjectControlState = GENX(MOCS),
1224
1225 db.SurfacePitch = image->depth_surface.isl.row_pitch - 1;
1226 db.Height = image->extent.height - 1;
1227 db.Width = image->extent.width - 1;
1228 db.LOD = iview->base_mip;
1229 db.Depth = image->array_size - 1; /* FIXME: 3-D */
1230 db.MinimumArrayElement = iview->base_layer;
1231
1232 #if GEN_GEN >= 8
1233 db.SurfaceQPitch =
1234 isl_surf_get_array_pitch_el_rows(&image->depth_surface.isl) >> 2,
1235 #endif
1236 db.RenderTargetViewExtent = 1 - 1;
1237 }
1238 } else {
1239 /* Even when no depth buffer is present, the hardware requires that
1240 * 3DSTATE_DEPTH_BUFFER be programmed correctly. The Broadwell PRM says:
1241 *
1242 * If a null depth buffer is bound, the driver must instead bind depth as:
1243 * 3DSTATE_DEPTH.SurfaceType = SURFTYPE_2D
1244 * 3DSTATE_DEPTH.Width = 1
1245 * 3DSTATE_DEPTH.Height = 1
1246 * 3DSTATE_DEPTH.SuraceFormat = D16_UNORM
1247 * 3DSTATE_DEPTH.SurfaceBaseAddress = 0
1248 * 3DSTATE_DEPTH.HierarchicalDepthBufferEnable = 0
1249 * 3DSTATE_WM_DEPTH_STENCIL.DepthTestEnable = 0
1250 * 3DSTATE_WM_DEPTH_STENCIL.DepthBufferWriteEnable = 0
1251 *
1252 * The PRM is wrong, though. The width and height must be programmed to
1253 * actual framebuffer's width and height, even when neither depth buffer
1254 * nor stencil buffer is present. Also, D16_UNORM is not allowed to
1255 * be combined with a stencil buffer so we use D32_FLOAT instead.
1256 */
1257 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_DEPTH_BUFFER), db) {
1258 db.SurfaceType = SURFTYPE_2D;
1259 db.SurfaceFormat = D32_FLOAT;
1260 db.Width = fb->width - 1;
1261 db.Height = fb->height - 1;
1262 db.StencilWriteEnable = has_stencil;
1263 }
1264 }
1265
1266 /* Emit 3DSTATE_STENCIL_BUFFER */
1267 if (has_stencil) {
1268 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_STENCIL_BUFFER), sb) {
1269 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1270 sb.StencilBufferEnable = true,
1271 #endif
1272 sb.StencilBufferObjectControlState = GENX(MOCS),
1273
1274 sb.SurfacePitch = image->stencil_surface.isl.row_pitch - 1,
1275
1276 #if GEN_GEN >= 8
1277 sb.SurfaceQPitch = isl_surf_get_array_pitch_el_rows(&image->stencil_surface.isl) >> 2,
1278 #endif
1279 sb.SurfaceBaseAddress = (struct anv_address) {
1280 .bo = image->bo,
1281 .offset = image->offset + image->stencil_surface.offset,
1282 };
1283 }
1284 } else {
1285 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_STENCIL_BUFFER), sb);
1286 }
1287
1288 /* Disable hierarchial depth buffers. */
1289 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_HIER_DEPTH_BUFFER), hz);
1290
1291 /* Clear the clear params. */
1292 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CLEAR_PARAMS), cp);
1293 }
1294
1295 /**
1296 * @see anv_cmd_buffer_set_subpass()
1297 */
1298 void
1299 genX(cmd_buffer_set_subpass)(struct anv_cmd_buffer *cmd_buffer,
1300 struct anv_subpass *subpass)
1301 {
1302 cmd_buffer->state.subpass = subpass;
1303
1304 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_FRAGMENT_BIT;
1305
1306 cmd_buffer_emit_depth_stencil(cmd_buffer);
1307 }
1308
1309 void genX(CmdBeginRenderPass)(
1310 VkCommandBuffer commandBuffer,
1311 const VkRenderPassBeginInfo* pRenderPassBegin,
1312 VkSubpassContents contents)
1313 {
1314 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1315 ANV_FROM_HANDLE(anv_render_pass, pass, pRenderPassBegin->renderPass);
1316 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, pRenderPassBegin->framebuffer);
1317
1318 cmd_buffer->state.framebuffer = framebuffer;
1319 cmd_buffer->state.pass = pass;
1320 cmd_buffer->state.render_area = pRenderPassBegin->renderArea;
1321 anv_cmd_state_setup_attachments(cmd_buffer, pRenderPassBegin);
1322
1323 genX(flush_pipeline_select_3d)(cmd_buffer);
1324
1325 genX(cmd_buffer_set_subpass)(cmd_buffer, pass->subpasses);
1326 anv_cmd_buffer_clear_subpass(cmd_buffer);
1327 }
1328
1329 void genX(CmdNextSubpass)(
1330 VkCommandBuffer commandBuffer,
1331 VkSubpassContents contents)
1332 {
1333 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1334
1335 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1336
1337 anv_cmd_buffer_resolve_subpass(cmd_buffer);
1338 genX(cmd_buffer_set_subpass)(cmd_buffer, cmd_buffer->state.subpass + 1);
1339 anv_cmd_buffer_clear_subpass(cmd_buffer);
1340 }
1341
1342 void genX(CmdEndRenderPass)(
1343 VkCommandBuffer commandBuffer)
1344 {
1345 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1346
1347 anv_cmd_buffer_resolve_subpass(cmd_buffer);
1348
1349 #ifndef NDEBUG
1350 anv_dump_add_framebuffer(cmd_buffer, cmd_buffer->state.framebuffer);
1351 #endif
1352 }
1353
1354 static void
1355 emit_ps_depth_count(struct anv_batch *batch,
1356 struct anv_bo *bo, uint32_t offset)
1357 {
1358 anv_batch_emit(batch, GENX(PIPE_CONTROL), pc) {
1359 pc.DestinationAddressType = DAT_PPGTT;
1360 pc.PostSyncOperation = WritePSDepthCount;
1361 pc.DepthStallEnable = true;
1362 pc.Address = (struct anv_address) { bo, offset };
1363 }
1364 }
1365
1366 static void
1367 emit_query_availability(struct anv_batch *batch,
1368 struct anv_bo *bo, uint32_t offset)
1369 {
1370 anv_batch_emit(batch, GENX(PIPE_CONTROL), pc) {
1371 pc.DestinationAddressType = DAT_PPGTT;
1372 pc.PostSyncOperation = WriteImmediateData;
1373 pc.Address = (struct anv_address) { bo, offset };
1374 pc.ImmediateData = 1;
1375 }
1376 }
1377
1378 void genX(CmdBeginQuery)(
1379 VkCommandBuffer commandBuffer,
1380 VkQueryPool queryPool,
1381 uint32_t query,
1382 VkQueryControlFlags flags)
1383 {
1384 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1385 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
1386
1387 /* Workaround: When meta uses the pipeline with the VS disabled, it seems
1388 * that the pipelining of the depth write breaks. What we see is that
1389 * samples from the render pass clear leaks into the first query
1390 * immediately after the clear. Doing a pipecontrol with a post-sync
1391 * operation and DepthStallEnable seems to work around the issue.
1392 */
1393 if (cmd_buffer->state.need_query_wa) {
1394 cmd_buffer->state.need_query_wa = false;
1395 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1396 pc.DepthCacheFlushEnable = true;
1397 pc.DepthStallEnable = true;
1398 }
1399 }
1400
1401 switch (pool->type) {
1402 case VK_QUERY_TYPE_OCCLUSION:
1403 emit_ps_depth_count(&cmd_buffer->batch, &pool->bo,
1404 query * sizeof(struct anv_query_pool_slot));
1405 break;
1406
1407 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
1408 default:
1409 unreachable("");
1410 }
1411 }
1412
1413 void genX(CmdEndQuery)(
1414 VkCommandBuffer commandBuffer,
1415 VkQueryPool queryPool,
1416 uint32_t query)
1417 {
1418 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1419 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
1420
1421 switch (pool->type) {
1422 case VK_QUERY_TYPE_OCCLUSION:
1423 emit_ps_depth_count(&cmd_buffer->batch, &pool->bo,
1424 query * sizeof(struct anv_query_pool_slot) + 8);
1425
1426 emit_query_availability(&cmd_buffer->batch, &pool->bo,
1427 query * sizeof(struct anv_query_pool_slot) + 16);
1428 break;
1429
1430 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
1431 default:
1432 unreachable("");
1433 }
1434 }
1435
1436 #define TIMESTAMP 0x2358
1437
1438 void genX(CmdWriteTimestamp)(
1439 VkCommandBuffer commandBuffer,
1440 VkPipelineStageFlagBits pipelineStage,
1441 VkQueryPool queryPool,
1442 uint32_t query)
1443 {
1444 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1445 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
1446 uint32_t offset = query * sizeof(struct anv_query_pool_slot);
1447
1448 assert(pool->type == VK_QUERY_TYPE_TIMESTAMP);
1449
1450 switch (pipelineStage) {
1451 case VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT:
1452 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
1453 srm.RegisterAddress = TIMESTAMP;
1454 srm.MemoryAddress = (struct anv_address) { &pool->bo, offset };
1455 }
1456 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
1457 srm.RegisterAddress = TIMESTAMP + 4;
1458 srm.MemoryAddress = (struct anv_address) { &pool->bo, offset + 4 };
1459 }
1460 break;
1461
1462 default:
1463 /* Everything else is bottom-of-pipe */
1464 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1465 pc.DestinationAddressType = DAT_PPGTT,
1466 pc.PostSyncOperation = WriteTimestamp,
1467 pc.Address = (struct anv_address) { &pool->bo, offset };
1468 }
1469 break;
1470 }
1471
1472 emit_query_availability(&cmd_buffer->batch, &pool->bo, query + 16);
1473 }
1474
1475 #if GEN_GEN > 7 || GEN_IS_HASWELL
1476
1477 #define alu_opcode(v) __gen_uint((v), 20, 31)
1478 #define alu_operand1(v) __gen_uint((v), 10, 19)
1479 #define alu_operand2(v) __gen_uint((v), 0, 9)
1480 #define alu(opcode, operand1, operand2) \
1481 alu_opcode(opcode) | alu_operand1(operand1) | alu_operand2(operand2)
1482
1483 #define OPCODE_NOOP 0x000
1484 #define OPCODE_LOAD 0x080
1485 #define OPCODE_LOADINV 0x480
1486 #define OPCODE_LOAD0 0x081
1487 #define OPCODE_LOAD1 0x481
1488 #define OPCODE_ADD 0x100
1489 #define OPCODE_SUB 0x101
1490 #define OPCODE_AND 0x102
1491 #define OPCODE_OR 0x103
1492 #define OPCODE_XOR 0x104
1493 #define OPCODE_STORE 0x180
1494 #define OPCODE_STOREINV 0x580
1495
1496 #define OPERAND_R0 0x00
1497 #define OPERAND_R1 0x01
1498 #define OPERAND_R2 0x02
1499 #define OPERAND_R3 0x03
1500 #define OPERAND_R4 0x04
1501 #define OPERAND_SRCA 0x20
1502 #define OPERAND_SRCB 0x21
1503 #define OPERAND_ACCU 0x31
1504 #define OPERAND_ZF 0x32
1505 #define OPERAND_CF 0x33
1506
1507 #define CS_GPR(n) (0x2600 + (n) * 8)
1508
1509 static void
1510 emit_load_alu_reg_u64(struct anv_batch *batch, uint32_t reg,
1511 struct anv_bo *bo, uint32_t offset)
1512 {
1513 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
1514 lrm.RegisterAddress = reg,
1515 lrm.MemoryAddress = (struct anv_address) { bo, offset };
1516 }
1517 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
1518 lrm.RegisterAddress = reg + 4;
1519 lrm.MemoryAddress = (struct anv_address) { bo, offset + 4 };
1520 }
1521 }
1522
1523 static void
1524 store_query_result(struct anv_batch *batch, uint32_t reg,
1525 struct anv_bo *bo, uint32_t offset, VkQueryResultFlags flags)
1526 {
1527 anv_batch_emit(batch, GENX(MI_STORE_REGISTER_MEM), srm) {
1528 srm.RegisterAddress = reg;
1529 srm.MemoryAddress = (struct anv_address) { bo, offset };
1530 }
1531
1532 if (flags & VK_QUERY_RESULT_64_BIT) {
1533 anv_batch_emit(batch, GENX(MI_STORE_REGISTER_MEM), srm) {
1534 srm.RegisterAddress = reg + 4;
1535 srm.MemoryAddress = (struct anv_address) { bo, offset + 4 };
1536 }
1537 }
1538 }
1539
1540 void genX(CmdCopyQueryPoolResults)(
1541 VkCommandBuffer commandBuffer,
1542 VkQueryPool queryPool,
1543 uint32_t firstQuery,
1544 uint32_t queryCount,
1545 VkBuffer destBuffer,
1546 VkDeviceSize destOffset,
1547 VkDeviceSize destStride,
1548 VkQueryResultFlags flags)
1549 {
1550 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1551 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
1552 ANV_FROM_HANDLE(anv_buffer, buffer, destBuffer);
1553 uint32_t slot_offset, dst_offset;
1554
1555 if (flags & VK_QUERY_RESULT_WAIT_BIT) {
1556 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1557 pc.CommandStreamerStallEnable = true;
1558 pc.StallAtPixelScoreboard = true;
1559 }
1560 }
1561
1562 dst_offset = buffer->offset + destOffset;
1563 for (uint32_t i = 0; i < queryCount; i++) {
1564
1565 slot_offset = (firstQuery + i) * sizeof(struct anv_query_pool_slot);
1566 switch (pool->type) {
1567 case VK_QUERY_TYPE_OCCLUSION:
1568 emit_load_alu_reg_u64(&cmd_buffer->batch,
1569 CS_GPR(0), &pool->bo, slot_offset);
1570 emit_load_alu_reg_u64(&cmd_buffer->batch,
1571 CS_GPR(1), &pool->bo, slot_offset + 8);
1572
1573 /* FIXME: We need to clamp the result for 32 bit. */
1574
1575 uint32_t *dw = anv_batch_emitn(&cmd_buffer->batch, 5, GENX(MI_MATH));
1576 dw[1] = alu(OPCODE_LOAD, OPERAND_SRCA, OPERAND_R1);
1577 dw[2] = alu(OPCODE_LOAD, OPERAND_SRCB, OPERAND_R0);
1578 dw[3] = alu(OPCODE_SUB, 0, 0);
1579 dw[4] = alu(OPCODE_STORE, OPERAND_R2, OPERAND_ACCU);
1580 break;
1581
1582 case VK_QUERY_TYPE_TIMESTAMP:
1583 emit_load_alu_reg_u64(&cmd_buffer->batch,
1584 CS_GPR(2), &pool->bo, slot_offset);
1585 break;
1586
1587 default:
1588 unreachable("unhandled query type");
1589 }
1590
1591 store_query_result(&cmd_buffer->batch,
1592 CS_GPR(2), buffer->bo, dst_offset, flags);
1593
1594 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
1595 emit_load_alu_reg_u64(&cmd_buffer->batch, CS_GPR(0),
1596 &pool->bo, slot_offset + 16);
1597 if (flags & VK_QUERY_RESULT_64_BIT)
1598 store_query_result(&cmd_buffer->batch,
1599 CS_GPR(0), buffer->bo, dst_offset + 8, flags);
1600 else
1601 store_query_result(&cmd_buffer->batch,
1602 CS_GPR(0), buffer->bo, dst_offset + 4, flags);
1603 }
1604
1605 dst_offset += destStride;
1606 }
1607 }
1608
1609 #else
1610 void genX(CmdCopyQueryPoolResults)(
1611 VkCommandBuffer commandBuffer,
1612 VkQueryPool queryPool,
1613 uint32_t firstQuery,
1614 uint32_t queryCount,
1615 VkBuffer destBuffer,
1616 VkDeviceSize destOffset,
1617 VkDeviceSize destStride,
1618 VkQueryResultFlags flags)
1619 {
1620 anv_finishme("Queries not yet supported on Ivy Bridge");
1621 }
1622 #endif