webgl: Track uniformlocation's program

This commit is contained in:
Emilio Cobos Álvarez 2015-12-29 13:51:10 +01:00
parent 532b53ddc9
commit 221a583784
3 changed files with 35 additions and 11 deletions

View file

@ -55,6 +55,10 @@ impl WebGLProgram {
impl WebGLProgram { impl WebGLProgram {
pub fn id(&self) -> u32 {
self.id
}
/// glDeleteProgram /// glDeleteProgram
pub fn delete(&self) { pub fn delete(&self) {
if !self.is_deleted.get() { if !self.is_deleted.get() {

View file

@ -79,6 +79,7 @@ pub struct WebGLRenderingContext {
bound_texture_cube_map: MutNullableHeap<JS<WebGLTexture>>, bound_texture_cube_map: MutNullableHeap<JS<WebGLTexture>>,
bound_buffer_array: MutNullableHeap<JS<WebGLBuffer>>, bound_buffer_array: MutNullableHeap<JS<WebGLBuffer>>,
bound_buffer_element_array: MutNullableHeap<JS<WebGLBuffer>>, bound_buffer_element_array: MutNullableHeap<JS<WebGLBuffer>>,
current_program: MutNullableHeap<JS<WebGLProgram>>,
} }
impl WebGLRenderingContext { impl WebGLRenderingContext {
@ -106,6 +107,7 @@ impl WebGLRenderingContext {
bound_texture_cube_map: MutNullableHeap::new(None), bound_texture_cube_map: MutNullableHeap::new(None),
bound_buffer_array: MutNullableHeap::new(None), bound_buffer_array: MutNullableHeap::new(None),
bound_buffer_element_array: MutNullableHeap::new(None), bound_buffer_element_array: MutNullableHeap::new(None),
current_program: MutNullableHeap::new(None),
} }
}) })
} }
@ -804,7 +806,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
name: DOMString) -> Option<Root<WebGLUniformLocation>> { name: DOMString) -> Option<Root<WebGLUniformLocation>> {
if let Some(program) = program { if let Some(program) = program {
handle_potential_webgl_error!(self, program.get_uniform_location(name), None) handle_potential_webgl_error!(self, program.get_uniform_location(name), None)
.map(|location| WebGLUniformLocation::new(self.global().r(), location)) .map(|location| WebGLUniformLocation::new(self.global().r(), location, program.id()))
} else { } else {
None None
} }
@ -934,13 +936,18 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
fn Uniform1f(&self, fn Uniform1f(&self,
uniform: Option<&WebGLUniformLocation>, uniform: Option<&WebGLUniformLocation>,
val: f32) { val: f32) {
let uniform_id = match uniform { let uniform = match uniform {
Some(uniform) => uniform.id(), Some(uniform) => uniform,
None => return, None => return,
}; };
match self.current_program.get() {
Some(ref program) if program.id() == uniform.program_id() => {},
_ => return self.webgl_error(InvalidOperation),
};
self.ipc_renderer self.ipc_renderer
.send(CanvasMsg::WebGL(CanvasWebGLMsg::Uniform1f(uniform_id, val))) .send(CanvasMsg::WebGL(CanvasWebGLMsg::Uniform1f(uniform.id(), val)))
.unwrap() .unwrap()
} }
@ -959,13 +966,18 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
fn Uniform4f(&self, fn Uniform4f(&self,
uniform: Option<&WebGLUniformLocation>, uniform: Option<&WebGLUniformLocation>,
x: f32, y: f32, z: f32, w: f32) { x: f32, y: f32, z: f32, w: f32) {
let uniform_id = match uniform { let uniform = match uniform {
Some(uniform) => uniform.id(), Some(uniform) => uniform,
None => return, None => return,
}; };
match self.current_program.get() {
Some(ref program) if program.id() == uniform.program_id() => {},
_ => return self.webgl_error(InvalidOperation),
};
self.ipc_renderer self.ipc_renderer
.send(CanvasMsg::WebGL(CanvasWebGLMsg::Uniform4f(uniform_id, x, y, z, w))) .send(CanvasMsg::WebGL(CanvasWebGLMsg::Uniform4f(uniform.id(), x, y, z, w)))
.unwrap() .unwrap()
} }
@ -993,7 +1005,8 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.9 // https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.9
fn UseProgram(&self, program: Option<&WebGLProgram>) { fn UseProgram(&self, program: Option<&WebGLProgram>) {
if let Some(program) = program { if let Some(program) = program {
program.use_program() program.use_program();
self.current_program.set(Some(program));
} }
} }

View file

@ -12,18 +12,21 @@ use dom::bindings::reflector::{Reflector, reflect_dom_object};
pub struct WebGLUniformLocation { pub struct WebGLUniformLocation {
reflector_: Reflector, reflector_: Reflector,
id: i32, id: i32,
program_id: u32,
} }
impl WebGLUniformLocation { impl WebGLUniformLocation {
fn new_inherited(id: i32) -> WebGLUniformLocation { fn new_inherited(id: i32, program_id: u32) -> WebGLUniformLocation {
WebGLUniformLocation { WebGLUniformLocation {
reflector_: Reflector::new(), reflector_: Reflector::new(),
id: id, id: id,
program_id: program_id,
} }
} }
pub fn new(global: GlobalRef, id: i32) -> Root<WebGLUniformLocation> { pub fn new(global: GlobalRef, id: i32, program_id: u32) -> Root<WebGLUniformLocation> {
reflect_dom_object(box WebGLUniformLocation::new_inherited(id), global, WebGLUniformLocationBinding::Wrap) reflect_dom_object(
box WebGLUniformLocation::new_inherited(id, program_id), global, WebGLUniformLocationBinding::Wrap)
} }
} }
@ -32,4 +35,8 @@ impl WebGLUniformLocation {
pub fn id(&self) -> i32 { pub fn id(&self) -> i32 {
self.id self.id
} }
pub fn program_id(&self) -> u32 {
self.program_id
}
} }