Skip to content

Commit

Permalink
[ll][render] Some descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastacyclop committed Sep 22, 2017
1 parent 7803718 commit e468599
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 608 deletions.
60 changes: 17 additions & 43 deletions examples/render/quad_render/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate env_logger;
#[macro_use]
extern crate gfx;
extern crate gfx_core as core;
extern crate gfx_backend_vulkan as back;
Expand All @@ -10,11 +11,11 @@ use std::mem;
use std::io::Cursor;

use core::{command, device as d, image as i, memory as m, pass, pso, state};
use core::{Adapter, Device, Instance};
use core::{DescriptorPool, Primitive};
use core::{Adapter, Device, Instance, Primitive};
use core::format::{Formatted, Srgba8 as ColorFormat, Vec2};
use core::pass::Subpass;
use core::target::Rect;
use gfx::pso::DescriptorStruct;
use gfx::allocators::StackAllocator as Allocator;

#[derive(Debug, Clone, Copy)]
Expand All @@ -36,6 +37,13 @@ const QUAD: [Vertex; 6] = [
Vertex { a_Pos: [ -0.5,-0.33 ], a_Uv: [0.0, 0.0] },
];

gfx_descriptor_struct! {
Descriptors {
sampled_image: gfx::pso::SampledImage,
sampler: gfx::pso::Sampler,
}
}

fn main() {
env_logger::init().unwrap();
let mut events_loop = winit::EventsLoop::new();
Expand Down Expand Up @@ -68,27 +76,10 @@ fn main() {
let vs_module = device.create_shader_module(include_bytes!("data/vs_main.spv")).unwrap();
let fs_module = device.create_shader_module(include_bytes!("data/ps_main.spv")).unwrap();

let set0_layout = device.create_descriptor_set_layout(&[
pso::DescriptorSetLayoutBinding {
binding: 0,
ty: pso::DescriptorType::SampledImage,
count: 1,
stage_flags: pso::STAGE_FRAGMENT,
}
],
);

let set1_layout = device.create_descriptor_set_layout(&[
pso::DescriptorSetLayoutBinding {
binding: 0,
ty: pso::DescriptorType::Sampler,
count: 1,
stage_flags: pso::STAGE_FRAGMENT,
}
],
gfx_allocate_descriptor_structs!(context.mut_device();
descriptors: Descriptors<back::Backend>,
);

let pipeline_layout = device.create_pipeline_layout(&[&set0_layout, &set1_layout]);
let pipeline_layout = device.create_pipeline_layout(&[&descriptors.layout()]);

let render_pass = {
let attachment = pass::Attachment {
Expand Down Expand Up @@ -168,19 +159,6 @@ fn main() {

println!("pipelines: {:?}", pipelines);

// Descriptors
let mut srv_pool = device.create_descriptor_pool(
1, // sets
&[pso::DescriptorRangeDesc { ty: pso::DescriptorType::SampledImage, count: 1 }],
);
let set0 = srv_pool.allocate_sets(&[&set0_layout]);

let mut sampler_pool = device.create_descriptor_pool(
1, // sets
&[pso::DescriptorRangeDesc { ty: pso::DescriptorType::Sampler, count: 1 }],
);
let set1 = sampler_pool.allocate_sets(&[&set1_layout]);

// Framebuffer creation
let frame_rtvs = backbuffers.iter().map(|backbuffer| {
context.mut_device()
Expand Down Expand Up @@ -266,14 +244,14 @@ fn main() {

device.update_descriptor_sets(&[
pso::DescriptorSetWrite {
set: &set0[0],
set: descriptors.set(),
binding: 0,
array_offset: 0,
write: pso::DescriptorWrite::SampledImage(vec![(image_srv.resource(), i::ImageLayout::Undefined)]),
},
pso::DescriptorSetWrite {
set: &set1[0],
binding: 0,
set: descriptors.set(),
binding: 1,
array_offset: 0,
write: pso::DescriptorWrite::Sampler(vec![sampler.resource()]),
},
Expand Down Expand Up @@ -348,7 +326,7 @@ fn main() {
// TODO: data instead of upload ?
// TODO: vertex access ?
cmd_buffer.bind_vertex_buffers(pso::VertexBufferSet(vec![(vertex_buffer.resource(), 0)]));
cmd_buffer.bind_graphics_descriptor_sets(&pipeline_layout, 0, &[&set0[0], &set1[0]]); //TODO
cmd_buffer.bind_graphics_descriptor_sets(&pipeline_layout, 0, &[&descriptors.set()]); // TODO

{
let mut encoder = cmd_buffer.begin_renderpass_inline(
Expand All @@ -366,10 +344,6 @@ fn main() {
}

println!("cleanup!");
device.destroy_descriptor_pool(srv_pool);
device.destroy_descriptor_pool(sampler_pool);
device.destroy_descriptor_set_layout(set0_layout);
device.destroy_descriptor_set_layout(set1_layout);
device.destroy_shader_module(vs_module);
device.destroy_shader_module(fs_module);
device.destroy_pipeline_layout(pipeline_layout);
Expand Down
1 change: 1 addition & 0 deletions src/backend/empty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ impl core::RawCommandBuffer<Backend> for RawCommandBuffer {
}

// Dummy descriptor pool.
#[derive(Debug)]
pub struct DescriptorPool;
impl core::DescriptorPool<Backend> for DescriptorPool {
fn allocate_sets(&mut self, _: &[&()]) -> Vec<()> {
Expand Down
1 change: 1 addition & 0 deletions src/backend/gl/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub struct DescriptorSetLayout;
pub struct DescriptorSet;

#[allow(missing_copy_implementations)]
#[derive(Debug)]
pub struct DescriptorPool {}

impl core::DescriptorPool<Backend> for DescriptorPool {
Expand Down
4 changes: 3 additions & 1 deletion src/core/src/pso/descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Descriptor sets and layouts.
use std::fmt;

use {Backend};
use image::ImageLayout;
use super::ShaderStageFlags;
Expand Down Expand Up @@ -65,7 +67,7 @@ pub struct DescriptorRangeDesc {
}

///
pub trait DescriptorPool<B: Backend>: Send {
pub trait DescriptorPool<B: Backend>: Send + fmt::Debug {
/// Allocate one or multiple descriptor sets from the pool.
///
/// Each descriptor set will be allocated from the pool according to the corresponding set layout.
Expand Down
47 changes: 46 additions & 1 deletion src/render/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::memory::{HeapProperties,
use memory::{self, Allocator, Typed};
use handle::{self, GarbageSender};
use handle::inner::*;
use {core, buffer, image, format, mapping};
use {core, buffer, image, format, mapping, pso};
use Backend;

/*
Expand Down Expand Up @@ -492,6 +492,51 @@ impl<B: Backend> Device<B> {
.map(Typed::new)
}

#[doc(hidden)]
pub fn allocate_descriptor_sets(
&mut self,
sets_bindings: &[&[core::pso::DescriptorSetLayoutBinding]],
) -> Vec<(handle::raw::DescriptorSetLayout<B>, pso::DescriptorSet<B>)> {
use core::DescriptorPool as CDP;

let set_count = sets_bindings.len();
let mut layouts = Vec::with_capacity(set_count);
let mut ranges = Vec::new();
for &bindings in sets_bindings {
layouts.push(self.create_descriptor_set_layout(bindings));
for binding in bindings {
ranges.push(core::pso::DescriptorRangeDesc {
ty: binding.ty,
count: binding.count,
});
}
}

let mut pool = self.raw.create_descriptor_pool(set_count, &ranges[..]);
let sets = {
let layout_refs = layouts.iter()
.map(|layout| layout.resource())
.collect::<Vec<_>>();
pool.allocate_sets(&layout_refs[..])
};

let pool = handle::raw::DescriptorPool::from(
DescriptorPool::new(pool, (), self.garbage.clone()));
layouts.into_iter().zip(sets.into_iter()).map(|(layout, set)| {
(layout, pso::DescriptorSet {
resource: set,
pool: pool.clone()
})
}).collect()
}

pub(crate) fn create_descriptor_set_layout(
&mut self,
bindings: &[core::pso::DescriptorSetLayoutBinding]
) -> handle::raw::DescriptorSetLayout<B> {
let layout = self.raw.create_descriptor_set_layout(bindings);
DescriptorSetLayout::new(layout, (), self.garbage.clone()).into()
}
/*
/// Creates a `ShaderSet` from the supplied vertex and pixel shader source code.
fn create_shader_set(&mut self, vs_code: &[u8], ps_code: &[u8])
Expand Down
8 changes: 5 additions & 3 deletions src/render/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ impl<B: Backend> InnerGarbageCollector<B> {
ShaderResourceView(srv) => dev.destroy_shader_resource_view(srv),
UnorderedAccessView(uav) => dev.destroy_unordered_access_view(uav),
Sampler(s) => dev.destroy_sampler(s),
DescriptorPool(dp) => dev.destroy_descriptor_pool(dp),
DescriptorSetLayout(dsl) => dev.destroy_descriptor_set_layout(dsl),
}
}
}
Expand All @@ -72,7 +74,7 @@ impl<B: Backend> Drop for InnerGarbageCollector<B> {
}

macro_rules! define_resources {
($($name:ident: $info:path,)*) => {
($($name:ident: $info:ty,)*) => {
#[derive(Debug)]
pub enum Garbage<B: Backend> {
$( $name(B::$name), )*
Expand Down Expand Up @@ -234,8 +236,8 @@ define_resources! {
ShaderResourceView: ::handle::ViewSource<B>,
UnorderedAccessView: ::handle::ViewSource<B>,
Sampler: ::image::SamplerInfo,
// DescriptorPool
// DescriptorSetLayout
DescriptorPool: (),
DescriptorSetLayout: (),
// Fence
// Semaphore
}
Expand Down
4 changes: 2 additions & 2 deletions src/render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ extern crate mint;
#[macro_use]
extern crate log;
extern crate draw_state;
extern crate gfx_core as core;
pub extern crate gfx_core as core;

/// public re-exported traits
pub mod traits {
Expand Down Expand Up @@ -135,9 +135,9 @@ pub mod pso;
/*
/// Shaders
pub mod shade;
*/
/// Convenience macros
pub mod macros;
*/

use std::collections::VecDeque;
use core::{CommandQueue, QueueType, Surface, Swapchain, Device as CoreDevice};
Expand Down
73 changes: 73 additions & 0 deletions src/render/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! Various helper macros.
#[macro_export]
macro_rules! gfx_format {
($name:ident : $surface:ident = $container:ident<$channel:ident>) => {
impl $crate::format::Formatted for $name {
type Surface = $crate::format::$surface;
type Channel = $crate::format::$channel;
type View = $crate::format::$container<
<$crate::format::$channel as $crate::format::ChannelTyped>::ShaderType
>;
}
}
}

#[macro_export]
macro_rules! gfx_descriptor_struct {
($name:ident { $( $field:ident: $bind:ty, )* }) => {
#[allow(missing_docs)]
#[derive(Debug)]
pub struct $name<B: $crate::Backend> {
$( pub $field: (), )*
layout: $crate::handle::raw::DescriptorSetLayout<B>,
set: $crate::pso::DescriptorSet<B>,
}

impl<B: $crate::Backend> $crate::pso::DescriptorStruct<B> for $name<B> {
fn from_raw(
layout: $crate::handle::raw::DescriptorSetLayout<B>,
set: $crate::pso::DescriptorSet<B>
) -> Self {
$name {
$( $field: (), )*
layout,
set
}
}

fn layout_bindings() -> Vec<$crate::core::pso::DescriptorSetLayoutBinding> {
let mut bindings = Vec::new();
$({
let binding = bindings.len();
bindings.push($crate::core::pso::DescriptorSetLayoutBinding {
binding,
ty: <$bind as $crate::pso::Bind>::desc_type(),
count: <$bind as $crate::pso::Bind>::desc_count(),
// TODO: specify stage
stage_flags: $crate::core::pso::ShaderStageFlags::all(),
});
})*
bindings
}

fn layout(&self) -> &B::DescriptorSetLayout { self.layout.resource() }
fn set(&self) -> &B::DescriptorSet { self.set.resource() }
}
}
}

#[macro_export]
macro_rules! gfx_allocate_descriptor_structs {
($device:expr; $( $name:ident: $struct:ty, )*) => {
let ($( $name, )*) = {
let mut layouts_sets = $device.allocate_descriptor_sets(
&[$( &<$struct as $crate::pso::DescriptorStruct<_>>::layout_bindings()[..], )*]
).into_iter();
($( {
let (layout, set) = layouts_sets.next().unwrap();
<$struct as $crate::pso::DescriptorStruct<_>>::from_raw(layout, set)
}, )*)
};
}
}
Loading

0 comments on commit e468599

Please sign in to comment.