From 40a0d8e7f3eed0a628931dfc1e5942ea23aef8bb Mon Sep 17 00:00:00 2001 From: Breno Cunha Queiroz Date: Wed, 3 Jan 2024 07:59:03 +0100 Subject: [PATCH] Feat: Starting grid pipeline --- src/atta/graphics/CMakeLists.txt | 7 +- src/atta/graphics/manager.cpp | 3 + .../renderers/common/gridPipeline.cpp | 88 +++++++++++++++++++ .../graphics/renderers/common/gridPipeline.h | 48 ++++++++++ src/atta/graphics/renderers/fastRenderer.cpp | 7 +- src/atta/graphics/renderers/fastRenderer.h | 2 + src/atta/resource/resources/mesh.h | 2 +- 7 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 src/atta/graphics/renderers/common/gridPipeline.cpp create mode 100644 src/atta/graphics/renderers/common/gridPipeline.h diff --git a/src/atta/graphics/CMakeLists.txt b/src/atta/graphics/CMakeLists.txt index 2f52f486..e052f23c 100644 --- a/src/atta/graphics/CMakeLists.txt +++ b/src/atta/graphics/CMakeLists.txt @@ -19,12 +19,13 @@ set(ATTA_GRAPHICS_MODULE_SOURCE compute/entityClick.cpp - renderers/renderer.cpp + renderers/common/drawerPipeline.cpp + renderers/common/gridPipeline.cpp + renderers/common/selectedPipeline.cpp renderers/fastRenderer.cpp renderers/pbrRenderer.cpp renderers/phongRenderer.cpp - renderers/common/selectedPipeline.cpp - renderers/common/drawerPipeline.cpp + renderers/renderer.cpp windows/window.cpp windows/nullWindow.cpp diff --git a/src/atta/graphics/manager.cpp b/src/atta/graphics/manager.cpp index 8ca92570..1ffe4518 100644 --- a/src/atta/graphics/manager.cpp +++ b/src/atta/graphics/manager.cpp @@ -409,6 +409,9 @@ void Manager::createMesh(StringId sid) { for (resource::Mesh::VertexElement element : mesh->getVertexLayout()) { BufferLayout::Element::Type type; switch (element.type) { + case resource::Mesh::VertexElement::FLOAT: + type = BufferLayout::Element::Type::FLOAT; + break; case resource::Mesh::VertexElement::VEC2: type = BufferLayout::Element::Type::VEC2; break; diff --git a/src/atta/graphics/renderers/common/gridPipeline.cpp b/src/atta/graphics/renderers/common/gridPipeline.cpp new file mode 100644 index 00000000..27ee310c --- /dev/null +++ b/src/atta/graphics/renderers/common/gridPipeline.cpp @@ -0,0 +1,88 @@ +//-------------------------------------------------- +// Atta Graphics Module +// gridPipeline.cpp +// Date: 2021-11-16 +// By Breno Cunha Queiroz +//-------------------------------------------------- +#include +#include +#include + +namespace atta::graphics { + +size_t GridPipeline::_gridId = 0; + +GridPipeline::GridPipeline(std::shared_ptr renderPass) : _numLines(0) { + Pipeline::CreateInfo pipelineInfo{}; + pipelineInfo.shader = gfx::create("shaders/grid/grid.asl"); + pipelineInfo.renderPass = renderPass; + pipelineInfo.primitive = Pipeline::Primitive::LINE; + pipelineInfo.debugName = StringId("Grid Pipeline"); + _pipeline = gfx::create(pipelineInfo); + + // Create line mesh + _gridMeshName = "atta::gfx::GridPipeline[" + std::to_string(_gridId++) + "]"; + LOG_DEBUG("GridPipeline", "Mesh id $0", _gridMeshName); + res::Mesh::CreateInfo meshInfo{}; + uint8_t* data = (uint8_t*)_lines.data(); + size_t size = _lines.size() * sizeof(Line); + meshInfo.vertices = std::vector(data, data + size); + meshInfo.vertexLayout.push_back({resource::Mesh::VertexElement::VEC3, "iPos"}); + meshInfo.vertexLayout.push_back({resource::Mesh::VertexElement::VEC4, "iColor"}); + meshInfo.vertexLayout.push_back({resource::Mesh::VertexElement::FLOAT, "iWidth"}); + res::create(_gridMeshName, meshInfo); +} + +GridPipeline::~GridPipeline() { + // TODO Delete grid +} + +void GridPipeline::update(std::shared_ptr camera) { + // Update grid + { + int i = 0; + int size = 100; + for (int j = -size; j <= size; j++) { + _lines[i++].pos = {float(j), -float(size), 0.0f}; + _lines[i++].pos = {float(j), float(size), 0.0f}; + _lines[i++].pos = {-float(size), float(j), 0.0f}; + _lines[i++].pos = {float(size), float(j), 0.0f}; + } + + for (int j = 0; j < i; j++) { + float width = 1.0f; + vec4 color = {1, 1, 1, 0.2}; + if ((j / 4) % 10 == 0) + color = {1, 1, 1, 0.4}; + + if (_lines[j].pos.y == 0.0f) + _lines[j].color = {0.6, 0.2, 0.2, 1.0f}; + else if (_lines[j].pos.x == 0.0f) + _lines[j].color = {0.2, 0.6, 0.2, 1.0f}; + else + _lines[j].color = color; + _lines[j].width = width; + } + _numLines = i; + } + + // Update mesh with grid + uint8_t* data = (uint8_t*)_lines.data(); + size_t size = _lines.size() * sizeof(Line); + res::get(_gridMeshName)->updateVertices(std::vector(data, data + size)); +} + +void GridPipeline::render(std::shared_ptr camera) { + if (_numLines == 0) + return; + + _pipeline->begin(); + { + _pipeline->setMat4("uProjection", camera->getProj()); + _pipeline->setMat4("uView", camera->getView()); + _pipeline->renderMesh(_gridMeshName, _numLines); + } + _pipeline->end(); +} + +} // namespace atta::graphics diff --git a/src/atta/graphics/renderers/common/gridPipeline.h b/src/atta/graphics/renderers/common/gridPipeline.h new file mode 100644 index 00000000..6045667d --- /dev/null +++ b/src/atta/graphics/renderers/common/gridPipeline.h @@ -0,0 +1,48 @@ +//-------------------------------------------------- +// Atta Graphics Module +// gridPipeline.h +// Date: 2021-11-16 +// By Breno Cunha Queiroz +//-------------------------------------------------- +#ifndef ATTA_GRAPHICS_RENDERERS_COMMON_GRID_PIPELINE_H +#define ATTA_GRAPHICS_RENDERERS_COMMON_GRID_PIPELINE_H +#include +#include +#include + +namespace atta::graphics { + +class GridPipeline final { + public: + GridPipeline(std::shared_ptr renderPass); + ~GridPipeline(); + + /// Update GPU line grid + void update(std::shared_ptr camera); + + /** + * @brief Render lines from grid + * + * @note GridPipeline::update should be called before rendering to make sure that the GPU data is up to date + */ + void render(std::shared_ptr camera); + + private: + std::shared_ptr _pipeline; + + struct Line { + vec3 pos; + vec4 color; + float width; + }; + static constexpr size_t MAX_LINES = 1000; + std::array _lines; + size_t _numLines; + + StringId _gridMeshName; + static size_t _gridId; +}; + +} // namespace atta::graphics + +#endif // ATTA_GRAPHICS_RENDERERS_COMMON_GRID_PIPELINE_H diff --git a/src/atta/graphics/renderers/fastRenderer.cpp b/src/atta/graphics/renderers/fastRenderer.cpp index db1e44c5..c02b1472 100644 --- a/src/atta/graphics/renderers/fastRenderer.cpp +++ b/src/atta/graphics/renderers/fastRenderer.cpp @@ -47,8 +47,9 @@ FastRenderer::FastRenderer() : Renderer("FastRenderer"), _wasResized(false) { _geometryPipeline = graphics::create(pipelineInfo); //---------- Common pipelines ----------// - _selectedPipeline = std::make_unique(_renderPass); _drawerPipeline = std::make_unique(_renderPass); + _gridPipeline = std::make_unique(_renderPass); + _selectedPipeline = std::make_unique(_renderPass); } FastRenderer::~FastRenderer() {} @@ -72,6 +73,9 @@ void FastRenderer::render(std::shared_ptr camera) { if (_renderDrawer) _drawerPipeline->update(); + // Update grid data + _gridPipeline->update(camera); + // Render _renderQueue->begin(); { @@ -113,6 +117,7 @@ void FastRenderer::render(std::shared_ptr camera) { if (_renderDrawer) _drawerPipeline->render(camera); + _gridPipeline->render(camera); } _renderPass->end(); } diff --git a/src/atta/graphics/renderers/fastRenderer.h b/src/atta/graphics/renderers/fastRenderer.h index 89b4a486..f8ee9870 100644 --- a/src/atta/graphics/renderers/fastRenderer.h +++ b/src/atta/graphics/renderers/fastRenderer.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -31,6 +32,7 @@ class FastRenderer final : public Renderer { std::shared_ptr _renderPass; std::shared_ptr _geometryPipeline; std::unique_ptr _selectedPipeline; + std::unique_ptr _gridPipeline; std::unique_ptr _drawerPipeline; bool _wasResized; }; diff --git a/src/atta/resource/resources/mesh.h b/src/atta/resource/resources/mesh.h index c1b59bbe..88bfe2fc 100644 --- a/src/atta/resource/resources/mesh.h +++ b/src/atta/resource/resources/mesh.h @@ -20,7 +20,7 @@ namespace atta::resource { class Mesh : public Resource, public memory::AllocatedObject { public: struct VertexElement { - enum Type { VEC2 = 0, VEC3, VEC4 }; + enum Type { FLOAT = 0, VEC2, VEC3, VEC4 }; Type type; std::string name; };