Skip to content

Commit

Permalink
Storm texture system: udim support.
Browse files Browse the repository at this point in the history
Udim textures are communicated from the scene delegate to the render delegate like all other texture types are communicated in the new texture system by an asset-valued parameter on a texture node in a material network. For a udim texture, the resolved part of the SdfAssetPath will be a resolved file path such as /filePath/myImage.<UDIM>.exr.

Thus, this change also involves a change to the usd scene delegate that finds asset-valued attributes in a material network and finds the first udim tile to use its resolved path as basis for the resolved file path.

The texture system (in the render delegate) is expanding the <UDIM> the resolved file path by different integers to find all udim tiles.

This change also removes the ptex sampler object trying to destruct texture handles (which might not work for the reasons indicated in the other sampler object d'tors.

(Internal change: 2058293)
  • Loading branch information
unhyperbolic authored and pixar-oss committed Apr 28, 2020
1 parent 6a51bfc commit 4e42656
Show file tree
Hide file tree
Showing 13 changed files with 631 additions and 84 deletions.
48 changes: 13 additions & 35 deletions pxr/imaging/hdSt/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,6 @@ HdStMaterial::~HdStMaterial()
GetId().GetText());
}

// The new texture system does not support all HdTextureType's yet.
// Use old texture system for those.
static
bool
_IsSupportedByNewTextureSystem(const HdTextureType type)
{
switch(type) {
case HdTextureType::Uv:
case HdTextureType::Field:
case HdTextureType::Ptex:
return true;
default:
return false;
}
}

// Use data authored on material network nodes to create
// textures with the new texture system.
static
Expand All @@ -112,22 +96,18 @@ _GetNamedTextureHandles(
HdStShaderCode::NamedTextureHandleVector result;

for (HdStMaterialNetwork::TextureDescriptor const &desc : descs) {

if (_IsSupportedByNewTextureSystem(desc.type)) {

HdStTextureHandleSharedPtr const textureHandle =
resourceRegistry->AllocateTextureHandle(
desc.textureId,
desc.type,
desc.samplerParameters,
desc.memoryRequest,
usesBindlessTextures,
shaderCode);

result.push_back({ desc.name,
desc.type,
textureHandle });
}
HdStTextureHandleSharedPtr const textureHandle =
resourceRegistry->AllocateTextureHandle(
desc.textureId,
desc.type,
desc.samplerParameters,
desc.memoryRequest,
usesBindlessTextures,
shaderCode);

result.push_back({ desc.name,
desc.type,
textureHandle });
}

return result;
Expand Down Expand Up @@ -276,9 +256,7 @@ HdStMaterial::Sync(HdSceneDelegate *sceneDelegate,
// variable is set and the texture type can be handled
// by the new texture system.
//
if (!(useNewTextureSystem &&
_IsSupportedByNewTextureSystem(param.textureType))) {

if (!useNewTextureSystem) {
HdSt_MaterialBufferSourceAndTextureHelper::
ProcessTextureMaterialParam(
param,
Expand Down
81 changes: 59 additions & 22 deletions pxr/imaging/hdSt/samplerObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,15 @@ _GenGLSampler(HdSamplerParameters const &samplerParameters)

// Get texture sampler handle for bindless textures.
static
GLuint64EXT
_GenGLTextureSamplerHandle(HgiTextureHandle const &textureHandle,
GLuint64EXT
_GenGLTextureSamplerHandle(const GLuint textureName,
const GLuint samplerName,
const bool createBindlessHandle)
{
if (!createBindlessHandle) {
return 0;
}

HgiTexture * const texture = textureHandle.Get();
if (texture == nullptr) {
return 0;
}

HgiGLTexture * const glTexture = dynamic_cast<HgiGLTexture*>(texture);
if (glTexture == nullptr) {
TF_CODING_ERROR("Only OpenGL textures supported");
return 0;
}

const GLuint textureName = glTexture->GetTextureId();

if (textureName == 0) {
return 0;
}
Expand All @@ -134,6 +121,32 @@ _GenGLTextureSamplerHandle(HgiTextureHandle const &textureHandle,
return result;
}

// Get texture sampler handle for bindless textures.
static
GLuint64EXT
_GenGLTextureSamplerHandle(HgiTextureHandle const &textureHandle,
const GLuint samplerName,
const bool createBindlessHandle)
{
if (!createBindlessHandle) {
return 0;
}

HgiTexture * const texture = textureHandle.Get();
if (texture == nullptr) {
return 0;
}

HgiGLTexture * const glTexture = dynamic_cast<HgiGLTexture*>(texture);
if (glTexture == nullptr) {
TF_CODING_ERROR("Only OpenGL textures supported");
return 0;
}

return _GenGLTextureSamplerHandle(
glTexture->GetTextureId(), samplerName, createBindlessHandle);
}

// Get texture handle for bindless textures.
static
GLuint64EXT
Expand Down Expand Up @@ -282,14 +295,38 @@ HdStPtexSamplerObject::HdStPtexSamplerObject(
{
}

HdStPtexSamplerObject::~HdStPtexSamplerObject()
// See above comment about destroying bindless texture handles
HdStPtexSamplerObject::~HdStPtexSamplerObject() = default;

///////////////////////////////////////////////////////////////////////////////
// Udim sampler

HdStUdimSamplerObject::HdStUdimSamplerObject(
HdStUdimTextureObject const &udimTexture,
HdSamplerParameters const &samplerParameters,
const bool createBindlessHandle)
: _glTexelsSamplerName(
_GenGLSampler(
samplerParameters))
, _texelsGLTextureHandle(
_GenGLTextureSamplerHandle(
udimTexture.GetTexelGLTextureName(),
_glTexelsSamplerName,
createBindlessHandle))
, _layoutGLTextureHandle(
_GenGlTextureHandle(
udimTexture.GetLayoutGLTextureName(),
createBindlessHandle))
{
}

HdStUdimSamplerObject::~HdStUdimSamplerObject()
{
if (_texelsGLTextureHandle) {
glMakeTextureHandleNonResidentARB(_texelsGLTextureHandle);
}
if (_layoutGLTextureHandle) {
glMakeTextureHandleNonResidentARB(_layoutGLTextureHandle);
// See above comment about destroying bindless texture handles

if (_glTexelsSamplerName) {
glDeleteSamplers(1, &_glTexelsSamplerName);
}
}

PXR_NAMESPACE_CLOSE_SCOPE
49 changes: 49 additions & 0 deletions pxr/imaging/hdSt/samplerObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ PXR_NAMESPACE_OPEN_SCOPE
class HdStUvTextureObject;
class HdStFieldTextureObject;
class HdStPtexTextureObject;
class HdStUdimTextureObject;

using HdStSamplerObjectSharedPtr =
std::shared_ptr<class HdStSamplerObject>;
Expand Down Expand Up @@ -166,6 +167,49 @@ class HdStPtexSamplerObject final : public HdStSamplerObject {
const uint64_t _layoutGLTextureHandle;
};

/// \class HdStUdimSamplerObject
///
/// A sampler suitable for Udim textures (wraps one GPU sampler
/// for the texels texture).
///
class HdStUdimSamplerObject final : public HdStSamplerObject {
public:
HdStUdimSamplerObject(
HdStUdimTextureObject const &ptexTexture,
HdSamplerParameters const &samplerParameters,
bool createBindlessHandle);

~HdStUdimSamplerObject() override;

/// The GL sampler (as understood by glBindSampler) for the
/// texels texture.
///
uint32_t GetTexelsGLSamplerName() const {
return _glTexelsSamplerName;
}

/// The GL texture handle for bindless textures (as returned by
/// glGetTextureHandleARB). This is for texels.
///
/// Only available when requested.
///
uint64_t GetTexelsGLTextureHandle() const {
return _texelsGLTextureHandle;
}

/// Similar to GetGLTexelsTextureHandle but for layout.
///
uint64_t GetLayoutGLTextureHandle() const {
return _layoutGLTextureHandle;
}

private:
const uint32_t _glTexelsSamplerName;

const uint64_t _texelsGLTextureHandle;
const uint64_t _layoutGLTextureHandle;
};

template<HdTextureType textureType>
struct HdSt_TypedSamplerObjectHelper;

Expand Down Expand Up @@ -193,6 +237,11 @@ struct HdSt_TypedSamplerObjectHelper<HdTextureType::Ptex> {
using type = HdStPtexSamplerObject;
};

template<>
struct HdSt_TypedSamplerObjectHelper<HdTextureType::Udim> {
using type = HdStUdimSamplerObject;
};

PXR_NAMESPACE_CLOSE_SCOPE

#endif
9 changes: 6 additions & 3 deletions pxr/imaging/hdSt/samplerObjectRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ _MakeSamplerObject(
case HdTextureType::Ptex:
return _MakeTypedSamplerObject<HdTextureType::Ptex>(
texture, samplerParameters, createBindlessHandle);
default:
TF_CODING_ERROR("Unsupported texture type");
return nullptr;
case HdTextureType::Udim:
return _MakeTypedSamplerObject<HdTextureType::Udim>(
texture, samplerParameters, createBindlessHandle);
}

TF_CODING_ERROR("Unsupported texture type");
return nullptr;
}

HdStSamplerObjectSharedPtr
Expand Down
64 changes: 60 additions & 4 deletions pxr/imaging/hdSt/textureBinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,16 @@ HdSt_TextureBinder::GetBufferSpecs(
_bindlessHandleTupleType);
}
break;
default:
TF_CODING_ERROR("Unsupported texture type");
case HdTextureType::Udim:
if (UsesBindlessTextures()) {
specs->emplace_back(
texture.name,
_bindlessHandleTupleType);
specs->emplace_back(
TfToken(texture.name.GetString() + "_layout"),
_bindlessHandleTupleType);
}
break;
}
}
}
Expand Down Expand Up @@ -195,6 +203,27 @@ class _ComputeBufferSourcesFunctor {
TfToken(name.GetString() + "_layout"),
sampler.GetLayoutGLTextureHandle()));
}

static void Compute(
TfToken const &name,
HdStUdimTextureObject const &texture,
HdStUdimSamplerObject const &sampler,
HdBufferSourceSharedPtrVector * const sources)
{
if (!HdSt_TextureBinder::UsesBindlessTextures()) {
return;
}

sources->push_back(
std::make_shared<HdSt_BindlessSamplerBufferSource>(
name,
sampler.GetTexelsGLTextureHandle()));

sources->push_back(
std::make_shared<HdSt_BindlessSamplerBufferSource>(
TfToken(name.GetString() + "_layout"),
sampler.GetLayoutGLTextureHandle()));
}
};

void
Expand Down Expand Up @@ -278,6 +307,31 @@ class _BindFunctor {
glBindTexture(GL_TEXTURE_BUFFER,
bind ? texture.GetLayoutGLTextureName() : 0);
}

static void Compute(
TfToken const &name,
HdStUdimTextureObject const &texture,
HdStUdimSamplerObject const &sampler,
HdSt_ResourceBinder const &binder,
const bool bind)
{
const HdBinding texelBinding = binder.GetBinding(name);
const int texelSamplerUnit = texelBinding.GetTextureUnit();

glActiveTexture(GL_TEXTURE0 + texelSamplerUnit);
glBindTexture(GL_TEXTURE_2D_ARRAY,
bind ? texture.GetTexelGLTextureName() : 0);
glBindSampler(texelSamplerUnit,
bind ? sampler.GetTexelsGLSamplerName() : 0);

const HdBinding layoutBinding = binder.GetBinding(
TfToken(name.GetString() + "_layout"));
const int layoutSamplerUnit = layoutBinding.GetTextureUnit();

glActiveTexture(GL_TEXTURE0 + layoutSamplerUnit);
glBindTexture(GL_TEXTURE_1D,
bind ? texture.GetLayoutGLTextureName() : 0);
}
};

template<HdTextureType textureType, class Functor, typename ...Args>
Expand Down Expand Up @@ -328,8 +382,10 @@ void _Dispatch(
_CastAndCompute<HdTextureType::Ptex, Functor>(
namedTextureHandle, std::forward<Args>(args)...);
break;
default:
TF_CODING_ERROR("Unsupported texture type");
case HdTextureType::Udim:
_CastAndCompute<HdTextureType::Udim, Functor>(
namedTextureHandle, std::forward<Args>(args)...);
break;
}
}

Expand Down
Loading

0 comments on commit 4e42656

Please sign in to comment.