• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016 Red Hat.
3  * Copyright © 2016 Bas Nieuwenhuizen
4  *
5  * based in part on anv driver which is:
6  * Copyright © 2015 Intel Corporation
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25  * IN THE SOFTWARE.
26  */
27 
28 #include "gfx10_format_table.h"
29 
30 #include "radv_private.h"
31 
32 void
radv_make_texel_buffer_descriptor(struct radv_device * device,uint64_t va,VkFormat vk_format,unsigned offset,unsigned range,uint32_t * state)33 radv_make_texel_buffer_descriptor(struct radv_device *device, uint64_t va, VkFormat vk_format, unsigned offset,
34                                   unsigned range, uint32_t *state)
35 {
36    const struct util_format_description *desc;
37    unsigned stride;
38    unsigned num_format, data_format;
39    int first_non_void;
40    enum pipe_swizzle swizzle[4];
41    unsigned rsrc_word3;
42 
43    desc = vk_format_description(vk_format);
44    first_non_void = vk_format_get_first_non_void_channel(vk_format);
45    stride = desc->block.bits / 8;
46 
47    radv_compose_swizzle(desc, NULL, swizzle);
48 
49    va += offset;
50 
51    if (device->physical_device->rad_info.gfx_level != GFX8 && stride) {
52       range /= stride;
53    }
54 
55    rsrc_word3 = S_008F0C_DST_SEL_X(radv_map_swizzle(swizzle[0])) | S_008F0C_DST_SEL_Y(radv_map_swizzle(swizzle[1])) |
56                 S_008F0C_DST_SEL_Z(radv_map_swizzle(swizzle[2])) | S_008F0C_DST_SEL_W(radv_map_swizzle(swizzle[3]));
57 
58    if (device->physical_device->rad_info.gfx_level >= GFX10) {
59       const struct gfx10_format *fmt =
60          &ac_get_gfx10_format_table(&device->physical_device->rad_info)[vk_format_to_pipe_format(vk_format)];
61 
62       /* OOB_SELECT chooses the out-of-bounds check.
63        *
64        * GFX10:
65        *  - 0: (index >= NUM_RECORDS) || (offset >= STRIDE)
66        *  - 1: index >= NUM_RECORDS
67        *  - 2: NUM_RECORDS == 0
68        *  - 3: if SWIZZLE_ENABLE:
69        *          swizzle_address >= NUM_RECORDS
70        *       else:
71        *          offset >= NUM_RECORDS
72        *
73        * GFX11:
74        *  - 0: (index >= NUM_RECORDS) || (offset+payload > STRIDE)
75        *  - 1: index >= NUM_RECORDS
76        *  - 2: NUM_RECORDS == 0
77        *  - 3: if SWIZZLE_ENABLE && STRIDE:
78        *          (index >= NUM_RECORDS) || ( offset+payload > STRIDE)
79        *       else:
80        *          offset+payload > NUM_RECORDS
81        */
82       rsrc_word3 |= S_008F0C_FORMAT(fmt->img_format) | S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_STRUCTURED_WITH_OFFSET) |
83                     S_008F0C_RESOURCE_LEVEL(device->physical_device->rad_info.gfx_level < GFX11);
84    } else {
85       num_format = radv_translate_buffer_numformat(desc, first_non_void);
86       data_format = radv_translate_buffer_dataformat(desc, first_non_void);
87 
88       assert(data_format != V_008F0C_BUF_DATA_FORMAT_INVALID);
89       assert(num_format != ~0);
90 
91       rsrc_word3 |= S_008F0C_NUM_FORMAT(num_format) | S_008F0C_DATA_FORMAT(data_format);
92    }
93 
94    state[0] = va;
95    state[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) | S_008F04_STRIDE(stride);
96    state[2] = range;
97    state[3] = rsrc_word3;
98 }
99 
100 void
radv_buffer_view_init(struct radv_buffer_view * view,struct radv_device * device,const VkBufferViewCreateInfo * pCreateInfo)101 radv_buffer_view_init(struct radv_buffer_view *view, struct radv_device *device,
102                       const VkBufferViewCreateInfo *pCreateInfo)
103 {
104    RADV_FROM_HANDLE(radv_buffer, buffer, pCreateInfo->buffer);
105    uint64_t va = radv_buffer_get_va(buffer->bo) + buffer->offset;
106 
107    vk_buffer_view_init(&device->vk, &view->vk, pCreateInfo);
108 
109    view->bo = buffer->bo;
110 
111    radv_make_texel_buffer_descriptor(device, va, view->vk.format, view->vk.offset, view->vk.range, view->state);
112 }
113 
114 void
radv_buffer_view_finish(struct radv_buffer_view * view)115 radv_buffer_view_finish(struct radv_buffer_view *view)
116 {
117    vk_buffer_view_finish(&view->vk);
118 }
119 
120 VKAPI_ATTR VkResult VKAPI_CALL
radv_CreateBufferView(VkDevice _device,const VkBufferViewCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkBufferView * pView)121 radv_CreateBufferView(VkDevice _device, const VkBufferViewCreateInfo *pCreateInfo,
122                       const VkAllocationCallbacks *pAllocator, VkBufferView *pView)
123 {
124    RADV_FROM_HANDLE(radv_device, device, _device);
125    struct radv_buffer_view *view;
126 
127    view = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*view), 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
128    if (!view)
129       return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
130 
131    radv_buffer_view_init(view, device, pCreateInfo);
132 
133    *pView = radv_buffer_view_to_handle(view);
134 
135    return VK_SUCCESS;
136 }
137 
138 VKAPI_ATTR void VKAPI_CALL
radv_DestroyBufferView(VkDevice _device,VkBufferView bufferView,const VkAllocationCallbacks * pAllocator)139 radv_DestroyBufferView(VkDevice _device, VkBufferView bufferView, const VkAllocationCallbacks *pAllocator)
140 {
141    RADV_FROM_HANDLE(radv_device, device, _device);
142    RADV_FROM_HANDLE(radv_buffer_view, view, bufferView);
143 
144    if (!view)
145       return;
146 
147    radv_buffer_view_finish(view);
148    vk_free2(&device->vk.alloc, pAllocator, view);
149 }
150