anv/cmd_buffer: Move state base address re-emit into ExecuteCommands
[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 /* Render targets live in the same binding table as fragment descriptors */
715 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_RENDER_TARGETS)
716 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_FRAGMENT_BIT;
717
718 /* We emit the binding tables and sampler tables first, then emit push
719 * constants and then finally emit binding table and sampler table
720 * pointers. It has to happen in this order, since emitting the binding
721 * tables may change the push constants (in case of storage images). After
722 * emitting push constants, on SKL+ we have to emit the corresponding
723 * 3DSTATE_BINDING_TABLE_POINTER_* for the push constants to take effect.
724 */
725 uint32_t dirty = 0;
726 if (cmd_buffer->state.descriptors_dirty)
727 dirty = anv_cmd_buffer_flush_descriptor_sets(cmd_buffer);
728
729 if (cmd_buffer->state.push_constants_dirty) {
730 #if GEN_GEN >= 9
731 /* On Sky Lake and later, the binding table pointers commands are
732 * what actually flush the changes to push constant state so we need
733 * to dirty them so they get re-emitted below.
734 */
735 dirty |= cmd_buffer_flush_push_constants(cmd_buffer);
736 #else
737 cmd_buffer_flush_push_constants(cmd_buffer);
738 #endif
739 }
740
741 if (dirty)
742 cmd_buffer_emit_descriptor_pointers(cmd_buffer, dirty);
743
744 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_DYNAMIC_VIEWPORT)
745 gen8_cmd_buffer_emit_viewport(cmd_buffer);
746
747 if (cmd_buffer->state.dirty & (ANV_CMD_DIRTY_DYNAMIC_VIEWPORT |
748 ANV_CMD_DIRTY_PIPELINE)) {
749 gen8_cmd_buffer_emit_depth_viewport(cmd_buffer,
750 pipeline->depth_clamp_enable);
751 }
752
753 if (cmd_buffer->state.dirty & ANV_CMD_DIRTY_DYNAMIC_SCISSOR)
754 gen7_cmd_buffer_emit_scissor(cmd_buffer);
755
756 genX(cmd_buffer_flush_dynamic_state)(cmd_buffer);
757
758 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
759 }
760
761 static void
762 emit_base_vertex_instance_bo(struct anv_cmd_buffer *cmd_buffer,
763 struct anv_bo *bo, uint32_t offset)
764 {
765 uint32_t *p = anv_batch_emitn(&cmd_buffer->batch, 5,
766 GENX(3DSTATE_VERTEX_BUFFERS));
767
768 GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, p + 1,
769 &(struct GENX(VERTEX_BUFFER_STATE)) {
770 .VertexBufferIndex = 32, /* Reserved for this */
771 .AddressModifyEnable = true,
772 .BufferPitch = 0,
773 #if (GEN_GEN >= 8)
774 .MemoryObjectControlState = GENX(MOCS),
775 .BufferStartingAddress = { bo, offset },
776 .BufferSize = 8
777 #else
778 .VertexBufferMemoryObjectControlState = GENX(MOCS),
779 .BufferStartingAddress = { bo, offset },
780 .EndAddress = { bo, offset + 8 },
781 #endif
782 });
783 }
784
785 static void
786 emit_base_vertex_instance(struct anv_cmd_buffer *cmd_buffer,
787 uint32_t base_vertex, uint32_t base_instance)
788 {
789 struct anv_state id_state =
790 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 8, 4);
791
792 ((uint32_t *)id_state.map)[0] = base_vertex;
793 ((uint32_t *)id_state.map)[1] = base_instance;
794
795 if (!cmd_buffer->device->info.has_llc)
796 anv_state_clflush(id_state);
797
798 emit_base_vertex_instance_bo(cmd_buffer,
799 &cmd_buffer->device->dynamic_state_block_pool.bo, id_state.offset);
800 }
801
802 void genX(CmdDraw)(
803 VkCommandBuffer commandBuffer,
804 uint32_t vertexCount,
805 uint32_t instanceCount,
806 uint32_t firstVertex,
807 uint32_t firstInstance)
808 {
809 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
810 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
811 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
812
813 genX(cmd_buffer_flush_state)(cmd_buffer);
814
815 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
816 emit_base_vertex_instance(cmd_buffer, firstVertex, firstInstance);
817
818 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
819 prim.VertexAccessType = SEQUENTIAL;
820 prim.PrimitiveTopologyType = pipeline->topology;
821 prim.VertexCountPerInstance = vertexCount;
822 prim.StartVertexLocation = firstVertex;
823 prim.InstanceCount = instanceCount;
824 prim.StartInstanceLocation = firstInstance;
825 prim.BaseVertexLocation = 0;
826 }
827 }
828
829 void genX(CmdDrawIndexed)(
830 VkCommandBuffer commandBuffer,
831 uint32_t indexCount,
832 uint32_t instanceCount,
833 uint32_t firstIndex,
834 int32_t vertexOffset,
835 uint32_t firstInstance)
836 {
837 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
838 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
839 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
840
841 genX(cmd_buffer_flush_state)(cmd_buffer);
842
843 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
844 emit_base_vertex_instance(cmd_buffer, vertexOffset, firstInstance);
845
846 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
847 prim.VertexAccessType = RANDOM;
848 prim.PrimitiveTopologyType = pipeline->topology;
849 prim.VertexCountPerInstance = indexCount;
850 prim.StartVertexLocation = firstIndex;
851 prim.InstanceCount = instanceCount;
852 prim.StartInstanceLocation = firstInstance;
853 prim.BaseVertexLocation = vertexOffset;
854 }
855 }
856
857 /* Auto-Draw / Indirect Registers */
858 #define GEN7_3DPRIM_END_OFFSET 0x2420
859 #define GEN7_3DPRIM_START_VERTEX 0x2430
860 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
861 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
862 #define GEN7_3DPRIM_START_INSTANCE 0x243C
863 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
864
865 void genX(CmdDrawIndirect)(
866 VkCommandBuffer commandBuffer,
867 VkBuffer _buffer,
868 VkDeviceSize offset,
869 uint32_t drawCount,
870 uint32_t stride)
871 {
872 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
873 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
874 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
875 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
876 struct anv_bo *bo = buffer->bo;
877 uint32_t bo_offset = buffer->offset + offset;
878
879 genX(cmd_buffer_flush_state)(cmd_buffer);
880
881 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
882 emit_base_vertex_instance_bo(cmd_buffer, bo, bo_offset + 8);
883
884 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
885 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
886 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
887 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 12);
888 emit_lri(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, 0);
889
890 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
891 prim.IndirectParameterEnable = true;
892 prim.VertexAccessType = SEQUENTIAL;
893 prim.PrimitiveTopologyType = pipeline->topology;
894 }
895 }
896
897 void genX(CmdDrawIndexedIndirect)(
898 VkCommandBuffer commandBuffer,
899 VkBuffer _buffer,
900 VkDeviceSize offset,
901 uint32_t drawCount,
902 uint32_t stride)
903 {
904 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
905 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
906 struct anv_pipeline *pipeline = cmd_buffer->state.pipeline;
907 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
908 struct anv_bo *bo = buffer->bo;
909 uint32_t bo_offset = buffer->offset + offset;
910
911 genX(cmd_buffer_flush_state)(cmd_buffer);
912
913 /* TODO: We need to stomp base vertex to 0 somehow */
914 if (vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance)
915 emit_base_vertex_instance_bo(cmd_buffer, bo, bo_offset + 12);
916
917 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
918 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
919 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
920 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, bo, bo_offset + 12);
921 emit_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 16);
922
923 anv_batch_emit(&cmd_buffer->batch, GENX(3DPRIMITIVE), prim) {
924 prim.IndirectParameterEnable = true;
925 prim.VertexAccessType = RANDOM;
926 prim.PrimitiveTopologyType = pipeline->topology;
927 }
928 }
929
930 #if GEN_GEN == 7
931
932 static bool
933 verify_cmd_parser(const struct anv_device *device,
934 int required_version,
935 const char *function)
936 {
937 if (device->instance->physicalDevice.cmd_parser_version < required_version) {
938 vk_errorf(VK_ERROR_FEATURE_NOT_PRESENT,
939 "cmd parser version %d is required for %s",
940 required_version, function);
941 return false;
942 } else {
943 return true;
944 }
945 }
946
947 #endif
948
949 void genX(CmdDispatch)(
950 VkCommandBuffer commandBuffer,
951 uint32_t x,
952 uint32_t y,
953 uint32_t z)
954 {
955 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
956 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
957 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
958
959 if (prog_data->uses_num_work_groups) {
960 struct anv_state state =
961 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 12, 4);
962 uint32_t *sizes = state.map;
963 sizes[0] = x;
964 sizes[1] = y;
965 sizes[2] = z;
966 if (!cmd_buffer->device->info.has_llc)
967 anv_state_clflush(state);
968 cmd_buffer->state.num_workgroups_offset = state.offset;
969 cmd_buffer->state.num_workgroups_bo =
970 &cmd_buffer->device->dynamic_state_block_pool.bo;
971 }
972
973 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
974
975 anv_batch_emit(&cmd_buffer->batch, GENX(GPGPU_WALKER), ggw) {
976 ggw.SIMDSize = prog_data->simd_size / 16;
977 ggw.ThreadDepthCounterMaximum = 0;
978 ggw.ThreadHeightCounterMaximum = 0;
979 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
980 ggw.ThreadGroupIDXDimension = x;
981 ggw.ThreadGroupIDYDimension = y;
982 ggw.ThreadGroupIDZDimension = z;
983 ggw.RightExecutionMask = pipeline->cs_right_mask;
984 ggw.BottomExecutionMask = 0xffffffff;
985 }
986
987 anv_batch_emit(&cmd_buffer->batch, GENX(MEDIA_STATE_FLUSH), msf);
988 }
989
990 #define GPGPU_DISPATCHDIMX 0x2500
991 #define GPGPU_DISPATCHDIMY 0x2504
992 #define GPGPU_DISPATCHDIMZ 0x2508
993
994 #define MI_PREDICATE_SRC0 0x2400
995 #define MI_PREDICATE_SRC1 0x2408
996
997 void genX(CmdDispatchIndirect)(
998 VkCommandBuffer commandBuffer,
999 VkBuffer _buffer,
1000 VkDeviceSize offset)
1001 {
1002 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1003 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1004 struct anv_pipeline *pipeline = cmd_buffer->state.compute_pipeline;
1005 const struct brw_cs_prog_data *prog_data = get_cs_prog_data(pipeline);
1006 struct anv_bo *bo = buffer->bo;
1007 uint32_t bo_offset = buffer->offset + offset;
1008 struct anv_batch *batch = &cmd_buffer->batch;
1009
1010 #if GEN_GEN == 7
1011 /* Linux 4.4 added command parser version 5 which allows the GPGPU
1012 * indirect dispatch registers to be written.
1013 */
1014 if (!verify_cmd_parser(cmd_buffer->device, 5, "vkCmdDispatchIndirect"))
1015 return;
1016 #endif
1017
1018 if (prog_data->uses_num_work_groups) {
1019 cmd_buffer->state.num_workgroups_offset = bo_offset;
1020 cmd_buffer->state.num_workgroups_bo = bo;
1021 }
1022
1023 genX(cmd_buffer_flush_compute_state)(cmd_buffer);
1024
1025 emit_lrm(batch, GPGPU_DISPATCHDIMX, bo, bo_offset);
1026 emit_lrm(batch, GPGPU_DISPATCHDIMY, bo, bo_offset + 4);
1027 emit_lrm(batch, GPGPU_DISPATCHDIMZ, bo, bo_offset + 8);
1028
1029 #if GEN_GEN <= 7
1030 /* Clear upper 32-bits of SRC0 and all 64-bits of SRC1 */
1031 emit_lri(batch, MI_PREDICATE_SRC0 + 4, 0);
1032 emit_lri(batch, MI_PREDICATE_SRC1 + 0, 0);
1033 emit_lri(batch, MI_PREDICATE_SRC1 + 4, 0);
1034
1035 /* Load compute_dispatch_indirect_x_size into SRC0 */
1036 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 0);
1037
1038 /* predicate = (compute_dispatch_indirect_x_size == 0); */
1039 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
1040 mip.LoadOperation = LOAD_LOAD;
1041 mip.CombineOperation = COMBINE_SET;
1042 mip.CompareOperation = COMPARE_SRCS_EQUAL;
1043 }
1044
1045 /* Load compute_dispatch_indirect_y_size into SRC0 */
1046 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 4);
1047
1048 /* predicate |= (compute_dispatch_indirect_y_size == 0); */
1049 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
1050 mip.LoadOperation = LOAD_LOAD;
1051 mip.CombineOperation = COMBINE_OR;
1052 mip.CompareOperation = COMPARE_SRCS_EQUAL;
1053 }
1054
1055 /* Load compute_dispatch_indirect_z_size into SRC0 */
1056 emit_lrm(batch, MI_PREDICATE_SRC0, bo, bo_offset + 8);
1057
1058 /* predicate |= (compute_dispatch_indirect_z_size == 0); */
1059 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
1060 mip.LoadOperation = LOAD_LOAD;
1061 mip.CombineOperation = COMBINE_OR;
1062 mip.CompareOperation = COMPARE_SRCS_EQUAL;
1063 }
1064
1065 /* predicate = !predicate; */
1066 #define COMPARE_FALSE 1
1067 anv_batch_emit(batch, GENX(MI_PREDICATE), mip) {
1068 mip.LoadOperation = LOAD_LOADINV;
1069 mip.CombineOperation = COMBINE_OR;
1070 mip.CompareOperation = COMPARE_FALSE;
1071 }
1072 #endif
1073
1074 anv_batch_emit(batch, GENX(GPGPU_WALKER), ggw) {
1075 ggw.IndirectParameterEnable = true;
1076 ggw.PredicateEnable = GEN_GEN <= 7;
1077 ggw.SIMDSize = prog_data->simd_size / 16;
1078 ggw.ThreadDepthCounterMaximum = 0;
1079 ggw.ThreadHeightCounterMaximum = 0;
1080 ggw.ThreadWidthCounterMaximum = prog_data->threads - 1;
1081 ggw.RightExecutionMask = pipeline->cs_right_mask;
1082 ggw.BottomExecutionMask = 0xffffffff;
1083 }
1084
1085 anv_batch_emit(batch, GENX(MEDIA_STATE_FLUSH), msf);
1086 }
1087
1088 static void
1089 flush_pipeline_before_pipeline_select(struct anv_cmd_buffer *cmd_buffer,
1090 uint32_t pipeline)
1091 {
1092 #if GEN_GEN >= 8 && GEN_GEN < 10
1093 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
1094 *
1095 * Software must clear the COLOR_CALC_STATE Valid field in
1096 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
1097 * with Pipeline Select set to GPGPU.
1098 *
1099 * The internal hardware docs recommend the same workaround for Gen9
1100 * hardware too.
1101 */
1102 if (pipeline == GPGPU)
1103 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CC_STATE_POINTERS), t);
1104 #elif GEN_GEN <= 7
1105 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
1106 * PIPELINE_SELECT [DevBWR+]":
1107 *
1108 * Project: DEVSNB+
1109 *
1110 * Software must ensure all the write caches are flushed through a
1111 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
1112 * command to invalidate read only caches prior to programming
1113 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
1114 */
1115 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1116 pc.RenderTargetCacheFlushEnable = true;
1117 pc.DepthCacheFlushEnable = true;
1118 pc.DCFlushEnable = true;
1119 pc.PostSyncOperation = NoWrite;
1120 pc.CommandStreamerStallEnable = true;
1121 }
1122
1123 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1124 pc.TextureCacheInvalidationEnable = true;
1125 pc.ConstantCacheInvalidationEnable = true;
1126 pc.StateCacheInvalidationEnable = true;
1127 pc.InstructionCacheInvalidateEnable = true;
1128 pc.PostSyncOperation = NoWrite;
1129 }
1130 #endif
1131 }
1132
1133 void
1134 genX(flush_pipeline_select_3d)(struct anv_cmd_buffer *cmd_buffer)
1135 {
1136 if (cmd_buffer->state.current_pipeline != _3D) {
1137 flush_pipeline_before_pipeline_select(cmd_buffer, _3D);
1138
1139 anv_batch_emit(&cmd_buffer->batch, GENX(PIPELINE_SELECT), ps) {
1140 #if GEN_GEN >= 9
1141 ps.MaskBits = 3;
1142 #endif
1143 ps.PipelineSelection = _3D;
1144 }
1145
1146 cmd_buffer->state.current_pipeline = _3D;
1147 }
1148 }
1149
1150 void
1151 genX(flush_pipeline_select_gpgpu)(struct anv_cmd_buffer *cmd_buffer)
1152 {
1153 if (cmd_buffer->state.current_pipeline != GPGPU) {
1154 flush_pipeline_before_pipeline_select(cmd_buffer, GPGPU);
1155
1156 anv_batch_emit(&cmd_buffer->batch, GENX(PIPELINE_SELECT), ps) {
1157 #if GEN_GEN >= 9
1158 ps.MaskBits = 3;
1159 #endif
1160 ps.PipelineSelection = GPGPU;
1161 }
1162
1163 cmd_buffer->state.current_pipeline = GPGPU;
1164 }
1165 }
1166
1167 struct anv_state
1168 genX(cmd_buffer_alloc_null_surface_state)(struct anv_cmd_buffer *cmd_buffer,
1169 struct anv_framebuffer *fb)
1170 {
1171 struct anv_state state =
1172 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
1173
1174 struct GENX(RENDER_SURFACE_STATE) null_ss = {
1175 .SurfaceType = SURFTYPE_NULL,
1176 .SurfaceArray = fb->layers > 0,
1177 .SurfaceFormat = ISL_FORMAT_R8G8B8A8_UNORM,
1178 #if GEN_GEN >= 8
1179 .TileMode = YMAJOR,
1180 #else
1181 .TiledSurface = true,
1182 #endif
1183 .Width = fb->width - 1,
1184 .Height = fb->height - 1,
1185 .Depth = fb->layers - 1,
1186 .RenderTargetViewExtent = fb->layers - 1,
1187 };
1188
1189 GENX(RENDER_SURFACE_STATE_pack)(NULL, state.map, &null_ss);
1190
1191 if (!cmd_buffer->device->info.has_llc)
1192 anv_state_clflush(state);
1193
1194 return state;
1195 }
1196
1197 static void
1198 cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer *cmd_buffer)
1199 {
1200 struct anv_device *device = cmd_buffer->device;
1201 const struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
1202 const struct anv_image_view *iview =
1203 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer);
1204 const struct anv_image *image = iview ? iview->image : NULL;
1205 const bool has_depth = image && (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
1206 const bool has_hiz = image != NULL && anv_image_has_hiz(image);
1207 const bool has_stencil =
1208 image && (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
1209
1210 /* FIXME: Implement the PMA stall W/A */
1211 /* FIXME: Width and Height are wrong */
1212
1213 /* Emit 3DSTATE_DEPTH_BUFFER */
1214 if (has_depth) {
1215 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_DEPTH_BUFFER), db) {
1216 db.SurfaceType = SURFTYPE_2D;
1217 db.DepthWriteEnable = true;
1218 db.StencilWriteEnable = has_stencil;
1219
1220 if (cmd_buffer->state.pass->subpass_count == 1) {
1221 db.HierarchicalDepthBufferEnable = has_hiz;
1222 } else {
1223 anv_finishme("Multiple-subpass HiZ not implemented");
1224 }
1225
1226 db.SurfaceFormat = isl_surf_get_depth_format(&device->isl_dev,
1227 &image->depth_surface.isl);
1228
1229 db.SurfaceBaseAddress = (struct anv_address) {
1230 .bo = image->bo,
1231 .offset = image->offset + image->depth_surface.offset,
1232 };
1233 db.DepthBufferObjectControlState = GENX(MOCS);
1234
1235 db.SurfacePitch = image->depth_surface.isl.row_pitch - 1;
1236 db.Height = image->extent.height - 1;
1237 db.Width = image->extent.width - 1;
1238 db.LOD = iview->isl.base_level;
1239 db.Depth = image->array_size - 1; /* FIXME: 3-D */
1240 db.MinimumArrayElement = iview->isl.base_array_layer;
1241
1242 #if GEN_GEN >= 8
1243 db.SurfaceQPitch =
1244 isl_surf_get_array_pitch_el_rows(&image->depth_surface.isl) >> 2;
1245 #endif
1246 db.RenderTargetViewExtent = 1 - 1;
1247 }
1248 } else {
1249 /* Even when no depth buffer is present, the hardware requires that
1250 * 3DSTATE_DEPTH_BUFFER be programmed correctly. The Broadwell PRM says:
1251 *
1252 * If a null depth buffer is bound, the driver must instead bind depth as:
1253 * 3DSTATE_DEPTH.SurfaceType = SURFTYPE_2D
1254 * 3DSTATE_DEPTH.Width = 1
1255 * 3DSTATE_DEPTH.Height = 1
1256 * 3DSTATE_DEPTH.SuraceFormat = D16_UNORM
1257 * 3DSTATE_DEPTH.SurfaceBaseAddress = 0
1258 * 3DSTATE_DEPTH.HierarchicalDepthBufferEnable = 0
1259 * 3DSTATE_WM_DEPTH_STENCIL.DepthTestEnable = 0
1260 * 3DSTATE_WM_DEPTH_STENCIL.DepthBufferWriteEnable = 0
1261 *
1262 * The PRM is wrong, though. The width and height must be programmed to
1263 * actual framebuffer's width and height, even when neither depth buffer
1264 * nor stencil buffer is present. Also, D16_UNORM is not allowed to
1265 * be combined with a stencil buffer so we use D32_FLOAT instead.
1266 */
1267 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_DEPTH_BUFFER), db) {
1268 db.SurfaceType = SURFTYPE_2D;
1269 db.SurfaceFormat = D32_FLOAT;
1270 db.Width = fb->width - 1;
1271 db.Height = fb->height - 1;
1272 db.StencilWriteEnable = has_stencil;
1273 }
1274 }
1275
1276 if (has_hiz) {
1277 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_HIER_DEPTH_BUFFER), hdb) {
1278 hdb.HierarchicalDepthBufferObjectControlState = GENX(MOCS);
1279 hdb.SurfacePitch = image->hiz_surface.isl.row_pitch - 1;
1280 hdb.SurfaceBaseAddress = (struct anv_address) {
1281 .bo = image->bo,
1282 .offset = image->offset + image->hiz_surface.offset,
1283 };
1284 #if GEN_GEN >= 8
1285 /* From the SKL PRM Vol2a:
1286 *
1287 * The interpretation of this field is dependent on Surface Type
1288 * as follows:
1289 * - SURFTYPE_1D: distance in pixels between array slices
1290 * - SURFTYPE_2D/CUBE: distance in rows between array slices
1291 * - SURFTYPE_3D: distance in rows between R - slices
1292 */
1293 hdb.SurfaceQPitch =
1294 image->hiz_surface.isl.dim == ISL_SURF_DIM_1D ?
1295 isl_surf_get_array_pitch_el(&image->hiz_surface.isl) >> 2 :
1296 isl_surf_get_array_pitch_el_rows(&image->hiz_surface.isl) >> 2;
1297 #endif
1298 }
1299 } else {
1300 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_HIER_DEPTH_BUFFER), hdb);
1301 }
1302
1303 /* Emit 3DSTATE_STENCIL_BUFFER */
1304 if (has_stencil) {
1305 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_STENCIL_BUFFER), sb) {
1306 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1307 sb.StencilBufferEnable = true;
1308 #endif
1309 sb.StencilBufferObjectControlState = GENX(MOCS);
1310
1311 sb.SurfacePitch = image->stencil_surface.isl.row_pitch - 1;
1312
1313 #if GEN_GEN >= 8
1314 sb.SurfaceQPitch = isl_surf_get_array_pitch_el_rows(&image->stencil_surface.isl) >> 2;
1315 #endif
1316 sb.SurfaceBaseAddress = (struct anv_address) {
1317 .bo = image->bo,
1318 .offset = image->offset + image->stencil_surface.offset,
1319 };
1320 }
1321 } else {
1322 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_STENCIL_BUFFER), sb);
1323 }
1324
1325 /* From the IVB PRM Vol2P1, 11.5.5.4 3DSTATE_CLEAR_PARAMS:
1326 *
1327 * 3DSTATE_CLEAR_PARAMS must always be programmed in the along with
1328 * the other Depth/Stencil state commands(i.e. 3DSTATE_DEPTH_BUFFER,
1329 * 3DSTATE_STENCIL_BUFFER, or 3DSTATE_HIER_DEPTH_BUFFER)
1330 *
1331 * Testing also shows that some variant of this restriction may exist HSW+.
1332 * On BDW+, it is not possible to emit 2 of these packets consecutively when
1333 * both have DepthClearValueValid set. An analysis of such state programming
1334 * on SKL showed that the GPU doesn't register the latter packet's clear
1335 * value.
1336 */
1337 anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CLEAR_PARAMS), cp) {
1338 if (has_hiz) {
1339 cp.DepthClearValueValid = true;
1340 const uint32_t ds =
1341 cmd_buffer->state.subpass->depth_stencil_attachment;
1342 cp.DepthClearValue =
1343 cmd_buffer->state.attachments[ds].clear_value.depthStencil.depth;
1344 }
1345 }
1346 }
1347
1348 static void
1349 genX(cmd_buffer_set_subpass)(struct anv_cmd_buffer *cmd_buffer,
1350 struct anv_subpass *subpass)
1351 {
1352 cmd_buffer->state.subpass = subpass;
1353
1354 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_RENDER_TARGETS;
1355
1356 cmd_buffer_emit_depth_stencil(cmd_buffer);
1357 genX(cmd_buffer_emit_hz_op)(cmd_buffer, BLORP_HIZ_OP_HIZ_RESOLVE);
1358 genX(cmd_buffer_emit_hz_op)(cmd_buffer, BLORP_HIZ_OP_DEPTH_CLEAR);
1359
1360 anv_cmd_buffer_clear_subpass(cmd_buffer);
1361 }
1362
1363 void genX(CmdBeginRenderPass)(
1364 VkCommandBuffer commandBuffer,
1365 const VkRenderPassBeginInfo* pRenderPassBegin,
1366 VkSubpassContents contents)
1367 {
1368 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1369 ANV_FROM_HANDLE(anv_render_pass, pass, pRenderPassBegin->renderPass);
1370 ANV_FROM_HANDLE(anv_framebuffer, framebuffer, pRenderPassBegin->framebuffer);
1371
1372 cmd_buffer->state.framebuffer = framebuffer;
1373 cmd_buffer->state.pass = pass;
1374 cmd_buffer->state.render_area = pRenderPassBegin->renderArea;
1375 anv_cmd_state_setup_attachments(cmd_buffer, pRenderPassBegin);
1376
1377 genX(flush_pipeline_select_3d)(cmd_buffer);
1378
1379 genX(cmd_buffer_set_subpass)(cmd_buffer, pass->subpasses);
1380 }
1381
1382 void genX(CmdNextSubpass)(
1383 VkCommandBuffer commandBuffer,
1384 VkSubpassContents contents)
1385 {
1386 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1387
1388 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1389
1390 anv_cmd_buffer_resolve_subpass(cmd_buffer);
1391 genX(cmd_buffer_set_subpass)(cmd_buffer, cmd_buffer->state.subpass + 1);
1392 }
1393
1394 void genX(CmdEndRenderPass)(
1395 VkCommandBuffer commandBuffer)
1396 {
1397 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1398
1399 genX(cmd_buffer_emit_hz_op)(cmd_buffer, BLORP_HIZ_OP_DEPTH_RESOLVE);
1400 anv_cmd_buffer_resolve_subpass(cmd_buffer);
1401
1402 #ifndef NDEBUG
1403 anv_dump_add_framebuffer(cmd_buffer, cmd_buffer->state.framebuffer);
1404 #endif
1405 }
1406
1407 static void
1408 emit_ps_depth_count(struct anv_batch *batch,
1409 struct anv_bo *bo, uint32_t offset)
1410 {
1411 anv_batch_emit(batch, GENX(PIPE_CONTROL), pc) {
1412 pc.DestinationAddressType = DAT_PPGTT;
1413 pc.PostSyncOperation = WritePSDepthCount;
1414 pc.DepthStallEnable = true;
1415 pc.Address = (struct anv_address) { bo, offset };
1416 }
1417 }
1418
1419 static void
1420 emit_query_availability(struct anv_batch *batch,
1421 struct anv_bo *bo, uint32_t offset)
1422 {
1423 anv_batch_emit(batch, GENX(PIPE_CONTROL), pc) {
1424 pc.DestinationAddressType = DAT_PPGTT;
1425 pc.PostSyncOperation = WriteImmediateData;
1426 pc.Address = (struct anv_address) { bo, offset };
1427 pc.ImmediateData = 1;
1428 }
1429 }
1430
1431 void genX(CmdBeginQuery)(
1432 VkCommandBuffer commandBuffer,
1433 VkQueryPool queryPool,
1434 uint32_t query,
1435 VkQueryControlFlags flags)
1436 {
1437 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1438 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
1439
1440 /* Workaround: When meta uses the pipeline with the VS disabled, it seems
1441 * that the pipelining of the depth write breaks. What we see is that
1442 * samples from the render pass clear leaks into the first query
1443 * immediately after the clear. Doing a pipecontrol with a post-sync
1444 * operation and DepthStallEnable seems to work around the issue.
1445 */
1446 if (cmd_buffer->state.need_query_wa) {
1447 cmd_buffer->state.need_query_wa = false;
1448 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1449 pc.DepthCacheFlushEnable = true;
1450 pc.DepthStallEnable = true;
1451 }
1452 }
1453
1454 switch (pool->type) {
1455 case VK_QUERY_TYPE_OCCLUSION:
1456 emit_ps_depth_count(&cmd_buffer->batch, &pool->bo,
1457 query * sizeof(struct anv_query_pool_slot));
1458 break;
1459
1460 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
1461 default:
1462 unreachable("");
1463 }
1464 }
1465
1466 void genX(CmdEndQuery)(
1467 VkCommandBuffer commandBuffer,
1468 VkQueryPool queryPool,
1469 uint32_t query)
1470 {
1471 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1472 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
1473
1474 switch (pool->type) {
1475 case VK_QUERY_TYPE_OCCLUSION:
1476 emit_ps_depth_count(&cmd_buffer->batch, &pool->bo,
1477 query * sizeof(struct anv_query_pool_slot) + 8);
1478
1479 emit_query_availability(&cmd_buffer->batch, &pool->bo,
1480 query * sizeof(struct anv_query_pool_slot) + 16);
1481 break;
1482
1483 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
1484 default:
1485 unreachable("");
1486 }
1487 }
1488
1489 #define TIMESTAMP 0x2358
1490
1491 void genX(CmdWriteTimestamp)(
1492 VkCommandBuffer commandBuffer,
1493 VkPipelineStageFlagBits pipelineStage,
1494 VkQueryPool queryPool,
1495 uint32_t query)
1496 {
1497 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1498 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
1499 uint32_t offset = query * sizeof(struct anv_query_pool_slot);
1500
1501 assert(pool->type == VK_QUERY_TYPE_TIMESTAMP);
1502
1503 switch (pipelineStage) {
1504 case VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT:
1505 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
1506 srm.RegisterAddress = TIMESTAMP;
1507 srm.MemoryAddress = (struct anv_address) { &pool->bo, offset };
1508 }
1509 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
1510 srm.RegisterAddress = TIMESTAMP + 4;
1511 srm.MemoryAddress = (struct anv_address) { &pool->bo, offset + 4 };
1512 }
1513 break;
1514
1515 default:
1516 /* Everything else is bottom-of-pipe */
1517 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1518 pc.DestinationAddressType = DAT_PPGTT;
1519 pc.PostSyncOperation = WriteTimestamp;
1520 pc.Address = (struct anv_address) { &pool->bo, offset };
1521 }
1522 break;
1523 }
1524
1525 emit_query_availability(&cmd_buffer->batch, &pool->bo, query + 16);
1526 }
1527
1528 #if GEN_GEN > 7 || GEN_IS_HASWELL
1529
1530 #define alu_opcode(v) __gen_uint((v), 20, 31)
1531 #define alu_operand1(v) __gen_uint((v), 10, 19)
1532 #define alu_operand2(v) __gen_uint((v), 0, 9)
1533 #define alu(opcode, operand1, operand2) \
1534 alu_opcode(opcode) | alu_operand1(operand1) | alu_operand2(operand2)
1535
1536 #define OPCODE_NOOP 0x000
1537 #define OPCODE_LOAD 0x080
1538 #define OPCODE_LOADINV 0x480
1539 #define OPCODE_LOAD0 0x081
1540 #define OPCODE_LOAD1 0x481
1541 #define OPCODE_ADD 0x100
1542 #define OPCODE_SUB 0x101
1543 #define OPCODE_AND 0x102
1544 #define OPCODE_OR 0x103
1545 #define OPCODE_XOR 0x104
1546 #define OPCODE_STORE 0x180
1547 #define OPCODE_STOREINV 0x580
1548
1549 #define OPERAND_R0 0x00
1550 #define OPERAND_R1 0x01
1551 #define OPERAND_R2 0x02
1552 #define OPERAND_R3 0x03
1553 #define OPERAND_R4 0x04
1554 #define OPERAND_SRCA 0x20
1555 #define OPERAND_SRCB 0x21
1556 #define OPERAND_ACCU 0x31
1557 #define OPERAND_ZF 0x32
1558 #define OPERAND_CF 0x33
1559
1560 #define CS_GPR(n) (0x2600 + (n) * 8)
1561
1562 static void
1563 emit_load_alu_reg_u64(struct anv_batch *batch, uint32_t reg,
1564 struct anv_bo *bo, uint32_t offset)
1565 {
1566 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
1567 lrm.RegisterAddress = reg,
1568 lrm.MemoryAddress = (struct anv_address) { bo, offset };
1569 }
1570 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
1571 lrm.RegisterAddress = reg + 4;
1572 lrm.MemoryAddress = (struct anv_address) { bo, offset + 4 };
1573 }
1574 }
1575
1576 static void
1577 store_query_result(struct anv_batch *batch, uint32_t reg,
1578 struct anv_bo *bo, uint32_t offset, VkQueryResultFlags flags)
1579 {
1580 anv_batch_emit(batch, GENX(MI_STORE_REGISTER_MEM), srm) {
1581 srm.RegisterAddress = reg;
1582 srm.MemoryAddress = (struct anv_address) { bo, offset };
1583 }
1584
1585 if (flags & VK_QUERY_RESULT_64_BIT) {
1586 anv_batch_emit(batch, GENX(MI_STORE_REGISTER_MEM), srm) {
1587 srm.RegisterAddress = reg + 4;
1588 srm.MemoryAddress = (struct anv_address) { bo, offset + 4 };
1589 }
1590 }
1591 }
1592
1593 void genX(CmdCopyQueryPoolResults)(
1594 VkCommandBuffer commandBuffer,
1595 VkQueryPool queryPool,
1596 uint32_t firstQuery,
1597 uint32_t queryCount,
1598 VkBuffer destBuffer,
1599 VkDeviceSize destOffset,
1600 VkDeviceSize destStride,
1601 VkQueryResultFlags flags)
1602 {
1603 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1604 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
1605 ANV_FROM_HANDLE(anv_buffer, buffer, destBuffer);
1606 uint32_t slot_offset, dst_offset;
1607
1608 if (flags & VK_QUERY_RESULT_WAIT_BIT) {
1609 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
1610 pc.CommandStreamerStallEnable = true;
1611 pc.StallAtPixelScoreboard = true;
1612 }
1613 }
1614
1615 dst_offset = buffer->offset + destOffset;
1616 for (uint32_t i = 0; i < queryCount; i++) {
1617
1618 slot_offset = (firstQuery + i) * sizeof(struct anv_query_pool_slot);
1619 switch (pool->type) {
1620 case VK_QUERY_TYPE_OCCLUSION:
1621 emit_load_alu_reg_u64(&cmd_buffer->batch,
1622 CS_GPR(0), &pool->bo, slot_offset);
1623 emit_load_alu_reg_u64(&cmd_buffer->batch,
1624 CS_GPR(1), &pool->bo, slot_offset + 8);
1625
1626 /* FIXME: We need to clamp the result for 32 bit. */
1627
1628 uint32_t *dw = anv_batch_emitn(&cmd_buffer->batch, 5, GENX(MI_MATH));
1629 dw[1] = alu(OPCODE_LOAD, OPERAND_SRCA, OPERAND_R1);
1630 dw[2] = alu(OPCODE_LOAD, OPERAND_SRCB, OPERAND_R0);
1631 dw[3] = alu(OPCODE_SUB, 0, 0);
1632 dw[4] = alu(OPCODE_STORE, OPERAND_R2, OPERAND_ACCU);
1633 break;
1634
1635 case VK_QUERY_TYPE_TIMESTAMP:
1636 emit_load_alu_reg_u64(&cmd_buffer->batch,
1637 CS_GPR(2), &pool->bo, slot_offset);
1638 break;
1639
1640 default:
1641 unreachable("unhandled query type");
1642 }
1643
1644 store_query_result(&cmd_buffer->batch,
1645 CS_GPR(2), buffer->bo, dst_offset, flags);
1646
1647 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
1648 emit_load_alu_reg_u64(&cmd_buffer->batch, CS_GPR(0),
1649 &pool->bo, slot_offset + 16);
1650 if (flags & VK_QUERY_RESULT_64_BIT)
1651 store_query_result(&cmd_buffer->batch,
1652 CS_GPR(0), buffer->bo, dst_offset + 8, flags);
1653 else
1654 store_query_result(&cmd_buffer->batch,
1655 CS_GPR(0), buffer->bo, dst_offset + 4, flags);
1656 }
1657
1658 dst_offset += destStride;
1659 }
1660 }
1661
1662 #else
1663 void genX(CmdCopyQueryPoolResults)(
1664 VkCommandBuffer commandBuffer,
1665 VkQueryPool queryPool,
1666 uint32_t firstQuery,
1667 uint32_t queryCount,
1668 VkBuffer destBuffer,
1669 VkDeviceSize destOffset,
1670 VkDeviceSize destStride,
1671 VkQueryResultFlags flags)
1672 {
1673 anv_finishme("Queries not yet supported on Ivy Bridge");
1674 }
1675 #endif