Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variable in Metal Shading Language

Tags:

macos

metal

I want to use mouse to control my camera position. The idea is to have a global variable float3 pos that carry the current camera position so that the next update will be from the pos itself.

When I declare a global variable in Metal:

float3  pos;

and get this error:

Global variable must have a constant address space qualifier.

But if I add constant, it become read only thus I cannot update my current position.

An alternative I can think of is using struct. But that seems a bit over do.

What is the best way to do this?

like image 340
sooon Avatar asked Nov 17 '25 15:11

sooon


1 Answers

Indeed, only read-only variables (constants) can be defined as globals in MSL. You could do this by sending the mouse coordinates from the host (API) code via a buffer, to your shader. In there update the value of these coordinates and then update the buffer so the host code sees your updated values in real time. Here is a playground that shows you the mouse coordinates in the console as you click inside the rendered area.

like image 146
gpu3d Avatar answered Nov 21 '25 09:11

gpu3d