| 1 |
/* |
| 2 |
* Copyright 2024 Alyssa Rosenzweig |
| 3 |
* Copyright 2024 Valve Corporation |
| 4 |
* Copyright 2022 Collabora Ltd. and Red Hat Inc. |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
#include "compiler/libcl/libcl.h" |
| 8 |
#include "compiler/libcl/libcl_vk.h" |
| 9 |
#include "query.h" |
| 10 |
|
| 11 |
static inline global uint64_t * |
| 12 |
query_report(global uint64_t *results, global uint16_t *oq_index, |
| 13 |
uint reports_per_query, uint query) |
| 14 |
{ |
| 15 |
/* For occlusion queries, results[] points to the device global heap. We |
| 16 |
* need to remap indices according to the query pool's allocation. |
| 17 |
*/ |
| 18 |
uint result_index = oq_index ? oq_index[query] : query; |
| 19 |
|
| 20 |
return results + (result_index * reports_per_query); |
| 21 |
} |
| 22 |
|
| 23 |
KERNEL(32) |
| 24 |
libagx_copy_query(global uint32_t *availability, global uint64_t *results, |
| 25 |
global uint16_t *oq_index, uint64_t dst_addr, |
| 26 |
uint64_t dst_stride, uint32_t first_query, |
| 27 |
VkQueryResultFlagBits flags, uint16_t reports_per_query) |
| 28 |
{ |
| 29 |
uint i = cl_global_id.x; |
| 30 |
uint64_t dst = dst_addr + (((uint64_t)i) * dst_stride); |
| 31 |
uint32_t query = first_query + i; |
| 32 |
|
| 33 |
bool available; |
| 34 |
if (availability) |
| 35 |
available = availability[query]; |
| 36 |
else |
| 37 |
available = (results[query] != LIBAGX_QUERY_UNAVAILABLE); |
| 38 |
|
| 39 |
global uint64_t *report = |
| 40 |
query_report(results, oq_index, reports_per_query, query); |
| 41 |
|
| 42 |
if (available || (flags & VK_QUERY_RESULT_PARTIAL_BIT)) { |
| 43 |
for (unsigned i = 0; i < reports_per_query; ++i) { |
| 44 |
vk_write_query(dst, i, flags, report[i]); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) { |
| 49 |
vk_write_query(dst, reports_per_query, flags, available); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Goes through a series of consecutive query indices in the given pool, |
| 55 |
* setting all element values to 0 and emitting them as available. |
| 56 |
*/ |
| 57 |
KERNEL(32) |
| 58 |
libagx_reset_query(global uint32_t *availability, global uint64_t *results, |
| 59 |
global uint16_t *oq_index, uint32_t first_query, |
| 60 |
uint16_t reports_per_query, int set_available) |
| 61 |
{ |
| 62 |
uint32_t query = first_query + cl_global_id.x; |
| 63 |
|
| 64 |
uint64_t value = 0; |
| 65 |
if (availability) { |
| 66 |
availability[query] = set_available; |
| 67 |
} else { |
| 68 |
value = set_available ? 0 : LIBAGX_QUERY_UNAVAILABLE; |
| 69 |
} |
| 70 |
|
| 71 |
global uint64_t *report = |
| 72 |
query_report(results, oq_index, reports_per_query, query); |
| 73 |
|
| 74 |
/* XXX: is this supposed to happen on the begin? */ |
| 75 |
for (unsigned j = 0; j < reports_per_query; ++j) { |
| 76 |
report[j] = value; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/* TODO: Share with Gallium... */ |
| 81 |
enum pipe_query_value_type { |
| 82 |
PIPE_QUERY_TYPE_I32, |
| 83 |
PIPE_QUERY_TYPE_U32, |
| 84 |
PIPE_QUERY_TYPE_I64, |
| 85 |
PIPE_QUERY_TYPE_U64, |
| 86 |
}; |
| 87 |
|
| 88 |
KERNEL(1) |
| 89 |
libagx_copy_query_gl(global uint64_t *query, global uint64_t *dest, |
| 90 |
ushort value_type, ushort bool_size) |
| 91 |
{ |
| 92 |
uint64_t value = *query; |
| 93 |
|
| 94 |
if (bool_size == 4) { |
| 95 |
value = (uint32_t)value; |
| 96 |
} |
| 97 |
|
| 98 |
if (bool_size) { |
| 99 |
value = value != 0; |
| 100 |
} |
| 101 |
|
| 102 |
if (value_type <= PIPE_QUERY_TYPE_U32) { |
| 103 |
global uint32_t *dest32 = (global uint32_t *)dest; |
| 104 |
bool u32 = (value_type == PIPE_QUERY_TYPE_U32); |
| 105 |
|
| 106 |
*dest32 = u32 ? convert_uint_sat(value) : convert_int_sat((int64_t)value); |
| 107 |
} else { |
| 108 |
*dest = value; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
KERNEL(4) |
| 113 |
libagx_copy_xfb_counters(constant struct libagx_xfb_counter_copy *push) |
| 114 |
{ |
| 115 |
unsigned i = cl_local_id.x; |
| 116 |
|
| 117 |
*(push->dest[i]) = push->src[i] ? *(push->src[i]) : 0; |
| 118 |
} |
| 119 |
|
| 120 |
KERNEL(1) |
| 121 |
libagx_increment_statistic(global uint32_t *statistic, uint32_t delta) |
| 122 |
{ |
| 123 |
*statistic += delta; |
| 124 |
} |
| 125 |
|
| 126 |
KERNEL(1) |
| 127 |
libagx_increment_cs_invocations(global uint *grid, global uint32_t *statistic, |
| 128 |
uint32_t local_size_threads) |
| 129 |
{ |
| 130 |
*statistic += local_size_threads * grid[0] * grid[1] * grid[2]; |
| 131 |
} |
| 132 |
|
| 133 |
KERNEL(32) |
| 134 |
libagx_write_u32s(constant struct libagx_imm_write *p) |
| 135 |
{ |
| 136 |
uint id = cl_global_id.x; |
| 137 |
*(p[id].address) = p[id].value; |
| 138 |
} |
| 139 |
|
| 140 |
/* |
| 141 |
* We set the source as volatile since the caching situation around timestamps |
| 142 |
* is a bit unclear. It might not be necessary but - absent hardware/firmware |
| 143 |
* documentation - this gives me peace of mind. |
| 144 |
*/ |
| 145 |
KERNEL(1) |
| 146 |
libagx_copy_timestamp(global uint64_t *dest, volatile global uint64_t *src) |
| 147 |
{ |
| 148 |
*dest = *src; |
| 149 |
} |
| 150 |
|
| 151 |
KERNEL(1) |
| 152 |
libagx_write_u32(global uint32_t *address, uint32_t value) |
| 153 |
{ |
| 154 |
*address = value; |
| 155 |
} |