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
|
2012-12-11 01:15:13 +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 <stdio.h>
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../osd/debug.h"
|
|
|
|
#include "../osd/error.h"
|
|
|
|
#include "../osd/glslKernelBundle.h"
|
2013-05-07 22:25:49 +00:00
|
|
|
#include "../osd/vertex.h"
|
|
|
|
|
|
|
|
#include "../far/subdivisionTables.h"
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2013-06-10 22:54:40 +00:00
|
|
|
#include "../osd/opengl.h"
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace OpenSubdiv {
|
|
|
|
namespace OPENSUBDIV_VERSION {
|
|
|
|
|
|
|
|
static const char *shaderSource =
|
2013-12-24 06:30:34 +00:00
|
|
|
#include "../osd/glslComputeKernel.gen.h"
|
2012-12-11 01:15:13 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
OsdGLSLComputeKernelBundle::OsdGLSLComputeKernelBundle()
|
|
|
|
: _program(0) {
|
|
|
|
|
|
|
|
// XXX: too rough!
|
|
|
|
_workGroupSize = 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
OsdGLSLComputeKernelBundle::~OsdGLSLComputeKernelBundle() {
|
|
|
|
if (_program)
|
|
|
|
glDeleteProgram(_program);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-17 02:53:49 +00:00
|
|
|
OsdGLSLComputeKernelBundle::Compile(int numVertexElements, int numVaryingElements) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2013-05-17 02:53:49 +00:00
|
|
|
_vdesc.Set(numVertexElements, numVaryingElements );
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
if (_program) {
|
|
|
|
glDeleteProgram(_program);
|
|
|
|
_program = 0;
|
|
|
|
}
|
|
|
|
_program = glCreateProgram();
|
|
|
|
|
|
|
|
GLuint shader = glCreateShader(GL_COMPUTE_SHADER);
|
|
|
|
|
|
|
|
char constantDefine[256];
|
|
|
|
snprintf(constantDefine, 256,
|
|
|
|
"#define NUM_VERTEX_ELEMENTS %d\n"
|
|
|
|
"#define NUM_VARYING_ELEMENTS %d\n"
|
|
|
|
"#define WORK_GROUP_SIZE %d\n",
|
|
|
|
numVertexElements, numVaryingElements, _workGroupSize);
|
|
|
|
|
|
|
|
const char *shaderSources[3];
|
|
|
|
shaderSources[0] = constantDefine;
|
|
|
|
shaderSources[1] = shaderSource;
|
|
|
|
glShaderSource(shader, 2, shaderSources, NULL);
|
|
|
|
glCompileShader(shader);
|
|
|
|
glAttachShader(_program, shader);
|
|
|
|
|
|
|
|
GLint linked = 0;
|
|
|
|
glLinkProgram(_program);
|
|
|
|
glGetProgramiv(_program, GL_LINK_STATUS, &linked);
|
|
|
|
|
|
|
|
if (linked == GL_FALSE) {
|
|
|
|
char buffer[1024];
|
|
|
|
glGetShaderInfoLog(shader, 1024, NULL, buffer);
|
|
|
|
OsdError(OSD_GLSL_LINK_ERROR, buffer);
|
|
|
|
|
|
|
|
glGetProgramInfoLog(_program, 1024, NULL, buffer);
|
|
|
|
OsdError(OSD_GLSL_LINK_ERROR, buffer);
|
|
|
|
|
|
|
|
glDeleteProgram(_program);
|
|
|
|
_program = 0;
|
|
|
|
// XXX ERROR HANDLE
|
|
|
|
printf("%s\n", constantDefine);
|
|
|
|
assert(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
glDeleteShader(shader);
|
|
|
|
|
|
|
|
_subComputeFace = glGetSubroutineIndex(_program,
|
|
|
|
GL_COMPUTE_SHADER,
|
|
|
|
"catmarkComputeFace");
|
|
|
|
_subComputeEdge = glGetSubroutineIndex(_program,
|
|
|
|
GL_COMPUTE_SHADER,
|
|
|
|
"catmarkComputeEdge");
|
|
|
|
_subComputeBilinearEdge = glGetSubroutineIndex(_program,
|
|
|
|
GL_COMPUTE_SHADER,
|
|
|
|
"bilinearComputeEdge");
|
|
|
|
_subComputeVertex = glGetSubroutineIndex(_program,
|
|
|
|
GL_COMPUTE_SHADER,
|
|
|
|
"bilinearComputeVertex");
|
|
|
|
_subComputeVertexA = glGetSubroutineIndex(_program,
|
|
|
|
GL_COMPUTE_SHADER,
|
|
|
|
"catmarkComputeVertexA");
|
|
|
|
_subComputeCatmarkVertexB = glGetSubroutineIndex(_program,
|
|
|
|
GL_COMPUTE_SHADER,
|
|
|
|
"catmarkComputeVertexB");
|
|
|
|
_subComputeLoopVertexB = glGetSubroutineIndex(_program,
|
|
|
|
GL_COMPUTE_SHADER,
|
|
|
|
"loopComputeVertexB");
|
|
|
|
|
|
|
|
// set uniform locations for compute
|
2013-03-08 01:50:15 +00:00
|
|
|
_uniformVertexPass = glGetUniformLocation(_program, "vertexPass");
|
|
|
|
_uniformVertexOffset = glGetUniformLocation(_program, "vertexOffset");
|
|
|
|
_uniformTableOffset = glGetUniformLocation(_program, "tableOffset");
|
|
|
|
_uniformIndexStart = glGetUniformLocation(_program, "indexStart");
|
|
|
|
_uniformIndexEnd = glGetUniformLocation(_program, "indexEnd");
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2014-03-19 18:44:51 +00:00
|
|
|
_tableUniforms[FarSubdivisionTables::F_IT] = glGetUniformLocation(_program, "_F0_IT");
|
|
|
|
_tableUniforms[FarSubdivisionTables::F_ITa] = glGetUniformLocation(_program, "_F0_ITa");
|
|
|
|
_tableUniforms[FarSubdivisionTables::E_IT] = glGetUniformLocation(_program, "_E0_IT");
|
|
|
|
_tableUniforms[FarSubdivisionTables::V_IT] = glGetUniformLocation(_program, "_V0_IT");
|
|
|
|
_tableUniforms[FarSubdivisionTables::V_ITa] = glGetUniformLocation(_program, "_V0_ITa");
|
|
|
|
_tableUniforms[FarSubdivisionTables::E_W] = glGetUniformLocation(_program, "_E0_S");
|
|
|
|
_tableUniforms[FarSubdivisionTables::V_W] = glGetUniformLocation(_program, "_V0_S");
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
// set unfiorm locations for edit
|
|
|
|
_subEditAdd = glGetSubroutineIndex(_program,
|
|
|
|
GL_COMPUTE_SHADER,
|
|
|
|
"editAdd");
|
|
|
|
|
|
|
|
_uniformEditPrimVarOffset = glGetUniformLocation(_program, "editPrimVarOffset");
|
|
|
|
_uniformEditPrimVarWidth = glGetUniformLocation(_program, "editPrimVarWidth");
|
|
|
|
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-03-08 01:50:15 +00:00
|
|
|
OsdGLSLComputeKernelBundle::dispatchCompute(
|
|
|
|
int vertexOffset, int tableOffset, int start, int end) const {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
int count = end - start;
|
|
|
|
if (count <= 0) return;
|
|
|
|
|
|
|
|
// set batch range
|
2013-03-08 01:50:15 +00:00
|
|
|
glUniform1i(_uniformVertexOffset, vertexOffset);
|
|
|
|
glUniform1i(_uniformTableOffset, tableOffset);
|
2012-12-11 01:15:13 +00:00
|
|
|
glUniform1i(_uniformIndexStart, start);
|
|
|
|
glUniform1i(_uniformIndexEnd, end);
|
|
|
|
|
2013-03-08 01:50:15 +00:00
|
|
|
// execute
|
2012-12-11 01:15:13 +00:00
|
|
|
glDispatchCompute(count/_workGroupSize + 1, 1, 1);
|
|
|
|
|
2013-03-08 01:50:15 +00:00
|
|
|
// sync for later reading (slow..)
|
|
|
|
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OsdGLSLComputeKernelBundle::ApplyBilinearFaceVerticesKernel(
|
2013-03-08 01:50:15 +00:00
|
|
|
int vertexOffset, int tableOffset, int start, int end) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
glUniformSubroutinesuiv(GL_COMPUTE_SHADER, 1, &_subComputeFace);
|
2013-03-08 01:50:15 +00:00
|
|
|
dispatchCompute(vertexOffset, tableOffset, start, end);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OsdGLSLComputeKernelBundle::ApplyBilinearEdgeVerticesKernel(
|
2013-03-08 01:50:15 +00:00
|
|
|
int vertexOffset, int tableOffset, int start, int end) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
glUniformSubroutinesuiv(GL_COMPUTE_SHADER, 1, &_subComputeBilinearEdge);
|
2013-03-08 01:50:15 +00:00
|
|
|
dispatchCompute(vertexOffset, tableOffset, start, end);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OsdGLSLComputeKernelBundle::ApplyBilinearVertexVerticesKernel(
|
2013-03-08 01:50:15 +00:00
|
|
|
int vertexOffset, int tableOffset, int start, int end) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
glUniformSubroutinesuiv(GL_COMPUTE_SHADER, 1, &_subComputeVertex);
|
2013-03-08 01:50:15 +00:00
|
|
|
dispatchCompute(vertexOffset, tableOffset, start, end);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
OsdGLSLComputeKernelBundle::ApplyCatmarkFaceVerticesKernel(
|
2013-03-08 01:50:15 +00:00
|
|
|
int vertexOffset, int tableOffset, int start, int end) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
glUniformSubroutinesuiv(GL_COMPUTE_SHADER, 1, &_subComputeFace);
|
2013-03-08 01:50:15 +00:00
|
|
|
dispatchCompute(vertexOffset, tableOffset, start, end);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OsdGLSLComputeKernelBundle::ApplyCatmarkEdgeVerticesKernel(
|
2013-03-08 01:50:15 +00:00
|
|
|
int vertexOffset, int tableOffset, int start, int end) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
glUniformSubroutinesuiv(GL_COMPUTE_SHADER, 1, &_subComputeEdge);
|
2013-03-08 01:50:15 +00:00
|
|
|
dispatchCompute(vertexOffset, tableOffset, start, end);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OsdGLSLComputeKernelBundle::ApplyCatmarkVertexVerticesKernelB(
|
2013-03-08 01:50:15 +00:00
|
|
|
int vertexOffset, int tableOffset, int start, int end) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
glUniformSubroutinesuiv(GL_COMPUTE_SHADER, 1, &_subComputeCatmarkVertexB);
|
2013-03-08 01:50:15 +00:00
|
|
|
dispatchCompute(vertexOffset, tableOffset, start, end);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OsdGLSLComputeKernelBundle::ApplyCatmarkVertexVerticesKernelA(
|
2013-03-08 01:50:15 +00:00
|
|
|
int vertexOffset, int tableOffset, int start, int end, bool pass) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
glUniformSubroutinesuiv(GL_COMPUTE_SHADER, 1, &_subComputeVertexA);
|
|
|
|
glUniform1i(_uniformVertexPass, pass ? 1 : 0);
|
2013-03-08 01:50:15 +00:00
|
|
|
dispatchCompute(vertexOffset, tableOffset, start, end);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OsdGLSLComputeKernelBundle::ApplyLoopEdgeVerticesKernel(
|
2013-03-08 01:50:15 +00:00
|
|
|
int vertexOffset, int tableOffset, int start, int end) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
glUniformSubroutinesuiv(GL_COMPUTE_SHADER, 1, &_subComputeEdge);
|
2013-03-08 01:50:15 +00:00
|
|
|
dispatchCompute(vertexOffset, tableOffset, start, end);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OsdGLSLComputeKernelBundle::ApplyLoopVertexVerticesKernelB(
|
2013-03-08 01:50:15 +00:00
|
|
|
int vertexOffset, int tableOffset, int start, int end) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
glUniformSubroutinesuiv(GL_COMPUTE_SHADER, 1, &_subComputeLoopVertexB);
|
2013-03-08 01:50:15 +00:00
|
|
|
dispatchCompute(vertexOffset, tableOffset, start, end);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OsdGLSLComputeKernelBundle::ApplyLoopVertexVerticesKernelA(
|
2013-03-08 01:50:15 +00:00
|
|
|
int vertexOffset, int tableOffset, int start, int end, bool pass) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
glUniformSubroutinesuiv(GL_COMPUTE_SHADER, 1, &_subComputeVertexA);
|
|
|
|
glUniform1i(_uniformVertexPass, pass ? 1 : 0);
|
2013-03-08 01:50:15 +00:00
|
|
|
dispatchCompute(vertexOffset, tableOffset, start, end);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OsdGLSLComputeKernelBundle::ApplyEditAdd(
|
2013-03-08 01:50:15 +00:00
|
|
|
int primvarOffset, int primvarWidth,
|
|
|
|
int vertexOffset, int tableOffset, int start, int end) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
glUniformSubroutinesuiv(GL_COMPUTE_SHADER, 1, &_subEditAdd);
|
|
|
|
glUniform1i(_uniformEditPrimVarOffset, primvarOffset);
|
|
|
|
glUniform1i(_uniformEditPrimVarWidth, primvarWidth);
|
2013-03-08 01:50:15 +00:00
|
|
|
dispatchCompute(vertexOffset, tableOffset, start, end);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OsdGLSLComputeKernelBundle::UseProgram() const
|
|
|
|
{
|
|
|
|
glUseProgram(_program);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace OPENSUBDIV_VERSION
|
|
|
|
} // end namespace OpenSubdiv
|