1+ import tgpu , { type TgpuRoot } from 'typegpu' ;
2+ import * as d from 'typegpu/data' ;
3+ import * as std from 'typegpu/std' ;
14import { convertRgbToY } from '@typegpu/color' ;
25import { accessViewportSize } from '@typegpu/common' ;
36import { randf } from '@typegpu/noise' ;
@@ -15,8 +18,6 @@ import {
1518 MarchResult ,
1619 ShapeContext ,
1720} from 'src/lib-ray-marching' ;
18- import tgpu , { type TgpuRoot } from 'typegpu' ;
19- import * as d from 'typegpu/data' ;
2021
2122import { store } from 'src/lib/store.ts' ;
2223import type { GBuffer } from '../../gBuffer.ts' ;
@@ -69,10 +70,7 @@ const reflect = tgpu['~unstable']
6970 . $name ( 'reflect' ) ;
7071
7172const renderSubPixel = tgpu [ '~unstable' ]
72- . fn (
73- [ d . vec2f ] ,
74- d . vec3f ,
75- ) ( /* wgsl */ `(coord: vec2f) -> vec3f {
73+ . fn ( [ d . vec2f ] , d . vec3f ) ( /* wgsl */ `(coord: vec2f) -> vec3f {
7674 // doing the first march before each sub-sample, since the first march result is the same for all of them
7775
7876 var init_shape_ctx: ShapeContext;
@@ -188,46 +186,51 @@ const mainComputeFn = tgpu['~unstable']
188186 . computeFn ( {
189187 workgroupSize : [ BlockSize , BlockSize ] ,
190188 in : { gid : d . builtin . globalInvocationId } ,
191- } ) ( /* wgsl */ `{
192- setupRandomSeed(vec2f(in.gid.xy) * ${ Math . random ( ) } + getRandomSeedPrimer * ${ Math . random ( ) } );
189+ } ) ( ( input ) => {
190+ randf . seed2 (
191+ std . add (
192+ std . mul ( d . vec2f ( input . gid . xy ) , 0.1646936793 ) ,
193+ std . mul ( getRandomSeedPrimer . value , 0.934534732 ) ,
194+ ) ,
195+ ) ;
193196
194- let prev_layers = getAccumulatedLayers;
195- let prev_render = textureLoad(previousRender, in.gid.xy, 0);
196-
197- var acc = vec3f(0., 0., 0.);
198- for (var sx = 0u; sx < SUPER_SAMPLES; sx++) {
199- for (var sy = 0u; sy < SUPER_SAMPLES; sy++) {
200- let offset = vec2f(
201- (f32(sx) + 0.5) * ONE_OVER_SUPER_SAMPLES,
202- (f32(sy) + 0.5) * ONE_OVER_SUPER_SAMPLES,
197+ const prev_layers = getAccumulatedLayers . value ;
198+ const prev_render = std . textureLoad (
199+ mainLayout . $ . previousRender ,
200+ input . gid . xy ,
201+ 0 ,
202+ ) ;
203+
204+ let acc = d . vec3f ( 0. , 0. , 0. ) ;
205+ for ( let sx = d . u32 ( 0 ) ; sx < SUPER_SAMPLES ; sx ++ ) {
206+ for ( let sy = d . u32 ( 0 ) ; sy < SUPER_SAMPLES ; sy ++ ) {
207+ const offset = d . vec2f (
208+ ( d . f32 ( sx ) + 0.5 ) * ONE_OVER_SUPER_SAMPLES ,
209+ ( d . f32 ( sy ) + 0.5 ) * ONE_OVER_SUPER_SAMPLES ,
210+ ) ;
211+
212+ acc = std . add (
213+ acc ,
214+ renderSubPixel ( std . add ( d . vec2f ( input . gid . xy ) , offset ) ) ,
203215 ) ;
204-
205- acc += renderSubPixel(vec2f(in.gid.xy) + offset);
206216 }
207217 }
208-
209- acc *= ONE_OVER_SUPER_SAMPLES * ONE_OVER_SUPER_SAMPLES;
210-
218+
219+ acc = std . mul ( acc , ONE_OVER_SUPER_SAMPLES * ONE_OVER_SUPER_SAMPLES ) ;
220+
211221 // applying gamma correction
212- let gamma = 2.2;
213- acc = pow(acc, vec3(1.0 / gamma));
214-
215- var new_render = vec4 (acc, 1.0);
222+ const gamma = 2.2 ;
223+ acc = std . pow ( acc , d . vec3f ( d . f32 ( 1 ) / gamma ) ) ;
224+
225+ let new_render = d . vec4f ( acc , 1.0 ) ;
216226 if ( prev_layers > 0 ) {
217- new_render = (prev_render * prev_layers + vec4(acc, 1.0)) / (prev_layers + 1);
227+ new_render = std . div (
228+ std . add ( std . mul ( prev_render , prev_layers ) , d . vec4f ( acc , 1.0 ) ) ,
229+ prev_layers + 1 ,
230+ ) ;
218231 }
219-
220- textureStore(mainOutput, in.gid.xy, new_render);
221- }` )
222- . $uses ( {
223- SUPER_SAMPLES ,
224- ONE_OVER_SUPER_SAMPLES ,
225- previousRender : mainLayout . bound . previousRender ,
226- mainOutput : mainLayout . bound . mainOutput ,
227- setupRandomSeed : randf . seed2 ,
228- renderSubPixel,
229- getRandomSeedPrimer,
230- getAccumulatedLayers,
232+
233+ std . textureStore ( mainLayout . $ . mainOutput , input . gid . xy , new_render ) ;
231234 } ) ;
232235
233236const auxLayout = tgpu
0 commit comments