Debian Codegraph @

src/asahi/vulkan/hk_buffer_view.c

raw
1 /*
2 * Copyright 2024 Valve Corporation
3 * Copyright 2024 Alyssa Rosenzweig
4 * Copyright 2022-2023 Collabora Ltd. and Red Hat Inc.
5 * SPDX-License-Identifier: MIT
6 */
7 #include "hk_buffer_view.h"
8 #include "asahi/layout/layout.h"
9 #include "asahi/lib/agx_nir_lower_vbo.h"
10 #include "util/bitscan.h"
11 #include "util/format/u_format.h"
12 #include "util/format/u_formats.h"
13
14 #include "agx_helpers.h"
15 #include "agx_nir_texture.h"
16 #include "agx_pack.h"
17 #include "hk_buffer.h"
18 #include "hk_device.h"
19 #include "hk_entrypoints.h"
20 #include "hk_image.h"
21 #include "hk_physical_device.h"
22
23 VkFormatFeatureFlags2
24 hk_get_buffer_format_features(struct hk_physical_device *pdev,
25 VkFormat vk_format)
26 {
27 VkFormatFeatureFlags2 features = 0;
28 enum pipe_format p_format = hk_format_to_pipe_format(vk_format);
29
30 if (p_format == PIPE_FORMAT_NONE)
31 return 0;
32
33 if (agx_vbo_supports_format(p_format))
34 features |= VK_FORMAT_FEATURE_2_VERTEX_BUFFER_BIT;
35
36 if (ail_pixel_format[p_format].texturable &&
37 !util_format_is_depth_or_stencil(p_format)) {
38
39 features |= VK_FORMAT_FEATURE_2_UNIFORM_TEXEL_BUFFER_BIT;
40
41 /* RGB32 specially supported for uniform texel buffers only. */
42 if (util_is_power_of_two_nonzero(util_format_get_blocksize(p_format))) {
43 features |= VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_BIT |
44 VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
45 }
46
47 if (p_format == PIPE_FORMAT_R32_UINT || p_format == PIPE_FORMAT_R32_SINT)
48 features |= VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
49 }
50
51 return features;
52 }
53
54 VKAPI_ATTR VkResult VKAPI_CALL
55 hk_CreateBufferView(VkDevice _device, const VkBufferViewCreateInfo *pCreateInfo,
56 const VkAllocationCallbacks *pAllocator,
57 VkBufferView *pBufferView)
58 {
59 VK_FROM_HANDLE(hk_device, device, _device);
60 VK_FROM_HANDLE(hk_buffer, buffer, pCreateInfo->buffer);
61 struct hk_buffer_view *view;
62
63 view = vk_buffer_view_create(&device->vk, pCreateInfo, pAllocator,
64 sizeof(*view));
65 if (!view)
66 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
67
68 enum pipe_format format = hk_format_to_pipe_format(view->vk.format);
69 const struct util_format_description *desc = util_format_description(format);
70
71 uint8_t format_swizzle[4] = {
72 desc->swizzle[0],
73 desc->swizzle[1],
74 desc->swizzle[2],
75 desc->swizzle[3],
76 };
77
78 if (util_format_is_depth_or_stencil(format)) {
79 assert(!util_format_is_depth_and_stencil(format) &&
80 "separate stencil always used");
81
82 /* Broadcast depth and stencil */
83 format_swizzle[0] = 0;
84 format_swizzle[1] = 0;
85 format_swizzle[2] = 0;
86 format_swizzle[3] = 0;
87 }
88
89 /* Decompose the offset into a multiple of 16-bytes (which we can include in
90 * the address) and an extra texel-aligned tail offset of up to 15 bytes.
91 *
92 * This lets us offset partially in the shader instead, getting
93 * around alignment restrictions on the base address pointer.
94 */
95 uint64_t head_offset_B = view->vk.offset & ~0xf;
96 uint64_t rw_base = hk_buffer_address_rw(buffer, 0) + head_offset_B;
97 uint64_t ro_base = hk_buffer_address_ro(buffer, 0) + head_offset_B;
98 uint32_t tail_offset_B = view->vk.offset & 0xf;
99 uint32_t tail_offset_el = tail_offset_B / util_format_get_blocksize(format);
100 assert(tail_offset_el * util_format_get_blocksize(format) == tail_offset_B &&
101 "must be texel aligned");
102
103 agx_pack(&view->tex, TEXTURE, cfg) {
104 cfg.dimension = AGX_TEXTURE_DIMENSION_2D;
105 cfg.layout = AGX_LAYOUT_LINEAR;
106 cfg.channels = ail_pixel_format[format].channels;
107 cfg.type = ail_pixel_format[format].type;
108 cfg.swizzle_r = agx_channel_from_pipe(format_swizzle[0]);
109 cfg.swizzle_g = agx_channel_from_pipe(format_swizzle[1]);
110 cfg.swizzle_b = agx_channel_from_pipe(format_swizzle[2]);
111 cfg.swizzle_a = agx_channel_from_pipe(format_swizzle[3]);
112
113 cfg.width = AGX_TEXTURE_BUFFER_WIDTH;
114 cfg.height = DIV_ROUND_UP(view->vk.elements, cfg.width);
115 cfg.first_level = cfg.last_level = 0;
116
117 cfg.address = ro_base;
118 cfg.buffer_size_sw = view->vk.elements;
119 cfg.buffer_offset_sw = tail_offset_el;
120
121 cfg.srgb = (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB);
122 cfg.srgb_2_channel = cfg.srgb && util_format_colormask(desc) == 0x3;
123
124 cfg.depth = 1;
125 cfg.stride = (cfg.width * util_format_get_blocksize(format)) - 16;
126 }
127
128 agx_pack(&view->pbe, PBE, cfg) {
129 cfg.dimension = AGX_TEXTURE_DIMENSION_2D;
130 cfg.layout = AGX_LAYOUT_LINEAR;
131 cfg.channels = ail_pixel_format[format].channels;
132 cfg.type = ail_pixel_format[format].type;
133 cfg.srgb = util_format_is_srgb(format);
134
135 assert(desc->nr_channels >= 1 && desc->nr_channels <= 4);
136
137 for (unsigned i = 0; i < desc->nr_channels; ++i) {
138 if (desc->swizzle[i] == 0)
139 cfg.swizzle_r = i;
140 else if (desc->swizzle[i] == 1)
141 cfg.swizzle_g = i;
142 else if (desc->swizzle[i] == 2)
143 cfg.swizzle_b = i;
144 else if (desc->swizzle[i] == 3)
145 cfg.swizzle_a = i;
146 }
147
148 cfg.buffer = rw_base;
149 cfg.buffer_offset_sw = tail_offset_el;
150
151 cfg.width = AGX_TEXTURE_BUFFER_WIDTH;
152 cfg.height = DIV_ROUND_UP(view->vk.elements, cfg.width);
153 cfg.level = 0;
154 cfg.stride = (cfg.width * util_format_get_blocksize(format)) - 4;
155 cfg.layers = 1;
156 cfg.levels = 1;
157 };
158
159 *pBufferView = hk_buffer_view_to_handle(view);
160
161 return VK_SUCCESS;
162 }
163
164 VKAPI_ATTR void VKAPI_CALL
165 hk_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
166 const VkAllocationCallbacks *pAllocator)
167 {
168 VK_FROM_HANDLE(hk_device, device, _device);
169 VK_FROM_HANDLE(hk_buffer_view, view, bufferView);
170
171 if (!view)
172 return;
173
174 vk_buffer_view_destroy(&device->vk, pAllocator, &view->vk);
175 }