diff --git a/engine/src/application.cpp b/engine/src/application.cpp index 3fee77b..f3c7852 100644 --- a/engine/src/application.cpp +++ b/engine/src/application.cpp @@ -10,10 +10,13 @@ #define GLM_FORCE_RADIANS #define GLM_FORCE_DEPTH_ZERO_TO_ONE +#include + #include #include "buffer.hpp" #include "device.hpp" +#include "imgui_renderer.hpp" #include "mesh.hpp" #include "pipelines/graphics_pipeline.hpp" #include "render_context.hpp" @@ -49,72 +52,10 @@ class Application::Impl { device{&window}, renderContext{window, device} {} - /*void run(const std::string& vertShaderPath, - const std::string& fragShaderPath) { - log::info("Running Render Triangle Test..."); - - RenderContext renderContext{window, device}; - - auto vertexShader = Shader::Builder() - .fromGLSL(vertShaderPath) - .setStage(ShaderStage::VERTEX) - .build(); - auto fragmentShader = Shader::Builder() - .fromGLSL(fragShaderPath) - .setStage(ShaderStage::FRAGMENT) - .build(); - - vk::PushConstantRange pushConstantRange{}; - pushConstantRange.stageFlags = - vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment; - pushConstantRange.offset = 0; - pushConstantRange.size = sizeof(PushConstants); - - vk::PipelineLayoutCreateInfo pipelineLayoutInfo{}; - pipelineLayoutInfo.pushConstantRangeCount = 1; - pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange; - - vk::UniquePipelineLayout pipelineLayout = - device.get()->createPipelineLayoutUnique(pipelineLayoutInfo); - - std::vector bindingDescriptions{ - {0, sizeof(Vertex), vk::VertexInputRate::eVertex}}; - std::vector attributeDescriptions{ - {0, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, position)}, - {1, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, color)}}; - - std::unique_ptr graphicsPipeline = - GraphicsPipeline::Builder(device) - .setVertexShader(*vertexShader) - .setFragmentShader(*fragmentShader) - .setPipelineLayout(pipelineLayout.get()) - .setColorAttachmentFormats({renderContext.getImageFormat()}) - .setVertexInput(bindingDescriptions, attributeDescriptions) - .build(); - - // Oversized triangle covering the full screen so the fragment shader runs - // for every pixel. - std::vector vertices{ - {{-1.0f, -1.0f}, {1.0f, 0.0f, 0.0f}}, - {{3.0f, -1.0f}, {0.0f, 1.0f, 0.0f}}, - {{-1.0f, 3.0f}, {0.0f, 0.0f, 1.0f}}, - }; - - Buffer vertexBuffer{device, sizeof(Vertex), - static_cast(vertices.size()), - vk::BufferUsageFlagBits::eVertexBuffer, - vk::MemoryPropertyFlagBits::eHostVisible | - vk::MemoryPropertyFlagBits::eHostCoherent}; - vertexBuffer.map(); - vertexBuffer.writeToBuffer(vertices.data(), - sizeof(Vertex) * vertices.size()); - vertexBuffer.unmap(); - - - }*/ - void run() { ModelRenderSystem modelRenderSystem{device, renderContext}; + ImGuiRenderer imguiRenderer{window, device, renderContext.getImageFormat(), + renderContext.getImageCount()}; this->application.onCreate(); @@ -139,7 +80,6 @@ class Application::Impl { static_cast(extent.height)), this->application.camera}; this->application.onUpdate(frameInfo); - this->application.onRender(); this->application.camera.setProjection( -frameInfo.currentFramebufferExtent.x / 2.f, frameInfo.currentFramebufferExtent.x / 2.f, @@ -151,6 +91,11 @@ class Application::Impl { continue; } + imguiRenderer.beginFrame(); + + this->application.panelRegistry.update(frameInfo); + this->application.onRender(); + renderContext.transitionCurrentImage( commandBuffer, vk::ImageLayout::eColorAttachmentOptimal); @@ -194,6 +139,8 @@ class Application::Impl { modelRenderSystem.render(commandBuffer, instances, frameInfo); this->drawCalls.clear(); + imguiRenderer.endFrame(commandBuffer); + commandBuffer.endRendering(); renderContext.transitionCurrentImage(commandBuffer, @@ -233,24 +180,11 @@ class Application::Impl { this->meshes.clear(); } - MeshHandle createMesh(std::vector vertices, - std::vector indices) { - Mesh::Builder builder{}; - builder.vertices.reserve(vertices.size()); - for (const CanvasVertex& vertex : vertices) { - builder.vertices.push_back({vertex.position, vertex.color}); - } - builder.indices = std::move(indices); - - this->persistentMeshes.push_back( - std::make_unique(this->device, builder)); - return static_cast(this->persistentMeshes.size() - 1); + void drawMesh(const Mesh& mesh, const glm::mat4& transform) { + this->drawCalls.push_back({&mesh, transform}); } - void drawMesh(MeshHandle handle, const glm::mat4& transform) { - this->drawCalls.push_back( - {this->persistentMeshes[handle].get(), transform}); - } + Device& getDevice() { return device; } private: Application& application; @@ -260,7 +194,6 @@ class Application::Impl { RenderContext renderContext; std::vector> meshes; - std::vector> persistentMeshes; std::vector drawCalls; }; @@ -271,7 +204,9 @@ void Application::setCommandLineArgs(int argc, const char* const* argv) { Application::Application(const Config& config) : impl{std::make_unique(*this, config)}, canvas{this}, - args{std::move(commandLineArgs)} {} + args{std::move(commandLineArgs)}, + assetRegistry{impl->getDevice()}, + _debugUI{this->panelRegistry} {} Application::~Application() = default; @@ -284,13 +219,8 @@ void Application::pushMesh(std::vector vertices, void Application::clearMeshes() { impl->clearMeshes(); } -MeshHandle Application::createMesh(std::vector vertices, - std::vector indices) { - return impl->createMesh(std::move(vertices), std::move(indices)); -} - void Application::drawMesh(MeshHandle handle, const glm::mat4& transform) { - impl->drawMesh(handle, transform); + impl->drawMesh(assetRegistry.get(handle), transform); } } // namespace sophia