Fixed pipeline and shaders.

Can now view 3D objects
This commit is contained in:
connellpaxton 2024-01-26 14:14:25 -05:00
parent 15dc595753
commit c4c796c98e
8 changed files with 38 additions and 14 deletions

View File

@ -21,15 +21,16 @@ Buffer::Buffer(vk::PhysicalDevice phys_dev, vk::Device dev, vk::DeviceSize sz, v
memory = dev.allocateMemory(alloc_info);
dev.bindBufferMemory(buffer, memory, 0);
p = dev.mapMemory(memory, 0, size);
}
void Buffer::upload(const uint8_t* data, vk::DeviceSize size) {
auto p = dev.mapMemory(memory, 0, size);
std::memcpy(p, data, size);
dev.unmapMemory(memory);
}
Buffer::~Buffer() {
dev.unmapMemory(memory);
dev.freeMemory(memory);
dev.destroyBuffer(buffer);
}

View File

@ -10,6 +10,8 @@ struct Buffer {
vk::DeviceSize size;
vk::DeviceMemory memory;
vk::Buffer buffer;
/* mapped memory */
void* p;
void upload(const uint8_t* data, vk::DeviceSize size);
inline void upload(const uint8_t* data) {

View File

@ -12,6 +12,8 @@
#include <Renderer/UniformBuffer.hpp>
#include <Renderer/VertexBuffer.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace std::string_literals;
Renderer::Renderer(Window& win) : win(win) {
@ -169,10 +171,10 @@ Renderer::Renderer(Window& win) : win(win) {
/* basic triangle */
std::vector<Vertex> triangle = {
{{ 1.0, 1.0, 0.0 }},
{{-1.0, 1.0, 0.0 }},
{{ 0.0,-1.0, 0.0 }},
const std::vector<Vertex> triangle = {
{{ 1.0, 1.0, -50.0 }, {1.0, 0.0}},
{{-1.0, 1.0, -50.0 }, {0.0, 0.0}},
{{ 0.0,-1.0, -50.0 }, {0.0, 1.0}},
};
vertex_buffer = std::make_unique<VertexBuffer>(phys_dev, dev, triangle.size());
@ -191,6 +193,8 @@ Renderer::Renderer(Window& win) : win(win) {
pipeline = std::make_unique<GraphicsPipeline>(dev, shaders, swapchain->extent, *render_pass, bindings, *vertex_buffer);
pipeline->update(0, *uniform_buffer);
shaders[0].cleanup();
shaders[1].cleanup();
}
@ -216,7 +220,7 @@ void Renderer::draw() {
command_buffer->begin();
vk::ClearValue clear_values[] = {
vk::ClearColorValue(1.0f, 0.0f, 1.0f, 1.0f),
vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f),
vk::ClearDepthStencilValue {.depth = 1.0f}
};
@ -235,7 +239,6 @@ void Renderer::draw() {
/* flip viewport */
auto viewport = vk::Viewport{
.x = 0.0f,
// .y = 0.0f,
.y = static_cast<float>(swapchain->extent.height),
.width = static_cast<float>(swapchain->extent.width),
.height = -static_cast<float>(swapchain->extent.height),
@ -260,11 +263,13 @@ void Renderer::draw() {
command_buffer->bind(*vertex_buffer);
command_buffer->bind(pipeline->layout, pipeline->desc_set);
const auto p = glm::perspective(glm::radians(90.0f), static_cast<float>(swapchain->extent.width) / static_cast<float>(swapchain->extent.height), 0.01f, 50.0f);
uniform_buffer->upload(UniformData{
.time = static_cast<float>(frame) * 0.0167f,
.mvp = p * glm::rotate(glm::mat4(1.0), glm::radians(static_cast<float>(frame)), glm::vec3(1.0, 1.0, 1.0)),
.time = static_cast<float>(frame),
});
pipeline->update(0, *uniform_buffer);
command_buffer->draw(9, 1, 0, 0);

View File

@ -6,6 +6,7 @@
struct Vertex {
glm::vec3 coord;
glm::vec2 tex_coord;
};
struct VertexBuffer {
@ -25,11 +26,16 @@ struct VertexBuffer {
inline std::vector<vk::VertexInputAttributeDescription> attrs(uint32_t binding) const {
return std::vector<vk::VertexInputAttributeDescription> {
vk::VertexInputAttributeDescription {
{
.location = 0,
.binding = binding,
.format = vk::Format::eR32G32B32Sfloat,
.offset = offsetof(Vertex, coord),
}, {
.location = 1,
.binding = binding,
.format = vk::Format::eR32G32Sfloat,
.offset = offsetof(Vertex, tex_coord),
}
};
}

View File

@ -1,4 +1,6 @@
#version 460 core
layout (location = 0) in vec2 texCoord;
layout (location = 0) out vec4 FragColor;
layout (set = 0, binding = 0) uniform Matrices {
@ -7,6 +9,5 @@ layout (set = 0, binding = 0) uniform Matrices {
};
void main() {
// FragColor = vec4(abs(cos(time)), 0.5, 1.0, 1.0);
FragColor = vec4(1.0);
FragColor = vec4(cos(time), 1.0, 0.0, 1.0);
}

Binary file not shown.

View File

@ -1,6 +1,15 @@
#version 460 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
layout (location = 0) out vec2 texCoord;
layout (set = 0, binding = 0) uniform Matrices {
mat4 mpv;
float time;
};
void main() {
gl_Position = vec4(aPos, 1.0);
gl_Position = mpv*vec4(aPos, 1.0);
texCoord = aTexCoord;
}

Binary file not shown.