Skip to content

Commit

Permalink
Auto merge of #1244 - msiglreith:ll_cbv, r=kvark
Browse files Browse the repository at this point in the history
[ll] Add Constant Buffer Views

Constant buffer views (Cbv) are a concept introduced by dx12, which uses views for everything now.
Cbvs needs to be explicitly created in contrast to e.g vertex buffer views.
In vulkan these are represented as the underlying buffer and a range.
  • Loading branch information
homu committed May 1, 2017
2 parents 40019ca + 7071ae1 commit 3073c5f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
37 changes: 32 additions & 5 deletions src/backend/vulkanll/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,14 @@ impl core::Factory<R> for Factory {
Ok(image.0)
}

fn view_buffer_as_constant(&mut self, buffer: &native::Buffer, offset: usize, size: usize) -> Result<native::ConstantBufferView, f::TargetViewError> {
Ok(native::ConstantBufferView {
buffer: buffer.inner,
offset: offset,
size: size,
})
}

fn view_image_as_render_target(&mut self, image: &native::Image, format: format::Format) -> Result<native::RenderTargetView, f::TargetViewError> {
let view = self.create_image_view(image, format);
let rtv = native::RenderTargetView {
Expand Down Expand Up @@ -1012,7 +1020,7 @@ impl core::Factory<R> for Factory {

fn update_descriptor_sets(&mut self, writes: &[f::DescriptorSetWrite<R>]) {
let mut image_infos = Vec::new();
// let mut buffer_infos = Vec::new();
let mut buffer_infos = Vec::new();
// let mut texel_buffer_views = Vec::new();

for write in writes {
Expand Down Expand Up @@ -1042,39 +1050,56 @@ impl core::Factory<R> for Factory {
}
}

f::DescriptorWrite::ConstantBuffer(ref cbvs) => {
for cbv in cbvs {
buffer_infos.push(vk::DescriptorBufferInfo {
buffer: cbv.buffer,
offset: cbv.offset as u64,
range: cbv.size as u64,
});
}
}

_ => unimplemented!(), // TODO
};
}

// Track current subslice for each write
let mut cur_image_index = 0;
let mut cur_buffer_index = 0;

let writes = writes.iter().map(|write| {
let (ty, count, image_info, buffer_info, texel_buffer_view) = match write.write {
f::DescriptorWrite::Sampler(ref samplers) => {
let info_ptr = &image_infos[cur_image_index];
let info_ptr = &image_infos[cur_image_index] as *const _;
cur_image_index += samplers.len();

(vk::DescriptorType::Sampler, samplers.len(),
info_ptr, ptr::null(), ptr::null())
}
f::DescriptorWrite::SampledImage(ref images) => {
let info_ptr = &image_infos[cur_image_index];
let info_ptr = &image_infos[cur_image_index] as *const _;
cur_image_index += images.len();

(vk::DescriptorType::SampledImage, images.len(),
info_ptr, ptr::null(), ptr::null())
}
f::DescriptorWrite::StorageImage(ref images) => {
let info_ptr = &image_infos[cur_image_index];
let info_ptr = &image_infos[cur_image_index] as *const _;
cur_image_index += images.len();

(vk::DescriptorType::StorageImage, images.len(),
info_ptr, ptr::null(), ptr::null())
}
f::DescriptorWrite::ConstantBuffer(ref cbvs) => {
let info_ptr = &buffer_infos[cur_buffer_index] as *const _;
cur_buffer_index += cbvs.len();

(vk::DescriptorType::UniformBuffer, cbvs.len(),
ptr::null(), info_ptr, ptr::null())
}
f::DescriptorWrite::InputAttachment(ref images) => {
let info_ptr = &image_infos[cur_image_index];
let info_ptr = &image_infos[cur_image_index] as *const _;
cur_image_index += images.len();

(vk::DescriptorType::InputAttachment, images.len(),
Expand Down Expand Up @@ -1249,6 +1274,8 @@ impl core::Factory<R> for Factory {
unsafe { self.inner.0.destroy_image_view(rtv.view, None); }
}

fn destroy_constant_buffer_view(&mut self, _: native::ConstantBufferView) { }

fn destroy_shader_resource_view(&mut self, srv: native::ShaderResourceView) {
match srv {
native::ShaderResourceView::Buffer => (),
Expand Down
1 change: 1 addition & 0 deletions src/backend/vulkanll/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ impl core::Resources for Resources {
type Buffer = native::Buffer;
type UnboundImage = factory::UnboundImage;
type Image = native::Image;
type ConstantBufferView = native::ConstantBufferView;
type ShaderResourceView = native::ShaderResourceView;
type UnorderedAccessView = native::UnorderedAccessView;
type RenderTargetView = native::RenderTargetView;
Expand Down
7 changes: 7 additions & 0 deletions src/backend/vulkanll/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ pub struct Image(pub vk::Image);
#[derive(Debug, Hash)]
pub struct Sampler(pub vk::Sampler);

#[derive(Debug, Hash)]
pub struct ConstantBufferView {
pub buffer: vk::Buffer,
pub offset: usize,
pub size: usize,
}

#[derive(Debug, Hash)]
pub enum ShaderResourceView {
Buffer,
Expand Down
8 changes: 7 additions & 1 deletion src/corell/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub enum DescriptorWrite<'a, R: Resources> {
StorageImage(Vec<(&'a R::ShaderResourceView, memory::ImageLayout)>),
UniformTexelBuffer,
StorageTexelBuffer,
ConstantBuffer,
ConstantBuffer(Vec<&'a R::ConstantBufferView>),
StorageBuffer,
InputAttachment(Vec<(&'a R::ShaderResourceView, memory::ImageLayout)>),
}
Expand Down Expand Up @@ -157,6 +157,9 @@ pub trait Factory<R: Resources> {
///
fn bind_image_memory(&mut self, heap: &R::Heap, offset: u64, image: R::UnboundImage) -> Result<R::Image, image::CreationError>;

///
fn view_buffer_as_constant(&mut self, buffer: &R::Buffer, offset: usize, size: usize) -> Result<R::ConstantBufferView, TargetViewError>;

///
fn view_image_as_render_target(&mut self, image: &R::Image, format: format::Format) -> Result<R::RenderTargetView, TargetViewError>;

Expand Down Expand Up @@ -268,6 +271,9 @@ pub trait Factory<R: Resources> {
///
fn destroy_render_target_view(&mut self, R::RenderTargetView);

///
fn destroy_constant_buffer_view(&mut self, R::ConstantBufferView);

///
fn destroy_shader_resource_view(&mut self, R::ShaderResourceView);

Expand Down
1 change: 1 addition & 0 deletions src/corell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ pub trait Resources: Clone + Hash + Debug + Any {
type Buffer: Debug + Any + Send + Sync;
type UnboundImage: Debug + Any + Send + Sync;
type Image: Debug + Any + Send + Sync;
type ConstantBufferView: Debug + Any + Send + Sync;
type ShaderResourceView: Debug + Any + Send + Sync;
type UnorderedAccessView: Debug + Any + Send + Sync;
type RenderTargetView: Debug + Any + Send + Sync;
Expand Down

0 comments on commit 3073c5f

Please sign in to comment.