Obeone
Obeone

Reputation: 1

MTLTextureDescriptor conundrum

I trying to convert the "Metal By Example" App -TextRendering- for IOS to OSX. Unfortunately, Xcode tells me that the MTLTextureDescriptor's storageMode needs to be set to MTLStorageModePrivate and when I do: replaceRegion:mipmapLevel:withBytes:bytesPerRow: throws "failed assertion `CPU access for textures with MTLResourceStorageModePrivate storage mode is disallowed."

MTLTextureDescriptor *textureDesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:AAPLDepthPixelFormat
            width:MBEFontAtlasSize
            height:MBEFontAtlasSize
            mipmapped:NO];
            
        textureDesc.storageMode = MTLStorageModePrivate;
        textureDesc.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
        textureDesc.usage = MTLTextureUsageShaderRead;//MTLTextureUsageRenderTarget;
    
        MTLRegion region = MTLRegionMake2D(0, 0, MBEFontAtlasSize, MBEFontAtlasSize);
        _fontTexture = [_device newTextureWithDescriptor:textureDesc];
        [_fontTexture setLabel:@"Font Atlas"];
        [_fontTexture replaceRegion:region mipmapLevel:0 withBytes:_fontAtlas.textureData.bytes bytesPerRow:MBEFontAtlasSize];

Any help would be tremendously appreciated !

Upvotes: 0

Views: 385

Answers (1)

Frank Rupprecht
Frank Rupprecht

Reputation: 10383

On macOS, you actually have to explicitly synchronize resources between CPU/RAM and GPU (because the Mac might have a dedicated GPU with its own memory, in contrast to the shared memory model on iOS).

For that, you need to set the storage mode to managed and use a MTLBlitCommandEncoder to copy memory between the devices (see the documentation of managed).

Upvotes: 1

Related Questions