|
| 1 | +"""Generic rounded-rectangle background overlay renderer.""" |
| 2 | + |
| 3 | +import ctypes as ct |
| 4 | + |
| 5 | +from .renderer import Renderer, RenderOptions |
| 6 | +from .uniforms import UniformBase |
| 7 | +from .utils import ( |
| 8 | + create_bind_group, |
| 9 | + get_device, |
| 10 | + read_shader_file, |
| 11 | +) |
| 12 | +from .webgpu_api import ( |
| 13 | + BlendComponent, |
| 14 | + BlendFactor, |
| 15 | + BlendOperation, |
| 16 | + BlendState, |
| 17 | + ColorTargetState, |
| 18 | + CompareFunction, |
| 19 | + DepthStencilState, |
| 20 | + FragmentState, |
| 21 | + PrimitiveState, |
| 22 | + VertexState, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +_BINDING = 50 |
| 27 | + |
| 28 | + |
| 29 | +class BackgroundUniforms(UniformBase): |
| 30 | + _binding = _BINDING |
| 31 | + _fields_ = [ |
| 32 | + ("position", ct.c_float * 2), |
| 33 | + ("width", ct.c_float), |
| 34 | + ("height", ct.c_float), |
| 35 | + ("bg_color", ct.c_float * 3), |
| 36 | + ("_pad", ct.c_float), |
| 37 | + ] |
| 38 | + |
| 39 | + |
| 40 | +class Background(Renderer): |
| 41 | + """Semi-transparent rounded-rectangle background overlay. |
| 42 | +
|
| 43 | + Place this before other renderers in a MultipleRenderer to provide |
| 44 | + a readable backdrop behind text or UI elements. |
| 45 | +
|
| 46 | + @param position: (x, y) top-left corner in NDC |
| 47 | + @param width: width in NDC |
| 48 | + @param height: height in NDC (of the content area, padding is added automatically) |
| 49 | + """ |
| 50 | + vertex_entry_point: str = "background_vertex" |
| 51 | + fragment_entry_point: str = "background_fragment" |
| 52 | + select_entry_point: str = "" |
| 53 | + n_vertices: int = 6 |
| 54 | + n_instances: int = 1 |
| 55 | + |
| 56 | + def __init__(self, position=(0, 0), width=1, height=0.05): |
| 57 | + super().__init__() |
| 58 | + self._position = position |
| 59 | + self._width = width |
| 60 | + self._height = height |
| 61 | + self.uniforms = None |
| 62 | + |
| 63 | + @property |
| 64 | + def position(self): |
| 65 | + return self._position |
| 66 | + |
| 67 | + @position.setter |
| 68 | + def position(self, value): |
| 69 | + self._position = value |
| 70 | + if self.uniforms is not None: |
| 71 | + self.uniforms.position = value |
| 72 | + self.uniforms.update_buffer() |
| 73 | + self.set_needs_update() |
| 74 | + |
| 75 | + @property |
| 76 | + def width(self): |
| 77 | + return self._width |
| 78 | + |
| 79 | + @width.setter |
| 80 | + def width(self, value): |
| 81 | + self._width = value |
| 82 | + if self.uniforms is not None: |
| 83 | + self.uniforms.width = value |
| 84 | + self.uniforms.update_buffer() |
| 85 | + self.set_needs_update() |
| 86 | + |
| 87 | + @property |
| 88 | + def height(self): |
| 89 | + return self._height |
| 90 | + |
| 91 | + @height.setter |
| 92 | + def height(self, value): |
| 93 | + self._height = value |
| 94 | + if self.uniforms is not None: |
| 95 | + self.uniforms.height = value |
| 96 | + self.uniforms.update_buffer() |
| 97 | + self.set_needs_update() |
| 98 | + |
| 99 | + def get_shader_code(self): |
| 100 | + return read_shader_file("background.wgsl") |
| 101 | + |
| 102 | + def get_bindings(self): |
| 103 | + return self.uniforms.get_bindings() |
| 104 | + |
| 105 | + def update(self, options: RenderOptions): |
| 106 | + if self.uniforms is None: |
| 107 | + self.uniforms = BackgroundUniforms() |
| 108 | + self.uniforms.position = self.position |
| 109 | + self.uniforms.width = self.width |
| 110 | + self.uniforms.height = self.height |
| 111 | + self.uniforms.bg_color = (1.0, 1.0, 1.0) |
| 112 | + self.uniforms.update_buffer() |
| 113 | + |
| 114 | + def create_render_pipeline(self, options: RenderOptions) -> None: |
| 115 | + bindings = options.get_bindings() + self.get_bindings() |
| 116 | + |
| 117 | + if bindings == self._last_bindings: |
| 118 | + return |
| 119 | + |
| 120 | + layout, self.group = create_bind_group( |
| 121 | + self.device, options.get_bindings() + self.get_bindings() |
| 122 | + ) |
| 123 | + pipeline_layout = self.device.createPipelineLayout([layout]) |
| 124 | + |
| 125 | + depth_stencil = DepthStencilState( |
| 126 | + format=options.canvas.depth_format, |
| 127 | + depthWriteEnabled=False, |
| 128 | + depthCompare=CompareFunction.always, |
| 129 | + ) |
| 130 | + |
| 131 | + bg_color_target = ColorTargetState( |
| 132 | + format=options.canvas.format, |
| 133 | + blend=BlendState( |
| 134 | + color=BlendComponent( |
| 135 | + srcFactor=BlendFactor.src_alpha, |
| 136 | + dstFactor=BlendFactor.one_minus_src_alpha, |
| 137 | + operation=BlendOperation.add, |
| 138 | + ), |
| 139 | + alpha=BlendComponent( |
| 140 | + srcFactor=BlendFactor.one, |
| 141 | + dstFactor=BlendFactor.one_minus_src_alpha, |
| 142 | + operation=BlendOperation.add, |
| 143 | + ), |
| 144 | + ), |
| 145 | + ) |
| 146 | + |
| 147 | + shader_module = self.device.createShaderModule(self._get_preprocessed_shader_code()) |
| 148 | + self.pipeline = self.device.createRenderPipeline( |
| 149 | + pipeline_layout, |
| 150 | + vertex=VertexState( |
| 151 | + module=shader_module, |
| 152 | + entryPoint=self.vertex_entry_point, |
| 153 | + buffers=[], |
| 154 | + ), |
| 155 | + fragment=FragmentState( |
| 156 | + module=shader_module, |
| 157 | + entryPoint=self.fragment_entry_point, |
| 158 | + targets=[bg_color_target], |
| 159 | + ), |
| 160 | + primitive=PrimitiveState(topology=self.topology), |
| 161 | + depthStencil=depth_stencil, |
| 162 | + multisample=options.canvas.multisample, |
| 163 | + label="Background", |
| 164 | + ) |
| 165 | + self._select_pipeline = None |
| 166 | + self._transparent_pipeline = None |
| 167 | + self._last_bindings = bindings |
| 168 | + |
| 169 | + def get_export_descriptor(self, options, buffer_registry): |
| 170 | + desc = super().get_export_descriptor(options, buffer_registry) |
| 171 | + desc.depth_write = False |
| 172 | + desc.pass_type = "transparent" |
| 173 | + return desc |
| 174 | + |
| 175 | + def get_theme_buffer_id(self, registry): |
| 176 | + """Return the buffer id for theme color updates (bg_color at offset 16).""" |
| 177 | + if self.uniforms is None or self.uniforms._buffer is None: |
| 178 | + return None |
| 179 | + key = id(self.uniforms._buffer) |
| 180 | + if key in registry._buffers: |
| 181 | + return registry._buffers[key][0] |
| 182 | + return None |
0 commit comments