1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2015 Mobica Ltd.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and/or associated documentation files (the
9 * "Materials"), to deal in the Materials without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Materials, and to
12 * permit persons to whom the Materials are furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice(s) and this permission notice shall be included
16 * in all copies or substantial portions of the Materials.
18 * The Materials are Confidential Information as defined by the
19 * Khronos Membership Agreement until designated non-confidential by Khronos,
20 * at which point this condition clause shall be removed.
22 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
32 * \brief Image load/store Tests
33 *//*--------------------------------------------------------------------*/
35 #include "vktImageLoadStoreTests.hpp"
36 #include "vktTestCaseUtil.hpp"
37 #include "vktImageTestsUtil.hpp"
38 #include "vktImageTexture.hpp"
42 #include "vkRefUtil.hpp"
43 #include "vkPlatform.hpp"
44 #include "vkPrograms.hpp"
45 #include "vkMemUtil.hpp"
46 #include "vkBuilderUtil.hpp"
47 #include "vkQueryUtil.hpp"
48 #include "vkImageUtil.hpp"
50 #include "deUniquePtr.hpp"
51 #include "deStringUtil.hpp"
53 #include "tcuImageCompare.hpp"
54 #include "tcuTexture.hpp"
55 #include "tcuTextureUtil.hpp"
56 #include "tcuFloat.hpp"
70 inline VkImageCreateInfo makeImageCreateInfo (const Texture& texture, const VkFormat format, const VkImageUsageFlags usage)
72 const VkImageCreateInfo imageParams =
74 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // VkStructureType sType;
75 DE_NULL, // const void* pNext;
76 (isCube(texture) ? (VkImageCreateFlags)VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT : 0u), // VkImageCreateFlags flags;
77 mapImageType(texture.type()), // VkImageType imageType;
78 format, // VkFormat format;
79 makeExtent3D(texture.layerSize()), // VkExtent3D extent;
80 1u, // deUint32 mipLevels;
81 (deUint32)texture.numLayers(), // deUint32 arrayLayers;
82 VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples;
83 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling tiling;
84 usage, // VkImageUsageFlags usage;
85 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
86 0u, // deUint32 queueFamilyIndexCount;
87 DE_NULL, // const deUint32* pQueueFamilyIndices;
88 VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout initialLayout;
93 inline VkBufferImageCopy makeBufferImageCopy (const Texture& texture)
95 return image::makeBufferImageCopy(makeExtent3D(texture.layerSize()), texture.numLayers());
98 ImageType getImageTypeForSingleLayer (const ImageType imageType)
103 case IMAGE_TYPE_1D_ARRAY:
104 return IMAGE_TYPE_1D;
107 case IMAGE_TYPE_2D_ARRAY:
108 case IMAGE_TYPE_CUBE:
109 case IMAGE_TYPE_CUBE_ARRAY:
110 // A single layer for cube is a 2d face
111 return IMAGE_TYPE_2D;
114 return IMAGE_TYPE_3D;
116 case IMAGE_TYPE_BUFFER:
117 return IMAGE_TYPE_BUFFER;
120 DE_FATAL("Internal test error");
121 return IMAGE_TYPE_LAST;
125 float computeStoreColorScale (const VkFormat format, const tcu::IVec3 imageSize)
127 const int maxImageDimension = de::max(imageSize.x(), de::max(imageSize.y(), imageSize.z()));
128 const float div = static_cast<float>(maxImageDimension - 1);
130 if (isUnormFormat(format))
132 else if (isSnormFormat(format))
138 inline float computeStoreColorBias (const VkFormat format)
140 return isSnormFormat(format) ? -1.0f : 0.0f;
143 inline bool isIntegerFormat (const VkFormat format)
145 return isIntFormat(format) || isUintFormat(format);
148 tcu::ConstPixelBufferAccess getLayerOrSlice (const Texture& texture, const tcu::ConstPixelBufferAccess access, const int layer)
150 switch (texture.type())
154 case IMAGE_TYPE_BUFFER:
156 DE_ASSERT(layer == 0);
159 case IMAGE_TYPE_1D_ARRAY:
160 return tcu::getSubregion(access, 0, layer, access.getWidth(), 1);
162 case IMAGE_TYPE_2D_ARRAY:
163 case IMAGE_TYPE_CUBE:
164 case IMAGE_TYPE_CUBE_ARRAY:
165 case IMAGE_TYPE_3D: // 3d texture is treated as if depth was the layers
166 return tcu::getSubregion(access, 0, 0, layer, access.getWidth(), access.getHeight(), 1);
169 DE_FATAL("Internal test error");
170 return tcu::ConstPixelBufferAccess();
174 std::string getFormatCaseName (const VkFormat format)
176 const std::string fullName = getFormatName(format);
178 DE_ASSERT(de::beginsWith(fullName, "VK_FORMAT_"));
180 return de::toLower(fullName.substr(10));
183 //! \return true if all layers match in both pixel buffers
184 bool comparePixelBuffers (tcu::TestLog& log,
185 const Texture& texture,
186 const VkFormat format,
187 const tcu::ConstPixelBufferAccess reference,
188 const tcu::ConstPixelBufferAccess result)
190 DE_ASSERT(reference.getFormat() == result.getFormat());
191 DE_ASSERT(reference.getSize() == result.getSize());
193 const bool intFormat = isIntegerFormat(format);
194 const bool is3d = (texture.type() == IMAGE_TYPE_3D);
195 const int numLayersOrSlices = (is3d ? texture.size().z() : texture.numLayers());
196 const int numCubeFaces = 6;
198 int passedLayers = 0;
199 for (int layerNdx = 0; layerNdx < numLayersOrSlices; ++layerNdx)
201 const std::string comparisonName = "Comparison" + de::toString(layerNdx);
202 const std::string comparisonDesc = "Image Comparison, " +
203 (isCube(texture) ? "face " + de::toString(layerNdx % numCubeFaces) + ", cube " + de::toString(layerNdx / numCubeFaces) :
204 is3d ? "slice " + de::toString(layerNdx) : "layer " + de::toString(layerNdx));
206 const tcu::ConstPixelBufferAccess refLayer = getLayerOrSlice(texture, reference, layerNdx);
207 const tcu::ConstPixelBufferAccess resultLayer = getLayerOrSlice(texture, result, layerNdx);
211 ok = tcu::intThresholdCompare(log, comparisonName.c_str(), comparisonDesc.c_str(), refLayer, resultLayer, tcu::UVec4(0), tcu::COMPARE_LOG_RESULT);
213 ok = tcu::floatThresholdCompare(log, comparisonName.c_str(), comparisonDesc.c_str(), refLayer, resultLayer, tcu::Vec4(0.01f), tcu::COMPARE_LOG_RESULT);
218 return passedLayers == numLayersOrSlices;
221 //!< Zero out invalid pixels in the image (denormalized, infinite, NaN values)
222 void replaceBadFloatReinterpretValues (const tcu::PixelBufferAccess access)
224 DE_ASSERT(tcu::getTextureChannelClass(access.getFormat().type) == tcu::TEXTURECHANNELCLASS_FLOATING_POINT);
226 for (int z = 0; z < access.getDepth(); ++z)
227 for (int y = 0; y < access.getHeight(); ++y)
228 for (int x = 0; x < access.getWidth(); ++x)
230 const tcu::Vec4 color(access.getPixel(x, y, z));
231 tcu::Vec4 newColor = color;
233 for (int i = 0; i < 4; ++i)
235 const tcu::Float32 f(color[i]);
236 if (f.isDenorm() || f.isInf() || f.isNaN())
240 if (newColor != color)
241 access.setPixel(newColor, x, y, z);
245 //!< replace invalid pixels in the image (-128)
246 void replaceSnormReinterpretValues (const tcu::PixelBufferAccess access)
248 DE_ASSERT(tcu::getTextureChannelClass(access.getFormat().type) == tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT);
250 for (int z = 0; z < access.getDepth(); ++z)
251 for (int y = 0; y < access.getHeight(); ++y)
252 for (int x = 0; x < access.getWidth(); ++x)
254 const tcu::IVec4 color(access.getPixelInt(x, y, z));
255 tcu::IVec4 newColor = color;
257 for (int i = 0; i < 4; ++i)
259 const deInt32 oldColor(color[i]);
260 if (oldColor == -128) newColor[i] = -127;
263 if (newColor != color)
264 access.setPixel(newColor, x, y, z);
268 tcu::TextureLevel generateReferenceImage (const tcu::IVec3& imageSize, const VkFormat imageFormat, const VkFormat readFormat)
270 // Generate a reference image data using the storage format
272 tcu::TextureLevel reference(mapVkFormat(imageFormat), imageSize.x(), imageSize.y(), imageSize.z());
273 const tcu::PixelBufferAccess access = reference.getAccess();
275 const float storeColorScale = computeStoreColorScale(imageFormat, imageSize);
276 const float storeColorBias = computeStoreColorBias(imageFormat);
278 const bool intFormat = isIntegerFormat(imageFormat);
279 const int xMax = imageSize.x() - 1;
280 const int yMax = imageSize.y() - 1;
282 for (int z = 0; z < imageSize.z(); ++z)
283 for (int y = 0; y < imageSize.y(); ++y)
284 for (int x = 0; x < imageSize.x(); ++x)
286 const tcu::IVec4 color(x^y^z, (xMax - x)^y^z, x^(yMax - y)^z, (xMax - x)^(yMax - y)^z);
289 access.setPixel(color, x, y, z);
291 access.setPixel(color.asFloat()*storeColorScale + storeColorBias, x, y, z);
294 // If the image is to be accessed as a float texture, get rid of invalid values
296 if (isFloatFormat(readFormat) && imageFormat != readFormat)
297 replaceBadFloatReinterpretValues(tcu::PixelBufferAccess(mapVkFormat(readFormat), imageSize, access.getDataPtr()));
298 if (isSnormFormat(readFormat) && imageFormat != readFormat)
299 replaceSnormReinterpretValues(tcu::PixelBufferAccess(mapVkFormat(readFormat), imageSize, access.getDataPtr()));
304 inline tcu::TextureLevel generateReferenceImage (const tcu::IVec3& imageSize, const VkFormat imageFormat)
306 return generateReferenceImage(imageSize, imageFormat, imageFormat);
309 void flipHorizontally (const tcu::PixelBufferAccess access)
311 const int xMax = access.getWidth() - 1;
312 const int halfWidth = access.getWidth() / 2;
314 if (isIntegerFormat(mapTextureFormat(access.getFormat())))
315 for (int z = 0; z < access.getDepth(); z++)
316 for (int y = 0; y < access.getHeight(); y++)
317 for (int x = 0; x < halfWidth; x++)
319 const tcu::UVec4 temp = access.getPixelUint(xMax - x, y, z);
320 access.setPixel(access.getPixelUint(x, y, z), xMax - x, y, z);
321 access.setPixel(temp, x, y, z);
324 for (int z = 0; z < access.getDepth(); z++)
325 for (int y = 0; y < access.getHeight(); y++)
326 for (int x = 0; x < halfWidth; x++)
328 const tcu::Vec4 temp = access.getPixel(xMax - x, y, z);
329 access.setPixel(access.getPixel(x, y, z), xMax - x, y, z);
330 access.setPixel(temp, x, y, z);
334 #if defined(DE_DEBUG)
335 inline bool colorScaleAndBiasAreValid (const VkFormat format, const float colorScale, const float colorBias)
337 // Only normalized (fixed-point) formats may have scale/bias
338 const bool integerOrFloatFormat = isIntFormat(format) || isUintFormat(format) || isFloatFormat(format);
339 return !integerOrFloatFormat || (colorScale == 1.0f && colorBias == 0.0f);
343 inline bool formatsAreCompatible (const VkFormat format0, const VkFormat format1)
345 return format0 == format1 || mapVkFormat(format0).getPixelSize() == mapVkFormat(format1).getPixelSize();
348 void commandImageWriteBarrierBetweenShaderInvocations (Context& context, const VkCommandBuffer cmdBuffer, const VkImage image, const Texture& texture)
350 const DeviceInterface& vk = context.getDeviceInterface();
352 const VkImageSubresourceRange fullImageSubresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, texture.numLayers());
353 const VkImageMemoryBarrier shaderWriteBarrier = makeImageMemoryBarrier(
354 VK_ACCESS_SHADER_WRITE_BIT, 0u,
355 VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL,
356 image, fullImageSubresourceRange);
358 const void* const barriers[] = { &shaderWriteBarrier };
360 vk.cmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, DE_FALSE, DE_LENGTH_OF_ARRAY(barriers), barriers);
363 void commandBufferWriteBarrierBeforeHostRead (Context& context, const VkCommandBuffer cmdBuffer, const VkBuffer buffer, const VkDeviceSize bufferSizeBytes)
365 const DeviceInterface& vk = context.getDeviceInterface();
367 const VkBufferMemoryBarrier shaderWriteBarrier = makeBufferMemoryBarrier(
368 VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT,
369 buffer, 0ull, bufferSizeBytes);
371 const void* const barriers[] = { &shaderWriteBarrier };
373 vk.cmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_HOST_BIT, DE_FALSE, DE_LENGTH_OF_ARRAY(barriers), barriers);
376 //! Copy all layers of an image to a buffer.
377 void commandCopyImageToBuffer (Context& context,
378 const VkCommandBuffer cmdBuffer,
380 const VkBuffer buffer,
381 const VkDeviceSize bufferSizeBytes,
382 const Texture& texture)
384 const DeviceInterface& vk = context.getDeviceInterface();
386 const VkImageSubresourceRange fullImageSubresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, texture.numLayers());
387 const VkImageMemoryBarrier prepareForTransferBarrier = makeImageMemoryBarrier(
388 VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
389 VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
390 image, fullImageSubresourceRange);
392 const void* const barriersBeforeCopy[] = { &prepareForTransferBarrier };
394 const VkBufferImageCopy copyRegion = makeBufferImageCopy(texture);
396 const VkBufferMemoryBarrier copyBarrier = makeBufferMemoryBarrier(
397 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT,
398 buffer, 0ull, bufferSizeBytes);
400 const void* const barriersAfterCopy[] = { ©Barrier };
402 vk.cmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, DE_FALSE, DE_LENGTH_OF_ARRAY(barriersBeforeCopy), barriersBeforeCopy);
403 vk.cmdCopyImageToBuffer(cmdBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buffer, 1u, ©Region);
404 vk.cmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, DE_FALSE, DE_LENGTH_OF_ARRAY(barriersAfterCopy), barriersAfterCopy);
407 //! Minimum chunk size is determined by the offset alignment requirements.
408 VkDeviceSize getOptimalUniformBufferChunkSize (Context& context, VkDeviceSize minimumRequiredChunkSizeBytes)
410 const VkPhysicalDeviceProperties properties = getPhysicalDeviceProperties(context.getInstanceInterface(), context.getPhysicalDevice());
411 const VkDeviceSize alignment = properties.limits.minUniformBufferOffsetAlignment;
413 if (minimumRequiredChunkSizeBytes > alignment)
414 return alignment + (minimumRequiredChunkSizeBytes / alignment) * alignment;
419 class StoreTest : public TestCase
424 FLAG_SINGLE_LAYER_BIND = 0x1, //!< Run the shader multiple times, each time binding a different layer.
427 StoreTest (tcu::TestContext& testCtx,
428 const std::string& name,
429 const std::string& description,
430 const Texture& texture,
431 const VkFormat format,
432 const TestFlags flags = static_cast<TestFlags>(0));
434 void initPrograms (SourceCollections& programCollection) const;
436 TestInstance* createInstance (Context& context) const;
439 const Texture m_texture;
440 const VkFormat m_format;
441 const bool m_singleLayerBind;
444 StoreTest::StoreTest (tcu::TestContext& testCtx,
445 const std::string& name,
446 const std::string& description,
447 const Texture& texture,
448 const VkFormat format,
449 const TestFlags flags)
450 : TestCase (testCtx, name, description)
451 , m_texture (texture)
453 , m_singleLayerBind ((flags & FLAG_SINGLE_LAYER_BIND) != 0)
455 if (m_singleLayerBind)
456 DE_ASSERT(m_texture.numLayers() > 1);
459 void StoreTest::initPrograms (SourceCollections& programCollection) const
461 const float storeColorScale = computeStoreColorScale(m_format, m_texture.size());
462 const float storeColorBias = computeStoreColorBias(m_format);
463 DE_ASSERT(colorScaleAndBiasAreValid(m_format, storeColorScale, storeColorBias));
465 const std::string xMax = de::toString(m_texture.size().x() - 1);
466 const std::string yMax = de::toString(m_texture.size().y() - 1);
467 const std::string signednessPrefix = isUintFormat(m_format) ? "u" : isIntFormat(m_format) ? "i" : "";
468 const std::string colorBaseExpr = signednessPrefix + "vec4("
470 + "(" + xMax + "-gx)^gy^gz, "
471 + "gx^(" + yMax + "-gy)^gz, "
472 + "(" + xMax + "-gx)^(" + yMax + "-gy)^gz)";
474 const std::string colorExpr = colorBaseExpr + (storeColorScale == 1.0f ? "" : "*" + de::toString(storeColorScale))
475 + (storeColorBias == 0.0f ? "" : " + float(" + de::toString(storeColorBias) + ")");
477 const int dimension = (m_singleLayerBind ? m_texture.layerDimension() : m_texture.dimension());
478 const std::string texelCoordStr = (dimension == 1 ? "gx" : dimension == 2 ? "ivec2(gx, gy)" : dimension == 3 ? "ivec3(gx, gy, gz)" : "");
480 const ImageType usedImageType = (m_singleLayerBind ? getImageTypeForSingleLayer(m_texture.type()) : m_texture.type());
481 const std::string formatQualifierStr = getShaderImageFormatQualifier(mapVkFormat(m_format));
482 const std::string imageTypeStr = getShaderImageType(mapVkFormat(m_format), usedImageType);
484 std::ostringstream src;
485 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_440) << "\n"
487 << "layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
488 << "layout (binding = 0, " << formatQualifierStr << ") writeonly uniform highp " << imageTypeStr << " u_image;\n";
490 if (m_singleLayerBind)
491 src << "layout (binding = 1) readonly uniform Constants {\n"
492 << " int u_layerNdx;\n"
496 << "void main (void)\n"
498 << " int gx = int(gl_GlobalInvocationID.x);\n"
499 << " int gy = int(gl_GlobalInvocationID.y);\n"
500 << " int gz = " << (m_singleLayerBind ? "u_layerNdx" : "int(gl_GlobalInvocationID.z)") << ";\n"
501 << " imageStore(u_image, " << texelCoordStr << ", " << colorExpr << ");\n"
504 programCollection.glslSources.add("comp") << glu::ComputeSource(src.str());
507 //! Generic test iteration algorithm for image tests
508 class BaseTestInstance : public TestInstance
511 BaseTestInstance (Context& context,
512 const Texture& texture,
513 const VkFormat format,
514 const bool singleLayerBind);
516 tcu::TestStatus iterate (void);
518 virtual ~BaseTestInstance (void) {}
521 virtual VkDescriptorSetLayout prepareDescriptors (void) = 0;
522 virtual tcu::TestStatus verifyResult (void) = 0;
524 virtual void commandBeforeCompute (const VkCommandBuffer cmdBuffer) = 0;
525 virtual void commandBetweenShaderInvocations (const VkCommandBuffer cmdBuffer) = 0;
526 virtual void commandAfterCompute (const VkCommandBuffer cmdBuffer) = 0;
528 virtual void commandBindDescriptorsForLayer (const VkCommandBuffer cmdBuffer,
529 const VkPipelineLayout pipelineLayout,
530 const int layerNdx) = 0;
532 const Texture m_texture;
533 const VkFormat m_format;
534 const bool m_singleLayerBind;
537 BaseTestInstance::BaseTestInstance (Context& context, const Texture& texture, const VkFormat format, const bool singleLayerBind)
538 : TestInstance (context)
539 , m_texture (texture)
541 , m_singleLayerBind (singleLayerBind)
545 tcu::TestStatus BaseTestInstance::iterate (void)
547 const DeviceInterface& vk = m_context.getDeviceInterface();
548 const VkDevice device = m_context.getDevice();
549 const VkQueue queue = m_context.getUniversalQueue();
550 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
552 const Unique<VkShaderModule> shaderModule(createShaderModule(vk, device, m_context.getBinaryCollection().get("comp"), 0));
554 const VkDescriptorSetLayout descriptorSetLayout = prepareDescriptors();
555 const Unique<VkPipelineLayout> pipelineLayout(makePipelineLayout(vk, device, descriptorSetLayout));
556 const Unique<VkPipeline> pipeline(makeComputePipeline(vk, device, *pipelineLayout, *shaderModule));
558 const Unique<VkCommandPool> cmdPool(makeCommandPool(vk, device, queueFamilyIndex));
559 const Unique<VkCommandBuffer> cmdBuffer(makeCommandBuffer(vk, device, *cmdPool));
561 beginCommandBuffer(vk, *cmdBuffer);
563 vk.cmdBindPipeline(*cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipeline);
564 commandBeforeCompute(*cmdBuffer);
566 const tcu::IVec3 workSize = (m_singleLayerBind ? m_texture.layerSize() : m_texture.size());
567 const int loopNumLayers = (m_singleLayerBind ? m_texture.numLayers() : 1);
568 for (int layerNdx = 0; layerNdx < loopNumLayers; ++layerNdx)
570 commandBindDescriptorsForLayer(*cmdBuffer, *pipelineLayout, layerNdx);
573 commandBetweenShaderInvocations(*cmdBuffer);
575 vk.cmdDispatch(*cmdBuffer, workSize.x(), workSize.y(), workSize.z());
578 commandAfterCompute(*cmdBuffer);
580 endCommandBuffer(vk, *cmdBuffer);
582 submitCommandsAndWait(vk, device, queue, *cmdBuffer);
584 return verifyResult();
587 //! Base store test implementation
588 class StoreTestInstance : public BaseTestInstance
591 StoreTestInstance (Context& context,
592 const Texture& texture,
593 const VkFormat format,
594 const bool singleLayerBind);
597 tcu::TestStatus verifyResult (void);
599 // Add empty implementations for functions that might be not needed
600 void commandBeforeCompute (const VkCommandBuffer) {}
601 void commandBetweenShaderInvocations (const VkCommandBuffer) {}
602 void commandAfterCompute (const VkCommandBuffer) {}
604 de::MovePtr<Buffer> m_imageBuffer;
605 const VkDeviceSize m_imageSizeBytes;
608 StoreTestInstance::StoreTestInstance (Context& context, const Texture& texture, const VkFormat format, const bool singleLayerBind)
609 : BaseTestInstance (context, texture, format, singleLayerBind)
610 , m_imageSizeBytes (getImageSizeBytes(texture.size(), format))
612 const DeviceInterface& vk = m_context.getDeviceInterface();
613 const VkDevice device = m_context.getDevice();
614 Allocator& allocator = m_context.getDefaultAllocator();
616 // A helper buffer with enough space to hold the whole image. Usage flags accommodate all derived test instances.
618 m_imageBuffer = de::MovePtr<Buffer>(new Buffer(
619 vk, device, allocator,
620 makeBufferCreateInfo(m_imageSizeBytes, VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT),
621 MemoryRequirement::HostVisible));
624 tcu::TestStatus StoreTestInstance::verifyResult (void)
626 const DeviceInterface& vk = m_context.getDeviceInterface();
627 const VkDevice device = m_context.getDevice();
629 const tcu::IVec3 imageSize = m_texture.size();
630 const tcu::TextureLevel reference = generateReferenceImage(imageSize, m_format);
632 const Allocation& alloc = m_imageBuffer->getAllocation();
633 invalidateMappedMemoryRange(vk, device, alloc.getMemory(), alloc.getOffset(), m_imageSizeBytes);
634 const tcu::ConstPixelBufferAccess result(mapVkFormat(m_format), imageSize, alloc.getHostPtr());
636 if (comparePixelBuffers(m_context.getTestContext().getLog(), m_texture, m_format, reference.getAccess(), result))
637 return tcu::TestStatus::pass("Passed");
639 return tcu::TestStatus::fail("Image comparison failed");
642 //! Store test for images
643 class ImageStoreTestInstance : public StoreTestInstance
646 ImageStoreTestInstance (Context& context,
647 const Texture& texture,
648 const VkFormat format,
649 const bool singleLayerBind);
652 VkDescriptorSetLayout prepareDescriptors (void);
653 void commandBeforeCompute (const VkCommandBuffer cmdBuffer);
654 void commandBetweenShaderInvocations (const VkCommandBuffer cmdBuffer);
655 void commandAfterCompute (const VkCommandBuffer cmdBuffer);
657 void commandBindDescriptorsForLayer (const VkCommandBuffer cmdBuffer,
658 const VkPipelineLayout pipelineLayout,
661 de::MovePtr<Image> m_image;
662 de::MovePtr<Buffer> m_constantsBuffer;
663 const VkDeviceSize m_constantsBufferChunkSizeBytes;
664 Move<VkDescriptorSetLayout> m_descriptorSetLayout;
665 Move<VkDescriptorPool> m_descriptorPool;
666 DynArray<Move<VkDescriptorSet> > m_allDescriptorSets;
667 DynArray<Move<VkImageView> > m_allImageViews;
670 ImageStoreTestInstance::ImageStoreTestInstance (Context& context,
671 const Texture& texture,
672 const VkFormat format,
673 const bool singleLayerBind)
674 : StoreTestInstance (context, texture, format, singleLayerBind)
675 , m_constantsBufferChunkSizeBytes (getOptimalUniformBufferChunkSize(context, sizeof(deUint32)))
676 , m_allDescriptorSets (texture.numLayers())
677 , m_allImageViews (texture.numLayers())
679 const DeviceInterface& vk = m_context.getDeviceInterface();
680 const VkDevice device = m_context.getDevice();
681 Allocator& allocator = m_context.getDefaultAllocator();
683 m_image = de::MovePtr<Image>(new Image(
684 vk, device, allocator,
685 makeImageCreateInfo(m_texture, m_format, VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT),
686 MemoryRequirement::Any));
688 // This buffer will be used to pass constants to the shader
690 const int numLayers = m_texture.numLayers();
691 const VkDeviceSize constantsBufferSizeBytes = numLayers * m_constantsBufferChunkSizeBytes;
692 m_constantsBuffer = de::MovePtr<Buffer>(new Buffer(
693 vk, device, allocator,
694 makeBufferCreateInfo(constantsBufferSizeBytes, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT),
695 MemoryRequirement::HostVisible));
698 const Allocation& alloc = m_constantsBuffer->getAllocation();
699 deUint8* const basePtr = static_cast<deUint8*>(alloc.getHostPtr());
701 deMemset(alloc.getHostPtr(), 0, static_cast<size_t>(constantsBufferSizeBytes));
703 for (int layerNdx = 0; layerNdx < numLayers; ++layerNdx)
705 deUint32* valuePtr = reinterpret_cast<deUint32*>(basePtr + layerNdx * m_constantsBufferChunkSizeBytes);
706 *valuePtr = static_cast<deUint32>(layerNdx);
709 flushMappedMemoryRange(vk, device, alloc.getMemory(), alloc.getOffset(), constantsBufferSizeBytes);
713 VkDescriptorSetLayout ImageStoreTestInstance::prepareDescriptors (void)
715 const DeviceInterface& vk = m_context.getDeviceInterface();
716 const VkDevice device = m_context.getDevice();
718 const int numLayers = m_texture.numLayers();
719 m_descriptorSetLayout = DescriptorSetLayoutBuilder()
720 .addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT)
721 .addSingleBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT)
724 m_descriptorPool = DescriptorPoolBuilder()
725 .addType(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, numLayers)
726 .addType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, numLayers)
727 .build(vk, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, numLayers);
729 if (m_singleLayerBind)
731 for (int layerNdx = 0; layerNdx < numLayers; ++layerNdx)
733 m_allDescriptorSets[layerNdx] = makeDescriptorSet(vk, device, *m_descriptorPool, *m_descriptorSetLayout);
734 m_allImageViews[layerNdx] = makeImageView(vk, device, m_image->get(), mapImageViewType(getImageTypeForSingleLayer(m_texture.type())), m_format,
735 makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, layerNdx, 1u));
738 else // bind all layers at once
740 m_allDescriptorSets[0] = makeDescriptorSet(vk, device, *m_descriptorPool, *m_descriptorSetLayout);
741 m_allImageViews[0] = makeImageView(vk, device, m_image->get(), mapImageViewType(m_texture.type()), m_format,
742 makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, numLayers));
745 return *m_descriptorSetLayout; // not passing the ownership
748 void ImageStoreTestInstance::commandBindDescriptorsForLayer (const VkCommandBuffer cmdBuffer, const VkPipelineLayout pipelineLayout, const int layerNdx)
750 const DeviceInterface& vk = m_context.getDeviceInterface();
751 const VkDevice device = m_context.getDevice();
753 const VkDescriptorSet descriptorSet = *m_allDescriptorSets[layerNdx];
754 const VkImageView imageView = *m_allImageViews[layerNdx];
756 const VkDescriptorImageInfo descriptorImageInfo = makeDescriptorImageInfo(DE_NULL, imageView, VK_IMAGE_LAYOUT_GENERAL);
758 // Set the next chunk of the constants buffer. Each chunk begins with layer index that we've set before.
759 const VkDescriptorBufferInfo descriptorConstantsBufferInfo = makeDescriptorBufferInfo(
760 m_constantsBuffer->get(), layerNdx*m_constantsBufferChunkSizeBytes, m_constantsBufferChunkSizeBytes);
762 DescriptorSetUpdateBuilder()
763 .writeSingle(descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &descriptorImageInfo)
764 .writeSingle(descriptorSet, DescriptorSetUpdateBuilder::Location::binding(1u), VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &descriptorConstantsBufferInfo)
766 vk.cmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout, 0u, 1u, &descriptorSet, 0u, DE_NULL);
769 void ImageStoreTestInstance::commandBeforeCompute (const VkCommandBuffer cmdBuffer)
771 const DeviceInterface& vk = m_context.getDeviceInterface();
773 const VkImageSubresourceRange fullImageSubresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, m_texture.numLayers());
774 const VkImageMemoryBarrier setImageLayoutBarrier = makeImageMemoryBarrier(
776 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL,
777 m_image->get(), fullImageSubresourceRange);
779 const VkDeviceSize constantsBufferSize = m_texture.numLayers() * m_constantsBufferChunkSizeBytes;
780 const VkBufferMemoryBarrier writeConstantsBarrier = makeBufferMemoryBarrier(
781 VK_ACCESS_HOST_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT,
782 m_constantsBuffer->get(), 0ull, constantsBufferSize);
784 const void* const barriersBefore[] = { &writeConstantsBarrier, &setImageLayoutBarrier };
786 vk.cmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, DE_FALSE, DE_LENGTH_OF_ARRAY(barriersBefore), barriersBefore);
789 void ImageStoreTestInstance::commandBetweenShaderInvocations (const VkCommandBuffer cmdBuffer)
791 commandImageWriteBarrierBetweenShaderInvocations(m_context, cmdBuffer, m_image->get(), m_texture);
794 void ImageStoreTestInstance::commandAfterCompute (const VkCommandBuffer cmdBuffer)
796 commandCopyImageToBuffer(m_context, cmdBuffer, m_image->get(), m_imageBuffer->get(), m_imageSizeBytes, m_texture);
799 //! Store test for buffers
800 class BufferStoreTestInstance : public StoreTestInstance
803 BufferStoreTestInstance (Context& context,
804 const Texture& texture,
805 const VkFormat format);
808 VkDescriptorSetLayout prepareDescriptors (void);
809 void commandAfterCompute (const VkCommandBuffer cmdBuffer);
811 void commandBindDescriptorsForLayer (const VkCommandBuffer cmdBuffer,
812 const VkPipelineLayout pipelineLayout,
815 Move<VkDescriptorSetLayout> m_descriptorSetLayout;
816 Move<VkDescriptorPool> m_descriptorPool;
817 Move<VkDescriptorSet> m_descriptorSet;
818 Move<VkBufferView> m_bufferView;
821 BufferStoreTestInstance::BufferStoreTestInstance (Context& context,
822 const Texture& texture,
823 const VkFormat format)
824 : StoreTestInstance(context, texture, format, false)
828 VkDescriptorSetLayout BufferStoreTestInstance::prepareDescriptors (void)
830 const DeviceInterface& vk = m_context.getDeviceInterface();
831 const VkDevice device = m_context.getDevice();
833 m_descriptorSetLayout = DescriptorSetLayoutBuilder()
834 .addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT)
837 m_descriptorPool = DescriptorPoolBuilder()
838 .addType(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)
839 .build(vk, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
841 m_descriptorSet = makeDescriptorSet(vk, device, *m_descriptorPool, *m_descriptorSetLayout);
842 m_bufferView = makeBufferView(vk, device, m_imageBuffer->get(), m_format, 0ull, m_imageSizeBytes);
844 return *m_descriptorSetLayout; // not passing the ownership
847 void BufferStoreTestInstance::commandBindDescriptorsForLayer (const VkCommandBuffer cmdBuffer, const VkPipelineLayout pipelineLayout, const int layerNdx)
849 DE_ASSERT(layerNdx == 0);
852 const VkDevice device = m_context.getDevice();
853 const DeviceInterface& vk = m_context.getDeviceInterface();
855 DescriptorSetUpdateBuilder()
856 .writeSingle(*m_descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, &m_bufferView.get())
858 vk.cmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout, 0u, 1u, &m_descriptorSet.get(), 0u, DE_NULL);
861 void BufferStoreTestInstance::commandAfterCompute (const VkCommandBuffer cmdBuffer)
863 commandBufferWriteBarrierBeforeHostRead(m_context, cmdBuffer, m_imageBuffer->get(), m_imageSizeBytes);
866 class LoadStoreTest : public TestCase
871 FLAG_SINGLE_LAYER_BIND = 1 << 0, //!< Run the shader multiple times, each time binding a different layer.
872 FLAG_RESTRICT_IMAGES = 1 << 1, //!< If given, images in the shader will be qualified with "restrict".
875 LoadStoreTest (tcu::TestContext& testCtx,
876 const std::string& name,
877 const std::string& description,
878 const Texture& texture,
879 const VkFormat format,
880 const VkFormat imageFormat,
881 const TestFlags flags = static_cast<TestFlags>(0));
883 void initPrograms (SourceCollections& programCollection) const;
884 TestInstance* createInstance (Context& context) const;
887 const Texture m_texture;
888 const VkFormat m_format; //!< Format as accessed in the shader
889 const VkFormat m_imageFormat; //!< Storage format
890 const bool m_singleLayerBind;
891 const bool m_restrictImages;
894 LoadStoreTest::LoadStoreTest (tcu::TestContext& testCtx,
895 const std::string& name,
896 const std::string& description,
897 const Texture& texture,
898 const VkFormat format,
899 const VkFormat imageFormat,
900 const TestFlags flags)
901 : TestCase (testCtx, name, description)
902 , m_texture (texture)
904 , m_imageFormat (imageFormat)
905 , m_singleLayerBind ((flags & FLAG_SINGLE_LAYER_BIND) != 0)
906 , m_restrictImages ((flags & FLAG_RESTRICT_IMAGES) != 0)
908 if (m_singleLayerBind)
909 DE_ASSERT(m_texture.numLayers() > 1);
911 DE_ASSERT(formatsAreCompatible(m_format, m_imageFormat));
914 void LoadStoreTest::initPrograms (SourceCollections& programCollection) const
916 const int dimension = (m_singleLayerBind ? m_texture.layerDimension() : m_texture.dimension());
917 const ImageType usedImageType = (m_singleLayerBind ? getImageTypeForSingleLayer(m_texture.type()) : m_texture.type());
918 const std::string formatQualifierStr = getShaderImageFormatQualifier(mapVkFormat(m_format));
919 const std::string imageTypeStr = getShaderImageType(mapVkFormat(m_format), usedImageType);
920 const std::string maybeRestrictStr = (m_restrictImages ? "restrict " : "");
921 const std::string xMax = de::toString(m_texture.size().x() - 1);
923 std::ostringstream src;
924 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_440) << "\n"
926 << "layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
927 << "layout (binding = 0, " << formatQualifierStr << ") " << maybeRestrictStr << "readonly uniform highp " << imageTypeStr << " u_image0;\n"
928 << "layout (binding = 1, " << formatQualifierStr << ") " << maybeRestrictStr << "writeonly uniform highp " << imageTypeStr << " u_image1;\n"
930 << "void main (void)\n"
933 " int pos = int(gl_GlobalInvocationID.x);\n"
934 " imageStore(u_image1, pos, imageLoad(u_image0, " + xMax + "-pos));\n"
936 " ivec2 pos = ivec2(gl_GlobalInvocationID.xy);\n"
937 " imageStore(u_image1, pos, imageLoad(u_image0, ivec2(" + xMax + "-pos.x, pos.y)));\n"
939 " ivec3 pos = ivec3(gl_GlobalInvocationID);\n"
940 " imageStore(u_image1, pos, imageLoad(u_image0, ivec3(" + xMax + "-pos.x, pos.y, pos.z)));\n"
944 programCollection.glslSources.add("comp") << glu::ComputeSource(src.str());
947 //! Load/store test base implementation
948 class LoadStoreTestInstance : public BaseTestInstance
951 LoadStoreTestInstance (Context& context,
952 const Texture& texture,
953 const VkFormat format,
954 const VkFormat imageFormat,
955 const bool singleLayerBind);
958 virtual Buffer* getResultBuffer (void) const = 0; //!< Get the buffer that contains the result image
960 tcu::TestStatus verifyResult (void);
962 // Add empty implementations for functions that might be not needed
963 void commandBeforeCompute (const VkCommandBuffer) {}
964 void commandBetweenShaderInvocations (const VkCommandBuffer) {}
965 void commandAfterCompute (const VkCommandBuffer) {}
967 de::MovePtr<Buffer> m_imageBuffer; //!< Source data and helper buffer
968 const VkDeviceSize m_imageSizeBytes;
969 const VkFormat m_imageFormat; //!< Image format (for storage, may be different than texture format)
970 tcu::TextureLevel m_referenceImage; //!< Used as input data and later to verify result image
973 LoadStoreTestInstance::LoadStoreTestInstance (Context& context,
974 const Texture& texture,
975 const VkFormat format,
976 const VkFormat imageFormat,
977 const bool singleLayerBind)
978 : BaseTestInstance (context, texture, format, singleLayerBind)
979 , m_imageSizeBytes (getImageSizeBytes(texture.size(), format))
980 , m_imageFormat (imageFormat)
981 , m_referenceImage (generateReferenceImage(texture.size(), imageFormat, format))
983 const DeviceInterface& vk = m_context.getDeviceInterface();
984 const VkDevice device = m_context.getDevice();
985 Allocator& allocator = m_context.getDefaultAllocator();
987 // A helper buffer with enough space to hold the whole image.
989 m_imageBuffer = de::MovePtr<Buffer>(new Buffer(
990 vk, device, allocator,
991 makeBufferCreateInfo(m_imageSizeBytes, VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT),
992 MemoryRequirement::HostVisible));
994 // Copy reference data to buffer for subsequent upload to image.
996 const Allocation& alloc = m_imageBuffer->getAllocation();
997 deMemcpy(alloc.getHostPtr(), m_referenceImage.getAccess().getDataPtr(), static_cast<size_t>(m_imageSizeBytes));
998 flushMappedMemoryRange(vk, device, alloc.getMemory(), alloc.getOffset(), m_imageSizeBytes);
1001 tcu::TestStatus LoadStoreTestInstance::verifyResult (void)
1003 const DeviceInterface& vk = m_context.getDeviceInterface();
1004 const VkDevice device = m_context.getDevice();
1006 // Apply the same transformation as done in the shader
1007 const tcu::PixelBufferAccess reference = m_referenceImage.getAccess();
1008 flipHorizontally(reference);
1010 const Allocation& alloc = getResultBuffer()->getAllocation();
1011 invalidateMappedMemoryRange(vk, device, alloc.getMemory(), alloc.getOffset(), m_imageSizeBytes);
1012 const tcu::ConstPixelBufferAccess result(mapVkFormat(m_imageFormat), m_texture.size(), alloc.getHostPtr());
1014 if (comparePixelBuffers(m_context.getTestContext().getLog(), m_texture, m_imageFormat, reference, result))
1015 return tcu::TestStatus::pass("Passed");
1017 return tcu::TestStatus::fail("Image comparison failed");
1020 //! Load/store test for images
1021 class ImageLoadStoreTestInstance : public LoadStoreTestInstance
1026 PerLayerData (Move<VkDescriptorSet> descriptorSet,
1027 Move<VkImageView> imageViewSrc,
1028 Move<VkImageView> imageViewDst);
1030 const Unique<VkDescriptorSet> descriptorSet;
1031 const Unique<VkImageView> imageViewSrc;
1032 const Unique<VkImageView> imageViewDst;
1035 ImageLoadStoreTestInstance (Context& context,
1036 const Texture& texture,
1037 const VkFormat format,
1038 const VkFormat imageFormat,
1039 const bool singleLayerBind);
1042 VkDescriptorSetLayout prepareDescriptors (void);
1043 void commandBeforeCompute (const VkCommandBuffer cmdBuffer);
1044 void commandBetweenShaderInvocations (const VkCommandBuffer cmdBuffer);
1045 void commandAfterCompute (const VkCommandBuffer cmdBuffer);
1047 void commandBindDescriptorsForLayer (const VkCommandBuffer cmdBuffer,
1048 const VkPipelineLayout pipelineLayout,
1049 const int layerNdx);
1051 Buffer* getResultBuffer (void) const { return m_imageBuffer.get(); }
1053 de::MovePtr<Image> m_imageSrc;
1054 de::MovePtr<Image> m_imageDst;
1055 Move<VkDescriptorSetLayout> m_descriptorSetLayout;
1056 Move<VkDescriptorPool> m_descriptorPool;
1057 DynArray<de::MovePtr<PerLayerData> > m_perLayerData;
1060 ImageLoadStoreTestInstance::PerLayerData::PerLayerData (Move<VkDescriptorSet> descriptorSet_,
1061 Move<VkImageView> imageViewSrc_,
1062 Move<VkImageView> imageViewDst_)
1063 : descriptorSet (descriptorSet_)
1064 , imageViewSrc (imageViewSrc_)
1065 , imageViewDst (imageViewDst_)
1069 ImageLoadStoreTestInstance::ImageLoadStoreTestInstance (Context& context,
1070 const Texture& texture,
1071 const VkFormat format,
1072 const VkFormat imageFormat,
1073 const bool singleLayerBind)
1074 : LoadStoreTestInstance (context, texture, format, imageFormat, singleLayerBind)
1075 , m_perLayerData (texture.numLayers())
1077 const DeviceInterface& vk = m_context.getDeviceInterface();
1078 const VkDevice device = m_context.getDevice();
1079 Allocator& allocator = m_context.getDefaultAllocator();
1081 m_imageSrc = de::MovePtr<Image>(new Image(
1082 vk, device, allocator,
1083 makeImageCreateInfo(m_texture, m_imageFormat, VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT),
1084 MemoryRequirement::Any));
1086 m_imageDst = de::MovePtr<Image>(new Image(
1087 vk, device, allocator,
1088 makeImageCreateInfo(m_texture, m_imageFormat, VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT),
1089 MemoryRequirement::Any));
1092 VkDescriptorSetLayout ImageLoadStoreTestInstance::prepareDescriptors (void)
1094 const VkDevice device = m_context.getDevice();
1095 const DeviceInterface& vk = m_context.getDeviceInterface();
1097 const int numLayers = m_texture.numLayers();
1098 m_descriptorSetLayout = DescriptorSetLayoutBuilder()
1099 .addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT)
1100 .addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT)
1103 m_descriptorPool = DescriptorPoolBuilder()
1104 .addType(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, numLayers)
1105 .addType(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, numLayers)
1106 .build(vk, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, numLayers);
1108 if (m_singleLayerBind)
1110 for (int layerNdx = 0; layerNdx < numLayers; ++layerNdx)
1112 const VkImageViewType viewType = mapImageViewType(getImageTypeForSingleLayer(m_texture.type()));
1113 const VkImageSubresourceRange subresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, layerNdx, 1u);
1115 de::MovePtr<PerLayerData> data(new PerLayerData(
1116 makeDescriptorSet(vk, device, *m_descriptorPool, *m_descriptorSetLayout),
1117 makeImageView(vk, device, m_imageSrc->get(), viewType, m_format, subresourceRange),
1118 makeImageView(vk, device, m_imageDst->get(), viewType, m_format, subresourceRange)));
1120 m_perLayerData[layerNdx] = data;
1123 else // bind all layers at once
1125 const VkImageViewType viewType = mapImageViewType(m_texture.type());
1126 const VkImageSubresourceRange subresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, numLayers);
1128 de::MovePtr<PerLayerData> data(new PerLayerData(
1129 makeDescriptorSet(vk, device, *m_descriptorPool, *m_descriptorSetLayout),
1130 makeImageView(vk, device, m_imageSrc->get(), viewType, m_format, subresourceRange),
1131 makeImageView(vk, device, m_imageDst->get(), viewType, m_format, subresourceRange)));
1133 m_perLayerData[0] = data;
1136 return *m_descriptorSetLayout; // not passing the ownership
1139 void ImageLoadStoreTestInstance::commandBindDescriptorsForLayer (const VkCommandBuffer cmdBuffer, const VkPipelineLayout pipelineLayout, const int layerNdx)
1141 const VkDevice device = m_context.getDevice();
1142 const DeviceInterface& vk = m_context.getDeviceInterface();
1144 const PerLayerData* data = m_perLayerData[layerNdx].get();
1146 const VkDescriptorImageInfo descriptorSrcImageInfo = makeDescriptorImageInfo(DE_NULL, *data->imageViewSrc, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
1147 const VkDescriptorImageInfo descriptorDstImageInfo = makeDescriptorImageInfo(DE_NULL, *data->imageViewDst, VK_IMAGE_LAYOUT_GENERAL);
1149 DescriptorSetUpdateBuilder()
1150 .writeSingle(*data->descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &descriptorSrcImageInfo)
1151 .writeSingle(*data->descriptorSet, DescriptorSetUpdateBuilder::Location::binding(1u), VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &descriptorDstImageInfo)
1152 .update(vk, device);
1153 vk.cmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout, 0u, 1u, &data->descriptorSet.get(), 0u, DE_NULL);
1156 void ImageLoadStoreTestInstance::commandBeforeCompute (const VkCommandBuffer cmdBuffer)
1158 const DeviceInterface& vk = m_context.getDeviceInterface();
1160 const VkImageSubresourceRange fullImageSubresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, m_texture.numLayers());
1162 const VkImageMemoryBarrier barrierSetSrcImageLayout = makeImageMemoryBarrier(
1164 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1165 m_imageSrc->get(), fullImageSubresourceRange);
1167 const VkImageMemoryBarrier barrierSetDstImageLayout = makeImageMemoryBarrier(
1169 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL,
1170 m_imageDst->get(), fullImageSubresourceRange);
1172 const VkBufferMemoryBarrier barrierFlushHostWriteBeforeCopy = makeBufferMemoryBarrier(
1173 VK_ACCESS_HOST_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
1174 m_imageBuffer->get(), 0ull, m_imageSizeBytes);
1176 const void* const barriers[] = { &barrierSetSrcImageLayout, &barrierSetDstImageLayout, &barrierFlushHostWriteBeforeCopy };
1178 vk.cmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT,
1179 DE_FALSE, DE_LENGTH_OF_ARRAY(barriers), barriers);
1182 const VkImageMemoryBarrier barrierAfterCopy = makeImageMemoryBarrier(
1183 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT,
1184 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1185 m_imageSrc->get(), fullImageSubresourceRange);
1187 const void* const barriers[] = { &barrierAfterCopy };
1189 const VkBufferImageCopy copyRegion = makeBufferImageCopy(m_texture);
1191 vk.cmdCopyBufferToImage(cmdBuffer, m_imageBuffer->get(), m_imageSrc->get(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1u, ©Region);
1192 vk.cmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, DE_FALSE, DE_LENGTH_OF_ARRAY(barriers), barriers);
1196 void ImageLoadStoreTestInstance::commandBetweenShaderInvocations (const VkCommandBuffer cmdBuffer)
1198 commandImageWriteBarrierBetweenShaderInvocations(m_context, cmdBuffer, m_imageDst->get(), m_texture);
1201 void ImageLoadStoreTestInstance::commandAfterCompute (const VkCommandBuffer cmdBuffer)
1203 commandCopyImageToBuffer(m_context, cmdBuffer, m_imageDst->get(), m_imageBuffer->get(), m_imageSizeBytes, m_texture);
1206 //! Load/store test for buffers
1207 class BufferLoadStoreTestInstance : public LoadStoreTestInstance
1210 BufferLoadStoreTestInstance (Context& context,
1211 const Texture& texture,
1212 const VkFormat format,
1213 const VkFormat imageFormat);
1216 VkDescriptorSetLayout prepareDescriptors (void);
1217 void commandAfterCompute (const VkCommandBuffer cmdBuffer);
1219 void commandBindDescriptorsForLayer (const VkCommandBuffer cmdBuffer,
1220 const VkPipelineLayout pipelineLayout,
1221 const int layerNdx);
1223 Buffer* getResultBuffer (void) const { return m_imageBufferDst.get(); }
1225 de::MovePtr<Buffer> m_imageBufferDst;
1226 Move<VkDescriptorSetLayout> m_descriptorSetLayout;
1227 Move<VkDescriptorPool> m_descriptorPool;
1228 Move<VkDescriptorSet> m_descriptorSet;
1229 Move<VkBufferView> m_bufferViewSrc;
1230 Move<VkBufferView> m_bufferViewDst;
1233 BufferLoadStoreTestInstance::BufferLoadStoreTestInstance (Context& context,
1234 const Texture& texture,
1235 const VkFormat format,
1236 const VkFormat imageFormat)
1237 : LoadStoreTestInstance(context, texture, format, imageFormat, false)
1239 const DeviceInterface& vk = m_context.getDeviceInterface();
1240 const VkDevice device = m_context.getDevice();
1241 Allocator& allocator = m_context.getDefaultAllocator();
1243 // Create a destination buffer.
1245 m_imageBufferDst = de::MovePtr<Buffer>(new Buffer(
1246 vk, device, allocator,
1247 makeBufferCreateInfo(m_imageSizeBytes, VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT),
1248 MemoryRequirement::HostVisible));
1251 VkDescriptorSetLayout BufferLoadStoreTestInstance::prepareDescriptors (void)
1253 const DeviceInterface& vk = m_context.getDeviceInterface();
1254 const VkDevice device = m_context.getDevice();
1256 m_descriptorSetLayout = DescriptorSetLayoutBuilder()
1257 .addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT)
1258 .addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT)
1261 m_descriptorPool = DescriptorPoolBuilder()
1262 .addType(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)
1263 .addType(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)
1264 .build(vk, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
1266 m_descriptorSet = makeDescriptorSet(vk, device, *m_descriptorPool, *m_descriptorSetLayout);
1267 m_bufferViewSrc = makeBufferView(vk, device, m_imageBuffer->get(), m_format, 0ull, m_imageSizeBytes);
1268 m_bufferViewDst = makeBufferView(vk, device, m_imageBufferDst->get(), m_format, 0ull, m_imageSizeBytes);
1270 return *m_descriptorSetLayout; // not passing the ownership
1273 void BufferLoadStoreTestInstance::commandBindDescriptorsForLayer (const VkCommandBuffer cmdBuffer, const VkPipelineLayout pipelineLayout, const int layerNdx)
1275 DE_ASSERT(layerNdx == 0);
1278 const VkDevice device = m_context.getDevice();
1279 const DeviceInterface& vk = m_context.getDeviceInterface();
1281 DescriptorSetUpdateBuilder()
1282 .writeSingle(*m_descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, &m_bufferViewSrc.get())
1283 .writeSingle(*m_descriptorSet, DescriptorSetUpdateBuilder::Location::binding(1u), VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, &m_bufferViewDst.get())
1284 .update(vk, device);
1285 vk.cmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout, 0u, 1u, &m_descriptorSet.get(), 0u, DE_NULL);
1288 void BufferLoadStoreTestInstance::commandAfterCompute (const VkCommandBuffer cmdBuffer)
1290 commandBufferWriteBarrierBeforeHostRead(m_context, cmdBuffer, m_imageBufferDst->get(), m_imageSizeBytes);
1293 TestInstance* StoreTest::createInstance (Context& context) const
1295 if (m_texture.type() == IMAGE_TYPE_BUFFER)
1296 return new BufferStoreTestInstance(context, m_texture, m_format);
1298 return new ImageStoreTestInstance(context, m_texture, m_format, m_singleLayerBind);
1301 TestInstance* LoadStoreTest::createInstance (Context& context) const
1303 if (m_texture.type() == IMAGE_TYPE_BUFFER)
1304 return new BufferLoadStoreTestInstance(context, m_texture, m_format, m_imageFormat);
1306 return new ImageLoadStoreTestInstance(context, m_texture, m_format, m_imageFormat, m_singleLayerBind);
1309 // TODO Which image/format combinations should be supported? Spec says it should be queried with vkGetPhysicalDeviceImageFormatProperties.
1310 // What about buffer/format? (texel storage buffer) (use vkGetPhysicalDeviceFormatProperties ?)
1312 static const Texture s_textures[] =
1314 Texture(IMAGE_TYPE_1D, tcu::IVec3(64, 1, 1), 1),
1315 Texture(IMAGE_TYPE_1D_ARRAY, tcu::IVec3(64, 1, 1), 8),
1316 Texture(IMAGE_TYPE_2D, tcu::IVec3(64, 64, 1), 1),
1317 Texture(IMAGE_TYPE_2D_ARRAY, tcu::IVec3(64, 64, 1), 8),
1318 Texture(IMAGE_TYPE_3D, tcu::IVec3(64, 64, 8), 1),
1319 Texture(IMAGE_TYPE_CUBE, tcu::IVec3(64, 64, 1), 6),
1320 Texture(IMAGE_TYPE_CUBE_ARRAY, tcu::IVec3(64, 64, 1), 2*6),
1321 Texture(IMAGE_TYPE_BUFFER, tcu::IVec3(64, 1, 1), 1),
1324 const Texture& getTestTexture (const ImageType imageType)
1326 for (int textureNdx = 0; textureNdx < DE_LENGTH_OF_ARRAY(s_textures); ++textureNdx)
1327 if (s_textures[textureNdx].type() == imageType)
1328 return s_textures[textureNdx];
1330 DE_FATAL("Internal error");
1331 return s_textures[0];
1334 static const VkFormat s_formats[] =
1336 VK_FORMAT_R32G32B32A32_SFLOAT,
1337 VK_FORMAT_R16G16B16A16_SFLOAT,
1338 VK_FORMAT_R32_SFLOAT,
1340 VK_FORMAT_R32G32B32A32_UINT,
1341 VK_FORMAT_R16G16B16A16_UINT,
1342 VK_FORMAT_R8G8B8A8_UINT,
1345 VK_FORMAT_R32G32B32A32_SINT,
1346 VK_FORMAT_R16G16B16A16_SINT,
1347 VK_FORMAT_R8G8B8A8_SINT,
1350 VK_FORMAT_R8G8B8A8_UNORM,
1352 VK_FORMAT_R8G8B8A8_SNORM,
1357 tcu::TestCaseGroup* createImageStoreTests (tcu::TestContext& testCtx)
1359 de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "store", "Plain imageStore() cases"));
1361 for (int textureNdx = 0; textureNdx < DE_LENGTH_OF_ARRAY(s_textures); ++textureNdx)
1363 const Texture& texture = s_textures[textureNdx];
1364 de::MovePtr<tcu::TestCaseGroup> groupByImageViewType (new tcu::TestCaseGroup(testCtx, getImageTypeName(texture.type()).c_str(), ""));
1365 const bool isLayered = (texture.numLayers() > 1);
1367 for (int formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(s_formats); ++formatNdx)
1369 groupByImageViewType->addChild(new StoreTest(testCtx, getFormatCaseName(s_formats[formatNdx]), "", texture, s_formats[formatNdx]));
1372 groupByImageViewType->addChild(new StoreTest(testCtx, getFormatCaseName(s_formats[formatNdx]) + "_single_layer", "",
1373 texture, s_formats[formatNdx], StoreTest::FLAG_SINGLE_LAYER_BIND));
1375 testGroup->addChild(groupByImageViewType.release());
1378 return testGroup.release();
1381 tcu::TestCaseGroup* createImageLoadStoreTests (tcu::TestContext& testCtx)
1383 de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "load_store", "Cases with imageLoad() followed by imageStore()"));
1385 for (int textureNdx = 0; textureNdx < DE_LENGTH_OF_ARRAY(s_textures); ++textureNdx)
1387 const Texture& texture = s_textures[textureNdx];
1388 de::MovePtr<tcu::TestCaseGroup> groupByImageViewType (new tcu::TestCaseGroup(testCtx, getImageTypeName(texture.type()).c_str(), ""));
1389 const bool isLayered = (texture.numLayers() > 1);
1391 for (int formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(s_formats); ++formatNdx)
1393 groupByImageViewType->addChild(new LoadStoreTest(testCtx, getFormatCaseName(s_formats[formatNdx]), "",
1394 texture, s_formats[formatNdx], s_formats[formatNdx]));
1397 groupByImageViewType->addChild(new LoadStoreTest(testCtx, getFormatCaseName(s_formats[formatNdx]) + "_single_layer", "",
1398 texture, s_formats[formatNdx], s_formats[formatNdx], LoadStoreTest::FLAG_SINGLE_LAYER_BIND));
1400 testGroup->addChild(groupByImageViewType.release());
1403 return testGroup.release();
1406 tcu::TestCaseGroup* createImageFormatReinterpretTests (tcu::TestContext& testCtx)
1408 de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "format_reinterpret", "Cases with differing texture and image formats"));
1410 for (int textureNdx = 0; textureNdx < DE_LENGTH_OF_ARRAY(s_textures); ++textureNdx)
1412 const Texture& texture = s_textures[textureNdx];
1413 de::MovePtr<tcu::TestCaseGroup> groupByImageViewType (new tcu::TestCaseGroup(testCtx, getImageTypeName(texture.type()).c_str(), ""));
1415 for (int imageFormatNdx = 0; imageFormatNdx < DE_LENGTH_OF_ARRAY(s_formats); ++imageFormatNdx)
1416 for (int formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(s_formats); ++formatNdx)
1418 //TODO Are all conversions valid or do we have to limit (or expand) somehow? Is it stated anywhere in the spec?
1420 const std::string caseName = getFormatCaseName(s_formats[imageFormatNdx]) + "_" + getFormatCaseName(s_formats[formatNdx]);
1421 if (imageFormatNdx != formatNdx && formatsAreCompatible(s_formats[imageFormatNdx], s_formats[formatNdx]))
1422 groupByImageViewType->addChild(new LoadStoreTest(testCtx, caseName, "", texture, s_formats[formatNdx], s_formats[imageFormatNdx]));
1424 testGroup->addChild(groupByImageViewType.release());
1427 return testGroup.release();
1430 de::MovePtr<TestCase> createImageQualifierRestrictCase (tcu::TestContext& testCtx, const ImageType imageType, const std::string& name)
1432 const VkFormat format = VK_FORMAT_R32G32B32A32_UINT;
1433 const Texture& texture = getTestTexture(imageType);
1434 return de::MovePtr<TestCase>(new LoadStoreTest(testCtx, name, "", texture, format, format, LoadStoreTest::FLAG_RESTRICT_IMAGES));