2014-05-15 20:53:43 +00:00
|
|
|
//
|
2015-05-19 01:04:38 +00:00
|
|
|
// Copyright 2015 Pixar
|
2014-05-15 20:53:43 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
|
|
|
// with the following modification; you may not use this file except in
|
|
|
|
// compliance with the Apache License and the following modification to it:
|
|
|
|
// Section 6. Trademarks. is deleted and replaced with:
|
|
|
|
//
|
|
|
|
// 6. Trademarks. This License does not grant permission to use the trade
|
|
|
|
// names, trademarks, service marks, or product names of the Licensor
|
|
|
|
// and its affiliates, except as required to comply with Section 4(c) of
|
|
|
|
// the License and to reproduce the content of the NOTICE file.
|
|
|
|
//
|
|
|
|
// You may obtain a copy of the Apache License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the Apache License with the above modification is
|
|
|
|
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
// KIND, either express or implied. See the Apache License for the specific
|
|
|
|
// language governing permissions and limitations under the Apache License.
|
|
|
|
//
|
|
|
|
|
2015-05-03 02:54:26 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2015-05-19 01:04:38 +00:00
|
|
|
#include "glUtils.h"
|
2014-05-15 20:53:43 +00:00
|
|
|
|
2015-05-19 01:04:38 +00:00
|
|
|
namespace GLUtils {
|
2014-05-15 20:53:43 +00:00
|
|
|
|
2015-05-19 01:04:38 +00:00
|
|
|
void
|
|
|
|
CheckGLErrors(std::string const & where) {
|
2014-05-15 20:53:43 +00:00
|
|
|
GLuint err;
|
|
|
|
while ((err = glGetError()) != GL_NO_ERROR) {
|
|
|
|
std::cerr << "GL error: "
|
|
|
|
<< (where.empty() ? "" : where + " ")
|
|
|
|
<< err << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint
|
2015-05-19 01:04:38 +00:00
|
|
|
CompileShader(GLenum shaderType, const char *source) {
|
2014-05-15 20:53:43 +00:00
|
|
|
GLuint shader = glCreateShader(shaderType);
|
|
|
|
glShaderSource(shader, 1, &source, NULL);
|
|
|
|
glCompileShader(shader);
|
|
|
|
|
|
|
|
GLint status;
|
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
|
|
|
|
if (status == GL_FALSE) {
|
|
|
|
GLchar emsg[40960];
|
|
|
|
glGetShaderInfoLog(shader, sizeof(emsg), 0, emsg);
|
|
|
|
fprintf(stderr, "Error compiling GLSL shader: %s\n", emsg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2015-05-19 01:04:38 +00:00
|
|
|
bool
|
|
|
|
SupportsAdaptiveTessellation() {
|
|
|
|
#ifdef OSD_USES_GLEW
|
2015-05-26 21:41:05 +00:00
|
|
|
// XXXtakahito:
|
|
|
|
// glewGetExtension doesn't work if the context is initialized
|
|
|
|
// with core profile, since glGetStrings(GL_EXTENSIONS) return
|
|
|
|
// NULL.
|
|
|
|
//
|
|
|
|
//return glewGetExtension("GL_ARB_tessellation_shader") != NULL;
|
2015-05-26 22:33:05 +00:00
|
|
|
return glewIsSupported("GL_ARB_tessellation_shader") == GL_TRUE;
|
2015-05-19 01:04:38 +00:00
|
|
|
#else
|
|
|
|
#if defined(GL_ARB_tessellation_shader) || defined(GL_VERSION_4_0)
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
2014-05-15 20:53:43 +00:00
|
|
|
|
2015-05-03 02:54:26 +00:00
|
|
|
///Helper function that parses the open gl version string, retrieving the major
|
|
|
|
///and minor version from it.
|
2015-05-23 03:28:37 +00:00
|
|
|
void GetMajorMinorVersion(int *major, int *minor){
|
2015-05-03 02:54:26 +00:00
|
|
|
static bool initialized = false;
|
|
|
|
int _major = -1, _minor = -1;
|
|
|
|
if (!initialized || _major == -1 || _minor == -1){
|
|
|
|
const GLubyte *ver = glGetString(GL_SHADING_LANGUAGE_VERSION);
|
|
|
|
if (!ver){
|
|
|
|
_major = -1;
|
|
|
|
_minor = -1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
std::string major_str(ver, ver + 1);
|
|
|
|
std::string minor_str(ver + 2, ver + 3);
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << major_str << " " << minor_str;
|
|
|
|
ss >> _major;
|
|
|
|
ss >> _minor;
|
|
|
|
}
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
*major = _major;
|
|
|
|
*minor = _minor;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets the shader version based on the current opengl version and returns
|
|
|
|
* it in a string form */
|
|
|
|
|
2015-05-23 03:28:37 +00:00
|
|
|
const std::string &GetShaderVersion(){
|
2015-05-03 02:54:26 +00:00
|
|
|
static bool initialized = false;
|
|
|
|
static std::string shader_version;
|
|
|
|
if (!initialized){
|
|
|
|
|
|
|
|
int major, minor;
|
2015-05-23 03:28:37 +00:00
|
|
|
GetMajorMinorVersion(&major, &minor);
|
2015-05-03 02:54:26 +00:00
|
|
|
int version_number = major * 10 + minor;
|
|
|
|
switch (version_number){
|
|
|
|
case 20:
|
|
|
|
shader_version = "110";
|
|
|
|
break;
|
|
|
|
case 21:
|
|
|
|
shader_version = "120";
|
|
|
|
break;
|
|
|
|
case 30:
|
|
|
|
shader_version = "130";
|
|
|
|
break;
|
|
|
|
case 31:
|
|
|
|
shader_version = "140";
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
shader_version = "150";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << version_number;
|
|
|
|
shader_version = ss.str() + "0";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
return shader_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generates the version defintion needed by the glsl shaders based on the
|
|
|
|
* opengl string
|
|
|
|
*/
|
2015-05-23 03:28:37 +00:00
|
|
|
const std::string &GetShaderVersionInclude(){
|
2015-05-03 02:54:26 +00:00
|
|
|
static bool initialized = false;
|
|
|
|
static std::string include;
|
|
|
|
if (!initialized){
|
2015-05-23 03:28:37 +00:00
|
|
|
include = "#version " + GetShaderVersion() + "\n";
|
2015-05-03 02:54:26 +00:00
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
return include;
|
|
|
|
}
|
|
|
|
|
2015-05-23 03:28:37 +00:00
|
|
|
bool GL_ARBSeparateShaderObjectsOrGL_VERSION_4_1(){
|
2015-05-03 02:54:26 +00:00
|
|
|
#if defined(OSD_USES_GLEW)
|
|
|
|
bool initialized = false, uses = false;
|
|
|
|
if (!initialized){
|
|
|
|
uses = glewGetExtension("GL_ARB_separate_shader_objects") ||
|
|
|
|
(GLEW_VERSION_4_1
|
|
|
|
&& glewGetExtension("GL_ARB_tessellation_shader"));
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
return uses;
|
|
|
|
#else
|
|
|
|
#if defined(GL_ARB_separate_shader_objects) || defined(GL_VERSION_4_1)
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-05-28 15:43:05 +00:00
|
|
|
bool GL_ARBComputeShaderOrGL_VERSION_4_3() {
|
|
|
|
#if defined(OSD_USES_GLEW)
|
|
|
|
return (glewIsSupported("GL_ARB_compute_shader") == GL_TRUE) ||
|
|
|
|
(GLEW_VERSION_4_3);
|
|
|
|
#else
|
|
|
|
#if defined(GL_ARB_compute_shader) || defined(GL_VERSION_4_3)
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-05-03 02:54:26 +00:00
|
|
|
|
2015-05-19 01:04:38 +00:00
|
|
|
} // namesapce GLUtils
|