1 //
2 //   Copyright 2015 Pixar
3 //
4 //   Licensed under the Apache License, Version 2.0 (the "Apache License")
5 //   with the following modification; you may not use this file except in
6 //   compliance with the Apache License and the following modification to it:
7 //   Section 6. Trademarks. is deleted and replaced with:
8 //
9 //   6. Trademarks. This License does not grant permission to use the trade
10 //      names, trademarks, service marks, or product names of the Licensor
11 //      and its affiliates, except as required to comply with Section 4(c) of
12 //      the License and to reproduce the content of the NOTICE file.
13 //
14 //   You may obtain a copy of the Apache License at
15 //
16 //       http://www.apache.org/licenses/LICENSE-2.0
17 //
18 //   Unless required by applicable law or agreed to in writing, software
19 //   distributed under the Apache License with the above modification is
20 //   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 //   KIND, either express or implied. See the Apache License for the specific
22 //   language governing permissions and limitations under the Apache License.
23 //
24 
25 #include "glLoader.h"
26 
27 #include <sstream>
28 #include <string>
29 #include <cstring>
30 #include <vector>
31 #include "glUtils.h"
32 
33 #define STB_IMAGE_WRITE_IMPLEMENTATION 1
34 #include "stb_image_write.h"
35 
36 #include <GLFW/glfw3.h>
37 
38 #if _MSC_VER
39 #define snprintf _snprintf
40 #endif
41 
42 namespace GLUtils {
43 
InitializeGL()44 void InitializeGL()
45 {
46     OpenSubdiv::internal::GLLoader::applicationInitializeGL();
47 }
48 
49 static void
_argParseBool(bool * ret,const char * lbl,int i,int argc,char ** argv)50 _argParseBool(bool *ret, const char *lbl, int i, int argc, char **argv)
51 {
52     if (i < argc-1) {
53         if (!strcmp(argv[i+1], "on")) {
54             *ret = true;
55         } else if (!strcmp(argv[i+1], "off")) {
56             *ret = false;
57         } else {
58             fprintf(stderr, "Unknown setting for %s: %s\n", lbl, argv[i+1]);
59             exit(1);
60         }
61     } else {
62         fprintf(stderr,
63             "Please specify \"on\" or \"off\" for %s.\n", lbl);
64         exit(1);
65     }
66 }
67 
68 void
SetMinimumGLVersion(int argc,char ** argv)69 SetMinimumGLVersion(int argc, char ** argv) {
70 
71 #if defined(__APPLE__)
72     // Here 3.2 is the minimum GL version supported, GLFW will allocate a
73     // higher version if possible. This works on OS X, but instead limits
74     // the version to 3.2 on Linux. On Linux & Windows, specifying no
75     // version hint should use the highest version available.
76     //
77     // http://www.glfw.org/faq.html#how-do-i-create-an-opengl-30-context
78     // http://www.glfw.org/faq.html#what-versions-of-opengl-are-supported-by-glfw
79     bool coreProfile = true;
80     bool forwardCompat = true;
81     bool versionSet = true;
82     int  major = 3;
83     int  minor = 2;
84 #else
85     bool coreProfile = false;
86     bool forwardCompat = false;
87     bool versionSet = false;
88     int  major = 4;
89     int  minor = 2;
90 #endif
91 
92     for (int i = 1; i < argc; ++i) {
93 
94         if (!strcmp(argv[i], "-glCoreProfile")) {
95             _argParseBool(&coreProfile, argv[i], i, argc, argv);
96         }
97         if (!strcmp(argv[i], "-glForwardCompat")) {
98             _argParseBool(&forwardCompat, argv[i], i, argc, argv);
99         }
100         if (!strcmp(argv[i], "-glVersion")) {
101             if (i < argc-1) {
102                 char *versionStr = argv[i+1];
103                 size_t len = strlen(versionStr);
104                 if (len == 3 && versionStr[1] == '.' &&
105                     versionStr[0] >= '0' && versionStr[0] <= '9' &&
106                     versionStr[2] >= '0' && versionStr[2] <= '9') {
107 
108                     major = versionStr[0] - '0';
109                     minor = versionStr[2] - '0';
110                     versionSet = true;
111 
112                 } else {
113                     fprintf(stderr,
114                         "Invalid version number: %s, please specify a number "
115                         "in the format M.n, e.g., -glVersion 4.2.\n",
116                         versionStr);
117                     exit(1);
118                 }
119             } else {
120                 fprintf(stderr,
121                     "Please specify a version number for glVersion "
122                     "in the form M.n, e.g., -glVersion 4.2.\n");
123                 exit(1);
124             }
125         }
126     }
127 
128     if (coreProfile) {
129         glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
130     }
131 
132     if (forwardCompat) {
133         glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
134     }
135 
136     if (versionSet) {
137         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
138         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
139     }
140 }
141 
142 void
PrintGLVersion()143 PrintGLVersion() {
144     std::cout << glGetString(GL_VENDOR) << "\n";
145     std::cout << glGetString(GL_RENDERER) << "\n";
146     std::cout << glGetString(GL_VERSION) << "\n";
147 
148     int i = -1;
149     std::cout << "Init OpenGL ";
150     glGetIntegerv(GL_MAJOR_VERSION, &i);
151     std::cout << i << ".";
152     glGetIntegerv(GL_MINOR_VERSION, &i);
153     std::cout << i << "\n";
154 
155     CheckGLErrors("PrintGLVersion");
156 }
157 
158 void
CheckGLErrors(std::string const & where)159 CheckGLErrors(std::string const & where) {
160     GLuint err;
161     while ((err = glGetError()) != GL_NO_ERROR) {
162         std::cerr << "GL error: "
163                   << (where.empty() ? "" : where + " ")
164                   << err << "\n";
165     }
166 }
167 
168 GLuint
CompileShader(GLenum shaderType,const char * source)169 CompileShader(GLenum shaderType, const char *source) {
170     GLuint shader = glCreateShader(shaderType);
171     glShaderSource(shader, 1, &source, NULL);
172     glCompileShader(shader);
173 
174     GLint status;
175     glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
176     if (status == GL_FALSE) {
177         GLchar emsg[40960];
178         glGetShaderInfoLog(shader, sizeof(emsg), 0, emsg);
179         fprintf(stderr, "Error compiling GLSL shader: %s\n", emsg);
180         return 0;
181     }
182 
183     return shader;
184 }
185 
186 void
WriteScreenshot(int width,int height)187 WriteScreenshot(int width, int height) {
188 
189     std::vector<unsigned char> data(width*height*4 /*RGBA*/);
190 
191     glPixelStorei(GL_PACK_ALIGNMENT, 1);
192     glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, &data[0]);
193 
194     static int counter=0;
195     char fname[64];
196     snprintf(fname, 64, "screenshot.%d.png", counter++);
197 
198     // flip vertical
199     stbi_write_png(fname, width, height, 4, &data[width*4*(height-1)], -width*4);
200 
201     fprintf(stdout, "Saved %s\n", fname);
202 }
203 
GetMajorMinorVersion(int * major,int * minor)204 void GetMajorMinorVersion(int *major, int *minor){
205     const GLubyte *ver = glGetString(GL_SHADING_LANGUAGE_VERSION);
206     if (!ver){
207         *major = -1;
208         *minor = -1;
209     }
210     else{
211         std::stringstream ss;
212         ss << std::string(ver, ver + 1) << " " << std::string(ver + 2, ver + 3);
213         ss >> *major;
214         ss >> *minor;
215     }
216 }
217 
218 std::string
GetShaderVersion()219 GetShaderVersion(){
220     std::string shader_version;
221     int major, minor;
222     GetMajorMinorVersion(&major, &minor);
223     int version_number = major * 10 + minor;
224     switch (version_number){
225     case 20:
226         shader_version = "110";
227         break;
228     case 21:
229         shader_version = "120";
230         break;
231     case 30:
232         shader_version = "130";
233         break;
234     case 31:
235         shader_version = "140";
236         break;
237     case 32:
238         shader_version = "150";
239         break;
240     default:
241         std::stringstream ss;
242         ss << version_number;
243         shader_version = ss.str() + "0";
244         break;
245     }
246     return shader_version;
247 }
248 
249 // Generates the version definition needed by the glsl shaders based on the
250 // opengl string
GetShaderVersionInclude()251 std::string GetShaderVersionInclude(){
252     return "#version " + GetShaderVersion() + "\n";
253 }
254 
SupportsAdaptiveTessellation()255 bool SupportsAdaptiveTessellation() {
256 #if defined(GL_VERSION_4_0)
257     if (OSD_OPENGL_HAS(VERSION_4_0)) {
258         return true;
259     }
260 #endif
261 #if defined(GL_ARB_tessellation_shader)
262     if (OSD_OPENGL_HAS(ARB_tessellation_shader)) {
263         return true;
264     }
265 #endif
266     return false;
267 }
268 
GL_ARBSeparateShaderObjectsOrGL_VERSION_4_1()269 bool GL_ARBSeparateShaderObjectsOrGL_VERSION_4_1() {
270 #if defined(GL_VERSION_4_1)
271     if (OSD_OPENGL_HAS(VERSION_4_1)) {
272         return true;
273     }
274 #endif
275 #if defined(GL_ARB_separate_shader_objects)
276     if (OSD_OPENGL_HAS(ARB_separate_shader_objects)) {
277         return true;
278     }
279 #endif
280     return false;
281 }
282 
GL_ARBComputeShaderOrGL_VERSION_4_3()283 bool GL_ARBComputeShaderOrGL_VERSION_4_3() {
284 #if defined(GL_VERSION_4_3)
285     if (OSD_OPENGL_HAS(VERSION_4_3)) {
286         return true;
287     }
288 #endif
289 #if defined(GL_ARB_compute_shader)
290     if (OSD_OPENGL_HAS(ARB_compute_shader)) {
291         return true;
292     }
293 #endif
294     return false;
295 }
296 
297 #undef IS_SUPPORTED
298 
299 }   // namespace GLUtils
300