1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2015 The Khronos Group Inc.
6 * Copyright (c) 2015 Intel Corporation
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 Dynamic State Tests - Base Class
23 *//*--------------------------------------------------------------------*/
25 #include "vktDynamicStateBaseClass.hpp"
27 #include "vkPrograms.hpp"
31 namespace DynamicState
34 DynamicStateBaseClass::DynamicStateBaseClass (Context& context, const char* vertexShaderName, const char* fragmentShaderName)
35 : TestInstance (context)
36 , m_colorAttachmentFormat (vk::VK_FORMAT_R8G8B8A8_UNORM)
37 , m_topology (vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP)
38 , m_vk (context.getDeviceInterface())
39 , m_vertexShaderName (vertexShaderName)
40 , m_fragmentShaderName (fragmentShaderName)
44 void DynamicStateBaseClass::initialize (void)
46 const vk::VkDevice device = m_context.getDevice();
47 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
49 const PipelineLayoutCreateInfo pipelineLayoutCreateInfo;
50 m_pipelineLayout = vk::createPipelineLayout(m_vk, device, &pipelineLayoutCreateInfo);
52 const vk::VkExtent3D targetImageExtent = { WIDTH, HEIGHT, 1 };
53 const ImageCreateInfo targetImageCreateInfo(vk::VK_IMAGE_TYPE_2D, m_colorAttachmentFormat, targetImageExtent, 1, 1, vk::VK_SAMPLE_COUNT_1_BIT,
54 vk::VK_IMAGE_TILING_OPTIMAL, vk::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | vk::VK_IMAGE_USAGE_TRANSFER_SRC_BIT | vk::VK_IMAGE_USAGE_TRANSFER_DST_BIT);
56 m_colorTargetImage = Image::createAndAlloc(m_vk, device, targetImageCreateInfo, m_context.getDefaultAllocator());
58 const ImageViewCreateInfo colorTargetViewInfo(m_colorTargetImage->object(), vk::VK_IMAGE_VIEW_TYPE_2D, m_colorAttachmentFormat);
59 m_colorTargetView = vk::createImageView(m_vk, device, &colorTargetViewInfo);
61 RenderPassCreateInfo renderPassCreateInfo;
62 renderPassCreateInfo.addAttachment(AttachmentDescription(m_colorAttachmentFormat,
63 vk::VK_SAMPLE_COUNT_1_BIT,
64 vk::VK_ATTACHMENT_LOAD_OP_LOAD,
65 vk::VK_ATTACHMENT_STORE_OP_STORE,
66 vk::VK_ATTACHMENT_LOAD_OP_DONT_CARE,
67 vk::VK_ATTACHMENT_STORE_OP_STORE,
68 vk::VK_IMAGE_LAYOUT_GENERAL,
69 vk::VK_IMAGE_LAYOUT_GENERAL));
71 const vk::VkAttachmentReference colorAttachmentReference =
74 vk::VK_IMAGE_LAYOUT_GENERAL
77 renderPassCreateInfo.addSubpass(SubpassDescription(
78 vk::VK_PIPELINE_BIND_POINT_GRAPHICS,
83 &colorAttachmentReference,
85 AttachmentReference(),
91 m_renderPass = vk::createRenderPass(m_vk, device, &renderPassCreateInfo);
93 std::vector<vk::VkImageView> colorAttachments(1);
94 colorAttachments[0] = *m_colorTargetView;
96 const FramebufferCreateInfo framebufferCreateInfo(*m_renderPass, colorAttachments, WIDTH, HEIGHT, 1);
98 m_framebuffer = vk::createFramebuffer(m_vk, device, &framebufferCreateInfo);
100 const vk::VkVertexInputBindingDescription vertexInputBindingDescription =
103 (deUint32)sizeof(tcu::Vec4) * 2,
104 vk::VK_VERTEX_INPUT_RATE_VERTEX,
107 const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[2] =
112 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
118 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
119 (deUint32)(sizeof(float)* 4),
123 m_vertexInputState = PipelineCreateInfo::VertexInputState(
125 &vertexInputBindingDescription,
127 vertexInputAttributeDescriptions);
129 const vk::VkDeviceSize dataSize = m_data.size() * sizeof(PositionColorVertex);
130 m_vertexBuffer = Buffer::createAndAlloc(m_vk, device, BufferCreateInfo(dataSize, vk::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT),
131 m_context.getDefaultAllocator(), vk::MemoryRequirement::HostVisible);
133 deUint8* ptr = reinterpret_cast<unsigned char *>(m_vertexBuffer->getBoundMemory().getHostPtr());
134 deMemcpy(ptr, &m_data[0], (size_t)dataSize);
136 vk::flushMappedMemoryRange(m_vk, device,
137 m_vertexBuffer->getBoundMemory().getMemory(),
138 m_vertexBuffer->getBoundMemory().getOffset(),
141 const CmdPoolCreateInfo cmdPoolCreateInfo(queueFamilyIndex);
142 m_cmdPool = vk::createCommandPool(m_vk, device, &cmdPoolCreateInfo);
144 const vk::VkCommandBufferAllocateInfo cmdBufferAllocateInfo =
146 vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // VkStructureType sType;
147 DE_NULL, // const void* pNext;
148 *m_cmdPool, // VkCommandPool commandPool;
149 vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY, // VkCommandBufferLevel level;
150 1u, // deUint32 bufferCount;
152 m_cmdBuffer = vk::allocateCommandBuffer(m_vk, device, &cmdBufferAllocateInfo);
154 initPipeline(device);
157 void DynamicStateBaseClass::initPipeline (const vk::VkDevice device)
159 const vk::Unique<vk::VkShaderModule> vs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
160 const vk::Unique<vk::VkShaderModule> fs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
162 const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
164 PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
165 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
166 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
167 pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
168 pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
169 pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &vkCbAttachmentState));
170 pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1));
171 pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
172 pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
173 pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
174 pipelineCreateInfo.addState(PipelineCreateInfo::DynamicState());
176 m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
179 tcu::TestStatus DynamicStateBaseClass::iterate (void)
182 return tcu::TestStatus::fail("Implement iterate() method!");
185 void DynamicStateBaseClass::beginRenderPass (void)
187 const vk::VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } };
188 beginRenderPassWithClearColor(clearColor);
191 void DynamicStateBaseClass::beginRenderPassWithClearColor (const vk::VkClearColorValue& clearColor)
193 const CmdBufferBeginInfo beginInfo;
194 m_vk.beginCommandBuffer(*m_cmdBuffer, &beginInfo);
196 initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL);
198 const ImageSubresourceRange subresourceRange(vk::VK_IMAGE_ASPECT_COLOR_BIT);
199 m_vk.cmdClearColorImage(*m_cmdBuffer, m_colorTargetImage->object(),
200 vk::VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &subresourceRange);
202 const vk::VkRect2D renderArea = { { 0, 0 }, { WIDTH, HEIGHT } };
203 const RenderPassBeginInfo renderPassBegin(*m_renderPass, *m_framebuffer, renderArea);
205 m_vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBegin, vk::VK_SUBPASS_CONTENTS_INLINE);
208 void DynamicStateBaseClass::setDynamicViewportState (const deUint32 width, const deUint32 height)
210 vk::VkViewport viewport;
213 viewport.width = static_cast<float>(width);
214 viewport.height = static_cast<float>(height);
215 viewport.minDepth = 0.0f;
216 viewport.maxDepth = 1.0f;
218 m_vk.cmdSetViewport(*m_cmdBuffer, 0, 1, &viewport);
220 vk::VkRect2D scissor;
221 scissor.offset.x = 0;
222 scissor.offset.y = 0;
223 scissor.extent.width = width;
224 scissor.extent.height = height;
225 m_vk.cmdSetScissor(*m_cmdBuffer, 0, 1, &scissor);
228 void DynamicStateBaseClass::setDynamicViewportState (deUint32 viewportCount, const vk::VkViewport* pViewports, const vk::VkRect2D* pScissors)
230 m_vk.cmdSetViewport(*m_cmdBuffer, 0, viewportCount, pViewports);
231 m_vk.cmdSetScissor(*m_cmdBuffer, 0, viewportCount, pScissors);
234 void DynamicStateBaseClass::setDynamicRasterizationState (const float lineWidth,
235 const float depthBiasConstantFactor,
236 const float depthBiasClamp,
237 const float depthBiasSlopeFactor)
239 m_vk.cmdSetLineWidth(*m_cmdBuffer, lineWidth);
240 m_vk.cmdSetDepthBias(*m_cmdBuffer, depthBiasConstantFactor, depthBiasClamp, depthBiasSlopeFactor);
243 void DynamicStateBaseClass::setDynamicBlendState (const float const1, const float const2, const float const3, const float const4)
245 float blendConstantsants[4] = { const1, const2, const3, const4 };
246 m_vk.cmdSetBlendConstants(*m_cmdBuffer, blendConstantsants);
249 void DynamicStateBaseClass::setDynamicDepthStencilState (const float minDepthBounds,
250 const float maxDepthBounds,
251 const deUint32 stencilFrontCompareMask,
252 const deUint32 stencilFrontWriteMask,
253 const deUint32 stencilFrontReference,
254 const deUint32 stencilBackCompareMask,
255 const deUint32 stencilBackWriteMask,
256 const deUint32 stencilBackReference)
258 m_vk.cmdSetDepthBounds(*m_cmdBuffer, minDepthBounds, maxDepthBounds);
259 m_vk.cmdSetStencilCompareMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontCompareMask);
260 m_vk.cmdSetStencilWriteMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontWriteMask);
261 m_vk.cmdSetStencilReference(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontReference);
262 m_vk.cmdSetStencilCompareMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackCompareMask);
263 m_vk.cmdSetStencilWriteMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackWriteMask);
264 m_vk.cmdSetStencilReference(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackReference);