Skip to content

Commit

Permalink
major rendering refactoring;
Browse files Browse the repository at this point in the history
  • Loading branch information
NateSeymour committed Nov 21, 2024
1 parent a690dc8 commit a451bcb
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 199 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ add_library(unlogic STATIC
src/graphic/ugl/Vertex.h
src/graphic/Plot.h
src/graphic/Plot.cpp
src/graphic/VertexBuffer.h
)
target_link_libraries(unlogic PUBLIC ${llvm_libs} glm::glm buffalo)
target_include_directories(unlogic PUBLIC ${LLVM_INCLUDE_DIRS} src)
Expand All @@ -78,6 +79,7 @@ qt_add_executable(unlogic-calculator
src/calculator/renderer/VulkanInstance.h
src/calculator/renderer/VulkanInstance.cpp
src/calculator/renderer/VulkanRenderer.cpp
src/calculator/renderer/VulkanVertexBuffer.h
src/calculator/resource/shaders/plot.frag
src/calculator/resource/shaders/plot.vert
)
Expand Down
5 changes: 3 additions & 2 deletions src/calculator/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <QTextEdit>
#include <QSplitter>
#include "renderer/VulkanInstance.h"
#include "renderer/VulkanVertexBuffer.h"
#include "renderer/VulkanWindow.h"

namespace ui
Expand All @@ -40,8 +41,6 @@ namespace ui
public:
Window()
{
this->scene_ = std::make_unique<unlogic::Scene>();

auto body = new QWidget;
auto main_layout = new QVBoxLayout;

Expand All @@ -58,6 +57,8 @@ namespace ui
splitter->addWidget(this->editor_);

auto render_window = new VulkanWindow;
auto vertex_buffer_provider = std::make_unique<unlogic::VulkanVertexBufferProvider>(render_window);
this->scene_ = std::make_unique<unlogic::Scene>(std::move(vertex_buffer_provider));
render_window->setVulkanInstance(ui::vk_global);
render_window->setScene(this->scene_.get());

Expand Down
67 changes: 14 additions & 53 deletions src/calculator/renderer/VulkanRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ void VulkanRenderer::initResources()

this->createStandardPipeline(this->plot_pipeline_, this->plot_pipeline_layout_, ":/shaders/plot.vert.qsb", ":/shaders/plot.frag.qsb");
this->createStandardPipeline(this->graph_pipeline_, this->graph_pipeline_layout_, ":/shaders/graph.vert.qsb", ":/shaders/graph.frag.qsb");

std::array corners = {
glm::vec2 { -1.f, -1.f },
glm::vec2 { 1.f, -1.f },
glm::vec2 { 1.f, 1.f },
glm::vec2 { -1.f, 1.f },
};
this->gridlines_ = this->window_->scene->Rect(corners, unlogic::Color::Green);
}

void VulkanRenderer::releaseResources()
Expand Down Expand Up @@ -222,7 +230,7 @@ void VulkanRenderer::startNextFrame()
// Commence drawing scene
if(this->window_->scene->draw_gridlines)
{
this->drawGridlines();
this->drawVertexBuffer(this->gridlines_.get(), this->graph_pipeline_);
}

this->dev_->vkCmdEndRenderPass(cmd);
Expand Down Expand Up @@ -269,61 +277,14 @@ VkShaderModule VulkanRenderer::loadShader(std::string_view path)
return module;
}

void VulkanRenderer::drawGridlines()
void VulkanRenderer::drawVertexBuffer(unlogic::VertexBuffer *vertex_buffer, VkPipeline pipeline)
{
VkCommandBuffer cmd = this->window_->currentCommandBuffer();
this->dev_->vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);

VkBuffer buffer;
VkDeviceMemory memory;

std::array vertices = {
unlogic::Vertex {{ -1.f, -1.f }, unlogic::Color::Green},
unlogic::Vertex {{ 1.f, -1.f }, unlogic::Color::Green},
unlogic::Vertex {{ 1.f, 1.f }, unlogic::Color::Green},

unlogic::Vertex {{ 1.f, 1.f }, unlogic::Color::Blue},
unlogic::Vertex {{ -1.f, 1.f }, unlogic::Color::Blue},
unlogic::Vertex {{ -1.f, -1.f }, unlogic::Color::Blue},
};

VkBufferCreateInfo buffer_info {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = sizeof(unlogic::Vertex) * vertices.size(),
.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};

if(this->dev_->vkCreateBuffer(this->window_->device(), &buffer_info, nullptr, &buffer) != VK_SUCCESS)
{
throw std::runtime_error("failed to create buffer");
}

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

if(this->dev_->vkAllocateMemory(this->window_->device(), &memory_allocate_info, nullptr, &memory) != VK_SUCCESS)
{
throw std::runtime_error("failed to allocate memory");
}

this->dev_->vkBindBufferMemory(this->window_->device(), buffer, memory, 0);

void *data = nullptr;
this->dev_->vkMapMemory(this->window_->device(), memory, 0, buffer_info.size, 0, &data);
memcpy(data, vertices.data(), buffer_info.size);
this->dev_->vkUnmapMemory(this->window_->device(), memory);

this->dev_->vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, this->graph_pipeline_);

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

this->dev_->vkCmdDraw(cmd, vertices.size(), 1, 0, 0);

//this->dev_->vkDestroyBuffer(this->window_->device(), buffer, nullptr);
//this->dev_->vkFreeMemory(this->window_->device(), memory, nullptr);
this->dev_->vkCmdDraw(cmd, vertex_buffer->GetVertexCount(), 1, 0, 0);
}
5 changes: 4 additions & 1 deletion src/calculator/renderer/VulkanRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QVulkanWindowRenderer>
#include <QVulkanDeviceFunctions>
#include "graphic/VertexBuffer.h"

namespace ui
{
Expand All @@ -20,6 +21,8 @@ namespace ui

QVulkanDeviceFunctions *dev_ = nullptr;

std::unique_ptr<unlogic::VertexBuffer> gridlines_ = nullptr;

public:
// Vulkan Utils
VkShaderModule loadShader(std::string_view path);
Expand All @@ -31,7 +34,7 @@ namespace ui
void startNextFrame() override;

// Draw Commands
void drawGridlines();
void drawVertexBuffer(unlogic::VertexBuffer *vertex_buffer, VkPipeline pipeline);

VulkanRenderer(VulkanWindow *window) : window_(window) {}
};
Expand Down
104 changes: 104 additions & 0 deletions src/calculator/renderer/VulkanVertexBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// Created by Nathan on 11/21/2024.
//

#ifndef VULKANVERTEXBUFFER_H
#define VULKANVERTEXBUFFER_H

#include <QVulkanWindow>
#include <QVulkanDeviceFunctions>
#include "graphic/VertexBuffer.h"

namespace unlogic
{
class VulkanVertexBuffer : public VertexBuffer
{
QVulkanWindow *window_ = nullptr;
QVulkanDeviceFunctions *dev_ = nullptr;

VkBuffer vk_buffer_ = nullptr;
VkDeviceMemory vk_memory_ = nullptr;

public:
void Release() override
{
if(this->vk_buffer_)
{
this->dev_->vkDestroyBuffer(this->window_->device(), this->vk_buffer_, nullptr);
this->vk_buffer_ = nullptr;
}

if(this->vk_memory_)
{
this->dev_->vkFreeMemory(this->window_->device(), this->vk_memory_, nullptr);
this->vk_memory_ = nullptr;
}
}

void Allocate(Vertex const *vertices, std::size_t vertex_count) override
{
this->Release();
this->SetVertexCount(vertex_count);

VkBufferCreateInfo buffer_info {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = sizeof(Vertex) * vertex_count,
.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};

if(this->dev_->vkCreateBuffer(this->window_->device(), &buffer_info, nullptr, &this->vk_buffer_) != VK_SUCCESS)
{
throw std::runtime_error("failed to create buffer");
}

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

if(this->dev_->vkAllocateMemory(this->window_->device(), &memory_allocate_info, nullptr, &this->vk_memory_) != VK_SUCCESS)
{
throw std::runtime_error("failed to allocate memory");
}

this->dev_->vkBindBufferMemory(this->window_->device(), this->vk_buffer_, this->vk_memory_, 0);

void *data = nullptr;
this->dev_->vkMapMemory(this->window_->device(), this->vk_memory_, 0, buffer_info.size, 0, &data);
memcpy(data, vertices, buffer_info.size);
this->dev_->vkUnmapMemory(this->window_->device(), this->vk_memory_);
}

void *GetNativeHandle() override
{
return (void*)this->vk_buffer_;
}

VulkanVertexBuffer(QVulkanWindow *window) : window_(window)
{
this->dev_ = this->window_->vulkanInstance()->deviceFunctions(this->window_->device());
}

~VulkanVertexBuffer() override
{
this->VulkanVertexBuffer::Release();
}
};

class VulkanVertexBufferProvider : public VertexBufferProvider
{
QVulkanWindow *window_ = nullptr;

public:
std::unique_ptr<VertexBuffer> GetVertexBuffer() override
{
return std::make_unique<VulkanVertexBuffer>(this->window_);
}

VulkanVertexBufferProvider(QVulkanWindow *window) : window_(window) {}
};
}

#endif //VULKANVERTEXBUFFER_H
44 changes: 0 additions & 44 deletions src/graphic/Graph.cpp

This file was deleted.

50 changes: 0 additions & 50 deletions src/graphic/Graph.h

This file was deleted.

3 changes: 3 additions & 0 deletions src/graphic/Plot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Created by Nathan on 11/14/2024.
//
#include <numbers>
#include <vector>
#include "Plot.h"
#include "ugl/Vertex.h"

Expand All @@ -21,6 +22,7 @@ Plot2d::Plot2d(std::string name, Plot2dFunctionType fn) : name_(std::move(name))
points.emplace_back(x, y);
}

/*
this->line_.vertices.clear();
this->line_.vertices.reserve(points.size() * 2);
double dx = 0.0;
Expand All @@ -44,4 +46,5 @@ Plot2d::Plot2d(std::string name, Plot2dFunctionType fn) : name_(std::move(name))
this->line_.vertices.push_back({glm::vec2(point.x + tx, point.y - ty), this->color_}); // a1
this->line_.vertices.push_back({glm::vec2(point.x - tx, point.y + ty), this->color_}); // a2
}
*/
}
3 changes: 1 addition & 2 deletions src/graphic/Plot.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include <string>
#include <glm/glm.hpp>
#include "ugl/shapes/Line.h"
#include "Color.h"

namespace unlogic
Expand All @@ -27,7 +26,7 @@ namespace unlogic
float precision_ = 0.1f;
float line_thickness_ = 10.f;

Line line_;
//Line line_;

Plot2d(std::string name, Plot2dFunctionType fn);

Expand Down
Loading

0 comments on commit a451bcb

Please sign in to comment.