Refactored the shader loading.

This commit is contained in:
Zed A. Shaw 2025-12-09 13:22:44 -05:00
parent 35198bce6b
commit 61931083c6
2 changed files with 58 additions and 52 deletions

View file

@ -410,14 +410,47 @@ void VulkanEngine::init_descriptors() {
void VulkanEngine::init_pipelines()
{
init_background_pipelines();
init_shaders();
}
void VulkanEngine::load_shader(const char *file_name, const char *entry_point, ComputeEffect sky) {
VkShaderModule skyShader;
bool good = vkutil::load_shader_module(file_name, _device, &skyShader);
assert(good && "failed to load shader");
VkPipelineShaderStageCreateInfo stageinfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_COMPUTE_BIT,
.module = skyShader,
.pName = entry_point,
};
VkComputePipelineCreateInfo computePipelineCreateInfo{
.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
.stage = stageinfo,
.layout = _gradientPipelineLayout,
};
sky.layout = _gradientPipelineLayout;
VK_CHECK(vkCreateComputePipelines(_device, VK_NULL_HANDLE, 1, &computePipelineCreateInfo, nullptr, &sky.pipeline));
backgroundEffects.push_back(sky);
vkDestroyShaderModule(_device, skyShader, nullptr);
_mainDeletionQueue.push_function([=,this]() {
vkDestroyPipeline(_device, sky.pipeline, nullptr);
});
}
void VulkanEngine::init_background_pipelines()
{
VkPushConstantRange pushConstant{
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
.offset = 0,
.size = sizeof(ComputePushConstants) ,
.size = sizeof(ComputePushConstants),
};
VkPipelineLayoutCreateInfo computeLayout{
@ -430,59 +463,28 @@ void VulkanEngine::init_background_pipelines()
VK_CHECK(vkCreatePipelineLayout(_device, &computeLayout, nullptr, &_gradientPipelineLayout));
VkShaderModule gradientShader;
bool good = vkutil::load_shader_module("gradient_color.comp.spv", _device, &gradientShader);
assert(good && "failed to load gradient_color.comp.spv");
VkShaderModule skyShader;
good = vkutil::load_shader_module("sky.comp.spv", _device, &skyShader);
VkPipelineShaderStageCreateInfo stageinfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_COMPUTE_BIT,
.module = gradientShader,
.pName = "main",
};
VkComputePipelineCreateInfo computePipelineCreateInfo{
.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
.stage = stageinfo,
.layout = _gradientPipelineLayout,
};
ComputeEffect gradient{
.name = "gradient",
.layout = _gradientPipelineLayout,
.data = {
.data1 = glm::vec4(1, 0, 0, 1),
.data2 = glm::vec4(0, 0, 1, 1),
},
};
VK_CHECK(vkCreateComputePipelines(_device, VK_NULL_HANDLE, 1, &computePipelineCreateInfo, nullptr, &gradient.pipeline));
// change the shader module only to create the sky
computePipelineCreateInfo.stage.module = skyShader;
ComputeEffect sky{
.name = "sky",
.layout = _gradientPipelineLayout,
.data{.data1 = glm::vec4(0.1, 0.2, 0.4, 0.97)},
};
VK_CHECK(vkCreateComputePipelines(_device, VK_NULL_HANDLE, 1, &computePipelineCreateInfo, nullptr, &sky.pipeline));
backgroundEffects.push_back(gradient);
backgroundEffects.push_back(sky);
vkDestroyShaderModule(_device, gradientShader, nullptr);
vkDestroyShaderModule(_device, skyShader, nullptr);
// final cleanup
_mainDeletionQueue.push_function([=,this]() {
vkDestroyPipelineLayout(_device, _gradientPipelineLayout, nullptr);
vkDestroyPipeline(_device, sky.pipeline, nullptr);
vkDestroyPipeline(_device, sky.pipeline, nullptr);
});
}
void VulkanEngine::init_shaders() {
/// gradient shader
load_shader("gradient_color.comp.spv", "main", {
.name="gradient",
.data={
.data1 = glm::vec4(1, 0, 0, 1),
.data2 = glm::vec4(0, 0, 1, 1),
}
});
load_shader("sky.comp.spv", "main", {
.name="sky",
.data = {
.data1 = glm::vec4(0.1, 0.2, 0.4, 0.97)
}
});
}

View file

@ -88,6 +88,8 @@ public:
// imgui shader stuff
std::vector<ComputeEffect> backgroundEffects;
int currentBackgroundEffect{0};
// ZED's REFACTOR
VkGUI _gui;
static VulkanEngine& Get();
@ -114,6 +116,8 @@ private:
void init_descriptors();
void init_pipelines();
void init_background_pipelines();
void init_shaders();
void load_shader(const char *file_name, const char *entry_point, ComputeEffect data);
void create_swapchain(uint32_t width, uint32_t height);
void destroy_swapchain();