| 1 | <?php |
| 2 | namespace Std\Lib\Syntax; |
| 3 | |
| 4 | /** |
| 5 | * Slang(HLSLの上位互換) |
| 6 | */ |
| 7 | class Slang extends Hlsl { |
| 8 | protected array $rules = [ |
| 9 | // 属性 [[vk::binding(0, 0)]] |
| 10 | ['re' => '\[\[[^\]\n]*\]\]', 'class' => 'attribute'], |
| 11 | ]; |
| 12 | |
| 13 | /** @var array HLSLに加えるSlangのトークン */ |
| 14 | protected array $slangTokens = [ |
| 15 | 'keyword' => [ |
| 16 | 'import', 'module', 'implementing', '__include', '__exported', 'public', 'private', 'internal', 'extension', 'associatedtype', |
| 17 | 'where', 'let', 'var', 'func', 'property', 'get', 'set', '__init', '__subscript', 'This', 'typealias', 'throw', 'throws', |
| 18 | 'try', 'catch', 'defer', 'is', 'as', 'new', 'some', 'dyn', 'ref', '__ref', '__generic', '__target_switch', 'spirv_asm', |
| 19 | '__intrinsic_asm', 'constexpr', 'override', 'no_diff', 'fwd_diff', 'bwd_diff', 'alignof', 'countof', |
| 20 | ], |
| 21 | 'constant' => [ |
| 22 | 'none', 'nullptr', |
| 23 | ], |
| 24 | 'type' => [ |
| 25 | 'Ptr', 'Optional', 'Tuple', 'Array', 'DescriptorHandle', 'ParameterBlock', 'NativeString', 'String', 'int8_t', 'uint8_t', |
| 26 | 'Sampler1D', 'Sampler2D', 'Sampler3D', 'SamplerCube', 'Sampler1DArray', 'Sampler2DArray', 'SamplerCubeArray', 'Sampler2DShadow', |
| 27 | 'Sampler2DMS', 'Sampler2DMSArray', 'Texture2DShadow', 'TextureCubeShadow', 'RWTexture2DArray', 'Atomic', |
| 28 | 'IDifferentiable', 'IDifferentiablePtrType', 'DifferentialPair', 'IArithmetic', 'IFloat', 'IInteger', 'ILogical', 'IComparable', |
| 29 | 'IRangedValue', 'IArray', 'IRWArray', 'IBufferDataLayout', 'DefaultDataLayout', 'Std140DataLayout', 'Std430DataLayout', 'ScalarDataLayout', |
| 30 | ], |
| 31 | 'builtin' => [ |
| 32 | 'detach', 'diffPair', 'updatePrimal', 'updateDiff', 'getDiff', 'getPrimal', 'isDifferentialNull', |
| 33 | 'printf', 'bit_cast', 'reinterpret', 'createDynamicObject', 'getStringHash', 'static_assert', |
| 34 | ], |
| 35 | ]; |
| 36 | |
| 37 | protected array $attributes = [ |
| 38 | // HLSL |
| 39 | 'numthreads', 'unroll', 'loop', 'branch', 'flatten', 'fastopt', 'allow_uav_condition', 'call', 'forcecase', 'earlydepthstencil', |
| 40 | 'maxvertexcount', 'domain', 'partitioning', 'outputtopology', 'outputcontrolpoints', 'patchconstantfunc', 'maxtessfactor', |
| 41 | 'instance', 'RootSignature', 'shader', 'WaveSize', 'NumThreads', 'vk', |
| 42 | // Slang |
| 43 | 'Differentiable', 'ForwardDifferentiable', 'BackwardDifferentiable', 'BackwardDerivative', 'ForwardDerivative', 'PrimalSubstitute', |
| 44 | 'PreferRecompute', 'PreferCheckpoint', 'TreatAsDifferentiable', 'mutating', 'nonmutating', 'ForceInline', 'noinline', 'require', |
| 45 | 'SpecializationConstant', 'push_constant', 'Payload', 'raypayload', 'AutoPyBindCUDA', 'CudaKernel', 'TorchEntryPoint', |
| 46 | 'Format', 'OverloadRank', 'Deprecated', 'KnownBuiltin', 'anyValueSize', 'COM', 'Flags', 'UnscopedEnum', 'ForceUnroll', |
| 47 | ]; |
| 48 | |
| 49 | public function prepare(string $code): void { |
| 50 | $tokens = $this->tokens; |
| 51 | foreach ($this->slangTokens as $k => $list) { |
| 52 | foreach ($list as $w) $tokens[$k][] = $w; |
| 53 | } |
| 54 | foreach (self::vectorTypes($this->scalars) as $t) $tokens['type'][] = $t; |
| 55 | foreach (self::vectorTypes(['int8_t', 'uint8_t']) as $t) $tokens['type'][] = $t; |
| 56 | $this->words = $this->buildWords($tokens); |
| 57 | } |
| 58 | } |