1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2016 The Khronos Group Inc.
6 * Copyright (c) 2016 The Android Open Source Project
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * \brief Geometry Basic Class
23 *//*--------------------------------------------------------------------*/
25 #include "vktGeometryBasicClass.hpp"
28 #include "vktTestCase.hpp"
29 #include "vktTestCaseUtil.hpp"
30 #include "vkImageUtil.hpp"
31 #include "vkPrograms.hpp"
32 #include "vkBuilderUtil.hpp"
33 #include "vkRefUtil.hpp"
34 #include "vkQueryUtil.hpp"
35 #include "vkMemUtil.hpp"
46 using tcu::TestStatus;
47 using tcu::TestContext;
48 using tcu::TestCaseGroup;
54 static const int TEST_CANVAS_SIZE = 256;
56 GeometryExpanderRenderTestInstance::GeometryExpanderRenderTestInstance (Context& context,
57 const VkPrimitiveTopology primitiveType,
59 : TestInstance (context)
60 , m_primitiveType (primitiveType)
63 checkGeometryShaderSupport(context.getInstanceInterface(), context.getPhysicalDevice());
66 tcu::TestStatus GeometryExpanderRenderTestInstance::iterate (void)
68 const DeviceInterface& vk = m_context.getDeviceInterface();
69 const VkDevice device = m_context.getDevice();
70 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
71 const VkQueue queue = m_context.getUniversalQueue();
72 Allocator& memAlloc = m_context.getDefaultAllocator();
73 const IVec2 resolution = IVec2(TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
74 const VkFormat colorFormat = VK_FORMAT_R8G8B8A8_UNORM;
75 const Image colorAttachmentImage (
79 makeImageCreateInfo(resolution, colorFormat, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT),
80 MemoryRequirement::Any
82 const Unique<VkRenderPass> renderPass (makeRenderPass(vk, device, colorFormat));
84 const Move<VkPipelineLayout> pipelineLayout (createPipelineLayout(vk, device));
85 const VkImageSubresourceRange colorSubRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u);
86 const Unique<VkImageView> colorAttachmentView (makeImageView(vk, device, *colorAttachmentImage, VK_IMAGE_VIEW_TYPE_2D, colorFormat, colorSubRange));
87 const Unique<VkFramebuffer> framebuffer (makeFramebuffer(vk, device, *renderPass, *colorAttachmentView, resolution.x(), resolution.y(), 1u));
88 const Unique<VkCommandPool> cmdPool (createCommandPool(vk, device, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, queueFamilyIndex));
89 const Unique<VkCommandBuffer> cmdBuffer (allocateCommandBuffer(vk, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
91 const VkDeviceSize vertexDataSizeBytes = sizeInBytes(m_vertexPosData) + sizeInBytes(m_vertexAttrData);
92 const deUint32 vertexPositionsOffset = 0u;
93 const deUint32 vertexAtrrOffset = static_cast<deUint32>(sizeof(Vec4));
94 const string geometryShaderName = (m_context.getBinaryCollection().contains("geometry_pointsize") && checkPointSize (m_context.getInstanceInterface(), m_context.getPhysicalDevice()))?
95 "geometry_pointsize" : "geometry";
97 const Unique<VkPipeline> pipeline (GraphicsPipelineBuilder()
98 .setRenderSize (resolution)
99 .setShader (vk, device, VK_SHADER_STAGE_VERTEX_BIT, m_context.getBinaryCollection().get("vertex"), DE_NULL)
100 .setShader (vk, device, VK_SHADER_STAGE_GEOMETRY_BIT, m_context.getBinaryCollection().get(geometryShaderName), DE_NULL)
101 .setShader (vk, device, VK_SHADER_STAGE_FRAGMENT_BIT, m_context.getBinaryCollection().get("fragment"), DE_NULL)
102 .addVertexBinding (makeVertexInputBindingDescription(0u, 2u*vertexAtrrOffset, VK_VERTEX_INPUT_RATE_VERTEX))
103 .addVertexAttribute (makeVertexInputAttributeDescription(0u, 0u, VK_FORMAT_R32G32B32A32_SFLOAT, vertexPositionsOffset))
104 .addVertexAttribute (makeVertexInputAttributeDescription(1u, 0u, VK_FORMAT_R32G32B32A32_SFLOAT, vertexAtrrOffset))
105 .setPrimitiveTopology (m_primitiveType)
106 .build (vk, device, *pipelineLayout, *renderPass));
108 const VkDeviceSize colorBufferSizeBytes = resolution.x()*resolution.y() * tcu::getPixelSize(mapVkFormat(colorFormat));
109 const Buffer colorBuffer (vk, device, memAlloc, makeBufferCreateInfo(colorBufferSizeBytes,
110 VK_BUFFER_USAGE_TRANSFER_DST_BIT), MemoryRequirement::HostVisible);
111 const Buffer vertexBuffer (vk, device, memAlloc, makeBufferCreateInfo(vertexDataSizeBytes,
112 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT ), MemoryRequirement::HostVisible);
114 const Allocation& alloc = vertexBuffer.getAllocation();
121 DataVec4* const pData = static_cast<DataVec4*>(alloc.getHostPtr());
122 for(int ndx = 0; ndx < m_numDrawVertices; ++ndx)
124 pData[ndx].pos = m_vertexPosData[ndx];
125 pData[ndx].color = m_vertexAttrData[ndx];
127 flushMappedMemoryRange(vk, device, alloc.getMemory(), alloc.getOffset(), vertexDataSizeBytes);
128 // No barrier needed, flushed memory is automatically visible
132 beginCommandBuffer(vk, *cmdBuffer);
134 // Change color attachment image layout
136 const VkImageMemoryBarrier colorAttachmentLayoutBarrier = makeImageMemoryBarrier(
137 (VkAccessFlags)0, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
138 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
139 *colorAttachmentImage, colorSubRange);
141 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0u,
142 0u, DE_NULL, 0u, DE_NULL, 1u, &colorAttachmentLayoutBarrier);
147 const VkRect2D renderArea = {
149 makeExtent2D(resolution.x(), resolution.y()),
151 const tcu::Vec4 clearColor(0.0f, 0.0f, 0.0f, 1.0f);
152 beginRenderPass(vk, *cmdBuffer, *renderPass, *framebuffer, renderArea, clearColor);
155 vk.cmdBindPipeline(*cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *pipeline);
157 const VkBuffer buffers[] = { vertexBuffer.get()};
158 const VkDeviceSize offsets[] = { vertexPositionsOffset };
159 vk.cmdBindVertexBuffers(*cmdBuffer, 0u, DE_LENGTH_OF_ARRAY(buffers), buffers, offsets);
162 bindDescriptorSets(vk, device, memAlloc, *cmdBuffer, *pipelineLayout);
164 drawCommand (*cmdBuffer);
165 endRenderPass(vk, *cmdBuffer);
167 // Copy render result to a host-visible buffer
169 const VkImageMemoryBarrier colorAttachmentPreCopyBarrier = makeImageMemoryBarrier(
170 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
171 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
172 *colorAttachmentImage, colorSubRange);
174 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u,
175 0u, DE_NULL, 0u, DE_NULL, 1u, &colorAttachmentPreCopyBarrier);
178 const VkBufferImageCopy copyRegion = makeBufferImageCopy(makeExtent3D(resolution.x(), resolution.y(), 1), makeImageSubresourceLayers(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 0u, 1u));
179 vk.cmdCopyImageToBuffer(*cmdBuffer, *colorAttachmentImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *colorBuffer, 1u, ©Region);
183 const VkBufferMemoryBarrier postCopyBarrier = makeBufferMemoryBarrier(VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT, *colorBuffer, 0ull, colorBufferSizeBytes);
185 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u,
186 0u, DE_NULL, 1u, &postCopyBarrier, 0u, DE_NULL);
189 endCommandBuffer(vk, *cmdBuffer);
190 submitCommandsAndWait(vk, device, queue, *cmdBuffer);
193 // Log the result image.
194 const Allocation& colorBufferAlloc = colorBuffer.getAllocation();
195 invalidateMappedMemoryRange(vk, device, colorBufferAlloc.getMemory(), colorBufferAlloc.getOffset(), colorBufferSizeBytes);
196 const tcu::ConstPixelBufferAccess imagePixelAccess(mapVkFormat(colorFormat), resolution.x(), resolution.y(), 1, colorBufferAlloc.getHostPtr());
198 if (!compareWithFileImage(m_context, imagePixelAccess, m_name))
199 return TestStatus::fail("Fail");
202 return TestStatus::pass("Pass");
205 Move<VkPipelineLayout> GeometryExpanderRenderTestInstance::createPipelineLayout (const DeviceInterface& vk, const VkDevice device)
207 return makePipelineLayout(vk, device);
210 void GeometryExpanderRenderTestInstance::drawCommand (const VkCommandBuffer& cmdBuffer)
212 const DeviceInterface& vk = m_context.getDeviceInterface();
213 vk.cmdDraw(cmdBuffer, static_cast<deUint32>(m_numDrawVertices), 1u, 0u, 0u);