yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

aryaReflection API Wasm Bindings Part1 (#5936)1b5679f95

master
19.8 KiB410 linesraw
1#include "slang-wasm.h"
2
3#include <emscripten/bind.h>
4#include <slang-com-ptr.h>
5
6using namespace emscripten;
7
8EMSCRIPTEN_BINDINGS(slang)
9{
10    constant("SLANG_OK", SLANG_OK);
11
12    function("getLastError", &slang::wgsl::getLastError);
13
14    function("getCompileTargets", &slang::wgsl::getCompileTargets);
15
16    class_<slang::wgsl::GlobalSession>("GlobalSession")
17        .function(
18            "createSession",
19            &slang::wgsl::GlobalSession::createSession,
20            allow_raw_pointers());
21
22    function("createGlobalSession", &slang::wgsl::createGlobalSession, allow_raw_pointers());
23
24    class_<slang::wgsl::Session>("Session")
25        .function(
26            "loadModuleFromSource",
27            &slang::wgsl::Session::loadModuleFromSource,
28            allow_raw_pointers())
29        .function(
30            "createCompositeComponentType",
31            &slang::wgsl::Session::createCompositeComponentType,
32            allow_raw_pointers());
33
34    class_<slang::wgsl::ComponentType>("ComponentType")
35        .function("link", &slang::wgsl::ComponentType::link, allow_raw_pointers())
36        .function("getEntryPointCode", &slang::wgsl::ComponentType::getEntryPointCode)
37        .function("getEntryPointCodeBlob", &slang::wgsl::ComponentType::getEntryPointCodeBlob)
38        .function("getTargetCodeBlob", &slang::wgsl::ComponentType::getTargetCodeBlob)
39        .function("getTargetCode", &slang::wgsl::ComponentType::getTargetCode)
40        .function("getLayout", &slang::wgsl::ComponentType::getLayout, allow_raw_pointers())
41        .function("loadStrings", &slang::wgsl::ComponentType::loadStrings, allow_raw_pointers());
42
43    class_<slang::wgsl::TypeLayoutReflection>("TypeLayoutReflection")
44        .function(
45            "getDescriptorSetDescriptorRangeType",
46            &slang::wgsl::TypeLayoutReflection::getDescriptorSetDescriptorRangeType);
47
48    enum_<slang::Modifier::ID>("ModifierID")
49        .value("Shared", slang::Modifier::ID::Shared)
50        .value("NoDiff", slang::Modifier::ID::NoDiff)
51        .value("Static", slang::Modifier::ID::Static)
52        .value("Const", slang::Modifier::ID::Const)
53        .value("Export", slang::Modifier::ID::Export)
54        .value("Extern", slang::Modifier::ID::Extern)
55        .value("Differentiable", slang::Modifier::ID::Differentiable)
56        .value("Mutating", slang::Modifier::ID::Mutating)
57        .value("In", slang::Modifier::ID::In)
58        .value("Out", slang::Modifier::ID::Out)
59        .value("InOut", slang::Modifier::ID::InOut);
60
61    class_<slang::Modifier>("Modifier");
62
63    class_<slang::wgsl::VariableReflection>("VariableReflection")
64        .function("getName", &slang::wgsl::VariableReflection::getName)
65        .function(
66            "findModifier",
67            &slang::wgsl::VariableReflection::findModifier,
68            allow_raw_pointers())
69        .function("getType", &slang::wgsl::VariableReflection::getType, allow_raw_pointers())
70        .function("getUserAttributeCount", &slang::wgsl::VariableReflection::getUserAttributeCount)
71        .function(
72            "getUserAttributeByIndex",
73            &slang::wgsl::VariableReflection::getUserAttributeByIndex,
74            allow_raw_pointers())
75        .function("hasDefaultValue", &slang::wgsl::VariableReflection::hasDefaultValue);
76
77
78    class_<slang::wgsl::VariableLayoutReflection>("VariableLayoutReflection")
79        .function("getName", &slang::wgsl::VariableLayoutReflection::getName)
80        .function(
81            "getTypeLayout",
82            &slang::wgsl::VariableLayoutReflection::getTypeLayout,
83            allow_raw_pointers())
84        .function("getBindingIndex", &slang::wgsl::VariableLayoutReflection::getBindingIndex);
85
86    class_<slang::wgsl::GenericReflection>("GenericReflection")
87        .function("getName", &slang::wgsl::GenericReflection::getName)
88        .function("getTypeParameterCount", &slang::wgsl::GenericReflection::getTypeParameterCount)
89        .function("getValueParameterCount", &slang::wgsl::GenericReflection::getValueParameterCount)
90        .function("getInnerKind", &slang::wgsl::GenericReflection::getInnerKind)
91        .function("asDecl", &slang::wgsl::GenericReflection::asDecl, allow_raw_pointers())
92        // .function(
93        //     "getTypeParameterConstraintCount",
94        //     &slang::wgsl::GenericReflection::getTypeParameterConstraintCount,
95        //     allow_raw_pointers())
96        .function(
97            "getTypeParameter",
98            &slang::wgsl::GenericReflection::getTypeParameter,
99            allow_raw_pointers())
100        .function(
101            "getValueParameter",
102            &slang::wgsl::GenericReflection::getValueParameter,
103            allow_raw_pointers())
104        .function(
105            "getInnerDecl",
106            &slang::wgsl::GenericReflection::getInnerDecl,
107            allow_raw_pointers())
108        .function(
109            "getOuterGenericContainer",
110            &slang::wgsl::GenericReflection::getOuterGenericContainer,
111            allow_raw_pointers());
112
113    enum_<SlangDeclKind>("SlangDeclKind")
114        .value(
115            "SLANG_DECL_KIND_UNSUPPORTED_FOR_REFLECTION",
116            SlangDeclKind::SLANG_DECL_KIND_UNSUPPORTED_FOR_REFLECTION)
117        .value("SLANG_DECL_KIND_STRUCT", SlangDeclKind::SLANG_DECL_KIND_STRUCT)
118        .value("SLANG_DECL_KIND_FUNC", SlangDeclKind::SLANG_DECL_KIND_FUNC)
119        .value("SLANG_DECL_KIND_MODULE", SlangDeclKind::SLANG_DECL_KIND_MODULE)
120        .value("SLANG_DECL_KIND_GENERIC", SlangDeclKind::SLANG_DECL_KIND_GENERIC)
121        .value("SLANG_DECL_KIND_VARIABLE", SlangDeclKind::SLANG_DECL_KIND_VARIABLE)
122        .value("SLANG_DECL_KIND_NAMESPACE", SlangDeclKind::SLANG_DECL_KIND_NAMESPACE);
123
124    class_<slang::wgsl::DeclReflection>("DeclReflection")
125        .function("getName", &slang::wgsl::DeclReflection::getName)
126        .function("getChildrenCount", &slang::wgsl::DeclReflection::getChildrenCount)
127        .function("getKind", &slang::wgsl::DeclReflection::getKind)
128        .function("getChild", &slang::wgsl::DeclReflection::getChild, allow_raw_pointers())
129        .function("getType", &slang::wgsl::DeclReflection::getType, allow_raw_pointers())
130        .function("asVariable", &slang::wgsl::DeclReflection::asVariable, allow_raw_pointers())
131        .function("asFunction", &slang::wgsl::DeclReflection::asFunction, allow_raw_pointers())
132        .function("asGeneric", &slang::wgsl::DeclReflection::asGeneric, allow_raw_pointers())
133        .function("getParent", &slang::wgsl::DeclReflection::getParent, allow_raw_pointers());
134
135    enum_<slang::DeclReflection::Kind>("DeclReflectionKind")
136        .value("Unsupported", slang::DeclReflection::Kind::Unsupported)
137        .value("Struct", slang::DeclReflection::Kind::Struct)
138        .value("Func", slang::DeclReflection::Kind::Func)
139        .value("Module", slang::DeclReflection::Kind::Module)
140        .value("Generic", slang::DeclReflection::Kind::Generic)
141        .value("Variable", slang::DeclReflection::Kind::Variable)
142        .value("Namespace", slang::DeclReflection::Kind::Namespace);
143
144    enum_<slang::TypeReflection::ScalarType>("ScalarType")
145        .value("None", slang::TypeReflection::ScalarType::None)
146        .value("Void", slang::TypeReflection::ScalarType::Void)
147        .value("Bool", slang::TypeReflection::ScalarType::Bool)
148        .value("Int32", slang::TypeReflection::ScalarType::Int32)
149        .value("UInt32", slang::TypeReflection::ScalarType::UInt32)
150        .value("Int64", slang::TypeReflection::ScalarType::Int64)
151        .value("UInt64", slang::TypeReflection::ScalarType::UInt64)
152        .value("Float16", slang::TypeReflection::ScalarType::Float16)
153        .value("Float32", slang::TypeReflection::ScalarType::Float32)
154        .value("Float64", slang::TypeReflection::ScalarType::Float64)
155        .value("Int8", slang::TypeReflection::ScalarType::Int8)
156        .value("UInt8", slang::TypeReflection::ScalarType::UInt8)
157        .value("Int16", slang::TypeReflection::ScalarType::Int16)
158        .value("UInt16", slang::TypeReflection::ScalarType::UInt16);
159
160    class_<slang::wgsl::TypeReflection>("TypeReflection")
161        .function("getScalarType", &slang::wgsl::TypeReflection::getScalarType)
162        .function("getKind", &slang::wgsl::TypeReflection::getKind);
163
164    enum_<slang::TypeReflection::Kind>("TypeReflectionKind")
165        .value("None", slang::TypeReflection::Kind::None)
166        .value("Struct", slang::TypeReflection::Kind::Struct)
167        .value("Array", slang::TypeReflection::Kind::Array)
168        .value("Matrix", slang::TypeReflection::Kind::Matrix)
169        .value("Vector", slang::TypeReflection::Kind::Vector)
170        .value("Scalar", slang::TypeReflection::Kind::Scalar)
171        .value("ConstantBuffer", slang::TypeReflection::Kind::ConstantBuffer)
172        .value("Resource", slang::TypeReflection::Kind::Resource)
173        .value("SamplerState", slang::TypeReflection::Kind::SamplerState)
174        .value("TextureBuffer", slang::TypeReflection::Kind::TextureBuffer)
175        .value("ShaderStorageBuffer", slang::TypeReflection::Kind::ShaderStorageBuffer)
176        .value("ParameterBlock", slang::TypeReflection::Kind::ParameterBlock)
177        .value("GenericTypeParameter", slang::TypeReflection::Kind::GenericTypeParameter)
178        .value("Interface", slang::TypeReflection::Kind::Interface)
179        .value("OutputStream", slang::TypeReflection::Kind::OutputStream)
180        .value("Specialized", slang::TypeReflection::Kind::Specialized)
181        .value("Feedback", slang::TypeReflection::Kind::Feedback)
182        .value("Pointer", slang::TypeReflection::Kind::Pointer)
183        .value("DynamicResource", slang::TypeReflection::Kind::DynamicResource);
184
185
186    class_<slang::wgsl::UserAttribute>("UserAttribute")
187        .function("getName", &slang::wgsl::UserAttribute::getName)
188        .function("getArgumentCount", &slang::wgsl::UserAttribute::getArgumentCount)
189        .function(
190            "getArgumentType",
191            &slang::wgsl::UserAttribute::getArgumentType,
192            allow_raw_pointers())
193        .function(
194            "getArgumentValueString",
195            &slang::wgsl::UserAttribute::getArgumentValueString,
196            allow_raw_pointers())
197        .function(
198            "getArgumentValueFloat",
199            &slang::wgsl::UserAttribute::getArgumentValueFloat,
200            allow_raw_pointers());
201
202    class_<slang::wgsl::FunctionReflection>("FunctionReflection")
203        .function("getName", &slang::wgsl::FunctionReflection::getName)
204        .function("getUserAttributeCount", &slang::wgsl::FunctionReflection::getUserAttributeCount)
205        .function(
206            "getUserAttributeByIndex",
207            &slang::wgsl::FunctionReflection::getUserAttributeByIndex,
208            allow_raw_pointers());
209
210    class_<slang::wgsl::EntryPointReflection>("EntryPointReflection")
211        .function(
212            "getComputeThreadGroupSize",
213            &slang::wgsl::EntryPointReflection::getComputeThreadGroupSize);
214
215    class_<slang::wgsl::EntryPointReflection::ThreadGroupSize>("ThreadGroupSize")
216        .property("x", &slang::wgsl::EntryPointReflection::ThreadGroupSize::x)
217        .property("y", &slang::wgsl::EntryPointReflection::ThreadGroupSize::y)
218        .property("z", &slang::wgsl::EntryPointReflection::ThreadGroupSize::z);
219
220    class_<slang::wgsl::ProgramLayout>("ProgramLayout")
221        .function("toJsonObject", &slang::wgsl::ProgramLayout::toJsonObject)
222        .function("getParameterCount", &slang::wgsl::ProgramLayout::getParameterCount)
223        .function(
224            "getParameterByIndex",
225            &slang::wgsl::ProgramLayout::getParameterByIndex,
226            allow_raw_pointers())
227        .function(
228            "getGlobalParamsTypeLayout",
229            &slang::wgsl::ProgramLayout::getGlobalParamsTypeLayout,
230            allow_raw_pointers())
231        .function(
232            "findEntryPointByName",
233            &slang::wgsl::ProgramLayout::findEntryPointByName,
234            allow_raw_pointers())
235        .function(
236            "findFunctionByName",
237            &slang::wgsl::ProgramLayout::findFunctionByName,
238            allow_raw_pointers());
239
240    enum_<slang::BindingType>("BindingType")
241        .value("Unknown", slang::BindingType::Unknown)
242        .value("Texture", slang::BindingType::Texture)
243        .value("ConstantBuffer", slang::BindingType::ConstantBuffer)
244        .value("MutableRawBuffer", slang::BindingType::MutableRawBuffer)
245        .value("MutableTypedBuffer", slang::BindingType::MutableTypedBuffer)
246        .value("MutableTexture", slang::BindingType::MutableTexture);
247
248    class_<slang::wgsl::Module, base<slang::wgsl::ComponentType>>("Module")
249        .function(
250            "findEntryPointByName",
251            &slang::wgsl::Module::findEntryPointByName,
252            allow_raw_pointers())
253        .function(
254            "findAndCheckEntryPoint",
255            &slang::wgsl::Module::findAndCheckEntryPoint,
256            allow_raw_pointers())
257        .function(
258            "getDefinedEntryPoint",
259            &slang::wgsl::Module::getDefinedEntryPoint,
260            allow_raw_pointers())
261        .function("getDefinedEntryPointCount", &slang::wgsl::Module::getDefinedEntryPointCount);
262
263    value_object<slang::wgsl::Error>("Error")
264        .field("type", &slang::wgsl::Error::type)
265        .field("result", &slang::wgsl::Error::result)
266        .field("message", &slang::wgsl::Error::message);
267
268    class_<slang::wgsl::EntryPoint, base<slang::wgsl::ComponentType>>("EntryPoint")
269        .function("getName", &slang::wgsl::EntryPoint::getName, allow_raw_pointers());
270
271    register_vector<std::string>("StringList");
272    register_optional<std::vector<std::string>>();
273
274    value_object<slang::wgsl::lsp::Position>("Position")
275        .field("line", &slang::wgsl::lsp::Position::line)
276        .field("character", &slang::wgsl::lsp::Position::character);
277
278    value_object<slang::wgsl::lsp::Range>("Range")
279        .field("start", &slang::wgsl::lsp::Range::start)
280        .field("end", &slang::wgsl::lsp::Range::end);
281
282    value_object<slang::wgsl::lsp::Location>("Location")
283        .field("uri", &slang::wgsl::lsp::Location::uri)
284        .field("range", &slang::wgsl::lsp::Location::range);
285    register_vector<slang::wgsl::lsp::Location>("LocationList");
286    register_optional<std::vector<slang::wgsl::lsp::Location>>();
287
288    value_object<slang::wgsl::lsp::TextEdit>("TextEdit")
289        .field("range", &slang::wgsl::lsp::TextEdit::range)
290        .field("text", &slang::wgsl::lsp::TextEdit::text);
291    register_optional<slang::wgsl::lsp::TextEdit>();
292    register_vector<slang::wgsl::lsp::TextEdit>("TextEditList");
293    register_optional<std::vector<slang::wgsl::lsp::TextEdit>>();
294
295    value_object<slang::wgsl::lsp::MarkupContent>("MarkupContent")
296        .field("kind", &slang::wgsl::lsp::MarkupContent::kind)
297        .field("value", &slang::wgsl::lsp::MarkupContent::value);
298    register_optional<slang::wgsl::lsp::MarkupContent>();
299
300    value_object<slang::wgsl::lsp::Hover>("Hover")
301        .field("contents", &slang::wgsl::lsp::Hover::contents)
302        .field("range", &slang::wgsl::lsp::Hover::range);
303    register_optional<slang::wgsl::lsp::Hover>();
304
305    value_object<slang::wgsl::lsp::CompletionItem>("CompletionItem")
306        .field("label", &slang::wgsl::lsp::CompletionItem::label)
307        .field("kind", &slang::wgsl::lsp::CompletionItem::kind)
308        .field("detail", &slang::wgsl::lsp::CompletionItem::detail)
309        .field("documentation", &slang::wgsl::lsp::CompletionItem::documentation)
310        .field("textEdit", &slang::wgsl::lsp::CompletionItem::textEdit)
311        .field("data", &slang::wgsl::lsp::CompletionItem::data)
312        .field("commitCharacters", &slang::wgsl::lsp::CompletionItem::commitCharacters);
313    register_optional<slang::wgsl::lsp::CompletionItem>();
314    register_vector<slang::wgsl::lsp::CompletionItem>("CompletionItemList");
315    register_optional<std::vector<slang::wgsl::lsp::CompletionItem>>();
316
317    value_object<slang::wgsl::lsp::CompletionContext>("CompletionContext")
318        .field("triggerKind", &slang::wgsl::lsp::CompletionContext::triggerKind)
319        .field("triggerCharacter", &slang::wgsl::lsp::CompletionContext::triggerCharacter);
320
321    value_array<std::array<uint32_t, 2>>("array_uint_2")
322        .element(emscripten::index<0>())
323        .element(emscripten::index<1>());
324
325    value_object<slang::wgsl::lsp::ParameterInformation>("ParameterInformation")
326        .field("label", &slang::wgsl::lsp::ParameterInformation::label)
327        .field("documentation", &slang::wgsl::lsp::ParameterInformation::documentation);
328
329    register_vector<slang::wgsl::lsp::ParameterInformation>("ParameterInformationList");
330
331    value_object<slang::wgsl::lsp::SignatureInformation>("SignatureInformation")
332        .field("label", &slang::wgsl::lsp::SignatureInformation::label)
333        .field("documentation", &slang::wgsl::lsp::SignatureInformation::documentation)
334        .field("parameters", &slang::wgsl::lsp::SignatureInformation::parameters);
335
336    register_vector<slang::wgsl::lsp::SignatureInformation>("SignatureInformationList");
337
338    value_object<slang::wgsl::lsp::SignatureHelp>("SignatureHelp")
339        .field("signatures", &slang::wgsl::lsp::SignatureHelp::signatures)
340        .field("activeSignature", &slang::wgsl::lsp::SignatureHelp::activeSignature)
341        .field("activeParameter", &slang::wgsl::lsp::SignatureHelp::activeParameter);
342    register_optional<slang::wgsl::lsp::SignatureHelp>();
343
344    value_object<slang::wgsl::lsp::DocumentSymbol>("DocumentSymbol")
345        .field("name", &slang::wgsl::lsp::DocumentSymbol::name)
346        .field("detail", &slang::wgsl::lsp::DocumentSymbol::detail)
347        .field("kind", &slang::wgsl::lsp::DocumentSymbol::kind)
348        .field("range", &slang::wgsl::lsp::DocumentSymbol::range)
349        .field("selectionRange", &slang::wgsl::lsp::DocumentSymbol::selectionRange)
350        .field("children", &slang::wgsl::lsp::DocumentSymbol::children);
351
352    register_vector<slang::wgsl::lsp::DocumentSymbol>("DocumentSymbolList");
353    register_optional<std::vector<slang::wgsl::lsp::DocumentSymbol>>();
354
355    value_object<slang::wgsl::lsp::Diagnostics>("Diagnostics")
356        .field("code", &slang::wgsl::lsp::Diagnostics::code)
357        .field("range", &slang::wgsl::lsp::Diagnostics::range)
358        .field("severity", &slang::wgsl::lsp::Diagnostics::severity)
359        .field("message", &slang::wgsl::lsp::Diagnostics::message);
360    register_vector<slang::wgsl::lsp::Diagnostics>("DiagnosticsList");
361    register_optional<std::vector<slang::wgsl::lsp::Diagnostics>>();
362
363    register_vector<uint32_t>("Uint32List");
364    register_optional<std::vector<uint32_t>>();
365
366    class_<slang::wgsl::lsp::LanguageServer>("LanguageServer")
367        .function(
368            "didOpenTextDocument",
369            &slang::wgsl::lsp::LanguageServer::didOpenTextDocument,
370            allow_raw_pointers())
371        .function(
372            "didCloseTextDocument",
373            &slang::wgsl::lsp::LanguageServer::didCloseTextDocument,
374            allow_raw_pointers())
375        .function(
376            "didChangeTextDocument",
377            &slang::wgsl::lsp::LanguageServer::didChangeTextDocument,
378            allow_raw_pointers())
379        .function("hover", &slang::wgsl::lsp::LanguageServer::hover, allow_raw_pointers())
380        .function(
381            "gotoDefinition",
382            &slang::wgsl::lsp::LanguageServer::gotoDefinition,
383            allow_raw_pointers())
384        .function("completion", &slang::wgsl::lsp::LanguageServer::completion, allow_raw_pointers())
385        .function(
386            "completionResolve",
387            &slang::wgsl::lsp::LanguageServer::completionResolve,
388            allow_raw_pointers())
389        .function(
390            "semanticTokens",
391            &slang::wgsl::lsp::LanguageServer::semanticTokens,
392            allow_raw_pointers())
393        .function(
394            "signatureHelp",
395            &slang::wgsl::lsp::LanguageServer::signatureHelp,
396            allow_raw_pointers())
397        .function(
398            "documentSymbol",
399            &slang::wgsl::lsp::LanguageServer::documentSymbol,
400            allow_raw_pointers())
401        .function(
402            "getDiagnostics",
403            &slang::wgsl::lsp::LanguageServer::getDiagnostics,
404            allow_raw_pointers());
405
406    function(
407        "createLanguageServer",
408        &slang::wgsl::lsp::createLanguageServer,
409        return_value_policy::take_ownership());
410};