@@ -26,13 +26,16 @@ const context = canvas.getContext('webgpu') as GPUCanvasContext;
2626const devicePixelRatio = window . devicePixelRatio ;
2727canvas . width = canvas . clientWidth * devicePixelRatio ;
2828canvas . height = canvas . clientHeight * devicePixelRatio ;
29- const presentationFormat = navigator . gpu . getPreferredCanvasFormat ( ) ;
30-
31- context . configure ( {
32- device,
33- format : presentationFormat ,
34- alphaMode : 'premultiplied' ,
35- } ) ;
29+ const presentationFormat = 'rgba16float' ;
30+
31+ function configureContext ( ) {
32+ context . configure ( {
33+ device,
34+ format : presentationFormat ,
35+ toneMapping : { mode : simulationParams . toneMappingMode } ,
36+ alphaMode : 'premultiplied' ,
37+ } ) ;
38+ }
3639
3740const particlesBuffer = device . createBuffer ( {
3841 size : numParticles * particleInstanceByteSize ,
@@ -317,10 +320,13 @@ let numMipLevels = 1;
317320const simulationParams = {
318321 simulate : true ,
319322 deltaTime : 0.04 ,
323+ toneMappingMode : 'standard' as GPUCanvasToneMappingMode ,
324+ brightnessFactor : 1.0 ,
320325} ;
321326
322327const simulationUBOBufferSize =
323328 1 * 4 + // deltaTime
329+ 1 * 4 + // brightnessFactor
324330 3 * 4 + // padding
325331 4 * 4 + // seed
326332 0 ;
@@ -330,8 +336,23 @@ const simulationUBOBuffer = device.createBuffer({
330336} ) ;
331337
332338const gui = new GUI ( ) ;
339+ gui . width = 325 ;
333340gui . add ( simulationParams , 'simulate' ) ;
334341gui . add ( simulationParams , 'deltaTime' ) ;
342+ const hdrFolder = gui . addFolder ( '' ) ;
343+ hdrFolder
344+ . add ( simulationParams , 'toneMappingMode' , [ 'standard' , 'extended' ] )
345+ . onChange ( configureContext ) ;
346+ hdrFolder . add ( simulationParams , 'brightnessFactor' , 0 , 4 , 0.1 ) ;
347+ hdrFolder . open ( ) ;
348+ const hdrMediaQuery = window . matchMedia ( '(dynamic-range: high)' ) ;
349+ function updateHdrFolderName ( ) {
350+ hdrFolder . name = `HDR settings ${
351+ hdrMediaQuery . matches ? '' : '⚠️ Your display is not compatible'
352+ } `;
353+ }
354+ updateHdrFolderName ( ) ;
355+ hdrMediaQuery . onchange = updateHdrFolderName ;
335356
336357const computePipeline = device . createComputePipeline ( {
337358 layout : 'auto' ,
@@ -377,6 +398,7 @@ function frame() {
377398 0 ,
378399 new Float32Array ( [
379400 simulationParams . simulate ? simulationParams . deltaTime : 0.0 ,
401+ simulationParams . brightnessFactor ,
380402 0.0 ,
381403 0.0 ,
382404 0.0 , // padding
@@ -438,4 +460,5 @@ function frame() {
438460
439461 requestAnimationFrame ( frame ) ;
440462}
463+ configureContext ( ) ;
441464requestAnimationFrame ( frame ) ;
0 commit comments