写入MTLTexture会导致致命错误

问题描述 投票:0回答:1

给出MTLTexture,定义如下。

// Create device.
id<MTLDevice> dev = MTLCreateDefaultSystemDevice();

// Size of texture.
const unsigned int W = 640;
const unsigned int H = 480;

// Define texture.
MTLTextureDescriptor *desc = [[MTLTextureDescriptor alloc] init];
desc.pixelFormat = MTLPixelFormatBGRA8Unorm;
desc.width = W;
desc.height = H;

// Create texture.
id<MTLTexture> tex = [device newTextureWithDescriptor:desc];

据我了解,这时我应该具有在设备desc上分配并可以通过dev访问的tex中定义的纹理。

现在,给定另一个纹理tex2(已知是可以分配和访问的)和一个定义如下的Metal计算内核。

kernel void foo(texture2d<float, access::read> in [[texture(0)]],
                texture2d<float, access::write> out [[texture(1)]],
                uint2 pix [[thread_position_in_grid]]) {
    // Out of bounds check.
    if (pix.x >= out.get_width() || pix.y >= out.get_height()) {
        return;
    }

    // Do some processing on the input texture.
    // ... All fine up to here.

    // Write out a pixel to the output buffer.
    const float4 p = abc; // abc is computed above.
    out.write(p, pix);
}

据我了解,当将像素p写入out时,p的值将转换为符合tex的像素格式,在这种情况下为MTLPixelFormatBGRA8Unorm

但是,当按如下方式启动内核时,将p写入out(定义为tex)的行会触发严重错误(SIGABRT)。

// Create a Metal library.
id<MTLLibrary> lib = [dev newDefaultLibrary];

// Load the kernel.
id<MTLFunction> kernel = [lib newFunctionWithName:@"foo"];

// Create a pipeline state.
id<MTLComputePipelineState> pipelineState = [dev newComputePipelineStateWithFunction:kernel error:NULL];

// Create a command queue.
id<MTLCommandQueue> cmdQueue = [dev newCommandQueue];

// Create command buffer.
id<MTLCommandBuffer> cmdBuff = [cmdQueue commandBuffer];

// Create compute encoder.
id<MTLComputeCommandEncoder> enc = [cmdBuff computeCommandEncoder];

// Set the pipeline state.
[enc setComputePipelineState:pipelineState];

// Set the input textures (tex2 is read only in the kernel, as above).
[enc setTexture:tex2 atIndex:0];
[enc setTexture:tex atIndex:1];

// 2D launch configuration.
const MTLSize groupDim = MTLSizeMake(16, 16, 1);
const MTLSize gridDim = MTLSizeMake((int)ceil((float)(W / (float)groupDim.width)),
                                    (int)ceil((float)(H / (float)groupDim.height)),
                                    1);

// Launch kernel.
[enc dispatchThreadgroups:gridDim threadsPerThreadgroup:groupDim];
[enc endEncoding];
[enc commit];
[cmdBuff waitUntilCompleted];

我的问题是,在上述情况下,我对如何分配MTLTexture的理解正确吗?还是上面的示例仅在需要单独分配的某些纹理周围定义了wrapper

ios objective-c textures metal compute-shader
1个回答
0
投票

以上纹理分配和计算内核启动是正确的。在进一步阅读文档时,缺少的部分是usageMTLTextureDescriptor属性。在documentation中,说明如下。

此属性的默认值为MTLTextureUsageShaderRead。

因此,在问题给出的示例中,需要在MTLTextureDescriptor上进行以下附加属性分配。

desc.usage = MTLTextureUsageShaderWrite;
© www.soinside.com 2019 - 2024. All rights reserved.