mirror of
https://github.com/PixarAnimationStudios/OpenSubdiv
synced 2024-12-24 00:40:33 +00:00
Adding a TBB SmoothNormal Context & Controller
Also: - Add a _numVertices member to cpuSmoothNormalContext (for memory reset function) - Fix memory reset function in cpuSmoothNormalContext (was performing redundant memsets) - Add a resetMemory boolean to cpuSmoothNormalContext to make reset step optional (default is off)
This commit is contained in:
parent
6465d3594c
commit
dabaac8187
@ -203,11 +203,13 @@ if( TBB_FOUND )
|
||||
tbbKernel.cpp
|
||||
tbbComputeController.cpp
|
||||
tbbEvalStencilsController.cpp
|
||||
tbbSmoothNormalController.cpp
|
||||
)
|
||||
list(APPEND PUBLIC_HEADER_FILES
|
||||
tbbKernel.h
|
||||
tbbComputeController.h
|
||||
tbbEvalStencilsController.h
|
||||
tbbSmoothNormalController.h
|
||||
)
|
||||
if (CMAKE_COMPILER_IS_GNUCXX)
|
||||
list(APPEND PLATFORM_CPU_LIBRARIES
|
||||
|
@ -27,7 +27,9 @@
|
||||
namespace OpenSubdiv {
|
||||
namespace OPENSUBDIV_VERSION {
|
||||
|
||||
OsdCpuSmoothNormalContext::OsdCpuSmoothNormalContext(FarPatchTables const *patchTables) {
|
||||
OsdCpuSmoothNormalContext::OsdCpuSmoothNormalContext(
|
||||
FarPatchTables const *patchTables, bool resetMemory) :
|
||||
_numVertices(0), _resetMemory(resetMemory) {
|
||||
|
||||
// copy the data from the FarTables
|
||||
_patches = patchTables->GetPatchTable();
|
||||
@ -36,9 +38,9 @@ OsdCpuSmoothNormalContext::OsdCpuSmoothNormalContext(FarPatchTables const *patch
|
||||
}
|
||||
|
||||
OsdCpuSmoothNormalContext *
|
||||
OsdCpuSmoothNormalContext::Create(FarPatchTables const *patchTables) {
|
||||
OsdCpuSmoothNormalContext::Create(FarPatchTables const *patchTables, bool resetMemory) {
|
||||
|
||||
return new OsdCpuSmoothNormalContext(patchTables);
|
||||
return new OsdCpuSmoothNormalContext(patchTables, resetMemory);
|
||||
}
|
||||
|
||||
|
||||
|
@ -42,9 +42,16 @@ public:
|
||||
|
||||
/// Creates an OsdCpuComputeContext instance
|
||||
///
|
||||
/// @param farmesh the FarMesh used for this Context.
|
||||
/// @param farmesh The FarMesh used for this Context.
|
||||
///
|
||||
static OsdCpuSmoothNormalContext * Create(FarPatchTables const *patchTables);
|
||||
/// @param resetMemory Set to true if the target vertex buffer needs its
|
||||
/// memory reset before accumulating the averaged normals.
|
||||
/// If the SmoothNormal Controller runs after a Computer
|
||||
/// Controller, then the vertex buffer will already have
|
||||
/// been reset and this step can be skipped to save time.
|
||||
///
|
||||
static OsdCpuSmoothNormalContext * Create(
|
||||
FarPatchTables const *patchTables, bool resetMemory=false);
|
||||
|
||||
/// Binds a vertex and a varying data buffers to the context. Binding ensures
|
||||
/// that data buffers are properly inter-operated between Contexts and
|
||||
@ -63,13 +70,16 @@ public:
|
||||
VERTEX_BUFFER * out, int oOfs) {
|
||||
|
||||
assert( ((iOfs+3)<=in->GetNumElements()) and
|
||||
((oOfs+3)<=out->GetNumElements()));
|
||||
((oOfs+3)<=out->GetNumElements()) and
|
||||
out->GetNumVertices()>=in->GetNumVertices());
|
||||
|
||||
_iBuffer = in ? in->BindCpuBuffer() : 0;
|
||||
_oBuffer = out ? out->BindCpuBuffer() : 0;
|
||||
|
||||
_iDesc = OsdVertexBufferDescriptor( iOfs, 3, in->GetNumElements() );
|
||||
_oDesc = OsdVertexBufferDescriptor( oOfs, 3, out->GetNumElements() );
|
||||
|
||||
_numVertices = out->GetNumVertices();
|
||||
}
|
||||
|
||||
/// Unbinds any previously bound vertex and varying data buffers.
|
||||
@ -78,6 +88,7 @@ public:
|
||||
_iBuffer = _oBuffer = 0;
|
||||
_iDesc.Reset();
|
||||
_oDesc.Reset();
|
||||
_numVertices = 0;
|
||||
}
|
||||
|
||||
/// Returns the vector of patch arrays
|
||||
@ -111,9 +122,27 @@ public:
|
||||
return _oDesc;
|
||||
}
|
||||
|
||||
/// Returns the number of vertices in output vertex buffer
|
||||
int GetNumVertices() const {
|
||||
return _numVertices;
|
||||
}
|
||||
|
||||
/// Returns whether the controller needs to reset the vertex buffer before
|
||||
/// accumulating smooth normals
|
||||
bool GetResetMemory() const {
|
||||
return _resetMemory;
|
||||
}
|
||||
|
||||
/// Set to true if the controller needs to reset the vertex buffer before
|
||||
/// accumulating smooth normals
|
||||
void SetResetMemory(bool resetMemory) {
|
||||
_resetMemory = resetMemory;
|
||||
}
|
||||
|
||||
protected:
|
||||
// Constructor
|
||||
explicit OsdCpuSmoothNormalContext(FarPatchTables const *patchTables);
|
||||
explicit OsdCpuSmoothNormalContext(
|
||||
FarPatchTables const *patchTables, bool resetMemory);
|
||||
|
||||
private:
|
||||
|
||||
@ -124,9 +153,12 @@ private:
|
||||
OsdVertexBufferDescriptor _iDesc,
|
||||
_oDesc;
|
||||
|
||||
int _numVertices;
|
||||
|
||||
float * _iBuffer,
|
||||
* _oBuffer;
|
||||
|
||||
bool _resetMemory; // set to true if the output buffer needs to be reset to 0
|
||||
};
|
||||
|
||||
|
||||
|
@ -26,21 +26,10 @@
|
||||
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
namespace OpenSubdiv {
|
||||
namespace OPENSUBDIV_VERSION {
|
||||
|
||||
OsdCpuSmoothNormalController::OsdCpuSmoothNormalController() {
|
||||
}
|
||||
|
||||
OsdCpuSmoothNormalController::~OsdCpuSmoothNormalController() {
|
||||
}
|
||||
|
||||
void
|
||||
OsdCpuSmoothNormalController::Synchronize() {
|
||||
}
|
||||
|
||||
inline void
|
||||
cross(float *n, const float *p0, const float *p1, const float *p2) {
|
||||
|
||||
@ -56,7 +45,8 @@ cross(float *n, const float *p0, const float *p1, const float *p2) {
|
||||
n[2] *= rn;
|
||||
}
|
||||
|
||||
void OsdCpuSmoothNormalController::_smootheNormals(OsdCpuSmoothNormalContext * context) {
|
||||
void OsdCpuSmoothNormalController::_smootheNormals(
|
||||
OsdCpuSmoothNormalContext * context) {
|
||||
|
||||
OsdVertexBufferDescriptor const & iDesc = context->GetInputVertexDescriptor(),
|
||||
& oDesc = context->GetOutputVertexDescriptor();
|
||||
@ -85,9 +75,12 @@ void OsdCpuSmoothNormalController::_smootheNormals(OsdCpuSmoothNormalContext * c
|
||||
|
||||
int nv = FarPatchTables::Descriptor::GetNumControlVertices(type);
|
||||
|
||||
// reset all normal values to 0
|
||||
for (int j=0, idx=pa.GetVertIndex(); j<(int)pa.GetNumPatches()*nv; ++j, ++idx) {
|
||||
memset(oBuffer + verts[idx]*oDesc.stride, 0, oDesc.length*sizeof(float));
|
||||
// if necessary, reset all normal values to 0
|
||||
if (context->GetResetMemory()) {
|
||||
float * ptr = oBuffer;
|
||||
for (int j=0; j<context->GetNumVertices(); ++j, ptr += oDesc.stride) {
|
||||
memset(ptr, 0, oDesc.length*sizeof(float));
|
||||
}
|
||||
}
|
||||
|
||||
for (int j=0, idx=pa.GetVertIndex(); j<(int)pa.GetNumPatches(); ++j, idx+=nv) {
|
||||
@ -116,5 +109,15 @@ void OsdCpuSmoothNormalController::_smootheNormals(OsdCpuSmoothNormalContext * c
|
||||
|
||||
}
|
||||
|
||||
OsdCpuSmoothNormalController::OsdCpuSmoothNormalController() {
|
||||
}
|
||||
|
||||
OsdCpuSmoothNormalController::~OsdCpuSmoothNormalController() {
|
||||
}
|
||||
|
||||
void
|
||||
OsdCpuSmoothNormalController::Synchronize() {
|
||||
}
|
||||
|
||||
} // end namespace OPENSUBDIV_VERSION
|
||||
} // end namespace OpenSubdiv
|
||||
|
207
opensubdiv/osd/tbbSmoothNormalController.cpp
Normal file
207
opensubdiv/osd/tbbSmoothNormalController.cpp
Normal file
@ -0,0 +1,207 @@
|
||||
//
|
||||
// Copyright 2013 Pixar
|
||||
//
|
||||
// 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:
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#include "../osd/tbbSmoothNormalController.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <tbb/parallel_for.h>
|
||||
|
||||
namespace OpenSubdiv {
|
||||
namespace OPENSUBDIV_VERSION {
|
||||
|
||||
inline void
|
||||
cross(float *n, const float *p0, const float *p1, const float *p2) {
|
||||
|
||||
float a[3] = { p1[0]-p0[0], p1[1]-p0[1], p1[2]-p0[2] };
|
||||
float b[3] = { p2[0]-p0[0], p2[1]-p0[1], p2[2]-p0[2] };
|
||||
n[0] = a[1]*b[2]-a[2]*b[1];
|
||||
n[1] = a[2]*b[0]-a[0]*b[2];
|
||||
n[2] = a[0]*b[1]-a[1]*b[0];
|
||||
|
||||
float rn = 1.0f/sqrtf(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
|
||||
n[0] *= rn;
|
||||
n[1] *= rn;
|
||||
n[2] *= rn;
|
||||
}
|
||||
|
||||
#define grain_size 200
|
||||
|
||||
// TBB kernel to reset normals to 0.0f
|
||||
class TBBResetKernel {
|
||||
|
||||
float * _oBuffer;
|
||||
int _oStride;
|
||||
|
||||
public:
|
||||
|
||||
void operator() (tbb::blocked_range<int> const &r) const {
|
||||
|
||||
float * dst = _oBuffer + r.begin() * _oStride;
|
||||
|
||||
// reset normals to 0
|
||||
for (int i=r.begin(); i<r.end(); ++i, dst+=_oStride) {
|
||||
memset(dst, 0, 3*sizeof(float));
|
||||
}
|
||||
}
|
||||
|
||||
TBBResetKernel(TBBResetKernel const & other) {
|
||||
this->_oBuffer = other._oBuffer;
|
||||
this->_oStride = other._oStride;
|
||||
}
|
||||
|
||||
TBBResetKernel(float * oBuffer, int oStride) :
|
||||
_oBuffer(oBuffer), _oStride(oStride) {
|
||||
}
|
||||
};
|
||||
|
||||
// TBB kernel that averages face normals into vertex buffer
|
||||
class TBBSmoothNormalKernel {
|
||||
|
||||
float const * _iBuffer;
|
||||
float * _oBuffer;
|
||||
|
||||
unsigned int const * _vertIndices;
|
||||
|
||||
int _iStride,
|
||||
_oStride,
|
||||
_numVertices;
|
||||
|
||||
public:
|
||||
|
||||
void operator() (tbb::blocked_range<int> const &r) const {
|
||||
|
||||
int idx = r.begin()*_numVertices;
|
||||
|
||||
for (int i=r.begin(); i<r.end(); ++i, idx+=_numVertices) {
|
||||
|
||||
float const * p0 = _iBuffer + _vertIndices[idx+0]*_iStride,
|
||||
* p1 = _iBuffer + _vertIndices[idx+1]*_iStride,
|
||||
* p2 = _iBuffer + _vertIndices[idx+2]*_iStride;
|
||||
|
||||
// compute face normal
|
||||
float n[3];
|
||||
cross( n, p0, p1, p2 );
|
||||
|
||||
// add normal to all vertices of the face
|
||||
for (int j=0; j<_numVertices; ++j) {
|
||||
|
||||
float * dst = _oBuffer + _vertIndices[idx+j]*_oStride;
|
||||
|
||||
dst[0] += n[0];
|
||||
dst[1] += n[1];
|
||||
dst[2] += n[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TBBSmoothNormalKernel( TBBSmoothNormalKernel const & other ) {
|
||||
this->_iBuffer = other._iBuffer;
|
||||
this->_oBuffer = other._oBuffer;
|
||||
this->_vertIndices = other._vertIndices;
|
||||
this->_iStride = other._iStride;
|
||||
this->_oStride = other._oStride;
|
||||
this->_numVertices = other._numVertices;
|
||||
}
|
||||
|
||||
TBBSmoothNormalKernel( float const * iBuffer,
|
||||
int iStride,
|
||||
float * oBuffer,
|
||||
int oStride,
|
||||
unsigned int const * vertIndices,
|
||||
int numVertices ) :
|
||||
_iBuffer(iBuffer),
|
||||
_oBuffer(oBuffer),
|
||||
_vertIndices(vertIndices),
|
||||
_iStride(iStride),
|
||||
_oStride(oStride),
|
||||
_numVertices(numVertices) {
|
||||
}
|
||||
};
|
||||
|
||||
void OsdTbbSmoothNormalController::_smootheNormals(
|
||||
OsdCpuSmoothNormalContext * context) {
|
||||
|
||||
OsdVertexBufferDescriptor const & iDesc = context->GetInputVertexDescriptor(),
|
||||
& oDesc = context->GetOutputVertexDescriptor();
|
||||
|
||||
assert(iDesc.length==3 and oDesc.length==3);
|
||||
|
||||
float const * iBuffer = context->GetCurrentInputVertexBuffer() + iDesc.offset;
|
||||
float * oBuffer = context->GetCurrentOutputVertexBuffer() + oDesc.offset;
|
||||
|
||||
std::vector<unsigned int> const & verts = context->GetControlVertices();
|
||||
|
||||
FarPatchTables::PatchArrayVector const & parrays = context->GetPatchArrayVector();
|
||||
|
||||
if (verts.empty() or parrays.empty() or (not iBuffer) or (not oBuffer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i=0; i<(int)parrays.size(); ++i) {
|
||||
|
||||
FarPatchTables::PatchArray const & pa = parrays[i];
|
||||
|
||||
FarPatchTables::Type type = pa.GetDescriptor().GetType();
|
||||
|
||||
if (type==FarPatchTables::QUADS or type==FarPatchTables::TRIANGLES) {
|
||||
|
||||
|
||||
// if necessary, reset all normal values to 0
|
||||
if (context->GetResetMemory()) {
|
||||
|
||||
TBBResetKernel resetKernel(oBuffer, oDesc.stride);
|
||||
tbb::blocked_range<int> range(0, context->GetNumVertices(), grain_size);
|
||||
tbb::parallel_for(range, resetKernel);
|
||||
}
|
||||
|
||||
{
|
||||
int nv = FarPatchTables::Descriptor::GetNumControlVertices(type);
|
||||
TBBSmoothNormalKernel smoothNormalkernel( iBuffer,
|
||||
iDesc.stride,
|
||||
oBuffer,
|
||||
oDesc.stride,
|
||||
&verts[pa.GetVertIndex()],
|
||||
nv);
|
||||
|
||||
tbb::blocked_range<int> range(0, pa.GetNumPatches(), grain_size);
|
||||
tbb::parallel_for(range, smoothNormalkernel);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OsdTbbSmoothNormalController::OsdTbbSmoothNormalController() {
|
||||
}
|
||||
|
||||
OsdTbbSmoothNormalController::~OsdTbbSmoothNormalController() {
|
||||
}
|
||||
|
||||
void
|
||||
OsdTbbSmoothNormalController::Synchronize() {
|
||||
}
|
||||
|
||||
} // end namespace OPENSUBDIV_VERSION
|
||||
} // end namespace OpenSubdiv
|
74
opensubdiv/osd/tbbSmoothNormalController.h
Normal file
74
opensubdiv/osd/tbbSmoothNormalController.h
Normal file
@ -0,0 +1,74 @@
|
||||
//
|
||||
// Copyright 2013 Pixar
|
||||
//
|
||||
// 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:
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// 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 OSD_TBB_SMOOTHNORMAL_CONTROLLER_H
|
||||
#define OSD_TBB_SMOOTHNORMAL_CONTROLLER_H
|
||||
|
||||
#include "../version.h"
|
||||
|
||||
#include "../osd/nonCopyable.h"
|
||||
#include "../osd/cpuSmoothNormalContext.h"
|
||||
|
||||
namespace OpenSubdiv {
|
||||
namespace OPENSUBDIV_VERSION {
|
||||
|
||||
class OsdTbbSmoothNormalController {
|
||||
|
||||
public:
|
||||
|
||||
/// Constructor
|
||||
OsdTbbSmoothNormalController();
|
||||
|
||||
/// Destructor
|
||||
~OsdTbbSmoothNormalController();
|
||||
|
||||
/// Computes smooth vertex normals
|
||||
template<class VERTEX_BUFFER>
|
||||
void SmootheNormals( OsdCpuSmoothNormalContext * context,
|
||||
VERTEX_BUFFER * iBuffer, int iOfs,
|
||||
VERTEX_BUFFER * oBuffer, int oOfs ) {
|
||||
|
||||
if (not context) return;
|
||||
|
||||
context->Bind(iBuffer, iOfs, oBuffer, oOfs);
|
||||
|
||||
_smootheNormals(context);
|
||||
|
||||
context->Unbind();
|
||||
}
|
||||
|
||||
/// Waits until all running subdivision kernels finish.
|
||||
void Synchronize();
|
||||
|
||||
private:
|
||||
|
||||
void _smootheNormals(OsdCpuSmoothNormalContext * context);
|
||||
};
|
||||
|
||||
} // end namespace OPENSUBDIV_VERSION
|
||||
using namespace OPENSUBDIV_VERSION;
|
||||
|
||||
} // end namespace OpenSubdiv
|
||||
|
||||
#endif // OSD_CPU_SMOOTHNORMAL_CONTROLLER_H
|
Loading…
Reference in New Issue
Block a user