Skip to content

Commit

Permalink
Storm: Clarifying that HdRepr is owning the draw items by using std::…
Browse files Browse the repository at this point in the history
…unique_ptr.

(Internal change: 2069901)
  • Loading branch information
unhyperbolic authored and pixar-oss committed May 21, 2020
1 parent ed3c85b commit 8a8ab0b
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 37 deletions.
10 changes: 3 additions & 7 deletions pxr/imaging/hd/renderIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1844,13 +1844,9 @@ HdRenderIndex::_AppendDrawItems(
if (reprSelector.IsActiveRepr(i)) {
TfToken const& reprToken = reprSelector[i];

const HdRprim::HdDrawItemPtrVector* rprimDrawItems =
rprim->GetDrawItems(reprToken);

if (TF_VERIFY(rprimDrawItems)) {
drawItems.insert(drawItems.end(),
rprimDrawItems->begin(),
rprimDrawItems->end() );
for (const HdRepr::DrawItemUniquePtr &rprimDrawItem
: rprim->GetDrawItems(reprToken)) {
drawItems.push_back(rprimDrawItem.get());
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions pxr/imaging/hd/repr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,7 @@ HdReprSelector::operator[](size_t topologyIndex) const

HdRepr::HdRepr() = default;

HdRepr::~HdRepr()
{
for (HdDrawItem* item : _drawItems) {
delete item;
}
}
HdRepr::~HdRepr() = default;

PXR_NAMESPACE_CLOSE_SCOPE

15 changes: 8 additions & 7 deletions pxr/imaging/hd/repr.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,29 +148,30 @@ class HdReprSelector
class HdRepr final
{
public:
typedef std::vector<HdDrawItem*> DrawItems;
using DrawItemUniquePtr = std::unique_ptr<HdDrawItem>;
using DrawItemUniquePtrVector = std::vector<DrawItemUniquePtr>;

HD_API
HdRepr();
HD_API
~HdRepr();

/// Returns the draw items for this representation.
const DrawItems& GetDrawItems() {
const DrawItemUniquePtrVector& GetDrawItems() const {
return _drawItems;
}

/// Transfers ownership of a draw item to this repr.
void AddDrawItem(HdDrawItem *item) {
_drawItems.push_back(item);
void AddDrawItem(std::unique_ptr<HdDrawItem> &&item) {
_drawItems.push_back(std::move(item));
}

/// Returns the draw item at the requested index.
///
/// Note that the pointer returned is owned by this object and must not be
/// deleted.
HdDrawItem* GetDrawItem(size_t index) {
return _drawItems[index];
HdDrawItem* GetDrawItem(size_t index) const {
return _drawItems[index].get();
}

private:
Expand All @@ -179,7 +180,7 @@ class HdRepr final
HdRepr& operator=(const HdRepr&) = delete;

private:
DrawItems _drawItems;
DrawItemUniquePtrVector _drawItems;
};


Expand Down
14 changes: 9 additions & 5 deletions pxr/imaging/hd/rprim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,18 @@ HdRprim::InitRepr(HdSceneDelegate* delegate,
// -------------------------------------------------------------------------- //
/// Rprim Hydra Engine API : Execute-Phase
// -------------------------------------------------------------------------- //
const HdRprim::HdDrawItemPtrVector*
const HdRepr::DrawItemUniquePtrVector &
HdRprim::GetDrawItems(TfToken const& reprToken) const
{
HdReprSharedPtr repr = _GetRepr(reprToken);
if (repr) {
return &(repr->GetDrawItems());
if (HdReprSharedPtr const repr = _GetRepr(reprToken)) {
return repr->GetDrawItems();
}
return nullptr;

static HdRepr::DrawItemUniquePtrVector empty;

TF_CODING_ERROR("Rprim has draw items for repr %s", reprToken.GetText());

return empty;
}

// -------------------------------------------------------------------------- //
Expand Down
3 changes: 1 addition & 2 deletions pxr/imaging/hd/rprim.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,8 @@ class HdRprim {
/// These draw items should be constructed and cached beforehand by Sync().
/// If no draw items exist, or reprToken cannot be found, nullptr will be
/// returned.
using HdDrawItemPtrVector = std::vector<HdDrawItem*>;
HD_API
const HdDrawItemPtrVector*
const HdRepr::DrawItemUniquePtrVector &
GetDrawItems(TfToken const& reprToken) const;

// ---------------------------------------------------------------------- //
Expand Down
3 changes: 2 additions & 1 deletion pxr/imaging/hd/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
// 31 -> 32: renamed HdShader{Param} to HdMaterial{Param}
// 32 -> 33: Deleted GetPathForInstanceIndex; added GetScenePrimPath.
// 32 -> 34: Added HdInstancerContext to GetScenePrimPath.
#define HD_API_VERSION 34
// 34 -> 35: HdRepr is using std::unique_ptr<HdDrawItem>
#define HD_API_VERSION 35

// 1 -> 2: SimpleLighting -> FallbackLighting
#define HD_SHADER_API 2
Expand Down
5 changes: 3 additions & 2 deletions pxr/imaging/hdSt/basisCurves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,10 @@ HdStBasisCurves::_InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits)
continue;
}

HdDrawItem *drawItem = new HdStDrawItem(&_sharedData);
HdRepr::DrawItemUniquePtr drawItem =
std::make_unique<HdStDrawItem>(&_sharedData);
HdDrawingCoord *drawingCoord = drawItem->GetDrawingCoord();
repr->AddDrawItem(drawItem);
repr->AddDrawItem(std::move(drawItem));
if (desc.geomStyle == HdBasisCurvesGeomStyleWire) {
// Why does geom style require this change?
drawingCoord->SetTopologyIndex(HdStBasisCurves::HullTopology);
Expand Down
5 changes: 3 additions & 2 deletions pxr/imaging/hdSt/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1915,9 +1915,10 @@ HdStMesh::_InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits)
if (numDrawItems == 0) continue;

for (size_t itemId = 0; itemId < numDrawItems; itemId++) {
HdDrawItem *drawItem = new HdStDrawItem(&_sharedData);
repr->AddDrawItem(drawItem);
HdRepr::DrawItemUniquePtr drawItem =
std::make_unique<HdStDrawItem>(&_sharedData);
HdDrawingCoord *drawingCoord = drawItem->GetDrawingCoord();
repr->AddDrawItem(std::move(drawItem));

switch (desc.geomStyle) {
case HdMeshGeomStyleHull:
Expand Down
5 changes: 3 additions & 2 deletions pxr/imaging/hdSt/points.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,10 @@ HdStPoints::_InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits)
const HdPointsReprDesc &desc = descs[descIdx];

if (desc.geomStyle != HdPointsGeomStyleInvalid) {
HdDrawItem *drawItem = new HdStDrawItem(&_sharedData);
HdRepr::DrawItemUniquePtr drawItem =
std::make_unique<HdStDrawItem>(&_sharedData);
HdDrawingCoord *drawingCoord = drawItem->GetDrawingCoord();
_smoothHullRepr->AddDrawItem(drawItem);
_smoothHullRepr->AddDrawItem(std::move(drawItem));

// Set up drawing coord instance primvars.
drawingCoord->SetInstancePrimvarBaseIndex(
Expand Down
6 changes: 3 additions & 3 deletions pxr/imaging/hdSt/volume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ HdStVolume::_InitRepr(TfToken const &reprToken, HdDirtyBits* dirtyBits)
{
// All representations point to _volumeRepr.
if (!_volumeRepr) {
_volumeRepr = HdReprSharedPtr(new HdRepr());
HdDrawItem * const drawItem = new HdStDrawItem(&_sharedData);
_volumeRepr->AddDrawItem(drawItem);
_volumeRepr = std::make_shared<HdRepr>();
_volumeRepr->AddDrawItem(
std::make_unique<HdStDrawItem>(&_sharedData));
*dirtyBits |= HdChangeTracker::NewRepr;
}

Expand Down

0 comments on commit 8a8ab0b

Please sign in to comment.