Skip to content

Commit 345f25c

Browse files
dakersankhesh
authored andcommitted
refactor(WebGPU): simplify vtkWebGPUCellArrayMapper
This commit centralizes the definition of pi as a constant and removes redundant declarations in the WGSL shader code. It streamlines the updateUBO function, making property and buffer updates more readable and efficient. The updateTextures function is also refactored for clarity.
1 parent b2815ae commit 345f25c

File tree

4 files changed

+215
-275
lines changed

4 files changed

+215
-275
lines changed

Sources/Rendering/Core/Texture/index.d.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export interface ITextureInitialValues {
1515
}
1616

1717
export interface vtkTexture extends vtkAlgorithm {
18-
1918
/**
2019
* Returns the canvas used by the texture.
2120
*/
@@ -39,22 +38,24 @@ export interface vtkTexture extends vtkAlgorithm {
3938
/**
4039
* Returns the image used by the texture.
4140
*/
42-
getImage(): HTMLImageElement;
41+
getImage(): Nullable<HTMLImageElement>;
4342

4443
/**
4544
* Returns an ImageBitmap object.
4645
*/
47-
getImageBitmap(): ImageBitmap;
46+
getImageBitmap(): Nullable<ImageBitmap>;
4847

4948
/**
5049
* Returns true if the image is loaded.
5150
*/
5251
getImageLoaded(): boolean;
5352

5453
/**
55-
* Returns the input image data as a JavaScript ImageData object.
54+
* Returns the input image data object.
5655
*/
57-
getInputAsJsImageData(): any;
56+
getInputAsJsImageData(): Nullable<
57+
ImageData | ImageBitmap | HTMLCanvasElement | HTMLImageElement
58+
>;
5859

5960
/**
6061
* Returns the current mip level of the texture.
@@ -105,7 +106,7 @@ export interface vtkTexture extends vtkAlgorithm {
105106
* Sets the input image data as a JavaScript ImageData object.
106107
* @param imageData
107108
*/
108-
setJsImageData(imageData: any): void;
109+
setJsImageData(imageData: ImageData): void;
109110

110111
/**
111112
* Sets the current mip level of the texture.
@@ -164,6 +165,11 @@ export function generateMipmaps(
164165
* create visualization pipelines to read, process, and construct textures. Note
165166
* that textures will only work if texture coordinates are also defined, and if
166167
* the rendering system supports texture.
168+
*
169+
* This class is used in both WebGL and WebGPU rendering backends, but the
170+
* implementation details may vary. In WebGL, it uses HTMLImageElement and
171+
* HTMLCanvasElement for textures, while in WebGPU, it uses HTMLImageElement,
172+
* HTMLCanvasElement, and ImageBitmap.
167173
*/
168174
export declare const vtkTexture: {
169175
newInstance: typeof newInstance;

Sources/Rendering/Core/Texture/index.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ function vtkTexture(publicAPI, model) {
134134
if (!model.imageLoaded || publicAPI.getInputData()) return null;
135135

136136
if (model.jsImageData) {
137-
return model.jsImageData();
137+
return model.jsImageData;
138138
}
139139

140140
if (model.imageBitmap) {
141-
return model.imageBitmap();
141+
return model.imageBitmap;
142142
}
143143

144144
if (model.canvas) {
@@ -151,21 +151,16 @@ function vtkTexture(publicAPI, model) {
151151
);
152152
return imageData;
153153
}
154+
154155
if (model.image) {
155-
const canvas = document.createElement('canvas');
156-
canvas.width = model.image.width;
157-
canvas.height = model.image.height;
156+
const width = model.image.width;
157+
const height = model.image.height;
158+
const canvas = new OffscreenCanvas(width, height);
158159
const context = canvas.getContext('2d');
159-
context.translate(0, canvas.height);
160+
context.translate(0, height);
160161
context.scale(1, -1);
161-
context.drawImage(
162-
model.image,
163-
0,
164-
0,
165-
model.image.width,
166-
model.image.height
167-
);
168-
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
162+
context.drawImage(model.image, 0, 0, width, height);
163+
const imageData = context.getImageData(0, 0, width, height);
169164
return imageData;
170165
}
171166

0 commit comments

Comments
 (0)