2 * Copyright (c) 2020 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "tvgGlProgram.h"
25 /************************************************************************/
26 /* Internal Class Implementation */
27 /************************************************************************/
29 uint32_t GlProgram::mCurrentProgram = 0;
32 /************************************************************************/
33 /* External Class Implementation */
34 /************************************************************************/
36 unique_ptr<GlProgram> GlProgram::gen(std::shared_ptr<GlShader> shader)
38 return make_unique<GlProgram>(shader);
42 GlProgram::GlProgram(std::shared_ptr<GlShader> shader)
48 GlProgram::~GlProgram()
50 if (mCurrentProgram == mProgramObj)
54 glDeleteProgram(mProgramObj);
58 void GlProgram::load()
60 if (mCurrentProgram == mProgramObj)
65 mCurrentProgram = mProgramObj;
66 GL_CHECK(glUseProgram(mProgramObj));
71 void GlProgram::unload()
77 int32_t GlProgram::getAttributeLocation(const char* name)
79 GL_CHECK(int32_t location = glGetAttribLocation(mCurrentProgram, name));
84 int32_t GlProgram::getUniformLocation(const char* name)
86 GL_CHECK(int32_t location = glGetUniformLocation(mCurrentProgram, name));
91 void GlProgram::setUniform1Value(int32_t location, int count, const int* values)
93 GL_CHECK(glUniform1iv(location, count, values));
97 void GlProgram::setUniform2Value(int32_t location, int count, const int* values)
99 GL_CHECK(glUniform2iv(location, count, values));
103 void GlProgram::setUniform3Value(int32_t location, int count, const int* values)
105 GL_CHECK(glUniform3iv(location, count, values));
109 void GlProgram::setUniform4Value(int32_t location, int count, const int* values)
111 GL_CHECK(glUniform4iv(location, count, values));
115 void GlProgram::setUniform1Value(int32_t location, int count, const float* values)
117 GL_CHECK(glUniform1fv(location, count, values));
121 void GlProgram::setUniform2Value(int32_t location, int count, const float* values)
123 GL_CHECK(glUniform2fv(location, count, values));
127 void GlProgram::setUniform3Value(int32_t location, int count, const float* values)
129 GL_CHECK(glUniform3fv(location, count, values));
133 void GlProgram::setUniform4Value(int32_t location, int count, const float* values)
135 GL_CHECK(glUniform4fv(location, count, values));
138 void GlProgram::setUniform4x4Value(int32_t location, int count, const float* values)
140 GL_CHECK(glUniformMatrix4fv(location, count, GL_FALSE, &values[0]));
143 void GlProgram::linkProgram(std::shared_ptr<GlShader> shader)
147 // Create the program object
148 uint32_t progObj = glCreateProgram();
151 glAttachShader(progObj, shader->getVertexShader());
152 glAttachShader(progObj, shader->getFragmentShader());
155 glLinkProgram(progObj);
157 // Check the link status
158 glGetProgramiv(progObj, GL_LINK_STATUS, &linked);
163 glGetProgramiv(progObj, GL_INFO_LOG_LENGTH, &infoLen);
166 auto infoLog = static_cast<char*>(malloc(sizeof(char) * infoLen));
167 glGetProgramInfoLog(progObj, infoLen, NULL, infoLog);
168 TVGERR("GL_ENGINE", "Error linking shader: %s", infoLog);
172 glDeleteProgram(progObj);
176 mProgramObj = progObj;