Skip to content

Commit 4c87d41

Browse files
authored
Added support for WGSL (#3455)
1 parent 646b2e0 commit 4c87d41

16 files changed

+933
-2
lines changed

components.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

+4
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,10 @@
15231523
"alias": "webidl",
15241524
"owner": "RunDevelopment"
15251525
},
1526+
"wgsl": {
1527+
"title": "WGSL",
1528+
"owner": "Dr4gonthree"
1529+
},
15261530
"wiki": {
15271531
"title": "Wiki markup",
15281532
"require": "markup",

components/prism-wgsl.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
Prism.languages.wgsl = {
2+
'comment': {
3+
pattern: /\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/,
4+
greedy: true,
5+
},
6+
'builtin-attribute': {
7+
pattern: /(@)builtin\(.*?\)/,
8+
lookbehind: true,
9+
inside: {
10+
'attribute': {
11+
pattern: /^builtin/,
12+
alias: 'attr-name',
13+
},
14+
'punctuation': /[(),]/,
15+
'built-in-values': {
16+
pattern: /\b(?:frag_depth|front_facing|global_invocation_id|instance_index|local_invocation_id|local_invocation_index|num_workgroups|position|sample_index|sample_mask|vertex_index|workgroup_id)\b/,
17+
alias: 'attr-value',
18+
},
19+
},
20+
},
21+
'attributes': {
22+
pattern: /(@)(?:align|binding|compute|const|fragment|group|id|interpolate|invariant|location|size|vertex|workgroup_size)/i,
23+
lookbehind: true,
24+
alias: 'attr-name',
25+
},
26+
'functions': {
27+
pattern: /\b(fn\s+)[_a-zA-Z]\w*(?=[(<])/,
28+
lookbehind: true,
29+
alias: 'function',
30+
},
31+
'keyword': /\b(?:bitcast|break|case|const|continue|continuing|default|discard|else|enable|fallthrough|fn|for|function|if|let|loop|private|return|storage|struct|switch|type|uniform|var|while|workgroup)\b/,
32+
'builtin': /\b(?:abs|acos|acosh|all|any|array|asin|asinh|atan|atan2|atanh|atomic|atomicAdd|atomicAnd|atomicCompareExchangeWeak|atomicExchange|atomicLoad|atomicMax|atomicMin|atomicOr|atomicStore|atomicSub|atomicXor|bool|ceil|clamp|cos|cosh|countLeadingZeros|countOneBits|countTrailingZeros|cross|degrees|determinant|distance|dot|dpdx|dpdxCoarse|dpdxFine|dpdy|dpdyCoarse|dpdyFine|exp|exp2|extractBits|f32|f64|faceForward|firstLeadingBit|floor|fma|fract|frexp|fwidth|fwidthCoarse|fwidthFine|i32|i64|insertBits|inverseSqrt|ldexp|length|log|log2|mat[2-4]x[2-4]|max|min|mix|modf|normalize|override|pack2x16float|pack2x16snorm|pack2x16unorm|pack4x8snorm|pack4x8unorm|pow|ptr|quantizeToF16|radians|reflect|refract|reverseBits|round|sampler|sampler_comparison|select|shiftLeft|shiftRight|sign|sin|sinh|smoothstep|sqrt|staticAssert|step|storageBarrier|tan|tanh|textureDimensions|textureGather|textureGatherCompare|textureLoad|textureNumLayers|textureNumLevels|textureNumSamples|textureSample|textureSampleBias|textureSampleCompare|textureSampleCompareLevel|textureSampleGrad|textureSampleLevel|textureStore|texture_1d|texture_2d|texture_2d_array|texture_3d|texture_cube|texture_cube_array|texture_depth_2d|texture_depth_2d_array|texture_depth_cube|texture_depth_cube_array|texture_depth_multisampled_2d|texture_multisampled_2d|texture_storage_1d|texture_storage_2d|texture_storage_2d_array|texture_storage_3d|transpose|trunc|u32|u64|unpack2x16float|unpack2x16snorm|unpack2x16unorm|unpack4x8snorm|unpack4x8unorm|vec[2-4]|workgroupBarrier)\b/,
33+
'function-calls': {
34+
pattern: /\b[_a-z]\w*(?=\()/i,
35+
alias: 'function',
36+
},
37+
'class-name': /\b(?:[A-Z][A-Za-z0-9]*)\b/,
38+
'bool-literal': {
39+
pattern: /\b(?:false|true)\b/,
40+
alias: 'boolean',
41+
},
42+
'hex-int-literal': {
43+
pattern: /\b0[xX][0-9a-fA-F]+[iu]?\b(?![.pP])/,
44+
alias: 'number',
45+
},
46+
'hex-float-literal': {
47+
pattern: /\b0[xX][0-9a-fA-F]*(?:\.[0-9a-fA-F]*)?(?:[pP][+-]?\d+[fh]?)?/, alias: 'number'
48+
},
49+
'decimal-float-literal': [
50+
{ pattern: /\d*\.\d+(?:[eE](?:\+|-)?\d+)?[fh]?/, alias: 'number' },
51+
{ pattern: /\d+\.\d*(?:[eE](?:\+|-)?\d+)?[fh]?/, alias: 'number' },
52+
{ pattern: /\d+[eE](?:\+|-)?\d+[fh]?/, alias: 'number' },
53+
{ pattern: /\b\d+[fh]\b/, alias: 'number' },
54+
],
55+
'int-literal': {
56+
pattern: /\b\d+[iu]?\b/,
57+
alias: 'number',
58+
},
59+
'operator': [
60+
{ pattern: /(?:\^|~|\|(?!\|)|\|\||&&|<<|>>|!)(?!=)/ },
61+
{ pattern: /&(?![&=])/ },
62+
{ pattern: /(?:\+=|-=|\*=|\/=|%=|\^=|&=|\|=|<<=|>>=)/ },
63+
{ pattern: /(^|[^<>=!])=(?![=>])/, lookbehind: true },
64+
{ pattern: /(?:==|!=|<=|\+\+|--|(^|[^=])>=)/, lookbehind: true },
65+
{ pattern: /(?:(?:[+%]|(?:\*(?!\w)))(?!=))|(?:-(?!>))|(?:\/(?!\/))/ },
66+
{ pattern: /->/ },
67+
],
68+
'punctuation': /[@(){}[\],;<>:.]/,
69+
};

components/prism-wgsl.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-wgsl.html

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<h2>Full example</h2>
2+
<pre><code>// Vertex shader
3+
struct CameraUniform {
4+
view_proj: mat4x4&lt;f32&gt;;
5+
};
6+
[[group(1), binding(0)]]
7+
var&lt;uniform&gt; camera: CameraUniform;
8+
9+
struct InstanceInput {
10+
[[location(5)]] model_matrix_0: vec4&lt;f32&gt;;
11+
[[location(6)]] model_matrix_1: vec4&lt;f32&gt;;
12+
[[location(7)]] model_matrix_2: vec4&lt;f32&gt;;
13+
[[location(8)]] model_matrix_3: vec4&lt;f32&gt;;
14+
};
15+
16+
struct VertexInput {
17+
[[location(0)]] position: vec3&lt;f32&gt;;
18+
[[location(1)]] tex_coords: vec2&lt;f32&gt;;
19+
};
20+
21+
struct VertexOutput {
22+
[[builtin(position)]] clip_position: vec4&lt;f32&gt;;
23+
[[location(0)]] tex_coords: vec2&lt;f32&gt;;
24+
};
25+
26+
@vertex
27+
[[stage(vertex)]]
28+
fn vs_main(
29+
model: VertexInput,
30+
instance: InstanceInput,
31+
) -&gt; VertexOutput {
32+
let model_matrix = mat4x4&lt;f32&gt;(
33+
instance.model_matrix_0,
34+
instance.model_matrix_1,
35+
instance.model_matrix_2,
36+
instance.model_matrix_3,
37+
);
38+
39+
bool mybool1 = true;
40+
bool mybool2 = false;
41+
42+
var out: VertexOutput;
43+
out.tex_coords = model.tex_coords;
44+
out.clip_position = camera.view_proj * model_matrix * vec4&lt;f32&gt;(model.position, 1.0);
45+
return out;
46+
}
47+
48+
// Fragment shader
49+
50+
[[group(0), binding(0)]]
51+
var t_diffuse: texture_2d&lt;f32&gt;;
52+
[[group(0), binding(1)]]
53+
var s_diffuse: sampler;
54+
55+
@fragment
56+
[[stage(fragment)]]
57+
fn fs_main(in: VertexOutput) -&gt; [[location(0)]] vec4&lt;f32&gt; {
58+
return textureSample(t_diffuse, s_diffuse, in.tex_coords);
59+
}
60+
</code></pre>

plugins/show-language/prism-show-language.js

+1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267
"wasm": "WebAssembly",
268268
"web-idl": "Web IDL",
269269
"webidl": "Web IDL",
270+
"wgsl": "WGSL",
270271
"wiki": "Wiki markup",
271272
"wolfram": "Wolfram language",
272273
"nb": "Mathematica Notebook",

0 commit comments

Comments
 (0)