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