Skip to content

Commit

Permalink
Uniform: Support float, int, int and double with buffer protocol (#2466)
Browse files Browse the repository at this point in the history
* Uniform: Support float, int, int and double with buffer protocol

* Fix import order
  • Loading branch information
einarf authored Nov 24, 2024
1 parent e243e7c commit 4232548
Showing 1 changed file with 16 additions and 4 deletions.
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

0 comments on commit 4232548

Please sign in to comment.