1 // Copyright 2018 Google LLC
2 //
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 // https://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 #include "tools/render/sdl_util.h"
16
17 #include "absl/flags/flag.h"
18
19 #if !defined(NDEBUG)
20 #define DEFAULT_OPENGL_DEBUG_FLAG_VALUE true
21 #else
22 #define DEFAULT_OPENGL_DEBUG_FLAG_VALUE false
23 #endif // !defined(NDEBUG)
24
25 ABSL_FLAG(bool,
26 opengl_debug,
27 DEFAULT_OPENGL_DEBUG_FLAG_VALUE,
28 "Show OpenGL debug messages");
29
30 namespace quic_trace {
31 namespace render {
32
OpenGlContext(SDL_Window * window)33 OpenGlContext::OpenGlContext(SDL_Window* window) {
34 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
35 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
36 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
37 context_ = SDL_GL_CreateContext(window);
38 CHECK(context_ != nullptr) << "GL context is null: " << SDL_GetError();
39
40 GLenum err = glewInit();
41 if (err != GLEW_OK) {
42 LOG(FATAL) << "Failed to initalize GLEW: " << glewGetErrorString(err);
43 }
44
45 if (GLEW_KHR_debug && absl::GetFlag(FLAGS_opengl_debug)) {
46 glEnable(GL_DEBUG_OUTPUT);
47 glDebugMessageCallback(
48 +[](GLenum source, GLenum type, GLuint id, GLenum severity,
49 GLsizei length, const GLchar* message, const void* userParam) {
50 switch (severity) {
51 case GL_DEBUG_SEVERITY_HIGH:
52 LOG(ERROR) << "[GL]: " << std::string(message, length);
53 break;
54 case GL_DEBUG_SEVERITY_MEDIUM:
55 LOG(WARNING) << "[GL]: " << std::string(message, length);
56 break;
57 case GL_DEBUG_SEVERITY_LOW:
58 LOG(INFO) << "[GL]: " << std::string(message, length);
59 break;
60 case GL_DEBUG_SEVERITY_NOTIFICATION:
61 break;
62 }
63 },
64 nullptr);
65 }
66 }
67
Compile(const char * source)68 bool GlShader::Compile(const char* source) {
69 glShaderSource(shader_, 1, &source, nullptr);
70 glCompileShader(shader_);
71
72 GLint compile_status = GL_FALSE;
73 glGetShaderiv(shader_, GL_COMPILE_STATUS, &compile_status);
74 return compile_status;
75 }
76
GetCompileInfoLog()77 std::string GlShader::GetCompileInfoLog() {
78 GLint buffer_size = 0;
79 GLint actual_size = 0;
80
81 glGetShaderiv(shader_, GL_INFO_LOG_LENGTH, &buffer_size);
82 auto buffer = std::make_unique<char[]>(buffer_size);
83
84 glGetShaderInfoLog(shader_, buffer_size, &actual_size, buffer.get());
85 return std::string(buffer.get(), actual_size);
86 }
87
CompileOrDie(const char * source)88 void GlShader::CompileOrDie(const char* source) {
89 if (!Compile(source)) {
90 LOG(FATAL) << "Failed to compile a shader: " << GetCompileInfoLog();
91 }
92 }
93
94 } // namespace render
95 } // namespace quic_trace
96