1/*
2 * Copyright (C) 2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(WEBGPU)
29
30#include "WHLSLPipelineDescriptor.h"
31#include <wtf/HashMap.h>
32#include <wtf/text/WTFString.h>
33
34namespace WebCore {
35
36namespace WHLSL {
37
38namespace AST {
39
40class FunctionDefinition;
41
42}
43
44struct EntryPointItems;
45class Intrinsics;
46
47namespace Metal {
48
49class TypeNamer;
50
51class EntryPointScaffolding {
52public:
53 EntryPointScaffolding(AST::FunctionDefinition&, Intrinsics&, TypeNamer&, EntryPointItems&, HashMap<Binding*, size_t>& resourceMap, Layout&, std::function<String()>&& generateNextVariableName);
54 virtual ~EntryPointScaffolding() = default;
55
56 virtual String helperTypes() = 0;
57 virtual String signature(String& functionName) = 0;
58 virtual String unpack() = 0;
59 virtual String pack(const String& existingVariableName, const String& variableName) = 0;
60
61 Vector<String>& parameterVariables() { return m_parameterVariables; }
62
63protected:
64 String resourceHelperTypes();
65 Optional<String> resourceSignature();
66 Optional<String> builtInsSignature();
67
68 String mangledInputPath(Vector<String>& path);
69 String mangledOutputPath(Vector<String>& path);
70 String unpackResourcesAndNamedBuiltIns();
71
72 AST::FunctionDefinition& m_functionDefinition;
73 Intrinsics& m_intrinsics;
74 TypeNamer& m_typeNamer;
75 EntryPointItems& m_entryPointItems;
76 HashMap<Binding*, size_t>& m_resourceMap;
77 Layout& m_layout;
78 std::function<String()> m_generateNextVariableName;
79
80 struct NamedBinding {
81 String elementName;
82 unsigned index;
83 };
84 struct NamedBindGroup {
85 String structName;
86 String variableName;
87 Vector<NamedBinding> namedBindings;
88 unsigned argumentBufferIndex;
89 };
90 Vector<NamedBindGroup> m_namedBindGroups; // Parallel to m_layout
91
92 struct NamedBuiltIn {
93 size_t indexInEntryPointItems;
94 String variableName;
95 };
96 Vector<NamedBuiltIn> m_namedBuiltIns;
97
98 Vector<String> m_parameterVariables;
99};
100
101class VertexEntryPointScaffolding : public EntryPointScaffolding {
102public:
103 VertexEntryPointScaffolding(AST::FunctionDefinition&, Intrinsics&, TypeNamer&, EntryPointItems&, HashMap<Binding*, size_t>& resourceMap, Layout&, std::function<String()>&& generateNextVariableName, HashMap<VertexAttribute*, size_t>& matchedVertexAttributes);
104 virtual ~VertexEntryPointScaffolding() = default;
105
106 String helperTypes() override;
107 String signature(String& functionName) override;
108 String unpack() override;
109 String pack(const String& existingVariableName, const String& variableName) override;
110
111private:
112 HashMap<VertexAttribute*, size_t>& m_matchedVertexAttributes;
113 String m_stageInStructName;
114 String m_returnStructName;
115 String m_stageInParameterName;
116
117 struct NamedStageIn {
118 size_t indexInEntryPointItems;
119 String elementName;
120 unsigned attributeIndex;
121 };
122 Vector<NamedStageIn> m_namedStageIns;
123
124 struct NamedOutput {
125 String elementName;
126 };
127 Vector<NamedOutput> m_namedOutputs;
128};
129
130class FragmentEntryPointScaffolding : public EntryPointScaffolding {
131public:
132 FragmentEntryPointScaffolding(AST::FunctionDefinition&, Intrinsics&, TypeNamer&, EntryPointItems&, HashMap<Binding*, size_t>& resourceMap, Layout&, std::function<String()>&& generateNextVariableName, HashMap<AttachmentDescriptor*, size_t>& matchedColorAttachments);
133 virtual ~FragmentEntryPointScaffolding() = default;
134
135 String helperTypes() override;
136 String signature(String& functionName) override;
137 String unpack() override;
138 String pack(const String& existingVariableName, const String& variableName) override;
139
140private:
141 String m_stageInStructName;
142 String m_returnStructName;
143 String m_stageInParameterName;
144
145 struct NamedStageIn {
146 size_t indexInEntryPointItems;
147 String elementName;
148 unsigned attributeIndex;
149 };
150 Vector<NamedStageIn> m_namedStageIns;
151
152 struct NamedOutput {
153 String elementName;
154 };
155 Vector<NamedOutput> m_namedOutputs;
156};
157
158class ComputeEntryPointScaffolding : public EntryPointScaffolding {
159public:
160 ComputeEntryPointScaffolding(AST::FunctionDefinition&, Intrinsics&, TypeNamer&, EntryPointItems&, HashMap<Binding*, size_t>& resourceMap, Layout&, std::function<String()>&& generateNextVariableName);
161 virtual ~ComputeEntryPointScaffolding() = default;
162
163 String helperTypes() override;
164 String signature(String& functionName) override;
165 String unpack() override;
166 String pack(const String& existingVariableName, const String& variableName) override;
167
168private:
169};
170
171}
172
173}
174
175}
176
177#endif
178