-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
example.html
197 lines (177 loc) · 6.08 KB
/
example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<html>
<body>
<h1>WGSL Reflection Library</h1>
<h1>Shader</h1>
<textarea id="shader" style="width: 100%; height: 300px"></textarea>
<button
id="parse"
style="margin-top: 10px; width: 100px; background-color: #afa"
>
COMPILE
</button>
<h1>Reflection Info</h1>
<div
id="shader_info"
style="
width: 100%;
background-color: #eee;
padding: 10px;
border: 1px solid #888;
"
></div>
<script type="module">
import * as wgsl from "./wgsl_reflect.module.js";
const shader = `
struct ViewUniforms {
viewProjection: mat4x4<f32>
}
struct ModelUniforms {
model: mat4x4<f32>,
color: vec4<f32>,
intensity: f32
}
@binding(0) @group(0) var<uniform> viewUniforms: ViewUniforms;
@binding(1) @group(0) var<uniform> modelUniforms: ModelUniforms;
@binding(2) @group(0) var u_sampler: sampler;
@binding(3) @group(0) var u_texture: texture_2d<f32>;
struct VertexInput {
@location(0) a_position: vec3<f32>,
@location(1) a_normal: vec3<f32>,
@location(2) a_color: vec4<f32>,
@location(3) a_uv: vec2<f32>
}
struct VertexOutput {
@builtin(position) Position: vec4<f32>,
@location(0) v_position: vec4<f32>,
@location(1) v_normal: vec3<f32>,
@location(2) v_color: vec4<f32>,
@location(3) v_uv: vec2<f32>
}
@vertex
fn main(input: VertexInput) -> VertexOutput {
var output: VertexOutput;
output.Position = viewUniforms.viewProjection * modelUniforms.model * vec4<f32>(input.a_position, 1.0);
output.v_position = output.Position;
output.v_normal = input.a_normal;
output.v_color = input.a_color * modelUniforms.color * modelUniforms.intensity;
output.v_uv = input.a_uv;
return output;
}`;
const shadercode = document.getElementById("shader");
shadercode.value = shader;
const shaderInfo = document.getElementById("shader_info");
function getTypeName(type) {
let name = type.name;
if (type.format) {
name += `<${type.format.name}>`;
}
return name;
}
function parse() {
let info = "";
const t1 = performance.now();
let reflect;
try {
reflect = new wgsl.WgslReflect(shadercode.value);
} catch (e) {
let msg = e.message;
if (e.token) {
msg += ` Line ${e.token.line}. Found: '${e.token}'.`;
}
shaderInfo.innerHTML = `<h2 style="color:#f00;">${msg}</h2>`;
return;
}
const t2 = performance.now();
info += `<b>TIME: ${t2 - t1}ms</b><br>`;
info += "<b>Entry Points</b>";
info += "<ul>";
for (let s in reflect.entry) {
for (let e of reflect.entry[s]) {
info += `<li><b>${e.name}</b>: ${s}</li>`;
if (e.inputs.length) {
info += "<ul>";
info += "<li><b>Inputs</b></li>";
info += "<ul>";
for (let ei of e.inputs) {
info += `<li><b>${ei.name}</b>: <b>${getTypeName(
ei.type
)}</b> location:<b> ${ei.location}</b></li>`;
}
info += "</ul>";
info += "</ul>";
}
}
}
info += "</ul>";
if (reflect.textures.length) {
info += `<b>Textures</b><p>`;
info += "<ul>";
for (let s of reflect.textures) {
info += `<li><b>${s.name}</b>: <b>${s.type.name}</b></li>`;
info += "<ul>";
info += `<li><b>Group:</b> ${s.group}</li>`;
info += `<li><b>Binding:</b> ${s.binding}</li>`;
info += `<li><b>Format:</b> ${s.type?.format?.name ?? "<unknown>"}</li>`;
info += "</ul>";
}
info += "</ul>";
}
if (reflect.samplers.length) {
info += `<b>Samplers</b><p>`;
info += "<ul>";
for (let s of reflect.samplers) {
info += `<li><b>${s.name}</b>: <b>${s.type.name}</b></li>`;
info += "<ul>";
info += `<li><b>Group:</b> ${s.group}</li>`;
info += `<li><b>Binding:</b> ${s.binding}</li>`;
info += "</ul>";
}
info += "</ul>";
}
if (reflect.storage.length) {
info += `<b>Storage Buffers</b><p>`;
info += "<ul>";
for (let s of reflect.storage) {
info += `<li><b>${s.name}</b>: <b>${s.type.name}</b></li>`;
info += "<ul>";
info += `<li><b>Group:</b> ${s.group}</li>`;
info += `<li><b>Binding:</b> ${s.binding}</li>`;
info += "</ul>";
}
info += "</ul>";
}
if (reflect.uniforms.length) {
info += `<b>Uniforms</b><p>`;
info += "<ul>";
for (let s of reflect.uniforms) {
info += `<li><b>${s.name}</b>: <b>${s.type.name}</b></li>`;
info += "<ul>";
info += `<li><b>Buffer Size:</b> ${s.type.size}</li>`;
info += `<li><b>Group:</b> ${s.group}</li>`;
info += `<li><b>Binding:</b> ${s.binding}</li>`;
info += `<li><b>Members:</b></li>`;
info += "<ul>";
for (let m in s.type.members) {
info += `<li><b>${s.type.members[m].name}</b> : <b>${getTypeName(
s.type.members[m].type
)}</b></li>`;
info += "<ul>";
info += `<li><b>Offset:</b>${s.type.members[m].offset}</li>`;
info += `<li><b>Size:</b>${s.type.members[m].size}</li>`;
info += "</ul>";
}
info += "</ul>";
info += "</ul>";
}
info += "</ul>";
}
shaderInfo.innerHTML = info;
}
parse();
const parseButton = document.getElementById("parse");
parseButton.addEventListener("click", function () {
parse();
});
</script>
</body>
</html>