1 /*-------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
4 *
5 * Copyright (c) 2015 The Khronos Group Inc.
6 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Copyright (c) 2015 Google Inc.
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*--------------------------------------------------------------------*/
22
23 #include "vktApiComputeInstanceResultBuffer.hpp"
24 #include "vktApiBufferComputeInstance.hpp"
25 #include "vkRefUtil.hpp"
26
27 namespace vkt
28 {
29 namespace api
30 {
31
32 using namespace vk;
33
ComputeInstanceResultBuffer(const DeviceInterface & vki,VkDevice device,Allocator & allocator,float initValue)34 ComputeInstanceResultBuffer::ComputeInstanceResultBuffer(const DeviceInterface &vki, VkDevice device,
35 Allocator &allocator, float initValue)
36 : m_vki(vki)
37 , m_device(device)
38 , m_bufferMem(DE_NULL)
39 , m_buffer(createResultBuffer(m_vki, m_device, allocator, &m_bufferMem, initValue))
40 , m_bufferBarrier(createResultBufferBarrier(*m_buffer))
41 {
42 }
43
readResultContentsTo(tcu::Vec4 (* results)[4]) const44 void ComputeInstanceResultBuffer::readResultContentsTo(tcu::Vec4 (*results)[4]) const
45 {
46 invalidateAlloc(m_vki, m_device, *m_bufferMem);
47 deMemcpy(*results, m_bufferMem->getHostPtr(), sizeof(*results));
48 }
49
readResultContentsTo(uint32_t * result) const50 void ComputeInstanceResultBuffer::readResultContentsTo(uint32_t *result) const
51 {
52 invalidateAlloc(m_vki, m_device, *m_bufferMem);
53 deMemcpy(result, m_bufferMem->getHostPtr(), sizeof(*result));
54 }
55
createResultBuffer(const DeviceInterface & vki,VkDevice device,Allocator & allocator,de::MovePtr<Allocation> * outAllocation,float initValue)56 Move<VkBuffer> ComputeInstanceResultBuffer::createResultBuffer(const DeviceInterface &vki, VkDevice device,
57 Allocator &allocator,
58 de::MovePtr<Allocation> *outAllocation, float initValue)
59 {
60 const VkBufferCreateInfo createInfo = {
61 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
62 DE_NULL,
63 0u, // flags
64 (VkDeviceSize)DATA_SIZE, // size
65 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, // usage
66 VK_SHARING_MODE_EXCLUSIVE, // sharingMode
67 0u, // queueFamilyCount
68 DE_NULL, // pQueueFamilyIndices
69 };
70
71 Move<VkBuffer> buffer(createBuffer(vki, device, &createInfo));
72
73 const VkMemoryRequirements requirements = getBufferMemoryRequirements(vki, device, *buffer);
74 de::MovePtr<Allocation> allocation = allocator.allocate(requirements, MemoryRequirement::HostVisible);
75
76 VK_CHECK(vki.bindBufferMemory(device, *buffer, allocation->getMemory(), allocation->getOffset()));
77
78 const float clearValue = initValue;
79 void *mapPtr = allocation->getHostPtr();
80
81 for (size_t offset = 0; offset < DATA_SIZE; offset += sizeof(float))
82 deMemcpy(((uint8_t *)mapPtr) + offset, &clearValue, sizeof(float));
83
84 flushAlloc(vki, device, *allocation);
85
86 *outAllocation = allocation;
87 return buffer;
88 }
89
createResultBufferBarrier(VkBuffer buffer)90 VkBufferMemoryBarrier ComputeInstanceResultBuffer::createResultBufferBarrier(VkBuffer buffer)
91 {
92 const VkBufferMemoryBarrier bufferBarrier = {
93 VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
94 DE_NULL,
95 VK_ACCESS_SHADER_WRITE_BIT, // srcAccessMask
96 VK_ACCESS_HOST_READ_BIT, // dstAccessMask
97 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
98 VK_QUEUE_FAMILY_IGNORED, // destQueueFamilyIndex
99 buffer, // buffer
100 (VkDeviceSize)0u, // offset
101 DATA_SIZE, // size
102 };
103
104 return bufferBarrier;
105 }
106
107 } // namespace api
108 } // namespace vkt
109