Skip to content

Commit

Permalink
Implement IsShader fn and IsTexture fn for WebGLRenderingContext
Browse files Browse the repository at this point in the history
  • Loading branch information
ddefisher committed May 10, 2016
1 parent a09b237 commit 8a5b0b8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
10 changes: 10 additions & 0 deletions components/script/dom/webglprogram.rs
Expand Up @@ -68,6 +68,14 @@ impl WebGLProgram {
if !self.is_deleted.get() {
self.is_deleted.set(true);
let _ = self.renderer.send(CanvasMsg::WebGL(WebGLCommand::DeleteProgram(self.id)));

if let Some(shader) = self.fragment_shader.get() {
shader.decrement_attached_counter();
}

if let Some(shader) = self.vertex_shader.get() {
shader.decrement_attached_counter();
}
}
}

Expand Down Expand Up @@ -118,6 +126,7 @@ impl WebGLProgram {
}

shader_slot.set(Some(shader));
shader.increment_attached_counter();

self.renderer.send(CanvasMsg::WebGL(WebGLCommand::AttachShader(self.id, shader.id()))).unwrap();

Expand All @@ -144,6 +153,7 @@ impl WebGLProgram {
}

shader_slot.set(None);
shader.decrement_attached_counter();

self.renderer.send(CanvasMsg::WebGL(WebGLCommand::DetachShader(self.id, shader.id()))).unwrap();

Expand Down
10 changes: 10 additions & 0 deletions components/script/dom/webglrenderingcontext.rs
Expand Up @@ -1157,6 +1157,16 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
.unwrap()
}

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.9
fn IsShader(&self, shader: Option<&WebGLShader>) -> bool {
shader.map_or(false, |s| !s.is_deleted() || s.is_attached())
}

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
fn IsTexture(&self, texture: Option<&WebGLTexture>) -> bool {
texture.map_or(false, |tex| tex.target().is_some() && !tex.is_deleted())
}

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.3
fn LineWidth(&self, width: f32) {
if width.is_nan() || width <= 0f32 {
Expand Down
21 changes: 21 additions & 0 deletions components/script/dom/webglshader.rs
Expand Up @@ -32,6 +32,7 @@ pub struct WebGLShader {
source: DOMRefCell<Option<DOMString>>,
info_log: DOMRefCell<Option<String>>,
is_deleted: Cell<bool>,
attached_counter: Cell<u32>,
compilation_status: Cell<ShaderCompilationStatus>,
#[ignore_heap_size_of = "Defined in ipc-channel"]
renderer: IpcSender<CanvasMsg>,
Expand All @@ -55,6 +56,7 @@ impl WebGLShader {
source: DOMRefCell::new(None),
info_log: DOMRefCell::new(None),
is_deleted: Cell::new(false),
attached_counter: Cell::new(0),
compilation_status: Cell::new(ShaderCompilationStatus::NotCompiled),
renderer: renderer,
}
Expand Down Expand Up @@ -126,13 +128,31 @@ impl WebGLShader {

/// Mark this shader as deleted (if it wasn't previously)
/// and delete it as if calling glDeleteShader.
/// Currently does not check if shader is attached
pub fn delete(&self) {
if !self.is_deleted.get() {
self.is_deleted.set(true);
let _ = self.renderer.send(CanvasMsg::WebGL(WebGLCommand::DeleteShader(self.id)));
}
}

pub fn is_deleted(&self) -> bool {
self.is_deleted.get()
}

pub fn is_attached(&self) -> bool {
self.attached_counter.get() > 0
}

pub fn increment_attached_counter(&self) {
self.attached_counter.set(self.attached_counter.get() + 1);
}

pub fn decrement_attached_counter(&self) {
assert!(self.attached_counter.get() > 0);
self.attached_counter.set(self.attached_counter.get() - 1);
}

/// glGetShaderInfoLog
pub fn info_log(&self) -> Option<String> {
self.info_log.borrow().clone()
Expand Down Expand Up @@ -162,6 +182,7 @@ impl WebGLShader {

impl Drop for WebGLShader {
fn drop(&mut self) {
assert!(self.attached_counter.get() == 0);
self.delete();
}
}
8 changes: 8 additions & 0 deletions components/script/dom/webgltexture.rs
Expand Up @@ -176,6 +176,14 @@ impl WebGLTexture {
}
}

pub fn is_deleted(&self) -> bool {
self.is_deleted.get()
}

pub fn target(&self) -> Option<u32> {
self.target.get()
}

/// We have to follow the conversion rules for GLES 2.0. See:
/// https://www.khronos.org/webgl/public-mailing-list/archives/1008/msg00014.html
///
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/webidls/WebGLRenderingContext.webidl
Expand Up @@ -605,8 +605,8 @@ interface WebGLRenderingContextBase
//[WebGLHandlesContextLoss] GLboolean isFramebuffer(WebGLFramebuffer? framebuffer);
//[WebGLHandlesContextLoss] GLboolean isProgram(WebGLProgram? program);
//[WebGLHandlesContextLoss] GLboolean isRenderbuffer(WebGLRenderbuffer? renderbuffer);
//[WebGLHandlesContextLoss] GLboolean isShader(WebGLShader? shader);
//[WebGLHandlesContextLoss] GLboolean isTexture(WebGLTexture? texture);
[WebGLHandlesContextLoss] GLboolean isShader(WebGLShader? shader);
[WebGLHandlesContextLoss] GLboolean isTexture(WebGLTexture? texture);
void lineWidth(GLfloat width);
void linkProgram(WebGLProgram? program);
void pixelStorei(GLenum pname, GLint param);
Expand Down

0 comments on commit 8a5b0b8

Please sign in to comment.