Update documentation and clean code for Uniform Buffers, added time variable to descriptor set, preparing for Texture mapping.

This commit is contained in:
2024-10-12 04:14:22 -05:00
parent 086adae47a
commit cb4500d97f
8 changed files with 54 additions and 18 deletions

View File

@@ -1,5 +1,4 @@
#include "entrypoint.h"
#include "global.h"
DeviceControl::devicelibrary deviceLibs;
Debug::vulkandebuglibs debugController;
Graphics::graphicspipeline graphicsPipeline;

View File

@@ -3,6 +3,7 @@
#include "debug/vulkandebuglibs.h"
#include "graphics/graphicspipeline.h"
#include "graphics/render.h"
#include "global.h"
class EntryApp {
public:
static EntryApp& getInstance();

View File

@@ -3,6 +3,7 @@
#include <glm/detail/qualifier.hpp>
#include <glm/ext/vector_float2.hpp>
#include <glm/ext/vector_float3.hpp>
#include <glm/fwd.hpp>
#include <iostream>
#include <vector>
#include <optional>
@@ -29,11 +30,11 @@ namespace Global {
extern VkDescriptorSetLayout descriptorSetLayout;
extern uint32_t currentFrame;
extern std::vector<VkDescriptorSet> descriptorSets;
struct UniformBufferObject {
glm::mat4 model;
glm::mat4 view;
glm::mat4 proj;
float time;
alignas(16) glm::mat4 model;
alignas(16) glm::mat4 view;
alignas(16) glm::mat4 proj;
};
struct Vertex {
glm::vec2 pos;

View File

@@ -165,7 +165,7 @@ namespace Buffers {
std::vector<uint16_t> bufferslibrary::getIndices() {
return indices;
}
// ------------------------------ Uniform Buffer Setup -------------------------------- //
void bufferslibrary::createDescriptorSetLayout() {
// Create a table of pointers to data, a Descriptor Set!
VkDescriptorSetLayoutBinding uboLayoutBinding{};
@@ -187,10 +187,9 @@ namespace Buffers {
throw std::runtime_error("Failed to create descriptor set layout!");
}
}
//void createMVPDescriptor() {
//}
void bufferslibrary::createUniformBuffers() {
// Map the uniform buffer to memory as a pointer we can use to write data to later. This stays mapped to memory for the applications lifetime.
// This technique is called "persistent mapping", not having to map the buffer every time we need to update it increases performance, though not free
VkDeviceSize bufferSize = sizeof(Global::UniformBufferObject);
uniformBuffers.resize(Global::MAX_FRAMES_IN_FLIGHT);
@@ -203,12 +202,16 @@ namespace Buffers {
}
}
void bufferslibrary::updateUniformBuffer(uint32_t currentImage) {
// Update the uniform buffer every frame to change the position, but notably, use chrono to know exactly how much to move
// so we aren't locked to the framerate as the world time.
static auto startTime = std::chrono::high_resolution_clock::now();
// Calculate the time in seconds since rendering has began to floating point precision.
auto currentTime = std::chrono::high_resolution_clock::now();
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
Global::UniformBufferObject ubo{};
ubo.time = time;
// Modify the model projection transformation to rotate around the Z over time.
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
// Modify the view transformation to look at the object from above at a 45 degree angle.
@@ -229,6 +232,7 @@ namespace Buffers {
}
}
void bufferslibrary::createDescriptorPool() {
// Create a pool to be used to allocate descriptor sets.
VkDescriptorPoolSize poolSize{};
poolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
poolSize.descriptorCount = static_cast<uint32_t>(Global::MAX_FRAMES_IN_FLIGHT);

View File

@@ -238,7 +238,8 @@ namespace Graphics {
}
}
void graphicspipeline::createCommandPool() {
// Commands in Vulkan are not executed using function calls, you have to record the ops you wish to perform
// to command buffers, pools manage the memory used by the buffer!
Global::QueueFamilyIndices queueFamilyIndices = Global::findQueueFamilies(Global::physicalDevice);
VkCommandPoolCreateInfo poolInfo{};

View File

@@ -1,6 +1,7 @@
#version 450
layout(binding = 0) uniform UniformBufferObject {
float time;
mat4 model;
mat4 view;
mat4 proj;
@@ -14,6 +15,5 @@ layout(location = 0) out vec3 fragColor;
void main() {
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0);
fragColor = inColor;
fragColor = inColor + sin(ubo.time*2);
}