Adding a Context / Controller pair for CPU evaluation of smooth normals

This commit is contained in:
manuelk 2014-01-09 17:36:35 -08:00
parent 8918173fda
commit 6465d3594c
5 changed files with 382 additions and 0 deletions

View File

@ -59,6 +59,8 @@ set(CPU_SOURCE_FILES
cpuEvalLimitKernel.cpp
cpuEvalStencilsContext.cpp
cpuEvalStencilsController.cpp
cpuSmoothNormalContext.cpp
cpuSmoothNormalController.cpp
cpuVertexBuffer.cpp
error.cpp
evalLimitContext.cpp
@ -89,6 +91,8 @@ set(PUBLIC_HEADER_FILES
cpuEvalLimitController.h
cpuEvalStencilsContext.h
cpuEvalStencilsController.h
cpuSmoothNormalContext.h
cpuSmoothNormalController.h
cpuVertexBuffer.h
error.h
evalLimitContext.h

View File

@ -0,0 +1,46 @@
//
// 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/cpuSmoothNormalContext.h"
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
OsdCpuSmoothNormalContext::OsdCpuSmoothNormalContext(FarPatchTables const *patchTables) {
// copy the data from the FarTables
_patches = patchTables->GetPatchTable();
_patchArrays = patchTables->GetPatchArrayVector();
}
OsdCpuSmoothNormalContext *
OsdCpuSmoothNormalContext::Create(FarPatchTables const *patchTables) {
return new OsdCpuSmoothNormalContext(patchTables);
}
} // end namespace OPENSUBDIV_VERSION
} // end namespace OpenSubdiv

View File

@ -0,0 +1,138 @@
//
// 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_CPU_SMOOTHNORMAL_CONTEXT_H
#define OSD_CPU_SMOOTHNORMAL_CONTEXT_H
#include "../version.h"
#include "../osd/nonCopyable.h"
#include "../osd/vertexDescriptor.h"
#include "../osd/vertex.h"
#include "../far/patchTables.h"
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
class OsdCpuSmoothNormalContext : OsdNonCopyable<OsdCpuSmoothNormalContext> {
public:
/// Creates an OsdCpuComputeContext instance
///
/// @param farmesh the FarMesh used for this Context.
///
static OsdCpuSmoothNormalContext * Create(FarPatchTables const *patchTables);
/// Binds a vertex and a varying data buffers to the context. Binding ensures
/// that data buffers are properly inter-operated between Contexts and
/// Controllers operating across multiple devices.
///
/// @param in a buffer containing input vertex-interpolated primvar data
///
/// @param iOfs offset to the buffer element describing the vertex position
///
/// @param out a buffer where the smooth normals will be output
///
/// @param oOfs offset to the buffer element describing the normal position
///
template<class VERTEX_BUFFER>
void Bind(VERTEX_BUFFER * in, int iOfs,
VERTEX_BUFFER * out, int oOfs) {
assert( ((iOfs+3)<=in->GetNumElements()) and
((oOfs+3)<=out->GetNumElements()));
_iBuffer = in ? in->BindCpuBuffer() : 0;
_oBuffer = out ? out->BindCpuBuffer() : 0;
_iDesc = OsdVertexBufferDescriptor( iOfs, 3, in->GetNumElements() );
_oDesc = OsdVertexBufferDescriptor( oOfs, 3, out->GetNumElements() );
}
/// Unbinds any previously bound vertex and varying data buffers.
void Unbind() {
_iBuffer = _oBuffer = 0;
_iDesc.Reset();
_oDesc.Reset();
}
/// Returns the vector of patch arrays
const FarPatchTables::PatchArrayVector & GetPatchArrayVector() const {
return _patchArrays;
}
/// The ordered array of control vertex indices for all the patches
const std::vector<unsigned int> & GetControlVertices() const {
return _patches;
}
/// Returns a pointer to the data of the input buffer
float const * GetCurrentInputVertexBuffer() const {
return _iBuffer;
}
/// Returns a pointer to the data of the output buffer
float * GetCurrentOutputVertexBuffer() {
return _oBuffer;
}
/// Returns an OsdVertexDescriptor for the input vertex data
OsdVertexBufferDescriptor const & GetInputVertexDescriptor() const {
return _iDesc;
}
/// Returns an OsdVertexDescriptor for the buffer where the normals data
/// will be stored
OsdVertexBufferDescriptor const & GetOutputVertexDescriptor() const {
return _oDesc;
}
protected:
// Constructor
explicit OsdCpuSmoothNormalContext(FarPatchTables const *patchTables);
private:
// Topology data for a mesh
FarPatchTables::PatchArrayVector _patchArrays; // patch descriptor for each patch in the mesh
FarPatchTables::PTable _patches; // patch control vertices
OsdVertexBufferDescriptor _iDesc,
_oDesc;
float * _iBuffer,
* _oBuffer;
};
} // end namespace OPENSUBDIV_VERSION
using namespace OPENSUBDIV_VERSION;
} // end namespace OpenSubdiv
#endif // OSD_CPU_SMOOTHNORMAL_CONTEXT_H

View File

@ -0,0 +1,120 @@
//
// 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/cpuSmoothNormalController.h"
#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) {
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;
}
void OsdCpuSmoothNormalController::_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) {
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));
}
for (int j=0, idx=pa.GetVertIndex(); j<(int)pa.GetNumPatches(); ++j, idx+=nv) {
float const * p0 = iBuffer + verts[idx+0]*iDesc.stride,
* p1 = iBuffer + verts[idx+1]*iDesc.stride,
* p2 = iBuffer + verts[idx+2]*iDesc.stride;
// compute face normal
float n[3];
cross( n, p0, p1, p2 );
// add normal to all vertices of the face
for (int k=0; k<nv; ++k) {
float * dst = oBuffer + verts[idx+k]*oDesc.stride;
dst[0] += n[0];
dst[1] += n[1];
dst[2] += n[2];
}
}
}
}
}
} // end namespace OPENSUBDIV_VERSION
} // end namespace OpenSubdiv

View 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_CPU_SMOOTHNORMAL_CONTROLLER_H
#define OSD_CPU_SMOOTHNORMAL_CONTROLLER_H
#include "../version.h"
#include "../osd/nonCopyable.h"
#include "../osd/cpuSmoothNormalContext.h"
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
class OsdCpuSmoothNormalController {
public:
/// Constructor
OsdCpuSmoothNormalController();
/// Destructor
~OsdCpuSmoothNormalController();
/// 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