Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/webgpu/compute/wavefront/ProcessHitsKernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { queuedRayStruct, queuedHitStruct } from './structs.js';
import { proxy, proxyFn } from '../../lib/nodes/NodeProxy.js';
import { weightedAlphaBlendFn } from '../../nodes/sampling.wgsl.js';
import { wgslTagFn } from '../../lib/nodes/WGSLTagFnNode.js';
import { getPcgSeed, setPcgSeed } from '../../nodes/random.wgsl.js';

export class ProcessHitsKernel extends ComputeKernel {

Expand All @@ -27,8 +28,8 @@ export class ProcessHitsKernel extends ComputeKernel {
hitQueue: storage( new IndirectStorageBufferAttribute( 1, queuedHitStruct.getLength() ), queuedHitStruct ),
queueSizes: storage( new IndirectStorageBufferAttribute( 4, 1 ), 'u32' ).toAtomic(),

textures: texture( new DataTexture( ) ),
textureSampler: sampler( new DataTexture( ) ),
textures: texture( new DataTexture() ),
textureSampler: sampler( new DataTexture() ),

globalId: globalId,
};
Expand Down Expand Up @@ -69,7 +70,7 @@ export class ProcessHitsKernel extends ComputeKernel {
let input = hitQueue[ hitIndex ];
let indexUV = vec2u( input.pixel_x, input.pixel_y );

g_state.s0 = input.pcgStateS0;
${ setPcgSeed }( input.pcgStateS0 );

let object = transforms[ input.objectIndex ];
var material = materials[ object.materialIndex ];
Expand Down Expand Up @@ -105,7 +106,7 @@ export class ProcessHitsKernel extends ComputeKernel {
rayQueue[ index ].pixel = indexUV;
rayQueue[ index ].throughputColor = input.throughputColor * scatterRec.color / scatterRec.pdf;
rayQueue[ index ].currentBounce = input.currentBounce + 1;
rayQueue[ index ].pcgStateS0 = g_state.s0;
rayQueue[ index ].pcgStateS0 = ${ getPcgSeed }();
rayQueue[ index ].resultColor = resultColor;

}
Expand Down
4 changes: 2 additions & 2 deletions src/webgpu/compute/wavefront/RayGenerationKernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IndirectStorageBufferAttribute, StorageTexture } from 'three/webgpu';
import { uniform, storage, globalId, textureStore } from 'three/tsl';
import { ComputeKernel } from '../ComputeKernel.js';
import { ndcToCameraRay } from '../../lib/wgsl/common.wgsl.js';
import { pcgInit, pcgRand2 } from '../../nodes/random.wgsl.js';
import { getPcgSeed, pcgInit, pcgRand2 } from '../../nodes/random.wgsl.js';
import { queuedRayStruct } from './structs.js';
import { wgslTagFn } from '../../lib/nodes/WGSLTagFnNode.js';

Expand Down Expand Up @@ -92,7 +92,7 @@ export class RayGenerationKernel extends ComputeKernel {
rayQueue[ index ].pixel = indexUV;
rayQueue[ index ].throughputColor = vec3f( 1.0 );
rayQueue[ index ].currentBounce = 0;
rayQueue[ index ].pcgStateS0 = g_state.s0;
rayQueue[ index ].pcgStateS0 = ${ getPcgSeed }();
rayQueue[ index ].resultColor = vec4f( 0.0, 0.0, 0.0, 1.0 );

// write the active params
Expand Down
4 changes: 2 additions & 2 deletions src/webgpu/compute/wavefront/RayIntersectionKernel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DataTexture, Matrix3, IndirectStorageBufferAttribute, StorageTexture } from 'three/webgpu';
import { ComputeKernel } from '../ComputeKernel.js';
import { uniform, texture, sampler, storage, textureStore, globalId } from 'three/tsl';
import { pcgRand2, pcgInit } from '../../nodes/random.wgsl.js';
import { pcgRand2, setPcgSeed } from '../../nodes/random.wgsl.js';
import { queuedRayStruct, queuedHitStruct } from './structs.js';
import { proxy } from '../../lib/nodes/NodeProxy.js';
import { sampleEnvironmentFn, weightedAlphaBlendFn } from '../../nodes/sampling.wgsl.js';
Expand Down Expand Up @@ -90,7 +90,7 @@ export class RayIntersectionKernel extends ComputeKernel {
let indexUV = input.pixel;
let seed = ( textureLoad( ${ params.sampleCountTarget }, indexUV ).r & ( ~ ACTIVE_FLAG ) ) + input.currentBounce;

${ pcgInit }( indexUV, seed );
${ setPcgSeed }( input.pcgStateS0 );

// run intersection
let ray = Ray( input.origin, input.direction );
Expand Down
19 changes: 12 additions & 7 deletions src/webgpu/nodes/random.wgsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ export const pcgInit = wgslFn( /* wgsl */`
}
`, [ pcgStateStruct ] );

export const getPcgSeed = wgslFn( /* wgsl */`
fn pcgGetSeed() -> vec4u {
return g_state.s0;
}
`, [ pcgStateStruct ] );

export const setPcgSeed = wgslFn( /* wgsl */`
fn pcgSetSeed( s0: vec4u ) -> void {
g_state.s0 = s0;
}
`, [ pcgStateStruct ] );

export const pcg4d = wgslFn( /* wgsl */ `
fn pcg4d(v: ptr<private, vec4u>) -> void {
*v = *v * 1664525u + 1013904223u;
Expand All @@ -31,13 +43,6 @@ export const pcg4d = wgslFn( /* wgsl */ `
}
` );

export const pcgCycleState = wgslFn( /* wgsl */ `
fn pcgCycleState(n: u32) -> void {
for (var i = 0u; i < n; i++) {
pcg4d(&g_state.s0);
}
` );

export const pcgRand = wgslFn( /*wgsl*/`
fn pcgRand() -> f32 {
pcg4d(&g_state.s0);
Expand Down