@@ -1228,5 +1228,46 @@ suite('WebGPU p5.Shader', function() {
12281228 } ) ;
12291229 }
12301230 } ) ;
1231+
1232+ suite ( 'compute shaders' , ( ) => {
1233+ test ( 'handle early return in void compute hook' , async ( ) => {
1234+ await myp5 . createCanvas ( 5 , 5 , myp5 . WEBGPU ) ;
1235+
1236+ // This test verifies that buildComputeShader and p5.compute
1237+ // correctly handle void hooks with early returns without crashing
1238+ // the strands compiler or hitting type errors.
1239+ expect ( ( ) => {
1240+ const computeShader = myp5 . buildComputeShader ( ( ) => {
1241+ const id = myp5 . index . x ;
1242+ if ( id > 10 ) {
1243+ return ; // Early return in void hook
1244+ }
1245+ } , { myp5 } ) ;
1246+
1247+ myp5 . compute ( computeShader , 1 ) ;
1248+ } ) . not . toThrow ( ) ;
1249+ } ) ;
1250+
1251+ test ( 'early return in void compute hook stops execution' , async ( ) => {
1252+ await myp5 . createCanvas ( 5 , 5 , myp5 . WEBGPU ) ;
1253+ const data = myp5 . createStorage ( [ 0 ] ) ;
1254+
1255+ const computeShader = myp5 . buildComputeShader ( ( ) => {
1256+ const buf = myp5 . uniformStorage ( ) ;
1257+ const id = myp5 . index . x ;
1258+ if ( id == 0 ) {
1259+ buf [ 0 ] = 1.0 ;
1260+ return ;
1261+ buf [ 0 ] = 2.0 ; // Should not execute
1262+ }
1263+ } , { myp5 } ) ;
1264+
1265+ computeShader . setUniform ( 'buf' , data ) ;
1266+
1267+ expect ( ( ) => {
1268+ myp5 . compute ( computeShader , 1 ) ;
1269+ } ) . not . toThrow ( ) ;
1270+ } ) ;
1271+ } ) ;
12311272 } ) ;
12321273} ) ;
0 commit comments