From d04e4a168a9783bf18f45c9a2d663162aab9e1cd Mon Sep 17 00:00:00 2001 From: bailwillharr Date: Thu, 27 Oct 2022 17:58:30 +0100 Subject: [PATCH] Do a ton of stuff. can't remember what --- include/engine.hpp | 7 +++- include/gfx_device.hpp | 4 ++- include/object.hpp | 12 +++---- include/resources/mesh.hpp | 15 +++----- include/resources/shader.hpp | 2 -- include/sceneroot.hpp | 1 - src/engine.cpp | 68 ++++++++++++------------------------ src/gfx_device_vulkan.cpp | 39 +++++++++++++-------- src/resources/mesh.cpp | 19 ++++------ src/sceneroot.cpp | 6 ---- 10 files changed, 74 insertions(+), 99 deletions(-) diff --git a/include/engine.hpp b/include/engine.hpp index 56b3db5..5f6818a 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -5,7 +5,10 @@ namespace engine { class Window; + class Input; class GFXDevice; + class ResourceManager; + class SceneRoot; class Application { @@ -21,7 +24,9 @@ namespace engine { private: std::unique_ptr m_win; - std::unique_ptr m_gfx; + std::unique_ptr m_input; + std::unique_ptr m_res; + std::unique_ptr m_scene; }; } diff --git a/include/gfx_device.hpp b/include/gfx_device.hpp index cdb4bd6..a0b325d 100644 --- a/include/gfx_device.hpp +++ b/include/gfx_device.hpp @@ -20,7 +20,7 @@ namespace engine { ~GFXDevice(); // adds a draw call to the queue - // vertexBuffer is required. indexBuffer and uniformData can be NULL + // vertexBuffer is required, indexBuffer can be NULL, uniformData is required void draw(const gfx::Pipeline* pipeline, const gfx::Buffer* vertexBuffer, const gfx::Buffer* indexBuffer, uint32_t count, const void* uniformData); // Call once per frame. Executes all queued draw calls and renders to the screen. @@ -42,4 +42,6 @@ namespace engine { }; + extern GFXDevice* gfxdev; + } diff --git a/include/object.hpp b/include/object.hpp index 06ecc0a..b47dfae 100644 --- a/include/object.hpp +++ b/include/object.hpp @@ -15,7 +15,6 @@ namespace engine { class Window; class Input; - class ResourceManager; class SceneRoot; @@ -30,9 +29,9 @@ namespace engine { } struct GameIO { - engine::Window* const win; - engine::Input* const input; - ResourceManager* const resMan; + Window* win; + Input* input; + ResourceManager* resMan; }; // This object lives until it is deleted by its parent(s) or finally when the "Scene" is destroyed. @@ -45,10 +44,11 @@ namespace engine { Object& operator=(const Object&) = delete; ~Object(); - engine::Window& win; - engine::Input& inp; + Window& win; + Input& inp; ResourceManager& res; + SceneRoot& root; std::string getName(); diff --git a/include/resources/mesh.hpp b/include/resources/mesh.hpp index fc48973..6c9a384 100644 --- a/include/resources/mesh.hpp +++ b/include/resources/mesh.hpp @@ -18,30 +18,23 @@ struct Vertex { glm::vec2 uv; }; -namespace engine { - class GFXDevice; -} - namespace engine::resources { class ENGINE_API Mesh : public Resource { public: - Mesh(GFXDevice* gfx, const std::vector& vertices); - Mesh(GFXDevice* gfx, const std::vector& vertices, const std::vector& indices); - Mesh(GFXDevice* gfx, const std::filesystem::path& resPath); + Mesh(const std::vector& vertices); + Mesh(const std::vector& vertices, const std::vector& indices); + Mesh(const std::filesystem::path& resPath); ~Mesh() override; - void drawMesh(const gfx::Pipeline* pipeline); - std::vector m_vertices; std::vector m_indices; -private: const gfx::Buffer* vb; const gfx::Buffer* ib; - GFXDevice* gfx; +private: void initMesh(); diff --git a/include/resources/shader.hpp b/include/resources/shader.hpp index 839e00b..511ac7a 100644 --- a/include/resources/shader.hpp +++ b/include/resources/shader.hpp @@ -4,8 +4,6 @@ #include "resource.hpp" -#include - #include #include diff --git a/include/sceneroot.hpp b/include/sceneroot.hpp index a3bc312..9850523 100644 --- a/include/sceneroot.hpp +++ b/include/sceneroot.hpp @@ -14,7 +14,6 @@ namespace engine { public: // create a new empty scene SceneRoot(struct GameIO things); - SceneRoot(const std::filesystem::path& file, struct GameIO things); SceneRoot(const SceneRoot&) = delete; SceneRoot& operator=(const SceneRoot&) = delete; diff --git a/src/engine.cpp b/src/engine.cpp index ce9763b..49710d7 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -1,12 +1,15 @@ #include "engine.hpp" -#include "window.hpp" -#include "gfx_device.hpp" -#include "resource_manager.hpp" #include "log.hpp" -#include -#include +#include "window.hpp" +#include "input.hpp" +#include "resource_manager.hpp" +#include "sceneroot.hpp" + +#include "gfx_device.hpp" + +#include "resources/mesh.hpp" struct UBO { glm::mat4 model{}; @@ -17,48 +20,27 @@ struct UBO { static engine::gfx::Pipeline* pipeline; static engine::gfx::Buffer* vb; static engine::gfx::Buffer* ib; -static engine::gfx::Buffer* ub; namespace engine { Application::Application(const char* appName, const char* appVersion) { m_win = std::make_unique(appName, true); - m_gfx = std::make_unique(appName, appVersion, m_win->getHandle()); - engine::ResourceManager resMan{}; - struct Vertex { - glm::vec2 pos; - glm::vec3 col; - }; - gfx::VertexFormat vertFormat{ - .stride = (uint32_t)sizeof(Vertex), - }; - vertFormat.attributeDescriptions.push_back({0, gfx::VertexAttribFormat::VEC2, 0}); - vertFormat.attributeDescriptions.push_back({1, gfx::VertexAttribFormat::VEC3, offsetof(Vertex, col)}); - - pipeline = m_gfx->createPipeline(resMan.getFilePath("shader.vert.spv").string().c_str(), resMan.getFilePath("shader.frag.spv").string().c_str(), vertFormat, sizeof(UBO)); - - const std::vector vertices = { - { { 0.5f, -0.5f}, {1.0f, 0.0f, 0.0f} }, - { { 0.5f, 0.5f}, {0.0f, 1.0f, 0.0f} }, - { {-0.5f, -0.5f}, {0.0f, 0.0f, 1.0f} }, - { {-0.5f, 0.5f}, {0.0f, 1.0f, 1.0f} }, - }; - vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, sizeof(Vertex) * vertices.size(), vertices.data()); - const std::vector indices = { - 0, 1, 2, 2, 1, 3, - }; - ib = m_gfx->createBuffer(gfx::BufferType::INDEX, sizeof(uint32_t) * indices.size(), indices.data()); + gfxdev = new GFXDevice(appName, appVersion, m_win->getHandle()); + m_input = std::make_unique(*m_win); + m_res = std::make_unique(); + GameIO things{}; + things.win = m_win.get(); + things.input = m_input.get(); + things.resMan = m_res.get(); + m_scene = std::make_unique(things); } Application::~Application() { -// m_gfx->destroyBuffer(ub); - m_gfx->destroyBuffer(ib); - m_gfx->destroyBuffer(vb); - m_gfx->destroyPipeline(pipeline); + delete gfxdev; } void Application::gameLoop() @@ -68,6 +50,8 @@ namespace engine { uint64_t lastTick = m_win->getNanos(); constexpr int TICKFREQ = 1; // in hz + auto myMesh = m_res->get("meshes/monke.mesh"); + // single-threaded game loop while (m_win->isRunning()) { @@ -88,23 +72,17 @@ namespace engine { m_win->setCloseFlag(); } - /* draw */ - UBO initialUbo{ - .model = glm::mat4{1.0f}, - .view = glm::mat4{1.0f}, - .proj = glm::mat4{1.0f} - }; - initialUbo.model = glm::scale(glm::mat4{ 1.0f }, glm::vec3{ 0.5f, 0.5f, 1.0f }); - m_gfx->draw(pipeline, vb, ib, 6, &initialUbo); + m_scene->updateStuff(); - m_gfx->renderFrame(); + /* draw */ + gfxdev->renderFrame(); /* poll events */ m_win->getInputAndEvents(); } - m_gfx->waitIdle(); + gfxdev->waitIdle(); } } diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index bdfd434..5384917 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -31,7 +31,12 @@ namespace engine { - static constexpr uint32_t FRAMES_IN_FLIGHT = 2; // This improved FPS by 5x! + // EXTERNED GLOBAL VARIABLE + GFXDevice* gfxdev = nullptr; + + static constexpr uint32_t FRAMES_IN_FLIGHT = 2; // This improved FPS by 5x! (on Intel IGPU) + + static constexpr size_t UNIFORM_BUFFER_MAX_SIZE = 256; // bytes // structures and enums @@ -73,6 +78,7 @@ namespace engine { const gfx::Buffer* vertexBuffer = nullptr; const gfx::Buffer* indexBuffer = nullptr; // if this is nullptr, don't use indexed uint32_t count = 0; + uint8_t uniformData[UNIFORM_BUFFER_MAX_SIZE]; }; enum class QueueFlags : uint32_t { @@ -634,6 +640,11 @@ namespace engine { GFXDevice::GFXDevice(const char* appName, const char* appVersion, SDL_Window* window) { + if (gfxdev != nullptr) { + throw std::runtime_error("There can only be one graphics device"); + } + gfxdev = this; + pimpl = std::make_unique(); VkResult res; @@ -1077,8 +1088,7 @@ namespace engine { assert(vertexBuffer->type == gfx::BufferType::VERTEX); assert(vertexBuffer != nullptr); assert(indexBuffer == nullptr || indexBuffer->type == gfx::BufferType::INDEX); - - VkResult res; + assert(uniformData != nullptr); DrawCall call{ .vertexBuffer = vertexBuffer, @@ -1086,18 +1096,9 @@ namespace engine { .count = count, }; - if (uniformData != nullptr) { - uint32_t frameIndex = pimpl->FRAMECOUNT % FRAMES_IN_FLIGHT; + size_t uniformDataSize = pipeline->uniformBuffers[pimpl->FRAMECOUNT % FRAMES_IN_FLIGHT]->size; - void* dest; - res = vmaMapMemory(pimpl->allocator, pipeline->uniformBuffers[frameIndex]->allocation, &dest); - assert(res == VK_SUCCESS); - - memcpy(dest, uniformData, pipeline->uniformBuffers[frameIndex]->size); - - vmaUnmapMemory(pimpl->allocator, pipeline->uniformBuffers[frameIndex]->allocation); - - } + memcpy(call.uniformData, uniformData, uniformDataSize); pimpl->drawQueues[pipeline].push(call); } @@ -1171,6 +1172,15 @@ namespace engine { vkCmdBindDescriptorSets(pimpl->commandBuffers[frameIndex], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->layout, 0, 1, &pipeline->descriptorSets[frameIndex], 0, nullptr); while (queue.empty() == false) { DrawCall call = queue.front(); + + void* uniformDest; + res = vmaMapMemory(pimpl->allocator, pipeline->uniformBuffers[frameIndex]->allocation, &uniformDest); + assert(res == VK_SUCCESS); + + memcpy(uniformDest, call.uniformData, pipeline->uniformBuffers[frameIndex]->size); + + vmaUnmapMemory(pimpl->allocator, pipeline->uniformBuffers[frameIndex]->allocation); + vkCmdBindVertexBuffers(pimpl->commandBuffers[frameIndex], 0, 1, &call.vertexBuffer->buffer, offsets); if (call.indexBuffer == nullptr) { @@ -1180,6 +1190,7 @@ namespace engine { vkCmdBindIndexBuffer(pimpl->commandBuffers[frameIndex], call.indexBuffer->buffer, 0, VK_INDEX_TYPE_UINT32); vkCmdDrawIndexed(pimpl->commandBuffers[frameIndex], call.count, 1, 0, 0, 0); } + queue.pop(); } } diff --git a/src/resources/mesh.cpp b/src/resources/mesh.cpp index 81e96f5..ce8de33 100644 --- a/src/resources/mesh.cpp +++ b/src/resources/mesh.cpp @@ -36,16 +36,11 @@ static void loadMeshFromFile(const std::filesystem::path& path, std::vectorcreateBuffer(gfx::BufferType::VERTEX, m_vertices.size() * sizeof(Vertex), m_vertices.data()); - ib = gfx->createBuffer(gfx::BufferType::INDEX, m_indices.size() * sizeof(uint32_t), m_indices.data()); + vb = gfxdev->createBuffer(gfx::BufferType::VERTEX, m_vertices.size() * sizeof(Vertex), m_vertices.data()); + ib = gfxdev->createBuffer(gfx::BufferType::INDEX, m_indices.size() * sizeof(uint32_t), m_indices.data()); } -void Mesh::drawMesh(const gfx::Pipeline* pipeline) -{ - gfx->draw(pipeline, vb, ib, m_indices.size(), nullptr); -} - -Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices) : Resource("", "mesh"), gfx(gfx) +Mesh::Mesh(const std::vector& vertices) : Resource("", "mesh") { // constructor for custom meshes without an index array m_vertices = vertices; // COPY over vertices @@ -55,7 +50,7 @@ Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices) : Resource("", " initMesh(); } -Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices, const std::vector& indices) : Resource("", "mesh"), gfx(gfx) +Mesh::Mesh(const std::vector& vertices, const std::vector& indices) : Resource("", "mesh") { m_vertices = vertices; // COPY over vertices m_indices = indices; // COPY over indices; @@ -63,7 +58,7 @@ Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices, const std::vecto } // To be used with the resource manager -Mesh::Mesh(GFXDevice* gfx, const std::filesystem::path& resPath) : Resource(resPath, "mesh"), gfx(gfx) +Mesh::Mesh(const std::filesystem::path& resPath) : Resource(resPath, "mesh") { loadMeshFromFile(resPath, &m_vertices, &m_indices); initMesh(); @@ -71,8 +66,8 @@ Mesh::Mesh(GFXDevice* gfx, const std::filesystem::path& resPath) : Resource(resP Mesh::~Mesh() { - gfx->destroyBuffer(ib); - gfx->destroyBuffer(vb); + gfxdev->destroyBuffer(ib); + gfxdev->destroyBuffer(vb); } } diff --git a/src/sceneroot.cpp b/src/sceneroot.cpp index 9d952b7..31ac13d 100644 --- a/src/sceneroot.cpp +++ b/src/sceneroot.cpp @@ -20,12 +20,6 @@ namespace engine { { } - SceneRoot::SceneRoot(const std::filesystem::path& file, struct GameIO things) : SceneRoot(things) - { - // TODO: make this a resource - //loadFromSceneFile(file); - } - SceneRoot::~SceneRoot() { }