Skip to content

Commit

Permalink
Expose more get_parameter types
Browse files Browse the repository at this point in the history
Signed-off-by: sagudev <[email protected]>
Co-authored-by: grovesNL <[email protected]>
  • Loading branch information
sagudev and grovesNL committed Oct 1, 2024
1 parent 08257cf commit 57995d9
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,25 @@ pub trait HasContext: __private::Sealed {
#[doc(alias = "glGetString")]
unsafe fn get_parameter_string(&self, parameter: u32) -> String;

unsafe fn get_parameter_buffer(&self, parameter: u32) -> Option<Self::Buffer>;

unsafe fn get_parameter_framebuffer(&self, parameter: u32) -> Option<Self::Framebuffer>;

unsafe fn get_parameter_program(&self, parameter: u32) -> Option<Self::Program>;

unsafe fn get_parameter_renderbuffer(&self, parameter: u32) -> Option<Self::Renderbuffer>;

unsafe fn get_parameter_sampler(&self, parameter: u32) -> Option<Self::Sampler>;

unsafe fn get_parameter_texture(&self, parameter: u32) -> Option<Self::Texture>;

unsafe fn get_parameter_transform_feedback(
&self,
parameter: u32,
) -> Option<Self::TransformFeedback>;

unsafe fn get_parameter_vertex_array(&self, parameter: u32) -> Option<Self::VertexArray>;

unsafe fn get_framebuffer_parameter_i32(&self, target: u32, parameter: u32) -> i32;

unsafe fn get_named_framebuffer_parameter_i32(
Expand Down
46 changes: 46 additions & 0 deletions src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ impl Context {
pub unsafe fn create_framebuffer_from_gl_name(gl_name: native_gl::GLuint) -> NativeFramebuffer {
NativeFramebuffer(non_zero_gl_name(gl_name))
}

unsafe fn get_parameter_gl_name(&self, parameter: u32) -> Option<NonZeroU32> {
let value = self.get_parameter_i32(parameter) as u32;
if value == 0 {
None
} else {
Some(non_zero_gl_name(value))
}
}
}

impl std::fmt::Debug for Context {
Expand Down Expand Up @@ -2049,6 +2058,43 @@ impl HasContext for Context {
.to_owned()
}

unsafe fn get_parameter_buffer(&self, parameter: u32) -> Option<Self::Buffer> {
self.get_parameter_gl_name(parameter).map(NativeBuffer)
}

unsafe fn get_parameter_framebuffer(&self, parameter: u32) -> Option<Self::Framebuffer> {
self.get_parameter_gl_name(parameter).map(NativeFramebuffer)
}

unsafe fn get_parameter_program(&self, parameter: u32) -> Option<Self::Program> {
self.get_parameter_gl_name(parameter).map(NativeProgram)
}

unsafe fn get_parameter_renderbuffer(&self, parameter: u32) -> Option<Self::Renderbuffer> {
self.get_parameter_gl_name(parameter)
.map(NativeRenderbuffer)
}

unsafe fn get_parameter_sampler(&self, parameter: u32) -> Option<Self::Sampler> {
self.get_parameter_gl_name(parameter).map(NativeSampler)
}

unsafe fn get_parameter_texture(&self, parameter: u32) -> Option<Self::Texture> {
self.get_parameter_gl_name(parameter).map(NativeTexture)
}

unsafe fn get_parameter_transform_feedback(
&self,
parameter: u32,
) -> Option<Self::TransformFeedback> {
self.get_parameter_gl_name(parameter)
.map(NativeTransformFeedback)
}

unsafe fn get_parameter_vertex_array(&self, parameter: u32) -> Option<Self::VertexArray> {
self.get_parameter_gl_name(parameter).map(NativeVertexArray)
}

unsafe fn get_framebuffer_parameter_i32(&self, target: u32, parameter: u32) -> i32 {
let gl = &self.raw;
let mut value = 0;
Expand Down
69 changes: 69 additions & 0 deletions src/web_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,40 @@ impl Context {
ext.restore_context()
}
}

unsafe fn get_parameter_gl_name<TKey, TResource>(
&self,
parameter: u32,
tracked_resource: &TrackedResource<TKey, TResource>,
) -> Option<TKey>
where
TKey: slotmap::Key,
TResource: From<wasm_bindgen::JsValue> + PartialEq,
{
let parameter_value = match self.raw {
RawRenderingContext::WebGl1(ref gl) => gl.get_parameter(parameter),
RawRenderingContext::WebGl2(ref gl) => gl.get_parameter(parameter),
}
.ok();
match parameter_value {
Some(pv) => {
let resource: TResource = pv.into();
// TODO: Make this search less expensive. If we had a bi-directional map, we could
// find the key immediately.
match tracked_resource
.borrow()
.iter()
.find(|(_, v)| **v == resource)
{
Some((k, _)) => Some(k),
None => panic!(
"A resource was created externally. This is not currently supported."
),
}
}
None => None,
}
}
}

new_key_type! { pub struct WebShaderKey; }
Expand Down Expand Up @@ -3431,6 +3465,41 @@ impl HasContext for Context {
.unwrap_or_else(|| String::from(""))
}

unsafe fn get_parameter_buffer(&self, parameter: u32) -> Option<Self::Buffer> {
self.get_parameter_gl_name(parameter, &self.buffers)
}

unsafe fn get_parameter_framebuffer(&self, parameter: u32) -> Option<Self::Framebuffer> {
self.get_parameter_gl_name(parameter, &self.framebuffers)
}

unsafe fn get_parameter_program(&self, parameter: u32) -> Option<Self::Program> {
self.get_parameter_gl_name(parameter, &self.programs)
}

unsafe fn get_parameter_renderbuffer(&self, parameter: u32) -> Option<Self::Renderbuffer> {
self.get_parameter_gl_name(parameter, &self.renderbuffers)
}

unsafe fn get_parameter_sampler(&self, parameter: u32) -> Option<Self::Sampler> {
self.get_parameter_gl_name(parameter, &self.samplers)
}

unsafe fn get_parameter_texture(&self, parameter: u32) -> Option<Self::Texture> {
self.get_parameter_gl_name(parameter, &self.textures)
}

unsafe fn get_parameter_transform_feedback(
&self,
parameter: u32,
) -> Option<Self::TransformFeedback> {
self.get_parameter_gl_name(parameter, &self.transform_feedbacks)
}

unsafe fn get_parameter_vertex_array(&self, parameter: u32) -> Option<Self::VertexArray> {
self.get_parameter_gl_name(parameter, &self.vertex_arrays)
}

unsafe fn get_framebuffer_parameter_i32(&self, _target: u32, _parameter: u32) -> i32 {
panic!("Get framebuffer parameter is not supported");
}
Expand Down

0 comments on commit 57995d9

Please sign in to comment.