Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct
- {
- VkShaderModule galaxy_vertex_module, galaxy_fragment_module, galaxy_compute_module;
- } vk_shaders;
- static void vk_shader_size_get(const char *filename, size_t *length)
- {
- FILE* fptr = fopen(filename, "rb");
- if (fptr == NULL)
- {
- printf("Failed to get file size (file location most likely changed)\n");
- }
- fseek(fptr, 0, SEEK_END);
- long len = ftell(fptr);
- *length = (size_t) len;
- if (len < 0)
- {
- printf("Failed to determine the size of the file\n");
- }
- fseek(fptr, 0, SEEK_SET);
- fclose(fptr);
- }
- static uint32_t *vk_shaders_load(const char *filename, size_t length, char *contents)
- {
- FILE* fptr = fopen(filename, "rb");
- if (fptr == NULL)
- {
- printf("Failed to open file\n");
- }
- fseek(fptr, 0, SEEK_END);
- length = ftell(fptr);
- fseek(fptr, 0, SEEK_SET);
- size_t read_count = fread(contents, length, 1, fptr);
- if (read_count == 0)
- {
- printf("Failed to read SPIR-V files\n");
- }
- fclose(fptr);
- return (uint32_t *) contents;
- }
- static VkShaderModuleCreateInfo vk_shader_module_info_init(const uint32_t *shader, const size_t shader_size)
- {
- VkShaderModuleCreateInfo module_info =
- {
- .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
- .pNext = NULL,
- .flags = 0,
- .codeSize = shader_size,
- .pCode = shader
- };
- return module_info;
- }
- static VkShaderModule vk_shader_module_init(vk_device *d, const uint32_t *shader, const size_t shader_size)
- {
- VkResult result = VK_SUCCESS;
- VkShaderModule shader_module = {0};
- VkShaderModuleCreateInfo module_info = vk_shader_module_info_init(shader, shader_size);
- result = vkCreateShaderModule(d->device, &module_info, NULL, &shader_module);
- if (result != VK_SUCCESS)
- {
- printf("Failed to create shader module\n");
- }
- return shader_module;
- }
- static VkShaderModule vk_vertex_shader_init(vk_device *d, const char *path)
- {
- size_t vert_spirv_size = 0;
- vk_shader_size_get(path, &vert_spirv_size);
- char vert_contents[UINT16_MAX] = {0};
- uint32_t *vert_spirv = vk_shaders_load(path, vert_spirv_size, vert_contents);
- if (vert_spirv == NULL)
- {
- printf("Failed to load vertex shader\n");
- }
- VkShaderModule vertex = vk_shader_module_init(d, vert_spirv, vert_spirv_size);
- return vertex;
- }
- static VkShaderModule vk_fragment_shader_init(vk_device *d, const char *path)
- {
- size_t frag_spirv_size = 0;
- vk_shader_size_get(path, &frag_spirv_size);
- char frag_contents[UINT16_MAX] = {0};
- uint32_t *frag_spirv = vk_shaders_load(path, frag_spirv_size, frag_contents);
- if (frag_spirv == NULL)
- {
- printf("Failed to load fragment shader\n");
- }
- VkShaderModule fragment = vk_shader_module_init(d, frag_spirv, frag_spirv_size);
- return fragment;
- }
- static VkShaderModule vk_compute_shader_init(vk_device *d, const char *path)
- {
- size_t compute_spirv_size = 0;
- vk_shader_size_get(path, &compute_spirv_size);
- char compute_contents[UINT16_MAX] = {0};
- uint32_t *compute_spirv = vk_shaders_load(path, compute_spirv_size, compute_contents);
- if (compute_spirv == NULL)
- {
- printf("Failed to load compute shader\n");
- }
- VkShaderModule compute = vk_shader_module_init(d, compute_spirv, compute_spirv_size);
- return compute;
- }
- vk_shaders vk_shaders_init(vk_device *d)
- {
- vk_shaders s =
- {
- .galaxy_vertex_module = vk_vertex_shader_init(d, "/shaders/galaxy.vert.spv"),
- .galaxy_fragment_module = vk_fragment_shader_init(d, "/shaders/galaxy.frag.spv"),
- .galaxy_compute_module = vk_compute_shader_init(d, "/shaders/galaxy.comp.spv")
- };
- return s;
- }
- void vk_shaders_destroy(vk_device *d, vk_shaders *s)
- {
- vkDestroyShaderModule(d->device, s->galaxy_vertex_module, NULL);
- vkDestroyShaderModule(d->device, s->galaxy_fragment_module, NULL);
- vkDestroyShaderModule(d->device, s->galaxy_compute_module, NULL);
- s = NULL;
- }
- typedef struct
- {
- VkCommandBuffer graphics_cmds[MAX_FRAMES_IN_FLIGHT], compute_cmds[MAX_FRAMES_IN_FLIGHT];
- VkRenderPass render_pass;
- vk_desc_set galaxy_set, compute_set;
- VkPipelineLayout compute_layout, galaxy_layout;
- VkPipeline galaxy, compute;
- } vk_pipeline;
- static VkPipelineShaderStageCreateInfo vk_pipeline_shader_info_init(VkShaderModule module, VkShaderStageFlagBits stage, const char *entry_name)
- {
- VkPipelineShaderStageCreateInfo shader_info =
- {
- .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
- .pNext = NULL,
- .flags = 0,
- .stage = stage,
- .module = module,
- .pName = entry_name,
- .pSpecializationInfo = NULL
- };
- return shader_info;
- }
- static VkPipelineVertexInputStateCreateInfo vk_pipeline_vertex_input_info_init(VkVertexInputBindingDescription *binding_desc, uint32_t binding_desc_count, VkVertexInputAttributeDescription *attrib_descs, uint32_t attrib_desc_count)
- {
- VkPipelineVertexInputStateCreateInfo vertex_input_info =
- {
- .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
- .pNext = NULL,
- .flags = 0,
- .vertexBindingDescriptionCount = binding_desc_count,
- .pVertexBindingDescriptions = binding_desc,
- .vertexAttributeDescriptionCount = attrib_desc_count,
- .pVertexAttributeDescriptions = attrib_descs
- };
- return vertex_input_info;
- }
- static VkPipelineInputAssemblyStateCreateInfo vk_pipeline_input_assembly_info_init(void)
- {
- VkPipelineInputAssemblyStateCreateInfo input_assembly_info =
- {
- .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
- .pNext = NULL,
- .flags = 0,
- .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
- .primitiveRestartEnable = VK_FALSE
- };
- return input_assembly_info;
- }
- static VkPipelineViewportStateCreateInfo vk_pipeline_viewport_state_info_init(vk_swapchain *s)
- {
- VkPipelineViewportStateCreateInfo viewport_info =
- {
- .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
- .pNext = NULL,
- .flags = 0,
- .viewportCount = 1,
- .pViewports = &s->viewport,
- .scissorCount = 1,
- .pScissors = &s->scissor
- };
- return viewport_info;
- }
- static VkPipelineRasterizationStateCreateInfo vk_pipeline_rasterization_info_init(VkPolygonMode mode)
- {
- VkPipelineRasterizationStateCreateInfo rasterization_info =
- {
- .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
- .pNext = NULL,
- .flags = 0,
- .depthClampEnable = VK_FALSE,
- .rasterizerDiscardEnable = VK_FALSE,
- .polygonMode = mode,
- .lineWidth = 1.0,
- .cullMode = VK_CULL_MODE_NONE,
- .frontFace = VK_FRONT_FACE_CLOCKWISE,
- .depthBiasEnable = VK_FALSE,
- .depthBiasConstantFactor = 0.0,
- .depthBiasClamp = 0.0,
- .depthBiasSlopeFactor = 0.0
- };
- return rasterization_info;
- }
- static VkPipelineMultisampleStateCreateInfo vk_pipeline_multisample_info_init(void)
- {
- VkPipelineMultisampleStateCreateInfo multisample_info =
- {
- .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
- .pNext = NULL,
- .flags = 0,
- .sampleShadingEnable = VK_FALSE,
- .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
- .minSampleShading = 1.0,
- .pSampleMask = NULL,
- .alphaToCoverageEnable = VK_FALSE,
- .alphaToOneEnable = VK_FALSE
- };
- return multisample_info;
- }
- static VkPipelineDepthStencilStateCreateInfo vk_pipeline_depth_stencil_info_init(void)
- {
- VkPipelineDepthStencilStateCreateInfo depth_stencil_info =
- {
- .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
- .pNext = NULL,
- .flags = 0,
- .depthTestEnable = VK_TRUE,
- .depthWriteEnable = VK_TRUE,
- .depthCompareOp = VK_COMPARE_OP_LESS,
- .depthBoundsTestEnable = VK_FALSE,
- .stencilTestEnable = VK_FALSE,
- .front = {0},
- .back = {0},
- .minDepthBounds = 0.0,
- .maxDepthBounds = 1.0
- };
- return depth_stencil_info;
- }
- static VkPipelineColorBlendAttachmentState vk_pipeline_color_blend_attachment_info_init(void)
- {
- VkPipelineColorBlendAttachmentState color_blend_attachment_info =
- {
- .blendEnable = VK_FALSE,
- .srcColorBlendFactor = VK_BLEND_FACTOR_ONE,
- .dstColorBlendFactor = VK_BLEND_FACTOR_ZERO,
- .colorBlendOp = VK_BLEND_OP_ADD,
- .srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
- .dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
- .alphaBlendOp = VK_BLEND_OP_ADD,
- .colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT
- };
- return color_blend_attachment_info;
- }
- static VkPipelineColorBlendStateCreateInfo vk_pipeline_color_blend_info_init(VkPipelineColorBlendAttachmentState *color_blend_attachment)
- {
- VkPipelineColorBlendStateCreateInfo color_blend_info =
- {
- .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
- .pNext = NULL,
- .flags = 0,
- .logicOpEnable = VK_FALSE,
- .logicOp = VK_LOGIC_OP_COPY,
- .attachmentCount = 1,
- .pAttachments = color_blend_attachment,
- .blendConstants[0] = 0.0,
- .blendConstants[1] = 0.0,
- .blendConstants[2] = 0.0,
- .blendConstants[3] = 0.0
- };
- return color_blend_info;
- }
- static VkGraphicsPipelineCreateInfo vk_pipeline_info_init(VkPipelineCreateFlags flags, VkPipeline handle, int32_t index, VkPipelineShaderStageCreateInfo *shader_stages, uint32_t shader_stage_count, VkPipelineVertexInputStateCreateInfo *vertex_input, VkPipelineInputAssemblyStateCreateInfo *input_assembly, VkPipelineViewportStateCreateInfo *viewport, VkPipelineRasterizationStateCreateInfo *rasterization, VkPipelineMultisampleStateCreateInfo *multisample, VkPipelineDepthStencilStateCreateInfo *depth_stencil, VkPipelineColorBlendStateCreateInfo *color_blend, VkPipelineLayout layout, VkRenderPass render_pass)
- {
- VkGraphicsPipelineCreateInfo pipeline_info =
- {
- .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
- .pNext = NULL,
- .flags = flags,
- .stageCount = shader_stage_count,
- .pStages = shader_stages,
- .pVertexInputState = vertex_input,
- .pInputAssemblyState = input_assembly,
- .pTessellationState = NULL,
- .pViewportState = viewport,
- .pRasterizationState = rasterization,
- .pMultisampleState = multisample,
- .pDepthStencilState = depth_stencil,
- .pColorBlendState = color_blend,
- .pDynamicState = NULL,
- .layout = layout,
- .renderPass = render_pass,
- .subpass = 0,
- .basePipelineHandle = handle,
- .basePipelineIndex = index
- };
- return pipeline_info;
- }
- static VkComputePipelineCreateInfo vk_compute_pipeline_init(VkPipelineCreateFlags flags, VkPipelineShaderStageCreateInfo stage, VkPipelineLayout layout, VkPipeline handle, int32_t index)
- {
- VkComputePipelineCreateInfo compute_info =
- {
- .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
- .pNext = NULL,
- .flags = flags,
- .stage = stage,
- .layout = layout,
- .basePipelineHandle = handle,
- .basePipelineIndex = index
- };
- return compute_info;
- }
- static VkPipeline vk_pipeline_galaxy_init(vk_device *d, vk_swapchain *s, vk_shaders *sh, VkPipelineLayout galaxy_layout, VkRenderPass render_pass)
- {
- VkPipeline galaxy = {0};
- VkPipelineShaderStageCreateInfo galaxy_vertex_stage = vk_pipeline_shader_info_init(sh->galaxy_vertex_module, VK_SHADER_STAGE_VERTEX_BIT, "main");
- VkPipelineShaderStageCreateInfo galaxy_fragment_stage = vk_pipeline_shader_info_init(sh->galaxy_fragment_module, VK_SHADER_STAGE_FRAGMENT_BIT, "main");
- VkPipelineShaderStageCreateInfo galaxy_stages[2] = {galaxy_vertex_stage, galaxy_fragment_stage};
- VkPipelineVertexInputStateCreateInfo galaxy_vertex_input = vk_pipeline_vertex_input_info_init(NULL, 0, NULL, 0);
- VkPipelineInputAssemblyStateCreateInfo input_assembly = vk_pipeline_input_assembly_info_init();
- VkPipelineViewportStateCreateInfo viewport = vk_pipeline_viewport_state_info_init(s);
- VkPipelineRasterizationStateCreateInfo rasterization = vk_pipeline_rasterization_info_init(VK_POLYGON_MODE_FILL);
- VkPipelineMultisampleStateCreateInfo multisample = vk_pipeline_multisample_info_init();
- VkPipelineDepthStencilStateCreateInfo depth_stencil = vk_pipeline_depth_stencil_info_init();
- VkPipelineColorBlendAttachmentState attachment = vk_pipeline_color_blend_attachment_info_init();
- VkPipelineColorBlendStateCreateInfo color_blend = vk_pipeline_color_blend_info_init(&attachment);
- VkGraphicsPipelineCreateInfo galaxy_info = vk_pipeline_info_init(0, VK_NULL_HANDLE, -1, galaxy_stages, 2, &galaxy_vertex_input, &input_assembly, &viewport, &rasterization, &multisample, &depth_stencil, &color_blend, galaxy_layout, render_pass);
- if (vkCreateGraphicsPipelines(d->device, VK_NULL_HANDLE, 1, &galaxy_info, NULL, &galaxy) != VK_SUCCESS)
- {
- printf("Failed to create galaxy pipeline\n");
- }
- return galaxy;
- }
- static VkPipeline vk_pipeline_compute_init(vk_device *d, vk_shaders *s, VkPipelineLayout compute_layout)
- {
- VkPipeline compute = {0};
- VkPipelineShaderStageCreateInfo compute_stage = vk_pipeline_shader_info_init(s->galaxy_compute_module, VK_SHADER_STAGE_COMPUTE_BIT, "main");
- VkComputePipelineCreateInfo compute_info = vk_compute_pipeline_init(0, compute_stage, compute_layout, 0, -1);
- if (vkCreateComputePipelines(d->device, VK_NULL_HANDLE, 1, &compute_info, NULL, &compute) != VK_SUCCESS)
- {
- printf("Failed to create compute pipeline\n");
- }
- return compute;
- }
- vk_pipeline vk_pipeline_init(vk_device *d, vk_swapchain *s)
- {
- vk_shaders shaders = vk_shaders_init(d);
- VkPushConstantRange galactic = vk_push_const_init(VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(galaxy_params));
- VkPushConstantRange galactic_info = vk_push_const_init(VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(galaxy_info));
- VkDescriptorSetLayoutBinding galaxy_uniform = vk_desc_set_layout_binding_init(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT);
- VkDescriptorSetLayoutBinding galaxy_storage = vk_desc_set_layout_binding_init(1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1, VK_SHADER_STAGE_VERTEX_BIT);
- VkDescriptorSetLayoutBinding galaxy_bindings[2] = {galaxy_uniform, galaxy_storage};
- VkDescriptorPoolSize galaxy_uniform_size = vk_desc_set_pool_size_init(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
- VkDescriptorPoolSize galaxy_storage_size = vk_desc_set_pool_size_init(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1);
- VkDescriptorPoolSize galaxy_sizes[2] = {galaxy_uniform_size, galaxy_storage_size};
- vk_desc_set galaxy_set = vk_desc_set_init(d, galaxy_bindings, 2, galaxy_sizes, 2);
- vk_desc_galaxy_sets_init(d, &galaxy_set);
- VkPipelineLayout galaxy_layout = vk_pipeline_layout_init(d, &galactic_info, 1, &galaxy_set.layout, 1);
- VkDescriptorSetLayoutBinding compute_storage = vk_desc_set_layout_binding_init(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1, VK_SHADER_STAGE_COMPUTE_BIT);
- VkDescriptorPoolSize compute_storage_size = vk_desc_set_pool_size_init(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1);
- vk_desc_set compute_set = vk_desc_set_init(d, &compute_storage, 1, &compute_storage_size, 1);
- VkPipelineLayout compute_layout = vk_pipeline_layout_init(d, &galactic, 1, &compute_set.layout, 1);
- vk_desc_compute_sets_init(d, &compute_set);
- VkRenderPass render_pass = vk_render_pass_init(d, s);
- VkPipeline galaxy = vk_pipeline_galaxy_init(d, s, &shaders, galaxy_layout, render_pass);
- VkPipeline compute = vk_pipeline_compute_init(d, &shaders, compute_layout);
- vk_pipeline p =
- {
- .render_pass = render_pass,
- .galaxy_set = galaxy_set,
- .compute_set = compute_set,
- .compute_layout = compute_layout,
- .galaxy_layout = galaxy_layout,
- .galaxy = galaxy,
- .compute = compute
- };
- vk_cmd_buffers_init(d, p.graphics_cmds);
- vk_cmd_buffers_init(d, p.compute_cmds);
- vk_shaders_destroy(d, &shaders);
- return p;
- }
Advertisement
Add Comment
Please, Sign In to add comment