@@ -7,7 +7,7 @@ import vtkWebGPUHardwareSelector from 'vtk.js/Sources/Rendering/WebGPU/HardwareS
77import vtkWebGPUViewNodeFactory from 'vtk.js/Sources/Rendering/WebGPU/ViewNodeFactory' ;
88import vtkRenderPass from 'vtk.js/Sources/Rendering/SceneGraph/RenderPass' ;
99import vtkRenderWindowViewNode from 'vtk.js/Sources/Rendering/SceneGraph/RenderWindowViewNode' ;
10- // import { VtkDataTypes } from 'vtk.js/Sources/Common/Core/DataArray/Constants ';
10+ import HalfFloat from 'vtk.js/Sources/Common/Core/HalfFloat ' ;
1111
1212const { vtkErrorMacro } = macro ;
1313// const IS_CHROME = navigator.userAgent.indexOf('Chrome') !== -1;
@@ -68,15 +68,15 @@ function vtkWebGPURenderWindow(publicAPI, model) {
6868 publicAPI . recreateSwapChain = ( ) => {
6969 if ( model . context ) {
7070 model . context . unconfigure ( ) ;
71- const presentationFormat = navigator . gpu . getPreferredCanvasFormat (
71+ model . presentationFormat = navigator . gpu . getPreferredCanvasFormat (
7272 model . adapter
7373 ) ;
7474
7575 /* eslint-disable no-undef */
7676 /* eslint-disable no-bitwise */
7777 model . context . configure ( {
7878 device : model . device . getHandle ( ) ,
79- format : presentationFormat ,
79+ format : model . presentationFormat ,
8080 alphaMode : 'premultiplied' ,
8181 usage : GPUTextureUsage . RENDER_ATTACHMENT | GPUTextureUsage . COPY_DST ,
8282 width : model . size [ 0 ] ,
@@ -478,9 +478,9 @@ function vtkWebGPURenderWindow(publicAPI, model) {
478478 height : texture . getHeight ( ) ,
479479 } ;
480480
481- // must be a multiple of 256 bytes, so 64 texels with rgba8
482- result . colorBufferWidth = 64 * Math . floor ( ( result . width + 63 ) / 64 ) ;
483- result . colorBufferSizeInBytes = result . colorBufferWidth * result . height * 4 ;
481+ // must be a multiple of 256 bytes, so 32 texels with rgba16
482+ result . colorBufferWidth = 32 * Math . floor ( ( result . width + 31 ) / 32 ) ;
483+ result . colorBufferSizeInBytes = result . colorBufferWidth * result . height * 8 ;
484484 const colorBuffer = vtkWebGPUBuffer . newInstance ( ) ;
485485 colorBuffer . setDevice ( device ) ;
486486 /* eslint-disable no-bitwise */
@@ -499,7 +499,7 @@ function vtkWebGPURenderWindow(publicAPI, model) {
499499 } ,
500500 {
501501 buffer : colorBuffer . getHandle ( ) ,
502- bytesPerRow : 4 * result . colorBufferWidth ,
502+ bytesPerRow : 8 * result . colorBufferWidth ,
503503 rowsPerImage : result . height ,
504504 } ,
505505 {
@@ -515,20 +515,22 @@ function vtkWebGPURenderWindow(publicAPI, model) {
515515 await cLoad ;
516516 /* eslint-enable no-undef */
517517
518- result . colorValues = new Uint8ClampedArray (
519- colorBuffer . getMappedRange ( ) . slice ( )
520- ) ;
518+ result . colorValues = new Uint16Array ( colorBuffer . getMappedRange ( ) . slice ( ) ) ;
521519 colorBuffer . unmap ( ) ;
522520 // repack the array
523521 const tmparray = new Uint8ClampedArray ( result . height * result . width * 4 ) ;
524522 for ( let y = 0 ; y < result . height ; y ++ ) {
525523 for ( let x = 0 ; x < result . width ; x ++ ) {
526524 const doffset = ( y * result . width + x ) * 4 ;
527525 const soffset = ( y * result . colorBufferWidth + x ) * 4 ;
528- tmparray [ doffset ] = result . colorValues [ soffset + 2 ] ;
529- tmparray [ doffset + 1 ] = result . colorValues [ soffset + 1 ] ;
530- tmparray [ doffset + 2 ] = result . colorValues [ soffset ] ;
531- tmparray [ doffset + 3 ] = result . colorValues [ soffset + 3 ] ;
526+ tmparray [ doffset ] =
527+ 255.0 * HalfFloat . fromHalf ( result . colorValues [ soffset ] ) ;
528+ tmparray [ doffset + 1 ] =
529+ 255.0 * HalfFloat . fromHalf ( result . colorValues [ soffset + 1 ] ) ;
530+ tmparray [ doffset + 2 ] =
531+ 255.0 * HalfFloat . fromHalf ( result . colorValues [ soffset + 2 ] ) ;
532+ tmparray [ doffset + 3 ] =
533+ 255.0 * HalfFloat . fromHalf ( result . colorValues [ soffset + 3 ] ) ;
532534 }
533535 }
534536 result . colorValues = tmparray ;
@@ -564,6 +566,7 @@ const DEFAULT_VALUES = {
564566 useBackgroundImage : false ,
565567 nextPropID : 1 ,
566568 xrSupported : false ,
569+ presentationFormat : null ,
567570} ;
568571
569572// ----------------------------------------------------------------------------
@@ -607,6 +610,7 @@ export function extend(publicAPI, model, initialValues = {}) {
607610 macro . get ( publicAPI , model , [
608611 'commandEncoder' ,
609612 'device' ,
613+ 'presentationFormat' ,
610614 'useBackgroundImage' ,
611615 'xrSupported' ,
612616 ] ) ;
0 commit comments