Skip to content

Commit

Permalink
Add support for WebGL2 TexImage2D
Browse files Browse the repository at this point in the history
Adds initial support for one of the WebGL2 `TexImage2D` call.
  • Loading branch information
Istvan Miklos committed Jun 18, 2020
1 parent ba5568a commit 6591fa5
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 49 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/canvas/Cargo.toml
Expand Up @@ -35,7 +35,7 @@ pixels = { path = "../pixels" }
raqote = { version = "0.8", features = ["text"] }
servo_arc = { path = "../servo_arc" }
servo_config = { path = "../config" }
sparkle = "0.1.24"
sparkle = "0.1.25"
style = { path = "../style" }
# NOTE: the sm-angle feature only enables ANGLE on Windows, not other platforms!
surfman = { version = "0.2", features = ["sm-angle", "sm-angle-default"] }
Expand Down
28 changes: 26 additions & 2 deletions components/canvas/webgl_thread.rs
Expand Up @@ -919,7 +919,7 @@ impl WebGLThread {
0,
gl::RGBA,
gl::UNSIGNED_BYTE,
None,
gl::TexImageSource::Pixels(None),
);
self.dom_outputs.insert(
pipeline_id,
Expand Down Expand Up @@ -1595,7 +1595,31 @@ impl WebGLImpl {
0,
format.as_gl_constant(),
effective_data_type,
Some(&pixels),
gl::TexImageSource::Pixels(Some(&pixels)),
);
},
WebGLCommand::TexImage2DPBO {
target,
level,
internal_format,
size,
format,
effective_data_type,
unpacking_alignment,
offset,
} => {
gl.pixel_store_i(gl::UNPACK_ALIGNMENT, unpacking_alignment as i32);

gl.tex_image_2d(
target,
level as i32,
internal_format.as_gl_constant() as i32,
size.width as i32,
size.height as i32,
0,
format.as_gl_constant(),
effective_data_type,
gl::TexImageSource::BufferOffset(offset),
);
},
WebGLCommand::TexSubImage2D {
Expand Down
10 changes: 10 additions & 0 deletions components/canvas_traits/webgl.rs
Expand Up @@ -398,6 +398,16 @@ pub enum WebGLCommand {
pixel_format: Option<PixelFormat>,
data: TruncatedDebug<IpcSharedMemory>,
},
TexImage2DPBO {
target: u32,
level: u32,
internal_format: TexFormat,
size: Size2D<u32>,
format: TexFormat,
effective_data_type: u32,
unpacking_alignment: u32,
offset: i64,
},
TexSubImage2D {
target: u32,
level: u32,
Expand Down
86 changes: 82 additions & 4 deletions components/script/dom/webgl2renderingcontext.rs
Expand Up @@ -29,7 +29,8 @@ use crate::dom::webglprogram::WebGLProgram;
use crate::dom::webglquery::WebGLQuery;
use crate::dom::webglrenderbuffer::WebGLRenderbuffer;
use crate::dom::webglrenderingcontext::{
uniform_get, uniform_typed, Operation, TexPixels, VertexAttrib, WebGLRenderingContext,
uniform_get, uniform_typed, Operation, TexPixels, TexSource, VertexAttrib,
WebGLRenderingContext,
};
use crate::dom::webglsampler::{WebGLSampler, WebGLSamplerValue};
use crate::dom::webglshader::WebGLShader;
Expand Down Expand Up @@ -2917,6 +2918,79 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingContext {

/// https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7.6
fn TexImage2D__(
&self,
target: u32,
level: i32,
internalformat: i32,
width: i32,
height: i32,
border: i32,
format: u32,
type_: u32,
pbo_offset: i64,
) -> Fallible<()> {
let pixel_unpack_buffer = match self.bound_pixel_unpack_buffer.get() {
Some(pixel_unpack_buffer) => pixel_unpack_buffer,
None => return Ok(self.base.webgl_error(InvalidOperation)),
};

if let Some(tf_buffer) = self.bound_transform_feedback_buffer.get() {
if pixel_unpack_buffer == tf_buffer {
return Ok(self.base.webgl_error(InvalidOperation));
}
}

if pbo_offset < 0 || pbo_offset as usize > pixel_unpack_buffer.capacity() {
return Ok(self.base.webgl_error(InvalidValue));
}

let unpacking_alignment = self.base.texture_unpacking_alignment();

let validator = TexImage2DValidator::new(
&self.base,
target,
level,
internalformat as u32,
width,
height,
border,
format,
type_,
);

let TexImage2DValidatorResult {
texture,
target,
width,
height,
level,
border,
internal_format,
format,
data_type,
} = match validator.validate() {
Ok(result) => result,
Err(_) => return Ok(()),
};

self.base.tex_image_2d(
&texture,
target,
data_type,
internal_format,
format,
level,
border,
unpacking_alignment,
Size2D::new(width, height),
TexSource::BufferOffset(pbo_offset),
);

Ok(())
}

/// https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7.6
fn TexImage2D___(
&self,
target: u32,
level: i32,
Expand Down Expand Up @@ -2975,15 +3049,16 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingContext {
level,
border,
unpacking_alignment,
pixels,
pixels.size(),
TexSource::Pixels(pixels),
);

Ok(())
}

/// https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7.6
#[allow(unsafe_code)]
fn TexImage2D___(
fn TexImage2D____(
&self,
target: u32,
level: i32,
Expand Down Expand Up @@ -3060,6 +3135,8 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingContext {
return Ok(self.base.webgl_error(InvalidOperation));
}

let size = Size2D::new(width, height);

self.base.tex_image_2d(
&texture,
target,
Expand All @@ -3069,7 +3146,8 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingContext {
level,
border,
unpacking_alignment,
TexPixels::from_array(buff, Size2D::new(width, height)),
size,
TexSource::Pixels(TexPixels::from_array(buff, size)),
);

Ok(())
Expand Down

0 comments on commit 6591fa5

Please sign in to comment.