adding support for optional GLSL compute kernels

This commit is contained in:
Manuel Kraemer 2012-06-09 13:40:48 -07:00
parent 9988aad7f4
commit a4a5bc253a
13 changed files with 1307 additions and 35 deletions

View File

@ -61,7 +61,7 @@
#
# GLEW_FOUND
# GLEW_INCLUDE_DIR
# GLEW_LIBRARIES
# GLEW_LIBRARY
#
IF (WIN32)
@ -69,7 +69,7 @@ IF (WIN32)
$ENV{PROGRAMFILES}/GLEW/include
${PROJECT_SOURCE_DIR}/extern/glew/include
DOC "The directory where GL/glew.h resides")
FIND_LIBRARY( GLEW_LIBRARIES
FIND_LIBRARY( GLEW_LIBRARY
NAMES glew GLEW glew32 glew32s
PATHS
$ENV{PROGRAMFILES}/GLEW/lib
@ -87,7 +87,7 @@ IF (${CMAKE_HOST_UNIX})
/sw/include
/opt/local/include
DOC "The directory where GL/glew.h resides")
FIND_LIBRARY( GLEW_LIBRARIES
FIND_LIBRARY( GLEW_LIBRARY
NAMES GLEW glew
PATHS
${GLEW_LOCATION}/lib
@ -104,7 +104,7 @@ ENDIF ()
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(GLEW DEFAULT_MSG
GLEW_INCLUDE_DIR
GLEW_LIBRARIES
GLEW_LIBRARY
)
IF (GLEW_INCLUDE_DIR)

View File

@ -55,10 +55,13 @@
# a particular purpose and non-infringement.
#
add_subdirectory(tools/stringify)
add_subdirectory(hbr)
add_subdirectory(far)
add_subdirectory(osd)
install( FILES version.h
DESTINATION include/
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ )

View File

@ -1,16 +1,75 @@
#
# Copyright (C) Pixar. All rights reserved.
#
# This license governs use of the accompanying software. If you
# use the software, you accept this license. If you do not accept
# the license, do not use the software.
#
# 1. Definitions
# The terms "reproduce," "reproduction," "derivative works," and
# "distribution" have the same meaning here as under U.S.
# copyright law. A "contribution" is the original software, or
# any additions or changes to the software.
# A "contributor" is any person or entity that distributes its
# contribution under this license.
# "Licensed patents" are a contributor's patent claims that read
# directly on its contribution.
#
# 2. Grant of Rights
# (A) Copyright Grant- Subject to the terms of this license,
# including the license conditions and limitations in section 3,
# each contributor grants you a non-exclusive, worldwide,
# royalty-free copyright license to reproduce its contribution,
# prepare derivative works of its contribution, and distribute
# its contribution or any derivative works that you create.
# (B) Patent Grant- Subject to the terms of this license,
# including the license conditions and limitations in section 3,
# each contributor grants you a non-exclusive, worldwide,
# royalty-free license under its licensed patents to make, have
# made, use, sell, offer for sale, import, and/or otherwise
# dispose of its contribution in the software or derivative works
# of the contribution in the software.
#
# 3. Conditions and Limitations
# (A) No Trademark License- This license does not grant you
# rights to use any contributor's name, logo, or trademarks.
# (B) If you bring a patent claim against any contributor over
# patents that you claim are infringed by the software, your
# patent license from such contributor to the software ends
# automatically.
# (C) If you distribute any portion of the software, you must
# retain all copyright, patent, trademark, and attribution
# notices that are present in the software.
# (D) If you distribute any portion of the software in source
# code form, you may do so only under this license by including a
# complete copy of this license with your distribution. If you
# distribute any portion of the software in compiled or object
# code form, you may only do so under a license that complies
# with this license.
# (E) The software is licensed "as-is." You bear the risk of
# using it. The contributors give no express warranties,
# guarantees or conditions. You may have additional consumer
# rights under your local laws which this license cannot change.
# To the extent permitted under your local laws, the contributors
# exclude the implied warranties of merchantability, fitness for
# a particular purpose and non-infringement.
#
#
find_package(IlmBase REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(OpenGL)
find_package(GLEW)
include_directories(
${ILMBASE_INCLUDE_DIR}
${OPENGL_INCLUDE_DIR}
${GLEW_INCLUDE_DIR}
${GLUT_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/opensubdiv
)
#-------------------------------------------------------------------------------
# source & headers
set(SOURCE_FILES
cpuDispatcher.cpp
cpuKernel.cpp
@ -18,47 +77,91 @@ set(SOURCE_FILES
mesh.cpp
)
set(HEADER_FILES
set(KERNEL_FILES
)
set(INC_FILES
)
set(PUBLIC_HEADERS
cpuDispatcher.h
cpuKernel.h
kernelDispatcher.h
local.h
mesh.h
vertex.h
)
#-------------------------------------------------------------------------------
# CPU (omp) code & dependencies
if(APPLE)
set(PLATFORM_COMPILE_FLAGS
-fopenmp
)
set(PLATFORM_LIBRARIES
#XXX:cleanup Remove opengl, glut, and OpenCL from this list.
/System/Library/Frameworks/OpenGL.framework
/System/Library/Frameworks/OpenCL.framework
/System/Library/Frameworks/GLUT.framework
gomp
)
endif(APPLE)
if(UNIX AND NOT APPLE)
elseif(UNIX)
set(PLATFORM_COMPILE_FLAGS
-fopenmp
-fPIC
)
set(PLATFORM_LIBRARIES
GLU
glut
gomp
)
endif(UNIX AND NOT APPLE)
if(WIN32)
elseif(WIN32 or WIN64)
set(PLATFORM_COMPILE_FLAGS
/openmp
)
endif()
#-------------------------------------------------------------------------------
# GL code & dependencies
# note : (GLSL compute kernels require GL 4.2, which excludes APPLE)
if( OPENGL_FOUND AND GLEW_FOUND AND (NOT APPLE) )
list(APPEND SOURCE_FILES
glslDispatcher.cpp
)
set(PLATFORM_LIBRARIES
list(APPEND PUBLIC_HEADERS
glslDispatcher.h
)
endif(WIN32)
list(APPEND KERNEL_FILES
glslKernel.glsl
)
list(APPEND PLATFORM_LIBRARIES
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
)
endif()
# We want to use preprocessor include directives to include GLSL and OpenCL
# kernel source files in cpp files, but since the sources contain newline
# characters we would need raw string literals from C++11 to do this directly.
# To avoid depending on C++11 we instead use a small tool called "line_quote"
# to generate source files that are suitable for direct inclusion.
foreach(kernel_file ${KERNEL_FILES})
string(REGEX REPLACE ".*[.](.*)" "\\1" extension ${kernel_file})
if(NOT ${extension} STREQUAL "cu")
string(REGEX REPLACE "(.*)[.].*" "\\1.inc" inc_file ${kernel_file})
list(APPEND INC_FILES ${inc_file})
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${inc_file}
COMMAND stringify ${CMAKE_CURRENT_SOURCE_DIR}/${kernel_file}
${CMAKE_CURRENT_SOURCE_DIR}/${inc_file}
DEPENDS stringify ${CMAKE_CURRENT_SOURCE_DIR}/${kernel_file}
)
endif()
endforeach()
source_group("Kernels" FILES ${KERNEL_FILES})
source_group("Inc" FILES ${INC_FILES})
macro(_add_library target)
if(CUDA_FOUND)
@ -68,14 +171,14 @@ macro(_add_library target)
endif()
endmacro()
_add_library(osd STATIC
_add_library(osd SHARED
${SOURCE_FILES}
${HEADER_FILES}
${KERNEL_FILES}
${INC_FILES}
)
target_link_libraries(osd
${OPENGL_LIBRARIES}
${GLUT_LIBRARIES}
${OPENGL_LIBRARY}
${PLATFORM_LIBRARIES}
glew
)

View File

@ -227,6 +227,4 @@ extern void computeLoopVertexB(const VertexDescriptor *vdesc, float *vertex, flo
}
} // end namespace OPENSUBDIV_VERSION
using namespace OPENSUBDIV_VERSION;
} // end namespace OpenSubdiv

View File

@ -1,5 +1,8 @@
#include <GL/glew.h>
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
static void
DumpBuffer(GLuint buffer) {
@ -75,3 +78,6 @@ DebugProgram(GLuint program) {
fclose(fp);
}
}
} // end namespace OPENSUBDIV_VERSION
} // end namespace OpenSubdiv

View File

@ -0,0 +1,479 @@
//
// Copyright (C) Pixar. All rights reserved.
//
// This license governs use of the accompanying software. If you
// use the software, you accept this license. If you do not accept
// the license, do not use the software.
//
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and
// "distribution" have the same meaning here as under U.S.
// copyright law. A "contribution" is the original software, or
// any additions or changes to the software.
// A "contributor" is any person or entity that distributes its
// contribution under this license.
// "Licensed patents" are a contributor's patent claims that read
// directly on its contribution.
//
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license,
// including the license conditions and limitations in section 3,
// each contributor grants you a non-exclusive, worldwide,
// royalty-free copyright license to reproduce its contribution,
// prepare derivative works of its contribution, and distribute
// its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license,
// including the license conditions and limitations in section 3,
// each contributor grants you a non-exclusive, worldwide,
// royalty-free license under its licensed patents to make, have
// made, use, sell, offer for sale, import, and/or otherwise
// dispose of its contribution in the software or derivative works
// of the contribution in the software.
//
// 3. Conditions and Limitations
// (A) No Trademark License- This license does not grant you
// rights to use any contributor's name, logo, or trademarks.
// (B) If you bring a patent claim against any contributor over
// patents that you claim are infringed by the software, your
// patent license from such contributor to the software ends
// automatically.
// (C) If you distribute any portion of the software, you must
// retain all copyright, patent, trademark, and attribution
// notices that are present in the software.
// (D) If you distribute any portion of the software in source
// code form, you may do so only under this license by including a
// complete copy of this license with your distribution. If you
// distribute any portion of the software in compiled or object
// code form, you may only do so under a license that complies
// with this license.
// (E) The software is licensed "as-is." You bear the risk of
// using it. The contributors give no express warranties,
// guarantees or conditions. You may have additional consumer
// rights under your local laws which this license cannot change.
// To the extent permitted under your local laws, the contributors
// exclude the implied warranties of merchantability, fitness for
// a particular purpose and non-infringement.
//
#include "../osd/glslDispatcher.h"
#include "../osd/local.h"
#include <GL/glew.h>
#include <stdlib.h>
#include <string.h>
#define OPT_E0_IT_VEC4
#define OPT_E0_S_VEC2
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
static const char *shaderSource =
#include "../osd/glslKernel.inc"
;
static const char *shaderDefines = ""
#ifdef OPT_CATMARK_V_IT_VEC2
"#define OPT_CATMARK_V_IT_VEC2\n"
#endif
#ifdef OPT_E0_IT_VEC4
"#define OPT_E0_IT_VEC4\n"
#endif
#ifdef OPT_E0_S_VEC2
"#define OPT_E0_S_VEC2\n"
#endif
;
OsdGlslKernelDispatcher::OsdGlslKernelDispatcher(int levels, int numVertexElements, int numVaryingElements)
: OsdKernelDispatcher(levels),
_numVertexElements(numVertexElements),
_numVarying(numVaryingElements)
{
_numVarying = 0; // XXX
_vertexBuffer = 0;
_varyingBuffer = 0;
_prgKernel = 0;
glGenTextures(1, &_vertexTexture);
glGenTextures(1, &_varyingTexture);
_tableBuffers.resize(TABLE_MAX);
_tableTextures.resize(TABLE_MAX);
_tableUniforms.resize(TABLE_MAX);
_tableOffsetUniforms.resize(TABLE_MAX);
glGenBuffers(TABLE_MAX, &_tableBuffers[0]);
glGenTextures(TABLE_MAX, &_tableTextures[0]);
subComputeFace = 0;
subComputeEdge = 0;
subComputeVertexA = 0;
subComputeVertexB = 0;
uniformVertexPass = 0;
uniformIndexStart = 0;
uniformIndexOffset = 0;
compile(shaderSource, shaderDefines);
subComputeFace = glGetSubroutineIndex(_prgKernel, GL_VERTEX_SHADER, "catmarkComputeFace");
subComputeEdge = glGetSubroutineIndex(_prgKernel, GL_VERTEX_SHADER, "catmarkComputeEdge");
subComputeVertexA = glGetSubroutineIndex(_prgKernel, GL_VERTEX_SHADER, "catmarkComputeVertexA");
subComputeVertexB = glGetSubroutineIndex(_prgKernel, GL_VERTEX_SHADER, "catmarkComputeVertexB");
subComputeLoopVertexB = glGetSubroutineIndex(_prgKernel, GL_VERTEX_SHADER, "loopComputeVertexB");
uniformVertexPass = glGetUniformLocation(_prgKernel, "vertexPass");
uniformIndexStart = glGetUniformLocation(_prgKernel, "indexStart");
uniformIndexOffset = glGetUniformLocation(_prgKernel, "indexOffset");
_tableUniforms[F_IT] = glGetUniformLocation(_prgKernel, "_F0_IT");
_tableUniforms[F_ITa] = glGetUniformLocation(_prgKernel, "_F0_ITa");
_tableUniforms[E_IT] = glGetUniformLocation(_prgKernel, "_E0_IT");
_tableUniforms[V_IT] = glGetUniformLocation(_prgKernel, "_V0_IT");
_tableUniforms[V_ITa] = glGetUniformLocation(_prgKernel, "_V0_ITa");
_tableUniforms[E_W] = glGetUniformLocation(_prgKernel, "_E0_S");
_tableUniforms[V_W] = glGetUniformLocation(_prgKernel, "_V0_S");
_tableOffsetUniforms[F_IT] = glGetUniformLocation(_prgKernel, "F_IT_ofs");
_tableOffsetUniforms[F_ITa] = glGetUniformLocation(_prgKernel, "F_ITa_ofs");
_tableOffsetUniforms[E_IT] = glGetUniformLocation(_prgKernel, "E_IT_ofs");
_tableOffsetUniforms[V_IT] = glGetUniformLocation(_prgKernel, "V_IT_ofs");
_tableOffsetUniforms[V_ITa] = glGetUniformLocation(_prgKernel, "V_ITa_ofs");
_tableOffsetUniforms[E_W] = glGetUniformLocation(_prgKernel, "E_W_ofs");
_tableOffsetUniforms[V_W] = glGetUniformLocation(_prgKernel, "V_W_ofs");
}
OsdGlslKernelDispatcher::~OsdGlslKernelDispatcher() {
if (_prgKernel)
glDeleteProgram(_prgKernel);
glDeleteTextures(1, &_vertexTexture);
glDeleteTextures(1, &_varyingTexture);
glDeleteBuffers(TABLE_MAX, &_tableBuffers[0]);
}
void
OsdGlslKernelDispatcher::CopyTable(int tableIndex, size_t size, const void *ptr) {
glBindBuffer(GL_ARRAY_BUFFER, _tableBuffers[tableIndex]);
glBufferData(GL_ARRAY_BUFFER, size, ptr, GL_STATIC_DRAW);
CHECK_GL_ERROR("UpdateTable tableIndex %d, size %ld, buffer =%d\n",
tableIndex, size, _tableBuffers[tableIndex]);
}
void
OsdGlslKernelDispatcher::BeginLaunchKernel() {
glUseProgram(_prgKernel);
glEnable(GL_RASTERIZER_DISCARD);
//XXX what if loop..
bindTextureBuffer(_tableUniforms[F_IT], _tableBuffers[F_IT],
_tableTextures[F_IT], GL_R32UI, 2);
bindTextureBuffer(_tableUniforms[F_ITa], _tableBuffers[F_ITa],
_tableTextures[F_ITa], GL_R32I, 3);
#ifdef OPT_E0_IT_VEC4
bindTextureBuffer(_tableUniforms[E_IT], _tableBuffers[E_IT],
_tableTextures[E_IT], GL_RGBA32UI, 4);
#else
bindTextureBuffer(_tableUniforms[E_IT], _tableBuffers[E_IT],
_tableTextures[E_IT], GL_R32UI, 4);
#endif
#ifdef OPT_CATMARK_V_IT_VEC2
bindTextureBuffer(_tableUniforms[V_IT], _tableBuffers[V_IT],
_tableTextures[V_IT], GL_RG32UI, 5);
#else
bindTextureBuffer(_tableUniforms[V_IT], _tableBuffers[V_IT],
_tableTextures[V_IT], GL_R32UI, 5);
#endif
bindTextureBuffer(_tableUniforms[V_ITa], _tableBuffers[V_ITa],
_tableTextures[V_ITa], GL_R32I, 6);
#ifdef OPT_E0_S_VEC2
bindTextureBuffer(_tableUniforms[E_W], _tableBuffers[E_W],
_tableTextures[E_W], GL_RG32F, 7);
#else
bindTextureBuffer(_tableUniforms[E_W], _tableBuffers[E_W],
_tableTextures[E_W], GL_R32F, 7);
#endif
bindTextureBuffer(_tableUniforms[V_W], _tableBuffers[V_W],
_tableTextures[V_W], GL_R32F, 8);
}
void
OsdGlslKernelDispatcher::EndLaunchKernel() {
glDisable(GL_RASTERIZER_DISCARD);
glUseProgram(0);
}
void
OsdGlslKernelDispatcher::BindVertexBuffer(GLuint vertexBuffer, GLuint varyingBuffer) {
glUseProgram(_prgKernel);
CHECK_GL_ERROR("BindVertexBufferA, glUniform %d\n", _vertexUniform);
glUniform1i(_vertexUniform, 0);
CHECK_GL_ERROR("BindVertexBufferB, glUniform %d\n", _vertexUniform);
bindTextureBuffer(_vertexUniform, vertexBuffer, _vertexTexture, GL_RGB32F, 0);
_vertexBuffer = vertexBuffer;
if (varyingBuffer) {
bindTextureBuffer(_varyingUniform, varyingBuffer, _varyingTexture, GL_R32F, 0);
_varyingBuffer = varyingBuffer;
}
#if 0
glActiveTexture(GL_TEXTURE0 + 0);
glBindImageTextureEXT(0, _vertexTexture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F);
if (_numVarying > 0) {
glUniform1i(_varyingUniform, 1);
glBindImageTextureEXT(1, _vertexTexture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F);
}
#endif
CHECK_GL_ERROR("BindVertexBuffer \n");
}
void
OsdGlslKernelDispatcher::UpdateVertexBuffer(size_t size, void *ptr) {
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
float * pointer = (float*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(pointer, ptr, size);
glUnmapBuffer(GL_ARRAY_BUFFER);
}
void
OsdGlslKernelDispatcher::UpdateVaryingBuffer(size_t size, void *ptr) {
if (_varyingBuffer) {
glBindBuffer(GL_ARRAY_BUFFER, _varyingBuffer);
float * pointer = (float*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(pointer, ptr, size);
glUnmapBuffer(GL_ARRAY_BUFFER);
}
}
void
OsdGlslKernelDispatcher::MapVertexBuffer() {
}
void
OsdGlslKernelDispatcher::MapVaryingBuffer() {
}
void
OsdGlslKernelDispatcher::UnmapVertexBuffer() {
// UnbindTableBuffer();
}
void
OsdGlslKernelDispatcher::UnmapVaryingBuffer() {
}
void
OsdGlslKernelDispatcher::Synchronize() {
glFinish();
}
void
OsdGlslKernelDispatcher::bindTextureBuffer(
GLuint sampler, GLuint buffer, GLuint texture, GLenum type, int unit) const {
if (sampler == -1) {
OSD_ERROR("BindTextureError:: sampler = %d\n", sampler);
return;
}
OSD_DEBUG("BindTextureBuffer sampler=%d, buffer=%d, texture = %d, E%x\n", sampler, buffer, texture, glGetError());
glUniform1i(sampler, unit);
glActiveTexture(GL_TEXTURE0 + unit);
CHECK_GL_ERROR("BindTextureBuffer glActiveTexture %d\n", unit);
glBindTexture(GL_TEXTURE_BUFFER, texture);
CHECK_GL_ERROR("BindTextureBuffer glBindTexture %d\n", texture);
glTexBuffer(GL_TEXTURE_BUFFER, type, buffer);
CHECK_GL_ERROR("BindTextureBuffer glTexBuffer\n");
glActiveTexture(GL_TEXTURE0);
}
bool
OsdGlslKernelDispatcher::compile(const char *shaderSource, const char *shaderDefine) {
_prgKernel = glCreateProgram();
GLuint shader = glCreateShader(GL_VERTEX_SHADER);
char constantDefine[256];
snprintf(constantDefine, 256,
"#define NUM_VARYING_ELEMENTS %d\n", _numVarying);
const char *shaderSources[3];
shaderSources[0] = constantDefine;
shaderSources[1] = shaderDefine;
shaderSources[2] = shaderSource;
glShaderSource(shader, 3, shaderSources, NULL);
glCompileShader(shader);
glAttachShader(_prgKernel, shader);
const char *outputs[] = { "outPosition",
"outNormal",
"gl_NextBuffer",
"outVaryingData" };
int nOutputs = _numVarying > 0 ? 4 : 2;
glTransformFeedbackVaryings(_prgKernel, nOutputs, outputs, GL_INTERLEAVED_ATTRIBS);
CHECK_GL_ERROR("Transform feedback initialize \n");
GLint linked = 0;
glLinkProgram(_prgKernel);
glGetProgramiv(_prgKernel, GL_LINK_STATUS, &linked);
if (linked == GL_FALSE) {
OSD_ERROR("Fail to link shader\n");
char buffer[1024];
glGetShaderInfoLog(shader, 1024, NULL, buffer);
OSD_ERROR(buffer);
glGetProgramInfoLog(_prgKernel, 1024, NULL, buffer);
OSD_ERROR(buffer);
glDeleteProgram(_prgKernel);
_prgKernel = 0;
// XXX ERROR HANDLE
return false;
}
glDeleteShader(shader);
_vertexUniform = glGetUniformLocation(_prgKernel, "vertex");
_varyingUniform = glGetUniformLocation(_prgKernel, "varyingData");
return true;
}
void
OsdGlslKernelDispatcher::unbindTextureBuffer(int unit) const {
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_BUFFER, 0);
}
void
OsdGlslKernelDispatcher::transformGpuBufferData(GLuint kernel, GLint offset, int start, int end, bool vertexPass) const {
int count = end - start;
if (count <= 0) return;
OSD_DEBUG("_transformGpuBufferData kernel=%d E%x, offset=%d, count=%d\n", kernel, glGetError(), offset, count);
glUniformSubroutinesuiv(GL_VERTEX_SHADER, 1, &kernel);
glUniform1i(uniformVertexPass, vertexPass); // XXX
// set batch range
glUniform1i(uniformIndexStart, start);
glUniform1i(uniformIndexOffset, offset);
// XXX: end is not used here now
CHECK_GL_ERROR("Uniform index set at offset=%d. start=%d\n", offset, start);
// set transform feedback buffer
int vertexStride = _numVertexElements*sizeof(float);
int varyingStride = _numVarying*sizeof(float);
glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, _vertexBuffer,
(start+offset)*vertexStride, count*vertexStride);
CHECK_GL_ERROR("transformGpuBufferData glBindBufferRange\n");
if (_numVarying > 0){
glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 1, _varyingBuffer,
(start+offset)*varyingStride, count*varyingStride);
}
glBeginTransformFeedback(GL_POINTS);
CHECK_GL_ERROR("transformGpuBufferData glBeginTransformFeedback\n");
// draw array -----------------------------------------
glDrawArrays(GL_POINTS, 0, count);
CHECK_GL_ERROR("transformGpuBufferData DrawArray (%d)\n", count);
glEndTransformFeedback();
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
GLsync sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
glWaitSync(sync, 0, GL_TIMEOUT_IGNORED);
glDeleteSync(sync);
}
void
OsdGlslKernelDispatcher::ApplyCatmarkFaceVerticesKernel(
FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const {
glUniform1i(_tableOffsetUniforms[F_IT], _tableOffsets[F_IT][level-1]);
glUniform1i(_tableOffsetUniforms[F_ITa], _tableOffsets[F_ITa][level-1]);
transformGpuBufferData(subComputeFace, offset, start, end);
}
void
OsdGlslKernelDispatcher::ApplyCatmarkEdgeVerticesKernel(
FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const {
glUniform1i(_tableOffsetUniforms[E_IT], _tableOffsets[E_IT][level-1]);
glUniform1i(_tableOffsetUniforms[E_W], _tableOffsets[E_W][level-1]);
transformGpuBufferData(subComputeEdge, offset, start, end);
}
void
OsdGlslKernelDispatcher::ApplyCatmarkVertexVerticesKernelB(
FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const {
glUniform1i(_tableOffsetUniforms[V_IT], _tableOffsets[V_IT][level-1]);
glUniform1i(_tableOffsetUniforms[V_ITa], _tableOffsets[V_ITa][level-1]);
glUniform1i(_tableOffsetUniforms[V_W], _tableOffsets[V_W][level-1]);
transformGpuBufferData(subComputeVertexB, offset, start, end);
}
void
OsdGlslKernelDispatcher::ApplyCatmarkVertexVerticesKernelA(
FarMesh<OsdVertex> * mesh, int offset, bool pass, int level, int start, int end, void * data) const {
glUniform1i(_tableOffsetUniforms[V_ITa], _tableOffsets[V_ITa][level-1]);
glUniform1i(_tableOffsetUniforms[V_W], _tableOffsets[V_W][level-1]);
transformGpuBufferData(subComputeVertexA, offset, start, end, pass);
}
void
OsdGlslKernelDispatcher::ApplyLoopEdgeVerticesKernel(
FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const {
glUniform1i(_tableOffsetUniforms[E_IT], _tableOffsets[E_IT][level-1]);
glUniform1i(_tableOffsetUniforms[E_W], _tableOffsets[E_W][level-1]);
transformGpuBufferData(subComputeEdge, offset, start, end);
}
void
OsdGlslKernelDispatcher::ApplyLoopVertexVerticesKernelB(
FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const {
glUniform1i(_tableOffsetUniforms[V_IT], _tableOffsets[V_IT][level-1]);
glUniform1i(_tableOffsetUniforms[V_ITa], _tableOffsets[V_ITa][level-1]);
glUniform1i(_tableOffsetUniforms[V_W], _tableOffsets[V_W][level-1]);
transformGpuBufferData(subComputeLoopVertexB, offset, start, end);
}
void
OsdGlslKernelDispatcher::ApplyLoopVertexVerticesKernelA(
FarMesh<OsdVertex> * mesh, int offset, bool pass, int level, int start, int end, void * data) const {
glUniform1i(_tableOffsetUniforms[V_ITa], _tableOffsets[V_ITa][level-1]);
glUniform1i(_tableOffsetUniforms[V_W], _tableOffsets[V_W][level-1]);
transformGpuBufferData(subComputeVertexA, offset, start, end, pass);
}
} // end namespace OPENSUBDIV_VERSION
} // end namespace OpenSubdiv

View File

@ -0,0 +1,163 @@
//
// Copyright (C) Pixar. All rights reserved.
//
// This license governs use of the accompanying software. If you
// use the software, you accept this license. If you do not accept
// the license, do not use the software.
//
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and
// "distribution" have the same meaning here as under U.S.
// copyright law. A "contribution" is the original software, or
// any additions or changes to the software.
// A "contributor" is any person or entity that distributes its
// contribution under this license.
// "Licensed patents" are a contributor's patent claims that read
// directly on its contribution.
//
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license,
// including the license conditions and limitations in section 3,
// each contributor grants you a non-exclusive, worldwide,
// royalty-free copyright license to reproduce its contribution,
// prepare derivative works of its contribution, and distribute
// its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license,
// including the license conditions and limitations in section 3,
// each contributor grants you a non-exclusive, worldwide,
// royalty-free license under its licensed patents to make, have
// made, use, sell, offer for sale, import, and/or otherwise
// dispose of its contribution in the software or derivative works
// of the contribution in the software.
//
// 3. Conditions and Limitations
// (A) No Trademark License- This license does not grant you
// rights to use any contributor's name, logo, or trademarks.
// (B) If you bring a patent claim against any contributor over
// patents that you claim are infringed by the software, your
// patent license from such contributor to the software ends
// automatically.
// (C) If you distribute any portion of the software, you must
// retain all copyright, patent, trademark, and attribution
// notices that are present in the software.
// (D) If you distribute any portion of the software in source
// code form, you may do so only under this license by including a
// complete copy of this license with your distribution. If you
// distribute any portion of the software in compiled or object
// code form, you may only do so under a license that complies
// with this license.
// (E) The software is licensed "as-is." You bear the risk of
// using it. The contributors give no express warranties,
// guarantees or conditions. You may have additional consumer
// rights under your local laws which this license cannot change.
// To the extent permitted under your local laws, the contributors
// exclude the implied warranties of merchantability, fitness for
// a particular purpose and non-infringement.
//
#ifndef OSD_GLSL_DISPATCHER_H
#define OSD_GLSL_DISPATCHER_H
#include "../osd/kernelDispatcher.h"
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
class OsdGlslKernelDispatcher : public OsdKernelDispatcher {
public:
OsdGlslKernelDispatcher(int levels, int numVertexElements, int numVaryingElements);
virtual ~OsdGlslKernelDispatcher();
virtual void ApplyCatmarkFaceVerticesKernel(FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const;
virtual void ApplyCatmarkEdgeVerticesKernel(FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const;
virtual void ApplyCatmarkVertexVerticesKernelB(FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const;
virtual void ApplyCatmarkVertexVerticesKernelA(FarMesh<OsdVertex> * mesh, int offset, bool pass, int level, int start, int end, void * data) const;
virtual void ApplyLoopEdgeVerticesKernel(FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const;
virtual void ApplyLoopVertexVerticesKernelB(FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const;
virtual void ApplyLoopVertexVerticesKernelA(FarMesh<OsdVertex> * mesh, int offset, bool pass, int level, int start, int end, void * data) const;
virtual void CopyTable(int tableIndex, size_t size, const void *ptr);
virtual void BeginLaunchKernel();
virtual void EndLaunchKernel();
virtual void BindVertexBuffer(GLuint vertexBuffer, GLuint varyingBuffer);
virtual void UpdateVertexBuffer(size_t size, void *ptr);
virtual void UpdateVaryingBuffer(size_t size, void *ptr);
virtual void MapVertexBuffer();
virtual void MapVaryingBuffer();
virtual void UnmapVertexBuffer();
virtual void UnmapVaryingBuffer();
virtual void Synchronize();
static OsdKernelDispatcher * Create(int levels, int numVertexElements, int numVaryingElements){
return new OsdGlslKernelDispatcher(levels, numVertexElements, numVaryingElements);
}
static void Register() {
Factory::GetInstance().Register("glsl", Create);
}
protected:
void bindTextureBuffer(GLuint sampler, GLuint buffer, GLuint texture, GLenum type, int unit) const;
void unbindTextureBuffer(int unit) const;
void transformGpuBufferData(GLuint kernel, GLint offset, int start, int end, bool vertexPass=false) const;
bool compile(const char *shaderSource, const char *shaderDefine);
GLuint _prgKernel;
int _numVertexElements,
_numVarying;
GLuint _vertexBuffer,
_varyingBuffer;
// texture for vertex
GLuint _vertexTexture,
_varyingTexture;
GLuint _vertexUniform,
_varyingUniform;
// table buffers
std::vector<GLuint> _tableBuffers;
std::vector<GLuint> _tableTextures;
std::vector<GLuint> _tableUniforms;
std::vector<GLuint> _tableOffsetUniforms;
GLuint uniformVertexPass;
GLuint uniformIndexStart;
GLuint uniformIndexOffset;
// shader locations
GLuint subComputeFace, subComputeEdge, subComputeVertexA, subComputeVertexB;
GLuint subComputeLoopVertexB;
};
} // end namespace OPENSUBDIV_VERSION
using namespace OPENSUBDIV_VERSION;
} // end namespace OpenSubdiv
#endif // OSD_GLSL_DISPATCHER_H

View File

@ -0,0 +1,346 @@
//
// Copyright (C) Pixar. All rights reserved.
//
// This license governs use of the accompanying software. If you
// use the software, you accept this license. If you do not accept
// the license, do not use the software.
//
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and
// "distribution" have the same meaning here as under U.S.
// copyright law. A "contribution" is the original software, or
// any additions or changes to the software.
// A "contributor" is any person or entity that distributes its
// contribution under this license.
// "Licensed patents" are a contributor's patent claims that read
// directly on its contribution.
//
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license,
// including the license conditions and limitations in section 3,
// each contributor grants you a non-exclusive, worldwide,
// royalty-free copyright license to reproduce its contribution,
// prepare derivative works of its contribution, and distribute
// its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license,
// including the license conditions and limitations in section 3,
// each contributor grants you a non-exclusive, worldwide,
// royalty-free license under its licensed patents to make, have
// made, use, sell, offer for sale, import, and/or otherwise
// dispose of its contribution in the software or derivative works
// of the contribution in the software.
//
// 3. Conditions and Limitations
// (A) No Trademark License- This license does not grant you
// rights to use any contributor's name, logo, or trademarks.
// (B) If you bring a patent claim against any contributor over
// patents that you claim are infringed by the software, your
// patent license from such contributor to the software ends
// automatically.
// (C) If you distribute any portion of the software, you must
// retain all copyright, patent, trademark, and attribution
// notices that are present in the software.
// (D) If you distribute any portion of the software in source
// code form, you may do so only under this license by including a
// complete copy of this license with your distribution. If you
// distribute any portion of the software in compiled or object
// code form, you may only do so under a license that complies
// with this license.
// (E) The software is licensed "as-is." You bear the risk of
// using it. The contributors give no express warranties,
// guarantees or conditions. You may have additional consumer
// rights under your local laws which this license cannot change.
// To the extent permitted under your local laws, the contributors
// exclude the implied warranties of merchantability, fitness for
// a particular purpose and non-infringement.
//
#version 400
subroutine void computeKernelType();
subroutine uniform computeKernelType computeKernel;
uniform isamplerBuffer _F0_IT;
uniform isamplerBuffer _F0_ITa;
uniform isamplerBuffer _E0_IT;
uniform isamplerBuffer _V0_IT;
uniform isamplerBuffer _V0_ITa;
uniform samplerBuffer _E0_S;
uniform samplerBuffer _V0_S;
uniform bool vertexPass;
uniform int indexOffset = 0; // index offset for the level
uniform int indexStart = 0; // start index for given batch
uniform int F_IT_ofs;
uniform int F_ITa_ofs;
uniform int E_IT_ofs;
uniform int V_IT_ofs;
uniform int V_ITa_ofs;
uniform int E_W_ofs;
uniform int V_W_ofs;
/*
+-----+---------------------------------+-----
n-1 | Level n |<batch range>| | n+1
+-----+---------------------------------+-----
^ ^
indexOffset |
indexStart
*/
//--------------------------------------------------------------------------------
struct Vertex
{
vec3 position;
vec3 normal;
#if NUM_VARYING > 0
float varyingData[NUM_VARYING]; // XXX: should use vec4 and packing
#endif
};
uniform samplerBuffer vertex; // vec3[2] (position, normal)
#if NUM_VARYING > 0
uniform samplerBuffer varyingData; // float[NUM_VARYING]
#endif
out vec3 outPosition;
out vec3 outNormal;
#if NUM_VARYING > 0
out float outVaryingData[NUM_VARYING]; // output feedback (mapped as a subrange of vertices)
#endif
//out vec3 outVaryingData; // output feedback (mapped as a subrange of vertices)
void clear(out Vertex v)
{
v.position = vec3(0);
v.normal = vec3(0);
#if NUM_VARYING > 0
for(int i = 0; i < NUM_VARYING; i++){
v.varyingData[i] = 0;
}
#endif
}
Vertex readVertex(int index)
{
// XXX: should be split into two parts for addWithWeight and addVaryingWithWeight
Vertex v;
// unpacking
v.position = texelFetch(vertex, index*2).xyz;
v.normal = texelFetch(vertex, index*2+1).xyz;
#if NUM_VARYING > 0
int stride = NUM_VARYING;
for(int i = 0; i < NUM_VARYING; i++){
v.varyingData[i] = texelFetch(varyingData, index*stride+i).x;
}
#endif
return v;
}
void writeVertex(Vertex v)
{
// packing
outPosition = v.position;
outNormal = normalize(v.normal);
#if NUM_VARYING > 0
for(int i = 0; i < NUM_VARYING; i++){
outVaryingData[i] = v.varyingData[i];
}
#endif
}
void addWithWeight(inout Vertex v, Vertex src, float weight)
{
v.position += weight * src.position;
v.normal += weight * src.normal;
}
void addVaryingWithWeight(inout Vertex v, Vertex src, float weight)
{
#if NUM_VARYING > 0
for(int i = 0; i < NUM_VARYING; i++){
v.varyingData[i] += weight * src.varyingData[i];
}
#endif
}
//--------------------------------------------------------------------------------
// Face-vertices compute Kernel
subroutine(computeKernelType)
void catmarkComputeFace()
{
int i = gl_VertexID + indexStart;
int h = texelFetch(_F0_ITa, F_ITa_ofs+2*i).x;
int n = texelFetch(_F0_ITa, F_ITa_ofs+2*i+1).x;
float weight = 1.0/n;
Vertex dst;
clear(dst);
for(int j=0; j<n; ++j){
int index = texelFetch(_F0_IT, F_IT_ofs+h+j).x;
addWithWeight(dst, readVertex(index), weight);
addVaryingWithWeight(dst, readVertex(index), weight);
}
writeVertex(dst);
}
// Edge-vertices compute Kernel
subroutine(computeKernelType)
void catmarkComputeEdge()
{
int i = gl_VertexID + indexStart;
Vertex dst;
clear(dst);
#ifdef OPT_E0_IT_VEC4
ivec4 eidx = texelFetch(_E0_IT, E_IT_ofs/4+i);
#else
int eidx0 = texelFetch(_E0_IT, E_IT_ofs+4*i+0).x;
int eidx1 = texelFetch(_E0_IT, E_IT_ofs+4*i+1).x;
int eidx2 = texelFetch(_E0_IT, E_IT_ofs+4*i+2).x;
int eidx3 = texelFetch(_E0_IT, E_IT_ofs+4*i+3).x;
ivec4 eidx = ivec4(eidx0, eidx1, eidx2, eidx3);
#endif
#ifdef OPT_E0_S_VEC2
vec2 weight = texelFetch(_E0_S, E_W_ofs/2+i).xy;
float vertWeight = weight.x;
#else
float vertWeight = texelFetch(_E0_S, E_W_ofs+i*2+0).x;
#endif
// Fully sharp edge : vertWeight = 0.5f;
addWithWeight(dst, readVertex(eidx.x), vertWeight);
addWithWeight(dst, readVertex(eidx.y), vertWeight);
if(eidx.z != -1){
#ifdef OPT_E0_S_VEC2
float faceWeight = weight.y;
#else
float faceWeight = texelFetch(_E0_S, E_W_ofs/2+i*2+1).x;
#endif
addWithWeight(dst, readVertex(eidx.z), faceWeight);
addWithWeight(dst, readVertex(eidx.w), faceWeight);
}
addVaryingWithWeight(dst, readVertex(eidx.x), 0.5f);
addVaryingWithWeight(dst, readVertex(eidx.y), 0.5f);
writeVertex(dst);
}
// Vertex-vertices compute Kernels 'A' / k_Crease and k_Corner rules
subroutine(computeKernelType)
void catmarkComputeVertexA()
{
int i = gl_VertexID + indexStart;
int n = texelFetch(_V0_ITa, V_ITa_ofs+5*i+1).x;
int p = texelFetch(_V0_ITa, V_ITa_ofs+5*i+2).x;
int eidx0 = texelFetch(_V0_ITa, V_ITa_ofs+5*i+3).x;
int eidx1 = texelFetch(_V0_ITa, V_ITa_ofs+5*i+4).x;
float weight = vertexPass
? texelFetch(_V0_S, V_W_ofs+i).x
: 1.0 - texelFetch(_V0_S, V_W_ofs+i).x;
// In the case of fractional weight, the weight must be inverted since
// the value is shared with the k_Smooth kernel (statistically the
// k_Smooth kernel runs much more often than this one)
if (weight>0.0 && weight<1.0 && n > 0)
weight=1.0-weight;
Vertex dst;
if(! vertexPass)
clear(dst);
else
dst = readVertex(i + indexOffset);
if (eidx0==-1 || (vertexPass==false && (n==-1)) ) {
addWithWeight(dst, readVertex(p), weight);
} else {
addWithWeight(dst, readVertex(p), weight * 0.75f);
addWithWeight(dst, readVertex(eidx0), weight * 0.125f);
addWithWeight(dst, readVertex(eidx1), weight * 0.125f);
}
if(! vertexPass)
addVaryingWithWeight(dst, readVertex(p), 1);
writeVertex(dst);
}
// Vertex-vertices compute Kernels 'B' / k_Dart and k_Smooth rules
subroutine(computeKernelType)
void catmarkComputeVertexB()
{
int i = gl_VertexID + indexStart;
int h = texelFetch(_V0_ITa, V_ITa_ofs+5*i).x;
#ifdef OPT_CATMARK_V_IT_VEC2
int h2 = h/2;
#endif
int n = texelFetch(_V0_ITa, V_ITa_ofs+5*i+1).x;
int p = texelFetch(_V0_ITa, V_ITa_ofs+5*i+2).x;
float weight = texelFetch(_V0_S, V_W_ofs+i).x;
float wp = 1.0/float(n*n);
float wv = (n-2.0) * n * wp;
Vertex dst;
clear(dst);
addWithWeight(dst, readVertex(p), weight * wv);
for(int j = 0; j < n; ++j){
#ifdef OPT_CATMARK_V_IT_VEC2
ivec2 v0it = texelFetch(_V0_IT, V_IT_ofs/2+h2+j).xy;
addWithWeight(dst, readVertex(v0it.x), weight * wp);
addWithWeight(dst, readVertex(v0it.y), weight * wp);
#else
addWithWeight(dst, readVertex(texelFetch(_V0_IT, V_IT_ofs+h+j*2).x), weight * wp);
addWithWeight(dst, readVertex(texelFetch(_V0_IT, V_IT_ofs+h+j*2+1).x), weight * wp);
#endif
}
addVaryingWithWeight(dst, readVertex(p), 1);
writeVertex(dst);
}
// Vertex-vertices compute Kernels 'B' / k_Dart and k_Smooth rules
subroutine(computeKernelType)
void loopComputeVertexB()
{
float PI = 3.14159265358979323846264;
int i = gl_VertexID + indexStart;
int h = texelFetch(_V0_ITa, V_ITa_ofs+5*i).x;
int n = texelFetch(_V0_ITa, V_ITa_ofs+5*i+1).x;
int p = texelFetch(_V0_ITa, V_ITa_ofs+5*i+2).x;
float weight = texelFetch(_V0_S, V_W_ofs+i).x;
float wp = 1.0/n;
float beta = 0.25 * cos(PI*2.0f*wp)+0.375f;
beta = beta * beta;
beta = (0.625f-beta)*wp;
Vertex dst;
clear(dst);
addWithWeight(dst, readVertex(p), weight * (1.0-(beta*n)));
for(int j = 0; j < n; ++j){
addWithWeight(dst, readVertex(texelFetch(_V0_IT, V_IT_ofs+h+j).x), weight * beta);
}
addVaryingWithWeight(dst, readVertex(p), 1);
writeVertex(dst);
}
void main()
{
// call subroutine
computeKernel();
}

View File

@ -65,7 +65,5 @@ namespace OPENSUBDIV_VERSION {
OsdKernelDispatcher::Factory OsdKernelDispatcher::Factory::_instance;
} // end namespace OPENSUBDIV_VERSION
using namespace OPENSUBDIV_VERSION;
} // end namespace OpenSubdiv

View File

@ -3,7 +3,8 @@
#include <stdio.h>
#define OSD_STRINGIFY(src) #src
#define OSD_STRINGIFY(src) OSD_XSTRINGIFY(src)
#define OSD_XSTRINGIFY(src) #src
#define CHECK_GL_ERROR(...) \
if(GLuint err = glGetError()) { \

View File

@ -239,7 +239,5 @@ OsdMesh::GetRefinedPoints(std::vector<float> &refinedPoints) {
}
} // end namespace OPENSUBDIV_VERSION
using namespace OPENSUBDIV_VERSION;
} // end namespace OpenSubdiv

View File

@ -0,0 +1,61 @@
#
# Copyright (C) Pixar. All rights reserved.
#
# This license governs use of the accompanying software. If you
# use the software, you accept this license. If you do not accept
# the license, do not use the software.
#
# 1. Definitions
# The terms "reproduce," "reproduction," "derivative works," and
# "distribution" have the same meaning here as under U.S.
# copyright law. A "contribution" is the original software, or
# any additions or changes to the software.
# A "contributor" is any person or entity that distributes its
# contribution under this license.
# "Licensed patents" are a contributor's patent claims that read
# directly on its contribution.
#
# 2. Grant of Rights
# (A) Copyright Grant- Subject to the terms of this license,
# including the license conditions and limitations in section 3,
# each contributor grants you a non-exclusive, worldwide,
# royalty-free copyright license to reproduce its contribution,
# prepare derivative works of its contribution, and distribute
# its contribution or any derivative works that you create.
# (B) Patent Grant- Subject to the terms of this license,
# including the license conditions and limitations in section 3,
# each contributor grants you a non-exclusive, worldwide,
# royalty-free license under its licensed patents to make, have
# made, use, sell, offer for sale, import, and/or otherwise
# dispose of its contribution in the software or derivative works
# of the contribution in the software.
#
# 3. Conditions and Limitations
# (A) No Trademark License- This license does not grant you
# rights to use any contributor's name, logo, or trademarks.
# (B) If you bring a patent claim against any contributor over
# patents that you claim are infringed by the software, your
# patent license from such contributor to the software ends
# automatically.
# (C) If you distribute any portion of the software, you must
# retain all copyright, patent, trademark, and attribution
# notices that are present in the software.
# (D) If you distribute any portion of the software in source
# code form, you may do so only under this license by including a
# complete copy of this license with your distribution. If you
# distribute any portion of the software in compiled or object
# code form, you may only do so under a license that complies
# with this license.
# (E) The software is licensed "as-is." You bear the risk of
# using it. The contributors give no express warranties,
# guarantees or conditions. You may have additional consumer
# rights under your local laws which this license cannot change.
# To the extent permitted under your local laws, the contributors
# exclude the implied warranties of merchantability, fitness for
# a particular purpose and non-infringement.
#
#
add_executable(stringify
main.cpp
)

View File

@ -0,0 +1,116 @@
//
// Copyright (C) Pixar. All rights reserved.
//
// This license governs use of the accompanying software. If you
// use the software, you accept this license. If you do not accept
// the license, do not use the software.
//
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and
// "distribution" have the same meaning here as under U.S.
// copyright law. A "contribution" is the original software, or
// any additions or changes to the software.
// A "contributor" is any person or entity that distributes its
// contribution under this license.
// "Licensed patents" are a contributor's patent claims that read
// directly on its contribution.
//
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license,
// including the license conditions and limitations in section 3,
// each contributor grants you a non-exclusive, worldwide,
// royalty-free copyright license to reproduce its contribution,
// prepare derivative works of its contribution, and distribute
// its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license,
// including the license conditions and limitations in section 3,
// each contributor grants you a non-exclusive, worldwide,
// royalty-free license under its licensed patents to make, have
// made, use, sell, offer for sale, import, and/or otherwise
// dispose of its contribution in the software or derivative works
// of the contribution in the software.
//
// 3. Conditions and Limitations
// (A) No Trademark License- This license does not grant you
// rights to use any contributor's name, logo, or trademarks.
// (B) If you bring a patent claim against any contributor over
// patents that you claim are infringed by the software, your
// patent license from such contributor to the software ends
// automatically.
// (C) If you distribute any portion of the software, you must
// retain all copyright, patent, trademark, and attribution
// notices that are present in the software.
// (D) If you distribute any portion of the software in source
// code form, you may do so only under this license by including a
// complete copy of this license with your distribution. If you
// distribute any portion of the software in compiled or object
// code form, you may only do so under a license that complies
// with this license.
// (E) The software is licensed "as-is." You bear the risk of
// using it. The contributors give no express warranties,
// guarantees or conditions. You may have additional consumer
// rights under your local laws which this license cannot change.
// To the extent permitted under your local laws, the contributors
// exclude the implied warranties of merchantability, fitness for
// a particular purpose and non-infringement.
//
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
std::string stringify( std::string const & line ) {
bool inconstant=false;
std::stringstream s;
for (int i=0; i<(int)line.size(); ++i) {
// escape double quotes
if (line[i]=='"') {
s << '\\' ;
inconstant = inconstant ? false : true;
}
// escape backslash
if (inconstant and line[i]=='\\')
s << '\\' ;
s << line[i];
}
return s.str();
}
int main(int argc, const char **argv) {
if (argc != 3) {
std::cerr << "Usage: quoter input-file output-file" << std::endl;
return 1;
}
std::ifstream input;
input.open(argv[1]);
if (not input.is_open()) {
std::cerr << "Can not read from: " << argv[1] << std::endl;
return 1;
}
std::ofstream output;
output.open(argv[2]);
if (not output.is_open()) {
std::cerr << "Can not write to: " << argv[2] << std::endl;
return 1;
}
std::string line;
while (not input.eof()) {
std::getline(input, line);
output << "\"" << stringify(line) << "\\n\"" << std::endl;
}
return 0;
}