Merge mozilla-central to autoland. a=merge CLOSED TREE
[gecko.git] / dom / canvas / WebGL2Context.cpp
blob5168f20162b6b1f88c9d62dc77d6281228505d48
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "WebGL2Context.h"
8 #include "mozilla/StaticPrefs_webgl.h"
9 #include "GLContext.h"
10 #include "mozilla/dom/WebGL2RenderingContextBinding.h"
11 #include "mozilla/ArrayUtils.h"
12 #include "mozilla/Telemetry.h"
13 #include "nsPrintfCString.h"
14 #include "WebGLBuffer.h"
15 #include "WebGLFormats.h"
16 #include "WebGLTransformFeedback.h"
18 namespace mozilla {
20 UniquePtr<webgl::FormatUsageAuthority> WebGL2Context::CreateFormatUsage(
21 gl::GLContext* gl) const {
22 return webgl::FormatUsageAuthority::CreateForWebGL2(gl);
25 ////////////////////////////////////////////////////////////////////////////////
26 // WebGL 2 initialisation
28 static const gl::GLFeature kRequiredFeatures[] = {
29 gl::GLFeature::blend_minmax,
30 gl::GLFeature::clear_buffers,
31 gl::GLFeature::copy_buffer,
32 gl::GLFeature::depth_texture,
33 gl::GLFeature::draw_instanced,
34 gl::GLFeature::element_index_uint,
35 gl::GLFeature::frag_color_float,
36 gl::GLFeature::frag_depth,
37 gl::GLFeature::framebuffer_object,
38 gl::GLFeature::get_integer_indexed,
39 gl::GLFeature::get_integer64_indexed,
40 gl::GLFeature::gpu_shader4,
41 gl::GLFeature::instanced_arrays,
42 gl::GLFeature::instanced_non_arrays,
43 gl::GLFeature::map_buffer_range, // Used by GetBufferSubData.
44 gl::GLFeature::occlusion_query2,
45 gl::GLFeature::packed_depth_stencil,
46 gl::GLFeature::query_objects,
47 gl::GLFeature::sRGB,
48 gl::GLFeature::sampler_objects,
49 gl::GLFeature::standard_derivatives,
50 gl::GLFeature::texture_3D,
51 gl::GLFeature::texture_3D_compressed,
52 gl::GLFeature::texture_3D_copy,
53 gl::GLFeature::texture_float,
54 gl::GLFeature::texture_half_float,
55 gl::GLFeature::texture_half_float_linear,
56 gl::GLFeature::texture_non_power_of_two,
57 gl::GLFeature::texture_storage,
58 gl::GLFeature::transform_feedback2,
59 gl::GLFeature::uniform_buffer_object,
60 gl::GLFeature::uniform_matrix_nonsquare,
61 gl::GLFeature::vertex_array_object};
63 bool WebGLContext::InitWebGL2(FailureReason* const out_failReason) {
64 MOZ_ASSERT(IsWebGL2(), "WebGLContext is not a WebGL 2 context!");
66 std::vector<gl::GLFeature> missingList;
68 const auto fnGatherMissing = [&](gl::GLFeature cur) {
69 if (!gl->IsSupported(cur)) {
70 missingList.push_back(cur);
74 const auto fnGatherMissing2 = [&](gl::GLFeature main, gl::GLFeature alt) {
75 if (!gl->IsSupported(main) && !gl->IsSupported(alt)) {
76 missingList.push_back(main);
80 ////
82 for (const auto& cur : kRequiredFeatures) {
83 fnGatherMissing(cur);
86 // On desktop, we fake occlusion_query_boolean with occlusion_query if
87 // necessary. (See WebGL2ContextQueries.cpp)
88 fnGatherMissing2(gl::GLFeature::occlusion_query_boolean,
89 gl::GLFeature::occlusion_query);
91 #ifdef XP_MACOSX
92 // On OSX, GL core profile is used. This requires texture swizzle
93 // support to emulate legacy texture formats: ALPHA, LUMINANCE,
94 // and LUMINANCE_ALPHA.
95 fnGatherMissing(gl::GLFeature::texture_swizzle);
96 #endif
98 fnGatherMissing2(gl::GLFeature::prim_restart_fixed,
99 gl::GLFeature::prim_restart);
101 ////
103 if (!missingList.empty()) {
104 nsAutoCString exts;
105 for (auto itr = missingList.begin(); itr != missingList.end(); ++itr) {
106 exts.AppendLiteral("\n ");
107 exts.Append(gl::GLContext::GetFeatureName(*itr));
110 const nsPrintfCString reason(
111 "WebGL 2 requires support for the following"
112 " features: %s",
113 exts.BeginReading());
114 *out_failReason = FailureReason("FEATURE_FAILURE_WEBGL2_OCCL", reason);
115 return false;
118 mGLMinProgramTexelOffset =
119 gl->GetIntAs<uint32_t>(LOCAL_GL_MIN_PROGRAM_TEXEL_OFFSET);
120 mGLMaxProgramTexelOffset =
121 gl->GetIntAs<uint32_t>(LOCAL_GL_MAX_PROGRAM_TEXEL_OFFSET);
123 mIndexedUniformBufferBindings.resize(mLimits->maxUniformBufferBindings);
125 mDefaultTransformFeedback = new WebGLTransformFeedback(this, 0);
126 mBoundTransformFeedback = mDefaultTransformFeedback;
128 gl->fGenTransformFeedbacks(1, &mEmptyTFO);
130 ////
132 if (!gl->IsGLES()) {
133 // Desktop OpenGL requires the following to be enabled in order to
134 // support sRGB operations on framebuffers.
135 gl->fEnable(LOCAL_GL_FRAMEBUFFER_SRGB_EXT);
138 if (gl->IsSupported(gl::GLFeature::prim_restart_fixed)) {
139 gl->fEnable(LOCAL_GL_PRIMITIVE_RESTART_FIXED_INDEX);
140 } else {
141 MOZ_ASSERT(gl->IsSupported(gl::GLFeature::prim_restart));
144 //////
146 return true;
149 // -
151 /*virtual*/
152 bool WebGL2Context::IsTexParamValid(GLenum pname) const {
153 switch (pname) {
154 case LOCAL_GL_TEXTURE_BASE_LEVEL:
155 case LOCAL_GL_TEXTURE_COMPARE_FUNC:
156 case LOCAL_GL_TEXTURE_COMPARE_MODE:
157 case LOCAL_GL_TEXTURE_IMMUTABLE_FORMAT:
158 case LOCAL_GL_TEXTURE_IMMUTABLE_LEVELS:
159 case LOCAL_GL_TEXTURE_MAX_LEVEL:
160 case LOCAL_GL_TEXTURE_WRAP_R:
161 case LOCAL_GL_TEXTURE_MAX_LOD:
162 case LOCAL_GL_TEXTURE_MIN_LOD:
163 return true;
165 default:
166 return WebGLContext::IsTexParamValid(pname);
170 } // namespace mozilla