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
verticesarray 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.
That is implementation-dependent. It lives where it needs to live to serve the requirements imposed by being declared as
static.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."Optimal" in terms of what? Declaring the array
constwill 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.