Repository: GraphiteEditor/Graphite
File: desktop/src/render/state.rs (Rust)

Code before:
	}

	pub(crate) fn set_viewport_offset(&mut self, offset: [f32; 2]) {
		self.viewport_offset = offset;
	}

	pub(crate) fn set_overlays_scene(&mut self, scene: vello::Scene) {
		self.overlays_scene = Some(scene);
	}

	fn render_overlays(&mut self, scene: vello::Scene) {
		let Some(viewport_texture) = self.viewport_texture.as_ref() else {
			tracing::warn!("No viewport texture bound, cannot render overlays");
			return;
		};
		let size = glam::UVec2::new(viewport_texture.width(), viewport_texture.height());
		let result = futures::executor::block_on(
			self.executor
				.render_vello_scene_to_target_texture(&scene, size, &Default::default(), Color::TRANSPARENT, &mut self.overlays_target_texture),
		);
		if let Err(e) = result {
			tracing::error!("Error rendering overlays: {:?}", e);
			return;
		}
		if let Some(target_texture) = &self.overlays_target_texture {
			self.overlays_texture = Some(target_texture.texture().clone());
			self.update_bindgroup();
		}
	}

	pub(crate) fn render(&mut self, window: &Window) -> Result<(), RenderError> {
		let ui_scale = if let Some(ui_texture) = &self.ui_texture
			&& (self.desired_width != ui_texture.width() || self.desired_height != ui_texture.height())
		{
			Some([self.desired_width as f32 / ui_texture.width() as f32, self.desired_height as f32 / ui_texture.height() as f32])
		} else {
			None
		};

		if let Some(scene) = self.overlays_scene.take() {
			self.render_overlays(scene);
		}

		let output = self.surface.get_current_texture().map_err(RenderError::SurfaceError)?;

		let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());

		let mut encoder = self.context.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("Render Encoder") });

		{
			let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {

Review comment (suggestion):
I think it is possible to remove `self.overlays_texture` entirely.
You should be able to just use `overlay_target_texture.view()` in update bind groups directly.

Code after:
		self.overlays_scene = Some(scene);
	}

	fn render_overlays(&mut self, scene: vello::Scene) {
		let Some(viewport_texture) = self.viewport_texture.as_ref() else {
			tracing::warn!("No viewport texture bound, cannot render overlays");
			return;
		};
		let size = glam::UVec2::new(viewport_texture.width(), viewport_texture.height());
		let result = futures::executor::block_on(
			self.executor
				.render_vello_scene_to_target_texture(&scene, size, &Default::default(), Color::TRANSPARENT, &mut self.overlays_texture),
		);
		if let Err(e) = result {
			tracing::error!("Error rendering overlays: {:?}", e);
			return;
		}
		self.update_bindgroup();
	}

	pub(crate) fn render(&mut self, window: &Window) -> Result<(), RenderError> {
		let ui_scale = if let Some(ui_texture) = &self.ui_texture
			&& (self.desired_width != ui_texture.width() || self.desired_height != ui_texture.height())
		{
			Some([self.desired_width as f32 / ui_texture.width() as f32, self.desired_height as f32 / ui_texture.height() as f32])
		} else {
			None
		};

		if let Some(scene) = self.overlays_scene.take() {
			self.render_overlays(scene);
		}

		let output = self.surface.get_current_texture().map_err(RenderError::SurfaceError)?;

		let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());

		let mut encoder = self.context.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("Render Encoder") });

		{
			let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
				label: Some("Graphite Composition Render Pass"),
				color_attachments: &[Some(wgpu::RenderPassColorAttachment {
					view: &view,
					resolve_target: None,
					ops: wgpu::Operations {
						load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.01, g: 0.01, b: 0.01, a: 1. }),
						store: wgpu::StoreOp::Store,
					},
					depth_slice: None,
				})],