-
Notifications
You must be signed in to change notification settings - Fork 60
/
headless_opengl_tests.py
292 lines (240 loc) · 12.2 KB
/
headless_opengl_tests.py
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
282
283
284
285
286
287
288
289
290
291
292
from __future__ import absolute_import, division, print_function
# fmt: off
from easyvolcap.utils.egl_utils import eglContextManager # must be imported before OpenGL.GL
import OpenGL.GL as gl
# fmt: on
import os
import cv2
import torch
import numpy as np
from os.path import join, dirname
from easyvolcap.utils.console_utils import *
from easyvolcap.utils.gl_utils import Quad, Mesh
from easyvolcap.utils.viewer_utils import Camera
from easyvolcap.utils.data_utils import save_image
from easyvolcap.utils.gl_utils import common_opengl_options, linearize_depth
from easyvolcap.utils.test_utils import my_tests
WIDTH, HEIGHT = 512, 512
eglctx = eglContextManager(HEIGHT, WIDTH) # will create a new context
common_opengl_options() # common init
def test_gl_context():
# Render triangle
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glBegin(gl.GL_TRIANGLES)
gl.glColor3f(1, 0, 0)
gl.glVertex2f(0, 1)
gl.glColor3f(0, 1, 0)
gl.glVertex2f(-1, -1)
gl.glColor3f(0, 0, 1)
gl.glVertex2f(1, -1)
gl.glEnd()
# Read result
img_buf = gl.glReadPixels(0, 0, WIDTH, HEIGHT, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE)
img = np.frombuffer(img_buf, np.uint8).reshape(HEIGHT, WIDTH, 4)[::-1]
assert all(img[0, 0, :3] == 0) # black corner
assert all(img[0, -1, :3] == 0) # black corner
assert img[10, WIDTH // 2, :3].argmax() == 0 # red corner
assert img[-1, 10, :3].argmax() == 1 # green corner
assert img[-1, -10, :3].argmax() == 2 # blue corner
def test_gl_mesh_rast():
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
mesh_path = 'assets/meshes/bunny.ply'
img_path = 'test_gl_mesh_rast.png'
camera = Camera(H=HEIGHT, W=WIDTH,
K=torch.tensor([[592., 0., 256.],
[0., 592., 256.],
[0., 0., 1.]]),
R=torch.tensor([[0.9908, -0.1353, 0.0000],
[-0.1341, -0.9815, -0.1365],
[0.0185, 0.1353, -0.9906]]),
T=torch.tensor([[0.0178],
[0.0953],
[0.3137]])
)
mesh = Mesh(filename=mesh_path, shade_flat=False)
mesh.render(camera)
# ndc = torch.cat([mesh.verts, torch.ones_like(mesh.verts)[..., -1:]], dim=-1).numpy() @ glm.transpose(camera.gl_ext) @ glm.transpose(camera.gl_ixt)
# Read result
img_buf = gl.glReadPixels(0, 0, WIDTH, HEIGHT, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE)
img = np.frombuffer(img_buf, np.uint8).reshape(HEIGHT, WIDTH, 4)[::-1]
save_image(img_path, img)
log(f'Rendered image saved at: {blue(img_path)}')
def test_gl_tex_blit():
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
quad = Quad(H=HEIGHT, W=WIDTH)
data = np.random.randint(0, 255, (HEIGHT, WIDTH, 4), dtype=np.uint8)
data[20:30, 20:30] = [255, 0, 0, 255]
data[30:40, 20:30] = [0, 255, 0, 255]
data[20:30, 30:40] = [0, 0, 255, 255]
data[30:40, 30:40] = [255, 255, 255, 255]
data = data[::-1] # flip 0
data = np.asarray(data, order='C')
quad.upload_to_texture(data) # upload the pointer
# FIXME: TEST FAILING AFTER CHANING ALIGNMENT
fbo = gl.glGenFramebuffers(1)
gl.glBindFramebuffer(gl.GL_READ_FRAMEBUFFER, fbo)
gl.glFramebufferTexture2D(gl.GL_READ_FRAMEBUFFER, gl.GL_COLOR_ATTACHMENT0, gl.GL_TEXTURE_2D, quad.tex, 0) # attach the texture to the framebuffer as logical buffer
gl.glBlitFramebuffer(0, 0, WIDTH, HEIGHT,
0, 0, WIDTH, HEIGHT,
gl.GL_COLOR_BUFFER_BIT, gl.GL_NEAREST)
gl.glBindFramebuffer(gl.GL_READ_FRAMEBUFFER, eglctx.fbo) # TODO: DEBUG LAG
# Read result
img_buf = gl.glReadPixels(0, 0, WIDTH, HEIGHT, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) # TODO: FIX THIS
img = np.frombuffer(img_buf, np.uint8).reshape(HEIGHT, WIDTH, 4)[::-1]
img_path = 'test_gl_tex_blit.png'
save_image(img_path, img)
log(f'Tex blit image saved at: {blue(img_path)}')
assert img[20, 20, :3].argmax() == 0 # red corner
assert img[30, 20, :3].argmax() == 1 # green corner
assert img[20, 30, :3].argmax() == 2 # blue corner
assert all(img[30, 30] == 255) # white corner
def test_gl_quad_blit():
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
quad = Quad(H=HEIGHT, W=WIDTH)
data = np.random.randint(0, 255, (HEIGHT, WIDTH, 4), dtype=np.uint8)
data[10:20, 10:20] = [255, 0, 0, 255]
data[20:30, 10:20] = [0, 255, 0, 255]
data[10:20, 20:30] = [0, 0, 255, 255]
data[20:30, 20:30] = [255, 255, 255, 255]
data = data[::-1] # flip 0
data = np.asarray(data, order='C')
quad.upload_to_texture(data) # upload the pointer
quad.blit() # no camera to render
# Read result
img_buf = gl.glReadPixels(0, 0, WIDTH, HEIGHT, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE)
img = np.frombuffer(img_buf, np.uint8).reshape(HEIGHT, WIDTH, 4)[::-1]
img_path = 'test_gl_quad_blit.png'
save_image(img_path, img)
log(f'Quad blit image saved at: {blue(img_path)}')
assert img[10, 10, :3].argmax() == 0 # red corner
assert img[20, 10, :3].argmax() == 1 # green corner
assert img[10, 20, :3].argmax() == 2 # blue corner
assert all(img[20, 20] == 255) # white corner
def test_gl_quad_draw():
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
quad = Quad(H=HEIGHT, W=WIDTH)
data = np.random.randint(0, 255, (HEIGHT, WIDTH, 4), dtype=np.uint8)
data[-20:-10, -20:-10] = [255, 0, 0, 255]
data[-30:-20, -20:-10] = [0, 255, 0, 255]
data[-20:-10, -30:-20] = [0, 0, 255, 255]
data[-30:-20, -30:-20] = [255, 255, 255, 255]
data = data[::-1] # flip 0
data = np.asarray(data, order='C')
quad.upload_to_texture(data) # upload the pointer
quad.draw() # no camera to render
# Read result
img_buf = gl.glReadPixels(0, 0, WIDTH, HEIGHT, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE)
img = np.frombuffer(img_buf, np.uint8).reshape(HEIGHT, WIDTH, 4)[::-1]
img_path = 'test_gl_quad_draw.png'
save_image(img_path, img)
log(f'Quad draw image saved at: {blue(img_path)}')
assert img[-20, -20, :3].argmax() == 0 # red corner
assert img[-30, -20, :3].argmax() == 1 # green corner
assert img[-20, -30, :3].argmax() == 2 # blue corner
assert all(img[-30, -30] == 255) # white corner
def test_eglctx_manual_fbo_rbo(): # TODO: BLACK RESULTS ON WSL (BUT COULD RENDER WITHOUT RESIZING)
render_w = 1024
render_h = 1024
# eglctx.resize(render_w, render_h)
gl.glViewport(0, 0, render_w, render_h) # wtf
gl.glScissor(0, 0, render_w, render_h) # wtf # NOTE: Need to redefine scissor size
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
# Add new buffer
old = gl.glGetInteger(gl.GL_FRAMEBUFFER_BINDING)
fbo = gl.glGenFramebuffers(1)
rbo0, rbo1, rbo_dpt = gl.glGenRenderbuffers(3)
gl.glBindRenderbuffer(gl.GL_RENDERBUFFER, rbo0)
gl.glRenderbufferStorage(gl.GL_RENDERBUFFER, gl.GL_RGBA8, render_w, render_h)
gl.glBindRenderbuffer(gl.GL_RENDERBUFFER, rbo1)
gl.glRenderbufferStorage(gl.GL_RENDERBUFFER, gl.GL_RGBA8, render_w, render_h)
gl.glBindRenderbuffer(gl.GL_RENDERBUFFER, rbo_dpt)
gl.glRenderbufferStorage(gl.GL_RENDERBUFFER, gl.GL_DEPTH_COMPONENT, render_w, render_h)
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, fbo)
gl.glFramebufferRenderbuffer(gl.GL_FRAMEBUFFER, gl.GL_COLOR_ATTACHMENT0, gl.GL_RENDERBUFFER, rbo0)
gl.glFramebufferRenderbuffer(gl.GL_FRAMEBUFFER, gl.GL_COLOR_ATTACHMENT1, gl.GL_RENDERBUFFER, rbo1)
gl.glFramebufferRenderbuffer(gl.GL_FRAMEBUFFER, gl.GL_DEPTH_ATTACHMENT, gl.GL_RENDERBUFFER, rbo_dpt)
gl.glDrawBuffers(2, [gl.GL_COLOR_ATTACHMENT0, gl.GL_COLOR_ATTACHMENT1])
mesh_path = 'assets/meshes/bunny.ply'
img_path = 'test_eglctx_manual_fbo_rbo.png'
dpt_path = 'test_eglctx_manual_fbo_rbo_dpt.png'
camera = Camera(H=render_h, W=render_w,
K=torch.tensor([[render_w * 592 / 512, 0., render_w / 2],
[0., render_h * 592 / 512, render_h / 2],
[0., 0., 1.]]),
R=torch.tensor([[0.9908, -0.1353, 0.0000],
[-0.1341, -0.9815, -0.1365],
[0.0185, 0.1353, -0.9906]]),
T=torch.tensor([[0.0178],
[0.0953],
[0.3137]]),
n=0.02,
f=1,
)
mesh = Mesh(filename=mesh_path, shade_flat=False)
# mesh.offscreen_render(eglctx, camera)
mesh.render(camera)
# Read result
gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0)
img_buf = gl.glReadPixels(0, 0, render_w, render_h, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE)
img = np.frombuffer(img_buf, np.uint8).reshape(render_h, render_w, 4)[::-1]
save_image(img_path, img)
log(f'Rendered image saved at: {blue(img_path)}')
# Read result
# gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1)
# dpt_buf = gl.glReadPixels(0, 0, render_w, render_h, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) # lost precision...
# dpt = np.frombuffer(dpt_buf, np.uint8).reshape(render_h, render_w, 4)[::-1]
# dpt = dpt[..., 0] / 1 + dpt[..., 0] / 256 + dpt[..., 0] / 65536 + dpt[..., 0] / 16777216
# dpt = (dpt - dpt.min()) / (dpt.max() - dpt.min())
dpt_buf = gl.glReadPixels(0, 0, render_w, render_h, gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT) # lost precision... # MARK: SYNC
# gl.glBindTexture(gl.GL_TEXTURE_2D, eglctx.tex_dpt)
# dpt_buf = gl.glGetTexImage(gl.GL_TEXTURE_2D, 0, gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
dpt = np.frombuffer(dpt_buf, np.float32).reshape(render_h, render_w, 1)[::-1]
dpt = linearize_depth(dpt, camera.n, camera.f)
dpt = (dpt.min() - dpt) / (dpt.min() - dpt.max())
save_image(dpt_path, dpt)
log(f'Rendered depth saved at: {blue(dpt_path)}')
gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0)
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, old)
def test_eglctx_auto_fbo_rbo():
render_w = 1024
render_h = 1024
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
mesh_path = 'assets/meshes/bunny.ply'
img_path = 'test_eglctx_auto_fbo_rbo.png'
dpt_path = 'test_eglctx_auto_fbo_rbo_dpt.png'
camera = Camera(H=render_h, W=render_w,
K=torch.tensor([[render_w * 592 / 512, 0., render_w / 2],
[0., render_h * 592 / 512, render_h / 2],
[0., 0., 1.]]),
R=torch.tensor([[0.9908, -0.1353, 0.0000],
[-0.1341, -0.9815, -0.1365],
[0.0185, 0.1353, -0.9906]]),
T=torch.tensor([[0.0178],
[0.0953],
[0.3137]]),
n=0.02,
f=1,
)
mesh = Mesh(filename=mesh_path, shade_flat=False)
mesh.offscreen_render(eglctx, camera)
# Read result
gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0)
img_buf = gl.glReadPixels(0, 0, render_w, render_h, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE)
img = np.frombuffer(img_buf, np.uint8).reshape(render_h, render_w, 4)[::-1]
save_image(img_path, img)
log(f'Rendered image saved at: {blue(img_path)}')
# Read result
# gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1)
# dpt_buf = gl.glReadPixels(0, 0, render_w, render_h, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) # lost precision...
# dpt = np.frombuffer(dpt_buf, np.uint8).reshape(render_h, render_w, 4)[::-1]
# dpt = dpt[..., 0] / 1 + dpt[..., 0] / 256 + dpt[..., 0] / 65536 + dpt[..., 0] / 16777216
dpt_buf = gl.glReadPixels(0, 0, render_w, render_h, gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT) # lost precision... # MARK: SYNC
# gl.glBindTexture(gl.GL_TEXTURE_2D, eglctx.tex_dpt)
# dpt_buf = gl.glGetTexImage(gl.GL_TEXTURE_2D, 0, gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
dpt = np.frombuffer(dpt_buf, np.float32).reshape(render_h, render_w, 1)[::-1]
dpt = linearize_depth(dpt, camera.n, camera.f) # this is the z component in camera coordinates
dpt = (dpt.min() - dpt) / (dpt.min() - dpt.max())
save_image(dpt_path, dpt)
log(f'Rendered depth saved at: {blue(dpt_path)}')
if __name__ == '__main__':
my_tests(globals())