Jump to content
  • Advertisement
Sign in to follow this  

DirectX 11: transparency, alpha from image

This topic is 2831 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, iam trying a simple transparency using a png with alpha channel, but it is not working
with the following code, Nothing appears on the screen, just the background

Here is where iam creating the blending state

D3D11_BLEND_DESC blendDesc;
ZeroMemory(&blendDesc, sizeof(D3D11_BLEND_DESC) );
blendDesc.AlphaToCoverageEnable = false;
blendDesc.IndependentBlendEnable = false;
blendDesc.RenderTarget[0].BlendEnable = true;
blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;

blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO; ///tryed D3D11_BLEND_ONE ... (and others desperate combinations ... )
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; ///tryed D3D11_BLEND_ONE ... (and others desperate combinations ... )
blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL ;
EImediateDeviceContext->OMSetBlendState( state,NULL,0xffffffff);

here is where iam creating the texture (I HAVE ALREADY TRYED WITH A LOADED PNG with alpha BUT did not worked)

//int color = 0xRRGGBBAA;
int color = 0;

color = (RC ) ;
color |= (GC << 8);
color |= (BC << 16);
color |= AC << 24;

#ifdef _DEBUG
char dest[50];
///IT WRITE, the texture contains the alpha

ID3D11Texture2D* Tex;
D3D11_TEXTURE2D_DESC textureDesc;
HRESULT result;

// Initialize the render target texture description.
ZeroMemory(&textureDesc, sizeof(textureDesc));

// Setup the render target texture description.
textureDesc.Width = textureWidth;
textureDesc.Height = textureHeight;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.Usage = D3D11_USAGE_IMMUTABLE;
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;

int* data = (int*) malloc(sizeof(int) * textureWidth * textureHeight);
for( UINT row = 0; row < textureDesc.Height; ++row )
for( UINT col = 0; col < textureDesc.Width; ++col )
data[col + row * textureDesc.Height] = color;

res.SysMemPitch = textureDesc.Height * sizeof(int) ;
res.SysMemSlicePitch = 0;
res.pSysMem = data;

// Create texture.
result = EngineStuff::EGraphicsDevice->CreateTexture2D(&textureDesc,&res, &Tex);

srvDesc.Format = textureDesc.Format;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = textureDesc.MipLevels;
srvDesc.Texture2D.MostDetailedMip = textureDesc.MipLevels -1;

ID3D11ShaderResourceView *pSRView = NULL;

result = EngineStuff::EGraphicsDevice->CreateShaderResourceView( Tex, &srvDesc, &pSRView );
CheckErrorNULL(result) ;

The shader

Texture2D gTexArray : register( t0 );
SamplerState gTriLinearSam : register( s0 );


float4 DrawPS(GS_OUT pIn) : SV_TARGET
float4 f = gTexArray.Sample( gTriLinearSam, pIn.texC ) ;
//f.w = 0.5f;
//return float4(f.w, f.w, f.w,1 -f.w);
return f;

if i set by hand f.w = 0.5f; it ALL WORKS !!!! (off course iam not considering the image alpha)

It appears that the W component is always zero when i read from the Texture2D
i tryed both, loading from file (png that i use in a particle system in XNA) and create the image procedurally but i always get the same ....
as i said, if i just set the w component to 0.3f ... by hand the transparency works .... !!!!
(the x,y,z components are arriving in the shader, the w is not .... )

any help ?

Share this post

Link to post
Share on other sites
i finally got it working.
it was a combination of problems.

first, the lines

[color="#000000"] blendDesc[color="#666600"].[color="#660066"]RenderTarget[color="#666600"][[color="#006666"]0[color="#666600"]].[color="#660066"]SrcBlendAlpha [color="#666600"]=[color="#000000"] D3D11_BLEND_ZERO[color="#666600"]; [color="#000000"]
blendDesc[color="#666600"].[color="#660066"]RenderTarget[color="#666600"][[color="#006666"]0[color="#666600"]].[color="#660066"]DestBlendAlpha [color="#666600"]=[color="#000000"] D3D11_BLEND_ZERO[color="#666600"];

[color="#666600"][color="#000000"][font="arial, verdana, tahoma, sans-serif"][color="#880000"]should be [color="#000000"]D3D11_BLEND_SRC_ALPHA and DEST ....[/font]
[color="#666600"][color="#000000"][font="arial, verdana, tahoma, sans-serif"] [/font]
[color="#666600"][color="#000000"][font="arial, verdana, tahoma, sans-serif"][color="#000000"]# this helped [/font]http://www.gamedev.net/topic/596801-d3d11-alpha-transparency-trouble/
[color="#666600"][color="#000000"][font="arial, verdana, tahoma, sans-serif"] [/font]
[color="#666600"][color="#000000"][font="arial, verdana, tahoma, sans-serif"][color="#000000"]and i had to fix a problem with the generation of my texture coordinates, it was always generating texels int the border of the texture,[/font]
[color="#666600"][color="#000000"][font="arial, verdana, tahoma, sans-serif"][color="#000000"]and the border color was set to a strange value ... (combinations of problems).[/font]
[color="#666600"][color="#000000"][font="arial, verdana, tahoma, sans-serif"] [/font]

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!