diff options
author | Connor Abbott <cwabbott0@gmail.com> | 2017-06-07 18:05:49 -0700 |
---|---|---|
committer | Connor Abbott <cwabbott0@gmail.com> | 2017-06-08 14:26:53 -0700 |
commit | 37522cce1f43dfdfb29540eaa08fab2fa0a8c38d (patch) | |
tree | 4951965009fc0900442a78bad48b0785691b22bf | |
parent | cebcbbc79defebfccef67887c7c7b684ab45011b (diff) |
util: add a simple_pipeline helper function
This helper creates and submits a pipeline with a very simple vertex
shader and two triangles that cover the viewport. The user gives a
fragment shader and optionally some push constants. The idea is to avoid
boilerplate for tests that specifically test shader-only functionality.
All you need to do is create a fragment shader that renders green on
success and call this function in your test.
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | include/util/simple_pipeline.h | 26 | ||||
-rw-r--r-- | src/util/simple_pipeline.c | 162 |
3 files changed, 190 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index 656ee22..1708c7c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -112,10 +112,12 @@ bin_crucible_SOURCES = \ src/util/cru_vec.c \ src/util/string.c \ src/util/xalloc.c \ + src/util/simple_pipeline.c \ $(NULL) BUILT_SOURCES = \ src/qonos/qonos_pipeline-spirv.h \ + src/util/simple_pipeline-spirv.h \ src/tests/func/4-vertex-buffers-spirv.h \ src/tests/func/depthstencil/basic-spirv.h \ src/tests/func/depthstencil/arrayed-clear-spirv.h \ diff --git a/include/util/simple_pipeline.h b/include/util/simple_pipeline.h new file mode 100644 index 0000000..43040e0 --- /dev/null +++ b/include/util/simple_pipeline.h @@ -0,0 +1,26 @@ +// Copyright 2017 Valve Corporation +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice (including the next +// paragraph) shall be included in all copies or substantial portions of the +// Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +#include <stddef.h> +#include "vk_wrapper.h" + +void run_simple_pipeline(VkShaderModule fs, void *push_constants, + size_t push_constants_size); diff --git a/src/util/simple_pipeline.c b/src/util/simple_pipeline.c new file mode 100644 index 0000000..6e3a6f0 --- /dev/null +++ b/src/util/simple_pipeline.c @@ -0,0 +1,162 @@ +// Copyright 2015 Intel Corporation +// Copyright 2017 Valve Corporation +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice (including the next +// paragraph) shall be included in all copies or substantial portions of the +// Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +#include "util/simple_pipeline.h" + +#include "tapi/t.h" + +#include "simple_pipeline-spirv.h" + +void +run_simple_pipeline(VkShaderModule fs, void *push_constants, + size_t push_constants_size) +{ + VkRenderPass pass = qoCreateRenderPass(t_device, + .attachmentCount = 1, + .pAttachments = (VkAttachmentDescription[]) { + { + QO_ATTACHMENT_DESCRIPTION_DEFAULTS, + .format = VK_FORMAT_R8G8B8A8_UNORM, + .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, + }, + }, + .subpassCount = 1, + .pSubpasses = (VkSubpassDescription[]) { + { + QO_SUBPASS_DESCRIPTION_DEFAULTS, + .colorAttachmentCount = 1, + .pColorAttachments = (VkAttachmentReference[]) { + { + .attachment = 0, + .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + }, + }, + .preserveAttachmentCount = 1, + .pPreserveAttachments = (uint32_t[]) { 0 }, + } + }); + + VkShaderModule vs = qoCreateShaderModuleGLSL(t_device, VERTEX, + layout(location = 0) in vec4 a_position; + void main() + { + gl_Position = a_position; + } + ); + + VkPipelineVertexInputStateCreateInfo vi_info = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, + .vertexBindingDescriptionCount = 1, + .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) { + { + .binding = 0, + .stride = 8, + .inputRate = VK_VERTEX_INPUT_RATE_VERTEX + }, + }, + .vertexAttributeDescriptionCount = 1, + .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) { + { + .location = 0, + .binding = 0, + .format = VK_FORMAT_R32G32_SFLOAT, + .offset = 0 + }, + } + }; + + VkPipelineLayout layout = VK_NULL_HANDLE; + if (push_constants_size) { + VkPushConstantRange constants = { + .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, + .offset = 0, + .size = push_constants_size + }; + + layout = qoCreatePipelineLayout(t_device, + .pushConstantRangeCount = 1, + .pPushConstantRanges = &constants); + } + + VkPipeline pipeline = qoCreateGraphicsPipeline(t_device, t_pipeline_cache, + &(QoExtraGraphicsPipelineCreateInfo) { + QO_EXTRA_GRAPHICS_PIPELINE_CREATE_INFO_DEFAULTS, + .vertexShader = vs, + .fragmentShader = fs, + .pNext = + &(VkGraphicsPipelineCreateInfo) { + .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, + .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) { + QO_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO_DEFAULTS, + .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, + }, + .pVertexInputState = &vi_info, + .renderPass = pass, + .layout = layout, + .subpass = 0, + }}); + + const float vertices[] = { + -1.0, 1.0, + 1.0, 1.0, + -1.0, -1.0, + 1.0, -1.0 + }; + + VkBuffer vb = qoCreateBuffer(t_device, .size = sizeof(vertices), + .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + + VkDeviceMemory vb_mem = qoAllocBufferMemory(t_device, vb, + .properties = VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); + qoBindBufferMemory(t_device, vb, vb_mem, 0); + + void *vb_map = qoMapMemory(t_device, vb_mem, 0, sizeof(vertices), 0); + + memcpy(vb_map, vertices, sizeof(vertices)); + + vkCmdBeginRenderPass(t_cmd_buffer, + &(VkRenderPassBeginInfo) { + .renderPass = pass, + .framebuffer = t_framebuffer, + .renderArea = { { 0, 0 }, { t_width, t_height } }, + .clearValueCount = 1, + .pClearValues = (VkClearValue[]) { + { .color = { .float32 = {1.0, 0.0, 0.0, 1.0} } }, + } + }, VK_SUBPASS_CONTENTS_INLINE); + + if (push_constants_size) { + vkCmdPushConstants(t_cmd_buffer, layout, VK_SHADER_STAGE_FRAGMENT_BIT, + 0, push_constants_size, push_constants); + } + + vkCmdBindVertexBuffers(t_cmd_buffer, 0, 1, (VkBuffer[]) { vb }, + (VkDeviceSize[]) { 0 }); + + vkCmdBindPipeline(t_cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); + + vkCmdDraw(t_cmd_buffer, 4, 1, 0, 0); + + vkCmdEndRenderPass(t_cmd_buffer); + qoEndCommandBuffer(t_cmd_buffer); + qoQueueSubmit(t_queue, 1, &t_cmd_buffer, VK_NULL_HANDLE); +} |