Skip to content

Commit 4232548

Browse files
authored
Uniform: Support float, int, int and double with buffer protocol (#2466)
* Uniform: Support float, int, int and double with buffer protocol * Fix import order
1 parent e243e7c commit 4232548

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

arcade/gl/uniform.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import struct
4-
from ctypes import POINTER, cast
4+
from ctypes import POINTER, c_double, c_float, c_int, c_uint, cast
55

66
from pyglet import gl
77

@@ -27,6 +27,13 @@ class Uniform:
2727
The array length of the uniform
2828
"""
2929

30+
_type_to_struct = {
31+
c_float: "f",
32+
c_int: "i",
33+
c_uint: "I",
34+
c_double: "d",
35+
}
36+
3037
_uniform_getters = {
3138
gl.GLint: gl.glGetUniformiv,
3239
gl.GLuint: gl.glGetUniformuiv,
@@ -234,6 +241,7 @@ def _setup_getters_and_setters(self):
234241
gl_program_setter,
235242
gl_setter,
236243
c_array,
244+
gl_type,
237245
length,
238246
self._array_length,
239247
count,
@@ -260,14 +268,16 @@ def getter_func2():
260268
else:
261269
return getter_func2
262270

263-
@staticmethod
271+
@classmethod
264272
def _create_setter_func(
273+
cls,
265274
ctx,
266275
program_id,
267276
location,
268277
gl_program_setter,
269278
gl_setter,
270279
c_array,
280+
gl_type,
271281
length,
272282
array_length,
273283
count,
@@ -284,7 +294,8 @@ def setter_func(value): # type: ignore #conditional function variants must have
284294
try:
285295
# FIXME: Configure the struct format on the uniform to support
286296
# other types than float
287-
c_array[:] = struct.unpack(f"{length}f", value)
297+
fmt = cls._type_to_struct[gl_type]
298+
c_array[:] = struct.unpack(f"{length}{fmt}", value)
288299
except Exception:
289300
c_array[:] = value
290301
gl_program_setter(program_id, location, array_length, gl.GL_FALSE, ptr)
@@ -324,7 +335,8 @@ def setter_func(values): # type: ignore #conditional function variants must hav
324335
try:
325336
# FIXME: Configure the struct format on the uniform to support
326337
# other types than float
327-
c_array[:] = struct.unpack(f"{length}f", values)
338+
fmt = cls._type_to_struct[gl_type]
339+
c_array[:] = struct.unpack(f"{length}{fmt}", values)
328340
except Exception:
329341
c_array[:] = values
330342

0 commit comments

Comments
 (0)