From 93981bca4c08d4d0a3bb94d151beb7dc127a4469 Mon Sep 17 00:00:00 2001 From: Anish Bhobe Date: Sat, 13 Jul 2024 23:44:30 +0200 Subject: [PATCH] Bindless VBO. --- aster/image.cpp | 7 +- aster/image.h | 2 +- samples/00_util/frame.cpp | 4 +- samples/02_box/box.cpp | 78 +++---- samples/03_model_render/CMakeLists.txt | 4 +- samples/03_model_render/model/BoxTextured.glb | 3 + samples/03_model_render/model_loader.cpp | 44 ++++ samples/03_model_render/model_loader.h | 30 +++ samples/03_model_render/model_render.cpp | 213 ++++++------------ samples/03_model_render/pipeline_utils.cpp | 12 +- samples/03_model_render/pipeline_utils.h | 27 +-- .../render_resource_manager.cpp | 22 +- .../03_model_render/render_resource_manager.h | 2 + .../03_model_render/shader/model.frag.glsl | 5 +- .../03_model_render/shader/model.vert.glsl | 40 +++- 15 files changed, 249 insertions(+), 244 deletions(-) create mode 100644 samples/03_model_render/model/BoxTextured.glb create mode 100644 samples/03_model_render/model_loader.cpp create mode 100644 samples/03_model_render/model_loader.h diff --git a/aster/image.cpp b/aster/image.cpp index b6f8c3e..5a86ec1 100644 --- a/aster/image.cpp +++ b/aster/image.cpp @@ -19,10 +19,9 @@ Image::Destroy(const Device *device) } void -Texture::Init(const Device *device, const vk::Extent2D extent, const bool isMipmapped, const cstr name) +Texture::Init(const Device *device, const vk::Extent2D extent, vk::Format imageFormat, const bool isMipmapped, + const cstr name) { - constexpr vk::Format imageFormat = vk::Format::eR8G8B8A8Srgb; - const u32 mipLevels = isMipmapped ? 1 + Cast(floor(log2(eastl::max(extent.width, extent.height)))) : 1; vk::ImageCreateInfo imageCreateInfo = { .imageType = vk::ImageType::e2D, @@ -76,7 +75,7 @@ Texture::Init(const Device *device, const vk::Extent2D extent, const bool isMipm void DepthImage::Init(const Device *device, vk::Extent2D extent, cstr name) { - constexpr vk::Format imageFormat = vk::Format::eD32Sfloat; + constexpr vk::Format imageFormat = vk::Format::eD24UnormS8Uint; vk::ImageCreateInfo imageCreateInfo = { .imageType = vk::ImageType::e2D, .format = imageFormat, diff --git a/aster/image.h b/aster/image.h index 74d2707..6a3208e 100644 --- a/aster/image.h +++ b/aster/image.h @@ -23,7 +23,7 @@ struct Image struct Texture : Image { - void Init(const Device *device, vk::Extent2D extent, bool isMipmapped, cstr name = nullptr); + void Init(const Device *device, vk::Extent2D extent, vk::Format imageFormat, bool isMipmapped, cstr name = nullptr); }; struct DepthImage : Image diff --git a/samples/00_util/frame.cpp b/samples/00_util/frame.cpp index 25bba57..40d0871 100644 --- a/samples/00_util/frame.cpp +++ b/samples/00_util/frame.cpp @@ -61,7 +61,7 @@ Frame::Present(const vk::Queue commandQueue, Swapchain *swapchain, const Window break; case vk::Result::eErrorOutOfDateKHR: case vk::Result::eSuboptimalKHR: - INFO("Recreating Swapchain. Cause: {}", result); + DEBUG("Recreating Swapchain. Cause: {}", result); swapchain->Create(window); break; // Present failed. We do nothing. Frame is skipped. default: @@ -116,7 +116,7 @@ FrameManager::GetNextFrame(Swapchain *swapchain, const Window *window) imageAcquired = true; break; // Image acquired. Break out of loop. case vk::Result::eErrorOutOfDateKHR: - INFO("Recreating Swapchain. Cause: {}", result); + DEBUG("Recreating Swapchain. Cause: {}", result); swapchain->Create(window); break; // Image acquire has failed. We move to the next frame. default: diff --git a/samples/02_box/box.cpp b/samples/02_box/box.cpp index 87cf3d7..95631fb 100644 --- a/samples/02_box/box.cpp +++ b/samples/02_box/box.cpp @@ -75,7 +75,7 @@ Pipeline CreatePipeline(const Device *device, const Swapchain *swapchain); struct Vertex { vec3 m_Position; - vec2 m_UV0; + vec2 m_TexCoord0; constexpr static vk::VertexInputBindingDescription GetBinding(const u32 binding) @@ -97,7 +97,7 @@ struct Vertex .location = 1, .binding = binding, .format = vk::Format::eR32G32Sfloat, - .offset = offsetof(Vertex, m_UV0), + .offset = offsetof(Vertex, m_TexCoord0), }, }; } @@ -185,47 +185,47 @@ main(int, char **) } eastl::array vertices = { - Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_UV0 = vec2(0.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_UV0 = vec2(1.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, }; ImageFile imageFile; @@ -236,7 +236,7 @@ main(int, char **) VertexBuffer vbo; Texture crate; vbo.Init(&device, vertices.size() * sizeof vertices[0], "VBO"); - crate.Init(&device, {imageFile.m_Width, imageFile.m_Height}, false, "Crate Texture"); + crate.Init(&device, {imageFile.m_Width, imageFile.m_Height}, vk::Format::eR8G8B8A8Srgb, false, "Crate Texture"); { StagingBuffer vertexStaging, imageStaging; vertexStaging.Init(&device, vertices.size() * sizeof vertices[0], "Vertex Staging"); diff --git a/samples/03_model_render/CMakeLists.txt b/samples/03_model_render/CMakeLists.txt index 8753a51..37f497a 100644 --- a/samples/03_model_render/CMakeLists.txt +++ b/samples/03_model_render/CMakeLists.txt @@ -9,7 +9,9 @@ add_executable(model_render model_render.cpp pipeline_utils.cpp pipeline_utils.h render_resource_manager.cpp - render_resource_manager.h) + render_resource_manager.h + model_loader.cpp + model_loader.h) add_shader(model_render shader/model.vert.glsl) add_shader(model_render shader/model.frag.glsl) diff --git a/samples/03_model_render/model/BoxTextured.glb b/samples/03_model_render/model/BoxTextured.glb new file mode 100644 index 0000000..6524740 --- /dev/null +++ b/samples/03_model_render/model/BoxTextured.glb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2d055d2d56a492d1b9302de6e733046b625cf51e5f2a3090bd3a8c11acc93c17 +size 6540 diff --git a/samples/03_model_render/model_loader.cpp b/samples/03_model_render/model_loader.cpp new file mode 100644 index 0000000..1f2f1ee --- /dev/null +++ b/samples/03_model_render/model_loader.cpp @@ -0,0 +1,44 @@ +// ============================================= +// Aster: model_loader.cpp +// Copyright (c) 2020-2024 Anish Bhobe +// ============================================= + +#define TINYGLTF_NOEXCEPTION +#define JSON_NOEXCEPTION + +#define TINYGLTF_IMPLEMENTATION +#define STB_IMAGE_IMPLEMENTATION +#define STB_IMAGE_WRITE_IMPLEMENTATION + +#include "model_loader.h" + +Model +LoadModel(RenderResourceManager *resourceManager, cstr path) +{ + namespace fs = std::filesystem; + tinygltf::Model model; + tinygltf::TinyGLTF loader; + + const auto fsPath = fs::absolute(path); + const auto ext = fsPath.extension(); + if (ext.c_str() == GLTF_ASCII_FILE_EXTENSION) + { + std::string err; + std::string warn; + if (loader.LoadASCIIFromFile(&model, &err, &warn, fsPath.generic_string())) + { + ERROR_IF(!err.empty(), "{}", err) + ELSE_IF_WARN(!warn.empty(), "{}", warn); + } + } + if (ext.c_str() == GLTF_BINARY_FILE_EXTENSION) + { + std::string err; + std::string warn; + if (loader.LoadBinaryFromFile(&model, &err, &warn, fsPath.generic_string())) + { + ERROR_IF(!err.empty(), "{}", err) + ELSE_IF_WARN(!warn.empty(), "{}", warn); + } + } +} \ No newline at end of file diff --git a/samples/03_model_render/model_loader.h b/samples/03_model_render/model_loader.h new file mode 100644 index 0000000..87b103b --- /dev/null +++ b/samples/03_model_render/model_loader.h @@ -0,0 +1,30 @@ +// ============================================= +// Aster: model_loader.h +// Copyright (c) 2020-2024 Anish Bhobe +// ============================================= + +#pragma once + +#include "constants.h" + +#include +#include + +struct RenderResourceManager; +struct Texture; +constexpr auto GLTF_ASCII_FILE_EXTENSION = ".gltf"; +constexpr auto GLTF_BINARY_FILE_EXTENSION = ".glb"; + +constexpr static auto NORMAL = "NORMAL"; +constexpr static auto POSITION = "POSITION"; +constexpr static auto TANGENT = "TANGENT"; +constexpr static auto TEXCOORD_0 = "TEXCOORD_0"; +constexpr static auto TEXCOORD_1 = "TEXCOORD_1"; +constexpr static auto JOINTS_0 = "JOINTS_0"; +constexpr static auto WEIGHTS_0 = "WEIGHTS_0"; + +struct Model +{ +}; + +Model LoadModel(RenderResourceManager *resourceManager, cstr path); \ No newline at end of file diff --git a/samples/03_model_render/model_render.cpp b/samples/03_model_render/model_render.cpp index e91010c..79e789b 100644 --- a/samples/03_model_render/model_render.cpp +++ b/samples/03_model_render/model_render.cpp @@ -3,13 +3,6 @@ // Copyright (c) 2020-2024 Anish Bhobe // ============================================= -#define TINYGLTF_NOEXCEPTION -#define JSON_NOEXCEPTION - -#define TINYGLTF_IMPLEMENTATION -#define STB_IMAGE_IMPLEMENTATION -#define STB_IMAGE_WRITE_IMPLEMENTATION - #include "buffer.h" #include "constants.h" #include "context.h" @@ -24,10 +17,12 @@ #include "frame.h" #include "helpers.h" +#include "model_loader.h" #include "pipeline_utils.h" #include "render_resource_manager.h" #include +#include #include #include @@ -55,47 +50,13 @@ struct Camera mat4 m_Perspective; }; -constexpr auto GLTF_ASCII_FILE_EXTENSION = ".gltf"; -constexpr auto GLTF_BINARY_FILE_EXTENSION = ".glb"; - -void -LoadModel(cstr path) -{ - namespace fs = std::filesystem; - tinygltf::Model model; - tinygltf::TinyGLTF loader; - - const auto fsPath = fs::absolute(path); - const auto ext = fsPath.extension(); - if (ext.c_str() == GLTF_ASCII_FILE_EXTENSION) - { - std::string err; - std::string warn; - if (loader.LoadASCIIFromFile(&model, &err, &warn, fsPath.generic_string())) - { - ERROR_IF(!err.empty(), "{}", err) - ELSE_IF_WARN(!warn.empty(), "{}", warn); - } - } - if (ext.c_str() == GLTF_BINARY_FILE_EXTENSION) - { - std::string err; - std::string warn; - if (loader.LoadBinaryFromFile(&model, &err, &warn, fsPath.generic_string())) - { - ERROR_IF(!err.empty(), "{}", err) - ELSE_IF_WARN(!warn.empty(), "{}", warn); - } - } -} - int main(int, char **) { MIN_LOG_LEVEL(Logger::LogType::eInfo); - Context context = {"Box", VERSION}; - Window window = {"Box (Aster)", &context, {640, 480}}; + Context context = {"ModelRender [WIP]", VERSION}; + Window window = {"ModelRender [WIP] (Aster)", &context, {640, 480}}; PhysicalDevices physicalDevices = {&window, &context}; PhysicalDevice deviceToUse = FindSuitableDevice(physicalDevices); @@ -182,47 +143,47 @@ main(int, char **) } eastl::array vertices = { - Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_UV0 = vec2(0.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_UV0 = vec2(1.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)}, - Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 0.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 1.0f)}, + Vertex{.m_Position = vec3(-0.5f, -0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_UV0 = vec2(1.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_UV0 = vec2(0.0f, 0.0f)}, - Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_UV0 = vec2(0.0f, 1.0f)}, - Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_UV0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(1.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, -0.5f), .m_TexCoord0 = vec2(0.0f, 0.0f)}, + Vertex{.m_Position = vec3(-0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(0.0f, 1.0f)}, + Vertex{.m_Position = vec3(0.5f, 0.5f, 0.5f), .m_TexCoord0 = vec2(1.0f, 1.0f)}, }; ImageFile crateImageFile; @@ -231,20 +192,22 @@ main(int, char **) INFO("Image {}x{} : {} channels", crateImageFile.m_Width, crateImageFile.m_Height, crateImageFile.m_NumChannels); assert(plainImageFile.Load({0.7f, 0.4f, 0.1f, 1.0f})); - VertexBuffer vbo; + UniformStorageBuffer pvbo; Texture crateTexture; Texture plainTexture; - vbo.Init(&device, vertices.size() * sizeof vertices[0], "VBO"); - crateTexture.Init(&device, {crateImageFile.m_Width, crateImageFile.m_Height}, false, "Crate Texture"); - plainTexture.Init(&device, {crateImageFile.m_Width, crateImageFile.m_Height}, false, "Plain Texture"); + crateTexture.Init(&device, {crateImageFile.m_Width, crateImageFile.m_Height}, vk::Format::eR8G8B8A8Srgb, false, + "Crate Texture"); + plainTexture.Init(&device, {crateImageFile.m_Width, crateImageFile.m_Height}, vk::Format::eR8G8B8A8Srgb, false, + "Plain Texture"); + pvbo.Init(&device, vertices.size() * sizeof vertices[0], "Pull VBO"); + pvbo.Write(&device, 0, pvbo.GetSize(), vertices.data()); auto crateTextureId = resourceManager.Commit(&crateTexture); auto plainTextureId = resourceManager.Commit(&plainTexture); + auto pvboBufferId = resourceManager.Commit(&pvbo); { - StagingBuffer vertexStaging, imageStaging1, imageStaging2; - vertexStaging.Init(&device, vertices.size() * sizeof vertices[0], "Vertex Staging"); - vertexStaging.Write(&device, 0, vertices.size() * sizeof vertices[0], vertices.data()); + StagingBuffer imageStaging1, imageStaging2; imageStaging1.Init(&device, crateImageFile.GetSize(), "Image Staging 1"); imageStaging1.Write(&device, 0, crateImageFile.GetSize(), crateImageFile.m_Data); @@ -327,9 +290,6 @@ main(int, char **) copyBuffer.pipelineBarrier(vk::PipelineStageFlagBits::eHost, vk::PipelineStageFlagBits::eTransfer, {}, 0, nullptr, 0, nullptr, Cast(imageReadyToWrite.size()), imageReadyToWrite.data()); - vk::BufferCopy bufferCopy = {.srcOffset = 0, .dstOffset = 0, .size = vertexStaging.GetSize()}; - copyBuffer.copyBuffer(vertexStaging.m_Buffer, vbo.m_Buffer, 1, &bufferCopy); - vk::BufferImageCopy imageCopy = { .bufferOffset = 0, .bufferRowLength = crateImageFile.m_Width, @@ -368,7 +328,6 @@ main(int, char **) AbortIfFailedM(device.m_Device.resetCommandPool(copyPool, {}), "Couldn't reset command pool."); device.m_Device.destroy(fence, nullptr); - vertexStaging.Destroy(&device); imageStaging1.Destroy(&device); imageStaging2.Destroy(&device); } @@ -442,52 +401,22 @@ main(int, char **) FrameManager frameManager = {&device, queueAllocation.m_Family, MAX_FRAMES_IN_FLIGHT}; eastl::fixed_vector depthImages(frameManager.m_FramesInFlight); - eastl::fixed_vector depthViews(frameManager.m_FramesInFlight); - - vk::ImageSubresourceRange depthSubresourceRange = { - .aspectMask = vk::ImageAspectFlagBits::eDepth, - .baseMipLevel = 0, - .levelCount = 1, - .baseArrayLayer = 0, - .layerCount = 1, - }; - - u32 index = 0; - for (auto &depthImage : depthImages) { - auto name = fmt::format("Depth image {}", index); - depthImage.Init(&device, swapchain.m_Extent, name.c_str()); - vk::ImageViewCreateInfo imageViewCreateInfo = { - .image = depthImage.m_Image, - .viewType = vk::ImageViewType::e2D, - .format = vk::Format::eD32Sfloat, - .components = vk::ComponentMapping{.r = vk::ComponentSwizzle::eIdentity}, - .subresourceRange = depthSubresourceRange, - }; - AbortIfFailed(device.m_Device.createImageView(&imageViewCreateInfo, nullptr, &depthViews[index])); - - index++; - } - - auto fnRecreateDepthBuffers = [&device, &depthImages, &depthViews, depthSubresourceRange](vk::Extent2D extent) { - for (const auto &depthView : depthViews) - { - device.m_Device.destroy(depthView, nullptr); - } - u32 index = 0; + u32 depthImageIdx = 0; for (auto &depthImage : depthImages) { + auto name = fmt::format("Depth {}", depthImageIdx++); + depthImage.Init(&device, swapchain.m_Extent, name.c_str()); + } + } + + auto fnRecreateDepthBuffers = [&device, &depthImages](vk::Extent2D extent) { + u32 depthImageIdx = 0; + for (auto &depthImage : depthImages) + { + auto name = fmt::format("Depth {}", depthImageIdx++); depthImage.Destroy(&device); - depthImage.Init(&device, extent, "Depth"); - vk::ImageViewCreateInfo imageViewCreateInfo = { - .image = depthImage.m_Image, - .viewType = vk::ImageViewType::e2D, - .format = vk::Format::eD32Sfloat, - .components = vk::ComponentMapping{.r = vk::ComponentSwizzle::eIdentity}, - .subresourceRange = depthSubresourceRange, - }; - AbortIfFailed(device.m_Device.createImageView(&imageViewCreateInfo, nullptr, &depthViews[index])); - ++index; + depthImage.Init(&device, extent, name.c_str()); } }; swapchain.RegisterResizeCallback(fnRecreateDepthBuffers); @@ -499,8 +428,8 @@ main(int, char **) bool prevPressed = false; auto isSpaceJustPressed = [&prevPressed, &window] { - bool pressed = glfwGetKey(window.m_Window, GLFW_KEY_SPACE) == GLFW_PRESS; - bool justPressed = pressed & !prevPressed; + const bool pressed = glfwGetKey(window.m_Window, GLFW_KEY_SPACE) == GLFW_PRESS; + const bool justPressed = pressed & !prevPressed; prevPressed = pressed; return justPressed; }; @@ -524,7 +453,7 @@ main(int, char **) vk::ImageView currentImageView = swapchain.m_ImageViews[imageIndex]; vk::Image currentImage = swapchain.m_Images[imageIndex]; vk::CommandBuffer cmd = currentFrame->m_CommandBuffer; - vk::ImageView currentDepthImageView = depthViews[currentFrame->m_FrameIdx]; + vk::ImageView currentDepthImageView = depthImages[currentFrame->m_FrameIdx].m_View; topOfThePipeBarrier.image = currentImage; renderToPresentBarrier.image = currentImage; @@ -570,11 +499,11 @@ main(int, char **) cmd.setScissor(0, 1, &scissor); cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline.m_Pipeline); cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, 0, sizeof *pushData, pushData); + cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, sizeof *pushData, sizeof pvboBufferId, + &pvboBufferId); cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline.m_Layout, 0, 1, &resourceManager.m_DescriptorSet, 0, nullptr); cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline.m_Layout, 1, 1, &descriptorSet, 0, nullptr); - usize offsets = 0; - cmd.bindVertexBuffers(0, 1, &vbo.m_Buffer, &offsets); cmd.draw(Cast(vertices.size()), 1, 0, 0); cmd.endRendering(); @@ -601,10 +530,6 @@ main(int, char **) AbortIfFailed(device.m_Device.waitIdle()); - for (auto &depthView : depthViews) - { - device.m_Device.destroy(depthView, nullptr); - } for (auto &depthImage : depthImages) { depthImage.Destroy(&device); @@ -614,7 +539,7 @@ main(int, char **) device.m_Device.destroy(copyPool, nullptr); crateTexture.Destroy(&device); plainTexture.Destroy(&device); - vbo.Destroy(&device); + pvbo.Destroy(&device); return 0; } @@ -625,8 +550,8 @@ ImageFile::Load(vec4 color) constexpr usize size = 512llu * 512llu * 4llu; u8 *pData = new u8[size]; - vec4 color255 = 255.999f * color; - glm::vec<4, u8> color8 = color255; + const vec4 color255 = 255.999f * color; + const glm::vec<4, u8> color8 = color255; for (usize i = 0; i < size; i += 4) { diff --git a/samples/03_model_render/pipeline_utils.cpp b/samples/03_model_render/pipeline_utils.cpp index 094c72d..3922924 100644 --- a/samples/03_model_render/pipeline_utils.cpp +++ b/samples/03_model_render/pipeline_utils.cpp @@ -73,15 +73,7 @@ CreatePipeline(const Device *device, const Swapchain *swapchain, const RenderRes descriptorSetLayouts[0] = nullptr; // Not owned. - vk::VertexInputBindingDescription inputBindingDescription = Vertex::GetBinding(0); - auto inputAttributeDescription = Vertex::GetAttributes(0); - - vk::PipelineVertexInputStateCreateInfo vertexInputStateCreateInfo = { - .vertexBindingDescriptionCount = 1, - .pVertexBindingDescriptions = &inputBindingDescription, - .vertexAttributeDescriptionCount = Cast(inputAttributeDescription.size()), - .pVertexAttributeDescriptions = inputAttributeDescription.data(), - }; + vk::PipelineVertexInputStateCreateInfo vertexInputStateCreateInfo = {}; vk::PipelineInputAssemblyStateCreateInfo inputAssemblyStateCreateInfo = { .topology = vk::PrimitiveTopology::eTriangleList, .primitiveRestartEnable = false, @@ -141,7 +133,7 @@ CreatePipeline(const Device *device, const Swapchain *swapchain, const RenderRes .viewMask = 0, .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapchain->m_Format, - .depthAttachmentFormat = vk::Format::eD32Sfloat, + .depthAttachmentFormat = vk::Format::eD24UnormS8Uint, }; vk::GraphicsPipelineCreateInfo pipelineCreateInfo = { diff --git a/samples/03_model_render/pipeline_utils.h b/samples/03_model_render/pipeline_utils.h index 2ece8fe..8b94fb0 100644 --- a/samples/03_model_render/pipeline_utils.h +++ b/samples/03_model_render/pipeline_utils.h @@ -19,32 +19,7 @@ constexpr auto FRAGMENT_SHADER_FILE = "shader/model.frag.glsl.spv"; struct Vertex { vec3 m_Position; - vec2 m_UV0; - - constexpr static vk::VertexInputBindingDescription - GetBinding(const u32 binding) - { - return {.binding = binding, .stride = sizeof(Vertex), .inputRate = vk::VertexInputRate::eVertex}; - } - - constexpr static eastl::array - GetAttributes(const u32 binding) - { - return { - vk::VertexInputAttributeDescription{ - .location = 0, - .binding = binding, - .format = vk::Format::eR32G32B32Sfloat, - .offset = offsetof(Vertex, m_Position), - }, - vk::VertexInputAttributeDescription{ - .location = 1, - .binding = binding, - .format = vk::Format::eR32G32Sfloat, - .offset = offsetof(Vertex, m_UV0), - }, - }; - } + vec2 m_TexCoord0; }; vk::ShaderModule CreateShader(const Device *device, cstr shaderFile); diff --git a/samples/03_model_render/render_resource_manager.cpp b/samples/03_model_render/render_resource_manager.cpp index ebee2db..cfa195c 100644 --- a/samples/03_model_render/render_resource_manager.cpp +++ b/samples/03_model_render/render_resource_manager.cpp @@ -50,6 +50,18 @@ RenderResourceManager::Commit(const UniformStorageBuffer *storageBuffer) return {handle}; } +void +RenderResourceManager::Release(BufferHandle handle) +{ + m_BufferFreeList.Free(handle.m_Index); +} + +void +RenderResourceManager::Release(TextureHandle handle) +{ + m_TextureFreeList.Free(handle.m_Index); +} + TextureHandle RenderResourceManager::Commit(const Texture *texture) { @@ -76,12 +88,10 @@ RenderResourceManager::Commit(const Texture *texture) void RenderResourceManager::Update() { - usize count = Cast(m_Writes.size()); - vk::WriteDescriptorSet *pData = m_Writes.data(); - if (count > 0) - { - m_Device->m_Device.updateDescriptorSets(count, pData, 0, nullptr); - } + if (m_Writes.empty()) + return; + + m_Device->m_Device.updateDescriptorSets(m_Writes.size(), m_Writes.data(), 0, nullptr); m_Writes.clear(); m_WriteInfos.clear(); diff --git a/samples/03_model_render/render_resource_manager.h b/samples/03_model_render/render_resource_manager.h index 1013789..79a59ed 100644 --- a/samples/03_model_render/render_resource_manager.h +++ b/samples/03_model_render/render_resource_manager.h @@ -99,7 +99,9 @@ struct RenderResourceManager vk::DescriptorSet m_DescriptorSet; BufferHandle Commit(const UniformStorageBuffer *storageBuffer); + void Release(BufferHandle handle); TextureHandle Commit(const Texture *texture); + void Release(TextureHandle handle); void Update(); diff --git a/samples/03_model_render/shader/model.frag.glsl b/samples/03_model_render/shader/model.frag.glsl index f1670eb..1408b70 100644 --- a/samples/03_model_render/shader/model.frag.glsl +++ b/samples/03_model_render/shader/model.frag.glsl @@ -8,9 +8,10 @@ layout (location = 0) out vec4 outColor; layout(set = 0, binding = 1) uniform sampler2D textures[]; layout(push_constant) uniform Block { - uint handle; + uint textureHandle; + uint vertexBufferHandle; }; void main() { - outColor = vec4(texture(textures[handle], inUV).rgb, 1.0f); + outColor = vec4(texture(textures[textureHandle], inUV).rgb, 1.0f); } \ No newline at end of file diff --git a/samples/03_model_render/shader/model.vert.glsl b/samples/03_model_render/shader/model.vert.glsl index 4463ab3..fbed2df 100644 --- a/samples/03_model_render/shader/model.vert.glsl +++ b/samples/03_model_render/shader/model.vert.glsl @@ -1,14 +1,32 @@ -#version 450 +#version 460 core #pragma shader_stage(vertex) #extension GL_EXT_nonuniform_qualifier : enable -layout(location = 0) in vec4 position; -layout(location = 1) in vec2 uv0; - layout(location = 0) out vec2 outUV; -//layout(std140, set=0, binding=0) readonly buffer buffers; -//layout(set = 0, binding = 1) uniform texture2D textures[]; +struct VertexData { + float position[3]; + float uv[2]; +}; + +layout(std430, set = 0, binding = 0) readonly buffer Vertices { + VertexData data[]; +} vertexBuffer[]; + +vec3 GetPosition(uint bufferId, uint vertexIdx) { + return vec3( + vertexBuffer[bufferId].data[vertexIdx].position[0], + vertexBuffer[bufferId].data[vertexIdx].position[1], + vertexBuffer[bufferId].data[vertexIdx].position[2] + ); +} + +vec2 GetUV(uint bufferId, uint vertexIdx) { + return vec2( + vertexBuffer[bufferId].data[vertexIdx].uv[0], + vertexBuffer[bufferId].data[vertexIdx].uv[1] + ); +} layout(set = 1, binding = 0) uniform Camera { mat4 model; @@ -16,8 +34,12 @@ layout(set = 1, binding = 0) uniform Camera { mat4 proj; } ubo; +layout(push_constant) uniform Block { + uint textureHandle; + uint vertexBufferHandle; +}; + void main() { - outUV = uv0; - gl_Position = ubo.proj * ubo.view * ubo.model * vec4(position.xyz, 1.0f); - // outColor = vec3(0.5f, 0.3f, 0.1f); + outUV = GetUV(vertexBufferHandle, gl_VertexIndex); + gl_Position = ubo.proj * ubo.view * ubo.model * vec4(GetPosition(vertexBufferHandle, gl_VertexIndex), 1.0f); } \ No newline at end of file