• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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/OpenGLVersion.h"
16 
17 #include <cctype>
18 #include <tuple>
19 
20 namespace dawn_native { namespace opengl {
21 
Initialize(GetProcAddress getProc)22     MaybeError OpenGLVersion::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         } else {
40             // OpenGL spec states the GL_VERSION string will be in the following format:
41             // <version number><space><vendor-specific information>
42             // The version number is either of the form major number.minor number or major
43             // number.minor number.release number, where the numbers all have one or more
44             // digits
45             mStandard = Standard::Desktop;
46             mMajorVersion = version[0] - '0';
47             mMinorVersion = version[2] - '0';
48 
49             // The minor version shouldn't get to two digits.
50             ASSERT(version.size() <= 3 || !isdigit(version[3]));
51         }
52 
53         return {};
54     }
55 
IsDesktop() const56     bool OpenGLVersion::IsDesktop() const {
57         return mStandard == Standard::Desktop;
58     }
59 
IsES() const60     bool OpenGLVersion::IsES() const {
61         return mStandard == Standard::ES;
62     }
63 
GetMajor() const64     uint32_t OpenGLVersion::GetMajor() const {
65         return mMajorVersion;
66     }
67 
GetMinor() const68     uint32_t OpenGLVersion::GetMinor() const {
69         return mMinorVersion;
70     }
71 
IsAtLeast(uint32_t majorVersion,uint32_t minorVersion) const72     bool OpenGLVersion::IsAtLeast(uint32_t majorVersion, uint32_t minorVersion) const {
73         return std::tie(mMajorVersion, mMinorVersion) >= std::tie(majorVersion, minorVersion);
74     }
75 
76 }}  // namespace dawn_native::opengl
77