Chillzy

Untitled

Nov 6th, 2024
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.47 KB | Source Code | 0 0
  1. typedef struct
  2. {
  3.     VkShaderModule galaxy_vertex_module, galaxy_fragment_module, galaxy_compute_module;
  4. }   vk_shaders;
  5.  
  6. static void vk_shader_size_get(const char *filename, size_t *length)
  7. {
  8.     FILE* fptr = fopen(filename, "rb");
  9.  
  10.     if (fptr == NULL)
  11.     {
  12.         printf("Failed to get file size (file location most likely changed)\n");
  13.     }
  14.  
  15.     fseek(fptr, 0, SEEK_END);
  16.     long len = ftell(fptr);
  17.     *length = (size_t) len;
  18.  
  19.     if (len < 0)
  20.     {
  21.         printf("Failed to determine the size of the file\n");
  22.     }
  23.  
  24.     fseek(fptr, 0, SEEK_SET);
  25.     fclose(fptr);
  26. }
  27.  
  28. static uint32_t *vk_shaders_load(const char *filename, size_t length, char *contents)
  29. {
  30.     FILE* fptr = fopen(filename, "rb");
  31.  
  32.     if (fptr == NULL)
  33.     {
  34.         printf("Failed to open file\n");
  35.     }
  36.  
  37.     fseek(fptr, 0, SEEK_END);
  38.     length = ftell(fptr);
  39.     fseek(fptr, 0, SEEK_SET);
  40.  
  41.     size_t read_count = fread(contents, length, 1, fptr);
  42.  
  43.     if (read_count == 0)
  44.     {
  45.         printf("Failed to read SPIR-V files\n");
  46.     }
  47.  
  48.     fclose(fptr);
  49.     return (uint32_t *) contents;
  50. }
  51.  
  52. static VkShaderModuleCreateInfo vk_shader_module_info_init(const uint32_t *shader, const size_t shader_size)
  53. {
  54.     VkShaderModuleCreateInfo module_info =
  55.     {
  56.         .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
  57.         .pNext = NULL,
  58.         .flags = 0,
  59.         .codeSize = shader_size,
  60.         .pCode = shader
  61.     };
  62.  
  63.     return module_info;
  64. }
  65.  
  66. static VkShaderModule vk_shader_module_init(vk_device *d, const uint32_t *shader, const size_t shader_size)
  67. {
  68.     VkResult result = VK_SUCCESS;
  69.     VkShaderModule shader_module = {0};
  70.  
  71.     VkShaderModuleCreateInfo module_info = vk_shader_module_info_init(shader, shader_size);
  72.  
  73.     result = vkCreateShaderModule(d->device, &module_info, NULL, &shader_module);
  74.     if (result != VK_SUCCESS)
  75.     {
  76.         printf("Failed to create shader module\n");
  77.     }
  78.  
  79.     return shader_module;
  80. }
  81.  
  82. static VkShaderModule vk_vertex_shader_init(vk_device *d, const char *path)
  83. {
  84.     size_t vert_spirv_size = 0;
  85.     vk_shader_size_get(path, &vert_spirv_size);
  86.     char vert_contents[UINT16_MAX] = {0};
  87.     uint32_t *vert_spirv = vk_shaders_load(path, vert_spirv_size, vert_contents);
  88.  
  89.     if (vert_spirv == NULL)
  90.     {
  91.         printf("Failed to load vertex shader\n");
  92.     }
  93.  
  94.     VkShaderModule vertex = vk_shader_module_init(d, vert_spirv, vert_spirv_size);
  95.  
  96.     return vertex;
  97. }
  98.  
  99. static VkShaderModule vk_fragment_shader_init(vk_device *d, const char *path)
  100. {
  101.     size_t frag_spirv_size = 0;
  102.     vk_shader_size_get(path, &frag_spirv_size);
  103.     char frag_contents[UINT16_MAX] = {0};
  104.     uint32_t *frag_spirv = vk_shaders_load(path, frag_spirv_size, frag_contents);
  105.  
  106.     if (frag_spirv == NULL)
  107.     {
  108.         printf("Failed to load fragment shader\n");
  109.     }
  110.  
  111.     VkShaderModule fragment = vk_shader_module_init(d, frag_spirv, frag_spirv_size);
  112.  
  113.     return fragment;
  114. }
  115.  
  116. static VkShaderModule vk_compute_shader_init(vk_device *d, const char *path)
  117. {
  118.     size_t compute_spirv_size = 0;
  119.     vk_shader_size_get(path, &compute_spirv_size);
  120.     char compute_contents[UINT16_MAX] = {0};
  121.     uint32_t *compute_spirv = vk_shaders_load(path, compute_spirv_size, compute_contents);
  122.  
  123.     if (compute_spirv == NULL)
  124.     {
  125.         printf("Failed to load compute shader\n");
  126.     }
  127.  
  128.     VkShaderModule compute = vk_shader_module_init(d, compute_spirv, compute_spirv_size);
  129.  
  130.     return compute;
  131. }
  132.  
  133. vk_shaders vk_shaders_init(vk_device *d)
  134. {
  135.     vk_shaders s =
  136.     {
  137.         .galaxy_vertex_module = vk_vertex_shader_init(d, "/shaders/galaxy.vert.spv"),
  138.         .galaxy_fragment_module = vk_fragment_shader_init(d, "/shaders/galaxy.frag.spv"),
  139.         .galaxy_compute_module = vk_compute_shader_init(d, "/shaders/galaxy.comp.spv")
  140.     };
  141.  
  142.     return s;
  143. }
  144.  
  145. void vk_shaders_destroy(vk_device *d, vk_shaders *s)
  146. {
  147.     vkDestroyShaderModule(d->device, s->galaxy_vertex_module, NULL);
  148.     vkDestroyShaderModule(d->device, s->galaxy_fragment_module, NULL);
  149.     vkDestroyShaderModule(d->device, s->galaxy_compute_module, NULL);
  150.     s = NULL;
  151. }
  152.  
  153. typedef struct
  154. {
  155.     VkCommandBuffer graphics_cmds[MAX_FRAMES_IN_FLIGHT], compute_cmds[MAX_FRAMES_IN_FLIGHT];
  156.     VkRenderPass render_pass;
  157.     vk_desc_set galaxy_set, compute_set;
  158.     VkPipelineLayout compute_layout, galaxy_layout;
  159.     VkPipeline galaxy, compute;
  160. }   vk_pipeline;
  161.  
  162. static VkPipelineShaderStageCreateInfo vk_pipeline_shader_info_init(VkShaderModule module, VkShaderStageFlagBits stage, const char *entry_name)
  163. {
  164.     VkPipelineShaderStageCreateInfo shader_info =
  165.     {
  166.         .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
  167.         .pNext = NULL,
  168.         .flags = 0,
  169.         .stage = stage,
  170.         .module = module,
  171.         .pName = entry_name,
  172.         .pSpecializationInfo = NULL
  173.     };
  174.  
  175.     return shader_info;
  176. }
  177.  
  178. static VkPipelineVertexInputStateCreateInfo vk_pipeline_vertex_input_info_init(VkVertexInputBindingDescription *binding_desc, uint32_t binding_desc_count, VkVertexInputAttributeDescription *attrib_descs, uint32_t attrib_desc_count)
  179. {
  180.     VkPipelineVertexInputStateCreateInfo vertex_input_info =
  181.     {
  182.         .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
  183.         .pNext = NULL,
  184.         .flags = 0,
  185.         .vertexBindingDescriptionCount = binding_desc_count,
  186.         .pVertexBindingDescriptions = binding_desc,
  187.         .vertexAttributeDescriptionCount = attrib_desc_count,
  188.         .pVertexAttributeDescriptions = attrib_descs
  189.     };
  190.  
  191.     return vertex_input_info;
  192. }
  193.  
  194. static VkPipelineInputAssemblyStateCreateInfo vk_pipeline_input_assembly_info_init(void)
  195. {
  196.     VkPipelineInputAssemblyStateCreateInfo input_assembly_info =
  197.     {
  198.         .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
  199.         .pNext = NULL,
  200.         .flags = 0,
  201.         .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
  202.         .primitiveRestartEnable = VK_FALSE
  203.     };
  204.  
  205.     return input_assembly_info;
  206. }
  207.  
  208. static VkPipelineViewportStateCreateInfo vk_pipeline_viewport_state_info_init(vk_swapchain *s)
  209. {
  210.     VkPipelineViewportStateCreateInfo viewport_info =
  211.     {
  212.         .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
  213.         .pNext = NULL,
  214.         .flags = 0,
  215.         .viewportCount = 1,
  216.         .pViewports = &s->viewport,
  217.         .scissorCount = 1,
  218.         .pScissors = &s->scissor
  219.     };
  220.  
  221.     return viewport_info;
  222. }
  223.  
  224. static VkPipelineRasterizationStateCreateInfo vk_pipeline_rasterization_info_init(VkPolygonMode mode)
  225. {
  226.     VkPipelineRasterizationStateCreateInfo rasterization_info =
  227.     {
  228.         .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
  229.         .pNext = NULL,
  230.         .flags = 0,
  231.         .depthClampEnable = VK_FALSE,
  232.         .rasterizerDiscardEnable = VK_FALSE,
  233.         .polygonMode = mode,
  234.         .lineWidth = 1.0,
  235.         .cullMode = VK_CULL_MODE_NONE,
  236.         .frontFace = VK_FRONT_FACE_CLOCKWISE,
  237.         .depthBiasEnable = VK_FALSE,
  238.         .depthBiasConstantFactor = 0.0,
  239.         .depthBiasClamp = 0.0,
  240.         .depthBiasSlopeFactor = 0.0
  241.     };
  242.  
  243.     return rasterization_info;
  244. }
  245.  
  246. static VkPipelineMultisampleStateCreateInfo vk_pipeline_multisample_info_init(void)
  247. {
  248.     VkPipelineMultisampleStateCreateInfo multisample_info =
  249.     {
  250.         .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
  251.         .pNext = NULL,
  252.         .flags = 0,
  253.         .sampleShadingEnable = VK_FALSE,
  254.         .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
  255.         .minSampleShading = 1.0,
  256.         .pSampleMask = NULL,
  257.         .alphaToCoverageEnable = VK_FALSE,
  258.         .alphaToOneEnable = VK_FALSE
  259.     };
  260.  
  261.     return multisample_info;
  262. }
  263.  
  264. static VkPipelineDepthStencilStateCreateInfo vk_pipeline_depth_stencil_info_init(void)
  265. {
  266.     VkPipelineDepthStencilStateCreateInfo depth_stencil_info =
  267.     {
  268.         .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
  269.         .pNext = NULL,
  270.         .flags = 0,
  271.         .depthTestEnable = VK_TRUE,
  272.         .depthWriteEnable = VK_TRUE,
  273.         .depthCompareOp = VK_COMPARE_OP_LESS,
  274.         .depthBoundsTestEnable = VK_FALSE,
  275.         .stencilTestEnable = VK_FALSE,
  276.         .front = {0},
  277.         .back = {0},
  278.         .minDepthBounds = 0.0,
  279.         .maxDepthBounds = 1.0
  280.     };
  281.  
  282.     return depth_stencil_info;
  283. }
  284.  
  285. static VkPipelineColorBlendAttachmentState vk_pipeline_color_blend_attachment_info_init(void)
  286. {
  287.     VkPipelineColorBlendAttachmentState color_blend_attachment_info =
  288.     {
  289.         .blendEnable = VK_FALSE,
  290.         .srcColorBlendFactor = VK_BLEND_FACTOR_ONE,
  291.         .dstColorBlendFactor = VK_BLEND_FACTOR_ZERO,
  292.         .colorBlendOp = VK_BLEND_OP_ADD,
  293.         .srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
  294.         .dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
  295.         .alphaBlendOp = VK_BLEND_OP_ADD,
  296.         .colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT
  297.     };
  298.  
  299.     return color_blend_attachment_info;
  300. }
  301.  
  302. static VkPipelineColorBlendStateCreateInfo vk_pipeline_color_blend_info_init(VkPipelineColorBlendAttachmentState *color_blend_attachment)
  303. {
  304.     VkPipelineColorBlendStateCreateInfo color_blend_info =
  305.     {
  306.         .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
  307.         .pNext = NULL,
  308.         .flags = 0,
  309.         .logicOpEnable = VK_FALSE,
  310.         .logicOp = VK_LOGIC_OP_COPY,
  311.         .attachmentCount = 1,
  312.         .pAttachments = color_blend_attachment,
  313.         .blendConstants[0] = 0.0,
  314.         .blendConstants[1] = 0.0,
  315.         .blendConstants[2] = 0.0,
  316.         .blendConstants[3] = 0.0
  317.     };
  318.  
  319.     return color_blend_info;
  320. }
  321.  
  322. 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)
  323. {
  324.     VkGraphicsPipelineCreateInfo pipeline_info =
  325.     {
  326.         .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
  327.         .pNext = NULL,
  328.         .flags = flags,
  329.         .stageCount = shader_stage_count,
  330.         .pStages = shader_stages,
  331.         .pVertexInputState = vertex_input,
  332.         .pInputAssemblyState = input_assembly,
  333.         .pTessellationState = NULL,
  334.         .pViewportState = viewport,
  335.         .pRasterizationState = rasterization,
  336.         .pMultisampleState = multisample,
  337.         .pDepthStencilState = depth_stencil,
  338.         .pColorBlendState = color_blend,
  339.         .pDynamicState = NULL,
  340.         .layout = layout,
  341.         .renderPass = render_pass,
  342.         .subpass = 0,
  343.         .basePipelineHandle = handle,
  344.         .basePipelineIndex = index
  345.     };
  346.  
  347.     return pipeline_info;
  348. }
  349.  
  350. static VkComputePipelineCreateInfo vk_compute_pipeline_init(VkPipelineCreateFlags flags, VkPipelineShaderStageCreateInfo stage, VkPipelineLayout layout, VkPipeline handle, int32_t index)
  351. {
  352.     VkComputePipelineCreateInfo compute_info =
  353.     {
  354.         .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
  355.         .pNext = NULL,
  356.         .flags = flags,
  357.         .stage = stage,
  358.         .layout = layout,
  359.         .basePipelineHandle = handle,
  360.         .basePipelineIndex = index
  361.     };
  362.  
  363.     return compute_info;
  364. }
  365.  
  366. static VkPipeline vk_pipeline_galaxy_init(vk_device *d, vk_swapchain *s, vk_shaders *sh, VkPipelineLayout galaxy_layout, VkRenderPass render_pass)
  367. {
  368.     VkPipeline galaxy = {0};
  369.  
  370.     VkPipelineShaderStageCreateInfo galaxy_vertex_stage = vk_pipeline_shader_info_init(sh->galaxy_vertex_module, VK_SHADER_STAGE_VERTEX_BIT, "main");
  371.     VkPipelineShaderStageCreateInfo galaxy_fragment_stage = vk_pipeline_shader_info_init(sh->galaxy_fragment_module, VK_SHADER_STAGE_FRAGMENT_BIT, "main");
  372.  
  373.     VkPipelineShaderStageCreateInfo galaxy_stages[2] = {galaxy_vertex_stage, galaxy_fragment_stage};
  374.  
  375.     VkPipelineVertexInputStateCreateInfo galaxy_vertex_input = vk_pipeline_vertex_input_info_init(NULL, 0, NULL, 0);
  376.     VkPipelineInputAssemblyStateCreateInfo input_assembly = vk_pipeline_input_assembly_info_init();
  377.     VkPipelineViewportStateCreateInfo viewport = vk_pipeline_viewport_state_info_init(s);
  378.     VkPipelineRasterizationStateCreateInfo rasterization = vk_pipeline_rasterization_info_init(VK_POLYGON_MODE_FILL);
  379.     VkPipelineMultisampleStateCreateInfo multisample = vk_pipeline_multisample_info_init();
  380.     VkPipelineDepthStencilStateCreateInfo depth_stencil = vk_pipeline_depth_stencil_info_init();
  381.     VkPipelineColorBlendAttachmentState attachment = vk_pipeline_color_blend_attachment_info_init();
  382.     VkPipelineColorBlendStateCreateInfo color_blend = vk_pipeline_color_blend_info_init(&attachment);
  383.  
  384.     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);
  385.  
  386.     if (vkCreateGraphicsPipelines(d->device, VK_NULL_HANDLE, 1, &galaxy_info, NULL, &galaxy) != VK_SUCCESS)
  387.     {
  388.         printf("Failed to create galaxy pipeline\n");
  389.     }
  390.  
  391.     return galaxy;
  392. }
  393.  
  394. static VkPipeline vk_pipeline_compute_init(vk_device *d, vk_shaders *s, VkPipelineLayout compute_layout)
  395. {
  396.     VkPipeline compute = {0};
  397.  
  398.     VkPipelineShaderStageCreateInfo compute_stage = vk_pipeline_shader_info_init(s->galaxy_compute_module, VK_SHADER_STAGE_COMPUTE_BIT, "main");
  399.  
  400.     VkComputePipelineCreateInfo compute_info = vk_compute_pipeline_init(0, compute_stage, compute_layout, 0, -1);
  401.  
  402.     if (vkCreateComputePipelines(d->device, VK_NULL_HANDLE, 1, &compute_info, NULL, &compute) != VK_SUCCESS)
  403.     {
  404.         printf("Failed to create compute pipeline\n");
  405.     }
  406.  
  407.     return compute;
  408. }
  409.  
  410. vk_pipeline vk_pipeline_init(vk_device *d, vk_swapchain *s)
  411. {
  412.     vk_shaders shaders = vk_shaders_init(d);
  413.  
  414.     VkPushConstantRange galactic = vk_push_const_init(VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(galaxy_params));
  415.     VkPushConstantRange galactic_info = vk_push_const_init(VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(galaxy_info));
  416.  
  417.     VkDescriptorSetLayoutBinding galaxy_uniform = vk_desc_set_layout_binding_init(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT);
  418.     VkDescriptorSetLayoutBinding galaxy_storage = vk_desc_set_layout_binding_init(1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1, VK_SHADER_STAGE_VERTEX_BIT);
  419.     VkDescriptorSetLayoutBinding galaxy_bindings[2] = {galaxy_uniform, galaxy_storage};
  420.  
  421.     VkDescriptorPoolSize galaxy_uniform_size = vk_desc_set_pool_size_init(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
  422.     VkDescriptorPoolSize galaxy_storage_size = vk_desc_set_pool_size_init(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1);
  423.     VkDescriptorPoolSize galaxy_sizes[2] = {galaxy_uniform_size, galaxy_storage_size};
  424.  
  425.     vk_desc_set galaxy_set = vk_desc_set_init(d, galaxy_bindings, 2, galaxy_sizes, 2);
  426.  
  427.     vk_desc_galaxy_sets_init(d, &galaxy_set);
  428.  
  429.     VkPipelineLayout galaxy_layout = vk_pipeline_layout_init(d, &galactic_info, 1, &galaxy_set.layout, 1);
  430.  
  431.     VkDescriptorSetLayoutBinding compute_storage = vk_desc_set_layout_binding_init(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1, VK_SHADER_STAGE_COMPUTE_BIT);
  432.  
  433.     VkDescriptorPoolSize compute_storage_size = vk_desc_set_pool_size_init(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1);
  434.  
  435.     vk_desc_set compute_set = vk_desc_set_init(d, &compute_storage, 1, &compute_storage_size, 1);
  436.  
  437.     VkPipelineLayout compute_layout = vk_pipeline_layout_init(d, &galactic, 1, &compute_set.layout, 1);
  438.  
  439.     vk_desc_compute_sets_init(d, &compute_set);
  440.  
  441.     VkRenderPass render_pass = vk_render_pass_init(d, s);
  442.  
  443.     VkPipeline galaxy = vk_pipeline_galaxy_init(d, s, &shaders, galaxy_layout, render_pass);
  444.  
  445.     VkPipeline compute = vk_pipeline_compute_init(d, &shaders, compute_layout);
  446.  
  447.     vk_pipeline p =
  448.     {
  449.         .render_pass = render_pass,
  450.         .galaxy_set = galaxy_set,
  451.         .compute_set = compute_set,
  452.         .compute_layout = compute_layout,
  453.         .galaxy_layout = galaxy_layout,
  454.         .galaxy = galaxy,
  455.         .compute = compute
  456.     };
  457.  
  458.     vk_cmd_buffers_init(d, p.graphics_cmds);
  459.     vk_cmd_buffers_init(d, p.compute_cmds);
  460.  
  461.     vk_shaders_destroy(d, &shaders);
  462.  
  463.     return p;
  464. }
Advertisement
Add Comment
Please, Sign In to add comment