add TextureDescriptor to ResourceInfo

This commit is contained in:
Carter Anderson 2020-05-11 21:28:11 -07:00
parent 4eb562975f
commit 10637c1010
19 changed files with 54 additions and 58 deletions

View File

@ -15,7 +15,7 @@ use bevy_render::{
}, },
render_resource::{ render_resource::{
BufferInfo, BufferUsage, RenderResource, RenderResourceAssignment, BufferInfo, BufferUsage, RenderResource, RenderResourceAssignment,
RenderResourceAssignments, RenderResourceAssignments, ResourceInfo,
}, },
renderer::RenderContext, renderer::RenderContext,
shader::{Shader, ShaderSource, ShaderStage, ShaderStages}, shader::{Shader, ShaderSource, ShaderStage, ShaderStages},
@ -310,23 +310,18 @@ impl<'a> BevyPathfinderDevice<'a> {
) )
} }
fn get_texture_format(&self, _render_resource: RenderResource) -> Option<TextureFormat> { fn get_texture_format(&self, render_resource: RenderResource) -> Option<TextureFormat> {
// TODO: lookup real texture format // TODO: add swap chain resource info so this isnt necessary
// let mut texture_format = None; let mut texture_format = Some(TextureFormat::Bgra8UnormSrgb);
// self.render_context.borrow().resources().get_resource_info( self.render_context.borrow().resources().get_resource_info(
// texture_resource, render_resource,
// &mut |info| { &mut |info| {
// if let Some(info) = info { if let Some(ResourceInfo::Texture(descriptor)) = info {
// match info { texture_format = Some(descriptor.format)
// ResourceInfo::Texture { }
},
// } );
// } texture_format
// texture_format = Some(info)
// }
// },
// );
Some(TextureFormat::Rgba16Float)
} }
pub fn setup_pipline_descriptor( pub fn setup_pipline_descriptor(
@ -606,7 +601,7 @@ impl<'a> Device for BevyPathfinderDevice<'a> {
.render_context .render_context
.borrow() .borrow()
.resources() .resources()
.create_texture(&descriptor), .create_texture(descriptor),
texture_descriptor: descriptor, texture_descriptor: descriptor,
sampler_resource: RefCell::new(None), sampler_resource: RefCell::new(None),
} }

View File

@ -4,7 +4,7 @@ use bevy_render::{
render_graph::{Node, ResourceSlotInfo, ResourceSlots}, render_graph::{Node, ResourceSlotInfo, ResourceSlots},
render_resource::ResourceInfo, render_resource::ResourceInfo,
renderer::RenderContext, renderer::RenderContext,
shader::Shader, shader::{FieldBindType, Shader},
}; };
use legion::prelude::{Resources, World}; use legion::prelude::{Resources, World};
use pathfinder_canvas::{vec2f, Canvas, CanvasFontContext, ColorF, Path2D, RectF, Vector2I}; use pathfinder_canvas::{vec2f, Canvas, CanvasFontContext, ColorF, Path2D, RectF, Vector2I};
@ -32,11 +32,11 @@ impl Node for PathfinderNode {
static INPUT: &[ResourceSlotInfo] = &[ static INPUT: &[ResourceSlotInfo] = &[
ResourceSlotInfo { ResourceSlotInfo {
name: Cow::Borrowed(PathfinderNode::IN_COLOR_TEXTURE), name: Cow::Borrowed(PathfinderNode::IN_COLOR_TEXTURE),
resource_type: ResourceInfo::Texture, resource_type: FieldBindType::Texture,
}, },
ResourceSlotInfo { ResourceSlotInfo {
name: Cow::Borrowed(PathfinderNode::IN_DEPTH_STENCIL_TEXTURE), name: Cow::Borrowed(PathfinderNode::IN_DEPTH_STENCIL_TEXTURE),
resource_type: ResourceInfo::Texture, resource_type: FieldBindType::Texture,
}, },
]; ];
INPUT INPUT

View File

@ -315,7 +315,7 @@ mod tests {
use crate::{ use crate::{
render_graph::{Edge, Node, NodeId, RenderGraphError, ResourceSlotInfo, ResourceSlots}, render_graph::{Edge, Node, NodeId, RenderGraphError, ResourceSlotInfo, ResourceSlots},
render_resource::ResourceInfo, render_resource::ResourceInfo,
renderer::RenderContext, renderer::RenderContext, shader::FieldBindType,
}; };
use legion::prelude::{Resources, World}; use legion::prelude::{Resources, World};
use std::{collections::HashSet, iter::FromIterator}; use std::{collections::HashSet, iter::FromIterator};
@ -332,13 +332,13 @@ mod tests {
inputs: (0..inputs) inputs: (0..inputs)
.map(|i| ResourceSlotInfo { .map(|i| ResourceSlotInfo {
name: format!("in_{}", i).into(), name: format!("in_{}", i).into(),
resource_type: ResourceInfo::Texture, resource_type: FieldBindType::Texture,
}) })
.collect(), .collect(),
outputs: (0..outputs) outputs: (0..outputs)
.map(|i| ResourceSlotInfo { .map(|i| ResourceSlotInfo {
name: format!("out_{}", i).into(), name: format!("out_{}", i).into(),
resource_type: ResourceInfo::Texture, resource_type: FieldBindType::Texture,
}) })
.collect(), .collect(),
} }

View File

@ -1,5 +1,5 @@
use super::RenderGraphError; use super::RenderGraphError;
use crate::render_resource::{RenderResource, ResourceInfo}; use crate::{shader::FieldBindType, render_resource::{RenderResource, ResourceInfo}};
use std::borrow::Cow; use std::borrow::Cow;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -122,11 +122,11 @@ impl From<&[ResourceSlotInfo]> for ResourceSlots {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ResourceSlotInfo { pub struct ResourceSlotInfo {
pub name: Cow<'static, str>, pub name: Cow<'static, str>,
pub resource_type: ResourceInfo, pub resource_type: FieldBindType,
} }
impl ResourceSlotInfo { impl ResourceSlotInfo {
pub fn new(name: impl Into<Cow<'static, str>>, resource_type: ResourceInfo) -> Self { pub fn new(name: impl Into<Cow<'static, str>>, resource_type: FieldBindType) -> Self {
ResourceSlotInfo { ResourceSlotInfo {
name: name.into(), name: name.into(),
resource_type, resource_type,

View File

@ -5,7 +5,7 @@ use crate::{
render_graph::{Node, ResourceSlotInfo, ResourceSlots}, render_graph::{Node, ResourceSlotInfo, ResourceSlots},
render_resource::{RenderResourceAssignments, ResourceInfo}, render_resource::{RenderResourceAssignments, ResourceInfo},
renderer::RenderContext, renderer::RenderContext,
shader::Shader, shader::{FieldBindType, Shader},
}; };
use bevy_asset::{AssetStorage, Handle}; use bevy_asset::{AssetStorage, Handle};
use legion::prelude::*; use legion::prelude::*;
@ -26,7 +26,7 @@ impl PassNode {
if let TextureAttachment::Input(ref name) = color_attachment.attachment { if let TextureAttachment::Input(ref name) = color_attachment.attachment {
inputs.push(ResourceSlotInfo::new( inputs.push(ResourceSlotInfo::new(
name.to_string(), name.to_string(),
ResourceInfo::Texture, FieldBindType::Texture,
)); ));
color_attachment_input_indices.push(Some(inputs.len() - 1)); color_attachment_input_indices.push(Some(inputs.len() - 1));
} else { } else {
@ -39,7 +39,7 @@ impl PassNode {
if let TextureAttachment::Input(ref name) = depth_stencil_attachment.attachment { if let TextureAttachment::Input(ref name) = depth_stencil_attachment.attachment {
inputs.push(ResourceSlotInfo::new( inputs.push(ResourceSlotInfo::new(
name.to_string(), name.to_string(),
ResourceInfo::Texture, FieldBindType::Texture,
)); ));
depth_stencil_attachment_input_index = Some(inputs.len() - 1); depth_stencil_attachment_input_index = Some(inputs.len() - 1);
} }

View File

@ -716,7 +716,7 @@ fn setup_uniform_texture_resources<T>(
let texture_descriptor: TextureDescriptor = texture.into(); let texture_descriptor: TextureDescriptor = texture.into();
let texture_resource = let texture_resource =
render_resource_context.create_texture(&texture_descriptor); render_resource_context.create_texture(texture_descriptor);
// TODO: queue texture copy // TODO: queue texture copy
// .create_texture_with_data(&texture_descriptor, &texture.data); // .create_texture_with_data(&texture_descriptor, &texture.data);
let texture_buffer = render_resource_context.create_buffer_with_data( let texture_buffer = render_resource_context.create_buffer_with_data(

View File

@ -1,7 +1,7 @@
use crate::{ use crate::{
render_graph::{Node, ResourceSlotInfo, ResourceSlots}, render_graph::{Node, ResourceSlotInfo, ResourceSlots},
render_resource::ResourceInfo, render_resource::ResourceInfo,
renderer::RenderContext, renderer::RenderContext, shader::FieldBindType,
}; };
use bevy_app::{EventReader, Events}; use bevy_app::{EventReader, Events};
use bevy_window::{WindowCreated, WindowReference, WindowResized, Windows}; use bevy_window::{WindowCreated, WindowReference, WindowResized, Windows};
@ -33,7 +33,7 @@ impl Node for WindowSwapChainNode {
fn output(&self) -> &[ResourceSlotInfo] { fn output(&self) -> &[ResourceSlotInfo] {
static OUTPUT: &[ResourceSlotInfo] = &[ResourceSlotInfo { static OUTPUT: &[ResourceSlotInfo] = &[ResourceSlotInfo {
name: Cow::Borrowed(WindowSwapChainNode::OUT_TEXTURE), name: Cow::Borrowed(WindowSwapChainNode::OUT_TEXTURE),
resource_type: ResourceInfo::Texture, resource_type: FieldBindType::Texture,
}]; }];
OUTPUT OUTPUT
} }

View File

@ -2,7 +2,7 @@ use crate::{
render_graph::{Node, ResourceSlotInfo, ResourceSlots}, render_graph::{Node, ResourceSlotInfo, ResourceSlots},
render_resource::ResourceInfo, render_resource::ResourceInfo,
renderer::RenderContext, renderer::RenderContext,
texture::TextureDescriptor, texture::TextureDescriptor, shader::FieldBindType,
}; };
use bevy_app::{EventReader, Events}; use bevy_app::{EventReader, Events};
use bevy_window::{WindowCreated, WindowReference, WindowResized, Windows}; use bevy_window::{WindowCreated, WindowReference, WindowResized, Windows};
@ -37,7 +37,7 @@ impl Node for WindowTextureNode {
fn output(&self) -> &[ResourceSlotInfo] { fn output(&self) -> &[ResourceSlotInfo] {
static OUTPUT: &[ResourceSlotInfo] = &[ResourceSlotInfo { static OUTPUT: &[ResourceSlotInfo] = &[ResourceSlotInfo {
name: Cow::Borrowed(WindowTextureNode::OUT_TEXTURE), name: Cow::Borrowed(WindowTextureNode::OUT_TEXTURE),
resource_type: ResourceInfo::Texture, resource_type: FieldBindType::Texture,
}]; }];
OUTPUT OUTPUT
} }
@ -78,7 +78,7 @@ impl Node for WindowTextureNode {
self.descriptor.size.width = window.width; self.descriptor.size.width = window.width;
self.descriptor.size.height = window.height; self.descriptor.size.height = window.height;
let texture_resource = render_resources.create_texture(&self.descriptor); let texture_resource = render_resources.create_texture(self.descriptor);
output.set(WINDOW_TEXTURE, texture_resource); output.set(WINDOW_TEXTURE, texture_resource);
} }
} }

View File

@ -265,7 +265,7 @@ mod tests {
use crate::{ use crate::{
render_graph::{Node, NodeId, RenderGraph, ResourceSlotInfo, ResourceSlots}, render_graph::{Node, NodeId, RenderGraph, ResourceSlotInfo, ResourceSlots},
render_resource::ResourceInfo, render_resource::ResourceInfo,
renderer::RenderContext, renderer::RenderContext, shader::FieldBindType,
}; };
use legion::prelude::{Resources, World}; use legion::prelude::{Resources, World};
@ -280,13 +280,13 @@ mod tests {
inputs: (0..inputs) inputs: (0..inputs)
.map(|i| ResourceSlotInfo { .map(|i| ResourceSlotInfo {
name: format!("in_{}", i).into(), name: format!("in_{}", i).into(),
resource_type: ResourceInfo::Texture, resource_type: FieldBindType::Texture,
}) })
.collect(), .collect(),
outputs: (0..outputs) outputs: (0..outputs)
.map(|i| ResourceSlotInfo { .map(|i| ResourceSlotInfo {
name: format!("out_{}", i).into(), name: format!("out_{}", i).into(),
resource_type: ResourceInfo::Texture, resource_type: FieldBindType::Texture,
}) })
.collect(), .collect(),
} }

View File

@ -1,4 +1,4 @@
use crate::render_resource::BufferUsage; use crate::{texture::TextureDescriptor, render_resource::BufferUsage};
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct BufferInfo { pub struct BufferInfo {
@ -18,6 +18,6 @@ impl Default for BufferInfo {
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub enum ResourceInfo { pub enum ResourceInfo {
Buffer(BufferInfo), Buffer(BufferInfo),
Texture, Texture(TextureDescriptor),
Sampler, Sampler,
} }

View File

@ -41,9 +41,9 @@ impl RenderResourceContext for HeadlessRenderResourceContext {
self.add_resource_info(resource, ResourceInfo::Sampler); self.add_resource_info(resource, ResourceInfo::Sampler);
resource resource
} }
fn create_texture(&self, _texture_descriptor: &TextureDescriptor) -> RenderResource { fn create_texture(&self, texture_descriptor: TextureDescriptor) -> RenderResource {
let resource = RenderResource::new(); let resource = RenderResource::new();
self.add_resource_info(resource, ResourceInfo::Texture); self.add_resource_info(resource, ResourceInfo::Texture(texture_descriptor));
resource resource
} }
fn create_buffer(&self, buffer_info: BufferInfo) -> RenderResource { fn create_buffer(&self, buffer_info: BufferInfo) -> RenderResource {

View File

@ -11,7 +11,7 @@ pub trait RenderContext {
fn create_texture_with_data( fn create_texture_with_data(
&mut self, &mut self,
texture_descriptor: &TextureDescriptor, texture_descriptor: TextureDescriptor,
bytes: &[u8], bytes: &[u8],
) -> RenderResource; ) -> RenderResource;
fn copy_buffer_to_buffer( fn copy_buffer_to_buffer(

View File

@ -31,7 +31,7 @@ pub trait RenderResourceContext: Downcast + Send + Sync + 'static {
fn drop_swap_chain_texture(&self, render_resource: RenderResource); fn drop_swap_chain_texture(&self, render_resource: RenderResource);
fn drop_all_swap_chain_textures(&self); fn drop_all_swap_chain_textures(&self);
fn create_sampler(&self, sampler_descriptor: &SamplerDescriptor) -> RenderResource; fn create_sampler(&self, sampler_descriptor: &SamplerDescriptor) -> RenderResource;
fn create_texture(&self, texture_descriptor: &TextureDescriptor) -> RenderResource; fn create_texture(&self, texture_descriptor: TextureDescriptor) -> RenderResource;
fn create_buffer(&self, buffer_info: BufferInfo) -> RenderResource; fn create_buffer(&self, buffer_info: BufferInfo) -> RenderResource;
// TODO: remove RenderResourceContext here // TODO: remove RenderResourceContext here
fn create_buffer_mapped( fn create_buffer_mapped(

View File

@ -68,6 +68,7 @@ impl ShaderDefSuffixProvider for bool {
} }
} }
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FieldBindType { pub enum FieldBindType {
Uniform { size: usize }, Uniform { size: usize },
Texture, Texture,

View File

@ -1,6 +1,6 @@
use super::{Extent3d, Texture, TextureDimension, TextureFormat, TextureUsage}; use super::{Extent3d, Texture, TextureDimension, TextureFormat, TextureUsage};
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct TextureDescriptor { pub struct TextureDescriptor {
pub size: Extent3d, pub size: Extent3d,
pub mip_level_count: u32, pub mip_level_count: u32,

View File

@ -10,7 +10,7 @@ pub enum TextureViewDimension {
D3, D3,
} }
#[derive(Copy, Clone, Debug, Hash)] #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum TextureDimension { pub enum TextureDimension {
D1, D1,
D2, D2,
@ -18,7 +18,7 @@ pub enum TextureDimension {
} }
// TODO: use math type here // TODO: use math type here
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Extent3d { pub struct Extent3d {
pub width: u32, pub width: u32,
pub height: u32, pub height: u32,

View File

@ -73,7 +73,7 @@ impl WgpuRenderContext {
impl RenderContext for WgpuRenderContext { impl RenderContext for WgpuRenderContext {
fn create_texture_with_data( fn create_texture_with_data(
&mut self, &mut self,
texture_descriptor: &TextureDescriptor, texture_descriptor: TextureDescriptor,
bytes: &[u8], bytes: &[u8],
) -> RenderResource { ) -> RenderResource {
self.render_resources.create_texture_with_data( self.render_resources.create_texture_with_data(

View File

@ -39,14 +39,14 @@ impl WgpuRenderResourceContext {
pub fn create_texture_with_data( pub fn create_texture_with_data(
&mut self, &mut self,
command_encoder: &mut wgpu::CommandEncoder, command_encoder: &mut wgpu::CommandEncoder,
texture_descriptor: &TextureDescriptor, texture_descriptor: TextureDescriptor,
bytes: &[u8], bytes: &[u8],
) -> RenderResource { ) -> RenderResource {
let mut resource_info = self.resources.resource_info.write().unwrap(); let mut resource_info = self.resources.resource_info.write().unwrap();
let mut texture_views = self.resources.texture_views.write().unwrap(); let mut texture_views = self.resources.texture_views.write().unwrap();
let mut textures = self.resources.textures.write().unwrap(); let mut textures = self.resources.textures.write().unwrap();
let descriptor: wgpu::TextureDescriptor = (*texture_descriptor).wgpu_into(); let descriptor: wgpu::TextureDescriptor = (&texture_descriptor).wgpu_into();
let texture = self.device.create_texture(&descriptor); let texture = self.device.create_texture(&descriptor);
let texture_view = texture.create_default_view(); let texture_view = texture.create_default_view();
let temp_buf = self let temp_buf = self
@ -69,7 +69,7 @@ impl WgpuRenderResourceContext {
); );
let resource = RenderResource::new(); let resource = RenderResource::new();
resource_info.insert(resource, ResourceInfo::Texture); resource_info.insert(resource, ResourceInfo::Texture(texture_descriptor));
texture_views.insert(resource, texture_view); texture_views.insert(resource, texture_view);
textures.insert(resource, texture); textures.insert(resource, texture);
@ -182,17 +182,17 @@ impl RenderResourceContext for WgpuRenderResourceContext {
resource resource
} }
fn create_texture(&self, texture_descriptor: &TextureDescriptor) -> RenderResource { fn create_texture(&self, texture_descriptor: TextureDescriptor) -> RenderResource {
let mut textures = self.resources.textures.write().unwrap(); let mut textures = self.resources.textures.write().unwrap();
let mut texture_views = self.resources.texture_views.write().unwrap(); let mut texture_views = self.resources.texture_views.write().unwrap();
let mut resource_info = self.resources.resource_info.write().unwrap(); let mut resource_info = self.resources.resource_info.write().unwrap();
let descriptor: wgpu::TextureDescriptor = (*texture_descriptor).wgpu_into(); let descriptor: wgpu::TextureDescriptor = (&texture_descriptor).wgpu_into();
let texture = self.device.create_texture(&descriptor); let texture = self.device.create_texture(&descriptor);
let texture_view = texture.create_default_view(); let texture_view = texture.create_default_view();
let resource = RenderResource::new(); let resource = RenderResource::new();
resource_info.insert(resource, ResourceInfo::Texture); resource_info.insert(resource, ResourceInfo::Texture(texture_descriptor));
texture_views.insert(resource, texture_view); texture_views.insert(resource, texture_view);
textures.insert(resource, texture); textures.insert(resource, texture);
resource resource

View File

@ -216,8 +216,8 @@ impl WgpuFrom<Extent3d> for wgpu::Extent3d {
} }
} }
impl WgpuFrom<TextureDescriptor> for wgpu::TextureDescriptor<'_> { impl WgpuFrom<&TextureDescriptor> for wgpu::TextureDescriptor<'_> {
fn from(texture_descriptor: TextureDescriptor) -> Self { fn from(texture_descriptor: &TextureDescriptor) -> Self {
wgpu::TextureDescriptor { wgpu::TextureDescriptor {
label: None, label: None,
size: texture_descriptor.size.wgpu_into(), size: texture_descriptor.size.wgpu_into(),