michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "WebGL2Context.h" michael@0: #include "GLContext.h" michael@0: #include "mozilla/dom/WebGL2RenderingContextBinding.h" michael@0: #include "mozilla/Preferences.h" michael@0: #include "mozilla/Telemetry.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::gl; michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // CONSTRUCTOR & DESTRUCTOR michael@0: michael@0: WebGL2Context::WebGL2Context() michael@0: : WebGLContext() michael@0: { michael@0: MOZ_ASSERT(IsSupported(), "not supposed to create a WebGL2Context" michael@0: "context when not supported"); michael@0: } michael@0: michael@0: WebGL2Context::~WebGL2Context() michael@0: { michael@0: michael@0: } michael@0: michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // STATIC FUNCTIONS michael@0: michael@0: bool michael@0: WebGL2Context::IsSupported() michael@0: { michael@0: return Preferences::GetBool("webgl.enable-prototype-webgl2", false); michael@0: } michael@0: michael@0: WebGL2Context* michael@0: WebGL2Context::Create() michael@0: { michael@0: return new WebGL2Context(); michael@0: } michael@0: michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // IMPLEMENT nsWrapperCache michael@0: michael@0: JSObject* michael@0: WebGL2Context::WrapObject(JSContext *cx) michael@0: { michael@0: return dom::WebGL2RenderingContextBinding::Wrap(cx, this); michael@0: } michael@0: michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // WebGL 2 initialisation michael@0: michael@0: bool michael@0: WebGLContext::InitWebGL2() michael@0: { michael@0: MOZ_ASSERT(IsWebGL2(), "WebGLContext is not a WebGL 2 context!"); michael@0: michael@0: const WebGLExtensionID sExtensionNativelySupportedArr[] = { michael@0: WebGLExtensionID::ANGLE_instanced_arrays, michael@0: WebGLExtensionID::OES_element_index_uint, michael@0: WebGLExtensionID::OES_standard_derivatives, michael@0: WebGLExtensionID::OES_texture_float, michael@0: WebGLExtensionID::OES_texture_float_linear, michael@0: WebGLExtensionID::OES_vertex_array_object, michael@0: WebGLExtensionID::WEBGL_depth_texture, michael@0: WebGLExtensionID::WEBGL_draw_buffers michael@0: }; michael@0: const GLFeature sFeatureRequiredArr[] = { michael@0: GLFeature::blend_minmax, michael@0: GLFeature::instanced_non_arrays, michael@0: GLFeature::transform_feedback michael@0: }; michael@0: michael@0: // check WebGL extensions that are supposed to be natively supported michael@0: for (size_t i = 0; i < size_t(MOZ_ARRAY_LENGTH(sExtensionNativelySupportedArr)); i++) michael@0: { michael@0: WebGLExtensionID extension = sExtensionNativelySupportedArr[i]; michael@0: michael@0: if (!IsExtensionSupported(extension)) { michael@0: GenerateWarning("WebGL 2 requires %s!", GetExtensionString(extension)); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: // check required OpenGL extensions michael@0: if (!gl->IsExtensionSupported(GLContext::EXT_gpu_shader4)) { michael@0: GenerateWarning("WebGL 2 requires GL_EXT_gpu_shader4!"); michael@0: return false; michael@0: } michael@0: michael@0: // check OpenGL features michael@0: if (!gl->IsSupported(GLFeature::occlusion_query) && michael@0: !gl->IsSupported(GLFeature::occlusion_query_boolean)) michael@0: { michael@0: /* michael@0: * on desktop, we fake occlusion_query_boolean with occlusion_query if michael@0: * necessary. See WebGLContextAsyncQueries.cpp. michael@0: */ michael@0: GenerateWarning("WebGL 2 requires occlusion queries!"); michael@0: return false; michael@0: } michael@0: michael@0: for (size_t i = 0; i < size_t(MOZ_ARRAY_LENGTH(sFeatureRequiredArr)); i++) michael@0: { michael@0: if (!gl->IsSupported(sFeatureRequiredArr[i])) { michael@0: GenerateWarning("WebGL 2 requires GLFeature::%s!", GLContext::GetFeatureName(sFeatureRequiredArr[i])); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: // ok WebGL 2 is compatible, we can enable natively supported extensions. michael@0: for (size_t i = 0; i < size_t(MOZ_ARRAY_LENGTH(sExtensionNativelySupportedArr)); i++) { michael@0: EnableExtension(sExtensionNativelySupportedArr[i]); michael@0: michael@0: MOZ_ASSERT(IsExtensionEnabled(sExtensionNativelySupportedArr[i])); michael@0: } michael@0: michael@0: // we initialise WebGL 2 related stuff. michael@0: gl->GetUIntegerv(LOCAL_GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, &mGLMaxTransformFeedbackSeparateAttribs); michael@0: michael@0: return true; michael@0: }