File: UboUnsizedArray.cpp

package info (click to toggle)
glslang 16.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 51,720 kB
  • sloc: cpp: 92,305; yacc: 4,320; sh: 603; python: 305; ansic: 94; javascript: 74; makefile: 17
file content (332 lines) | stat: -rw-r--r-- 11,714 bytes parent folder | download | duplicates (5)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright (C) 2025 NVIDIA Corporation
//
// 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 <gtest/gtest.h>

#include "TestFixture.h"

namespace glslangtest {
namespace {

class UboUnsizedArrayTest : public GlslangTest<::testing::Test> {
protected:
    // Helper function to compile shader and check for specific error message.
    bool compileShouldFailWith(const std::string& code, const std::string& expectedError,
                               EShLanguage stage = EShLangVertex)
    {
        glslang::TShader shader(stage);
        EShMessages controls = static_cast<EShMessages>(EShMsgDefault | EShMsgSpvRules | EShMsgVulkanRules);

        bool success = compile(&shader, code, "", controls);

        if (success) {
            // Compilation should have failed.
            return false;
        }

        std::string errorLog = shader.getInfoLog();
        return errorLog.find(expectedError) != std::string::npos;
    }
};

// Test that unsized arrays in uniform blocks work when extension is enabled.
TEST_F(UboUnsizedArrayTest, BasicFunctionality)
{
    const std::string code = R"(
        #version 450
        #extension GL_EXT_uniform_buffer_unsized_array : require
        
        layout(std140, binding=0) uniform DataBlock {
            float scale;
            float values[];  // unsized array as last member
        };
        
        void main() {
            gl_Position = vec4(values[0] * scale, 0.0, 0.0, 1.0);
        }
    )";

    glslang::TShader shader(EShLangVertex);
    EShMessages controls = static_cast<EShMessages>(EShMsgDefault | EShMsgSpvRules | EShMsgVulkanRules);
    EXPECT_TRUE(compile(&shader, code, "", controls));
}

// Test that unsized arrays work when extension is enabled.
TEST_F(UboUnsizedArrayTest, ExtensionRequired)
{
    const std::string code = R"(
        #version 450
        #extension GL_EXT_uniform_buffer_unsized_array : require
        
        layout(std140, binding=0) uniform DataBlock {
            float scale;
            float values[];  // Should work with extension
        };
        
        void main() {
            gl_Position = vec4(values[0] * scale, 0.0, 0.0, 1.0);
        }
    )";

    glslang::TShader shader(EShLangVertex);
    EShMessages controls = static_cast<EShMessages>(EShMsgDefault | EShMsgSpvRules | EShMsgVulkanRules);
    EXPECT_TRUE(compile(&shader, code, "", controls));
}

// Test that only the last member can be unsized.
TEST_F(UboUnsizedArrayTest, OnlyLastMemberCanBeUnsized)
{
    const std::string code = R"(
        #version 450
        #extension GL_EXT_uniform_buffer_unsized_array : require
        
        layout(std140, binding=0) uniform DataBlock {
            float scale;
            float values[];  // Last member - should work
        };
        
        void main() {
            gl_Position = vec4(values[0] * scale, 0.0, 0.0, 1.0);
        }
    )";

    glslang::TShader shader(EShLangVertex);
    EShMessages controls = static_cast<EShMessages>(EShMsgDefault | EShMsgSpvRules | EShMsgVulkanRules);
    EXPECT_TRUE(compile(&shader, code, "", controls));
}

// Test that .length() method fails on unsized arrays.
TEST_F(UboUnsizedArrayTest, LengthMethodNotAllowed)
{
    const std::string code = R"(
        #version 450
        #extension GL_EXT_uniform_buffer_unsized_array : require
        
        layout(std140, binding=0) uniform DataBlock {
            float scale;
            float values[];
        };
        
        layout(location = 0) out vec4 fragColor;
        
        void main() {
            int len = values.length();  // Should fail - length() not supported for unsized arrays in uniform blocks
            fragColor = vec4(len, 0.0, 0.0, 1.0);
        }
    )";

    EXPECT_TRUE(
        compileShouldFailWith(code, "array must be declared with a size before using this method", EShLangFragment));
}

// Test that function parameters cannot be unsized arrays from uniform blocks.
TEST_F(UboUnsizedArrayTest, FunctionParameterRestriction)
{
    const std::string code = R"(
        #version 450
        #extension GL_EXT_uniform_buffer_unsized_array : require
        
        layout(std140, binding=0) uniform DataBlock {
            float scale;
            float values[];
        };
        
        void processArray(float arr[10]) {
            // Process array
        }
        
        void main() {
            processArray(values);  // Should fail - cannot pass unsized arrays as function arguments
            gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
        }
    )";

    EXPECT_TRUE(compileShouldFailWith(code, "no matching overloaded function found"));
}

// Test negative constant indexing.
TEST_F(UboUnsizedArrayTest, NegativeIndexingNotAllowed)
{
    const std::string code = R"(
        #version 450
        #extension GL_EXT_uniform_buffer_unsized_array : require
        
        layout(std140, binding=0) uniform DataBlock {
            float scale;
            float values[];
        };
        
        void main() {
            float value = values[-1];  // Should fail
            gl_Position = vec4(value * scale, 0.0, 0.0, 1.0);
        }
    )";

    EXPECT_TRUE(compileShouldFailWith(code, "index out of range"));
}

// Test that different data types work correctly.
TEST_F(UboUnsizedArrayTest, MultipleDataTypes)
{
    const std::string code = R"(
        #version 450
        #extension GL_EXT_uniform_buffer_unsized_array : require
        
        layout(std140, binding=0) uniform FloatBlock {
            float scale;
            float floatValues[];
        };
        
        layout(std140, binding=1) uniform IntBlock {
            int count;
            int intValues[];
        };
        
        layout(std140, binding=2) uniform VecBlock {
            mat4 transform;
            vec4 vecValues[];
        };
        
        void main() {
            int baseIndex = gl_VertexIndex % 10;
            float value = floatValues[baseIndex] * scale;
            int ivalue = intValues[baseIndex] * count;
            vec4 vvalue = vecValues[baseIndex] * transform;
            
            gl_Position = vec4(value + float(ivalue), vvalue.xy, 1.0);
        }
    )";

    glslang::TShader shader(EShLangVertex);
    EShMessages controls = static_cast<EShMessages>(EShMsgDefault | EShMsgSpvRules | EShMsgVulkanRules);
    EXPECT_TRUE(compile(&shader, code, "", controls));
}

// Test that general integer expressions work for indexing.
TEST_F(UboUnsizedArrayTest, GeneralIntegerIndexing)
{
    const std::string code = R"(
        #version 450
        #extension GL_EXT_uniform_buffer_unsized_array : require
        
        layout(std140, binding=0) uniform DataBlock {
            float scale;
            float values[];
        };
        
        layout(std140, binding=1) uniform SizeInfo {
            int arraySize;
        };
        
        void main() {
            // Various forms of general integer expressions
            int baseIndex = gl_VertexIndex % arraySize;
            int offsetIndex = (baseIndex + 1) % arraySize;
            int computedIndex = min(baseIndex + offsetIndex, arraySize - 1);
            
            float result = values[baseIndex] + values[offsetIndex] + values[computedIndex];
            gl_Position = vec4(result * scale, 0.0, 0.0, 1.0);
        }
    )";

    glslang::TShader shader(EShLangVertex);
    EShMessages controls = static_cast<EShMessages>(EShMsgDefault | EShMsgSpvRules | EShMsgVulkanRules);
    EXPECT_TRUE(compile(&shader, code, "", controls));
}

// Test SPIR-V generation for unsized arrays in uniform blocks.
TEST_F(UboUnsizedArrayTest, SpvGeneration)
{
    const std::string code = R"(
        #version 450
        #extension GL_EXT_uniform_buffer_unsized_array : require
        
        layout(std140, binding=0) uniform DataBlock {
            float scale;
            float values[];
        };
        
        layout(std140, binding=1) uniform SizeBlock {
            int arraySize;
        };
        
        void main() {
            int index = gl_VertexIndex % arraySize;
            float value = values[index];
            gl_Position = vec4(value * scale, 0.0, 0.0, 1.0);
        }
    )";

    // Compile the shader.
    glslang::TShader shader(EShLangVertex);
    const char* shaderStrings[1] = {code.c_str()};
    shader.setStrings(shaderStrings, 1);

    // Set up compilation options.
    EShMessages messages = static_cast<EShMessages>(EShMsgSpvRules | EShMsgVulkanRules);
    shader.setEnvInput(glslang::EShSourceGlsl, EShLangVertex, glslang::EShClientVulkan, 450);
    shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_0);
    shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_0);

    // Compile.
    bool success = shader.parse(GetDefaultResources(), 450, false, messages);
    EXPECT_TRUE(success) << "Shader compilation failed: " << shader.getInfoLog();

    // Link and generate SPIR-V.
    glslang::TProgram program;
    program.addShader(&shader);
    success = program.link(messages);
    EXPECT_TRUE(success) << "Program linking failed: " << program.getInfoLog();

    // Generate SPIR-V.
    spv::SpvBuildLogger logger;
    std::vector<uint32_t> spirv;
    glslang::SpvOptions options;
    glslang::GlslangToSpv(*program.getIntermediate(EShLangVertex), spirv, &logger, &options);

    // Disassemble SPIR-V to text for easier checking.
    std::ostringstream disassembly_stream;
    spv::Disassemble(disassembly_stream, spirv);
    std::string spirvText = disassembly_stream.str();

    // Check for key SPIR-V elements that indicate successful compilation.
    // 1. SourceExtension for the extension
    EXPECT_TRUE(spirvText.find("SourceExtension") != std::string::npos)
        << "SPIR-V should contain SourceExtension for GL_EXT_uniform_buffer_unsized_array";

    // 2. TypeRuntimeArray for the unsized array
    EXPECT_TRUE(spirvText.find("TypeRuntimeArray") != std::string::npos)
        << "SPIR-V should contain TypeRuntimeArray for unsized arrays";

    // 3. Block decoration (for uniform blocks with runtime arrays)
    EXPECT_TRUE(spirvText.find("Block") != std::string::npos)
        << "SPIR-V should contain Block decoration for uniform blocks with runtime arrays";

    // 4. RuntimeDescriptorArrayEXT capability
    EXPECT_TRUE(spirvText.find("RuntimeDescriptorArrayEXT") != std::string::npos)
        << "SPIR-V should contain RuntimeDescriptorArrayEXT capability";

    // 5. SPV_EXT_descriptor_indexing extension
    EXPECT_TRUE(spirvText.find("SPV_EXT_descriptor_indexing") != std::string::npos)
        << "SPIR-V should contain SPV_EXT_descriptor_indexing extension";
}

} // anonymous namespace
} // namespace glslangtest