Skip to content

Commit

Permalink
feat: adding resource deletion queue
Browse files Browse the repository at this point in the history
  • Loading branch information
SkillerRaptor committed Sep 24, 2024
1 parent 5ac0346 commit d501efe
Show file tree
Hide file tree
Showing 16 changed files with 322 additions and 100 deletions.
14 changes: 7 additions & 7 deletions crates/hyper_render/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ pub struct Renderer {

indices: Arc<dyn Buffer>,
mesh: Arc<dyn Buffer>,
normals: Arc<dyn Buffer>,
positions: Arc<dyn Buffer>,
_normals: Arc<dyn Buffer>,
_positions: Arc<dyn Buffer>,
material: Arc<dyn Buffer>,

pipeline: Arc<dyn GraphicsPipeline>,
pipeline_layout: Arc<dyn PipelineLayout>,
_pipeline_layout: Arc<dyn PipelineLayout>,

command_list: Arc<dyn CommandList>,

Expand Down Expand Up @@ -113,20 +113,20 @@ impl Renderer {

indices,
mesh,
normals,
positions,
_normals: normals,
_positions: positions,
material,

pipeline,
pipeline_layout,
_pipeline_layout: pipeline_layout,

command_list,

graphics_device: Arc::clone(graphics_device),
}
}

pub fn render(&mut self, mut surface: &mut Box<dyn Surface>, scene: &Scene) {
pub fn render(&mut self, mut surface: &mut Box<dyn Surface>, _scene: &Scene) {
self.graphics_device
.begin_frame(&mut surface, self.frame_index);

Expand Down
4 changes: 2 additions & 2 deletions crates/hyper_render/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use hyper_ecs::Registry;

pub struct Scene {
registry: Registry,
_registry: Registry,
}

impl Scene {
pub fn new() -> Self {
Self {
registry: Registry::new(),
_registry: Registry::new(),
}
}
}
31 changes: 10 additions & 21 deletions crates/hyper_rhi/src/d3d12/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use std::{
fmt::{self, Debug, Formatter},
mem::ManuallyDrop,
sync::Arc,
};

Expand Down Expand Up @@ -45,7 +46,7 @@ pub(crate) struct Buffer {
resource_handle_pair: ResourceHandlePair,

size: usize,
resource: Option<gpu_allocator::d3d12::Resource>,
resource: ManuallyDrop<gpu_allocator::d3d12::Resource>,

graphics_device: Arc<GraphicsDeviceShared>,
}
Expand Down Expand Up @@ -81,7 +82,7 @@ impl Buffer {
resource_handle_pair,

size: size as usize,
resource: Some(resource),
resource: ManuallyDrop::new(resource),

graphics_device: Arc::clone(graphics_device),
}
Expand Down Expand Up @@ -112,7 +113,7 @@ impl Buffer {
resource_handle_pair: ResourceHandlePair::default(),

size: size as usize,
resource: Some(resource),
resource: ManuallyDrop::new(resource),

graphics_device: Arc::clone(graphics_device),
}
Expand Down Expand Up @@ -178,32 +179,20 @@ impl Buffer {
}

pub(crate) fn gpu_address(&self) -> u64 {
unsafe {
self.resource
.as_ref()
.unwrap()
.resource()
.GetGPUVirtualAddress()
}
unsafe { self.resource.resource().GetGPUVirtualAddress() }
}

pub(crate) fn resource(&self) -> &gpu_allocator::d3d12::Resource {
self.resource.as_ref().unwrap()
&self.resource
}
}

impl Drop for Buffer {
fn drop(&mut self) {
self.graphics_device
.retire_handle(self.resource_handle_pair.srv());
self.graphics_device
.retire_handle(self.resource_handle_pair.uav());
self.graphics_device
.allocator()
.lock()
.unwrap()
.free_resource(self.resource.take().unwrap())
.unwrap();
self.graphics_device.resource_queue().push_buffer(
unsafe { ManuallyDrop::take(&mut self.resource) },
self.resource_handle_pair,
);
}
}

Expand Down
97 changes: 92 additions & 5 deletions crates/hyper_rhi/src/d3d12/graphics_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//

use std::{
mem::{self, ManuallyDrop},
ptr,
sync::{Arc, Mutex},
};
Expand All @@ -30,7 +31,9 @@ use windows::{
ID3D12Fence,
ID3D12GraphicsCommandList,
ID3D12Object,
ID3D12PipelineState,
ID3D12Resource,
ID3D12RootSignature,
D3D12_COMMAND_LIST_TYPE_DIRECT,
D3D12_COMMAND_QUEUE_DESC,
D3D12_COMMAND_QUEUE_FLAG_NONE,
Expand Down Expand Up @@ -71,12 +74,54 @@ use crate::{
graphics_device::GraphicsDeviceDescriptor,
graphics_pipeline::GraphicsPipelineDescriptor,
pipeline_layout::PipelineLayoutDescriptor,
resource::ResourceHandle,
shader_module::ShaderModuleDescriptor,
surface::SurfaceDescriptor,
texture::TextureDescriptor,
};

pub(crate) struct ResourceQueue {
buffers: Mutex<
Vec<(
ManuallyDrop<gpu_allocator::d3d12::Resource>,
ResourceHandlePair,
)>,
>,
graphics_pipelines: Mutex<Vec<ID3D12PipelineState>>,
pipeline_layouts: Mutex<Vec<ID3D12RootSignature>>,
textures: Mutex<Vec<ManuallyDrop<gpu_allocator::d3d12::Resource>>>,
}

impl ResourceQueue {
pub(crate) fn push_buffer(
&self,
resource: gpu_allocator::d3d12::Resource,
resource_handle_pair: ResourceHandlePair,
) {
self.buffers
.lock()
.unwrap()
.push((ManuallyDrop::new(resource), resource_handle_pair));
}

pub(crate) fn push_graphics_pipeline(&self, graphics_pipeline: ID3D12PipelineState) {
self.graphics_pipelines
.lock()
.unwrap()
.push(graphics_pipeline);
}

pub(crate) fn push_pipeline_layout(&self, pipeline_layout: ID3D12RootSignature) {
self.pipeline_layouts.lock().unwrap().push(pipeline_layout);
}

pub(crate) fn push_texture(&self, resource: gpu_allocator::d3d12::Resource) {
self.textures
.lock()
.unwrap()
.push(ManuallyDrop::new(resource));
}
}

#[derive(Debug)]
pub(crate) struct FrameData {
pub(crate) command_list: ID3D12GraphicsCommandList,
Expand All @@ -90,6 +135,8 @@ pub(crate) struct GraphicsDeviceShared {
fence_event: HANDLE,
fence: ID3D12Fence,

resource_queue: ResourceQueue,

upload_manager: UploadManager,
descriptor_manager: DescriptorManager,

Expand All @@ -105,6 +152,35 @@ pub(crate) struct GraphicsDeviceShared {
}

impl GraphicsDeviceShared {
pub(crate) fn free_resources(&self) {
let buffers = mem::take(&mut *self.resource_queue.buffers.lock().unwrap());
for (mut resource, resource_handle_pair) in buffers {
self.descriptor_manager
.retire_handle(resource_handle_pair.srv());
self.descriptor_manager
.retire_handle(resource_handle_pair.uav());
self.allocator()
.lock()
.unwrap()
.free_resource(unsafe { ManuallyDrop::take(&mut resource) })
.unwrap();
}

let _graphics_pipelines =
mem::take(&mut *self.resource_queue.graphics_pipelines.lock().unwrap());
let _pipeline_layouts =
mem::take(&mut *self.resource_queue.pipeline_layouts.lock().unwrap());

let textures = mem::take(&mut *self.resource_queue.textures.lock().unwrap());
for mut resource in textures {
self.allocator()
.lock()
.unwrap()
.free_resource(unsafe { ManuallyDrop::take(&mut resource) })
.unwrap();
}
}

pub(crate) fn set_debug_name(&self, object: &ID3D12Object, label: &str) {
unsafe {
object.SetName(&HSTRING::from(label)).unwrap();
Expand All @@ -120,10 +196,6 @@ impl GraphicsDeviceShared {
.allocate_buffer_handle(&self.device, resource, size)
}

pub(crate) fn retire_handle(&self, handle: ResourceHandle) {
self.descriptor_manager.retire_handle(handle);
}

pub(crate) fn upload_buffer(
self: &Arc<GraphicsDeviceShared>,
source: &[u8],
Expand Down Expand Up @@ -156,6 +228,10 @@ impl GraphicsDeviceShared {
&self.rtv_heap
}

pub(crate) fn resource_queue(&self) -> &ResourceQueue {
&self.resource_queue
}

pub(crate) fn current_frame(&self) -> &FrameData {
let index = *self.current_frame_index.lock().unwrap() % crate::graphics_device::FRAME_COUNT;
&self.frames[index as usize]
Expand All @@ -164,6 +240,8 @@ impl GraphicsDeviceShared {

impl Drop for GraphicsDeviceShared {
fn drop(&mut self) {
self.free_resources();

unsafe {
CloseHandle(self.fence_event).unwrap();
}
Expand Down Expand Up @@ -235,6 +313,13 @@ impl GraphicsDevice {
fence_event,
fence,

resource_queue: ResourceQueue {
buffers: Mutex::new(Vec::new()),
graphics_pipelines: Mutex::new(Vec::new()),
pipeline_layouts: Mutex::new(Vec::new()),
textures: Mutex::new(Vec::new()),
},

upload_manager,
descriptor_manager,

Expand Down Expand Up @@ -416,6 +501,8 @@ impl crate::graphics_device::GraphicsDevice for GraphicsDevice {
}
}

// self.shared.free_resources();

if surface.resized() {
surface.rebuild();
}
Expand Down
22 changes: 15 additions & 7 deletions crates/hyper_rhi/src/d3d12/graphics_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use std::{
ffi::c_void,
fmt::{self, Debug, Formatter},
mem,
mem::{self, ManuallyDrop},
sync::Arc,
};

Expand Down Expand Up @@ -42,7 +42,7 @@ use crate::{
};

pub struct GraphicsPipeline {
pipeline_state: ID3D12PipelineState,
raw: ManuallyDrop<ID3D12PipelineState>,

layout: Arc<dyn crate::pipeline_layout::PipelineLayout>,

Expand All @@ -68,7 +68,7 @@ impl GraphicsPipeline {
.unwrap()
.code();
let mut state_descriptor = D3D12_GRAPHICS_PIPELINE_STATE_DESC {
pRootSignature: unsafe { mem::transmute_copy(layout.root_signature()) },
pRootSignature: unsafe { mem::transmute_copy(layout.raw()) },
VS: D3D12_SHADER_BYTECODE {
pShaderBytecode: vertex_shader_code.as_ptr() as *const c_void,
BytecodeLength: vertex_shader_code.len(),
Expand Down Expand Up @@ -131,7 +131,7 @@ impl GraphicsPipeline {
}

Self {
pipeline_state,
raw: ManuallyDrop::new(pipeline_state),

layout: Arc::clone(descriptor.layout),

Expand All @@ -143,15 +143,23 @@ impl GraphicsPipeline {
&self.layout
}

pub(crate) fn pipeline_state(&self) -> &ID3D12PipelineState {
&self.pipeline_state
pub(crate) fn raw(&self) -> &ID3D12PipelineState {
&self.raw
}
}

impl Drop for GraphicsPipeline {
fn drop(&mut self) {
self.graphics_device
.resource_queue()
.push_graphics_pipeline(unsafe { ManuallyDrop::take(&mut self.raw) });
}
}

impl Debug for GraphicsPipeline {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("GraphicsPipeline")
.field("pipeline_state", &self.pipeline_state)
.field("raw", &self.raw)
.field("layout", &self.layout)
.finish()
}
Expand Down
Loading

0 comments on commit d501efe

Please sign in to comment.