Make random number usage platform independent
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / protected_memory / vktProtectedMemAttachmentClearTests.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 memory attachment clear tests
23  *//*--------------------------------------------------------------------*/
24
25 #include "vktProtectedMemAttachmentClearTests.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 AttachmentClearTestInstance : public ProtectedTestInstance
57 {
58 public:
59                                                                 AttachmentClearTestInstance     (Context&                                       ctx,
60                                                                                                                          const vk::VkClearValue&        clearValue,
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::VkClearValue&         m_clearValue;
69         const ValidationData&           m_refData;
70         const ImageValidator&           m_validator;
71         const CmdBufferType                     m_cmdBufferType;
72 };
73
74
75 class AttachmentClearTestCase : public TestCase
76 {
77 public:
78                                                         AttachmentClearTestCase         (tcu::TestContext&              testCtx,
79                                                                                                                  const std::string&             name,
80                                                                                                                  vk::VkClearValue               clearValue,
81                                                                                                                  ValidationData                 data,
82                                                                                                                  CmdBufferType                  cmdBufferType)
83                                                                 : TestCase                      (testCtx, name, "Clear attachment.")
84                                                                 , m_clearValue          (clearValue)
85                                                                 , m_refData                     (data)
86                                                                 , m_cmdBufferType       (cmdBufferType)
87                                                         {
88                                                         }
89
90         virtual                                 ~AttachmentClearTestCase        (void) {}
91         virtual TestInstance*   createInstance                          (Context& ctx) const
92                                                         {
93                                                                 return new AttachmentClearTestInstance(ctx, m_clearValue, m_refData, m_validator, m_cmdBufferType);
94                                                         }
95         virtual void                    initPrograms                            (vk::SourceCollections& programCollection) const
96                                                         {
97                                                                 m_validator.initPrograms(programCollection);
98                                                         }
99 private:
100         vk::VkClearValue                m_clearValue;
101         ValidationData                  m_refData;
102         ImageValidator                  m_validator;
103         CmdBufferType                   m_cmdBufferType;
104 };
105
106 AttachmentClearTestInstance::AttachmentClearTestInstance        (Context&                                       ctx,
107                                                                                                                          const vk::VkClearValue&        clearValue,
108                                                                                                                          const ValidationData&          refData,
109                                                                                                                          const ImageValidator&          validator,
110                                                                                                                          const CmdBufferType            cmdBufferType)
111         : ProtectedTestInstance (ctx)
112         , m_imageFormat                 (vk::VK_FORMAT_R8G8B8A8_UNORM)
113         , m_clearValue                  (clearValue)
114         , m_refData                             (refData)
115         , m_validator                   (validator)
116         , m_cmdBufferType               (cmdBufferType)
117 {
118 }
119
120 tcu::TestStatus AttachmentClearTestInstance::iterate()
121 {
122         ProtectedContext&                                       ctx                                     (m_protectedContext);
123         const vk::DeviceInterface&                      vk                                      = ctx.getDeviceInterface();
124         const vk::VkDevice                                      device                          = ctx.getDevice();
125         const vk::VkQueue                                       queue                           = ctx.getQueue();
126         const deUint32                                          queueFamilyIndex        = ctx.getQueueFamilyIndex();
127
128         // Create output image
129         de::MovePtr<vk::ImageWithMemory>        colorImage                      (createImage2D(ctx, PROTECTION_ENABLED, queueFamilyIndex,
130                                                                                                                                                         RENDER_WIDTH, RENDER_HEIGHT,
131                                                                                                                                                         m_imageFormat,
132                                                                                                                                                         vk::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT|vk::VK_IMAGE_USAGE_SAMPLED_BIT));
133         vk::Unique<vk::VkImageView>                     colorImageView          (createImageView(ctx, **colorImage, m_imageFormat));
134
135         vk::Unique<vk::VkRenderPass>            renderPass                      (createRenderPass(ctx, m_imageFormat));
136         vk::Unique<vk::VkFramebuffer>           framebuffer                     (createFramebuffer(ctx, RENDER_WIDTH, RENDER_HEIGHT, *renderPass, *colorImageView));
137         vk::Unique<vk::VkPipelineLayout>        pipelineLayout          (createPipelineLayout(ctx, 0u, DE_NULL));
138
139         vk::Unique<vk::VkCommandPool>           cmdPool                         (makeCommandPool(vk, device, PROTECTION_ENABLED, queueFamilyIndex));
140         vk::Unique<vk::VkCommandBuffer>         cmdBuffer                       (vk::allocateCommandBuffer(vk, device, *cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY));
141         vk::Unique<vk::VkCommandBuffer>         secondaryCmdBuffer      (vk::allocateCommandBuffer(vk, device, *cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_SECONDARY));
142         vk::VkCommandBuffer                                     targetCmdBuffer         = (m_cmdBufferType == CMD_BUFFER_SECONDARY) ? *secondaryCmdBuffer : *cmdBuffer;
143
144         // Begin cmd buffer
145         beginCommandBuffer(vk, *cmdBuffer);
146
147         // Start image barrier
148         {
149                 const vk::VkImageMemoryBarrier  startImgBarrier         =
150                 {
151                         vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                     // sType
152                         DE_NULL,                                                                                        // pNext
153                         0,                                                                                                      // srcAccessMask
154                         vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,                       // dstAccessMask
155                         vk::VK_IMAGE_LAYOUT_UNDEFINED,                                          // oldLayout
156                         vk::VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,           // newLayout
157                         queueFamilyIndex,                                                                       // srcQueueFamilyIndex
158                         queueFamilyIndex,                                                                       // dstQueueFamilyIndex
159                         **colorImage,                                                                           // image
160                         {
161                                 vk::VK_IMAGE_ASPECT_COLOR_BIT,                                  // aspectMask
162                                 0u,                                                                                             // baseMipLevel
163                                 1u,                                                                                             // mipLevels
164                                 0u,                                                                                             // baseArraySlice
165                                 1u,                                                                                             // subresourceRange
166                         }
167                 };
168
169                 vk.cmdPipelineBarrier(*cmdBuffer,
170                                                           vk::VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
171                                                           vk::VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
172                                                           (vk::VkDependencyFlags)0,
173                                                           0, (const vk::VkMemoryBarrier*)DE_NULL,
174                                                           0, (const vk::VkBufferMemoryBarrier*)DE_NULL,
175                                                           1, &startImgBarrier);
176         }
177
178         // Image clear to different from input color
179
180         const tcu::Vec4                                         clearValue                      (m_clearValue.color.float32[0] < 0.5f ? 1.0f : 0.0f,
181                                                                                                                          m_clearValue.color.float32[1] < 0.5f ? 1.0f : 0.0f,
182                                                                                                                          m_clearValue.color.float32[2] < 0.5f ? 1.0f : 0.0f,
183                                                                                                                          m_clearValue.color.float32[3] < 0.5f ? 1.0f : 0.0f);
184
185         const vk::VkSubpassContents                     subpassContents         = m_cmdBufferType == CMD_BUFFER_SECONDARY
186                                                                                                                           ? vk::VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS
187                                                                                                                           : vk::VK_SUBPASS_CONTENTS_INLINE;
188         beginRenderPass(vk, *cmdBuffer, *renderPass, *framebuffer, vk::makeRect2D(RENDER_WIDTH, RENDER_HEIGHT), clearValue, subpassContents);
189
190         if (m_cmdBufferType == CMD_BUFFER_SECONDARY)
191         {
192                 // Begin secondary command buffer
193                 const vk::VkCommandBufferInheritanceInfo        bufferInheritanceInfo   =
194                 {
195                         vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,          // sType
196                         DE_NULL,                                                                                                        // pNext
197                         *renderPass,                                                                                            // renderPass
198                         0u,                                                                                                                     // subpass
199                         *framebuffer,                                                                                           // framebuffer
200                         VK_FALSE,                                                                                                       // occlusionQueryEnable
201                         (vk::VkQueryControlFlags)0u,                                                            // queryFlags
202                         (vk::VkQueryPipelineStatisticFlags)0u,                                          // pipelineStatistics
203                 };
204                 beginSecondaryCommandBuffer(vk, *secondaryCmdBuffer, bufferInheritanceInfo);
205         }
206
207         {
208                 const vk::VkClearAttachment             pAttachments            =
209                 {
210                         vk::VK_IMAGE_ASPECT_COLOR_BIT,                                          //VkImageAspectFlags    aspectMask
211                         0u,                                                                                                     //uint32_t                              colorAttachment
212                         m_clearValue                                                                            // VkClearValue                 clearValue;
213                 };
214                 const vk::VkRect2D                              rect2D                          = vk::makeRect2D(RENDER_WIDTH, RENDER_HEIGHT);
215                 const vk::VkClearRect                   clearRect                       =
216                 {
217                         rect2D,                                                                                         // VkRect2D                             rect;
218                         0u,                                                                                                     // deUint32                             baseArrayLayer;
219                         1u                                                                                                      // deUint32                             layerCount;
220                 };
221                 vk.cmdClearAttachments(targetCmdBuffer, 1u, &pAttachments, 1u, &clearRect);
222         }
223
224         if (m_cmdBufferType == CMD_BUFFER_SECONDARY)
225         {
226                 endCommandBuffer(vk, *secondaryCmdBuffer);
227                 vk.cmdExecuteCommands(*cmdBuffer, 1u, &secondaryCmdBuffer.get());
228         }
229
230         endRenderPass(vk, *cmdBuffer);
231
232         {
233                 // Image validator reads image in compute shader
234                 const vk::VkImageMemoryBarrier  endImgBarrier           =
235                 {
236                         vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                     // sType
237                         DE_NULL,                                                                                        // pNext
238                         vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,                       // srcAccessMask
239                         vk::VK_ACCESS_SHADER_READ_BIT,                                          // dstAccessMask
240                         vk::VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,           // oldLayout
241                         vk::VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,           // newLayout
242                         queueFamilyIndex,                                                                       // srcQueueFamilyIndex
243                         queueFamilyIndex,                                                                       // dstQueueFamilyIndex
244                         **colorImage,                                                                           // image
245                         {
246                                 vk::VK_IMAGE_ASPECT_COLOR_BIT,                                  // aspectMask
247                                 0u,                                                                                             // baseMipLevel
248                                 1u,                                                                                             // mipLevels
249                                 0u,                                                                                             // baseArraySlice
250                                 1u,                                                                                             // subresourceRange
251                         }
252                 };
253                 vk.cmdPipelineBarrier(*cmdBuffer,
254                                                           vk::VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
255                                                           vk::VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
256                                                           (vk::VkDependencyFlags)0,
257                                                           0, (const vk::VkMemoryBarrier*)DE_NULL,
258                                                           0, (const vk::VkBufferMemoryBarrier*)DE_NULL,
259                                                           1, &endImgBarrier);
260         }
261
262         endCommandBuffer(vk, *cmdBuffer);
263
264         // Submit command buffer
265         const vk::Unique<vk::VkFence>   fence           (vk::createFence(vk, device));
266         VK_CHECK(queueSubmit(ctx, PROTECTION_ENABLED, queue, *cmdBuffer, *fence, ~0ull));
267
268         // Log out test data
269         ctx.getTestContext().getLog()
270                 << tcu::TestLog::Message << "Color clear value: " << tcu::Vec4(m_clearValue.color.float32) << tcu::TestLog::EndMessage
271                 << tcu::TestLog::Message << "Depth clear value: " << m_clearValue.depthStencil.depth << tcu::TestLog::EndMessage
272                 << tcu::TestLog::Message << "Stencil clear value: " << m_clearValue.depthStencil.stencil << tcu::TestLog::EndMessage;
273
274         // Validate resulting image
275         if (m_validator.validateImage(ctx, m_refData, **colorImage, m_imageFormat, vk::VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL))
276                 return tcu::TestStatus::pass("Everything went OK");
277         else
278                 return tcu::TestStatus::fail("Something went really wrong");
279 }
280
281 tcu::TestCaseGroup*     createAttachmentClearTests (tcu::TestContext& testCtx, CmdBufferType cmdBufferType)
282 {
283         struct {
284                 const vk::VkClearValue          clearValue;
285                 const ValidationData            data;
286         } testData[] = {
287                 {       vk::makeClearValueColorF32(1.0f, 0.0f, 0.0f, 1.0f),
288                         {
289                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
290                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
291                                 { tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f),
292                                   tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), }
293                         }
294                 },
295                 {       vk::makeClearValueColorF32(0.0f, 1.0f, 0.0f, 1.0f),
296                         {
297                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
298                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
299                                 { tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f), tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f),
300                                   tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f), tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f), }
301                         }
302                 },
303                 {       vk::makeClearValueColorF32(0.0f, 0.0f, 1.0f, 1.0f),
304                         {
305                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
306                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
307                                 { tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f),
308                                   tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), }
309                         }
310                 },
311                 {       vk::makeClearValueColorF32(0.0f, 0.0f, 0.0f, 1.0f),
312                         {
313                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
314                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
315                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f),
316                                   tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), }
317                         }
318                 },
319                 {       vk::makeClearValueColorF32(1.0f, 0.0f, 0.0f, 1.0f),
320                         {
321                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
322                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
323                                 { tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f),
324                                   tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), }
325                         }
326                 },
327                 {       vk::makeClearValueColorF32(1.0f, 0.0f, 0.0f, 0.0f),
328                         {
329                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
330                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
331                                 { tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f),
332                                   tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 0.0f, 0.0f, 0.0f), }
333                         }
334                 },
335                 {       vk::makeClearValueColorF32(0.1f, 0.2f, 0.3f, 0.0f),
336                         {
337                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.0f, 0.0f),
338                                   tcu::Vec4(0.1f, 0.1f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.0f, 0.0f), },
339                                 { tcu::Vec4(0.1f, 0.2f, 0.3f, 0.0f), tcu::Vec4(0.1f, 0.2f, 0.3f, 0.0f),
340                                   tcu::Vec4(0.1f, 0.2f, 0.3f, 0.0f), tcu::Vec4(0.1f, 0.2f, 0.3f, 0.0f), }
341                         }
342                 },
343         };
344
345         de::MovePtr<tcu::TestCaseGroup> clearStaticTests        (new tcu::TestCaseGroup(testCtx, "static", "Attachment Clear Op Tests with static input"));
346
347         for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(testData); ++ndx)
348         {
349                 const std::string name = "clear_" +  de::toString(ndx + 1);
350                 clearStaticTests->addChild(new AttachmentClearTestCase(testCtx, name.c_str(), testData[ndx].clearValue, testData[ndx].data, cmdBufferType));
351         }
352
353         /* Add a few randomized tests */
354         de::MovePtr<tcu::TestCaseGroup> clearRandomTests        (new tcu::TestCaseGroup(testCtx, "random", "Attachment Clear Op Tests with random input"));
355         const int                                               testCount                       = 10;
356         de::Random                                              rnd                                     (testCtx.getCommandLine().getBaseSeed());
357         for (int ndx = 0; ndx < testCount; ++ndx)
358         {
359                 const std::string       name            = "clear_" +  de::toString(ndx + 1);
360                 vk::VkClearValue        clearValue      = vk::makeClearValueColorVec4(tcu::randomVec4(rnd));
361                 const tcu::Vec4         refValue        (clearValue.color.float32[0], clearValue.color.float32[1], clearValue.color.float32[2], clearValue.color.float32[3]);
362                 const tcu::Vec4         vec0            = tcu::randomVec4(rnd);
363                 const tcu::Vec4         vec1            = tcu::randomVec4(rnd);
364                 const tcu::Vec4         vec2            = tcu::randomVec4(rnd);
365                 const tcu::Vec4         vec3            = tcu::randomVec4(rnd);
366
367                 ValidationData          data            =
368                 {
369                         { vec0, vec1, vec2, vec3 },
370                         { refValue, refValue, refValue, refValue }
371                 };
372
373                 clearRandomTests->addChild(new AttachmentClearTestCase(testCtx, name.c_str(), clearValue, data, cmdBufferType));
374         }
375
376         std::string groupName = getCmdBufferTypeStr(cmdBufferType);
377         std::string groupDesc = "Attachment Clear Op Tests with " + groupName + " command buffer";
378         de::MovePtr<tcu::TestCaseGroup> clearTests (new tcu::TestCaseGroup(testCtx, groupName.c_str(), groupDesc.c_str()));
379         clearTests->addChild(clearStaticTests.release());
380         clearTests->addChild(clearRandomTests.release());
381         return clearTests.release();
382 }
383
384 } // anonymous
385
386 tcu::TestCaseGroup*     createAttachmentClearTests (tcu::TestContext& testCtx)
387 {
388         de::MovePtr<tcu::TestCaseGroup> clearTests (new tcu::TestCaseGroup(testCtx, "clear_op", "Attachment Clear Op Tests"));
389
390         clearTests->addChild(createAttachmentClearTests(testCtx, CMD_BUFFER_PRIMARY));
391         clearTests->addChild(createAttachmentClearTests(testCtx, CMD_BUFFER_SECONDARY));
392
393         return clearTests.release();
394 }
395
396 } // ProtectedMem
397 } // vkt