Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGPURenderer: Add Array Type Variable Declaration Support #30376

Conversation

RenaudRohlinger
Copy link
Collaborator

@RenaudRohlinger RenaudRohlinger commented Jan 22, 2025

Fixed: #30097

Description

This PR introduces the support of array type in variable declarations in both WGSL and GLSL backends.

Example usage:

const a = vec2().toVar( 'a' ).toArray( 10 );

// assign first element of the array of vec2
a.element(0).assign(vec2(10, 10))

// access to the first element of the array of vec2
const b = a.element(0).add(1);

Related comment: #30097 (comment)

This PR cover array of var so far.
Next we could also support array of Const with a new syntax such as Const([vec2(),vec2()]) by updating NodeVar to take an optional array as node argument and by updating the generateConst of the NodeBuilder to support assigning array types:

generateConst(type, value = null, length = undefined) {
    // Handle array types
    if (length && length > 1) {
        if (value === null || !Array.isArray(value)) {
            // Create default array with the base type's default value
            const defaultValue = this.generateConst(type);
            return `array<${this.getType(type)}, ${length}>(${Array(length).fill(defaultValue).join(', ')})`;
        }

        // Generate array with provided values
        const arrayValues = value.map(v => this.generateConst(type, v)).join(', ');
        return `array<${this.getType(type)}, ${length}>(${arrayValues})`;
    }
    ....

This contribution is funded by Utsubo

Copy link

github-actions bot commented Jan 22, 2025

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 336.25
78.28
336.25
78.28
+0 B
+0 B
WebGPU 510.39
141.66
510.82
141.8
+434 B
+136 B
WebGPU Nodes 509.86
141.55
510.29
141.69
+434 B
+136 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 465.25
112.13
465.25
112.13
+0 B
+0 B
WebGPU 584.39
158.38
584.82
158.5
+434 B
+126 B
WebGPU Nodes 539.77
147.93
540.21
148.06
+434 B
+131 B

@sunag
Copy link
Collaborator

sunag commented Jan 23, 2025

I had created an implementation for this, I was going to publish it today ^^

@sunag
Copy link
Collaborator

sunag commented Jan 23, 2025

Sorry for not letting you know in advance, I also didn't think you'd be working on this :/
#30386

The implementation I made already matches .toConst() and .toVar() and does the auto-caching if neither of them are used.
You can enter an array as a parameter, this allows users to create array constants with predefined parameters.

The idea of ​​having an array as parameters can help us create multi-dimensional arrays (soon).

@sunag
Copy link
Collaborator

sunag commented Jan 23, 2025

I know this is irrelevant now, but perhaps my mistake was not publishing the draft earlier.

image

image

@RenaudRohlinger
Copy link
Collaborator Author

RenaudRohlinger commented Jan 23, 2025

closed in favor of #30386

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TSL 'property', using array of float or vec, or mat etc..
2 participants