Skip to content

Commit

Permalink
created msaa image resource, some todos to do for sure and renaming, …
Browse files Browse the repository at this point in the history
…but should work.
  • Loading branch information
brandonpollack23 committed May 4, 2020
1 parent 5abe8fc commit 1733b82
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 124 deletions.
2 changes: 1 addition & 1 deletion examples/09_mip_levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use sarekt::{
buffers_and_images::{
BufferType, IndexBufferElemSize, MagnificationMinificationFilter, TextureAddressMode,
},
config::{Config, PresentMode},
config::Config,
drawable_object::DrawableObject,
vertex_bindings::{DefaultForwardShaderLayout, DefaultForwardShaderVertex},
Drawer, Renderer, VulkanRenderer,
Expand Down
31 changes: 18 additions & 13 deletions src/image_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ pub trait ImageData {

#[derive(Copy, Clone, Debug)]
pub enum ImageDataFormat {
R8G8B8,
B8G8R8,
B8G8R8A8,
R8G8B8A8,
RGB16,
RGBA16,
R8G8B8Srgb,
B8G8R8Srgb,
B8G8R8A8Srgb,
R8G8B8A8Srgb,

R8G8B8Unorm,
B8G8R8Unorm,
B8G8R8A8Unorm,
R8G8B8A8Unorm,
RGB16Unorm,
RGBA16Unorm,
// Depth Buffer Formats
D32Float,
D32FloatS8,
Expand Down Expand Up @@ -72,16 +77,16 @@ impl ImageData for image::DynamicImage {

fn format(&self) -> SarektResult<ImageDataFormat> {
match self {
image::DynamicImage::ImageBgr8(_) => Ok(B8G8R8A8),
image::DynamicImage::ImageBgr8(_) => Ok(B8G8R8A8Srgb),
image::DynamicImage::ImageLuma8(_) => Err(SarektError::UnsupportedImageFormat),
image::DynamicImage::ImageLumaA8(_) => Err(SarektError::UnsupportedImageFormat),
image::DynamicImage::ImageRgb8(_) => Ok(R8G8B8),
image::DynamicImage::ImageRgba8(_) => Ok(R8G8B8A8),
image::DynamicImage::ImageBgra8(_) => Ok(B8G8R8A8),
image::DynamicImage::ImageRgb8(_) => Ok(R8G8B8Srgb),
image::DynamicImage::ImageRgba8(_) => Ok(R8G8B8A8Srgb),
image::DynamicImage::ImageBgra8(_) => Ok(B8G8R8A8Srgb),
image::DynamicImage::ImageLuma16(_) => Err(SarektError::UnsupportedImageFormat),
image::DynamicImage::ImageLumaA16(_) => Err(SarektError::UnsupportedImageFormat),
image::DynamicImage::ImageRgb16(_) => Ok(RGB16),
image::DynamicImage::ImageRgba16(_) => Ok(RGBA16),
image::DynamicImage::ImageRgb16(_) => Ok(RGB16Unorm),
image::DynamicImage::ImageRgba16(_) => Ok(RGBA16Unorm),
}
}
}
Expand Down Expand Up @@ -115,6 +120,6 @@ impl ImageData for Monocolor {
}

fn format(&self) -> SarektResult<ImageDataFormat> {
Ok(R8G8B8A8)
Ok(R8G8B8A8Srgb)
}
}
14 changes: 12 additions & 2 deletions src/renderer/buffers_and_images.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
error::{SarektError, SarektResult},
image_data::{ImageData, ImageDataFormat},
renderer::config::NumSamples,
};
use log::warn;
use slotmap::{DefaultKey, SlotMap};
Expand Down Expand Up @@ -163,7 +164,7 @@ pub unsafe trait BufferAndImageLoader {
/// does not give it any initial value, only a size and format. This is
/// useful for initializing internally used attachments, depth buffers, etc.
fn create_uninitialized_image(
&self, dimensions: (u32, u32), format: ImageDataFormat,
&self, dimensions: (u32, u32), format: ImageDataFormat, num_samples: NumSamples,
) -> SarektResult<Self::BackendHandle>;

/// Deletes that resource, baby!
Expand Down Expand Up @@ -318,14 +319,23 @@ where
/// memory.
pub(crate) fn create_uninitialized_image(
this: &Arc<RwLock<Self>>, dimensions: (u32, u32), format: ImageDataFormat,
) -> SarektResult<(BufferImageHandle<BL>, BufferOrImage<BL::BackendHandle>)> {
Self::create_uninitialized_image_msaa(this, dimensions, format, NumSamples::One)
}

/// Same as above but allows for MSAA to be used, useful when creating
/// internal render targets.
pub(crate) fn create_uninitialized_image_msaa(
this: &Arc<RwLock<Self>>, dimensions: (u32, u32), format: ImageDataFormat,
num_samples: NumSamples,
) -> SarektResult<(BufferImageHandle<BL>, BufferOrImage<BL::BackendHandle>)> {
let mut buffer_store = this
.write()
.expect("Could not unlock BufferStore due to previous panic");

let buffer_backend_handle = buffer_store
.buffer_image_loader
.create_uninitialized_image(dimensions, format)?;
.create_uninitialized_image(dimensions, format, num_samples)?;
let buffer_or_image = BufferOrImage::new(buffer_backend_handle, ResourceType::Image);

let inner_key = buffer_store
Expand Down
30 changes: 22 additions & 8 deletions src/renderer/config.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/// Sarekt configuration. Sane defaults provided (no AA, etc).
#[derive(Builder)]
#[builder(default)]
pub struct Config<'a> {
pub struct Config {
pub requested_width: u32,
pub requested_height: u32,
pub application_details: ApplicationDetails<'a>,
pub engine_details: EngineDetails<'a>,
pub application_details: ApplicationDetails<'static>,
pub engine_details: EngineDetails<'static>,
pub present_mode: PresentMode,
pub aa_config: AntiAliasingConfig,
}
impl<'a> Config<'a> {
pub fn builder() -> ConfigBuilder<'a> {
impl Config {
pub fn builder() -> ConfigBuilder {
ConfigBuilder::default()
}
}
impl<'a> Default for Config<'a> {
impl<'a> Default for Config {
fn default() -> Self {
Self {
requested_width: 800,
Expand Down Expand Up @@ -132,10 +132,24 @@ impl Default for PresentMode {
/// TODO(issue#33) other AA styles.
#[derive(Copy, Clone)]
pub enum AntiAliasingConfig {
MSAA(u32),
MSAA(NumSamples),
// TODO(issue#32) just here to get rid of lint errors, remove when there are more types.
Unreachable,
}
impl Default for AntiAliasingConfig {
fn default() -> AntiAliasingConfig {
AntiAliasingConfig::MSAA(1)
AntiAliasingConfig::MSAA(NumSamples::default())
}
}
#[derive(Copy, Clone)]
pub enum NumSamples {
One,
Two,
Four,
Eight,
}
impl Default for NumSamples {
fn default() -> NumSamples {
NumSamples::One
}
}
2 changes: 1 addition & 1 deletion src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub use crate::{
};
pub use shaders::{ShaderHandle, ShaderType};
pub use vulkan::{
vulkan_buffer_image_functions::VulkanBufferFunctions, vulkan_renderer::VulkanRenderer,
vulkan_buffer_image_functions::VulkanBufferImageFunctions, vulkan_renderer::VulkanRenderer,
};

use crate::{
Expand Down
71 changes: 70 additions & 1 deletion src/renderer/vulkan/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
use crate::renderer::{vulkan::vulkan_shader_functions::VulkanShaderFunctions, ShaderHandle};
use crate::{
error::{SarektError, SarektResult},
image_data::ImageDataFormat,
renderer::{
config::NumSamples, vulkan::vulkan_shader_functions::VulkanShaderFunctions, ShaderHandle,
},
};
use ash::vk;
use std::convert::TryFrom;

pub mod images;
pub mod queues;
Expand All @@ -8,3 +16,64 @@ pub mod vulkan_shader_functions;
pub mod vulkan_vertex_bindings;

pub type VulkanShaderHandle = ShaderHandle<VulkanShaderFunctions>;

impl From<NumSamples> for vk::SampleCountFlags {
fn from(num_samples: NumSamples) -> vk::SampleCountFlags {
match num_samples {
NumSamples::One => vk::SampleCountFlags::TYPE_1,
NumSamples::Two => vk::SampleCountFlags::TYPE_2,
NumSamples::Four => vk::SampleCountFlags::TYPE_4,
NumSamples::Eight => vk::SampleCountFlags::TYPE_8,
}
}
}

impl From<ImageDataFormat> for vk::Format {
fn from(image_data_format: ImageDataFormat) -> vk::Format {
match image_data_format {
ImageDataFormat::R8G8B8Srgb => vk::Format::R8G8B8_SRGB,
ImageDataFormat::B8G8R8Srgb => vk::Format::B8G8R8_SRGB,
ImageDataFormat::B8G8R8A8Srgb => vk::Format::B8G8R8A8_SRGB,
ImageDataFormat::R8G8B8A8Srgb => vk::Format::R8G8B8A8_SRGB,

ImageDataFormat::R8G8B8Unorm => vk::Format::R8G8B8_UNORM,
ImageDataFormat::B8G8R8Unorm => vk::Format::B8G8R8_UNORM,
ImageDataFormat::B8G8R8A8Unorm => vk::Format::B8G8R8A8_UNORM,
ImageDataFormat::R8G8B8A8Unorm => vk::Format::R8G8B8A8_UNORM,

ImageDataFormat::RGB16Unorm => vk::Format::R5G6B5_UNORM_PACK16,
ImageDataFormat::RGBA16Unorm => vk::Format::R5G5B5A1_UNORM_PACK16,

ImageDataFormat::D32Float => vk::Format::D32_SFLOAT,
ImageDataFormat::D32FloatS8 => vk::Format::D32_SFLOAT_S8_UINT,
ImageDataFormat::D24NormS8 => vk::Format::D24_UNORM_S8_UINT,
}
}
}

impl TryFrom<vk::Format> for ImageDataFormat {
type Error = SarektError;

fn try_from(format: vk::Format) -> SarektResult<ImageDataFormat> {
match format {
vk::Format::R8G8B8_SRGB => Ok(ImageDataFormat::R8G8B8Srgb),
vk::Format::B8G8R8_SRGB => Ok(ImageDataFormat::B8G8R8Srgb),
vk::Format::B8G8R8A8_SRGB => Ok(ImageDataFormat::B8G8R8A8Srgb),
vk::Format::R8G8B8A8_SRGB => Ok(ImageDataFormat::R8G8B8A8Srgb),

vk::Format::R8G8B8_UNORM => Ok(ImageDataFormat::R8G8B8Unorm),
vk::Format::B8G8R8_UNORM => Ok(ImageDataFormat::B8G8R8Unorm),
vk::Format::B8G8R8A8_UNORM => Ok(ImageDataFormat::B8G8R8A8Unorm),
vk::Format::R8G8B8A8_UNORM => Ok(ImageDataFormat::R8G8B8A8Unorm),

vk::Format::R5G6B5_UNORM_PACK16 => Ok(ImageDataFormat::RGB16Unorm),
vk::Format::R5G5B5A1_UNORM_PACK16 => Ok(ImageDataFormat::RGBA16Unorm),

vk::Format::D32_SFLOAT => Ok(ImageDataFormat::D32Float),
vk::Format::D32_SFLOAT_S8_UINT => Ok(ImageDataFormat::D32FloatS8),
vk::Format::D24_UNORM_S8_UINT => Ok(ImageDataFormat::D24NormS8),

_ => Err(SarektError::UnsupportedImageFormat),
}
}
}
63 changes: 14 additions & 49 deletions src/renderer/vulkan/vulkan_buffer_image_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
BackendHandleTrait, BufferAndImageLoader, BufferImageHandle, BufferType, IndexBufferElemSize,
MagnificationMinificationFilter, TextureAddressMode,
},
config::NumSamples,
vulkan::{
images::ImageAndView,
vulkan_renderer::vulkan_core::{VulkanCoreStructures, VulkanDeviceStructures},
Expand All @@ -14,12 +15,10 @@ use crate::{
};
use ash::{
version::{DeviceV1_0, InstanceV1_0},
vk,
vk::Format,
Device, Instance,
vk, Device, Instance,
};
use log::{info, warn};
use std::{convert::TryFrom, sync::Arc};
use std::sync::Arc;

/// TODO(issue#27) PERFORMANCE stage buffer allocations to be transfered in one
/// staging buffer commit load operation instead of doing each one seperate and
Expand All @@ -30,7 +29,7 @@ use std::{convert::TryFrom, sync::Arc};
/// Vulkan implementation of [BufferLoader](trait.BufferLoader.html).
#[derive(Clone)]
pub struct VulkanBufferFunctions {
pub struct VulkanBufferImageFunctions {
instance: Arc<Instance>,
logical_device: Arc<Device>,
physical_device: vk::PhysicalDevice,
Expand All @@ -45,7 +44,7 @@ pub struct VulkanBufferFunctions {

ownership_semaphore: [vk::Semaphore; 1],
}
impl VulkanBufferFunctions {
impl VulkanBufferImageFunctions {
pub fn new(
vulkan_core: &VulkanCoreStructures, device_bundle: &VulkanDeviceStructures,
allocator: Arc<vk_mem::Allocator>, graphics_queue_family: u32, transfer_queue_family: u32,
Expand Down Expand Up @@ -177,9 +176,11 @@ impl VulkanBufferFunctions {

/// Creates a buffer with TRANSFER_DST and appropriate image type flags
/// flipped.
///
/// num_samples referes to msaa samples.
fn create_gpu_image(
&self, dimens: (u32, u32), format: vk::Format, usage: vk::ImageUsageFlags,
queue_family_index: u32, mip_levels: u32,
queue_family_index: u32, mip_levels: u32, num_samples: NumSamples,
) -> SarektResult<(vk::Image, vk_mem::Allocation, vk_mem::AllocationInfo)> {
let image_ci = vk::ImageCreateInfo::builder()
.image_type(vk::ImageType::TYPE_2D)
Expand All @@ -196,7 +197,7 @@ impl VulkanBufferFunctions {
.initial_layout(vk::ImageLayout::UNDEFINED)
.queue_family_indices(&[queue_family_index])
.sharing_mode(vk::SharingMode::EXCLUSIVE) // Only used by the one queue family.
.samples(vk::SampleCountFlags::TYPE_1) // Not multisampling, this isn't for an attachment.
.samples(num_samples.into()) // Not multisampling, this isn't for an attachment.
.build();
let alloc_ci = vk_mem::AllocationCreateInfo {
usage: vk_mem::MemoryUsage::GpuOnly,
Expand Down Expand Up @@ -747,10 +748,10 @@ impl VulkanBufferFunctions {
Ok((src_queue_family, dst_queue_family))
}
}
unsafe impl BufferAndImageLoader for VulkanBufferFunctions {
unsafe impl BufferAndImageLoader for VulkanBufferImageFunctions {
type BackendHandle = ResourceWithMemory;
type UniformBufferDataHandle = Vec<BufferAndMemoryMapped>;
type UniformBufferHandle = Vec<BufferImageHandle<VulkanBufferFunctions>>;
type UniformBufferHandle = Vec<BufferImageHandle<VulkanBufferImageFunctions>>;

unsafe fn cleanup(&self) -> SarektResult<()> {
if self.ownership_semaphore[0] != vk::Semaphore::null() {
Expand Down Expand Up @@ -925,6 +926,7 @@ unsafe impl BufferAndImageLoader for VulkanBufferFunctions {
vk::ImageUsageFlags::TRANSFER_DST | vk::ImageUsageFlags::SAMPLED | transfer_src_flag,
self.transfer_queue_family,
mip_levels,
NumSamples::One,
)?;

let extent = vk::Extent3D {
Expand Down Expand Up @@ -970,7 +972,7 @@ unsafe impl BufferAndImageLoader for VulkanBufferFunctions {
}

fn create_uninitialized_image(
&self, dimensions: (u32, u32), format: ImageDataFormat,
&self, dimensions: (u32, u32), format: ImageDataFormat, num_samples: NumSamples,
) -> SarektResult<ResourceWithMemory> {
info!("Creating image with dimensions {:?}", dimensions);

Expand All @@ -980,6 +982,7 @@ unsafe impl BufferAndImageLoader for VulkanBufferFunctions {
vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT,
self.graphics_queue_family,
1,
num_samples,
)?;
let image_view =
self.create_image_view(image, format.into(), vk::ImageAspectFlags::DEPTH, 1)?;
Expand Down Expand Up @@ -1103,41 +1106,3 @@ impl ImageOrBuffer {
}
}
}

impl From<ImageDataFormat> for vk::Format {
fn from(image_data_format: ImageDataFormat) -> vk::Format {
match image_data_format {
ImageDataFormat::R8G8B8 => vk::Format::R8G8B8_SRGB,
ImageDataFormat::B8G8R8 => vk::Format::B8G8R8_SRGB,
ImageDataFormat::B8G8R8A8 => vk::Format::B8G8R8A8_SRGB,
ImageDataFormat::R8G8B8A8 => vk::Format::R8G8B8A8_SRGB,
ImageDataFormat::RGB16 => vk::Format::R5G6B5_UNORM_PACK16,
ImageDataFormat::RGBA16 => vk::Format::R5G5B5A1_UNORM_PACK16,

ImageDataFormat::D32Float => vk::Format::D32_SFLOAT,
ImageDataFormat::D32FloatS8 => vk::Format::D32_SFLOAT_S8_UINT,
ImageDataFormat::D24NormS8 => vk::Format::D24_UNORM_S8_UINT,
}
}
}

impl TryFrom<vk::Format> for ImageDataFormat {
type Error = SarektError;

fn try_from(format: Format) -> SarektResult<ImageDataFormat> {
match format {
vk::Format::R8G8B8_SRGB => Ok(ImageDataFormat::R8G8B8),
vk::Format::B8G8R8_SRGB => Ok(ImageDataFormat::B8G8R8),
vk::Format::B8G8R8A8_SRGB => Ok(ImageDataFormat::B8G8R8A8),
vk::Format::R8G8B8A8_SRGB => Ok(ImageDataFormat::R8G8B8A8),
vk::Format::R5G6B5_UNORM_PACK16 => Ok(ImageDataFormat::RGB16),
vk::Format::R5G5B5A1_UNORM_PACK16 => Ok(ImageDataFormat::RGBA16),

vk::Format::D32_SFLOAT => Ok(ImageDataFormat::D32Float),
vk::Format::D32_SFLOAT_S8_UINT => Ok(ImageDataFormat::D32FloatS8),
vk::Format::D24_UNORM_S8_UINT => Ok(ImageDataFormat::D24NormS8),

_ => Err(SarektError::UnsupportedImageFormat),
}
}
}
Loading

0 comments on commit 1733b82

Please sign in to comment.