2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Copyright 2013 Pixar
|
2012-12-11 01:15:13 +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-12-11 01:15:13 +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-12-11 01:15:13 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// You may obtain a copy of the Apache License at
|
2012-12-11 01:15:13 +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-12-11 01:15:13 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "../osd/ompKernel.h"
|
|
|
|
#include "../osd/vertexDescriptor.h"
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
#include <cassert>
|
2014-05-09 00:20:54 +00:00
|
|
|
#include <cstdlib>
|
2012-12-11 01:15:13 +00:00
|
|
|
#include <omp.h>
|
2014-10-23 19:07:12 +00:00
|
|
|
#include <vector>
|
2012-12-11 01:15:13 +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 *
|
|
|
|
elementAtIndex(T * src, int index, VertexBufferDescriptor 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
|
2014-09-05 22:07:46 +00:00
|
|
|
clear(float *dst, VertexBufferDescriptor const &desc) {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
assert(dst);
|
|
|
|
memset(dst, 0, desc.length*sizeof(float));
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
static inline void
|
|
|
|
addWithWeight(float *dst, const float *src, int srcIndex, float weight,
|
|
|
|
VertexBufferDescriptor const &desc) {
|
2014-05-09 00:20:54 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
assert(src and dst);
|
|
|
|
src = elementAtIndex(src, srcIndex, desc);
|
|
|
|
for (int k = 0; k < desc.length; ++k) {
|
|
|
|
dst[k] += src[k] * weight;
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
static inline void
|
|
|
|
copy(float *dst, int dstIndex, const float *src,
|
|
|
|
VertexBufferDescriptor const &desc) {
|
2014-06-10 23:31:44 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
assert(src and dst);
|
2014-06-10 23:31:44 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
dst = elementAtIndex(dst, dstIndex, desc);
|
|
|
|
memcpy(dst, src, desc.length*sizeof(float));
|
2014-06-10 23:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
// XXXX manuelk this should be optimized further by using SIMD - considering
|
|
|
|
// OMP is somewhat obsolete - this is probably not worth it.
|
|
|
|
void
|
2015-05-07 23:11:00 +00:00
|
|
|
OmpComputeStencils(float const * src,
|
|
|
|
VertexBufferDescriptor const &srcDesc,
|
|
|
|
float * dst,
|
|
|
|
VertexBufferDescriptor const &dstDesc,
|
|
|
|
unsigned char const * sizes,
|
|
|
|
int const * offsets,
|
|
|
|
int const * indices,
|
|
|
|
float const * weights,
|
|
|
|
int start, int end) {
|
|
|
|
|
|
|
|
if (start > 0) {
|
|
|
|
sizes += start;
|
|
|
|
indices += offsets[start];
|
|
|
|
weights += offsets[start];
|
|
|
|
}
|
|
|
|
src += srcDesc.offset;
|
|
|
|
dst += dstDesc.offset;
|
2014-06-10 23:31:44 +00:00
|
|
|
|
2015-05-07 23:11:00 +00:00
|
|
|
int numThreads = omp_get_max_threads();
|
|
|
|
int n = end - start;
|
2014-06-10 23:31:44 +00:00
|
|
|
|
2015-05-07 23:11:00 +00:00
|
|
|
float * result = (float*)alloca(srcDesc.length * numThreads * sizeof(float));
|
2014-06-10 23:31:44 +00:00
|
|
|
|
|
|
|
#pragma omp parallel for
|
2015-05-07 23:11:00 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2014-06-10 23:31:44 +00:00
|
|
|
|
2015-05-07 23:11:00 +00:00
|
|
|
int index = i + (start > 0 ? start : 0); // Stencil index
|
2014-06-10 23:31:44 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
// Get thread-local pointers
|
|
|
|
int const * threadIndices = indices + offsets[index];
|
|
|
|
float const * threadWeights = weights + offsets[index];
|
2014-06-10 23:31:44 +00:00
|
|
|
|
|
|
|
int threadId = omp_get_thread_num();
|
|
|
|
|
2015-05-07 23:11:00 +00:00
|
|
|
float * threadResult = result + threadId*srcDesc.length;
|
2014-06-10 23:31:44 +00:00
|
|
|
|
2015-05-07 23:11:00 +00:00
|
|
|
clear(threadResult, dstDesc);
|
2014-06-10 23:31:44 +00:00
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
for (int j=0; j<(int)sizes[index]; ++j) {
|
2015-05-07 23:11:00 +00:00
|
|
|
addWithWeight(threadResult, src,
|
|
|
|
threadIndices[j], threadWeights[j], srcDesc);
|
2014-06-10 23:31:44 +00:00
|
|
|
}
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2015-05-07 23:11:00 +00:00
|
|
|
copy(dst, i, threadResult, dstDesc);
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
2014-05-09 00:20:54 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
} // end namespace Osd
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
} // end namespace OPENSUBDIV_VERSION
|
|
|
|
} // end namespace OpenSubdiv
|