-
Notifications
You must be signed in to change notification settings - Fork 0
/
opengl.m
177 lines (147 loc) · 5.87 KB
/
opengl.m
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
:- module opengl.
%==============================================================================%
% Wrapper for common OpenGL functions that are used by both the GL2 and GL4
% renderers. All functions here must be present in OpenGL 1.3 so that no
% function loading is required. If we end up needing some later functionality
% that should be shared between GL2 and GL4, we can make an opengl_ext.m
% module that will handle that.
:- interface.
%==============================================================================%
:- use_module mglow.
:- type shader.
:- type frag_shader.
:- type vert_shader.
:- type texture.
:- type shader_type ---> vert ; frag.
:- type shape_type ---> triangle_strip ; triangle_fan ; triangles ; line_loop ; point ; lines.
:- type filter_set ---> mag_filter ; min_filter.
:- type filter_type ---> linear ; nearest.
% upload_texture(Output, Pixels, W, H, !Window)
:- pred upload_texture(texture::uo, c_pointer::in, int::in, int::in,
mglow.window::di, mglow.window::uo) is det.
:- pred bind_texture(texture::in, mglow.window::di, mglow.window::uo) is det.
:- pred texture_filter(filter_set::in, filter_type::in, mglow.window::di, mglow.window::uo) is det.
% draw_arrays(Type, FirstIndex, Count, !Window)
:- pred draw_arrays(shape_type::in, int::in, int::in,
mglow.window::di, mglow.window::uo) is det.
:- pred clear_color(float::in, float::in, float::in, float::in,
mglow.window::di, mglow.window::uo) is det.
:- pred clear(mglow.window::di, mglow.window::uo) is det.
% viewport(X, Y, W, H)
:- pred viewport(int::in, int::in, int::in, int::in,
mglow.window::di, mglow.window::uo) is det.
:- pred enable_depth(mglow.window::di, mglow.window::uo) is det.
:- pred disable_depth(mglow.window::di, mglow.window::uo) is det.
:- pred clear_depth(mglow.window::di, mglow.window::uo) is det.
%==============================================================================%
:- implementation.
%==============================================================================%
:- type shader == int.
:- type frag_shader == int.
:- type vert_shader == int.
:- pragma foreign_decl("C", "#include ""glow/glow.h"" ").
:- pragma foreign_decl("C", "
#ifdef _WIN32
#include <Windows.h>
#endif
#include <GL/gl.h>
").
:- pragma foreign_decl("C", "
void OpenGL_TextureFinalizer(void *in, void *unused);
").
:- pragma foreign_code("C", "
void OpenGL_TextureFinalizer(void *in, void *win){
Glow_MakeCurrent((struct Glow_Window*)win);
glDeleteTextures(1, (GLuint*)in);
}
").
:- pragma foreign_enum("C", shape_type/0,
[
triangle_strip - "GL_TRIANGLE_STRIP",
triangle_fan - "GL_TRIANGLE_FAN",
triangles - "GL_TRIANGLES",
line_loop - "GL_LINE_LOOP",
lines - "GL_LINES",
point - "GL_POINTS"
]).
:- pragma foreign_enum("C", filter_set/0,
[
min_filter - "GL_TEXTURE_MAG_FILTER",
mag_filter - "GL_TEXTURE_MIN_FILTER"
]).
:- pragma foreign_enum("C", filter_type/0,
[
linear - "GL_LINEAR",
nearest - "GL_NEAREST"
]).
:- pragma foreign_type("C", texture, "GLuint*").
:- pragma foreign_proc("C",
upload_texture(Tex::uo, Data::in, W::in, H::in, Win0::di, Win1::uo),
[will_not_call_mercury, will_not_throw_exception, thread_safe, promise_pure],
"
GLuint tex;
Win1 = Win0;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, W, H, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)Data);
Tex = MR_GC_malloc_atomic(8);
Tex[0] = tex;
MR_GC_register_finalizer(Tex, OpenGL_TextureFinalizer, Win1);
").
:- pragma foreign_proc("C",
bind_texture(Tex::in, Win0::di, Win1::uo),
[will_not_call_mercury, will_not_throw_exception,
thread_safe, promise_pure, does_not_affect_liveness],
"
Win1 = Win0;
glBindTexture(GL_TEXTURE_2D, *((GLuint*)Tex));
").
:- pragma foreign_proc("C",
texture_filter(Set::in, Type::in, Win0::di, Win1::uo),
[will_not_call_mercury, will_not_throw_exception,
thread_safe, promise_pure, does_not_affect_liveness],
"
Win1 = Win0;
glTexParameterf(GL_TEXTURE_2D, Set, Type);
").
:- pragma foreign_proc("C", clear_color(R::in, G::in, B::in, A::in, Win0::di, Win1::uo),
[will_not_call_mercury, will_not_throw_exception,
thread_safe, promise_pure, does_not_affect_liveness],
"
Win1 = Win0;
glClearColor(R, G, B, A);
").
:- pragma foreign_proc("C", draw_arrays(Type::in, I::in, Count::in, Win0::di, Win1::uo),
[will_not_call_mercury, will_not_throw_exception,
thread_safe, promise_pure, does_not_affect_liveness],
"
Win1 = Win0;
glDrawArrays(Type, I, Count);
").
:- pragma foreign_proc("C", clear(Win0::di, Win1::uo),
[will_not_call_mercury, will_not_throw_exception,
thread_safe, promise_pure, does_not_affect_liveness],
"
Win1 = Win0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
").
:- pragma foreign_proc("C",
viewport(X::in, Y::in, W::in, H::in, Win0::di, Win1::uo),
[will_not_call_mercury, will_not_throw_exception,
thread_safe, promise_pure, does_not_affect_liveness],
"
Win1 = Win0;
glViewport(X, Y, W, H);
").
:- pragma foreign_proc("C", enable_depth(Win0::di, Win1::uo),
[will_not_call_mercury, will_not_throw_exception,
thread_safe, promise_pure, does_not_affect_liveness],
" Win1 = Win0; glEnable(GL_DEPTH_TEST); ").
:- pragma foreign_proc("C", disable_depth(Win0::di, Win1::uo),
[will_not_call_mercury, will_not_throw_exception,
thread_safe, promise_pure, does_not_affect_liveness],
" Win1 = Win0; glDisable(GL_DEPTH_TEST); ").
:- pragma foreign_proc("C", clear_depth(Win0::di, Win1::uo),
[will_not_call_mercury, will_not_throw_exception,
thread_safe, promise_pure, does_not_affect_liveness],
" Win1 = Win0; glClear(GL_DEPTH_BUFFER_BIT); ").