英文:
Where do shader "static" variables reside in memory?
问题
以下是翻译好的部分:
我想编写一个能够渲染2D纹理四边形到渲染目标的HLSL着色器。
我编写了以下顶点着色器:
struct VertexData
{
float2 position;
float2 uv;
};
// 两个2D三角形,覆盖整个渲染目标
static VertexData vertices[6] =
{
{float2(-1.0, 1.0), float2(0.0, 0.0)},
{float2(-1.0, -1.0), float2(0.0, 1.0)},
{float2(1.0, 1.0), float2(0.0, 1.0)},
{float2(1.0, 1.0), float2(0.0, 1.0)},
{float2(-1.0, -1.0), float2(0.0, 1.0)},
{float2(1.0, -1.0), float2(1.0, 1.0)}
};
struct VSOutput
{
float4 position : SV_POSITION; // 规范化设备坐标中的位置
[[vk::location(0)]] float2 uv : TEXCOORD0;
};
VSOutput main(uint vertexIndex : SV_VertexID)
{
VSOutput output = (VSOutput)0;
output.position = float4(vertices[vertexIndex].position, 0.0, 1.0);
output.uv = vertices[vertexIndex].uv;
return output;
}
我正在尝试理解这是否是“最佳”或“最简单”的方法。
具体来说:
vertices
数组的内存位于哪里?常量内存?全局内存?它是否被缓存?它是只读的吗?- 这是否是最佳方法?我应该使用常量缓冲区吗?据我所知,常量缓冲区被优化用于在着色器中进行统一访问,而这里并非如此。
英文:
I want to write an hlsl shader that renders a 2D textured quad onto a render target.
I wrote the following vertex shader:
struct VertexData
{
float2 position;
float2 uv;
};
// 2 2D triangles that cover the whole render target
static VertexData vertices[6] =
{
{float2(-1.0, 1.0), float2(0.0, 0.0)},
{float2(-1.0, -1.0), float2(0.0, 1.0)},
{float2(1.0, 1.0), float2(0.0, 1.0)},
{float2(1.0, 1.0), float2(0.0, 1.0)},
{float2(-1.0, -1.0), float2(0.0, 1.0)},
{float2(1.0, -1.0), float2(1.0, 1.0)}
};
struct VSOutput
{
float4 position : SV_POSITION; // position in normalized device coordinates
[[vk::location(0)]] float2 uv : TEXCOORD0;
};
VSOutput main(uint vertexIndex : SV_VertexID)
{
VSOutput output = (VSOutput)0;
output.position = float4(vertices[vertexIndex].position, 0.0, 1.0);
output.uv = vertices[vertexIndex].uv;
return output;
}
I am trying to understand whether this is the 'best' or 'simplest' approach.
Specifically:
- Where does the memory for the
vertices
array reside? constant memory? global memory? is it cached? is it read only? - Is this the optimal approach? Should I use a constant buffer? AFAIK constant buffers are optimized for uniform access across the shader. Which is not the case here.
答案1
得分: 2
关于顶点数组的内存分配取决于具体的实现。它存在于为满足static
声明所施加的要求而需要存在的地方。
它是只读的吗?
你没有声明它为const
,所以就语言而言,该数组是可以被写入的。编译器可以看到你实际上从未对其进行写入,因此它可以根据这些信息采取某些措施。但这不是必须的。
这是最优的方法吗?
关于什么方面的“最优”?声明数组为const
将为编译器提供更多信息,以便它能够更好地处理数组。但除此之外,你几乎无法再做更多事情。
英文:
> Where does the memory for the vertices array reside?
That is implementation-dependent. It lives where it needs to live to serve the requirements imposed by being declared as static
.
> is it read only?
You did not declare it const
, so as far as the language is concerned, the array can be written to. The compiler can see that you never actually write to it, so it could do something with that information. But it doesn't have to.
> Is this the optimal approach?
"Optimal" in terms of what? Declaring the array const
will give the compiler more information to do what it needs to with the array. But outside of that, there's really nothing more that you can do.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论