1

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?

simd
  • 1,779
  • 3
  • 17
  • 23
  • 1
    Does this answer your question? [what the purpose of declaring a variable with "const constant"?](https://stackoverflow.com/questions/59010429/what-the-purpose-of-declaring-a-variable-with-const-constant) – Hamid Yusifli Jul 31 '23 at 15:28
  • @HamidYusifli no, it only says that "you often treat device buffers as read-only (e.g., in most vertex functions)", but never explains why you do that. I don't remember if vertex functions specifically require the use of device buffers in read-only situations, but in my question I'm asking about the situations where you're free to choose device vs constant on your own, like in the case of kernel functions. – simd Jul 31 '23 at 15:41

0 Answers0