Skip to content

TSL: Switch statement compiled to If else in WGSL #33548

@thelazylamaGit

Description

@thelazylamaGit

Description

With TSL code such as

const EMPTY = int(0)
const TREE = int(1)
const FIRE = int(2)

Switch(mat.a.toInt())
  .Case(EMPTY, () => {
    If(rTree.lessThan(treeProb.uniform), () => {
      next.assign(palette(treeSeed).tree)
    })
  })
  .Case(TREE, () => {
    const burning = isBurning(x, y.sub(1))
      .or(isBurning(x.sub(1), y))
      .or(isBurning(x.add(1), y))
      .or(isBurning(x, y.add(1)))
      .toConst()

    //* If Neighbours burning or lightning strikes, burn
    If(burning.or(rFire.lessThan(fireProb.uniform)), () => {
      next.assign(palette(fireSeed).fire)
    })
  })
  .Case(FIRE, () => {
    next.assign(vec4(0, 0, 0, uniforms.deadDuration.negate()))
  })
  .Default(() => {
    next.assign(EMPTY)
  })

The generated WGSL becomes

if ( ( i32( nodeVar0.w ) == 0 ) ) {

			...

			if ( ( ( f32( ( ( nodeVar3 >> 22u ) ^ nodeVar3 ) ) / 4294967296.0 ) < object.nodeUniform4 ) ) {

			...
				

			}

			

		} else {

			...

				if ( ( nodeConst3 || ( ( f32( ( ( nodeVar14 >> 22u ) ^ nodeVar14 ) ) / 4294967296.0 ) < object.nodeUniform8 ) ) ) {

					...
					
				}

				

			} else {


				if ( ( i32( nodeVar0.w ) == 2 ) ) {

					...
					

				} else {

					...
					

				}

				
		    }

As WGSL supports it, I would expect the generated code to use a switch statement. Now i'm not too sure theres a major performance difference in the end anyways but I am curious if there is a specific reason for this? i.e. due to how TSL generates code it needs to use if else statements to ensure proper flow, if that is the case this issue can be closed, otherwise, it may be worthwile making TSL generate proper switch statements.

Reproduction steps

  1. Use TSL Switch Node
  2. View generated WGSL code

Code

see above

Live example

not applicable

Screenshots

No response

Version

r184

Device

Desktop

Browser

Chrome

OS

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions