Search code examples
metal

What does the qualifier/keyword "thread" mean at the end of a metal (MSL) function?


How can I understand what the keyword thread does in a metal shader language (MSL) function? I know that "all arguments to a graphics or kernel function that are a pointer or reference to a type must be declared with an address space attribute" and that thread is one of those adress spaces. thread refers to the per-thread memory address space and variables allocated in this adress space and not visible to other threads. Moreover, the reference says that "Variables declared inside a graphics or kernel function are allocated in the thread address space.". So far so good.

Now, what confuses me is that if you look at the signatures of the standard library, you will find that thread also appears at the end of member functions. For example, let's look a ray from the standard library (METAL_FUNC just means always inline):

struct ray
{
  METAL_FUNC ray(float3 origin = 0.0f, float3 direction = 0.0f, float min_distance = 0.0f, float max_distance = INFINITY) thread
      : origin(origin),
        direction(direction),
        min_distance(min_distance),
        max_distance(max_distance)
  {
  }
  METAL_FUNC ray(const thread ray &) thread = default;
  METAL_FUNC thread ray &operator=(const thread ray &) thread = default;

  float3 origin;
  float3 direction;
  float min_distance;
  float max_distance;
};

For instance on the default copy constructor

METAL_FUNC ray(const thread ray &) thread = default;

The parameter argument is a const reference to a ray (in the same calling thread, right?). What does thread at the end do? what happens if we omit it?

On the other hand, see the default copy assignment operator. Where does the returning ray reference live? is it in a different thread address space?


Solution

  • Metal supports additional overloading on address spaces of argument. it is specified after member functions for current object(this):

    enter image description here

    from 1.4.1 https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf