code formatting / quick cleanup pass

This commit is contained in:
Manuel Kraemer 2012-06-09 11:12:34 -07:00
parent 92b1930927
commit 9988aad7f4
9 changed files with 276 additions and 271 deletions

View File

@ -65,88 +65,84 @@
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
OsdCpuKernelDispatcher::DeviceTable::~DeviceTable()
{
if(devicePtr) free(devicePtr);
OsdCpuKernelDispatcher::DeviceTable::~DeviceTable() {
if (devicePtr)
free(devicePtr);
}
void
OsdCpuKernelDispatcher::DeviceTable::Copy(int size, const void *table)
{
if(size > 0){
if(devicePtr) free(devicePtr);
OsdCpuKernelDispatcher::DeviceTable::Copy( int size, const void *table ) {
if (size > 0) {
if (devicePtr)
free(devicePtr);
devicePtr = malloc(size);
memcpy(devicePtr, table, size);
}
}
OsdCpuKernelDispatcher::OsdCpuKernelDispatcher(int levels, int numVertexElements, int numVaryingElements) :
OsdKernelDispatcher(levels),
_vertexBuffer(0),
_varyingBuffer(0),
_vbo(NULL),
_varyingVbo(NULL),
_numVertexElements(numVertexElements),
_numVaryingElements(numVaryingElements)
{
OsdCpuKernelDispatcher::OsdCpuKernelDispatcher( int levels, int numVertexElements, int numVaryingElements )
: OsdKernelDispatcher(levels), _vertexBuffer(0), _varyingBuffer(0), _vbo(NULL), _varyingVbo(NULL), _numVertexElements(numVertexElements), _numVaryingElements(numVaryingElements) {
_tables.resize(TABLE_MAX);
}
OsdCpuKernelDispatcher::~OsdCpuKernelDispatcher()
{
if(_vbo) delete[] _vbo;
if(_varyingVbo) delete[] _varyingVbo;
OsdCpuKernelDispatcher::~OsdCpuKernelDispatcher() {
if(_vbo)
delete[] _vbo;
if(_varyingVbo)
delete[] _varyingVbo;
}
void
OsdCpuKernelDispatcher::CopyTable(int tableIndex, size_t size, const void *ptr)
{
OsdCpuKernelDispatcher::CopyTable(int tableIndex, size_t size, const void *ptr) {
_tables[tableIndex].Copy(size, ptr);
}
void
OsdCpuKernelDispatcher::BeginLaunchKernel()
{
}
OsdCpuKernelDispatcher::BeginLaunchKernel() { }
void
OsdCpuKernelDispatcher::EndLaunchKernel()
{
}
OsdCpuKernelDispatcher::EndLaunchKernel() { }
void
OsdCpuKernelDispatcher::BindVertexBuffer(GLuint vertexBuffer, GLuint varyingBuffer)
{
OsdCpuKernelDispatcher::BindVertexBuffer(GLuint vertexBuffer, GLuint varyingBuffer) {
_vertexBuffer = vertexBuffer;
_varyingBuffer = varyingBuffer;
}
void
OsdCpuKernelDispatcher::UpdateVertexBuffer(size_t size, void *ptr)
{
OsdCpuKernelDispatcher::UpdateVertexBuffer(size_t size, void *ptr) {
memcpy(_vbo, ptr, size);
}
void
OsdCpuKernelDispatcher::UpdateVaryingBuffer(size_t size, void *ptr)
{
OsdCpuKernelDispatcher::UpdateVaryingBuffer(size_t size, void *ptr) {
memcpy(_varyingVbo, ptr, size);
}
void
OsdCpuKernelDispatcher::MapVertexBuffer()
{
OsdCpuKernelDispatcher::MapVertexBuffer() {
// XXX not efficient for CPU
// copy vbo content to kernel-buffer
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &_vboSize);
if(_vbo) delete[] _vbo;
if (_vbo)
delete[] _vbo;
_vbo = new float[_vboSize/sizeof(float)];
// too slow...
float *buffer = (float*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
if(buffer){
if (buffer) {
memcpy(_vbo, buffer, _vboSize);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
@ -154,62 +150,60 @@ OsdCpuKernelDispatcher::MapVertexBuffer()
}
void
OsdCpuKernelDispatcher::MapVaryingBuffer()
{
if(_varyingBuffer){
OsdCpuKernelDispatcher::MapVaryingBuffer() {
if (_varyingBuffer) {
glBindBuffer(GL_ARRAY_BUFFER, _varyingBuffer);
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &_varyingVboSize);
if(_varyingVbo) delete[] _varyingVbo;
if (_varyingVbo)
delete[] _varyingVbo;
_varyingVbo = new float[_varyingVboSize/sizeof(float)];
float *buffer = (float*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
if(buffer){
if (buffer)
memcpy(_varyingVbo, buffer, _varyingVboSize);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}
void
OsdCpuKernelDispatcher::UnmapVertexBuffer()
{
OsdCpuKernelDispatcher::UnmapVertexBuffer() {
// write back kernel-buffer to vbo
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
float *buffer = (float*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if(buffer){
if (buffer)
memcpy(buffer, _vbo, _vboSize);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void
OsdCpuKernelDispatcher::UnmapVaryingBuffer()
{
if(_varyingBuffer){
OsdCpuKernelDispatcher::UnmapVaryingBuffer() {
if (_varyingBuffer) {
// write back kernel-buffer to vbo
glBindBuffer(GL_ARRAY_BUFFER, _varyingBuffer);
float *buffer = (float*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if(buffer){
if (buffer)
memcpy(buffer, _varyingVbo, _varyingVboSize);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}
void
OsdCpuKernelDispatcher::Synchronize()
{
}
OsdCpuKernelDispatcher::Synchronize() { }
void
OsdCpuKernelDispatcher::ApplyCatmarkFaceVerticesKernel(FarMesh<OsdVertex> * mesh, int offset,
int level, int start, int end, void * data) const
{
OsdCpuKernelDispatcher::ApplyCatmarkFaceVerticesKernel( FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const {
VertexDescriptor vd(_numVertexElements, _numVaryingElements);
computeFace(&vd, _vbo, _varyingVbo,
@ -219,9 +213,8 @@ OsdCpuKernelDispatcher::ApplyCatmarkFaceVerticesKernel(FarMesh<OsdVertex> * mesh
}
void
OsdCpuKernelDispatcher::ApplyCatmarkEdgeVerticesKernel(FarMesh<OsdVertex> * mesh, int offset,
int level, int start, int end, void * data) const
{
OsdCpuKernelDispatcher::ApplyCatmarkEdgeVerticesKernel( FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const {
VertexDescriptor vd(_numVertexElements, _numVaryingElements);
computeEdge(&vd, _vbo, _varyingVbo,
@ -232,9 +225,8 @@ OsdCpuKernelDispatcher::ApplyCatmarkEdgeVerticesKernel(FarMesh<OsdVertex> * mesh
}
void
OsdCpuKernelDispatcher::ApplyCatmarkVertexVerticesKernelB(FarMesh<OsdVertex> * mesh, int offset,
int level, int start, int end, void * data) const
{
OsdCpuKernelDispatcher::ApplyCatmarkVertexVerticesKernelB( FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const {
VertexDescriptor vd(_numVertexElements, _numVaryingElements);
computeVertexB(&vd, _vbo, _varyingVbo,
@ -245,9 +237,8 @@ OsdCpuKernelDispatcher::ApplyCatmarkVertexVerticesKernelB(FarMesh<OsdVertex> * m
}
void
OsdCpuKernelDispatcher::ApplyCatmarkVertexVerticesKernelA(FarMesh<OsdVertex> * mesh, int offset,
bool pass, int level, int start, int end, void * data) const
{
OsdCpuKernelDispatcher::ApplyCatmarkVertexVerticesKernelA( FarMesh<OsdVertex> * mesh, int offset, bool pass, int level, int start, int end, void * data) const {
VertexDescriptor vd(_numVertexElements, _numVaryingElements);
computeVertexA(&vd, _vbo, _varyingVbo,
@ -257,9 +248,8 @@ OsdCpuKernelDispatcher::ApplyCatmarkVertexVerticesKernelA(FarMesh<OsdVertex> * m
}
void
OsdCpuKernelDispatcher::ApplyLoopEdgeVerticesKernel(FarMesh<OsdVertex> * mesh, int offset,
int level, int start, int end, void * data) const
{
OsdCpuKernelDispatcher::ApplyLoopEdgeVerticesKernel( FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const {
VertexDescriptor vd(_numVertexElements, _numVaryingElements);
computeEdge(&vd, _vbo, _varyingVbo,
@ -270,9 +260,8 @@ OsdCpuKernelDispatcher::ApplyLoopEdgeVerticesKernel(FarMesh<OsdVertex> * mesh, i
}
void
OsdCpuKernelDispatcher::ApplyLoopVertexVerticesKernelB(FarMesh<OsdVertex> * mesh, int offset,
int level, int start, int end, void * data) const
{
OsdCpuKernelDispatcher::ApplyLoopVertexVerticesKernelB( FarMesh<OsdVertex> * mesh, int offset, int level, int start, int end, void * data) const {
VertexDescriptor vd(_numVertexElements, _numVaryingElements);
computeLoopVertexB(&vd, _vbo, _varyingVbo,
@ -283,9 +272,8 @@ OsdCpuKernelDispatcher::ApplyLoopVertexVerticesKernelB(FarMesh<OsdVertex> * mesh
}
void
OsdCpuKernelDispatcher::ApplyLoopVertexVerticesKernelA(FarMesh<OsdVertex> * mesh, int offset,
bool pass, int level, int start, int end, void * data) const
{
OsdCpuKernelDispatcher::ApplyLoopVertexVerticesKernelA( FarMesh<OsdVertex> * mesh, int offset, bool pass, int level, int start, int end, void * data) const {
VertexDescriptor vd(_numVertexElements, _numVaryingElements);
computeVertexA(&vd, _vbo, _varyingVbo,

View File

@ -75,39 +75,43 @@ public:
OsdCpuKernelDispatcher(int levels, int numVertexElements, int numVaryingElements);
virtual ~OsdCpuKernelDispatcher();
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 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){
static OsdKernelDispatcher * Create(int levels, int numVertexElements, int numVaryingElements) {
return new OsdCpuKernelDispatcher(levels, numVertexElements, numVaryingElements);
}
static void Register() {
@ -116,21 +120,27 @@ public:
protected:
struct DeviceTable
{
DeviceTable() : devicePtr(NULL) {}
~DeviceTable();
struct DeviceTable {
DeviceTable() : devicePtr(NULL) { }
~DeviceTable();
void Copy(int size, const void *ptr);
void *devicePtr;
void *devicePtr;
};
std::vector<DeviceTable> _tables;
GLuint _vertexBuffer, _varyingBuffer;
float *_vbo, *_varyingVbo;
int _vboSize, _varyingVboSize;
int _numVertexElements, _numVaryingElements;
GLuint _vertexBuffer,
_varyingBuffer;
float *_vbo,
*_varyingVbo;
int _vboSize,
_varyingVboSize,
_numVertexElements,
_numVaryingElements;
};
} // end namespace OPENSUBDIV_VERSION

View File

@ -61,15 +61,15 @@
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
extern void computeFace(const VertexDescriptor *vdesc, float * vertex, float * varying,
const int *F_IT, const int *F_ITa, int offset, int start, int end)
{
extern void computeFace( const VertexDescriptor *vdesc, float * vertex, float * varying, const int *F_IT, const int *F_ITa, int offset, int start, int end) {
int ve = vdesc->numVertexElements;
int vev = vdesc->numVaryingElements;
#ifdef _OPENMP
#pragma omp parallel for
#endif
for(int i = start; i < end; i++){
for (int i = start; i < end; i++) {
int h = F_ITa[2*i];
int n = F_ITa[2*i+1];
@ -78,26 +78,25 @@ extern void computeFace(const VertexDescriptor *vdesc, float * vertex, float * v
// XXX: should use local vertex struct variable instead of accumulating directly into global memory.
float *dst = &vertex[(offset + i)*ve];
float *dstVarying = &varying[(offset + i)*vev];
vdesc->clear(dst, dstVarying);
vdesc->Clear(dst, dstVarying);
for(int j=0; j<n; ++j){
for (int j=0; j<n; ++j) {
int index = F_IT[h+j];
vdesc->addWithWeight(dst, &vertex[index*ve], weight);
vdesc->addVaryingWithWeight(dstVarying, &varying[index*vev], weight);
vdesc->AddWithWeight(dst, &vertex[index*ve], weight);
vdesc->AddVaryingWithWeight(dstVarying, &varying[index*vev], weight);
}
}
}
extern void computeEdge(const VertexDescriptor *vdesc, float *vertex, float *varying,
const int *E_IT, const float *E_W, int offset, int start, int end)
{
extern void computeEdge( const VertexDescriptor *vdesc, float *vertex, float *varying, const int *E_IT, const float *E_W, int offset, int start, int end) {
int ve = vdesc->numVertexElements;
int vev = vdesc->numVaryingElements;
#ifdef _OPENMP
#pragma omp parallel for
#endif
for(int i = start; i < end; i++){
for (int i = start; i < end; i++) {
int eidx0 = E_IT[4*i+0];
int eidx1 = E_IT[4*i+1];
int eidx2 = E_IT[4*i+2];
@ -107,34 +106,32 @@ extern void computeEdge(const VertexDescriptor *vdesc, float *vertex, float *var
float *dst = &vertex[(offset+i)*ve];
float *dstVarying = &varying[(offset+i)*vev];
vdesc->clear(dst, dstVarying);
vdesc->Clear(dst, dstVarying);
vdesc->addWithWeight(dst, &vertex[eidx0*ve], vertWeight);
vdesc->addWithWeight(dst, &vertex[eidx1*ve], vertWeight);
vdesc->AddWithWeight(dst, &vertex[eidx0*ve], vertWeight);
vdesc->AddWithWeight(dst, &vertex[eidx1*ve], vertWeight);
if(eidx2 != -1){
if (eidx2 != -1) {
float faceWeight = E_W[i*2+1];
vdesc->addWithWeight(dst, &vertex[eidx2*ve], faceWeight);
vdesc->addWithWeight(dst, &vertex[eidx3*ve], faceWeight);
vdesc->AddWithWeight(dst, &vertex[eidx2*ve], faceWeight);
vdesc->AddWithWeight(dst, &vertex[eidx3*ve], faceWeight);
}
vdesc->addVaryingWithWeight(dstVarying, &varying[eidx0*vev], 0.5f);
vdesc->addVaryingWithWeight(dstVarying, &varying[eidx1*vev], 0.5f);
vdesc->AddVaryingWithWeight(dstVarying, &varying[eidx0*vev], 0.5f);
vdesc->AddVaryingWithWeight(dstVarying, &varying[eidx1*vev], 0.5f);
}
}
extern void computeVertexA(const VertexDescriptor *vdesc, float *vertex, float *varying,
const int *V_ITa, const float *V_W,
int offset, int start, int end, int pass)
{
extern void computeVertexA(const VertexDescriptor *vdesc, float *vertex, float *varying, const int *V_ITa, const float *V_W, int offset, int start, int end, int pass) {
int ve = vdesc->numVertexElements;
int vev = vdesc->numVaryingElements;
#ifdef _OPENMP
#pragma omp parallel for
#endif
for(int i = start; i < end; i++){
for (int i = start; i < end; i++) {
int n = V_ITa[5*i+1];
int p = V_ITa[5*i+2];
int eidx0 = V_ITa[5*i+3];
@ -151,32 +148,30 @@ extern void computeVertexA(const VertexDescriptor *vdesc, float *vertex, float *
float *dst = &vertex[(offset+i)*ve];
float *dstVarying = &varying[(offset+i)*vev];
if(not pass)
vdesc->clear(dst, dstVarying);
vdesc->Clear(dst, dstVarying);
if (eidx0==-1 || (pass==0 && (n==-1)) ) {
vdesc->addWithWeight(dst, &vertex[p*ve], weight);
vdesc->AddWithWeight(dst, &vertex[p*ve], weight);
} else {
vdesc->addWithWeight(dst, &vertex[p*ve], weight * 0.75f);
vdesc->addWithWeight(dst, &vertex[eidx0*ve], weight * 0.125f);
vdesc->addWithWeight(dst, &vertex[eidx1*ve], weight * 0.125f);
vdesc->AddWithWeight(dst, &vertex[p*ve], weight * 0.75f);
vdesc->AddWithWeight(dst, &vertex[eidx0*ve], weight * 0.125f);
vdesc->AddWithWeight(dst, &vertex[eidx1*ve], weight * 0.125f);
}
if(not pass)
vdesc->addVaryingWithWeight(dstVarying, &varying[p*vev], 1.0);
if (not pass)
vdesc->AddVaryingWithWeight(dstVarying, &varying[p*vev], 1.0);
}
}
extern void computeVertexB(const VertexDescriptor *vdesc, float *vertex, float *varying,
const int *V_ITa, const int *V_IT, const float *V_W,
int offset, int start, int end)
{
extern void computeVertexB(const VertexDescriptor *vdesc, float *vertex, float *varying, const int *V_ITa, const int *V_IT, const float *V_W, int offset, int start, int end) {
int ve = vdesc->numVertexElements;
int vev = vdesc->numVaryingElements;
#ifdef _OPENMP
#pragma omp parallel for
#endif
for(int i = start; i < end; i++){
for (int i = start; i < end; i++) {
int h = V_ITa[5*i];
int n = V_ITa[5*i+1];
int p = V_ITa[5*i+2];
@ -187,29 +182,27 @@ extern void computeVertexB(const VertexDescriptor *vdesc, float *vertex, float *
float *dst = &vertex[(offset+i)*ve];
float *dstVarying = &varying[(offset+i)*vev];
vdesc->clear(dst, dstVarying);
vdesc->Clear(dst, dstVarying);
vdesc->addWithWeight(dst, &vertex[p*ve], weight * wv);
vdesc->AddWithWeight(dst, &vertex[p*ve], weight * wv);
for(int j = 0; j < n; ++j){
vdesc->addWithWeight(dst, &vertex[V_IT[h+j*2]*ve], weight * wp);
vdesc->addWithWeight(dst, &vertex[V_IT[h+j*2+1]*ve], weight * wp);
for (int j = 0; j < n; ++j) {
vdesc->AddWithWeight(dst, &vertex[V_IT[h+j*2]*ve], weight * wp);
vdesc->AddWithWeight(dst, &vertex[V_IT[h+j*2+1]*ve], weight * wp);
}
vdesc->addVaryingWithWeight(dstVarying, &varying[p*vev], 1.0);
vdesc->AddVaryingWithWeight(dstVarying, &varying[p*vev], 1.0);
}
}
extern void computeLoopVertexB(const VertexDescriptor *vdesc, float *vertex, float *varying,
const int *V_ITa, const int *V_IT, const float *V_W,
int offset, int start, int end)
{
extern void computeLoopVertexB(const VertexDescriptor *vdesc, float *vertex, float *varying, const int *V_ITa, const int *V_IT, const float *V_W, int offset, int start, int end) {
int ve = vdesc->numVertexElements;
int vev = vdesc->numVaryingElements;
#ifdef _OPENMP
#pragma omp parallel for
#endif
for(int i = start; i < end; i++){
for (int i = start; i < end; i++) {
int h = V_ITa[5*i];
int n = V_ITa[5*i+1];
int p = V_ITa[5*i+2];
@ -222,15 +215,14 @@ extern void computeLoopVertexB(const VertexDescriptor *vdesc, float *vertex, flo
float *dst = &vertex[(offset+i)*ve];
float *dstVarying = &varying[(offset+i)*vev];
vdesc->clear(dst, dstVarying);
vdesc->Clear(dst, dstVarying);
vdesc->addWithWeight(dst, &vertex[p*ve], weight * (1.0f - (beta * n)));
vdesc->AddWithWeight(dst, &vertex[p*ve], weight * (1.0f - (beta * n)));
for(int j = 0; j < n; ++j){
vdesc->addWithWeight(dst, &vertex[V_IT[h+j]*ve], weight * beta);
}
for (int j = 0; j < n; ++j)
vdesc->AddWithWeight(dst, &vertex[V_IT[h+j]*ve], weight * beta);
vdesc->addVaryingWithWeight(dstVarying, &varying[p*vev], 1.0f);
vdesc->AddVaryingWithWeight(dstVarying, &varying[p*vev], 1.0f);
}
}

View File

@ -60,29 +60,27 @@
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
struct VertexDescriptor
{
VertexDescriptor(int numVertexElem, int numVaryingElem) :
numVertexElements(numVertexElem), numVaryingElements(numVaryingElem) {}
struct VertexDescriptor {
void clear(float *vertex, float *varying) const{
for(int i = 0; i < numVertexElements; ++i){
VertexDescriptor(int numVertexElem, int numVaryingElem)
: numVertexElements(numVertexElem), numVaryingElements(numVaryingElem) { }
void Clear(float *vertex, float *varying) const {
for (int i = 0; i < numVertexElements; ++i)
vertex[i] = 0.0f;
}
for(int i = 0; i < numVaryingElements; ++i){
for (int i = 0; i < numVaryingElements; ++i)
varying[i] = 0.0f;
}
}
void addWithWeight(float *vertex, const float *src, float weight) const {
for(int i = 0; i < numVertexElements; ++i){
void AddWithWeight(float *vertex, const float *src, float weight) const {
for (int i = 0; i < numVertexElements; ++i)
vertex[i] += src[i] * weight;
}
}
void addVaryingWithWeight(float *varying, const float *src, float weight) const {
for(int i = 0; i < numVaryingElements; ++i){
void AddVaryingWithWeight(float *varying, const float *src, float weight) const {
for (int i = 0; i < numVaryingElements; ++i)
varying[i] += src[i] * weight;
}
}
int numVertexElements;
int numVaryingElements;
};

View File

@ -1,36 +1,37 @@
#include <GL/glew.h>
static void
DumpBuffer(GLuint buffer)
{
DumpBuffer(GLuint buffer) {
printf("----------------------------------------------------\n");
glBindBuffer(GL_ARRAY_BUFFER, buffer);
int size = 0;
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
float *p = (float*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
if(p){
for(int i = 0; i < size/16; i++){
if (p)
for (int i = 0; i < size/16; i++, p+=4)
printf("%d: %f %f %f %f\n", i, p[0], p[1], p[2], p[3]);
p += 4;
}
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
static void
DumpBufferInt(GLuint buffer)
{
if(buffer == 0) return;
DumpBufferInt(GLuint buffer) {
if (buffer == 0) return;
printf("---------------------------------------------------\n");
glBindBuffer(GL_ARRAY_BUFFER, buffer);
int size = 0;
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
unsigned int *p = (unsigned int*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
if(p){
for(int i = 0; i < size/4; i++){
if (p) {
for (int i = 0; i < size/4; i++)
printf("%04x ", *p++);
}
printf("\n");
}
glUnmapBuffer(GL_ARRAY_BUFFER);
@ -38,8 +39,8 @@ DumpBufferInt(GLuint buffer)
}
static void
DebugAttribs(GLuint program)
{
DebugAttribs(GLuint program) {
GLint numAttributes = 0;
glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &numAttributes);
if (numAttributes == 0) {
@ -55,22 +56,21 @@ DebugAttribs(GLuint program)
for (int i=0; i<numAttributes; ++i) {
GLint size;
GLenum type;
glGetActiveAttrib(program, i,
maxNameLength, NULL,
&size, &type, name);
glGetActiveAttrib(program, i, maxNameLength, NULL, &size, &type, name);
printf("Attrib %s, size=%d, type=%x\n", name, size, type);
}
}
static void
DebugProgram(GLuint program)
{
DebugProgram(GLuint program) {
unsigned char buffer[1*1024*1024];
GLsizei length = 0;
GLenum format;
glGetProgramBinary(program, 10*1024*1024, &length, &format, buffer);
FILE *fp =fopen ("out.bin", "wb");
if(fp){
if (fp) {
fwrite(buffer, 1, length, fp);
fclose(fp);
}

View File

@ -69,44 +69,50 @@
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
class OsdKernelDispatcher : public FarDispatcher<OsdVertex>
{
class OsdKernelDispatcher : public FarDispatcher<OsdVertex> {
public:
OsdKernelDispatcher (int maxLevel) : _maxLevel(maxLevel) {}
virtual ~OsdKernelDispatcher() {}
OsdKernelDispatcher (int maxLevel) : _maxLevel(maxLevel) { }
virtual ~OsdKernelDispatcher() { }
virtual void CopyTable(int tableIndex, size_t size, const void *ptr) = 0;
virtual void BeginLaunchKernel() = 0;
virtual void EndLaunchKernel() = 0;
virtual void BindVertexBuffer(GLuint vertexBuffer, GLuint varyingBuffer) = 0;
virtual void UpdateVertexBuffer(size_t size, void *ptr) = 0;
virtual void UpdateVaryingBuffer(size_t size, void *ptr) = 0;
virtual void MapVertexBuffer() = 0;
virtual void MapVaryingBuffer() = 0;
virtual void UnmapVertexBuffer() = 0;
virtual void UnmapVaryingBuffer() = 0;
virtual void Synchronize() = 0;
template<class T> void UpdateTable(int tableIndex, const T & table)
{
template<class T> void UpdateTable(int tableIndex, const T & table) {
CopyTable(tableIndex, table.GetMemoryUsed(), table[0]);
_tableOffsets[tableIndex].resize(_maxLevel);
for(int i = 0; i < _maxLevel; ++i){
for (int i = 0; i < _maxLevel; ++i)
_tableOffsets[tableIndex][i] = table[i] - table[0];
}
}
static OsdKernelDispatcher *CreateKernelDispatcher(
const std::string &kernel,
int levels,
int numVertexElements,
int numVaryingElements) {
return Factory::GetInstance().Create(
kernel, levels, numVertexElements, numVaryingElements);
static OsdKernelDispatcher *CreateKernelDispatcher( const std::string &kernel, int levels, int numVertexElements, int numVaryingElements ) {
return Factory::GetInstance().Create( kernel, levels, numVertexElements, numVaryingElements );
}
enum { E_IT,
@ -116,37 +122,40 @@ public:
V_W,
F_IT,
F_ITa,
TABLE_MAX
};
TABLE_MAX };
protected:
class Factory {
private:
typedef OsdKernelDispatcher *(*Creator)(
int levels, int numVertexElements, int numVaryingElements);
typedef std::map<const std::string, Creator> CreatorMap;
CreatorMap _creators;
public:
typedef OsdKernelDispatcher *(*Creator)( int levels, int numVertexElements, int numVaryingElements );
typedef std::map<const std::string, Creator> CreatorMap;
bool Register(const std::string &kernel, Creator creator) {
return _creators.insert(CreatorMap::value_type(kernel, creator)).second;
}
bool Unregister(const std::string &kernel) {
return _creators.erase(kernel) == 1;
}
OsdKernelDispatcher *Create(
const std::string &kernel,
int levels,
int numVertexElements,
int numVaryingElements) {
OsdKernelDispatcher *Create ( const std::string &kernel, int levels, int numVertexElements, int numVaryingElements ) {
CreatorMap::const_iterator it = _creators.find(kernel);
if (it != _creators.end())
return (it->second)(levels, numVertexElements, numVaryingElements);
return NULL;
}
static Factory &GetInstance() {
return _instance;
}
static Factory _instance;
static Factory _instance;
private:
CreatorMap _creators;
};
protected:

View File

@ -3,6 +3,8 @@
#include <stdio.h>
#define OSD_STRINGIFY(src) #src
#define CHECK_GL_ERROR(...) \
if(GLuint err = glGetError()) { \
printf("GL error %x :", err); \

View File

@ -67,18 +67,15 @@
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
OsdMesh::OsdMesh(int numVertexElements, int numVaryingElements) :
_numVertexElements(numVertexElements),
_numVaryingElements(numVaryingElements),
_mMesh(NULL),
_dispatcher(NULL)
{
OsdMesh::OsdMesh(int numVertexElements, int numVaryingElements)
: _numVertexElements(numVertexElements), _numVaryingElements(numVaryingElements), _mMesh(NULL), _dispatcher(NULL) {
glGenBuffers(1, &_vertexBuffer);
glGenBuffers(1, &_varyingBuffer);
}
OsdMesh::~OsdMesh()
{
OsdMesh::~OsdMesh() {
if(_dispatcher) delete _dispatcher;
if(_mMesh) delete _mMesh;
@ -87,11 +84,11 @@ OsdMesh::~OsdMesh()
}
bool
OsdMesh::Create(OsdHbrMesh *hbrMesh, int level, const std::string &kernel)
{
if(_dispatcher) delete _dispatcher;
_dispatcher = OsdKernelDispatcher::CreateKernelDispatcher(kernel, level,
_numVertexElements, _numVaryingElements);
OsdMesh::Create(OsdHbrMesh *hbrMesh, int level, const std::string &kernel) {
if (_dispatcher)
delete _dispatcher;
_dispatcher = OsdKernelDispatcher::CreateKernelDispatcher(kernel, level, _numVertexElements, _numVaryingElements);
if(_dispatcher == NULL){
OSD_ERROR("Unknown kernel %s\n", kernel.c_str());
return false;
@ -99,10 +96,10 @@ OsdMesh::Create(OsdHbrMesh *hbrMesh, int level, const std::string &kernel)
_level = level;
// create cctable
// create Far mesh
OSD_DEBUG("Create MeshFactory\n");
FarMeshFactory<OsdVertex> meshFactory(hbrMesh, _level);
OSD_DEBUG("Create Mesh\n");
_mMesh = meshFactory.Create(_dispatcher);
@ -118,12 +115,12 @@ OsdMesh::Create(OsdHbrMesh *hbrMesh, int level, const std::string &kernel)
_dispatcher->UpdateTable(OsdKernelDispatcher::E_W, table->Get_E_W());
_dispatcher->UpdateTable(OsdKernelDispatcher::V_W, table->Get_V_W());
if(const FarCatmarkSubdivisionTables<OsdVertex>* cctable =
dynamic_cast<const FarCatmarkSubdivisionTables<OsdVertex>*>(table)){
if ( const FarCatmarkSubdivisionTables<OsdVertex> * cctable =
dynamic_cast<const FarCatmarkSubdivisionTables<OsdVertex>*>(table) ) {
// catmark
_dispatcher->UpdateTable(OsdKernelDispatcher::F_IT, cctable->Get_F_IT());
_dispatcher->UpdateTable(OsdKernelDispatcher::F_ITa, cctable->Get_F_ITa());
}else{
} else {
// XXX for glsl shader...
_dispatcher->CopyTable(OsdKernelDispatcher::F_IT, 0, NULL);
_dispatcher->CopyTable(OsdKernelDispatcher::F_ITa, 0, NULL);
@ -149,11 +146,9 @@ OsdMesh::Create(OsdHbrMesh *hbrMesh, int level, const std::string &kernel)
return true;
}
// ------------------------------------------------------------------------------
void
OsdMesh::UpdatePoints(const std::vector<float> &points)
{
OsdMesh::UpdatePoints(const std::vector<float> &points) {
int numCoarseVertices = _mMesh->GetNumCoarseVertices();
if(numCoarseVertices * _numVertexElements != points.size()) {
OSD_ERROR("UpdatePoints points size mismatch %d != %d\n", numCoarseVertices, (int)points.size());
@ -162,11 +157,9 @@ OsdMesh::UpdatePoints(const std::vector<float> &points)
float * updateVertexBuffer = new float[GetNumCoarseVertices() * _numVertexElements];
float *p = updateVertexBuffer;
for(int i = 0; i < numCoarseVertices; ++i){
for(int j = 0; j < _numVertexElements; ++j){
for (int i = 0; i < numCoarseVertices; ++i)
for (int j = 0; j < _numVertexElements; ++j)
*p++ = points[i*_numVertexElements+j];
}
}
// send to gpu
_dispatcher->MapVertexBuffer();
@ -181,10 +174,10 @@ OsdMesh::UpdatePoints(const std::vector<float> &points)
}
void
OsdMesh::UpdateVaryings(const std::vector<float> &varyings)
{
OsdMesh::UpdateVaryings(const std::vector<float> &varyings) {
int numCoarseVertices = _mMesh->GetNumCoarseVertices();
if(numCoarseVertices * _numVaryingElements != varyings.size()) {
if (numCoarseVertices * _numVaryingElements != varyings.size()) {
OSD_ERROR("UpdateVaryings array size mismatch %d != %d\n", numCoarseVertices, (int)varyings.size());
return;
}
@ -199,8 +192,8 @@ OsdMesh::UpdateVaryings(const std::vector<float> &varyings)
}
void
OsdMesh::Subdivide()
{
OsdMesh::Subdivide() {
_dispatcher->MapVertexBuffer();
_dispatcher->MapVaryingBuffer();
_dispatcher->BeginLaunchKernel();
@ -210,24 +203,24 @@ OsdMesh::Subdivide()
_dispatcher->EndLaunchKernel();
_dispatcher->UnmapVertexBuffer();
_dispatcher->UnmapVaryingBuffer();
//DumpBuffer(_varyingBuffer);
}
void
OsdMesh::Synchronize()
{
OsdMesh::Synchronize() {
_dispatcher->Synchronize();
}
void
OsdMesh::GetRefinedPoints(std::vector<float> &refinedPoints)
{
OsdMesh::GetRefinedPoints(std::vector<float> &refinedPoints) {
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
int size = 0;
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
int numTotalVertices = _mMesh->GetNumVertices();
if(size != numTotalVertices*_numVertexElements*sizeof(float)){
if (size != numTotalVertices*_numVertexElements*sizeof(float)) {
OSD_ERROR("vertex count mismatch %d != %d\n", (int)(size/_numVertexElements/sizeof(float)), numTotalVertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
return;
@ -235,13 +228,11 @@ OsdMesh::GetRefinedPoints(std::vector<float> &refinedPoints)
refinedPoints.resize(numTotalVertices*_numVertexElements);
float *p = (float*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
if(p){
for(int i = 0; i < numTotalVertices; ++i){
for(int j = 0; j < _numVertexElements; ++j){
if (p)
for (int i = 0; i < numTotalVertices; ++i)
for (int j = 0; j < _numVertexElements; ++j)
refinedPoints[i*_numVertexElements+j] = *p++;
}
}
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);

View File

@ -79,39 +79,54 @@ typedef HbrVertex<OsdVertex> OsdHbrVertex;
typedef HbrHalfedge<OsdVertex> OsdHbrHalfedge;
typedef HbrFace<OsdVertex> OsdHbrFace;
class OsdMesh
{
class OsdMesh {
public:
OsdMesh(int numVertexElements, int numVaryingElements);
virtual ~OsdMesh();
bool Create(OsdHbrMesh *hbrMesh, int level, const std::string &kernel);
void UpdatePoints(const std::vector<float> &points);
void GetRefinedPoints(std::vector<float> &refinedPoints);
void UpdateVaryings(const std::vector<float> &varyings);
void GetRefinedVaryings(std::vector<float> &refinedVaryings);
FarMesh<OsdVertex> *GetFarMesh() { return _mMesh; }
void Subdivide();
void Synchronize();
int GetNumVertexElements() const { return _numVertexElements; }
int GetNumVaryingElements() const { return _numVaryingElements; }
GLuint GetVertexBuffer() const { return _vertexBuffer; }
GLuint GetVaryingBuffer() const { return _varyingBuffer; }
int GetTotalVertices() const { return _mMesh->GetNumVertices(); }
int GetNumCoarseVertices() const { return _mMesh->GetNumCoarseVertices(); }
protected:
int GetTotalVertices() const { return _mMesh->GetNumVertices(); }
int GetNumCoarseVertices() const { return _mMesh->GetNumCoarseVertices(); }
FarMesh<OsdVertex> *_mMesh;
int _numVertexElements, _numVaryingElements;
int _level;
int _numVertexElements,
_numVaryingElements,
_level;
GLuint _vertexBuffer, _varyingBuffer;
OsdKernelDispatcher * _dispatcher;
};