Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use device and when to use constant address space qualifier in metal shading language?

Tags:

swift

3d

metal

I know that device address space is used when indexing a buffer and constant address space is used when many invocations of the function will access the same portion of the buffer. But I am still not very clear. Thank you!

like image 263
Saumya Avatar asked Oct 29 '19 17:10

Saumya


1 Answers

Based on this Metal Shading Language Specification

device Address Space

The device address space name refers to buffer memory objects allocated from the device memory pool that are both readable and writeable. A buffer memory object can be declared as a pointer or reference to a scalar, vector or userdefined structure. In an app, Metal API calls allocate the memory for the buffer object, which determines the actual size of the buffer memory. Some examples are:

// An array of a float vector with four components.

device float4 *color;
struct Foo {
float a[3];
int b[2];
}
// An array of Foo elements.
device Foo *my_info;

Since you always allocate texture objects from the device address space, you do not need the device address attribute for texture types.

constant Address Space

The constant address space name refers to buffer memory objects allocated from the device memory pool but are read-only. Variables in program scope must be declared in the constant address space and initialized during the declaration statement. The initializer(s) expression must be a core constant expression. Variables in program scope have the same lifetime as the program, and their values persist between calls to any of the compute or graphics functions in the program.

constant float samples[] = { 1.0f, 2.0f, 3.0f, 4.0f };

Pointers or references to the constant address space are allowed as arguments to functions. Writing to variables declared in the constant address space is a compile-time error. Declaring such a variable without initialization is also a compile-time error. To decide which address space (device or constant) a read-only buffer passed to a graphics or kernel function uses, look at how the buffer is accessed inside the graphics or kernel function. The constant address space is optimized for multiple instances executing a graphics or kernel function accessing the same location in the buffer. Some examples of this access pattern are accessing light or material properties for lighting / shading, matrix of a matrix array used for skinning, filter weight accessed from a filter weight array for convolution. If multiple executing instances of a graphics or kernel function are accessing the buffer using an index such as the vertex ID, fragment coordinate, or the thread position in grid, the buffer must be allocated in the device address space.

like image 193
Hamid Yusifli Avatar answered Nov 15 '22 00:11

Hamid Yusifli