Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove async logic from texture2D since it is handled by pipeline #35

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 1 addition & 48 deletions runtime/functor-runtime-common/src/texture/texture_2d.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::{
cell::RefCell,
future::Future,
pin::Pin,
task::{Context, Poll},
};
use std::cell::RefCell;

use glow::HasContext;

Expand All @@ -27,15 +22,6 @@ impl Texture2D {
state: RefCell::new(Some(TextureState::Unloaded(data, opts))),
}
}

pub fn init_from_future<F>(future: F, opts: TextureOptions) -> Texture2D
where
F: Future<Output = Result<TextureData, String>> + 'static,
{
Texture2D {
state: RefCell::new(Some(TextureState::Loading(Box::pin(future), opts))),
}
}
}

impl RuntimeTexture for Texture2D {
Expand All @@ -50,11 +36,6 @@ impl RuntimeTexture for Texture2D {
gl.active_texture(glow::TEXTURE0 + index);
gl.bind_texture(glow::TEXTURE_2D, Some(tex));
},
TextureState::Loading(_, _) => {
// TODO: Have a default texture to use
// While loading, we can't actually do anything
println!("Still waiting for texture loading...")
}
TextureState::Unloaded(_, _) => {
panic!("Unable to load texture; should never happen")
}
Expand All @@ -67,18 +48,13 @@ impl RuntimeTexture for Texture2D {

pub enum TextureState {
Unloaded(TextureData, TextureOptions),
Loading(
Pin<Box<dyn Future<Output = Result<TextureData, String>>>>,
TextureOptions,
),
Loaded(glow::Texture),
}

impl TextureState {
pub fn ensure_loaded(self, render_context: &RenderContext) -> Self {
match self {
TextureState::Loaded(_) => self,
TextureState::Loading(..) => self.poll_load(render_context),
TextureState::Unloaded(texture_data, texture_opts) => unsafe {
let gl = render_context.gl;
let texture = gl.create_texture().expect("Texture to be created");
Expand Down Expand Up @@ -124,27 +100,4 @@ impl TextureState {
},
}
}

fn poll_load(self, render_context: &RenderContext) -> Self {
if let Self::Loading(mut future, opts) = self {
let waker = futures::task::noop_waker();
let mut cx = Context::from_waker(&waker);

match Future::poll(Pin::new(&mut future), &mut cx) {
Poll::Ready(Ok(texture_data)) => {
TextureState::Unloaded(texture_data, opts).ensure_loaded(render_context)
}
Poll::Ready(Err(e)) => {
// TODO: More robust error handling...
panic!("Failed to load texture: {}", e);
}
Poll::Pending => {
println!("Waiting for texture to load...");
TextureState::Loading(future, opts)
}
}
} else {
self
}
}
}
Loading