Skip to content

Commit

Permalink
clear_depth, depth_range with auto select the best function (#310)
Browse files Browse the repository at this point in the history
* clear_depth, depth_range with auto select

Signed-off-by: sagudev <[email protected]>

* `supports_f64_precision` accept &self to work properly on native GLES

Signed-off-by: sagudev <[email protected]>

---------

Signed-off-by: sagudev <[email protected]>
  • Loading branch information
sagudev authored Oct 16, 2024
1 parent 3d2d3fa commit 3d537eb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,14 @@ pub trait HasContext: __private::Sealed {

unsafe fn clear_color(&self, red: f32, green: f32, blue: f32, alpha: f32);

unsafe fn supports_f64_precision() -> bool;
unsafe fn supports_f64_precision(&self) -> bool;

unsafe fn clear_depth_f64(&self, depth: f64);

unsafe fn clear_depth_f32(&self, depth: f32);

unsafe fn clear_depth(&self, depth: f64);

unsafe fn clear_stencil(&self, stencil: i32);

unsafe fn clear(&self, mask: u32);
Expand Down Expand Up @@ -1471,6 +1473,8 @@ pub trait HasContext: __private::Sealed {

unsafe fn depth_range_f64(&self, near: f64, far: f64);

unsafe fn depth_range(&self, near: f64, far: f64);

unsafe fn depth_range_f64_slice(&self, first: u32, count: i32, values: &[[f64; 2]]);

unsafe fn scissor(&self, x: i32, y: i32, width: i32, height: i32);
Expand Down
21 changes: 18 additions & 3 deletions src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,9 +1236,8 @@ impl HasContext for Context {
gl.ClearColor(red, green, blue, alpha);
}

unsafe fn supports_f64_precision() -> bool {
// TODO: Handle OpenGL ES
true
unsafe fn supports_f64_precision(&self) -> bool {
!self.version.is_embedded
}

unsafe fn clear_depth_f64(&self, depth: f64) {
Expand All @@ -1251,6 +1250,14 @@ impl HasContext for Context {
gl.ClearDepthf(depth);
}

unsafe fn clear_depth(&self, depth: f64) {
if self.supports_f64_precision() {
self.clear_depth_f64(depth);
} else {
self.clear_depth_f32(depth as f32);
}
}

unsafe fn clear_stencil(&self, stencil: i32) {
let gl = &self.raw;
gl.ClearStencil(stencil);
Expand Down Expand Up @@ -3266,6 +3273,14 @@ impl HasContext for Context {
gl.DepthRange(near, far);
}

unsafe fn depth_range(&self, near: f64, far: f64) {
if self.supports_f64_precision() {
self.depth_range_f64(near, far);
} else {
self.depth_range_f32(near as f32, far as f32);
}
}

unsafe fn depth_range_f64_slice(&self, first: u32, count: i32, values: &[[f64; 2]]) {
let gl = &self.raw;
gl.DepthRangeArrayv(first, count, values.as_ptr() as *const f64);
Expand Down
10 changes: 9 additions & 1 deletion src/web_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2546,7 +2546,7 @@ impl HasContext for Context {
}
}

unsafe fn supports_f64_precision() -> bool {
unsafe fn supports_f64_precision(&self) -> bool {
false
}

Expand All @@ -2561,6 +2561,10 @@ impl HasContext for Context {
}
}

unsafe fn clear_depth(&self, depth: f64) {
self.clear_depth_f32(depth as f32);
}

unsafe fn clear_stencil(&self, stencil: i32) {
match self.raw {
RawRenderingContext::WebGl1(ref gl) => gl.clear_stencil(stencil),
Expand Down Expand Up @@ -4891,6 +4895,10 @@ impl HasContext for Context {
panic!("Depth range with 64-bit float values is not supported");
}

unsafe fn depth_range(&self, near: f64, far: f64) {
self.depth_range_f32(near as f32, far as f32)
}

unsafe fn depth_range_f64_slice(&self, _first: u32, _count: i32, _values: &[[f64; 2]]) {
panic!("Depth range with 64-bit float slices is not supported");
}
Expand Down

0 comments on commit 3d537eb

Please sign in to comment.