Skip to content

Commit

Permalink
Prevent mipmapping of 1x1 textures.
Browse files Browse the repository at this point in the history
In Metal, if a texture has all dimensions being a single texel big the mipmapping call `generateMipmapsForTexture` will fail and throw an exception.  So we add a check to the existing one in `HgiMetalBlitCmds::GenerateMipMaps()` which catches this.

Thanks to @Morteeza for this.

Contribution: Apple

Fixes #2264

(Internal change: 2281900)
  • Loading branch information
davidgyu authored and pixar-oss committed Jun 20, 2023
1 parent 9222cec commit c6e09a4
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions pxr/imaging/hgiMetal/blitCmds.mm
Original file line number Diff line number Diff line change
Expand Up @@ -444,29 +444,46 @@
}

static bool
_HgiCanBeFiltered(HgiFormat format)
_HgiTextureCanBeFiltered(HgiTextureDesc const &descriptor)
{
HgiFormat const componentFormat = HgiGetComponentBaseFormat(format);
HgiFormat const componentFormat =
HgiGetComponentBaseFormat(descriptor.format);

switch(componentFormat) {
case HgiFormatInt16:
case HgiFormatUInt16:
case HgiFormatInt32:
if (componentFormat == HgiFormatInt16 ||
componentFormat == HgiFormatUInt16 ||
componentFormat == HgiFormatInt32) {
return false;
default:
return true;
}

GfVec3i const dims = descriptor.dimensions;
switch (descriptor.type) {
case HgiTextureType1D:
case HgiTextureType1DArray:
return (dims[0] > 1);

case HgiTextureType2D:
case HgiTextureType2DArray:
return (dims[0] > 1 || dims[1] > 1);

case HgiTextureType3D:
return (dims[0] > 1 || dims[1] > 1 || dims[2] > 1);

default:
TF_CODING_ERROR("Unsupported HgiTextureType enum value");
return false;
}

return false;
}

void
HgiMetalBlitCmds::GenerateMipMaps(HgiTextureHandle const& texture)
{
HgiMetalTexture* metalTex = static_cast<HgiMetalTexture*>(texture.Get());

if (metalTex) {
HgiFormat const format = metalTex->GetDescriptor().format;
if (_HgiCanBeFiltered(format)) {
if (_HgiTextureCanBeFiltered(metalTex->GetDescriptor())) {
_CreateEncoder();
// Can fail if the texture format is not filterable.
[_blitEncoder generateMipmapsForTexture:metalTex->GetTextureId()];
}
}
Expand Down

0 comments on commit c6e09a4

Please sign in to comment.