1- import { ShaderAssembler } from " @luma.gl/shadertools" ;
1+ import { ShaderAssembler } from ' @luma.gl/shadertools' ;
22// oops, this may cause problems for publishing... or maybe not if this is only internal.
3- import type { AssembleShaderProps } from " @luma.gl/shadertools/dist/lib/shader-assembly/assemble-shaders" ;
4- import { MAX_CHANNELS } from " @vivjs/constants" ;
3+ import type { AssembleShaderProps } from ' @luma.gl/shadertools/dist/lib/shader-assembly/assemble-shaders' ;
4+ import { MAX_CHANNELS } from ' @vivjs/constants' ;
55
66// open question - should VivShaderAssembler be exposed as public API?
77// I doubt many people would have a reason to want it... but I likely will.
@@ -10,29 +10,59 @@ import { MAX_CHANNELS } from "@vivjs/constants";
1010// Users should probably be discouraged from managing their own ShaderAssemblers
1111// especially until we are more confident in their use.
1212// So in deciding the public form of viv extension API (which should be ergonic/well-typed & stable),
13- // it should also allow where possible for somewhat arbitrary extensibility
13+ // it should also allow where possible for somewhat arbitrary extensibility
1414// without getting too complex... i.e. some kind of shader generation pipeline thing.
1515// Viv shouldn't be the place where any complex functionality along those lines
1616// is maintained... but it should expose enough to allow other applications to experiment
1717// with appropriate documentation on what is considered stable & supported,
1818// vs what is there for experimental purposes.
1919
20- export const VIV_CHANNEL_INDEX_PLACEHOLDER = Symbol ( '<VIV_CHANNEL_INDEX>' ) ;
20+ //may want to keep this as something that wouldn't make unprocessed shader code invalid syntax
21+ //maybe we want to use a parser to process the shader code?
22+ //(probably viv is not the layer at which we introduce something like that)
23+ export const VIV_CHANNEL_INDEX_PLACEHOLDER = Symbol ( 'VIV_CHANNEL_INDEX' ) ;
24+ const I = String ( VIV_CHANNEL_INDEX_PLACEHOLDER ) ;
25+ //not a practical example, but the line-duplication will not work if there's a multiline expression
26+ //with mutable state. Not clear how we'd handle that in a simple way.
27+ `
28+ float mutableThing = 0.;
29+ mutableThing = opacity${ I } ;
30+ float value${ I } = mutableThing;
31+
32+ mutableThing = opacity0;
33+ float value0 = mutableThing;
34+ mutableThing = opacity1;
35+ float value1 = mutableThing;
36+
37+ mutableThing = opacity0;
38+ mutableThing = opacity1;
39+ float value0 = mutableThing;
40+ float value1 = mutableThing;
41+ ` ;
2142
2243/**
23- * Tagged-template function that expands the VIV_CHANNEL_INDEX_PLACEHOLDER symbol to the given number of channels.
24- *
25- * This is probably more appropriate for a public API once it's working.
44+ * (totally broken) Tagged-template function that expands the VIV_CHANNEL_INDEX_PLACEHOLDER symbol to the given number of channels.
45+ *
46+ * Something like this may be more appropriate for a public API once it's working.
2647 */
2748export function vivShader ( numChannels : number ) {
28- return ( strings : TemplateStringsArray , ...expressions : ( string | symbol ) [ ] ) : string => {
49+ // this is not doing remotely the right thing...
50+ // maybe the proper approach is indeed rather than trying to have something that wraps an entire shader
51+ // having ways of processing more local blocks of code
52+ // consider that repeating individual lines may be undesirable - have a way of grouping that makes sense
53+ //... comments in string above...
54+ return (
55+ strings : TemplateStringsArray ,
56+ ...expressions : ( string | symbol ) [ ]
57+ ) : string => {
2958 let result = '' ;
3059 for ( let i = 0 ; i < strings . length ; i ++ ) {
3160 const str = strings [ i ] ;
3261 const expr = i < expressions . length ? expressions [ i ] : null ;
3362
3463 // Check if this line contains the channel index placeholder
35- const hasPlaceholder = expr === VIV_CHANNEL_INDEX_PLACEHOLDER ||
64+ const hasPlaceholder =
65+ expr === VIV_CHANNEL_INDEX_PLACEHOLDER ||
3666 ( typeof expr === 'string' && expr . includes ( '<VIV_CHANNEL_INDEX>' ) ) ;
3767
3868 if ( hasPlaceholder ) {
@@ -63,9 +93,9 @@ export function vivShader(numChannels: number) {
6393
6494function processGLSLShaderLine ( line : string , numChannels : number ) {
6595 if ( ! line . includes ( VIV_CHANNEL_INDEX_PLACEHOLDER . toString ( ) ) ) return line ;
66- let str = "" ;
67- for ( let i = 0 ; i < numChannels ; i ++ ) {
68- str += `${ line . replaceAll ( VIV_CHANNEL_INDEX_PLACEHOLDER . toString ( ) , i . toString ( ) ) } \n`
96+ let str = '' ;
97+ for ( let i = 0 ; i < numChannels ; i ++ ) {
98+ str += `${ line . replaceAll ( VIV_CHANNEL_INDEX_PLACEHOLDER . toString ( ) , i . toString ( ) ) } \n` ;
6999 }
70100 // Remove the trailing comma if present
71101 if ( str . endsWith ( ',\n' ) ) {
@@ -74,7 +104,10 @@ function processGLSLShaderLine(line: string, numChannels: number) {
74104 return str ;
75105}
76106function processGLSLShader ( shader : string , n : number ) {
77- return shader . split ( '\n' ) . map ( line => processGLSLShaderLine ( line , n ) ) . join ( '\n' )
107+ return shader
108+ . split ( '\n' )
109+ . map ( line => processGLSLShaderLine ( line , n ) )
110+ . join ( '\n' ) ;
78111}
79112export default class VivShaderAssembler extends ShaderAssembler {
80113 static defaultVivAssemblers : Record < number , VivShaderAssembler > = { } ;
@@ -102,7 +135,8 @@ export default class VivShaderAssembler extends ShaderAssembler {
102135 }
103136 static getVivAssembler ( NUM_CHANNELS = MAX_CHANNELS ) {
104137 if ( ! VivShaderAssembler . defaultVivAssemblers [ NUM_CHANNELS ] ) {
105- VivShaderAssembler . defaultVivAssemblers [ NUM_CHANNELS ] = new VivShaderAssembler ( NUM_CHANNELS ) ;
138+ VivShaderAssembler . defaultVivAssemblers [ NUM_CHANNELS ] =
139+ new VivShaderAssembler ( NUM_CHANNELS ) ;
106140 }
107141 return VivShaderAssembler . defaultVivAssemblers [ NUM_CHANNELS ] ;
108142 // return ShaderAssembler.getDefaultShaderAssembler();
0 commit comments