Debian Codegraph @

ruby/video/opengl/program.hpp

raw
1 void OpenGLProgram::bind(OpenGL* instance, const Markup::Node& node, const string& pathname) {
2 filter = glrFilter(node["filter"].text());
3 wrap = glrWrap(node["wrap"].text());
4 modulo = glrModulo(node["modulo"].integer());
5
6 string w = node["width"].text(), h = node["height"].text();
7 if(w.endsWith("%")) relativeWidth = real(w.rtrim<1>("%")) / 100.0;
8 else absoluteWidth = decimal(w);
9 if(h.endsWith("%")) relativeHeight = real(h.rtrim<1>("%")) / 100.0;
10 else absoluteHeight = decimal(h);
11
12 format = glrFormat(node["format"].text());
13
14 program = glCreateProgram();
15 glGenFramebuffers(1, &framebuffer);
16
17 if(file::exists({pathname, node["vertex"].text()})) {
18 string source = file::read({pathname, node["vertex"].text()});
19 parse(instance, source);
20 vertex = glrCreateShader(program, GL_VERTEX_SHADER, source);
21 } else {
22 vertex = glrCreateShader(program, GL_VERTEX_SHADER, OpenGLVertexShader);
23 }
24
25 if(file::exists({pathname, node["geometry"].text()})) {
26 string source = file::read({pathname, node["geometry"].text()});
27 parse(instance, source);
28 geometry = glrCreateShader(program, GL_GEOMETRY_SHADER, source);
29 } else {
30 //geometry shaders, when attached, must pass all vertex output through to the fragment shaders
31 //geometry = glrCreateShader(program, GL_GEOMETRY_SHADER, OpenGLGeometryShader);
32 }
33
34 if(file::exists({pathname, node["fragment"].text()})) {
35 string source = file::read({pathname, node["fragment"].text()});
36 parse(instance, source);
37 fragment = glrCreateShader(program, GL_FRAGMENT_SHADER, source);
38 } else {
39 fragment = glrCreateShader(program, GL_FRAGMENT_SHADER, OpenGLFragmentShader);
40 }
41
42 for(auto& leaf : node.find("pixmap")) {
43 nall::image image({pathname, leaf.text()});
44 image.transform(0, 32, 255u << 24, 255u << 16, 255u << 8, 255u << 0);
45 if(image.empty()) continue;
46
47 GLuint texture;
48 glGenTextures(1, &texture);
49
50 unsigned n = pixmaps.size();
51 pixmaps(n).texture = texture;
52 pixmaps(n).width = image.width;
53 pixmaps(n).height = image.height;
54 pixmaps(n).format = format;
55 pixmaps(n).filter = filter;
56 pixmaps(n).wrap = wrap;
57 if(leaf["format"].exists()) pixmaps(n).format = glrFormat(leaf["format"].text());
58 if(leaf["filter"].exists()) pixmaps(n).filter = glrFilter(leaf["filter"].text());
59 if(leaf["wrap"].exists()) pixmaps(n).wrap = glrWrap(leaf["wrap"].text());
60
61 unsigned w = glrSize(image.width), h = glrSize(image.height);
62 uint32_t* buffer = new uint32_t[w * h]();
63 glBindTexture(GL_TEXTURE_2D, texture);
64 glTexImage2D(GL_TEXTURE_2D, 0, pixmaps(n).format, w, h, 0, pixmaps(n).getFormat(), pixmaps(n).getType(), buffer);
65 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image.width, image.height, getFormat(), getType(), image.data);
66 delete[] buffer;
67 }
68
69 OpenGLSurface::allocate();
70 glrLinkProgram(program);
71 }
72
73 //apply manifest settings to shader source #in tags
74 void OpenGLProgram::parse(OpenGL* instance, string& source) {
75 lstring lines = source.split("\n");
76 for(auto& line : lines) {
77 string s = line;
78 if(auto position = s.find("//")) s.resize(position()); //strip comments
79 s.strip(); //remove extraneous whitespace
80 if(s.match("#in ?*")) {
81 s.ltrim<1>("#in ").strip();
82 if(auto setting = instance->settings.find({s})) {
83 line = {"#define ", setting().name, " ", setting().value};
84 } else {
85 line.reset(); //undefined variable (test in source with #ifdef)
86 }
87 }
88 }
89 source = lines.merge("\n");
90 }
91
92 void OpenGLProgram::release() {
93 OpenGLSurface::release();
94 for(auto& pixmap : pixmaps) glDeleteTextures(1, &pixmap.texture);
95 pixmaps.reset();
96
97 width = 0;
98 height = 0;
99 format = GL_RGBA8;
100 filter = GL_LINEAR;
101 wrap = GL_CLAMP_TO_BORDER;
102 phase = 0;
103 modulo = 0;
104 absoluteWidth = 0;
105 absoluteHeight = 0;
106 relativeWidth = 0;
107 relativeHeight = 0;
108 }