Debian Codegraph @

ruby/video/opengl/utility.hpp

raw
1 static unsigned glrSize(unsigned size) {
2 return size;
3 //return bit::round(size); //return nearest power of two
4 }
5
6 static GLuint glrFormat(const string& format) {
7 if(format == "r32i" ) return GL_R32I;
8 if(format == "r32ui" ) return GL_R32UI;
9 if(format == "rgba8" ) return GL_RGBA8;
10 if(format == "rgb10a2") return GL_RGB10_A2;
11 if(format == "rgba12" ) return GL_RGBA12;
12 if(format == "rgba16" ) return GL_RGBA16;
13 if(format == "rgba16f") return GL_RGBA16F;
14 if(format == "rgba32f") return GL_RGBA32F;
15 return GL_RGBA8;
16 }
17
18 static GLuint glrFilter(const string& filter) {
19 if(filter == "nearest") return GL_NEAREST;
20 if(filter == "linear" ) return GL_LINEAR;
21 return GL_LINEAR;
22 }
23
24 static GLuint glrWrap(const string& wrap) {
25 if(wrap == "border") return GL_CLAMP_TO_BORDER;
26 if(wrap == "edge" ) return GL_CLAMP_TO_EDGE;
27 if(wrap == "repeat") return GL_REPEAT;
28 return GL_CLAMP_TO_BORDER;
29 }
30
31 static unsigned glrModulo(unsigned modulo) {
32 if(modulo) return modulo;
33 return 300; //divisible by 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 100, 150
34 }
35
36 static GLuint glrProgram() {
37 GLuint program = 0;
38 glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&program);
39 return program;
40 }
41
42 static void glrUniform1i(const string& name, GLint value) {
43 GLint location = glGetUniformLocation(glrProgram(), name);
44 glUniform1i(location, value);
45 }
46
47 static void glrUniform4f(const string& name, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3) {
48 GLint location = glGetUniformLocation(glrProgram(), name);
49 glUniform4f(location, value0, value1, value2, value3);
50 }
51
52 static void glrUniformMatrix4fv(const string& name, GLfloat *values) {
53 GLint location = glGetUniformLocation(glrProgram(), name);
54 glUniformMatrix4fv(location, 1, GL_FALSE, values);
55 }
56
57 static void glrParameters(GLuint filter, GLuint wrap) {
58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
62 }
63
64 static GLuint glrCreateShader(GLuint program, GLuint type, const char* source) {
65 GLuint shader = glCreateShader(type);
66 glShaderSource(shader, 1, &source, 0);
67 glCompileShader(shader);
68 GLint result = GL_FALSE;
69 glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
70 if(result == GL_FALSE) {
71 GLint length = 0;
72 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
73 char text[length + 1];
74 glGetShaderInfoLog(shader, length, &length, text);
75 text[length] = 0;
76 print("[ruby::OpenGL: shader compiler error]\n", (const char*)text, "\n\n");
77 return 0;
78 }
79 glAttachShader(program, shader);
80 return shader;
81 }
82
83 static void glrLinkProgram(GLuint program) {
84 glLinkProgram(program);
85 GLint result = GL_FALSE;
86 glGetProgramiv(program, GL_LINK_STATUS, &result);
87 if(result == GL_FALSE) {
88 GLint length = 0;
89 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
90 char text[length + 1];
91 glGetProgramInfoLog(program, length, &length, text);
92 text[length] = 0;
93 print("[ruby::OpenGL: shader linker error]\n", (const char*)text, "\n\n");
94 }
95 glValidateProgram(program);
96 result = GL_FALSE;
97 glGetProgramiv(program, GL_VALIDATE_STATUS, &result);
98 if(result == GL_FALSE) {
99 GLint length = 0;
100 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
101 char text[length + 1];
102 glGetProgramInfoLog(program, length, &length, text);
103 text[length] = 0;
104 print("[ruby::OpenGL: shader validation error]\n", (const char*)text, "\n\n");
105 }
106 }