Debian Codegraph @

Sources/nCine/Graphics/RHI/GL/GLAttribute.cpp

raw
1 #include "GLAttribute.h"
2 #include "GLDebug.h"
3 #include "../../../../Main.h"
4
5 #include <cstring>
6
7 namespace nCine::RHI::GL
8 {
9 GLAttribute::GLAttribute()
10 : _location(-1), _size(0), _type(GL_FLOAT)
11 {
12 _name[0] = '\0';
13 }
14
15 GLAttribute::GLAttribute(GLuint program, GLuint index)
16 : _location(-1), _size(0), _type(GL_FLOAT)
17 {
18 GLsizei length;
19 glGetActiveAttrib(program, index, MaxNameLength, &length, &_size, &_type, _name);
20 DEATH_ASSERT(length <= MaxNameLength);
21
22 if (!HasReservedPrefix()) {
23 _location = glGetAttribLocation(program, _name);
24 if (_location == -1) {
25 LOGW("Attribute location not found for attribute \"{}\" ({}) in shader program {}", _name, index, program);
26 }
27 }
28 GL_LOG_ERRORS();
29 }
30
31 GLAttribute::GLAttribute(GLuint program, const char* name, GLenum type)
32 : _location(-1), _size(1), _type(type)
33 {
34 std::size_t length = strnlen(name, MaxNameLength);
35 DEATH_ASSERT(length < MaxNameLength);
36 std::memcpy(_name, name, length);
37 _name[length] = '\0';
38
39 if (!HasReservedPrefix()) {
40 // Unlike GL introspection, reflection also lists attributes the driver optimized out,
41 // so a location of -1 is expected here and the attribute is simply never enabled
42 _location = glGetAttribLocation(program, _name);
43 }
44 GL_LOG_ERRORS();
45 }
46
47 GLenum GLAttribute::GetBasicType() const
48 {
49 switch (_type) {
50 case GL_FLOAT:
51 case GL_FLOAT_VEC2:
52 case GL_FLOAT_VEC3:
53 case GL_FLOAT_VEC4:
54 return GL_FLOAT;
55 case GL_INT:
56 case GL_INT_VEC2:
57 case GL_INT_VEC3:
58 case GL_INT_VEC4:
59 return GL_INT;
60 #if !defined(DEATH_TARGET_VITA) // vitaGL declares none of the bool / unsigned-int vector types
61 case GL_BOOL:
62 case GL_BOOL_VEC2:
63 case GL_BOOL_VEC3:
64 case GL_BOOL_VEC4:
65 return GL_BOOL;
66 #endif
67 case GL_UNSIGNED_INT:
68 #if !defined(DEATH_TARGET_VITA)
69 case GL_UNSIGNED_INT_VEC2:
70 case GL_UNSIGNED_INT_VEC3:
71 case GL_UNSIGNED_INT_VEC4:
72 #endif
73 return GL_UNSIGNED_INT;
74 default:
75 LOGW("No available case to handle type: {}", _type);
76 return _type;
77 }
78 }
79
80 std::int32_t GLAttribute::GetComponentCount() const
81 {
82 switch (_type) {
83 case GL_BYTE:
84 case GL_UNSIGNED_BYTE:
85 case GL_SHORT:
86 case GL_UNSIGNED_SHORT:
87 return 1;
88 case GL_FLOAT:
89 case GL_INT:
90 #if !defined(DEATH_TARGET_VITA)
91 case GL_BOOL:
92 #endif
93 case GL_UNSIGNED_INT:
94 return 1;
95 case GL_FLOAT_VEC2:
96 case GL_INT_VEC2:
97 #if !defined(DEATH_TARGET_VITA)
98 case GL_BOOL_VEC2:
99 case GL_UNSIGNED_INT_VEC2:
100 #endif
101 return 2;
102 case GL_FLOAT_VEC3:
103 case GL_INT_VEC3:
104 #if !defined(DEATH_TARGET_VITA)
105 case GL_BOOL_VEC3:
106 case GL_UNSIGNED_INT_VEC3:
107 #endif
108 return 3;
109 case GL_FLOAT_VEC4:
110 case GL_INT_VEC4:
111 #if !defined(DEATH_TARGET_VITA)
112 case GL_BOOL_VEC4:
113 case GL_UNSIGNED_INT_VEC4:
114 #endif
115 return 4;
116 default:
117 LOGW("No available case to handle type: {}", _type);
118 return 0;
119 }
120 }
121
122 bool GLAttribute::HasReservedPrefix() const
123 {
124 return (MaxNameLength >= 3 && _name[0] == 'g' && _name[1] == 'l' && _name[2] == '_');
125 }
126 }