1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
4 *
5 * Copyright (c) 2019 The Khronos Group Inc.
6 * Copyright (c) 2019 The Android Open Source Project
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 Cube image with misaligned baseArrayLayer tests
23 *//*--------------------------------------------------------------------*/
24
25 #include "vktImageMisalignedCubeTests.hpp"
26 #include "vktTestCaseUtil.hpp"
27 #include "vktImageTestsUtil.hpp"
28 #include "vktImageTexture.hpp"
29
30 #include "vkDefs.hpp"
31 #include "vkRef.hpp"
32 #include "vkRefUtil.hpp"
33 #include "vkPlatform.hpp"
34 #include "vkPrograms.hpp"
35 #include "vkMemUtil.hpp"
36 #include "vkBarrierUtil.hpp"
37 #include "vkBuilderUtil.hpp"
38 #include "vkImageUtil.hpp"
39 #include "vkCmdUtil.hpp"
40 #include "vkObjUtil.hpp"
41 #include "vkTypeUtil.hpp"
42
43 #include "deUniquePtr.hpp"
44 #include "deStringUtil.hpp"
45 #include "deMath.h"
46
47 #include <string>
48
49 using namespace vk;
50
51 namespace vkt
52 {
53 namespace image
54 {
55 namespace
56 {
57
makeImageCreateInfo(const tcu::IVec3 & size,const VkFormat format)58 inline VkImageCreateInfo makeImageCreateInfo (const tcu::IVec3& size, const VkFormat format)
59 {
60 const VkImageUsageFlags usage = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
61 const VkImageCreateInfo imageParams =
62 {
63 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // VkStructureType sType;
64 DE_NULL, // const void* pNext;
65 VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT, // VkImageCreateFlags flags;
66 VK_IMAGE_TYPE_2D, // VkImageType imageType;
67 format, // VkFormat format;
68 makeExtent3D(size.x(), size.y(), 1u), // VkExtent3D extent;
69 1u, // deUint32 mipLevels;
70 (deUint32)size.z(), // deUint32 arrayLayers;
71 VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples;
72 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling tiling;
73 usage, // VkImageUsageFlags usage;
74 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
75 0u, // deUint32 queueFamilyIndexCount;
76 DE_NULL, // const deUint32* pQueueFamilyIndices;
77 VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout initialLayout;
78 };
79
80 return imageParams;
81 }
82
fillBuffer(const DeviceInterface & vk,const VkDevice device,const Allocation & alloc,const VkDeviceSize offset,const VkDeviceSize size,const VkFormat format,const tcu::Vec4 & color)83 void fillBuffer (const DeviceInterface& vk, const VkDevice device, const Allocation& alloc, const VkDeviceSize offset, const VkDeviceSize size, const VkFormat format, const tcu::Vec4& color)
84 {
85 const tcu::TextureFormat textureFormat = mapVkFormat(format);
86 const deUint32 colorPixelSize = static_cast<deUint32>(tcu::getPixelSize(textureFormat));
87 tcu::TextureLevel colorPixelBuffer (textureFormat, 1, 1);
88 tcu::PixelBufferAccess colorPixel (colorPixelBuffer);
89
90 colorPixel.setPixel(color, 0, 0);
91
92 const deUint8* src = static_cast<deUint8*>(colorPixel.getDataPtr());
93 deUint8* dstBase = static_cast<deUint8*>(alloc.getHostPtr());
94 deUint8* dst = &dstBase[offset];
95
96 for (deUint32 pixelPos = 0; pixelPos < size; pixelPos += colorPixelSize)
97 deMemcpy(&dst[pixelPos], src, colorPixelSize);
98
99 flushMappedMemoryRange(vk, device, alloc.getMemory(), alloc.getOffset() + offset, size);
100 }
101
makeBufferImageCopy(const vk::VkDeviceSize & bufferOffset,const vk::VkImageSubresourceLayers & imageSubresource,const vk::VkOffset3D & imageOffset,const vk::VkExtent3D & imageExtent)102 VkBufferImageCopy makeBufferImageCopy (const vk::VkDeviceSize& bufferOffset,
103 const vk::VkImageSubresourceLayers& imageSubresource,
104 const vk::VkOffset3D& imageOffset,
105 const vk::VkExtent3D& imageExtent)
106 {
107 const VkBufferImageCopy copyParams =
108 {
109 bufferOffset, // VkDeviceSize bufferOffset;
110 0u, // deUint32 bufferRowLength;
111 0u, // deUint32 bufferImageHeight;
112 imageSubresource, // VkImageSubresourceLayers imageSubresource;
113 imageOffset, // VkOffset3D imageOffset;
114 imageExtent, // VkExtent3D imageExtent;
115 };
116 return copyParams;
117 }
118
119 //! Interpret the memory as IVec4
readVec4(const void * const data,const deUint32 ndx)120 inline tcu::Vec4 readVec4 (const void* const data, const deUint32 ndx)
121 {
122 const float* const p = reinterpret_cast<const float*>(data);
123 const deUint32 ofs = 4 * ndx;
124
125 return tcu::Vec4(p[ofs+0], p[ofs+1], p[ofs+2], p[ofs+3]);
126 }
127
128 class MisalignedCubeTestInstance : public TestInstance
129 {
130 public:
131 MisalignedCubeTestInstance (Context& context,
132 const tcu::IVec3& size,
133 const VkFormat format);
134 tcu::TestStatus iterate (void);
135
136 private:
137 const tcu::IVec3& m_size;
138 const VkFormat m_format;
139 };
140
MisalignedCubeTestInstance(Context & context,const tcu::IVec3 & size,const VkFormat format)141 MisalignedCubeTestInstance::MisalignedCubeTestInstance (Context& context, const tcu::IVec3& size, const VkFormat format)
142 : TestInstance (context)
143 , m_size (size)
144 , m_format (format)
145 {
146 }
147
iterate(void)148 tcu::TestStatus MisalignedCubeTestInstance::iterate (void)
149 {
150 DE_ASSERT(de::inRange(m_size.z(), 6, 16));
151 DE_ASSERT(m_format == VK_FORMAT_R8G8B8A8_UNORM);
152
153 const DeviceInterface& vk = m_context.getDeviceInterface();
154 const VkDevice device = m_context.getDevice();
155 Allocator& allocator = m_context.getDefaultAllocator();
156 const VkQueue queue = m_context.getUniversalQueue();
157 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
158 const deUint32 numLayers = m_size.z();
159 const deUint32 cube0LayerStart = 0;
160 const deUint32 cube1LayerStart = numLayers - 6u;
161 const VkDeviceSize resultBufferSizeBytes = 2 * 6 * 4 * sizeof(float); // vec4[6] in shader
162 const VkExtent3D imageExtent = makeExtent3D(m_size.x(), m_size.y(), 1u);
163 const deUint32 pixelSize = static_cast<deUint32>(tcu::getPixelSize(mapVkFormat(m_format)));
164 const deUint32 layerSize = imageExtent.width * imageExtent.height * pixelSize;
165 const float eps = 1.0f / float(2 * 256);
166
167 const VkBufferCreateInfo resultBufferCreateInfo = makeBufferCreateInfo(resultBufferSizeBytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
168 de::MovePtr<Buffer> resultBuffer = de::MovePtr<Buffer>(new Buffer(vk, device, allocator, resultBufferCreateInfo, MemoryRequirement::HostVisible));
169 const Allocation& resultBufferAlloc = resultBuffer->getAllocation();
170 const VkImageCreateInfo imageCreateInfo = makeImageCreateInfo(m_size, m_format);
171 de::MovePtr<Image> image = de::MovePtr<Image>(new Image(vk, device, allocator, imageCreateInfo, MemoryRequirement::Any));
172 const VkImageSubresourceRange imageSubresourceRange0 = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, cube0LayerStart, 6u);
173 Move<VkImageView> imageView0 = makeImageView(vk, device, image->get(), VK_IMAGE_VIEW_TYPE_CUBE, m_format, imageSubresourceRange0);
174 const VkImageSubresourceRange imageSubresourceRange1 = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, cube1LayerStart, 6u);
175 Move<VkImageView> imageView1 = makeImageView(vk, device, image->get(), VK_IMAGE_VIEW_TYPE_CUBE, m_format, imageSubresourceRange1);
176
177 Move<VkDescriptorSetLayout> descriptorSetLayout = DescriptorSetLayoutBuilder()
178 .addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT)
179 .addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT)
180 .addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT)
181 .build(vk, device);
182 Move<VkDescriptorPool> descriptorPool = DescriptorPoolBuilder()
183 .addType(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE)
184 .addType(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE)
185 .addType(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)
186 .build(vk, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
187 Move<VkDescriptorSet> descriptorSet = makeDescriptorSet(vk, device, *descriptorPool, *descriptorSetLayout);
188 const VkDescriptorImageInfo descriptorImageInfo0 = makeDescriptorImageInfo(DE_NULL, *imageView0, VK_IMAGE_LAYOUT_GENERAL);
189 const VkDescriptorImageInfo descriptorImageInfo1 = makeDescriptorImageInfo(DE_NULL, *imageView1, VK_IMAGE_LAYOUT_GENERAL);
190 const VkDescriptorBufferInfo descriptorBufferInfo = makeDescriptorBufferInfo(resultBuffer->get(), 0ull, resultBufferSizeBytes);
191
192 const Move<VkShaderModule> shaderModule = createShaderModule(vk, device, m_context.getBinaryCollection().get("comp"), 0);
193 const Move<VkPipelineLayout> pipelineLayout = makePipelineLayout(vk, device, *descriptorSetLayout);
194 const Move<VkPipeline> pipeline = makeComputePipeline(vk, device, *pipelineLayout, *shaderModule);
195 const Move<VkCommandPool> cmdPool = createCommandPool(vk, device, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, queueFamilyIndex);
196 const Move<VkCommandBuffer> cmdBuffer = allocateCommandBuffer(vk, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
197
198 const VkDeviceSize clearBufferSize = layerSize * numLayers;
199 const Move<VkBuffer> clearBuffer = makeBuffer(vk, device, clearBufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
200 const de::MovePtr<Allocation> clearBufferAlloc = bindBuffer(vk, device, allocator, *clearBuffer, MemoryRequirement::HostVisible);
201 const VkImageSubresourceRange clearSubresRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, numLayers);
202 const VkImageMemoryBarrier clearBarrier = makeImageMemoryBarrier(0u, VK_ACCESS_TRANSFER_WRITE_BIT,
203 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
204 image->get(), clearSubresRange);
205 const VkImageMemoryBarrier preShaderImageBarrier = makeImageMemoryBarrier(VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT,
206 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL,
207 image->get(), clearSubresRange);
208 const VkBufferMemoryBarrier postShaderBarrier = makeBufferMemoryBarrier(VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT,
209 resultBuffer->get(), 0ull, VK_WHOLE_SIZE);
210 bool result = true;
211
212 DescriptorSetUpdateBuilder()
213 .writeSingle(*descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &descriptorImageInfo0)
214 .writeSingle(*descriptorSet, DescriptorSetUpdateBuilder::Location::binding(1u), VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &descriptorImageInfo1)
215 .writeSingle(*descriptorSet, DescriptorSetUpdateBuilder::Location::binding(2u), VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, &descriptorBufferInfo)
216 .update(vk, device);
217
218 beginCommandBuffer(vk, *cmdBuffer);
219
220 vk.cmdBindPipeline(*cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipeline);
221 vk.cmdBindDescriptorSets(*cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipelineLayout, 0u, 1u, &*descriptorSet, 0u, DE_NULL);
222
223 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, 1u, &clearBarrier);
224
225 // Clear layers with predefined values
226 for (deUint32 layerNdx = 0; layerNdx < numLayers; ++layerNdx)
227 {
228 const float componentValue = float(16 * layerNdx) / 255.0f;
229 const tcu::Vec4 clearColor = tcu::Vec4(componentValue, componentValue, componentValue, 1.0f);
230 const VkDeviceSize bufferOffset = layerNdx * layerSize;
231 const VkImageSubresourceLayers imageSubresource = makeImageSubresourceLayers(VK_IMAGE_ASPECT_COLOR_BIT, 0u, layerNdx, 1u);
232 const VkBufferImageCopy bufferImageCopyRegion = makeBufferImageCopy(bufferOffset, imageSubresource, makeOffset3D(0u, 0u, 0u), imageExtent);
233
234 fillBuffer(vk, device, *clearBufferAlloc, bufferOffset, layerSize, m_format, clearColor);
235
236 vk.cmdCopyBufferToImage(*cmdBuffer, *clearBuffer, image->get(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1u, &bufferImageCopyRegion);
237 }
238
239 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, 1u, &preShaderImageBarrier);
240
241 vk.cmdDispatch(*cmdBuffer, 1, 1, 1);
242
243 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 0, DE_NULL, 1, &postShaderBarrier, 0, DE_NULL);
244
245 endCommandBuffer(vk, *cmdBuffer);
246
247 submitCommandsAndWait(vk, device, queue, *cmdBuffer);
248
249 invalidateAlloc(vk, device, resultBufferAlloc);
250
251 // Check cube 0
252 for (deUint32 layerNdx = 0; layerNdx < 6; ++layerNdx)
253 {
254 const deUint32 layerUsed = cube0LayerStart + layerNdx;
255 const float componentValue = float(16 * layerUsed) / 255.0f;
256 const tcu::Vec4 expectedColor = tcu::Vec4(componentValue, componentValue, componentValue, 1.0f);
257 const tcu::Vec4 resultColor = readVec4(resultBufferAlloc.getHostPtr(), layerNdx);
258 const tcu::Vec4 delta = expectedColor - resultColor;
259
260 if (deFloatAbs(delta.x()) > eps || deFloatAbs(delta.y()) > eps || deFloatAbs(delta.z()) > eps || deFloatAbs(delta.w()) > eps)
261 result = false;
262 }
263
264 // Check cube 1
265 for (deUint32 layerNdx = 0; layerNdx < 6; ++layerNdx)
266 {
267 const deUint32 layerUsed = cube1LayerStart + layerNdx;
268 const float componentValue = float(16 * layerUsed) / 255.0f;
269 const tcu::Vec4 expectedColor = tcu::Vec4(componentValue, componentValue, componentValue, 1.0f);
270 const tcu::Vec4 resultColor = readVec4(resultBufferAlloc.getHostPtr(), layerNdx + 6u);
271 const tcu::Vec4 delta = expectedColor - resultColor;
272
273 if (deFloatAbs(delta.x()) > eps || deFloatAbs(delta.y()) > eps || deFloatAbs(delta.z()) > eps || deFloatAbs(delta.w()) > eps)
274 result = false;
275 }
276
277 if (result)
278 return tcu::TestStatus::pass("pass");
279 else
280 return tcu::TestStatus::fail("fail");
281 }
282
283 class MisalignedCubeTest : public TestCase
284 {
285 public:
286 MisalignedCubeTest (tcu::TestContext& testCtx,
287 const std::string& name,
288 const std::string& description,
289 const tcu::IVec3& size,
290 const VkFormat format);
291
292 void initPrograms (SourceCollections& programCollection) const;
293 TestInstance* createInstance (Context& context) const;
294
295 private:
296 const tcu::IVec3 m_size;
297 const VkFormat m_format;
298 };
299
MisalignedCubeTest(tcu::TestContext & testCtx,const std::string & name,const std::string & description,const tcu::IVec3 & size,const VkFormat format)300 MisalignedCubeTest::MisalignedCubeTest (tcu::TestContext& testCtx,
301 const std::string& name,
302 const std::string& description,
303 const tcu::IVec3& size,
304 const VkFormat format)
305 : TestCase (testCtx, name, description)
306 , m_size (size)
307 , m_format (format)
308 {
309 }
310
initPrograms(SourceCollections & programCollection) const311 void MisalignedCubeTest::initPrograms (SourceCollections& programCollection) const
312 {
313 const std::string formatQualifierStr = getShaderImageFormatQualifier(mapVkFormat(m_format));
314
315 std::ostringstream src;
316 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_440) << "\n"
317 << "\n"
318 << "layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
319 << "layout (binding = 0, " << formatQualifierStr << ") " << "readonly uniform highp imageCube u_cubeImage0;\n"
320 << "layout (binding = 1, " << formatQualifierStr << ") " << "readonly uniform highp imageCube u_cubeImage1;\n"
321 << "layout (binding = 2) writeonly buffer Output\n"
322 << "{\n"
323 << " vec4 cube0_color0;\n"
324 << " vec4 cube0_color1;\n"
325 << " vec4 cube0_color2;\n"
326 << " vec4 cube0_color3;\n"
327 << " vec4 cube0_color4;\n"
328 << " vec4 cube0_color5;\n"
329 << " vec4 cube1_color0;\n"
330 << " vec4 cube1_color1;\n"
331 << " vec4 cube1_color2;\n"
332 << " vec4 cube1_color3;\n"
333 << " vec4 cube1_color4;\n"
334 << " vec4 cube1_color5;\n"
335 << "} sb_out;\n"
336 << "\n"
337 << "void main (void)\n"
338 << "{\n"
339 << " sb_out.cube0_color0 = imageLoad(u_cubeImage0, ivec3(1, 1, 0));\n"
340 << " sb_out.cube0_color1 = imageLoad(u_cubeImage0, ivec3(1, 1, 1));\n"
341 << " sb_out.cube0_color2 = imageLoad(u_cubeImage0, ivec3(1, 1, 2));\n"
342 << " sb_out.cube0_color3 = imageLoad(u_cubeImage0, ivec3(1, 1, 3));\n"
343 << " sb_out.cube0_color4 = imageLoad(u_cubeImage0, ivec3(1, 1, 4));\n"
344 << " sb_out.cube0_color5 = imageLoad(u_cubeImage0, ivec3(1, 1, 5));\n"
345 << " sb_out.cube1_color0 = imageLoad(u_cubeImage1, ivec3(1, 1, 0));\n"
346 << " sb_out.cube1_color1 = imageLoad(u_cubeImage1, ivec3(1, 1, 1));\n"
347 << " sb_out.cube1_color2 = imageLoad(u_cubeImage1, ivec3(1, 1, 2));\n"
348 << " sb_out.cube1_color3 = imageLoad(u_cubeImage1, ivec3(1, 1, 3));\n"
349 << " sb_out.cube1_color4 = imageLoad(u_cubeImage1, ivec3(1, 1, 4));\n"
350 << " sb_out.cube1_color5 = imageLoad(u_cubeImage1, ivec3(1, 1, 5));\n"
351 << "}\n";
352
353 programCollection.glslSources.add("comp") << glu::ComputeSource(src.str());
354 }
355
createInstance(Context & context) const356 TestInstance* MisalignedCubeTest::createInstance (Context& context) const
357 {
358 return new MisalignedCubeTestInstance(context, m_size, m_format);
359 }
360
361 //! Base sizes used to generate actual imager sizes in the test.
362 static const tcu::IVec3 s_baseImageSizes[] =
363 {
364 tcu::IVec3(16, 16, 7),
365 tcu::IVec3(16, 16, 8),
366 tcu::IVec3(16, 16, 9),
367 tcu::IVec3(16, 16, 10),
368 tcu::IVec3(16, 16, 11),
369 };
370
371 } // anonymous ns
372
createMisalignedCubeTests(tcu::TestContext & testCtx)373 tcu::TestCaseGroup* createMisalignedCubeTests (tcu::TestContext& testCtx)
374 {
375 de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "misaligned_cube", "Cube image with misaligned baseArrayLayer test cases"));
376
377 const VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
378
379 for (int imageSizeNdx = 0; imageSizeNdx < DE_LENGTH_OF_ARRAY(s_baseImageSizes); ++imageSizeNdx)
380 {
381 const tcu::IVec3 size = s_baseImageSizes[imageSizeNdx];
382
383 testGroup->addChild(new MisalignedCubeTest(testCtx, de::toString(size.z()), "", size, format));
384 }
385
386 return testGroup.release();
387 }
388
389 } // image
390 } // vkt
391