# Losing my alpha channel somewhere !

This topic is 2189 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to render a quad (with character sprite from a spritesheet) over the top of my terrain. But I seem to be losing my alpha data, I'm saving my 32bit bitmap out of photoshop, and I've checked that the alpha channel is still there using Pixelformer (which shows alpha clearly)

Here's the shader code that should demonstrate the alpha channel :

 float4 finalColor = gSpriteSheet.Sample(PointSampler, input.texcoord); // ------------------------------------------------------------------------ // final composition // ------------------------------------------------------------------------ return float4(finalColor.a,0,0,1); 

But all I get is red !

What could I be doing wrong ?

Here's my rendertarget construction :

 // create DirectX11 device CreateDevice11(form, out swapChain, out device11); // create depth buffer CreateDepthBuffer(form.ClientSize.Width, form.ClientSize.Height); // create a view of our render target, which is the backbuffer of the swap chain we just created RenderTargetView renderTarget; using (Resource resource = Resource.FromSwapChain<Texture2D>(swapChain, 0)) renderTarget = new RenderTargetView(device11, resource); // setting a viewport is required if you want to actually see anything var context = device11.ImmediateContext; //var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height); context.OutputMerger.SetTargets(renderTarget); 

and here's my device/swapchain construction :

 private static Result CreateDevice11(RenderForm form, out SwapChain swapChain, out Device device11) { var description = new SwapChainDescription() { BufferCount = 2, Usage = Usage.RenderTargetOutput, OutputHandle = form.Handle, IsWindowed = true, ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm), SampleDescription = new SampleDescription(1, 0), Flags = SwapChainFlags.AllowModeSwitch, SwapEffect = SwapEffect.Discard }; return SlimDX.Direct3D11.Device.CreateWithSwapChain(adapter1, DeviceCreationFlags.Debug, description, out device11, out swapChain); } 

##### Share on other sites
return float4(finalColor.a, 0, 0, 1);

this will return red. you should do it like this:

return float4(Red, Green, Blue, Alpha);

if your just trying to return the color of the texel in the sprite sheet with the alpha value, you can just do this:

return finalColor;

if you just want to completely not draw the transparent parts, you can clip, or stop pixels with a certain alpha value from even getting processed like this:

float4 finalColor = gSpriteSheet.Sample(PointSampler, input.texcoord); clip(finalColor.a - .25); return finalColor;

so anything with an alpha value of .25 or less will not be rendered.

##### Share on other sites
return float4(finalColor.a, 0, 0, 1);
this code is just to demonstrate that all alpha values are coming through as 1.0, rather than the mix of 0 & 1 (which the texure contains). In other words, when rendering float4(finalColor.a,0,0,1); I would expect the areas of the texture with alpha=1.0 to be rendered red and the areas with alpha = 0 to be rendered black. But this is not happening, by the time the texture gets to the shader, the whole alpha plane is 1.0.

So when i use return finalColor; I don't get the alpha values that are present in the texture at design time.

##### Share on other sites
ooh, i get what you were trying to do, sorry for not seeing that, haha

have you tried using a different format? like jpeg? I've come across other people having problems with directx, bmp, and alpha channels before. maybe the alpha channel in the bmp is not being recognized, my only suggestion at the moment

also, i don't think you would want to use a format like bmp anyway since it's a larger file than better compressed formats like jpeg or png

##### Share on other sites
Well it looks to me like photoshop can't save png with alpha, there is a warning sign next to alpha channels in the save box. And when i look at the image in Pixelformer, the background is black. I'm so bored with all these alpha channel issues, I don't understand why these issues still exist.

##### Share on other sites
Jpeg doesn't support alpha either out of photoshop.

##### Share on other sites
I just can't believe that Adobe think like this .... to save transparency in png format I had to remove the transparency channel and only save it as an rgb. That is so so counter-intuitive, that i would say it is completely disfunctional reasoning in software design. So very frustrating that some things are like this in software. And why haven't MS fixed the issue with bitmaps not loading alpha ? Do I not understand alpha and it's usage or do these giant companies who make this stuff not understand it ?

Anyway, I have transparency in my quads now, and thanks for introducing me to the clip function iedoc, that's a handly little thing aint it.

##### Share on other sites
yeah that clip function really is nice.

I'm glad to see you got it working. That really is strange your having problems with photoshop exporting the alpha channel. I always use jpg and alpha seems to be fine. Another format you might be interested in is the tiff format, which seems to be a common format for a lot of games. I'm not real experienced though, so i couldn't tell you which one is best ;)

##### Share on other sites
Bitmaps not having alpha is not an issue, it's part of the bitmap specification. If MS were to change that, imagine all the legacy programs that would need to be re-tested. It's not going to happen.

##### Share on other sites
Hi,

you should use DDS format (there is an export plugin from NVidia for Photoshop).
It allows you to control exactly the format of the loaded image. Exactly in the sense that dds format contains the data in the format which is used by the hardware and there is no need to do any (unless the format isn't supported of course) conversion at the load time.

Cheers!

##### Share on other sites
Thank you kauna, It took me no time to test alpha blending with a texture in dds format. I never knew about that exporter. So very helpful.

##### Share on other sites
Sounds like you've solved your problem already by switching to the dds format, but thought I'd mention I had exactly the same problem with png's output from Photoshop losing their Alpha Channel. It looks as though Photoshop converts the png to use pre-multiplied alpha when loading the file. The thing is, the png I was editing was originally exported from Illustrator, another Adobe product, and guess what... It writes the alpha channel in the png properly. Bizarre huh? Not sure why those two products are inconsistent.

I discovered a plugin for Photoshop called SuperPNG which fixes the problem. To load the png, you select "Open As" and then pick the super png format, and the alpha channel is properly imported as a separate channel.