File: d3d12_vertex_attr_zoo.cpp

package info (click to toggle)
renderdoc 1.24%2Bdfsg-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 105,156 kB
  • sloc: cpp: 759,405; ansic: 309,460; python: 26,606; xml: 22,599; java: 11,365; cs: 7,181; makefile: 6,707; yacc: 5,682; ruby: 4,648; perl: 3,461; sh: 2,354; php: 2,119; lisp: 1,835; javascript: 1,524; tcl: 1,068; ml: 747
file content (242 lines) | stat: -rw-r--r-- 7,032 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/******************************************************************************
 * The MIT License (MIT)
 *
 * Copyright (c) 2019-2022 Baldur Karlsson
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/

#include "d3d12_test.h"

RD_TEST(D3D12_Vertex_Attr_Zoo, D3D12GraphicsTest)
{
  static constexpr const char *Description =
      "Draws a triangle but using different kinds of vertex attributes, including doubles, arrays, "
      "matrices, and formats that require manual decode as they are vertex-buffer exclusive on "
      "some hardware such as USCALED.";

  struct vertin
  {
    int16_t i16[4];
    uint16_t u16[4];
    double df[2];
    float arr0[2];
    float arr1[2];
    float arr2[2];
    float mat0[2];
    float mat1[2];
  };

  std::string common = R"EOSHADER(

struct a2v
{
 float4 SNorm : SNORM;
 float4 UNorm : UNORM;
 uint4 UInt : UINT;
 float2 Array[3] : ARRAY;
 float2x2 Matrix : MATRIX;
};

struct v2f
{
  float4 pos : SV_Position;
  a2v data;
};

)EOSHADER";

  std::string vertex = R"EOSHADER(

v2f main(in a2v IN, in uint idx : SV_VertexID)
{
  float2 pos[3] = {float2(-0.5f, 0.5f), float2(0.0f, -0.5f), float2(0.5f, 0.5f)};

  v2f OUT = (v2f)0;
  OUT.pos = float4(pos[idx], 0.0f, 1.0f);
  OUT.data = IN;
  return OUT;
}

)EOSHADER";

  std::string pixel = R"EOSHADER(

float4 main(in v2f IN) : SV_Target0
{
  // check values came through correctly

  // SNorm should be in [-1, 1]
  if(any(clamp(IN.data.SNorm, -1.0, 1.0) != IN.data.SNorm))
    return float4(0.1f, 0, 0, 1);

  // UNorm should be in [0, 1]
  if(any(clamp(IN.data.UNorm, 0.0, 1.0) != IN.data.UNorm))
    return float4(0.2f, 0, 0, 1);

  // Similar for UInt
  if(IN.data.UInt.x > 65535 || IN.data.UInt.y > 65535 || IN.data.UInt.z > 65535 || IN.data.UInt.w > 65535)
    return float4(0.3f, 0, 0, 1);

  return float4(0, 1.0f, 0, 1);
}

)EOSHADER";

  std::string geom = R"EOSHADER(

[maxvertexcount(3)]
void main(triangle v2f input[3], inout TriangleStream<v2f> TriStream)
{
  for(int i = 0; i < 3; i++)
  {
    v2f output = input[i];
    output.pos = float4(output.pos.yx, 0.4f, 1.2f);
    TriStream.Append(output);
  }

  TriStream.RestartStrip();
}

)EOSHADER";

  int main()
  {
    // initialise, create window, create device, etc
    if(!Init())
      return 3;

    vertin triangle[] = {
        {
            {32767, -32768, 32767, -32767},
            {12345, 6789, 1234, 567},
            {9.8765432109, -5.6789012345},
            {1.0f, 2.0f},
            {3.0f, 4.0f},
            {5.0f, 6.0f},
            {7.0, 8.0f},
            {9.0f, 10.0f},
        },
        {
            {32766, -32766, 16000, -16000},
            {56, 7890, 123, 4567},
            {-7.89012345678, 6.54321098765},
            {11.0f, 12.0f},
            {13.0f, 14.0f},
            {15.0f, 16.0f},
            {17.0, 18.0f},
            {19.0f, 20.0f},
        },
        {
            {5, -5, 0, 0},
            {8765, 43210, 987, 65432},
            {0.1234567890123, 4.5678901234},
            {21.0f, 22.0f},
            {23.0f, 24.0f},
            {25.0f, 26.0f},
            {27.0, 28.0f},
            {29.0f, 30.0f},
        },
    };

    ID3DBlobPtr vsblob = Compile(common + vertex, "main", "vs_4_0");
    ID3DBlobPtr psblob = Compile(common + pixel, "main", "ps_4_0");
    ID3DBlobPtr gsblob = Compile(common + geom, "main", "gs_4_0");

    ID3D12ResourcePtr vb = MakeBuffer().Data(triangle);

    ID3D12RootSignaturePtr sig = MakeSig({});

    ID3D12PipelineStatePtr pso = MakePSO().RootSig(sig).VS(vsblob).PS(psblob).GS(gsblob).InputLayout({
        {
            "SNORM", 0, DXGI_FORMAT_R16G16B16A16_SNORM, 0, offsetof(vertin, i16),
            D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0,
        },
        {
            "UNORM", 0, DXGI_FORMAT_R16G16B16A16_UNORM, 0, offsetof(vertin, u16),
            D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0,
        },
        {
            "UINT", 0, DXGI_FORMAT_R16G16B16A16_UINT, 0, offsetof(vertin, u16),
            D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0,
        },
        {
            "ARRAY", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(vertin, arr0),
            D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0,
        },
        {
            "ARRAY", 1, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(vertin, arr1),
            D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0,
        },
        {
            "ARRAY", 2, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(vertin, arr2),
            D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0,
        },
        {
            "MATRIX", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(vertin, mat0),
            D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0,
        },
        {
            "MATRIX", 1, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(vertin, mat1),
            D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0,
        },
    });

    ResourceBarrier(vb, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);

    while(Running())
    {
      ID3D12GraphicsCommandListPtr cmd = GetCommandBuffer();

      Reset(cmd);

      ID3D12ResourcePtr bb = StartUsingBackbuffer(cmd, D3D12_RESOURCE_STATE_RENDER_TARGET);

      D3D12_CPU_DESCRIPTOR_HANDLE rtv =
          MakeRTV(bb).Format(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB).CreateCPU(0);

      ClearRenderTargetView(cmd, rtv, {0.2f, 0.2f, 0.2f, 1.0f});

      cmd->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

      IASetVertexBuffer(cmd, vb, sizeof(vertin), 0);
      cmd->SetPipelineState(pso);
      cmd->SetGraphicsRootSignature(sig);

      RSSetViewport(cmd, {0.0f, 0.0f, (float)screenWidth, (float)screenHeight, 0.0f, 1.0f});
      RSSetScissorRect(cmd, {0, 0, screenWidth, screenHeight});

      OMSetRenderTargets(cmd, {rtv}, {});

      cmd->DrawInstanced(3, 1, 0, 0);

      FinishUsingBackbuffer(cmd, D3D12_RESOURCE_STATE_RENDER_TARGET);

      cmd->Close();

      Submit({cmd});

      Present();
    }

    return 0;
  }
};

REGISTER_TEST();