eda041b1d6d08e2e5e28a3b281a4e78980ee919a
[platform/core/graphics/tizenvg.git] / src / lib / gl_engine / tvgGlProgram.cpp
1 /*
2  * Copyright (c) 2020 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
3
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:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
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
20  * SOFTWARE.
21  */
22
23 #include "tvgGlProgram.h"
24
25 /************************************************************************/
26 /* Internal Class Implementation                                        */
27 /************************************************************************/
28
29 uint32_t GlProgram::mCurrentProgram = 0;
30
31
32 /************************************************************************/
33 /* External Class Implementation                                        */
34 /************************************************************************/
35
36 unique_ptr<GlProgram> GlProgram::gen(std::shared_ptr<GlShader> shader)
37 {
38     return make_unique<GlProgram>(shader);
39 }
40
41
42 GlProgram::GlProgram(std::shared_ptr<GlShader> shader)
43 {
44     linkProgram(shader);
45 }
46
47
48 GlProgram::~GlProgram()
49 {
50     if (mCurrentProgram == mProgramObj)
51     {
52         unload();
53     }
54     glDeleteProgram(mProgramObj);
55 }
56
57
58 void GlProgram::load()
59 {
60     if (mCurrentProgram == mProgramObj)
61     {
62         return;
63     }
64
65     mCurrentProgram = mProgramObj;
66     GL_CHECK(glUseProgram(mProgramObj));
67
68 }
69
70
71 void GlProgram::unload()
72 {
73     mCurrentProgram = 0;
74 }
75
76
77 int32_t GlProgram::getAttributeLocation(const char* name)
78 {
79     GL_CHECK(int32_t location = glGetAttribLocation(mCurrentProgram, name));
80     return location;
81 }
82
83
84 int32_t GlProgram::getUniformLocation(const char* name)
85 {
86     GL_CHECK(int32_t location = glGetUniformLocation(mCurrentProgram, name));
87     return location;
88 }
89
90
91 void GlProgram::setUniform1Value(int32_t location, int count, const int* values)
92 {
93     GL_CHECK(glUniform1iv(location, count, values));
94 }
95
96
97 void GlProgram::setUniform2Value(int32_t location, int count, const int* values)
98 {
99     GL_CHECK(glUniform2iv(location, count, values));
100 }
101
102
103 void GlProgram::setUniform3Value(int32_t location, int count, const int* values)
104 {
105     GL_CHECK(glUniform3iv(location, count, values));
106 }
107
108
109 void GlProgram::setUniform4Value(int32_t location, int count, const int* values)
110 {
111     GL_CHECK(glUniform4iv(location, count, values));
112 }
113
114
115 void GlProgram::setUniform1Value(int32_t location, int count, const float* values)
116 {
117     GL_CHECK(glUniform1fv(location, count, values));
118 }
119
120
121 void GlProgram::setUniform2Value(int32_t location, int count, const float* values)
122 {
123     GL_CHECK(glUniform2fv(location, count, values));
124 }
125
126
127 void GlProgram::setUniform3Value(int32_t location, int count, const float* values)
128 {
129     GL_CHECK(glUniform3fv(location, count, values));
130 }
131
132
133 void GlProgram::setUniform4Value(int32_t location, int count, const float* values)
134 {
135     GL_CHECK(glUniform4fv(location, count, values));
136 }
137
138 void GlProgram::setUniform4x4Value(int32_t location, int count, const float* values)
139 {
140     GL_CHECK(glUniformMatrix4fv(location, count, GL_FALSE, &values[0]));
141 }
142
143 void GlProgram::linkProgram(std::shared_ptr<GlShader> shader)
144 {
145     GLint linked;
146
147     // Create the program object
148     uint32_t progObj = glCreateProgram();
149     assert(progObj);
150
151     glAttachShader(progObj, shader->getVertexShader());
152     glAttachShader(progObj, shader->getFragmentShader());
153
154     // Link the program
155     glLinkProgram(progObj);
156
157     // Check the link status
158     glGetProgramiv(progObj, GL_LINK_STATUS, &linked);
159
160     if (!linked)
161     {
162         GLint infoLen = 0;
163         glGetProgramiv(progObj, GL_INFO_LOG_LENGTH, &infoLen);
164         if (infoLen > 0)
165         {
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);
169             free(infoLog);
170
171         }
172         glDeleteProgram(progObj);
173         progObj = 0;
174         assert(0);
175     }
176     mProgramObj = progObj;
177 }
178