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

Remove conservative unbinding #187

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ edition = "2021"
[dependencies]
posh-derive = { path = "derive" }

sealed = "0.4.0"
log = "0.4.17"
thiserror = "1.0.38"
bytemuck = { git = "https://github.com/leod/bytemuck", branch = "specify_crate_in_derive", features = ["derive"] }
crevice = { git = "https://github.com/leod/crevice", branch = "specify_crate_in_derive" }
fxhash = "0.2"
glam = { version = "0.27.0", optional = true }
glow = "0.13.0"
log = "0.4.17"
mint = { version = "0.5.9", optional = true }
glam = { version = "0.27.0", optional = true }
fxhash = "0.2"
sealed = "0.4.0"
smallvec = "1.13.2"
thiserror = "1.0.38"

[workspace]
members = ["derive", "run-wasm"]
Expand All @@ -26,24 +27,24 @@ default = ["glam", "mint"]
[dev-dependencies]
glam = "0.27.0"
image = "0.24.5"
instant = "0.1.12"
nanorand = "0.7.0"
simple_logger = "4.1.0"
instant = "0.1.12"

[target.'cfg(not(target_family = "wasm"))'.dev-dependencies]
sdl2 = "0.35.2"

[target.'cfg(target_family = "wasm")'.dev-dependencies]
console_log = "1"
winit = "0.28.6"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4.37"
winit = "0.28.6"

[target.'cfg(target_family = "wasm")'.dev-dependencies.web-sys]
version = "0.3.22"
features = [
'CanvasRenderingContext2d',
'OrientationLockType',
'Screen',
'ScreenOrientation',
'OrientationLockType',
]
18 changes: 12 additions & 6 deletions examples/deferred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ mod present_pass {
// Host code

struct Demo {
gl: gl::Context,
scene_program: gl::Program<Globals<Sl>, (), SceneAttachments<Sl>>,
present_program: gl::Program<SceneSamplers<Sl>, ()>,

Expand Down Expand Up @@ -199,6 +200,7 @@ impl Demo {
};

Ok(Self {
gl: gl.clone(),
scene_program: gl
.create_program(scene_pass::vertex_shader, scene_pass::fragment_shader)?,
present_program: gl
Expand All @@ -214,19 +216,23 @@ impl Demo {
let time = Instant::now().duration_since(self.start_time).as_secs_f32();
self.globals.set(Globals::new(time));

self.gl.clear(
gl::Framebuffer::default(),
gl::ClearParams {
color: Some([0.0; 4]),
depth: Some(1.0),
..Default::default()
},
)?;

self.scene_program
.with_uniforms(self.globals.as_binding())
.with_framebuffer(
self.depth_texture
.as_depth_attachment()
.with_color(self.scene_attachments.clone()),
)
.with_params(
gl::DrawParams::new()
.with_clear_color([0.0; 4])
.with_clear_depth(1.0)
.with_depth_test(gl::Comparison::Less),
)
.with_params(gl::DrawParams::new().with_depth_test(gl::Comparison::Less))
.draw(gl::PrimitiveMode::Triangles.as_vertex_spec_with_range(0..36))?;

self.present_program
Expand Down
11 changes: 10 additions & 1 deletion examples/hello_triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn fragment_shader(globals: Globals<Sl>, interp: sl::Vec2) -> sl::Vec4 {
// Host code

struct Demo {
gl: gl::Context,
program: gl::Program<Globals<Sl>, sl::Vec2>,

globals: gl::UniformBuffer<Globals<Gl>>,
Expand All @@ -52,6 +53,7 @@ impl Demo {
];

Ok(Self {
gl: gl.clone(),
program: gl.create_program(vertex_shader, fragment_shader)?,
globals: gl.create_uniform_buffer(Default::default(), StreamDraw)?,
vertices: gl.create_vertex_buffer(&vertices, StaticDraw)?,
Expand All @@ -65,9 +67,16 @@ impl Demo {
triangle_size: [1.0, 1.0].into(),
});

self.gl.clear(
gl::Framebuffer::default(),
gl::ClearParams {
color: Some([0.1, 0.2, 0.3, 1.0]),
..Default::default()
},
)?;

self.program
.with_uniforms(self.globals.as_binding())
.with_params(gl::DrawParams::new().with_clear_color([0.1, 0.2, 0.3, 1.0]))
.draw(self.vertices.as_vertex_spec(gl::PrimitiveMode::Triangles))?;

Ok(())
Expand Down
18 changes: 12 additions & 6 deletions examples/instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn fragment_shader(color: sl::Vec3) -> sl::Vec4 {
// Host code

struct Demo {
gl: gl::Context,
program: gl::Program<Camera<Sl>, VsInput<Sl>>,

camera: gl::UniformBuffer<Camera<Gl>>,
Expand All @@ -62,6 +63,7 @@ impl Demo {
use gl::BufferUsage::*;

Ok(Self {
gl: gl.clone(),
program: gl.create_program(vertex_shader, fragment_shader)?,
camera: gl.create_uniform_buffer(Camera::default(), StaticDraw)?,
instances: gl.create_vertex_buffer(&instances(0.0), StaticDraw)?,
Expand All @@ -70,14 +72,18 @@ impl Demo {
}

pub fn draw(&self) -> Result<(), gl::DrawError> {
self.gl.clear(
gl::Framebuffer::default(),
gl::ClearParams {
color: Some([0.1, 0.2, 0.3, 1.0]),
depth: Some(1.0),
..Default::default()
},
)?;

self.program
.with_uniforms(self.camera.as_binding())
.with_params(
gl::DrawParams::new()
.with_clear_color([0.1, 0.2, 0.3, 1.0])
.with_clear_depth(1.0)
.with_depth_test(gl::Comparison::Less),
)
.with_params(gl::DrawParams::new().with_depth_test(gl::Comparison::Less))
.draw(
gl::VertexSpec::new(gl::PrimitiveMode::Triangles).with_vertex_data(VsInput {
instance: self.instances.as_binding().with_instancing(),
Expand Down
1 change: 0 additions & 1 deletion examples/shadertoy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ impl Demo {

self.program
.with_uniforms(self.globals.as_binding())
.with_params(gl::DrawParams::new().with_clear_color([0.1, 0.2, 0.3, 1.0]))
.draw(gl::PrimitiveMode::Triangles.as_vertex_spec_with_range(0..VERTICES.len()))?;

Ok(())
Expand Down
20 changes: 17 additions & 3 deletions examples/shadow_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ impl Demo {
.as_vertex_spec(gl::PrimitiveMode::Triangles)
.with_element_data(self.scene_elements.as_binding());

self.gl.clear(
self.light_depth_map.as_depth_attachment(),
gl::ClearParams {
depth: Some(1.0),
..Default::default()
},
)?;

self.gl
.program(
|light: Light<Sl>, vertex: SceneVertex<Sl>| {
Expand All @@ -233,12 +241,20 @@ impl Demo {
.with_framebuffer(self.light_depth_map.as_depth_attachment())
.with_params(
gl::DrawParams::new()
.with_clear_depth(1.0)
.with_depth_test(gl::Comparison::Less)
.with_cull_face(gl::CullFace::Back),
)
.draw(scene_vertex_spec.clone())?;

self.gl.clear(
gl::Framebuffer::default(),
gl::ClearParams {
color: Some(glam::Vec4::ONE.into()),
depth: Some(1.0),
..Default::default()
},
)?;

self.gl
.program(scene_pass::vertex_shader, scene_pass::fragment_shader)
.with_uniforms(SceneUniforms {
Expand All @@ -250,8 +266,6 @@ impl Demo {
})
.with_params(
gl::DrawParams::new()
.with_clear_color(glam::Vec4::ONE.into())
.with_clear_depth(1.0)
.with_depth_test(gl::Comparison::Less)
.with_cull_face(gl::CullFace::Back),
)
Expand Down
18 changes: 12 additions & 6 deletions examples/textured_cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn vertex_shader(uniforms: Uniforms<Sl>, vertex: Vertex<Sl>) -> sl::VsOutput<sl:
// Host code

struct Demo {
gl: gl::Context,
program: gl::Program<(Uniforms<Sl>, sl::ColorSampler2d), Vertex<Sl>>,

camera: gl::UniformBuffer<Camera<Gl>>,
Expand Down Expand Up @@ -81,6 +82,7 @@ impl Demo {
);

Ok(Self {
gl: gl.clone(),
program: gl.create_program(vertex_shader, sl::ColorSampler2d::sample)?,
camera: gl.create_uniform_buffer(Camera::default(), StaticDraw)?,
time: gl.create_uniform_buffer(0.0, StreamDraw)?,
Expand All @@ -95,6 +97,15 @@ impl Demo {
let time = Instant::now().duration_since(self.start_time).as_secs_f32();
self.time.set(time);

self.gl.clear(
gl::Framebuffer::default(),
gl::ClearParams {
color: Some([0.1, 0.2, 0.3, 1.0]),
depth: Some(1.0),
..Default::default()
},
)?;

self.program
.with_uniforms((
Uniforms {
Expand All @@ -103,12 +114,7 @@ impl Demo {
},
self.texture.as_color_sampler(gl::Sampler2dParams::linear()),
))
.with_params(
gl::DrawParams::new()
.with_clear_color([0.1, 0.2, 0.3, 1.0])
.with_clear_depth(1.0)
.with_depth_test(gl::Comparison::Less),
)
.with_params(gl::DrawParams::new().with_depth_test(gl::Comparison::Less))
.draw(
self.vertices
.as_vertex_spec(gl::PrimitiveMode::Triangles)
Expand Down
6 changes: 3 additions & 3 deletions src/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub use program::{
DrawBuilderWithUniformsAndFramebuffer, Program,
};
pub use raw::{
BlendEquation, BlendFunc, Blending, BufferError, BufferUsage, Caps, Comparison, ContextError,
CreateError, CullFace, DrawError, DrawParams, ElementType, FramebufferError, ImageFormat,
ImageInternalFormat, PrimitiveMode, ProgramError, ProgramValidationError, Rect,
BlendEquation, BlendFunc, Blending, BufferError, BufferUsage, Caps, ClearParams, Comparison,
ContextError, CreateError, CullFace, DrawError, DrawParams, ElementType, FramebufferError,
ImageFormat, ImageInternalFormat, PrimitiveMode, ProgramError, ProgramValidationError, Rect,
Sampler2dParams, SamplerMagFilter, SamplerMinFilter, SamplerWrap, StencilOp, StencilOps,
StencilTest, TextureError, VertexArrayError,
};
Expand Down
30 changes: 19 additions & 11 deletions src/gl/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
any::{type_name, TypeId},
cell::{Cell, RefCell},
cell::RefCell,
collections::hash_map,
marker::PhantomData,
rc::Rc,
Expand All @@ -13,14 +13,14 @@
transpile::{transpile_to_program_def, transpile_to_program_def_with_consts},
ColorSample, FsFunc, FsSig, VsFunc, VsSig,
},
Block, Gl, Sl, Uniform, UniformUnion,
Block, FsInterface, Gl, Sl, Uniform, UniformUnion,
};

use super::{
program::{DrawBuilder, DrawBuilderWithUniforms},
raw, BufferError, BufferUsage, Caps, ColorImage, ColorTexture2d, ContextError, CreateError,
DepthImage, DepthTexture2d, DrawError, Element, ElementBuffer, Program, ProgramError,
TextureError, UniformBuffer, VertexBuffer,
raw, BufferError, BufferUsage, Caps, ClearParams, ColorImage, ColorTexture2d, ContextError,
CreateError, DepthImage, DepthTexture2d, DrawError, Element, ElementBuffer, Framebuffer,
FramebufferError, Program, ProgramError, TextureError, UniformBuffer, VertexBuffer,
};

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -92,7 +92,7 @@
_phantom: PhantomData<(VSig, FSig)>,
}

impl<'a, VSig, VFunc, FSig, FFunc> CacheDrawBuilder<'a, VSig, VFunc, FSig, FFunc>

Check warning on line 95 in src/gl/context.rs

View workflow job for this annotation

GitHub Actions / clippy

the following explicit lifetimes could be elided: 'a

warning: the following explicit lifetimes could be elided: 'a --> src/gl/context.rs:95:6 | 95 | impl<'a, VSig, VFunc, FSig, FFunc> CacheDrawBuilder<'a, VSig, VFunc, FSig, FFunc> | ^^ ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes = note: `#[warn(clippy::needless_lifetimes)]` on by default help: elide the lifetimes | 95 - impl<'a, VSig, VFunc, FSig, FFunc> CacheDrawBuilder<'a, VSig, VFunc, FSig, FFunc> 95 + impl<VSig, VFunc, FSig, FFunc> CacheDrawBuilder<'_, VSig, VFunc, FSig, FFunc> |
where
VSig: VsSig<C = ()>,
VFunc: VsFunc<VSig>,
Expand All @@ -112,7 +112,7 @@
&self.gl.raw,
self.vertex_shader,
self.fragment_shader,
self.gl.enable_program_source_logging.get(),
*self.gl.enable_program_source_logging.borrow(),
);

let inner = DrawBuilder {
Expand All @@ -134,7 +134,7 @@
pub struct Context {
raw: Rc<raw::Context>,
program_cache: Rc<RefCell<ProgramCache>>,
enable_program_source_logging: Cell<bool>,
enable_program_source_logging: Rc<RefCell<bool>>,
}

impl Context {
Expand All @@ -144,7 +144,7 @@
Ok(Self {
raw: Rc::new(raw),
program_cache: Default::default(),
enable_program_source_logging: Cell::new(false),
enable_program_source_logging: Default::default(),
})
}

Expand Down Expand Up @@ -239,7 +239,7 @@
let program_def =
transpile_to_program_def::<U, VSig, VFunc, FSig, FFunc>(vertex_shader, fragment_shader);

if self.enable_program_source_logging.get() {
if *self.enable_program_source_logging.borrow() {
log::info!("Vertex shader:\n{}", program_def.vertex_shader_source);
log::info!("Fragment shader:\n{}", program_def.fragment_shader_source);
}
Expand Down Expand Up @@ -268,7 +268,7 @@
fragment_shader,
);

if self.enable_program_source_logging.get() {
if *self.enable_program_source_logging.borrow() {
log::info!("Vertex shader:\n{}", program_def.vertex_shader_source);
log::info!("Fragment shader:\n{}", program_def.fragment_shader_source);
}
Expand Down Expand Up @@ -310,6 +310,14 @@
}

pub fn set_enable_program_source_logging(&self, value: bool) {
self.enable_program_source_logging.set(value);
*self.enable_program_source_logging.borrow_mut() = value;
}

pub fn clear<F: FsInterface<Sl>>(
&self,
framebuffer: impl Into<Framebuffer<F>>,
params: ClearParams,
) -> Result<(), FramebufferError> {
self.raw.clear(&framebuffer.into().raw(), params)
}
}
Loading
Loading