@@ -2,6 +2,7 @@ import { convertRgbToY } from '@typegpu/color';
22import { accessViewportSize } from '@typegpu/common' ;
33import tgpu , { type TgpuRoot } from 'typegpu' ;
44import * as d from 'typegpu/data' ;
5+ import * as std from 'typegpu/std' ;
56
67import {
78 convolveFn ,
@@ -68,63 +69,43 @@ const { weights, biases } = layerLayout.bound;
6869
6970const sampleGlobal = tgpu [ '~unstable' ] . derived ( ( ) => {
7071 return tgpu [ '~unstable' ]
71- . fn ( [
72- d . i32 ,
73- d . i32 ,
74- d . ptrFn ( d . arrayOf ( d . vec4f , inChannelsQuarter . value ) ) ,
75- ] ) ( `(x: i32, y: i32, result: ptr<function, array<vec4f, inChannelsQuarter>>) {
76- let canvasSize = accessViewportSize;
77- let coord = vec2u(
78- u32(max(0, min(x, i32(canvasSize.x) - 1))),
79- u32(max(0, min(y, i32(canvasSize.y) - 1))),
80- );
81-
82- if (inputFromGBufferSlot) {
83- let blurred = textureLoad(
84- blurred_tex,
85- coord,
86- 0
72+ . fn ( [ d . i32 , d . i32 , d . ptrFn ( d . arrayOf ( d . vec4f , inChannelsQuarter . value ) ) ] ) (
73+ ( x , y , result ) => {
74+ const canvasSize = accessViewportSize . value ;
75+ const coord = d . vec2u (
76+ d . u32 ( std . max ( 0 , std . min ( x , d . i32 ( canvasSize . x ) - 1 ) ) ) ,
77+ d . u32 ( std . max ( 0 , std . min ( y , d . i32 ( canvasSize . y ) - 1 ) ) ) ,
8778 ) ;
8879
89- var aux = textureLoad(
90- aux_tex,
91- coord,
92- 0
93- );
80+ if ( inputFromGBufferSlot ) {
81+ const blurred = std . textureLoad ( ioLayout . $ . blurred_tex , coord , 0 ) ;
9482
95- (*result)[0] = vec4f(
96- convertRgbToY(blurred.rgb),
97- aux.z, // albedo luminance
98- aux.x, // normal.x
99- aux.y, // normal.y
100- );
101- (*result)[1] = vec4f(
102- aux.w, // emission luminance
103- 0, // zero padding
104- 0, // zero padding
105- 0, // zero padding
106- );
107- }
108- else {
109- for (var i: u32 = 0; i < inChannelsQuarter; i++) {
110- let index =
111- (coord.y * u32(canvasSize.x) +
112- coord.x) * inChannelsQuarter +
113- i;
114-
115- (*result)[i] = input_buffer[index];
83+ const aux = std . textureLoad ( ioLayout . $ . aux_tex , coord , 0 ) ;
84+
85+ result [ 0 ] = d . vec4f (
86+ convertRgbToY ( blurred . xyz ) ,
87+ aux . z , // albedo luminance
88+ aux . x , // normal.x
89+ aux . y , // normal.y
90+ ) ;
91+ result [ 1 ] = d . vec4f (
92+ aux . w , // emission luminance
93+ 0 , // zero padding
94+ 0 , // zero padding
95+ 0 , // zero padding
96+ ) ;
97+ } else {
98+ for ( let i = d . u32 ( 0 ) ; i < inChannelsQuarter . value ; i ++ ) {
99+ const index =
100+ ( coord . y * d . u32 ( canvasSize . x ) + coord . x ) *
101+ inChannelsQuarter . value +
102+ i ;
103+
104+ result [ i ] = ioLayout . $ . input_buffer [ index ] ;
105+ }
116106 }
117- }
118- }` )
119- . $uses ( {
120- inChannelsQuarter,
121- accessViewportSize,
122- inputFromGBufferSlot,
123- blurred_tex : ioLayout . bound . blurred_tex ,
124- aux_tex : ioLayout . bound . aux_tex ,
125- input_buffer : ioLayout . bound . input_buffer ,
126- convertRgbToY,
127- } )
107+ } ,
108+ )
128109 . $name ( 'sample_global' ) ;
129110} ) ;
130111
0 commit comments