11#include <pxr/base/gf/vec2f.h>
12#include <pxr/imaging/hd/geomSubsetSchema.h>
13#include <pxr/imaging/hd/legacyDisplayStyleSchema.h>
14#include <pxr/imaging/hd/materialBindingSchema.h>
15#include <pxr/imaging/hd/materialBindingsSchema.h>
16#include <pxr/imaging/hd/meshSchema.h>
17#include <pxr/imaging/hd/meshTopologySchema.h>
18#include <pxr/imaging/hd/sceneIndex.h>
19#include <pxr/imaging/hd/subdivisionTagsSchema.h>
29 output.reserve(primitiveParams.size());
30 const T &input = value.Get<
T>();
32 for (
size_t i = 0; i < primitiveParams.size(); ++i) {
33 const int faceIndex = HdMeshUtil::DecodeFaceIndexFromCoarseFaceParam(primitiveParams[i]);
35 output.push_back(input[faceIndex]);
42 const HdType valueType,
43 const VtIntArray &primitiveParams)
55 TF_RUNTIME_ERROR(
"Unsupported attribute type %d",
static_cast<int>(valueType));
61 const HdType valueType,
64 if (meshUtil.ComputeTriangulatedFaceVaryingPrimvar(
65 HdGetValueData(value), value.GetArraySize(), valueType, &value)
66#
if PXR_VERSION >= 2511
67 != HdMeshComputationResult::Error
105 bits |= HdChangeTracker::DirtyPoints | HdChangeTracker::DirtyNormals |
106 HdChangeTracker::DirtyPrimvar | HdChangeTracker::DirtyTopology |
107 HdChangeTracker::DirtyDisplayStyle | HdChangeTracker::DirtySubdivTags;
111HdDirtyBits HdCyclesMesh::_PropagateDirtyBits(HdDirtyBits bits)
const
113 if (bits & (HdChangeTracker::DirtyMaterialId)) {
115 bits |= HdChangeTracker::DirtyTopology;
118 if (bits & (HdChangeTracker::DirtyTopology | HdChangeTracker::DirtyDisplayStyle |
119 HdChangeTracker::DirtySubdivTags))
122 bits |= HdChangeTracker::DirtyTopology | HdChangeTracker::DirtyDisplayStyle |
123 HdChangeTracker::DirtySubdivTags;
126 if (bits & (HdChangeTracker::DirtyTopology)) {
128 bits |= HdChangeTracker::DirtyPoints | HdChangeTracker::DirtyNormals |
129 HdChangeTracker::DirtyPrimvar;
135void HdCyclesMesh::Populate(HdSceneDelegate *sceneDelegate, HdDirtyBits dirtyBits,
bool &rebuild)
137 if (HdChangeTracker::IsTopologyDirty(dirtyBits, GetId())) {
138 PopulateTopology(sceneDelegate);
141 if (dirtyBits & HdChangeTracker::DirtyPoints) {
142 PopulatePoints(sceneDelegate);
146 if (dirtyBits & HdChangeTracker::DirtyNormals) {
147 PopulateNormals(sceneDelegate);
151 if (dirtyBits & HdChangeTracker::DirtyPrimvar) {
152 PopulatePrimvars(sceneDelegate);
155 rebuild = (
_geom->triangles_is_modified()) || (
_geom->subd_start_corner_is_modified()) ||
156 (
_geom->subd_num_corners_is_modified()) || (
_geom->subd_shader_is_modified()) ||
157 (
_geom->subd_smooth_is_modified()) || (
_geom->subd_ptex_offset_is_modified()) ||
158 (
_geom->subd_face_corners_is_modified());
161void HdCyclesMesh::PopulatePoints(HdSceneDelegate *sceneDelegate)
163 const HdSceneIndexPrim prim =
GetPrim(sceneDelegate, GetId());
164 const HdPrimvarsSchema primvars = HdPrimvarsSchema::GetFromParent(prim.dataSource);
165 const VtValue value =
ReadPrimvar(primvars, HdTokens->points);
167 if (!value.IsHolding<VtVec3fArray>()) {
168 TF_WARN(
"Invalid points data for %s", GetId().GetText());
172 const auto &points = value.UncheckedGet<VtVec3fArray>();
174 TF_VERIFY(points.size() >=
static_cast<size_t>(_topology.GetNumPoints()));
177 AttributeSet &attributes = (subdivision) ?
_geom->subd_attributes :
_geom->attributes;
182 reinterpret_cast<const packed_float3 *
>(points.data()), _topology.GetNumPoints(), verts);
183 _geom->tag_position_modified();
186void HdCyclesMesh::PopulateNormals(HdSceneDelegate *sceneDelegate)
195 const HdSceneIndexPrim prim =
GetPrim(sceneDelegate, GetId());
196 const HdPrimvarsSchema primvars = HdPrimvarsSchema::GetFromParent(prim.dataSource);
198 if (interpolation == HdInterpolationCount) {
201 const VtValue value =
ReadPrimvar(primvars, HdTokens->normals);
203 if (!value.IsHolding<VtVec3fArray>()) {
204 TF_WARN(
"Invalid normals data for %s", GetId().GetText());
208 const auto &normals = value.UncheckedGet<VtVec3fArray>();
210 if (interpolation == HdInterpolationConstant) {
211 TF_VERIFY(normals.size() == 1);
213 const GfVec3f constantNormal = normals[0];
215 packed_normal *
const N =
217 for (
size_t i = 0; i <
_geom->num_verts(); ++i) {
221 else if (interpolation == HdInterpolationUniform) {
222 TF_VERIFY(normals.size() ==
static_cast<size_t>(_topology.GetNumFaces()));
225 else if (interpolation == HdInterpolationVertex || interpolation == HdInterpolationVarying) {
226 TF_VERIFY(normals.size() ==
static_cast<size_t>(_topology.GetNumPoints()) &&
227 static_cast<size_t>(_topology.GetNumPoints()) ==
_geom->num_verts());
229 packed_normal *
const N =
231 for (
size_t i = 0; i <
_geom->num_verts(); ++i) {
235 else if (interpolation == HdInterpolationFaceVarying) {
236 TF_VERIFY(normals.size() ==
static_cast<size_t>(_topology.GetNumFaceVaryings()));
240 if (!_util.ComputeTriangulatedFaceVaryingPrimvar(
241 normals.data(), normals.size(), HdTypeFloatVec3, &value))
246 const auto &normalsTriangulated = value.UncheckedGet<VtVec3fArray>();
251void HdCyclesMesh::PopulatePrimvars(HdSceneDelegate *sceneDelegate)
256 AttributeSet &attributes = subdivision ?
_geom->subd_attributes :
_geom->attributes;
258 const HdSceneIndexPrim prim =
GetPrim(sceneDelegate, GetId());
259 const HdPrimvarsSchema primvars = HdPrimvarsSchema::GetFromParent(prim.dataSource);
261 const std::pair<HdInterpolation, AttributeElement> interpolations[] = {
269 for (
const auto &interpolation : interpolations) {
272 if (primvarName == HdTokens->points || primvarName == HdTokens->normals) {
276 VtValue value =
ReadPrimvar(primvars, primvarName);
277 if (value.IsEmpty()) {
282 const ustring name(primvarName.GetString());
285 if (role == HdPrimvarRoleTokens->textureCoordinate) {
288 else if (interpolation.first == HdInterpolationVertex) {
289 if (primvarName == HdTokens->displayColor || role == HdPrimvarRoleTokens->color) {
292 else if (primvarName == HdTokens->normals) {
296 else if (primvarName == HdTokens->displayColor &&
297 interpolation.first == HdInterpolationConstant)
299 if (value.IsHolding<VtVec3fArray>() && value.GetArraySize() == 1) {
300 const GfVec3f color = value.UncheckedGet<VtVec3fArray>()[0];
307 _geom->need_attribute(scene, name))
309 const HdType valueType = HdGetValueTupleType(value).type;
313 if (interpolation.first == HdInterpolationUniform) {
315 if (value.IsEmpty()) {
319 else if (interpolation.first == HdInterpolationFaceVarying) {
321 if (value.IsEmpty()) {
327 ApplyPrimvars(attributes, name, value, interpolation.second, std);
333void HdCyclesMesh::PopulateTopology(HdSceneDelegate *sceneDelegate)
338 const HdSceneIndexPrim prim =
GetPrim(sceneDelegate, GetId());
339 const HdLegacyDisplayStyleSchema displayStyleSchema = HdLegacyDisplayStyleSchema::GetFromParent(
343 if (
auto ds = displayStyleSchema.GetRefineLevel()) {
344 refineLevel = ds->GetTypedValue(0.0f);
346 bool flatShadingEnabled =
false;
347 if (
auto ds = displayStyleSchema.GetFlatShadingEnabled()) {
348 flatShadingEnabled = ds->GetTypedValue(0.0f);
351 const HdMeshSchema meshSchema = HdMeshSchema::GetFromParent(prim.dataSource);
352 const HdMeshTopologySchema topoSchema = meshSchema.GetTopology();
354 TfToken scheme = PxOsdOpenSubdivTokens->none;
355 if (
auto ds = meshSchema.GetSubdivisionScheme()) {
356 scheme = ds->GetTypedValue(0.0f);
358 TfToken orientation = HdTokens->rightHanded;
359 if (
auto ds = topoSchema.GetOrientation()) {
360 orientation = ds->GetTypedValue(0.0f);
362 VtIntArray faceVertexCounts;
363 if (
auto ds = topoSchema.GetFaceVertexCounts()) {
364 faceVertexCounts = ds->GetTypedValue(0.0f);
366 VtIntArray faceVertexIndices;
367 if (
auto ds = topoSchema.GetFaceVertexIndices()) {
368 faceVertexIndices = ds->GetTypedValue(0.0f);
370 VtIntArray holeIndices;
371 if (
auto ds = topoSchema.GetHoleIndices()) {
372 holeIndices = ds->GetTypedValue(0.0f);
375 _topology = HdMeshTopology(
376 scheme, orientation, faceVertexCounts, faceVertexIndices, holeIndices, refineLevel);
379 HdGeomSubsets geomSubsetsList;
380 if (HdSceneIndexBaseRefPtr si = sceneDelegate->GetRenderIndex().GetTerminalSceneIndex()) {
381 for (
const SdfPath &childPath : si->GetChildPrimPaths(GetId())) {
382 const HdSceneIndexPrim childPrim = si->GetPrim(childPath);
383 if (childPrim.primType != HdPrimTypeTokens->geomSubset) {
386 const HdGeomSubsetSchema subsetSchema = HdGeomSubsetSchema::GetFromParent(
387 childPrim.dataSource);
389 if (
auto typeDs = subsetSchema.GetType()) {
390 if (typeDs->GetTypedValue(0.0f) != HdGeomSubsetSchemaTokens->typeFaceSet) {
395 subset.type = HdGeomSubset::TypeFaceSet;
396 if (
auto ds = subsetSchema.GetIndices()) {
397 subset.indices = ds->GetTypedValue(0.0f);
399 if (
auto pathDs = HdMaterialBindingsSchema::GetFromParent(childPrim.dataSource)
400 .GetMaterialBinding()
403 subset.materialId = pathDs->GetTypedValue(0.0f);
405 geomSubsetsList.push_back(subset);
408 _topology.SetGeomSubsets(geomSubsetsList);
410 const TfToken subdivScheme = _topology.GetScheme();
411 if (subdivScheme == PxOsdOpenSubdivTokens->bilinear && _topology.GetRefineLevel() > 0) {
414 else if (subdivScheme == PxOsdOpenSubdivTokens->catmullClark && _topology.GetRefineLevel() > 0) {
421 const bool smooth = !flatShadingEnabled;
425 VtIntArray faceShaders(_topology.GetNumFaces(), 0);
427 const HdGeomSubsets &geomSubsets = _topology.GetGeomSubsets();
428 if (!geomSubsets.empty()) {
429 array<Node *> usedShaders = std::move(
_geom->get_used_shaders());
433 std::unordered_map<SdfPath, int, SdfPath::Hash> materials;
435 for (
const HdGeomSubset &geomSubset : geomSubsets) {
436 TF_VERIFY(geomSubset.type == HdGeomSubset::TypeFaceSet);
439 const auto it = materials.find(geomSubset.materialId);
440 if (it != materials.end()) {
444 const auto *
const material =
static_cast<const HdCyclesMaterial *
>(
445 sceneDelegate->GetRenderIndex().GetSprim(HdPrimTypeTokens->material,
446 geomSubset.materialId));
448 if (material && material->GetCyclesShader()) {
449 shader =
static_cast<int>(usedShaders.
size());
452 materials.emplace(geomSubset.materialId, shader);
456 for (
const int face : geomSubset.indices) {
457 faceShaders[face] = shader;
461 _geom->set_used_shaders(usedShaders);
464 const VtIntArray vertIndx = _topology.GetFaceVertexIndices();
465 const VtIntArray vertCounts = _topology.GetFaceVertexCounts();
468 VtVec3iArray triangles;
469 _util.ComputeTriangleIndices(&triangles, &_primitiveParams);
471 _geom->resize_mesh(_topology.GetNumPoints(), triangles.size());
473 int *geom_indices =
_geom->get_triangles().data();
474 for (
size_t i = 0; i < _primitiveParams.size(); ++i) {
475 const GfVec3i triangle = triangles[i];
476 geom_indices[i * 3 + 0] = triangle[0];
477 geom_indices[i * 3 + 1] = triangle[1];
478 geom_indices[i * 3 + 2] = triangle[2];
481 int *shader =
_geom->get_shader().data();
482 for (
size_t i = 0; i < _primitiveParams.size(); ++i) {
483 const int faceIndex = HdMeshUtil::DecodeFaceIndexFromCoarseFaceParam(_primitiveParams[i]);
484 shader[i] = faceShaders[faceIndex];
489 _geom->tag_triangles_modified();
490 _geom->tag_shader_modified();
491 _geom->tag_smooth_modified();
494 const HdSubdivisionTagsSchema subdivSchema = HdSubdivisionTagsSchema::GetFromParent(
496 PxOsdSubdivTags subdivTags;
497 if (
auto ds = subdivSchema.GetInterpolateBoundary()) {
498 subdivTags.SetVertexInterpolationRule(ds->GetTypedValue(0.0f));
500 if (
auto ds = subdivSchema.GetFaceVaryingLinearInterpolation()) {
501 subdivTags.SetFaceVaryingInterpolationRule(ds->GetTypedValue(0.0f));
503 if (
auto ds = subdivSchema.GetTriangleSubdivisionRule()) {
504 subdivTags.SetTriangleSubdivision(ds->GetTypedValue(0.0f));
506 if (
auto ds = subdivSchema.GetCornerIndices()) {
507 subdivTags.SetCornerIndices(ds->GetTypedValue(0.0f));
509 if (
auto ds = subdivSchema.GetCornerSharpnesses()) {
510 subdivTags.SetCornerWeights(ds->GetTypedValue(0.0f));
512 if (
auto ds = subdivSchema.GetCreaseIndices()) {
513 subdivTags.SetCreaseIndices(ds->GetTypedValue(0.0f));
515 if (
auto ds = subdivSchema.GetCreaseLengths()) {
516 subdivTags.SetCreaseLengths(ds->GetTypedValue(0.0f));
518 if (
auto ds = subdivSchema.GetCreaseSharpnesses()) {
519 subdivTags.SetCreaseWeights(ds->GetTypedValue(0.0f));
521 _topology.SetSubdivTags(subdivTags);
523 size_t numCorners = 0;
524 for (
const int vertCount : vertCounts) {
525 numCorners += vertCount;
528 _geom->resize_subd_faces(_topology.GetNumFaces(), numCorners);
529 _geom->resize_mesh(_topology.GetNumPoints(), 0);
531 subd_attr_P->
resize(_topology.GetNumPoints());
533 std::copy_n(vertIndx.data(), vertIndx.size(),
_geom->get_subd_face_corners().data());
535 int *subd_start_corner =
_geom->get_subd_start_corner().data();
536 int *subd_num_corners =
_geom->get_subd_num_corners().data();
537 int *subd_ptex_offset =
_geom->get_subd_ptex_offset().data();
541 size_t faceIndex = 0;
542 size_t indexOffset = 0;
543 for (
const int vertCount : vertCounts) {
544 subd_start_corner[faceIndex] = indexOffset;
545 subd_num_corners[faceIndex] = vertCount;
546 subd_ptex_offset[faceIndex] = ptex_offset;
547 const int num_ptex = (vertCount == 4) ? 1 : vertCount;
548 ptex_offset += num_ptex;
551 indexOffset += vertCount;
554 std::copy_n(faceShaders.data(), faceShaders.size(),
_geom->get_subd_shader().data());
555 std::ranges::fill(
_geom->get_subd_smooth(),
smooth);
557 _geom->tag_subd_face_corners_modified();
558 _geom->tag_subd_start_corner_modified();
559 _geom->tag_subd_num_corners_modified();
560 _geom->tag_subd_shader_modified();
561 _geom->tag_subd_smooth_modified();
562 _geom->tag_subd_ptex_offset_modified();
564 const VtIntArray creaseLengths = subdivTags.GetCreaseLengths();
565 if (!creaseLengths.empty()) {
566 size_t numCreases = 0;
567 for (
const int creaseLength : creaseLengths) {
568 numCreases += creaseLength - 1;
571 _geom->reserve_subd_creases(numCreases);
573 const VtIntArray creaseIndices = subdivTags.GetCreaseIndices();
574 const VtFloatArray creaseWeights = subdivTags.GetCreaseWeights();
577 size_t creaseLengthOffset = 0;
578 size_t createWeightOffset = 0;
579 for (
const int creaseLength : creaseLengths) {
580 for (
int j = 0; j < creaseLength - 1; ++j, ++createWeightOffset) {
581 const int v0 = creaseIndices[indexOffset + j];
582 const int v1 = creaseIndices[indexOffset + j + 1];
584 const float weight = creaseWeights.size() == creaseLengths.size() ?
585 creaseWeights[creaseLengthOffset] :
586 creaseWeights[createWeightOffset];
588 _geom->add_edge_crease(v0, v1, weight);
591 indexOffset += creaseLength;
592 creaseLengthOffset++;
595 const VtIntArray cornerIndices = subdivTags.GetCornerIndices();
596 const VtFloatArray cornerWeights = subdivTags.GetCornerWeights();
598 for (
size_t i = 0; i < cornerIndices.size(); ++i) {
599 _geom->add_vertex_crease(cornerIndices[i], cornerWeights[i]);
603 _geom->set_subd_dicing_rate(1.0f);
604 _geom->set_subd_max_level(_topology.GetRefineLevel());
611 _topology = HdMeshTopology();
612 _primitiveParams.clear();
blender::float3 packed_float3
Attribute * add(ustring name, const TypeDesc type, AttributeElement element)
void resize(Geometry *geom, AttributePrimitive prim)
T * data_for_write(const int step=0)
void Finalize(PXR_NS::HdRenderParam *renderParam) override
HdCyclesGeometry(const PXR_NS::SdfPath &rprimId)
PXR_NS::HdDirtyBits GetInitialDirtyBitsMask() const override
std::vector< CCL_NS::Object * > _instances
PXR_NS::HdDirtyBits GetInitialDirtyBitsMask() const override
HdCyclesMesh(const PXR_NS::SdfPath &rprimId)
void Finalize(PXR_NS::HdRenderParam *renderParam) override
@ SUBDIVISION_CATMULL_CLARK
T * resize(const size_t newsize)
void push_back_slow(const T &t)
HDCYCLES_NAMESPACE_OPEN_SCOPE Transform convert_transform(const GfMatrix4d &matrix)
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
HdInterpolation ReadPrimvarInterpolation(const HdPrimvarsSchema &primvars, const TfToken &name)
TfToken ReadPrimvarRole(const HdPrimvarsSchema &primvars, const TfToken &name)
HDCYCLES_NAMESPACE_OPEN_SCOPE HdSceneIndexPrim GetPrim(HdSceneDelegate *delegate, const SdfPath &id)
TfTokenVector PrimvarNamesAtInterpolation(const HdPrimvarsSchema &primvars, HdInterpolation interpolation)
VtValue ReadPrimvar(const HdPrimvarsSchema &primvars, const TfToken &name)
void ApplyPrimvars(AttributeSet &attributes, const ustring &name, VtValue value, AttributeElement elem, AttributeStandard std)
VtValue ComputeTriangulatedUniformPrimvar(VtValue value, const VtIntArray &primitiveParams)
VtValue ComputeTriangulatedFaceVaryingPrimvar(VtValue value, const HdType valueType, HdMeshUtil &meshUtil)