From 42325488695c65447f9674f023af179bf6d343ba Mon Sep 17 00:00:00 2001 From: Einar Forselv Date: Sun, 24 Nov 2024 18:48:26 +0100 Subject: [PATCH] Uniform: Support float, int, int and double with buffer protocol (#2466) * Uniform: Support float, int, int and double with buffer protocol * Fix import order --- arcade/gl/uniform.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/arcade/gl/uniform.py b/arcade/gl/uniform.py index 3fca202cd..6ca4af747 100644 --- a/arcade/gl/uniform.py +++ b/arcade/gl/uniform.py @@ -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 @@ -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, @@ -234,6 +241,7 @@ def _setup_getters_and_setters(self): gl_program_setter, gl_setter, c_array, + gl_type, length, self._array_length, count, @@ -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, @@ -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) @@ -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