2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Copyright 2013 Pixar
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +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:
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// 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.
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// You may obtain a copy of the Apache License at
|
2013-07-18 21:19:50 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2013-07-18 21:19:50 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// 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.
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "../osd/glDrawRegistry.h"
|
|
|
|
#include "../osd/error.h"
|
|
|
|
|
2013-06-10 22:54:40 +00:00
|
|
|
#include "../osd/opengl.h"
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace OpenSubdiv {
|
|
|
|
namespace OPENSUBDIV_VERSION {
|
|
|
|
|
|
|
|
OsdGLDrawConfig::~OsdGLDrawConfig()
|
|
|
|
{
|
|
|
|
glDeleteProgram(program);
|
|
|
|
}
|
|
|
|
|
2012-12-20 18:25:41 +00:00
|
|
|
#if defined(GL_ARB_tessellation_shader) || defined(GL_VERSION_4_0)
|
2012-12-11 01:15:13 +00:00
|
|
|
static const char *commonShaderSource =
|
2013-12-24 06:30:34 +00:00
|
|
|
#include "glslPatchCommon.gen.h"
|
2012-12-11 01:15:13 +00:00
|
|
|
;
|
2013-11-20 23:47:59 +00:00
|
|
|
static const char *ptexShaderSource =
|
2013-12-24 06:30:34 +00:00
|
|
|
#include "glslPtexCommon.gen.h"
|
2013-11-20 23:47:59 +00:00
|
|
|
;
|
2013-06-24 19:03:57 +00:00
|
|
|
static const char *bsplineShaderSource =
|
2013-12-24 06:30:34 +00:00
|
|
|
#include "glslPatchBSpline.gen.h"
|
2012-12-11 01:15:13 +00:00
|
|
|
;
|
|
|
|
static const char *gregoryShaderSource =
|
2013-12-24 06:30:34 +00:00
|
|
|
#include "glslPatchGregory.gen.h"
|
2012-12-11 01:15:13 +00:00
|
|
|
;
|
|
|
|
static const char *transitionShaderSource =
|
2013-12-24 06:30:34 +00:00
|
|
|
#include "glslPatchTransition.gen.h"
|
2012-12-11 01:15:13 +00:00
|
|
|
;
|
2012-12-20 18:25:41 +00:00
|
|
|
#endif
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
OsdGLDrawRegistryBase::~OsdGLDrawRegistryBase() {}
|
|
|
|
|
|
|
|
OsdGLDrawSourceConfig *
|
2013-11-20 23:47:59 +00:00
|
|
|
OsdGLDrawRegistryBase::_CreateDrawSourceConfig(
|
|
|
|
OsdDrawContext::PatchDescriptor const & desc)
|
2012-12-11 01:15:13 +00:00
|
|
|
{
|
|
|
|
OsdGLDrawSourceConfig * sconfig = _NewDrawSourceConfig();
|
|
|
|
|
2012-12-20 18:25:41 +00:00
|
|
|
#if defined(GL_ARB_tessellation_shader) || defined(GL_VERSION_4_0)
|
2012-12-11 01:15:13 +00:00
|
|
|
sconfig->commonShader.source = commonShaderSource;
|
2013-11-20 23:47:59 +00:00
|
|
|
|
|
|
|
if (IsPtexEnabled()) {
|
|
|
|
sconfig->commonShader.source += ptexShaderSource;
|
|
|
|
}
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
2013-05-09 16:23:01 +00:00
|
|
|
ss << (int)desc.GetMaxValence();
|
2012-12-11 01:15:13 +00:00
|
|
|
sconfig->commonShader.AddDefine("OSD_MAX_VALENCE", ss.str());
|
|
|
|
ss.str("");
|
2013-05-09 16:23:01 +00:00
|
|
|
ss << (int)desc.GetNumElements();
|
2012-12-11 01:15:13 +00:00
|
|
|
sconfig->commonShader.AddDefine("OSD_NUM_ELEMENTS", ss.str());
|
|
|
|
}
|
|
|
|
|
2013-05-09 16:23:01 +00:00
|
|
|
if (desc.GetPattern() == FarPatchTables::NON_TRANSITION) {
|
|
|
|
switch (desc.GetType()) {
|
|
|
|
case FarPatchTables::REGULAR:
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->vertexShader.source = bsplineShaderSource;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->vertexShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->vertexShader.AddDefine("OSD_PATCH_VERTEX_BSPLINE_SHADER");
|
|
|
|
sconfig->tessControlShader.source = bsplineShaderSource;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->tessControlShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessControlShader.AddDefine("OSD_PATCH_TESS_CONTROL_BSPLINE_SHADER");
|
|
|
|
sconfig->tessEvalShader.source = bsplineShaderSource;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->tessEvalShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessEvalShader.AddDefine("OSD_PATCH_TESS_EVAL_BSPLINE_SHADER");
|
2013-05-09 16:23:01 +00:00
|
|
|
break;
|
|
|
|
case FarPatchTables::BOUNDARY:
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->vertexShader.source = bsplineShaderSource;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->vertexShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->vertexShader.AddDefine("OSD_PATCH_VERTEX_BSPLINE_SHADER");
|
|
|
|
sconfig->tessControlShader.source = bsplineShaderSource;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->tessControlShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessControlShader.AddDefine("OSD_PATCH_TESS_CONTROL_BSPLINE_SHADER");
|
|
|
|
sconfig->tessControlShader.AddDefine("OSD_PATCH_BOUNDARY");
|
|
|
|
sconfig->tessEvalShader.source = bsplineShaderSource;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->tessEvalShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessEvalShader.AddDefine("OSD_PATCH_TESS_EVAL_BSPLINE_SHADER");
|
2013-05-09 16:23:01 +00:00
|
|
|
break;
|
|
|
|
case FarPatchTables::CORNER:
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->vertexShader.source = bsplineShaderSource;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->vertexShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->vertexShader.AddDefine("OSD_PATCH_VERTEX_BSPLINE_SHADER");
|
|
|
|
sconfig->tessControlShader.source = bsplineShaderSource;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->tessControlShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessControlShader.AddDefine("OSD_PATCH_TESS_CONTROL_BSPLINE_SHADER");
|
|
|
|
sconfig->tessControlShader.AddDefine("OSD_PATCH_CORNER");
|
|
|
|
sconfig->tessEvalShader.source = bsplineShaderSource;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->tessEvalShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessEvalShader.AddDefine("OSD_PATCH_TESS_EVAL_BSPLINE_SHADER");
|
2013-05-09 16:23:01 +00:00
|
|
|
break;
|
|
|
|
case FarPatchTables::GREGORY:
|
|
|
|
sconfig->vertexShader.source = gregoryShaderSource;
|
|
|
|
sconfig->vertexShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->vertexShader.AddDefine("OSD_PATCH_VERTEX_GREGORY_SHADER");
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->tessControlShader.source = gregoryShaderSource;
|
|
|
|
sconfig->tessControlShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessControlShader.AddDefine("OSD_PATCH_TESS_CONTROL_GREGORY_SHADER");
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->tessEvalShader.source = gregoryShaderSource;
|
|
|
|
sconfig->tessEvalShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessEvalShader.AddDefine("OSD_PATCH_TESS_EVAL_GREGORY_SHADER");
|
2013-05-09 16:23:01 +00:00
|
|
|
break;
|
|
|
|
case FarPatchTables::GREGORY_BOUNDARY:
|
2013-07-01 18:58:28 +00:00
|
|
|
sconfig->vertexShader.source = gregoryShaderSource;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->vertexShader.version = "#version 410\n";
|
2013-07-01 18:58:28 +00:00
|
|
|
sconfig->vertexShader.AddDefine("OSD_PATCH_VERTEX_GREGORY_SHADER");
|
|
|
|
sconfig->vertexShader.AddDefine("OSD_PATCH_GREGORY_BOUNDARY");
|
|
|
|
sconfig->tessControlShader.source = gregoryShaderSource;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->tessControlShader.version = "#version 410\n";
|
2013-07-01 18:58:28 +00:00
|
|
|
sconfig->tessControlShader.AddDefine("OSD_PATCH_TESS_CONTROL_GREGORY_SHADER");
|
|
|
|
sconfig->tessControlShader.AddDefine("OSD_PATCH_GREGORY_BOUNDARY");
|
|
|
|
sconfig->tessEvalShader.source = gregoryShaderSource;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->tessEvalShader.version = "#version 410\n";
|
2013-07-01 18:58:28 +00:00
|
|
|
sconfig->tessEvalShader.AddDefine("OSD_PATCH_TESS_EVAL_GREGORY_SHADER");
|
|
|
|
sconfig->tessEvalShader.AddDefine("OSD_PATCH_GREGORY_BOUNDARY");
|
2013-05-09 16:23:01 +00:00
|
|
|
break;
|
2013-11-08 18:46:41 +00:00
|
|
|
default: // POINTS, LINES, QUADS, TRIANGLES
|
|
|
|
// do nothing
|
2013-05-09 16:23:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else { // pattern != NON_TRANSITION
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->vertexShader.source = bsplineShaderSource;
|
2012-12-11 01:15:13 +00:00
|
|
|
sconfig->vertexShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->vertexShader.AddDefine("OSD_PATCH_VERTEX_BSPLINE_SHADER");
|
|
|
|
sconfig->tessControlShader.source =
|
|
|
|
std::string(transitionShaderSource) + bsplineShaderSource;
|
2012-12-11 01:15:13 +00:00
|
|
|
sconfig->tessControlShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessControlShader.AddDefine("OSD_PATCH_TESS_CONTROL_BSPLINE_SHADER");
|
|
|
|
sconfig->tessControlShader.AddDefine("OSD_PATCH_TRANSITION");
|
|
|
|
sconfig->tessEvalShader.source =
|
|
|
|
std::string(transitionShaderSource) + bsplineShaderSource;
|
2012-12-11 01:15:13 +00:00
|
|
|
sconfig->tessEvalShader.version = "#version 410\n";
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessEvalShader.AddDefine("OSD_PATCH_TESS_EVAL_BSPLINE_SHADER");
|
|
|
|
sconfig->tessEvalShader.AddDefine("OSD_PATCH_TRANSITION");
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2013-05-09 16:23:01 +00:00
|
|
|
int pattern = desc.GetPattern() - 1;
|
|
|
|
int rotation = desc.GetRotation();
|
|
|
|
int subpatch = desc.GetSubPatch();
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2013-05-09 16:23:01 +00:00
|
|
|
std::ostringstream ss;
|
2013-06-24 19:03:57 +00:00
|
|
|
ss << "OSD_TRANSITION_PATTERN" << pattern << subpatch;
|
2013-05-09 16:23:01 +00:00
|
|
|
sconfig->tessControlShader.AddDefine(ss.str());
|
|
|
|
sconfig->tessEvalShader.AddDefine(ss.str());
|
|
|
|
|
|
|
|
ss.str("");
|
|
|
|
ss << rotation;
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessControlShader.AddDefine("OSD_TRANSITION_ROTATE", ss.str());
|
|
|
|
sconfig->tessEvalShader.AddDefine("OSD_TRANSITION_ROTATE", ss.str());
|
2013-05-09 16:23:01 +00:00
|
|
|
|
|
|
|
if (desc.GetType() == FarPatchTables::BOUNDARY) {
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessControlShader.AddDefine("OSD_PATCH_BOUNDARY");
|
2013-05-09 16:23:01 +00:00
|
|
|
} else if (desc.GetType() == FarPatchTables::CORNER) {
|
2013-06-24 19:03:57 +00:00
|
|
|
sconfig->tessControlShader.AddDefine("OSD_PATCH_CORNER");
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-20 18:25:41 +00:00
|
|
|
#endif
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
return sconfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GLuint
|
|
|
|
_CompileShader(
|
|
|
|
GLenum shaderType,
|
|
|
|
OpenSubdiv::OsdDrawShaderSource const & common,
|
|
|
|
OpenSubdiv::OsdDrawShaderSource const & source)
|
|
|
|
{
|
|
|
|
const char *sources[4];
|
|
|
|
std::stringstream definitions;
|
|
|
|
for (int i=0; i<(int)common.defines.size(); ++i) {
|
|
|
|
definitions << "#define "
|
|
|
|
<< common.defines[i].first << " "
|
|
|
|
<< common.defines[i].second << "\n";
|
|
|
|
}
|
|
|
|
for (int i=0; i<(int)source.defines.size(); ++i) {
|
|
|
|
definitions << "#define "
|
|
|
|
<< source.defines[i].first << " "
|
|
|
|
<< source.defines[i].second << "\n";
|
|
|
|
}
|
|
|
|
std::string defString = definitions.str();
|
|
|
|
|
2013-01-29 23:54:18 +00:00
|
|
|
sources[0] = source.version.c_str();
|
|
|
|
sources[1] = defString.c_str();
|
2012-12-11 01:15:13 +00:00
|
|
|
sources[2] = common.source.c_str();
|
|
|
|
sources[3] = source.source.c_str();
|
|
|
|
|
|
|
|
GLuint shader = glCreateShader(shaderType);
|
|
|
|
glShaderSource(shader, 4, sources, NULL);
|
|
|
|
glCompileShader(shader);
|
|
|
|
|
|
|
|
GLint status;
|
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
|
|
|
|
if( status == GL_FALSE ) {
|
|
|
|
GLint infoLogLength;
|
|
|
|
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
|
|
|
|
char * infoLog = new char[infoLogLength];
|
|
|
|
glGetShaderInfoLog(shader, infoLogLength, NULL, infoLog);
|
|
|
|
OsdError(OSD_GLSL_COMPILE_ERROR,
|
|
|
|
"Error compiling GLSL shader: %s\nDefines: \n%s\n",
|
|
|
|
infoLog, defString.c_str());
|
|
|
|
delete[] infoLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
OsdGLDrawConfig *
|
|
|
|
OsdGLDrawRegistryBase::_CreateDrawConfig(
|
2013-05-09 20:14:02 +00:00
|
|
|
OsdDrawContext::PatchDescriptor const & desc,
|
2012-12-11 01:15:13 +00:00
|
|
|
OsdGLDrawSourceConfig const * sconfig)
|
|
|
|
{
|
|
|
|
assert(sconfig);
|
|
|
|
|
|
|
|
OsdGLDrawConfig * config = _NewDrawConfig();
|
|
|
|
assert(config);
|
|
|
|
|
|
|
|
GLuint vertexShader =
|
|
|
|
sconfig->vertexShader.source.empty() ? 0 :
|
|
|
|
_CompileShader(GL_VERTEX_SHADER,
|
|
|
|
sconfig->commonShader, sconfig->vertexShader);
|
|
|
|
|
|
|
|
GLuint tessControlShader = 0;
|
|
|
|
GLuint tessEvalShader = 0;
|
|
|
|
|
|
|
|
#if defined(GL_ARB_tessellation_shader) || defined(GL_VERSION_4_0)
|
|
|
|
tessControlShader =
|
|
|
|
sconfig->tessControlShader.source.empty() ? 0 :
|
|
|
|
_CompileShader(GL_TESS_CONTROL_SHADER,
|
|
|
|
sconfig->commonShader, sconfig->tessControlShader);
|
|
|
|
|
|
|
|
tessEvalShader =
|
|
|
|
sconfig->tessEvalShader.source.empty() ? 0 :
|
|
|
|
_CompileShader(GL_TESS_EVALUATION_SHADER,
|
|
|
|
sconfig->commonShader, sconfig->tessEvalShader);
|
|
|
|
#endif
|
|
|
|
|
2012-12-20 18:25:41 +00:00
|
|
|
GLuint geometryShader = 0;
|
|
|
|
|
|
|
|
#if defined(GL_ARB_geometry_shader4) || defined(GL_VERSION_3_1)
|
|
|
|
geometryShader =
|
2012-12-11 01:15:13 +00:00
|
|
|
sconfig->geometryShader.source.empty() ? 0 :
|
|
|
|
_CompileShader(GL_GEOMETRY_SHADER,
|
|
|
|
sconfig->commonShader, sconfig->geometryShader);
|
2012-12-20 18:25:41 +00:00
|
|
|
#endif
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
GLuint fragmentShader =
|
|
|
|
sconfig->fragmentShader.source.empty() ? 0 :
|
|
|
|
_CompileShader(GL_FRAGMENT_SHADER,
|
|
|
|
sconfig->commonShader, sconfig->fragmentShader);
|
|
|
|
|
|
|
|
GLuint program = glCreateProgram();
|
|
|
|
if (vertexShader) glAttachShader(program, vertexShader);
|
|
|
|
if (tessControlShader) glAttachShader(program, tessControlShader);
|
|
|
|
if (tessEvalShader) glAttachShader(program, tessEvalShader);
|
|
|
|
if (geometryShader) glAttachShader(program, geometryShader);
|
|
|
|
if (fragmentShader) glAttachShader(program, fragmentShader);
|
|
|
|
|
|
|
|
glLinkProgram(program);
|
|
|
|
|
|
|
|
glDeleteShader(vertexShader);
|
|
|
|
glDeleteShader(tessControlShader);
|
|
|
|
glDeleteShader(tessEvalShader);
|
|
|
|
glDeleteShader(geometryShader);
|
|
|
|
glDeleteShader(fragmentShader);
|
|
|
|
|
|
|
|
GLint status;
|
|
|
|
glGetProgramiv(program, GL_LINK_STATUS, &status );
|
|
|
|
if( status == GL_FALSE ) {
|
|
|
|
GLint infoLogLength;
|
|
|
|
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
|
|
|
|
char * infoLog = new char[infoLogLength];
|
|
|
|
glGetProgramInfoLog(program, infoLogLength, NULL, infoLog);
|
2013-06-19 19:59:43 +00:00
|
|
|
OsdError(OSD_GLSL_LINK_ERROR,
|
2012-12-11 01:15:13 +00:00
|
|
|
"Error linking GLSL program: %s\n", infoLog);
|
|
|
|
delete[] infoLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
config->program = program;
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace OPENSUBDIV_VERSION
|
|
|
|
} // end namespace OpenSubdiv
|