用"const constant"声明变量的目的是什么? [英] what the purpose of declaring a variable with "const constant"?
问题描述
在Metal shader中,声明像const constant Vertex *vertexArray [[buffer(0)]]
这样的变量的目的是什么(我指的是const constant
)?为什么仅靠常数还不够?另外,constant
和const
有什么区别?
In Metal shader, What is the purpose of declaring a variable like const constant Vertex *vertexArray [[buffer(0)]]
(with const constant
I mean)? why constant alone is not enough? also, what is the difference between constant
and const
?
以同样的方式const device
和constant
有什么区别?
Also in the same way what is the difference between const device
and constant
?
推荐答案
const
是类型限定符. constant
和device
是地址空间.
const
阻止您修改其适用的内容:
const
prevents you from modifying the thing to which it applies:
int a = 15;
a = 16; // Fine; reassigning to a non-const variable
const int b = 15;
b = a; // Error: attempt to write to a read-only constant
int d = 18;
int const* c = &a;
*c = 17; // Error: attempt to write value through a pointer to const int
c = &d; // Fine; reassigning (a different address) to a (non-const) pointer
int *const e = &d;
*e = a; // Fine; assigning to pointee through pointer to non-const int
e = c; // Error: attempt to reassign const pointer
希望这些示例充分说明了变量和常量的语义,以及在使用指针的情况下规则如何工作.
Hopefully these examples adequately illustrate the semantics of variables and constants, and how the rules work in the case of pointers.
在Metal中,指针始终位于特定的地址空间中.如果您在Metal着色器函数中获取具有自动存储的变量的地址(即本地"变量),则该指针位于 thread 地址空间中.另一方面,缓冲区参数始终位于恒定或设备地址空间中.
In Metal, pointers always reside in a particular address space. If you take the address of a variable with automatic storage in a Metal shader function (i.e. a "local" variable), that pointer is in the thread address space. Buffer parameters, on the other hand, are always in the constant or device address space.
device
缓冲区用于保存其元素将被大致访问一次的内存,就像在顶点函数中按顺序获取顶点数据时所执行的操作一样.另一方面,constant
缓冲区保存的数据可能会被函数的许多调用访问,就像统一数据一样.
device
buffers are used to hold memory whose elements will be accessed roughly once, as you might do when fetching vertex data sequentially in a vertex function. On the other hand, constant
buffers hold data that might be accessed by many invocations of a function, as with uniform data.
您不能在constant
地址空间中写入缓冲区.这是此答案中最重要的一句话: constant
地址空间中的所有指针都是隐式const限定的.
You cannot write to a buffer in the constant
address space. Here's the most important sentence in this answer: All pointers in the constant
address space are implicitly const-qualified.
您可以在恒定地址空间中形成新的指针,并且根据上述规则,您可以重新分配它们.但是尝试写给他们的对象将产生编译器错误.
You can form new pointers in the constant address space, and by the rules explained above, you can reassign them. But attempting to write to their pointee will produce a compiler error.
假设您使用以下参数编写一个片段函数:
Suppose you write a fragment function with the following parameter:
constant Light *lights [[buffer(0)]]
然后在函数主体中可以这样说:
Then in the function body you could say this:
constant Light *light = &lights[0];
这:
light = &lights[1];
但不是这样:
light->color = float4(1, 1, 1, 1); // Error: attempt to write to variable with const-qualified type "const constant Light *"
同样,请注意,在最后一个示例中,即使我们没有说常量指针应该是指向const的指针,也是如此.因此,用const
(在星号之前)进一步限定constant
指针是多余的.
Again, note that in this last example, even though we didn't say that the constant pointer should be a pointer-to-const, it is. For this reason, further qualifying a constant
pointer with const
(before the asterisk) is redundant.
现在让我们谈谈device
指针.
Now let's talk a bit about device
pointers.
与始终是只读的constant
缓冲区相反,在许多情况下都可以写入device
缓冲区.但是,您通常会将设备缓冲区视为只读(例如,在大多数顶点函数中).为了向编译器表明此意图,可以将const
添加到设备缓冲区指针参数.这将防止您无意中写入只打算读取的缓冲区.如果在不适当的上下文中使用device
指向非常量类型的指针,则Metal着色器编译器的最新版本会发出警告,这就是为什么养成为此类参数编写const device
的习惯通常是一个好主意的原因.
In contrast to constant
buffers, which are always read-only, it is possible in many contexts to write to device
buffers. However, you often treat device buffers as read-only (e.g., in most vertex functions). To indicate this intent to the compiler, you can add const
to a device buffer pointer parameter. This will prevent you from inadvertently writing to a buffer you are only intending to read. Recent versions of the Metal shader compiler emit a warning if you take a device
pointer to non-const type in an inappropriate context, which is why it's generally a good idea to get in the habit of writing const device
for such parameters.
但是写const constant
是多余的,而且从不需要.
But writing const constant
is redundant and never necessary.
这篇关于用"const constant"声明变量的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!