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
|
|
|
|
2015-05-19 18:22:37 +00:00
|
|
|
#ifndef OPENSUBDIV3_OSD_MESH_H
|
|
|
|
#define OPENSUBDIV3_OSD_MESH_H
|
2012-06-09 00:06:35 +00:00
|
|
|
|
2012-06-12 00:02:27 +00:00
|
|
|
#include "../version.h"
|
2012-06-20 00:15:07 +00:00
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
#include <bitset>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
#include "../far/topologyRefiner.h"
|
2015-05-22 18:50:01 +00:00
|
|
|
#include "../far/patchTableFactory.h"
|
|
|
|
#include "../far/stencilTable.h"
|
|
|
|
#include "../far/stencilTableFactory.h"
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2015-05-29 16:21:14 +00:00
|
|
|
#include "../osd/bufferDescriptor.h"
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2015-05-09 00:31:26 +00:00
|
|
|
struct ID3D11DeviceContext;
|
|
|
|
|
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,
|
2015-05-11 17:59:16 +00:00
|
|
|
MeshFVarData = 2,
|
2016-09-29 16:58:47 +00:00
|
|
|
MeshFVarAdaptive = 3,
|
|
|
|
MeshUseSingleCreasePatch = 4,
|
|
|
|
MeshEndCapBSplineBasis = 5, // exclusive
|
|
|
|
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
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2015-05-20 21:36:57 +00:00
|
|
|
template <class PATCH_TABLE>
|
2014-09-05 22:07:46 +00:00
|
|
|
class MeshInterface {
|
2012-06-09 00:06:35 +00:00
|
|
|
public:
|
2015-05-20 21:36:57 +00:00
|
|
|
typedef PATCH_TABLE PatchTable;
|
|
|
|
typedef typename PatchTable::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
|
|
|
|
2015-05-20 21:36:57 +00:00
|
|
|
virtual int GetMaxValence() const = 0;
|
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
virtual void UpdateVertexBuffer(float const *vertexData,
|
|
|
|
int startVertex, int numVerts) = 0;
|
2012-06-09 18:12:34 +00:00
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
virtual void UpdateVaryingBuffer(float const *varyingData,
|
|
|
|
int startVertex, int numVerts) = 0;
|
2013-06-27 19:57:21 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual void Refine() = 0;
|
2012-06-09 00:06:35 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual void Synchronize() = 0;
|
2012-06-09 18:12:34 +00:00
|
|
|
|
2015-05-20 21:36:57 +00:00
|
|
|
virtual PatchTable * GetPatchTable() const = 0;
|
|
|
|
|
2015-05-22 18:50:01 +00:00
|
|
|
virtual Far::PatchTable const *GetFarPatchTable() const = 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
|
|
|
protected:
|
2015-04-27 18:27:05 +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;
|
|
|
|
|
2016-09-29 16:58:47 +00:00
|
|
|
Far::TopologyRefiner::UniformOptions options(level);
|
|
|
|
options.fullTopologyInLastLevel = fullTopologyInLastLevel;
|
|
|
|
refiner.RefineUniform(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static inline void refineMesh(Far::TopologyRefiner & refiner,
|
|
|
|
int level, MeshBitset bits) {
|
|
|
|
if (bits.test(MeshAdaptive)) {
|
|
|
|
Far::TopologyRefiner::AdaptiveOptions options(level);
|
|
|
|
options.useSingleCreasePatch = bits.test(MeshUseSingleCreasePatch);
|
|
|
|
options.considerFVarChannels = bits.test(MeshFVarAdaptive);
|
|
|
|
refiner.RefineAdaptive(options);
|
|
|
|
} else {
|
|
|
|
// 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
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-05-28 00:23:36 +00:00
|
|
|
template <typename STENCIL_TABLE, typename SRC_STENCIL_TABLE,
|
|
|
|
typename DEVICE_CONTEXT>
|
2015-05-22 18:50:01 +00:00
|
|
|
STENCIL_TABLE const *
|
|
|
|
convertToCompatibleStencilTable(
|
2015-05-28 00:23:36 +00:00
|
|
|
SRC_STENCIL_TABLE const *table, DEVICE_CONTEXT *context) {
|
2016-02-19 20:12:46 +00:00
|
|
|
if (! table) return NULL;
|
2015-05-22 18:50:01 +00:00
|
|
|
return STENCIL_TABLE::Create(table, context);
|
2015-05-09 00:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2015-07-19 16:06:54 +00:00
|
|
|
inline Far::StencilTable const *
|
2015-05-28 00:23:36 +00:00
|
|
|
convertToCompatibleStencilTable<Far::StencilTable, Far::StencilTable, void>(
|
2015-05-22 18:50:01 +00:00
|
|
|
Far::StencilTable const *table, void * /*context*/) {
|
2015-05-09 00:31:26 +00:00
|
|
|
// no need for conversion
|
|
|
|
// XXX: We don't want to even copy.
|
2016-02-19 20:12:46 +00:00
|
|
|
if (! table) return NULL;
|
2015-05-22 18:50:01 +00:00
|
|
|
return new Far::StencilTable(*table);
|
2015-05-09 00:31:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 00:23:36 +00:00
|
|
|
template <>
|
2015-07-19 16:06:54 +00:00
|
|
|
inline Far::LimitStencilTable const *
|
2015-05-28 00:23:36 +00:00
|
|
|
convertToCompatibleStencilTable<Far::LimitStencilTable, Far::LimitStencilTable, void>(
|
|
|
|
Far::LimitStencilTable const *table, void * /*context*/) {
|
|
|
|
// no need for conversion
|
|
|
|
// XXX: We don't want to even copy.
|
2016-02-19 20:12:46 +00:00
|
|
|
if (! table) return NULL;
|
2015-05-28 00:23:36 +00:00
|
|
|
return new Far::LimitStencilTable(*table);
|
|
|
|
}
|
|
|
|
|
2015-05-09 00:31:26 +00:00
|
|
|
template <>
|
2015-07-19 16:06:54 +00:00
|
|
|
inline Far::StencilTable const *
|
2015-05-28 00:23:36 +00:00
|
|
|
convertToCompatibleStencilTable<Far::StencilTable, Far::StencilTable, ID3D11DeviceContext>(
|
2015-05-22 18:50:01 +00:00
|
|
|
Far::StencilTable const *table, ID3D11DeviceContext * /*context*/) {
|
2015-05-09 00:31:26 +00:00
|
|
|
// no need for conversion
|
|
|
|
// XXX: We don't want to even copy.
|
2016-02-19 20:12:46 +00:00
|
|
|
if (! table) return NULL;
|
2015-05-22 18:50:01 +00:00
|
|
|
return new Far::StencilTable(*table);
|
2015-05-09 00:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2015-05-28 00:23:36 +00:00
|
|
|
// Osd evaluator cache: for the GPU backends require compiled instance
|
|
|
|
// (GLXFB, GLCompue, CL)
|
|
|
|
//
|
|
|
|
// note: this is just an example usage and client applications are supposed
|
|
|
|
// to implement their own structure for Evaluator instance.
|
|
|
|
//
|
2015-05-09 00:31:26 +00:00
|
|
|
template <typename EVALUATOR>
|
|
|
|
class EvaluatorCacheT {
|
|
|
|
public:
|
|
|
|
~EvaluatorCacheT() {
|
|
|
|
for(typename Evaluators::iterator it = _evaluators.begin();
|
|
|
|
it != _evaluators.end(); ++it) {
|
|
|
|
delete it->evaluator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: FIXME, linear search
|
|
|
|
struct Entry {
|
2015-07-30 01:46:18 +00:00
|
|
|
Entry(BufferDescriptor const &srcDescArg,
|
|
|
|
BufferDescriptor const &dstDescArg,
|
|
|
|
BufferDescriptor const &duDescArg,
|
|
|
|
BufferDescriptor const &dvDescArg,
|
|
|
|
EVALUATOR *evalArg) : srcDesc(srcDescArg), dstDesc(dstDescArg),
|
|
|
|
duDesc(duDescArg), dvDesc(dvDescArg), evaluator(evalArg) {}
|
2015-05-29 16:21:14 +00:00
|
|
|
BufferDescriptor srcDesc, dstDesc, duDesc, dvDesc;
|
2015-05-09 00:31:26 +00:00
|
|
|
EVALUATOR *evaluator;
|
|
|
|
};
|
|
|
|
typedef std::vector<Entry> Evaluators;
|
|
|
|
|
|
|
|
template <typename DEVICE_CONTEXT>
|
2015-05-29 16:21:14 +00:00
|
|
|
EVALUATOR *GetEvaluator(BufferDescriptor const &srcDesc,
|
|
|
|
BufferDescriptor const &dstDesc,
|
2015-05-09 00:31:26 +00:00
|
|
|
DEVICE_CONTEXT *deviceContext) {
|
2015-05-28 00:23:36 +00:00
|
|
|
return GetEvaluator(srcDesc, dstDesc,
|
2015-05-29 16:21:14 +00:00
|
|
|
BufferDescriptor(),
|
|
|
|
BufferDescriptor(),
|
2015-05-28 00:23:36 +00:00
|
|
|
deviceContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename DEVICE_CONTEXT>
|
2015-05-29 16:21:14 +00:00
|
|
|
EVALUATOR *GetEvaluator(BufferDescriptor const &srcDesc,
|
|
|
|
BufferDescriptor const &dstDesc,
|
|
|
|
BufferDescriptor const &duDesc,
|
|
|
|
BufferDescriptor const &dvDesc,
|
2015-05-28 00:23:36 +00:00
|
|
|
DEVICE_CONTEXT *deviceContext) {
|
2015-05-09 00:31:26 +00:00
|
|
|
|
|
|
|
for(typename Evaluators::iterator it = _evaluators.begin();
|
|
|
|
it != _evaluators.end(); ++it) {
|
2015-05-28 00:23:36 +00:00
|
|
|
if (isEqual(srcDesc, it->srcDesc) &&
|
|
|
|
isEqual(dstDesc, it->dstDesc) &&
|
|
|
|
isEqual(duDesc, it->duDesc) &&
|
|
|
|
isEqual(dvDesc, it->dvDesc)) {
|
2015-05-09 00:31:26 +00:00
|
|
|
return it->evaluator;
|
|
|
|
}
|
|
|
|
}
|
2015-05-28 00:23:36 +00:00
|
|
|
EVALUATOR *e = EVALUATOR::Create(srcDesc, dstDesc,
|
|
|
|
duDesc, dvDesc,
|
|
|
|
deviceContext);
|
|
|
|
_evaluators.push_back(Entry(srcDesc, dstDesc, duDesc, dvDesc, e));
|
2015-05-09 00:31:26 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-05-29 16:21:14 +00:00
|
|
|
static bool isEqual(BufferDescriptor const &a,
|
|
|
|
BufferDescriptor const &b) {
|
2015-05-28 00:23:36 +00:00
|
|
|
int offsetA = a.stride ? (a.offset % a.stride) : 0;
|
|
|
|
int offsetB = b.stride ? (b.offset % b.stride) : 0;
|
|
|
|
|
|
|
|
// Note: XFB kernel needs to be configured with the local offset
|
|
|
|
// of the dstDesc to skip preceding primvars.
|
|
|
|
return (offsetA == offsetB &&
|
|
|
|
a.length == b.length &&
|
|
|
|
a.stride == b.stride);
|
|
|
|
}
|
|
|
|
|
2015-05-09 00:31:26 +00:00
|
|
|
Evaluators _evaluators;
|
|
|
|
};
|
|
|
|
|
2015-06-19 01:11:23 +00:00
|
|
|
/// @cond INTERNAL
|
2015-05-09 00:31:26 +00:00
|
|
|
|
|
|
|
// template helpers to see if the evaluator is instantiatable or not.
|
|
|
|
template <typename EVALUATOR>
|
|
|
|
struct instantiatable
|
|
|
|
{
|
|
|
|
typedef char yes[1];
|
|
|
|
typedef char no[2];
|
|
|
|
template <typename C> static yes &chk(typename C::Instantiatable *t=0);
|
|
|
|
template <typename C> static no &chk(...);
|
|
|
|
static bool const value = sizeof(chk<EVALUATOR>(0)) == sizeof(yes);
|
|
|
|
};
|
|
|
|
template <bool C, typename T=void>
|
|
|
|
struct enable_if { typedef T type; };
|
|
|
|
template <typename T>
|
|
|
|
struct enable_if<false, T> { };
|
|
|
|
|
2015-06-19 01:11:23 +00:00
|
|
|
/// @endcond
|
|
|
|
|
2015-05-09 00:31:26 +00:00
|
|
|
// extract a kernel from cache if available
|
2015-05-28 00:23:36 +00:00
|
|
|
template <typename EVALUATOR, typename DEVICE_CONTEXT>
|
|
|
|
static EVALUATOR *GetEvaluator(
|
|
|
|
EvaluatorCacheT<EVALUATOR> *cache,
|
2015-05-29 16:21:14 +00:00
|
|
|
BufferDescriptor const &srcDesc,
|
|
|
|
BufferDescriptor const &dstDesc,
|
|
|
|
BufferDescriptor const &duDesc,
|
|
|
|
BufferDescriptor const &dvDesc,
|
2015-05-28 00:23:36 +00:00
|
|
|
DEVICE_CONTEXT deviceContext,
|
|
|
|
typename enable_if<instantiatable<EVALUATOR>::value, void>::type*t=0) {
|
|
|
|
(void)t;
|
|
|
|
if (cache == NULL) return NULL;
|
|
|
|
return cache->GetEvaluator(srcDesc, dstDesc, duDesc, dvDesc, deviceContext);
|
|
|
|
}
|
|
|
|
|
2015-05-09 00:31:26 +00:00
|
|
|
template <typename EVALUATOR, typename DEVICE_CONTEXT>
|
|
|
|
static EVALUATOR *GetEvaluator(
|
|
|
|
EvaluatorCacheT<EVALUATOR> *cache,
|
2015-05-29 16:21:14 +00:00
|
|
|
BufferDescriptor const &srcDesc,
|
|
|
|
BufferDescriptor const &dstDesc,
|
2015-05-09 00:31:26 +00:00
|
|
|
DEVICE_CONTEXT deviceContext,
|
|
|
|
typename enable_if<instantiatable<EVALUATOR>::value, void>::type*t=0) {
|
|
|
|
(void)t;
|
|
|
|
if (cache == NULL) return NULL;
|
2015-05-28 00:23:36 +00:00
|
|
|
return cache->GetEvaluator(srcDesc, dstDesc,
|
2015-05-29 16:21:14 +00:00
|
|
|
BufferDescriptor(),
|
|
|
|
BufferDescriptor(),
|
2015-05-28 00:23:36 +00:00
|
|
|
deviceContext);
|
2015-05-09 00:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fallback
|
2015-05-28 00:23:36 +00:00
|
|
|
template <typename EVALUATOR, typename DEVICE_CONTEXT>
|
|
|
|
static EVALUATOR *GetEvaluator(
|
|
|
|
EvaluatorCacheT<EVALUATOR> *,
|
2015-05-29 16:21:14 +00:00
|
|
|
BufferDescriptor const &,
|
|
|
|
BufferDescriptor const &,
|
|
|
|
BufferDescriptor const &,
|
|
|
|
BufferDescriptor const &,
|
2015-05-28 00:23:36 +00:00
|
|
|
DEVICE_CONTEXT,
|
|
|
|
typename enable_if<!instantiatable<EVALUATOR>::value, void>::type*t=0) {
|
|
|
|
(void)t;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-09 00:31:26 +00:00
|
|
|
template <typename EVALUATOR, typename DEVICE_CONTEXT>
|
|
|
|
static EVALUATOR *GetEvaluator(
|
|
|
|
EvaluatorCacheT<EVALUATOR> *,
|
2015-05-29 16:21:14 +00:00
|
|
|
BufferDescriptor const &,
|
|
|
|
BufferDescriptor const &,
|
2015-05-09 00:31:26 +00:00
|
|
|
DEVICE_CONTEXT,
|
|
|
|
typename enable_if<!instantiatable<EVALUATOR>::value, void>::type*t=0) {
|
|
|
|
(void)t;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <typename VERTEX_BUFFER,
|
2015-05-22 18:50:01 +00:00
|
|
|
typename STENCIL_TABLE,
|
2015-05-09 00:31:26 +00:00
|
|
|
typename EVALUATOR,
|
2015-05-20 21:36:57 +00:00
|
|
|
typename PATCH_TABLE,
|
2015-05-09 00:31:26 +00:00
|
|
|
typename DEVICE_CONTEXT = void>
|
2015-05-20 21:36:57 +00:00
|
|
|
class Mesh : public MeshInterface<PATCH_TABLE> {
|
2012-12-11 01:15:13 +00:00
|
|
|
public:
|
|
|
|
typedef VERTEX_BUFFER VertexBuffer;
|
2015-05-09 00:31:26 +00:00
|
|
|
typedef EVALUATOR Evaluator;
|
2015-05-22 18:50:01 +00:00
|
|
|
typedef STENCIL_TABLE StencilTable;
|
2015-05-20 21:36:57 +00:00
|
|
|
typedef PATCH_TABLE PatchTable;
|
2015-04-27 18:27:05 +00:00
|
|
|
typedef DEVICE_CONTEXT DeviceContext;
|
2015-05-09 00:31:26 +00:00
|
|
|
typedef EvaluatorCacheT<Evaluator> EvaluatorCache;
|
2015-05-20 21:36:57 +00:00
|
|
|
typedef typename PatchTable::VertexBufferBinding VertexBufferBinding;
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2015-05-09 00:31:26 +00:00
|
|
|
Mesh(Far::TopologyRefiner * refiner,
|
2015-04-27 18:27:05 +00:00
|
|
|
int numVertexElements,
|
|
|
|
int numVaryingElements,
|
|
|
|
int level,
|
|
|
|
MeshBitset bits = MeshBitset(),
|
2015-05-09 00:31:26 +00:00
|
|
|
EvaluatorCache * evaluatorCache = NULL,
|
2015-04-27 18:27:05 +00:00
|
|
|
DeviceContext * deviceContext = NULL) :
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
_refiner(refiner),
|
2015-05-22 18:50:01 +00:00
|
|
|
_farPatchTable(NULL),
|
2015-04-27 18:27:05 +00:00
|
|
|
_numVertices(0),
|
2015-05-20 21:36:57 +00:00
|
|
|
_maxValence(0),
|
2015-04-27 18:27:05 +00:00
|
|
|
_vertexBuffer(NULL),
|
|
|
|
_varyingBuffer(NULL),
|
2015-05-22 18:50:01 +00:00
|
|
|
_vertexStencilTable(NULL),
|
|
|
|
_varyingStencilTable(NULL),
|
2015-05-09 00:31:26 +00:00
|
|
|
_evaluatorCache(evaluatorCache),
|
2015-05-20 21:36:57 +00:00
|
|
|
_patchTable(NULL),
|
2015-04-27 18:27:05 +00:00
|
|
|
_deviceContext(deviceContext) {
|
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
|
|
|
|
2015-05-20 21:36:57 +00:00
|
|
|
MeshInterface<PATCH_TABLE>::refineMesh(
|
2016-09-29 16:58:47 +00:00
|
|
|
*_refiner, level, bits);
|
2013-08-08 20:11:16 +00:00
|
|
|
|
2015-05-09 00:31:26 +00:00
|
|
|
int vertexBufferStride = numVertexElements +
|
2015-04-27 18:27:05 +00:00
|
|
|
(bits.test(MeshInterleaveVarying) ? numVaryingElements : 0);
|
2015-05-09 00:31:26 +00:00
|
|
|
int varyingBufferStride =
|
2015-04-27 18:27:05 +00:00
|
|
|
(bits.test(MeshInterleaveVarying) ? 0 : numVaryingElements);
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
initializeContext(numVertexElements,
|
|
|
|
numVaryingElements,
|
2015-05-12 01:06:46 +00:00
|
|
|
level, bits);
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
initializeVertexBuffers(_numVertices,
|
2015-05-09 00:31:26 +00:00
|
|
|
vertexBufferStride,
|
|
|
|
varyingBufferStride);
|
|
|
|
|
|
|
|
// configure vertex buffer descriptor
|
2015-05-29 16:21:14 +00:00
|
|
|
_vertexDesc =
|
|
|
|
BufferDescriptor(0, numVertexElements, vertexBufferStride);
|
2015-05-09 00:31:26 +00:00
|
|
|
if (bits.test(MeshInterleaveVarying)) {
|
2015-05-29 16:21:14 +00:00
|
|
|
_varyingDesc = BufferDescriptor(
|
|
|
|
numVertexElements, numVaryingElements, vertexBufferStride);
|
2015-05-09 00:31:26 +00:00
|
|
|
} else {
|
2015-05-29 16:21:14 +00:00
|
|
|
_varyingDesc = BufferDescriptor(
|
|
|
|
0, numVaryingElements, varyingBufferStride);
|
2015-05-09 00:31:26 +00:00
|
|
|
}
|
2015-04-27 18:27:05 +00:00
|
|
|
}
|
2014-04-19 00:19:08 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
virtual ~Mesh() {
|
|
|
|
delete _refiner;
|
2015-05-22 18:50:01 +00:00
|
|
|
delete _farPatchTable;
|
2012-12-11 01:15:13 +00:00
|
|
|
delete _vertexBuffer;
|
2013-06-27 19:57:21 +00:00
|
|
|
delete _varyingBuffer;
|
2015-05-22 18:50:01 +00:00
|
|
|
delete _vertexStencilTable;
|
|
|
|
delete _varyingStencilTable;
|
2015-05-20 21:36:57 +00:00
|
|
|
delete _patchTable;
|
2015-05-09 00:31:26 +00:00
|
|
|
// deviceContext and evaluatorCache are not owned by this class.
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
virtual void UpdateVertexBuffer(float const *vertexData,
|
|
|
|
int startVertex, int numVerts) {
|
|
|
|
_vertexBuffer->UpdateData(vertexData, startVertex, numVerts,
|
|
|
|
_deviceContext);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
virtual void UpdateVaryingBuffer(float const *varyingData,
|
|
|
|
int startVertex, int numVerts) {
|
|
|
|
_varyingBuffer->UpdateData(varyingData, startVertex, numVerts,
|
|
|
|
_deviceContext);
|
2013-06-27 19:57:21 +00:00
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual void Refine() {
|
2015-04-27 18:27:05 +00:00
|
|
|
|
2015-05-22 03:20:16 +00:00
|
|
|
int numControlVertices = _refiner->GetLevel(0).GetNumVertices();
|
2015-05-09 00:31:26 +00:00
|
|
|
|
2015-05-29 16:21:14 +00:00
|
|
|
BufferDescriptor srcDesc = _vertexDesc;
|
|
|
|
BufferDescriptor dstDesc(srcDesc);
|
2015-05-09 00:31:26 +00:00
|
|
|
dstDesc.offset += numControlVertices * dstDesc.stride;
|
|
|
|
|
|
|
|
// note that the _evaluatorCache can be NULL and thus
|
|
|
|
// the evaluatorInstance can be NULL
|
|
|
|
// (for uninstantiatable kernels CPU,TBB etc)
|
|
|
|
Evaluator const *instance = GetEvaluator<Evaluator>(
|
2015-05-28 00:23:36 +00:00
|
|
|
_evaluatorCache, srcDesc, dstDesc,
|
|
|
|
_deviceContext);
|
2015-05-09 00:31:26 +00:00
|
|
|
|
|
|
|
Evaluator::EvalStencils(_vertexBuffer, srcDesc,
|
|
|
|
_vertexBuffer, dstDesc,
|
2015-05-22 18:50:01 +00:00
|
|
|
_vertexStencilTable,
|
2015-05-09 00:31:26 +00:00
|
|
|
instance, _deviceContext);
|
|
|
|
|
|
|
|
if (_varyingDesc.length > 0) {
|
2015-07-30 01:46:18 +00:00
|
|
|
BufferDescriptor vSrcDesc = _varyingDesc;
|
|
|
|
BufferDescriptor vDstDesc(vSrcDesc);
|
|
|
|
vDstDesc.offset += numControlVertices * vDstDesc.stride;
|
2015-05-09 00:31:26 +00:00
|
|
|
|
|
|
|
instance = GetEvaluator<Evaluator>(
|
2015-07-30 01:46:18 +00:00
|
|
|
_evaluatorCache, vSrcDesc, vDstDesc,
|
2015-05-28 00:23:36 +00:00
|
|
|
_deviceContext);
|
2015-05-09 00:31:26 +00:00
|
|
|
|
|
|
|
if (_varyingBuffer) {
|
|
|
|
// non-interleaved
|
2015-07-30 01:46:18 +00:00
|
|
|
Evaluator::EvalStencils(_varyingBuffer, vSrcDesc,
|
|
|
|
_varyingBuffer, vDstDesc,
|
2015-05-22 18:50:01 +00:00
|
|
|
_varyingStencilTable,
|
2015-05-09 00:31:26 +00:00
|
|
|
instance, _deviceContext);
|
|
|
|
} else {
|
|
|
|
// interleaved
|
2015-07-30 01:46:18 +00:00
|
|
|
Evaluator::EvalStencils(_vertexBuffer, vSrcDesc,
|
|
|
|
_vertexBuffer, vDstDesc,
|
2015-05-22 18:50:01 +00:00
|
|
|
_varyingStencilTable,
|
2015-05-09 00:31:26 +00:00
|
|
|
instance, _deviceContext);
|
|
|
|
}
|
|
|
|
}
|
2014-05-09 00:20:54 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual void Synchronize() {
|
2015-05-09 00:31:26 +00:00
|
|
|
Evaluator::Synchronize(_deviceContext);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-05-20 21:36:57 +00:00
|
|
|
virtual PatchTable * GetPatchTable() const {
|
|
|
|
return _patchTable;
|
2015-04-27 18:27:05 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 18:50:01 +00:00
|
|
|
virtual Far::PatchTable const *GetFarPatchTable() const {
|
|
|
|
return _farPatchTable;
|
2015-04-27 18:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual int GetNumVertices() const { return _numVertices; }
|
|
|
|
|
2015-05-20 21:36:57 +00:00
|
|
|
virtual int GetMaxValence() const { return _maxValence; }
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
virtual VertexBufferBinding BindVertexBuffer() {
|
2015-04-27 18:27:05 +00:00
|
|
|
return _vertexBuffer->BindVBO(_deviceContext);
|
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 VertexBufferBinding BindVaryingBuffer() {
|
2015-04-27 18:27:05 +00:00
|
|
|
return _varyingBuffer->BindVBO(_deviceContext);
|
2013-06-27 19:57:21 +00:00
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
virtual VertexBuffer * GetVertexBuffer() {
|
|
|
|
return _vertexBuffer;
|
2014-09-05 22:07:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
virtual VertexBuffer * GetVaryingBuffer() {
|
|
|
|
return _varyingBuffer;
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
virtual Far::TopologyRefiner const * GetTopologyRefiner() const {
|
|
|
|
return _refiner;
|
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
private:
|
|
|
|
void initializeContext(int numVertexElements,
|
|
|
|
int numVaryingElements,
|
2015-05-12 01:06:46 +00:00
|
|
|
int level, MeshBitset bits) {
|
2014-09-05 22:07:46 +00:00
|
|
|
assert(_refiner);
|
|
|
|
|
2015-05-22 18:50:01 +00:00
|
|
|
Far::StencilTableFactory::Options options;
|
2015-04-27 18:27:05 +00:00
|
|
|
options.generateOffsets = true;
|
|
|
|
options.generateIntermediateLevels =
|
|
|
|
_refiner->IsUniform() ? false : true;
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-05-22 18:50:01 +00:00
|
|
|
Far::StencilTable const * vertexStencils = NULL;
|
|
|
|
Far::StencilTable const * varyingStencils = NULL;
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
if (numVertexElements>0) {
|
|
|
|
|
2015-05-22 18:50:01 +00:00
|
|
|
vertexStencils = Far::StencilTableFactory::Create(*_refiner,
|
|
|
|
options);
|
2014-09-05 22:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (numVaryingElements>0) {
|
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
options.interpolationMode =
|
2015-05-22 18:50:01 +00:00
|
|
|
Far::StencilTableFactory::INTERPOLATE_VARYING;
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-05-22 18:50:01 +00:00
|
|
|
varyingStencils = Far::StencilTableFactory::Create(*_refiner,
|
|
|
|
options);
|
2014-09-05 22:07:46 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 18:50:01 +00:00
|
|
|
Far::PatchTableFactory::Options poptions(level);
|
2015-04-27 18:27:05 +00:00
|
|
|
poptions.generateFVarTables = bits.test(MeshFVarData);
|
2016-09-29 16:58:47 +00:00
|
|
|
poptions.generateFVarLegacyLinearPatches = !bits.test(MeshFVarAdaptive);
|
2015-04-27 18:27:05 +00:00
|
|
|
poptions.useSingleCreasePatch = bits.test(MeshUseSingleCreasePatch);
|
|
|
|
|
|
|
|
if (bits.test(MeshEndCapBSplineBasis)) {
|
|
|
|
poptions.SetEndCapType(
|
2015-05-22 18:50:01 +00:00
|
|
|
Far::PatchTableFactory::Options::ENDCAP_BSPLINE_BASIS);
|
2015-04-27 18:27:05 +00:00
|
|
|
} else if (bits.test(MeshEndCapGregoryBasis)) {
|
|
|
|
poptions.SetEndCapType(
|
2015-05-22 18:50:01 +00:00
|
|
|
Far::PatchTableFactory::Options::ENDCAP_GREGORY_BASIS);
|
2015-04-27 18:27:05 +00:00
|
|
|
// points on gregory basis endcap boundary can be shared among
|
|
|
|
// adjacent patches to save some stencils.
|
|
|
|
poptions.shareEndCapPatchPoints = true;
|
|
|
|
} else if (bits.test(MeshEndCapLegacyGregory)) {
|
|
|
|
poptions.SetEndCapType(
|
2015-05-22 18:50:01 +00:00
|
|
|
Far::PatchTableFactory::Options::ENDCAP_LEGACY_GREGORY);
|
2015-04-27 18:27:05 +00:00
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-05-22 18:50:01 +00:00
|
|
|
_farPatchTable = Far::PatchTableFactory::Create(*_refiner, poptions);
|
2015-04-27 18:27:05 +00:00
|
|
|
|
|
|
|
// if there's endcap stencils, merge it into regular stencils.
|
2015-05-29 19:41:11 +00:00
|
|
|
if (_farPatchTable->GetLocalPointStencilTable()) {
|
2015-04-27 18:27:05 +00:00
|
|
|
// append stencils
|
2015-05-29 19:41:11 +00:00
|
|
|
if (Far::StencilTable const *vertexStencilsWithLocalPoints =
|
|
|
|
Far::StencilTableFactory::AppendLocalPointStencilTable(
|
2015-04-27 18:27:05 +00:00
|
|
|
*_refiner,
|
|
|
|
vertexStencils,
|
2015-05-29 19:41:11 +00:00
|
|
|
_farPatchTable->GetLocalPointStencilTable())) {
|
2015-04-27 18:27:05 +00:00
|
|
|
delete vertexStencils;
|
2015-05-29 19:41:11 +00:00
|
|
|
vertexStencils = vertexStencilsWithLocalPoints;
|
2015-04-27 18:27:05 +00:00
|
|
|
}
|
|
|
|
if (varyingStencils) {
|
2015-05-29 19:41:11 +00:00
|
|
|
if (Far::StencilTable const *varyingStencilsWithLocalPoints =
|
|
|
|
Far::StencilTableFactory::AppendLocalPointStencilTable(
|
2015-04-27 18:27:05 +00:00
|
|
|
*_refiner,
|
|
|
|
varyingStencils,
|
2015-05-29 19:41:11 +00:00
|
|
|
_farPatchTable->GetLocalPointVaryingStencilTable())) {
|
2015-04-27 18:27:05 +00:00
|
|
|
delete varyingStencils;
|
2015-05-29 19:41:11 +00:00
|
|
|
varyingStencils = varyingStencilsWithLocalPoints;
|
2015-04-27 18:27:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-05-22 18:50:01 +00:00
|
|
|
_maxValence = _farPatchTable->GetMaxValence();
|
|
|
|
_patchTable = PatchTable::Create(_farPatchTable, _deviceContext);
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
// numvertices = coarse verts + refined verts + gregory basis verts
|
|
|
|
_numVertices = vertexStencils->GetNumControlVertices()
|
|
|
|
+ vertexStencils->GetNumStencils();
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-05-22 18:50:01 +00:00
|
|
|
// convert to device stenciltable if necessary.
|
|
|
|
_vertexStencilTable =
|
|
|
|
convertToCompatibleStencilTable<StencilTable>(
|
2015-05-09 00:31:26 +00:00
|
|
|
vertexStencils, _deviceContext);
|
2015-05-22 18:50:01 +00:00
|
|
|
_varyingStencilTable =
|
|
|
|
convertToCompatibleStencilTable<StencilTable>(
|
2015-05-09 00:31:26 +00:00
|
|
|
varyingStencils, _deviceContext);
|
|
|
|
|
|
|
|
// FIXME: we do extra copyings for Far::Stencils.
|
2015-04-27 18:27:05 +00:00
|
|
|
delete vertexStencils;
|
|
|
|
delete varyingStencils;
|
2014-09-05 22:07:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
void initializeVertexBuffers(int numVertices,
|
|
|
|
int numVertexElements,
|
|
|
|
int numVaryingElements) {
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
if (numVertexElements) {
|
2015-04-27 18:27:05 +00:00
|
|
|
_vertexBuffer = VertexBuffer::Create(numVertexElements,
|
|
|
|
numVertices, _deviceContext);
|
2014-09-05 22:07:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
if (numVaryingElements) {
|
|
|
|
_varyingBuffer = VertexBuffer::Create(numVaryingElements,
|
|
|
|
numVertices, _deviceContext);
|
2014-09-05 22:07:46 +00:00
|
|
|
}
|
2015-04-18 00:36:55 +00:00
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
Far::TopologyRefiner * _refiner;
|
2015-05-22 18:50:01 +00:00
|
|
|
Far::PatchTable * _farPatchTable;
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
int _numVertices;
|
2015-05-20 21:36:57 +00:00
|
|
|
int _maxValence;
|
2015-04-27 18:27:05 +00:00
|
|
|
|
2015-05-09 00:31:26 +00:00
|
|
|
VertexBuffer * _vertexBuffer;
|
|
|
|
VertexBuffer * _varyingBuffer;
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-05-29 16:21:14 +00:00
|
|
|
BufferDescriptor _vertexDesc;
|
|
|
|
BufferDescriptor _varyingDesc;
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-05-22 18:50:01 +00:00
|
|
|
StencilTable const * _vertexStencilTable;
|
|
|
|
StencilTable const * _varyingStencilTable;
|
2015-05-09 00:31:26 +00:00
|
|
|
EvaluatorCache * _evaluatorCache;
|
2015-04-27 18:27:05 +00:00
|
|
|
|
2015-05-20 21:36:57 +00:00
|
|
|
PatchTable *_patchTable;
|
2015-04-27 18:27:05 +00:00
|
|
|
DeviceContext *_deviceContext;
|
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
|
|
|
|
2015-05-19 18:22:37 +00:00
|
|
|
#endif // OPENSUBDIV3_OSD_MESH_H
|