dEQP-VK.renderpass: Set IMAGE_USAGE_TRANSFER_SRC_BIT when needed
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / api / vktApiBufferViewCreateTests.cpp
1 /*------------------------------------------------------------------------
2  *  Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and/or associated documentation files (the
10  * "Materials"), to deal in the Materials without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Materials, and to
13  * permit persons to whom the Materials are furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice(s) and this permission notice shall be included
17  * in all copies or substantial portions of the Materials.
18  *
19  * The Materials are Confidential Information as defined by the
20  * Khronos Membership Agreement until designated non-confidential by Khronos,
21  * at which point this condition clause shall be removed.
22  *
23  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
27  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
30  *
31  *//*!
32  * \file
33  * \brief Vulkan Buffer View Creation Tests
34  *//*--------------------------------------------------------------------*/
35
36 #include "vktApiBufferViewCreateTests.hpp"
37
38 #include "deStringUtil.hpp"
39 #include "gluVarType.hpp"
40 #include "tcuTestLog.hpp"
41 #include "vkPrograms.hpp"
42 #include "vktTestCase.hpp"
43
44 namespace vkt
45 {
46
47 using namespace vk;
48
49 namespace api
50 {
51
52 namespace
53 {
54
55 struct BufferViewCaseParameters
56 {
57         VkFormat                                format;
58         VkDeviceSize                    offset;
59         VkDeviceSize                    range;
60         VkBufferUsageFlags              usage;
61         VkFormatFeatureFlags    features;
62 };
63
64 class BufferViewTestInstance : public TestInstance
65 {
66 public:
67                                                                 BufferViewTestInstance          (Context&                                       ctx,
68                                                                                                                          BufferViewCaseParameters       createInfo);
69         virtual tcu::TestStatus         iterate                                         (void);
70
71 private:
72         BufferViewCaseParameters                m_testCase;
73 };
74
75 class BufferViewTestCase : public TestCase
76 {
77 public:
78                                                         BufferViewTestCase              (tcu::TestContext&                      testCtx,
79                                                                                                          const std::string&                     name,
80                                                                                                          const std::string&                     description,
81                                                                                                          BufferViewCaseParameters       createInfo)
82                                                                 : TestCase                      (testCtx, name, description)
83                                                                 , m_testCase            (createInfo)
84                                                         {}
85
86         virtual                                 ~BufferViewTestCase             (void) {}
87         virtual TestInstance*   createInstance                  (Context&       ctx) const
88                                                         {
89                                                                 return new BufferViewTestInstance(ctx, m_testCase);
90                                                         }
91
92 private:
93         BufferViewCaseParameters        m_testCase;
94 };
95
96 BufferViewTestInstance::BufferViewTestInstance (Context&                                        ctx,
97                                                                                                 BufferViewCaseParameters        createInfo)
98         : TestInstance  (ctx)
99         , m_testCase    (createInfo)
100 {
101 }
102
103 tcu::TestStatus BufferViewTestInstance::iterate (void)
104 {
105         // Create buffer
106         const VkDevice                          vkDevice                                = m_context.getDevice();
107         const DeviceInterface&          vk                                              = m_context.getDeviceInterface();
108         const deUint32                          queueFamilyIndex                = m_context.getUniversalQueueFamilyIndex();
109         const VkDeviceSize                      size                                    = 16 * 1024;
110         VkBuffer                                        testBuffer;
111         VkMemoryRequirements            memReqs;
112         VkFormatProperties                      properties;
113         const VkBufferCreateInfo        bufferParams =
114         {
115                 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,                                                                                                   //      VkStructureType                 sType;
116                 DE_NULL,                                                                                                                                                                //      const void*                             pNext;
117                 0u,                                                                                                                                                                             //      VkBufferCreateFlags             flags;
118                 size,                                                                                                                                                                   //      VkDeviceSize                    size;
119                 m_testCase.usage,                                                                                                                                               //      VkBufferUsageFlags              usage;
120                 VK_SHARING_MODE_EXCLUSIVE,                                                                                                                              //      VkSharingMode                   sharingMode;
121                 1u,                                                                                                                                                                             //      deUint32                                queueFamilyCount;
122                 &queueFamilyIndex,                                                                                                                                              //      const deUint32*                 pQueueFamilyIndices;
123         };
124
125         m_context.getInstanceInterface().getPhysicalDeviceFormatProperties(m_context.getPhysicalDevice(), m_testCase.format, &properties);
126         if (!(properties.bufferFeatures & m_testCase.features))
127                 TCU_THROW(NotSupportedError, "Format not supported");
128
129         if (vk.createBuffer(vkDevice, &bufferParams, (const VkAllocationCallbacks*)DE_NULL, &testBuffer) != VK_SUCCESS)
130                 return tcu::TestStatus::fail("Buffer creation failed!");
131
132         vk.getBufferMemoryRequirements(vkDevice, testBuffer, &memReqs);
133
134         if (size > memReqs.size)
135         {
136                 std::ostringstream errorMsg;
137                 errorMsg << "Requied memory size (" << memReqs.size << " bytes) smaller than the buffer's size (" << size << " bytes)!";
138                 return tcu::TestStatus::fail(errorMsg.str());
139         }
140
141         VkDeviceMemory                          memory;
142         const VkMemoryAllocateInfo      memAlloc                                =
143         {
144                 VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,         //      VkStructureType         sType
145                 NULL,                                                                           //      const void*                     pNext
146                 memReqs.size,                                                           //      VkDeviceSize            allocationSize
147                 0                                                                                       //      deUint32                        memoryTypeIndex
148         };
149
150         {
151                 // Create buffer view.
152                 VkBufferView                                    bufferView;
153                 const VkBufferViewCreateInfo    bufferViewCreateInfo    =
154                 {
155                         VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO,      //      VkStructureType         sType;
156                         NULL,                                                                           //      const void*                     pNext;
157                         (VkBufferViewCreateFlags)0,
158                         testBuffer,                                                                     //      VkBuffer                        buffer;
159                         m_testCase.format,                                                      //      VkFormat                        format;
160                         m_testCase.offset,                                                      //      VkDeviceSize            offset;
161                         m_testCase.range,                                                       //      VkDeviceSize            range;
162                 };
163
164                 if (vk.allocateMemory(vkDevice, &memAlloc, (const VkAllocationCallbacks*)DE_NULL, &memory) != VK_SUCCESS)
165                         return tcu::TestStatus::fail("Alloc memory failed!");
166
167                 if (vk.bindBufferMemory(vkDevice, testBuffer, memory, 0) != VK_SUCCESS)
168                         return tcu::TestStatus::fail("Bind buffer memory failed!");
169
170                 if (vk.createBufferView(vkDevice, &bufferViewCreateInfo, (const VkAllocationCallbacks*)DE_NULL, &bufferView) != VK_SUCCESS)
171                         return tcu::TestStatus::fail("Buffer View creation failed!");
172
173                 vk.destroyBufferView(vkDevice, bufferView, (const VkAllocationCallbacks*)DE_NULL);
174         }
175
176         // Testing complete view size.
177         {
178                 VkBufferView                    completeBufferView;
179                 VkBufferViewCreateInfo  completeBufferViewCreateInfo    =
180                 {
181                         VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO,      //      VkStructureType         sType;
182                         NULL,                                                                           //      const void*                     pNext;
183                         (VkBufferViewCreateFlags)0,
184                         testBuffer,                                                                     //      VkBuffer                        buffer;
185                         m_testCase.format,                                                      //      VkFormat                        format;
186                         m_testCase.offset,                                                      //      VkDeviceSize            offset;
187                         size,                                                                           //      VkDeviceSize            range;
188                 };
189
190                 if (vk.createBufferView(vkDevice, &completeBufferViewCreateInfo, (const VkAllocationCallbacks*)DE_NULL, &completeBufferView) != VK_SUCCESS)
191                         return tcu::TestStatus::fail("Buffer View creation failed!");
192
193                 vk.destroyBufferView(vkDevice, completeBufferView, (const VkAllocationCallbacks*)DE_NULL);
194         }
195
196         vk.freeMemory(vkDevice, memory, (const VkAllocationCallbacks*)DE_NULL);
197         vk.destroyBuffer(vkDevice, testBuffer, (const VkAllocationCallbacks*)DE_NULL);
198
199         return tcu::TestStatus::pass("BufferView test");
200 }
201
202 } // anonymous
203
204  tcu::TestCaseGroup* createBufferViewCreateTests (tcu::TestContext& testCtx)
205 {
206         de::MovePtr<tcu::TestCaseGroup> bufferViewTests (new tcu::TestCaseGroup(testCtx, "create", "BufferView Construction Tests"));
207
208         const VkDeviceSize range = 96;
209         for (deUint32 format = VK_FORMAT_UNDEFINED + 1; format < VK_FORMAT_LAST; format++)
210         {
211                 std::ostringstream      testName;
212                 std::ostringstream      testDescription;
213                 testName << "createBufferView_" << format;
214                 testDescription << "vkBufferView test " << testName.str();
215                 {
216                         BufferViewCaseParameters testParams     =
217                         {
218                                 (VkFormat)format,                                                       // VkFormat                             format;
219                                 0,                                                                                      // VkDeviceSize                 offset;
220                                 range,                                                                          // VkDeviceSize                 range;
221                                 VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT,       // VkBufferUsageFlags   usage;
222                                 VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT, // VkFormatFeatureFlags flags;
223                         };
224                         bufferViewTests->addChild(new BufferViewTestCase(testCtx, testName.str() + "_uniform", testDescription.str(), testParams));
225                 }
226                 {
227                         BufferViewCaseParameters testParams     =
228                         {
229                                 (VkFormat)format,                                                       // VkFormat                             format;
230                                 0,                                                                                      // VkDeviceSize                 offset;
231                                 range,                                                                          // VkDeviceSize                 range;
232                                 VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT,       // VkBufferUsageFlags   usage;
233                                 VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT, // VkFormatFeatureFlags flags;
234                         };
235                         bufferViewTests->addChild(new BufferViewTestCase(testCtx, testName.str() + "_storage", testDescription.str(), testParams));
236                 }
237         }
238
239         return bufferViewTests.release();
240 }
241
242 } // api
243 } // vk