1 /*
2  * Copyright © 2022 Collabora, Ltd.
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 "vk_buffer_view.h"
25 
26 #include "vk_alloc.h"
27 #include "vk_buffer.h"
28 #include "vk_device.h"
29 #include "vk_format.h"
30 
31 void
vk_buffer_view_init(struct vk_device * device,struct vk_buffer_view * buffer_view,const VkBufferViewCreateInfo * pCreateInfo)32 vk_buffer_view_init(struct vk_device *device,
33                     struct vk_buffer_view *buffer_view,
34                     const VkBufferViewCreateInfo *pCreateInfo)
35 {
36    VK_FROM_HANDLE(vk_buffer, buffer, pCreateInfo->buffer);
37 
38    vk_object_base_init(device, &buffer_view->base, VK_OBJECT_TYPE_BUFFER_VIEW);
39 
40    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO);
41    assert(pCreateInfo->flags == 0);
42    assert(pCreateInfo->range > 0);
43 
44    buffer_view->buffer = buffer;
45    buffer_view->format = pCreateInfo->format;
46    buffer_view->offset = pCreateInfo->offset;
47    buffer_view->range = vk_buffer_range(buffer, pCreateInfo->offset,
48                                         pCreateInfo->range);
49    buffer_view->elements = buffer_view->range /
50                            vk_format_get_blocksize(buffer_view->format);
51 }
52 
53 void *
vk_buffer_view_create(struct vk_device * device,const VkBufferViewCreateInfo * pCreateInfo,const VkAllocationCallbacks * alloc,size_t size)54 vk_buffer_view_create(struct vk_device *device,
55                       const VkBufferViewCreateInfo *pCreateInfo,
56                       const VkAllocationCallbacks *alloc,
57                       size_t size)
58 {
59    struct vk_buffer_view *buffer_view;
60 
61    buffer_view = vk_zalloc2(&device->alloc, alloc, size, 8,
62                             VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
63    if (!buffer_view)
64       return NULL;
65 
66    vk_buffer_view_init(device, buffer_view, pCreateInfo);
67 
68    return buffer_view;
69 }
70 
71 void
vk_buffer_view_finish(struct vk_buffer_view * buffer_view)72 vk_buffer_view_finish(struct vk_buffer_view *buffer_view)
73 {
74    vk_object_base_finish(&buffer_view->base);
75 }
76 
77 void
vk_buffer_view_destroy(struct vk_device * device,const VkAllocationCallbacks * alloc,struct vk_buffer_view * buffer_view)78 vk_buffer_view_destroy(struct vk_device *device,
79                        const VkAllocationCallbacks *alloc,
80                        struct vk_buffer_view *buffer_view)
81 {
82    vk_object_free(device, alloc, buffer_view);
83 }
84