Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


YashwinderSingh

Member Since 10 May 2012
Offline Last Active Sep 23 2014 01:57 AM

Topics I've Started

How to set Alpha value from pixel shader in SlimDX Direct3d9

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.


Directshow EVR slow video playback

10 May 2012 - 07:37 AM

I have coded a video player using directshow and EVR filter. I am having a problem of slow playback in a machine with configuration:
Processor: AMD E-350 1.60 GHz Ram: 2 GB
The videos with resolution 1440*1080 or 1920*1080 and above plays fine in windows media player and Media player classic-HC but not the way I am rendering it.

PLayback is smooth in other machines with processors like intel dual core and above.

I am just adding the evr filter to the graph i have made of the video and playing it. Is there any way to improve the playback quality of the video on this machine. any help is appreciated.

PARTNERS