1 // Copyright 2019 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "dawn_native/opengl/OpenGLFunctions.h"
16 
17 #include <cctype>
18 #include <tuple>
19 
20 namespace dawn_native { namespace opengl {
21 
Initialize(GetProcAddress getProc)22     MaybeError OpenGLFunctions::Initialize(GetProcAddress getProc) {
23         PFNGLGETSTRINGPROC getString = reinterpret_cast<PFNGLGETSTRINGPROC>(getProc("glGetString"));
24         if (getString == nullptr) {
25             return DAWN_INTERNAL_ERROR("Couldn't load glGetString");
26         }
27 
28         std::string version = reinterpret_cast<const char*>(getString(GL_VERSION));
29 
30         if (version.find("OpenGL ES") != std::string::npos) {
31             // ES spec states that the GL_VERSION string will be in the following format:
32             // "OpenGL ES N.M vendor-specific information"
33             mStandard = Standard::ES;
34             mMajorVersion = version[10] - '0';
35             mMinorVersion = version[12] - '0';
36 
37             // The minor version shouldn't get to two digits.
38             ASSERT(version.size() <= 13 || !isdigit(version[13]));
39 
40             DAWN_TRY(LoadOpenGLESProcs(getProc, mMajorVersion, mMinorVersion));
41         } else {
42             // OpenGL spec states the GL_VERSION string will be in the following format:
43             // <version number><space><vendor-specific information>
44             // The version number is either of the form major number.minor number or major
45             // number.minor number.release number, where the numbers all have one or more
46             // digits
47             mStandard = Standard::Desktop;
48             mMajorVersion = version[0] - '0';
49             mMinorVersion = version[2] - '0';
50 
51             // The minor version shouldn't get to two digits.
52             ASSERT(version.size() <= 3 || !isdigit(version[3]));
53 
54             DAWN_TRY(LoadDesktopGLProcs(getProc, mMajorVersion, mMinorVersion));
55         }
56 
57         InitializeSupportedGLExtensions();
58 
59         return {};
60     }
61 
InitializeSupportedGLExtensions()62     void OpenGLFunctions::InitializeSupportedGLExtensions() {
63         int32_t numExtensions;
64         GetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
65 
66         for (int32_t i = 0; i < numExtensions; ++i) {
67             const char* extensionName = reinterpret_cast<const char*>(GetStringi(GL_EXTENSIONS, i));
68             mSupportedGLExtensionsSet.insert(extensionName);
69         }
70     }
71 
IsGLExtensionSupported(const char * extension) const72     bool OpenGLFunctions::IsGLExtensionSupported(const char* extension) const {
73         ASSERT(extension != nullptr);
74         return mSupportedGLExtensionsSet.count(extension) != 0;
75     }
76 
IsAtLeastGL(uint32_t majorVersion,uint32_t minorVersion) const77     bool OpenGLFunctions::IsAtLeastGL(uint32_t majorVersion, uint32_t minorVersion) const {
78         return mStandard == Standard::Desktop &&
79                std::tie(mMajorVersion, mMinorVersion) >= std::tie(majorVersion, minorVersion);
80     }
81 
IsAtLeastGLES(uint32_t majorVersion,uint32_t minorVersion) const82     bool OpenGLFunctions::IsAtLeastGLES(uint32_t majorVersion, uint32_t minorVersion) const {
83         return mStandard == Standard::ES &&
84                std::tie(mMajorVersion, mMinorVersion) >= std::tie(majorVersion, minorVersion);
85     }
86 
87 }}  // namespace dawn_native::opengl
88