Skip to content

Commit

Permalink
connect ubo binding to draw;
Browse files Browse the repository at this point in the history
  • Loading branch information
NateSeymour committed Nov 23, 2024
1 parent 29fcc7a commit b52ed91
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 48 deletions.
9 changes: 6 additions & 3 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ AlignOperands: true
AlignTrailingComments: false
AlwaysBreakTemplateDeclarations: Yes
AllowShortFunctionsOnASingleLine: Empty
BraceWrapping:
BinPackArguments: false
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
Expand All @@ -27,13 +29,13 @@ BraceWrapping:
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBraces: Custom
BracedInitializerIndentWidth: 8
BreakConstructorInitializers: AfterColon
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 180
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ContinuationIndentWidth: 8
IncludeCategories:
IncludeCategories:
- Regex: '^<.*'
Priority: 1
- Regex: '^".*'
Expand All @@ -44,6 +46,7 @@ IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentWidth: 4
InsertNewlineAtEOF: true
InsertTrailingCommas: Wrapped
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 2
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Unlogic
A math JIT compiler and graph generator.

A math JIT compiler and graph generator.

## Examples

### JIT-Compiling and Running a Function

```c++
unlogic::Compiler compiler;
auto f = compiler.CompileFunction<double, double>("f(x, y) := x * y");
Expand All @@ -12,20 +14,22 @@ ASSERT_EQ(f(2, 2), 4);
```
### Graphing a Function
```c++
sf::RenderTexture texture;
texture.create(1000, 1000);
unlogic::Graph graph("f(x) := x^2");
texture.Draw(graph);
texture.DrawVertexBuffer(graph);
sf::Image image = texture.getTexture().copyToImage();
std::vector<sf::Uint8> buffer;
image.saveToMemory(buffer, "png");
```

### Sample Render

<p align="center">
<img src="docs/public/SampleRender.png" width="500">
</p>
Expand Down
68 changes: 65 additions & 3 deletions src/calculator/renderer/VulkanPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <rhi/qshader.h>
#include "graphic/ugl/Vertex.h"

VkShaderModule ui::VulkanPipeline::loadShader(char const *path)
VkShaderModule ui::VulkanPipeline::LoadShader(char const *path)
{
QFile shader_file(path);
if (!shader_file.open(QIODevice::ReadOnly))
Expand Down Expand Up @@ -45,8 +45,8 @@ ui::VulkanPipeline::VulkanPipeline(QVulkanWindow *window, char const *vert, char
{
this->dev_ = this->window_->vulkanInstance()->deviceFunctions(this->window_->device());

VkShaderModule vert_shader = this->loadShader(vert);
VkShaderModule frag_shader = this->loadShader(frag);
VkShaderModule vert_shader = this->LoadShader(vert);
VkShaderModule frag_shader = this->LoadShader(frag);

VkPipelineShaderStageCreateInfo vert_shader_stage_info{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
Expand Down Expand Up @@ -245,13 +245,61 @@ ui::VulkanPipeline::VulkanPipeline(QVulkanWindow *window, char const *vert, char
this->dev_->vkBindBufferMemory(this->window_->device(), this->camera_buffer_, this->camera_memory_, 0);

this->dev_->vkMapMemory(this->window_->device(), this->camera_memory_, 0, camera_buffer_info.size, 0, (void **)&this->camera);

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->camera_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);
}

void ui::VulkanPipeline::Destroy()
{
this->dev_->vkDestroyBuffer(this->window_->device(), this->camera_buffer_, nullptr);
this->dev_->vkFreeMemory(this->window_->device(), this->camera_memory_, nullptr);

this->dev_->vkDestroyDescriptorPool(this->window_->device(), this->descriptor_pool_, nullptr);
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 @@ -266,3 +314,17 @@ VkPipeline ui::VulkanPipeline::NativeHandle()
{
return this->pipeline_;
}

void ui::VulkanPipeline::DrawVertexBuffer(unlogic::VertexBuffer *vertex_buffer)
{
VkCommandBuffer cmd = this->window_->currentCommandBuffer();

this->dev_->vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, this->pipeline_);
this->dev_->vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, this->pipeline_layout_, 0, 1, &this->descriptor_set_, 0, nullptr);

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, vertex_buffer->GetVertexCount(), 1, 0, 0);
}
6 changes: 5 additions & 1 deletion src/calculator/renderer/VulkanPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QVulkanDeviceFunctions>
#include <QVulkanWindow>
#include "graphic/Camera.h"
#include "graphic/VertexBuffer.h"

namespace ui
{
Expand All @@ -14,6 +15,7 @@ namespace ui

VkDescriptorPool descriptor_pool_ = nullptr;
VkDescriptorSetLayout descriptor_set_layout_ = nullptr;
VkDescriptorSet descriptor_set_ = nullptr;
VkPipelineLayout pipeline_layout_ = nullptr;
VkPipeline pipeline_ = nullptr;

Expand All @@ -23,9 +25,11 @@ namespace ui
public:
unlogic::Camera *camera = nullptr;

VkShaderModule loadShader(char const *path);
VkShaderModule LoadShader(char const *path);
VkPipeline NativeHandle();

void DrawVertexBuffer(unlogic::VertexBuffer *vertex_buffer);

void Destroy();

VulkanPipeline(QVulkanWindow *window, char const *vert, char const *frag);
Expand Down
55 changes: 22 additions & 33 deletions src/calculator/renderer/VulkanRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <array>
#include "VulkanRenderer.h"
#include <array>
#include "VulkanWindow.h"
#include "graphic/ugl/Vertex.h"

Expand All @@ -13,10 +13,10 @@ void VulkanRenderer::initResources()
this->plot_pipeline_ = std::make_unique<VulkanPipeline>(this->window_, ":/shaders/plot.vert.qsb", ":/shaders/plot.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 },
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->grid_ = this->window_->scene->Rect(corners, unlogic::Color::Green);
Expand All @@ -32,7 +32,7 @@ void VulkanRenderer::releaseResources()

void VulkanRenderer::startNextFrame()
{
if(!this->window_->scene)
if (!this->window_->scene)
{
this->window_->frameReady();
return;
Expand All @@ -48,12 +48,12 @@ void VulkanRenderer::startNextFrame()
unlogic::Color background = this->window_->scene->background;

VkClearColorValue clearColor = {
background.r,
background.g,
background.b,
background.a,
background.r,
background.g,
background.b,
background.a,
};
VkClearDepthStencilValue clearDS = { 1.0f, 0 };
VkClearDepthStencilValue clearDS = {1.0f, 0};
VkClearValue clearValues[2];
memset(clearValues, 0, sizeof(clearValues));
clearValues[0].color = clearColor;
Expand All @@ -64,7 +64,7 @@ void VulkanRenderer::startNextFrame()
rpBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
rpBeginInfo.renderPass = this->window_->defaultRenderPass();
rpBeginInfo.framebuffer = this->window_->currentFramebuffer();
const QSize sz = this->window_->swapChainImageSize();
QSize const sz = this->window_->swapChainImageSize();
rpBeginInfo.renderArea.extent.width = sz.width();
rpBeginInfo.renderArea.extent.height = sz.height();
rpBeginInfo.clearValueCount = 2;
Expand All @@ -73,7 +73,7 @@ void VulkanRenderer::startNextFrame()
this->dev_->vkCmdBeginRenderPass(cmd, &rpBeginInfo, VK_SUBPASS_CONTENTS_INLINE);

// Set Viewport
VkViewport viewport {
VkViewport viewport{
.x = 0.f,
.y = 0.f,
.width = (float)sz.width(),
Expand All @@ -83,35 +83,24 @@ void VulkanRenderer::startNextFrame()
};
this->dev_->vkCmdSetViewport(cmd, 0, 1, &viewport);

VkRect2D scissor {
.offset = { 0, 0 },
.extent = {
.width = (std::uint32_t)sz.width(),
.height = (std::uint32_t)sz.height(),
},
VkRect2D scissor{
.offset = {0, 0},
.extent =
{
.width = (std::uint32_t)sz.width(),
.height = (std::uint32_t)sz.height(),
},
};
this->dev_->vkCmdSetScissor(cmd, 0, 1, &scissor);

// Commence drawing scene
if(this->window_->scene->draw_gridlines)
if (this->window_->scene->draw_gridlines)
{
this->drawVertexBuffer(this->grid_.get(), *this->grid_pipeline_);
this->grid_pipeline_->DrawVertexBuffer(this->grid_.get());
}

this->dev_->vkCmdEndRenderPass(cmd);

this->window_->frameReady();
this->window_->requestUpdate(); // render continuously, throttled by the presentation rate
}

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

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, vertex_buffer->GetVertexCount(), 1, 0, 0);
}
9 changes: 3 additions & 6 deletions src/calculator/renderer/VulkanRenderer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef UNLOGIC_VULKANRENDERER_H
#define UNLOGIC_VULKANRENDERER_H

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

Expand All @@ -27,11 +27,8 @@ namespace ui
void releaseResources() override;
void startNextFrame() override;

// Draw Commands
void drawVertexBuffer(unlogic::VertexBuffer *vertex_buffer, VulkanPipeline &pipeline);

VulkanRenderer(VulkanWindow *window) : window_(window) {}
};
}
} // namespace ui

#endif //UNLOGIC_VULKANRENDERER_H
#endif // UNLOGIC_VULKANRENDERER_H
6 changes: 6 additions & 0 deletions src/calculator/resource/shaders/grid.frag
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#version 450

layout (binding = 0) uniform Camera {
mat4 model;
mat4 view;
mat4 projection;
} camera;

layout (location = 0) in vec4 in_color;

layout (location = 0) out vec4 out_color;
Expand Down

0 comments on commit b52ed91

Please sign in to comment.