2012-06-09 00:06:35 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Copyright 2013 Pixar
|
2012-06-09 00:06:35 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
|
|
|
// with the following modification; you may not use this file except in
|
|
|
|
// compliance with the Apache License and the following modification to it:
|
|
|
|
// Section 6. Trademarks. is deleted and replaced with:
|
2012-06-09 00:06:35 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// 6. Trademarks. This License does not grant permission to use the trade
|
|
|
|
// names, trademarks, service marks, or product names of the Licensor
|
|
|
|
// and its affiliates, except as required to comply with Section 4(c) of
|
|
|
|
// the License and to reproduce the content of the NOTICE file.
|
2012-06-09 00:06:35 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// You may obtain a copy of the Apache License at
|
2012-06-09 00:06:35 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2013-07-18 21:19:50 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the Apache License with the above modification is
|
|
|
|
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
// KIND, either express or implied. See the Apache License for the specific
|
|
|
|
// language governing permissions and limitations under the Apache License.
|
2012-06-09 00:06:35 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
|
2012-06-09 00:06:35 +00:00
|
|
|
#ifndef OSD_MESH_H
|
|
|
|
#define OSD_MESH_H
|
|
|
|
|
2012-06-12 00:02:27 +00:00
|
|
|
#include "../version.h"
|
2012-06-20 00:15:07 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
#include "../far/topologyRefiner.h"
|
|
|
|
#include "../far/patchTablesFactory.h"
|
|
|
|
#include "../far/stencilTables.h"
|
|
|
|
#include "../far/stencilTablesFactory.h"
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2012-06-20 00:15:07 +00:00
|
|
|
#include "../osd/vertex.h"
|
2014-05-09 00:20:54 +00:00
|
|
|
#include "../osd/vertexDescriptor.h"
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
#include <bitset>
|
2014-09-05 22:07:46 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
2012-06-12 23:04:12 +00:00
|
|
|
|
2012-06-09 00:06:35 +00:00
|
|
|
namespace OpenSubdiv {
|
|
|
|
namespace OPENSUBDIV_VERSION {
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
namespace Osd {
|
2012-08-10 22:14:02 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
enum MeshBits {
|
2014-10-13 15:52:09 +00:00
|
|
|
MeshAdaptive = 0,
|
|
|
|
MeshInterleaveVarying = 1,
|
|
|
|
MeshPtexData = 2,
|
|
|
|
MeshFVarData = 3,
|
|
|
|
MeshUseSingleCreasePatch = 4,
|
2015-04-23 23:58:45 +00:00
|
|
|
MeshEndCapBSplineBasis = 5, // exclusive
|
2015-04-18 00:36:55 +00:00
|
|
|
MeshEndCapGregoryBasis = 6, // exclusive
|
|
|
|
MeshEndCapLegacyGregory = 7, // exclusive
|
|
|
|
NUM_MESH_BITS = 8,
|
2012-12-11 01:15:13 +00:00
|
|
|
};
|
2014-09-05 22:07:46 +00:00
|
|
|
typedef std::bitset<NUM_MESH_BITS> MeshBitset;
|
2012-06-09 18:12:34 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
template <class DRAW_CONTEXT>
|
2014-09-05 22:07:46 +00:00
|
|
|
class MeshInterface {
|
2012-06-09 00:06:35 +00:00
|
|
|
public:
|
2012-12-11 01:15:13 +00:00
|
|
|
typedef DRAW_CONTEXT DrawContext;
|
|
|
|
typedef typename DrawContext::VertexBufferBinding VertexBufferBinding;
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
public:
|
2014-09-05 22:07:46 +00:00
|
|
|
MeshInterface() { }
|
2012-06-12 23:28:17 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
virtual ~MeshInterface() { }
|
2012-06-09 18:12:34 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual int GetNumVertices() const = 0;
|
2012-06-09 00:06:35 +00:00
|
|
|
|
2013-03-08 01:50:15 +00:00
|
|
|
virtual void UpdateVertexBuffer(float const *vertexData, int startVertex, int numVerts) = 0;
|
2012-06-09 18:12:34 +00:00
|
|
|
|
2013-06-27 19:57:21 +00:00
|
|
|
virtual void UpdateVaryingBuffer(float const *varyingData, int startVertex, int numVerts) = 0;
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual void Refine() = 0;
|
2012-06-09 00:06:35 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
virtual void Refine(VertexBufferDescriptor const *vertexDesc,
|
|
|
|
VertexBufferDescriptor const *varyingDesc,
|
2014-05-09 00:20:54 +00:00
|
|
|
bool interleaved) = 0;
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual void Synchronize() = 0;
|
2012-06-09 18:12:34 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual DrawContext * GetDrawContext() = 0;
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual VertexBufferBinding BindVertexBuffer() = 0;
|
2013-06-27 19:57:21 +00:00
|
|
|
|
|
|
|
virtual VertexBufferBinding BindVaryingBuffer() = 0;
|
2014-06-27 08:59:20 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
virtual void SetFVarDataChannel(int fvarWidth, std::vector<float> const & fvarData) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2014-10-13 15:52:09 +00:00
|
|
|
static inline void refineMesh(Far::TopologyRefiner & refiner, int level, bool adaptive, bool singleCreasePatch) {
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
if (adaptive) {
|
2014-12-30 22:07:24 +00:00
|
|
|
Far::TopologyRefiner::AdaptiveOptions options(level);
|
2014-12-23 18:07:24 +00:00
|
|
|
options.useSingleCreasePatch = singleCreasePatch;
|
2014-12-30 22:07:24 +00:00
|
|
|
refiner.RefineAdaptive(options);
|
2014-09-05 22:07:46 +00:00
|
|
|
} else {
|
2015-04-01 21:57:28 +00:00
|
|
|
// This dependency on FVar channels should not be necessary
|
|
|
|
bool fullTopologyInLastLevel = refiner.GetNumFVarChannels()>0;
|
|
|
|
|
2014-12-30 22:07:24 +00:00
|
|
|
Far::TopologyRefiner::UniformOptions options(level);
|
2014-12-23 18:07:24 +00:00
|
|
|
options.fullTopologyInLastLevel = fullTopologyInLastLevel;
|
2014-12-30 22:07:24 +00:00
|
|
|
refiner.RefineUniform(options);
|
2014-09-05 22:07:46 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-11 01:15:13 +00:00
|
|
|
};
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
template <class VERTEX_BUFFER, class COMPUTE_CONTROLLER, class DRAW_CONTEXT>
|
2014-09-05 22:07:46 +00:00
|
|
|
class Mesh : public MeshInterface<DRAW_CONTEXT> {
|
2012-12-11 01:15:13 +00:00
|
|
|
public:
|
|
|
|
typedef VERTEX_BUFFER VertexBuffer;
|
|
|
|
typedef COMPUTE_CONTROLLER ComputeController;
|
2014-09-05 22:07:46 +00:00
|
|
|
typedef typename ComputeController::ComputeContext ComputeContext;
|
|
|
|
typedef DRAW_CONTEXT DrawContext;
|
2012-12-11 01:15:13 +00:00
|
|
|
typedef typename DrawContext::VertexBufferBinding VertexBufferBinding;
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
Mesh(ComputeController * computeController,
|
|
|
|
Far::TopologyRefiner * refiner,
|
2013-06-27 19:57:21 +00:00
|
|
|
int numVertexElements,
|
|
|
|
int numVaryingElements,
|
2012-12-11 01:15:13 +00:00
|
|
|
int level,
|
2014-09-05 22:07:46 +00:00
|
|
|
MeshBitset bits = MeshBitset()) :
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
_refiner(refiner),
|
2014-09-12 20:13:43 +00:00
|
|
|
_patchTables(0),
|
2012-12-11 01:15:13 +00:00
|
|
|
_vertexBuffer(0),
|
2013-06-27 19:57:21 +00:00
|
|
|
_varyingBuffer(0),
|
2012-12-11 01:15:13 +00:00
|
|
|
_computeContext(0),
|
2013-02-20 01:33:32 +00:00
|
|
|
_computeController(computeController),
|
2014-09-05 22:07:46 +00:00
|
|
|
_drawContext(0) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
assert(_refiner);
|
2013-08-08 20:11:16 +00:00
|
|
|
|
2014-10-13 15:52:09 +00:00
|
|
|
MeshInterface<DRAW_CONTEXT>::refineMesh(*_refiner, level, bits.test(MeshAdaptive), bits.test(MeshUseSingleCreasePatch));
|
2013-08-08 20:11:16 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
initializeVertexBuffers(numVertexElements, numVaryingElements, bits);
|
|
|
|
|
|
|
|
initializeComputeContext(numVertexElements, numVaryingElements);
|
|
|
|
|
2014-10-13 15:52:09 +00:00
|
|
|
initializeDrawContext(numVertexElements, level, bits);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
Mesh(ComputeController * computeController,
|
|
|
|
Far::TopologyRefiner * refiner,
|
2014-09-12 20:13:43 +00:00
|
|
|
Far::PatchTables * patchTables,
|
2014-04-19 00:19:08 +00:00
|
|
|
VertexBuffer * vertexBuffer,
|
|
|
|
VertexBuffer * varyingBuffer,
|
|
|
|
ComputeContext * computeContext,
|
|
|
|
DrawContext * drawContext) :
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
_refiner(refiner),
|
2014-09-12 20:13:43 +00:00
|
|
|
_patchTables(patchTables),
|
2014-04-19 00:19:08 +00:00
|
|
|
_vertexBuffer(vertexBuffer),
|
|
|
|
_varyingBuffer(varyingBuffer),
|
|
|
|
_computeContext(computeContext),
|
|
|
|
_computeController(computeController),
|
2015-04-18 00:36:55 +00:00
|
|
|
_drawContext(drawContext) {}
|
2014-04-19 00:19:08 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
virtual ~Mesh() {
|
|
|
|
delete _refiner;
|
|
|
|
delete _patchTables;
|
2012-12-11 01:15:13 +00:00
|
|
|
delete _vertexBuffer;
|
2013-06-27 19:57:21 +00:00
|
|
|
delete _varyingBuffer;
|
2012-12-11 01:15:13 +00:00
|
|
|
delete _computeContext;
|
|
|
|
delete _drawContext;
|
|
|
|
}
|
|
|
|
|
2013-03-08 01:50:15 +00:00
|
|
|
virtual void UpdateVertexBuffer(float const *vertexData, int startVertex, int numVerts) {
|
|
|
|
_vertexBuffer->UpdateData(vertexData, startVertex, numVerts);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2013-06-27 19:57:21 +00:00
|
|
|
virtual void UpdateVaryingBuffer(float const *varyingData, int startVertex, int numVerts) {
|
|
|
|
_varyingBuffer->UpdateData(varyingData, startVertex, numVerts);
|
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual void Refine() {
|
2015-04-09 18:16:54 +00:00
|
|
|
_computeController->Compute(_computeContext, _vertexBuffer, _varyingBuffer);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
virtual void Refine(VertexBufferDescriptor const *vertexDesc, VertexBufferDescriptor const *varyingDesc) {
|
2015-04-09 18:16:54 +00:00
|
|
|
_computeController->Refine(_computeContext, _vertexBuffer, _varyingBuffer, vertexDesc, varyingDesc);
|
2014-05-09 00:20:54 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual void Synchronize() {
|
|
|
|
_computeController->Synchronize();
|
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual VertexBufferBinding BindVertexBuffer() {
|
|
|
|
return VertexBufferBinding(0);
|
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2013-06-27 19:57:21 +00:00
|
|
|
virtual VertexBufferBinding BindVaryingBuffer() {
|
|
|
|
return VertexBufferBinding(0);
|
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
virtual DrawContext * GetDrawContext() {
|
|
|
|
return _drawContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetFVarDataChannel(int fvarWidth, std::vector<float> const & fvarData) {
|
|
|
|
if (_patchTables and _drawContext and fvarWidth and (not fvarData.empty())) {
|
|
|
|
_drawContext->SetFVarDataTexture(*_patchTables, fvarWidth, fvarData);
|
|
|
|
}
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-09-05 22:07:46 +00:00
|
|
|
void initializeComputeContext(int numVertexElements,
|
|
|
|
int numVaryingElements ) {
|
|
|
|
|
|
|
|
assert(_refiner);
|
|
|
|
|
|
|
|
Far::StencilTablesFactory::Options options;
|
|
|
|
options.generateOffsets=true;
|
2014-10-21 23:36:26 +00:00
|
|
|
options.generateIntermediateLevels=_refiner->IsUniform() ? false : true;
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
Far::StencilTables const * vertexStencils=0, * varyingStencils=0;
|
|
|
|
|
|
|
|
if (numVertexElements>0) {
|
|
|
|
|
|
|
|
vertexStencils = Far::StencilTablesFactory::Create(*_refiner, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numVaryingElements>0) {
|
|
|
|
|
|
|
|
options.interpolationMode = Far::StencilTablesFactory::INTERPOLATE_VARYING;
|
|
|
|
|
|
|
|
varyingStencils = Far::StencilTablesFactory::Create(*_refiner, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
_computeContext = ComputeContext::Create(vertexStencils, varyingStencils);
|
|
|
|
|
|
|
|
delete vertexStencils;
|
|
|
|
delete varyingStencils;
|
2013-08-08 20:11:16 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 15:52:09 +00:00
|
|
|
void initializeDrawContext(int numElements, int level, MeshBitset bits) {
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
assert(_refiner and _vertexBuffer);
|
|
|
|
|
2015-04-23 23:58:45 +00:00
|
|
|
Far::PatchTablesFactory::Options options(level);
|
2014-09-05 22:07:46 +00:00
|
|
|
options.generateFVarTables = bits.test(MeshFVarData);
|
2014-10-13 15:52:09 +00:00
|
|
|
options.useSingleCreasePatch = bits.test(MeshUseSingleCreasePatch);
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
_patchTables = Far::PatchTablesFactory::Create(*_refiner);
|
|
|
|
|
|
|
|
_drawContext = DrawContext::Create(
|
|
|
|
_patchTables, numElements, bits.test(MeshFVarData));
|
|
|
|
|
|
|
|
_drawContext->UpdateVertexTexture(_vertexBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
int initializeVertexBuffers(int numVertexElements,
|
|
|
|
int numVaryingElements, MeshBitset bits) {
|
|
|
|
|
|
|
|
int numVertices = MeshInterface<DRAW_CONTEXT>::getNumVertices(*_refiner);
|
|
|
|
|
|
|
|
int numElements = numVertexElements +
|
|
|
|
(bits.test(MeshInterleaveVarying) ? numVaryingElements : 0);
|
|
|
|
|
|
|
|
if (numVertexElements) {
|
|
|
|
|
|
|
|
_vertexBuffer = VertexBuffer::Create(numElements, numVertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numVaryingElements>0 and (not bits.test(MeshInterleaveVarying))) {
|
|
|
|
_varyingBuffer = VertexBuffer::Create(numVaryingElements, numVertices);
|
|
|
|
}
|
|
|
|
return numElements;
|
2015-04-18 00:36:55 +00:00
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
Far::TopologyRefiner * _refiner;
|
|
|
|
Far::PatchTables * _patchTables;
|
|
|
|
|
|
|
|
VertexBuffer * _vertexBuffer,
|
|
|
|
* _varyingBuffer;
|
|
|
|
|
|
|
|
ComputeContext * _computeContext;
|
|
|
|
ComputeController * _computeController;
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
DrawContext *_drawContext;
|
2012-06-09 00:06:35 +00:00
|
|
|
};
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
} // end namespace Osd
|
|
|
|
|
|
|
|
} // end namespace OPENSUBDIV_VERSION
|
2012-06-09 00:06:35 +00:00
|
|
|
using namespace OPENSUBDIV_VERSION;
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
} // end namespace OpenSubdiv
|
2012-06-09 00:06:35 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
#endif // OSD_MESH_H
|