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
27namespace vkt
28{
29namespace api
30{
31
32using namespace vk;
33
34ComputeInstanceResultBuffer::ComputeInstanceResultBuffer (const DeviceInterface &vki,
35																	  VkDevice device,
36																	  Allocator &allocator)
37		: m_vki(vki),
38		m_device(device),
39		m_bufferMem(DE_NULL),
40		m_buffer(createResultBuffer(m_vki, m_device, allocator, &m_bufferMem)),
41		m_bufferBarrier(createResultBufferBarrier(*m_buffer))
42{
43}
44
45void ComputeInstanceResultBuffer::readResultContentsTo(tcu::Vec4 (*results)[4]) const
46{
47	invalidateMappedMemoryRange(m_vki, m_device, m_bufferMem->getMemory(), m_bufferMem->getOffset(), sizeof(*results));
48	deMemcpy(*results, m_bufferMem->getHostPtr(), sizeof(*results));
49}
50
51Move<VkBuffer> ComputeInstanceResultBuffer::createResultBuffer(const DeviceInterface &vki,
52																	 VkDevice device,
53																	 Allocator &allocator,
54																	 de::MovePtr<Allocation> *outAllocation)
55{
56	const VkBufferCreateInfo createInfo =
57	{
58		VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
59		DE_NULL,
60		0u,															// flags
61		(VkDeviceSize) DATA_SIZE,									// size
62		VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,							// usage
63		VK_SHARING_MODE_EXCLUSIVE,									// sharingMode
64		0u,															// queueFamilyCount
65		DE_NULL,													// pQueueFamilyIndices
66	};
67
68	Move<VkBuffer> buffer(createBuffer(vki, device, &createInfo));
69
70	const VkMemoryRequirements				requirements			= getBufferMemoryRequirements(vki, device, *buffer);
71	de::MovePtr<Allocation>					allocation				= allocator.allocate(requirements, MemoryRequirement::HostVisible);
72
73	VK_CHECK(vki.bindBufferMemory(device, *buffer, allocation->getMemory(), allocation->getOffset()));
74
75	const float								clearValue				= -1.0f;
76	void*									mapPtr					= allocation->getHostPtr();
77
78	for (size_t offset = 0; offset < DATA_SIZE; offset += sizeof(float))
79		deMemcpy(((deUint8 *) mapPtr) + offset, &clearValue, sizeof(float));
80
81	flushMappedMemoryRange(vki, device, allocation->getMemory(), allocation->getOffset(), (VkDeviceSize) DATA_SIZE);
82
83	*outAllocation = allocation;
84	return buffer;
85}
86
87VkBufferMemoryBarrier ComputeInstanceResultBuffer::createResultBufferBarrier(VkBuffer buffer)
88{
89	const VkBufferMemoryBarrier bufferBarrier =
90	{
91		VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
92		DE_NULL,
93		VK_ACCESS_SHADER_WRITE_BIT,									// outputMask
94		VK_ACCESS_SHADER_READ_BIT,									// inputMask
95		VK_QUEUE_FAMILY_IGNORED,									// srcQueueFamilyIndex
96		VK_QUEUE_FAMILY_IGNORED,									// destQueueFamilyIndex
97		buffer,														// buffer
98		(VkDeviceSize) 0u,											// offset
99		DATA_SIZE,													// size
100	};
101
102	return bufferBarrier;
103}
104
105} // api
106} // vkt
107