pythonarcade / arcade

Easy to use Python library for creating 2D arcade games.
http://arcade.academy
Other
1.71k stars 327 forks source link

GL: Uniform setters don't accept objects supporting buffer protocol #2451

Open einarf opened 13 hours ago

einarf commented 13 hours ago

When using numpy arrays or glm this is extremely useful

The problem is very apparent when using glm since a glm matrix. We can convert it into a list or tuple but this creates a tuple/list for each column in the matrix. Just cut all that complexity and use buffer protocol.

# Set identity matrix
program["some_matrix"] = glm.mat4()

The current uniform setters are function based. It might be cleaner to just expose a Uniform object instead that can also be inspected.

# Setting python value directly
program["member"].value = value

# Supporting explicit buffer protocol write
program["member"].write(data)

# Progam can have __setitem__ like before that will forward the value to the right member
program["something"] = value

There's just more you can do when returning an instance. We can still do some hidden function magic in the background to optimize how different uniforms types are written.

It also means the user can query other useful information

# Get the slot the uniform is assigned to
program["my_uniform"].location
# Get the array length
program["my_uniform"].length
# Get the value of the uniform
program["my_uniform"].value

It can also query for the internal uniform type and other lower level things useful for debugging.

einarf commented 13 hours ago

We can do a temporary fix modifying the existing functions. Possibly a try/except and try to interpret the value as a buffer.