add new feature enable-renderdoc

This commit is contained in:
Junkui Zhang 2025-07-17 16:57:23 +08:00
parent a57cbe4636
commit e560c6813f
3 changed files with 39 additions and 24 deletions

View file

@ -71,6 +71,7 @@ screen-capture = [
"scap", "scap",
] ]
windows-manifest = [] windows-manifest = []
enable-renderdoc = []
[lib] [lib]
path = "src/gpui.rs" path = "src/gpui.rs"

View file

@ -2,18 +2,15 @@ use std::{mem::ManuallyDrop, sync::Arc};
use ::util::ResultExt; use ::util::ResultExt;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
// #[cfg(not(feature = "enable-renderdoc"))] #[cfg(not(feature = "enable-renderdoc"))]
use windows::Win32::Graphics::DirectComposition::*; use windows::Win32::Graphics::DirectComposition::*;
use windows::{ use windows::Win32::{
Win32::{ Foundation::{HMODULE, HWND},
Foundation::{HMODULE, HWND}, Graphics::{
Graphics::{ Direct3D::*,
Direct3D::*, Direct3D11::*,
Direct3D11::*, Dxgi::{Common::*, *},
Dxgi::{Common::*, *},
},
}, },
core::*,
}; };
use crate::*; use crate::*;
@ -28,7 +25,7 @@ pub(crate) struct DirectXRenderer {
resources: DirectXResources, resources: DirectXResources,
globals: DirectXGlobalElements, globals: DirectXGlobalElements,
pipelines: DirectXRenderPipelines, pipelines: DirectXRenderPipelines,
// #[cfg(not(feature = "enable-renderdoc"))] #[cfg(not(feature = "enable-renderdoc"))]
_direct_composition: DirectComposition, _direct_composition: DirectComposition,
} }
@ -36,6 +33,7 @@ pub(crate) struct DirectXRenderer {
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct DirectXDevices { pub(crate) struct DirectXDevices {
dxgi_factory: IDXGIFactory6, dxgi_factory: IDXGIFactory6,
#[cfg(not(feature = "enable-renderdoc"))]
dxgi_device: IDXGIDevice, dxgi_device: IDXGIDevice,
device: ID3D11Device, device: ID3D11Device,
device_context: ID3D11DeviceContext, device_context: ID3D11DeviceContext,
@ -78,7 +76,7 @@ struct DrawInstancedIndirectArgs {
start_instance_location: u32, start_instance_location: u32,
} }
// #[cfg(not(feature = "enable-renderdoc"))] #[cfg(not(feature = "enable-renderdoc"))]
struct DirectComposition { struct DirectComposition {
comp_device: IDCompositionDevice, comp_device: IDCompositionDevice,
comp_target: IDCompositionTarget, comp_target: IDCompositionTarget,
@ -95,10 +93,12 @@ impl DirectXDevices {
get_device(&adapter, Some(&mut device), Some(&mut context))?; get_device(&adapter, Some(&mut device), Some(&mut context))?;
(device.unwrap(), context.unwrap()) (device.unwrap(), context.unwrap())
}; };
#[cfg(not(feature = "enable-renderdoc"))]
let dxgi_device: IDXGIDevice = device.cast()?; let dxgi_device: IDXGIDevice = device.cast()?;
Ok(Self { Ok(Self {
dxgi_factory, dxgi_factory,
#[cfg(not(feature = "enable-renderdoc"))]
dxgi_device, dxgi_device,
device, device,
device_context, device_context,
@ -112,20 +112,27 @@ impl DirectXRenderer {
devices.device.clone(), devices.device.clone(),
devices.device_context.clone(), devices.device_context.clone(),
)); ));
#[cfg(not(feature = "enable-renderdoc"))]
let resources = DirectXResources::new(devices)?; let resources = DirectXResources::new(devices)?;
#[cfg(feature = "enable-renderdoc")]
let resources = DirectXResources::new(devices, hwnd)?;
let globals = DirectXGlobalElements::new(&devices.device)?; let globals = DirectXGlobalElements::new(&devices.device)?;
let pipelines = DirectXRenderPipelines::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)?; 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)?; direct_composition.set_swap_chain(&resources.swap_chain)?;
Ok(DirectXRenderer { Ok(DirectXRenderer {
atlas, atlas,
devices: devices.clone(), devices: devices.clone(),
resources, resources,
globals, globals,
pipelines, pipelines,
// #[cfg(not(feature = "enable-renderdoc"))] #[cfg(not(feature = "enable-renderdoc"))]
_direct_composition: direct_composition, _direct_composition: direct_composition,
}) })
} }
@ -392,14 +399,19 @@ impl DirectXRenderer {
} }
impl DirectXResources { impl DirectXResources {
pub fn new(devices: &DirectXDevices) -> Result<Self> { pub fn new(
devices: &DirectXDevices,
#[cfg(feature = "enable-renderdoc")] hwnd: HWND,
) -> Result<Self> {
let width = 1; let width = 1;
let height = 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)?; let swap_chain = create_swap_chain(&devices.dxgi_factory, &devices.device, width, height)?;
// #[cfg(feature = "enable-renderdoc")] #[cfg(feature = "enable-renderdoc")]
// let swap_chain = let swap_chain =
// create_swap_chain_default(&devices.dxgi_factory, &devices.device, hwnd, transparent)?; create_swap_chain(&devices.dxgi_factory, &devices.device, hwnd, width, height)?;
let (render_target, render_target_view, msaa_target, msaa_view, viewport) = let (render_target, render_target_view, msaa_target, msaa_view, viewport) =
create_resources(devices, &swap_chain, width, height)?; create_resources(devices, &swap_chain, width, height)?;
set_rasterizer_state(&devices.device, &devices.device_context)?; 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 { impl DirectComposition {
pub fn new(dxgi_device: &IDXGIDevice, hwnd: HWND) -> Result<Self> { pub fn new(dxgi_device: &IDXGIDevice, hwnd: HWND) -> Result<Self> {
let comp_device = get_comp_device(&dxgi_device)?; 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<IDCompositionDevice> { fn get_comp_device(dxgi_device: &IDXGIDevice) -> Result<IDCompositionDevice> {
Ok(unsafe { DCompositionCreateDevice(dxgi_device)? }) Ok(unsafe { DCompositionCreateDevice(dxgi_device)? })
} }
#[cfg(not(feature = "enable-renderdoc"))]
fn create_swap_chain( fn create_swap_chain(
dxgi_factory: &IDXGIFactory6, dxgi_factory: &IDXGIFactory6,
device: &ID3D11Device, device: &ID3D11Device,
@ -998,8 +1011,8 @@ fn create_swap_chain(
Ok(unsafe { dxgi_factory.CreateSwapChainForComposition(device, &desc, None)? }) Ok(unsafe { dxgi_factory.CreateSwapChainForComposition(device, &desc, None)? })
} }
// #[cfg(feature = "enable-renderdoc")] #[cfg(feature = "enable-renderdoc")]
fn create_swap_chain_default( fn create_swap_chain(
dxgi_factory: &IDXGIFactory6, dxgi_factory: &IDXGIFactory6,
device: &ID3D11Device, device: &ID3D11Device,
hwnd: HWND, hwnd: HWND,

View file

@ -69,6 +69,7 @@ gpui = { workspace = true, features = [
"x11", "x11",
"font-kit", "font-kit",
"windows-manifest", "windows-manifest",
"enable-renderdoc",
] } ] }
gpui_tokio.workspace = true gpui_tokio.workspace = true