❔ Generic method that takes in a struct and calls the correctly-typed method
I'm working with OpenTK (an OpenGL binding) and I'd like to create a method that allows setting uniforms (basically variables inside of a shader).
The solution I've come up with is the following. For context,
Matrix4
is a struct
. This method is part of my Shader
class, in which I'm trying to abstract the native types.
The cast (Matrix4)value
doesn't work though. I wrote it like this so that I can write a single method to set any type of uniform value, here the only implemented one is Matrix4
.
My question is : Is there a way to make this work, or do I have to write a method for each type of argument?
If that's a bad solution, what would be a good one?
(Alternatively, I could abstract uniforms further with an Uniform
class so I'm not directly working with the native types in my shader abstraction, but that would also require adding an abstraction for the uniform use cases.)11 Replies
If you want to use this approach then you could use
if (value is Matrix4 matrix4)
instead of typeof(T) == typeof(Matrix4)
Otherwise, if you want different cases for different types, then personally I'd just have an overload for each typeIt still complains that
cannot convert from 'ref T' to 'ref OpenTK.Mathematics.Matrix4'
. I think I'll go with the overload approach though, somehow it didn't cross my mind. Thank you :)well first you need to put ref on the argument
try
That I did, that's what caused the error I sent
tested it with an int and it works
Mhh, weird
Ah wait I didn't set the if like you though, that was the problem
Thanks
you should use
Unsafe.As
here
ah nvm
if the value were passed by ref, then you would use thatThis boxes the value. I would not except my value to be boxed when just calling OpenGL API
it actually doesn't box it
it shouldn't at least
it's treated as a static brach by the jit
Yeah you're right
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.