aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
blob: 01f8d4f97b092120d17b8ffac5b196fb85c46997 (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//=== LexHLSLRootSignatureTest.cpp - Lex Root Signature tests -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/Lex/LexHLSLRootSignature.h"
#include "gtest/gtest.h"

using namespace clang;
using TokenKind = hlsl::RootSignatureToken::Kind;

namespace {

// The test fixture.
class LexHLSLRootSignatureTest : public ::testing::Test {
protected:
  LexHLSLRootSignatureTest() {}

  void checkTokens(hlsl::RootSignatureLexer &Lexer,
                   SmallVector<hlsl::RootSignatureToken> &Computed,
                   SmallVector<TokenKind> &Expected) {
    for (unsigned I = 0, E = Expected.size(); I != E; ++I) {
      // Skip these to help with the macro generated test
      if (Expected[I] == TokenKind::invalid ||
          Expected[I] == TokenKind::end_of_stream)
        continue;
      hlsl::RootSignatureToken Result = Lexer.consumeToken();
      ASSERT_EQ(Result.TokKind, Expected[I]);
      Computed.push_back(Result);
    }
    hlsl::RootSignatureToken EndOfStream = Lexer.consumeToken();
    ASSERT_EQ(EndOfStream.TokKind, TokenKind::end_of_stream);
    ASSERT_TRUE(Lexer.isEndOfBuffer());
  }
};

// Lexing Tests

TEST_F(LexHLSLRootSignatureTest, ValidLexNumbersTest) {
  // This test will check that we can lex different number tokens
  const llvm::StringLiteral Source = R"cc(
    -42 42 +42 +2147483648
    42. 4.2 .42
    42f 4.2F
    .42e+3 4.2E-12
    42.e+10f
  )cc";

  hlsl::RootSignatureLexer Lexer(Source);

  SmallVector<hlsl::RootSignatureToken> Tokens;
  SmallVector<TokenKind> Expected = {
      TokenKind::pu_minus,      TokenKind::int_literal,
      TokenKind::int_literal,   TokenKind::pu_plus,
      TokenKind::int_literal,   TokenKind::pu_plus,
      TokenKind::int_literal,   TokenKind::float_literal,
      TokenKind::float_literal, TokenKind::float_literal,
      TokenKind::float_literal, TokenKind::float_literal,
      TokenKind::float_literal, TokenKind::float_literal,
      TokenKind::float_literal,
  };
  checkTokens(Lexer, Tokens, Expected);

  // Sample negative: int component
  hlsl::RootSignatureToken IntToken = Tokens[1];
  ASSERT_EQ(IntToken.NumSpelling, "42");

  // Sample unsigned int
  IntToken = Tokens[2];
  ASSERT_EQ(IntToken.NumSpelling, "42");

  // Sample positive: int component
  IntToken = Tokens[4];
  ASSERT_EQ(IntToken.NumSpelling, "42");

  // Sample positive int that would overflow the signed representation but
  // is treated as an unsigned integer instead
  IntToken = Tokens[6];
  ASSERT_EQ(IntToken.NumSpelling, "2147483648");

  // Sample decimal end
  hlsl::RootSignatureToken FloatToken = Tokens[7];
  ASSERT_EQ(FloatToken.NumSpelling, "42.");

  // Sample decimal middle
  FloatToken = Tokens[8];
  ASSERT_EQ(FloatToken.NumSpelling, "4.2");

  // Sample decimal start
  FloatToken = Tokens[9];
  ASSERT_EQ(FloatToken.NumSpelling, ".42");

  // Sample float lower
  FloatToken = Tokens[10];
  ASSERT_EQ(FloatToken.NumSpelling, "42f");

  // Sample float upper
  FloatToken = Tokens[11];
  ASSERT_EQ(FloatToken.NumSpelling, "4.2F");

  // Sample exp +
  FloatToken = Tokens[12];
  ASSERT_EQ(FloatToken.NumSpelling, ".42e+3");

  // Sample exp -
  FloatToken = Tokens[13];
  ASSERT_EQ(FloatToken.NumSpelling, "4.2E-12");

  // Sample all combined
  FloatToken = Tokens[14];
  ASSERT_EQ(FloatToken.NumSpelling, "42.e+10f");
}

TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
  // This test will check that we can lex all defined tokens as defined in
  // HLSLRootSignatureTokenKinds.def, plus some additional integer variations
  const llvm::StringLiteral Source = R"cc(
    42 42.0f

    b0 t43 u987 s234

    (),|=+-

    RootSignature

    RootFlags DescriptorTable RootConstants StaticSampler

    num32BitConstants

    CBV SRV UAV Sampler
    space visibility flags
    numDescriptors offset

    filter mipLODBias addressU addressV addressW
    maxAnisotropy comparisonFunc borderColor
    minLOD maxLOD

    unbounded
    DESCRIPTOR_RANGE_OFFSET_APPEND

    allow_input_assembler_input_layout
    deny_vertex_shader_root_access
    deny_hull_shader_root_access
    deny_domain_shader_root_access
    deny_geometry_shader_root_access
    deny_pixel_shader_root_access
    deny_amplification_shader_root_access
    deny_mesh_shader_root_access
    allow_stream_output
    local_root_signature
    cbv_srv_uav_heap_directly_indexed
    sampler_heap_directly_indexed

    DATA_VOLATILE
    DATA_STATIC_WHILE_SET_AT_EXECUTE
    DATA_STATIC
    DESCRIPTORS_VOLATILE
    DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS

    shader_visibility_all
    shader_visibility_vertex
    shader_visibility_hull
    shader_visibility_domain
    shader_visibility_geometry
    shader_visibility_pixel
    shader_visibility_amplification
    shader_visibility_mesh

    FILTER_MIN_MAG_MIP_POINT
    FILTER_MIN_MAG_POINT_MIP_LINEAR
    FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT
    FILTER_MIN_POINT_MAG_MIP_LINEAR
    FILTER_MIN_LINEAR_MAG_MIP_POINT
    FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR
    FILTER_MIN_MAG_LINEAR_MIP_POINT
    FILTER_MIN_MAG_MIP_LINEAR
    FILTER_ANISOTROPIC
    FILTER_COMPARISON_MIN_MAG_MIP_POINT
    FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR
    FILTER_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT
    FILTER_COMPARISON_MIN_POINT_MAG_MIP_LINEAR
    FILTER_COMPARISON_MIN_LINEAR_MAG_MIP_POINT
    FILTER_COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR
    FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT
    FILTER_COMPARISON_MIN_MAG_MIP_LINEAR
    FILTER_COMPARISON_ANISOTROPIC
    FILTER_MINIMUM_MIN_MAG_MIP_POINT
    FILTER_MINIMUM_MIN_MAG_POINT_MIP_LINEAR
    FILTER_MINIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT
    FILTER_MINIMUM_MIN_POINT_MAG_MIP_LINEAR
    FILTER_MINIMUM_MIN_LINEAR_MAG_MIP_POINT
    FILTER_MINIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR
    FILTER_MINIMUM_MIN_MAG_LINEAR_MIP_POINT
    FILTER_MINIMUM_MIN_MAG_MIP_LINEAR
    FILTER_MINIMUM_ANISOTROPIC
    FILTER_MAXIMUM_MIN_MAG_MIP_POINT
    FILTER_MAXIMUM_MIN_MAG_POINT_MIP_LINEAR
    FILTER_MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT
    FILTER_MAXIMUM_MIN_POINT_MAG_MIP_LINEAR
    FILTER_MAXIMUM_MIN_LINEAR_MAG_MIP_POINT
    FILTER_MAXIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR
    FILTER_MAXIMUM_MIN_MAG_LINEAR_MIP_POINT
    FILTER_MAXIMUM_MIN_MAG_MIP_LINEAR
    FILTER_MAXIMUM_ANISOTROPIC

    TEXTURE_ADDRESS_WRAP
    TEXTURE_ADDRESS_MIRROR
    TEXTURE_ADDRESS_CLAMP
    TEXTURE_ADDRESS_BORDER
    TEXTURE_ADDRESS_MIRRORONCE

    comparison_never
    comparison_less
    comparison_equal
    comparison_less_equal
    comparison_greater
    comparison_not_equal
    comparison_greater_equal
    comparison_always

    STATIC_BORDER_COLOR_TRANSPARENT_BLACK
    STATIC_BORDER_COLOR_OPAQUE_BLACK
    STATIC_BORDER_COLOR_OPAQUE_WHITE
    STATIC_BORDER_COLOR_OPAQUE_BLACK_UINT
    STATIC_BORDER_COLOR_OPAQUE_WHITE_UINT
  )cc";
  hlsl::RootSignatureLexer Lexer(Source);

  SmallVector<hlsl::RootSignatureToken> Tokens;
  SmallVector<TokenKind> Expected = {
#define TOK(NAME, SPELLING) TokenKind::NAME,
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
  };

  checkTokens(Lexer, Tokens, Expected);
}

TEST_F(LexHLSLRootSignatureTest, ValidCaseInsensitiveKeywordsTest) {
  // This test will check that we can lex keywords in an case-insensitive
  // manner
  const llvm::StringLiteral Source = R"cc(
    DeScRiPtOrTaBlE

    CBV srv UAV sampler
    SPACE visibility FLAGS
    numDescriptors OFFSET
  )cc";
  hlsl::RootSignatureLexer Lexer(Source);

  SmallVector<hlsl::RootSignatureToken> Tokens;
  SmallVector<TokenKind> Expected = {
      TokenKind::kw_DescriptorTable,
      TokenKind::kw_CBV,
      TokenKind::kw_SRV,
      TokenKind::kw_UAV,
      TokenKind::kw_Sampler,
      TokenKind::kw_space,
      TokenKind::kw_visibility,
      TokenKind::kw_flags,
      TokenKind::kw_numDescriptors,
      TokenKind::kw_offset,
  };

  checkTokens(Lexer, Tokens, Expected);
}

TEST_F(LexHLSLRootSignatureTest, ValidLexPeekTest) {
  // This test will check that we the peek api is correctly used
  const llvm::StringLiteral Source = R"cc(
    )1
  )cc";
  hlsl::RootSignatureLexer Lexer(Source);

  // Test basic peek
  hlsl::RootSignatureToken Res = Lexer.peekNextToken();
  ASSERT_EQ(Res.TokKind, TokenKind::pu_r_paren);

  // Ensure it doesn't peek past one element
  Res = Lexer.peekNextToken();
  ASSERT_EQ(Res.TokKind, TokenKind::pu_r_paren);

  Res = Lexer.consumeToken();
  ASSERT_EQ(Res.TokKind, TokenKind::pu_r_paren);

  // Invoke after reseting the NextToken
  Res = Lexer.peekNextToken();
  ASSERT_EQ(Res.TokKind, TokenKind::int_literal);

  // Ensure we can still consume the second token
  Res = Lexer.consumeToken();
  ASSERT_EQ(Res.TokKind, TokenKind::int_literal);

  // Ensure end of stream token
  Res = Lexer.peekNextToken();
  ASSERT_EQ(Res.TokKind, TokenKind::end_of_stream);
}

} // anonymous namespace