28 #include "../version.h"
30 #include "../osd/mesh.h"
31 #include "../osd/glDrawContext.h"
32 #include "../osd/vertexDescriptor.h"
34 #ifdef OPENSUBDIV_HAS_OPENCL
35 # include "../osd/clComputeController.h"
36 # include "../osd/opencl.h"
39 namespace OpenSubdiv {
40 namespace OPENSUBDIV_VERSION {
46 template <
class VERTEX_BUFFER,
class COMPUTE_CONTROLLER>
57 int numVertexElements,
58 int numVaryingElements,
67 _computeController(computeController),
74 initializeVertexBuffers(numVertexElements, numVaryingElements, bits);
76 initializeComputeContext(numVertexElements, numVaryingElements);
78 initializeDrawContext(numElements, bits);
89 _vertexBuffer(vertexBuffer),
90 _varyingBuffer(varyingBuffer),
91 _computeContext(computeContext),
92 _computeController(computeController),
93 _drawContext(drawContext)
95 _drawContext->UpdateVertexTexture(_vertexBuffer);
101 delete _vertexBuffer;
102 delete _varyingBuffer;
103 delete _computeContext;
114 _vertexBuffer->UpdateData(vertexData, startVertex, numVerts);
118 _varyingBuffer->UpdateData(varyingData, startVertex, numVerts);
122 _computeController->Compute(_computeContext, _kernelBatches, _vertexBuffer, _varyingBuffer);
128 _computeController->Compute(_computeContext, _kernelBatches,
129 _vertexBuffer, (interleaved ? _vertexBuffer : _varyingBuffer),
130 vertexDesc, varyingDesc);
134 _computeController->Synchronize();
138 return _vertexBuffer->BindVBO();
142 return _varyingBuffer->BindVBO();
150 return _vertexBuffer;
154 return _varyingBuffer;
162 if (_patchTables and _drawContext and fvarWidth and (not fvarData.empty())) {
163 _drawContext->SetFVarDataTexture(*_patchTables, fvarWidth, fvarData);
169 void initializeComputeContext(
int numVertexElements,
170 int numVaryingElements ) {
180 if (numVertexElements>0) {
187 if (numVaryingElements>0) {
194 _computeContext = ComputeContext::Create(vertexStencils, varyingStencils);
196 delete vertexStencils;
197 delete varyingStencils;
200 void initializeDrawContext(
int numElements,
MeshBitset bits) {
202 assert(_refiner and _vertexBuffer);
204 Far::PatchTablesFactory::Options options;
209 _drawContext = DrawContext::Create(_patchTables, numElements);
211 _drawContext->UpdateVertexTexture(_vertexBuffer);
214 int initializeVertexBuffers(
int numVertexElements,
219 int numElements = numVertexElements +
222 if (numVertexElements) {
224 _vertexBuffer = VertexBuffer::Create(numElements, numVertices);
228 _varyingBuffer = VertexBuffer::Create(numVaryingElements, numVertices);
233 Far::TopologyRefiner * _refiner;
234 Far::PatchTables * _patchTables;
246 #ifdef OPENSUBDIV_HAS_OPENCL
248 template <
class VERTEX_BUFFER>
249 class Mesh<VERTEX_BUFFER, CLComputeController, GLDrawContext> :
public GLMeshInterface {
253 typedef typename ComputeController::ComputeContext
ComputeContext;
258 Far::TopologyRefiner * refiner,
259 int numVertexElements,
260 int numVaryingElements,
263 cl_context clContext,
264 cl_command_queue clQueue) :
270 _computeController(computeController),
272 _clContext(clContext),
280 initializeVertexBuffers(numVertexElements, numVaryingElements, bits);
282 initializeComputeContext(numVertexElements, numVaryingElements);
284 initializeDrawContext(numElements, bits);
288 Far::TopologyRefiner * refiner,
293 cl_context clContext,
294 cl_command_queue clQueue) :
297 _vertexBuffer(vertexBuffer),
298 _varyingBuffer(varyingBuffer),
299 _computeContext(computeContext),
300 _computeController(computeController),
301 _drawContext(drawContext),
302 _clContext(clContext),
305 _drawContext->UpdateVertexTexture(_vertexBuffer);
311 delete _vertexBuffer;
312 delete _varyingBuffer;
313 delete _computeContext;
319 virtual void UpdateVertexBuffer(
float const *vertexData,
int startVertex,
int numVerts) {
320 _vertexBuffer->UpdateData(vertexData, startVertex, numVerts, _clQueue);
323 virtual void UpdateVaryingBuffer(
float const *varyingData,
int startVertex,
int numVerts) {
324 _varyingBuffer->UpdateData(varyingData, startVertex, numVerts, _clQueue);
328 _computeController->Compute(_computeContext, _kernelBatches, _vertexBuffer, _varyingBuffer);
331 virtual void Refine(VertexBufferDescriptor
const *vertexDesc,
332 VertexBufferDescriptor
const *varyingDesc,
334 _computeController->Compute(_computeContext, _kernelBatches,
335 _vertexBuffer, (interleaved ? _vertexBuffer : _varyingBuffer),
336 vertexDesc, varyingDesc);
340 _computeController->Synchronize();
344 return _vertexBuffer->BindVBO();
348 return _varyingBuffer->BindVBO();
356 return _vertexBuffer;
360 return _varyingBuffer;
363 virtual Far::TopologyRefiner
const * GetTopologyRefiner()
const {
368 if (_patchTables and _drawContext and fvarWidth and (not fvarData.empty())) {
369 _drawContext->SetFVarDataTexture(*_patchTables, fvarWidth, fvarData);
375 void initializeComputeContext(
int numVertexElements,
376 int numVaryingElements ) {
380 Far::StencilTablesFactory::Options options;
381 options.generateOffsets=
true;
382 options.generateAllLevels=_refiner->
IsUniform() ?
false :
true;
384 Far::StencilTables
const * vertexStencils=0, * varyingStencils=0;
386 if (numVertexElements>0) {
393 if (numVaryingElements>0) {
400 _computeContext = ComputeContext::Create(_clContext, vertexStencils, varyingStencils);
402 delete vertexStencils;
403 delete varyingStencils;
406 void initializeDrawContext(
int numElements,
MeshBitset bits) {
408 assert(_refiner and _vertexBuffer);
410 Far::PatchTablesFactory::Options options;
415 _drawContext = DrawContext::Create(_patchTables, numElements);
417 _drawContext->UpdateVertexTexture(_vertexBuffer);
420 int initializeVertexBuffers(
int numVertexElements,
425 int numElements = numVertexElements +
428 if (numVertexElements) {
430 _vertexBuffer = VertexBuffer::Create(numElements, numVertices, _clContext);
434 _varyingBuffer = VertexBuffer::Create(numVaryingElements, numVertices, _clContext);
439 Far::TopologyRefiner * _refiner;
440 Far::PatchTables * _patchTables;
451 cl_context _clContext;
452 cl_command_queue _clQueue;
459 using namespace OPENSUBDIV_VERSION;
463 #endif // OSD_GL_MESH_H
virtual DrawContext * GetDrawContext()
COMPUTE_CONTROLLER ComputeController
int generateOffsets
populate optional "_offsets" field
static PatchTables * Create(TopologyRefiner const &refiner, Options options=Options())
Factory constructor for PatchTables.
virtual void Synchronize()
static void refineMesh(Far::TopologyRefiner &refiner, int level, bool adaptive)
int generateAllLevels
vertices at all levels or highest only
Mesh(ComputeController *computeController, Far::TopologyRefiner *refiner, VertexBuffer *vertexBuffer, VertexBuffer *varyingBuffer, ComputeContext *computeContext, DrawContext *drawContext)
virtual void UpdateVaryingBuffer(float const *varyingData, int startVertex, int numVerts)
virtual void UpdateVertexBuffer(float const *vertexData, int startVertex, int numVerts)
Describes vertex elements in interleaved data buffers.
virtual void SetFVarDataChannel(int fvarWidth, std::vector< float > const &fvarData)
virtual void UpdateVertexBuffer(float const *vertexData, int startVertex, int numVerts)
Table of subdivision stencils.
GLuint VertexBufferBinding
MeshInterface< GLDrawContext > GLMeshInterface
std::vector< KernelBatch > KernelBatchVector
virtual VertexBuffer * GetVertexBuffer()
GLDrawContext DrawContext
virtual VertexBuffer * GetVaryingBuffer()
virtual void Synchronize()
virtual void UpdateVaryingBuffer(float const *varyingData, int startVertex, int numVerts)
virtual Far::TopologyRefiner const * GetTopologyRefiner() const
DrawContext::VertexBufferBinding VertexBufferBinding
DrawContext::VertexBufferBinding VertexBufferBinding
int interpolationMode
interpolation mode
virtual VertexBufferBinding BindVaryingBuffer()
int GetNumVerticesTotal() const
Returns the total number of vertices in all levels.
virtual DrawContext * GetDrawContext()
VERTEX_BUFFER VertexBuffer
virtual void Refine(VertexBufferDescriptor const *vertexDesc, VertexBufferDescriptor const *varyingDesc, bool interleaved)
virtual VertexBufferBinding BindVertexBuffer()
virtual int GetNumVertices() const
std::bitset< NUM_MESH_BITS > MeshBitset
Mesh(ComputeController *computeController, Far::TopologyRefiner *refiner, int numVertexElements, int numVaryingElements, int level, MeshBitset bits)
ComputeController::ComputeContext ComputeContext
Mesh(ComputeController *computeController, Far::TopologyRefiner *refiner, int numVertexElements, int numVaryingElements, int level, MeshBitset bits=MeshBitset())
VERTEX_BUFFER VertexBuffer
virtual int GetNumVertices() const
virtual VertexBufferBinding BindVaryingBuffer()
COMPUTE_CONTROLLER ComputeController
static StencilTables const * Create(TopologyRefiner const &refiner, Options options=Options())
Instantiates StencilTables from TopologyRefiner that have been refined uniformly or adaptively...
OpenGL specialized DrawContext class.
bool IsUniform() const
Returns true if uniform subdivision has been applied.
Stores topology data for a specified set of refinement options.
virtual VertexBufferBinding BindVertexBuffer()
virtual void SetFVarDataChannel(int fvarWidth, std::vector< float > const &fvarData)
static int getNumVertices(Far::TopologyRefiner const &refiner)
ComputeController::ComputeContext ComputeContext