1- import createCheckedImageData from " ./createCheckedImageData"
2- import generateMipmapsArray from " ./generateMimapsArray"
1+ import createCheckedImageData from ' ./createCheckedImageData'
2+ import generateMipmapsArray from ' ./generateMimapsArray'
33
44interface Options {
55 mips ?: boolean
@@ -8,26 +8,26 @@ interface Options {
88}
99
1010type TextureSource =
11- | ImageBitmap
12- | HTMLVideoElement
13- | HTMLCanvasElement
14- | HTMLImageElement
15- | OffscreenCanvas ;
11+ | ImageBitmap
12+ | HTMLVideoElement
13+ | HTMLCanvasElement
14+ | HTMLImageElement
15+ | OffscreenCanvas
1616
1717const numMipLevels = ( ...sizes : number [ ] ) => {
1818 const maxSize = Math . max ( ...sizes )
19- return 1 + Math . log2 ( maxSize ) | 0
19+ return ( 1 + Math . log2 ( maxSize ) ) | 0
2020}
2121
2222export interface TextureSlice {
2323 source : GPUCopyExternalImageSource
2424 fakeMipmaps : boolean
2525}
2626
27- function createCheckedMipmap ( levels : Array < { size : number , color : string } > ) {
28- const ctx = document . createElement ( 'canvas' ) . getContext ( '2d' , { willReadFrequently : true } ) !
27+ function createCheckedMipmap ( levels : Array < { size : number ; color : string } > ) {
28+ const ctx = new OffscreenCanvas ( 0 , 0 ) . getContext ( '2d' , { willReadFrequently : true } ) !
2929
30- return levels . map ( ( { size, color} , i ) => {
30+ return levels . map ( ( { size, color } , i ) => {
3131 ctx . canvas . width = size
3232 ctx . canvas . height = size
3333 ctx . fillStyle = i & 1 ? '#000' : '#fff'
@@ -45,24 +45,29 @@ function createCheckedMipmap(levels: Array<{ size: number, color: string }>) {
4545 { x : 0.25 , y : 0.75 } ,
4646 { x : 0.75 , y : 0.75 } ,
4747 { x : 0.75 , y : 0.25 } ,
48- ] . forEach ( p => {
48+ ] . forEach ( ( p ) => {
4949 ctx . fillText ( i . toString ( ) , p . x * size , p . y * size )
5050 } )
5151
52-
5352 return ctx . getImageData ( 0 , 0 , size , size )
5453 } )
5554}
5655
57- export function createTextureArray ( device : GPUDevice , width : number , height : number , slices : number ) {
56+ export function createTextureArray (
57+ device : GPUDevice ,
58+ width : number ,
59+ height : number ,
60+ slices : number
61+ ) {
5862 return device . createTexture ( {
5963 label : '2d array texture' ,
6064 format : 'rgba8unorm' ,
6165 mipLevelCount : 1 + Math . log2 ( 2048 ) ,
6266 size : [ width , height , slices ] ,
63- usage : GPUTextureUsage . TEXTURE_BINDING |
64- GPUTextureUsage . COPY_DST |
65- GPUTextureUsage . RENDER_ATTACHMENT ,
67+ usage :
68+ GPUTextureUsage . TEXTURE_BINDING |
69+ GPUTextureUsage . COPY_DST |
70+ GPUTextureUsage . RENDER_ATTACHMENT ,
6671 } )
6772}
6873
@@ -79,7 +84,7 @@ export function attachSlice(
7984 device . queue . copyExternalImageToTexture (
8085 { source } ,
8186 { texture : textue2dArray , origin : { z : sliceIndex } , mipLevel : 0 } ,
82- { width, height } ,
87+ { width, height }
8388 )
8489
8590 // if (texSlice.fakeMipmaps) {
@@ -102,28 +107,37 @@ export function attachSlice(
102107 // }
103108}
104109
105-
106- export function createTextureFromSource ( device : GPUDevice , source : TextureSource , options : Options = { } ) {
110+ export function createTextureFromSource (
111+ device : GPUDevice ,
112+ source : TextureSource ,
113+ options : Options = { }
114+ ) {
107115 const texture = device . createTexture ( {
108116 format : 'rgba8unorm' ,
109117 mipLevelCount : options . mips ? numMipLevels ( source . width , source . height ) : 1 ,
110118 size : [ source . width , source . height ] ,
111- usage : GPUTextureUsage . TEXTURE_BINDING |
112- GPUTextureUsage . COPY_DST |
113- GPUTextureUsage . RENDER_ATTACHMENT ,
119+ usage :
120+ GPUTextureUsage . TEXTURE_BINDING |
121+ GPUTextureUsage . COPY_DST |
122+ GPUTextureUsage . RENDER_ATTACHMENT ,
114123 } )
115124 copySourceToTexture ( device , texture , source , options )
116125 return texture
117126}
118127
119- function copySourceToTexture ( device : GPUDevice , texture : GPUTexture , source : TextureSource , { flipY, depthOrArrayLayers} : Options = { } ) {
120-
128+ function copySourceToTexture (
129+ device : GPUDevice ,
130+ texture : GPUTexture ,
131+ source : TextureSource ,
132+ { flipY, depthOrArrayLayers } : Options = { }
133+ ) {
121134 device . queue . copyExternalImageToTexture (
122- { source, flipY, } ,
123- { texture,
135+ { source, flipY } ,
136+ {
137+ texture,
124138 // premultipliedAlpha: true
125139 } ,
126- { width : source . width , height : source . height , depthOrArrayLayers } ,
140+ { width : source . width , height : source . height , depthOrArrayLayers }
127141 )
128142
129143 // if (texture.mipLevelCount > 1) {
@@ -134,7 +148,10 @@ function copySourceToTexture(device: GPUDevice, texture: GPUTexture, source: Tex
134148export async function loadImageBitmap ( url : string ) {
135149 const res = await fetch ( url )
136150 const blob = await res . blob ( )
137- return await createImageBitmap ( blob , { colorSpaceConversion : 'none' , premultiplyAlpha : 'premultiply' } )
151+ return await createImageBitmap ( blob , {
152+ colorSpaceConversion : 'none' ,
153+ premultiplyAlpha : 'premultiply' ,
154+ } )
138155}
139156
140157export async function createTextureFromImage ( device : GPUDevice , url : string , options : Options ) {
0 commit comments