Skip to content

Commit

Permalink
major rendering refactoring;
Browse files Browse the repository at this point in the history
  • Loading branch information
NateSeymour committed Dec 1, 2024
1 parent 9fa6258 commit b23a73f
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 112 deletions.
42 changes: 39 additions & 3 deletions src/calculator/renderer/VulkanBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

using namespace ui;

VulkanBuffer::VulkanBuffer(QVulkanWindow *window, BufferType type, std::size_t size) : window_(window)
VulkanBuffer::VulkanBuffer(QVulkanWindow *window, BufferType type, std::size_t element_size, std::size_t count) :
window_(window), type_(type), element_size_(element_size), count_(count)
{
this->dev_ = this->window_->vulkanInstance()->deviceFunctions(this->window_->device());

VkBufferCreateInfo buffer_info{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = size,
.size = this->GetSize(),
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};

Expand All @@ -37,9 +38,12 @@ VulkanBuffer::VulkanBuffer(QVulkanWindow *window, BufferType type, std::size_t s
throw std::runtime_error("failed to create camera buffer");
}

VkMemoryRequirements memory_requirements;
this->dev_->vkGetBufferMemoryRequirements(this->window_->device(), this->buffer_, &memory_requirements);

VkMemoryAllocateInfo memory_allocate_info{
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.allocationSize = buffer_info.size,
.allocationSize = memory_requirements.size,
.memoryTypeIndex = this->window_->hostVisibleMemoryIndex(),
};

Expand All @@ -52,6 +56,38 @@ VulkanBuffer::VulkanBuffer(QVulkanWindow *window, BufferType type, std::size_t s
this->dev_->vkMapMemory(this->window_->device(), this->memory_, 0, buffer_info.size, 0, (void **)&this->data_);
}

VkBuffer VulkanBuffer::GetNativeHandle() const noexcept
{
return this->buffer_;
}

std::size_t VulkanBuffer::GetSize() const noexcept
{
return this->element_size_ * this->count_;
}

std::size_t VulkanBuffer::GetElementSize() const noexcept
{
return this->element_size_;
}

std::size_t VulkanBuffer::GetCount() const noexcept
{
return this->count_;
}

void VulkanBuffer::Bind(VkCommandBuffer cmd)
{
if (this->type_ != BufferType::Vertex)
{
throw std::runtime_error("can only bind vertex buffers");
}

std::array buffers = {this->buffer_};
constexpr std::array<VkDeviceSize, 1> offsets = {0};
this->dev_->vkCmdBindVertexBuffers(cmd, 0, 1, buffers.data(), offsets.data());
}

VulkanBuffer::~VulkanBuffer()
{
this->dev_->vkUnmapMemory(this->window_->device(), this->memory_);
Expand Down
24 changes: 16 additions & 8 deletions src/calculator/renderer/VulkanBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,41 @@ namespace ui
VkBuffer buffer_ = nullptr;
VkDeviceMemory memory_ = nullptr;

std::size_t size_ = 0;
void *data_ = nullptr;
BufferType type_;
std::size_t element_size_ = 0;
std::size_t count_ = 0;

protected:
VulkanBuffer(QVulkanWindow *window, BufferType type, std::size_t size);
void *data_ = nullptr;

public:
template<typename T>
static VulkanBuffer Create(QVulkanWindow *window, BufferType type, std::size_t count = 1)
static std::unique_ptr<VulkanBuffer> Create(QVulkanWindow *window, BufferType type, std::size_t count = 1)
{
return {window, type, sizeof(T) * count};
return std::make_unique<VulkanBuffer>(window, type, sizeof(T), count);
}

template<typename T>
void Write(T const *value, std::size_t count = 1)
{
if (this->size_ != sizeof(T) * count)
if (this->GetSize() != sizeof(T) * count)
{
throw std::runtime_error("attempting to write to buffer of incorrect size!");
}

memcpy(this->data_, &value, sizeof(T) * count);
}

[[nodiscard]] VkBuffer GetNativeHandle() const noexcept;
[[nodiscard]] std::size_t GetSize() const noexcept;
[[nodiscard]] std::size_t GetElementSize() const noexcept;
[[nodiscard]] std::size_t GetCount() const noexcept;

void Bind(VkCommandBuffer cmd);

VulkanBuffer() = delete;
VulkanBuffer(VulkanBuffer const &) = delete;
VulkanBuffer(VulkanBuffer &&) = delete;
VulkanBuffer(QVulkanWindow *window, BufferType type, std::size_t element_size, std::size_t count);

~VulkanBuffer();
};
} // namespace ui
Expand Down
77 changes: 9 additions & 68 deletions src/calculator/renderer/VulkanPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ VkShaderModule ui::VulkanPipeline::LoadShader(char const *path)
return module;
}

VkPipelineLayout ui::VulkanPipeline::GetLayout() const noexcept
{
return this->pipeline_layout_;
}

void ui::VulkanPipeline::Bind(VkCommandBuffer cmd)
{
this->dev_->vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, this->pipeline_);
Expand Down Expand Up @@ -174,29 +179,14 @@ ui::VulkanPipeline::VulkanPipeline(CreateVulkanPipelineInfo const &info) : windo
.blendConstants = {0.f, 0.f, 0.f, 0.f},
};

VkDescriptorSetLayoutBinding descriptor_layout_binding{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_ALL_GRAPHICS,
.pImmutableSamplers = nullptr,
VkPipelineDepthStencilStateCreateInfo depth_stencil_state_info{
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
};

VkDescriptorSetLayoutCreateInfo descriptor_set_layout_info{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.bindingCount = 1,
.pBindings = &descriptor_layout_binding,
};

if (this->dev_->vkCreateDescriptorSetLayout(this->window_->device(), &descriptor_set_layout_info, nullptr, &this->descriptor_set_layout_) != VK_SUCCESS)
{
throw std::runtime_error("failed to create descriptor set layout");
}

VkPipelineLayoutCreateInfo pipeline_layout_info{
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 1,
.pSetLayouts = &this->descriptor_set_layout_,
.pSetLayouts = &info.descriptor_set_layout_,
.pushConstantRangeCount = 0,
.pPushConstantRanges = nullptr,
};
Expand All @@ -215,7 +205,7 @@ ui::VulkanPipeline::VulkanPipeline(CreateVulkanPipelineInfo const &info) : windo
.pViewportState = &viewport_state_info,
.pRasterizationState = &rasterization_state_info,
.pMultisampleState = &multisample_state_info,
.pDepthStencilState = nullptr,
.pDepthStencilState = &depth_stencil_state_info,
.pColorBlendState = &color_blend_state_info,
.pDynamicState = &dynamic_state_info,
.layout = this->pipeline_layout_,
Expand All @@ -236,7 +226,6 @@ ui::VulkanPipeline::VulkanPipeline(CreateVulkanPipelineInfo const &info) : windo

void ui::VulkanPipeline::Destroy()
{
this->dev_->vkDestroyDescriptorSetLayout(this->window_->device(), this->descriptor_set_layout_, nullptr);
this->dev_->vkDestroyPipeline(this->window_->device(), this->pipeline_, nullptr);
this->dev_->vkDestroyPipelineLayout(this->window_->device(), this->pipeline_layout_, nullptr);
}
Expand All @@ -252,54 +241,6 @@ this->dev_->vkDestroyBuffer(this->window_->device(), this->ubo_buffer_, nullptr)
this->dev_->vkDestroyDescriptorPool(this->window_->device(), this->descriptor_pool_, nullptr);
VkDescriptorPoolSize descriptor_pool_size{
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
};
VkDescriptorPoolCreateInfo descriptor_pool_info{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.maxSets = 1,
.poolSizeCount = 1,
.pPoolSizes = &descriptor_pool_size,
};
if (this->dev_->vkCreateDescriptorPool(this->window_->device(), &descriptor_pool_info, nullptr, &this->descriptor_pool_) != VK_SUCCESS)
{
throw std::runtime_error("failed to create descriptor pool");
}
VkDescriptorSetAllocateInfo descriptor_set_allocate_info{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.descriptorPool = this->descriptor_pool_,
.descriptorSetCount = 1,
.pSetLayouts = &this->descriptor_set_layout_,
};
if (this->dev_->vkAllocateDescriptorSets(this->window_->device(), &descriptor_set_allocate_info, &this->descriptor_set_) != VK_SUCCESS)
{
throw std::runtime_error("failed to allocate descriptor set");
}
VkDescriptorBufferInfo descriptor_buffer_info{
.buffer = this->ubo_buffer_,
.offset = 0,
.range = sizeof(unlogic::Camera),
};
VkWriteDescriptorSet write_descriptor_set{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = this->descriptor_set_,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.pBufferInfo = &descriptor_buffer_info,
};
this->dev_->vkUpdateDescriptorSets(this->window_->device(), 1, &write_descriptor_set, 0, nullptr);
*/

/*
Expand Down
6 changes: 5 additions & 1 deletion src/calculator/renderer/VulkanPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace ui
struct CreateVulkanPipelineInfo
{
QVulkanWindow *window = nullptr;

VkDescriptorSetLayout descriptor_set_layout_ = nullptr;

VkPrimitiveTopology topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
char const *vert_shader = nullptr;
char const *frag_shader = nullptr;
Expand All @@ -20,14 +23,15 @@ namespace ui
QVulkanWindow *window_;
QVulkanDeviceFunctions *dev_;

VkDescriptorSetLayout descriptor_set_layout_ = nullptr;
VkPipelineLayout pipeline_layout_ = nullptr;
VkPipeline pipeline_ = nullptr;

protected:
VkShaderModule LoadShader(char const *path);

public:
[[nodiscard]] VkPipelineLayout GetLayout() const noexcept;

void Bind(VkCommandBuffer cmd);
void BindDescriptorSets(VkCommandBuffer cmd, VkDescriptorSet const *descriptor_set, std::size_t count);
void Destroy();
Expand Down
Loading

0 comments on commit b23a73f

Please sign in to comment.