Diligent Engine API Reference: D:/GitHub/DiligentEngine/DiligentCore/Graphics/GraphicsEngineOpenGL/include/GLObjectWrapper.h Source File

Diligent Engine API Reference

GLObjectWrapper.h
1 /* Copyright 2015-2018 Egor Yusov
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12 *
13 * In no event and under no legal theory, whether in tort (including negligence),
14 * contract, or otherwise, unless required by applicable law (such as deliberate
15 * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16 * liable for any damages, including any direct, indirect, special, incidental,
17 * or consequential damages of any character arising as a result of this License or
18 * out of the use or inability to use the software (including but not limited to damages
19 * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20 * all other commercial damages or losses), even if such Contributor has been advised
21 * of the possibility of such damages.
22 */
23
24 #pragma once
25
26 #include "UniqueIdentifier.h"
27
28 namespace GLObjectWrappers
29 {
30
31 template<class CreateReleaseHelperType>
32 class GLObjWrapper
33 {
34 public:
35 GLObjWrapper(bool CreateObject, CreateReleaseHelperType CreateReleaseHelper = CreateReleaseHelperType()) :
36 m_uiHandle(0),
37 m_CreateReleaseHelper(CreateReleaseHelper)
38 {
39 if( CreateObject )
40 {
41 Create();
42 if(!m_uiHandle)
43 {
44 LOG_ERROR_AND_THROW("Failed to create ", m_CreateReleaseHelper.Name, " GL object");
45 }
46 }
47 }
48
49 ~GLObjWrapper()
50 {
51 Release();
52 }
53
54 GLObjWrapper(GLObjWrapper&& Wrapper) :
55 m_uiHandle(Wrapper.m_uiHandle),
56 m_CreateReleaseHelper( std::move( Wrapper.m_CreateReleaseHelper ) ),
57 m_UniqueId( std::move(Wrapper.m_UniqueId) )
58 {
59 Wrapper.m_uiHandle = 0;
60 }
61
62 GLObjWrapper& operator = (GLObjWrapper&& Wrapper)
63 {
64 Release();
65 m_uiHandle = Wrapper.m_uiHandle;
66 Wrapper.m_uiHandle = 0;
67 m_CreateReleaseHelper = std::move( Wrapper.m_CreateReleaseHelper );
68 m_UniqueId = std::move(Wrapper.m_UniqueId);
69 return *this;
70 }
71
72 void Create()
73 {
74 VERIFY(m_uiHandle == 0, "GL object is already initialized");
75 Release();
76 m_CreateReleaseHelper.Create(m_uiHandle);
77 VERIFY(m_uiHandle, "Failed to initialize GL object" );
78 }
79
80 void Release()
81 {
82 if( m_uiHandle )
83 {
84 m_CreateReleaseHelper.Release(m_uiHandle);
85 m_uiHandle = 0;
86 }
87 }
88
89 Diligent::UniqueIdentifier GetUniqueID()const
90 {
91 // This unique ID is used to unambiguously identify the object for
92 // tracking purposes.
93 // Niether GL handle nor pointer could be safely used for this purpose
94 // as both GL reuses released handles and the OS reuses released pointers
95 return m_UniqueId.GetID();
96 }
97
98 operator GLuint()const{return m_uiHandle;}
99
100 private:
101 GLObjWrapper(const GLObjWrapper&);
102 GLObjWrapper& operator = (const GLObjWrapper&);
103
104 GLuint m_uiHandle;
105 CreateReleaseHelperType m_CreateReleaseHelper;
106
107 // Have separate counter for every type of wrappers
108 Diligent::UniqueIdHelper<CreateReleaseHelperType> m_UniqueId;
109 };
110
111 class GLBufferObjCreateReleaseHelper
112 {
113 public:
114 explicit GLBufferObjCreateReleaseHelper(GLuint ExternalGLBufferHandle = 0) :
115 m_ExternalGLBufferHandle(ExternalGLBufferHandle)
116 {}
117
118 void Create(GLuint &BuffObj)
119 {
120 if (m_ExternalGLBufferHandle != 0)
121 BuffObj = m_ExternalGLBufferHandle; // Attach to external GL buffer handle
122 else
123 glGenBuffers(1, &BuffObj);
124 }
125
126 void Release(GLuint BuffObj)
127 {
128 if (m_ExternalGLBufferHandle != 0)
129 m_ExternalGLBufferHandle = 0; // Detach from external GL buffer. DO NOT delete the buffer
130 else
131 glDeleteBuffers(1, &BuffObj);
132 }
133 static const char *Name;
134
135 private:
136 GLuint m_ExternalGLBufferHandle;
137 };
138 typedef GLObjWrapper<GLBufferObjCreateReleaseHelper> GLBufferObj;
139
140
141 class GLProgramObjCreateReleaseHelper
142 {
143 public:
144 static void Create(GLuint &ProgObj){ ProgObj = glCreateProgram(); }
145 static void Release(GLuint ProgObj){ glDeleteProgram(ProgObj); }
146 static const char *Name;
147 };
148 typedef GLObjWrapper<GLProgramObjCreateReleaseHelper> GLProgramObj;
149
150
151 class GLShaderObjCreateReleaseHelper
152 {
153 public:
154 GLShaderObjCreateReleaseHelper(GLenum ShaderType) : m_ShaderType(ShaderType){}
155 void Create(GLuint &ShaderObj){ ShaderObj = glCreateShader(m_ShaderType); }
156 void Release(GLuint ShaderObj){ glDeleteShader(ShaderObj); }
157 static const char *Name;
158 private:
159 GLenum m_ShaderType;
160 };
161 typedef GLObjWrapper<GLShaderObjCreateReleaseHelper> GLShaderObj;
162
163
164 class GLPipelineObjCreateReleaseHelper
165 {
166 public:
167 void Create(GLuint &Pipeline) { glGenProgramPipelines(1, &Pipeline); }
168 void Release(GLuint Pipeline) { glDeleteProgramPipelines(1, &Pipeline); }
169 static const char *Name;
170 };
171 typedef GLObjWrapper<GLPipelineObjCreateReleaseHelper> GLPipelineObj;
172
173
174 class GLVAOCreateReleaseHelper
175 {
176 public:
177 void Create(GLuint &VAO) { glGenVertexArrays(1, &VAO); }
178 void Release(GLuint VAO) { glDeleteVertexArrays(1, &VAO); }
179 static const char *Name;
180 };
181 typedef GLObjWrapper<GLVAOCreateReleaseHelper> GLVertexArrayObj;
182
183
184 class GLTextureCreateReleaseHelper
185 {
186 public:
187 explicit GLTextureCreateReleaseHelper(GLuint ExternalGLTextureHandle = 0) :
188 m_ExternalGLTextureHandle(ExternalGLTextureHandle)
189 {}
190
191 void Create(GLuint &Tex)
192 {
193 if (m_ExternalGLTextureHandle != 0)
194 Tex = m_ExternalGLTextureHandle; // Attach to the external texture
195 else
196 glGenTextures(1, &Tex);
197 }
198
199 void Release(GLuint Tex)
200 {
201 if (m_ExternalGLTextureHandle != 0)
202 m_ExternalGLTextureHandle = 0; // Detach from the external texture. DO NOT delete it!
203 else
204 glDeleteTextures(1, &Tex);
205 }
206
207 static const char *Name;
208
209 private:
210 GLuint m_ExternalGLTextureHandle;
211 };
212 typedef GLObjWrapper<GLTextureCreateReleaseHelper> GLTextureObj;
213
214 class GLSamplerCreateReleaseHelper
215 {
216 public:
217 void Create(GLuint &Sampler) { glGenSamplers(1, &Sampler); }
218 void Release(GLuint Sampler) { glDeleteSamplers(1, &Sampler); }
219 static const char *Name;
220 };
221 typedef GLObjWrapper<GLSamplerCreateReleaseHelper> GLSamplerObj;
222
223
224 class GLFBOCreateReleaseHelper
225 {
226 public:
227 void Create(GLuint &FBO) { glGenFramebuffers(1, &FBO); }
228 void Release(GLuint FBO) { glDeleteFramebuffers(1, &FBO); }
229 static const char *Name;
230 };
231 typedef GLObjWrapper<GLFBOCreateReleaseHelper> GLFrameBufferObj;
232
233 }
GLObjectWrappers
Definition: GLObjectWrapper.h:28
std
Definition: AdvancedMath.h:316