MCPcopy Create free account
hub / github.com/OpenImageDebugger/OpenImageDebugger / create

Method create

src/visualization/shader.cpp:97–156  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

95}
96
97bool ShaderProgram::create(const std::string_view v_source,
98 const std::string_view f_source,
99 const TexelChannels texel_format,
100 const std::string& pixel_layout,
101 const std::vector<std::string>& uniforms) {
102 if (program_ != 0) {
103 // Check if the program needs to be recompiled
104 if (!is_shader_outdated(texel_format, uniforms, pixel_layout)) {
105 return true;
106 }
107 // Delete old program
108 gl_canvas_.glDeleteProgram(program_);
109 }
110
111 texel_format_ = texel_format;
112 pixel_layout_ = pixel_layout.substr(0, 4);
113 // Convert std::string_view to std::string for OpenGL API (requires
114 // null-terminated strings)
115 const auto vertex_shader =
116 compile(GL_VERTEX_SHADER, std::string(v_source).c_str());
117 const auto fragment_shader =
118 compile(GL_FRAGMENT_SHADER, std::string(f_source).c_str());
119
120 if (vertex_shader == 0 || fragment_shader == 0) [[unlikely]] {
121 return false;
122 }
123
124 program_ = gl_canvas_.glCreateProgram();
125 gl_canvas_.glAttachShader(program_, vertex_shader);
126 gl_canvas_.glAttachShader(program_, fragment_shader);
127 gl_canvas_.glLinkProgram(program_);
128
129 // Delete shaders. We don't need them anymore.
130 gl_canvas_.glDeleteShader(vertex_shader);
131 gl_canvas_.glDeleteShader(fragment_shader);
132
133 // Check for link errors
134 auto linked = GLint{};
135 gl_canvas_.glGetProgramiv(program_, GL_LINK_STATUS, &linked);
136 if (!linked) [[unlikely]] {
137 GLint length;
138 gl_canvas_.glGetProgramiv(program_, GL_INFO_LOG_LENGTH, &length);
139 auto log = std::string(length, ' ');
140 gl_canvas_.glGetProgramInfoLog(program_, length, &length, &log[0]);
141 std::cerr << "[Error] Failed to link shader program:" << std::endl
142 << log << std::endl;
143 gl_canvas_.glDeleteProgram(program_);
144 program_ = 0;
145 return false;
146 }
147
148 // Get uniform locations
149 for (const auto& name : uniforms) {
150 const auto loc =
151 gl_canvas_.glGetUniformLocation(program_, name.c_str());
152 uniforms_[name] = loc;
153 }
154

Callers 3

initializeMethod · 0.45
create_shader_programMethod · 0.45
initializeMethod · 0.45

Calls 8

glDeleteProgramMethod · 0.80
glCreateProgramMethod · 0.80
glAttachShaderMethod · 0.80
glLinkProgramMethod · 0.80
glDeleteShaderMethod · 0.80
glGetProgramivMethod · 0.80
glGetProgramInfoLogMethod · 0.80
glGetUniformLocationMethod · 0.80

Tested by

no test coverage detected