Hymn to Beauty
C++ 3D Engine
OpenGLRenderer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../Interface/LowLevelRenderer.hpp"
4
6#include <glad/glad.h>
7
8struct GLFWwindow;
9
10namespace Utility {
11class Window;
12}
13
14namespace Video {
15
16class OpenGLShaderProgram;
17class OpenGLBufferAllocator;
18class OpenGLRenderPassAllocator;
19class OpenGLRenderTargetAllocator;
20class OpenGLSampler;
21
24 public:
26
29 explicit OpenGLRenderer(Utility::Window* window);
30
32 ~OpenGLRenderer() final;
33
35 void BeginFrame() final;
36 void Submit(CommandBuffer* commandBuffer) final;
37 void Present() final;
38 Buffer* CreateBuffer(Buffer::BufferUsage bufferUsage, uint32_t size, const void* data = nullptr) final;
39 Buffer* CreateTemporaryBuffer(Buffer::BufferUsage bufferUsage, uint32_t size, const void* data = nullptr) final;
40 VertexDescription* CreateVertexDescription(unsigned int attributeCount, const VertexDescription::Attribute* attributes, bool indexBuffer = false) final;
41 GeometryBinding* CreateGeometryBinding(const VertexDescription* vertexDescription, Buffer* vertexBuffer, GeometryBinding::IndexType indexType = GeometryBinding::IndexType::NONE, const Buffer* indexBuffer = nullptr) final;
42 Shader* CreateShader(const ShaderSource& shaderSource, Shader::Type type) final;
43 ShaderProgram* CreateShaderProgram(std::initializer_list<const Shader*> shaders) final;
44 Texture* CreateTexture(const glm::uvec2 size, Texture::Format format, int components, unsigned char* data) final;
45 const Sampler* GetSampler(Sampler::Filter filter, Sampler::Clamping clamping) const final;
46 Texture* CreateRenderTarget(const glm::uvec2& size, Texture::Format format) final;
47 void FreeRenderTarget(Texture* renderTarget) final;
48 GraphicsPipeline* CreateGraphicsPipeline(const ShaderProgram* shaderProgram, const GraphicsPipeline::Configuration& configuration, const VertexDescription* vertexDescription = nullptr) final;
49 ComputePipeline* CreateComputePipeline(const ShaderProgram* shaderProgram) final;
50 void Wait() final;
51 unsigned char* ReadImage(Texture* texture) final;
52 const std::vector<Profiling::Event>& GetTimeline() const final;
53 const OptionalFeatures& GetOptionalFeatures() const final;
54
56
60
62
65 GLuint GetFreeQuery();
66
67 private:
68 OpenGLRenderer(const OpenGLRenderer& other) = delete;
69
70 GLFWwindow* window;
71
72 // Blitting to swap chain.
73 Shader* blitVertexShader;
74 Shader* blitFragmentShader;
75 ShaderProgram* blitShaderProgram;
76
77 OpenGLBufferAllocator* bufferAllocator;
78 OpenGLRenderPassAllocator* renderPassAllocator;
79 OpenGLRenderTargetAllocator* renderTargetAllocator;
80
81 OpenGLSampler* samplers[static_cast<uint32_t>(Sampler::Filter::COUNT) * static_cast<uint32_t>(Sampler::Clamping::COUNT)];
82
83 OptionalFeatures optionalFeatures;
84
85 static const unsigned int buffering = 3;
86 std::vector<Profiling::Event> finishedEvents;
87 unsigned int currentFrame = 0;
88 static const unsigned int maxQueries = buffering * 2 * 50;
89 GLuint queries[maxQueries];
90 std::vector<GLuint> freeQueries;
91 std::vector<OpenGLCommandBuffer::Timing> submittedTimings[buffering];
92
93 bool firstSubmission;
94 double submissionTimes[buffering];
95};
96
97} // namespace Video
Definition: Window.hpp:14
A buffer containing GPU accessible data.
Definition: Buffer.hpp:8
BufferUsage
How the buffer is going to be used.
Definition: Buffer.hpp:11
A buffer into which rendering commands are recorded.
Definition: CommandBuffer.hpp:24
A compute pipeline.
Definition: ComputePipeline.hpp:11
Binds together a vertex description with buffers.
Definition: GeometryBinding.hpp:8
IndexType
The type of values in the index buffer.
Definition: GeometryBinding.hpp:11
A graphics pipeline.
Definition: GraphicsPipeline.hpp:11
Low level renderer abstracting the underlaying graphics API (OpenGL or Vulkan).
Definition: LowLevelRenderer.hpp:27
OpenGL implementation of BufferAllocator.
Definition: OpenGLBufferAllocator.hpp:44
OpenGL implementation of RenderPassAllocator.
Definition: OpenGLRenderPassAllocator.hpp:8
OpenGL implementation of RenderTargetAllocator.
Definition: OpenGLRenderTargetAllocator.hpp:8
Low-level renderer implementing OpenGL.
Definition: OpenGLRenderer.hpp:23
OpenGLRenderer(Utility::Window *window)
Create new OpenGL renderer.
Definition: OpenGLRenderer.cpp:31
const OpenGLShaderProgram * GetBlitShaderProgram() const
Get the shader program used for blitting.
Definition: OpenGLRenderer.cpp:262
void FreeRenderTarget(Texture *renderTarget) final
Free a render target.
Definition: OpenGLRenderer.cpp:214
Shader * CreateShader(const ShaderSource &shaderSource, Shader::Type type) final
Create a shader.
Definition: OpenGLRenderer.cpp:190
CommandBuffer * CreateCommandBuffer() final
Create a command buffer.
Definition: OpenGLRenderer.cpp:104
Buffer * CreateBuffer(Buffer::BufferUsage bufferUsage, uint32_t size, const void *data=nullptr) final
Definition: OpenGLRenderer.cpp:174
VertexDescription * CreateVertexDescription(unsigned int attributeCount, const VertexDescription::Attribute *attributes, bool indexBuffer=false) final
Create a vertex description.
Definition: OpenGLRenderer.cpp:182
Texture * CreateRenderTarget(const glm::uvec2 &size, Texture::Format format) final
Create a render target.
Definition: OpenGLRenderer.cpp:210
void BeginFrame() final
Begin a new frame.
Definition: OpenGLRenderer.cpp:108
ShaderProgram * CreateShaderProgram(std::initializer_list< const Shader * > shaders) final
Create a shader program.
Definition: OpenGLRenderer.cpp:194
GraphicsPipeline * CreateGraphicsPipeline(const ShaderProgram *shaderProgram, const GraphicsPipeline::Configuration &configuration, const VertexDescription *vertexDescription=nullptr) final
Create a graphics pipeline.
Definition: OpenGLRenderer.cpp:218
void Present() final
Present the rendered image to the swap chain.
Definition: OpenGLRenderer.cpp:131
const std::vector< Profiling::Event > & GetTimeline() const final
Get profiling timeline.
Definition: OpenGLRenderer.cpp:254
Texture * CreateTexture(const glm::uvec2 size, Texture::Format format, int components, unsigned char *data) final
Create a texture.
Definition: OpenGLRenderer.cpp:198
ComputePipeline * CreateComputePipeline(const ShaderProgram *shaderProgram) final
Create a compute pipeline.
Definition: OpenGLRenderer.cpp:222
const OptionalFeatures & GetOptionalFeatures() const final
Get which optional features are supported.
Definition: OpenGLRenderer.cpp:258
Buffer * CreateTemporaryBuffer(Buffer::BufferUsage bufferUsage, uint32_t size, const void *data=nullptr) final
Definition: OpenGLRenderer.cpp:178
const Sampler * GetSampler(Sampler::Filter filter, Sampler::Clamping clamping) const final
Get a sampler.
Definition: OpenGLRenderer.cpp:203
GeometryBinding * CreateGeometryBinding(const VertexDescription *vertexDescription, Buffer *vertexBuffer, GeometryBinding::IndexType indexType=GeometryBinding::IndexType::NONE, const Buffer *indexBuffer=nullptr) final
Create a geometry binding.
Definition: OpenGLRenderer.cpp:186
GLuint GetFreeQuery()
Get a free query.
Definition: OpenGLRenderer.cpp:266
void Submit(CommandBuffer *commandBuffer) final
Submit a command buffer for execution.
Definition: OpenGLRenderer.cpp:114
void Wait() final
Wait for all rendering to finish.
Definition: OpenGLRenderer.cpp:226
unsigned char * ReadImage(Texture *texture) final
Read a render texture color image.
Definition: OpenGLRenderer.cpp:230
~OpenGLRenderer() final
Destructor.
Definition: OpenGLRenderer.cpp:88
OpenGL implementation of Sampler.
Definition: OpenGLSampler.hpp:10
OpenGL implementation of ShaderProgram.
Definition: OpenGLShaderProgram.hpp:14
Determines how a texture should be sampled.
Definition: Sampler.hpp:6
Filter
The interpolation to apply.
Definition: Sampler.hpp:9
@ COUNT
Number of entries in enum.
Clamping
How to handle sampling outside the texture dimensions.
Definition: Sampler.hpp:16
@ COUNT
Number of entries in enum.
Compiles and handles a shader. Shaders should be linked together into a ShaderProgram.
Definition: Shader.hpp:8
Type
The type of shader.
Definition: Shader.hpp:11
A shader program.
Definition: ShaderProgram.hpp:11
A texture.
Definition: Texture.hpp:8
Format
The format of the texture.
Definition: Texture.hpp:18
Describes how a vertex buffer is accessed by a shader.
Definition: VertexDescription.hpp:8
Definition: ResourceView.hpp:12
Definition: Editor.hpp:18
The configuration of the graphics pipeline.
Definition: GraphicsPipeline.hpp:14
Optional features.
Definition: LowLevelRenderer.hpp:30
Describes an attribute used in a vertex shader.
Definition: VertexDescription.hpp:18