Diligent Engine API Reference
GLTypeConversions.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 namespace Diligent
27 {
28 
29 static const GLenum PrimTopologyToGLTopologyMap[] =
30 {
31  0, //PRIMITIVE_TOPOLOGY_UNDEFINED = 0
32  GL_TRIANGLES, //PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
33  GL_TRIANGLE_STRIP, //PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
34  GL_POINTS, //PRIMITIVE_TOPOLOGY_POINT_LIST
35  GL_LINES //PRIMITIVE_TOPOLOGY_LINE_LIST
36 };
37 
38 inline GLenum PrimitiveTopologyToGLTopology(PRIMITIVE_TOPOLOGY PrimTopology)
39 {
40  VERIFY_EXPR(PrimTopology < _countof(PrimTopologyToGLTopologyMap));
41  auto GLTopology = PrimTopologyToGLTopologyMap[PrimTopology];
42 #ifdef _DEBUG
43  switch(PrimTopology)
44  {
45  case PRIMITIVE_TOPOLOGY_UNDEFINED: VERIFY_EXPR(GLTopology == 0); break;
46  case PRIMITIVE_TOPOLOGY_TRIANGLE_LIST: VERIFY_EXPR(GLTopology == GL_TRIANGLES); break;
47  case PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP: VERIFY_EXPR(GLTopology == GL_TRIANGLE_STRIP); break;
48  case PRIMITIVE_TOPOLOGY_POINT_LIST: VERIFY_EXPR(GLTopology == GL_POINTS); break;
49  case PRIMITIVE_TOPOLOGY_LINE_LIST: VERIFY_EXPR(GLTopology == GL_LINES); break;
50  default: UNEXPECTED("Unexpected primitive topology");
51  }
52 #endif
53  return GLTopology;
54 }
55 
56 static const GLenum TypeToGLTypeMap[] =
57 {
58  0, //VT_UNDEFINED = 0
59  GL_BYTE, //VT_INT8
60  GL_SHORT, //VT_INT16
61  GL_INT, //VT_INT32
62  GL_UNSIGNED_BYTE, //VT_UINT8
63  GL_UNSIGNED_SHORT, //VT_UINT16
64  GL_UNSIGNED_INT, //VT_UINT32
65  0, //VT_FLOAT16
66  GL_FLOAT //VT_FLOAT32
67 };
68 
69 inline GLenum TypeToGLType(VALUE_TYPE Value)
70 {
71  VERIFY_EXPR(Value < _countof(TypeToGLTypeMap));
72  auto GLType = TypeToGLTypeMap[Value];
73 #ifdef _DEBUG
74  switch(Value)
75  {
76  case VT_INT8: VERIFY_EXPR(GLType == GL_BYTE); break;
77  case VT_INT16: VERIFY_EXPR(GLType == GL_SHORT); break;
78  case VT_INT32: VERIFY_EXPR(GLType == GL_INT); break;
79  case VT_UINT8: VERIFY_EXPR(GLType == GL_UNSIGNED_BYTE); break;
80  case VT_UINT16: VERIFY_EXPR(GLType == GL_UNSIGNED_SHORT); break;
81  case VT_UINT32: VERIFY_EXPR(GLType == GL_UNSIGNED_INT); break;
82  case VT_FLOAT32: VERIFY_EXPR(GLType == GL_FLOAT); break;
83  default: UNEXPECTED("Unexpected value type");
84  }
85 #endif
86  return GLType;
87 }
88 
89 inline GLenum UsageToGLUsage(USAGE Usage)
90 {
91  // http://www.informit.com/articles/article.aspx?p=2033340&seqNum=2
92  switch(Usage)
93  {
94  case USAGE_STATIC: return GL_STATIC_DRAW;
95  case USAGE_DEFAULT: return GL_DYNAMIC_DRAW;
96  case USAGE_DYNAMIC: return GL_STREAM_DRAW;
97  case USAGE_CPU_ACCESSIBLE: return GL_DYNAMIC_READ;
98  default: UNEXPECTED( "Unknow usage" ); return 0;
99  }
100 }
101 
102 inline void FilterTypeToGLFilterType(FILTER_TYPE Filter, GLenum &GLFilter, Bool &bIsAnisotropic, Bool &bIsComparison)
103 {
104  switch(Filter)
105  {
106  case FILTER_TYPE_UNKNOWN:
107  UNEXPECTED( "Unspecified filter type" );
108  bIsAnisotropic = false;
109  bIsComparison = false;
110  GLFilter = GL_NEAREST;
111  break;
112 
113  case FILTER_TYPE_POINT:
114  bIsAnisotropic = false;
115  bIsComparison = false;
116  GLFilter = GL_NEAREST;
117  break;
118 
119  case FILTER_TYPE_LINEAR:
120  bIsAnisotropic = false;
121  bIsComparison = false;
122  GLFilter = GL_LINEAR;
123  break;
124 
126  bIsAnisotropic = true;
127  bIsComparison = false;
128  GLFilter = GL_LINEAR;
129  break;
130 
132  bIsAnisotropic = false;
133  bIsComparison = true;
134  GLFilter = GL_NEAREST;
135  break;
136 
138  bIsAnisotropic = false;
139  bIsComparison = true;
140  GLFilter = GL_LINEAR;
141  break;
142 
144  bIsAnisotropic = true;
145  bIsComparison = true;
146  GLFilter = GL_LINEAR;
147  break;
148 
149  default:
150  bIsAnisotropic = false;
151  bIsComparison = false;
152  UNEXPECTED( "Unknown filter type" );
153  GLFilter = GL_NEAREST;
154  break;
155  }
156 }
157 
158 GLenum TexFormatToGLInternalTexFormat(TEXTURE_FORMAT TexFormat, Uint32 BindFlags = 0);
159 TEXTURE_FORMAT GLInternalTexFormatToTexFormat(GLenum GlFormat);
160 GLenum CorrectGLTexFormat(GLenum GLTexFormat, Uint32 BindFlags);
161 
162 inline GLenum TexAddressModeToGLAddressMode(TEXTURE_ADDRESS_MODE Mode)
163 {
164  switch(Mode)
165  {
166  case TEXTURE_ADDRESS_UNKNOWN: UNEXPECTED( "Texture address mode is not specified" ); return GL_CLAMP_TO_EDGE;
167  case TEXTURE_ADDRESS_WRAP: return GL_REPEAT;
168  case TEXTURE_ADDRESS_MIRROR: return GL_MIRRORED_REPEAT;
169  case TEXTURE_ADDRESS_CLAMP: return GL_CLAMP_TO_EDGE;
170  case TEXTURE_ADDRESS_BORDER: return GL_CLAMP_TO_BORDER;
171  case TEXTURE_ADDRESS_MIRROR_ONCE: return GL_MIRROR_CLAMP_TO_EDGE; // only available with OpenGL 4.4
172  // This mode seems to be different from D3D11_TEXTURE_ADDRESS_MIRROR_ONCE
173  // The texture coord is clamped to the [-1, 1] range, but mirrors the
174  // negative direction with the positive. Basically, it acts as
175  // GL_CLAMP_TO_EDGE except that it takes the absolute value of the texture
176  // coordinates before clamping.
177  default: UNEXPECTED( "Unknown texture address mode" ); return GL_CLAMP_TO_EDGE;
178  }
179 }
180 
181 inline GLenum CompareFuncToGLCompareFunc(COMPARISON_FUNCTION Func)
182 {
183  switch(Func)
184  {
185  case COMPARISON_FUNC_UNKNOWN: UNEXPECTED( "Comparison function is not specified" ); return GL_ALWAYS;
186  case COMPARISON_FUNC_NEVER: return GL_NEVER;
187  case COMPARISON_FUNC_LESS: return GL_LESS;
188  case COMPARISON_FUNC_EQUAL: return GL_EQUAL;
189  case COMPARISON_FUNC_LESS_EQUAL: return GL_LEQUAL;
190  case COMPARISON_FUNC_GREATER: return GL_GREATER;
191  case COMPARISON_FUNC_NOT_EQUAL: return GL_NOTEQUAL;
192  case COMPARISON_FUNC_GREATER_EQUAL: return GL_GEQUAL;
193  case COMPARISON_FUNC_ALWAYS: return GL_ALWAYS;
194  default: UNEXPECTED( "Unknown comparison func" ); return GL_ALWAYS;
195  }
196 }
197 
198 struct NativePixelAttribs
199 {
200  GLenum PixelFormat;
201  GLenum DataType;
202  Bool IsCompressed;
203  explicit NativePixelAttribs(GLenum _PixelFormat = 0, GLenum _DataType = 0, Bool _IsCompressed = False) :
204  PixelFormat(_PixelFormat),
205  DataType(_DataType),
206  IsCompressed(_IsCompressed)
207  {}
208 };
209 
210 inline Uint32 GetNumPixelFormatComponents(GLenum Format)
211 {
212  switch(Format)
213  {
214  case GL_RGBA:
215  case GL_RGBA_INTEGER:
216  return 4;
217 
218  case GL_RGB:
219  case GL_RGB_INTEGER:
220  return 3;
221 
222  case GL_RG:
223  case GL_RG_INTEGER:
224  return 2;
225 
226  case GL_RED:
227  case GL_RED_INTEGER:
228  case GL_DEPTH_COMPONENT:
229  case GL_DEPTH_STENCIL:
230  return 1;
231 
232  default: UNEXPECTED( "Unknonw pixel format" ); return 0;
233  };
234 }
235 
236 inline Uint32 GetPixelTypeSize(GLenum Type)
237 {
238  switch(Type)
239  {
240  case GL_FLOAT: return sizeof(GLfloat);
241 
242  case GL_UNSIGNED_INT_10_10_10_2:
243  case GL_UNSIGNED_INT_2_10_10_10_REV:
244  case GL_UNSIGNED_INT_10F_11F_11F_REV:
245  case GL_UNSIGNED_INT_24_8:
246  case GL_UNSIGNED_INT_5_9_9_9_REV:
247  case GL_UNSIGNED_INT: return sizeof(GLuint);
248 
249  case GL_INT: return sizeof(GLint);
250  case GL_HALF_FLOAT: return sizeof(GLhalf);
251 
252  case GL_UNSIGNED_SHORT_5_6_5:
253  case GL_UNSIGNED_SHORT_5_6_5_REV:
254  case GL_UNSIGNED_SHORT_5_5_5_1:
255  case GL_UNSIGNED_SHORT_1_5_5_5_REV:
256  case GL_UNSIGNED_SHORT: return sizeof(GLushort);
257 
258  case GL_SHORT: return sizeof(GLshort);
259  case GL_UNSIGNED_BYTE: return sizeof(GLubyte);
260  case GL_BYTE: return sizeof(GLbyte);
261 
262  case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:return sizeof(GLfloat) + sizeof(GLuint);
263 
264  default: UNEXPECTED( "Unknonw pixel type" ); return 0;
265  }
266 }
267 
268 NativePixelAttribs GetNativePixelTransferAttribs(TEXTURE_FORMAT TexFormat);
269 GLenum AccessFlags2GLAccess( Uint32 UAVAccessFlags );
270 GLenum TypeToGLTexFormat( VALUE_TYPE ValType, Uint32 NumComponents, Bool bIsNormalized );
271 GLenum StencilOp2GlStencilOp( STENCIL_OP StencilOp );
272 GLenum BlendFactor2GLBlend( BLEND_FACTOR bf );
273 GLenum BlendOperation2GLBlendOp( BLEND_OPERATION BlendOp );
274 
275 }
Interpret the vertex data as a list of points. D3D counterpart: D3D_PRIMITIVE_TOPOLOGY_POINTLIST. OpenGL counterpart: GL_POINTS.
Definition: DeviceContext.h:71
Comparison passes if the source data is less than or equal to the destination data. Direct3D counterpart: D3D11_COMPARISON_LESS_EQUAL/D3D12_COMPARISON_FUNC_LESS_EQUAL. OpenGL counterpart: GL_LEQUAL.
Definition: GraphicsTypes.h:866
BLEND_OPERATION
Blending operation.
Definition: BlendState.h:130
TEXTURE_FORMAT
Texture formats.
Definition: GraphicsTypes.h:244
Unsigned 8-bit integer.
Definition: GraphicsTypes.h:45
Signed 8-bit integer.
Definition: GraphicsTypes.h:42
Comparison always passes. Direct3D counterpart: D3D11_COMPARISON_ALWAYS/D3D12_COMPARISON_FUNC_ALWAY...
Definition: GraphicsTypes.h:882
PRIMITIVE_TOPOLOGY
Input primitive topology.
Definition: DeviceContext.h:56
STENCIL_OP
Stencil operation.
Definition: DepthStencilState.h:42
Comparison passes if the source data is equal to the destination data. Direct3D counterpart: D3D11_C...
Definition: GraphicsTypes.h:862
Texture coordinates outside the range [0.0, 1.0] are set to the texture color at 0.0 or 1.0, respectively. Direct3D Counterpart: D3D11_TEXTURE_ADDRESS_CLAMP/D3D12_TEXTURE_ADDRESS_MODE_CLAMP. OpenGL counterpart: GL_CLAMP_TO_EDGE.
Definition: GraphicsTypes.h:821
Comparison never passes. Direct3D counterpart: D3D11_COMPARISON_NEVER/D3D12_COMPARISON_FUNC_NEVER. OpenGL counterpart: GL_NEVER.
Definition: GraphicsTypes.h:854
Comparison-anisotropic filtering.
Definition: GraphicsTypes.h:788
Unsigned 16-bit integer.
Definition: GraphicsTypes.h:46
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Comparison-point filtering.
Definition: GraphicsTypes.h:786
Comparison passes if the source data is greater than the destination data. Direct3D counterpart: 3D1...
Definition: GraphicsTypes.h:870
TEXTURE_ADDRESS_MODE
Texture address mode.
Definition: GraphicsTypes.h:805
Undefined topology.
Definition: DeviceContext.h:59
Signed 32-bit integer.
Definition: GraphicsTypes.h:44
Comparison-linear filtering.
Definition: GraphicsTypes.h:787
Comparison passes if the source data is less than the destination data. Direct3D counterpart: D3D11_...
Definition: GraphicsTypes.h:858
Interpret the vertex data as a list of lines. D3D counterpart: D3D_PRIMITIVE_TOPOLOGY_LINELIST. OpenGL counterpart: GL_LINES.
Definition: DeviceContext.h:75
Tile the texture at every integer junction. Direct3D Counterpart: D3D11_TEXTURE_ADDRESS_WRAP/D3D12_...
Definition: GraphicsTypes.h:812
Unknown filter type.
Definition: GraphicsTypes.h:782
Comparison passes if the source data is not equal to the destination data. Direct3D counterpart: D3D...
Definition: GraphicsTypes.h:874
A resource that requires read and write access by the GPU and can also be occasionally written by the...
Definition: GraphicsTypes.h:95
Signed 16-bit integer.
Definition: GraphicsTypes.h:43
Comparison passes if the source data is greater than or equal to the destination data. Direct3D counterpart: D3D11_COMPARISON_GREATER_EQUAL/D3D12_COMPARISON_FUNC_GREATER_EQUAL. OpenGL counterpart: GL_GEQUAL.
Definition: GraphicsTypes.h:878
A resource that can only be read by the GPU. It cannot be written by the GPU, and cannot be accessed ...
Definition: GraphicsTypes.h:90
VALUE_TYPE
Value type.
Definition: GraphicsTypes.h:39
Texture coordinates outside the range [0.0, 1.0] are set to the border color specified specified in S...
Definition: GraphicsTypes.h:826
Unknown comparison function.
Definition: GraphicsTypes.h:850
A resource that facilitates transferring data from GPU to CPU. D3D11 Counterpart: D3D11_USAGE_STAGI...
Definition: GraphicsTypes.h:103
Point filtering.
Definition: GraphicsTypes.h:783
USAGE
Resource usage.
Definition: GraphicsTypes.h:84
Unknown mode.
Definition: GraphicsTypes.h:808
Full-precision 32-bit floating point.
Definition: GraphicsTypes.h:49
FILTER_TYPE
Filter type.
Definition: GraphicsTypes.h:780
Interpret the vertex data as a triangle strip. D3D counterpart: D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP...
Definition: DeviceContext.h:67
Unsigned 32-bit integer.
Definition: GraphicsTypes.h:47
BLEND_FACTOR
Blend factors.
Definition: BlendState.h:43
Interpret the vertex data as a list of triangles. D3D counterpart: D3D_PRIMITIVE_TOPOLOGY_TRIANGLELI...
Definition: DeviceContext.h:63
COMPARISON_FUNCTION
Comparison function.
Definition: GraphicsTypes.h:847
Flip the texture at every integer junction. Direct3D Counterpart: D3D11_TEXTURE_ADDRESS_MIRROR/D3D1...
Definition: GraphicsTypes.h:816
A resource that can be read by the GPU and written at least once per frame by the CPU...
Definition: GraphicsTypes.h:99
Anisotropic filtering.
Definition: GraphicsTypes.h:785
Linear filtering.
Definition: GraphicsTypes.h:784
Similar to TEXTURE_ADDRESS_MIRROR and TEXTURE_ADDRESS_CLAMP. Takes the absolute value of the texture ...
Definition: GraphicsTypes.h:833