1- import { convertRgbToY } from '@typegpu/color' ;
21import { accessViewportSize } from '@typegpu/common' ;
2+ import { convertRgbToY } from '@typegpu/color' ;
33import tgpu , { type TgpuRoot } from 'typegpu' ;
44import * as d from 'typegpu/data' ;
55import * as std from 'typegpu/std' ;
@@ -66,62 +66,58 @@ const ioLayout = tgpu.bindGroupLayout({
6666const { weights, biases } = layerLayout . bound ;
6767
6868const sampleGlobal = tgpu [ '~unstable' ] . derived ( ( ) => {
69- return tgpu
70- . fn ( [ d . i32 , d . i32 , d . ptrFn ( d . arrayOf ( d . vec4f , inChannelsQuarter . value ) ) ] ) (
71- ( x , y , result ) => {
72- const canvasSize = accessViewportSize . value ;
73- const coord = d . vec2u (
74- d . u32 ( std . max ( 0 , std . min ( x , d . i32 ( canvasSize . x ) - 1 ) ) ) ,
75- d . u32 ( std . max ( 0 , std . min ( y , d . i32 ( canvasSize . y ) - 1 ) ) ) ,
76- ) ;
77-
78- if ( inputFromGBufferSlot . $ ) {
79- const blurred = std . textureLoad ( ioLayout . $ . blurred_tex , coord , 0 ) ;
80-
81- const aux = std . textureLoad ( ioLayout . $ . aux_tex , coord , 0 ) ;
82-
83- result [ 0 ] = d . vec4f (
84- convertRgbToY ( blurred . xyz ) ,
85- aux . z , // albedo luminance
86- aux . x , // normal.x
87- aux . y , // normal.y
88- ) ;
89- result [ 1 ] = d . vec4f (
90- aux . w , // emission luminance
91- 0 , // zero padding
92- 0 , // zero padding
93- 0 , // zero padding
94- ) ;
95- } else {
96- for ( let i = d . u32 ( 0 ) ; i < inChannelsQuarter . value ; i ++ ) {
97- const index =
98- ( coord . y * d . u32 ( canvasSize . x ) + coord . x ) *
99- inChannelsQuarter . value +
100- i ;
101-
102- result [ i ] = ioLayout . $ . input_buffer [ index ] ;
103- }
104- }
105- } ,
106- )
107- . $name ( 'sample_global' ) ;
69+ return tgpu . fn ( [
70+ d . i32 ,
71+ d . i32 ,
72+ d . ptrFn ( d . arrayOf ( d . vec4f , inChannelsQuarter . $ ) ) ,
73+ ] ) ( ( x , y , result ) => {
74+ const canvasSize = accessViewportSize . $ ;
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 ) ) ) ,
78+ ) ;
79+
80+ if ( inputFromGBufferSlot . $ ) {
81+ const blurred = std . textureLoad ( ioLayout . $ . blurred_tex , coord , 0 ) ;
82+
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 . $ ; i ++ ) {
99+ const index =
100+ ( coord . y * d . u32 ( canvasSize . x ) + coord . x ) * inChannelsQuarter . $ + i ;
101+
102+ result [ i ] = ioLayout . $ . input_buffer [ index ] ;
103+ }
104+ }
105+ } ) ;
108106} ) ;
109107
110108const f32Array = ( n : number ) => d . arrayOf ( d . f32 , n ) ;
111109
112110const applyReLU = tgpu [ '~unstable' ] . derived ( ( ) => {
113- const outChannels = outChannelsSlot . value ;
111+ const outChannels = outChannelsSlot . $ ;
114112
115- return tgpu [ '~unstable' ]
116- . fn ( [ d . ptrFn ( f32Array ( outChannels ) ) ] ) ( ( result ) => {
117- for ( let i = 0 ; i < outChannels ; i ++ ) {
118- result [ i ] = std . max ( 0 , result [ i ] ) ;
119- }
120- } )
121- . $name ( 'apply_relu' ) ;
113+ return tgpu . fn ( [ d . ptrFn ( f32Array ( outChannels ) ) ] ) ( ( result ) => {
114+ for ( let i = 0 ; i < outChannels ; i ++ ) {
115+ result [ i ] = std . max ( 0 , result [ i ] ) ;
116+ }
117+ } ) ;
122118} ) ;
123119
124- const readKernel = tgpu . fn ( [ d . u32 ] , d . vec4f ) ( ( idx ) => weights . value [ idx ] ) ;
120+ const readKernel = tgpu . fn ( [ d . u32 ] , d . vec4f ) ( ( idx ) => weights . $ [ idx ] ) ;
125121
126122const menderConvolveFn = convolveFn ( {
127123 sampleFiller : sampleGlobal ,
@@ -168,27 +164,24 @@ const entryComputeFn = tgpu['~unstable'].computeFn({
168164export const MenderStep = ( { root, gBuffer, targetTexture } : Options ) => {
169165 // Resource locators
170166
171- const viewportSizeBuffer = root . createBuffer ( d . vec2f ) . $usage ( 'uniform' ) ;
167+ const viewportSizeUniform = root . createUniform ( d . vec2f ) ;
172168
173169 // Textures
174170
175171 const firstWorkBuffer = root
176172 . createBuffer (
177173 d . arrayOf ( d . f32 , gBuffer . size [ 0 ] * gBuffer . size [ 1 ] * FIRST_DEPTH ) ,
178174 )
179- . $name ( 'First Work Buffer' )
180175 . $usage ( 'storage' ) ;
181176
182177 const secondWorkBuffer = root
183178 . createBuffer (
184179 d . arrayOf ( d . f32 , gBuffer . size [ 0 ] * gBuffer . size [ 1 ] * SECOND_DEPTH ) ,
185180 )
186- . $name ( 'Second Work Buffer' )
187181 . $usage ( 'storage' ) ;
188182
189183 const mendedResultBuffer = root
190184 . createBuffer ( d . arrayOf ( d . f32 , gBuffer . size [ 0 ] * gBuffer . size [ 1 ] ) )
191- . $name ( 'Mender Result Buffer' )
192185 . $usage ( 'storage' ) ;
193186
194187 //
@@ -217,7 +210,7 @@ export const MenderStep = ({ root, gBuffer, targetTexture }: Options) => {
217210 . with ( outChannelsSlot , options . outChannels )
218211 . with ( reluSlot , options . relu )
219212 . with ( inputFromGBufferSlot , options . inputFromGBuffer )
220- . with ( accessViewportSize , viewportSizeBuffer . as ( 'uniform' ) )
213+ . with ( accessViewportSize , viewportSizeUniform )
221214 // ---
222215 . withCompute ( entryComputeFn )
223216 . createPipeline ( )
@@ -281,7 +274,7 @@ export const MenderStep = ({ root, gBuffer, targetTexture }: Options) => {
281274 // ---
282275
283276 const combinationPipeline = root [ '~unstable' ]
284- . with ( accessViewportSize , viewportSizeBuffer . as ( 'uniform' ) )
277+ . with ( accessViewportSize , viewportSizeUniform )
285278 . withVertex ( fullScreenQuadVertexFn , { } )
286279 . withFragment ( combinationEntryFn , { format : 'rgba8unorm' } )
287280 . createPipeline ( ) ;
@@ -291,7 +284,7 @@ export const MenderStep = ({ root, gBuffer, targetTexture }: Options) => {
291284 mendedBuffer : mendedResultBuffer ,
292285 } ) ;
293286
294- viewportSizeBuffer . write ( d . vec2f ( ...gBuffer . size ) ) ;
287+ viewportSizeUniform . write ( d . vec2f ( ...gBuffer . size ) ) ;
295288
296289 return {
297290 perform ( ) {
0 commit comments