CSEngine
Loading...
Searching...
No Matches
CSE::GLProgramHandle Class Reference
Inheritance diagram for CSE::GLProgramHandle:
CSE::SResource CSE::SObject

Classes

struct  Element
 

Public Types

typedef std::map< std::string, GLProgramHandle::Element * > GLElementList
 

Public Member Functions

void Exterminate () override
 
void SetProgram (GLuint program)
 
ElementAttributeLocation (const char *location) const
 
ElementUniformLocation (const char *location) const
 
void SetAttribVec3 (const std::string &location, vec3 &value)
 
void SetAttribVec4 (const std::string &location, vec4 &value)
 
void SetUniformInt (const std::string &location, int value)
 
void SetUniformFloat (const std::string &location, float value)
 
void SetUniformMat4 (const std::string &location, mat4 &value)
 
void SetUniformMat3 (const std::string &location, mat3 &value)
 
void SetAttributesList (std::map< std::string, std::string > &vert, std::map< std::string, std::string > &frag)
 
void SetUniformsList (std::map< std::string, std::string > &vert, std::map< std::string, std::string > &frag)
 
GLElementList GetAttributesList () const
 
GLElementList GetUniformsList () const
 
void SaveShader (const std::string &path)
 
- Public Member Functions inherited from CSE::SResource
 SResource (bool isRegister)
 
 SResource (const SResource *resource, bool isRegister)
 
void SetName (std::string name)
 
void SetAbsoluteID (std::string id)
 
std::string GetName () const
 
std::string GetAbsoluteID () const
 
AssetMgr::AssetReferenceGetAssetReference (std::string hash="") const
 
void LinkResource (AssetMgr::AssetReference *asset)
 
void LinkResource (std::string name)
 
void SetHash (std::string &hash) override
 
- Public Member Functions inherited from CSE::SObject
 SObject (bool isRegister)
 
virtual void SetUndestroyable (bool enable)
 
virtual void Destroy ()
 
virtual void __FORCE_DESTROY__ ()
 
virtual std::string GenerateMeta ()
 
std::string GetHash () const
 

Public Attributes

GLuint Program
 
GLAttributeHandles Attributes
 
GLUniformHandles Uniforms
 
GLElementList AttributesList
 
GLElementList UniformsList
 

Protected Member Functions

void Init (const AssetMgr::AssetReference *asset) override
 

Additional Inherited Members

- Static Public Member Functions inherited from CSE::SResource
template<class T >
static T * Create (const std::string &name)
 
template<class T >
static T * Create (const AssetMgr::AssetReference *asset)
 
template<class T >
static T * Get (std::string name)
 
- Protected Attributes inherited from CSE::SObject
std::string m_hash
 

Detailed Description

Definition at line 50 of file GLProgramHandle.h.

Member Typedef Documentation

◆ GLElementList

typedef std::map<std::string, GLProgramHandle::Element*> CSE::GLProgramHandle::GLElementList

Definition at line 57 of file GLProgramHandle.h.

Constructor & Destructor Documentation

◆ GLProgramHandle()

GLProgramHandle::GLProgramHandle ( )

Definition at line 12 of file GLProgramHandle.cpp.

12 : Program(HANDLE_NULL) {
13 SetUndestroyable(true);
14}

Member Function Documentation

◆ AttributeLocation()

Element * CSE::GLProgramHandle::AttributeLocation ( const char *  location) const
inline

Definition at line 69 of file GLProgramHandle.h.

69 {
70 auto pair = AttributesList.find(location);
71 return pair == AttributesList.end() ? nullptr : pair->second;
72 }

◆ Exterminate()

void GLProgramHandle::Exterminate ( )
overridevirtual

Implements CSE::SObject.

Definition at line 18 of file GLProgramHandle.cpp.

18 {
19 glDeleteProgram(Program);
20
21 for (const auto& element_pair : AttributesList) {
22 Element* element = element_pair.second;
23 SAFE_DELETE(element);
24 }
25
26 for (const auto& element_pair : UniformsList) {
27 Element* element = element_pair.second;
28 SAFE_DELETE(element);
29 }
30}

◆ GetAttributesList()

GLProgramHandle::GLElementList GLProgramHandle::GetAttributesList ( ) const

Definition at line 163 of file GLProgramHandle.cpp.

163 {
164 return AttributesList;
165}

◆ GetUniformsList()

GLProgramHandle::GLElementList GLProgramHandle::GetUniformsList ( ) const

Definition at line 167 of file GLProgramHandle.cpp.

167 {
168 return UniformsList;
169}

◆ Init()

void GLProgramHandle::Init ( const AssetMgr::AssetReference asset)
overrideprotectedvirtual

Implements CSE::SResource.

Definition at line 187 of file GLProgramHandle.cpp.

187 {
188 //
189 std::string shader_combine = CSE::AssetMgr::LoadAssetFile(asset->name_path);
190
191 auto shader_combine_vector = split(shader_combine, ',');
192
193 if (shader_combine_vector.size() < 2) return;
194 m_vertShaderName = trim(shader_combine_vector.at(0));
195 m_fragShaderName = trim(shader_combine_vector.at(1));
196
197 //
198 auto resMgr = CORE->GetCore(ResMgr);
199 auto vert_asset = resMgr->GetAssetReference(m_vertShaderName);
200 auto frag_asset = resMgr->GetAssetReference(m_fragShaderName);
201
202 std::string vert_str = CSE::AssetMgr::LoadAssetFile(vert_asset->name_path);
203 std::string frag_str = CSE::AssetMgr::LoadAssetFile(frag_asset->name_path);
204
205 if (vert_str.empty() || frag_str.empty()) return;
206
207 if (ShaderUtil::CreateProgramHandle(vert_str.c_str(), frag_str.c_str(), this) == nullptr) {
208 return;
209 }
210}
static GLProgramHandle * CreateProgramHandle(const GLchar *vertexSource, const GLchar *fragmentSource, GLProgramHandle *handle=nullptr)
Creates a program handle.

◆ SaveShader()

void GLProgramHandle::SaveShader ( const std::string &  path)

Definition at line 172 of file GLProgramHandle.cpp.

172 {
173 return;
174}

◆ SetAttributesList()

void GLProgramHandle::SetAttributesList ( std::map< std::string, std::string > &  vert,
std::map< std::string, std::string > &  frag 
)

Definition at line 92 of file GLProgramHandle.cpp.

93 {
94 GLint count = 0;
95 GLsizei length = 0;
96 GLint size = 0;
97 GLenum type;
98
99 glGetProgramiv(Program, GL_ACTIVE_ATTRIBUTES, &count);
100 AttributesList.clear();
101
102 for (int i = 0; i < count; i++) {
103 GLchar name[32];
104 glGetActiveAttrib(Program, (GLuint) i, 32, &length, &size, &type, name);
105
106 std::string name_str = name;
107 std::string imp_name = getImplementName(vert, name_str);
108 bool isTargetChanged = false;
109
110 if (imp_name.empty()) {
111 imp_name = getImplementName(frag, name_str);
112 if (imp_name.empty()) continue;
113
114 isTargetChanged = true;
115 }
116
117 int id = glGetAttribLocation(Program, (isTargetChanged ? frag : vert).find(imp_name)->second.c_str());
118
119 auto element = new Element;
120 element->type = type;
121 element->id = id;
122
123 AttributesList[imp_name] = element;
124 }
125}

◆ SetAttribVec3()

void GLProgramHandle::SetAttribVec3 ( const std::string &  location,
vec3 value 
)

Definition at line 32 of file GLProgramHandle.cpp.

32 {
33 glUseProgram(Program);
34 auto iter = AttributesList.find(location);
35 if (iter != AttributesList.end()) {
36 glVertexAttrib3fv(iter->second->id, value.Pointer());
37 return;
38 }
39 glVertexAttrib3fv(glGetAttribLocation(Program, location.c_str()), value.Pointer());
40}

◆ SetAttribVec4()

void GLProgramHandle::SetAttribVec4 ( const std::string &  location,
vec4 value 
)

Definition at line 42 of file GLProgramHandle.cpp.

42 {
43 glUseProgram(Program);
44 auto iter = AttributesList.find(location);
45 if (iter != AttributesList.end()) {
46 glVertexAttrib4fv(iter->second->id, value.Pointer());
47 return;
48 }
49 glVertexAttrib4fv(glGetAttribLocation(Program, location.c_str()), value.Pointer());
50}

◆ SetProgram()

void CSE::GLProgramHandle::SetProgram ( GLuint  program)
inline

Definition at line 65 of file GLProgramHandle.h.

65 {
66 Program = program;
67 }

◆ SetUniformFloat()

void GLProgramHandle::SetUniformFloat ( const std::string &  location,
float  value 
)

Definition at line 62 of file GLProgramHandle.cpp.

62 {
63 glUseProgram(Program);
64 auto iter = UniformsList.find(location);
65 if (iter != UniformsList.end()) {
66 glUniform1f(iter->second->id, value);
67 return;
68 }
69 glUniform1f(glGetUniformLocation(Program, location.c_str()), value);
70}

◆ SetUniformInt()

void GLProgramHandle::SetUniformInt ( const std::string &  location,
int  value 
)

Definition at line 52 of file GLProgramHandle.cpp.

52 {
53 glUseProgram(Program);
54 auto iter = UniformsList.find(location);
55 if (iter != UniformsList.end()) {
56 glUniform1i(iter->second->id, value);
57 return;
58 }
59 glUniform1i(glGetUniformLocation(Program, location.c_str()), value);
60}

◆ SetUniformMat3()

void GLProgramHandle::SetUniformMat3 ( const std::string &  location,
mat3 value 
)

Definition at line 82 of file GLProgramHandle.cpp.

82 {
83 glUseProgram(Program);
84 auto iter = UniformsList.find(location);
85 if (iter != UniformsList.end()) {
86 glUniformMatrix3fv(iter->second->id, 1, 0, value.Pointer());
87 return;
88 }
89 glUniformMatrix3fv(glGetUniformLocation(Program, location.c_str()), 1, 0, value.Pointer());
90}

◆ SetUniformMat4()

void GLProgramHandle::SetUniformMat4 ( const std::string &  location,
mat4 value 
)

Definition at line 72 of file GLProgramHandle.cpp.

72 {
73 glUseProgram(Program);
74 auto iter = UniformsList.find(location);
75 if (iter != UniformsList.end()) {
76 glUniformMatrix4fv(iter->second->id, 1, 0, value.Pointer());
77 return;
78 }
79 glUniformMatrix4fv(glGetUniformLocation(Program, location.c_str()), 1, 0, value.Pointer());
80}

◆ SetUniformsList()

void GLProgramHandle::SetUniformsList ( std::map< std::string, std::string > &  vert,
std::map< std::string, std::string > &  frag 
)

Definition at line 128 of file GLProgramHandle.cpp.

128 {
129 GLint count = 0;
130 GLsizei length = 0;
131 GLint size = 0;
132 GLenum type;
133
134 glGetProgramiv(Program, GL_ACTIVE_UNIFORMS, &count);
135 UniformsList.clear();
136
137 for (int i = 0; i < count; i++) {
138 GLchar name[64];
139 glGetActiveUniform(Program, (GLuint) i, 64, &length, &size, &type, name);
140
141 std::string name_str = name;
142
143 std::string imp_name = getImplementName(vert, name_str);
144 bool isTargetChanged = false;
145
146 if (imp_name.empty()) {
147 imp_name = getImplementName(frag, name_str);
148 if (imp_name.empty()) continue;
149
150 isTargetChanged = true;
151 }
152
153 int id = glGetUniformLocation(Program, (isTargetChanged ? frag : vert).find(imp_name)->second.c_str());
154
155 auto element = new Element;
156 element->type = type;
157 element->id = id;
158
159 UniformsList[imp_name] = element;
160 }
161}

◆ UniformLocation()

Element * CSE::GLProgramHandle::UniformLocation ( const char *  location) const
inline

Definition at line 74 of file GLProgramHandle.h.

74 {
75 auto pair = UniformsList.find(location);
76 return pair == UniformsList.end() ? nullptr : pair->second;
77 }

Member Data Documentation

◆ Attributes

GLAttributeHandles CSE::GLProgramHandle::Attributes

Definition at line 109 of file GLProgramHandle.h.

◆ AttributesList

GLElementList CSE::GLProgramHandle::AttributesList

Definition at line 112 of file GLProgramHandle.h.

◆ Program

GLuint CSE::GLProgramHandle::Program

Definition at line 108 of file GLProgramHandle.h.

◆ Uniforms

GLUniformHandles CSE::GLProgramHandle::Uniforms

Definition at line 110 of file GLProgramHandle.h.

◆ UniformsList

GLElementList CSE::GLProgramHandle::UniformsList

Definition at line 113 of file GLProgramHandle.h.


The documentation for this class was generated from the following files: