Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import json
import moderngl.next as mgl
ctx = mgl.create_context(standalone=True, debug=True)
print('Limits:', json.dumps(ctx.limits.json(), indent=2))
print('Extensions:', json.dumps(mgl.extensions(ctx), indent=2))
print('Hardware Info:', json.dumps(mgl.hwinfo(ctx), indent=2))
print('Version Code:', ctx.version_code)
with self.subTest(vert_src=vert_src, vtype=vtype):
if self.ctx.version_code < vtype['version']:
warnings.warn('skipping version %s' % vtype['version'])
continue
prog = self.ctx.program(vertex_shader=vert_src % vtype, varyings=['v_out'])
if 'v_in' not in prog.attributes:
warnings.warn('skipping %s' % vtype['type'])
continue
fmt = moderngl.detect_format(prog, ['v_in'])
vbo1 = self.ctx.buffer(struct.pack(fmt, *vtype['input']))
vbo2 = self.ctx.buffer(b'\xAA' * struct.calcsize(fmt))
vao = self.ctx.simple_vertex_array(prog, vbo1, 'v_in')
vao.transform(vbo2, moderngl.POINTS, 1)
for a, b in zip(struct.unpack(fmt, vbo2.read()), vtype['output']):
self.assertAlmostEqual(a, b)
buf_v = self.ctx.buffer(struct.pack('2f', 4, 7))
res = self.ctx.buffer(reserve=buf_v.size)
vao = self.ctx.vertex_array(self.prog, [
(buf_m, '4f', 'in_m'),
(buf_v, '2f', 'in_v'),
])
self.prog['mult'].value = 0.0
vao.transform(res, moderngl.POINTS)
x, y = struct.unpack('2f', res.read())
self.assertAlmostEqual(x, 0.0)
self.assertAlmostEqual(y, 0.0)
self.prog['mult'].value = 1.0
vao.transform(res, moderngl.POINTS)
x, y = struct.unpack('2f', res.read())
self.assertAlmostEqual(x, 11.0)
self.assertAlmostEqual(y, 18.0)
self.prog['mult'].value = 2.0
vao.transform(res, moderngl.POINTS)
x, y = struct.unpack('2f', res.read())
self.assertAlmostEqual(x, 22.0)
self.assertAlmostEqual(y, 36.0)
def test_geometry_shader(self):
shader = self.create_program(path='vgf_quads.glsl')
self.assertAttributes(shader)
assert shader.geometry_input == moderngl.POINTS
assert shader.geometry_output == moderngl.TRIANGLE_STRIP
assert shader.geometry_vertices == 4
self.assertAlmostEqual(a, b)
vao.bind(0, 'f', vbo, '4f', offset=4, stride=0, divisor=0)
vao.transform(res, moderngl.POINTS, 2)
for a, b in zip(struct.unpack('8f', res.read()), (2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)):
self.assertAlmostEqual(a, b)
vao.bind(0, 'f', vbo, '4f', offset=8, stride=20, divisor=0)
vao.transform(res, moderngl.POINTS, 2)
for a, b in zip(struct.unpack('8f', res.read()), (3.0, 4.0, 5.0, 6.0, 8.0, 9.0, 10.0, 11.0)):
self.assertAlmostEqual(a, b)
vao.bind(0, 'f', vbo, '4f', offset=12, stride=0, divisor=1)
vao.transform(res, moderngl.POINTS, 2)
for a, b in zip(struct.unpack('8f', res.read()), (4.0, 5.0, 6.0, 7.0, 4.0, 5.0, 6.0, 7.0)):
self.assertAlmostEqual(a, b)
varyings=['out_vert']
)
vertices = [
4.0, 2.0, 7.5, 1.8,
3.0, 2.8, 6.0, 10.0
]
count = 10
indices = [0, 1] * 10
# UNSIGNED_INT index
vbo1 = self.ctx.buffer(np.array(vertices, dtype='f4').tobytes())
vbo2 = self.ctx.buffer(reserve=vbo1.size * count)
index = self.ctx.buffer(np.array(indices, dtype='u4').tobytes())
vao = self.ctx.simple_vertex_array(prog, vbo1, 'in_vert', index_buffer=index, index_element_size=4)
vao.transform(vbo2, moderngl.POINTS)
res = np.frombuffer(vbo2.read(), dtype='f4')
np.testing.assert_almost_equal(res, vertices * count)
# UNSIGNED_SHORT index
vbo1 = self.ctx.buffer(np.array(vertices, dtype='f4').tobytes())
vbo2 = self.ctx.buffer(reserve=vbo1.size * count)
index = self.ctx.buffer(np.array(indices, dtype='u2').tobytes())
vao = self.ctx.simple_vertex_array(prog, vbo1, 'in_vert', index_buffer=index, index_element_size=2)
vao.transform(vbo2, moderngl.POINTS)
res = np.frombuffer(vbo2.read(), dtype='f4')
np.testing.assert_almost_equal(res, vertices * count)
# UNSIGNED_BYTE index
vbo1 = self.ctx.buffer(np.array(vertices, dtype='f4').tobytes())
vbo2 = self.ctx.buffer(reserve=vbo1.size * count)
index = self.ctx.buffer(np.array(indices, dtype='u1').tobytes())
def test_vertex_attrib_per_render_3(self):
self.vao2.transform(self.res, moderngl.POINTS, vertices=1, instances=4)
a, b, c, d = struct.unpack('4f', self.res.read(16))
self.assertAlmostEqual(a, 111.0)
self.assertAlmostEqual(b, 121.0)
self.assertAlmostEqual(c, 131.0)
self.assertAlmostEqual(d, 141.0)
def test_vertex_attrib_per_instance_1(self):
self.vao1.transform(self.res, moderngl.POINTS, vertices=4, instances=1)
a, b, c, d = struct.unpack('4f', self.res.read(16))
self.assertAlmostEqual(a, 11.0)
self.assertAlmostEqual(b, 12.0)
self.assertAlmostEqual(c, 13.0)
self.assertAlmostEqual(d, 14.0)
def fmt(x):
x = moderngl.pack([x], 'f2')
t = ('0' * 16 + bin(int.from_bytes(x, 'little'))[2:])[-16:]
return t[0 : 1] + ' ' + t[1 : 6] + ' ' + t[6 : 16]
f_color = texture(Texture, v_text);
}
''',
)
pixels = np.tile([0.0, 0.5, 1.0], 4 * 4).astype('f2')
self.tex = self.ctx.texture((4, 4), 3, pixels, dtype='f2')
self.vbo = self.ctx.buffer(np.array([-1, -1, -1, 1, 1, -1, 1, 1], dtype='f4'))
self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert')
self.fbo = self.ctx.simple_framebuffer((4, 4))
self.fbo.use()
self.fbo.clear()
self.tex.use()
self.vao.render(moderngl.TRIANGLE_STRIP)
res = np.frombuffer(self.fbo.read(dtype='f2'), dtype='f2')
npt.assert_almost_equal(res, pixels, decimal=2)