4#include "GLProgramHandle.h"
7#include "Render/ShaderUtil.h"
8#include "../Manager/EngineCore.h"
12GLProgramHandle::GLProgramHandle() : Program(HANDLE_NULL) {
13 SetUndestroyable(
true);
16GLProgramHandle::~GLProgramHandle() =
default;
18void GLProgramHandle::Exterminate() {
19 glDeleteProgram(Program);
21 for (
const auto& element_pair : AttributesList) {
22 Element* element = element_pair.second;
26 for (
const auto& element_pair : UniformsList) {
27 Element* element = element_pair.second;
32void GLProgramHandle::SetAttribVec3(
const std::string& location,
vec3& value) {
33 glUseProgram(Program);
34 auto iter = AttributesList.find(location);
35 if (iter != AttributesList.end()) {
36 glVertexAttrib3fv(iter->second->id, value.Pointer());
39 glVertexAttrib3fv(glGetAttribLocation(Program, location.c_str()), value.Pointer());
42void GLProgramHandle::SetAttribVec4(
const std::string& location,
vec4& value) {
43 glUseProgram(Program);
44 auto iter = AttributesList.find(location);
45 if (iter != AttributesList.end()) {
46 glVertexAttrib4fv(iter->second->id, value.Pointer());
49 glVertexAttrib4fv(glGetAttribLocation(Program, location.c_str()), value.Pointer());
52void GLProgramHandle::SetUniformInt(
const std::string& location,
int value) {
53 glUseProgram(Program);
54 auto iter = UniformsList.find(location);
55 if (iter != UniformsList.end()) {
56 glUniform1i(iter->second->id, value);
59 glUniform1i(glGetUniformLocation(Program, location.c_str()), value);
62void GLProgramHandle::SetUniformFloat(
const std::string& location,
float value) {
63 glUseProgram(Program);
64 auto iter = UniformsList.find(location);
65 if (iter != UniformsList.end()) {
66 glUniform1f(iter->second->id, value);
69 glUniform1f(glGetUniformLocation(Program, location.c_str()), value);
72void GLProgramHandle::SetUniformMat4(
const std::string& location,
mat4& value) {
73 glUseProgram(Program);
74 auto iter = UniformsList.find(location);
75 if (iter != UniformsList.end()) {
76 glUniformMatrix4fv(iter->second->id, 1, 0, value.Pointer());
79 glUniformMatrix4fv(glGetUniformLocation(Program, location.c_str()), 1, 0, value.Pointer());
82void GLProgramHandle::SetUniformMat3(
const std::string& location,
mat3& value) {
83 glUseProgram(Program);
84 auto iter = UniformsList.find(location);
85 if (iter != UniformsList.end()) {
86 glUniformMatrix3fv(iter->second->id, 1, 0, value.Pointer());
89 glUniformMatrix3fv(glGetUniformLocation(Program, location.c_str()), 1, 0, value.Pointer());
92void GLProgramHandle::SetAttributesList(std::map<std::string, std::string>& vert,
93 std::map<std::string, std::string>& frag) {
99 glGetProgramiv(Program, GL_ACTIVE_ATTRIBUTES, &count);
100 AttributesList.clear();
102 for (
int i = 0; i < count; i++) {
104 glGetActiveAttrib(Program, (GLuint) i, 32, &length, &size, &type, name);
106 std::string name_str = name;
107 std::string imp_name = getImplementName(vert, name_str);
108 bool isTargetChanged =
false;
110 if (imp_name.empty()) {
111 imp_name = getImplementName(frag, name_str);
112 if (imp_name.empty())
continue;
114 isTargetChanged =
true;
117 int id = glGetAttribLocation(Program, (isTargetChanged ? frag : vert).find(imp_name)->second.c_str());
119 auto element =
new Element;
120 element->type = type;
123 AttributesList[imp_name] = element;
128GLProgramHandle::SetUniformsList(std::map<std::string, std::string>& vert, std::map<std::string, std::string>& frag) {
134 glGetProgramiv(Program, GL_ACTIVE_UNIFORMS, &count);
135 UniformsList.clear();
137 for (
int i = 0; i < count; i++) {
139 glGetActiveUniform(Program, (GLuint) i, 64, &length, &size, &type, name);
141 std::string name_str = name;
143 std::string imp_name = getImplementName(vert, name_str);
144 bool isTargetChanged =
false;
146 if (imp_name.empty()) {
147 imp_name = getImplementName(frag, name_str);
148 if (imp_name.empty())
continue;
150 isTargetChanged =
true;
153 int id = glGetUniformLocation(Program, (isTargetChanged ? frag : vert).find(imp_name)->second.c_str());
155 auto element =
new Element;
156 element->type = type;
159 UniformsList[imp_name] = element;
163GLProgramHandle::GLElementList GLProgramHandle::GetAttributesList()
const {
164 return AttributesList;
167GLProgramHandle::GLElementList GLProgramHandle::GetUniformsList()
const {
172void GLProgramHandle::SaveShader(
const std::string& path) {
176std::string GLProgramHandle::getImplementName(std::map<std::string, std::string>& list,
const std::string& name) {
178 for (
auto it = list.begin(); it != list.end(); it++) {
179 if (name.find(it->second) != std::string::npos) {
189 std::string shader_combine = CSE::AssetMgr::LoadAssetFile(asset->name_path);
191 auto shader_combine_vector = split(shader_combine,
',');
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));
198 auto resMgr = CORE->GetCore(
ResMgr);
199 auto vert_asset = resMgr->GetAssetReference(m_vertShaderName);
200 auto frag_asset = resMgr->GetAssetReference(m_fragShaderName);
202 std::string vert_str = CSE::AssetMgr::LoadAssetFile(vert_asset->name_path);
203 std::string frag_str = CSE::AssetMgr::LoadAssetFile(frag_asset->name_path);
205 if (vert_str.empty() || frag_str.empty())
return;
static GLProgramHandle * CreateProgramHandle(const GLchar *vertexSource, const GLchar *fragmentSource, GLProgramHandle *handle=nullptr)
Creates a program handle.