From now on, hbr assumes that its clients will provide the defition of a

mutex class with Lock / Unlock public functions.

- remove Mutex implementation from Hbr (and revert to original PRman code)
- provide a Mutex class stub in osd
- add some forward declarations in OsdMesh to limit some of the mutex spills
- #include <osd/mutex.h> where needed (little hackish until we can refactor
some of far better)
- remove ILM_BASE from some CMakeLists

Closes #48
This commit is contained in:
manuelk 2012-08-10 15:14:02 -07:00
parent bc959f6411
commit e2217e182c
20 changed files with 152 additions and 108 deletions

View File

@ -63,6 +63,7 @@
#include <GL/glut.h> #include <GL/glut.h>
#endif #endif
#include <osd/mutex.h>
#include <osd/vertex.h> #include <osd/vertex.h>
#include <osd/mesh.h> #include <osd/mesh.h>
#include <osd/cpuDispatcher.h> #include <osd/cpuDispatcher.h>

View File

@ -64,6 +64,7 @@
#include <GL/glut.h> #include <GL/glut.h>
#endif #endif
#include <osd/mutex.h>
#include <osd/vertex.h> #include <osd/vertex.h>
#include <osd/mesh.h> #include <osd/mesh.h>
#include <osd/cpuDispatcher.h> #include <osd/cpuDispatcher.h>

View File

@ -86,6 +86,7 @@
#include <maya/MPxVertexBufferGenerator.h> #include <maya/MPxVertexBufferGenerator.h>
#include <maya/MStateManager.h> #include <maya/MStateManager.h>
#include <osd/mutex.h>
#include <osd/mesh.h> #include <osd/mesh.h>
#include <osd/pTexture.h> #include <osd/pTexture.h>
#include <osd/cpuDispatcher.h> #include <osd/cpuDispatcher.h>

View File

@ -58,6 +58,7 @@
#define OSD_HBR_UTIL_H #define OSD_HBR_UTIL_H
#include <vector> #include <vector>
#include <osd/mutex.h>
#include <osd/mesh.h> #include <osd/mesh.h>
extern "C" OpenSubdiv::OsdHbrMesh * ConvertToHBR(int nVertices, extern "C" OpenSubdiv::OsdHbrMesh * ConvertToHBR(int nVertices,

View File

@ -60,6 +60,7 @@
// Include this first to avoid winsock2.h problems on Windows: // Include this first to avoid winsock2.h problems on Windows:
#include <maya/MTypes.h> #include <maya/MTypes.h>
#include <osd/mutex.h>
#include <osd/cpuDispatcher.h> #include <osd/cpuDispatcher.h>
#include <osd/cudaDispatcher.h> #include <osd/cudaDispatcher.h>
#include <osd/mesh.h> #include <osd/mesh.h>

View File

@ -58,6 +58,7 @@
#define OSD_HBR_UTIL_H #define OSD_HBR_UTIL_H
#include <vector> #include <vector>
#include <osd/mutex.h>
#include <osd/mesh.h> #include <osd/mesh.h>
extern "C" OpenSubdiv::OsdHbrMesh * ConvertToHBR(int nVertices, extern "C" OpenSubdiv::OsdHbrMesh * ConvertToHBR(int nVertices,

View File

@ -62,6 +62,7 @@
#include <GL/glut.h> #include <GL/glut.h>
#endif #endif
#include <osd/mutex.h>
#include <osd/vertex.h> #include <osd/vertex.h>
#include <osd/mesh.h> #include <osd/mesh.h>
#include <osd/cpuDispatcher.h> #include <osd/cpuDispatcher.h>

View File

@ -57,20 +57,17 @@
#ifndef HBRMESH_H #ifndef HBRMESH_H
#define HBRMESH_H #define HBRMESH_H
#if PRMAN
#include "libtarget/TgMalloc.h" // only for alloca
#include "libtarget/TgThread.h"
#endif
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
#include <vector> #include <vector>
#include <set> #include <set>
#include <iostream> #include <iostream>
#if PRMAN
#include "path-to-TgSpinLock"
#elif MENV
#include "bedrock/tf/mutex.h"
#else
#include "IlmThreadMutex.h"
#endif
#include "../hbr/vertex.h" #include "../hbr/vertex.h"
#include "../hbr/face.h" #include "../hbr/face.h"
#include "../hbr/hierarchicalEdit.h" #include "../hbr/hierarchicalEdit.h"
@ -262,42 +259,14 @@ public:
void FreeTransientData(); void FreeTransientData();
private: private:
// The mutex type depends on where hbr is being used.
#if PRMAN #if PRMAN
// This code is intended to be shared with PRman which provides its own
// TgSpinLock mutex. Other clients are responsible for providing a Mutex
// object with public Lock() and Unlock() functions.
typedef TgSpinLock Mutex; typedef TgSpinLock Mutex;
#elif MENV
typedef TfMutex Mutex;
#else
typedef IlmThread::Mutex Mutex;
#endif #endif
#if PRMAN or MENV mutable Mutex m_mutex;
// Helper class used to automatically unlock a mutex
// when an instance of this class goes out of scope.
class ScopedLock {
public:
ScopedLock(Mutex *mutex) : _mutex(mutex) {
mutex->Lock();
}
~ScopedLock() {
Release();
}
void Release() {
if (_mutex) {
_mutex->Unlock();
}
_mutex = NULL;
}
private:
Mutex *_mutex;
};
#endif
// Mutex used to lock access to the "vertices" data member.
mutable Mutex m_verticesMutex;
private:
// Subdivision method used in this mesh // Subdivision method used in this mesh
HbrSubdivision<T>* subdivision; HbrSubdivision<T>* subdivision;
@ -492,11 +461,7 @@ HbrMesh<T>::NewVertex(int id, const T &data) {
int arrayindex = id / vsetsize; int arrayindex = id / vsetsize;
int vertindex = id % vsetsize; int vertindex = id % vsetsize;
#if PRMAN or MENV m_mutex.Lock();
ScopedLock lock(&m_verticesMutex);
#else
IlmThread::Lock lock(m_verticesMutex);
#endif
HbrVertex<T>** vset = 0; HbrVertex<T>** vset = 0;
if (arrayindex >= nvsets) { if (arrayindex >= nvsets) {
HbrVertex<T>*** nvertices = new HbrVertex<T>**[arrayindex + 1]; HbrVertex<T>*** nvertices = new HbrVertex<T>**[arrayindex + 1];
@ -517,11 +482,8 @@ HbrMesh<T>::NewVertex(int id, const T &data) {
vertices = nvertices; vertices = nvertices;
} }
vset = vertices[arrayindex]; vset = vertices[arrayindex];
#if PRMAN or MENV m_mutex.Unlock();
lock.Release();
#else
lock.release();
#endif
v = vset[vertindex]; v = vset[vertindex];
if (v) { if (v) {
v->Destroy(); v->Destroy();
@ -587,15 +549,13 @@ HbrMesh<T>::GetVertex(int id) const {
int arrayindex = id / vsetsize; int arrayindex = id / vsetsize;
int vertindex = id % vsetsize; int vertindex = id % vsetsize;
#if PRMAN or MENV m_mutex.Lock();
ScopedLock lock(&m_verticesMutex);
#else
IlmThread::Lock lock(m_verticesMutex);
#endif
if (arrayindex >= nvsets) { if (arrayindex >= nvsets) {
m_mutex.Unlock();
return 0; return 0;
} }
HbrVertex<T>** vset = vertices[arrayindex]; HbrVertex<T>** vset = vertices[arrayindex];
m_mutex.Unlock();
return vset[vertindex]; return vset[vertindex];
} }
@ -798,18 +758,10 @@ HbrMesh<T>::DeleteVertex(HbrVertex<T>* vertex) {
recycleIDs.insert(vertex->GetID()); recycleIDs.insert(vertex->GetID());
int id = vertex->GetID(); int id = vertex->GetID();
int arrayindex = id / vsetsize; int arrayindex = id / vsetsize;
#if PRMAN or MENV m_mutex.Lock();
ScopedLock lock(&m_verticesMutex);
#else
IlmThread::Lock lock(m_verticesMutex);
#endif
int vertindex = id % vsetsize; int vertindex = id % vsetsize;
HbrVertex<T>** vset = vertices[arrayindex]; HbrVertex<T>** vset = vertices[arrayindex];
#if PRMAN or MENV m_mutex.Unlock();
lock.Release();
#else
lock.release();
#endif
vset[vertindex] = 0; vset[vertindex] = 0;
vertex->Destroy(); vertex->Destroy();
m_vertexAllocator.Deallocate(vertex); m_vertexAllocator.Deallocate(vertex);
@ -820,17 +772,14 @@ template <class T>
int int
HbrMesh<T>::GetNumVertices() const { HbrMesh<T>::GetNumVertices() const {
int count = 0; int count = 0;
#if PRMAN or MENV m_mutex.Lock();
ScopedLock lock(&m_verticesMutex);
#else
IlmThread::Lock lock(m_verticesMutex);
#endif
for (int vi = 0; vi < nvsets; ++vi) { for (int vi = 0; vi < nvsets; ++vi) {
HbrVertex<T>** vset = vertices[vi]; HbrVertex<T>** vset = vertices[vi];
for (int i = 0; i < vsetsize; ++i) { for (int i = 0; i < vsetsize; ++i) {
if (vset[i]) count++; if (vset[i]) count++;
} }
} }
m_mutex.Unlock();
return count; return count;
} }
@ -838,11 +787,7 @@ template <class T>
int int
HbrMesh<T>::GetNumDisconnectedVertices() const { HbrMesh<T>::GetNumDisconnectedVertices() const {
int disconnected = 0; int disconnected = 0;
#if PRMAN or MENV m_mutex.Lock();
ScopedLock lock(&m_verticesMutex);
#else
IlmThread::Lock lock(m_verticesMutex);
#endif
for (int vi = 0; vi < nvsets; ++vi) { for (int vi = 0; vi < nvsets; ++vi) {
HbrVertex<T>** vset = vertices[vi]; HbrVertex<T>** vset = vertices[vi];
for (int i = 0; i < vsetsize; ++i) { for (int i = 0; i < vsetsize; ++i) {
@ -853,6 +798,7 @@ HbrMesh<T>::GetNumDisconnectedVertices() const {
} }
} }
} }
m_mutex.Unlock();
return disconnected; return disconnected;
} }
@ -891,33 +837,27 @@ HbrMesh<T>::GetFace(int id) const {
template <class T> template <class T>
void void
HbrMesh<T>::GetVertices(std::vector<HbrVertex<T>*>& lvertices) const { HbrMesh<T>::GetVertices(std::vector<HbrVertex<T>*>& lvertices) const {
#if PRMAN or MENV m_mutex.Lock();
ScopedLock lock(&m_verticesMutex);
#else
IlmThread::Lock lock(m_verticesMutex);
#endif
for (int vi = 0; vi < nvsets; ++vi) { for (int vi = 0; vi < nvsets; ++vi) {
HbrVertex<T>** vset = vertices[vi]; HbrVertex<T>** vset = vertices[vi];
for (int i = 0; i < vsetsize; ++i) { for (int i = 0; i < vsetsize; ++i) {
if (vset[i]) lvertices.push_back(vset[i]); if (vset[i]) lvertices.push_back(vset[i]);
} }
} }
m_mutex.Unlock();
} }
template <class T> template <class T>
void void
HbrMesh<T>::ApplyOperatorAllVertices(HbrVertexOperator<T> &op) const { HbrMesh<T>::ApplyOperatorAllVertices(HbrVertexOperator<T> &op) const {
#if PRMAN or MENV m_mutex.Lock();
ScopedLock lock(&m_verticesMutex);
#else
IlmThread::Lock lock(m_verticesMutex);
#endif
for (int vi = 0; vi < nvsets; ++vi) { for (int vi = 0; vi < nvsets; ++vi) {
HbrVertex<T>** vset = vertices[vi]; HbrVertex<T>** vset = vertices[vi];
for (int i = 0; i < vsetsize; ++i) { for (int i = 0; i < vsetsize; ++i) {
if (vset[i]) op(*vset[i]); if (vset[i]) op(*vset[i]);
} }
} }
m_mutex.Unlock();
} }
template <class T> template <class T>
@ -1068,11 +1008,7 @@ HbrMesh<T>::FreeTransientData() {
} }
} }
// Reset max vertex ID. Slightly more complicated // Reset max vertex ID. Slightly more complicated
#if PRMAN or MENV m_mutex.Lock();
ScopedLock lock(&m_verticesMutex);
#else
IlmThread::Lock lock(m_verticesMutex);
#endif
for (i = (nvsets * vsetsize) - 1; i >= 0; --i) { for (i = (nvsets * vsetsize) - 1; i >= 0; --i) {
int arrayindex = i / vsetsize; int arrayindex = i / vsetsize;
int vertindex = i % vsetsize; int vertindex = i % vsetsize;
@ -1081,6 +1017,7 @@ HbrMesh<T>::FreeTransientData() {
break; break;
} }
} }
m_mutex.Unlock();
} }
} // end namespace OPENSUBDIV_VERSION } // end namespace OPENSUBDIV_VERSION

View File

@ -58,7 +58,6 @@
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
include_directories( include_directories(
${ILMBASE_INCLUDE_DIR}
${OPENGL_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR}
${GLEW_INCLUDE_DIR} ${GLEW_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/opensubdiv ${PROJECT_SOURCE_DIR}/opensubdiv
@ -82,6 +81,7 @@ set(INC_FILES
set(PRIVATE_HEADER_FILES set(PRIVATE_HEADER_FILES
local.h local.h
mutex.h
) )
set(PUBLIC_HEADER_FILES set(PUBLIC_HEADER_FILES
@ -154,7 +154,6 @@ if( OPENGL_FOUND AND GLEW_FOUND AND (NOT APPLE) )
else( OPENGL_FOUND AND APPLE ) else( OPENGL_FOUND AND APPLE )
list(APPEND PLATFORM_LIBRARIES list(APPEND PLATFORM_LIBRARIES
${OPENGL_LIBRARY} ${OPENGL_LIBRARY}
${ILMBASE_LIBRARIES}
) )
endif() endif()

View File

@ -55,6 +55,7 @@
// a particular purpose and non-infringement. // a particular purpose and non-infringement.
// //
#include "../version.h" #include "../version.h"
#include "../osd/mutex.h"
#include "../osd/clDispatcher.h" #include "../osd/clDispatcher.h"
#include "../osd/local.h" #include "../osd/local.h"

View File

@ -56,6 +56,7 @@
// //
#include "../version.h" #include "../version.h"
#include "../osd/mutex.h"
#include "../osd/cpuDispatcher.h" #include "../osd/cpuDispatcher.h"
#include "../osd/cpuKernel.h" #include "../osd/cpuKernel.h"

View File

@ -55,6 +55,7 @@
// a particular purpose and non-infringement. // a particular purpose and non-infringement.
// //
#include "../version.h" #include "../version.h"
#include "../osd/mutex.h"
#include "../osd/cudaDispatcher.h" #include "../osd/cudaDispatcher.h"
#include <cuda_runtime.h> #include <cuda_runtime.h>

View File

@ -62,6 +62,7 @@
#include <OpenGL/gl3.h> #include <OpenGL/gl3.h>
#endif #endif
#include "../osd/mutex.h"
#include "../osd/glslDispatcher.h" #include "../osd/glslDispatcher.h"
#include "../osd/local.h" #include "../osd/local.h"

View File

@ -56,6 +56,7 @@
// //
#include "../version.h" #include "../version.h"
#include "../osd/mutex.h"
#include "../osd/kernelDispatcher.h" #include "../osd/kernelDispatcher.h"
#include "../osd/cpuDispatcher.h" #include "../osd/cpuDispatcher.h"

View File

@ -64,12 +64,22 @@
#include <string.h> #include <string.h>
#include "../version.h" #include "../version.h"
#include "../osd/mutex.h"
#include "../hbr/mesh.h"
#include "../hbr/vertex.h"
#include "../hbr/face.h"
#include "../hbr/halfedge.h"
#include "../far/mesh.h"
#include "../far/meshFactory.h"
#include "../osd/mesh.h" #include "../osd/mesh.h"
#include "../osd/local.h" #include "../osd/local.h"
#include "../osd/kernelDispatcher.h" #include "../osd/kernelDispatcher.h"
#include "../osd/cpuDispatcher.h" #include "../osd/cpuDispatcher.h"
#include "../far/meshFactory.h"
namespace OpenSubdiv { namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION { namespace OPENSUBDIV_VERSION {

View File

@ -61,13 +61,6 @@
#include <vector> #include <vector>
#include "../version.h" #include "../version.h"
#include "../hbr/mesh.h"
#include "../hbr/vertex.h"
#include "../hbr/face.h"
#include "../hbr/halfedge.h"
#include "../far/mesh.h"
#include "../far/meshFactory.h"
#include "../osd/vertex.h" #include "../osd/vertex.h"
#include "../osd/vertexBuffer.h" #include "../osd/vertexBuffer.h"
@ -76,12 +69,17 @@
namespace OpenSubdiv { namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION { namespace OPENSUBDIV_VERSION {
class OsdKernelDispatcher; template <class T> class HbrMesh;
template <class T> class HbrVertex;
typedef HbrMesh<OsdVertex> OsdHbrMesh; typedef HbrMesh<OsdVertex> OsdHbrMesh;
typedef HbrVertex<OsdVertex> OsdHbrVertex; typedef HbrVertex<OsdVertex> OsdHbrVertex;
typedef HbrHalfedge<OsdVertex> OsdHbrHalfedge;
typedef HbrFace<OsdVertex> OsdHbrFace; typedef HbrFace<OsdVertex> OsdHbrFace;
typedef HbrHalfedge<OsdVertex> OsdHbrHalfedge;
template <class T, class U> class FarMesh;
class OsdKernelDispatcher;
class OsdPtexIndicesBuffer; class OsdPtexIndicesBuffer;

83
opensubdiv/osd/mutex.h Normal file
View File

@ -0,0 +1,83 @@
//
// 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_MUTEX_H
#define OSD_MUTEX_H
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
// XXXX manuelk : for now we don't actually need this mutex
// but we will eventually implement a cross-platform one.
class Mutex {
public:
void Lock() { };
void Unlock() { };
private:
};
} // end namespace OPENSUBDIV_VERSION
using namespace OPENSUBDIV_VERSION;
} // end namespace OpenSubdiv
#endif // OSD_PTEXTURE_H

View File

@ -59,12 +59,13 @@
#define OSD_VERTEX_H #define OSD_VERTEX_H
#include "../version.h" #include "../version.h"
#include "../hbr/face.h"
#include "../hbr/vertexEdit.h"
namespace OpenSubdiv { namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION { namespace OPENSUBDIV_VERSION {
template <class T> class HbrVertexEdit;
template <class T> class HbrMovingVertexEdit;
class OsdVertex { class OsdVertex {
public: public:
OsdVertex() {} OsdVertex() {}
@ -74,8 +75,8 @@ public:
void AddWithWeight(const OsdVertex & i, float weight, void * = 0) {} void AddWithWeight(const OsdVertex & i, float weight, void * = 0) {}
void AddVaryingWithWeight(const OsdVertex & i, float weight, void * = 0) {} void AddVaryingWithWeight(const OsdVertex & i, float weight, void * = 0) {}
void Clear(void * = 0) {} void Clear(void * = 0) {}
void ApplyVertexEdit(const OpenSubdiv::HbrVertexEdit<OsdVertex> &) { } void ApplyVertexEdit(const HbrVertexEdit<OsdVertex> &) { }
void ApplyMovingVertexEdit(const OpenSubdiv::HbrMovingVertexEdit<OsdVertex> &) { } void ApplyMovingVertexEdit(const HbrMovingVertexEdit<OsdVertex> &) { }
}; };
} // end namespace OPENSUBDIV_VERSION } // end namespace OPENSUBDIV_VERSION

View File

@ -57,6 +57,8 @@
#include <stdio.h> #include <stdio.h>
#include <osd/mutex.h>
#include <hbr/mesh.h> #include <hbr/mesh.h>
#include <hbr/face.h> #include <hbr/face.h>
#include <hbr/vertex.h> #include <hbr/vertex.h>

View File

@ -66,6 +66,8 @@
#include <stdio.h> #include <stdio.h>
#include <cassert> #include <cassert>
#include <osd/mutex.h>
#include <hbr/mesh.h> #include <hbr/mesh.h>
#include <hbr/face.h> #include <hbr/face.h>
#include <hbr/vertex.h> #include <hbr/vertex.h>