1

每当我尝试在 Metal 中对具有透明部分的图像进行 blit 处理时,透明部分都会被黑色替换。

需要明确的是,我的图像不包含任何半透明像素。它们要么是纯色的,要么是完全透明的。

我有一个简单的 ViewController 设置 Metal 并尝试 blit 2 图像。但是第二张图片的透明部分是黑色的,不会显示下面的第一张图片。

- (void)viewDidLoad
{
    [super viewDidLoad];

    id <MTLDevice> device = MTLCreateSystemDefaultDevice();
    self.commandQueue = [device newCommandQueue];
    MTKTextureLoader *textureLoader = [[MTKTextureLoader alloc] initWithDevice:device];
    self.texture = [textureLoader newTextureWithCGImage:[UIImage imageNamed:@"image1"].CGImage options:nil error:nil];
    self.texture2 = [textureLoader newTextureWithCGImage:[UIImage imageNamed:@"image2"].CGImage options:nil error:nil];

    self.metalView = [[MTKView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) device:device];
    self.metalView.framebufferOnly = NO;
    self.metalView.delegate = self;
    self.metalView.colorPixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
    [self.view addSubview:self.metalView];
}

- (void)drawInMTKView:(MTKView *)view
{
    id<MTLCommandBuffer> commandBuffer = [self.commandQueue commandBuffer];
    id<MTLBlitCommandEncoder> blitEncoder = [commandBuffer blitCommandEncoder];

    [blitEncoder copyFromTexture:self.texture sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(self.texture.width, self.texture.height, self.texture.depth) toTexture:self.metalView.currentDrawable.texture destinationSlice:0 destinationLevel:0 destinationOrigin:MTLOriginMake(0, 0, 0)];
    [blitEncoder copyFromTexture:self.texture2 sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(self.texture2.width, self.texture2.height, self.texture2.depth) toTexture:self.metalView.currentDrawable.texture destinationSlice:0 destinationLevel:0 destinationOrigin:MTLOriginMake(20, 20, 0)];
    [blitEncoder endEncoding];

    [commandBuffer presentDrawable:self.metalView.currentDrawable];
    [commandBuffer commit];
}

任何帮助将不胜感激!

4

0 回答 0