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 "gflags/gflags.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 DEFINE_bool(opengl_debug,
26             DEFAULT_OPENGL_DEBUG_FLAG_VALUE,
27             "Show OpenGL debug messages");
28 
29 namespace quic_trace {
30 namespace render {
31 
OpenGlContext(SDL_Window * window)32 OpenGlContext::OpenGlContext(SDL_Window* window) {
33   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
34   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
35   SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
36   context_ = SDL_GL_CreateContext(window);
37   CHECK(context_ != nullptr) << "GL context is null: " << SDL_GetError();
38 
39   GLenum err = glewInit();
40   if (err != GLEW_OK) {
41     LOG(FATAL) << "Failed to initalize GLEW: " << glewGetErrorString(err);
42   }
43 
44   if (GLEW_KHR_debug && FLAGS_opengl_debug) {
45     glEnable(GL_DEBUG_OUTPUT);
46     glDebugMessageCallback(
47         +[](GLenum source, GLenum type, GLuint id, GLenum severity,
48             GLsizei length, const GLchar* message, const void* userParam) {
49           switch (severity) {
50             case GL_DEBUG_SEVERITY_HIGH:
51               LOG(ERROR) << "[GL]: " << std::string(message, length);
52               break;
53             case GL_DEBUG_SEVERITY_MEDIUM:
54               LOG(WARNING) << "[GL]: " << std::string(message, length);
55               break;
56             case GL_DEBUG_SEVERITY_LOW:
57               LOG(INFO) << "[GL]: " << std::string(message, length);
58               break;
59             case GL_DEBUG_SEVERITY_NOTIFICATION:
60               VLOG(1) << "[GL]: " << std::string(message, length);
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 = absl::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