xref: /aosp_15_r20/external/deqp/external/vulkancts/modules/vulkan/draw/vktDrawBufferObjectUtil.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Intel Corporation
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *//*!
21  * \file
22  * \brief Buffer Object Util
23  *//*--------------------------------------------------------------------*/
24 
25 #include "vktDrawBufferObjectUtil.hpp"
26 
27 #include "vkQueryUtil.hpp"
28 
29 namespace vkt
30 {
31 namespace Draw
32 {
33 
Buffer(const vk::DeviceInterface & vk,vk::VkDevice device,vk::Move<vk::VkBuffer> object_)34 Buffer::Buffer(const vk::DeviceInterface &vk, vk::VkDevice device, vk::Move<vk::VkBuffer> object_)
35     : m_allocation(DE_NULL)
36     , m_allocOffset(0ull)
37     , m_object(object_)
38     , m_vk(vk)
39     , m_device(device)
40 {
41 }
42 
bindMemory(de::MovePtr<vk::Allocation> allocation,vk::VkDeviceSize allocOffset)43 void Buffer::bindMemory(de::MovePtr<vk::Allocation> allocation, vk::VkDeviceSize allocOffset)
44 {
45     DE_ASSERT(allocation);
46     VK_CHECK(
47         m_vk.bindBufferMemory(m_device, *m_object, allocation->getMemory(), allocation->getOffset() + allocOffset));
48 
49     DE_ASSERT(!m_allocation);
50     m_allocation  = allocation;
51     m_allocOffset = allocOffset;
52 }
53 
createAndAlloc(const vk::DeviceInterface & vk,vk::VkDevice device,const vk::VkBufferCreateInfo & createInfo,vk::Allocator & allocator,vk::MemoryRequirement memoryRequirement,vk::VkDeviceSize allocationOffset)54 de::SharedPtr<Buffer> Buffer::createAndAlloc(const vk::DeviceInterface &vk, vk::VkDevice device,
55                                              const vk::VkBufferCreateInfo &createInfo, vk::Allocator &allocator,
56                                              vk::MemoryRequirement memoryRequirement, vk::VkDeviceSize allocationOffset)
57 {
58     de::SharedPtr<Buffer> ret = create(vk, device, createInfo);
59 
60     vk::VkMemoryRequirements bufferRequirements = vk::getBufferMemoryRequirements(vk, device, ret->object());
61 
62     // If requested, allocate more memory for the extra offset inside the allocation.
63     const auto extraRoom = de::roundUp(allocationOffset, bufferRequirements.alignment);
64     bufferRequirements.size += extraRoom;
65 
66     ret->bindMemory(allocator.allocate(bufferRequirements, memoryRequirement), extraRoom);
67     return ret;
68 }
69 
create(const vk::DeviceInterface & vk,vk::VkDevice device,const vk::VkBufferCreateInfo & createInfo)70 de::SharedPtr<Buffer> Buffer::create(const vk::DeviceInterface &vk, vk::VkDevice device,
71                                      const vk::VkBufferCreateInfo &createInfo)
72 {
73     return de::SharedPtr<Buffer>(new Buffer(vk, device, vk::createBuffer(vk, device, &createInfo)));
74 }
75 
getHostPtr(void) const76 void *Buffer::getHostPtr(void) const
77 {
78     if (!m_allocation)
79         return nullptr;
80     return reinterpret_cast<uint8_t *>(m_allocation->getHostPtr()) + m_allocOffset;
81 }
82 
bufferBarrier(const vk::DeviceInterface & vk,vk::VkCommandBuffer cmdBuffer,vk::VkBuffer buffer,vk::VkAccessFlags srcAccessMask,vk::VkAccessFlags dstAccessMask,vk::VkPipelineStageFlags srcStageMask,vk::VkPipelineStageFlags dstStageMask)83 void bufferBarrier(const vk::DeviceInterface &vk, vk::VkCommandBuffer cmdBuffer, vk::VkBuffer buffer,
84                    vk::VkAccessFlags srcAccessMask, vk::VkAccessFlags dstAccessMask,
85                    vk::VkPipelineStageFlags srcStageMask, vk::VkPipelineStageFlags dstStageMask)
86 {
87     vk::VkBufferMemoryBarrier barrier;
88     barrier.sType               = vk::VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
89     barrier.pNext               = DE_NULL;
90     barrier.srcAccessMask       = srcAccessMask;
91     barrier.dstAccessMask       = dstAccessMask;
92     barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
93     barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
94     barrier.buffer              = buffer;
95     barrier.offset              = 0;
96     barrier.size                = VK_WHOLE_SIZE;
97 
98     vk.cmdPipelineBarrier(cmdBuffer, srcStageMask, dstStageMask, (vk::VkDependencyFlags)0, 0,
99                           (const vk::VkMemoryBarrier *)DE_NULL, 1, &barrier, 0,
100                           (const vk::VkImageMemoryBarrier *)DE_NULL);
101 }
102 
103 } // namespace Draw
104 } // namespace vkt
105