Skip to content

Commit

Permalink
Metal: Align texture view constructors with the API
Browse files Browse the repository at this point in the history
Fully expose Metal API parameters
through texture view constructors.

No behavioral change.

Bug: angleproject:8355
Change-Id: I2a55f1e799d6e06b2528eefdee4bf5936e5b4301
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5426671
Commit-Queue: Alexey Knyazev <[email protected]>
Reviewed-by: Kenneth Russell <[email protected]>
  • Loading branch information
lexaknyazev authored and Angle LUCI CQ committed Apr 4, 2024
1 parent f8bcfc3 commit cdbc58f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 52 deletions.
20 changes: 11 additions & 9 deletions src/libANGLE/renderer/metal/mtl_resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,18 @@ class Texture final : public Resource,
bool renderTargetOnly);

// Create a texture view
Texture(Texture *original, MTLPixelFormat format);
Texture(Texture *original, MTLTextureType type, NSRange mipmapLevelRange, NSRange slices);
Texture(Texture *original, MTLPixelFormat format, const TextureSwizzleChannels &swizzle);

// Creates a view for a shader image binding.
Texture(Texture *original, MTLPixelFormat pixelFormat);
Texture(Texture *original,
MTLPixelFormat pixelFormat,
MTLTextureType textureType,
NSRange levels,
NSRange slices);
Texture(Texture *original,
MTLTextureType type,
const MipmapNativeLevel &level,
int layer,
MTLPixelFormat pixelFormat);
MTLPixelFormat pixelFormat,
MTLTextureType textureType,
NSRange levels,
NSRange slices,
const TextureSwizzleChannels &swizzle);

void syncContentIfNeeded(ContextMtl *context);

Expand Down
75 changes: 32 additions & 43 deletions src/libANGLE/renderer/metal/mtl_resources.mm
Original file line number Diff line number Diff line change
Expand Up @@ -481,29 +481,33 @@ bool needMultisampleColorFormatShaderReadWorkaround(ContextMtl *context, MTLText
}
}

Texture::Texture(Texture *original, MTLPixelFormat format)
Texture::Texture(Texture *original, MTLPixelFormat pixelFormat)
: Resource(original),
mColorWritableMask(original->mColorWritableMask) // Share color write mask property
{
ANGLE_MTL_OBJC_SCOPE
{
auto view = [original->get() newTextureViewWithPixelFormat:format];
auto view = [original->get() newTextureViewWithPixelFormat:pixelFormat];

set([view ANGLE_MTL_AUTORELEASE]);
// Texture views consume no additional memory
mEstimatedByteSize = 0;
}
}

Texture::Texture(Texture *original, MTLTextureType type, NSRange mipmapLevelRange, NSRange slices)
Texture::Texture(Texture *original,
MTLPixelFormat pixelFormat,
MTLTextureType textureType,
NSRange levels,
NSRange slices)
: Resource(original),
mColorWritableMask(original->mColorWritableMask) // Share color write mask property
{
ANGLE_MTL_OBJC_SCOPE
{
auto view = [original->get() newTextureViewWithPixelFormat:original->pixelFormat()
textureType:type
levels:mipmapLevelRange
auto view = [original->get() newTextureViewWithPixelFormat:pixelFormat
textureType:textureType
levels:levels
slices:slices];

set([view ANGLE_MTL_AUTORELEASE]);
Expand All @@ -512,19 +516,23 @@ bool needMultisampleColorFormatShaderReadWorkaround(ContextMtl *context, MTLText
}
}

Texture::Texture(Texture *original, MTLPixelFormat format, const TextureSwizzleChannels &swizzle)
Texture::Texture(Texture *original,
MTLPixelFormat pixelFormat,
MTLTextureType textureType,
NSRange levels,
NSRange slices,
const TextureSwizzleChannels &swizzle)
: Resource(original),
mColorWritableMask(original->mColorWritableMask) // Share color write mask property
{
#if ANGLE_MTL_SWIZZLE_AVAILABLE
ANGLE_MTL_OBJC_SCOPE
{
auto view = [original->get()
newTextureViewWithPixelFormat:format
textureType:original->textureType()
levels:NSMakeRange(0, original->mipmapLevels())
slices:NSMakeRange(0, original->cubeFacesOrArrayLength())
swizzle:swizzle];
auto view = [original->get() newTextureViewWithPixelFormat:pixelFormat
textureType:textureType
levels:levels
slices:slices
swizzle:swizzle];

set([view ANGLE_MTL_AUTORELEASE]);
// Texture views consume no additional memory
Expand All @@ -535,28 +543,6 @@ bool needMultisampleColorFormatShaderReadWorkaround(ContextMtl *context, MTLText
#endif
}

Texture::Texture(Texture *original,
MTLTextureType type,
const MipmapNativeLevel &level,
int layer,
MTLPixelFormat pixelFormat)
: Resource(original),
mColorWritableMask(std::make_shared<MTLColorWriteMask>(MTLColorWriteMaskAll))
{
ANGLE_MTL_OBJC_SCOPE
{
ASSERT(original->pixelFormat() == pixelFormat || original->supportFormatView());
auto view = [original->get() newTextureViewWithPixelFormat:pixelFormat
textureType:type
levels:NSMakeRange(level.get(), 1)
slices:NSMakeRange(layer, 1)];

set([view ANGLE_MTL_AUTORELEASE]);
// Texture views consume no additional memory
mEstimatedByteSize = 0;
}
}

void Texture::syncContent(ContextMtl *context, mtl::BlitCommandEncoder *blitEncoder)
{
InvokeCPUMemSync(context, blitEncoder, this);
Expand Down Expand Up @@ -681,8 +667,9 @@ bool needMultisampleColorFormatShaderReadWorkaround(ContextMtl *context, MTLText
switch (textureType())
{
case MTLTextureTypeCube:
return TextureRef(new Texture(
this, MTLTextureType2D, NSMakeRange(0, mipmapLevels()), NSMakeRange(face, 1)));
return TextureRef(new Texture(this, pixelFormat(), MTLTextureType2D,
NSMakeRange(0, mipmapLevels()),
NSMakeRange(face, 1)));
default:
UNREACHABLE();
return nullptr;
Expand All @@ -699,8 +686,8 @@ bool needMultisampleColorFormatShaderReadWorkaround(ContextMtl *context, MTLText
case MTLTextureTypeCube:
case MTLTextureType2D:
case MTLTextureType2DArray:
return TextureRef(new Texture(this, MTLTextureType2D, NSMakeRange(level.get(), 1),
NSMakeRange(slice, 1)));
return TextureRef(new Texture(this, pixelFormat(), MTLTextureType2D,
NSMakeRange(level.get(), 1), NSMakeRange(slice, 1)));
default:
UNREACHABLE();
return nullptr;
Expand All @@ -713,8 +700,8 @@ bool needMultisampleColorFormatShaderReadWorkaround(ContextMtl *context, MTLText
ANGLE_MTL_OBJC_SCOPE
{
NSUInteger slices = cubeFacesOrArrayLength();
return TextureRef(
new Texture(this, textureType(), NSMakeRange(level.get(), 1), NSMakeRange(0, slices)));
return TextureRef(new Texture(this, pixelFormat(), textureType(),
NSMakeRange(level.get(), 1), NSMakeRange(0, slices)));
}
}

Expand All @@ -731,7 +718,8 @@ bool needMultisampleColorFormatShaderReadWorkaround(ContextMtl *context, MTLText
ASSERT(isShaderReadable());
ASSERT(isShaderWritable());
ASSERT(format == pixelFormat() || supportFormatView());
return TextureRef(new Texture(this, textureType(), level, layer, format));
return TextureRef(new Texture(this, format, textureType(), NSMakeRange(level.get(), 1),
NSMakeRange(layer, 1)));
}

TextureRef Texture::createViewWithCompatibleFormat(MTLPixelFormat format)
Expand All @@ -742,7 +730,8 @@ bool needMultisampleColorFormatShaderReadWorkaround(ContextMtl *context, MTLText
TextureRef Texture::createSwizzleView(MTLPixelFormat format, const TextureSwizzleChannels &swizzle)
{
#if ANGLE_MTL_SWIZZLE_AVAILABLE
return TextureRef(new Texture(this, format, swizzle));
return TextureRef(new Texture(this, format, textureType(), NSMakeRange(0, mipmapLevels()),
NSMakeRange(0, cubeFacesOrArrayLength()), swizzle));
#else
WARN() << "Texture swizzle is not supported on pre iOS 13.0 and macOS 15.0";
UNIMPLEMENTED();
Expand Down

0 comments on commit cdbc58f

Please sign in to comment.