[oden] Oh boy here we go
This commit is contained in:
parent
8218b88820
commit
14f9eb655f
4 changed files with 206 additions and 31 deletions
94
src/lib.rs
94
src/lib.rs
|
|
@ -8,8 +8,7 @@ use winit::{
|
||||||
};
|
};
|
||||||
|
|
||||||
mod script;
|
mod script;
|
||||||
// use script::ScriptContext;
|
use script::graphics::{ClearCommand, GraphicsCommand, PrintCommand};
|
||||||
|
|
||||||
mod texture;
|
mod texture;
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
|
|
@ -298,7 +297,7 @@ impl State {
|
||||||
|
|
||||||
fn update(&mut self) {}
|
fn update(&mut self) {}
|
||||||
|
|
||||||
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
|
fn render(&mut self, commands: Vec<GraphicsCommand>) -> Result<(), wgpu::SurfaceError> {
|
||||||
let output = self.surface.get_current_texture()?;
|
let output = self.surface.get_current_texture()?;
|
||||||
let view = output
|
let view = output
|
||||||
.texture
|
.texture
|
||||||
|
|
@ -309,37 +308,95 @@ impl State {
|
||||||
label: Some("Render Encoder"),
|
label: Some("Render Encoder"),
|
||||||
});
|
});
|
||||||
|
|
||||||
{
|
// Group the commands into passes.
|
||||||
// BEGIN GARBAGE
|
struct Pass {
|
||||||
let r: f64 = (self.mouse_x / f64::from(self.size.width)).clamp(0.0, 1.0) * 0.1;
|
color: Option<[f64; 4]>,
|
||||||
let g: f64 = (self.mouse_y / f64::from(self.size.height)).clamp(0.0, 1.0) * 0.2;
|
commands: Vec<GraphicsCommand>,
|
||||||
// END GARBAGE
|
}
|
||||||
|
let mut passes = Vec::new();
|
||||||
|
for command in commands {
|
||||||
|
match command {
|
||||||
|
GraphicsCommand::Clear(ClearCommand { color }) => passes.push(Pass {
|
||||||
|
color: Some(color),
|
||||||
|
commands: Vec::new(),
|
||||||
|
}),
|
||||||
|
GraphicsCommand::EndFrame => (),
|
||||||
|
other => match passes.last_mut() {
|
||||||
|
Some(pass) => pass.commands.push(other),
|
||||||
|
None => passes.push(Pass {
|
||||||
|
color: None,
|
||||||
|
commands: vec![other],
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for pass in passes {
|
||||||
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||||
label: Some("Render Pass"),
|
label: Some("Render Pass"),
|
||||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||||
view: &view,
|
view: &view,
|
||||||
resolve_target: None,
|
resolve_target: None,
|
||||||
ops: wgpu::Operations {
|
ops: wgpu::Operations {
|
||||||
load: wgpu::LoadOp::Clear(wgpu::Color {
|
load: if let Some([r, g, b, a]) = pass.color {
|
||||||
|
wgpu::LoadOp::Clear(wgpu::Color {
|
||||||
r, //0.1,
|
r, //0.1,
|
||||||
g, //0.2,
|
g, //0.2,
|
||||||
b: 0.3,
|
b,
|
||||||
a: 1.0,
|
a,
|
||||||
}),
|
})
|
||||||
|
} else {
|
||||||
|
wgpu::LoadOp::Load
|
||||||
|
},
|
||||||
store: true,
|
store: true,
|
||||||
},
|
},
|
||||||
})],
|
})],
|
||||||
depth_stencil_attachment: None,
|
depth_stencil_attachment: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
render_pass.set_pipeline(&self.render_pipeline);
|
for command in pass.commands {
|
||||||
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
|
match command {
|
||||||
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
GraphicsCommand::Print(PrintCommand { text }) => {
|
||||||
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
println!("{}", text);
|
||||||
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GraphicsCommand::Clear(_) => (), // Already handled
|
||||||
|
GraphicsCommand::EndFrame => (), // Should never appear
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// {
|
||||||
|
// // BEGIN GARBAGE
|
||||||
|
// let r: f64 = (self.mouse_x / f64::from(self.size.width)).clamp(0.0, 1.0) * 0.1;
|
||||||
|
// let g: f64 = (self.mouse_y / f64::from(self.size.height)).clamp(0.0, 1.0) * 0.2;
|
||||||
|
// // END GARBAGE
|
||||||
|
|
||||||
|
// let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||||
|
// label: Some("Render Pass"),
|
||||||
|
// color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||||
|
// view: &view,
|
||||||
|
// resolve_target: None,
|
||||||
|
// ops: wgpu::Operations {
|
||||||
|
// load: wgpu::LoadOp::Clear(wgpu::Color {
|
||||||
|
// r, //0.1,
|
||||||
|
// g, //0.2,
|
||||||
|
// b: 0.3,
|
||||||
|
// a: 1.0,
|
||||||
|
// }),
|
||||||
|
// store: true,
|
||||||
|
// },
|
||||||
|
// })],
|
||||||
|
// depth_stencil_attachment: None,
|
||||||
|
// });
|
||||||
|
|
||||||
|
// render_pass.set_pipeline(&self.render_pipeline);
|
||||||
|
// render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
|
||||||
|
// render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
||||||
|
// render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
||||||
|
// render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
|
||||||
|
// }
|
||||||
|
|
||||||
// Submit will accept anything that implements IntoIter
|
// Submit will accept anything that implements IntoIter
|
||||||
self.queue.submit(std::iter::once(encoder.finish()));
|
self.queue.submit(std::iter::once(encoder.finish()));
|
||||||
output.present();
|
output.present();
|
||||||
|
|
@ -403,8 +460,7 @@ pub async fn run() {
|
||||||
context.update();
|
context.update();
|
||||||
state.update();
|
state.update();
|
||||||
|
|
||||||
context.render();
|
match state.render(context.render()) {
|
||||||
match state.render() {
|
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
// Reconfigure the surface if lost
|
// Reconfigure the surface if lost
|
||||||
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
|
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
|
||||||
|
|
|
||||||
14
src/main.js
14
src/main.js
|
|
@ -1,11 +1,11 @@
|
||||||
import * as graphics from 'graphics';
|
import { cls, print } from "graphics";
|
||||||
|
|
||||||
function init() {
|
export function init() {
|
||||||
graphics.print("Hello world!");
|
print("Hello world!");
|
||||||
}
|
}
|
||||||
|
|
||||||
function update() {}
|
export function update() {}
|
||||||
|
|
||||||
function draw() {}
|
export function draw() {
|
||||||
|
cls(0.1, 0.2, 0.3);
|
||||||
export { init, update, draw }
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,17 @@
|
||||||
use oden_js::{Context, Runtime, Value};
|
use oden_js::{Context, Runtime, Value};
|
||||||
|
use std::sync::mpsc::{channel, Receiver};
|
||||||
|
|
||||||
mod graphics;
|
pub mod graphics;
|
||||||
|
use graphics::GraphicsCommand;
|
||||||
|
|
||||||
pub struct ScriptContext {
|
pub struct ScriptContext {
|
||||||
context: Context,
|
context: Context,
|
||||||
init: Value,
|
init: Value,
|
||||||
update: Value,
|
update: Value,
|
||||||
draw: Value,
|
draw: Value,
|
||||||
|
|
||||||
|
gfx: graphics::GraphicsAPI,
|
||||||
|
gfx_receive: Receiver<graphics::GraphicsCommand>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ScriptContext {
|
impl ScriptContext {
|
||||||
|
|
@ -18,7 +23,10 @@ impl ScriptContext {
|
||||||
context.add_intrinsic_bigdecimal();
|
context.add_intrinsic_bigdecimal();
|
||||||
context.add_intrinsic_operators();
|
context.add_intrinsic_operators();
|
||||||
|
|
||||||
graphics::GraphicsAPI::define(&context).expect("Graphics module should load without error");
|
let (gfx_send, gfx_receive) = channel();
|
||||||
|
|
||||||
|
let gfx = graphics::GraphicsAPI::define(&context, gfx_send)
|
||||||
|
.expect("Graphics module should load without error");
|
||||||
|
|
||||||
let js = include_str!("main.js");
|
let js = include_str!("main.js");
|
||||||
let module = context
|
let module = context
|
||||||
|
|
@ -37,12 +45,20 @@ impl ScriptContext {
|
||||||
|
|
||||||
ScriptContext {
|
ScriptContext {
|
||||||
context,
|
context,
|
||||||
|
|
||||||
init,
|
init,
|
||||||
update,
|
update,
|
||||||
draw,
|
draw,
|
||||||
|
|
||||||
|
gfx,
|
||||||
|
gfx_receive,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: The script could really be on a background thread you know.
|
||||||
|
// We would want a bi-directional gate for frames to not let the
|
||||||
|
// game thread go to fast probably? And to discard whole frames &c.
|
||||||
|
|
||||||
pub fn init(&self) {
|
pub fn init(&self) {
|
||||||
self.init.call(&self.context).expect("Exception in init");
|
self.init.call(&self.context).expect("Exception in init");
|
||||||
}
|
}
|
||||||
|
|
@ -53,7 +69,17 @@ impl ScriptContext {
|
||||||
.expect("Exception in update");
|
.expect("Exception in update");
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self) {
|
pub fn render(&self) -> Vec<graphics::GraphicsCommand> {
|
||||||
self.draw.call(&self.context).expect("Exception in draw");
|
self.draw.call(&self.context).expect("Exception in draw");
|
||||||
|
self.gfx.end_frame();
|
||||||
|
|
||||||
|
let mut commands = Vec::new();
|
||||||
|
loop {
|
||||||
|
match self.gfx_receive.recv().unwrap() {
|
||||||
|
GraphicsCommand::EndFrame => break,
|
||||||
|
other => commands.push(other),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
commands
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
93
src/script/graphics.rs
Normal file
93
src/script/graphics.rs
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
use oden_js::{module, ContextRef, Error, Value, ValueRef, ValueResult};
|
||||||
|
use std::sync::mpsc::Sender;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub struct PrintCommand {
|
||||||
|
pub text: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ClearCommand {
|
||||||
|
pub color: [f64; 4],
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum GraphicsCommand {
|
||||||
|
Clear(ClearCommand),
|
||||||
|
Print(PrintCommand),
|
||||||
|
EndFrame,
|
||||||
|
}
|
||||||
|
|
||||||
|
const MAGIC_VALUE: u64 = 0xABCDB00BABCDBEEB;
|
||||||
|
|
||||||
|
struct GraphicsImpl {
|
||||||
|
magic: u64,
|
||||||
|
sender: Sender<GraphicsCommand>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GraphicsImpl {
|
||||||
|
pub fn new(sender: Sender<GraphicsCommand>) -> Self {
|
||||||
|
GraphicsImpl {
|
||||||
|
magic: MAGIC_VALUE,
|
||||||
|
sender,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_fn(&self, ctx: &ContextRef, args: &[&ValueRef]) -> ValueResult {
|
||||||
|
assert_eq!(self.magic, MAGIC_VALUE);
|
||||||
|
let mut text = String::with_capacity(128);
|
||||||
|
for arg in args {
|
||||||
|
let v = arg.to_string(ctx)?;
|
||||||
|
text.push_str(&v);
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = self
|
||||||
|
.sender
|
||||||
|
.send(GraphicsCommand::Print(PrintCommand { text }));
|
||||||
|
|
||||||
|
Ok(Value::undefined(ctx))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cls_fn(&self, ctx: &ContextRef, args: &[&ValueRef]) -> ValueResult {
|
||||||
|
assert_eq!(self.magic, MAGIC_VALUE);
|
||||||
|
if args.len() != 3 {
|
||||||
|
return Err(Error::ArgumentCountMismatch {
|
||||||
|
expected: 3,
|
||||||
|
received: args.len(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let r = args[0].to_float64(&ctx)?;
|
||||||
|
let g = args[1].to_float64(&ctx)?;
|
||||||
|
let b = args[1].to_float64(&ctx)?;
|
||||||
|
|
||||||
|
let _ = self.sender.send(GraphicsCommand::Clear(ClearCommand {
|
||||||
|
color: [r, g, b, 1.0],
|
||||||
|
}));
|
||||||
|
Ok(Value::undefined(ctx))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct GraphicsAPI {
|
||||||
|
gfx: Arc<GraphicsImpl>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GraphicsAPI {
|
||||||
|
pub fn define(ctx: &ContextRef, sender: Sender<GraphicsCommand>) -> oden_js::Result<Self> {
|
||||||
|
let gfx = Arc::new(GraphicsImpl::new(sender));
|
||||||
|
let gfx_a = gfx.clone();
|
||||||
|
let gfx_b = gfx.clone();
|
||||||
|
module::NativeModuleBuilder::new(ctx)
|
||||||
|
.export(
|
||||||
|
"print",
|
||||||
|
ctx.new_dynamic_fn(move |ctx, _, args| gfx_a.print_fn(ctx, args))?,
|
||||||
|
)?
|
||||||
|
.export(
|
||||||
|
"cls",
|
||||||
|
ctx.new_dynamic_fn(move |ctx, _, args| gfx_b.cls_fn(ctx, args))?,
|
||||||
|
)?
|
||||||
|
.build("graphics")?;
|
||||||
|
Ok(GraphicsAPI { gfx })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn end_frame(&self) {
|
||||||
|
let _ = self.gfx.sender.send(GraphicsCommand::EndFrame);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue