Search code examples
c++metal

How to define struct field type in Metal Shading Language?


It is not clear how to define ref or ptr type of struct field?

struct uint128_t {
    uint64_t lo;
    uint64_t hi;
    
    device uint64_t& operator[](int i) {
        return (i == 0) ? lo : hi;
    }

    ...
}
Reference to type 'device uint64_t' (aka 'device unsigned long') could not bind to an lvalue of type 'uint64_t' (aka 'unsigned long')

Solution

  • You need to specify an address space for the function itself. Otherwise, you can't use address-space specific stuff.

    Here's the right definition:

    
    struct uint128_t {
        uint64_t lo;
        uint64_t hi;
    
        device uint64_t& operator[](int i) device {
            return (i == 0) ? lo : hi;
        }
    };