OpenSubdiv/opensubdiv/osdutil/batchCL.h

261 lines
10 KiB
C
Raw Normal View History

2013-05-10 16:20:45 +00:00
//
//
// Copyright 2013 Pixar
2013-05-10 16:20:45 +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:
2013-05-10 16:20:45 +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.
2013-05-10 16:20:45 +00:00
//
// You may obtain a copy of the Apache License at
2013-07-18 21:19:50 +00:00
//
// http://www.apache.org/licenses/LICENSE-2.0
2013-05-10 16:20:45 +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.
//
#ifndef OSDUTIL_MESH_BATCH_CL_H
#define OSDUTIL_MESH_BATCH_CL_H
2013-05-10 16:20:45 +00:00
#include "../version.h"
#include "../osdutil/batch.h"
2013-05-10 16:20:45 +00:00
#include "../osd/clComputeController.h"
#include <vector>
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
// ----------------------------------------------------------------------------
// OsdUtilMeshBatch OpenCL specialized class
//
template <typename VERTEX_BUFFER, typename DRAW_CONTEXT>
class OsdUtilMeshBatch<VERTEX_BUFFER, DRAW_CONTEXT, OsdCLComputeController> : public OsdUtilMeshBatchBase<DRAW_CONTEXT> {
public:
typedef VERTEX_BUFFER VertexBuffer;
typedef OsdCLComputeController::ComputeContext ComputeContext;
typedef DRAW_CONTEXT DrawContext;
typedef OsdCLComputeController ComputeController;
typedef OsdUtilMeshBatchBase<DRAW_CONTEXT> Base;
// XXX: not happy with retaining compute controller..
static OsdUtilMeshBatch* Create(ComputeController *computeController,
const std::vector<FarMesh<OsdVertex> const * > &meshVector,
int numVertexElements,
int numVaryingElements,
int batchIndex,
bool requireFVarData=false);
2013-05-10 16:20:45 +00:00
virtual ~OsdUtilMeshBatch();
virtual typename DrawContext::VertexBufferBinding BindVertexBuffer() {
if (not _vertexBuffer) return 0;
return _vertexBuffer->BindVBO();
}
virtual typename DrawContext::VertexBufferBinding BindVaryingBuffer() {
if (not _varyingBuffer) return 0;
return _varyingBuffer->BindVBO();
}
2013-05-10 16:20:45 +00:00
virtual DrawContext * GetDrawContext() const { return _drawContext; }
// update API
virtual void UpdateCoarseVertices(int meshIndex, const float *data, int numVertices) {
if (not _vertexBuffer)
return;
Base::setMeshDirty(meshIndex);
_vertexBuffer->UpdateData(data, Base::GetVertexOffset(meshIndex), numVertices,
_computeController->GetCommandQueue());
}
virtual void UpdateCoarseVaryings(int meshIndex, const float *data, int numVertices) {
if (not _varyingBuffer)
return;
2013-05-10 16:20:45 +00:00
Base::setMeshDirty(meshIndex);
2013-05-10 16:20:45 +00:00
_varyingBuffer->UpdateData(data, Base::GetVertexOffset(meshIndex), numVertices,
_computeController->GetCommandQueue());
2013-05-10 16:20:45 +00:00
}
// utility function
virtual void FinalizeUpdate() {
// create kernel batch for dirty handles
FarKernelBatchVector batches;
Base::populateDirtyKernelBatches(batches);
2013-05-10 16:20:45 +00:00
Base::resetMeshDirty();
_computeController->Refine(_computeContext, batches, _vertexBuffer);
}
VertexBuffer *GetVertexBuffer() const { return _vertexBuffer; }
VertexBuffer *GetVaryingBuffer() const { return _varyingBuffer; }
private:
2013-05-22 02:19:00 +00:00
OsdUtilMeshBatch();
bool initialize(ComputeController *computeController,
OsdUtilMeshBatchEntryVector const &entries,
FarMesh<OsdVertex> const *farMultiMesh,
int numVertexElements,
int numVaryingElements,
int batchIndex,
bool requireFVarData);
2013-05-22 02:19:00 +00:00
bool initialize(FarPatchTables const *patchTables,
int numVertices,
int numPtexFaces,
int numVertexElements,
int numVaryingElements,
int batchIndex,
bool requireFVarData);
2013-05-10 16:20:45 +00:00
ComputeController *_computeController;
ComputeContext *_computeContext;
VertexBuffer *_vertexBuffer, *_varyingBuffer;
DrawContext *_drawContext;
};
// -----------------------------------------------------------------------------
2013-05-10 16:20:45 +00:00
template <typename VERTEX_BUFFER, typename DRAW_CONTEXT>
2013-05-22 02:19:00 +00:00
OsdUtilMeshBatch<VERTEX_BUFFER, DRAW_CONTEXT, OsdCLComputeController>::OsdUtilMeshBatch() :
_computeController(NULL), _computeContext(NULL), _vertexBuffer(NULL), _varyingBuffer(NULL), _drawContext(NULL) {
}
template <typename VERTEX_BUFFER, typename DRAW_CONTEXT> bool
OsdUtilMeshBatch<VERTEX_BUFFER, DRAW_CONTEXT, OsdCLComputeController>::initialize(ComputeController *computeController,
OsdUtilMeshBatchEntryVector const &entries,
FarMesh<OsdVertex> const *farMultiMesh,
int numVertexElements,
int numVaryingElements,
int batchIndex,
bool requireFVarData) {
2013-05-22 02:19:00 +00:00
Base::initialize(entries, farMultiMesh->GetNumVertices(), farMultiMesh->GetNumPtexFaces(),
numVertexElements, batchIndex);
_computeController = computeController;
// copy batches
Base::setKernelBatches(farMultiMesh->GetKernelBatches());
2013-05-10 16:20:45 +00:00
// create compute contexts
_computeContext = ComputeContext::Create(farMultiMesh, _computeController->GetContext());
2013-05-22 02:19:00 +00:00
if (not _computeContext) return false;
FarPatchTables const * patchTables = farMultiMesh->GetPatchTables();
if (numVertexElements) {
_vertexBuffer = VertexBuffer::Create(numVertexElements, Base::GetNumVertices(), _computeController->GetContext());
if (not _vertexBuffer) return false;
}
if (numVaryingElements) {
_varyingBuffer = VertexBuffer::Create(numVaryingElements, Base::GetNumVertices(), _computeController->GetContext());
if (not _varyingBuffer) return false;
}
_drawContext = DrawContext::Create(patchTables, requireFVarData);
2013-05-22 02:19:00 +00:00
if (not _drawContext) return false;
_drawContext->UpdateVertexTexture(_vertexBuffer);
2013-05-22 02:19:00 +00:00
return true;
}
2013-05-22 02:19:00 +00:00
template <typename VERTEX_BUFFER, typename DRAW_CONTEXT> bool
OsdUtilMeshBatch<VERTEX_BUFFER, DRAW_CONTEXT, OsdCLComputeController>::initialize(FarPatchTables const *patchTables,
int numVertices,
int numPtexFaces,
int numVertexElements,
int numVaryingElements,
int batchIndex,
bool requireFVarData) {
2013-05-22 02:19:00 +00:00
Base::initialize(numVertices, numPtexFaces, numVertexElements, batchIndex);
if (numVertexElements) {
_vertexBuffer = VertexBuffer::Create(numVertexElements, Base::GetNumVertices(), _computeController->GetContext());
if (not _vertexBuffer) return false;
}
if (numVaryingElements) {
_varyingBuffer = VertexBuffer::Create(numVaryingElements, Base::GetNumVertices(), _computeController->GetContext());
if (not _varyingBuffer) return false;
}
_drawContext = DrawContext::Create(patchTables, requireFVarData);
2013-05-22 02:19:00 +00:00
if (not _drawContext) return false;
_drawContext->UpdateVertexTexture(_vertexBuffer);
2013-05-22 02:19:00 +00:00
return true;
2013-05-10 16:20:45 +00:00
}
template <typename VERTEX_BUFFER, typename DRAW_CONTEXT>
OsdUtilMeshBatch<VERTEX_BUFFER, DRAW_CONTEXT, OsdCLComputeController>::~OsdUtilMeshBatch()
{
2013-05-10 16:20:45 +00:00
delete _computeContext;
delete _drawContext;
delete _vertexBuffer;
delete _varyingBuffer;
}
template <typename VERTEX_BUFFER, typename DRAW_CONTEXT>
OsdUtilMeshBatch<VERTEX_BUFFER, DRAW_CONTEXT, OsdCLComputeController> *
OsdUtilMeshBatch<VERTEX_BUFFER, DRAW_CONTEXT, OsdCLComputeController>::Create(ComputeController *computeController,
const std::vector<FarMesh<OsdVertex> const * > &meshVector,
int numVertexElements,
int numVaryingElements,
int batchIndex,
bool requireFVarData) {
std::vector<FarPatchTables::PatchArrayVector> multiFarPatchArray;
FarMesh <OsdVertex> *farMultiMesh = createMultiMesh(meshVector, multiFarPatchArray);
FarPatchTables const * patchTables = farMultiMesh->GetPatchTables();
// create entries
OsdUtilMeshBatchEntryVector entries;
createEntries(entries, multiFarPatchArray, patchTables->GetMaxValence(), meshVector);
OsdUtilMeshBatch<VERTEX_BUFFER, DRAW_CONTEXT, OsdCLComputeController> *batch =
2013-05-22 02:19:00 +00:00
new OsdUtilMeshBatch<VERTEX_BUFFER, DRAW_CONTEXT, OsdCLComputeController>();
if (not batch->initialize(computeController, entries, farMultiMesh,
numVertexElements, numVaryingElements,
batchIndex, requireFVarData)) {
delete batch;
batch = NULL;
}
delete farMultiMesh;
return batch;
}
2013-05-10 16:20:45 +00:00
} // end namespace OPENSUBDIV_VERSION
using namespace OPENSUBDIV_VERSION;
} // end namespace OpenSubdiv
#endif /* OSDUTIL_MESH_BATCH_H */