CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
OpenGL.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
12
13#include "GLLoader.hpp"
14
16
18
20
21#define CE_GL_SAFE_CAST
22
23namespace CeresEngine {
24 enum class Format;
25}
26
27namespace CeresEngine {
28#define CE_GL_RENDER_API_OBJECT_FORWARD(N) \
29 class GL##N; \
30 using GL##N##Ptr = RC<GL##N>;
32#undef CE_GL_RENDER_API_OBJECT_FORWARD
33
34 template<typename ChildTypeName, typename ParentType = void> class GLObject {
35 public:
41#if defined(CE_GL_SAFE_CAST)
42 return RC<ChildTypeName>(static_cast<ChildTypeName*>(ptr.get()));
43#else
44 return safeCast<ChildTypeName>(ptr);
45#endif
46 }
47
52 static RC<ChildTypeName> safeCast(const RC<ParentType>& ptr) { return RC<ChildTypeName>(dynamic_cast<ChildTypeName*>(ptr.get())); }
53
58 static ChildTypeName& cast(ParentType& object) {
59#if defined(CE_GL_SAFE_CAST)
60 return static_cast<ChildTypeName&>(object);
61#else
62 return dynamic_cast<ChildTypeName&>(object);
63#endif
64 }
65
70 static const ChildTypeName& cast(const ParentType& object) {
71#if defined(CE_GL_SAFE_CAST)
72 return static_cast<const ChildTypeName&>(object);
73#else
74 return dynamic_cast<const ChildTypeName&>(object);
75#endif
76 }
77
82 static ChildTypeName* cast(ParentType* object) {
83#if defined(CE_GL_SAFE_CAST)
84 return static_cast<ChildTypeName*>(object);
85#else
86 return safeCast<ChildTypeName*>(object);
87#endif
88 }
89
94 static const ChildTypeName* cast(const ParentType* object) {
95#if defined(CE_GL_SAFE_CAST)
96 return static_cast<const ChildTypeName*>(object);
97#else
98 return safeCast<const ChildTypeName*>(object);
99#endif
100 }
101
106 static ChildTypeName* safeCast(ParentType* object) { return dynamic_cast<ChildTypeName*>(object); }
107
112 static const ChildTypeName* safeCast(const ParentType* object) { return dynamic_cast<const ChildTypeName*>(object); }
113 };
114
115 template<typename ChildTypeName> class GLObject<ChildTypeName, void> {};
116
117 template<typename ChildTypeName, typename ParentType = void> class GLDeviceObject : public GLObject<ChildTypeName, ParentType> {
118 protected:
121
122 protected:
125 explicit GLDeviceObject(GLDevicePtr device) : deviceGL(*device) {}
126 };
127
128 void GLThrowIfFailed(const char* info);
129
130 template<auto Alloc, auto Dealloc, bool BatchAlloc = true, bool BatchDealloc = BatchAlloc> struct GLObjectHandle final {
132
133 template<typename... Args> explicit GLObjectHandle(Args&&... args) {
134 glGetError();
135 if constexpr(BatchAlloc) {
136 (*Alloc)(1, &raw, args...);
137 } else {
138 raw = (*Alloc)(args...);
139 }
140 GLThrowIfFailed("handle allocation");
141 }
143 if constexpr(BatchDealloc) {
144 (*Dealloc)(1, &raw);
145 } else {
146 (*Dealloc)(raw);
147 }
148 }
149
150 explicit GLObjectHandle(std::nullptr_t) : raw(0) {}
151 explicit GLObjectHandle(bool, const GLuint raw) : raw(raw) {}
152
155
158
159 operator GLuint() const { // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
160 return raw;
161 }
162 };
163
165
167
175 GLenum GLMap(PrimitiveTopology primitiveTopology);
187
189
192 GLenum toImageCubeMap(UInt32 arrayLayer) noexcept;
193
196
197 // void convert(VkViewport& dst, const Viewport& src );
198 // void convert(VkRect2& dst, const Scissor& src );
199 // void convert(VkRect2& dst, const Viewport& src );
200
201 // Converts the boolean value into a VkBool322 value.
202 inline constexpr GLboolean GLBoolean(const bool value) noexcept { return (value ? GL_TRUE : GL_FALSE); }
203
204} // namespace CeresEngine
#define GL_TRUE
Definition GLLoader.hpp:801
#define glGetError
Definition GLLoader.hpp:2176
unsigned int GLenum
Definition GLLoader.hpp:723
unsigned char GLboolean
Definition GLLoader.hpp:724
unsigned int GLuint
Definition GLLoader.hpp:733
#define GL_FALSE
Definition GLLoader.hpp:800
#define CE_GL_RENDER_API_OBJECT_FORWARD(N)
Definition OpenGL.hpp:28
#define CE_RENDER_API_OBJECT_EACH(F)
A macro that invokes the function-macro F for every RenderAPI object type.
Definition Forward.hpp:18
A retain-release type of smart pointer.
Definition SmartPtr.hpp:132
Pointer get() const noexcept
Definition SmartPtr.hpp:244
Definition GLDevice.hpp:29
Definition OpenGL.hpp:117
GLDeviceObject(GLDevicePtr device)
Creates a new GLDeviceObject from the given device.
Definition OpenGL.hpp:125
GLDevice & deviceGL
The owning GLDevice instance.
Definition OpenGL.hpp:120
Definition OpenGL.hpp:34
static ChildTypeName * cast(ParentType *object)
Casts a object of type ParentType to a object of type ChildTypeName.
Definition OpenGL.hpp:82
static RC< ChildTypeName > safeCast(const RC< ParentType > &ptr)
Safely casts a RC containing a ParentType pointer to a pointer to a pointer to a ChildTypeName.
Definition OpenGL.hpp:52
static ChildTypeName * safeCast(ParentType *object)
Casts a object of type ParentType to a object of type ChildTypeName.
Definition OpenGL.hpp:106
static const ChildTypeName * cast(const ParentType *object)
Casts a object of type ParentType to a object of type ChildTypeName.
Definition OpenGL.hpp:94
static ChildTypeName & cast(ParentType &object)
Casts a object of type ParentType to a object of type ChildTypeName.
Definition OpenGL.hpp:58
static const ChildTypeName & cast(const ParentType &object)
Casts a object of type ParentType to a object of type ChildTypeName.
Definition OpenGL.hpp:70
static RC< ChildTypeName > cast(const RC< ParentType > &ptr)
Casts a RC containing a ParentType pointer to a pointer to a pointer to a ChildTypeName.
Definition OpenGL.hpp:40
static const ChildTypeName * safeCast(const ParentType *object)
Casts a object of type ParentType to a object of type ChildTypeName.
Definition OpenGL.hpp:112
Definition Application.hpp:19
GPUPolygonMode
Polygon filling modes enumeration.
Definition GPUGraphicsPipeline.hpp:143
void GLThrowIfFailed(const char *info)
GLenum toColorAttachment(UInt32 attachmentIndex) noexcept
Returns an enum in [GL_COLOR_ATTACHMENT0, ..., GL_COLOR_ATTACHMENT7].
GLenum GLMap(ShaderType shaderType)
GPUCullMode
Polygon culling modes enumeration.
Definition GPUGraphicsPipeline.hpp:153
GLenum GLMapOrZero(Format format)
constexpr GLboolean GLBoolean(const bool value) noexcept
Definition OpenGL.hpp:202
GPUSamplerAddressMode
Technique for resolving image coordinates that are outside of the range [0, 1].
Definition GPUSampler.hpp:29
GPUCompareOp
Compare operations enumeration.
Definition GPUGraphicsPipeline.hpp:37
GPUBlendOp
Blending operations enumeration.
Definition GPUGraphicsPipeline.hpp:84
Format
Definition TextureFormat.hpp:54
GLenum toImageCubeMap(UInt32 arrayLayer) noexcept
Returns an enum in [GL_TEXTURE_CUBE_MAP_POSITIVE_X, ..., GL_TEXTURE_CUBE_MAP_NEGATIVE_Z] for (arrayLa...
GPUBlendArithmetic
Blending arithmetic operations enumeration.
Definition GPUGraphicsPipeline.hpp:129
GPUStencilOp
Stencil operations enumeration.
Definition GPUGraphicsPipeline.hpp:63
GLenum GLMapInternalFormat(Format format)
GPUImageType
Definition GPUImage.hpp:36
GPUBufferType
Hardware buffer type enumeration.
Definition GPUBuffer.hpp:32
GPUSamplerFilter
Sampling filter enumeration.
Definition GPUSampler.hpp:49
void GLNotImplemented()
DataType
Renderer data types enumeration.
Definition GPUFormat.hpp:109
std::uint32_t UInt32
Definition DataTypes.hpp:23
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
bool GLHasExtension(GLuint extension)
GPULogicOp
Logical pixel operation enumeration.
Definition GPUGraphicsPipeline.hpp:166
Definition OpenGL.hpp:130
GLObjectHandle & operator=(const GLObjectHandle &)=delete
GLObjectHandle(bool, const GLuint raw)
Definition OpenGL.hpp:151
GLObjectHandle(std::nullptr_t)
Definition OpenGL.hpp:150
GLObjectHandle(GLObjectHandle &&) noexcept=default
GLuint raw
Definition OpenGL.hpp:131
~GLObjectHandle()
Definition OpenGL.hpp:142
GLObjectHandle(const GLObjectHandle &)=delete
GLObjectHandle(Args &&... args)
Definition OpenGL.hpp:133
Definition IndexType.hpp:20
Primitive topology enumeration.
Definition PrimitiveTopology.hpp:21
Shader type enumeration.
Definition ShaderType.hpp:59
Determines the data type used for the each component of a VertexElement.
Definition VertexDeclaration.hpp:103