Skip to content

Commit

Permalink
simplify MetalBlitter
Browse files Browse the repository at this point in the history
- remove metal blit workarounds
- don't issue a blit from a renderpass, this only affect Renderer::copyFrame
- We only handle a single texture now (instead of color+depth), so we
  can simplify the code a lot.
  Didn't touch the "slowpath" much, but it now assumes it blits color or
  depth, not both.
  • Loading branch information
pixelflinger committed Nov 15, 2023
1 parent c6fde5f commit 6847198
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 299 deletions.
61 changes: 34 additions & 27 deletions filament/backend/src/metal/MetalBlitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
#include <tsl/robin_map.h>
#include <utils/Hash.h>

namespace filament {
namespace backend {
namespace filament::backend {

struct MetalContext;

Expand All @@ -35,36 +34,45 @@ class MetalBlitter {

explicit MetalBlitter(MetalContext& context) noexcept;

enum class FormatType { NONE, COLOR, DEPTH, STENCIL, DEPTH_STENCIL };
static FormatType getFormatType(TextureFormat format) noexcept {
bool const depth = isDepthFormat(format);
bool const stencil = isStencilFormat(format);
if (depth && stencil) return FormatType::DEPTH_STENCIL;
if (depth) return FormatType::DEPTH;
if (stencil) return FormatType::STENCIL;
return FormatType::COLOR;
}

struct BlitArgs {
struct Attachment {
id<MTLTexture> color = nil;
id<MTLTexture> depth = nil;
using Type = FormatType;
id<MTLTexture> texture = nil;
MTLRegion region = {};
uint8_t level = 0;
uint32_t slice = 0; // must be 0 on source attachment
Type type = Type::NONE;
};

// Valid source formats: 2D, 2DArray, 2DMultisample, 3D
// Valid destination formats: 2D, 2DArray, 3D, Cube
Attachment source, destination;
Attachment source;
Attachment destination;
SamplerMagFilter filter;

bool blitColor() const {
return source.color != nil && destination.color != nil;
return source.type == Attachment::Type::COLOR &&
destination.type == Attachment::Type::COLOR;
}

bool blitDepth() const {
return source.depth != nil && destination.depth != nil;
}

bool colorDestinationIsFullAttachment() const {
return destination.color.width == destination.region.size.width &&
destination.color.height == destination.region.size.height;
return source.type == Attachment::Type::DEPTH &&
destination.type == Attachment::Type::DEPTH;
}

bool depthDestinationIsFullAttachment() const {
return destination.depth.width == destination.region.size.width &&
destination.depth.height == destination.region.size.height;
bool destinationIsFullAttachment() const {
return destination.texture.width == destination.region.size.width &&
destination.texture.height == destination.region.size.height;
}
};

Expand All @@ -78,10 +86,8 @@ class MetalBlitter {

private:

static void setupColorAttachment(const BlitArgs& args, MTLRenderPassDescriptor* descriptor,
uint32_t depthPlane);
static void setupDepthAttachment(const BlitArgs& args, MTLRenderPassDescriptor* descriptor,
uint32_t depthPlane);
static void setupAttachment(MTLRenderPassAttachmentDescriptor* descriptor,
const BlitArgs& args, uint32_t depthPlane);

struct BlitFunctionKey {
bool blitColor;
Expand All @@ -94,7 +100,7 @@ class MetalBlitter {

bool isValid() const noexcept {
// MSAA 3D textures do not exist.
bool hasMsaa = msaaColorSource || msaaDepthSource;
bool const hasMsaa = msaaColorSource || msaaDepthSource;
return !(hasMsaa && sources3D);
}

Expand All @@ -111,15 +117,17 @@ class MetalBlitter {
}
};

void blitFastPath(id<MTLCommandBuffer> cmdBuffer, bool& blitColor, bool& blitDepth,
static bool blitFastPath(id<MTLCommandBuffer> cmdBuffer,
const BlitArgs& args, const char* label);
void blitSlowPath(id<MTLCommandBuffer> cmdBuffer, bool& blitColor, bool& blitDepth,

void blitSlowPath(id<MTLCommandBuffer> cmdBuffer,
const BlitArgs& args, const char* label);

void blitDepthPlane(id<MTLCommandBuffer> cmdBuffer, bool blitColor, bool blitDepth,
const BlitArgs& args, uint32_t depthPlaneSource, uint32_t depthPlaneDest,
const char* label);
id<MTLTexture> createIntermediateTexture(id<MTLTexture> t, MTLSize size);
id<MTLFunction> compileFragmentFunction(BlitFunctionKey key);

id<MTLFunction> compileFragmentFunction(BlitFunctionKey key) const;
id<MTLFunction> getBlitVertexFunction();
id<MTLFunction> getBlitFragmentFunction(BlitFunctionKey key);

Expand All @@ -130,10 +138,9 @@ class MetalBlitter {
tsl::robin_map<BlitFunctionKey, Function, HashFn> mBlitFunctions;

id<MTLFunction> mVertexFunction = nil;

};

} // namespace backend
} // namespace filament
} // namespace filament::backend


#endif //TNT_METALBLITTER_H
Loading

0 comments on commit 6847198

Please sign in to comment.