Skip to content

Commit ff7b382

Browse files
committed
Switch to glProgramUniform + remove pyglet_rendering context manager
1 parent dc8e3e5 commit ff7b382

File tree

8 files changed

+86
-118
lines changed

8 files changed

+86
-118
lines changed

arcade/context.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def __init__(self, window: pyglet.window.Window, gc_mode: str = "context_gc"):
190190
# These multiple labels with different configurations are stored
191191
self.pyglet_label_cache: Dict[str, pyglet.text.Label] = {}
192192

193-
self.active_program = None
193+
# self.active_program = None
194194
self.point_size = 1.0
195195

196196
def reset(self) -> None:
@@ -200,7 +200,7 @@ def reset(self) -> None:
200200
"""
201201
self.screen.use(force=True)
202202
self.bind_window_block()
203-
self.active_program = None
203+
# self.active_program = None
204204
arcade.set_viewport(0, self.window.width, 0, self.window.height)
205205
self.enable_only(self.BLEND)
206206
self.blend_func = self.BLEND_DEFAULT
@@ -282,24 +282,6 @@ def projection_2d_matrix(self, value: Mat4):
282282
self._projection_2d_matrix = value
283283
self.window.projection = self._projection_2d_matrix
284284

285-
@contextmanager
286-
def pyglet_rendering(self):
287-
"""Context manager for pyglet rendering.
288-
Since arcade and pyglet needs slightly different
289-
states we needs some initialization and cleanup.
290-
291-
Examples::
292-
293-
with window.ctx.pyglet_rendering():
294-
# Draw with pyglet here
295-
"""
296-
try:
297-
yield None
298-
finally:
299-
self.active_program = None
300-
# self.enable(self.BLEND, pyglet.gl.GL_SCISSOR_TEST)
301-
# self.blend_func = self.BLEND_DEFAULT
302-
303285
def load_program(
304286
self,
305287
*,

arcade/experimental/video_player.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ def on_draw(self):
2424
# video_width, video_height = self.get_video_size()
2525
# print((video_width, video_height), self.player.source.duration, self.player.time)
2626

27-
with self.ctx.pyglet_rendering():
28-
self.ctx.disable(self.ctx.BLEND)
29-
video_texture = self.player.texture
30-
video_texture.blit(
31-
0,
32-
0,
33-
width=self.width,
34-
height=self.height,
35-
)
27+
self.ctx.disable(self.ctx.BLEND)
28+
video_texture = self.player.texture
29+
video_texture.blit(
30+
0,
31+
0,
32+
width=self.width,
33+
height=self.height,
34+
)
3635

3736
def on_update(self, delta_time: float):
3837
pass

arcade/gl/compute_shader.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def glo(self) -> int:
8484

8585
def use(self):
8686
gl.glUseProgram(self._glo)
87-
self._ctx.active_program = self
87+
# self._ctx.active_program = self
8888

8989
def run(self, group_x=1, group_y=1, group_z=1) -> None:
9090
"""
@@ -109,8 +109,8 @@ def __getitem__(self, item) -> Union[Uniform, UniformBlock]:
109109
def __setitem__(self, key, value):
110110
"""Set a uniform value"""
111111
# Ensure we are setting the uniform on this program
112-
if self._ctx.active_program != self:
113-
self.use()
112+
# if self._ctx.active_program != self:
113+
# self.use()
114114

115115
try:
116116
uniform = self._uniforms[key]

arcade/gl/program.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,10 @@ def __getitem__(self, item) -> Union[Uniform, UniformBlock]:
227227

228228
def __setitem__(self, key, value):
229229
"""Set a uniform value"""
230+
# NOTE: We don't need this when using glProgramUniform
230231
# Ensure we are setting the uniform on this program
231-
if self._ctx.active_program != self:
232-
self.use()
232+
# if self._ctx.active_program != self:
233+
# self.use()
233234

234235
try:
235236
uniform = self._uniforms[key]
@@ -245,9 +246,9 @@ def use(self):
245246
"""
246247
# IMPORTANT: This is the only place glUseProgram should be called
247248
# so we can track active program.
248-
if self._ctx.active_program != self:
249-
gl.glUseProgram(self._glo)
250-
self._ctx.active_program = self
249+
# if self._ctx.active_program != self:
250+
gl.glUseProgram(self._glo)
251+
# self._ctx.active_program = self
251252

252253
def _setup_out_attributes(self):
253254
"""Set up transform feedback varyings"""

arcade/gl/uniform.py

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,53 +20,53 @@ class Uniform:
2020

2121
_uniform_setters = {
2222
# uniform type: (gl_type, setter, length, count)
23-
gl.GL_INT: (gl.GLint, gl.glUniform1iv, 1, 1),
24-
gl.GL_INT_VEC2: (gl.GLint, gl.glUniform2iv, 2, 1),
25-
gl.GL_INT_VEC3: (gl.GLint, gl.glUniform3iv, 3, 1),
26-
gl.GL_INT_VEC4: (gl.GLint, gl.glUniform4iv, 4, 1),
23+
gl.GL_INT: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
24+
gl.GL_INT_VEC2: (gl.GLint, gl.glProgramUniform2iv, 2, 1),
25+
gl.GL_INT_VEC3: (gl.GLint, gl.glProgramUniform3iv, 3, 1),
26+
gl.GL_INT_VEC4: (gl.GLint, gl.glProgramUniform4iv, 4, 1),
2727
gl.GL_BOOL: (gl.GLint, gl.glUniform1iv, 1, 1),
28-
gl.GL_BOOL_VEC2: (gl.GLint, gl.glUniform2iv, 2, 1),
29-
gl.GL_BOOL_VEC3: (gl.GLint, gl.glUniform3iv, 3, 1),
30-
gl.GL_BOOL_VEC4: (gl.GLint, gl.glUniform4iv, 4, 1),
31-
gl.GL_FLOAT: (gl.GLfloat, gl.glUniform1fv, 1, 1),
32-
gl.GL_FLOAT_VEC2: (gl.GLfloat, gl.glUniform2fv, 2, 1),
33-
gl.GL_FLOAT_VEC3: (gl.GLfloat, gl.glUniform3fv, 3, 1),
34-
gl.GL_FLOAT_VEC4: (gl.GLfloat, gl.glUniform4fv, 4, 1),
28+
gl.GL_BOOL_VEC2: (gl.GLint, gl.glProgramUniform2iv, 2, 1),
29+
gl.GL_BOOL_VEC3: (gl.GLint, gl.glProgramUniform3iv, 3, 1),
30+
gl.GL_BOOL_VEC4: (gl.GLint, gl.glProgramUniform4iv, 4, 1),
31+
gl.GL_FLOAT: (gl.GLfloat, gl.glProgramUniform1fv, 1, 1),
32+
gl.GL_FLOAT_VEC2: (gl.GLfloat, gl.glProgramUniform2fv, 2, 1),
33+
gl.GL_FLOAT_VEC3: (gl.GLfloat, gl.glProgramUniform3fv, 3, 1),
34+
gl.GL_FLOAT_VEC4: (gl.GLfloat, gl.glProgramUniform4fv, 4, 1),
3535
# 1D Samplers
36-
gl.GL_SAMPLER_1D: (gl.GLint, gl.glUniform1iv, 1, 1),
37-
gl.GL_INT_SAMPLER_1D: (gl.GLint, gl.glUniform1iv, 1, 1),
38-
gl.GL_UNSIGNED_INT_SAMPLER_1D: (gl.GLint, gl.glUniform1iv, 1, 1),
39-
gl.GL_TEXTURE_1D_ARRAY: (gl.GLint, gl.glUniform1iv, 1, 1),
36+
gl.GL_SAMPLER_1D: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
37+
gl.GL_INT_SAMPLER_1D: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
38+
gl.GL_UNSIGNED_INT_SAMPLER_1D: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
39+
gl.GL_TEXTURE_1D_ARRAY: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
4040
# 2D samplers
41-
gl.GL_SAMPLER_2D: (gl.GLint, gl.glUniform1iv, 1, 1),
42-
gl.GL_SAMPLER_2D_MULTISAMPLE: (gl.GLint, gl.glUniform1iv, 1, 1),
43-
gl.GL_INT_SAMPLER_2D: (gl.GLint, gl.glUniform1iv, 1, 1),
44-
gl.GL_UNSIGNED_INT_SAMPLER_2D: (gl.GLint, gl.glUniform1iv, 1, 1),
45-
gl.GL_TEXTURE_2D_MULTISAMPLE: (gl.GLint, gl.glUniform1iv, 1, 1),
41+
gl.GL_SAMPLER_2D: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
42+
gl.GL_SAMPLER_2D_MULTISAMPLE: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
43+
gl.GL_INT_SAMPLER_2D: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
44+
gl.GL_UNSIGNED_INT_SAMPLER_2D: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
45+
gl.GL_TEXTURE_2D_MULTISAMPLE: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
4646
# Array
47-
gl.GL_SAMPLER_2D_ARRAY: (gl.GLint, gl.glUniform1iv, 1, 1),
48-
gl.GL_TEXTURE_2D_MULTISAMPLE_ARRAY: (gl.GLint, gl.glUniform1iv, 1, 1),
47+
gl.GL_SAMPLER_2D_ARRAY: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
48+
gl.GL_TEXTURE_2D_MULTISAMPLE_ARRAY: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
4949
# 3D
50-
gl.GL_SAMPLER_3D: (gl.GLint, gl.glUniform1iv, 1, 1),
50+
gl.GL_SAMPLER_3D: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
5151
# Cube
52-
gl.GL_SAMPLER_CUBE: (gl.GLint, gl.glUniform1iv, 1, 1),
53-
gl.GL_TEXTURE_CUBE_MAP_ARRAY: (gl.GLint, gl.glUniform1iv, 1, 1),
52+
gl.GL_SAMPLER_CUBE: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
53+
gl.GL_TEXTURE_CUBE_MAP_ARRAY: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
5454
# Matrices
55-
gl.GL_FLOAT_MAT2: (gl.GLfloat, gl.glUniformMatrix2fv, 4, 1),
56-
gl.GL_FLOAT_MAT3: (gl.GLfloat, gl.glUniformMatrix3fv, 9, 1),
57-
gl.GL_FLOAT_MAT4: (gl.GLfloat, gl.glUniformMatrix4fv, 16, 1),
55+
gl.GL_FLOAT_MAT2: (gl.GLfloat, gl.glProgramUniformMatrix2fv, 4, 1),
56+
gl.GL_FLOAT_MAT3: (gl.GLfloat, gl.glProgramUniformMatrix3fv, 9, 1),
57+
gl.GL_FLOAT_MAT4: (gl.GLfloat, gl.glProgramUniformMatrix4fv, 16, 1),
5858
# Image (compute shader)
59-
gl.GL_IMAGE_1D: (gl.GLint, gl.glUniform1iv, 1, 1),
60-
gl.GL_IMAGE_2D: (gl.GLint, gl.glUniform1iv, 1, 1),
61-
gl.GL_IMAGE_2D_RECT: (gl.GLint, gl.glUniform1iv, 1, 1),
62-
gl.GL_IMAGE_3D: (gl.GLint, gl.glUniform1iv, 1, 1),
63-
gl.GL_IMAGE_CUBE: (gl.GLint, gl.glUniform1iv, 1, 1),
64-
gl.GL_IMAGE_1D_ARRAY: (gl.GLint, gl.glUniform1iv, 1, 1),
65-
gl.GL_IMAGE_2D_ARRAY: (gl.GLint, gl.glUniform1iv, 1, 1),
66-
gl.GL_IMAGE_CUBE_MAP_ARRAY: (gl.GLint, gl.glUniform1iv, 1, 1),
67-
gl.GL_IMAGE_2D_MULTISAMPLE: (gl.GLint, gl.glUniform1iv, 1, 1),
68-
gl.GL_IMAGE_2D_MULTISAMPLE_ARRAY: (gl.GLint, gl.glUniform1iv, 1, 1),
69-
gl.GL_IMAGE_BUFFER: (gl.GLint, gl.glUniform1iv, 1, 1),
59+
gl.GL_IMAGE_1D: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
60+
gl.GL_IMAGE_2D: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
61+
gl.GL_IMAGE_2D_RECT: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
62+
gl.GL_IMAGE_3D: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
63+
gl.GL_IMAGE_CUBE: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
64+
gl.GL_IMAGE_1D_ARRAY: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
65+
gl.GL_IMAGE_2D_ARRAY: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
66+
gl.GL_IMAGE_CUBE_MAP_ARRAY: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
67+
gl.GL_IMAGE_2D_MULTISAMPLE: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
68+
gl.GL_IMAGE_2D_MULTISAMPLE_ARRAY: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
69+
gl.GL_IMAGE_BUFFER: (gl.GLint, gl.glProgramUniform1iv, 1, 1),
7070

7171
# TODO: test/implement these:
7272
# gl.GL_FLOAT_MAT2x3: glUniformMatrix2x3fv,
@@ -137,21 +137,18 @@ def _setup_getters_and_setters(self):
137137
self._program_id, self._location, gl_getter, c_array, length
138138
)
139139
self.setter = Uniform._create_setter_func(
140-
self._location, gl_setter, c_array, length, self._array_length, count, ptr, is_matrix
140+
self._program_id, self._location, gl_setter, c_array, length, self._array_length, count, ptr, is_matrix
141141
)
142142

143143
@staticmethod
144144
def _create_getter_func(program_id, location, gl_getter, c_array, length):
145145
""" Create a function for getting/setting OpenGL data. """
146146
if length == 1:
147-
148147
def getter_func():
149148
""" Get single-element OpenGL uniform data. """
150149
gl_getter(program_id, location, c_array)
151150
return c_array[0]
152-
153151
else:
154-
155152
def getter_func():
156153
""" Get list of OpenGL uniform data. """
157154
gl_getter(program_id, location, c_array)
@@ -161,30 +158,26 @@ def getter_func():
161158

162159
@staticmethod
163160
def _create_setter_func(
164-
location, gl_setter, c_array, length, array_length, count, ptr, is_matrix
161+
program_id, location, gl_setter, c_array, length, array_length, count, ptr, is_matrix
165162
):
166163
""" Create setters for OpenGL data. """
167164
if is_matrix:
168-
169165
def setter_func(value): # type: ignore #conditional function variants must have identical signature
170166
""" Set OpenGL matrix uniform data. """
171167
c_array[:] = value
172-
gl_setter(location, array_length, gl.GL_FALSE, ptr)
168+
gl_setter(program_id, location, array_length, gl.GL_FALSE, ptr)
173169

174170
elif length == 1 and count == 1:
175-
176171
def setter_func(value): # type: ignore #conditional function variants must have identical signature
177172
""" Set OpenGL uniform data value. """
178173
c_array[0] = value
179-
gl_setter(location, array_length, ptr)
174+
gl_setter(program_id, location, array_length, ptr)
180175

181176
elif length > 1 and count == 1:
182-
183177
def setter_func(values): # type: ignore #conditional function variants must have identical signature
184178
""" Set list of OpenGL uniform data. """
185179
c_array[:] = values
186-
gl_setter(location, array_length, ptr)
187-
180+
gl_setter(program_id, location, array_length, ptr)
188181
else:
189182
raise NotImplementedError("Uniform type not yet supported.")
190183

arcade/gui/widgets.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -841,8 +841,7 @@ def rect(self, value):
841841

842842
def do_render(self, surface: Surface):
843843
self.prepare_render(surface)
844-
with surface.ctx.pyglet_rendering():
845-
self.label.draw()
844+
self.label.draw()
846845

847846

848847
class UITextArea(UIWidget):
@@ -937,8 +936,7 @@ def rect(self, value):
937936

938937
def do_render(self, surface: Surface):
939938
self.prepare_render(surface)
940-
with surface.ctx.pyglet_rendering():
941-
self.layout.draw()
939+
self.layout.draw()
942940

943941
def on_event(self, event: UIEvent) -> Optional[bool]:
944942
if isinstance(event, UIMouseScrollEvent):
@@ -1124,9 +1122,7 @@ def text(self, value):
11241122

11251123
def do_render(self, surface: Surface):
11261124
self.prepare_render(surface)
1127-
1128-
with surface.ctx.pyglet_rendering():
1129-
self.layout.draw()
1125+
self.layout.draw()
11301126

11311127

11321128
class UIFlatButton(UIInteractiveWidget):

arcade/text_pyglet.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,29 +87,26 @@ def _draw_label_with_rotation(label: pyglet.text.Label, rotation: float) -> None
8787
:param pyglet.text.Label label: a pyglet label to wrap and draw
8888
:param float rotation: rotate this many degrees from horizontal around anchor
8989
"""
90-
91-
# raw pyglet draw functions need this context helper inside arcade
9290
window = arcade.get_window()
93-
with window.ctx.pyglet_rendering():
94-
95-
# execute view matrix magic to rotate cleanly
96-
if rotation:
97-
angle_radians = math.radians(rotation)
98-
x = label.x
99-
y = label.y
100-
# Create a matrix translating the label to 0,0
101-
# then rotating it and then move it back
102-
r_view = Mat4.from_rotation(angle_radians, (0, 0, 1))
103-
t1_view = Mat4.from_translation((-x, -y, 0))
104-
t2_view = Mat4.from_translation((x, y, 0))
105-
final_view = t1_view @ r_view @ t2_view
106-
window.view = final_view
107-
108-
label.draw()
109-
110-
# Reset the view matrix
111-
if rotation:
112-
window.view = Mat4()
91+
92+
# execute view matrix magic to rotate cleanly
93+
if rotation:
94+
angle_radians = math.radians(rotation)
95+
x = label.x
96+
y = label.y
97+
# Create a matrix translating the label to 0,0
98+
# then rotating it and then move it back
99+
r_view = Mat4.from_rotation(angle_radians, (0, 0, 1))
100+
t1_view = Mat4.from_translation((-x, -y, 0))
101+
t2_view = Mat4.from_translation((x, y, 0))
102+
final_view = t1_view @ r_view @ t2_view
103+
window.view = final_view
104+
105+
label.draw()
106+
107+
# Reset the view matrix
108+
if rotation:
109+
window.view = Mat4()
113110

114111

115112
class Text:

tests/unit2/test_opengl_program.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def test_program_basic(ctx):
138138
assert program.ctx == ctx
139139
assert program.glo > 0
140140
program.use()
141-
assert ctx.active_program == program
141+
# assert ctx.active_program == program
142142
assert repr(program).startswith('<Program')
143143

144144
# TODO: Test all uniform types

0 commit comments

Comments
 (0)