For the longest time I was thinking that whenever the buffer is writeable we need to declare it as device
, if read-only then constant
. But now I realized that in many code samples on the internet including the official resources it is often not that simple. It's obvious that device
would work everywhere where constant
works, but in many cases I see that people use device
on read-only buffers seemingly intentionally even going as far as declaring one buffer as const device
but using constant
on another buffers in the same kernel like so:
kernel void test
(
constant float4 *buffer0 [[buffer(0)]],
const device float4 *buffer1 [[buffer(1)]],
device float4 *out_buffer [[buffer(2)]],
)
{
// both buffer0 and buffer1 are read-only, why they possibly would need different address spaces???
}
The documentation is not clear enough for my understanding.
So the question is - in which cases it is better to use device
/const device
instead of constant
address space on read-only buffers?