summaryrefslogtreecommitdiff
path: root/src/gl/error.cc
blob: 20b2c74e5897a7973e56a7e4dc4abda6903c28d3 (plain)
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
// Copyright 2021 Benjamin Barenblat
//
// 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
//
//     https://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 "src/gl/error.h"

#include <new>
#include <stdexcept>
#include <thread>

#include "third_party/glew/include/GL/glew.h"
#include "third_party/abseil/absl/base/const_init.h"
#include "third_party/abseil/absl/synchronization/mutex.h"
#include "third_party/abseil/absl/types/optional.h"
#include "third_party/abseil/absl/meta/type_traits.h"

namespace gl_internal {

#ifndef GLPLANET_DISABLE_GL_THREAD_SAFETY_CHECKS

namespace {

ABSL_CONST_INIT absl::Mutex thread_id_mu(absl::kConstInit);
ABSL_CONST_INIT absl::optional<std::thread::id> thread_id;
static_assert(absl::is_trivially_destructible<decltype(thread_id)>::value);

}  // namespace

void RecordThreadImpl() {
  absl::MutexLock lock(&thread_id_mu);
  if (thread_id.has_value()) {
    throw std::logic_error("GL: gl_internal::RecordThread called twice");
  }
  thread_id = std::this_thread::get_id();
}

void CheckThreadSafetyImpl() {
  absl::MutexLock lock(&thread_id_mu);
  if (!thread_id.has_value()) {
    throw std::logic_error(
        "GL: gl_internal::CheckThreadSafety called before "
        "gl_internal::RecordThread");
  }
  if (std::this_thread::get_id() != *thread_id) {
    throw std::logic_error("GL: detected access from multiple threads");
  }
}

#endif  // !defined(GLPLANET_DISABLE_GL_THREAD_SAFETY_CHECKS)

void ErrorCheck() {
  switch (glGetError()) {
    case GL_NO_ERROR:
      return;
    case GL_INVALID_ENUM:
      throw std::invalid_argument(
          "GL: unacceptable value specified for enumerated argument");
    case GL_INVALID_VALUE:
      throw std::out_of_range("GL: numeric argument out of range");
    case GL_INVALID_OPERATION:
      throw std::logic_error(
          "GL: specified operation not allowed in current state");
    case GL_INVALID_FRAMEBUFFER_OPERATION:
      throw std::logic_error("GL: framebuffer object is not complete");
    case GL_OUT_OF_MEMORY:
      throw std::bad_alloc();
    case GL_STACK_UNDERFLOW:
      throw std::runtime_error("GL: stack underflow");
    case GL_STACK_OVERFLOW:
      throw std::runtime_error("GL: stack overflow");
  }
}

}  // namespace gl_internal