Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


How to set Alpha value from pixel shader in SlimDX Direct3d9


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 YashwinderSingh   Members   -  Reputation: 115

Like
0Likes
Like

Posted 25 August 2014 - 04:30 AM

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, 01 September 2014 - 02:27 AM.


Sponsor:

#2 Buckeye   Crossbones+   -  Reputation: 6148

Like
0Likes
Like

Posted 25 August 2014 - 05:07 AM

Can you post the actual section of code where you get  the exception, including which line results in the exception?


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#3 YashwinderSingh   Members   -  Reputation: 115

Like
0Likes
Like

Posted 01 September 2014 - 02:31 AM

Can you post the actual section of code where you get  the exception, including which line results in the exception?

I have created a sample code for the same problem I was facing to set alpha and have given the complete code above



#4 Hodgman   Moderators   -  Reputation: 31781

Like
0Likes
Like

Posted 01 September 2014 - 05:43 AM

Are you trying to make it 20% transparent? What blend state is active before the draw call?

#5 Adam_42   Crossbones+   -  Reputation: 2615

Like
0Likes
Like

Posted 01 September 2014 - 04:55 PM

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



#6 YashwinderSingh   Members   -  Reputation: 115

Like
0Likes
Like

Posted 08 September 2014 - 01:59 AM

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, 08 September 2014 - 02:08 AM.


#7 YashwinderSingh   Members   -  Reputation: 115

Like
0Likes
Like

Posted 10 September 2014 - 07:41 AM

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. 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS