galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Renderer.cpp
Go to the documentation of this file.
1
7
10
11#include "Renderer.hpp"
12
13#ifdef GALAXY_WIN_PLATFORM
16#endif
17
18namespace galaxy
19{
21 : m_quads {Settings::max_quads(), 4, galaxy::graphics::gen_default_indices()}
22 , m_pass {nullptr}
23 , m_pipeline {nullptr}
24 , m_uniform_data_index {0u}
25 {
28
29 // Optimization here would be to object pool this.
30 m_commands.reserve(Settings::max_quads() / 4);
31
32 // Convert to object pool.
34 }
35
37 {
38 m_pass = nullptr;
39 m_pipeline = nullptr;
40 }
41
43 {
45
46 m_commands.clear();
48
49 m_pass = nullptr;
50 m_pipeline = nullptr;
51 }
52
54 {
55 glBindFramebuffer(GL_FRAMEBUFFER, pass.target);
56 glViewport(pass.viewport.x, pass.viewport.y, pass.viewport.z, pass.viewport.w);
57 glScissor(pass.scissor.x, pass.scissor.y, pass.scissor.z, pass.scissor.w);
58
59 glClearColor(pass.clear_col.x, pass.clear_col.y, pass.clear_col.z, pass.clear_col.w);
60 glClearDepth(pass.clear_depth);
61 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
62
63 m_pass = &pass;
64 }
65
67 {
68 for (const auto& feature : pipeline.enabled)
69 {
70 glEnable(feature);
71 }
72
73 for (const auto& feature : pipeline.disabled)
74 {
75 glDisable(feature);
76 }
77
78 glCullFace(pipeline.cull_face);
79 glDepthFunc(pipeline.depth_func);
80 glBlendEquation(pipeline.blend_equation);
81 glBlendFunc(GL_SRC_ALPHA, pipeline.blend_function);
82
83 glUseProgram(pipeline.shader);
84 glActiveTexture(GL_TEXTURE0);
85
86 // There is no texture sampler object to bind because we use bindless textures.
87
88 m_pipeline = &pipeline;
89 }
90
91 void Renderer::push_cmd(RenderCmd& cmd) noexcept
92 {
93 // Add uniform data first.
94 m_uniform_data[m_uniform_data_index] = cmd.data;
95
96 // update uniform data location.
97 for (auto& vert : cmd.vertices)
98 {
99 vert.m_index = m_uniform_data_index;
100 }
101
102 // Then increment for next call.
103 m_uniform_data_index++;
104
105 // We want to batch quads and just draw the rest regularly.
106 if (cmd.type == GLRenderMode::QUADS)
107 {
108 m_quads.push(cmd.vertices);
109 }
110 else
111 {
112 m_commands.emplace_back(cmd);
113 }
114 }
115
117 {
119 m_camera_storage.buffer(1, &camera.get_data());
120
122 m_uniform_storage.buffer(static_cast<unsigned int>(m_uniform_data.size()), m_uniform_data.data());
123
124 m_quads.flush();
125 m_quads.bind();
126
127 glDrawElements(GL_TRIANGLES, m_quads.count(), GL_UNSIGNED_INT, m_quads.offset());
128
129 for (const auto& cmd : m_commands)
130 {
131 cmd.vao->bind();
132 glDrawElements(static_cast<unsigned int>(cmd.type), cmd.vao->count(), GL_UNSIGNED_INT, cmd.vao->offset());
133 }
134 }
135} // namespace galaxy
136
137#ifdef GALAXY_WIN_PLATFORM
139#endif
#define GALAXY_DISABLE_WARNING_POP
Definition Pragma.hpp:62
#define GALAXY_DISABLE_WARNING(x)
Definition Pragma.hpp:63
#define GALAXY_DISABLE_WARNING_PUSH
Macro for windows platform detection.
Definition Pragma.hpp:61
Orthographic 2D camera.
Definition Camera.hpp:20
Data & get_data() noexcept
Get camera view and proj.
Definition Camera.cpp:246
ShaderBuffer m_camera_storage
Camera buffer.
Definition Renderer.hpp:105
~Renderer()
Destructor.
Definition Renderer.cpp:36
unsigned int m_uniform_data_index
Uniform data current max index.
Definition Renderer.hpp:115
std::vector< RenderCmd > m_commands
RenderCmd storage.
Definition Renderer.hpp:85
std::vector< RenderData > m_uniform_data
Renderable uniform data storage.
Definition Renderer.hpp:110
void end_pass(Camera &camera)
Renders everything configured in this pass.
Definition Renderer.cpp:116
ShaderBuffer m_uniform_storage
Renderable uniforms buffer.
Definition Renderer.hpp:100
Renderer()
Constructor.
Definition Renderer.cpp:20
VertexBatch m_quads
Quad vertex batch.
Definition Renderer.hpp:80
void push_cmd(RenderCmd &cmd) noexcept
Push a renderable object.
Definition Renderer.cpp:91
void bind_pipeline(RenderPipeline &pipeline)
Binds the GPU state specified in pipeline.
Definition Renderer.cpp:66
RenderPass * m_pass
Currently active pass.
Definition Renderer.hpp:90
void begin_pass(RenderPass &pass)
Binds framebuffer, viewport, sissor region and clears buffers.
Definition Renderer.cpp:53
void prepare()
Called only in the update loop when we need to refresh data to render.
Definition Renderer.cpp:42
RenderPipeline * m_pipeline
Currently active pipeline.
Definition Renderer.hpp:95
void bind() const
Bind buffer.
void buffer(const unsigned int count, Object *data)
Buffer data.
void set_index(const GLBufferBinding index)
Set vertex shader binding point.
void bind() const noexcept
Bind this batchs vertex array object.
void flush() noexcept
Copy all data to GPU.
int count() const noexcept
Get the index count.
void prepare() noexcept
Sets up counters to begin pushing vertices.
void * offset() noexcept
Gets index offset.
Application.hpp galaxy.
@ UNIFORMS
Shader uniforms.
@ CAMERA
Camera transform data.
Data required to draw an object.
The configuration for drawing something.
unsigned int target
This is the framebuffer to draw to.
glm::ivec4 viewport
This is the glViewport. z,w is width/height.
glm::ivec4 scissor
OpenGL scissor region. https://gamedev.stackexchange.com/a/40713.
glm::vec4 clear_col
Colour buffer clear colour.
float clear_depth
Depth buffer clear depth.
GPU configuration.
unsigned int blend_equation
glBlendEquation.
unsigned int cull_face
glCullFace.
std::vector< unsigned int > enabled
glEnable features.
unsigned int blend_function
glBlendFunc for GL_SRC_ALPHA.
unsigned int depth_func
glDepthFunc.
std::vector< unsigned int > disabled
glDisable features.
unsigned int shader
Pipeline Ubershader.
Helper class to encapsulate our global settings we want to quickly access across app.
Definition Settings.hpp:23
static auto max_quads() noexcept -> int
Max quads to render at once.
Definition Settings.cpp:209