From 667c19907a54248a78b6a7113d894ed308954cc4 Mon Sep 17 00:00:00 2001 From: Junkui Zhang <364772080@qq.com> Date: Wed, 16 Jul 2025 14:14:31 +0800 Subject: [PATCH] refactor --- .../src/platform/windows/directx_renderer.rs | 121 ++++++++---------- 1 file changed, 54 insertions(+), 67 deletions(-) diff --git a/crates/gpui/src/platform/windows/directx_renderer.rs b/crates/gpui/src/platform/windows/directx_renderer.rs index dd5e1a5b32e58b0d6cca9e49aafe7ae8e37840a1..273d5a1a2bbca0396c629bff9c56dcb0ddee5de5 100644 --- a/crates/gpui/src/platform/windows/directx_renderer.rs +++ b/crates/gpui/src/platform/windows/directx_renderer.rs @@ -230,13 +230,10 @@ impl DirectXRenderer { &self.devices.device_context, shadows, )?; - draw_normal( + self.pipelines.shadow_pipeline.draw( &self.devices.device_context, - &self.pipelines.shadow_pipeline, &self.context.viewport, &self.globals.global_params_buffer, - D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, - 4, shadows.len() as u32, ) } @@ -250,13 +247,10 @@ impl DirectXRenderer { &self.devices.device_context, quads, )?; - draw_normal( + self.pipelines.quad_pipeline.draw( &self.devices.device_context, - &self.pipelines.quad_pipeline, &self.context.viewport, &self.globals.global_params_buffer, - D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, - 4, quads.len() as u32, ) } @@ -355,13 +349,10 @@ impl DirectXRenderer { &self.devices.device_context, underlines, )?; - draw_normal( + self.pipelines.underline_pipeline.draw( &self.devices.device_context, - &self.pipelines.underline_pipeline, &self.context.viewport, &self.globals.global_params_buffer, - D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, - 4, underlines.len() as u32, ) } @@ -380,9 +371,8 @@ impl DirectXRenderer { sprites, )?; let texture_view = self.atlas.get_texture_view(texture_id); - draw_with_texture( + self.pipelines.mono_sprites.draw_with_texture( &self.devices.device_context, - &self.pipelines.mono_sprites, &texture_view, &self.context.viewport, &self.globals.global_params_buffer, @@ -405,9 +395,8 @@ impl DirectXRenderer { sprites, )?; let texture_view = self.atlas.get_texture_view(texture_id); - draw_with_texture( + self.pipelines.poly_sprites.draw_with_texture( &self.devices.device_context, - &self.pipelines.poly_sprites, &texture_view, &self.context.viewport, &self.globals.global_params_buffer, @@ -658,6 +647,55 @@ impl PipelineState { } Ok(()) } + + fn draw( + &self, + device_context: &ID3D11DeviceContext, + viewport: &[D3D11_VIEWPORT], + global_params: &[Option], + instance_count: u32, + ) -> Result<()> { + unsafe { + device_context.VSSetShaderResources(1, Some(&self.view)); + device_context.PSSetShaderResources(1, Some(&self.view)); + device_context.IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + device_context.RSSetViewports(Some(viewport)); + device_context.VSSetShader(&self.vertex, None); + device_context.PSSetShader(&self.fragment, None); + device_context.VSSetConstantBuffers(0, Some(global_params)); + device_context.PSSetConstantBuffers(0, Some(global_params)); + + device_context.DrawInstanced(4, instance_count, 0, 0); + } + Ok(()) + } + + fn draw_with_texture( + &self, + device_context: &ID3D11DeviceContext, + texture: &[Option], + viewport: &[D3D11_VIEWPORT], + global_params: &[Option], + sampler: &[Option], + instance_count: u32, + ) -> Result<()> { + unsafe { + device_context.IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + device_context.RSSetViewports(Some(viewport)); + device_context.VSSetShader(&self.vertex, None); + device_context.PSSetShader(&self.fragment, None); + device_context.VSSetConstantBuffers(0, Some(global_params)); + device_context.PSSetConstantBuffers(0, Some(global_params)); + device_context.VSSetShaderResources(1, Some(&self.view)); + device_context.PSSetShaderResources(1, Some(&self.view)); + device_context.PSSetSamplers(0, Some(sampler)); + device_context.VSSetShaderResources(0, Some(texture)); + device_context.PSSetShaderResources(0, Some(texture)); + + device_context.DrawInstanced(4, instance_count, 0, 0); + } + Ok(()) + } } impl PathsPipelineState { @@ -1167,57 +1205,6 @@ fn draw_indirect( } } -fn draw_normal( - device_context: &ID3D11DeviceContext, - pipeline: &PipelineState, - viewport: &[D3D11_VIEWPORT], - global_params: &[Option], - topology: D3D_PRIMITIVE_TOPOLOGY, - vertex_count: u32, - instance_count: u32, -) -> Result<()> { - unsafe { - device_context.VSSetShaderResources(1, Some(&pipeline.view)); - device_context.PSSetShaderResources(1, Some(&pipeline.view)); - device_context.IASetPrimitiveTopology(topology); - device_context.RSSetViewports(Some(viewport)); - device_context.VSSetShader(&pipeline.vertex, None); - device_context.PSSetShader(&pipeline.fragment, None); - device_context.VSSetConstantBuffers(0, Some(global_params)); - device_context.PSSetConstantBuffers(0, Some(global_params)); - - device_context.DrawInstanced(vertex_count, instance_count, 0, 0); - } - Ok(()) -} - -fn draw_with_texture( - device_context: &ID3D11DeviceContext, - pipeline: &PipelineState, - texture: &[Option], - viewport: &[D3D11_VIEWPORT], - global_params: &[Option], - sampler: &[Option], - instance_count: u32, -) -> Result<()> { - unsafe { - device_context.IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); - device_context.RSSetViewports(Some(viewport)); - device_context.VSSetShader(&pipeline.vertex, None); - device_context.PSSetShader(&pipeline.fragment, None); - device_context.VSSetConstantBuffers(0, Some(global_params)); - device_context.PSSetConstantBuffers(0, Some(global_params)); - device_context.VSSetShaderResources(1, Some(&pipeline.view)); - device_context.PSSetShaderResources(1, Some(&pipeline.view)); - device_context.PSSetSamplers(0, Some(sampler)); - device_context.VSSetShaderResources(0, Some(texture)); - device_context.PSSetShaderResources(0, Some(texture)); - - device_context.DrawInstanced(4, instance_count, 0, 0); - } - Ok(()) -} - const BUFFER_COUNT: usize = 3; mod shader_resources {