ac/llvm: drop v4f32empty. (v2)
[mesa.git] / src / intel / vulkan / genX_query.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 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 #include "genxml/gen_macros.h"
33 #include "genxml/genX_pack.h"
34
35 VkResult genX(CreateQueryPool)(
36 VkDevice _device,
37 const VkQueryPoolCreateInfo* pCreateInfo,
38 const VkAllocationCallbacks* pAllocator,
39 VkQueryPool* pQueryPool)
40 {
41 ANV_FROM_HANDLE(anv_device, device, _device);
42 const struct anv_physical_device *pdevice = &device->instance->physicalDevice;
43 struct anv_query_pool *pool;
44 VkResult result;
45
46 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO);
47
48 /* Query pool slots are made up of some number of 64-bit values packed
49 * tightly together. The first 64-bit value is always the "available" bit
50 * which is 0 when the query is unavailable and 1 when it is available.
51 * The 64-bit values that follow are determined by the type of query.
52 */
53 uint32_t uint64s_per_slot = 1;
54
55 VkQueryPipelineStatisticFlags pipeline_statistics = 0;
56 switch (pCreateInfo->queryType) {
57 case VK_QUERY_TYPE_OCCLUSION:
58 /* Occlusion queries have two values: begin and end. */
59 uint64s_per_slot += 2;
60 break;
61 case VK_QUERY_TYPE_TIMESTAMP:
62 /* Timestamps just have the one timestamp value */
63 uint64s_per_slot += 1;
64 break;
65 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
66 pipeline_statistics = pCreateInfo->pipelineStatistics;
67 /* We're going to trust this field implicitly so we need to ensure that
68 * no unhandled extension bits leak in.
69 */
70 pipeline_statistics &= ANV_PIPELINE_STATISTICS_MASK;
71
72 /* Statistics queries have a min and max for every statistic */
73 uint64s_per_slot += 2 * _mesa_bitcount(pipeline_statistics);
74 break;
75 default:
76 assert(!"Invalid query type");
77 }
78
79 pool = vk_alloc2(&device->alloc, pAllocator, sizeof(*pool), 8,
80 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
81 if (pool == NULL)
82 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
83
84 pool->type = pCreateInfo->queryType;
85 pool->pipeline_statistics = pipeline_statistics;
86 pool->stride = uint64s_per_slot * sizeof(uint64_t);
87 pool->slots = pCreateInfo->queryCount;
88
89 uint64_t size = pool->slots * pool->stride;
90 result = anv_bo_init_new(&pool->bo, device, size);
91 if (result != VK_SUCCESS)
92 goto fail;
93
94 if (pdevice->supports_48bit_addresses)
95 pool->bo.flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
96
97 if (pdevice->has_exec_async)
98 pool->bo.flags |= EXEC_OBJECT_ASYNC;
99
100 /* For query pools, we set the caching mode to I915_CACHING_CACHED. On LLC
101 * platforms, this does nothing. On non-LLC platforms, this means snooping
102 * which comes at a slight cost. However, the buffers aren't big, won't be
103 * written frequently, and trying to handle the flushing manually without
104 * doing too much flushing is extremely painful.
105 */
106 anv_gem_set_caching(device, pool->bo.gem_handle, I915_CACHING_CACHED);
107
108 pool->bo.map = anv_gem_mmap(device, pool->bo.gem_handle, 0, size, 0);
109
110 *pQueryPool = anv_query_pool_to_handle(pool);
111
112 return VK_SUCCESS;
113
114 fail:
115 vk_free2(&device->alloc, pAllocator, pool);
116
117 return result;
118 }
119
120 void genX(DestroyQueryPool)(
121 VkDevice _device,
122 VkQueryPool _pool,
123 const VkAllocationCallbacks* pAllocator)
124 {
125 ANV_FROM_HANDLE(anv_device, device, _device);
126 ANV_FROM_HANDLE(anv_query_pool, pool, _pool);
127
128 if (!pool)
129 return;
130
131 anv_gem_munmap(pool->bo.map, pool->bo.size);
132 anv_gem_close(device, pool->bo.gem_handle);
133 vk_free2(&device->alloc, pAllocator, pool);
134 }
135
136 static void
137 cpu_write_query_result(void *dst_slot, VkQueryResultFlags flags,
138 uint32_t value_index, uint64_t result)
139 {
140 if (flags & VK_QUERY_RESULT_64_BIT) {
141 uint64_t *dst64 = dst_slot;
142 dst64[value_index] = result;
143 } else {
144 uint32_t *dst32 = dst_slot;
145 dst32[value_index] = result;
146 }
147 }
148
149 static bool
150 query_is_available(uint64_t *slot)
151 {
152 return *(volatile uint64_t *)slot;
153 }
154
155 static VkResult
156 wait_for_available(struct anv_device *device,
157 struct anv_query_pool *pool, uint64_t *slot)
158 {
159 while (true) {
160 if (query_is_available(slot))
161 return VK_SUCCESS;
162
163 int ret = anv_gem_busy(device, pool->bo.gem_handle);
164 if (ret == 1) {
165 /* The BO is still busy, keep waiting. */
166 continue;
167 } else if (ret == -1) {
168 /* We don't know the real error. */
169 device->lost = true;
170 return vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
171 "gem wait failed: %m");
172 } else {
173 assert(ret == 0);
174 /* The BO is no longer busy. */
175 if (query_is_available(slot)) {
176 return VK_SUCCESS;
177 } else {
178 VkResult status = anv_device_query_status(device);
179 if (status != VK_SUCCESS)
180 return status;
181
182 /* If we haven't seen availability yet, then we never will. This
183 * can only happen if we have a client error where they call
184 * GetQueryPoolResults on a query that they haven't submitted to
185 * the GPU yet. The spec allows us to do anything in this case,
186 * but returning VK_SUCCESS doesn't seem right and we shouldn't
187 * just keep spinning.
188 */
189 return VK_NOT_READY;
190 }
191 }
192 }
193 }
194
195 VkResult genX(GetQueryPoolResults)(
196 VkDevice _device,
197 VkQueryPool queryPool,
198 uint32_t firstQuery,
199 uint32_t queryCount,
200 size_t dataSize,
201 void* pData,
202 VkDeviceSize stride,
203 VkQueryResultFlags flags)
204 {
205 ANV_FROM_HANDLE(anv_device, device, _device);
206 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
207
208 assert(pool->type == VK_QUERY_TYPE_OCCLUSION ||
209 pool->type == VK_QUERY_TYPE_PIPELINE_STATISTICS ||
210 pool->type == VK_QUERY_TYPE_TIMESTAMP);
211
212 if (unlikely(device->lost))
213 return VK_ERROR_DEVICE_LOST;
214
215 if (pData == NULL)
216 return VK_SUCCESS;
217
218 void *data_end = pData + dataSize;
219
220 VkResult status = VK_SUCCESS;
221 for (uint32_t i = 0; i < queryCount; i++) {
222 uint64_t *slot = pool->bo.map + (firstQuery + i) * pool->stride;
223
224 /* Availability is always at the start of the slot */
225 bool available = slot[0];
226
227 if (!available && (flags & VK_QUERY_RESULT_WAIT_BIT)) {
228 status = wait_for_available(device, pool, slot);
229 if (status != VK_SUCCESS)
230 return status;
231
232 available = true;
233 }
234
235 /* From the Vulkan 1.0.42 spec:
236 *
237 * "If VK_QUERY_RESULT_WAIT_BIT and VK_QUERY_RESULT_PARTIAL_BIT are
238 * both not set then no result values are written to pData for
239 * queries that are in the unavailable state at the time of the call,
240 * and vkGetQueryPoolResults returns VK_NOT_READY. However,
241 * availability state is still written to pData for those queries if
242 * VK_QUERY_RESULT_WITH_AVAILABILITY_BIT is set."
243 */
244 bool write_results = available || (flags & VK_QUERY_RESULT_PARTIAL_BIT);
245
246 if (write_results) {
247 switch (pool->type) {
248 case VK_QUERY_TYPE_OCCLUSION: {
249 cpu_write_query_result(pData, flags, 0, slot[2] - slot[1]);
250 break;
251 }
252
253 case VK_QUERY_TYPE_PIPELINE_STATISTICS: {
254 uint32_t statistics = pool->pipeline_statistics;
255 uint32_t idx = 0;
256 while (statistics) {
257 uint32_t stat = u_bit_scan(&statistics);
258 uint64_t result = slot[idx * 2 + 2] - slot[idx * 2 + 1];
259
260 /* WaDividePSInvocationCountBy4:HSW,BDW */
261 if ((device->info.gen == 8 || device->info.is_haswell) &&
262 (1 << stat) == VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT)
263 result >>= 2;
264
265 cpu_write_query_result(pData, flags, idx, result);
266
267 idx++;
268 }
269 assert(idx == _mesa_bitcount(pool->pipeline_statistics));
270 break;
271 }
272
273 case VK_QUERY_TYPE_TIMESTAMP: {
274 cpu_write_query_result(pData, flags, 0, slot[1]);
275 break;
276 }
277 default:
278 unreachable("invalid pool type");
279 }
280 } else {
281 status = VK_NOT_READY;
282 }
283
284 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
285 uint32_t idx = (pool->type == VK_QUERY_TYPE_PIPELINE_STATISTICS) ?
286 _mesa_bitcount(pool->pipeline_statistics) : 1;
287 cpu_write_query_result(pData, flags, idx, available);
288 }
289
290 pData += stride;
291 if (pData >= data_end)
292 break;
293 }
294
295 return status;
296 }
297
298 static void
299 emit_ps_depth_count(struct anv_cmd_buffer *cmd_buffer,
300 struct anv_bo *bo, uint32_t offset)
301 {
302 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
303 pc.DestinationAddressType = DAT_PPGTT;
304 pc.PostSyncOperation = WritePSDepthCount;
305 pc.DepthStallEnable = true;
306 pc.Address = (struct anv_address) { bo, offset };
307
308 if (GEN_GEN == 9 && cmd_buffer->device->info.gt == 4)
309 pc.CommandStreamerStallEnable = true;
310 }
311 }
312
313 static void
314 emit_query_availability(struct anv_cmd_buffer *cmd_buffer,
315 struct anv_bo *bo, uint32_t offset)
316 {
317 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
318 pc.DestinationAddressType = DAT_PPGTT;
319 pc.PostSyncOperation = WriteImmediateData;
320 pc.Address = (struct anv_address) { bo, offset };
321 pc.ImmediateData = 1;
322 }
323 }
324
325 void genX(CmdResetQueryPool)(
326 VkCommandBuffer commandBuffer,
327 VkQueryPool queryPool,
328 uint32_t firstQuery,
329 uint32_t queryCount)
330 {
331 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
332 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
333
334 for (uint32_t i = 0; i < queryCount; i++) {
335 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdm) {
336 sdm.Address = (struct anv_address) {
337 .bo = &pool->bo,
338 .offset = (firstQuery + i) * pool->stride,
339 };
340 sdm.ImmediateData = 0;
341 }
342 }
343 }
344
345 static const uint32_t vk_pipeline_stat_to_reg[] = {
346 GENX(IA_VERTICES_COUNT_num),
347 GENX(IA_PRIMITIVES_COUNT_num),
348 GENX(VS_INVOCATION_COUNT_num),
349 GENX(GS_INVOCATION_COUNT_num),
350 GENX(GS_PRIMITIVES_COUNT_num),
351 GENX(CL_INVOCATION_COUNT_num),
352 GENX(CL_PRIMITIVES_COUNT_num),
353 GENX(PS_INVOCATION_COUNT_num),
354 GENX(HS_INVOCATION_COUNT_num),
355 GENX(DS_INVOCATION_COUNT_num),
356 GENX(CS_INVOCATION_COUNT_num),
357 };
358
359 static void
360 emit_pipeline_stat(struct anv_cmd_buffer *cmd_buffer, uint32_t stat,
361 struct anv_bo *bo, uint32_t offset)
362 {
363 STATIC_ASSERT(ANV_PIPELINE_STATISTICS_MASK ==
364 (1 << ARRAY_SIZE(vk_pipeline_stat_to_reg)) - 1);
365
366 assert(stat < ARRAY_SIZE(vk_pipeline_stat_to_reg));
367 uint32_t reg = vk_pipeline_stat_to_reg[stat];
368
369 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), lrm) {
370 lrm.RegisterAddress = reg,
371 lrm.MemoryAddress = (struct anv_address) { bo, offset };
372 }
373 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), lrm) {
374 lrm.RegisterAddress = reg + 4,
375 lrm.MemoryAddress = (struct anv_address) { bo, offset + 4 };
376 }
377 }
378
379 void genX(CmdBeginQuery)(
380 VkCommandBuffer commandBuffer,
381 VkQueryPool queryPool,
382 uint32_t query,
383 VkQueryControlFlags flags)
384 {
385 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
386 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
387
388 /* Workaround: When meta uses the pipeline with the VS disabled, it seems
389 * that the pipelining of the depth write breaks. What we see is that
390 * samples from the render pass clear leaks into the first query
391 * immediately after the clear. Doing a pipecontrol with a post-sync
392 * operation and DepthStallEnable seems to work around the issue.
393 */
394 if (cmd_buffer->state.need_query_wa) {
395 cmd_buffer->state.need_query_wa = false;
396 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
397 pc.DepthCacheFlushEnable = true;
398 pc.DepthStallEnable = true;
399 }
400 }
401
402 switch (pool->type) {
403 case VK_QUERY_TYPE_OCCLUSION:
404 emit_ps_depth_count(cmd_buffer, &pool->bo, query * pool->stride + 8);
405 break;
406
407 case VK_QUERY_TYPE_PIPELINE_STATISTICS: {
408 /* TODO: This might only be necessary for certain stats */
409 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
410 pc.CommandStreamerStallEnable = true;
411 pc.StallAtPixelScoreboard = true;
412 }
413
414 uint32_t statistics = pool->pipeline_statistics;
415 uint32_t offset = query * pool->stride + 8;
416 while (statistics) {
417 uint32_t stat = u_bit_scan(&statistics);
418 emit_pipeline_stat(cmd_buffer, stat, &pool->bo, offset);
419 offset += 16;
420 }
421 break;
422 }
423
424 default:
425 unreachable("");
426 }
427 }
428
429 void genX(CmdEndQuery)(
430 VkCommandBuffer commandBuffer,
431 VkQueryPool queryPool,
432 uint32_t query)
433 {
434 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
435 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
436
437 switch (pool->type) {
438 case VK_QUERY_TYPE_OCCLUSION:
439 emit_ps_depth_count(cmd_buffer, &pool->bo, query * pool->stride + 16);
440 emit_query_availability(cmd_buffer, &pool->bo, query * pool->stride);
441 break;
442
443 case VK_QUERY_TYPE_PIPELINE_STATISTICS: {
444 /* TODO: This might only be necessary for certain stats */
445 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
446 pc.CommandStreamerStallEnable = true;
447 pc.StallAtPixelScoreboard = true;
448 }
449
450 uint32_t statistics = pool->pipeline_statistics;
451 uint32_t offset = query * pool->stride + 16;
452 while (statistics) {
453 uint32_t stat = u_bit_scan(&statistics);
454 emit_pipeline_stat(cmd_buffer, stat, &pool->bo, offset);
455 offset += 16;
456 }
457
458 emit_query_availability(cmd_buffer, &pool->bo, query * pool->stride);
459 break;
460 }
461
462 default:
463 unreachable("");
464 }
465 }
466
467 #define TIMESTAMP 0x2358
468
469 void genX(CmdWriteTimestamp)(
470 VkCommandBuffer commandBuffer,
471 VkPipelineStageFlagBits pipelineStage,
472 VkQueryPool queryPool,
473 uint32_t query)
474 {
475 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
476 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
477 uint32_t offset = query * pool->stride;
478
479 assert(pool->type == VK_QUERY_TYPE_TIMESTAMP);
480
481 switch (pipelineStage) {
482 case VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT:
483 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
484 srm.RegisterAddress = TIMESTAMP;
485 srm.MemoryAddress = (struct anv_address) { &pool->bo, offset + 8 };
486 }
487 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
488 srm.RegisterAddress = TIMESTAMP + 4;
489 srm.MemoryAddress = (struct anv_address) { &pool->bo, offset + 12 };
490 }
491 break;
492
493 default:
494 /* Everything else is bottom-of-pipe */
495 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
496 pc.DestinationAddressType = DAT_PPGTT;
497 pc.PostSyncOperation = WriteTimestamp;
498 pc.Address = (struct anv_address) { &pool->bo, offset + 8 };
499
500 if (GEN_GEN == 9 && cmd_buffer->device->info.gt == 4)
501 pc.CommandStreamerStallEnable = true;
502 }
503 break;
504 }
505
506 emit_query_availability(cmd_buffer, &pool->bo, offset);
507 }
508
509 #if GEN_GEN > 7 || GEN_IS_HASWELL
510
511 static uint32_t
512 mi_alu(uint32_t opcode, uint32_t operand1, uint32_t operand2)
513 {
514 struct GENX(MI_MATH_ALU_INSTRUCTION) instr = {
515 .ALUOpcode = opcode,
516 .Operand1 = operand1,
517 .Operand2 = operand2,
518 };
519
520 uint32_t dw;
521 GENX(MI_MATH_ALU_INSTRUCTION_pack)(NULL, &dw, &instr);
522
523 return dw;
524 }
525
526 #define CS_GPR(n) (0x2600 + (n) * 8)
527
528 static void
529 emit_load_alu_reg_u64(struct anv_batch *batch, uint32_t reg,
530 struct anv_bo *bo, uint32_t offset)
531 {
532 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
533 lrm.RegisterAddress = reg,
534 lrm.MemoryAddress = (struct anv_address) { bo, offset };
535 }
536 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
537 lrm.RegisterAddress = reg + 4;
538 lrm.MemoryAddress = (struct anv_address) { bo, offset + 4 };
539 }
540 }
541
542 static void
543 emit_load_alu_reg_imm32(struct anv_batch *batch, uint32_t reg, uint32_t imm)
544 {
545 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
546 lri.RegisterOffset = reg;
547 lri.DataDWord = imm;
548 }
549 }
550
551 static void
552 emit_load_alu_reg_imm64(struct anv_batch *batch, uint32_t reg, uint64_t imm)
553 {
554 emit_load_alu_reg_imm32(batch, reg, (uint32_t)imm);
555 emit_load_alu_reg_imm32(batch, reg + 4, (uint32_t)(imm >> 32));
556 }
557
558 static void
559 emit_load_alu_reg_reg32(struct anv_batch *batch, uint32_t src, uint32_t dst)
560 {
561 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_REG), lrr) {
562 lrr.SourceRegisterAddress = src;
563 lrr.DestinationRegisterAddress = dst;
564 }
565 }
566
567 /*
568 * GPR0 = GPR0 & ((1ull << n) - 1);
569 */
570 static void
571 keep_gpr0_lower_n_bits(struct anv_batch *batch, uint32_t n)
572 {
573 assert(n < 64);
574 emit_load_alu_reg_imm64(batch, CS_GPR(1), (1ull << n) - 1);
575
576 uint32_t *dw = anv_batch_emitn(batch, 5, GENX(MI_MATH));
577 if (!dw) {
578 anv_batch_set_error(batch, VK_ERROR_OUT_OF_HOST_MEMORY);
579 return;
580 }
581
582 dw[1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, MI_ALU_REG0);
583 dw[2] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, MI_ALU_REG1);
584 dw[3] = mi_alu(MI_ALU_AND, 0, 0);
585 dw[4] = mi_alu(MI_ALU_STORE, MI_ALU_REG0, MI_ALU_ACCU);
586 }
587
588 /*
589 * GPR0 = GPR0 << 30;
590 */
591 static void
592 shl_gpr0_by_30_bits(struct anv_batch *batch)
593 {
594 /* First we mask 34 bits of GPR0 to prevent overflow */
595 keep_gpr0_lower_n_bits(batch, 34);
596
597 const uint32_t outer_count = 5;
598 const uint32_t inner_count = 6;
599 STATIC_ASSERT(outer_count * inner_count == 30);
600 const uint32_t cmd_len = 1 + inner_count * 4;
601
602 /* We'll emit 5 commands, each shifting GPR0 left by 6 bits, for a total of
603 * 30 left shifts.
604 */
605 for (int o = 0; o < outer_count; o++) {
606 /* Submit one MI_MATH to shift left by 6 bits */
607 uint32_t *dw = anv_batch_emitn(batch, cmd_len, GENX(MI_MATH));
608 if (!dw) {
609 anv_batch_set_error(batch, VK_ERROR_OUT_OF_HOST_MEMORY);
610 return;
611 }
612
613 dw++;
614 for (int i = 0; i < inner_count; i++, dw += 4) {
615 dw[0] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, MI_ALU_REG0);
616 dw[1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, MI_ALU_REG0);
617 dw[2] = mi_alu(MI_ALU_ADD, 0, 0);
618 dw[3] = mi_alu(MI_ALU_STORE, MI_ALU_REG0, MI_ALU_ACCU);
619 }
620 }
621 }
622
623 /*
624 * GPR0 = GPR0 >> 2;
625 *
626 * Note that the upper 30 bits of GPR are lost!
627 */
628 static void
629 shr_gpr0_by_2_bits(struct anv_batch *batch)
630 {
631 shl_gpr0_by_30_bits(batch);
632 emit_load_alu_reg_reg32(batch, CS_GPR(0) + 4, CS_GPR(0));
633 emit_load_alu_reg_imm32(batch, CS_GPR(0) + 4, 0);
634 }
635
636 static void
637 gpu_write_query_result(struct anv_batch *batch,
638 struct anv_buffer *dst_buffer, uint32_t dst_offset,
639 VkQueryResultFlags flags,
640 uint32_t value_index, uint32_t reg)
641 {
642 if (flags & VK_QUERY_RESULT_64_BIT)
643 dst_offset += value_index * 8;
644 else
645 dst_offset += value_index * 4;
646
647 anv_batch_emit(batch, GENX(MI_STORE_REGISTER_MEM), srm) {
648 srm.RegisterAddress = reg;
649 srm.MemoryAddress = (struct anv_address) {
650 .bo = dst_buffer->bo,
651 .offset = dst_buffer->offset + dst_offset,
652 };
653 }
654
655 if (flags & VK_QUERY_RESULT_64_BIT) {
656 anv_batch_emit(batch, GENX(MI_STORE_REGISTER_MEM), srm) {
657 srm.RegisterAddress = reg + 4;
658 srm.MemoryAddress = (struct anv_address) {
659 .bo = dst_buffer->bo,
660 .offset = dst_buffer->offset + dst_offset + 4,
661 };
662 }
663 }
664 }
665
666 static void
667 compute_query_result(struct anv_batch *batch, uint32_t dst_reg,
668 struct anv_bo *bo, uint32_t offset)
669 {
670 emit_load_alu_reg_u64(batch, CS_GPR(0), bo, offset);
671 emit_load_alu_reg_u64(batch, CS_GPR(1), bo, offset + 8);
672
673 /* FIXME: We need to clamp the result for 32 bit. */
674
675 uint32_t *dw = anv_batch_emitn(batch, 5, GENX(MI_MATH));
676 if (!dw) {
677 anv_batch_set_error(batch, VK_ERROR_OUT_OF_HOST_MEMORY);
678 return;
679 }
680
681 dw[1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, MI_ALU_REG1);
682 dw[2] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, MI_ALU_REG0);
683 dw[3] = mi_alu(MI_ALU_SUB, 0, 0);
684 dw[4] = mi_alu(MI_ALU_STORE, dst_reg, MI_ALU_ACCU);
685 }
686
687 void genX(CmdCopyQueryPoolResults)(
688 VkCommandBuffer commandBuffer,
689 VkQueryPool queryPool,
690 uint32_t firstQuery,
691 uint32_t queryCount,
692 VkBuffer destBuffer,
693 VkDeviceSize destOffset,
694 VkDeviceSize destStride,
695 VkQueryResultFlags flags)
696 {
697 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
698 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
699 ANV_FROM_HANDLE(anv_buffer, buffer, destBuffer);
700 uint32_t slot_offset;
701
702 if (flags & VK_QUERY_RESULT_WAIT_BIT) {
703 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
704 pc.CommandStreamerStallEnable = true;
705 pc.StallAtPixelScoreboard = true;
706 }
707 }
708
709 for (uint32_t i = 0; i < queryCount; i++) {
710 slot_offset = (firstQuery + i) * pool->stride;
711 switch (pool->type) {
712 case VK_QUERY_TYPE_OCCLUSION:
713 compute_query_result(&cmd_buffer->batch, MI_ALU_REG2,
714 &pool->bo, slot_offset + 8);
715 gpu_write_query_result(&cmd_buffer->batch, buffer, destOffset,
716 flags, 0, CS_GPR(2));
717 break;
718
719 case VK_QUERY_TYPE_PIPELINE_STATISTICS: {
720 uint32_t statistics = pool->pipeline_statistics;
721 uint32_t idx = 0;
722 while (statistics) {
723 uint32_t stat = u_bit_scan(&statistics);
724
725 compute_query_result(&cmd_buffer->batch, MI_ALU_REG0,
726 &pool->bo, slot_offset + idx * 16 + 8);
727
728 /* WaDividePSInvocationCountBy4:HSW,BDW */
729 if ((cmd_buffer->device->info.gen == 8 ||
730 cmd_buffer->device->info.is_haswell) &&
731 (1 << stat) == VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT) {
732 shr_gpr0_by_2_bits(&cmd_buffer->batch);
733 }
734
735 gpu_write_query_result(&cmd_buffer->batch, buffer, destOffset,
736 flags, idx, CS_GPR(0));
737
738 idx++;
739 }
740 assert(idx == _mesa_bitcount(pool->pipeline_statistics));
741 break;
742 }
743
744 case VK_QUERY_TYPE_TIMESTAMP:
745 emit_load_alu_reg_u64(&cmd_buffer->batch,
746 CS_GPR(2), &pool->bo, slot_offset + 8);
747 gpu_write_query_result(&cmd_buffer->batch, buffer, destOffset,
748 flags, 0, CS_GPR(2));
749 break;
750
751 default:
752 unreachable("unhandled query type");
753 }
754
755 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
756 uint32_t idx = (pool->type == VK_QUERY_TYPE_PIPELINE_STATISTICS) ?
757 _mesa_bitcount(pool->pipeline_statistics) : 1;
758
759 emit_load_alu_reg_u64(&cmd_buffer->batch, CS_GPR(0),
760 &pool->bo, slot_offset);
761 gpu_write_query_result(&cmd_buffer->batch, buffer, destOffset,
762 flags, idx, CS_GPR(0));
763 }
764
765 destOffset += destStride;
766 }
767 }
768
769 #else
770 void genX(CmdCopyQueryPoolResults)(
771 VkCommandBuffer commandBuffer,
772 VkQueryPool queryPool,
773 uint32_t firstQuery,
774 uint32_t queryCount,
775 VkBuffer destBuffer,
776 VkDeviceSize destOffset,
777 VkDeviceSize destStride,
778 VkQueryResultFlags flags)
779 {
780 anv_finishme("Queries not yet supported on Ivy Bridge");
781 }
782 #endif