diff --git a/Memory/Buffer.cpp b/Memory/Buffer.cpp index fbd2a92..12d299e 100644 --- a/Memory/Buffer.cpp +++ b/Memory/Buffer.cpp @@ -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); } diff --git a/Memory/Buffer.hpp b/Memory/Buffer.hpp index d29d049..87bfa65 100644 --- a/Memory/Buffer.hpp +++ b/Memory/Buffer.hpp @@ -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) { diff --git a/Renderer/Renderer.cpp b/Renderer/Renderer.cpp index 842e766..d7dcdfb 100644 --- a/Renderer/Renderer.cpp +++ b/Renderer/Renderer.cpp @@ -12,6 +12,8 @@ #include #include +#include + 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 triangle = { - {{ 1.0, 1.0, 0.0 }}, - {{-1.0, 1.0, 0.0 }}, - {{ 0.0,-1.0, 0.0 }}, + const std::vector 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(phys_dev, dev, triangle.size()); @@ -191,6 +193,8 @@ Renderer::Renderer(Window& win) : win(win) { pipeline = std::make_unique(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(swapchain->extent.height), .width = static_cast(swapchain->extent.width), .height = -static_cast(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(swapchain->extent.width) / static_cast(swapchain->extent.height), 0.01f, 50.0f); + uniform_buffer->upload(UniformData{ - .time = static_cast(frame) * 0.0167f, + .mvp = p * glm::rotate(glm::mat4(1.0), glm::radians(static_cast(frame)), glm::vec3(1.0, 1.0, 1.0)), + .time = static_cast(frame), }); - pipeline->update(0, *uniform_buffer); command_buffer->draw(9, 1, 0, 0); diff --git a/Renderer/VertexBuffer.hpp b/Renderer/VertexBuffer.hpp index 677098d..06f3c78 100644 --- a/Renderer/VertexBuffer.hpp +++ b/Renderer/VertexBuffer.hpp @@ -6,6 +6,7 @@ struct Vertex { glm::vec3 coord; + glm::vec2 tex_coord; }; struct VertexBuffer { @@ -25,11 +26,16 @@ struct VertexBuffer { inline std::vector attrs(uint32_t binding) const { return std::vector { - 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), } }; } diff --git a/assets/shaders/basic.frag b/assets/shaders/basic.frag index fcbe8a9..9b961c0 100644 --- a/assets/shaders/basic.frag +++ b/assets/shaders/basic.frag @@ -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); } \ No newline at end of file diff --git a/assets/shaders/basic.frag.spv b/assets/shaders/basic.frag.spv index f46c35c..925aa70 100644 Binary files a/assets/shaders/basic.frag.spv and b/assets/shaders/basic.frag.spv differ diff --git a/assets/shaders/basic.vert b/assets/shaders/basic.vert index 8dca425..3ed02c6 100644 --- a/assets/shaders/basic.vert +++ b/assets/shaders/basic.vert @@ -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; } \ No newline at end of file diff --git a/assets/shaders/basic.vert.spv b/assets/shaders/basic.vert.spv index 730454e..8b0dbc5 100644 Binary files a/assets/shaders/basic.vert.spv and b/assets/shaders/basic.vert.spv differ