1

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.
Ahmed Sbai
  • 10,695
  • 9
  • 19
  • 38
Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37
  • 1
    In directx the constants will be placed in temporary registers. In Vulcan it's likely the same, but not 100% sure. [dcl_indexableTemp](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dcl-indexabletemp) – viruseg Feb 08 '23 at 16:49
  • 1
    And if the const keyword is added, the array will be placed in an immediate-constant buffer. [dcl_immediateConstantBuffer](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dcl-immediateconstantbuffer) – viruseg Feb 08 '23 at 16:58

1 Answers1

2

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • so where will it be placed by most common implementations then? optimal in terms of performance (vs other approaches) – Elad Maimoni Feb 08 '23 at 16:54
  • @EladMaimoni: "*so where will it be placed by most common implementations then?*" Different implementations have different pools of memory and storage. They don't all share the same architecture. They'll go where the hardware needs them to go to do what you asked the hardware to do. So just be clear about what you ask the hardware for and let the implementation do its job. – Nicol Bolas Feb 08 '23 at 16:55