2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <mv_private.h>
18 #include "mv_util_shader.h"
20 GLuint compile_shader_text(GLenum shaderType, const char *text)
25 shader = glCreateShader(shaderType);
26 glShaderSource(shader, 1, (const char **) &text, NULL);
27 glCompileShader(shader);
29 glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
34 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
35 lpBuf = (char *) malloc(len);
37 glGetShaderInfoLog(shader, len, &len, lpBuf);
38 LOGE("Error: problem compiling shader.");
39 LOGE("-----------------------------------");
41 LOGE("-----------------------------------");
51 GLuint link_shaders(GLuint vertShader, GLuint fragShader)
53 GLuint program = glCreateProgram();
56 glAttachShader(program, fragShader);
58 glAttachShader(program, vertShader);
60 glLinkProgram(program);
64 glGetProgramiv(program, GL_LINK_STATUS, &stat);
69 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len);
70 lpBuf = (char *) malloc(len);
72 glGetProgramInfoLog(program, len, &len, lpBuf);
73 LOGE("Error: problem linking shader.");
74 LOGE("-----------------------------------");
76 LOGE("-----------------------------------");
87 int generate_shader(shader_obj_t *sobj, char *str_vs, char *str_fs)
89 GLuint fs, vs, program;
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.");
98 program = link_shaders(vs, fs);
100 LOGE("Failed to link shaders.");
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");