Alpha Blending with SharpDX/DirectX11/WP8 using feature level 9_3

Started by
3 comments, last by Laurie Brown 11 years, 5 months ago
I'm using SharpDX on the Windows Phone 8 and therefore although it uses DirectX11 it is feature level 9_3. This means certain things won't work on the device but will work on the emulator. I have a shader working on the emulator that uses alpha blending but I can't get it working on the phone at all. Basically I am drawing to a render target without clearing it and then drawing that to another one and multiplying the alpha by 0.95 to slowly fade it out over time. On the emulator this creates a nice fading trail behind a moving object. On the phone however the trail is just always the same colour and doesn't fade.

I am assuming this has something to do with feature level 9_3 but I can't figure out what it is I am doing that is wrong.

Here's the pixel shader:


[source lang="cpp"]Texture2D<float4> shaderTexture : register(t0);

sampler TextureSampler =
sampler_state
{
Texture = <shaderTexture>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Mirror;
AddressV = Mirror;
};

struct PSInput
{
float4 ScreenPosition : SV_POSITION;
float2 UV : TEXCOORD;
};

float4 main( PSInput input ) : SV_TARGET
{
float4 textureColor = shaderTexture.Sample(TextureSampler, input.UV);

textureColor.a *= 0.95f;
return textureColor;
}[/source]

Thanks for any suggestions.
Advertisement
Not strictly related, but I'm surprised by your shader, as the declared sampler TextureSampler won't be used in WP8. This was part of old Effect framework but this is not working under WP8. Compilation is probably running fine on your shader, but the TextureSampler must be setup programmaticaly from the code and not from the shader (and if you didn't set it, it is set to null by default, not sure about the default behavior of a texture sampler when it is not bound).
Hmm maybe that's the problem then. I am really having trouble switching to DX11 and WP8. It seems like finding the right info out there is incredibly hard. All the tutorials are for DX9. Thanks once again for the help Xoofx, you're really saving me here.

I'll edit with an update after I've rewritten it to pass the samplerstate in from code.

Edit:
Creating a SamplerState causes a SharpDXException for some reason. The device is already set and creating the BlendState works fine:

[source lang="csharp"]var samplerStateDesc = new SamplerStateDescription();
samplerStateDesc.Filter = Filter.MinMagMipLinear;
samplerStateDesc.AddressU = TextureAddressMode.Mirror;
samplerStateDesc.AddressV = TextureAddressMode.Mirror;
samplerStateDesc.ComparisonFunction = Comparison.Never;
samplerStateDesc.MinimumLod = 0;
samplerStateDesc.MaximumLod = int.MaxValue;
_samplerState = new SamplerState(_device, ref samplerStateDesc);[/source]
SamplerStateDescription needs to have the exact same default values as stated in the msdn documentation, otherwise It will failed to create (this is relevant for all states).
As there are no custom default values for struct in C# (yeah, that sucks), you need to use SamplerStateDescription.Default() like this:

var samplerStateDesc = SamplerStateDescription.Default();
samplerStateDesc.AddressU = TextureAddressMode.Mirror;
samplerStateDesc.AddressV = TextureAddressMode.Mirror;
_samplerState = new SamplerState(_device, ref samplerStateDesc);


Btw, you should try to use SharpDX Toolkit as it should simplify a *lot* managing D3D11.
Ah thanks, yes that is annoying about not having default values for structs.

This didn't change the alpha blending on the device unfortunately, it is still either off or on. If you multiply in the shader by anything below 0.5f it is off and anything above it is on, it isn't blending at all. I also changed the blendStateDesc to use BlendStateDescription.Default() but that didn't change anything.

Yes I realise I should have started fresh with the Toolkit but I had already started the code in XNA and then C++ so this project has been moved about a lot. All just for 2D rendering, it's pretty frustrating. Next time I will definitely use Toolkit, it looks a lot more like XNA, which is nice.

This topic is closed to new replies.

Advertisement