content/canvas/src/WebGL2Context.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/canvas/src/WebGL2Context.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "WebGL2Context.h"
    1.10 +#include "GLContext.h"
    1.11 +#include "mozilla/dom/WebGL2RenderingContextBinding.h"
    1.12 +#include "mozilla/Preferences.h"
    1.13 +#include "mozilla/Telemetry.h"
    1.14 +
    1.15 +using namespace mozilla;
    1.16 +using namespace mozilla::gl;
    1.17 +
    1.18 +// -----------------------------------------------------------------------------
    1.19 +// CONSTRUCTOR & DESTRUCTOR
    1.20 +
    1.21 +WebGL2Context::WebGL2Context()
    1.22 +    : WebGLContext()
    1.23 +{
    1.24 +    MOZ_ASSERT(IsSupported(), "not supposed to create a WebGL2Context"
    1.25 +                              "context when not supported");
    1.26 +}
    1.27 +
    1.28 +WebGL2Context::~WebGL2Context()
    1.29 +{
    1.30 +
    1.31 +}
    1.32 +
    1.33 +
    1.34 +// -----------------------------------------------------------------------------
    1.35 +// STATIC FUNCTIONS
    1.36 +
    1.37 +bool
    1.38 +WebGL2Context::IsSupported()
    1.39 +{
    1.40 +    return Preferences::GetBool("webgl.enable-prototype-webgl2", false);
    1.41 +}
    1.42 +
    1.43 +WebGL2Context*
    1.44 +WebGL2Context::Create()
    1.45 +{
    1.46 +    return new WebGL2Context();
    1.47 +}
    1.48 +
    1.49 +
    1.50 +// -----------------------------------------------------------------------------
    1.51 +// IMPLEMENT nsWrapperCache
    1.52 +
    1.53 +JSObject*
    1.54 +WebGL2Context::WrapObject(JSContext *cx)
    1.55 +{
    1.56 +    return dom::WebGL2RenderingContextBinding::Wrap(cx, this);
    1.57 +}
    1.58 +
    1.59 +
    1.60 +// -----------------------------------------------------------------------------
    1.61 +// WebGL 2 initialisation
    1.62 +
    1.63 +bool
    1.64 +WebGLContext::InitWebGL2()
    1.65 +{
    1.66 +    MOZ_ASSERT(IsWebGL2(), "WebGLContext is not a WebGL 2 context!");
    1.67 +
    1.68 +    const WebGLExtensionID sExtensionNativelySupportedArr[] = {
    1.69 +        WebGLExtensionID::ANGLE_instanced_arrays,
    1.70 +        WebGLExtensionID::OES_element_index_uint,
    1.71 +        WebGLExtensionID::OES_standard_derivatives,
    1.72 +        WebGLExtensionID::OES_texture_float,
    1.73 +        WebGLExtensionID::OES_texture_float_linear,
    1.74 +        WebGLExtensionID::OES_vertex_array_object,
    1.75 +        WebGLExtensionID::WEBGL_depth_texture,
    1.76 +        WebGLExtensionID::WEBGL_draw_buffers
    1.77 +    };
    1.78 +    const GLFeature sFeatureRequiredArr[] = {
    1.79 +        GLFeature::blend_minmax,
    1.80 +        GLFeature::instanced_non_arrays,
    1.81 +        GLFeature::transform_feedback
    1.82 +    };
    1.83 +
    1.84 +    // check WebGL extensions that are supposed to be natively supported
    1.85 +    for (size_t i = 0; i < size_t(MOZ_ARRAY_LENGTH(sExtensionNativelySupportedArr)); i++)
    1.86 +    {
    1.87 +        WebGLExtensionID extension = sExtensionNativelySupportedArr[i];
    1.88 +
    1.89 +        if (!IsExtensionSupported(extension)) {
    1.90 +            GenerateWarning("WebGL 2 requires %s!", GetExtensionString(extension));
    1.91 +            return false;
    1.92 +        }
    1.93 +    }
    1.94 +
    1.95 +    // check required OpenGL extensions
    1.96 +    if (!gl->IsExtensionSupported(GLContext::EXT_gpu_shader4)) {
    1.97 +        GenerateWarning("WebGL 2 requires GL_EXT_gpu_shader4!");
    1.98 +        return false;
    1.99 +    }
   1.100 +
   1.101 +    // check OpenGL features
   1.102 +    if (!gl->IsSupported(GLFeature::occlusion_query) &&
   1.103 +        !gl->IsSupported(GLFeature::occlusion_query_boolean))
   1.104 +    {
   1.105 +        /*
   1.106 +         * on desktop, we fake occlusion_query_boolean with occlusion_query if
   1.107 +         * necessary. See WebGLContextAsyncQueries.cpp.
   1.108 +         */
   1.109 +        GenerateWarning("WebGL 2 requires occlusion queries!");
   1.110 +        return false;
   1.111 +    }
   1.112 +
   1.113 +    for (size_t i = 0; i < size_t(MOZ_ARRAY_LENGTH(sFeatureRequiredArr)); i++)
   1.114 +    {
   1.115 +        if (!gl->IsSupported(sFeatureRequiredArr[i])) {
   1.116 +            GenerateWarning("WebGL 2 requires GLFeature::%s!", GLContext::GetFeatureName(sFeatureRequiredArr[i]));
   1.117 +            return false;
   1.118 +        }
   1.119 +    }
   1.120 +
   1.121 +    // ok WebGL 2 is compatible, we can enable natively supported extensions.
   1.122 +    for (size_t i = 0; i < size_t(MOZ_ARRAY_LENGTH(sExtensionNativelySupportedArr)); i++) {
   1.123 +        EnableExtension(sExtensionNativelySupportedArr[i]);
   1.124 +
   1.125 +        MOZ_ASSERT(IsExtensionEnabled(sExtensionNativelySupportedArr[i]));
   1.126 +    }
   1.127 +
   1.128 +    // we initialise WebGL 2 related stuff.
   1.129 +    gl->GetUIntegerv(LOCAL_GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, &mGLMaxTransformFeedbackSeparateAttribs);
   1.130 +
   1.131 +    return true;
   1.132 +}

mercurial