(shader)
| 2259 | } |
| 2260 | |
| 2261 | getUniformMetadata(shader) { |
| 2262 | // Parse all uniform struct bindings in group 0. |
| 2263 | // TODO: support non-sampler uniforms being in other groups |
| 2264 | |
| 2265 | // Each binding represents a logical group of uniforms, since they get |
| 2266 | // updated or cached all at once. |
| 2267 | |
| 2268 | const uniformGroups = []; |
| 2269 | const uniformVarRegex = /@group\((\d+)\)\s+@binding\((\d+)\)\s+var<uniform>\s+(\w+)\s*:\s*(\w+);/g; |
| 2270 | |
| 2271 | let match; |
| 2272 | const src = shader.shaderType === 'compute' ? shader.computeSrc() : shader.vertSrc(); |
| 2273 | while ((match = uniformVarRegex.exec(src)) !== null) { |
| 2274 | const [_, groupNum, binding, varName, structType] = match; |
| 2275 | const bindingIndex = parseInt(binding); |
| 2276 | const uniforms = this._parseStruct(src, structType); |
| 2277 | |
| 2278 | uniformGroups.push({ |
| 2279 | group: parseInt(groupNum), |
| 2280 | binding: bindingIndex, |
| 2281 | varName, |
| 2282 | structType, |
| 2283 | uniforms |
| 2284 | }); |
| 2285 | } |
| 2286 | |
| 2287 | if (uniformGroups.length === 0 && shader.shaderType !== 'compute') { |
| 2288 | throw new Error('Expected at least one uniform struct bound to @group(0)'); |
| 2289 | } |
| 2290 | |
| 2291 | // While we're also keeping track of the groups, the API we expose |
| 2292 | // to users of p5 is just a flat list of uniforms (which can be the |
| 2293 | // individual struct items in the group.) |
| 2294 | const allUniforms = {}; |
| 2295 | for (const group of uniformGroups) { |
| 2296 | for (const [uniformName, uniformData] of Object.entries(group.uniforms)) { |
| 2297 | allUniforms[uniformName] = { |
| 2298 | ...uniformData, |
| 2299 | group: group.group, |
| 2300 | binding: group.binding, |
| 2301 | varName: group.varName |
| 2302 | }; |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | // Store uniform groups for buffer pooling |
| 2307 | shader._uniformGroups = uniformGroups; |
| 2308 | |
| 2309 | // Extract samplers from group bindings |
| 2310 | const samplers = {}; |
| 2311 | // TODO: support other texture types |
| 2312 | const samplerRegex = /@group\((\d+)\)\s*@binding\((\d+)\)\s*var\s+(\w+)\s*:\s*(texture_2d<f32>|sampler);/g; |
| 2313 | |
| 2314 | // Extract storage buffers |
| 2315 | const storageBuffers = {}; |
| 2316 | const storageRegex = /@group\((\d+)\)\s*@binding\((\d+)\)\s*var<storage,\s*(read|read_write)>\s+(\w+)\s*:\s*array<\w+>/g; |
| 2317 | |
| 2318 | // Track which bindings are taken by the struct properties we've parsed |
nothing calls this directly
no test coverage detected