Elevate Engine 1
Loading...
Searching...
No Matches
Renderer.cpp
Go to the documentation of this file.
1#include "eepch.h"
2#include "Renderer.h"
3
4// todo remove or fix these includes
7
11
12namespace Elevate
13{
14 Renderer::RendererStorage Renderer::s_data = RendererStorage();
15 RenderState Renderer::s_currentState = RenderState();
16 RendererAPI* Renderer::s_API = new OpenGLRendererAPI();
17 RenderCommandQueue Renderer::s_commands = RenderCommandQueue();
18 uint32_t Renderer::s_currentShaderID = 0;
19 uintptr_t Renderer::s_textures[16];
20
21 void Renderer::BeginFrame(const ScenePtr scene, const Camera& cam)
22 {
23 s_API->ClearTextureBindings();
24 s_currentShaderID = 0;
25 s_data.CameraPosition = cam.gameObject->GetPosition();
26 s_data.ViewProj = cam.GenViewProjectionMatrix();
27 s_data.ActiveLighting = scene->GetSceneLighting();
28 }
29
30 bool Renderer::BindShader(const std::shared_ptr<Shader>& shader)
31 {
32 uint32_t id = shader->GetID();
33 if (s_currentShaderID != id)
34 {
35 shader->Bind();
36 s_currentShaderID = id;
37 return true;
38 }
39 return false;
40 }
41
42 void Renderer::ApplySystemUniforms(const std::shared_ptr<Shader>& shader)
43 {
44 // If the binded shader changed
45 if (BindShader(shader))
46 {
47 shader->SetProjectionViewMatrix(s_data.ViewProj);
48 shader->SetCameraPosition(s_data.CameraPosition);
49 }
50 }
51
52 // RENDER API STATIC WRAPPER
53 void Renderer::SetClearColor(const glm::vec4& color)
54 {
55 s_API->SetClearColor(color);
56 }
57
59 {
60 s_API->Clear();
61 }
62
64 {
65 s_API->FlushBuffers();
66 }
67
68 void Renderer::SetViewport(int x, int y, int width, int height)
69 {
70 s_API->SetViewport(x, y, width, height);
71 }
72
74 {
75 if (vao)
76 {
77 s_API->DrawArray(vao, primitive);
78 }
79 }
80
81 void Renderer::DrawArray(const std::shared_ptr<VertexArray>& vao, DrawPrimitiveType primitive)
82 {
83 DrawArray(vao.get(), primitive);
84 }
85
87 {
88 s_commands.FlushAll();
89 }
90
92 {
93 // todo make a first invalid call to make sure the GPU is synced with this cache before the user does anything
94 if (newState.Cullface != s_currentState.Cullface)
95 {
96 s_API->SetCullingState(newState.Cullface);
97 }
98
99 if (newState.DepthWrite != s_currentState.DepthWrite)
100 {
101 s_API->SetDepthWrittingState(newState.DepthWrite);
102 }
103
104 if (newState.DepthTest != s_currentState.DepthTest)
105 {
106 s_API->SetDepthTestingState(newState.DepthTest);
107 }
108
109 s_currentState = newState;
110 }
111
113 {
114 // Update the renderer state if necessary
115 PushRenderState(command.m_State);
116
117 // Setup the Material and Shader
118 uint32_t prevshader = s_currentShaderID;
119 if (command.m_MaterialInstance)
120 {
121 auto shader = command.m_MaterialInstance->GetShader();
122 if (shader)
123 {
124 ApplySystemUniforms(shader);
125 shader->SetModelMatrix(command.Transform);
126 s_data.ActiveLighting->UploadToShader(shader);
127 command.m_MaterialInstance->Apply();
128 }
129 }
130 // Actually render the vertex array
132 }
133
135 {
136 s_commands.Submit(type, command);
137 }
138
139 void Renderer::SubmitMesh(const std::shared_ptr<VertexArray>& vao, const std::shared_ptr<Material>& material, const glm::mat4& transform, RenderBucket::Type bucketType)
140 {
141 RenderCommand command;
142 command.m_VertexArray = vao.get();
143 command.m_MaterialInstance = material.get();
144 command.Transform = transform;
145
146 if (bucketType == RenderBucket::Transparent)
147 {
148 command.m_State.BlendEnable = true;
149 command.m_State.DepthWrite = false; // Transparents usually don't write to depth
150 }
151 else
152 {
153 command.m_State.BlendEnable = false;
154 command.m_State.DepthWrite = true;
155 }
156
157 Submit(bucketType, command);
158 }
159
160 void Renderer::BindTexture(const std::shared_ptr<Texture>& texture, uint8_t slot)
161 {
162 // todo optimize and make sure EVERY texture uses this
163 uintptr_t textureID = texture ? reinterpret_cast<uintptr_t>(texture->GetNativeHandle()) : 0;
164 if (s_textures[slot] != textureID)
165 {
166 if (texture)
167 {
168 texture->Bind(slot);
169 }
170 s_textures[slot] = textureID;
171 }
172 }
173
175 {
176 // Make each texture ptr to an impossible value to make sure we bind each frame.
177 for (int i = 0; i < std::size(s_textures); i++) {
178 s_textures[i] = (uintptr_t)-1;
179 }
180 }
181}
glm::mat4 GenViewProjectionMatrix() const
Definition Camera.cpp:64
GameObject * gameObject
Definition Component.h:73
std::shared_ptr< Shader > GetShader()
Definition Material.cpp:94
void Submit(RenderBucket::Type type, const RenderCommand &command)
virtual void SetCullingState(bool enabled) const =0
virtual void SetViewport(int x, int y, int width, int height) const =0
virtual void FlushBuffers() const =0
virtual void DrawArray(const VertexArray *vao, DrawPrimitiveType primitive=DrawPrimitiveType::Triangles) const =0
virtual void Clear() const =0
virtual void SetClearColor(const glm::vec4 &color) const =0
virtual void SetDepthWrittingState(bool enabled) const =0
virtual void ClearTextureBindings() const =0
virtual void SetDepthTestingState(bool enabled) const =0
static void PushRenderState(const RenderState &newState)
Definition Renderer.cpp:91
static void FlushBuffers()
Definition Renderer.cpp:63
static void SetClearColor(const glm::vec4 &color)
Definition Renderer.cpp:53
static void SetViewport(int x, int y, int width, int height)
Definition Renderer.cpp:68
static void BindTexture(const std::shared_ptr< Texture > &texture, uint8_t slot=0)
Definition Renderer.cpp:160
static void SubmitMesh(const std::shared_ptr< VertexArray > &vao, const std::shared_ptr< Material > &material, const glm::mat4 &transform, RenderBucket::Type bucketType=RenderBucket::Opaque)
Definition Renderer.cpp:139
static void BeginFrame(const ScenePtr scene, const Camera &cam)
Definition Renderer.cpp:21
static void InvalidateStateCache()
Definition Renderer.cpp:174
static void DrawStack()
Definition Renderer.cpp:86
static bool BindShader(const std::shared_ptr< Shader > &shader)
Definition Renderer.cpp:30
static void Submit(RenderBucket::Type type, const RenderCommand &command)
Definition Renderer.cpp:134
static void DrawArray(const VertexArray *vao, DrawPrimitiveType primitive=DrawPrimitiveType::Triangles)
Definition Renderer.cpp:73
static void Dispatch(const RenderCommand &command)
Immediatly process a RenderCommand. Do not use directly unless you know what you are donig.
Definition Renderer.cpp:112
static void ApplySystemUniforms(const std::shared_ptr< Shader > &shader)
Definition Renderer.cpp:42
static void Clear()
Definition Renderer.cpp:58
std::shared_ptr< Scene > ScenePtr
Definition Renderer.h:12
DrawPrimitiveType
Definition RendererAPI.h:17
Material * m_MaterialInstance
VertexArray * m_VertexArray