Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codestyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ jobs:
if: steps.linter.outputs.checks-failed > 0
run: |
echo "cpp-linter found issues."
exit 1
exit 1
1 change: 0 additions & 1 deletion src/core/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ void Mesh::remove_model() {
}
}


void Mesh::set_transform(glm::mat4 t) {
VulkanEngine &engine = VulkanEngine::Get();

Expand Down
45 changes: 26 additions & 19 deletions src/graphics/vulkan/ComputePipeline.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,68 @@
#include "graphics/vulkan/ComputePipeline.h"

#include "graphics/vulkan/vk_initializers.h"

ComputePipeline::ComputePipeline(const ComputePipelineConfig& config)
: _config(config) {
}
ComputePipeline::ComputePipeline(const ComputePipelineConfig& config)
: _config(config) {}

void ComputePipeline::init(VkDevice device) {
_device = device;

VkPipelineLayoutCreateInfo computeLayout{};
computeLayout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
computeLayout.pNext = nullptr;
computeLayout.pSetLayouts = &_config.descriptorSetLayout;
computeLayout.setLayoutCount = 1;

VK_CHECK(vkCreatePipelineLayout(_device, &computeLayout, nullptr, &_pipelineLayout));

VK_CHECK(vkCreatePipelineLayout(_device, &computeLayout, nullptr,
&_pipelineLayout));

VkShaderModule computeShader;
if (!vkutil::load_shader_module(_config.shaderPath.c_str(), _device, &computeShader)) {
if (!vkutil::load_shader_module(_config.shaderPath.c_str(), _device,
&computeShader)) {
fmt::println("Error when building the compute shader \n");
return;
}

VkPipelineShaderStageCreateInfo stageInfo{};
stageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stageInfo.pNext = nullptr;
stageInfo.stage = VK_SHADER_STAGE_COMPUTE_BIT;
stageInfo.module = computeShader;
stageInfo.pName = "main";

VkComputePipelineCreateInfo computePipelineCreateInfo{};
computePipelineCreateInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
computePipelineCreateInfo.sType =
VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
computePipelineCreateInfo.pNext = nullptr;
computePipelineCreateInfo.layout = _pipelineLayout;
computePipelineCreateInfo.stage = stageInfo;

VK_CHECK(vkCreateComputePipelines(_device, VK_NULL_HANDLE, 1,
&computePipelineCreateInfo, nullptr,
&_pipeline));
&computePipelineCreateInfo, nullptr,
&_pipeline));

if (_config.customSetupCallback) {
_config.customSetupCallback(_device, _pipeline, _pipelineLayout);
}

vkDestroyShaderModule(_device, computeShader, nullptr);
}

void ComputePipeline::bind(VkCommandBuffer cmd) {
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, _pipeline);
}

void ComputePipeline::bindDescriptorSets(VkCommandBuffer cmd, const VkDescriptorSet* descriptorSets, uint32_t setCount) {
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, _pipelineLayout,
0, setCount, descriptorSets, 0, nullptr);
void ComputePipeline::bindDescriptorSets(VkCommandBuffer cmd,
const VkDescriptorSet* descriptorSets,
uint32_t setCount) {
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE,
_pipelineLayout, 0, setCount, descriptorSets, 0,
nullptr);
}

void ComputePipeline::dispatch(VkCommandBuffer cmd, uint32_t x, uint32_t y, uint32_t z) {
void ComputePipeline::dispatch(VkCommandBuffer cmd, uint32_t x, uint32_t y,
uint32_t z) {
vkCmdDispatch(cmd, x, y, z);
}

Expand Down
60 changes: 34 additions & 26 deletions src/graphics/vulkan/GraphicsPipeline.cpp
Original file line number Diff line number Diff line change
@@ -1,68 +1,73 @@
#include "graphics/vulkan/GraphicsPipeline.h"

GraphicsPipeline::GraphicsPipeline(const GraphicsPipelineConfig& config)
: _config(config) {
}
GraphicsPipeline::GraphicsPipeline(const GraphicsPipelineConfig& config)
: _config(config) {}

void GraphicsPipeline::init(VkDevice device) {
_device = device;

VkPipelineLayoutCreateInfo layoutInfo = vkinit::pipeline_layout_create_info();


VkPipelineLayoutCreateInfo layoutInfo =
vkinit::pipeline_layout_create_info();

if (!_config.descriptorSetLayouts.empty()) {
layoutInfo.setLayoutCount = static_cast<uint32_t>(_config.descriptorSetLayouts.size());
layoutInfo.setLayoutCount =
static_cast<uint32_t>(_config.descriptorSetLayouts.size());
layoutInfo.pSetLayouts = _config.descriptorSetLayouts.data();
}

if (!_config.pushConstants.empty()) {
layoutInfo.pushConstantRangeCount = static_cast<uint32_t>(_config.pushConstants.size());
layoutInfo.pushConstantRangeCount =
static_cast<uint32_t>(_config.pushConstants.size());
layoutInfo.pPushConstantRanges = _config.pushConstants.data();
}

VK_CHECK(vkCreatePipelineLayout(device, &layoutInfo, nullptr, &_pipelineLayout));


VK_CHECK(vkCreatePipelineLayout(device, &layoutInfo, nullptr,
&_pipelineLayout));

VkShaderModule vertexShader;
if (!vkutil::load_shader_module(_config.vertexShaderPath.c_str(), _device, &vertexShader)) {
if (!vkutil::load_shader_module(_config.vertexShaderPath.c_str(), _device,
&vertexShader)) {
fmt::println("Error when building the vertex shader");
return;
}

VkShaderModule fragmentShader;
if (!vkutil::load_shader_module(_config.fragmentShaderPath.c_str(), _device, &fragmentShader)) {
if (!vkutil::load_shader_module(_config.fragmentShaderPath.c_str(), _device,
&fragmentShader)) {
fmt::println("Error when building the fragment shader");
vkDestroyShaderModule(_device, vertexShader, nullptr);
return;
}

PipelineBuilder pipelineBuilder;
pipelineBuilder.set_shaders(vertexShader, fragmentShader);
pipelineBuilder.set_input_topology(_config.topology);
pipelineBuilder.set_polygon_mode(VK_POLYGON_MODE_FILL);
pipelineBuilder.set_cull_mode(_config.cullMode, VK_FRONT_FACE_CLOCKWISE);
pipelineBuilder.set_multisampling_none();

if (_config.blending) {
pipelineBuilder.enable_blending_additive();
} else {
pipelineBuilder.disable_blending();
}

if (_config.depthTest) {
pipelineBuilder.enable_depthtest(true, _config.depthCompareOp);
} else {
pipelineBuilder.disable_depthtest();
}

pipelineBuilder.set_color_attachment_format(_config.colorFormat);
pipelineBuilder.set_depth_format(_config.depthFormat);
pipelineBuilder._pipelineLayout = _pipelineLayout;

if (_config.customPipelineSetup) {
_config.customPipelineSetup(pipelineBuilder);
}

_pipeline = pipelineBuilder.build_pipeline(_device);

vkDestroyShaderModule(_device, vertexShader, nullptr);
vkDestroyShaderModule(_device, fragmentShader, nullptr);
}
Expand All @@ -75,12 +80,15 @@ void GraphicsPipeline::bindDescriptorSets(VkCommandBuffer cmd,
const VkDescriptorSet* descriptorSets,
uint32_t setCount,
uint32_t firstSet) {
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, _pipelineLayout,
firstSet, setCount, descriptorSets, 0, nullptr);
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
_pipelineLayout, firstSet, setCount, descriptorSets,
0, nullptr);
}

void GraphicsPipeline::pushConstants(VkCommandBuffer cmd, VkShaderStageFlags stageFlags,
uint32_t offset, uint32_t size, const void* data) {
void GraphicsPipeline::pushConstants(VkCommandBuffer cmd,
VkShaderStageFlags stageFlags,
uint32_t offset, uint32_t size,
const void* data) {
vkCmdPushConstants(cmd, _pipelineLayout, stageFlags, offset, size, data);
}

Expand Down
22 changes: 12 additions & 10 deletions src/graphics/vulkan/pipelines.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "graphics/vulkan/pipelines.h"

#include "graphics/vulkan/vk_engine.h"

void GLTFMetallic_Roughness::build_pipelines(VulkanEngine* engine) {
Expand Down Expand Up @@ -78,7 +79,8 @@ void GLTFMetallic_Roughness::build_opaque_pipeline(VulkanEngine* engine,
pipelineBuilder.set_multisampling_none();
pipelineBuilder.disable_blending();
pipelineBuilder.enable_depthtest(true, VK_COMPARE_OP_GREATER_OR_EQUAL);
pipelineBuilder.set_color_attachment_format(engine->_drawImage->get().imageFormat);
pipelineBuilder.set_color_attachment_format(
engine->_drawImage->get().imageFormat);
pipelineBuilder.set_depth_format(engine->_depthImage->get().imageFormat);
pipelineBuilder._pipelineLayout = layout;

Expand Down Expand Up @@ -156,7 +158,7 @@ void Pipelines::init(VkDevice device,

trianglePipeline = std::make_unique<GraphicsPipeline>(triangleConfig);
trianglePipeline->init(device);

// Mesh pipeline config
GraphicsPipeline::GraphicsPipelineConfig meshConfig;
meshConfig.vertexShaderPath = "./shaders/colored_triangle_mesh.vert.spv";
Expand All @@ -167,30 +169,30 @@ void Pipelines::init(VkDevice device,
meshConfig.depthCompareOp = VK_COMPARE_OP_GREATER;
meshConfig.cullMode = VK_CULL_MODE_NONE;
meshConfig.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
// Explicitly setup pipeline details via callback

// Explicitly setup pipeline details via callback
meshConfig.customPipelineSetup = [](PipelineBuilder& builder) {
builder.enable_depthtest(true, VK_COMPARE_OP_GREATER);
builder.set_multisampling_none();
builder.set_polygon_mode(VK_POLYGON_MODE_FILL);
};

VkPushConstantRange bufferRange{};
bufferRange.offset = 0;
bufferRange.size = sizeof(GPUDrawPushConstants);
bufferRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
meshConfig.pushConstants.push_back(bufferRange);

meshConfig.descriptorSetLayouts.push_back(_singleImageDescriptorLayout);

meshPipeline = std::make_unique<GraphicsPipeline>(meshConfig);
meshPipeline->init(device);

// Compute pipeline
ComputePipeline::ComputePipelineConfig gradientConfig;
gradientConfig.descriptorSetLayout = _drawImageDescriptorLayout;
gradientConfig.shaderPath = "./shaders/gradient.comp.spv";

gradientPipeline = std::make_unique<ComputePipeline>(gradientConfig);
gradientPipeline->init(device);

Expand All @@ -201,11 +203,11 @@ void Pipelines::destroy() {
if (trianglePipeline) {
trianglePipeline->destroy();
}

if (meshPipeline) {
meshPipeline->destroy();
}

if (gradientPipeline) {
gradientPipeline->destroy();
}
Expand Down
49 changes: 32 additions & 17 deletions src/graphics/vulkan/vk_command_buffers.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "graphics/vulkan/vk_command_buffers.h"

#include "graphics/vulkan/vk_engine.h"
#include "graphics/vulkan/vk_initializers.h"

Expand All @@ -14,14 +15,15 @@ void CommandBuffers::init_commands(VulkanEngine* vk_engine) {
for (auto& _frame : vk_engine->command_buffers_container._frames) {
VkCommandPool commandPool;
VK_CHECK(vkCreateCommandPool(vk_engine->_device, &commandPoolInfo,
nullptr,
&commandPool));
nullptr, &commandPool));

_frame._commandPool = std::make_unique<VulkanCommandPool>(vk_engine->_device, commandPool);
_frame._commandPool = std::make_unique<VulkanCommandPool>(
vk_engine->_device, commandPool);

// allocate the default command buffer that we will use for rendering
VkCommandBufferAllocateInfo cmdAllocInfo =
vkinit::command_buffer_allocate_info(_frame._commandPool->get(), 1);
vkinit::command_buffer_allocate_info(_frame._commandPool->get(),
1);

VK_CHECK(vkAllocateCommandBuffers(vk_engine->_device, &cmdAllocInfo,
&_frame._mainCommandBuffer));
Expand All @@ -31,25 +33,35 @@ void CommandBuffers::init_commands(VulkanEngine* vk_engine) {
VK_CHECK(vkCreateCommandPool(vk_engine->_device, &commandPoolInfo, nullptr,
&immCommandPool));

vk_engine->command_buffers_container._immCommandPool = std::make_unique<VulkanCommandPool>(vk_engine->_device, immCommandPool);
vk_engine->command_buffers_container._immCommandPool =
std::make_unique<VulkanCommandPool>(vk_engine->_device,
immCommandPool);

// allocate the command buffer for immediate submits
const VkCommandBufferAllocateInfo cmdAllocInfo =
vkinit::command_buffer_allocate_info(vk_engine->command_buffers_container._immCommandPool->get(), 1);
vkinit::command_buffer_allocate_info(
vk_engine->command_buffers_container._immCommandPool->get(),
1);

VK_CHECK(vkAllocateCommandBuffers(vk_engine->_device, &cmdAllocInfo,
&(vk_engine->command_buffers_container._immCommandBuffer)));
VK_CHECK(vkAllocateCommandBuffers(
vk_engine->_device, &cmdAllocInfo,
&(vk_engine->command_buffers_container._immCommandBuffer)));

// Smart pointer will automatically handle cleanup - no need for deletion queue
// Smart pointer will automatically handle cleanup - no need for deletion
// queue
}

void CommandBuffers::immediate_submit(
std::function<void(VkCommandBuffer cmd)>&& function,
VulkanEngine* vk_engine) const {
VK_CHECK(vkResetFences(vk_engine->_device, 1, vk_engine->command_buffers_container._immFence->getPtr()));
VK_CHECK(vkResetCommandBuffer(vk_engine->command_buffers_container._immCommandBuffer, 0));
VK_CHECK(vkResetFences(
vk_engine->_device, 1,
vk_engine->command_buffers_container._immFence->getPtr()));
VK_CHECK(vkResetCommandBuffer(
vk_engine->command_buffers_container._immCommandBuffer, 0));

const VkCommandBuffer cmd = vk_engine->command_buffers_container._immCommandBuffer;
const VkCommandBuffer cmd =
vk_engine->command_buffers_container._immCommandBuffer;

const VkCommandBufferBeginInfo cmdBeginInfo =
vkinit::command_buffer_begin_info(
Expand All @@ -68,9 +80,12 @@ void CommandBuffers::immediate_submit(

// submit command buffer to the queue and execute it.
// _renderFence will now block until the graphic commands finish execution
VK_CHECK(vkQueueSubmit2(vk_engine->_graphicsQueue, 1, &submit,
vk_engine->command_buffers_container._immFence->get()));

VK_CHECK(vkWaitForFences(vk_engine->_device, 1, vk_engine->command_buffers_container._immFence->getPtr(), true,
9999999999));
VK_CHECK(vkQueueSubmit2(
vk_engine->_graphicsQueue, 1, &submit,
vk_engine->command_buffers_container._immFence->get()));

VK_CHECK(vkWaitForFences(
vk_engine->_device, 1,
vk_engine->command_buffers_container._immFence->getPtr(), true,
9999999999));
}
Loading
Loading