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
|
|
|
//
|
|
|
|
|
|
|
|
#include "../osd/cpuKernel.h"
|
2015-05-29 16:21:14 +00:00
|
|
|
#include "../osd/bufferDescriptor.h"
|
2012-06-09 00:06:35 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
#include <cassert>
|
2014-05-09 00:20:54 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
2014-09-05 22:07:46 +00:00
|
|
|
#include <vector>
|
2014-05-09 00:20:54 +00:00
|
|
|
|
2012-06-09 00:06:35 +00:00
|
|
|
namespace OpenSubdiv {
|
|
|
|
namespace OPENSUBDIV_VERSION {
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
namespace Osd {
|
2014-05-09 00:20:54 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
template <class T> T *
|
2015-05-29 16:21:14 +00:00
|
|
|
elementAtIndex(T * src, int index, BufferDescriptor const &desc) {
|
2014-05-09 00:20:54 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
return src + index * desc.stride;
|
2014-05-09 00:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2015-05-29 16:21:14 +00:00
|
|
|
clear(float *dst, BufferDescriptor const &desc) {
|
2014-05-09 00:20:54 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
assert(dst);
|
|
|
|
memset(dst, 0, desc.length*sizeof(float));
|
2014-05-13 23:06:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
static inline void
|
|
|
|
addWithWeight(float *dst, const float *src, int srcIndex, float weight,
|
2015-05-29 16:21:14 +00:00
|
|
|
BufferDescriptor const &desc) {
|
2014-05-13 23:06:58 +00:00
|
|
|
|
2016-02-19 09:02:07 +00:00
|
|
|
assert(src && dst);
|
2014-09-05 22:07:46 +00:00
|
|
|
src = elementAtIndex(src, srcIndex, desc);
|
|
|
|
for (int k = 0; k < desc.length; ++k) {
|
|
|
|
dst[k] += src[k] * weight;
|
2014-05-13 23:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
static inline void
|
2015-05-29 16:21:14 +00:00
|
|
|
copy(float *dst, int dstIndex, const float *src, BufferDescriptor const &desc) {
|
2012-06-09 00:06:35 +00:00
|
|
|
|
2016-02-19 09:02:07 +00:00
|
|
|
assert(src && dst);
|
2014-05-09 00:20:54 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
dst = elementAtIndex(dst, dstIndex, desc);
|
|
|
|
memcpy(dst, src, desc.length*sizeof(float));
|
2012-06-09 00:06:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
void
|
2015-05-29 16:21:14 +00:00
|
|
|
CpuEvalStencils(float const * src, BufferDescriptor const &srcDesc,
|
|
|
|
float * dst, BufferDescriptor const &dstDesc,
|
2015-05-19 17:16:56 +00:00
|
|
|
int const * sizes,
|
2015-05-09 00:31:26 +00:00
|
|
|
int const * offsets,
|
|
|
|
int const * indices,
|
|
|
|
float const * weights,
|
|
|
|
int start, int end) {
|
2014-05-27 22:25:54 +00:00
|
|
|
|
2016-02-19 09:02:07 +00:00
|
|
|
assert(start>=0 && start<end);
|
2014-05-27 22:25:54 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
if (start>0) {
|
|
|
|
sizes += start;
|
|
|
|
indices += offsets[start];
|
|
|
|
weights += offsets[start];
|
2014-05-27 22:25:54 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 23:11:00 +00:00
|
|
|
src += srcDesc.offset;
|
|
|
|
dst += dstDesc.offset;
|
|
|
|
|
2016-02-19 09:02:07 +00:00
|
|
|
if (srcDesc.length == 4 && dstDesc.length == 4 &&
|
|
|
|
srcDesc.stride == 4 && dstDesc.stride == 4) {
|
2013-08-14 23:41:35 +00:00
|
|
|
|
2015-05-19 17:16:56 +00:00
|
|
|
// SIMD fast path for aligned primvar data (4 floats)
|
2015-05-07 23:11:00 +00:00
|
|
|
ComputeStencilKernel<4>(src, dst,
|
2014-09-05 22:07:46 +00:00
|
|
|
sizes, indices, weights, start, end);
|
2013-08-14 23:41:35 +00:00
|
|
|
|
2016-02-19 09:02:07 +00:00
|
|
|
} else if (srcDesc.length == 8 && dstDesc.length == 8 &&
|
|
|
|
srcDesc.stride == 8 && dstDesc.stride == 8) {
|
2012-06-09 00:06:35 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
// SIMD fast path for aligned primvar data (8 floats)
|
2015-05-07 23:11:00 +00:00
|
|
|
ComputeStencilKernel<8>(src, dst,
|
2014-09-05 22:07:46 +00:00
|
|
|
sizes, indices, weights, start, end);
|
2015-05-07 23:11:00 +00:00
|
|
|
} else {
|
2014-05-09 00:20:54 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
// Slow path for non-aligned data
|
2015-05-07 23:11:00 +00:00
|
|
|
|
|
|
|
float * result = (float*)alloca(srcDesc.length * sizeof(float));
|
2013-08-14 23:41:35 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
int nstencils = end-start;
|
|
|
|
for (int i=0; i<nstencils; ++i, ++sizes) {
|
2013-08-14 23:41:35 +00:00
|
|
|
|
2015-05-07 23:11:00 +00:00
|
|
|
clear(result, srcDesc);
|
2012-06-09 18:12:34 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
for (int j=0; j<*sizes; ++j) {
|
2015-05-07 23:11:00 +00:00
|
|
|
addWithWeight(result, src, *indices++, *weights++, srcDesc);
|
2013-08-14 23:41:35 +00:00
|
|
|
}
|
2014-05-09 00:20:54 +00:00
|
|
|
|
2015-05-07 23:11:00 +00:00
|
|
|
copy(dst, i, result, dstDesc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-29 16:21:14 +00:00
|
|
|
CpuEvalStencils(float const * src, BufferDescriptor const &srcDesc,
|
|
|
|
float * dst, BufferDescriptor const &dstDesc,
|
|
|
|
float * dstDu, BufferDescriptor const &dstDuDesc,
|
|
|
|
float * dstDv, BufferDescriptor const &dstDvDesc,
|
2015-05-19 17:16:56 +00:00
|
|
|
int const * sizes,
|
2015-05-09 00:31:26 +00:00
|
|
|
int const * offsets,
|
|
|
|
int const * indices,
|
|
|
|
float const * weights,
|
|
|
|
float const * duWeights,
|
|
|
|
float const * dvWeights,
|
|
|
|
int start, int end) {
|
2015-05-07 23:11:00 +00:00
|
|
|
if (start > 0) {
|
|
|
|
sizes += start;
|
|
|
|
indices += offsets[start];
|
|
|
|
weights += offsets[start];
|
|
|
|
duWeights += offsets[start];
|
|
|
|
dvWeights += offsets[start];
|
|
|
|
}
|
|
|
|
|
|
|
|
src += srcDesc.offset;
|
|
|
|
dst += dstDesc.offset;
|
|
|
|
dstDu += dstDuDesc.offset;
|
|
|
|
dstDv += dstDvDesc.offset;
|
|
|
|
|
|
|
|
int nOutLength = dstDesc.length + dstDuDesc.length + dstDvDesc.length;
|
|
|
|
float * result = (float*)alloca(nOutLength * sizeof(float));
|
|
|
|
float * resultDu = result + dstDesc.length;
|
|
|
|
float * resultDv = resultDu + dstDuDesc.length;
|
|
|
|
|
|
|
|
int nStencils = end - start;
|
|
|
|
for (int i = 0; i < nStencils; ++i, ++sizes) {
|
|
|
|
|
|
|
|
// clear
|
|
|
|
memset(result, 0, nOutLength * sizeof(float));
|
|
|
|
|
|
|
|
for (int j=0; j<*sizes; ++j) {
|
|
|
|
addWithWeight(result, src, *indices, *weights++, srcDesc);
|
|
|
|
addWithWeight(resultDu, src, *indices, *duWeights++, srcDesc);
|
|
|
|
addWithWeight(resultDv, src, *indices, *dvWeights++, srcDesc);
|
|
|
|
++indices;
|
2012-06-09 00:06:35 +00:00
|
|
|
}
|
2015-05-07 23:11:00 +00:00
|
|
|
copy(dst, i, result, dstDesc);
|
|
|
|
copy(dstDu, i, resultDu, dstDuDesc);
|
|
|
|
copy(dstDv, i, resultDv, dstDvDesc);
|
2014-05-09 00:20:54 +00:00
|
|
|
}
|
2012-06-09 00:06:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 22:36:30 +00:00
|
|
|
void
|
|
|
|
CpuEvalStencils(float const * src, BufferDescriptor const &srcDesc,
|
|
|
|
float * dst, BufferDescriptor const &dstDesc,
|
|
|
|
float * dstDu, BufferDescriptor const &dstDuDesc,
|
|
|
|
float * dstDv, BufferDescriptor const &dstDvDesc,
|
|
|
|
float * dstDuu, BufferDescriptor const &dstDuuDesc,
|
|
|
|
float * dstDuv, BufferDescriptor const &dstDuvDesc,
|
|
|
|
float * dstDvv, BufferDescriptor const &dstDvvDesc,
|
|
|
|
int const * sizes,
|
|
|
|
int const * offsets,
|
|
|
|
int const * indices,
|
|
|
|
float const * weights,
|
|
|
|
float const * duWeights,
|
|
|
|
float const * dvWeights,
|
|
|
|
float const * duuWeights,
|
|
|
|
float const * duvWeights,
|
|
|
|
float const * dvvWeights,
|
|
|
|
int start, int end) {
|
|
|
|
if (start > 0) {
|
|
|
|
sizes += start;
|
|
|
|
indices += offsets[start];
|
|
|
|
weights += offsets[start];
|
|
|
|
duWeights += offsets[start];
|
|
|
|
dvWeights += offsets[start];
|
|
|
|
duuWeights += offsets[start];
|
|
|
|
duvWeights += offsets[start];
|
|
|
|
dvvWeights += offsets[start];
|
|
|
|
}
|
|
|
|
|
|
|
|
src += srcDesc.offset;
|
|
|
|
dst += dstDesc.offset;
|
|
|
|
dstDu += dstDuDesc.offset;
|
|
|
|
dstDv += dstDvDesc.offset;
|
|
|
|
dstDuu += dstDuuDesc.offset;
|
|
|
|
dstDuv += dstDuvDesc.offset;
|
|
|
|
dstDvv += dstDvvDesc.offset;
|
|
|
|
|
|
|
|
int nOutLength = dstDesc.length + dstDuDesc.length + dstDvDesc.length
|
|
|
|
+ dstDuuDesc.length + dstDuvDesc.length + dstDvvDesc.length;
|
|
|
|
float * result = (float*)alloca(nOutLength * sizeof(float));
|
|
|
|
float * resultDu = result + dstDesc.length;
|
|
|
|
float * resultDv = resultDu + dstDuDesc.length;
|
2017-01-30 22:10:38 +00:00
|
|
|
float * resultDuu = resultDv + dstDvDesc.length;
|
|
|
|
float * resultDuv = resultDuu + dstDuuDesc.length;
|
|
|
|
float * resultDvv = resultDuv + dstDuvDesc.length;
|
2017-01-26 22:36:30 +00:00
|
|
|
|
|
|
|
int nStencils = end - start;
|
|
|
|
for (int i = 0; i < nStencils; ++i, ++sizes) {
|
|
|
|
|
|
|
|
// clear
|
|
|
|
memset(result, 0, nOutLength * sizeof(float));
|
|
|
|
|
|
|
|
for (int j=0; j<*sizes; ++j) {
|
|
|
|
addWithWeight(result, src, *indices, *weights++, srcDesc);
|
|
|
|
addWithWeight(resultDu, src, *indices, *duWeights++, srcDesc);
|
|
|
|
addWithWeight(resultDv, src, *indices, *dvWeights++, srcDesc);
|
|
|
|
addWithWeight(resultDuu, src, *indices, *duuWeights++, srcDesc);
|
|
|
|
addWithWeight(resultDuv, src, *indices, *duvWeights++, srcDesc);
|
|
|
|
addWithWeight(resultDvv, src, *indices, *dvvWeights++, srcDesc);
|
|
|
|
++indices;
|
|
|
|
}
|
|
|
|
copy(dst, i, result, dstDesc);
|
|
|
|
copy(dstDu, i, resultDu, dstDuDesc);
|
|
|
|
copy(dstDv, i, resultDv, dstDvDesc);
|
|
|
|
copy(dstDuu, i, resultDuu, dstDuuDesc);
|
2017-01-30 22:10:38 +00:00
|
|
|
copy(dstDuv, i, resultDuv, dstDuvDesc);
|
2017-01-26 22:36:30 +00:00
|
|
|
copy(dstDvv, i, resultDvv, dstDvvDesc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
} // end namespace Osd
|
2012-09-18 01:41:48 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
} // end namespace OPENSUBDIV_VERSION
|
|
|
|
} // end namespace OpenSubdiv
|