Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/. */
5 :
6 : #include "WebGL2Context.h"
7 :
8 : #include "gfxPrefs.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"
17 :
18 : namespace mozilla {
19 :
20 0 : WebGL2Context::WebGL2Context()
21 0 : : WebGLContext()
22 : {
23 0 : MOZ_ASSERT(IsSupported(), "not supposed to create a WebGL2Context"
24 : "context when not supported");
25 0 : }
26 :
27 0 : WebGL2Context::~WebGL2Context()
28 : {
29 :
30 0 : }
31 :
32 : UniquePtr<webgl::FormatUsageAuthority>
33 0 : WebGL2Context::CreateFormatUsage(gl::GLContext* gl) const
34 : {
35 0 : return webgl::FormatUsageAuthority::CreateForWebGL2(gl);
36 : }
37 :
38 : /*static*/ bool
39 0 : WebGL2Context::IsSupported()
40 : {
41 0 : return gfxPrefs::WebGL2Enabled();
42 : }
43 :
44 : /*static*/ WebGL2Context*
45 0 : WebGL2Context::Create()
46 : {
47 0 : return new WebGL2Context();
48 : }
49 :
50 : JSObject*
51 0 : WebGL2Context::WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto)
52 : {
53 0 : return dom::WebGL2RenderingContextBinding::Wrap(cx, this, givenProto);
54 : }
55 :
56 : ////////////////////////////////////////////////////////////////////////////////
57 : // WebGL 2 initialisation
58 :
59 : static const gl::GLFeature kRequiredFeatures[] = {
60 : gl::GLFeature::blend_minmax,
61 : gl::GLFeature::clear_buffers,
62 : gl::GLFeature::copy_buffer,
63 : gl::GLFeature::depth_texture,
64 : gl::GLFeature::draw_instanced,
65 : gl::GLFeature::draw_range_elements,
66 : gl::GLFeature::element_index_uint,
67 : gl::GLFeature::frag_color_float,
68 : gl::GLFeature::frag_depth,
69 : gl::GLFeature::framebuffer_object,
70 : gl::GLFeature::get_integer_indexed,
71 : gl::GLFeature::get_integer64_indexed,
72 : gl::GLFeature::gpu_shader4,
73 : gl::GLFeature::instanced_arrays,
74 : gl::GLFeature::instanced_non_arrays,
75 : gl::GLFeature::map_buffer_range, // Used by GetBufferSubData.
76 : gl::GLFeature::occlusion_query2,
77 : gl::GLFeature::packed_depth_stencil,
78 : gl::GLFeature::query_objects,
79 : gl::GLFeature::renderbuffer_color_float,
80 : gl::GLFeature::renderbuffer_color_half_float,
81 : gl::GLFeature::sRGB_framebuffer,
82 : gl::GLFeature::sRGB_texture,
83 : gl::GLFeature::sampler_objects,
84 : gl::GLFeature::standard_derivatives,
85 : gl::GLFeature::texture_3D,
86 : gl::GLFeature::texture_3D_compressed,
87 : gl::GLFeature::texture_3D_copy,
88 : gl::GLFeature::texture_float,
89 : gl::GLFeature::texture_half_float,
90 : gl::GLFeature::texture_half_float_linear,
91 : gl::GLFeature::texture_non_power_of_two,
92 : gl::GLFeature::texture_storage,
93 : gl::GLFeature::transform_feedback2,
94 : gl::GLFeature::uniform_buffer_object,
95 : gl::GLFeature::uniform_matrix_nonsquare,
96 : gl::GLFeature::vertex_array_object
97 : };
98 :
99 : bool
100 0 : WebGLContext::InitWebGL2(FailureReason* const out_failReason)
101 : {
102 0 : MOZ_ASSERT(IsWebGL2(), "WebGLContext is not a WebGL 2 context!");
103 :
104 0 : std::vector<gl::GLFeature> missingList;
105 :
106 0 : const auto fnGatherMissing = [&](gl::GLFeature cur) {
107 0 : if (!gl->IsSupported(cur)) {
108 0 : missingList.push_back(cur);
109 : }
110 0 : };
111 :
112 0 : const auto fnGatherMissing2 = [&](gl::GLFeature main, gl::GLFeature alt) {
113 0 : if (!gl->IsSupported(main) && !gl->IsSupported(alt)) {
114 0 : missingList.push_back(main);
115 : }
116 0 : };
117 :
118 : ////
119 :
120 0 : for (const auto& cur : kRequiredFeatures) {
121 0 : fnGatherMissing(cur);
122 : }
123 :
124 : // On desktop, we fake occlusion_query_boolean with occlusion_query if
125 : // necessary. (See WebGL2ContextQueries.cpp)
126 : fnGatherMissing2(gl::GLFeature::occlusion_query_boolean,
127 0 : gl::GLFeature::occlusion_query);
128 :
129 : #ifdef XP_MACOSX
130 : // On OSX, GL core profile is used. This requires texture swizzle
131 : // support to emulate legacy texture formats: ALPHA, LUMINANCE,
132 : // and LUMINANCE_ALPHA.
133 : fnGatherMissing(gl::GLFeature::texture_swizzle);
134 : #endif
135 :
136 : fnGatherMissing2(gl::GLFeature::prim_restart_fixed,
137 0 : gl::GLFeature::prim_restart);
138 :
139 : ////
140 :
141 0 : if (missingList.size()) {
142 0 : nsAutoCString exts;
143 0 : for (auto itr = missingList.begin(); itr != missingList.end(); ++itr) {
144 0 : exts.AppendLiteral("\n ");
145 0 : exts.Append(gl::GLContext::GetFeatureName(*itr));
146 : }
147 :
148 : const nsPrintfCString reason("WebGL 2 requires support for the following"
149 : " features: %s",
150 0 : exts.BeginReading());
151 0 : *out_failReason = FailureReason("FEATURE_FAILURE_WEBGL2_OCCL", reason);
152 0 : return false;
153 : }
154 :
155 : // we initialise WebGL 2 related stuff.
156 0 : gl->GetUIntegerv(LOCAL_GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS,
157 0 : &mGLMaxTransformFeedbackSeparateAttribs);
158 0 : gl->GetUIntegerv(LOCAL_GL_MAX_UNIFORM_BUFFER_BINDINGS,
159 0 : &mGLMaxUniformBufferBindings);
160 :
161 0 : mIndexedUniformBufferBindings.resize(mGLMaxUniformBufferBindings);
162 :
163 0 : mDefaultTransformFeedback = new WebGLTransformFeedback(this, 0);
164 0 : mBoundTransformFeedback = mDefaultTransformFeedback;
165 :
166 0 : gl->fGenTransformFeedbacks(1, &mEmptyTFO);
167 :
168 : ////
169 :
170 0 : if (!gl->IsGLES()) {
171 : // Desktop OpenGL requires the following to be enabled in order to
172 : // support sRGB operations on framebuffers.
173 0 : gl->fEnable(LOCAL_GL_FRAMEBUFFER_SRGB_EXT);
174 : }
175 :
176 0 : if (gl->IsSupported(gl::GLFeature::prim_restart_fixed)) {
177 0 : gl->fEnable(LOCAL_GL_PRIMITIVE_RESTART_FIXED_INDEX);
178 : } else {
179 0 : MOZ_ASSERT(gl->IsSupported(gl::GLFeature::prim_restart));
180 : }
181 :
182 : //////
183 :
184 0 : return true;
185 : }
186 :
187 : } // namespace mozilla
|