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:
vertices
array reside? constant memory? global memory? is it cached? is it read only?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.