f57d5c077cc4d2fd99ad1204c147c1a8d41945d3
[WebKit-https.git] / Source / WebCore / Modules / webgpu / WHLSL / Metal / WHLSLNativeTypeWriter.cpp
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 #include "config.h"
27 #include "WHLSLNativeTypeWriter.h"
28
29 #if ENABLE(WEBGPU)
30
31 #include "WHLSLNamedType.h"
32 #include "WHLSLNativeTypeDeclaration.h"
33 #include "WHLSLTypeReference.h"
34
35 namespace WebCore {
36
37 namespace WHLSL {
38
39 namespace Metal {
40
41 String writeNativeType(AST::NativeTypeDeclaration& nativeTypeDeclaration)
42 {
43     if (nativeTypeDeclaration.name() == "void")
44         return "void"_str;
45     if (nativeTypeDeclaration.name() == "bool")
46         return "bool"_str;
47     if (nativeTypeDeclaration.name() == "uchar")
48         return "uint8_t"_str;
49     if (nativeTypeDeclaration.name() == "ushort")
50         return "uint16_t"_str;
51     if (nativeTypeDeclaration.name() == "uint")
52         return "uint32_t"_str;
53     if (nativeTypeDeclaration.name() == "char")
54         return "int8_t"_str;
55     if (nativeTypeDeclaration.name() == "short")
56         return "int16_t"_str;
57     if (nativeTypeDeclaration.name() == "int")
58         return "int32_t"_str;
59     if (nativeTypeDeclaration.name() == "half")
60         return "half"_str;
61     if (nativeTypeDeclaration.name() == "float")
62         return "float"_str;
63     if (nativeTypeDeclaration.name() == "atomic_int")
64         return "atomic_int"_str;
65     if (nativeTypeDeclaration.name() == "atomic_uint")
66         return "atomic_uint"_str;
67     if (nativeTypeDeclaration.name() == "sampler")
68         return "sampler"_str;
69     if (nativeTypeDeclaration.name() == "vector") {
70         ASSERT(nativeTypeDeclaration.typeArguments().size() == 2);
71         ASSERT(WTF::holds_alternative<UniqueRef<AST::TypeReference>>(nativeTypeDeclaration.typeArguments()[0]));
72         auto& typeReference = WTF::get<UniqueRef<AST::TypeReference>>(nativeTypeDeclaration.typeArguments()[0]);
73         auto& unifyNode = typeReference->unifyNode();
74         auto& namedType = downcast<AST::NamedType>(unifyNode);
75         auto& parameterType = downcast<AST::NativeTypeDeclaration>(namedType);
76         auto prefix = ([&]() -> String {
77             if (parameterType.name() == "bool")
78                 return "bool";
79             if (parameterType.name() == "uchar")
80                 return "uchar";
81             if (parameterType.name() == "ushort")
82                 return "ushort";
83             if (parameterType.name() == "uint")
84                 return "uint";
85             if (parameterType.name() == "char")
86                 return "char";
87             if (parameterType.name() == "short")
88                 return "short";
89             if (parameterType.name() == "int")
90                 return "int";
91             if (parameterType.name() == "half")
92                 return "half";
93             ASSERT(parameterType.name() == "float");
94             return "float";
95         })();
96         ASSERT(WTF::holds_alternative<AST::ConstantExpression>(nativeTypeDeclaration.typeArguments()[1]));
97         auto& constantExpression = WTF::get<AST::ConstantExpression>(nativeTypeDeclaration.typeArguments()[1]);
98         auto& integerLiteral = constantExpression.integerLiteral();
99         auto suffix = ([&]() -> String {
100             switch (integerLiteral.value()) {
101             case 2:
102                 return "2"_str;
103             case 3:
104                 return "3"_str;
105             default:
106                 ASSERT(integerLiteral.value() == 4);
107                 return "4"_str;
108             }
109         })();
110         return makeString(prefix, suffix);
111     }
112     if (nativeTypeDeclaration.name() == "matrix") {
113         ASSERT(nativeTypeDeclaration.typeArguments().size() == 3);
114         ASSERT(WTF::holds_alternative<UniqueRef<AST::TypeReference>>(nativeTypeDeclaration.typeArguments()[0]));
115         auto& typeReference = WTF::get<UniqueRef<AST::TypeReference>>(nativeTypeDeclaration.typeArguments()[0]);
116         auto& unifyNode = typeReference->unifyNode();
117         auto& namedType = downcast<AST::NamedType>(unifyNode);
118         auto& parameterType = downcast<AST::NativeTypeDeclaration>(namedType);
119         auto prefix = ([&]() -> String {
120             if (parameterType.name() == "half")
121                 return "half";
122             ASSERT(parameterType.name() == "float");
123             return "float";
124         })();
125         ASSERT(WTF::holds_alternative<AST::ConstantExpression>(nativeTypeDeclaration.typeArguments()[1]));
126         auto& constantExpression1 = WTF::get<AST::ConstantExpression>(nativeTypeDeclaration.typeArguments()[1]);
127         auto& integerLiteral1 = constantExpression1.integerLiteral();
128         auto middle = ([&]() -> String {
129             switch (integerLiteral1.value()) {
130             case 2:
131                 return "2"_str;
132             case 3:
133                 return "3"_str;
134             default:
135                 ASSERT(integerLiteral1.value() == 4);
136                 return "4"_str;
137             }
138         })();
139         ASSERT(WTF::holds_alternative<AST::ConstantExpression>(nativeTypeDeclaration.typeArguments()[2]));
140         auto& constantExpression2 = WTF::get<AST::ConstantExpression>(nativeTypeDeclaration.typeArguments()[2]);
141         auto& integerLiteral2 = constantExpression2.integerLiteral();
142         auto suffix = ([&]() -> String {
143             switch (integerLiteral2.value()) {
144             case 2:
145                 return "2"_str;
146             case 3:
147                 return "3"_str;
148             default:
149                 ASSERT(integerLiteral2.value() == 4);
150                 return "4"_str;
151             }
152         })();
153         return makeString(prefix, middle, 'x', suffix);
154     }
155     ASSERT(nativeTypeDeclaration.typeArguments().size() == 1);
156     ASSERT(WTF::holds_alternative<UniqueRef<AST::TypeReference>>(nativeTypeDeclaration.typeArguments()[0]));
157     auto& typeReference = WTF::get<UniqueRef<AST::TypeReference>>(nativeTypeDeclaration.typeArguments()[0]);
158     auto prefix = ([&]() -> String {
159         if (nativeTypeDeclaration.name() == "Texture1D")
160             return "texture1d"_str;
161         if (nativeTypeDeclaration.name() == "RWTexture1D")
162             return "texture1d"_str;
163         if (nativeTypeDeclaration.name() == "Texture1DArray")
164             return "texture1d_array"_str;
165         if (nativeTypeDeclaration.name() == "RWTexture1DArray")
166             return "texture1d_array"_str;
167         if (nativeTypeDeclaration.name() == "Texture2D")
168             return "texture2d"_str;
169         if (nativeTypeDeclaration.name() == "RWTexture2D")
170             return "texture2d"_str;
171         if (nativeTypeDeclaration.name() == "Texture2DArray")
172             return "texture2d_array"_str;
173         if (nativeTypeDeclaration.name() == "RWTexture2DArray")
174             return "texture2d_array"_str;
175         if (nativeTypeDeclaration.name() == "Texture3D")
176             return "texture3d"_str;
177         if (nativeTypeDeclaration.name() == "RWTexture3D")
178             return "texture3d"_str;
179         if (nativeTypeDeclaration.name() == "TextureCube")
180             return "texturecube"_str;
181         if (nativeTypeDeclaration.name() == "TextureDepth2D")
182             return "depth2d"_str;
183         if (nativeTypeDeclaration.name() == "RWTextureDepth2D")
184             return "depth2d"_str;
185         if (nativeTypeDeclaration.name() == "TextureDepth2DArray")
186             return "depth2d_array"_str;
187         if (nativeTypeDeclaration.name() == "RWTextureDepth2DArray")
188             return "depth2d_array"_str;
189         ASSERT(nativeTypeDeclaration.name() == "TextureDepthCube");
190         return "depthcube"_str;
191     })();
192     auto innerType = ([&]() -> String {
193         if (typeReference->name() == "ushort")
194             return "ushort"_str;
195         if (typeReference->name() == "ushort2")
196             return "ushort"_str;
197         if (typeReference->name() == "ushort3")
198             return "ushort"_str;
199         if (typeReference->name() == "ushort4")
200             return "ushort"_str;
201         if (typeReference->name() == "uint")
202             return "uint"_str;
203         if (typeReference->name() == "uint2")
204             return "uint"_str;
205         if (typeReference->name() == "uint3")
206             return "uint"_str;
207         if (typeReference->name() == "uint4")
208             return "uint"_str;
209         if (typeReference->name() == "short")
210             return "short"_str;
211         if (typeReference->name() == "short2")
212             return "short"_str;
213         if (typeReference->name() == "short3")
214             return "short"_str;
215         if (typeReference->name() == "short4")
216             return "short"_str;
217         if (typeReference->name() == "int")
218             return "int"_str;
219         if (typeReference->name() == "int2")
220             return "int"_str;
221         if (typeReference->name() == "int3")
222             return "int"_str;
223         if (typeReference->name() == "int4")
224             return "int"_str;
225         if (typeReference->name() == "half")
226             return "half"_str;
227         if (typeReference->name() == "half2")
228             return "half"_str;
229         if (typeReference->name() == "half3")
230             return "half"_str;
231         if (typeReference->name() == "half4")
232             return "half"_str;
233         if (typeReference->name() == "float")
234             return "float"_str;
235         if (typeReference->name() == "float2")
236             return "float"_str;
237         if (typeReference->name() == "float3")
238             return "float"_str;
239         ASSERT(typeReference->name() == "float4");
240         return "float"_str;
241     })();
242     // FIXME: https://bugs.webkit.org/show_bug.cgi?id=195813 Specify the second template argument to Metal texture types.
243     return makeString(prefix, '<', innerType, '>');
244 }
245
246 } // namespace Metal
247
248 } // namespace WHLSL
249
250 } // namespace WebCore
251
252 #endif