Skip to content

Commit

Permalink
Make WindowTextureNode extend TextureNode
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsr committed Apr 24, 2021
1 parent 1ad2adb commit e569993
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 19 deletions.
85 changes: 76 additions & 9 deletions crates/bevy_render/src/render_graph/nodes/texture_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ use bevy_ecs::world::World;
use std::borrow::Cow;

use crate::{
prelude::Texture,
render_graph::{Node, ResourceSlotInfo, ResourceSlots},
renderer::{RenderContext, RenderResourceId, RenderResourceType},
texture::{SamplerDescriptor, TextureDescriptor, SAMPLER_ASSET_INDEX, TEXTURE_ASSET_INDEX},
};
pub struct TextureNode {
pub texture_descriptor: TextureDescriptor,
pub sampler_descriptor: Option<SamplerDescriptor>,
pub handle: Option<HandleUntyped>,
texture_descriptor: TextureDescriptor,
sampler_descriptor: Option<SamplerDescriptor>,
handle: Option<HandleUntyped>,
has_changed: bool,
}

impl TextureNode {
pub const TEXTURE: &'static str = "texture";
pub const OUT_TEXTURE: &'static str = "texture";
pub const OUT_SAMPLER: &'static str = "sampler";

pub fn new(
texture_descriptor: TextureDescriptor,
Expand All @@ -25,17 +28,57 @@ impl TextureNode {
texture_descriptor,
sampler_descriptor,
handle,
has_changed: true,
}
}
}

impl TextureNode {
pub fn texture_descriptor(&self) -> &TextureDescriptor {
&self.texture_descriptor
}

pub fn texture_descriptor_mut(&mut self) -> &mut TextureDescriptor {
self.set_changed();
&mut self.texture_descriptor
}

pub fn sampler_descriptor(&self) -> &Option<SamplerDescriptor> {
&self.sampler_descriptor
}

pub fn sampler_descriptor_mut(&mut self) -> &mut Option<SamplerDescriptor> {
self.set_changed();
&mut self.sampler_descriptor
}

pub fn set_changed(&mut self) {
self.has_changed = true;
}
}

impl Node for TextureNode {
fn output(&self) -> &[ResourceSlotInfo] {
static OUTPUT: &[ResourceSlotInfo] = &[ResourceSlotInfo {
name: Cow::Borrowed(TextureNode::TEXTURE),
static WITHOUT_SAMPLER: &[ResourceSlotInfo] = &[ResourceSlotInfo {
name: Cow::Borrowed(TextureNode::OUT_TEXTURE),
resource_type: RenderResourceType::Texture,
}];
OUTPUT
static WITH_SAMPLER: &[ResourceSlotInfo] = &[
ResourceSlotInfo {
name: Cow::Borrowed(TextureNode::OUT_TEXTURE),
resource_type: RenderResourceType::Texture,
},
ResourceSlotInfo {
name: Cow::Borrowed(TextureNode::OUT_SAMPLER),
resource_type: RenderResourceType::Sampler,
},
];

if self.sampler_descriptor.is_none() {
WITHOUT_SAMPLER
} else {
WITH_SAMPLER
}
}

fn update(
Expand All @@ -45,25 +88,49 @@ impl Node for TextureNode {
_input: &ResourceSlots,
output: &mut ResourceSlots,
) {
if output.get(0).is_none() {
// Need to update
if self.has_changed {
let render_resource_context = render_context.resources_mut();

// First create new texture
let texture_id = render_resource_context.create_texture(self.texture_descriptor);

// And update handle and output
if let Some(handle) = &self.handle {
// For the texture itself
render_resource_context.set_asset_resource_untyped(
handle.clone(),
RenderResourceId::Texture(texture_id),
TEXTURE_ASSET_INDEX,
);

// And remove the old resource
if let Some(old_texture) =
output.get(0).replace(RenderResourceId::Texture(texture_id))
{
render_resource_context.remove_texture(old_texture.get_texture().unwrap());
}

// And if needed for the sampler
if let Some(sampler_descriptor) = self.sampler_descriptor {
let sampler_id = render_resource_context.create_sampler(&sampler_descriptor);
render_resource_context.set_asset_resource_untyped(
handle.clone(),
RenderResourceId::Sampler(sampler_id),
SAMPLER_ASSET_INDEX,
);

// And remove the old resource
if let Some(old_sampler) =
output.get(1).replace(RenderResourceId::Sampler(sampler_id))
{
render_resource_context.remove_sampler(old_sampler.get_sampler().unwrap());
}
}
}
output.set(0, RenderResourceId::Texture(texture_id));

// Remove changed flag
self.has_changed = false;
}
}
}
28 changes: 18 additions & 10 deletions crates/bevy_render/src/render_graph/nodes/window_texture_node.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
use crate::{
render_graph::{Node, ResourceSlotInfo, ResourceSlots},
renderer::{RenderContext, RenderResourceId, RenderResourceType},
texture::TextureDescriptor,
texture::{SamplerDescriptor, TextureDescriptor},
};
use bevy_app::{Events, ManualEventReader};
use bevy_asset::HandleUntyped;
use bevy_ecs::world::World;
use bevy_window::{WindowCreated, WindowId, WindowResized, Windows};
use std::borrow::Cow;

use super::TextureNode;

pub struct WindowTextureNode {
inner: TextureNode,
window_id: WindowId,
descriptor: TextureDescriptor,
window_created_event_reader: ManualEventReader<WindowCreated>,
window_resized_event_reader: ManualEventReader<WindowResized>,
}

impl WindowTextureNode {
pub const OUT_TEXTURE: &'static str = "texture";
pub const OUT_TEXTURE: &'static str = TextureNode::OUT_TEXTURE;

pub fn new(window_id: WindowId, descriptor: TextureDescriptor) -> Self {
pub fn new(
window_id: WindowId,
texture_descriptor: TextureDescriptor,
sampler_descriptor: Option<SamplerDescriptor>,
handle: Option<HandleUntyped>,
) -> Self {
WindowTextureNode {
inner: TextureNode::new(texture_descriptor, sampler_descriptor, handle),
window_id,
descriptor,
window_created_event_reader: Default::default(),
window_resized_event_reader: Default::default(),
}
Expand All @@ -41,7 +49,7 @@ impl Node for WindowTextureNode {
&mut self,
world: &World,
render_context: &mut dyn RenderContext,
_input: &ResourceSlots,
input: &ResourceSlots,
output: &mut ResourceSlots,
) {
const WINDOW_TEXTURE: usize = 0;
Expand All @@ -67,10 +75,10 @@ impl Node for WindowTextureNode {
render_resource_context.remove_texture(old_texture);
}

self.descriptor.size.width = window.physical_width();
self.descriptor.size.height = window.physical_height();
let texture_resource = render_resource_context.create_texture(self.descriptor);
output.set(WINDOW_TEXTURE, RenderResourceId::Texture(texture_resource));
self.inner.texture_descriptor_mut().size.width = window.physical_width();
self.inner.texture_descriptor_mut().size.height = window.physical_height();

self.inner.update(world, render_context, input, output);
}
}
}

0 comments on commit e569993

Please sign in to comment.