Berserk
GLDefs.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_GLDEFS_HPP
29 #define BERSERK_GLDEFS_HPP
30 
31 #include <GL/glew.h>
32 
33 #include <core/io/Logger.hpp>
34 #include <core/math/TVecN.hpp>
35 #include <rhi/RHIDefs.hpp>
36 
37 #ifdef BERSERK_DEBUG
38  #define BRK_GL_CATCH_ERR() \
39  do { GLDefs::CatchErrors(); } while (false);
40 #else
41  #define BRK_GL_CATCH_ERR()
42 #endif
43 
45 
60 class GLDefs {
61 public:
62  static void CatchErrors() {
63  GLenum error;
64 
65  while ((error = glGetError()) != GL_NO_ERROR) {
66  BRK_ERROR("[GL] error desc: " << GetErrorDescription(error));
67  }
68  }
69 
70  static bool NeedClearBefore(RHIRenderTargetOption option) {
71  switch (option) {
74  return true;
75  default:
76  return false;
77  }
78  }
79 
80  static const char *GetErrorDescription(GLenum error) {
81  switch (error) {
82  case GL_INVALID_ENUM:
83  return BRK_TEXT("Invalid enum passed");
84  case GL_INVALID_VALUE:
85  return BRK_TEXT("Invalid value passed");
86  case GL_INVALID_OPERATION:
87  return BRK_TEXT("Set of state for a command is not legal for the parameters given to command");
88  case GL_OUT_OF_MEMORY:
89  return BRK_TEXT("Out of memory");
90  case GL_INVALID_FRAMEBUFFER_OPERATION:
91  return BRK_TEXT("Invalid framebuffer operation");
92  default:
93  return BRK_TEXT("Something else, refer to documentation");
94  }
95  }
96 
97  static GLenum GetBufferUsage(RHIBufferUsage bufferUsage) {
98  switch (bufferUsage) {
100  return GL_DYNAMIC_DRAW;
102  return GL_STATIC_DRAW;
103  default:
104  BRK_ERROR("Unsupported RHIBufferUsage");
105  return GL_NONE;
106  }
107  }
108 
109  static GLenum GetShaderType(RHIShaderType type) {
110  switch (type) {
112  return GL_VERTEX_SHADER;
114  return GL_FRAGMENT_SHADER;
115  default:
116  BRK_ERROR("Unsupported RHIShaderType");
117  return GL_NONE;
118  }
119  }
120 
121  static GLenum GetIndexType(RHIIndexType type) {
122  switch (type) {
124  return GL_UNSIGNED_INT;
126  return GL_UNSIGNED_SHORT;
127  default:
128  BRK_ERROR("Unsupported RHIIndexType");
129  return GL_NONE;
130  }
131  }
132 
133  static GLenum GetIndexSize(RHIIndexType type) {
134  switch (type) {
136  return sizeof(uint32);
138  return sizeof(uint16);
139  default:
140  BRK_ERROR("Unsupported RHIIndexType");
141  }
142  }
143 
144  static void GetVertexElementType(RHIVertexElementType type, GLenum &baseType, uint32 &count) {
145  switch (type) {
147  baseType = GL_FLOAT;
148  count = 1;
149  return;
150  }
152  baseType = GL_FLOAT;
153  count = 2;
154  return;
155  }
157  baseType = GL_FLOAT;
158  count = 3;
159  return;
160  }
162  baseType = GL_FLOAT;
163  count = 4;
164  return;
165  }
167  baseType = GL_INT;
168  count = 1;
169  return;
170  }
172  baseType = GL_INT;
173  count = 2;
174  return;
175  }
177  baseType = GL_INT;
178  count = 3;
179  return;
180  }
182  baseType = GL_INT;
183  count = 4;
184  return;
185  }
186  default:
187  BRK_ERROR("Unsupported RHIVertexElementType");
188  }
189  }
190 
192  switch (format) {
194  return GL_R8;
196  return GL_R8_SNORM;
198  return GL_R16;
200  return GL_R16_SNORM;
202  return GL_RG8;
204  return GL_RG8_SNORM;
206  return GL_RG16;
208  return GL_RG16_SNORM;
210  return GL_RGB8;
212  return GL_RGB8_SNORM;
214  return GL_RGB16_SNORM;
216  return GL_RGBA8;
218  return GL_RGBA8_SNORM;
220  return GL_RGBA16;
222  return GL_SRGB8;
224  return GL_SRGB8_ALPHA8;
226  return GL_R16F;
228  return GL_RG16F;
230  return GL_RGB16F;
232  return GL_RGBA16F;
234  return GL_R32F;
236  return GL_RG32F;
238  return GL_RGB32F;
240  return GL_RGBA32F;
242  return GL_DEPTH_COMPONENT32F;
244  return GL_DEPTH32F_STENCIL8;
246  return GL_DEPTH24_STENCIL8;
247  default:
248  BRK_ERROR("Unsupported RHITextureFormat");
249  return GL_NONE;
250  }
251  }
252 
253  static bool IsSuitableForDepthStencil(RHITextureFormat format, bool &depth, bool &stencil) {
254  switch (format) {
256  depth = true;
257  stencil = false;
258  return true;
261  depth = true;
262  stencil = true;
263  return true;
264  default:
265  depth = false;
266  stencil = false;
267  return false;
268  }
269  }
270 
272  switch (format) {
279  return GL_RED;
286  return GL_RG;
293  return GL_RGB;
300  return GL_RGBA;
302  return GL_DEPTH_COMPONENT;
305  return GL_DEPTH_STENCIL;
306  default:
307  BRK_ERROR("Unsupported RHITextureFormat");
308  return GL_NONE;
309  }
310  }
311 
312  static GLenum GetTextureDataType(RHITextureFormat format) {
313  switch (format) {
320  return GL_UNSIGNED_BYTE;
325  return GL_BYTE;
329  return GL_UNSIGNED_SHORT;
333  return GL_SHORT;
338  return GL_HALF_FLOAT;
344  return GL_FLOAT;
346  return GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
348  return GL_UNSIGNED_INT_24_8;
349  default:
350  BRK_ERROR("Unsupported RHITextureFormat");
351  return GL_NONE;
352  }
353  }
354 
356  auto id = static_cast<uint32>(face);
357 
359  BRK_ERROR("Face id out of the range");
360  return GL_NONE;
361  }
362 
363  static const GLenum targets[] = {
364  GL_TEXTURE_CUBE_MAP_POSITIVE_X,
365  GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
366  GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
367  GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
368  GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
369  GL_TEXTURE_CUBE_MAP_NEGATIVE_Z};
370 
371  return targets[id];
372  }
373 
375  switch (filter) {
377  return GL_NEAREST_MIPMAP_NEAREST;
379  return GL_LINEAR_MIPMAP_NEAREST;
381  return GL_NEAREST_MIPMAP_LINEAR;
383  return GL_LINEAR_MIPMAP_LINEAR;
385  return GL_NEAREST;
387  return GL_LINEAR;
388  default:
389  BRK_ERROR("Unsupported RHISamplerMinFilter");
390  return GL_NONE;
391  }
392  }
393 
395  switch (filter) {
397  return GL_NEAREST;
399  return GL_LINEAR;
400  default:
401  BRK_ERROR("Unsupported RHISamplerMagFilter");
402  return GL_NONE;
403  }
404  }
405 
407  switch (mode) {
409  return GL_REPEAT;
411  return GL_CLAMP_TO_BORDER;
413  return GL_CLAMP_TO_EDGE;
415  return GL_MIRRORED_REPEAT;
417  return GL_MIRROR_CLAMP_TO_EDGE;
418  default:
419  BRK_ERROR("Unsupported RHISamplerRepeatMode");
420  return GL_NONE;
421  }
422  }
423 
425  switch (color) {
427  return {0.0f, 0.0f, 0.0f, 1.0f};
429  return {1.0f, 1.0f, 1.0f, 1.0f};
430  default:
431  BRK_ERROR("Unsupported RHISamplerBorderColor");
432  return {};
433  }
434  }
435 
436  static GLenum GetPrimitivesType(RHIPrimitivesType type) {
437  switch (type) {
439  return GL_TRIANGLES;
441  return GL_LINES;
443  return GL_POINTS;
444  default:
445  BRK_ERROR("Unsupported RHIPrimitivesType");
446  return GL_NONE;
447  }
448  }
449 
450  static GLenum GetPolygonMode(RHIPolygonMode mode) {
451  switch (mode) {
453  return GL_FILL;
455  return GL_LINE;
457  return GL_POINT;
458  default:
459  BRK_ERROR("Unsupported RHIPolygonMode");
460  return GL_NONE;
461  }
462  }
463 
465  switch (mode) {
467  return GL_FRONT;
469  return GL_BACK;
471  return GL_NONE;
473  return GL_FRONT_AND_BACK;
474  default:
475  BRK_ERROR("Unsupported RHIPolygonCullMode");
476  return GL_NONE;
477  }
478  }
479 
480  static GLenum GetPolygonFrontFace(RHIPolygonFrontFace frontFace) {
481  switch (frontFace) {
483  return GL_CW;
485  return GL_CCW;
486  default:
487  BRK_ERROR("Unsupported RHIPolygonFrontFace");
488  return GL_NONE;
489  }
490  }
491 
492  static GLenum GetCompareFunc(RHICompareFunction function) {
493  switch (function) {
495  return GL_NEVER;
497  return GL_LESS;
499  return GL_EQUAL;
501  return GL_LEQUAL;
503  return GL_GREATER;
505  return GL_GEQUAL;
507  return GL_NOTEQUAL;
509  return GL_ALWAYS;
510  default:
511  BRK_ERROR("Unsupported RHICompareFunction");
512  return GL_NONE;
513  }
514  }
515 
516  static GLenum GetBlendFactor(RHIBlendFactor factor) {
517  switch (factor) {
519  return GL_ZERO;
520  case RHIBlendFactor::One:
521  return GL_ONE;
523  return GL_SRC_COLOR;
525  return GL_ONE_MINUS_SRC_COLOR;
527  return GL_DST_COLOR;
529  return GL_ONE_MINUS_DST_COLOR;
531  return GL_SRC_ALPHA;
533  return GL_ONE_MINUS_SRC_ALPHA;
535  return GL_DST_ALPHA;
537  return GL_ONE_MINUS_DST_ALPHA;
538  default:
539  BRK_ERROR("Unsupported RHIBlendFactor");
540  return GL_NONE;
541  }
542  }
543 
544  static GLenum GetBlendOperation(RHIBlendOperation operation) {
545  switch (operation) {
547  return GL_FUNC_ADD;
549  return GL_FUNC_SUBTRACT;
551  return GL_FUNC_REVERSE_SUBTRACT;
553  return GL_MIN;
555  return GL_MAX;
556  default:
557  BRK_ERROR("Unsupported RHIBlendOperation");
558  return GL_NONE;
559  }
560  }
561 
562  static RHIShaderDataType GetShaderDataParam(GLenum type) {
563  switch (type) {
564  case GL_FLOAT:
566  case GL_FLOAT_VEC2:
568  case GL_FLOAT_VEC3:
570  case GL_FLOAT_VEC4:
572  case GL_INT:
574  case GL_INT_VEC2:
576  case GL_INT_VEC3:
578  case GL_INT_VEC4:
580  case GL_UNSIGNED_INT:
582  case GL_UNSIGNED_INT_VEC2:
584  case GL_UNSIGNED_INT_VEC3:
586  case GL_UNSIGNED_INT_VEC4:
588  case GL_BOOL:
590  case GL_BOOL_VEC2:
592  case GL_BOOL_VEC3:
594  case GL_BOOL_VEC4:
596  case GL_FLOAT_MAT2:
598  case GL_FLOAT_MAT3:
600  case GL_FLOAT_MAT4:
602  default:
603  BRK_ERROR("Unsupported ShaderData");
605  }
606  }
607 
608  static bool IsMatrixType(GLenum type) {
609  switch (type) {
610  case GL_FLOAT_MAT2:
611  case GL_FLOAT_MAT3:
612  case GL_FLOAT_MAT4:
613  return true;
614  default:
615  return false;
616  }
617  }
618 
619  static RHIShaderParamType GetShaderParam(GLenum type) {
620  switch (type) {
621  case GL_SAMPLER_2D:
623  case GL_SAMPLER_2D_ARRAY:
625  case GL_SAMPLER_3D:
627  case GL_SAMPLER_CUBE:
629  default:
630  BRK_ERROR("Unsupported RHIShaderParamType");
632  }
633  }
634 
635  static RHIVertexElementType GetElementType(GLenum type) {
636  switch (type) {
637  case GL_FLOAT:
639  case GL_FLOAT_VEC2:
641  case GL_FLOAT_VEC3:
643  case GL_FLOAT_VEC4:
645  case GL_INT:
647  case GL_INT_VEC2:
649  case GL_INT_VEC3:
651  case GL_INT_VEC4:
653  default:
654  BRK_ERROR("Unsupported RHIVertexElementType");
656  }
657  }
658 
659  static int32 GetShaderDataSize(GLenum type) {
660  switch (type) {
661  case GL_FLOAT:
662  return sizeof(float) * 1;
663  case GL_FLOAT_VEC2:
664  return sizeof(float) * 2;
665  case GL_FLOAT_VEC3:
666  return sizeof(float) * 3;
667  case GL_FLOAT_VEC4:
668  return sizeof(float) * 4;
669  case GL_INT:
670  return sizeof(int32) * 1;
671  case GL_INT_VEC2:
672  return sizeof(int32) * 2;
673  case GL_INT_VEC3:
674  return sizeof(int32) * 3;
675  case GL_INT_VEC4:
676  return sizeof(int32) * 4;
677  case GL_UNSIGNED_INT:
678  return sizeof(uint32) * 1;
679  case GL_UNSIGNED_INT_VEC2:
680  return sizeof(uint32) * 2;
681  case GL_UNSIGNED_INT_VEC3:
682  return sizeof(uint32) * 3;
683  case GL_UNSIGNED_INT_VEC4:
684  return sizeof(uint32) * 4;
685  case GL_BOOL:
686  return sizeof(uint32) * 1;
687  case GL_BOOL_VEC2:
688  return sizeof(uint32) * 2;
689  case GL_BOOL_VEC3:
690  return sizeof(uint32) * 3;
691  case GL_BOOL_VEC4:
692  return sizeof(uint32) * 4;
693  case GL_FLOAT_MAT2:
694  return sizeof(float) * 2 * 2;
695  case GL_FLOAT_MAT3:
696  return sizeof(float) * 3 * 3;
697  case GL_FLOAT_MAT4:
698  return sizeof(float) * 4 * 4;
699  case GL_SAMPLER_2D:
700  case GL_SAMPLER_3D:
701  case GL_SAMPLER_CUBE:
702  default:
703  return 0;
704  }
705  }
706 
707  static GLenum GetStencilOp(RHIOperation operation) {
708  switch (operation) {
710  return GL_DECR;
712  return GL_INCR;
714  return GL_INVERT;
715  case RHIOperation::Keep:
716  return GL_KEEP;
718  return GL_REPLACE;
719  case RHIOperation::Zero:
720  return GL_ZERO;
721  default:
722  BRK_ERROR("Unsupported RHIOperation");
723  return GL_NONE;
724  }
725  }
726 };
727 
733 
734 #endif//BERSERK_GLDEFS_HPP
#define BRK_NS_END
Definition: Config.hpp:48
#define BRK_TEXT(text)
Definition: Config.hpp:53
std::int32_t int32
Definition: Typedefs.hpp:43
std::uint32_t uint32
Definition: Typedefs.hpp:44
std::uint16_t uint16
Definition: Typedefs.hpp:41
GL mapping of RHI definitions.
Definition: GLDefs.hpp:60
static GLint GetSamplerRepeatMode(RHISamplerRepeatMode mode)
Definition: GLDefs.hpp:406
static GLenum GetPolygonCullMode(RHIPolygonCullMode mode)
Definition: GLDefs.hpp:464
static GLenum GetIndexType(RHIIndexType type)
Definition: GLDefs.hpp:121
static GLint GetSamplerMagFilter(RHISamplerMagFilter filter)
Definition: GLDefs.hpp:394
static const char * GetErrorDescription(GLenum error)
Definition: GLDefs.hpp:80
static GLenum GetBufferUsage(RHIBufferUsage bufferUsage)
Definition: GLDefs.hpp:97
static GLenum GetShaderType(RHIShaderType type)
Definition: GLDefs.hpp:109
static RHIShaderParamType GetShaderParam(GLenum type)
Definition: GLDefs.hpp:619
static GLenum GetTextureDataBaseFormat(RHITextureFormat format)
Definition: GLDefs.hpp:271
static bool IsMatrixType(GLenum type)
Definition: GLDefs.hpp:608
static bool NeedClearBefore(RHIRenderTargetOption option)
Definition: GLDefs.hpp:70
static GLenum GetStencilOp(RHIOperation operation)
Definition: GLDefs.hpp:707
static GLenum GetCompareFunc(RHICompareFunction function)
Definition: GLDefs.hpp:492
static GLenum GetBlendFactor(RHIBlendFactor factor)
Definition: GLDefs.hpp:516
static GLint GetSamplerMinFilter(RHISamplerMinFilter filter)
Definition: GLDefs.hpp:374
static GLenum GetPolygonFrontFace(RHIPolygonFrontFace frontFace)
Definition: GLDefs.hpp:480
static GLenum GetIndexSize(RHIIndexType type)
Definition: GLDefs.hpp:133
static GLenum GetTextureDataType(RHITextureFormat format)
Definition: GLDefs.hpp:312
static GLenum GetTextureCubeFaceTarget(RHITextureCubemapFace face)
Definition: GLDefs.hpp:355
static GLenum GetPolygonMode(RHIPolygonMode mode)
Definition: GLDefs.hpp:450
static GLenum GetBlendOperation(RHIBlendOperation operation)
Definition: GLDefs.hpp:544
static RHIVertexElementType GetElementType(GLenum type)
Definition: GLDefs.hpp:635
static void CatchErrors()
Definition: GLDefs.hpp:62
static GLenum GetPrimitivesType(RHIPrimitivesType type)
Definition: GLDefs.hpp:436
static Vec4f GetBorderColor(RHISamplerBorderColor color)
Definition: GLDefs.hpp:424
static RHIShaderDataType GetShaderDataParam(GLenum type)
Definition: GLDefs.hpp:562
static GLenum GetTextureInternalFormat(RHITextureFormat format)
Definition: GLDefs.hpp:191
static void GetVertexElementType(RHIVertexElementType type, GLenum &baseType, uint32 &count)
Definition: GLDefs.hpp:144
static bool IsSuitableForDepthStencil(RHITextureFormat format, bool &depth, bool &stencil)
Definition: GLDefs.hpp:253
static int32 GetShaderDataSize(GLenum type)
Definition: GLDefs.hpp:659
Generic vector class for an N dimensional space base on type T.
Definition: TVecN.hpp:55
#define BRK_ERROR(message)
Definition: Logger.hpp:148
RHITextureCubemapFace
Definition: RHIDefs.hpp:177
RHIShaderParamType
Definition: RHIDefs.hpp:106
RHIShaderDataType
Definition: RHIDefs.hpp:83
RHITextureFormat
Definition: RHIDefs.hpp:135
RHIIndexType
Definition: RHIDefs.hpp:57
RHIBufferUsage
Definition: RHIDefs.hpp:50
RHIPolygonFrontFace
Definition: RHIDefs.hpp:241
RHIBlendOperation
Definition: RHIDefs.hpp:289
RHIRenderTargetOption
Definition: RHIDefs.hpp:302
RHIShaderType
Definition: RHIDefs.hpp:122
RHIOperation
Definition: RHIDefs.hpp:257
RHIPolygonMode
Definition: RHIDefs.hpp:227
RHISamplerBorderColor
Definition: RHIDefs.hpp:214
RHICompareFunction
Definition: RHIDefs.hpp:246
RHIVertexElementType
Definition: RHIDefs.hpp:71
RHIPolygonCullMode
Definition: RHIDefs.hpp:233
RHISamplerMagFilter
Definition: RHIDefs.hpp:186
RHISamplerMinFilter
Definition: RHIDefs.hpp:191
RHISamplerRepeatMode
Definition: RHIDefs.hpp:206
RHIBlendFactor
Definition: RHIDefs.hpp:266
RHIPrimitivesType
Definition: RHIDefs.hpp:220
Definition: GLDevice.cpp:46
static const uint32 MAX_TEXTURE_CUBE_FACES
Definition: RHIDefs.hpp:335