-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathmain.hpp
173 lines (146 loc) · 5.04 KB
/
main.hpp
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
#if defined(PLATFORM_MACOS)
#import <mach-o/dyld.h>
#endif
auto OpenGL::setShader(const string& pathname) -> void {
#if !defined(NO_LIBRASHADER)
format = inputFormat;
filter = GL_NEAREST;
wrap = GL_CLAMP_TO_BORDER;
absoluteWidth = 0, absoluteHeight = 0;
if(_chain != NULL) {
_libra.gl_filter_chain_free(&_chain);
}
if(_preset != NULL) {
_libra.preset_free(&_preset);
}
if(file::exists(pathname)) {
if(_libra.preset_create(pathname.data(), &_preset) != NULL) {
print(string{"OpenGL: Failed to load shader: ", pathname, "\n"});
setShader("");
return;
}
if(auto error = _libra.gl_filter_chain_create(&_preset, NULL, &_chain)) {
print(string{"OpenGL: Failed to create filter chain for: ", pathname, "\n"});
_libra.error_print(error);
setShader("");
return;
}
}
#endif
}
auto OpenGL::clear() -> void {
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
}
auto OpenGL::lock(u32*& data, u32& pitch) -> bool {
pitch = width * sizeof(u32);
return data = buffer;
}
auto OpenGL::output() -> void {
clear();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, getFormat(), getType(), buffer);
glGenerateMipmap(GL_TEXTURE_2D);
struct Source {
GLuint texture;
u32 width, height;
GLuint filter, wrap;
};
vector<Source> sources;
sources.prepend({texture, width, height, filter, wrap});
u32 targetWidth = absoluteWidth ? absoluteWidth : outputWidth;
u32 targetHeight = absoluteHeight ? absoluteHeight : outputHeight;
u32 x = (outputWidth - targetWidth) / 2;
u32 y = (outputHeight - targetHeight) / 2;
if(has_shader()) {
// Shader path: our intermediate framebuffer matches the output size
if(!framebuffer || framebufferWidth != outputWidth || framebufferHeight != outputHeight) {
if(framebuffer) {
glDeleteFramebuffers(1, &framebuffer);
framebuffer = 0;
}
if(framebufferTexture) {
glDeleteTextures(1, &framebufferTexture);
framebufferTexture = 0;
}
framebufferWidth = outputWidth, framebufferHeight = outputHeight;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glGenTextures(1, &framebufferTexture);
glBindTexture(GL_TEXTURE_2D, framebufferTexture);
framebufferFormat = GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, framebufferFormat, framebufferWidth, framebufferHeight, 0, framebufferFormat,
GL_UNSIGNED_BYTE, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebufferTexture, 0);
}
} else {
// Non-shader path: our intermediate framebuffer matches the source size and re-uses the source texture
if(!framebuffer || framebufferWidth != width || framebufferHeight != height) {
if(framebuffer) {
glDeleteFramebuffers(1, &framebuffer);
framebuffer = 0;
}
if(framebufferTexture) {
glDeleteTextures(1, &framebufferTexture);
framebufferTexture = 0;
}
framebufferWidth = width, framebufferHeight = height;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
}
}
render(sources[0].width, sources[0].height, outputX + x, outputY + y, targetWidth, targetHeight);
}
auto OpenGL::initialize(const string& shader) -> bool {
if(!OpenGLBind()) return false;
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisable(GL_POLYGON_SMOOTH);
glDisable(GL_STENCIL_TEST);
glEnable(GL_DITHER);
#if !defined(NO_LIBRASHADER)
_libra = librashader_load_instance();
if(!_libra.instance_loaded) {
print("OpenGL: Failed to load librashader: shaders will be disabled\n");
}
if(_libra.gl_init_context(resolveSymbol) != NULL) {
print("OpenGL: Failed to initialize librashader context: shaders will be disabled\n");
};
#endif
setShader(shader);
return initialized = true;
}
auto OpenGL::resolveSymbol(const char* name) -> const void * {
#if defined(PLATFORM_MACOS)
NSSymbol symbol;
char *symbolName;
symbolName = (char*)malloc(strlen(name) + 2);
strcpy(symbolName + 1, name);
symbolName[0] = '_';
symbol = NULL;
if(NSIsSymbolNameDefined (symbolName)) symbol = NSLookupAndBindSymbol (symbolName);
free(symbolName); // 5
return (void*)(symbol ? NSAddressOfSymbol(symbol) : NULL);
#else
void* symbol = (void*)glGetProcAddress(name);
#if defined(PLATFORM_WINDOWS)
if(!symbol) {
// (w)glGetProcAddress will not return function pointers from any OpenGL functions
// that are directly exported by the opengl32.dll
HMODULE module = LoadLibraryA("opengl32.dll");
symbol = (void*)GetProcAddress(module, name);
}
#endif
#endif
return symbol;
}
auto OpenGL::terminate() -> void {
if(!initialized) return;
setShader("");
OpenGLSurface::release();
if(buffer) { delete[] buffer; buffer = nullptr; }
initialized = false;
}