From 72c55b46536c6f5c3e052b752b9ee4719c7ce6ee Mon Sep 17 00:00:00 2001 From: Junkui Zhang <364772080@qq.com> Date: Thu, 17 Jul 2025 16:57:23 +0800 Subject: [PATCH] add new feature `enable-renderdoc` --- crates/gpui/Cargo.toml | 1 + .../src/platform/windows/directx_renderer.rs | 61 +++++++++++-------- crates/zed/Cargo.toml | 1 + 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index 29e81269e3..e926bdfcc5 100644 --- a/crates/gpui/Cargo.toml +++ b/crates/gpui/Cargo.toml @@ -72,6 +72,7 @@ screen-capture = [ "scap", ] windows-manifest = [] +enable-renderdoc = [] [lib] path = "src/gpui.rs" diff --git a/crates/gpui/src/platform/windows/directx_renderer.rs b/crates/gpui/src/platform/windows/directx_renderer.rs index 02469043b5..062f0a4315 100644 --- a/crates/gpui/src/platform/windows/directx_renderer.rs +++ b/crates/gpui/src/platform/windows/directx_renderer.rs @@ -2,18 +2,15 @@ use std::{mem::ManuallyDrop, sync::Arc}; use ::util::ResultExt; use anyhow::{Context, Result}; -// #[cfg(not(feature = "enable-renderdoc"))] +#[cfg(not(feature = "enable-renderdoc"))] use windows::Win32::Graphics::DirectComposition::*; -use windows::{ - Win32::{ - Foundation::{HMODULE, HWND}, - Graphics::{ - Direct3D::*, - Direct3D11::*, - Dxgi::{Common::*, *}, - }, +use windows::Win32::{ + Foundation::{HMODULE, HWND}, + Graphics::{ + Direct3D::*, + Direct3D11::*, + Dxgi::{Common::*, *}, }, - core::*, }; use crate::*; @@ -28,7 +25,7 @@ pub(crate) struct DirectXRenderer { resources: DirectXResources, globals: DirectXGlobalElements, pipelines: DirectXRenderPipelines, - // #[cfg(not(feature = "enable-renderdoc"))] + #[cfg(not(feature = "enable-renderdoc"))] _direct_composition: DirectComposition, } @@ -36,6 +33,7 @@ pub(crate) struct DirectXRenderer { #[derive(Clone)] pub(crate) struct DirectXDevices { dxgi_factory: IDXGIFactory6, + #[cfg(not(feature = "enable-renderdoc"))] dxgi_device: IDXGIDevice, device: ID3D11Device, device_context: ID3D11DeviceContext, @@ -78,7 +76,7 @@ struct DrawInstancedIndirectArgs { start_instance_location: u32, } -// #[cfg(not(feature = "enable-renderdoc"))] +#[cfg(not(feature = "enable-renderdoc"))] struct DirectComposition { comp_device: IDCompositionDevice, comp_target: IDCompositionTarget, @@ -95,10 +93,12 @@ impl DirectXDevices { get_device(&adapter, Some(&mut device), Some(&mut context))?; (device.unwrap(), context.unwrap()) }; + #[cfg(not(feature = "enable-renderdoc"))] let dxgi_device: IDXGIDevice = device.cast()?; Ok(Self { dxgi_factory, + #[cfg(not(feature = "enable-renderdoc"))] dxgi_device, device, device_context, @@ -112,20 +112,27 @@ impl DirectXRenderer { devices.device.clone(), devices.device_context.clone(), )); + + #[cfg(not(feature = "enable-renderdoc"))] let resources = DirectXResources::new(devices)?; + #[cfg(feature = "enable-renderdoc")] + let resources = DirectXResources::new(devices, hwnd)?; + let globals = DirectXGlobalElements::new(&devices.device)?; let pipelines = DirectXRenderPipelines::new(&devices.device)?; - // #[cfg(not(feature = "enable-renderdoc"))] + + #[cfg(not(feature = "enable-renderdoc"))] let direct_composition = DirectComposition::new(&devices.dxgi_device, hwnd)?; - // #[cfg(not(feature = "enable-renderdoc"))] + #[cfg(not(feature = "enable-renderdoc"))] direct_composition.set_swap_chain(&resources.swap_chain)?; + Ok(DirectXRenderer { atlas, devices: devices.clone(), resources, globals, pipelines, - // #[cfg(not(feature = "enable-renderdoc"))] + #[cfg(not(feature = "enable-renderdoc"))] _direct_composition: direct_composition, }) } @@ -392,14 +399,19 @@ impl DirectXRenderer { } impl DirectXResources { - pub fn new(devices: &DirectXDevices) -> Result { + pub fn new( + devices: &DirectXDevices, + #[cfg(feature = "enable-renderdoc")] hwnd: HWND, + ) -> Result { let width = 1; let height = 1; - // #[cfg(not(feature = "enable-renderdoc"))] + + #[cfg(not(feature = "enable-renderdoc"))] let swap_chain = create_swap_chain(&devices.dxgi_factory, &devices.device, width, height)?; - // #[cfg(feature = "enable-renderdoc")] - // let swap_chain = - // create_swap_chain_default(&devices.dxgi_factory, &devices.device, hwnd, transparent)?; + #[cfg(feature = "enable-renderdoc")] + let swap_chain = + create_swap_chain(&devices.dxgi_factory, &devices.device, hwnd, width, height)?; + let (render_target, render_target_view, msaa_target, msaa_view, viewport) = create_resources(devices, &swap_chain, width, height)?; set_rasterizer_state(&devices.device, &devices.device_context)?; @@ -481,7 +493,7 @@ impl DirectXRenderPipelines { } } -// #[cfg(not(feature = "enable-renderdoc"))] +#[cfg(not(feature = "enable-renderdoc"))] impl DirectComposition { pub fn new(dxgi_device: &IDXGIDevice, hwnd: HWND) -> Result { let comp_device = get_comp_device(&dxgi_device)?; @@ -967,11 +979,12 @@ fn get_device( }) } -// #[cfg(not(feature = "enable-renderdoc"))] +#[cfg(not(feature = "enable-renderdoc"))] fn get_comp_device(dxgi_device: &IDXGIDevice) -> Result { Ok(unsafe { DCompositionCreateDevice(dxgi_device)? }) } +#[cfg(not(feature = "enable-renderdoc"))] fn create_swap_chain( dxgi_factory: &IDXGIFactory6, device: &ID3D11Device, @@ -998,8 +1011,8 @@ fn create_swap_chain( Ok(unsafe { dxgi_factory.CreateSwapChainForComposition(device, &desc, None)? }) } -// #[cfg(feature = "enable-renderdoc")] -fn create_swap_chain_default( +#[cfg(feature = "enable-renderdoc")] +fn create_swap_chain( dxgi_factory: &IDXGIFactory6, device: &ID3D11Device, hwnd: HWND, diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.toml index a864ece683..dd72260f52 100644 --- a/crates/zed/Cargo.toml +++ b/crates/zed/Cargo.toml @@ -70,6 +70,7 @@ gpui = { workspace = true, features = [ "x11", "font-kit", "windows-manifest", + "enable-renderdoc", ] } gpui_tokio.workspace = true