2 * Copyright (C) 2019 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "WHLSLMangledNames.h"
31 #include "WHLSLPipelineDescriptor.h"
32 #include <wtf/HashMap.h>
33 #include <wtf/text/WTFString.h>
41 class FunctionDefinition;
45 struct EntryPointItems;
52 class EntryPointScaffolding {
53 WTF_MAKE_FAST_ALLOCATED;
55 virtual ~EntryPointScaffolding() = default;
57 virtual void emitHelperTypes(StringBuilder&, Indentation<4>) = 0;
58 virtual void emitSignature(StringBuilder&, MangledFunctionName, Indentation<4>) = 0;
59 virtual void emitUnpack(StringBuilder&, Indentation<4>) = 0;
60 virtual void emitPack(StringBuilder&, MangledVariableName existingVariableName, MangledVariableName, Indentation<4>) = 0;
62 Vector<MangledVariableName>& parameterVariables() { return m_parameterVariables; }
65 EntryPointScaffolding(AST::FunctionDefinition&, Intrinsics&, TypeNamer&, EntryPointItems&, HashMap<Binding*, size_t>& resourceMap, Layout&, std::function<MangledVariableName()>&& generateNextVariableName);
67 void emitResourceHelperTypes(StringBuilder&, Indentation<4>, ShaderStage);
69 enum class IncludePrecedingComma {
73 bool emitResourceSignature(StringBuilder&, IncludePrecedingComma);
74 bool emitBuiltInsSignature(StringBuilder&, IncludePrecedingComma);
76 void emitMangledInputPath(StringBuilder&, Vector<String>& path);
77 void emitMangledOutputPath(StringBuilder&, Vector<String>& path);
78 void emitUnpackResourcesAndNamedBuiltIns(StringBuilder&, Indentation<4>);
80 AST::FunctionDefinition& m_functionDefinition;
81 Intrinsics& m_intrinsics;
82 TypeNamer& m_typeNamer;
83 EntryPointItems& m_entryPointItems;
84 HashMap<Binding*, size_t>& m_resourceMap;
86 std::function<MangledVariableName()> m_generateNextVariableName;
88 struct LengthInformation {
89 MangledStructureElementName elementName;
90 MangledVariableName temporaryName;
94 MangledStructureElementName elementName;
96 Optional<LengthInformation> lengthInformation;
98 struct NamedBindGroup {
99 MangledTypeName structName;
100 MangledVariableName variableName;
101 Vector<NamedBinding> namedBindings;
102 unsigned argumentBufferIndex;
104 Vector<NamedBindGroup> m_namedBindGroups; // Parallel to m_layout
106 struct NamedBuiltIn {
107 size_t indexInEntryPointItems;
108 MangledVariableName variableName;
110 Vector<NamedBuiltIn> m_namedBuiltIns;
112 Vector<MangledVariableName> m_parameterVariables;
115 class VertexEntryPointScaffolding final : public EntryPointScaffolding {
117 VertexEntryPointScaffolding(AST::FunctionDefinition&, Intrinsics&, TypeNamer&, EntryPointItems&, HashMap<Binding*, size_t>& resourceMap, Layout&, std::function<MangledVariableName()>&& generateNextVariableName, HashMap<VertexAttribute*, size_t>& matchedVertexAttributes);
118 virtual ~VertexEntryPointScaffolding() = default;
121 void emitHelperTypes(StringBuilder&, Indentation<4>) override;
122 void emitSignature(StringBuilder&, MangledFunctionName, Indentation<4>) override;
123 void emitUnpack(StringBuilder&, Indentation<4>) override;
124 void emitPack(StringBuilder&, MangledVariableName existingVariableName, MangledVariableName, Indentation<4>) override;
126 HashMap<VertexAttribute*, size_t>& m_matchedVertexAttributes;
127 MangledTypeName m_stageInStructName;
128 MangledTypeName m_returnStructName;
129 MangledVariableName m_stageInParameterName;
131 struct NamedStageIn {
132 size_t indexInEntryPointItems;
133 MangledStructureElementName elementName;
134 unsigned attributeIndex;
136 Vector<NamedStageIn> m_namedStageIns;
139 MangledStructureElementName elementName;
140 MangledOrNativeTypeName internalTypeName;
142 Vector<NamedOutput> m_namedOutputs;
145 class FragmentEntryPointScaffolding final : public EntryPointScaffolding {
147 FragmentEntryPointScaffolding(AST::FunctionDefinition&, Intrinsics&, TypeNamer&, EntryPointItems&, HashMap<Binding*, size_t>& resourceMap, Layout&, std::function<MangledVariableName()>&& generateNextVariableName, HashMap<AttachmentDescriptor*, size_t>& matchedColorAttachments);
148 virtual ~FragmentEntryPointScaffolding() = default;
151 void emitHelperTypes(StringBuilder&, Indentation<4>) override;
152 void emitSignature(StringBuilder&, MangledFunctionName, Indentation<4>) override;
153 void emitUnpack(StringBuilder&, Indentation<4>) override;
154 void emitPack(StringBuilder&, MangledVariableName existingVariableName, MangledVariableName, Indentation<4>) override;
156 MangledTypeName m_stageInStructName;
157 MangledTypeName m_returnStructName;
158 MangledVariableName m_stageInParameterName;
160 struct NamedStageIn {
161 size_t indexInEntryPointItems;
162 MangledStructureElementName elementName;
163 unsigned attributeIndex;
165 Vector<NamedStageIn> m_namedStageIns;
168 MangledStructureElementName elementName;
169 MangledOrNativeTypeName internalTypeName;
171 Vector<NamedOutput> m_namedOutputs;
174 class ComputeEntryPointScaffolding final : public EntryPointScaffolding {
176 ComputeEntryPointScaffolding(AST::FunctionDefinition&, Intrinsics&, TypeNamer&, EntryPointItems&, HashMap<Binding*, size_t>& resourceMap, Layout&, std::function<MangledVariableName()>&& generateNextVariableName);
177 virtual ~ComputeEntryPointScaffolding() = default;
180 void emitHelperTypes(StringBuilder&, Indentation<4>) override;
181 void emitSignature(StringBuilder&, MangledFunctionName, Indentation<4>) override;
182 void emitUnpack(StringBuilder&, Indentation<4>) override;
183 void emitPack(StringBuilder&, MangledVariableName existingVariableName, MangledVariableName, Indentation<4>) override;