195 const int motion_step)
200 const int attr_step = motion_step + 1;
202 const typename T::Type *subd_data = subd_attr.
data<
typename T::Type>(attr_step);
203 typename T::Type *mesh_data = mesh_attr.
data_for_write<
typename T::Type>(attr_step);
205 assert(mesh_data !=
nullptr);
207 attr.
interp = [
this, subd_data, mesh_data](
const int ,
208 const int face_index,
210 const int *vert_index,
212 const int vert_num) {
214 const int *subd_face_corners =
mesh.get_subd_face_corners().data();
220 const typename T::AccumType value0 = T::read(
222 const typename T::AccumType value1 = T::read(
224 const typename T::AccumType value2 = T::read(
226 const typename T::AccumType value3 = T::read(
229 for (
int i = 0; i < vert_num; i++) {
230 const float2 uv = vert_uv[i];
231 const typename T::AccumType value =
interp(
233 mesh_data[vert_index[i]] = T::output(value);
240 typename T::AccumType value_center = T::read(
243 value_center += T::read(subd_data[subd_face_corners[face.
start_corner + j]]);
248 const typename T::AccumType value_corner = T::read(
249 subd_data[subd_face_corners[face.
start_corner + corner]]);
250 const typename T::AccumType value_prev =
251 0.5f * (value_corner +
254 const typename T::AccumType value_next =
255 0.5f * (value_corner +
259 for (
int i = 0; i < vert_num; i++) {
260 const float2 uv = vert_uv[i];
263 const typename T::AccumType value =
interp(
264 interp(value_corner, value_next, uv.
x),
interp(value_prev, value_center, uv.
x), uv.
y);
266 mesh_data[vert_index[i]] = T::output(value);
274#ifdef WITH_OPENSUBDIV
276void SubdAttributeInterpolation::setup_attribute_vertex_smooth(
const Attribute &subd_attr,
278 const int motion_step)
284 const int num_refiner_verts = osd_data.refiner->GetNumVerticesTotal();
285 const int num_local_points = osd_data.patch_table->GetNumLocalPoints();
289 attr.
refined_data.resize((num_refiner_verts + num_local_points) *
sizeof(
typename T::AccumType));
290 typename T::AccumType *subd_data =
reinterpret_cast<typename T::AccumType *
>(
293 const int attr_step = motion_step + 1;
295 const typename T::Type *base_src = subd_attr.
data<
typename T::Type>(attr_step);
296 typename T::AccumType *base_dst = subd_data;
297 for (
int i = 0; i < num_base_verts; i++) {
298 base_dst[i] = T::read(base_src[i]);
301 Far::PrimvarRefiner primvar_refiner(*osd_data.refiner);
302 typename T::AccumType *src = subd_data;
303 for (
int i = 0; i < osd_data.refiner->GetMaxLevel(); i++) {
304 typename T::AccumType *dest = src + osd_data.refiner->GetLevel(i).GetNumVertices();
305 primvar_refiner.Interpolate(
306 i + 1, (OsdValue<typename T::AccumType> *)src, (OsdValue<typename T::AccumType> *&)dest);
310 if (num_local_points) {
311 osd_data.patch_table->ComputeLocalPointValues(
312 (OsdValue<typename T::AccumType> *)subd_data,
313 (OsdValue<typename T::AccumType> *)(subd_data + num_refiner_verts));
318 typename T::Type *mesh_data = mesh_attr.
data_for_write<
typename T::Type>(attr_step);
319 assert(mesh_data !=
nullptr);
322 packed_normal *mesh_normal_data =
nullptr;
323 if constexpr (std::is_same_v<typename T::AccumType, float3>) {
328 mesh_normal_data = attr_normal->
data_for_write<packed_normal>(attr_step);
333 attr.
interp = [
this, subd_data, mesh_data, mesh_normal_data](
const int patch_index,
336 const int *vert_index,
338 const int vert_num) {
339 for (
int i = 0; i < vert_num; i++) {
341 const float2 uv = vert_uv[i];
343 const Far::PatchTable::PatchHandle &handle = *osd_data.patch_map->FindPatch(
344 patch_index, (
double)uv.
x, (
double)uv.
y);
346 float p_weights[20], du_weights[20], dv_weights[20];
347 osd_data.patch_table->EvaluateBasis(handle, uv.
x, uv.
y, p_weights, du_weights, dv_weights);
348 Far::ConstIndexArray cv = osd_data.patch_table->GetPatchVertices(handle);
351 typename T::AccumType value = subd_data[cv[0]] * p_weights[0];
352 for (
int k = 1; k < cv.size(); k++) {
353 value += subd_data[cv[k]] * p_weights[k];
355 mesh_data[vert_index[i]] = T::output(value);
358 if (mesh_normal_data) {
359 if constexpr (std::is_same_v<typename T::AccumType, float3>) {
362 for (
int k = 0; k < cv.size(); k++) {
363 const float3 p = subd_data[cv[k]];
364 du += p * du_weights[k];
365 dv += p * dv_weights[k];
382 const int motion_step)
387 const int attr_step = motion_step + 1;
389 const typename T::Type *subd_data = subd_attr.
data<
typename T::Type>(attr_step);
390 typename T::Type *mesh_data = mesh_attr.
data_for_write<
typename T::Type>(attr_step);
392 assert(mesh_data !=
nullptr);
394 attr.
interp = [
this, subd_data, mesh_data](
const int ,
395 const int face_index,
397 const int *triangle_index,
398 const float2 *triangle_uv,
399 const int triangle_num) {
404 const typename T::AccumType value0 = T::read(subd_data[face.
start_corner + 0]);
405 const typename T::AccumType value1 = T::read(subd_data[face.
start_corner + 1]);
406 const typename T::AccumType value2 = T::read(subd_data[face.
start_corner + 2]);
407 const typename T::AccumType value3 = T::read(subd_data[face.
start_corner + 3]);
409 for (
size_t i = 0; i < triangle_num; i++) {
410 for (
int j = 0; j < 3; j++) {
411 const float2 uv = triangle_uv[(i * 3) + j];
412 const typename T::AccumType value =
interp(
414 mesh_data[triangle_index[i] * 3 + j] = T::output(value);
422 typename T::AccumType value_center = T::read(subd_data[face.
start_corner]);
424 value_center += T::read(subd_data[face.
start_corner + j]);
429 const typename T::AccumType value_corner = T::read(subd_data[face.
start_corner + corner]);
430 const typename T::AccumType value_prev =
431 0.5f * (value_corner +
433 const typename T::AccumType value_next =
434 0.5f * (value_corner +
437 for (
size_t i = 0; i < triangle_num; i++) {
438 for (
int j = 0; j < 3; j++) {
439 const float2 uv = triangle_uv[(i * 3) + j];
442 const typename T::AccumType value =
interp(
interp(value_corner, value_next, uv.
x),
443 interp(value_prev, value_center, uv.
x),
446 mesh_data[triangle_index[i] * 3 + j] = T::output(value);
455#ifdef WITH_OPENSUBDIV
457void SubdAttributeInterpolation::setup_attribute_corner_smooth(
Attribute &mesh_attr,
464 const int num_refiner_fvars = osd_data.refiner->GetNumFVarValuesTotal(channel);
465 const int num_local_points = osd_data.patch_table->GetNumLocalPointsFaceVarying(channel);
466 const int num_base_fvars = osd_data.refiner->GetLevel(0).GetNumFVarValues(channel);
469 attr.
refined_data.resize((num_refiner_fvars + num_local_points) *
sizeof(
typename T::AccumType));
470 typename T::AccumType *refined_data =
reinterpret_cast<typename T::AccumType *
>(
473 const typename T::Type *base_src =
reinterpret_cast<const typename T::Type *
>(
474 merged_values.data());
475 typename T::AccumType *base_dst = refined_data;
476 for (
int i = 0; i < num_base_fvars; i++) {
477 base_dst[i] = T::read(base_src[i]);
480 Far::PrimvarRefiner primvar_refiner(*osd_data.refiner);
481 typename T::AccumType *src = refined_data;
482 for (
int i = 0; i < osd_data.refiner->GetMaxLevel(); i++) {
483 typename T::AccumType *dest = src + osd_data.refiner->GetLevel(i).GetNumFVarValues(channel);
484 primvar_refiner.InterpolateFaceVarying(i + 1,
485 (OsdValue<typename T::AccumType> *)src,
486 (OsdValue<typename T::AccumType> *&)dest,
491 if (num_local_points) {
492 osd_data.patch_table->ComputeLocalPointValuesFaceVarying(
493 (OsdValue<typename T::AccumType> *)refined_data,
494 (OsdValue<typename T::AccumType> *)(refined_data + num_refiner_fvars),
499 const typename T::AccumType *subd_data = refined_data;
500 typename T::Type *mesh_data =
reinterpret_cast<typename T::Type *
>(mesh_attr.
data_for_write());
502 assert(mesh_data !=
nullptr);
504 attr.
interp = [
this, subd_data, mesh_data, channel](
const int patch_index,
507 const int *triangle_index,
508 const float2 *triangle_uv,
509 const int triangle_num) {
510 for (
size_t i = 0; i < triangle_num; i++) {
511 for (
int j = 0; j < 3; j++) {
513 const float2 uv = triangle_uv[(i * 3) + j];
514 const Far::PatchTable::PatchHandle &handle = *osd_data.patch_map->FindPatch(
515 patch_index, (
double)uv.
x, (
double)uv.
y);
517 float p_weights[20], du_weights[20], dv_weights[20];
518 osd_data.patch_table->EvaluateBasisFaceVarying(handle,
528 Far::ConstIndexArray cv = osd_data.patch_table->GetPatchFVarValues(handle, channel);
531 typename T::AccumType value = subd_data[cv[0]] * p_weights[0];
532 for (
int k = 1; k < cv.size(); k++) {
533 value += subd_data[cv[k]] * p_weights[k];
535 mesh_data[triangle_index[i] * 3 + j] = T::output(value);
550 const typename T::Type *subd_data =
reinterpret_cast<const typename T::Type *
>(subd_attr.
data());
551 typename T::Type *mesh_data =
reinterpret_cast<typename T::Type *
>(mesh_attr.
data_for_write());
553 assert(mesh_data !=
nullptr);
555 attr.
interp = [subd_data, mesh_data](
const int ,
556 const int face_index,
558 const int *triangle_index,
560 const int triangle_num) {
561 for (
int i = 0; i < triangle_num; i++) {
562 mesh_data[triangle_index[i]] = subd_data[face_index];
586#ifdef WITH_OPENSUBDIV
589 switch (subd_attr.
std) {
593 setup_attribute_vertex_smooth<T>(subd_attr, mesh_attr);
609#ifdef WITH_OPENSUBDIV
611 setup_attribute_vertex_smooth<T>(subd_attr, mesh_attr,
step);
625#ifdef WITH_OPENSUBDIV
626 if (osd_mesh.use_smooth_fvar(subd_attr)) {
627 for (
const auto &merged_fvar : osd_mesh.merged_fvars) {
628 if (&merged_fvar.attr == &subd_attr) {
629 if constexpr (std::is_same_v<typename T::Type, float2>) {
630 setup_attribute_corner_smooth<T>(mesh_attr, merged_fvar.channel, merged_fvar.values);
Attribute & copy(const Attribute &attr)
list< Attribute > attributes
Attribute * find(ustring name) const
const T * data(const int step=0) const
T * data_for_write(const int step=0)
void add_motion(const Geometry *geom)
size_t data_sizeof() const
static bool same_storage(const TypeDesc a, const TypeDesc b)
size_t get_num_subd_faces() const
size_t get_num_subd_base_verts() const
AttributeSet subd_attributes
SubdFace get_subd_face(const size_t index) const
@ SUBDIVISION_CATMULL_CLARK
void setup_attribute_face(const Attribute &subd_attr, Attribute &mesh_attr)
SubdAttributeInterpolation(Mesh &mesh)
void setup_attribute_type(const Attribute &subd_attr, Attribute &mesh_attr)
void setup_attribute_corner_linear(const Attribute &subd_attr, Attribute &mesh_attr, const int motion_step=-1)
void setup_attribute(const Attribute &subd_attr, Attribute &mesh_attr)
vector< SubdAttribute > triangle_attributes
bool support_interp_attribute(const Attribute &attr) const
vector< SubdAttribute > vertex_attributes
void setup_attribute_vertex_linear(const Attribute &subd_attr, Attribute &mesh_attr, const int motion_step=-1)
ccl_device_inline float4 color_uchar4_to_float4(const uchar4 c)
ccl_device uchar4 color_float4_to_uchar4(const float4 c)
#define CCL_NAMESPACE_END
#define assert(assertion)
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
VecBase< float, 2 > float2
VecBase< float, 3 > float3
@ ATTR_ELEMENT_VERTEX_NORMAL
@ ATTR_ELEMENT_CORNER_BYTE
@ ATTR_ELEMENT_CORNER_NORMAL
@ ATTR_STD_POSITION_UNDISPLACED
@ ATTR_STD_POSITION_UNDEFORMED
ccl_device_inline float interp(const float a, const float b, const float t)
ccl_device_inline float3 safe_normalize_fallback(const float3 a, const float3 fallback)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
std::function< void(const int, const int, const int, const int *, const float2 *, const int)> interp
vector< char > refined_data
static AccumType read(const Type &value)
static Type output(const AccumType &value)
static Type read(const Type &value)
static Type output(const Type &value)
static Type output(const AccumType &value)
static AccumType read(const Type &value)
static Type output(const AccumType &value)
static AccumType read(const Type &value)
static AccumType read(const Type &value)
static Type output(const AccumType &value)
ccl_device_inline_method float3 decode() const
ccl_device_inline Quaternion make_quaternion(const float w, const float x, const float y, const float z)