Next part of the tutorial where we have a couple compute shaders controlled by ImGUI.
This commit is contained in:
parent
14f307b1b3
commit
a996440c61
7 changed files with 219 additions and 21 deletions
|
|
@ -19,7 +19,7 @@
|
|||
#define VMA_IMPLEMENTATION
|
||||
#include "vk_mem_alloc.h"
|
||||
|
||||
constexpr bool bUseValidationLayers = false;
|
||||
constexpr bool bUseValidationLayers = true;
|
||||
|
||||
VulkanEngine* loadedEngine = nullptr;
|
||||
|
||||
|
|
@ -202,17 +202,35 @@ void VulkanEngine::run()
|
|||
continue;
|
||||
}
|
||||
|
||||
ImGui_ImplVulkan_NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
ImGui::ShowDemoWindow();
|
||||
ImGui::Render();
|
||||
|
||||
render_imgui();
|
||||
draw();
|
||||
}
|
||||
}
|
||||
|
||||
void VulkanEngine::render_imgui() {
|
||||
ImGui_ImplVulkan_NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame();
|
||||
|
||||
ImGui::NewFrame();
|
||||
|
||||
if (ImGui::Begin("background")) {
|
||||
|
||||
ComputeEffect& selected = backgroundEffects[currentBackgroundEffect];
|
||||
|
||||
ImGui::Text("Selected effect: %s", selected.name);
|
||||
|
||||
ImGui::SliderInt("Effect Index", ¤tBackgroundEffect,0, backgroundEffects.size() - 1);
|
||||
|
||||
ImGui::InputFloat4("data1",(float*)& selected.data.data1);
|
||||
ImGui::InputFloat4("data2",(float*)& selected.data.data2);
|
||||
ImGui::InputFloat4("data3",(float*)& selected.data.data3);
|
||||
ImGui::InputFloat4("data4",(float*)& selected.data.data4);
|
||||
}
|
||||
ImGui::End();
|
||||
|
||||
ImGui::Render();
|
||||
}
|
||||
|
||||
void VulkanEngine::init_vulkan() {
|
||||
vkb::InstanceBuilder builder;
|
||||
|
||||
|
|
@ -394,13 +412,14 @@ void VulkanEngine::init_sync_structures() {
|
|||
|
||||
void VulkanEngine::draw_background(VkCommandBuffer cmd)
|
||||
{
|
||||
// bind the gradient drawing compute pipeline
|
||||
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, _gradientPipeline);
|
||||
ComputeEffect &effect = backgroundEffects[currentBackgroundEffect];
|
||||
|
||||
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, effect.pipeline);
|
||||
|
||||
// bind the descriptor set containing the draw image for the compute pipeline
|
||||
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, _gradientPipelineLayout, 0, 1, &_drawImageDescriptors, 0, nullptr);
|
||||
|
||||
// execute the compute pipeline dispatch. We are using 16x16 workgroup size so we need to divide by it
|
||||
vkCmdPushConstants(cmd, _gradientPipelineLayout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(ComputePushConstants), &effect.data);
|
||||
|
||||
vkCmdDispatch(cmd, std::ceil(_drawExtent.width / 16.0), std::ceil(_drawExtent.height / 16.0), 1);
|
||||
}
|
||||
|
||||
|
|
@ -461,19 +480,28 @@ void VulkanEngine::init_background_pipelines()
|
|||
computeLayout.pSetLayouts = &_drawImageDescriptorLayout;
|
||||
computeLayout.setLayoutCount = 1;
|
||||
|
||||
VkPushConstantRange pushConstant{};
|
||||
pushConstant.offset = 0;
|
||||
pushConstant.size = sizeof(ComputePushConstants) ;
|
||||
pushConstant.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
|
||||
|
||||
computeLayout.pPushConstantRanges = &pushConstant;
|
||||
computeLayout.pushConstantRangeCount = 1;
|
||||
|
||||
VK_CHECK(vkCreatePipelineLayout(_device, &computeLayout, nullptr, &_gradientPipelineLayout));
|
||||
|
||||
VkShaderModule computeDrawShader;
|
||||
VkShaderModule gradientShader;
|
||||
bool good = vkutil::load_shader_module("gradient_color.comp.spv", _device, &gradientShader);
|
||||
assert(good && "failed to load gradient_color.comp.spv");
|
||||
|
||||
if(!vkutil::load_shader_module("gradient.comp.spv", _device, &computeDrawShader)) {
|
||||
std::println("Error when building compute shader");
|
||||
}
|
||||
VkShaderModule skyShader;
|
||||
good = vkutil::load_shader_module("sky.comp.spv", _device, &skyShader);
|
||||
|
||||
VkPipelineShaderStageCreateInfo stageinfo{};
|
||||
stageinfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||
stageinfo.pNext = nullptr;
|
||||
stageinfo.stage = VK_SHADER_STAGE_COMPUTE_BIT;
|
||||
stageinfo.module = computeDrawShader;
|
||||
stageinfo.module = gradientShader;
|
||||
stageinfo.pName = "main";
|
||||
|
||||
VkComputePipelineCreateInfo computePipelineCreateInfo{};
|
||||
|
|
@ -482,13 +510,37 @@ void VulkanEngine::init_background_pipelines()
|
|||
computePipelineCreateInfo.layout = _gradientPipelineLayout;
|
||||
computePipelineCreateInfo.stage = stageinfo;
|
||||
|
||||
VK_CHECK(vkCreateComputePipelines(_device,VK_NULL_HANDLE,1, &computePipelineCreateInfo, nullptr, &_gradientPipeline));
|
||||
ComputeEffect gradient;
|
||||
gradient.layout = _gradientPipelineLayout;
|
||||
gradient.name = "gradient";
|
||||
gradient.data = {};
|
||||
gradient.data.data1 = glm::vec4(1, 0, 0, 1);
|
||||
gradient.data.data2 = glm::vec4(0, 0, 1, 1);
|
||||
|
||||
vkDestroyShaderModule(_device, computeDrawShader, nullptr);
|
||||
VK_CHECK(vkCreateComputePipelines(_device, VK_NULL_HANDLE, 1, &computePipelineCreateInfo, nullptr, &gradient.pipeline));
|
||||
|
||||
_mainDeletionQueue.push_function([&]() {
|
||||
// change the shader module only to create the sky
|
||||
computePipelineCreateInfo.stage.module = skyShader;
|
||||
ComputeEffect sky;
|
||||
sky.layout = _gradientPipelineLayout;
|
||||
sky.name = "sky";
|
||||
sky.data = {};
|
||||
// default sky
|
||||
sky.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);
|
||||
|
||||
_mainDeletionQueue.push_function([=,this]() {
|
||||
vkDestroyPipelineLayout(_device, _gradientPipelineLayout, nullptr);
|
||||
vkDestroyPipeline(_device, _gradientPipeline, nullptr);
|
||||
vkDestroyPipeline(_device, sky.pipeline, nullptr);
|
||||
vkDestroyPipeline(_device, sky.pipeline, nullptr);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue