Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/nodes/accessors/Arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export const attributeArray = ( count, type = 'float' ) => {

let itemSize, typedArray;

if ( type.isStruct === true ) {
if ( type.isStructTypeNode === true ) {

itemSize = type.layout.getLength();
itemSize = type.getLength();
typedArray = getTypedArrayFromType( 'float' );

} else {
Expand Down Expand Up @@ -48,9 +48,9 @@ export const instancedArray = ( count, type = 'float' ) => {

let itemSize, typedArray;

if ( type.isStruct === true ) {
if ( type.isStructTypeNode === true ) {

itemSize = type.layout.getLength();
itemSize = type.getLength();
typedArray = getTypedArrayFromType( 'float' );

} else {
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/accessors/StorageBufferNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class StorageBufferNode extends BufferNode {

let nodeType, structTypeNode = null;

if ( bufferType && bufferType.isStruct ) {
if ( bufferType && bufferType.isStructTypeNode ) {

nodeType = 'struct';
structTypeNode = bufferType.layout;
structTypeNode = bufferType;

if ( value.isStorageBufferAttribute || value.isStorageInstancedBufferAttribute ) {

Expand Down
14 changes: 14 additions & 0 deletions src/nodes/accessors/StorageTextureNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ class StorageTextureNode extends TextureNode {

}

/**
* Overwrites the default implementation since storage texture
* coordinates are texel coordinates and should not be transformed
* by the texture uv matrix.
*
* @param {Node} uvNode - The uv node.
* @return {Node} The unmodified uv node.
*/
getTransformedUV( uvNode ) {

return uvNode;

}

setup( builder ) {

super.setup( builder );
Expand Down
18 changes: 2 additions & 16 deletions src/nodes/code/FunctionNode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CodeNode from './CodeNode.js';
import { nodeProxyConstructor } from '../tsl/TSLCore.js';

/**
* This class represents a native shader function. It can be used to implement
Expand Down Expand Up @@ -155,26 +156,11 @@ export default FunctionNode;

const nativeFn = ( code, includes = [], language = '' ) => {

for ( let i = 0; i < includes.length; i ++ ) {

const include = includes[ i ];

// TSL Function: glslFn, wgslFn

if ( typeof include === 'function' ) {

includes[ i ] = include.functionNode;

}

}

const functionNode = new FunctionNode( code, includes, language );

const fn = ( ...params ) => functionNode.call( ...params );
fn.functionNode = functionNode;

return fn;
return nodeProxyConstructor( fn, functionNode );

};

Expand Down
26 changes: 13 additions & 13 deletions src/nodes/core/NodeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export function getTypedArrayFromType( type ) {
*/
export function getLengthFromType( type ) {

if ( /float|int|uint/.test( type ) ) return 1;
if ( /float|int|uint|bool/.test( type ) ) return 1;
if ( /vec2/.test( type ) ) return 2;
if ( /vec3/.test( type ) ) return 3;
if ( /vec4/.test( type ) ) return 4;
Expand All @@ -161,16 +161,16 @@ export function getLengthFromType( type ) {
}

/**
* Returns the gpu memory length for the given data type.
* Returns the gpu memory length for the given data type in 4-byte elements.
*
* @private
* @method
* @param {string} type - The data type.
* @return {number} The length.
* @return {number} The memory length in 4-byte elements.
*/
export function getMemoryLengthFromType( type ) {

if ( /float|int|uint/.test( type ) ) return 1;
if ( /float|int|uint|bool/.test( type ) ) return 1;
if ( /vec2/.test( type ) ) return 2;
if ( /vec3/.test( type ) ) return 3;
if ( /vec4/.test( type ) ) return 4;
Expand All @@ -183,22 +183,22 @@ export function getMemoryLengthFromType( type ) {
}

/**
* Returns the alignment requirement for the given data type.
* Returns the alignment requirement for the given data type in 4-byte elements.
*
* @private
* @method
* @param {string} type - The data type.
* @return {number} The alignment requirement in bytes.
* @return {number} The alignment requirement in 4-byte elements.
*/
export function getAlignmentFromType( type ) {

if ( /float|int|uint/.test( type ) ) return 4;
if ( /vec2/.test( type ) ) return 8;
if ( /vec3/.test( type ) ) return 16;
if ( /vec4/.test( type ) ) return 16;
if ( /mat2/.test( type ) ) return 8;
if ( /mat3/.test( type ) ) return 16;
if ( /mat4/.test( type ) ) return 16;
if ( /float|int|uint|bool/.test( type ) ) return 1;
if ( /vec2/.test( type ) ) return 2;
if ( /vec3/.test( type ) ) return 4;
if ( /vec4/.test( type ) ) return 4;
if ( /mat2/.test( type ) ) return 2;
if ( /mat3/.test( type ) ) return 4;
if ( /mat4/.test( type ) ) return 4;

error( `TSL: Unsupported type: ${ type }`, new StackTrace() );

Expand Down
10 changes: 4 additions & 6 deletions src/nodes/core/StructNode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Node from './Node.js';
import StructTypeNode from './StructTypeNode.js';
import { nodeProxyConstructor } from '../tsl/TSLCore.js';

/**
* StructNode allows to create custom structures with multiple members.
Expand Down Expand Up @@ -94,7 +95,7 @@ export default StructNode;
*/
export const struct = ( membersLayout, name = null ) => {

const structLayout = new StructTypeNode( membersLayout, name );
const structType = new StructTypeNode( membersLayout, name );

const struct = ( ...params ) => {

Expand Down Expand Up @@ -122,13 +123,10 @@ export const struct = ( membersLayout, name = null ) => {

}

return new StructNode( structLayout, values );
return new StructNode( structType, values );

};

struct.layout = structLayout;
struct.isStruct = true;

return struct;
return nodeProxyConstructor( struct, structType );

};
12 changes: 6 additions & 6 deletions src/nodes/core/StructTypeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ class StructTypeNode extends Node {
* @readonly
* @default true
*/
this.isStructLayoutNode = true;
this.isStructTypeNode = true;

}

/**
* Returns the length of the struct.
* The length is calculated by summing the lengths of the struct's members.
* Returns the length of the struct in 4-byte elements (e.g. float or int components).
* The length is calculated by summing the lengths of the struct's members, accounting for memory alignment.
* To get the size in bytes, multiply the returned value by 4.
*
* @returns {number} The length of the struct.
* @returns {number} The length of the struct in 4-byte elements.
*/
getLength() {

const BYTES_PER_ELEMENT = Float32Array.BYTES_PER_ELEMENT;
let maxAlignment = 1; // maximum alignment value in this struct
let offset = 0; // global buffer offset in 4 byte elements

Expand All @@ -95,7 +95,7 @@ class StructTypeNode extends Node {
const type = member.type;

const itemSize = getMemoryLengthFromType( type );
const alignment = getAlignmentFromType( type ) / BYTES_PER_ELEMENT;
const alignment = getAlignmentFromType( type );
maxAlignment = Math.max( maxAlignment, alignment );

const chunkOffset = offset % maxAlignment; // offset in the current chunk of maxAlignment elements
Expand Down
20 changes: 20 additions & 0 deletions src/nodes/tsl/TSLCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,26 @@ export const nodeProxy = ( NodeClass, scope = null, factor = null, settings = nu
export const nodeImmutable = ( NodeClass, ...params ) => new ShaderNodeImmutable( NodeClass, ...params );
export const nodeProxyIntent = ( NodeClass, scope = null, factor = null, settings = {} ) => new ShaderNodeProxy( NodeClass, scope, factor, { ...settings, intent: true } );

export const nodeProxyConstructor = ( constructorFunction, nodeInstance ) => {

return new Proxy( constructorFunction, {

get( target, prop, receiver ) {

return Reflect.get( nodeInstance, prop, receiver );

},

set( target, prop, value ) {

return Reflect.set( nodeInstance, prop, value );

}

} );

};

let fnId = 0;

class FnNode extends Node {
Expand Down
Loading