In my Metal game, I have noticed that none of the textures have any transparent pixels, yet the alpha channel still exists. They are all redundantly filled with 0xFF
bytes.
let TextureDescriptor = MTLTextureDescriptor()
TextureDescriptor.pixelFormat = View.colorPixelFormat
TextureDescriptor.width = 16
TextureDescriptor.height = 16
TextureDescriptor.mipmapLevelCount = 5
TextureDescriptor.textureType = MTLTextureType.type2DArray
TextureDescriptor.arrayLength = 22
TerrainTextures = Device.makeTexture(descriptor: TextureDescriptor)
// Load Textures
View
is a global variable defined as as the NSViewController
view
cast to MTKView
. If I try anything other than view.colorPixelFormat
, I get issues such as discolored textures, or even nothing at all being rendered. But this happens to be bgra8Unorm_srgb
, which includes alpha. I would like to cut out one quarter of texture memory by simply dropping the alpha channel for textures that do not have it. But how do I do that? How can I specify an RGB format with 3 bytes per pixel without an alpha? Then I can make the fragment shader to give an alpha channel instead of the texture coming with one? I took a look here and there seem to be no 24 bit formats, so is there some way to do it? Can I somehow specify a custom format instead of one of the default formats? Or will I have to simulate this by some stretch such as having 3 textures per texture, because I would be using 1 8 bit per pixel texture for each of the three RGB channels of the each complete texture?
To answer your questions:
How can I specify an RGB format with 3 bytes per pixel without an alpha?
There are no texture formats that fit your description. There are packed 32-bit formats that make use of greater precision, but there are no formats that have 8 bits per channel and fit into 24 bit, because these 24 bit formats don't fit well with modern hardware anyway.
Can I somehow specify a custom format instead of one of the default formats?
Not really, textures are handled by a hardware unit called TPU and you can't really come up with your own texture format
I would be using 1 8 bit per pixel texture for each of the three RGB channels of the each complete texture?
I would strongly recommend against that, because there is actually a texture cache and if you are sampling three different textures at the same texture coordinates, it doesn't know how to correlate them and you will end up with a performance hit.
Instead, you have three options.
First option is kinda hard to do cause you need a lot of infrastructure. You can either use compressed formats like ASTC or BCn. To learn more about that, there's an upcomming talk in this years WWDC2021 called Discover Metal debugging, profiling, and asset creation tools that will post on Wednesday.
Second option is to use a spare channel for some useful information, like occlusion, or some sort of mask, or anything domain-specific to your game.
Third option is just to use 32-bit formats with 8 bits per channel and just ignore the alpha or populate it with 0 or 1 as you wish. This is the easiest option and I would recommend going with it before advancing to other options.