~vladh/peony

peony/src/util.cpp -rw-r--r-- 7.6 KiB
7fc9922bVlad-Stefan Harbuz changes shaders namespace into class a month ago
                                                                                
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
  Peony Game Engine
  Copyright (C) 2020 Vlad-Stefan Harbuz <vlad@vladh.net>
  All rights reserved.
*/

#include "logs.hpp"
#include "util.hpp"


const char* util::stringify_glenum(GLenum thing) {
  switch (thing) {
    case 0:
      return "(none)";
    case GL_BYTE:
      return "GLbyte";
    case GL_UNSIGNED_BYTE:
      return "GLubyte";
    case GL_SHORT:
      return "GLshort";
    case GL_UNSIGNED_SHORT:
      return "GLushort";
    case GL_INT:
      return "GLint";
    case GL_UNSIGNED_INT:
      return "GLuint";
    case GL_HALF_FLOAT:
      return "GLhalf";
    case GL_FLOAT:
      return "GLfloat";
    case GL_DOUBLE:
      return "GLdouble";
    case GL_RGB8:
      return "GL_RGB8";
    case GL_RGBA8:
      return "GL_RGBA8";
    case GL_SRGB8:
      return "GL_SRGB8";
    case GL_UNSIGNED_INT_8_8_8_8:
      return "GL_UNSIGNED_INT_8_8_8_8";
    case GL_UNSIGNED_INT_8_8_8_8_REV:
      return "GL_UNSIGNED_INT_8_8_8_8_REV";
    case GL_DEPTH_COMPONENT:
      return "GL_DEPTH_COMPONENT";
    case GL_DEPTH_STENCIL:
      return "GL_DEPTH_STENCIL";
    case GL_RED:
      return "GL_RED";
    case GL_RGB:
      return "GL_RGB";
    case GL_RGBA:
      return "GL_RGBA";
    default:
      logs::warning("Unknown GLenum %d", thing);
      return "Unknown GLenum";
  }
}


GLenum util::get_texture_format_from_n_components(int32 n_components) {
  if (n_components == 1) {
    return GL_RED;
  } else if (n_components == 3) {
    return GL_BGR;
  } else if (n_components == 4) {
    return GL_BGRA;
  } else {
    logs::fatal("Don't know what to do with n_components = %d", n_components);
    return 0;
  }
}


real64 util::random(real64 min, real64 max) {
  uint32 r = rand();
  real64 r_normalized = (real64)r / (real64)RAND_MAX;
  return min + ((r_normalized) * (max - min));
}


v3 util::aiVector3D_to_glm(aiVector3D *vec) {
  return v3(vec->x, vec->y, vec->z);
}


quat util::aiQuaternion_to_glm(aiQuaternion *rotation) {
  return quat(rotation->w, rotation->x, rotation->y, rotation->z);
}


m4 util::aimatrix4x4_to_glm(aiMatrix4x4 *from) {
  m4 to;

  to[0][0] = (GLfloat)from->a1;
  to[0][1] = (GLfloat)from->b1;
  to[0][2] = (GLfloat)from->c1;
  to[0][3] = (GLfloat)from->d1;

  to[1][0] = (GLfloat)from->a2;
  to[1][1] = (GLfloat)from->b2;
  to[1][2] = (GLfloat)from->c2;
  to[1][3] = (GLfloat)from->d2;

  to[2][0] = (GLfloat)from->a3;
  to[2][1] = (GLfloat)from->b3;
  to[2][2] = (GLfloat)from->c3;
  to[2][3] = (GLfloat)from->d3;

  to[3][0] = (GLfloat)from->a4;
  to[3][1] = (GLfloat)from->b4;
  to[3][2] = (GLfloat)from->c4;
  to[3][3] = (GLfloat)from->d4;

  return to;
}


void util::print_texture_internalformat_info(GLenum internal_format) {
  if (!GLAD_GL_ARB_internalformat_query) {
    logs::warning(
      "Not printing texture_internalformat as this feature is not supported "
      "on this system."
    );
    return;
  }

  if (!GLAD_GL_ARB_internalformat_query2) {
    logs::warning(
      "Printing texture_internalformat, but some information may be missing, "
      "as internalformat_query2 is not supported on this system."
    );
  }

  GLint preferred_format;
  GLint optimal_image_format;
  GLint optimal_image_type;

  glGetInternalformativ(
    GL_TEXTURE_2D, internal_format, GL_INTERNALFORMAT_PREFERRED, 1, &preferred_format
  );
  glGetInternalformativ(
    GL_TEXTURE_2D, internal_format, GL_TEXTURE_IMAGE_FORMAT, 1, &optimal_image_format
  );
  glGetInternalformativ(
    GL_TEXTURE_2D, internal_format, GL_TEXTURE_IMAGE_TYPE, 1, &optimal_image_type
  );

  logs::info("internal format: %s", stringify_glenum(internal_format));
  logs::info("preferred format: %s", stringify_glenum(preferred_format));
  logs::info("optimal image format: %s", stringify_glenum(optimal_image_format));
  logs::info("optimal image type: %s", stringify_glenum(optimal_image_type));
  logs::print_newline();
}


void APIENTRY util::debug_message_callback(
  GLenum source, GLenum type, unsigned int id, GLenum severity, GLsizei length,
  const char *message, const void *userParam
) {
  // Ignore insignificant error/warning codes
  if (
    // Framebuffer detailed info: The driver allocated storage for
    // renderbuffer 1.
    id == 131169 ||
    // Program/shader state performance warning: Vertex shader in program 19
    // is being recompiled based on GL state.
    id == 131218 ||
    // Buffer detailed info: Buffer object 1522 (bound to
    // GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB (0), and GL_ARRAY_BUFFER_ARB,
    // usage hint is GL_DYNAMIC_DRAW) will use VIDEO memory as the source for
    // buffer object operations.
    id == 131185 ||
    // Texture state usage warning: The texture object (0) bound to texture
    // image unit 4 does not have a defined base level and cannot be used for
    // texture mapping.
    id == 131204 ||
    // Pixel-path performance warning: Pixel transfer is synchronized with 3D rendering.
    id == 131154
  ) {
    return;
  }

  logs::warning("Debug message (%d): %s", id, message);

  switch (source) {
    case GL_DEBUG_SOURCE_API:
      logs::warning("Source: API");
      break;
    case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
      logs::warning("Source: Window System");
      break;
    case GL_DEBUG_SOURCE_SHADER_COMPILER:
      logs::warning("Source: Shader Compiler");
      break;
    case GL_DEBUG_SOURCE_THIRD_PARTY:
      logs::warning("Source: Third Party");
      break;
    case GL_DEBUG_SOURCE_APPLICATION:
      logs::warning("Source: Application");
      break;
    case GL_DEBUG_SOURCE_OTHER:
      logs::warning("Source: Other");
      break;
  }

  switch (type) {
    case GL_DEBUG_TYPE_ERROR:
      logs::warning("Type: Error");
      break;
    case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
      logs::warning("Type: Deprecated Behaviour");
      break;
    case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
      logs::warning("Type: Undefined Behaviour");
      break;
    case GL_DEBUG_TYPE_PORTABILITY:
      logs::warning("Type: Portability");
      break;
    case GL_DEBUG_TYPE_PERFORMANCE:
      logs::warning("Type: Performance");
      break;
    case GL_DEBUG_TYPE_MARKER:
      logs::warning("Type: Marker");
      break;
    case GL_DEBUG_TYPE_PUSH_GROUP:
      logs::warning("Type: Push Group");
      break;
    case GL_DEBUG_TYPE_POP_GROUP:
      logs::warning("Type: Pop Group");
      break;
    case GL_DEBUG_TYPE_OTHER:
      logs::warning("Type: Other");
      break;
  }

  switch (severity) {
    case GL_DEBUG_SEVERITY_HIGH:
      logs::warning("Severity: high");
      break;
    case GL_DEBUG_SEVERITY_MEDIUM:
      logs::warning("Severity: medium");
      break;
    case GL_DEBUG_SEVERITY_LOW:
      logs::warning("Severity: low");
      break;
    case GL_DEBUG_SEVERITY_NOTIFICATION:
      logs::warning("Severity: notification");
      break;
  }

  logs::print_newline();
}


real32 util::round_to_nearest_multiple(real32 n, real32 multiple_of) {
  return (floor((n) / multiple_of) * multiple_of) + multiple_of;
}


real64 util::get_us_from_duration(chrono::duration<real64> duration) {
  return chrono::duration_cast<chrono::duration<real64>>(duration).count();
}


v3 util::get_orthogonal_vector(v3 *v) {
  if (v->z < v->x) {
    return v3(v->y, -v->x, 0.0f);
  } else {
    return v3(0.0f, -v->z, v->y);
  }
}


uint32 util::kb_to_b(uint32 value) { return value * 1024; }
uint32 util::mb_to_b(uint32 value) { return kb_to_b(value) * 1024; }
uint32 util::gb_to_b(uint32 value) { return mb_to_b(value) * 1024; }
uint32 util::tb_to_b(uint32 value) { return gb_to_b(value) * 1024; }
real32 util::b_to_kb(uint32 value) { return value / 1024.0f; }
real32 util::b_to_mb(uint32 value) { return b_to_kb(value) / 1024.0f; }
real32 util::b_to_gb(uint32 value) { return b_to_mb(value) / 1024.0f; }
real32 util::b_to_tb(uint32 value) { return b_to_gb(value) / 1024.0f; }