Jump to content
  • Advertisement
Sign in to follow this  

BlendState in an Effect

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys


Just got into using the FX style of shaders. I would like to know how do I set up the BlendState object so that a Alpha of 0 is transparent.

Any ideas?




BlendState SrcAlphaBlendingAdd 
	BlendEnable[0] = TRUE; 
	SrcBlend = SRC_COLOR; 
	DestBlend = INV_SRC_COLOR; 
	BlendOp = ADD; 
	SrcBlendAlpha = ZERO; 
	DestBlendAlpha = ZERO; 
	BlendOpAlpha = ADD; 
	RenderTargetWriteMask[0] = 0x0F; 
Edited by Naruto-kun

Share this post

Link to post
Share on other sites

So with blending you have two colors: SrcColor, which is the color output by your pixel shader, and DstColor, which is the color that's currently in the render target. The equation goes like this:


FinalColor.rgb = SrcColor.rgb * SrcBlend [BlendOp] DstColor.rgb * DstBlend;
FinalColor.a = SrcColor.a * SrcBlendAlpha [BlendOpAlpha] DstColor.a * DstBlendAlpha;


So with the settings you've specified, you're doing this:


FinalColor.rgb = SrcColor.rgb * SrcColor.rgb +  DstColor.rgb * (1.0f - DstColor.rgb);
FinalColor.a = SrcColor.a * 0 + DstColor.a * 0;


This is clearly not what you want. Instead you want to multiply the SrcColor by your source alpha value, and your DstColor by the inverse of your source alpha:


FinalColor.rgb = SrcColor.rgb * SrcColor.a +  DstColor.rgb * (1.0f - DstColor.a);


You can achieve this by setting SrcBlend = SRC_ALPHA, and DestBlend = INV_SRC_ALPHA.

Share this post

Link to post
Share on other sites

Thanks. Figured it would be that way. However for some reason, the alpha component in my texture is not being respected. The texture is a 32bit BMP. All areas that are transparent have an alpha of 0.


Here is what the pixel shader looks like

float4 PS( PS_INPUT input ) : SV_Target
	float4 color = Map.Sample(linearSampler, input.t);
	color.a = color.a*input.opacity;
	return color;      

input.opacity is a little extra that I use to change transparency even further when I need to.


Any ideas?

Share this post

Link to post
Share on other sites

Hmm...perhaps your texture loader isn't handling the alpha correctly for bitmaps. Is it possible to try saving the texture as .png instead, and see if that works?

Share this post

Link to post
Share on other sites

Here is how I load the texture.

		ZeroMemory(&rtd, sizeof(rtd));
		rtd.Width = 1024;
		rtd.Height = 1024;
		rtd.MipLevels = 1;
		rtd.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
		rtd.Usage = D3D11_USAGE_DEFAULT;
		rtd.BindFlags = D3D11_BIND_SHADER_RESOURCE;
		rtd.CpuAccessFlags = D3DX11_DEFAULT;
		rtd.MiscFlags = D3DX11_DEFAULT;
		rtd.Depth = D3DX11_DEFAULT;
		rtd.FirstMipLevel = D3DX11_DEFAULT;
		rtd.Filter = D3DX11_DEFAULT;
		rtd.MipFilter = D3DX11_DEFAULT;
		rtd.pSrcInfo = NULL;
		HRSRC Srsrc = FindResource(modinst, MAKEINTRESOURCE(200), MAKEINTRESOURCE(10));
		//DWORD temp = GetLastError();
		HGLOBAL Simg = LoadResource(modinst, Srsrc);

		DWORD size = SizeofResource(modinst, Srsrc);
		void* buf= LockResource(Simg);
		hr = D3DX11CreateShaderResourceViewFromMemory(dev, buf, size, &rtd, NULL, &srv, NULL);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!