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 "WHLSLNativeFunctionDeclaration.h" |
31 | #include "WHLSLNativeTypeDeclaration.h" |
32 | #include <cstring> |
33 | #include <wtf/HashSet.h> |
34 | #include <wtf/StdLibExtras.h> |
35 | #include <wtf/Vector.h> |
36 | #include <wtf/text/WTFString.h> |
37 | |
38 | namespace WebCore { |
39 | |
40 | namespace WHLSL { |
41 | |
42 | class Intrinsics { |
43 | public: |
44 | Intrinsics(); |
45 | |
46 | void add(AST::NativeFunctionDeclaration&); |
47 | void add(AST::NativeTypeDeclaration&); |
48 | |
49 | AST::NativeTypeDeclaration& voidType() const |
50 | { |
51 | ASSERT(m_voidType); |
52 | return *m_voidType; |
53 | } |
54 | |
55 | AST::NativeTypeDeclaration& boolType() const |
56 | { |
57 | ASSERT(m_boolType); |
58 | return *m_boolType; |
59 | } |
60 | |
61 | AST::NativeTypeDeclaration& ucharType() const |
62 | { |
63 | ASSERT(m_ucharType); |
64 | return *m_ucharType; |
65 | } |
66 | |
67 | AST::NativeTypeDeclaration& ushortType() const |
68 | { |
69 | ASSERT(m_ushortType); |
70 | return *m_ushortType; |
71 | } |
72 | |
73 | AST::NativeTypeDeclaration& uintType() const |
74 | { |
75 | ASSERT(m_uintType); |
76 | return *m_uintType; |
77 | } |
78 | |
79 | AST::NativeTypeDeclaration& charType() const |
80 | { |
81 | ASSERT(m_charType); |
82 | return *m_charType; |
83 | } |
84 | |
85 | AST::NativeTypeDeclaration& shortType() const |
86 | { |
87 | ASSERT(m_shortType); |
88 | return *m_shortType; |
89 | } |
90 | |
91 | AST::NativeTypeDeclaration& intType() const |
92 | { |
93 | ASSERT(m_intType); |
94 | return *m_intType; |
95 | } |
96 | |
97 | AST::NativeTypeDeclaration& uchar2Type() const |
98 | { |
99 | ASSERT(m_vectorUchar[0]); |
100 | return *m_vectorUchar[0]; |
101 | } |
102 | |
103 | AST::NativeTypeDeclaration& uchar4Type() const |
104 | { |
105 | ASSERT(m_vectorUchar[2]); |
106 | return *m_vectorUchar[2]; |
107 | } |
108 | |
109 | AST::NativeTypeDeclaration& ushort2Type() const |
110 | { |
111 | ASSERT(m_vectorUshort[0]); |
112 | return *m_vectorUshort[0]; |
113 | } |
114 | |
115 | AST::NativeTypeDeclaration& ushort4Type() const |
116 | { |
117 | ASSERT(m_vectorUshort[2]); |
118 | return *m_vectorUshort[2]; |
119 | } |
120 | |
121 | AST::NativeTypeDeclaration& uint2Type() const |
122 | { |
123 | ASSERT(m_vectorUint[0]); |
124 | return *m_vectorUint[0]; |
125 | } |
126 | |
127 | AST::NativeTypeDeclaration& uint4Type() const |
128 | { |
129 | ASSERT(m_vectorUint[2]); |
130 | return *m_vectorUint[2]; |
131 | } |
132 | |
133 | AST::NativeTypeDeclaration& char2Type() const |
134 | { |
135 | ASSERT(m_vectorChar[0]); |
136 | return *m_vectorChar[0]; |
137 | } |
138 | |
139 | AST::NativeTypeDeclaration& char4Type() const |
140 | { |
141 | ASSERT(m_vectorChar[2]); |
142 | return *m_vectorChar[2]; |
143 | } |
144 | |
145 | AST::NativeTypeDeclaration& short2Type() const |
146 | { |
147 | ASSERT(m_vectorShort[0]); |
148 | return *m_vectorShort[0]; |
149 | } |
150 | |
151 | AST::NativeTypeDeclaration& short4Type() const |
152 | { |
153 | ASSERT(m_vectorShort[2]); |
154 | return *m_vectorShort[2]; |
155 | } |
156 | |
157 | AST::NativeTypeDeclaration& int2Type() const |
158 | { |
159 | ASSERT(m_vectorInt[0]); |
160 | return *m_vectorInt[0]; |
161 | } |
162 | |
163 | AST::NativeTypeDeclaration& int4Type() const |
164 | { |
165 | ASSERT(m_vectorInt[2]); |
166 | return *m_vectorInt[2]; |
167 | } |
168 | |
169 | AST::NativeTypeDeclaration& samplerType() const |
170 | { |
171 | ASSERT(m_samplerType); |
172 | return *m_samplerType; |
173 | } |
174 | |
175 | AST::NativeTypeDeclaration& floatType() const |
176 | { |
177 | ASSERT(m_floatType); |
178 | return *m_floatType; |
179 | } |
180 | |
181 | AST::NativeTypeDeclaration& float2Type() const |
182 | { |
183 | ASSERT(m_vectorFloat[0]); |
184 | return *m_vectorFloat[0]; |
185 | } |
186 | |
187 | AST::NativeTypeDeclaration& float3Type() const |
188 | { |
189 | ASSERT(m_vectorFloat[1]); |
190 | return *m_vectorFloat[1]; |
191 | } |
192 | |
193 | AST::NativeTypeDeclaration& float4Type() const |
194 | { |
195 | ASSERT(m_vectorFloat[2]); |
196 | return *m_vectorFloat[2]; |
197 | } |
198 | |
199 | AST::NativeFunctionDeclaration& ddx() const |
200 | { |
201 | ASSERT(m_ddx); |
202 | return *m_ddx; |
203 | } |
204 | |
205 | AST::NativeFunctionDeclaration& ddy() const |
206 | { |
207 | ASSERT(m_ddy); |
208 | return *m_ddy; |
209 | } |
210 | |
211 | AST::NativeFunctionDeclaration& allMemoryBarrier() const |
212 | { |
213 | ASSERT(m_allMemoryBarrier); |
214 | return *m_allMemoryBarrier; |
215 | } |
216 | |
217 | AST::NativeFunctionDeclaration& deviceMemoryBarrier() const |
218 | { |
219 | ASSERT(m_deviceMemoryBarrier); |
220 | return *m_deviceMemoryBarrier; |
221 | } |
222 | |
223 | AST::NativeFunctionDeclaration& groupMemoryBarrier() const |
224 | { |
225 | ASSERT(m_groupMemoryBarrier); |
226 | return *m_groupMemoryBarrier; |
227 | } |
228 | |
229 | private: |
230 | bool addPrimitive(AST::NativeTypeDeclaration&); |
231 | bool addVector(AST::NativeTypeDeclaration&); |
232 | bool addMatrix(AST::NativeTypeDeclaration&); |
233 | bool addFullTexture(AST::NativeTypeDeclaration&, AST::TypeReference&); |
234 | bool addDepthTexture(AST::NativeTypeDeclaration&, AST::TypeReference&); |
235 | void addTexture(AST::NativeTypeDeclaration&); |
236 | |
237 | HashSet<const AST::NativeTypeDeclaration*> m_textureSet; |
238 | |
239 | AST::NativeTypeDeclaration* m_voidType { nullptr }; |
240 | AST::NativeTypeDeclaration* m_boolType { nullptr }; |
241 | AST::NativeTypeDeclaration* m_ucharType { nullptr }; |
242 | AST::NativeTypeDeclaration* m_ushortType { nullptr }; |
243 | AST::NativeTypeDeclaration* m_uintType { nullptr }; |
244 | AST::NativeTypeDeclaration* m_charType { nullptr }; |
245 | AST::NativeTypeDeclaration* m_shortType { nullptr }; |
246 | AST::NativeTypeDeclaration* m_intType { nullptr }; |
247 | AST::NativeTypeDeclaration* m_halfType { nullptr }; |
248 | AST::NativeTypeDeclaration* m_floatType { nullptr }; |
249 | AST::NativeTypeDeclaration* m_atomicIntType { nullptr }; |
250 | AST::NativeTypeDeclaration* m_atomicUintType { nullptr }; |
251 | AST::NativeTypeDeclaration* m_samplerType { nullptr }; |
252 | |
253 | AST::NativeTypeDeclaration* m_vectorBool[3] { 0 }; |
254 | AST::NativeTypeDeclaration* m_vectorUchar[3] { 0 }; |
255 | AST::NativeTypeDeclaration* m_vectorUshort[3] { 0 }; |
256 | AST::NativeTypeDeclaration* m_vectorUint[3] { 0 }; |
257 | AST::NativeTypeDeclaration* m_vectorChar[3] { 0 }; |
258 | AST::NativeTypeDeclaration* m_vectorShort[3] { 0 }; |
259 | AST::NativeTypeDeclaration* m_vectorInt[3] { 0 }; |
260 | AST::NativeTypeDeclaration* m_vectorHalf[3] { 0 }; |
261 | AST::NativeTypeDeclaration* m_vectorFloat[3] { 0 }; |
262 | |
263 | AST::NativeTypeDeclaration* m_matrixHalf[3][3] {{ 0 }}; |
264 | AST::NativeTypeDeclaration* m_matrixFloat[3][3] {{ 0 }}; |
265 | |
266 | static constexpr const char* m_textureTypeNames[] = { "Texture1D" , "RWTexture1D" , "Texture1DArray" , "RWTexture1DArray" , "Texture2D" , "RWTexture2D" , "Texture2DArray" , "RWTexture2DArray" , "Texture3D" , "RWTexture3D" , "TextureCube" }; |
267 | |
268 | static constexpr const char* m_textureInnerTypeNames[] = { "uchar" , "ushort" , "uint" , "char" , "short" , "int" , "half" , "float" }; |
269 | |
270 | AST::NativeTypeDeclaration* m_fullTextures[WTF_ARRAY_LENGTH(m_textureTypeNames)][WTF_ARRAY_LENGTH(m_textureInnerTypeNames)][4] {{{ 0 }}}; |
271 | |
272 | static constexpr const char* m_depthTextureInnerTypes[] = { "half" , "float" }; |
273 | |
274 | AST::NativeTypeDeclaration* m_textureDepth2D[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)] { 0 }; |
275 | AST::NativeTypeDeclaration* m_rwTextureDepth2D[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)] { 0 }; |
276 | AST::NativeTypeDeclaration* m_textureDepth2DArray[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)] { 0 }; |
277 | AST::NativeTypeDeclaration* m_rwTextureDepth2DArray[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)] { 0 }; |
278 | AST::NativeTypeDeclaration* m_textureDepthCube[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)] { 0 }; |
279 | |
280 | AST::NativeFunctionDeclaration* m_ddx { nullptr }; |
281 | AST::NativeFunctionDeclaration* m_ddy { nullptr }; |
282 | AST::NativeFunctionDeclaration* m_allMemoryBarrier { nullptr }; |
283 | AST::NativeFunctionDeclaration* m_deviceMemoryBarrier { nullptr }; |
284 | AST::NativeFunctionDeclaration* m_groupMemoryBarrier { nullptr }; |
285 | }; |
286 | |
287 | } // namespace WHLSL |
288 | |
289 | } // namespace WebCore |
290 | |
291 | #endif // ENABLE(WEBGPU) |
292 | |