1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "graphic/render_program.h"
17 #include <GLES3/gl3.h>
18 #include "effect_log.h"
19 
20 namespace OHOS {
21 namespace Media {
22 namespace Effect {
RenderProgram(RenderContext * context)23 RenderProgram::RenderProgram(RenderContext *context) : program_(0), context_(context) {}
24 
SetUniform(const std::string & name,float value)25 void RenderProgram::SetUniform(const std::string &name, float value)
26 {
27     CHECK_AND_RETURN_LOG(program_ != 0, "RenderProgram SetUniform failed!, name=%{public}s, program=%{public}d",
28         name.c_str(), program_);
29     GLint location = glGetUniformLocation(program_, name.c_str());
30     CHECK_AND_RETURN_LOG(location != -1, "glGetUniformLocation failed!, name=%{public}s", name.c_str());
31     glUniform1f(location, value);
32 }
33 
SetUniform(const std::string & name,int value)34 void RenderProgram::SetUniform(const std::string &name, int value)
35 {
36     CHECK_AND_RETURN_LOG(program_ != 0, "RenderProgram SetUniform failed!, name=%{public}s, program=%{public}d",
37         name.c_str(), program_);
38     GLint location = glGetUniformLocation(program_, name.c_str());
39     CHECK_AND_RETURN_LOG(location != -1, "glGetUniformLocation failed!, name=%{public}s", name.c_str());
40     glUniform1i(location, value);
41 }
42 
SetUniform(const std::string & name,unsigned int value)43 void RenderProgram::SetUniform(const std::string &name, unsigned int value)
44 {
45     CHECK_AND_RETURN_LOG(program_ != 0, "RenderProgram SetUniform failed!, name=%{public}s, program=%{public}d",
46         name.c_str(), program_);
47     GLint location = glGetUniformLocation(program_, name.c_str());
48     CHECK_AND_RETURN_LOG(location != -1, "glGetUniformLocation failed!, name=%{public}s", name.c_str());
49     glUniform1ui(location, value);
50 }
51 
SetUniform(const std::string & name,const void * value)52 void RenderProgram::SetUniform(const std::string &name, const void *value)
53 {
54     CHECK_AND_RETURN_LOG(program_ != 0, "RenderProgram SetUniform failed!, name=%{public}s, program=%{public}d",
55         name.c_str(), program_);
56     GLint location = glGetUniformLocation(program_, name.c_str());
57     CHECK_AND_RETURN_LOG(location != -1, "glGetUniformLocation failed!, name=%{public}s", name.c_str());
58     glUniformMatrix4fv(location, 1, GL_FALSE, reinterpret_cast<const GLfloat *>(value));
59 }
60 
Bind()61 void RenderProgram::Bind()
62 {
63     glUseProgram(program_);
64 }
65 
Unbind()66 void RenderProgram::Unbind()
67 {
68     glUseProgram(INVALID_PROGRAM_NAME);
69 }
70 
GetName()71 unsigned int RenderProgram::GetName()
72 {
73     return program_;
74 }
75 
GetAttributeLocation(const std::string & name)76 int RenderProgram::GetAttributeLocation(const std::string &name)
77 {
78     int location = glGetAttribLocation(program_, name.c_str());
79     return location;
80 }
81 
GetUniformLocation(const std::string & name)82 int RenderProgram::GetUniformLocation(const std::string &name)
83 {
84     int location = glGetUniformLocation(program_, name.c_str());
85     return location;
86 }
87 } // namespace Effect
88 } // namespace Media
89 } // namespace OHOS