Skip to content

Commit

Permalink
expose supported_extensions by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Frizi committed Jun 15, 2021
1 parent 505e799 commit 3a315fb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub trait HasContext {
type TransformFeedback: Copy + Clone + Debug + Eq + Hash + Ord + PartialEq + PartialOrd;
type UniformLocation: Clone + Debug;

fn get_supported_extensions(&self) -> HashSet<String>;
fn supported_extensions(&self) -> &HashSet<String>;

fn supports_debug(&self) -> bool;

Expand Down
4 changes: 2 additions & 2 deletions src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ impl HasContext for Context {
type UniformLocation = native_gl::GLuint;
type TransformFeedback = native_gl::GLuint;

fn get_supported_extensions(&self) -> HashSet<String> {
self.extensions.clone()
fn supported_extensions(&self) -> &HashSet<String> {
&self.extensions
}

fn supports_debug(&self) -> bool {
Expand Down
32 changes: 18 additions & 14 deletions src/web_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fn tracked_resource<K: slotmap::Key, V>() -> TrackedResource<K, V> {
pub struct Context {
raw: RawRenderingContext,
extensions: Extensions,
supported_extensions: HashSet<String>,
shaders: TrackedResource<WebShaderKey, WebGlShader>,
programs: TrackedResource<WebProgramKey, WebGlProgram>,
buffers: TrackedResource<WebBufferKey, WebGlBuffer>,
Expand Down Expand Up @@ -102,7 +103,7 @@ macro_rules! build_extensions {
.and_then(|maybe_ext| maybe_ext.map(|_| ()))
}

Extensions {
let extensions = Extensions {
angle_instanced_arrays: get_extension::<web_sys::AngleInstancedArrays>(
&$context,
"ANGLE_instanced_arrays",
Expand Down Expand Up @@ -229,16 +230,26 @@ macro_rules! build_extensions {
&$context,
"WEBGL_lose_context",
),
}
};

let supported_extensions = $context
.get_supported_extensions()
.unwrap()
.iter()
.map(|val| val.as_string().unwrap())
.collect::<HashSet<String>>();

(extensions, supported_extensions)
}};
}

impl Context {
pub fn from_webgl1_context(context: WebGlRenderingContext) -> Self {
let extensions = build_extensions!(context, WebGlRenderingContext);
let (extensions, supported_extensions) = build_extensions!(context, WebGlRenderingContext);
Self {
raw: RawRenderingContext::WebGl1(context),
extensions,
supported_extensions,
shaders: tracked_resource(),
programs: tracked_resource(),
buffers: tracked_resource(),
Expand All @@ -254,10 +265,11 @@ impl Context {
}

pub fn from_webgl2_context(context: WebGl2RenderingContext) -> Self {
let extensions = build_extensions!(context, WebGl2RenderingContext);
let (extensions, supported_extensions) = build_extensions!(context, WebGl2RenderingContext);
Self {
raw: RawRenderingContext::WebGl2(context),
extensions,
supported_extensions,
shaders: tracked_resource(),
programs: tracked_resource(),
buffers: tracked_resource(),
Expand Down Expand Up @@ -436,16 +448,8 @@ impl HasContext for Context {
type UniformLocation = WebGlUniformLocation;
type TransformFeedback = WebTransformFeedbackKey;

fn get_supported_extensions(&self) -> HashSet<String> {
let extensions_array = match self.raw {
RawRenderingContext::WebGl1(ref gl) => gl.get_supported_extensions(),
RawRenderingContext::WebGl2(ref gl) => gl.get_supported_extensions(),
}
.unwrap();
extensions_array
.iter()
.map(|val| val.as_string().unwrap())
.collect()
fn supported_extensions(&self) -> &HashSet<String> {
&self.supported_extensions
}

fn supports_debug(&self) -> bool {
Expand Down

0 comments on commit 3a315fb

Please sign in to comment.