Skip to content

Commit

Permalink
Feat: Starting grid pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
brenocq committed Jan 3, 2024
1 parent 06cb8e3 commit 40a0d8e
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/atta/graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/atta/graphics/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
88 changes: 88 additions & 0 deletions src/atta/graphics/renderers/common/gridPipeline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//--------------------------------------------------
// Atta Graphics Module
// gridPipeline.cpp
// Date: 2021-11-16
// By Breno Cunha Queiroz
//--------------------------------------------------
#include <atta/graphics/interface.h>
#include <atta/graphics/renderers/common/gridPipeline.h>
#include <atta/resource/interface.h>

namespace atta::graphics {

size_t GridPipeline::_gridId = 0;

GridPipeline::GridPipeline(std::shared_ptr<RenderPass> renderPass) : _numLines(0) {
Pipeline::CreateInfo pipelineInfo{};
pipelineInfo.shader = gfx::create<Shader>("shaders/grid/grid.asl");
pipelineInfo.renderPass = renderPass;
pipelineInfo.primitive = Pipeline::Primitive::LINE;
pipelineInfo.debugName = StringId("Grid Pipeline");
_pipeline = gfx::create<Pipeline>(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<uint8_t>(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<res::Mesh>(_gridMeshName, meshInfo);
}

GridPipeline::~GridPipeline() {
// TODO Delete grid
}

void GridPipeline::update(std::shared_ptr<Camera> 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<res::Mesh>(_gridMeshName)->updateVertices(std::vector<uint8_t>(data, data + size));
}

void GridPipeline::render(std::shared_ptr<Camera> 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
48 changes: 48 additions & 0 deletions src/atta/graphics/renderers/common/gridPipeline.h
Original file line number Diff line number Diff line change
@@ -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 <atta/graphics/cameras/camera.h>
#include <atta/graphics/pipeline.h>
#include <atta/graphics/renderQueue.h>

namespace atta::graphics {

class GridPipeline final {
public:
GridPipeline(std::shared_ptr<RenderPass> renderPass);
~GridPipeline();

/// Update GPU line grid
void update(std::shared_ptr<Camera> 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> camera);

private:
std::shared_ptr<Pipeline> _pipeline;

struct Line {
vec3 pos;
vec4 color;
float width;
};
static constexpr size_t MAX_LINES = 1000;
std::array<Line, MAX_LINES> _lines;
size_t _numLines;

StringId _gridMeshName;
static size_t _gridId;
};

} // namespace atta::graphics

#endif // ATTA_GRAPHICS_RENDERERS_COMMON_GRID_PIPELINE_H
7 changes: 6 additions & 1 deletion src/atta/graphics/renderers/fastRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ FastRenderer::FastRenderer() : Renderer("FastRenderer"), _wasResized(false) {
_geometryPipeline = graphics::create<Pipeline>(pipelineInfo);

//---------- Common pipelines ----------//
_selectedPipeline = std::make_unique<SelectedPipeline>(_renderPass);
_drawerPipeline = std::make_unique<DrawerPipeline>(_renderPass);
_gridPipeline = std::make_unique<GridPipeline>(_renderPass);
_selectedPipeline = std::make_unique<SelectedPipeline>(_renderPass);
}

FastRenderer::~FastRenderer() {}
Expand All @@ -72,6 +73,9 @@ void FastRenderer::render(std::shared_ptr<Camera> camera) {
if (_renderDrawer)
_drawerPipeline->update();

// Update grid data
_gridPipeline->update(camera);

// Render
_renderQueue->begin();
{
Expand Down Expand Up @@ -113,6 +117,7 @@ void FastRenderer::render(std::shared_ptr<Camera> camera) {

if (_renderDrawer)
_drawerPipeline->render(camera);
_gridPipeline->render(camera);
}
_renderPass->end();
}
Expand Down
2 changes: 2 additions & 0 deletions src/atta/graphics/renderers/fastRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <atta/graphics/pipeline.h>
#include <atta/graphics/renderPass.h>
#include <atta/graphics/renderers/common/drawerPipeline.h>
#include <atta/graphics/renderers/common/gridPipeline.h>
#include <atta/graphics/renderers/common/selectedPipeline.h>
#include <atta/graphics/renderers/renderer.h>

Expand All @@ -31,6 +32,7 @@ class FastRenderer final : public Renderer {
std::shared_ptr<RenderPass> _renderPass;
std::shared_ptr<Pipeline> _geometryPipeline;
std::unique_ptr<SelectedPipeline> _selectedPipeline;
std::unique_ptr<GridPipeline> _gridPipeline;
std::unique_ptr<DrawerPipeline> _drawerPipeline;
bool _wasResized;
};
Expand Down
2 changes: 1 addition & 1 deletion src/atta/resource/resources/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace atta::resource {
class Mesh : public Resource, public memory::AllocatedObject<Mesh, SID("ResourceAllocator")> {
public:
struct VertexElement {
enum Type { VEC2 = 0, VEC3, VEC4 };
enum Type { FLOAT = 0, VEC2, VEC3, VEC4 };
Type type;
std::string name;
};
Expand Down

0 comments on commit 40a0d8e

Please sign in to comment.