Added a public constructor to OsdMesh that initializes the members objects from its arguments, allowing those members to be created by a subclass or an external function.

* maintainance work on the D3D11 specialization of OsdMesh to bring it in line with the other template specializations
* updated the facePartition example to derive PartitionedMesh from OsdMesh in order to allow other vertex buffer and compute controller configurations
This commit is contained in:
Nathan Litke 2014-04-18 17:19:08 -07:00
parent feb74f51f0
commit 0c837b73b2
4 changed files with 305 additions and 92 deletions

View File

@ -53,6 +53,7 @@
#include <osd/vertex.h>
#include <osd/glDrawContext.h>
#include <osd/glDrawRegistry.h>
#include <osd/glMesh.h>
#include <osd/cpuGLVertexBuffer.h>
#include <osd/cpuComputeContext.h>
@ -62,76 +63,69 @@
OpenSubdiv::OsdCpuComputeController *g_cpuComputeController = NULL;
class PartitionedMesh {
template <class VERTEX_BUFFER, class COMPUTE_CONTROLLER>
class PartitionedMesh : public OpenSubdiv::OsdMesh<VERTEX_BUFFER, COMPUTE_CONTROLLER, OpenSubdiv::OsdGLDrawContext>
{
public:
PartitionedMesh(OpenSubdiv::FarMesh<OpenSubdiv::OsdVertex> const *farMesh,
std::vector<int> const &partitionPerFace) {
typedef OpenSubdiv::OsdMesh<VERTEX_BUFFER, COMPUTE_CONTROLLER, OpenSubdiv::OsdGLDrawContext> Inherited;
typedef typename Inherited::VertexBuffer VertexBuffer;
typedef typename Inherited::ComputeController ComputeController;
typedef typename Inherited::ComputeContext ComputeContext;
typedef typename Inherited::DrawContext DrawContext;
int numVertices = farMesh->GetNumVertices();
_vertexBuffer = OpenSubdiv::OsdCpuGLVertexBuffer::Create(3, numVertices);
_computeContext = OpenSubdiv::OsdCpuComputeContext::Create(
farMesh->GetSubdivisionTables(), farMesh->GetVertexEditTables());
_kernelBatches = farMesh->GetKernelBatches();
static PartitionedMesh*
Create(ComputeController *computeController, OpenSubdiv::FarMesh<OpenSubdiv::OsdVertex> *farMesh,
std::vector<int> const &partitionPerFace) {
OpenSubdiv::OsdUtilPatchPartitioner partitioner(farMesh->GetPatchTables(), partitionPerFace);
int numVertices = farMesh->GetNumVertices();
VertexBuffer* vertexBuffer = VertexBuffer::Create(3, numVertices);
ComputeContext* computeContext = ComputeContext::Create(
farMesh->GetSubdivisionTables(), farMesh->GetVertexEditTables());
DrawContext* drawContext = DrawContext::Create(&partitioner.GetPatchTables(), 3, false);
PartitionedMesh* mesh = new PartitionedMesh(computeController, farMesh, vertexBuffer,
NULL, computeContext, drawContext);
// convert farpatch to osdpatch
int maxMaterial = partitioner.GetNumPartitions();
int maxValence = farMesh->GetPatchTables()->GetMaxValence();;
int maxValence = farMesh->GetPatchTables()->GetMaxValence();
_partitionedOsdPatchArrays.resize(maxMaterial);
mesh->_partitionedOsdPatchArrays.resize(maxMaterial);
for (int i = 0; i < maxMaterial; ++i) {
OpenSubdiv::OsdDrawContext::ConvertPatchArrays(partitioner.GetPatchArrays(i),
_partitionedOsdPatchArrays[i],
mesh->_partitionedOsdPatchArrays[i],
maxValence, 3);
}
_drawContext = OpenSubdiv::OsdGLDrawContext::Create(&partitioner.GetPatchTables(), 3, false);
_drawContext->UpdateVertexTexture(_vertexBuffer);
return mesh;
}
~PartitionedMesh() {
delete _vertexBuffer;
delete _computeContext;
delete _drawContext;
int GetNumPartitions() const {
return (int)_partitionedOsdPatchArrays.size();
}
void UpdateVertexBuffer(float const *vertexData, int numVerts) {
_vertexBuffer->UpdateData(vertexData, 0, numVerts);
OpenSubdiv::OsdDrawContext::PatchArrayVector const & GetPatchArrays(int partition) const {
return _partitionedOsdPatchArrays[partition];
}
void Refine() {
g_cpuComputeController->Refine(_computeContext,
_kernelBatches,
_vertexBuffer);
}
OpenSubdiv::OsdGLDrawContext *GetDrawContext() const {
return _drawContext;
}
GLuint BindVertexBuffer() {
return _vertexBuffer->BindVBO();
}
int GetNumPartitions() const {
return (int)_partitionedOsdPatchArrays.size();
}
OpenSubdiv::OsdDrawContext::PatchArrayVector const & GetPatchArrays(int partition) const {
return _partitionedOsdPatchArrays[partition];
}
private:
PartitionedMesh(ComputeController * computeController,
OpenSubdiv::FarMesh<OpenSubdiv::OsdVertex> * fmesh,
VertexBuffer * vertexBuffer,
VertexBuffer * varyingBuffer,
ComputeContext * computeContext,
DrawContext * drawContext) :
OpenSubdiv::OsdCpuComputeContext *_computeContext;
OpenSubdiv::OsdCpuGLVertexBuffer *_vertexBuffer;
OpenSubdiv::FarKernelBatchVector _kernelBatches;
Inherited(computeController, fmesh, vertexBuffer, varyingBuffer, computeContext, drawContext)
{
}
OpenSubdiv::OsdGLDrawContext *_drawContext;
std::vector<OpenSubdiv::OsdDrawContext::PatchArrayVector> _partitionedOsdPatchArrays;
std::vector<OpenSubdiv::OsdDrawContext::PatchArrayVector> _partitionedOsdPatchArrays;
};
PartitionedMesh *g_mesh = NULL;
PartitionedMesh<OpenSubdiv::OsdCpuGLVertexBuffer, OpenSubdiv::OsdCpuComputeController> *g_mesh = NULL;
#include <common/shape_utils.h>
#include "../common/stopwatch.h"
@ -421,7 +415,7 @@ updateGeom() {
pp += 3;
}
g_mesh->UpdateVertexBuffer(&vertex[0], nverts);
g_mesh->UpdateVertexBuffer(&vertex[0], 0, nverts);
Stopwatch s;
s.Start();
@ -486,11 +480,10 @@ createOsdMesh( const std::string &shape, int level, Scheme scheme=kCatmark ) {
// create partitioned patcharray
delete g_mesh;
g_mesh = new PartitionedMesh(farMesh, idsOnPtexFaces);
g_mesh = PartitionedMesh<OpenSubdiv::OsdCpuGLVertexBuffer, OpenSubdiv::OsdCpuComputeController>::Create(g_cpuComputeController, farMesh, idsOnPtexFaces);
// Hbr,Far mesh can be deleted
// Hbr mesh can be deleted
delete hmesh;
delete farMesh;
// compute model bounding
float min[3] = { FLT_MAX, FLT_MAX, FLT_MAX};

View File

@ -65,20 +65,43 @@ public:
FarMeshFactory<OsdVertex> meshFactory(hmesh, level, bits.test(MeshAdaptive));
_farMesh = meshFactory.Create(bits.test(MeshFVarData));
ID3D11Device * pd3d11Device;
_pd3d11DeviceContext->GetDevice(&pd3d11Device);
_initialize(numVertexElements, numVaryingElements, bits);
}
int numVertices = _farMesh->GetNumVertices();
_vertexBuffer = VertexBuffer::Create(numVertexElements, numVertices, pd3d11Device);
if (numVaryingElements)
_vertexBuffer = VertexBuffer::Create(numVaryingElements, numVertices, pd3d11Device);
_computeContext = ComputeContext::Create(_farMesh->GetSubdivisionTables(),
_farMesh->GetVertexEditTables());
_drawContext = DrawContext::Create(_farMesh->GetPatchTables(),
_pd3d11DeviceContext,
numVertexElements,
bits.test(MeshFVarData));
assert(_drawContext);
OsdMesh(ComputeController * computeController,
FarMesh<OsdVertex> * fmesh,
int numVertexElements,
int numVaryingElements,
OsdMeshBitset bits,
ID3D11DeviceContext *d3d11DeviceContext) :
_farMesh(fmesh),
_vertexBuffer(0),
_varyingBuffer(0),
_computeContext(0),
_computeController(computeController),
_drawContext(0),
_pd3d11DeviceContext(d3d11DeviceContext)
{
_initialize(numVertexElements, numVaryingElements, bits);
}
OsdMesh(ComputeController * computeController,
FarMesh<OsdVertex> * fmesh,
VertexBuffer * vertexBuffer,
VertexBuffer * varyingBuffer,
ComputeContext * computeContext,
DrawContext * drawContext,
ID3D11DeviceContext *d3d11DeviceContext) :
_farMesh(fmesh),
_vertexBuffer(vertexBuffer),
_varyingBuffer(varyingBuffer),
_computeContext(computeContext),
_computeController(computeController),
_drawContext(drawContext),
_pd3d11DeviceContext(d3d11DeviceContext)
{
_drawContext->UpdateVertexTexture(_vertexBuffer, _pd3d11DeviceContext);
}
@ -113,8 +136,38 @@ public:
virtual DrawContext * GetDrawContext() {
return _drawContext;
}
virtual VertexBuffer * GetVertexBuffer() {
return _vertexBuffer;
}
virtual VertexBuffer * GetVaryingBuffer() {
return _varyingBuffer;
}
virtual FarMesh<OsdVertex> const * GetFarMesh() const {
return _farMesh;
}
private:
void _initialize( int numVertexElements,
int numVaryingElements,
OsdMeshBitset bits)
{
ID3D11Device * pd3d11Device;
_pd3d11DeviceContext->GetDevice(&pd3d11Device);
int numVertices = _farMesh->GetNumVertices();
if (numVertexElements)
_vertexBuffer = VertexBuffer::Create(numVertexElements, numVertices, pd3d11Device);
if (numVaryingElements)
_varyingBuffer = VertexBuffer::Create(numVaryingElements, numVertices, pd3d11Device);
_computeContext = ComputeContext::Create(_farMesh->GetSubdivisionTables(), _farMesh->GetVertexEditTables());
_drawContext = DrawContext::Create(_farMesh->GetPatchTables(),
_pd3d11DeviceContext,
numVertexElements,
bits.test(MeshFVarData));
_drawContext->UpdateVertexTexture(_vertexBuffer, _pd3d11DeviceContext);
}
FarMesh<OsdVertex> *_farMesh;
VertexBuffer *_vertexBuffer;
VertexBuffer *_varyingBuffer;
@ -153,20 +206,43 @@ public:
FarMeshFactory<OsdVertex> meshFactory(hmesh, level, bits.test(MeshAdaptive));
_farMesh = meshFactory.Create(bits.test(MeshFVarData));
ID3D11Device * pd3d11Device;
_pd3d11DeviceContext->GetDevice(&pd3d11Device);
_initialize(numVertexElements, numVaryingElements, bits);
}
int numVertices = _farMesh->GetNumVertices();
_vertexBuffer = VertexBuffer::Create(numVertexElements, numVertices, pd3d11Device);
if (numVaryingElements)
_varyingBuffer = VertexBuffer::Create(numVaryingElements, numVertices, pd3d11Device);
_computeContext = ComputeContext::Create(_farMesh->GetSubdivisionTables(),
_farMesh->GetVertexEditTables(),
_pd3d11DeviceContext);
_drawContext = DrawContext::Create(_farMesh->GetPatchTables(),
_pd3d11DeviceContext,
numVertexElements,
bits.test(MeshFVarData));
OsdMesh(ComputeController * computeController,
FarMesh<OsdVertex> * fmesh,
int numVertexElements,
int numVaryingElements,
OsdMeshBitset bits,
ID3D11DeviceContext *d3d11DeviceContext) :
_farMesh(fmesh),
_vertexBuffer(0),
_varyingBuffer(0),
_computeContext(0),
_computeController(computeController),
_drawContext(0),
_pd3d11DeviceContext(d3d11DeviceContext)
{
_initialize(numVertexElements, numVaryingElements, bits);
}
OsdMesh(ComputeController * computeController,
FarMesh<OsdVertex> * fmesh,
VertexBuffer * vertexBuffer,
VertexBuffer * varyingBuffer,
ComputeContext * computeContext,
DrawContext * drawContext,
ID3D11DeviceContext *d3d11DeviceContext) :
_farMesh(fmesh),
_vertexBuffer(vertexBuffer),
_varyingBuffer(varyingBuffer),
_computeContext(computeContext),
_computeController(computeController),
_drawContext(drawContext),
_pd3d11DeviceContext(d3d11DeviceContext)
{
_drawContext->UpdateVertexTexture(_vertexBuffer, _pd3d11DeviceContext);
}
@ -201,8 +277,40 @@ public:
virtual DrawContext * GetDrawContext() {
return _drawContext;
}
virtual VertexBuffer * GetVertexBuffer() {
return _vertexBuffer;
}
virtual VertexBuffer * GetVaryingBuffer() {
return _varyingBuffer;
}
virtual FarMesh<OsdVertex> const * GetFarMesh() const {
return _farMesh;
}
private:
void _initialize( int numVertexElements,
int numVaryingElements,
OsdMeshBitset bits)
{
ID3D11Device * pd3d11Device;
_pd3d11DeviceContext->GetDevice(&pd3d11Device);
int numVertices = _farMesh->GetNumVertices();
if (numVertexElements)
_vertexBuffer = VertexBuffer::Create(numVertexElements, numVertices, pd3d11Device);
if (numVaryingElements)
_varyingBuffer = VertexBuffer::Create(numVaryingElements, numVertices, pd3d11Device);
_computeContext = ComputeContext::Create(_farMesh->GetSubdivisionTables(),
_farMesh->GetVertexEditTables(),
_pd3d11DeviceContext);
_drawContext = DrawContext::Create(_farMesh->GetPatchTables(),
_pd3d11DeviceContext,
numVertexElements,
bits.test(MeshFVarData));
_drawContext->UpdateVertexTexture(_vertexBuffer, _pd3d11DeviceContext);
}
FarMesh<OsdVertex> *_farMesh;
VertexBuffer *_vertexBuffer;
VertexBuffer *_varyingBuffer;
@ -255,20 +363,51 @@ public:
FarMeshFactory<OsdVertex> meshFactory(hmesh, level, bits.test(MeshAdaptive));
_farMesh = meshFactory.Create(bits.test(MeshFVarData));
ID3D11Device * pd3d11Device;
_pd3d11DeviceContext->GetDevice(&pd3d11Device);
_initialize(numVertexElements, numVaryingElements, bits);
}
int numVertices = _farMesh->GetNumVertices();
_vertexBuffer = typename VertexBuffer::Create(numVertexElements, numVertices, _clContext, pd3d11Device);
if (numVaryingElements)
_varyingBuffer = typename VertexBuffer::Create(numVaryingElements, numVertices, _clContext, pd3d11Device);
_computeContext = ComputeContext::Create(_farMesh->GetSubdivisionTables(),
_farMesh->GetVertexEditTables(), _clContext);
_drawContext = DrawContext::Create(_farMesh->GetPatchTables(),
_pd3d11DeviceContext,
numVertexElements,
bits.test(MeshFVarData));
assert(_drawContext);
OsdMesh(ComputeController * computeController,
FarMesh<OsdVertex> * fmesh,
int numVertexElements,
int numVaryingElements,
OsdMeshBitset bits,
cl_context clContext,
cl_command_queue clQueue,
ID3D11DeviceContext *d3d11DeviceContext) :
_farMesh(fmesh),
_vertexBuffer(0),
_varyingBuffer(0),
_computeContext(0),
_computeController(computeController),
_drawContext(0),
_clContext(clContext),
_clQueue(clQueue),
_pd3d11DeviceContext(d3d11DeviceContext)
{
_initialize(numVertexElements, numVaryingElements, bits);
}
OsdMesh(ComputeController * computeController,
FarMesh<OsdVertex> * fmesh,
VertexBuffer * vertexBuffer,
VertexBuffer * varyingBuffer,
ComputeContext * computeContext,
DrawContext * drawContext,
cl_context clContext,
cl_command_queue clQueue,
ID3D11DeviceContext *d3d11DeviceContext) :
_farMesh(fmesh),
_vertexBuffer(vertexBuffer),
_varyingBuffer(varyingBuffer),
_computeContext(computeContext),
_computeController(computeController),
_drawContext(drawContext),
_clContext(clContext),
_clQueue(clQueue),
_pd3d11DeviceContext(d3d11DeviceContext)
{
_drawContext->UpdateVertexTexture(_vertexBuffer, _pd3d11DeviceContext);
}
@ -307,8 +446,38 @@ public:
virtual DrawContext * GetDrawContext() {
return _drawContext;
}
virtual VertexBuffer * GetVertexBuffer() {
return _vertexBuffer;
}
virtual VertexBuffer * GetVaryingBuffer() {
return _varyingBuffer;
}
virtual FarMesh<OsdVertex> const * GetFarMesh() const {
return _farMesh;
}
private:
void _initialize( int numVertexElements,
int numVaryingElements,
OsdMeshBitset bits)
{
ID3D11Device * pd3d11Device;
_pd3d11DeviceContext->GetDevice(&pd3d11Device);
int numVertices = _farMesh->GetNumVertices();
_vertexBuffer = typename VertexBuffer::Create(numVertexElements, numVertices, _clContext, pd3d11Device);
if (numVaryingElements)
_varyingBuffer = typename VertexBuffer::Create(numVaryingElements, numVertices, _clContext, pd3d11Device);
_computeContext = ComputeContext::Create(_farMesh->GetSubdivisionTables(),
_farMesh->GetVertexEditTables(), _clContext);
_drawContext = DrawContext::Create(_farMesh->GetPatchTables(),
_pd3d11DeviceContext,
numVertexElements,
bits.test(MeshFVarData));
_drawContext->UpdateVertexTexture(_vertexBuffer, _pd3d11DeviceContext);
}
FarMesh<OsdVertex> *_farMesh;
VertexBuffer *_vertexBuffer;
VertexBuffer *_varyingBuffer;

View File

@ -77,7 +77,6 @@ public:
FarMesh<OsdVertex> * fmesh,
int numVertexElements,
int numVaryingElements,
int level,
OsdMeshBitset bits) :
_farMesh(fmesh),
@ -90,6 +89,23 @@ public:
_initialize(numVertexElements, numVaryingElements, bits);
}
OsdMesh(ComputeController * computeController,
FarMesh<OsdVertex> * fmesh,
VertexBuffer * vertexBuffer,
VertexBuffer * varyingBuffer,
ComputeContext * computeContext,
DrawContext * drawContext) :
_farMesh(fmesh),
_vertexBuffer(vertexBuffer),
_varyingBuffer(varyingBuffer),
_computeContext(computeContext),
_computeController(computeController),
_drawContext(drawContext)
{
_drawContext->UpdateVertexTexture(_vertexBuffer);
}
virtual ~OsdMesh() {
delete _farMesh;
delete _vertexBuffer;
@ -194,7 +210,6 @@ public:
FarMesh<OsdVertex> * fmesh,
int numVertexElements,
int numVaryingElements,
int level,
OsdMeshBitset bits,
cl_context clContext,
cl_command_queue clQueue) :
@ -211,6 +226,27 @@ public:
_initialize(numVertexElements, numVaryingElements, bits);
}
OsdMesh(ComputeController * computeController,
FarMesh<OsdVertex> * fmesh,
VertexBuffer * vertexBuffer,
VertexBuffer * varyingBuffer,
ComputeContext * computeContext,
DrawContext * drawContext,
cl_context clContext,
cl_command_queue clQueue) :
_farMesh(fmesh),
_vertexBuffer(vertexBuffer),
_varyingBuffer(varyingBuffer),
_computeContext(computeContext),
_computeController(computeController),
_drawContext(drawContext),
_clContext(clContext),
_clQueue(clQueue)
{
_drawContext->UpdateVertexTexture(_vertexBuffer);
}
virtual ~OsdMesh() {
delete _farMesh;
delete _vertexBuffer;

View File

@ -111,7 +111,6 @@ public:
FarMesh<OsdVertex> * fmesh,
int numVertexElements,
int numVaryingElements,
int level,
OsdMeshBitset bits = OsdMeshBitset()) :
_farMesh(fmesh),
@ -124,6 +123,22 @@ public:
_initialize(numVertexElements, numVaryingElements, bits);
}
OsdMesh(ComputeController * computeController,
FarMesh<OsdVertex> * fmesh,
VertexBuffer * vertexBuffer,
VertexBuffer * varyingBuffer,
ComputeContext * computeContext,
DrawContext * drawContext) :
_farMesh(fmesh),
_vertexBuffer(vertexBuffer),
_varyingBuffer(varyingBuffer),
_computeContext(computeContext),
_computeController(computeController),
_drawContext(drawContext)
{
}
virtual ~OsdMesh() {
delete _farMesh;
delete _vertexBuffer;