dom/canvas/WebGL2Context.cpp
author Gurzau Raul <rgurzau@mozilla.com>
Sat, 25 May 2019 09:07:49 +0300
changeset 475564 af54b2de7028db03f42207598f7a0b4ba81e262f
parent 475524 e0cd10d35327b9061a7b825153896fed5e5c151b
child 475614 37fd585d6c8ffd435e422eec6f3e77fca4dcc798
permissions -rw-r--r--
Backed out 31 changesets (bug 1552643, bug 1550422) for xpcshell crash on a CLOSED TREE. Backed out changeset e30c1aa75529 (bug 1552643) Backed out changeset caadcd7e02d3 (bug 1552643) Backed out changeset aa7086ab09be (bug 1552643) Backed out changeset 0b4029671710 (bug 1550422) Backed out changeset a16295296035 (bug 1550422) Backed out changeset 3b70307c0db5 (bug 1550422) Backed out changeset 69df7818d4a3 (bug 1550422) Backed out changeset d98dfc565927 (bug 1550422) Backed out changeset 6f0997976944 (bug 1550422) Backed out changeset 0edd264464c2 (bug 1550422) Backed out changeset 9ea6da7a74ec (bug 1550422) Backed out changeset f855f9309c8b (bug 1550422) Backed out changeset 1033546224a7 (bug 1550422) Backed out changeset ade7384c6186 (bug 1550422) Backed out changeset 75b04de7e99c (bug 1550422) Backed out changeset 91c3acdb2454 (bug 1550422) Backed out changeset 77d2f80257d1 (bug 1550422) Backed out changeset e0cd10d35327 (bug 1550422) Backed out changeset 097091082423 (bug 1550422) Backed out changeset 2f328853c1ab (bug 1550422) Backed out changeset f92f2cc29cb1 (bug 1550422) Backed out changeset 6dc82f88333d (bug 1550422) Backed out changeset c20f66494d69 (bug 1550422) Backed out changeset 2ba22cddeb6f (bug 1550422) Backed out changeset 3aa72f89e295 (bug 1550422) Backed out changeset ab4c4e806977 (bug 1550422) Backed out changeset 72e5de040dda (bug 1550422) Backed out changeset 7d3c2d486706 (bug 1550422) Backed out changeset 132e0b8d8468 (bug 1550422) Backed out changeset 54c85ac75dd0 (bug 1550422) Backed out changeset d7ba4a18dd54 (bug 1550422)

/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "WebGL2Context.h"

#include "gfxPrefs.h"
#include "GLContext.h"
#include "mozilla/dom/WebGL2RenderingContextBinding.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/Telemetry.h"
#include "nsPrintfCString.h"
#include "WebGLBuffer.h"
#include "WebGLFormats.h"
#include "WebGLTransformFeedback.h"

namespace mozilla {

WebGL2Context::WebGL2Context() : WebGLContext() {
  MOZ_ASSERT(IsSupported(),
             "not supposed to create a WebGL2Context"
             "context when not supported");
}

WebGL2Context::~WebGL2Context() {}

UniquePtr<webgl::FormatUsageAuthority> WebGL2Context::CreateFormatUsage(
    gl::GLContext* gl) const {
  return webgl::FormatUsageAuthority::CreateForWebGL2(gl);
}

/*static*/
bool WebGL2Context::IsSupported() { return gfxPrefs::WebGL2Enabled(); }

/*static*/
WebGL2Context* WebGL2Context::Create() { return new WebGL2Context(); }

JSObject* WebGL2Context::WrapObject(JSContext* cx,
                                    JS::Handle<JSObject*> givenProto) {
  return dom::WebGL2RenderingContext_Binding::Wrap(cx, this, givenProto);
}

////////////////////////////////////////////////////////////////////////////////
// WebGL 2 initialisation

static const gl::GLFeature kRequiredFeatures[] = {
    gl::GLFeature::blend_minmax,
    gl::GLFeature::clear_buffers,
    gl::GLFeature::copy_buffer,
    gl::GLFeature::depth_texture,
    gl::GLFeature::draw_instanced,
    gl::GLFeature::draw_range_elements,
    gl::GLFeature::element_index_uint,
    gl::GLFeature::frag_color_float,
    gl::GLFeature::frag_depth,
    gl::GLFeature::framebuffer_object,
    gl::GLFeature::get_integer_indexed,
    gl::GLFeature::get_integer64_indexed,
    gl::GLFeature::gpu_shader4,
    gl::GLFeature::instanced_arrays,
    gl::GLFeature::instanced_non_arrays,
    gl::GLFeature::map_buffer_range,  // Used by GetBufferSubData.
    gl::GLFeature::occlusion_query2,
    gl::GLFeature::packed_depth_stencil,
    gl::GLFeature::query_objects,
    gl::GLFeature::renderbuffer_color_float,
    gl::GLFeature::renderbuffer_color_half_float,
    gl::GLFeature::sRGB,
    gl::GLFeature::sampler_objects,
    gl::GLFeature::standard_derivatives,
    gl::GLFeature::texture_3D,
    gl::GLFeature::texture_3D_compressed,
    gl::GLFeature::texture_3D_copy,
    gl::GLFeature::texture_float,
    gl::GLFeature::texture_half_float,
    gl::GLFeature::texture_half_float_linear,
    gl::GLFeature::texture_non_power_of_two,
    gl::GLFeature::texture_storage,
    gl::GLFeature::transform_feedback2,
    gl::GLFeature::uniform_buffer_object,
    gl::GLFeature::uniform_matrix_nonsquare,
    gl::GLFeature::vertex_array_object};

bool WebGLContext::InitWebGL2(FailureReason* const out_failReason) {
  MOZ_ASSERT(IsWebGL2(), "WebGLContext is not a WebGL 2 context!");

  std::vector<gl::GLFeature> missingList;

  const auto fnGatherMissing = [&](gl::GLFeature cur) {
    if (!gl->IsSupported(cur)) {
      missingList.push_back(cur);
    }
  };

  const auto fnGatherMissing2 = [&](gl::GLFeature main, gl::GLFeature alt) {
    if (!gl->IsSupported(main) && !gl->IsSupported(alt)) {
      missingList.push_back(main);
    }
  };

  ////

  for (const auto& cur : kRequiredFeatures) {
    fnGatherMissing(cur);
  }

  // On desktop, we fake occlusion_query_boolean with occlusion_query if
  // necessary. (See WebGL2ContextQueries.cpp)
  fnGatherMissing2(gl::GLFeature::occlusion_query_boolean,
                   gl::GLFeature::occlusion_query);

#ifdef XP_MACOSX
  // On OSX, GL core profile is used. This requires texture swizzle
  // support to emulate legacy texture formats: ALPHA, LUMINANCE,
  // and LUMINANCE_ALPHA.
  fnGatherMissing(gl::GLFeature::texture_swizzle);
#endif

  fnGatherMissing2(gl::GLFeature::prim_restart_fixed,
                   gl::GLFeature::prim_restart);

  ////

  if (!missingList.empty()) {
    nsAutoCString exts;
    for (auto itr = missingList.begin(); itr != missingList.end(); ++itr) {
      exts.AppendLiteral("\n  ");
      exts.Append(gl::GLContext::GetFeatureName(*itr));
    }

    const nsPrintfCString reason(
        "WebGL 2 requires support for the following"
        " features: %s",
        exts.BeginReading());
    *out_failReason = FailureReason("FEATURE_FAILURE_WEBGL2_OCCL", reason);
    return false;
  }

  // we initialise WebGL 2 related stuff.
  gl->GetUIntegerv(LOCAL_GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS,
                   &mGLMaxTransformFeedbackSeparateAttribs);
  gl->GetUIntegerv(LOCAL_GL_MAX_UNIFORM_BUFFER_BINDINGS,
                   &mGLMaxUniformBufferBindings);

  mGLMinProgramTexelOffset =
      gl->GetIntAs<uint32_t>(LOCAL_GL_MIN_PROGRAM_TEXEL_OFFSET);
  mGLMaxProgramTexelOffset =
      gl->GetIntAs<uint32_t>(LOCAL_GL_MAX_PROGRAM_TEXEL_OFFSET);

  mIndexedUniformBufferBindings.resize(mGLMaxUniformBufferBindings);

  mDefaultTransformFeedback = new WebGLTransformFeedback(this, 0);
  mBoundTransformFeedback = mDefaultTransformFeedback;

  gl->fGenTransformFeedbacks(1, &mEmptyTFO);

  ////

  if (!gl->IsGLES()) {
    // Desktop OpenGL requires the following to be enabled in order to
    // support sRGB operations on framebuffers.
    gl->fEnable(LOCAL_GL_FRAMEBUFFER_SRGB_EXT);
  }

  if (gl->IsSupported(gl::GLFeature::prim_restart_fixed)) {
    gl->fEnable(LOCAL_GL_PRIMITIVE_RESTART_FIXED_INDEX);
  } else {
    MOZ_ASSERT(gl->IsSupported(gl::GLFeature::prim_restart));
  }

  //////

  return true;
}

}  // namespace mozilla