Created
January 28, 2010 03:05
-
-
Save chrisforbes/288402 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Runtime.InteropServices; | |
using System.Windows.Forms; | |
using System.Drawing; | |
namespace dxtest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var d3d9 = DX9.Direct3DCreate9(DX9.D3D_SDK_VERSION); | |
var wnd = new Form(); | |
wnd.ClientSize = new Size(640, 480); | |
wnd.Visible = true; | |
wnd.CreateControl(); | |
var presentParams = new PresentParameters | |
{ | |
BackBufferWidth = wnd.ClientSize.Width, | |
BackBufferHeight = wnd.ClientSize.Height, | |
BackBufferCount = 2, | |
BackBufferFormat = SurfaceFormat.X8R8G8B8, | |
EnableAutoDepthStencil = 1, | |
AutoDepthStencilFormat = SurfaceFormat.D24X8, | |
SwapEffect = SwapEffect.Discard, | |
Windowed = 1, | |
PresentInterval = PresentInterval.Immediate, | |
}; | |
var device = d3d9.CreateDevice(DX9.D3DADAPTER_DEFAULT, DeviceType.HAL, wnd.Handle, BehaviorFlags.Pure | BehaviorFlags.HardwareVP, | |
ref presentParams); | |
IDirect3DTexture9 tex; | |
device.CreateTexture(256, 256, 1, 0, SurfaceFormat.A8R8G8B8, Pool.Managed, out tex, IntPtr.Zero); | |
if (tex == null) | |
throw new InvalidOperationException("Can't create textures!"); | |
while (wnd.Created && wnd.Visible) | |
{ | |
Application.DoEvents(); | |
device.Clear(0, IntPtr.Zero, ClearFlags.Color | ClearFlags.Depth, Color.Blue.ToArgb(), 1f, 0); | |
device.Present(0, 0, 0, 0); | |
} | |
} | |
} | |
static class DX9 | |
{ | |
public const int D3D_SDK_VERSION = 32; | |
public const int D3DADAPTER_DEFAULT = 0; | |
[DllImport("d3d9")] | |
public static extern IDirect3D9 Direct3DCreate9(int version); | |
[DllImport("d3dx9_32")] | |
public static extern void D3DXCreateEffectPool(out ID3DXEffectPool p); | |
} | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("81BDCBCA-64D4-426d-AE8D-AD0147F4275C")] | |
interface IDirect3D9 | |
{ | |
/* dummy funcs, just to pad out the vtable */ | |
void _RegisterSoftwareDevice(); | |
void _GetAdapterCount(); | |
void _GetAdapterIdentifier(); | |
void _GetAdapterModeCount(); | |
void _EnumAdapterModes(); | |
void _GetAdapterDisplayMode(); | |
void _CheckDeviceType(); | |
void _CheckDeviceFormat(); | |
void _CheckDeviceMultiSampleType(); | |
void _CheckDepthStencilMatch(); | |
void _CheckDeviceFormatConversion(); | |
void _GetDeviceCaps(); | |
void _GetAdapterMonitor(); | |
/* the ONE function we're interested in */ | |
IDirect3DDevice9 CreateDevice(int adapter, DeviceType devtype, | |
IntPtr focusWindow, | |
BehaviorFlags flags, | |
[In] ref PresentParameters pp); | |
} | |
enum DeviceType { HAL = 1, REF = 2, SW = 3, NULLREF = 4 }; | |
enum BehaviorFlags { Pure = 0x10, SoftwareVP = 0x20, HardwareVP = 0x40, MixedVP = 0x80 }; | |
struct PresentParameters | |
{ | |
public int BackBufferWidth; | |
public int BackBufferHeight; | |
public SurfaceFormat BackBufferFormat; | |
public int BackBufferCount; | |
public int MultisampleType; | |
public int MultisampleQuality; | |
public SwapEffect SwapEffect; | |
public IntPtr DeviceWindow; | |
public int Windowed; | |
public int EnableAutoDepthStencil; | |
public SurfaceFormat AutoDepthStencilFormat; | |
public PresentFlags Flags; | |
public int RefreshRate; | |
public PresentInterval PresentInterval; | |
} | |
enum SurfaceFormat { A8R8G8B8 = 21, X8R8G8B8 = 22, D24X8 = 77, D24S8 = 75 } | |
enum SwapEffect { Discard = 1, Flip = 2, Copy = 3 } | |
enum Pool { Default = 0, Managed = 1, SystemMemory = 2, Scratch = 3 } | |
[Flags] | |
enum PresentFlags { } | |
enum PresentInterval : uint { One = 1, Immediate = 0x80000000u } | |
[Flags] | |
enum UsageFlags { } | |
[Flags] | |
enum VertexFormat { Position = 0x2, Normal = 0x10, Texture1 = 0x100, Texture2 = 0x200 } | |
[Flags] | |
enum ClearFlags { Color = 1, Depth = 2, Stencil = 4 } | |
enum PrimitiveType { PointList = 1, TriangleList = 4, LineList = 2 } | |
[Flags] | |
enum LockFlags { } | |
enum RenderState { ScissorTestEnable = 174 } | |
struct LockedRect | |
{ | |
public int Pitch; | |
public IntPtr Bits; | |
} | |
struct Rect | |
{ | |
public int Left, Top, Right, Bottom; | |
} | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("D0223B96-BF7A-43fd-92BD-A43B0D82B9EB")] | |
interface IDirect3DDevice9 | |
{ | |
void _TestCooperativeLevel(); | |
void _GetAvailableTextureMem(); | |
void _EvictManagedResources(); | |
void _GetDirect3D(); | |
void _GetDeviceCaps(); | |
void _GetDisplayMode(); | |
void _GetCreationParameters(); | |
void _SetCursorProperties(); | |
void _SetCursorPosition(); | |
void _ShowCursor(); | |
void _CreateAdditionalSwapChain(); | |
void _GetSwapChain(); | |
void _GetNumberOfSwapChains(); | |
void _Reset(); | |
void Present(int _1, int _2, int _3, int _4); | |
void _GetBackBuffer(); | |
void _GetRasterStatus(); | |
void _SetDialogBoxMode(); | |
void _SetGammaRamp(); | |
void _GetGammaRamp(); | |
void CreateTexture( int width, int height, int levels, UsageFlags usage, SurfaceFormat format, Pool pool, out IDirect3DTexture9 tex, IntPtr sharedHandle ); | |
void _CreateCubeTexture(); | |
void _CreateVolumeTexture(); | |
void CreateVertexBuffer(int length, UsageFlags usage, VertexFormat fvf, Pool pool, out IDirect3DVertexBuffer9 vb, IntPtr sharedHandle); | |
void CreateIndexBuffer(int length, UsageFlags usage, SurfaceFormat format, Pool pool, out IDirect3DIndexBuffer9 ib, IntPtr sharedHandle); | |
void _CreateRenderTarget(); | |
void _CreateDepthStencilSurface(); | |
void _UpdateSurface(); | |
void _UpdateTexture(); | |
void _GetRenderTargetData(); | |
void _GetFrontBufferData(); | |
void _StretchRect(); | |
void _ColorFill(); | |
void _CreateOffscreenPlainSurface(); | |
void _SetRenderTarget(); | |
void _GetRenderTarget(); | |
void _SetDepthStencilSurface(); | |
void _GetDepthStencilSurface(); | |
void BeginScene(); | |
void EndScene(); | |
void Clear(int sbz, IntPtr sbz2, ClearFlags flags, int color, float z, int stencil); | |
void _SetTransform(); | |
void _GetTransform(); | |
void _MultiplyTransform(); | |
void _SetViewport(); | |
void _GetViewport(); | |
void _SetMaterial(); | |
void _GetMaterial(); | |
void _SetLight(); | |
void _GetLight(); | |
void _LightEnable(); | |
void _GetLightEnable(); | |
void _SetClipPlane(); | |
void _GetClipPlane(); | |
void SetRenderState(RenderState rs, int value); | |
void _GetRenderState(); | |
void _CreateStateBlock(); | |
void _BeginStateBlock(); | |
void _EndStateBlock(); | |
void _SetClipStatus(); | |
void _GetClipStatus(); | |
void _GetTexture(); | |
void _SetTexture(); | |
void _GetTextureStageState(); | |
void _SetTextureStageState(); | |
void _GetSamplerState(); | |
void _SetSamplerState(); | |
void _ValidateDevice(); | |
void _SetPaletteEntries(); | |
void _GetPaletteEntries(); | |
void _SetCurrentTexturePalette(); | |
void _GetCurrentTexturePalette(); | |
void SetScissorRect([In] ref Rect rect); | |
void _GetScissorRect(); | |
void _SetSoftwareVertexProcessing(); | |
void _GetSoftwareVertexProcessing(); | |
void _SetNPatchMode(); | |
void _GetNPatchMode(); | |
void DrawPrimitive(PrimitiveType pt, int startVertex, int primCount); | |
void DrawIndexedPrimitive(PrimitiveType pt, int baseVertexIndex, int minVertexIndex, int numVertices, int startVertex, int primCount); | |
void _DrawPrimitiveUP(); | |
void _DrawIndexedPrimitiveUP(); | |
void _ProcessVertices(); | |
void _CreateVertexDeclaration(); | |
void _SetVertexDeclaration(); | |
void _GetVertexDeclaration(); | |
void SetFVF(VertexFormat fvf); | |
void _GetFVF(); | |
void _CreateVertexShader(); | |
void _SetVertexShader(); | |
void _GetVertexShader(); | |
void _SetVertexShaderConstantF(); | |
void _GetVertexShaderConstantF(); | |
void _SetVertexShaderConstantI(); | |
void _GetVertexShaderConstantI(); | |
void _SetVertexShaderConstantB(); | |
void _GetVertexShaderConstantB(); | |
void SetStreamSource(int stream, IDirect3DVertexBuffer9 vb, int offset, int stride); | |
void _GetStreamSource(); | |
void _SetStreamSourceFreq(); | |
void _GetStreamSourceFreq(); | |
void SetIndices(IDirect3DIndexBuffer9 ib); | |
/* and the rest of this vtable is boring... */ | |
} | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("05EEC05D-8F7D-4362-B999-D1BAF357C704")] | |
interface IDirect3DResource9 | |
{ | |
/* *nothing* in this interface is any use, but we need to use up the vtable slots. */ | |
void _GetDevice(); | |
void _SetPrivateData(); | |
void _GetPrivateData(); | |
void _FreePrivateData(); | |
void _SetPriority(); | |
void _GetPriority(); | |
void _PreLoad(); | |
void _GetType(); | |
} | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("85C31227-3DE5-4f00-9B3A-F11AC38C18B5")] | |
interface IDirect3DTexture9 : IDirect3DResource9 | |
{ | |
void _SetLOD(); | |
void _GetLOD(); | |
void _GetLevelCount(); | |
void _SetAutoGenFilterType(); | |
void _GetAutoGenFilterType(); | |
void _GenerateMipSubLevels(); | |
void _GetLevelDesc(); | |
void _GetSurfaceLevel(); | |
/* only two functions we care about */ | |
void LockRect(int level, ref LockedRect lockedRect, [In] ref Rect rect, int flags); | |
void UnlockRect(int level); | |
/* and the rest of this vtable is boring... */ | |
} | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("B64BB1B5-FD70-4df6-BF91-19D0A12455E3")] | |
interface IDirect3DVertexBuffer9 : IDirect3DResource9 | |
{ | |
void Lock(int offset, int size, out IntPtr bits, LockFlags flags); | |
void Unlock(); | |
/* the rest is boring */ | |
} | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("7C9DD65E-D3F7-4529-ACEE-785830ACDE35")] | |
interface IDirect3DIndexBuffer9 : IDirect3DResource9 | |
{ | |
void Lock(int offset, int size, out IntPtr bits, LockFlags flags); | |
void Unlock(); | |
/* the rest is boring */ | |
} | |
/* from here on, it's D3DX crap; if you use it, you need d3dx9_32.dll */ | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("9537AB04-3250-412e-8213-FCD2F8677933")] | |
interface ID3DXEffectPool { } | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("8BA5FB08-5195-40e2-AC58-0D989C3A0102")] | |
interface ID3DXBuffer | |
{ | |
IntPtr GetBufferPointer(); | |
int GetBufferSize(); | |
} | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("F6CEB4B3-4E4C-40dd-B883-8D8DE5EA0CD5")] | |
interface ID3DXEffect | |
{ | |
void _GetDesc(); | |
void _GetParameterDesc(); | |
void _GetTechniqueDesc(); | |
void _GetPassDesc(); | |
void _GetFunctionDesc(); | |
void _GetParameter(); | |
IntPtr GetParameterByName(IntPtr parent, string name); | |
void _GetParameterBySemantic(); | |
void _GetParameterElement(); | |
void _GetTechnique(); | |
IntPtr GetTechniqueByName(string name); | |
void _GetPass(); | |
void _GetPassByName(); | |
void _GetFunction(); | |
void _GetFunctionByName(); | |
void _GetAnnotation(); | |
void _GetAnnotationByName(); | |
void SetValue(IntPtr parameter, IntPtr data, int size); | |
void _GetValue(); | |
void _SetBool(); | |
void _GetBool(); | |
void _SetBoolArray(); | |
void _GetBoolArray(); | |
void _SetInt(); | |
void _GetInt(); | |
void _SetIntArray(); | |
void _GetIntArray(); | |
void _SetFloat(); | |
void _GetFloat(); | |
void _SetFloatArray(); | |
void _GetFloatArray(); | |
void _SetVector(); | |
void _GetVector(); | |
void _SetVectorArray(); | |
void _GetVectorArray(); | |
void _SetMatrix(); | |
void _GetMatrix(); | |
void _SetMatrixArray(); | |
void _GetMatrixArray(); | |
void _SetMatrixPointerArray(); | |
void _GetMatrixPointerArray(); | |
void _SetMatrixTranspose(); | |
void _GetMatrixTranspose(); | |
void _SetMatrixTransposeArray(); | |
void _GetMatrixTransposeArray(); | |
void _SetMatrixTransposePointerArray(); | |
void _GetMatrixTransposePointerArray(); | |
void _SetString(); | |
void _GetString(); | |
void SetTexture(IntPtr parameter, IDirect3DTexture9 tex); | |
void _GetTexture(); | |
void _GetPixelShader(); | |
void _GetVertexShader(); | |
void _SetArrayRange(); | |
void _GetPool(); | |
void SetTechnique(IntPtr technique); | |
void _GetCurrentTechnique(); | |
void _ValidateTechnique(); | |
void _FindNextValidTechnique(); | |
void _IsParameterUsed(); | |
void Begin( out int passes, EffectFlags flags ); | |
void BeginPass( int pass ); | |
void CommitChanges(); | |
void EndPass(); | |
void End(); | |
/* rest of the vtable sucks */ | |
} | |
enum EffectFlags { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment