Fix build break
[platform/core/api/mediavision.git] / test / testsuites / common / visualizer / src / mv_util_shader.cpp
1 /**
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <mv_private.h>
18 #include "mv_util_shader.h"
19
20 GLuint compile_shader_text(GLenum shaderType, const char *text)
21 {
22         GLuint shader;
23         GLint stat;
24
25         shader = glCreateShader(shaderType);
26         glShaderSource(shader, 1, (const char **) &text, NULL);
27         glCompileShader(shader);
28
29         glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
30         if (!stat) {
31                 GLsizei len;
32                 char *lpBuf;
33
34                 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
35                 lpBuf = (char *) malloc(len);
36
37                 glGetShaderInfoLog(shader, len, &len, lpBuf);
38                 LOGE("Error: problem compiling shader.");
39                 LOGE("-----------------------------------");
40                 LOGE("%s", lpBuf);
41                 LOGE("-----------------------------------");
42
43                 free(lpBuf);
44
45                 return 0;
46         }
47
48         return shader;
49 }
50
51 GLuint link_shaders(GLuint vertShader, GLuint fragShader)
52 {
53         GLuint program = glCreateProgram();
54
55         if (fragShader)
56                 glAttachShader(program, fragShader);
57         if (vertShader)
58                 glAttachShader(program, vertShader);
59
60         glLinkProgram(program);
61
62         {
63                 GLint stat;
64                 glGetProgramiv(program, GL_LINK_STATUS, &stat);
65                 if (!stat) {
66                         GLsizei len;
67                         char *lpBuf;
68
69                         glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len);
70                         lpBuf = (char *) malloc(len);
71
72                         glGetProgramInfoLog(program, len, &len, lpBuf);
73                         LOGE("Error: problem linking shader.");
74                         LOGE("-----------------------------------");
75                         LOGE("%s", lpBuf);
76                         LOGE("-----------------------------------");
77
78                         free(lpBuf);
79
80                         return 0;
81                 }
82         }
83
84         return program;
85 }
86
87 int generate_shader(shader_obj_t *sobj, char *str_vs, char *str_fs)
88 {
89         GLuint fs, vs, program;
90
91         vs = compile_shader_text(GL_VERTEX_SHADER, str_vs);
92         fs = compile_shader_text(GL_FRAGMENT_SHADER, str_fs);
93         if (vs == 0 || fs == 0) {
94                 LOGE("Failed to compile shader.");
95                 return -1;
96         }
97
98         program = link_shaders(vs, fs);
99         if (program == 0) {
100                 LOGE("Failed to link shaders.");
101                 return -1;
102         }
103
104         glDeleteShader(vs);
105         glDeleteShader(fs);
106
107         sobj->program = program;
108         sobj->loc_vtx = glGetAttribLocation(program, "a_Vertex");
109         sobj->loc_nrm = glGetAttribLocation(program, "a_Normal");
110         sobj->loc_clr = glGetAttribLocation(program, "a_Color");
111         sobj->loc_uv = glGetAttribLocation(program, "a_TexCoord");
112         sobj->loc_tex = glGetUniformLocation(program, "u_sampler");
113         sobj->loc_mtx = glGetUniformLocation(program, "u_PMVMatrix");
114         sobj->loc_mtx_nrm = glGetUniformLocation(program, "u_NrmMatrix");
115
116         return 0;
117 }