[oden] Writable Textures and re-work TS API

Now we return Texture objects to make things a little bit more
type-safe, at the cost of a small allocation (I hope!)
This commit is contained in:
John Doty 2023-07-08 17:54:48 -07:00
parent 12cc715873
commit 89045ccbcc
7 changed files with 287 additions and 24 deletions

View file

@ -40,7 +40,15 @@ pub enum GraphicsCommand {
Print(PrintCommand),
Sprite(SpriteCommand),
CreateTexture(CreateTextureCommand),
CreateWritableTexture {
id: u32,
width: u32,
height: u32,
label: Option<String>,
},
UseTexture(u32),
WriteToTexture(u32),
WriteToScreen,
EndFrame,
}
@ -109,6 +117,31 @@ impl GraphicsImpl {
fn use_texture(&self, id: u32) {
let _ = self.sender.send(GraphicsCommand::UseTexture(id));
}
fn create_writable_texture(
&self,
width: u32,
height: u32,
label: Option<String>,
) -> Result<u32> {
let id = self.next_texture_id.fetch_add(1, Ordering::SeqCst);
let _ = self.sender.send(GraphicsCommand::CreateWritableTexture {
id,
width,
height,
label,
});
Ok(id)
}
fn write_to_screen(&self) {
let _ = self.sender.send(GraphicsCommand::WriteToScreen);
}
fn write_to_texture(&self, id: u32) {
let _ = self.sender.send(GraphicsCommand::WriteToTexture(id));
}
}
pub struct GraphicsAPI {
@ -168,6 +201,32 @@ impl GraphicsAPI {
)?,
)?;
}
{
let gfx = gfx.clone();
builder.export(
"create_writable_texture",
ctx.new_fn(
move |_: &ContextRef, width: u32, height: u32, label: Option<String>| {
gfx.create_writable_texture(width, height, label)
},
)?,
)?;
}
{
let gfx = gfx.clone();
builder.export(
"write_to_screen",
ctx.new_fn(move |_: &ContextRef| gfx.write_to_screen())?,
)?;
}
{
let gfx = gfx.clone();
builder.export(
"write_to_texture",
ctx.new_fn(move |_: &ContextRef, id: u32| gfx.write_to_texture(id))?,
)?;
}
builder.build("graphics-core")?;
Ok(GraphicsAPI { gfx })
}