Debian Codegraph @

src/imagination/vulkan/pvr_transfer_frag_store.c

raw
1 /*
2 * Copyright © 2023 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * 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 THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stddef.h>
26 #include <stdint.h>
27 #include <vulkan/vulkan_core.h>
28
29 #include "hwdef/rogue_hw_utils.h"
30 #include "pvr_bo.h"
31 #include "pvr_common.h"
32 #include "pvr_device.h"
33 #include "pvr_device_info.h"
34 #include "pvr_pds.h"
35 #include "pvr_physical_device.h"
36 #include "pvr_transfer_frag_store.h"
37 #include "pvr_types.h"
38 #include "pvr_usc.h"
39 #include "util/hash_table.h"
40 #include "util/macros.h"
41 #include "util/ralloc.h"
42 #include "util/u_dynarray.h"
43 #include "util/u_math.h"
44 #include "vk_log.h"
45
46 #define PVR_TRANSFER_BYTE_UNWIND_MAX 16U
47
48 struct pvr_transfer_frag_store_entry_data {
49 pvr_dev_addr_t kick_usc_pds_offset;
50 struct pvr_bo *kick_usc_pds_upload;
51
52 struct pvr_suballoc_bo *usc_upload;
53 struct pvr_tq_frag_sh_reg_layout sh_reg_layout;
54 };
55
56 #define to_pvr_entry_data(_entry) \
57 _Generic((_entry), \
58 struct hash_entry *: (struct pvr_transfer_frag_store_entry_data \
59 *)((_entry)->data), \
60 const struct hash_entry *: ( \
61 const struct pvr_transfer_frag_store_entry_data *)((_entry) \
62 ->data))
63
64 VkResult pvr_transfer_frag_store_init(struct pvr_device *device,
65 struct pvr_transfer_frag_store *store)
66 {
67 const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
68
69 *store = (struct pvr_transfer_frag_store){
70 .max_multisample = PVR_GET_FEATURE_VALUE(dev_info, max_multisample, 1U),
71 .hash_table = _mesa_hash_table_create_u32_keys(NULL),
72 };
73
74 if (!store->hash_table)
75 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
76
77 return VK_SUCCESS;
78 }
79
80 /**
81 * \brief Returns a key based on shader properties.
82 *
83 * Returns a unique key that can be used to uniquely identify a transfer
84 * fragment shader based on the provided shader properties.
85 *
86 * Make sure that the non valid parts of shader_props are memset to 0. Otherwise
87 * these bits might appear in the key as uninitialized data and might not
88 * match a key for the same shader.
89 */
90 static uint32_t pvr_transfer_frag_shader_key(
91 uint32_t max_multisample,
92 const struct pvr_tq_shader_properties *shader_props)
93 {
94 const struct pvr_tq_layer_properties *layer = &shader_props->layer_props;
95 uint32_t resolve_op_num = max_multisample + PVR_RESOLVE_SAMPLE0;
96
97 uint32_t num_layers_bits = util_logbase2_ceil(PVR_TRANSFER_MAX_LAYERS + 1U);
98 uint32_t layer_float_bits = util_logbase2_ceil(PVR_INT_COORD_SET_FLOATS_NUM);
99 uint32_t pixel_src_bits = util_logbase2_ceil(PVR_TRANSFER_PBE_PIXEL_SRC_NUM);
100 uint32_t byte_unwind_bits = util_logbase2_ceil(PVR_TRANSFER_BYTE_UNWIND_MAX);
101 uint32_t resolve_op_bits = util_logbase2_ceil(resolve_op_num);
102 uint32_t sample_cnt_bits = util_last_bit(util_logbase2(max_multisample));
103 uint32_t hash = 0U;
104
105 #if MESA_DEBUG
106 uint32_t max_shift = 0U;
107 # define shift_hash(hash, num) \
108 do { \
109 max_shift += (num); \
110 assert(max_shift <= 32U); \
111 \
112 (hash) <<= (num); \
113 } while (0U)
114 #else
115 # define shift_hash(hash, num) hash <<= (num)
116 #endif
117
118 /* Hash layer info. */
119
120 shift_hash(hash, layer_float_bits);
121 hash |= (uint32_t)shader_props->layer_props.layer_floats;
122
123 shift_hash(hash, 1U);
124 hash |= layer->sample;
125
126 shift_hash(hash, 1U);
127 hash |= (uint32_t)false;
128
129 shift_hash(hash, 1U);
130 hash |= (uint32_t)false;
131
132 shift_hash(hash, pixel_src_bits);
133 hash |= (uint32_t)layer->pbe_format;
134
135 shift_hash(hash, resolve_op_bits);
136 hash |= (uint32_t)layer->resolve_op;
137
138 assert(util_is_power_of_two_nonzero(layer->sample_count));
139 shift_hash(hash, sample_cnt_bits);
140 hash |= (uint32_t)util_logbase2(layer->sample_count);
141
142 shift_hash(hash, 1U);
143 hash |= (uint32_t)layer->msaa;
144
145 shift_hash(hash, byte_unwind_bits);
146 hash |= layer->byte_unwind;
147
148 shift_hash(hash, 1U);
149 hash |= (uint32_t)layer->linear;
150
151 /* End layer info. */
152
153 shift_hash(hash, 1U);
154 hash |= (uint32_t)shader_props->full_rate;
155
156 shift_hash(hash, 1U);
157 hash |= (uint32_t)shader_props->iterated;
158
159 shift_hash(hash, 1U);
160 hash |= (uint32_t)shader_props->pick_component;
161
162 shift_hash(hash, num_layers_bits);
163 /* Just 1 layer. */
164 hash |= 1;
165
166 shift_hash(hash, 3U);
167 /* alpha type none */
168 hash |= 0;
169
170 #undef shift_hash
171
172 return hash;
173 }
174
175 #define to_hash_table_key(_key) ((void *)(uintptr_t)(_key))
176
177 static VkResult pvr_transfer_frag_store_entry_data_compile(
178 struct pvr_device *device,
179 struct pvr_transfer_frag_store_entry_data *const entry_data,
180 const struct pvr_tq_shader_properties *shader_props,
181 uint32_t *const num_usc_temps_out)
182 {
183 const uint32_t image_desc_offset =
184 offsetof(struct pvr_combined_image_sampler_descriptor, image) / 4;
185 const uint32_t sampler_desc_offset =
186 offsetof(struct pvr_combined_image_sampler_descriptor, sampler) / 4;
187
188 const uint32_t cache_line_size =
189 pvr_get_slc_cache_line_size(&device->pdevice->dev_info);
190
191 struct pvr_tq_frag_sh_reg_layout *sh_reg_layout = &entry_data->sh_reg_layout;
192 uint32_t next_free_sh_reg = 0;
193 VkResult result;
194
195 /* TODO: Allocate all combined image samplers if needed? Otherwise change the
196 * array to a single descriptor.
197 */
198 sh_reg_layout->combined_image_samplers.offsets[0].image =
199 next_free_sh_reg + image_desc_offset;
200 sh_reg_layout->combined_image_samplers.offsets[0].sampler =
201 next_free_sh_reg + sampler_desc_offset;
202 sh_reg_layout->combined_image_samplers.count = 1;
203 next_free_sh_reg += sizeof(struct pvr_combined_image_sampler_descriptor) / 4;
204
205 /* TODO: Handle dynamic_const_regs used for PVR_INT_COORD_SET_FLOATS_{4,6}, Z
206 * position, texel unwind, etc. when compiler adds support for them.
207 */
208 sh_reg_layout->dynamic_consts.offset = next_free_sh_reg;
209 sh_reg_layout->dynamic_consts.count = 0;
210
211 sh_reg_layout->driver_total = next_free_sh_reg;
212
213 pco_shader *tq =
214 pvr_uscgen_tq(device->pdevice->pco_ctx, shader_props, sh_reg_layout);
215
216 *num_usc_temps_out = pco_shader_data(tq)->common.temps;
217
218 result = pvr_gpu_upload_usc(device,
219 pco_shader_binary_data(tq),
220 pco_shader_binary_size(tq),
221 cache_line_size,
222 &entry_data->usc_upload);
223
224 ralloc_free(tq);
225
226 if (result != VK_SUCCESS)
227 return result;
228
229 return VK_SUCCESS;
230 }
231
232 static uint32_t get_doutu_sample_rate(const struct pvr_device_info *dev_info,
233 bool full_rate);
234
235 static VkResult pvr_transfer_frag_store_entry_data_create(
236 struct pvr_device *device,
237 struct pvr_transfer_frag_store *store,
238 const struct pvr_tq_shader_properties *shader_props,
239 const struct pvr_transfer_frag_store_entry_data **const entry_data_out)
240 {
241 struct pvr_pds_kickusc_program kick_usc_pds_prog = { 0 };
242 struct pvr_transfer_frag_store_entry_data *entry_data;
243 pvr_dev_addr_t dev_addr;
244 uint32_t num_usc_temps;
245 VkResult result;
246
247 entry_data = ralloc(store->hash_table, __typeof__(*entry_data));
248 if (!entry_data)
249 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
250
251 result = pvr_transfer_frag_store_entry_data_compile(device,
252 entry_data,
253 shader_props,
254 &num_usc_temps);
255 if (result != VK_SUCCESS)
256 goto err_free_entry;
257
258 dev_addr = entry_data->usc_upload->dev_addr;
259 dev_addr.addr -= device->heaps.usc_heap->base_addr.addr;
260
261 pvr_pds_setup_doutu(&kick_usc_pds_prog.usc_task_control,
262 dev_addr.addr,
263 num_usc_temps,
264 get_doutu_sample_rate(&device->pdevice->dev_info,
265 shader_props->full_rate),
266 false);
267
268 pvr_pds_kick_usc(&kick_usc_pds_prog, NULL, 0U, false, PDS_GENERATE_SIZES);
269
270 result = pvr_bo_alloc(device,
271 device->heaps.pds_heap,
272 PVR_DW_TO_BYTES(kick_usc_pds_prog.data_size +
273 kick_usc_pds_prog.code_size),
274 16,
275 PVR_BO_ALLOC_FLAG_CPU_MAPPED,
276 &entry_data->kick_usc_pds_upload);
277 if (result != VK_SUCCESS)
278 goto err_free_usc_upload;
279
280 pvr_pds_kick_usc(&kick_usc_pds_prog,
281 entry_data->kick_usc_pds_upload->bo->map,
282 0U,
283 false,
284 PDS_GENERATE_CODEDATA_SEGMENTS);
285
286 dev_addr = entry_data->kick_usc_pds_upload->vma->dev_addr;
287 dev_addr.addr -= device->heaps.pds_heap->base_addr.addr;
288 entry_data->kick_usc_pds_offset = dev_addr;
289
290 *entry_data_out = entry_data;
291
292 return VK_SUCCESS;
293
294 err_free_usc_upload:
295 pvr_bo_suballoc_free(entry_data->usc_upload);
296
297 err_free_entry:
298 ralloc_free(entry_data);
299
300 return result;
301 }
302
303 static void inline pvr_transfer_frag_store_entry_data_destroy_no_ralloc_free(
304 struct pvr_device *device,
305 const struct pvr_transfer_frag_store_entry_data *entry_data)
306 {
307 pvr_bo_free(device, entry_data->kick_usc_pds_upload);
308 pvr_bo_suballoc_free(entry_data->usc_upload);
309 }
310
311 static void inline pvr_transfer_frag_store_entry_data_destroy(
312 struct pvr_device *device,
313 const struct pvr_transfer_frag_store_entry_data *entry_data)
314 {
315 pvr_transfer_frag_store_entry_data_destroy_no_ralloc_free(device,
316 entry_data);
317 /* Casting away the const :( */
318 ralloc_free((void *)entry_data);
319 }
320
321 static VkResult pvr_transfer_frag_store_get_entry(
322 struct pvr_device *device,
323 struct pvr_transfer_frag_store *store,
324 const struct pvr_tq_shader_properties *shader_props,
325 const struct pvr_transfer_frag_store_entry_data **const entry_data_out)
326 {
327 const uint32_t key =
328 pvr_transfer_frag_shader_key(store->max_multisample, shader_props);
329 const struct hash_entry *entry;
330 VkResult result;
331
332 entry = _mesa_hash_table_search(store->hash_table, to_hash_table_key(key));
333 if (!entry) {
334 /* Init so that gcc stops complaining. */
335 const struct pvr_transfer_frag_store_entry_data *entry_data = NULL;
336
337 result = pvr_transfer_frag_store_entry_data_create(device,
338 store,
339 shader_props,
340 &entry_data);
341 if (result != VK_SUCCESS)
342 return result;
343
344 assert(entry_data);
345
346 entry = _mesa_hash_table_insert(store->hash_table,
347 to_hash_table_key(key),
348 (void *)entry_data);
349 if (!entry) {
350 pvr_transfer_frag_store_entry_data_destroy(device, entry_data);
351 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
352 }
353 }
354
355 *entry_data_out = to_pvr_entry_data(entry);
356
357 return VK_SUCCESS;
358 }
359
360 VkResult pvr_transfer_frag_store_get_shader_info(
361 struct pvr_device *device,
362 struct pvr_transfer_frag_store *store,
363 const struct pvr_tq_shader_properties *shader_props,
364 pvr_dev_addr_t *const pds_dev_addr_out,
365 const struct pvr_tq_frag_sh_reg_layout **const reg_layout_out)
366 {
367 /* Init so that gcc stops complaining. */
368 const struct pvr_transfer_frag_store_entry_data *entry_data = NULL;
369 VkResult result;
370
371 result = pvr_transfer_frag_store_get_entry(device,
372 store,
373 shader_props,
374 &entry_data);
375 if (result != VK_SUCCESS)
376 return result;
377
378 *pds_dev_addr_out = entry_data->kick_usc_pds_offset;
379 *reg_layout_out = &entry_data->sh_reg_layout;
380
381 return VK_SUCCESS;
382 }
383
384 void pvr_transfer_frag_store_fini(struct pvr_device *device,
385 struct pvr_transfer_frag_store *store)
386 {
387 hash_table_foreach_remove(store->hash_table, entry)
388 {
389 /* ralloc_free() in _mesa_hash_table_destroy() will free each entry's
390 * memory so let's not waste extra time freeing them one by one and
391 * unliking.
392 */
393 pvr_transfer_frag_store_entry_data_destroy_no_ralloc_free(
394 device,
395 to_pvr_entry_data(entry));
396 }
397
398 _mesa_hash_table_destroy(store->hash_table, NULL);
399 }
400
401 /* Leave this at the very end, to avoid leakage of HW-defs here */
402 #define PVR_BUILD_ARCH_ROGUE
403 #include "pvr_csb.h"
404
405 static uint32_t get_doutu_sample_rate(const struct pvr_device_info *dev_info,
406 bool full_rate)
407 {
408 return full_rate ? ROGUE_PDSINST_DOUTU_SAMPLE_RATE_FULL
409 : ROGUE_PDSINST_DOUTU_SAMPLE_RATE_INSTANCE;
410 }