Make random number usage platform independent
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / protected_memory / vktProtectedMemClearColorImageTests.cpp
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2017 The Khronos Group Inc.
6  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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 Protected content clear color image tests
23  *//*--------------------------------------------------------------------*/
24
25 #include "vktProtectedMemClearColorImageTests.hpp"
26
27 #include "deRandom.hpp"
28 #include "tcuTestLog.hpp"
29 #include "tcuVector.hpp"
30 #include "tcuVectorUtil.hpp"
31
32 #include "vkPrograms.hpp"
33 #include "vktTestCase.hpp"
34 #include "vktTestGroupUtil.hpp"
35 #include "vkTypeUtil.hpp"
36 #include "vkBuilderUtil.hpp"
37 #include "vkCmdUtil.hpp"
38
39 #include "vktProtectedMemContext.hpp"
40 #include "vktProtectedMemUtils.hpp"
41 #include "vktProtectedMemImageValidator.hpp"
42
43 namespace vkt
44 {
45 namespace ProtectedMem
46 {
47
48 namespace
49 {
50
51 enum {
52         RENDER_WIDTH    = 128,
53         RENDER_HEIGHT   = 128,
54 };
55
56 class ClearColorImageTestInstance : public ProtectedTestInstance
57 {
58 public:
59                                                                                 ClearColorImageTestInstance     (Context&                                               ctx,
60                                                                                                                                          const vk::VkClearColorValue&   clearColorValue,
61                                                                                                                                          const ValidationData&                  refData,
62                                                                                                                                          const ImageValidator&                  validator,
63                                                                                                                                          const CmdBufferType                    cmdBufferType);
64         virtual tcu::TestStatus                         iterate                                         (void);
65
66 private:
67         const vk::VkFormat                                      m_imageFormat;
68         const vk::VkClearColorValue&            m_clearColorValue;
69         const ValidationData&                           m_refData;
70         const ImageValidator&                           m_validator;
71         const CmdBufferType                                     m_cmdBufferType;
72 };
73
74 class ClearColorImageTestCase : public TestCase
75 {
76 public:
77                                                                 ClearColorImageTestCase         (tcu::TestContext&                      testCtx,
78                                                                                                                          const std::string&                     name,
79                                                                                                                          vk::VkClearColorValue          clearColorValue,
80                                                                                                                          ValidationData                         data,
81                                                                                                                          CmdBufferType                          cmdBufferType)
82                                                                         : TestCase                              (testCtx, name, "Clear color image.")
83                                                                         , m_clearColorValue             (clearColorValue)
84                                                                         , m_refData                             (data)
85                                                                         , m_cmdBufferType               (cmdBufferType)
86                                                                 {
87                                                                 }
88
89         virtual                                         ~ClearColorImageTestCase        (void) {}
90         virtual TestInstance*           createInstance                          (Context& ctx) const
91                                                                 {
92                                                                         return new ClearColorImageTestInstance(ctx, m_clearColorValue, m_refData, m_validator, m_cmdBufferType);
93                                                                 }
94         virtual void                            initPrograms                            (vk::SourceCollections& programCollection) const
95                                                                 {
96                                                                         m_validator.initPrograms(programCollection);
97                                                                 }
98 private:
99         vk::VkClearColorValue           m_clearColorValue;
100         ValidationData                          m_refData;
101         ImageValidator                          m_validator;
102         CmdBufferType                           m_cmdBufferType;
103 };
104
105 ClearColorImageTestInstance::ClearColorImageTestInstance        (Context&                                               ctx,
106                                                                                                                          const vk::VkClearColorValue&   clearColorValue,
107                                                                                                                          const ValidationData&                  refData,
108                                                                                                                          const ImageValidator&                  validator,
109                                                                                                                          const CmdBufferType                    cmdBufferType)
110         : ProtectedTestInstance         (ctx)
111         , m_imageFormat                         (vk::VK_FORMAT_R8G8B8A8_UNORM)
112         , m_clearColorValue                     (clearColorValue)
113         , m_refData                                     (refData)
114         , m_validator                           (validator)
115         , m_cmdBufferType                       (cmdBufferType)
116 {
117 }
118
119 tcu::TestStatus ClearColorImageTestInstance::iterate()
120 {
121         ProtectedContext&                                       ctx                                     (m_protectedContext);
122         const vk::DeviceInterface&                      vk                                      = ctx.getDeviceInterface();
123         const vk::VkDevice                                      device                          = ctx.getDevice();
124         const vk::VkQueue                                       queue                           = ctx.getQueue();
125         const deUint32                                          queueFamilyIndex        = ctx.getQueueFamilyIndex();
126
127         // Create output image
128         de::MovePtr<vk::ImageWithMemory>        colorImage                      = createImage2D(ctx, PROTECTION_ENABLED, queueFamilyIndex,
129                                                                                                                                                         RENDER_WIDTH, RENDER_HEIGHT,
130                                                                                                                                                         vk::VK_FORMAT_R8G8B8A8_UNORM,
131                                                                                                                                                         vk::VK_IMAGE_USAGE_TRANSFER_DST_BIT|vk::VK_IMAGE_USAGE_SAMPLED_BIT);
132
133         vk::Unique<vk::VkPipelineLayout>        pipelineLayout          (createPipelineLayout(ctx, 0u, DE_NULL));
134
135         vk::Unique<vk::VkCommandPool>           cmdPool                         (makeCommandPool(vk, device, PROTECTION_ENABLED, queueFamilyIndex));
136         vk::Unique<vk::VkCommandBuffer>         cmdBuffer                       (vk::allocateCommandBuffer(vk, device, *cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY));
137         vk::Unique<vk::VkCommandBuffer>         secondaryCmdBuffer      (vk::allocateCommandBuffer(vk, device, *cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_SECONDARY));
138         vk::VkCommandBuffer                                     targetCmdBuffer         = (m_cmdBufferType == CMD_BUFFER_SECONDARY) ? *secondaryCmdBuffer : *cmdBuffer;
139
140         const vk::VkImageSubresourceRange subresourceRange =
141         {
142                 vk::VK_IMAGE_ASPECT_COLOR_BIT,  // VkImageAspectFlags   aspectMask
143                 0u,                                                             // uint32_t                             baseMipLevel
144                 1u,                                                             // uint32_t                             levelCount
145                 0u,                                                             // uint32_t                             baseArrayLayer
146                 1u,                                                             // uint32_t                             layerCount
147         };
148         // Begin cmd buffer
149         beginCommandBuffer(vk, *cmdBuffer);
150
151         if (m_cmdBufferType == CMD_BUFFER_SECONDARY)
152         {
153                 // Begin secondary command buffer
154                 const vk::VkCommandBufferInheritanceInfo        bufferInheritanceInfo   =
155                 {
156                         vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,          // sType
157                         DE_NULL,                                                                                                        // pNext
158                         DE_NULL,                                                                                                        // renderPass
159                         0u,                                                                                                                     // subpass
160                         DE_NULL,                                                                                                        // framebuffer
161                         VK_FALSE,                                                                                                       // occlusionQueryEnable
162                         (vk::VkQueryControlFlags)0u,                                                            // queryFlags
163                         (vk::VkQueryPipelineStatisticFlags)0u,                                          // pipelineStatistics
164                 };
165                 beginSecondaryCommandBuffer(vk, *secondaryCmdBuffer, bufferInheritanceInfo);
166         }
167
168         // Start image barrier
169         {
170                 const vk::VkImageMemoryBarrier  initializeBarrier       =
171                 {
172                         vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                     // sType
173                         DE_NULL,                                                                                        // pNext
174                         0,                                                                                                      // srcAccessMask
175                         vk::VK_ACCESS_TRANSFER_WRITE_BIT,                                       // dstAccessMask
176                         vk::VK_IMAGE_LAYOUT_UNDEFINED,                                          // oldLayout
177                         vk::VK_IMAGE_LAYOUT_GENERAL,                                            // newLayout
178                         queueFamilyIndex,                                                                       // srcQueueFamilyIndex
179                         queueFamilyIndex,                                                                       // dstQueueFamilyIndex
180                         **colorImage,                                                                           // image
181                         subresourceRange,                                                                       // subresourceRange
182                 };
183
184                 vk.cmdPipelineBarrier(targetCmdBuffer,                                                          // commandBuffer
185                                                           vk::VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,                // srcStageMask
186                                                           vk::VK_PIPELINE_STAGE_TRANSFER_BIT,                   // dstStageMask
187                                                           (vk::VkDependencyFlags)0,                                             // dependencyFlags
188                                                           0, (const vk::VkMemoryBarrier*)DE_NULL,               // memoryBarrierCount, pMemoryBarriers
189                                                           0, (const vk::VkBufferMemoryBarrier*)DE_NULL, // bufferMemoryBarrierCount, pBufferMemoryBarriers
190                                                           1, &initializeBarrier);                                               // imageMemoryBarrierCount, pImageMemoryBarriers
191         }
192
193         // Image clear
194         vk.cmdClearColorImage(targetCmdBuffer, **colorImage, vk::VK_IMAGE_LAYOUT_GENERAL, &m_clearColorValue, 1, &subresourceRange);
195
196         // Image barrier to change accessMask.
197         {
198                 const vk::VkImageMemoryBarrier  initializeBarrier       =
199                 {
200                         vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                     // sType
201                         DE_NULL,                                                                                        // pNext
202                         vk::VK_ACCESS_TRANSFER_WRITE_BIT,                                       // srcAccessMask
203                         vk::VK_ACCESS_SHADER_READ_BIT,                                          // dstAccessMask
204                         vk::VK_IMAGE_LAYOUT_GENERAL,                                            // oldLayout
205                         vk::VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,           // newLayout
206                         queueFamilyIndex,                                                                       // srcQueueFamilyIndex
207                         queueFamilyIndex,                                                                       // dstQueueFamilyIndex
208                         **colorImage,                                                                           // image
209                         subresourceRange                                                                        // subresourceRange
210                 };
211                 vk.cmdPipelineBarrier(targetCmdBuffer,
212                                                           vk::VK_PIPELINE_STAGE_TRANSFER_BIT,           // srcStageMask
213                                                           vk::VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,       // dstStageMask
214                                                           (vk::VkDependencyFlags)0,
215                                                           0, (const vk::VkMemoryBarrier*)DE_NULL,
216                                                           0, (const vk::VkBufferMemoryBarrier*)DE_NULL,
217                                                           1, &initializeBarrier);
218         }
219
220         if (m_cmdBufferType == CMD_BUFFER_SECONDARY)
221         {
222                 endCommandBuffer(vk, *secondaryCmdBuffer);
223                 vk.cmdExecuteCommands(*cmdBuffer, 1u, &secondaryCmdBuffer.get());
224         }
225
226         endCommandBuffer(vk, *cmdBuffer);
227
228         // Submit command buffer
229         const vk::Unique<vk::VkFence>   fence           (vk::createFence(vk, device));
230         VK_CHECK(queueSubmit(ctx, PROTECTION_ENABLED, queue, *cmdBuffer, *fence, ~0ull));
231
232         // Log out test data
233         ctx.getTestContext().getLog()
234                 << tcu::TestLog::Message << "Color clear value: " << tcu::Vec4(m_clearColorValue.float32) << tcu::TestLog::EndMessage;
235
236         // Validate resulting image
237         if (m_validator.validateImage(ctx, m_refData, **colorImage, m_imageFormat, vk::VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL))
238                 return tcu::TestStatus::pass("Everything went OK");
239         else
240                 return tcu::TestStatus::fail("Something went really wrong");
241 }
242
243 tcu::TestCaseGroup*     createClearColorImageTests (tcu::TestContext& testCtx, CmdBufferType cmdBufferType)
244 {
245         struct {
246                 vk::VkClearColorValue   clearColorValue;
247                 ValidationData                  data;
248         } testData[] = {
249                 {       { { 1.0f, 0.0f, 0.0f, 1.0f } },
250                         {
251                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
252                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
253                                 { tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f),
254                                   tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), }
255                         }
256                 },
257                 {       { { 0.0f, 1.0f, 0.0f, 1.0f } },
258                         {
259                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
260                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
261                                 { tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f), tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f),
262                                   tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f), tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f), }
263                         }
264                 },
265                 {       { { 0.0f, 0.0f, 1.0f, 1.0f } },
266                         {
267                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
268                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
269                                 { tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f),
270                                   tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), }
271                         }
272                 },
273                 {       { { 0.0f, 0.0f, 0.0f, 1.0f } },
274                         {
275                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
276                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
277                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f),
278                                   tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), }
279                         }
280                 },
281                 {       { { 1.0f, 0.0f, 0.0f, 1.0f } },
282                         {
283                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
284                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
285                                 { tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f),
286                                   tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), }
287                         }
288                 },
289                 {       { { 1.0f, 0.0f, 0.0f, 0.0f } },
290                         {
291                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
292                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
293                                 { tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f),
294                                   tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f), }
295                         }
296                 },
297                 {       { { 0.1f, 0.2f, 0.3f, 0.0f } },
298                         {
299                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
300                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
301                                 { tcu::Vec4(0.1f, 0.2f, 0.3f, 0.0f), tcu::Vec4(0.1f, 0.2f, 0.3f, 0.0f),
302                                   tcu::Vec4(0.1f, 0.2f, 0.3f, 0.0f), tcu::Vec4(0.1f, 0.2f, 0.3f, 0.0f), }
303                         }
304                 },
305         };
306
307         de::MovePtr<tcu::TestCaseGroup> clearStaticTests        (new tcu::TestCaseGroup(testCtx, "static", "Clear Color Image Tests with static input"));
308
309         for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(testData); ++ndx)
310         {
311                 const std::string name = "clear_" + de::toString(ndx + 1);
312                 clearStaticTests->addChild(new ClearColorImageTestCase(testCtx, name.c_str(), testData[ndx].clearColorValue, testData[ndx].data, cmdBufferType));
313         }
314
315         /* Add a few randomized tests */
316         de::MovePtr<tcu::TestCaseGroup> clearRandomTests        (new tcu::TestCaseGroup(testCtx, "random", "Clear Color Image Tests with random input"));
317         const int                                               testCount                       = 10;
318         de::Random                                              rnd                                     (testCtx.getCommandLine().getBaseSeed());
319         for (int ndx = 0; ndx < testCount; ++ndx)
320         {
321                 const std::string       name            = "clear_" + de::toString(ndx + 1);
322                 vk::VkClearValue        clearValue      = vk::makeClearValueColorVec4(tcu::randomVec4(rnd));
323                 const tcu::Vec4         refValue        (clearValue.color.float32[0], clearValue.color.float32[1], clearValue.color.float32[2], clearValue.color.float32[3]);
324                 const tcu::Vec4         vec0            = tcu::randomVec4(rnd);
325                 const tcu::Vec4         vec1            = tcu::randomVec4(rnd);
326                 const tcu::Vec4         vec2            = tcu::randomVec4(rnd);
327                 const tcu::Vec4         vec3            = tcu::randomVec4(rnd);
328
329                 ValidationData          data            =
330                 {
331                         { vec0, vec1, vec2, vec3 },
332                         { refValue, refValue, refValue, refValue }
333                 };
334                 clearRandomTests->addChild(new ClearColorImageTestCase(testCtx, name.c_str(), clearValue.color, data, cmdBufferType));
335         }
336
337         std::string groupName = getCmdBufferTypeStr(cmdBufferType);
338         std::string groupDesc = "Clear Color Image Tests with " + groupName + " command buffer";
339         de::MovePtr<tcu::TestCaseGroup> clearTests (new tcu::TestCaseGroup(testCtx, groupName.c_str(), groupDesc.c_str()));
340         clearTests->addChild(clearStaticTests.release());
341         clearTests->addChild(clearRandomTests.release());
342         return clearTests.release();
343 }
344
345 } // anonymous
346
347 tcu::TestCaseGroup*     createClearColorImageTests (tcu::TestContext& testCtx)
348 {
349         de::MovePtr<tcu::TestCaseGroup> clearTests (new tcu::TestCaseGroup(testCtx, "clear_color", "Clear Color Image Tests"));
350
351         clearTests->addChild(createClearColorImageTests(testCtx, CMD_BUFFER_PRIMARY));
352         clearTests->addChild(createClearColorImageTests(testCtx, CMD_BUFFER_SECONDARY));
353
354         return clearTests.release();
355 }
356
357 } // ProtectedMem
358 } // vkt