Skip to content

Commit 198b5f1

Browse files
committed
cache textures and views to prevent roundtrips in frame rendering
1 parent 043cc05 commit 198b5f1

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

webgpu/canvas.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,16 @@ class Canvas:
235235
device: Device
236236
depth_format: TextureFormat
237237
depth_texture: Texture = None
238+
depth_texture_view = None
238239
multisample_texture: Texture = None
240+
multisample_texture_view = None
239241
multisample: MultisampleState = None
240242
target_texture: Texture = None
243+
target_texture_view = None
241244
select_depth_texture: Texture = None
245+
select_depth_texture_view = None
242246
select_texture: Texture = None
247+
select_texture_view = None
243248

244249
width: int = 0
245250
height: int = 0
@@ -389,6 +394,11 @@ def destroy_textures(self):
389394
]:
390395
if tex is not None:
391396
tex.destroy()
397+
self.target_texture_view = None
398+
self.multisample_texture_view = None
399+
self.depth_texture_view = None
400+
self.select_texture_view = None
401+
self.select_depth_texture_view = None
392402

393403
@debounce(5)
394404
def resize(self):
@@ -434,6 +444,9 @@ def resize(self):
434444
usage=TextureUsage.RENDER_ATTACHMENT,
435445
label="multisample",
436446
)
447+
self.multisample_texture_view = self.multisample_texture.createView()
448+
else:
449+
self.multisample_texture_view = None
437450

438451
self.depth_texture = device.createTexture(
439452
size=[width, height, 1],
@@ -442,6 +455,7 @@ def resize(self):
442455
label="depth_texture",
443456
sampleCount=self.multisample.count,
444457
)
458+
self.depth_texture_view = self.depth_texture.createView()
445459

446460
self.target_texture_view = self.target_texture.createView()
447461

@@ -458,6 +472,7 @@ def resize(self):
458472
label="select_depth",
459473
)
460474
self.select_texture_view = self.select_texture.createView()
475+
self.select_depth_texture_view = self.select_depth_texture.createView()
461476

462477
self.width = width
463478
self.height = height
@@ -470,7 +485,7 @@ def color_attachments(self, loadOp: LoadOp):
470485
return [
471486
RenderPassColorAttachment(
472487
view=(
473-
self.multisample_texture.createView()
488+
self.multisample_texture_view
474489
if have_multisample
475490
else self.target_texture_view
476491
),
@@ -491,14 +506,14 @@ def select_attachments(self, loadOp: LoadOp):
491506

492507
def select_depth_stencil_attachment(self, loadOp: LoadOp):
493508
return RenderPassDepthStencilAttachment(
494-
self.select_depth_texture.createView(),
509+
self.select_depth_texture_view,
495510
depthClearValue=1.0,
496511
depthLoadOp=loadOp,
497512
)
498513

499514
def depth_stencil_attachment(self, loadOp: LoadOp):
500515
return RenderPassDepthStencilAttachment(
501-
self.depth_texture.createView(),
516+
self.depth_texture_view,
502517
depthClearValue=1.0,
503518
depthLoadOp=loadOp,
504519
)

webgpu/scene.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ def __on_update_html_canvas(self, html_canvas):
310310

311311
def get_position(self, x: int, y: int):
312312
"""Return the 3D position under canvas pixel (x, y) using the selection buffer."""
313+
if self.canvas is None or self.canvas.height == 0:
314+
return None
313315
objects = self.render_objects
314316

315317
with self._render_mutex:
@@ -352,7 +354,7 @@ def select(self, x: int, y: int):
352354
"""Perform an object selection at (x, y) and dispatch callbacks on matching renderers."""
353355
if self._render_mutex is None:
354356
return
355-
if self.canvas is None:
357+
if self.canvas is None or self.canvas.height == 0:
356358
return
357359
objects = self.render_objects
358360

@@ -409,7 +411,7 @@ def select(self, x: int, y: int):
409411
# @print_communications
410412
def _render_objects(self, to_canvas=True, update_pipelines=True):
411413
"""Update pipelines and render all active objects, optionally copying to the canvas."""
412-
if self.canvas is None:
414+
if self.canvas is None or self.canvas.height == 0:
413415
return
414416
options = self.options
415417

0 commit comments

Comments
 (0)