11import { linearToSrgb , srgbToLinear } from '@typegpu/color' ;
2- import tgpu , { d } from 'typegpu' ;
3- import { add , discard , div , max , min , mul , normalize , pow , sub } from 'typegpu/std' ;
2+ import tgpu , { d , type TgpuFragmentFn , type TgpuVertexFn } from 'typegpu' ;
3+ import { discard , max , min , mul , normalize , pow , sub } from 'typegpu/std' ;
44import { mat4 } from 'wgpu-matrix' ;
55import { defineControls } from '../../common/defineControls.ts' ;
66
@@ -133,41 +133,39 @@ const getBoxIntersection = tgpu
133133}` )
134134 . $uses ( { IntersectionStruct } ) ;
135135
136- const Varying = {
137- rayWorldOrigin : d . vec3f ,
138- } ;
139-
140- const mainVertex = tgpu . vertexFn ( {
141- in : { vertexIndex : d . builtin . vertexIndex } ,
142- out : { pos : d . builtin . position , ...Varying } ,
143- } ) ( ( input ) => {
136+ const mainVertex = ( { $vertexIndex : vid } : TgpuVertexFn . AutoIn < { } > ) => {
137+ 'use gpu' ;
144138 const pos = [ d . vec2f ( - 1 , - 1 ) , d . vec2f ( 3 , - 1 ) , d . vec2f ( - 1 , 3 ) ] ;
145139
146- const rayWorldOrigin = mul ( uniforms . $ . invViewMatrix , d . vec4f ( 0 , 0 , 0 , 1 ) ) . xyz ;
140+ const rayWorldOrigin = ( uniforms . $ . invViewMatrix * d . vec4f ( 0 , 0 , 0 , 1 ) ) . xyz ;
147141
148- return { pos : d . vec4f ( pos [ input . vertexIndex ] , 0.0 , 1.0 ) , rayWorldOrigin } ;
149- } ) ;
142+ return {
143+ $position : d . vec4f ( pos [ vid ] , 0 , 1 ) ,
144+ rayWorldOrigin,
145+ } satisfies TgpuVertexFn . AutoOut ;
146+ } ;
150147
151- const fragmentFunction = tgpu . fragmentFn ( {
152- in : { position : d . builtin . position , ...Varying } ,
153- out : d . vec4f ,
154- } ) ( ( input ) => {
155- const boxSize3 = d . vec3f ( d . f32 ( uniforms . $ . boxSize ) ) ;
156- const halfBoxSize3 = mul ( 0.5 , boxSize3 ) ;
157- const halfCanvasDims = mul ( 0.5 , uniforms . $ . canvasDims ) ;
148+ const fragmentFunction = ( {
149+ $position,
150+ rayWorldOrigin,
151+ } : TgpuFragmentFn . AutoIn < { rayWorldOrigin : d . v3f } > ) => {
152+ 'use gpu' ;
153+ const boxSize3 = d . vec3f ( uniforms . $ . boxSize ) ;
154+ const halfBoxSize3 = 0.5 * boxSize3 ;
155+ const halfCanvasDims = 0.5 * uniforms . $ . canvasDims ;
158156
159157 const minDim = min ( uniforms . $ . canvasDims . x , uniforms . $ . canvasDims . y ) ;
160- const viewCoords = div ( sub ( input . position . xy , halfCanvasDims ) , minDim ) ;
158+ const viewCoords = ( $ position. xy - halfCanvasDims ) / minDim ;
161159
162160 const ray = Ray ( {
163- origin : input . rayWorldOrigin ,
164- direction : mul ( uniforms . $ . invViewMatrix , d . vec4f ( normalize ( d . vec3f ( viewCoords , 1 ) ) , 0 ) ) . xyz ,
161+ origin : rayWorldOrigin ,
162+ direction : ( uniforms . $ . invViewMatrix * d . vec4f ( normalize ( d . vec3f ( viewCoords , 1 ) ) , 0 ) ) . xyz ,
165163 } ) ;
166164
167165 const bigBoxIntersection = getBoxIntersection (
168166 AxisAlignedBounds ( {
169- min : mul ( - 1 , halfBoxSize3 ) ,
170- max : add ( cubeSize , halfBoxSize3 ) ,
167+ min : - 1 * halfBoxSize3 ,
168+ max : cubeSize + halfBoxSize3 ,
171169 } ) ,
172170 ray ,
173171 ) ;
@@ -188,12 +186,12 @@ const fragmentFunction = tgpu.fragmentFn({
188186 continue ;
189187 }
190188
191- const ijkScaled = d . vec3f ( d . f32 ( i ) , d . f32 ( j ) , d . f32 ( k ) ) ;
189+ const ijkScaled = d . vec3f ( i , j , k ) ;
192190
193191 const intersection = getBoxIntersection (
194192 AxisAlignedBounds ( {
195- min : sub ( ijkScaled , halfBoxSize3 ) ,
196- max : add ( ijkScaled , halfBoxSize3 ) ,
193+ min : ijkScaled - halfBoxSize3 ,
194+ max : ijkScaled + halfBoxSize3 ,
197195 } ) ,
198196 ray ,
199197 ) ;
@@ -202,25 +200,25 @@ const fragmentFunction = tgpu.fragmentFn({
202200 const boxDensity =
203201 max ( 0 , intersection . tMax - intersection . tMin ) * pow ( uniforms . $ . materialDensity , 2 ) ;
204202 density += boxDensity ;
205- invColor = add ( invColor , mul ( boxDensity , div ( d . vec3f ( 1 ) , boxMatrix . $ [ i ] [ j ] [ k ] . albedo ) ) ) ;
203+ invColor += boxDensity * ( 1 / boxMatrix . $ [ i ] [ j ] [ k ] . albedo ) ;
206204 intersectionFound = true ;
207205 }
208206 }
209207 }
210208 }
211209
212- const linear = div ( d . vec3f ( 1 ) , invColor ) ;
210+ const linear = 1 / invColor ;
213211 const srgb = linearToSrgb ( linear ) ;
214212 const gamma = 2.2 ;
215- const corrected = pow ( srgb , d . vec3f ( 1.0 / gamma ) ) ;
213+ const corrected = pow ( srgb , d . vec3f ( 1 / gamma ) ) ;
216214
217215 if ( intersectionFound ) {
218- return mul ( min ( density , 1 ) , d . vec4f ( min ( corrected , d . vec3f ( 1 ) ) , 1 ) ) ;
216+ return min ( density , 1 ) * d . vec4f ( min ( corrected , d . vec3f ( 1 ) ) , 1 ) ;
219217 }
220218
221219 discard ( ) ;
222220 return d . vec4f ( ) ;
223- } ) ;
221+ } ;
224222
225223// pipeline
226224
0 commit comments