diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2019-11-05 16:52:51 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-05 16:52:51 -0500 |
commit | afbcbf75eaa63ff0eec8fd3858e9155eb8dbadaa (patch) | |
tree | 8b0f5cf2b882ba3b7fc0fef8df49ac9a3a9d0e6a /components/script/dom/webglbuffer.rs | |
parent | 593f8bd0f66d0c4a4f8f636a78337a7f029b9b8f (diff) | |
parent | 4050b7f9eca4c581d100fed778fa09f21d7e09dd (diff) | |
download | servo-afbcbf75eaa63ff0eec8fd3858e9155eb8dbadaa.tar.gz servo-afbcbf75eaa63ff0eec8fd3858e9155eb8dbadaa.zip |
Auto merge of #24473 - mmatyas:webgl_fns_buffer, r=jdm
Implement the basic WebGL2 buffer data operations
Adds support for the WebGL2 calls `bufferData`, `bufferSubData`, `copyBufferSubData` and `getBufferSubData`.
Reference: https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7.3
---
<!-- Please describe your changes on the following line: -->
This patch depends on https://github.com/servo/sparkle/pull/8. Some tests cause a crash for me at the moment, as they depend on other, not yet implemented buffer calls and transform feedback objects.
As for the code, there are a few parts I'm not sure about:
- To get the element byte size of a TypedArray I've wrote a simple `match`, as the relevant field is not published in `rust-mozjs`. Is that okay or there's some other way to get this already?
- The WebGL1 BufferData implementations were copied into the WebGL2 code as a workaround, due to the difference in the available buffer slots (ie. `self.bound_buffer`). An alternative could be is to pass this function and self as parameters to an internal buffer data implementation function, but personally I found that code to be quite ugly.
cc @jdm @zakorgy
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors (with the sparkle patch)
- [x] `./mach test-tidy` does not report any errors
- [ ] These changes fix #___ (GitHub issue number if applicable)
<!-- Either: -->
- [x] There are tests for these changes OR
- [ ] These changes do not require tests because ___
<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Diffstat (limited to 'components/script/dom/webglbuffer.rs')
-rw-r--r-- | components/script/dom/webglbuffer.rs | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/components/script/dom/webglbuffer.rs b/components/script/dom/webglbuffer.rs index 9fd8df489d6..182100ae41f 100644 --- a/components/script/dom/webglbuffer.rs +++ b/components/script/dom/webglbuffer.rs @@ -3,6 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ // https://www.khronos.org/registry/webgl/specs/latest/1.0/webgl.idl +use crate::dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::WebGL2RenderingContextConstants; use crate::dom::bindings::codegen::Bindings::WebGLBufferBinding; use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants; use crate::dom::bindings::inheritance::Castable; @@ -16,6 +17,11 @@ use dom_struct::dom_struct; use ipc_channel::ipc; use std::cell::Cell; +fn target_is_copy_buffer(target: u32) -> bool { + target == WebGL2RenderingContextConstants::COPY_READ_BUFFER || + target == WebGL2RenderingContextConstants::COPY_WRITE_BUFFER +} + #[dom_struct] pub struct WebGLBuffer { webgl_object: WebGLObject, @@ -65,7 +71,7 @@ impl WebGLBuffer { self.id } - pub fn buffer_data(&self, data: &[u8], usage: u32) -> WebGLResult<()> { + pub fn buffer_data(&self, target: u32, data: &[u8], usage: u32) -> WebGLResult<()> { match usage { WebGLRenderingContextConstants::STREAM_DRAW | WebGLRenderingContextConstants::STATIC_DRAW | @@ -78,11 +84,7 @@ impl WebGLBuffer { let (sender, receiver) = ipc::bytes_channel().unwrap(); self.upcast::<WebGLObject>() .context() - .send_command(WebGLCommand::BufferData( - self.target.get().unwrap(), - receiver, - usage, - )); + .send_command(WebGLCommand::BufferData(target, receiver, usage)); sender.send(data).unwrap(); Ok(()) } @@ -124,11 +126,24 @@ impl WebGLBuffer { self.target.get() } - pub fn set_target(&self, target: u32) -> WebGLResult<()> { - if self.target.get().map_or(false, |t| t != target) { + fn can_bind_to(&self, new_target: u32) -> bool { + if let Some(current_target) = self.target.get() { + if [current_target, new_target] + .contains(&WebGLRenderingContextConstants::ELEMENT_ARRAY_BUFFER) + { + return target_is_copy_buffer(new_target) || new_target == current_target; + } + } + true + } + + pub fn set_target_maybe(&self, target: u32) -> WebGLResult<()> { + if !self.can_bind_to(target) { return Err(WebGLError::InvalidOperation); } - self.target.set(Some(target)); + if !target_is_copy_buffer(target) { + self.target.set(Some(target)); + } Ok(()) } |