Sign in to follow this  

How to set Alpha value from pixel shader in SlimDX Direct3d9

This topic is 1188 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

I am trying to set alpha value of color as color.a = 0.2 in my pixel shader but it is not showing any effect. If I set color.r, color.g, color.b then they work fine according to the values set in the pixel shader. Simple pixel shader code is given below that I am using
    sampler2D ourImage : register(s0);
    float4 main(float2 locationInSource : TEXCOORD) : COLOR
    {
       float4 color = tex2D( ourImage , locationInSource.xy);
       color.a = 0.2;
       return color;
    }
 
My complete rendering code is as below
 
   

  using System;using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
using SlimDX;
using SlimDX.Direct3D9;
using Point = System.Windows.Point;

namespace AlphaBlendTesting
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
#region Private Variables

private Device _device;
private VertexBuffer _vertexBuffer;
private static VertexDeclaration _vertexDeclaration;
private Texture _texture;
private Surface _renderTarget;

#endregion

public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
InitializeDevice();
InitializeVertices();
ThreadPool.QueueUserWorkItem(delegate
{
RenderEnvironment();
});
}

private void InitializeDevice()
{
var direct3D = new Direct3D();
var windowHandle = new WindowInteropHelper(this).Handle;
var presentParams = new PresentParameters
{
Windowed = true,
BackBufferWidth = (int)SystemParameters.PrimaryScreenWidth,
BackBufferHeight = (int)SystemParameters.PrimaryScreenHeight,
// Enable Z-Buffer
// This is not really needed in this sample but real applications generaly use it
EnableAutoDepthStencil = true,
AutoDepthStencilFormat = Format.D16,
// How to swap backbuffer in front and how many per screen refresh
BackBufferCount = 1,
SwapEffect = SwapEffect.Copy,
BackBufferFormat = direct3D.Adapters[0].CurrentDisplayMode.Format,
PresentationInterval = PresentInterval.Default,
DeviceWindowHandle = windowHandle
};
_device = new Device(direct3D, 0, DeviceType.Hardware, windowHandle, CreateFlags.SoftwareVertexProcessing | CreateFlags.Multithreaded, presentParams);

var shaderByteCode = ShaderBytecode.Compile(File.ReadAllBytes(@"EdgeBlenDing.fx"), "main", "ps_2_0", ShaderFlags.None);
var pixelShader = new PixelShader(_device, shaderByteCode);
_device.PixelShader = pixelShader;

}

private void InitializeVertices()
{
_renderTarget = _device.GetRenderTarget(0);

var vertexBuffer = new VertexBuffer(_device, 6 * Vertex.SizeBytes, Usage.WriteOnly, VertexFormat.Normal, Pool.Managed);
using (DataStream stream = vertexBuffer.Lock(0, 0, LockFlags.None))
{
stream.WriteRange(BuildVertexData());
vertexBuffer.Unlock();
}
_vertexBuffer = vertexBuffer;

//Setting the vertex elements
var vertexElems = new[] 
{
new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position , 0),
new VertexElement(0, 12 , DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
VertexElement.VertexDeclarationEnd
};

//Declaring the vertex
_vertexDeclaration = new VertexDeclaration(_device, vertexElems);

SetRenderState();
_texture = Texture.FromFile(_device, @"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg");
}

private void SetRenderState()
{
// Turn off culling, so we see the front and back of the triangle
_device.SetRenderState(RenderState.CullMode, Cull.None);

// Turn off lighting
_device.SetRenderState(RenderState.Lighting, false);
}

private void RenderEnvironment()
{

while (true)
{

try
{
_device.BeginScene();
_device.Clear(ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);


_device.SetTexture(0, _texture);
_device.SetRenderTarget(0, _renderTarget);




_device.VertexDeclaration = _vertexDeclaration;
_device.SetStreamSource(0, _vertexBuffer, 0, Vertex.SizeBytes);
_device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
_device.EndScene();

//Show what we draw
_device.Present();
}
catch (Exception e)
{

}
}
}


private Vertex[] BuildVertexData()
{

var bottomLeftVertex = new Point(0, 0);
var topLeftVertex = new Point(0, 1);
var bottomRightVertex = new Point(1, 0);
var topRightVertex = new Point(1, 1);

var vertexData = new Vertex[6];

vertexData[0].Position = new Vector3(-1.0f, 1.0f, 0.0f);
vertexData[0].TextureCoordinate = new Vector2((float)bottomLeftVertex.X, (float)bottomLeftVertex.Y);

vertexData[1].Position = new Vector3(-1.0f, -1.0f, 0.0f);
vertexData[1].TextureCoordinate = new Vector2((float)topLeftVertex.X, (float)topLeftVertex.Y);

vertexData[2].Position = new Vector3(1.0f, 1.0f, 0.0f);
vertexData[2].TextureCoordinate = new Vector2((float)bottomRightVertex.X, (float)bottomRightVertex.Y);

vertexData[3].Position = new Vector3(-1.0f, -1.0f, 0.0f);
vertexData[3].TextureCoordinate = new Vector2((float)topLeftVertex.X, (float)topLeftVertex.Y);

vertexData[4].Position = new Vector3(1.0f, -1.0f, 0.0f);
vertexData[4].TextureCoordinate = new Vector2((float)topRightVertex.X, (float)topRightVertex.Y);

vertexData[5].Position = new Vector3(1.0f, 1.0f, 0.0f);
vertexData[5].TextureCoordinate = new Vector2((float)bottomRightVertex.X, (float)bottomRightVertex.Y);

return vertexData;
}

[StructLayout(LayoutKind.Sequential)]
struct Vertex
{
public Vector3 Position;
public Vector2 TextureCoordinate;

public static int SizeBytes
{
get { return Marshal.SizeOf(typeof(Vertex)); }
}
}
}

I am unable to figure out why alpha value is not getting set. Any kind of help is appreciated.

Edited by YashwinderSingh

Share this post


Link to post
Share on other sites

I have tried using all these below stated rendering states before assigning the pixel shader to the device and in the render loop per frame also but even then there is no effect on the alpha value

 

            _device.SetRenderState(RenderState.AlphaBlendEnable, true);
            _device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
            _device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
            _device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.Modulate);
            _device.SetTextureStageState(0, TextureStage.ColorArg1, TextureArgument.Texture);
            _device.SetTextureStageState(0, TextureStage.ColorArg2, TextureArgument.TFactor);

Can any one please tell whats the proper method of setting alpha value for the texture to change its transperancy. 

Edited by YashwinderSingh

Share this post


Link to post
Share on other sites

It looks like the posted code isn't setting any of the alpha blend states, and I think the default is to not blend.

I have tried using all these below stated rendering states before assigning the pixel shader to the device and in the render loop per frame also but even then there is no effect on the alpha value

 

  _device.SetRenderState(RenderState.AlphaBlendEnable, true);
            _device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
            _device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
            _device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.Modulate);
            _device.SetTextureStageState(0, TextureStage.ColorArg1, TextureArgument.Texture);
            _device.SetTextureStageState(0, TextureStage.ColorArg2, TextureArgument.TFactor);

Can any one please tell whats the proper method of setting alpha value for the texture to change its transperancy. 

Share this post


Link to post
Share on other sites

This topic is 1188 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this