2

First off, I am a complete DirectX noob. I'm loading a bitmap into a surface and need to apply an HLSL effect to it prior to display. The goal is to display real-time effects on the image. For whatever reason, the image displays but without the effect.

I think it might have to do with the fact I'm stretching the surface to the backbuffer and that circumvents the effect but I can't figure out how to resolve it. Ultimately, this surface will contain a stereo image pair plus Nvidia's stereo image signature so that it works with 3D Vision. While that's immaterial to this question, it might explain why I'm using a surface when some other method may otherwise be better.

Here's my code:

In the form:

public partial class frmMain : Form
{
    private Device device;
    private Effect effect = null;
    private Surface ImageSurface;
    private Rectangle ImageSize;

    public frmMain()
    {
        InitializeComponent();
    }

    public bool InitializeGraphics()
    {
        //Get Bitmap
        Bitmap MyImage = (Bitmap)Bitmap.FromFile(@"C:\test\OD_1_L.jpg");
        ImageSize = new Rectangle(0, 0, MyImage.Width, MyImage.Height);

        //Set Presentation Parameters
        PresentParameters presentParams = new PresentParameters();
        presentParams.Windowed = true;
        presentParams.SwapEffect = SwapEffect.Discard;
        presentParams.AutoDepthStencilFormat = DepthFormat.D16;
        presentParams.EnableAutoDepthStencil = true;
        presentParams.BackBufferFormat = Format.A8R8G8B8;
        presentParams.BackBufferCount = 1;
        presentParams.BackBufferWidth = MyImage.Width;
        presentParams.BackBufferHeight = MyImage.Height;
        presentParams.PresentationInterval = PresentInterval.One;


        bool canDoShaders = true;

        //Determine whether or not the hardware can support shaders...
        Caps hardware = Manager.GetDeviceCaps(0, DeviceType.Hardware);

        if (hardware.VertexShaderVersion >= new Version(1, 1))
        {
            //Default to software processing
            CreateFlags flags = CreateFlags.SoftwareVertexProcessing;

            //Use hardware if it's available
            if (hardware.DeviceCaps.SupportsHardwareTransformAndLight)
            {
                flags = CreateFlags.HardwareVertexProcessing;
            }

            //Use pure if it's available
            if (hardware.DeviceCaps.SupportsPureDevice)
            {
                flags |= CreateFlags.PureDevice;
            }

            //Create Device
            device = new Device(0, DeviceType.Hardware, this, flags, presentParams);

            //Create effect
            effect = Effect.FromFile(device, @"..\..\invert.fx", null, ShaderFlags.None, null);
            effect.Technique = "InvertTechnique";

        }
        else
        {
            //No Shader Support
            canDoShaders = false;

            //Create a reference device
            device = new Device(0, DeviceType.Reference, this, CreateFlags.SoftwareVertexProcessing, presentParams);
        }

        //Create Image Surface...            
        ImageSurface = Surface.FromBitmap(device, MyImage, Pool.Default);

        return canDoShaders;
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        //do nothing
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0);

        Surface backbuffer = device.GetBackBuffer(0, 0, BackBufferType.Mono);

        device.BeginScene();
        int numPasses = effect.Begin(0);
        for (int i = 0; i < numPasses; i++)
        {
            effect.BeginPass(i);
            device.StretchRectangle(ImageSurface, ImageSize, backbuffer, ImageSize, TextureFilter.None);
            effect.EndPass();
        }
        effect.End();
        device.EndScene();
        device.Present();

        this.Invalidate();
    }
}

In the shader:

sampler2D input : register(s0); 
float inversionAmount : register(c0);

float4 Invert(float2 uv : TEXCOORD) : COLOR { 
float4 color;
color = tex2D(input, uv.xy); 

//this changes inversion
float4 inverted_color =  1 - color;
inverted_color.a = color.a;
inverted_color.rgb *= inverted_color.a;
return inverted_color;
}

technique InvertTechnique
{
  pass P0
  {
    CullMode = None;

    // shaders
    VertexShader = null;
    PixelShader  = compile ps_1_1 Invert();
  }
}
magnvs
  • 63
  • 1
  • 9
  • Why managed DirectX? It's old and deprecated. Why not XNA or SlimDX? – Cameron Aug 16 '12 at 19:59
  • Managed DirectX is the only platform i've had any success in getting NVidia 3D Vision to work. I haven't tried XNA but couldn't get SlimDX to do what I needed to do. – magnvs Aug 17 '12 at 13:16

0 Answers0