Skip to content

Added new create_heap functions to corell factory #1381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/trianglell/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn main() {
// Buffer allocations
println!("Memory heaps: {:?}", heap_types);

let heap = factory.create_heap(upload_heap, 1024);
let heap = factory.create_buffer_heap(upload_heap, 1024);
let buffer_stride = std::mem::size_of::<Vertex>() as u64;
let buffer_len = TRIANGLE.len() as u64 * buffer_stride;

Expand Down Expand Up @@ -278,7 +278,7 @@ fn main() {
let upload_size = (height * row_pitch) as u64;
println!("upload row pitch {}, total size {}", row_pitch, upload_size);

let image_upload_heap = factory.create_heap(upload_heap, upload_size);
let image_upload_heap = factory.create_buffer_heap(upload_heap, upload_size);
let image_upload_buffer = {
let buffer = factory.create_buffer(upload_size, image_stride as u64, buffer::TRANSFER_SRC).unwrap();
factory.bind_buffer_memory(&image_upload_heap, 0, buffer).unwrap()
Expand All @@ -299,7 +299,7 @@ fn main() {
let image_req = factory.get_image_requirements(&image);

let device_heap = heap_types.iter().find(|&&heap_type| heap_type.properties.contains(memory::DEVICE_LOCAL)).unwrap();
let image_heap = factory.create_heap(device_heap, image_req.size);
let image_heap = factory.create_texture_heap(device_heap, image_req.size);

let image_logo = factory.bind_image_memory(&image_heap, 0, image).unwrap();
let image_srv = factory.view_image_as_shader_resource(&image_logo, gfx_corell::format::Srgba8::get_format()).unwrap();
Expand Down
44 changes: 44 additions & 0 deletions src/backend/dx12ll/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,35 @@ impl Factory {
}
}
}
fn create_flagged_heap(&mut self, heap_type: &core::HeapType, size: u64, flags: winapi::D3D12_HEAP_FLAGS) -> native::Heap {
let mut heap = ptr::null_mut();
let desc = winapi::D3D12_HEAP_DESC {
SizeInBytes: size,
Properties: data::map_heap_properties(heap_type.properties),
Alignment: 0,
Flags: flags,
};

assert_eq!(winapi::S_OK, unsafe {
self.inner.CreateHeap(&desc, &dxguid::IID_ID3D12Heap, &mut heap)
});

native::Heap {
inner: ComPtr::new(heap as *mut _),
ty: heap_type.clone(),
size: size,
//TODO: merge with `map_heap_properties`
default_state: if !heap_type.properties.contains(memory::CPU_VISIBLE) {
winapi::D3D12_RESOURCE_STATE_COMMON
} else if heap_type.properties.contains(memory::COHERENT) {
winapi::D3D12_RESOURCE_STATE_GENERIC_READ
} else {
winapi::D3D12_RESOURCE_STATE_COPY_DEST
},
}
}


}

impl core::Factory<R> for Factory {
Expand Down Expand Up @@ -231,6 +260,21 @@ impl core::Factory<R> for Factory {
}
}

fn create_buffer_heap(&mut self, heap_type: &core::HeapType, size: u64) -> native::Heap {
self.create_flagged_heap(heap_type, size,
winapi::D3D12_HEAP_FLAGS(0) | winapi::D3D12_HEAP_FLAG_DENY_NON_RT_DS_TEXTURES | winapi::D3D12_HEAP_FLAG_DENY_RT_DS_TEXTURES)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are aliases which should be used here instead of manually assembling the flags:

  • D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS
  • D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES
  • D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES

}

fn create_texture_heap(&mut self, heap_type: &core::HeapType, size: u64) -> native::Heap {
self.create_flagged_heap(heap_type, size,
winapi::D3D12_HEAP_FLAGS(0) | winapi::D3D12_HEAP_FLAG_DENY_RT_DS_TEXTURES | winapi::D3D12_HEAP_FLAG_DENY_BUFFERS)
}

fn create_render_texture_heap(&mut self, heap_type: &core::HeapType, size: u64) -> native::Heap {
self.create_flagged_heap(heap_type, size,
winapi::D3D12_HEAP_FLAGS(0) | winapi::D3D12_HEAP_FLAG_DENY_NON_RT_DS_TEXTURES | winapi::D3D12_HEAP_FLAG_DENY_BUFFERS)
}

fn create_renderpass(&mut self, attachments: &[pass::Attachment],
subpasses: &[pass::SubpassDesc], dependencies: &[pass::SubpassDependency]) -> native::RenderPass
{
Expand Down
10 changes: 10 additions & 0 deletions src/backend/vulkanll/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ impl core::Factory<R> for Factory {
native::Heap(memory)
}

fn create_buffer_heap(&mut self, heap_type: &HeapType, size: u64) -> native::Heap {
self.create_heap(heap_type, size)
}
fn create_texture_heap(&mut self, heap_type: &HeapType, size: u64) -> native::Heap {
self.create_heap(heap_type, size)
}
fn create_render_texture_heap(&mut self, heap_type: &HeapType, size: u64) -> native::Heap {
self.create_heap(heap_type, size)
}

fn create_renderpass(&mut self, attachments: &[pass::Attachment],
subpasses: &[pass::SubpassDesc], dependencies: &[pass::SubpassDependency]) -> native::RenderPass
{
Expand Down
8 changes: 8 additions & 0 deletions src/corell/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ pub trait Factory<R: Resources> {
/// There is only a limited amount of allocations allowed depending on the implementation!
fn create_heap(&mut self, heap_type: &HeapType, size: u64) -> R::Heap;


/// Create a heap with flags depending on the resource type
///
/// DX12 implementation needs this to provide the correct flags
fn create_buffer_heap(&mut self, heap_type: &HeapType, size: u64) -> R::Heap;
fn create_texture_heap(&mut self, heap_type: &HeapType, size: u64) -> R::Heap;
fn create_render_texture_heap(&mut self, heap_type: &HeapType, size: u64) -> R::Heap;

///
fn create_renderpass(&mut self, attachments: &[pass::Attachment], subpasses: &[pass::SubpassDesc], dependencies: &[pass::SubpassDependency]) -> R::RenderPass;

Expand Down