1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2015 The Khronos Group Inc.
6 * Copyright (c) 2015 Intel Corporation
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and/or associated documentation files (the
10 * "Materials"), to deal in the Materials without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Materials, and to
13 * permit persons to whom the Materials are furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice(s) and this permission notice shall be included
17 * in all copies or substantial portions of the Materials.
19 * The Materials are Confidential Information as defined by the
20 * Khronos Membership Agreement until designated non-confidential by Khronos,
21 * at which point this condition clause shall be removed.
23 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
27 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
33 * \brief Dynamic State Tests - Base Class
34 *//*--------------------------------------------------------------------*/
36 #include "vktDynamicStateBaseClass.hpp"
38 #include "vkPrograms.hpp"
42 namespace DynamicState
45 DynamicStateBaseClass::DynamicStateBaseClass (Context& context, const char* vertexShaderName, const char* fragmentShaderName)
46 : TestInstance (context)
47 , m_colorAttachmentFormat (vk::VK_FORMAT_R8G8B8A8_UNORM)
48 , m_topology (vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP)
49 , m_vk (context.getDeviceInterface())
50 , m_vertexShaderName (vertexShaderName)
51 , m_fragmentShaderName (fragmentShaderName)
55 void DynamicStateBaseClass::initialize (void)
57 const vk::VkDevice device = m_context.getDevice();
58 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
60 const PipelineLayoutCreateInfo pipelineLayoutCreateInfo;
61 m_pipelineLayout = vk::createPipelineLayout(m_vk, device, &pipelineLayoutCreateInfo);
63 const vk::VkExtent3D targetImageExtent = { WIDTH, HEIGHT, 1 };
64 const ImageCreateInfo targetImageCreateInfo(vk::VK_IMAGE_TYPE_2D, m_colorAttachmentFormat, targetImageExtent, 1, 1, vk::VK_SAMPLE_COUNT_1_BIT,
65 vk::VK_IMAGE_TILING_OPTIMAL, vk::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | vk::VK_IMAGE_USAGE_TRANSFER_SRC_BIT);
67 m_colorTargetImage = Image::createAndAlloc(m_vk, device, targetImageCreateInfo, m_context.getDefaultAllocator());
69 const ImageViewCreateInfo colorTargetViewInfo(m_colorTargetImage->object(), vk::VK_IMAGE_VIEW_TYPE_2D, m_colorAttachmentFormat);
70 m_colorTargetView = vk::createImageView(m_vk, device, &colorTargetViewInfo);
72 RenderPassCreateInfo renderPassCreateInfo;
73 renderPassCreateInfo.addAttachment(AttachmentDescription(m_colorAttachmentFormat,
74 vk::VK_SAMPLE_COUNT_1_BIT,
75 vk::VK_ATTACHMENT_LOAD_OP_LOAD,
76 vk::VK_ATTACHMENT_STORE_OP_STORE,
77 vk::VK_ATTACHMENT_LOAD_OP_DONT_CARE,
78 vk::VK_ATTACHMENT_STORE_OP_STORE,
79 vk::VK_IMAGE_LAYOUT_GENERAL,
80 vk::VK_IMAGE_LAYOUT_GENERAL));
82 const vk::VkAttachmentReference colorAttachmentReference =
85 vk::VK_IMAGE_LAYOUT_GENERAL
88 renderPassCreateInfo.addSubpass(SubpassDescription(
89 vk::VK_PIPELINE_BIND_POINT_GRAPHICS,
94 &colorAttachmentReference,
96 AttachmentReference(),
102 m_renderPass = vk::createRenderPass(m_vk, device, &renderPassCreateInfo);
104 std::vector<vk::VkImageView> colorAttachments(1);
105 colorAttachments[0] = *m_colorTargetView;
107 const FramebufferCreateInfo framebufferCreateInfo(*m_renderPass, colorAttachments, WIDTH, HEIGHT, 1);
109 m_framebuffer = vk::createFramebuffer(m_vk, device, &framebufferCreateInfo);
111 const vk::VkVertexInputBindingDescription vertexInputBindingDescription =
114 (deUint32)sizeof(tcu::Vec4) * 2,
115 vk::VK_VERTEX_INPUT_RATE_VERTEX,
118 const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[2] =
123 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
129 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
130 (deUint32)(sizeof(float)* 4),
134 m_vertexInputState = PipelineCreateInfo::VertexInputState(
136 &vertexInputBindingDescription,
138 vertexInputAttributeDescriptions);
140 const vk::VkDeviceSize dataSize = m_data.size() * sizeof(PositionColorVertex);
141 m_vertexBuffer = Buffer::createAndAlloc(m_vk, device, BufferCreateInfo(dataSize, vk::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT),
142 m_context.getDefaultAllocator(), vk::MemoryRequirement::HostVisible);
144 deUint8* ptr = reinterpret_cast<unsigned char *>(m_vertexBuffer->getBoundMemory().getHostPtr());
145 deMemcpy(ptr, &m_data[0], (size_t)dataSize);
147 vk::flushMappedMemoryRange(m_vk, device,
148 m_vertexBuffer->getBoundMemory().getMemory(),
149 m_vertexBuffer->getBoundMemory().getOffset(),
152 const CmdPoolCreateInfo cmdPoolCreateInfo(queueFamilyIndex);
153 m_cmdPool = vk::createCommandPool(m_vk, device, &cmdPoolCreateInfo);
155 const vk::VkCommandBufferAllocateInfo cmdBufferAllocateInfo =
157 vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // VkStructureType sType;
158 DE_NULL, // const void* pNext;
159 *m_cmdPool, // VkCommandPool commandPool;
160 vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY, // VkCommandBufferLevel level;
161 1u, // deUint32 bufferCount;
163 m_cmdBuffer = vk::allocateCommandBuffer(m_vk, device, &cmdBufferAllocateInfo);
165 initPipeline(device);
168 void DynamicStateBaseClass::initPipeline (const vk::VkDevice device)
170 const vk::Unique<vk::VkShaderModule> vs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
171 const vk::Unique<vk::VkShaderModule> fs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
173 const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
175 PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
176 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
177 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
178 pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
179 pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
180 pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &vkCbAttachmentState));
181 pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1));
182 pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
183 pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
184 pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
185 pipelineCreateInfo.addState(PipelineCreateInfo::DynamicState());
187 m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
190 tcu::TestStatus DynamicStateBaseClass::iterate (void)
193 return tcu::TestStatus::fail("Implement iterate() method!");
196 void DynamicStateBaseClass::beginRenderPass (void)
198 const vk::VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } };
199 beginRenderPassWithClearColor(clearColor);
202 void DynamicStateBaseClass::beginRenderPassWithClearColor (const vk::VkClearColorValue& clearColor)
204 const CmdBufferBeginInfo beginInfo;
205 m_vk.beginCommandBuffer(*m_cmdBuffer, &beginInfo);
207 initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL);
209 const ImageSubresourceRange subresourceRange(vk::VK_IMAGE_ASPECT_COLOR_BIT);
210 m_vk.cmdClearColorImage(*m_cmdBuffer, m_colorTargetImage->object(),
211 vk::VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &subresourceRange);
213 const vk::VkRect2D renderArea = { { 0, 0 }, { WIDTH, HEIGHT } };
214 const RenderPassBeginInfo renderPassBegin(*m_renderPass, *m_framebuffer, renderArea);
216 m_vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBegin, vk::VK_SUBPASS_CONTENTS_INLINE);
219 void DynamicStateBaseClass::setDynamicViewportState (const deUint32 width, const deUint32 height)
221 vk::VkViewport viewport;
224 viewport.width = static_cast<float>(width);
225 viewport.height = static_cast<float>(height);
226 viewport.minDepth = 0.0f;
227 viewport.maxDepth = 1.0f;
229 m_vk.cmdSetViewport(*m_cmdBuffer, 1, &viewport);
231 vk::VkRect2D scissor;
232 scissor.offset.x = 0;
233 scissor.offset.y = 0;
234 scissor.extent.width = width;
235 scissor.extent.height = height;
236 m_vk.cmdSetScissor(*m_cmdBuffer, 1, &scissor);
239 void DynamicStateBaseClass::setDynamicViewportState (deUint32 viewportCount, const vk::VkViewport* pViewports, const vk::VkRect2D* pScissors)
241 m_vk.cmdSetViewport(*m_cmdBuffer, viewportCount, pViewports);
242 m_vk.cmdSetScissor(*m_cmdBuffer, viewportCount, pScissors);
245 void DynamicStateBaseClass::setDynamicRasterizationState (const float lineWidth,
246 const float depthBiasConstantFactor,
247 const float depthBiasClamp,
248 const float depthBiasSlopeFactor)
250 m_vk.cmdSetLineWidth(*m_cmdBuffer, lineWidth);
251 m_vk.cmdSetDepthBias(*m_cmdBuffer, depthBiasConstantFactor, depthBiasClamp, depthBiasSlopeFactor);
254 void DynamicStateBaseClass::setDynamicBlendState (const float const1, const float const2, const float const3, const float const4)
256 float blendConstantsants[4] = { const1, const2, const3, const4 };
257 m_vk.cmdSetBlendConstants(*m_cmdBuffer, blendConstantsants);
260 void DynamicStateBaseClass::setDynamicDepthStencilState (const float minDepthBounds,
261 const float maxDepthBounds,
262 const deUint32 stencilFrontCompareMask,
263 const deUint32 stencilFrontWriteMask,
264 const deUint32 stencilFrontReference,
265 const deUint32 stencilBackCompareMask,
266 const deUint32 stencilBackWriteMask,
267 const deUint32 stencilBackReference)
269 m_vk.cmdSetDepthBounds(*m_cmdBuffer, minDepthBounds, maxDepthBounds);
270 m_vk.cmdSetStencilCompareMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontCompareMask);
271 m_vk.cmdSetStencilWriteMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontWriteMask);
272 m_vk.cmdSetStencilReference(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontReference);
273 m_vk.cmdSetStencilCompareMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackCompareMask);
274 m_vk.cmdSetStencilWriteMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackWriteMask);
275 m_vk.cmdSetStencilReference(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackReference);