pygfx / pyshader

Write modern GPU shaders in Python!
BSD 2-Clause "Simplified" License
72 stars 1 forks source link

Implicit type conversions #17

Closed almarklein closed 4 years ago

almarklein commented 4 years ago

Pyhon implicitly converts ints to floats when one of the operands is a float, or when the op is division. Should we follow this approach or enforce strict types?

I think the former because it makes the code feel more Pythonic, and I don't think it will cause much confusion as long as we stick to only doing it with int/float.

Korijn commented 4 years ago

I'm not sure, implicit conversion in shaders smells like trouble... What's the use case for it? You have to be explicit about input and output types anyway right?

almarklein commented 4 years ago

The use case is being able to write e.g. n / 3 instead of f32(n) / 3.0 inside a shader.

Korijn commented 4 years ago

I see. Maybe I'm overthinking this :) I was mostly wondering how much control you would want to have over the generated spirv code in optimization scenarios, but maybe it's just not a very big deal

almarklein commented 4 years ago

Closing. I think the answer is no. SpirV is much more strictly typed than Python, and it's ok to "feel" that when writing a shader.

Plus it helps to argue about the types of your code if scalar literals comply with the types of other values in an operation:

...
b = a + 1   # I now know that b is an integer type,
            # otherwise the compiler would have complained