Avara3D 0.2.0
C++ API reference
OGLRenderer.h
1//
2// OGLRenderer.h
3// avara3d
4//
5// Created by Morgan Davis on 4/24/18.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_RENDER_BACKEND_OPENGL_OGLRENDERER_H
10#define AVARA3D_RENDER_BACKEND_OPENGL_OGLRENDERER_H
11
12#include "a3d/render/Renderer.h"
13#include "a3d/render/backend/opengl/GLTypes.h"
14#include "a3d/render/backend/opengl/ImguiContext.h"
15#include "a3d/render/backend/opengl/OGLDebugLines.h"
16#include "a3d/render/backend/opengl/OGLDrawTimer.h"
17#include "a3d/render/backend/opengl/OGLMemoryTracker.h"
18#include "a3d/render/backend/opengl/OGLResourceCache.h"
19#include "a3d/render/backend/opengl/StatsOverlay.h"
20
21namespace a3d {
22
23 struct LinesPass;
24
25 class Color;
26 class GLSLProgram;
27 class Mesh;
28 class MeshElement;
29 class Line;
30 class Texture;
31
32 class OGLRenderer : public Renderer {
33
34 public:
35 // [Internal Types]
36
37 struct GLStateCache {
38 PipelineId pipelineId = INVALID_PIPELINE_ID;
39 gl::uint_t program = 0; // currently bound GL program
40 const Material* material = nullptr; // last bound material
41 uint32_t indexCount = 0;
42 };
43
44 struct BoundElement {
45 gl::uint_t vao = 0;
46 gl::sizei_t indexCount = 0;
47 gl::enum_t indexType = gl::value::unsigned_int;
48 gl::sizei_t vertexCount = 0;
49 };
50
51 // [Internal Static Members]
52
53 using GLGetProcAddress = void* (*) (const char* name);
54 static bool InitGL(GLGetProcAddress getProcAddress);
55
56 // [Internal Lifecycle Functions]
57
58 OGLRenderer();
59
60 OGLRenderer(const OGLRenderer& other) = delete;
61 OGLRenderer& operator=(const OGLRenderer& other) = delete;
62
63 OGLRenderer(OGLRenderer&& other) = delete;
64 OGLRenderer& operator=(OGLRenderer&& other) = delete;
65
66 ~OGLRenderer() override;
67
68 // [Renderer Internal Member Functions]
69
70 bool initialize(const RenderContext& context) override;
71 bool isInitialized() const override;
72
73 const Renderer::Capabilities& capabilities() const override;
74
75 void beginFrame(const Scene& scene,
76 const RenderContext& context,
77 const Scene::DebugOptions& debugOptions,
78 FrameStats& stats,
79 Profiler& profiler) override;
80 void endFrame(const Scene& scene,
81 const RenderContext& context,
82 const Scene::DebugOptions& debugOptions,
83 FrameStats& stats,
84 Profiler& profiler,
85 const FrameStatsHistory& statsHistory) override;
86
87 void preTraversal(const Scene& scene,
88 const RenderContext& context,
89 const Scene::DebugOptions& debugOptions,
90 FrameStats& stats) override;
91 void postTraversal(const Scene& scene,
92 const RenderContext& context,
93 const math::mat4& view,
94 const std::vector<Node*>& lightNodes,
95 const Scene::DebugOptions& debugOptions,
96 FrameStats& stats) override;
97
98 void clear(const ClearCommand& cmd, const RenderContext& context) override;
99
100 void renderPacket(DrawPacket& packet, const FrameParams& frame) override;
101
102 std::unique_ptr<Image> snapshot(const RenderContext& context) const override;
103
104 protected:
105 // [Renderer Protected Member Functions]
106
107 void drawBackground(const BackgroundPass& backgroundPass,
108 const math::mat4& view,
109 const math::mat4& proj) override;
110 void drawGround(const GroundPass& groundPass, const math::mat4& view, const math::mat4& proj) override;
111 void bindPipeline(PipelineId pipelineId) override;
112 void bindMaterial(const Material& material) override;
113 void bindMeshElement(const MeshElement& element) override;
114 void applyMVP(const math::mat4& model, const math::mat4& view, const math::mat4& proj) override;
115 void drawElements() override;
116 void drawLines(const LinesPass& pass,
117 const RenderContext& context,
118 const math::mat4& view,
119 const math::mat4& proj) override;
120 void resolvePacket(DrawPacket& packet, const FrameParams& frame) override;
121 void drawPacket(const DrawPacket& packet, const FrameParams& frame) override;
122
123 private:
124 // [Private Types]
125
126 struct Capabilities {
127 bool polygonMode {false};
128 bool drawTimer {false};
129 };
130
131 // [Private Member Functions]
132
133 GLSLProgram& programForShaderKind(ShaderKind kind) const;
134 void drawDebugLines(const math::mat4& model, const math::mat4& view, const math::mat4& proj);
135 void syncImguiMemoryStats();
136
137 // [Private Member Variables]
138
139 bool _isInitialized;
140 OGLRenderer::Capabilities _glCapabilities;
141 Renderer::Capabilities _capabilities;
142 unsigned _glEnvironmentUBO;
143 std::unique_ptr<GLSLProgram> _skyboxProgram;
144 std::unique_ptr<GLSLProgram> _groundProgram;
145 std::unique_ptr<GLSLProgram> _defaultProgram;
146 std::unique_ptr<GLSLProgram> _wireframeProgram;
147 std::unique_ptr<GLSLProgram> _linesProgram;
148 OGLMemoryTracker _memoryTracker;
149 OGLResourceCache _resourceCache;
150 GLStateCache _state;
151 BoundElement _boundElement;
152 std::unique_ptr<Mesh> _skyboxMesh; // should be value?
153 gl::uint_t _fullscreenTriangleVao;
154 OGLDebugLines _debugLines;
155 ImguiContext _imguiContext;
156 StatsOverlay _statsOverlay;
157 OGLDrawTimer _drawTimer;
158 };
159
160}
161
162#endif // AVARA3D_RENDER_BACKEND_OPENGL_OGLRENDERER_H
DebugOptions
Scene-wide debug visualization options.
Definition: Scene.h:61