webxr: Use the same texture format as the original GL context's framebuffer when creating an XR GL layer.

This commit is contained in:
Josh Matthews 2019-09-19 17:46:09 -04:00
parent 778b48fa47
commit 5bd1e86d42
6 changed files with 43 additions and 17 deletions

View file

@ -4,14 +4,14 @@
use super::webgl_thread::{GLState, WebGLImpl}; use super::webgl_thread::{GLState, WebGLImpl};
use canvas_traits::webgl::{ use canvas_traits::webgl::{
GLContextAttributes, GLLimits, WebGLCommand, WebGLCommandBacktrace, WebGLVersion, GLContextAttributes, GLFormats, GLLimits, WebGLCommand, WebGLCommandBacktrace, WebGLVersion,
}; };
use euclid::default::Size2D; use euclid::default::Size2D;
use offscreen_gl_context::{ use offscreen_gl_context::{
ColorAttachmentType, DrawBuffer, GLContext, GLContextAttributes as RawGLContextAttributes, ColorAttachmentType, DrawBuffer, GLContext, GLContextAttributes as RawGLContextAttributes,
GLContextDispatcher, GLContextDispatcher,
}; };
use offscreen_gl_context::{GLLimits as RawGLLimits, GLVersion}; use offscreen_gl_context::{GLFormats as RawGLFormats, GLLimits as RawGLLimits, GLVersion};
use offscreen_gl_context::{NativeGLContext, NativeGLContextHandle, NativeGLContextMethods}; use offscreen_gl_context::{NativeGLContext, NativeGLContextHandle, NativeGLContextMethods};
use offscreen_gl_context::{OSMesaContext, OSMesaContextHandle}; use offscreen_gl_context::{OSMesaContext, OSMesaContextHandle};
use sparkle::gl; use sparkle::gl;
@ -179,7 +179,7 @@ impl GLContextWrapper {
} }
} }
pub fn get_info(&self) -> (Size2D<i32>, u32, GLLimits) { pub fn get_info(&self) -> (Size2D<i32>, u32, GLLimits, GLFormats) {
match *self { match *self {
GLContextWrapper::Native(ref ctx) => { GLContextWrapper::Native(ref ctx) => {
let (real_size, texture_id) = { let (real_size, texture_id) = {
@ -191,8 +191,9 @@ impl GLContextWrapper {
}; };
let limits = ctx.borrow_limits().clone(); let limits = ctx.borrow_limits().clone();
let formats = map_formats(ctx.borrow_formats());
(real_size, texture_id, map_limits(limits)) (real_size, texture_id, map_limits(limits), formats)
}, },
GLContextWrapper::OSMesa(ref ctx) => { GLContextWrapper::OSMesa(ref ctx) => {
let (real_size, texture_id) = { let (real_size, texture_id) = {
@ -204,8 +205,9 @@ impl GLContextWrapper {
}; };
let limits = ctx.borrow_limits().clone(); let limits = ctx.borrow_limits().clone();
let formats = map_formats(ctx.borrow_formats());
(real_size, texture_id, map_limits(limits)) (real_size, texture_id, map_limits(limits), formats)
}, },
} }
} }
@ -260,3 +262,10 @@ pub fn map_attrs_to_script_attrs(attrs: RawGLContextAttributes) -> GLContextAttr
preserve_drawing_buffer: attrs.preserve_drawing_buffer, preserve_drawing_buffer: attrs.preserve_drawing_buffer,
} }
} }
fn map_formats(formats: &RawGLFormats) -> GLFormats {
GLFormats {
texture_format: formats.texture,
texture_type: formats.texture_type,
}
}

View file

@ -238,7 +238,7 @@ impl WebGLThread {
WebGLMsg::CreateContext(version, size, attributes, result_sender) => { WebGLMsg::CreateContext(version, size, attributes, result_sender) => {
let result = self.create_webgl_context(version, size, attributes); let result = self.create_webgl_context(version, size, attributes);
result_sender result_sender
.send(result.map(|(id, limits, share_mode)| { .send(result.map(|(id, limits, share_mode, framebuffer_format)| {
let data = Self::make_current_if_needed( let data = Self::make_current_if_needed(
id, id,
&self.contexts, &self.contexts,
@ -276,6 +276,7 @@ impl WebGLThread {
share_mode, share_mode,
glsl_version, glsl_version,
api_type, api_type,
framebuffer_format,
} }
})) }))
.unwrap(); .unwrap();
@ -406,7 +407,7 @@ impl WebGLThread {
version: WebGLVersion, version: WebGLVersion,
size: Size2D<u32>, size: Size2D<u32>,
attributes: GLContextAttributes, attributes: GLContextAttributes,
) -> Result<(WebGLContextId, GLLimits, WebGLContextShareMode), String> { ) -> Result<(WebGLContextId, GLLimits, WebGLContextShareMode, GLFormats), String> {
// Creating a new GLContext may make the current bound context_id dirty. // Creating a new GLContext may make the current bound context_id dirty.
// Clear it to ensure that make_current() is called in subsequent commands. // Clear it to ensure that make_current() is called in subsequent commands.
self.bound_context_id = None; self.bound_context_id = None;
@ -434,7 +435,7 @@ impl WebGLThread {
.next_id(WebrenderImageHandlerType::WebGL) .next_id(WebrenderImageHandlerType::WebGL)
.0 as usize, .0 as usize,
); );
let (size, texture_id, limits) = ctx.get_info(); let (size, texture_id, limits, framebuffer_formats) = ctx.get_info();
let use_apple_vertex_arrays = needs_apple_vertex_arrays(ctx.gl(), version); let use_apple_vertex_arrays = needs_apple_vertex_arrays(ctx.gl(), version);
self.contexts.insert( self.contexts.insert(
id, id,
@ -458,7 +459,7 @@ impl WebGLThread {
}, },
); );
Ok((id, limits, share_mode)) Ok((id, limits, share_mode, framebuffer_formats))
} }
/// Resizes a WebGLContext /// Resizes a WebGLContext
@ -476,7 +477,7 @@ impl WebGLThread {
.expect("Missing WebGL context!"); .expect("Missing WebGL context!");
match data.ctx.resize(size) { match data.ctx.resize(size) {
Ok(old_draw_buffer) => { Ok(old_draw_buffer) => {
let (real_size, texture_id, _) = data.ctx.get_info(); let (real_size, texture_id, _, _) = data.ctx.get_info();
let info = self.cached_context_info.get_mut(&context_id).unwrap(); let info = self.cached_context_info.get_mut(&context_id).unwrap();
if let ContextRenderState::Locked(ref mut in_use) = info.render_state { if let ContextRenderState::Locked(ref mut in_use) = info.render_state {
// If there's already an outdated draw buffer present, we can ignore // If there's already an outdated draw buffer present, we can ignore

View file

@ -91,6 +91,8 @@ pub struct WebGLCreateContextResult {
pub glsl_version: WebGLSLVersion, pub glsl_version: WebGLSLVersion,
/// The GL API used by the context. /// The GL API used by the context.
pub api_type: GlType, pub api_type: GlType,
/// The format for creating new offscreen framebuffers for this context.
pub framebuffer_format: GLFormats,
} }
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, Serialize)] #[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, Serialize)]
@ -886,3 +888,9 @@ pub struct GLLimits {
pub max_vertex_texture_image_units: u32, pub max_vertex_texture_image_units: u32,
pub max_vertex_uniform_vectors: u32, pub max_vertex_uniform_vectors: u32,
} }
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, Serialize)]
pub struct GLFormats {
pub texture_format: u32,
pub texture_type: u32,
}

View file

@ -45,8 +45,8 @@ use canvas_traits::canvas::{
CanvasGradientStop, CanvasId, LinearGradientStyle, RadialGradientStyle, CanvasGradientStop, CanvasId, LinearGradientStyle, RadialGradientStyle,
}; };
use canvas_traits::canvas::{CompositionOrBlending, LineCapStyle, LineJoinStyle, RepetitionStyle}; use canvas_traits::canvas::{CompositionOrBlending, LineCapStyle, LineJoinStyle, RepetitionStyle};
use canvas_traits::webgl::GLLimits;
use canvas_traits::webgl::{ActiveAttribInfo, ActiveUniformInfo, GlType, TexDataType, TexFormat}; use canvas_traits::webgl::{ActiveAttribInfo, ActiveUniformInfo, GlType, TexDataType, TexFormat};
use canvas_traits::webgl::{GLFormats, GLLimits};
use canvas_traits::webgl::{WebGLBufferId, WebGLChan, WebGLContextShareMode, WebGLError}; use canvas_traits::webgl::{WebGLBufferId, WebGLChan, WebGLContextShareMode, WebGLError};
use canvas_traits::webgl::{WebGLFramebufferId, WebGLMsgSender, WebGLPipeline, WebGLProgramId}; use canvas_traits::webgl::{WebGLFramebufferId, WebGLMsgSender, WebGLPipeline, WebGLProgramId};
use canvas_traits::webgl::{WebGLReceiver, WebGLRenderbufferId, WebGLSLVersion, WebGLSender}; use canvas_traits::webgl::{WebGLReceiver, WebGLRenderbufferId, WebGLSLVersion, WebGLSender};
@ -437,7 +437,7 @@ unsafe_no_jsmanaged_fields!(StorageType);
unsafe_no_jsmanaged_fields!(CanvasGradientStop, LinearGradientStyle, RadialGradientStyle); unsafe_no_jsmanaged_fields!(CanvasGradientStop, LinearGradientStyle, RadialGradientStyle);
unsafe_no_jsmanaged_fields!(LineCapStyle, LineJoinStyle, CompositionOrBlending); unsafe_no_jsmanaged_fields!(LineCapStyle, LineJoinStyle, CompositionOrBlending);
unsafe_no_jsmanaged_fields!(RepetitionStyle); unsafe_no_jsmanaged_fields!(RepetitionStyle);
unsafe_no_jsmanaged_fields!(WebGLError, GLLimits, GlType); unsafe_no_jsmanaged_fields!(WebGLError, GLFormats, GLLimits, GlType);
unsafe_no_jsmanaged_fields!(TimeProfilerChan); unsafe_no_jsmanaged_fields!(TimeProfilerChan);
unsafe_no_jsmanaged_fields!(MemProfilerChan); unsafe_no_jsmanaged_fields!(MemProfilerChan);
unsafe_no_jsmanaged_fields!(PseudoElement); unsafe_no_jsmanaged_fields!(PseudoElement);

View file

@ -53,8 +53,8 @@ use crate::script_runtime::JSContext as SafeJSContext;
use backtrace::Backtrace; use backtrace::Backtrace;
use canvas_traits::webgl::WebGLError::*; use canvas_traits::webgl::WebGLError::*;
use canvas_traits::webgl::{ use canvas_traits::webgl::{
webgl_channel, AlphaTreatment, DOMToTextureCommand, GLContextAttributes, GLLimits, GlType, webgl_channel, AlphaTreatment, DOMToTextureCommand, GLContextAttributes, GLFormats, GLLimits,
Parameter, TexDataType, TexFormat, TexParameter, WebGLChan, WebGLCommand, GlType, Parameter, TexDataType, TexFormat, TexParameter, WebGLChan, WebGLCommand,
WebGLCommandBacktrace, WebGLContextId, WebGLContextShareMode, WebGLError, WebGLCommandBacktrace, WebGLContextId, WebGLContextShareMode, WebGLError,
WebGLFramebufferBindingRequest, WebGLMsg, WebGLMsgSender, WebGLProgramId, WebGLResult, WebGLFramebufferBindingRequest, WebGLMsg, WebGLMsgSender, WebGLProgramId, WebGLResult,
WebGLSLVersion, WebGLSendResult, WebGLSender, WebGLVersion, WebVRCommand, YAxisTreatment, WebGLSLVersion, WebGLSendResult, WebGLSender, WebGLVersion, WebVRCommand, YAxisTreatment,
@ -170,6 +170,7 @@ pub struct WebGLRenderingContext {
current_vao: MutNullableDom<WebGLVertexArrayObjectOES>, current_vao: MutNullableDom<WebGLVertexArrayObjectOES>,
textures: Textures, textures: Textures,
api_type: GlType, api_type: GlType,
framebuffer_format: GLFormats,
} }
impl WebGLRenderingContext { impl WebGLRenderingContext {
@ -229,6 +230,7 @@ impl WebGLRenderingContext {
current_vao: Default::default(), current_vao: Default::default(),
textures: Textures::new(max_combined_texture_image_units), textures: Textures::new(max_combined_texture_image_units),
api_type: ctx_data.api_type, api_type: ctx_data.api_type,
framebuffer_format: ctx_data.framebuffer_format,
} }
}) })
} }
@ -1109,6 +1111,10 @@ impl WebGLRenderingContext {
pub fn extension_manager(&self) -> &WebGLExtensions { pub fn extension_manager(&self) -> &WebGLExtensions {
&self.extension_manager &self.extension_manager
} }
pub fn formats(&self) -> &GLFormats {
&self.framebuffer_format
}
} }
#[cfg(not(feature = "webgl_backtrace"))] #[cfg(not(feature = "webgl_backtrace"))]

View file

@ -110,16 +110,18 @@ impl XRWebGLLayer {
let mut pixels = CustomAutoRooter::new(None); let mut pixels = CustomAutoRooter::new(None);
let mut clear_bits = constants::COLOR_BUFFER_BIT; let mut clear_bits = constants::COLOR_BUFFER_BIT;
let formats = context.formats();
context.BindTexture(constants::TEXTURE_2D, Some(&texture)); context.BindTexture(constants::TEXTURE_2D, Some(&texture));
let sc = context.TexImage2D( let sc = context.TexImage2D(
constants::TEXTURE_2D, constants::TEXTURE_2D,
0, 0,
constants::RGBA, formats.texture_format,
resolution.width, resolution.width,
resolution.height, resolution.height,
0, 0,
constants::RGBA, formats.texture_format,
constants::UNSIGNED_BYTE, formats.texture_type,
pixels.root(*cx), pixels.root(*cx),
); );