Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions arcade/gl/uniform.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import struct
from ctypes import POINTER, cast
from ctypes import POINTER, c_double, c_float, c_int, c_uint, cast

from pyglet import gl

Expand All @@ -27,6 +27,13 @@ class Uniform:
The array length of the uniform
"""

_type_to_struct = {
c_float: "f",
c_int: "i",
c_uint: "I",
c_double: "d",
}

_uniform_getters = {
gl.GLint: gl.glGetUniformiv,
gl.GLuint: gl.glGetUniformuiv,
Expand Down Expand Up @@ -234,6 +241,7 @@ def _setup_getters_and_setters(self):
gl_program_setter,
gl_setter,
c_array,
gl_type,
length,
self._array_length,
count,
Expand All @@ -260,14 +268,16 @@ def getter_func2():
else:
return getter_func2

@staticmethod
@classmethod
def _create_setter_func(
cls,
ctx,
program_id,
location,
gl_program_setter,
gl_setter,
c_array,
gl_type,
length,
array_length,
count,
Expand All @@ -284,7 +294,8 @@ def setter_func(value): # type: ignore #conditional function variants must have
try:
# FIXME: Configure the struct format on the uniform to support
# other types than float
c_array[:] = struct.unpack(f"{length}f", value)
fmt = cls._type_to_struct[gl_type]
c_array[:] = struct.unpack(f"{length}{fmt}", value)
except Exception:
c_array[:] = value
gl_program_setter(program_id, location, array_length, gl.GL_FALSE, ptr)
Expand Down Expand Up @@ -324,7 +335,8 @@ def setter_func(values): # type: ignore #conditional function variants must hav
try:
# FIXME: Configure the struct format on the uniform to support
# other types than float
c_array[:] = struct.unpack(f"{length}f", values)
fmt = cls._type_to_struct[gl_type]
c_array[:] = struct.unpack(f"{length}{fmt}", values)
except Exception:
c_array[:] = values

Expand Down
Loading