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

(Wrong) Fix webgpu attribute code - but might inspire correct fix #25663

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 41 additions & 102 deletions examples/jsm/renderers/webgpu/WebGPURenderPipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ import {
ZeroFactor, OneFactor, SrcColorFactor, OneMinusSrcColorFactor, SrcAlphaFactor, OneMinusSrcAlphaFactor, DstAlphaFactor, OneMinusDstAlphaFactor, DstColorFactor, OneMinusDstColorFactor, SrcAlphaSaturateFactor
} from 'three';

const typedArraysToVertexFormatPrefix = new Map( [

[ Int8Array, [ 'sint8', 'snorm8' ] ],
[ Uint8Array, [ 'uint8', 'unorm8' ] ],
[ Int16Array, [ 'sint16', 'snorm16' ] ],
[ Uint16Array, [ 'uint16', 'unorm16' ] ],
[ Int32Array, [ 'sint32', 'snorm32' ] ],
[ Uint32Array, [ 'uint32', 'unorm32' ] ],
[ Float32Array, [ 'float32', ] ],

] );

const typeArraysToVertexFormatPrefixForItemSize1 = new Map( [

[ Int32Array, 'sint32' ],
[ Uint32Array, 'uint32' ],
[ Float32Array, 'float32' ],

] );

class WebGPURenderPipeline {

constructor( device, utils ) {
Expand Down Expand Up @@ -563,127 +583,46 @@ class WebGPURenderPipeline {

}

_getVertexFormat( type, bytesPerElement ) {

// float

if ( type === 'float' ) return GPUVertexFormat.Float32;

if ( type === 'vec2' ) {

if ( bytesPerElement === 2 ) {

return GPUVertexFormat.Float16x2;

} else {

return GPUVertexFormat.Float32x2;

}

}

if ( type === 'vec3' ) return GPUVertexFormat.Float32x3;

if ( type === 'vec4' ) {

if ( bytesPerElement === 2 ) {

return GPUVertexFormat.Float16x4;

} else {

return GPUVertexFormat.Float32x4;

}

}

// int

if ( type === 'int' ) return GPUVertexFormat.Sint32;

if ( type === 'ivec2' ) {

if ( bytesPerElement === 1 ) {
_getVertexFormat( geometryAttribute ) {

return GPUVertexFormat.Sint8x2;
const { itemSize, normalized } = geometryAttribute;
const ArrayType = geometryAttribute.array.__proto__.constructor;

} else if ( bytesPerElement === 2 ) {
let format;

return GPUVertexFormat.Sint16x2;
if ( itemSize == 1 ) {

} else {
format = typeArraysToVertexFormatPrefixForItemSize1.get(ArrayType)

Check notice

Code scanning / CodeQL

Semicolon insertion

Avoid automated semicolon insertion (91% of all statements in [the enclosing function](1) have an explicit semicolon).

return GPUVertexFormat.Sint32x2;

}

}

if ( type === 'ivec3' ) return GPUVertexFormat.Sint32x3;

if ( type === 'ivec4' ) {

if ( bytesPerElement === 1 ) {

return GPUVertexFormat.Sint8x4;

} else if ( bytesPerElement === 2 ) {

return GPUVertexFormat.Sint16x4;

} else {

return GPUVertexFormat.Sint32x4;

}

}

// uint

if ( type === 'uint' ) return GPUVertexFormat.Uint32;
} else {

if ( type === 'uvec2' ) {
const prefix = typedArraysToVertexFormatPrefix.get(ArrayType)[ normalized ? 1 : 0 ];

if ( bytesPerElement === 1 ) {
if ( prefix ) {

return GPUVertexFormat.Uint8x2;
const bytesPerUnit = ArrayType.BYTES_PER_ELEMENT * itemSize;
const paddedBytesPerUnit = Math.floor( ( bytesPerUnit + 3 ) / 4 ) * 4;
const paddedItemSize = paddedBytesPerUnit / ArrayType.BYTES_PER_ELEMENT;

} else if ( bytesPerElement === 2 ) {
if ( paddedItemSize % 1) {

return GPUVertexFormat.Uint16x2;
throw new Error( `bad item size `);

} else {
}

return GPUVertexFormat.Uint32x2;
format = `${prefix}x${paddedItemSize}`;

}

}

if ( type === 'uvec3' ) return GPUVertexFormat.Uint32x3;

if ( type === 'uvec4' ) {

if ( bytesPerElement === 1 ) {

return GPUVertexFormat.Uint8x4;
if ( !format ) {

} else if ( bytesPerElement === 2 ) {

return GPUVertexFormat.Uint16x4;

} else {

return GPUVertexFormat.Uint32x4;

}
console.error( 'THREE.WebGPURenderer: Shader variable type not supported yet.' );

}

console.error( 'THREE.WebGPURenderer: Shader variable type not supported yet.', type );
return format;

}

Expand All @@ -700,10 +639,10 @@ class WebGPURenderPipeline {
const type = nodeAttribute.type;

const geometryAttribute = geometry.getAttribute( name );
const bytesPerElement = geometryAttribute.array.BYTES_PER_ELEMENT;

const format = this._getVertexFormat( type, bytesPerElement );
const format = this._getVertexFormat( geometryAttribute );

const bytesPerElement = geometryAttribute.array.BYTES_PER_ELEMENT;
let arrayStride = geometryAttribute.itemSize * bytesPerElement;
let offset = 0;

Expand Down