-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathgl_program.cc
165 lines (135 loc) · 6.26 KB
/
gl_program.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright 2020 The TensorFlow Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "gl_program.h"
#include "macros.h"
#include "cleanup.h"
#include "tensorflow/core/lib/core/status.h"
namespace gl_utils {
Program::Program(GLuint program_handle) : program_handle_(program_handle) {}
Program::~Program() { glDeleteProgram(program_handle_); }
absl::Status Program::CompileShader(const std::string& shader_code,
const GLenum& shader_type,
GLuint* shader_idx) {
// Create an empty shader object.
TFG_RETURN_IF_EGL_ERROR(*shader_idx = glCreateShader(shader_type));
if (*shader_idx == 0)
return TFG_INTERNAL_ERROR("Error while creating the shader object.");
auto shader_cleanup =
MakeCleanup([shader_idx]() { glDeleteShader(*shader_idx); });
// Set the source code in the shader object.
auto shader_code_c_str = shader_code.c_str();
TFG_RETURN_IF_EGL_ERROR(
glShaderSource(*shader_idx, 1, &shader_code_c_str, nullptr));
// Compile the shader.
TFG_RETURN_IF_EGL_ERROR(glCompileShader(*shader_idx));
GLint compilation_status;
TFG_RETURN_IF_EGL_ERROR(
glGetShaderiv(*shader_idx, GL_COMPILE_STATUS, &compilation_status));
if (compilation_status != GL_TRUE) {
GLsizei log_length;
TFG_RETURN_IF_EGL_ERROR(
glGetShaderiv(*shader_idx, GL_INFO_LOG_LENGTH, &log_length));
std::vector<char> info_log(log_length + 1);
TFG_RETURN_IF_EGL_ERROR(
glGetShaderInfoLog(*shader_idx, log_length, nullptr, &info_log[0]));
TFG_RETURN_IF_EGL_ERROR(glDeleteShader(*shader_idx));
return TFG_INTERNAL_ERROR("Error while compiling the shader: " +
std::string(&info_log[0]));
}
shader_cleanup.release();
return absl::Status();
}
absl::Status Program::Create(
const std::vector<std::pair<std::string, GLenum>>& shaders,
std::unique_ptr<Program>* program) {
// Create an empty program object.
GLuint program_handle;
program_handle = glCreateProgram();
if (program_handle == 0)
return TFG_INTERNAL_ERROR("Error while creating the program object.");
auto program_cleanup =
MakeCleanup([program_handle]() { glDeleteProgram(program_handle); });
// Compile and attach the input shaders to the program.
std::vector<Cleanup<std::function<void()>>> shader_cleanups;
for (const auto& shader : shaders) {
GLuint shader_idx;
TF_RETURN_IF_ERROR(CompileShader(shader.first, shader.second, &shader_idx));
std::function<void()> compile_cleanup = [shader_idx]() {
glDeleteShader(shader_idx);
};
shader_cleanups.push_back(MakeCleanup(compile_cleanup));
TFG_RETURN_IF_EGL_ERROR(glAttachShader(program_handle, shader_idx));
std::function<void()> attach_cleanup = [program_handle, shader_idx]() {
glDetachShader(program_handle, shader_idx);
};
shader_cleanups.push_back(MakeCleanup(attach_cleanup));
}
// Link the program to the executable that will run on the programmable
// vertex/fragment processors.
TFG_RETURN_IF_EGL_ERROR(glLinkProgram(program_handle));
*program = std::unique_ptr<Program>(new Program(program_handle));
program_cleanup.release();
// The content of shader_cleanups needs cleanup and hence is not released; see
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDeleteProgram.xhtml.
return absl::Status();
}
absl::Status Program::Detach() const {
TFG_RETURN_IF_GL_ERROR(glUseProgram(0));
return absl::Status();
}
absl::Status Program::GetProgramResourceIndex(GLenum program_interface,
absl::string_view resource_name,
GLuint* resource_index) const {
TFG_RETURN_IF_EGL_ERROR(*resource_index = glGetProgramResourceIndex(
program_handle_, program_interface,
resource_name.data()));
return absl::Status();
}
absl::Status Program::GetProgramResourceiv(
GLenum program_interface, GLuint resource_index, int num_properties,
const GLenum* properties, int num_property_value, GLsizei* length,
GLint* property_value) const {
TFG_RETURN_IF_EGL_ERROR(glGetProgramResourceiv(
program_handle_, program_interface, resource_index, num_properties,
properties, num_property_value, length, property_value));
return absl::Status();
}
absl::Status Program::GetResourceProperty(const std::string& resource_name,
GLenum program_interface,
int num_properties,
const GLenum* properties,
int num_property_value,
GLint* property_value) {
if (num_property_value != num_properties)
return TFG_INTERNAL_ERROR("num_property_value != num_properties");
GLuint resource_index;
// Query the index of the named resource within the program.
TF_RETURN_IF_ERROR(GetProgramResourceIndex(program_interface, resource_name,
&resource_index));
// No resource is active under that name.
if (resource_index == GL_INVALID_INDEX)
return TFG_INTERNAL_ERROR("GL_INVALID_INDEX");
// Retrieve the value for the property.
GLsizei length;
TF_RETURN_IF_ERROR(GetProgramResourceiv(
program_interface, resource_index, num_properties, properties,
num_property_value, &length, property_value));
if (length != num_properties)
return TFG_INTERNAL_ERROR("length != num_properties: ", length,
" != ", num_properties);
return absl::Status();
}
absl::Status Program::Use() const {
TFG_RETURN_IF_EGL_ERROR(glUseProgram(program_handle_));
return absl::Status();
}
} // namespace gl_utils