File: TestWGSLAPI.h

package info (click to toggle)
wpewebkit 2.50.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 438,176 kB
  • sloc: cpp: 3,776,128; javascript: 197,881; ansic: 156,930; python: 49,118; asm: 21,987; ruby: 18,540; perl: 16,723; xml: 4,623; yacc: 2,360; sh: 2,096; java: 2,019; lex: 1,327; pascal: 366; makefile: 90
file content (174 lines) | stat: -rw-r--r-- 6,028 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2022 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include "WGSLShaderModule.h"
#include <JavaScriptCore/RegularExpression.h>
#include <wtf/FileSystem.h>
#include <wtf/text/MakeString.h>

#ifdef __OBJC__
#import <Metal/Metal.h>
#endif

#define EXPECT_SHADER(shader) \
    do { \
        if (!shader.has_value()) { \
            ::TestWGSLAPI::logCompilationError(shader.error()); \
            return; \
        } \
    } while (false)

namespace WGSL {
struct FailedCheck;
}

namespace TestWGSLAPI {

void logCompilationError(WGSL::FailedCheck& error);

inline String fn(const String& test)
{
    return makeString("@compute @workgroup_size(1) fn test() {\n"_s, test, "\n}"_s);
}

inline String enableF16(const String& test)
{
    return makeString("enable f16;\n\n"_s, test);
}

#ifdef __OBJC__
inline String file(const String& filePath)
{
    static NSString *shadersPath = nil;
    static std::once_flag onceFlag;
    std::call_once(onceFlag, [&] {
        shadersPath = [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:@"shaders"];
    });

    auto cfFilePath = filePath.createCFString();
    EXPECT_TRUE(cfFilePath);
    auto path = [shadersPath stringByAppendingPathComponent:(__bridge NSString *)cfFilePath.get()];
    auto readResult = FileSystem::readEntireFile(path);
    EXPECT_TRUE(readResult.has_value());
    return String::fromUTF8WithLatin1Fallback(readResult->span());
}
#endif

using Check = Function<unsigned(const String&, unsigned offset)>;

inline Check checkLiteral(const String& pattern)
{
    return [&](const String& msl, unsigned offset) -> unsigned {
        auto result = msl.find(pattern, offset);
        EXPECT_TRUE(result != notFound);
        return result;
    };
}

inline Check checkNotLiteral(const String& pattern)
{
    return [&](const String& msl, unsigned offset) -> unsigned {
        auto result = msl.find(pattern, offset);
        EXPECT_TRUE(result == notFound);
        return offset;
    };
}

inline Check checkNot(const String& pattern)
{
    return [&](const String& msl, unsigned offset) -> unsigned {
        JSC::Yarr::RegularExpression test(pattern);
        auto result = test.match(msl, offset);
        EXPECT_EQ(result, -1);
        return offset;
    };
}

inline Check check(const String& pattern)
{
    return [&](const String& msl, unsigned offset) -> unsigned {
        JSC::Yarr::RegularExpression test(pattern);
        auto result = test.match(msl, offset);
        EXPECT_NE(result, -1);
        return result;
    };
}

inline Variant<WGSL::SuccessfulCheck, WGSL::FailedCheck> staticCheck(const String& wgsl)
{
    return WGSL::staticCheck(wgsl, std::nullopt, { 8 });
}

inline Variant<WGSL::PrepareResult, WGSL::Error> prepare(const WGSL::SuccessfulCheck& staticCheckResult)
{
    auto& shaderModule = staticCheckResult.ast;
    HashMap<String, WGSL::PipelineLayout*> pipelineLayouts;
    for (auto& entryPoint : shaderModule->callGraph().entrypoints())
        pipelineLayouts.add(entryPoint.originalName, nullptr);
    return WGSL::prepare(shaderModule, pipelineLayouts);
}

inline Variant<String, WGSL::Error> generate(const WGSL::SuccessfulCheck& staticCheckResult, WGSL::PrepareResult& prepareResult)
{
    auto& shaderModule = staticCheckResult.ast;
    HashMap<String, WGSL::ConstantValue> constantValues;
    for (auto& entryPoint : shaderModule->callGraph().entrypoints()) {
        const auto& entryPointInformation = prepareResult.entryPoints.get(entryPoint.originalName);
        for (const auto& [originalName, constant] : entryPointInformation.specializationConstants) {
            EXPECT_TRUE(constant.defaultValue);
            auto defaultValue = WGSL::evaluate(*constant.defaultValue, constantValues);
            EXPECT_TRUE(defaultValue.has_value());
            constantValues.add(constant.mangledName, *defaultValue);
        }
    }

    return WGSL::generate(shaderModule, prepareResult, constantValues, { });
}

#ifdef __OBJC__
inline Variant<id<MTLLibrary>, NSError *> metalCompile(const String& msl)
{
    auto device = MTLCreateSystemDefaultDevice();
    auto options = [MTLCompileOptions new];
    options.preprocessorMacros = @{ @"__wgslMetalAppleGPUFamily" : [NSString stringWithFormat:@"%u", 8] };
    NSError *error = nil;
    id<MTLLibrary> library = [device newLibraryWithSource:msl.createNSString().get() options:options error:&error];
    if (error != nil)
        return { error };
    return { library };
}
#endif

template<typename... Checks>
inline void performChecks(const String& input, Checks&&... checks)
{
    unsigned offset = 0;
    for (const auto& check : std::initializer_list<Check> { std::forward<Checks>(checks)... })
        offset = check(input, offset);
}

} // namespace TestWGSLAPI