Skip to content

Commit

Permalink
Storm volumetric rendering: respecting the grid name and loading the …
Browse files Browse the repository at this point in the history
…grid from a VDB file identified by the grid name rather than just the first grid.

To achieve this, a texture container are introduced: the texture registry stores a texture container (per VDB file) which stores textures for the different grids in a VDB file.

The texture container is responsible for garbage collecting its own textures (requiring the texture registy to call the new GlfTexture::GarbageCollect()).

(Internal change: 2042134)
  • Loading branch information
unhyperbolic authored and pixar-oss committed Feb 18, 2020
1 parent 7f7ad4c commit 1228653
Show file tree
Hide file tree
Showing 14 changed files with 489 additions and 79 deletions.
3 changes: 3 additions & 0 deletions pxr/imaging/glf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pxr_library(glf
uvTextureStorage
uvTextureStorageData
vdbTexture
vdbTextureContainer
${optionalPublicClasses}

PRIVATE_CLASSES
Expand All @@ -97,12 +98,14 @@ pxr_library(glf

PUBLIC_HEADERS
api.h
textureContainer.h

PRIVATE_HEADERS
rankedTypeMap.h
stb/stb_image.h
stb/stb_image_resize.h
stb/stb_image_write.h
textureContainerImpl.h

CPPFILES
${optionalCppFiles}
Expand Down
4 changes: 2 additions & 2 deletions pxr/imaging/glf/plugInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"textureTypes": ["*"],
"precedence": 0
},
"GlfVdbTexture" : {
"bases": ["GlfBaseTexture"],
"GlfVdbTextureContainer" : {
"bases": ["GlfTexture"],
"textureTypes": ["vdb"],
"precedence": 0
},
Expand Down
7 changes: 7 additions & 0 deletions pxr/imaging/glf/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,12 @@ GlfTexture::IsOriginLowerLeft() const
return _originLocation == GlfImage::OriginLowerLeft;
}

void
GlfTexture::GarbageCollect()
{
// Nothing to do here.
// Only needed for containers of textures.
}

PXR_NAMESPACE_CLOSE_SCOPE

5 changes: 5 additions & 0 deletions pxr/imaging/glf/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ class GlfTexture : public TfRefBase, public TfWeakBase, boost::noncopyable {
GLF_API
bool IsOriginLowerLeft() const;

/// An opportunity to throw out unused textures if this is
/// a container for textures.
GLF_API
virtual void GarbageCollect();

protected:
GLF_API
GlfTexture();
Expand Down
86 changes: 86 additions & 0 deletions pxr/imaging/glf/textureContainer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// Copyright 2020 Pixar
//
// 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:
//
// 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.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
//
#ifndef PXR_IMAGING_GLF_TEXTURE_CONTAINER_H
#define PXR_IMAGING_GLF_TEXTURE_CONTAINER_H

/// \file glf/textureContainer.h

#include "pxr/pxr.h"
#include "pxr/imaging/glf/api.h"

#include "pxr/base/tf/declarePtrs.h"

#include <map>

PXR_NAMESPACE_OPEN_SCOPE

TF_DECLARE_WEAK_AND_REF_PTRS(GlfTextureHandle);
TF_DECLARE_REF_PTRS(GlfTexture);

/// \class GlfTextureContainer
///
/// A base class for texture containers, e.g., for a movie file where a frame
/// corresponds to a texture, for an exr file where a subimage corresponds
/// to a texture, or for an OpenVDB file where a grid corresponds to a texture.
///
/// Templated since for, e.g., a movie we would key the container by frame
/// number but for an exr file by subimage name.
///
/// A note on garbage collection: texture containers are registered with and
/// will be garbage collected by the texture registry. Thus, a texture in a
/// container has to hold on to a ref ptr to the container's handle so that
/// the registry won't delete the container while any of the textures in the
/// container is in use. See GarbageCollect for more details.
///
template<typename Identifier>
class GlfTextureContainer : public GlfTexture {
public:

/// Get texture handle for a particular frame, subimage, grid, ...
GLF_API
GlfTextureHandlePtr GetTextureHandle(Identifier const &identifier);

/// Implements the garbage collection of textures in this container.
///
/// When the Glf clients give up all their references to all textures in
/// this container, garbage collection will happen in two steps:
/// the container will note that there are no other references and
/// deletes the textures which references the container.
/// Thus, the texture registry is having the only remaining reference to
/// the container and will delete the container.
GLF_API
void GarbageCollect() override;

protected:
// Create texture for a particular frame, subimage, grid, ...
virtual GlfTextureRefPtr _CreateTexture(Identifier const &identifier) = 0;

// Texture handles for frames, subimage, grids, ...
std::map<Identifier, GlfTextureHandleRefPtr> _textureHandles;
};

PXR_NAMESPACE_CLOSE_SCOPE

#endif

67 changes: 67 additions & 0 deletions pxr/imaging/glf/textureContainerImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// Copyright 2020 Pixar
//
// 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:
//
// 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.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
//
#ifndef PXR_IMAGING_GLF_TEXTURE_CONTAINER_IMPL_H
#define PXR_IMAGING_GLF_TEXTURE_CONTAINER_IMPL_H

#include "pxr/imaging/glf/textureContainer.h"

#include "pxr/imaging/glf/textureHandle.h"
#include "pxr/base/trace/trace.h"

PXR_NAMESPACE_OPEN_SCOPE

template<typename Identifier>
GlfTextureHandlePtr
GlfTextureContainer<Identifier>::GetTextureHandle(
Identifier const &identifier)
{
GlfTextureHandleRefPtr &textureHandle = _textureHandles[identifier];
if (!textureHandle) {
// Create texture handle if it was not in map
textureHandle = GlfTextureHandle::New(_CreateTexture(identifier));
}
return textureHandle;
}

template<typename Identifier>
void
GlfTextureContainer<Identifier>::GarbageCollect()
{
TRACE_FUNCTION();

// Garbage collection similar to texture registry.
for (auto it = _textureHandles.begin(); it != _textureHandles.end(); ) {
const GlfTextureHandleRefPtr &handle = it->second;
// Delete texture if we hold on to the only reference.
if (TF_VERIFY(handle) && handle->IsUnique()) {
it = _textureHandles.erase(it);
} else {
++it;
}
}
}

PXR_NAMESPACE_CLOSE_SCOPE

#endif
8 changes: 8 additions & 0 deletions pxr/imaging/glf/textureRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ GlfTextureRegistry::GarbageCollectIfNeeded()
while (it != _textureRegistry.end()){
const GlfTextureHandleRefPtr &handle = it->second.GetHandle();

// Call garbage collection on texture containers first, see
// GlfTextureContainer::GarbageCollect() for reason.
if (TF_VERIFY(handle)) {
if (GlfTexturePtr texture = handle->GetTexture()) {
texture->GarbageCollect();
}
}

// Null handles should not have been added to the registry
if (TF_VERIFY(handle) && handle->IsUnique()) {
it = _textureRegistry.erase(it);
Expand Down
35 changes: 15 additions & 20 deletions pxr/imaging/glf/vdbTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//

#include "pxr/imaging/glf/vdbTexture.h"
#include "pxr/imaging/glf/vdbTextureContainer.h"
#ifdef PXR_OPENVDB_SUPPORT_ENABLED
#include "pxr/imaging/glf/vdbTextureData.h"
#else
Expand All @@ -35,23 +36,12 @@

PXR_NAMESPACE_OPEN_SCOPE

TF_REGISTRY_FUNCTION(TfType)
{
using Type = GlfVdbTexture;
TfType t = TfType::Define<Type, TfType::Bases<GlfBaseTexture> >();
t.SetFactory< GlfTextureFactory<GlfVdbTexture> >();
}

GlfVdbTextureRefPtr
GlfVdbTexture::New(TfToken const &filePath)
{
return TfCreateRefPtr(new GlfVdbTexture(filePath));
}

GlfVdbTextureRefPtr
GlfVdbTexture::New(std::string const &filePath)
GlfVdbTexture::New(
GlfVdbTextureContainerRefPtr const &textureContainer,
TfToken const &gridName)
{
return New(TfToken(filePath));
return TfCreateRefPtr(new GlfVdbTexture(textureContainer, gridName));
}

int
Expand All @@ -60,9 +50,11 @@ GlfVdbTexture::GetNumDimensions() const
return 3;
}

GlfVdbTexture::GlfVdbTexture(TfToken const &filePath)
: GlfBaseTexture()
, _filePath(filePath)
GlfVdbTexture::GlfVdbTexture(
GlfVdbTextureContainerRefPtr const &textureContainer,
TfToken const &gridName)
: _textureContainer(textureContainer)
, _gridName(gridName)
{
}

Expand All @@ -71,7 +63,7 @@ GlfVdbTexture::GetTextureInfo(bool forceLoad)
{
VtDictionary info = GlfBaseTexture::GetTextureInfo(forceLoad);

info["imageFilePath"] = _filePath;
info["imageFilePath"] = _textureContainer->GetFilePath().GetString();

return info;
}
Expand All @@ -89,7 +81,10 @@ GlfVdbTexture::_ReadTexture()
#ifdef PXR_OPENVDB_SUPPORT_ENABLED

GlfVdbTextureDataRefPtr const texData =
GlfVdbTextureData::New(_filePath, GetMemoryRequested());
GlfVdbTextureData::New(
_textureContainer->GetFilePath().GetString(),
_gridName,
GetMemoryRequested());

if (texData) {
texData->Read(0, _GenerateMipmap());
Expand Down
26 changes: 14 additions & 12 deletions pxr/imaging/glf/vdbTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,24 @@

PXR_NAMESPACE_OPEN_SCOPE

TF_DECLARE_WEAK_AND_REF_PTRS(GlfVdbTextureContainer);
TF_DECLARE_WEAK_AND_REF_PTRS(GlfVdbTexture);

/// \class GlfVdbTexture
///
/// Represents a 3-dimensional texture read from an OpenVDB file.
/// Represents a 3-dimensional texture read from grid in an OpenVDB file.
///
/// Current limitations: we always use the first grid in the OpenVDB file.
/// The texture is always loaded at the full resolution of the OpenVDB grid,
/// ignoring the memory request.
/// This texture is supposed to be held by a GlfVdbTextureContainer which
/// tells this texture also what OpenVDB file to read.
///
class GlfVdbTexture : public GlfBaseTexture {
public:
/// Creates a new texture instance for the OpenVDB file at \p filePath
/// Creates a new texture instance for the grid named \gridName in
/// the OpenVDB file opened by \p textureContainer.
GLF_API
static GlfVdbTextureRefPtr New(TfToken const &filePath);

/// Creates a new texture instance for the OpenVDB file at \p filePath
GLF_API
static GlfVdbTextureRefPtr New(std::string const &filePath);
static GlfVdbTextureRefPtr New(
GlfVdbTextureContainerRefPtr const &textureContainer,
TfToken const &gridName);

/// Returns the transform of the grid in the OpenVDB file as well as the
/// bounding box of the samples in the corresponding OpenVDB tree.
Expand All @@ -72,7 +71,9 @@ class GlfVdbTexture : public GlfBaseTexture {

protected:
GLF_API
GlfVdbTexture(TfToken const &filePath);
GlfVdbTexture(
GlfVdbTextureContainerRefPtr const &textureContainer,
TfToken const &gridName);

GLF_API
void _ReadTexture() override;
Expand All @@ -81,7 +82,8 @@ class GlfVdbTexture : public GlfBaseTexture {
bool _GenerateMipmap() const;

private:
const TfToken _filePath;
GlfVdbTextureContainerRefPtr const _textureContainer;
const TfToken _gridName;

GfBBox3d _boundingBox;
};
Expand Down
Loading

0 comments on commit 1228653

Please sign in to comment.