Search code examples
c#texturesslimdxpixel-shaderdirect3d9

Applying a pixel Shader to a texture within a sprite with SlimDX Direct3D9


So, I am very new to Developing with DirectX, I'm slowly getting there... But!

I have this which instantiates the Direct3D9 runtime, creates a device and loads a Pixel Shader. - I can confirm the Pixel shader is loaded as 1) it works in other applications not developed by me. 2) I can break and see that all the various porperties are available and 3) no errors are reported.

Here is the setup.

Direct3DInstance = new SlimDX.Direct3D9.Direct3D();
                SlimDX.Direct3D9.PresentParameters PP = new SlimDX.Direct3D9.PresentParameters();
                PP.Windowed = true;
                PP.BackBufferHeight = PAN_Video.ClientSize.Height;
                PP.BackBufferWidth = PAN_Video.ClientSize.Width;
                PP.BackBufferCount = 0;
                PP.BackBufferFormat = SlimDX.Direct3D9.Format.R5G6B5;
                PP.PresentationInterval = SlimDX.Direct3D9.PresentInterval.Immediate;
                PP.DeviceWindowHandle = PAN_Video.Handle;


                Direct3DDevice = new SlimDX.Direct3D9.Device(Direct3DInstance, 0, SlimDX.Direct3D9.DeviceType.Hardware, PAN_Video.Handle, SlimDX.Direct3D9.CreateFlags.HardwareVertexProcessing, PP);


                string error = null;
                SlimDX.Direct3D9.ShaderBytecode SBC = SlimDX.Direct3D9.ShaderBytecode.Compile(File.ReadAllText(@"C:\Users\Marcus\Desktop\scanlines.txt"), null, null, "main", "ps_3_0", SlimDX.Direct3D9.ShaderFlags.UseLegacyD3DX9_31Dll, out error);

                SlimDX.Direct3D9.PixelShader PS = new SlimDX.Direct3D9.PixelShader(Direct3DDevice, SBC);
                Direct3DDevice.PixelShader = PS;

Here is the Shader - its not mine but one I am using to test with.

// hlslf output by Cg compiler
// cgc version 3.1.0013, build date Apr 18 2012
// command line args: -profile hlslf -po version=110
//vendor NVIDIA Corporation
//version 3.1.0.13
//profile hlslf
//program main_fragment
//semantic main_fragment.s_p : TEXUNIT0
//semantic uIntensity
//var sampler2D s_p : TEXUNIT0 : _s_p 0 : 2 : 1
//var float uIntensity :  : _uIntensity : -1 : 1
//var float2 texcoord : $vin.TEXCOORD0 : TEXCOORD0 : 0 : 1
//var float2 wpos : $vin.WPOS : WPOS : 1 : 1
//var float4 main_fragment : $vout.COLOR : COLOR : -1 : 1
//default uIntensity = 100

#
pragma pack_matrix(row_major)

struct input
{
    float2 _video_size;
    float2 _texture_size;
    float2 _output_size;
};

float _TMP1;
float _TMP0;
uniform float _uIntensity;

// main procedure, the original name was main_fragment
float4 main( in float2 _texcoord: TEXCOORD0, in float2 _wpos: VPOS, uniform sampler2D _s_p: TEXUNIT0): COLOR0
    {

        float4 _temp;

        _temp = tex2D(_s_p, _texcoord);
        _TMP0 = floor(_wpos.y / 2.00000000000000000E000 f);
        _TMP1 = floor(_wpos.y);
        if(_TMP0 != _TMP1 / 2.00000000000000000E000 f)
        { // if begin
            _temp.xyz = _temp.xyz * _uIntensity;
        } // end if
        return _temp;
    } // main end

Now then, I thought the moment you load a Shader it is applied to all rendered output, but obviously I'm wrong.

Below is what happens every 2-5 Milliseconds- all video is rendered out to screen so all the D3D stuff is working, but the loaded pixel shader is not making any difference.

Interface.VideoInfo VI = Interface.EX_GetVideoBuffer();
        Direct3DDevice.Clear(SlimDX.Direct3D9.ClearFlags.Target, Color.Black, 1.0f, 0);

        using (System.Drawing.Bitmap WindowsBMP = new System.Drawing.Bitmap(VI.Width, VI.Height, VI.Stride, System.Drawing.Imaging.PixelFormat.Format16bppRgb565, VI.Buffer))
        {

            BitmapData WindowsBPMData  = WindowsBMP.LockBits(new Rectangle(0, 0, VI.Width, VI.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            byte[] Data = new byte[WindowsBPMData.Stride * WindowsBPMData.Height];

            Marshal.Copy(WindowsBPMData.Scan0, Data, 0, Data.Length);
            WindowsBMP.UnlockBits(WindowsBPMData);

            using (SlimDX.Direct3D9.Texture TEX = new SlimDX.Direct3D9.Texture(Direct3DDevice, WindowsBPMData.Width, WindowsBPMData.Height, 0, SlimDX.Direct3D9.Usage.None, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.Managed))
            {

                DataRectangle DR = TEX.LockRectangle(0, SlimDX.Direct3D9.LockFlags.Discard);
                DR.Data.Position = 0;
                DR.Data.Write(Data, 0, Data.Length);
                DR.Data.Close();
                TEX.UnlockRectangle(0);
                using (SlimDX.Direct3D9.Sprite S = new SlimDX.Direct3D9.Sprite(Direct3DDevice))
                {
                    Direct3DDevice.BeginScene();

                    S.Begin(SlimDX.Direct3D9.SpriteFlags.None);
                    S.Draw(TEX, Color.Transparent);

                    S.End();
                    Direct3DDevice.EndScene();
                    Direct3DDevice.Present(new Rectangle(0, 0, WindowsBPMData.Width, WindowsBPMData.Height), new Rectangle(0, 0, PAN_Video.Width, PAN_Video.Height));
                }
            }

         }

I know I'm missing some stuff - but all examples I have come across deal with rendering wireframes and alike - I'm rendering images from a native Lib and all I have is the Pixel Buffer.

Please be gentle as this DirectX stuff is all very new for me.


Solution

  • I couldn't fulfil my dream in providing shader support in my project (Sega Genesis Emulator)

    I instead used a framework called ReShade, in essence it allows output from a DirectX enabled application to have its render intercepted by a modified driver (d3d9.dll), this hook allows loading precompiled shaders using a varient of HLSL.

    you simply drop the driver in the applications directory, then any calls to DirectX are managed by this driver.

    I had results instantly.

    Resahde Website