Add Direct3D 8 implementation.

Bug swiftshader:5

Change-Id: I6aec7237f2db6222702e828a2b9dae1a1a6ac9af
Reviewed-on: https://swiftshader-review.googlesource.com/7191
Tested-by: Nicolas Capens <capn@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
diff --git a/SwiftShader.sln b/SwiftShader.sln
index 857329b..d09eb80 100644
--- a/SwiftShader.sln
+++ b/SwiftShader.sln
@@ -255,6 +255,8 @@
 EndProject

 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OGLES3Tools", "third_party\PowerVR_SDK\Tools\OGLES3\Build\WindowsVC2010\OGLES3Tools.vcxproj", "{9088FC9E-9843-4E0D-85D0-1B657AFC480A}"

 EndProject

+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "D3D8", "src\D3D8\D3D8.vcxproj", "{F18D5ABF-CA3A-4B74-BDB2-4A1956C86F18}"

+EndProject

 Global

 	GlobalSection(SolutionConfigurationPlatforms) = preSolution

 		Debug|Win32 = Debug|Win32

@@ -721,6 +723,15 @@
 		{9088FC9E-9843-4E0D-85D0-1B657AFC480A}.Release|Win32.Build.0 = Release|Win32

 		{9088FC9E-9843-4E0D-85D0-1B657AFC480A}.Release|x64.ActiveCfg = Release|x64

 		{9088FC9E-9843-4E0D-85D0-1B657AFC480A}.Release|x64.Build.0 = Release|x64

+		{F18D5ABF-CA3A-4B74-BDB2-4A1956C86F18}.Debug|Win32.ActiveCfg = Debug|Win32

+		{F18D5ABF-CA3A-4B74-BDB2-4A1956C86F18}.Debug|Win32.Build.0 = Debug|Win32

+		{F18D5ABF-CA3A-4B74-BDB2-4A1956C86F18}.Debug|x64.ActiveCfg = Debug|Win32

+		{F18D5ABF-CA3A-4B74-BDB2-4A1956C86F18}.Profile|Win32.ActiveCfg = Profile|Win32

+		{F18D5ABF-CA3A-4B74-BDB2-4A1956C86F18}.Profile|Win32.Build.0 = Profile|Win32

+		{F18D5ABF-CA3A-4B74-BDB2-4A1956C86F18}.Profile|x64.ActiveCfg = Profile|Win32

+		{F18D5ABF-CA3A-4B74-BDB2-4A1956C86F18}.Release|Win32.ActiveCfg = Release|Win32

+		{F18D5ABF-CA3A-4B74-BDB2-4A1956C86F18}.Release|Win32.Build.0 = Release|Win32

+		{F18D5ABF-CA3A-4B74-BDB2-4A1956C86F18}.Release|x64.ActiveCfg = Release|Win32

 	EndGlobalSection

 	GlobalSection(SolutionProperties) = preSolution

 		HideSolutionNode = FALSE

diff --git a/include/Direct3D/d3d8.h b/include/Direct3D/d3d8.h
new file mode 100644
index 0000000..2600282
--- /dev/null
+++ b/include/Direct3D/d3d8.h
@@ -0,0 +1,1279 @@
+/*==========================================================================;

+ *

+ *  Copyright (C) Microsoft Corporation.  All Rights Reserved.

+ *

+ *  File:   d3d8.h

+ *  Content:    Direct3D include file

+ *

+ ****************************************************************************/

+

+#ifndef _D3D8_H_

+#define _D3D8_H_

+

+#ifndef DIRECT3D_VERSION

+#define DIRECT3D_VERSION         0x0800

+#endif  //DIRECT3D_VERSION

+

+// include this file content only if compiling for DX8 interfaces

+#if(DIRECT3D_VERSION >= 0x0800)

+

+

+/* This identifier is passed to Direct3DCreate8 in order to ensure that an

+ * application was built against the correct header files. This number is

+ * incremented whenever a header (or other) change would require applications

+ * to be rebuilt. If the version doesn't match, Direct3DCreate8 will fail.

+ * (The number itself has no meaning.)*/

+

+#define D3D_SDK_VERSION 220

+

+

+#include <stdlib.h>

+

+#define COM_NO_WINDOWS_H

+#include <objbase.h>

+

+#include <windows.h>

+

+#if !defined(HMONITOR_DECLARED) && (WINVER < 0x0500)

+    #define HMONITOR_DECLARED

+    DECLARE_HANDLE(HMONITOR);

+#endif

+

+#define D3DAPI WINAPI

+

+/*

+ * Interface IID's

+ */

+#if defined( _WIN32 ) && !defined( _NO_COM)

+

+/* IID_IDirect3D8 */

+/* {1DD9E8DA-1C77-4d40-B0CF-98FEFDFF9512} */

+DEFINE_GUID(IID_IDirect3D8, 0x1dd9e8da, 0x1c77, 0x4d40, 0xb0, 0xcf, 0x98, 0xfe, 0xfd, 0xff, 0x95, 0x12);

+

+/* IID_IDirect3DDevice8 */

+/* {7385E5DF-8FE8-41D5-86B6-D7B48547B6CF} */

+DEFINE_GUID(IID_IDirect3DDevice8, 0x7385e5df, 0x8fe8, 0x41d5, 0x86, 0xb6, 0xd7, 0xb4, 0x85, 0x47, 0xb6, 0xcf);

+

+/* IID_IDirect3DResource8 */

+/* {1B36BB7B-09B7-410a-B445-7D1430D7B33F} */

+DEFINE_GUID(IID_IDirect3DResource8, 0x1b36bb7b, 0x9b7, 0x410a, 0xb4, 0x45, 0x7d, 0x14, 0x30, 0xd7, 0xb3, 0x3f);

+

+/* IID_IDirect3DBaseTexture8 */

+/* {B4211CFA-51B9-4a9f-AB78-DB99B2BB678E} */

+DEFINE_GUID(IID_IDirect3DBaseTexture8, 0xb4211cfa, 0x51b9, 0x4a9f, 0xab, 0x78, 0xdb, 0x99, 0xb2, 0xbb, 0x67, 0x8e);

+

+/* IID_IDirect3DTexture8 */

+/* {E4CDD575-2866-4f01-B12E-7EECE1EC9358} */

+DEFINE_GUID(IID_IDirect3DTexture8, 0xe4cdd575, 0x2866, 0x4f01, 0xb1, 0x2e, 0x7e, 0xec, 0xe1, 0xec, 0x93, 0x58);

+

+/* IID_IDirect3DCubeTexture8 */

+/* {3EE5B968-2ACA-4c34-8BB5-7E0C3D19B750} */

+DEFINE_GUID(IID_IDirect3DCubeTexture8, 0x3ee5b968, 0x2aca, 0x4c34, 0x8b, 0xb5, 0x7e, 0x0c, 0x3d, 0x19, 0xb7, 0x50);

+

+/* IID_IDirect3DVolumeTexture8 */

+/* {4B8AAAFA-140F-42ba-9131-597EAFAA2EAD} */

+DEFINE_GUID(IID_IDirect3DVolumeTexture8, 0x4b8aaafa, 0x140f, 0x42ba, 0x91, 0x31, 0x59, 0x7e, 0xaf, 0xaa, 0x2e, 0xad);

+

+/* IID_IDirect3DVertexBuffer8 */

+/* {8AEEEAC7-05F9-44d4-B591-000B0DF1CB95} */

+DEFINE_GUID(IID_IDirect3DVertexBuffer8, 0x8aeeeac7, 0x05f9, 0x44d4, 0xb5, 0x91, 0x00, 0x0b, 0x0d, 0xf1, 0xcb, 0x95);

+

+/* IID_IDirect3DIndexBuffer8 */

+/* {0E689C9A-053D-44a0-9D92-DB0E3D750F86} */

+DEFINE_GUID(IID_IDirect3DIndexBuffer8, 0x0e689c9a, 0x053d, 0x44a0, 0x9d, 0x92, 0xdb, 0x0e, 0x3d, 0x75, 0x0f, 0x86);

+

+/* IID_IDirect3DSurface8 */

+/* {B96EEBCA-B326-4ea5-882F-2FF5BAE021DD} */

+DEFINE_GUID(IID_IDirect3DSurface8, 0xb96eebca, 0xb326, 0x4ea5, 0x88, 0x2f, 0x2f, 0xf5, 0xba, 0xe0, 0x21, 0xdd);

+

+/* IID_IDirect3DVolume8 */

+/* {BD7349F5-14F1-42e4-9C79-972380DB40C0} */

+DEFINE_GUID(IID_IDirect3DVolume8, 0xbd7349f5, 0x14f1, 0x42e4, 0x9c, 0x79, 0x97, 0x23, 0x80, 0xdb, 0x40, 0xc0);

+

+/* IID_IDirect3DSwapChain8 */

+/* {928C088B-76B9-4C6B-A536-A590853876CD} */

+DEFINE_GUID(IID_IDirect3DSwapChain8, 0x928c088b, 0x76b9, 0x4c6b, 0xa5, 0x36, 0xa5, 0x90, 0x85, 0x38, 0x76, 0xcd);

+

+#endif

+

+#ifdef __cplusplus

+

+interface IDirect3D8;

+interface IDirect3DDevice8;

+

+interface IDirect3DResource8;

+interface IDirect3DBaseTexture8;

+interface IDirect3DTexture8;

+interface IDirect3DVolumeTexture8;

+interface IDirect3DCubeTexture8;

+

+interface IDirect3DVertexBuffer8;

+interface IDirect3DIndexBuffer8;

+

+interface IDirect3DSurface8;

+interface IDirect3DVolume8;

+

+interface IDirect3DSwapChain8;

+

+#endif

+

+

+typedef interface IDirect3D8                IDirect3D8;

+typedef interface IDirect3DDevice8          IDirect3DDevice8;

+typedef interface IDirect3DResource8        IDirect3DResource8;

+typedef interface IDirect3DBaseTexture8     IDirect3DBaseTexture8;

+typedef interface IDirect3DTexture8         IDirect3DTexture8;

+typedef interface IDirect3DVolumeTexture8   IDirect3DVolumeTexture8;

+typedef interface IDirect3DCubeTexture8     IDirect3DCubeTexture8;

+typedef interface IDirect3DVertexBuffer8    IDirect3DVertexBuffer8;

+typedef interface IDirect3DIndexBuffer8     IDirect3DIndexBuffer8;

+typedef interface IDirect3DSurface8         IDirect3DSurface8;

+typedef interface IDirect3DVolume8          IDirect3DVolume8;

+typedef interface IDirect3DSwapChain8       IDirect3DSwapChain8;

+

+#include "d3d8types.h"

+#include "d3d8caps.h"

+

+

+#ifdef __cplusplus

+extern "C" {

+#endif

+

+/*

+ * DLL Function for creating a Direct3D8 object. This object supports

+ * enumeration and allows the creation of Direct3DDevice8 objects.

+ * Pass the value of the constant D3D_SDK_VERSION to this function, so

+ * that the run-time can validate that your application was compiled

+ * against the right headers.

+ */

+

+IDirect3D8 * WINAPI Direct3DCreate8(UINT SDKVersion);

+

+

+/*

+ * Direct3D interfaces

+ */

+

+

+

+

+

+

+#undef INTERFACE

+#define INTERFACE IDirect3D8

+

+DECLARE_INTERFACE_(IDirect3D8, IUnknown)

+{

+    /*** IUnknown methods ***/

+    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;

+    STDMETHOD_(ULONG,AddRef)(THIS) PURE;

+    STDMETHOD_(ULONG,Release)(THIS) PURE;

+

+    /*** IDirect3D8 methods ***/

+    STDMETHOD(RegisterSoftwareDevice)(THIS_ void* pInitializeFunction) PURE;

+    STDMETHOD_(UINT, GetAdapterCount)(THIS) PURE;

+    STDMETHOD(GetAdapterIdentifier)(THIS_ UINT Adapter,DWORD Flags,D3DADAPTER_IDENTIFIER8* pIdentifier) PURE;

+    STDMETHOD_(UINT, GetAdapterModeCount)(THIS_ UINT Adapter) PURE;

+    STDMETHOD(EnumAdapterModes)(THIS_ UINT Adapter,UINT Mode,D3DDISPLAYMODE* pMode) PURE;

+    STDMETHOD(GetAdapterDisplayMode)(THIS_ UINT Adapter,D3DDISPLAYMODE* pMode) PURE;

+    STDMETHOD(CheckDeviceType)(THIS_ UINT Adapter,D3DDEVTYPE CheckType,D3DFORMAT DisplayFormat,D3DFORMAT BackBufferFormat,BOOL Windowed) PURE;

+    STDMETHOD(CheckDeviceFormat)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,DWORD Usage,D3DRESOURCETYPE RType,D3DFORMAT CheckFormat) PURE;

+    STDMETHOD(CheckDeviceMultiSampleType)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT SurfaceFormat,BOOL Windowed,D3DMULTISAMPLE_TYPE MultiSampleType) PURE;

+    STDMETHOD(CheckDepthStencilMatch)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,D3DFORMAT RenderTargetFormat,D3DFORMAT DepthStencilFormat) PURE;

+    STDMETHOD(GetDeviceCaps)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DCAPS8* pCaps) PURE;

+    STDMETHOD_(HMONITOR, GetAdapterMonitor)(THIS_ UINT Adapter) PURE;

+    STDMETHOD(CreateDevice)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,HWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DDevice8** ppReturnedDeviceInterface) PURE;

+};

+

+typedef struct IDirect3D8 *LPDIRECT3D8, *PDIRECT3D8;

+

+#if !defined(__cplusplus) || defined(CINTERFACE)

+#define IDirect3D8_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)

+#define IDirect3D8_AddRef(p) (p)->lpVtbl->AddRef(p)

+#define IDirect3D8_Release(p) (p)->lpVtbl->Release(p)

+#define IDirect3D8_RegisterSoftwareDevice(p,a) (p)->lpVtbl->RegisterSoftwareDevice(p,a)

+#define IDirect3D8_GetAdapterCount(p) (p)->lpVtbl->GetAdapterCount(p)

+#define IDirect3D8_GetAdapterIdentifier(p,a,b,c) (p)->lpVtbl->GetAdapterIdentifier(p,a,b,c)

+#define IDirect3D8_GetAdapterModeCount(p,a) (p)->lpVtbl->GetAdapterModeCount(p,a)

+#define IDirect3D8_EnumAdapterModes(p,a,b,c) (p)->lpVtbl->EnumAdapterModes(p,a,b,c)

+#define IDirect3D8_GetAdapterDisplayMode(p,a,b) (p)->lpVtbl->GetAdapterDisplayMode(p,a,b)

+#define IDirect3D8_CheckDeviceType(p,a,b,c,d,e) (p)->lpVtbl->CheckDeviceType(p,a,b,c,d,e)

+#define IDirect3D8_CheckDeviceFormat(p,a,b,c,d,e,f) (p)->lpVtbl->CheckDeviceFormat(p,a,b,c,d,e,f)

+#define IDirect3D8_CheckDeviceMultiSampleType(p,a,b,c,d,e) (p)->lpVtbl->CheckDeviceMultiSampleType(p,a,b,c,d,e)

+#define IDirect3D8_CheckDepthStencilMatch(p,a,b,c,d,e) (p)->lpVtbl->CheckDepthStencilMatch(p,a,b,c,d,e)

+#define IDirect3D8_GetDeviceCaps(p,a,b,c) (p)->lpVtbl->GetDeviceCaps(p,a,b,c)

+#define IDirect3D8_GetAdapterMonitor(p,a) (p)->lpVtbl->GetAdapterMonitor(p,a)

+#define IDirect3D8_CreateDevice(p,a,b,c,d,e,f) (p)->lpVtbl->CreateDevice(p,a,b,c,d,e,f)

+#else

+#define IDirect3D8_QueryInterface(p,a,b) (p)->QueryInterface(a,b)

+#define IDirect3D8_AddRef(p) (p)->AddRef()

+#define IDirect3D8_Release(p) (p)->Release()

+#define IDirect3D8_RegisterSoftwareDevice(p,a) (p)->RegisterSoftwareDevice(a)

+#define IDirect3D8_GetAdapterCount(p) (p)->GetAdapterCount()

+#define IDirect3D8_GetAdapterIdentifier(p,a,b,c) (p)->GetAdapterIdentifier(a,b,c)

+#define IDirect3D8_GetAdapterModeCount(p,a) (p)->GetAdapterModeCount(a)

+#define IDirect3D8_EnumAdapterModes(p,a,b,c) (p)->EnumAdapterModes(a,b,c)

+#define IDirect3D8_GetAdapterDisplayMode(p,a,b) (p)->GetAdapterDisplayMode(a,b)

+#define IDirect3D8_CheckDeviceType(p,a,b,c,d,e) (p)->CheckDeviceType(a,b,c,d,e)

+#define IDirect3D8_CheckDeviceFormat(p,a,b,c,d,e,f) (p)->CheckDeviceFormat(a,b,c,d,e,f)

+#define IDirect3D8_CheckDeviceMultiSampleType(p,a,b,c,d,e) (p)->CheckDeviceMultiSampleType(a,b,c,d,e)

+#define IDirect3D8_CheckDepthStencilMatch(p,a,b,c,d,e) (p)->CheckDepthStencilMatch(a,b,c,d,e)

+#define IDirect3D8_GetDeviceCaps(p,a,b,c) (p)->GetDeviceCaps(a,b,c)

+#define IDirect3D8_GetAdapterMonitor(p,a) (p)->GetAdapterMonitor(a)

+#define IDirect3D8_CreateDevice(p,a,b,c,d,e,f) (p)->CreateDevice(a,b,c,d,e,f)

+#endif

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+#undef INTERFACE

+#define INTERFACE IDirect3DDevice8

+

+DECLARE_INTERFACE_(IDirect3DDevice8, IUnknown)

+{

+    /*** IUnknown methods ***/

+    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;

+    STDMETHOD_(ULONG,AddRef)(THIS) PURE;

+    STDMETHOD_(ULONG,Release)(THIS) PURE;

+

+    /*** IDirect3DDevice8 methods ***/

+    STDMETHOD(TestCooperativeLevel)(THIS) PURE;

+    STDMETHOD_(UINT, GetAvailableTextureMem)(THIS) PURE;

+    STDMETHOD(ResourceManagerDiscardBytes)(THIS_ DWORD Bytes) PURE;

+    STDMETHOD(GetDirect3D)(THIS_ IDirect3D8** ppD3D8) PURE;

+    STDMETHOD(GetDeviceCaps)(THIS_ D3DCAPS8* pCaps) PURE;

+    STDMETHOD(GetDisplayMode)(THIS_ D3DDISPLAYMODE* pMode) PURE;

+    STDMETHOD(GetCreationParameters)(THIS_ D3DDEVICE_CREATION_PARAMETERS *pParameters) PURE;

+    STDMETHOD(SetCursorProperties)(THIS_ UINT XHotSpot,UINT YHotSpot,IDirect3DSurface8* pCursorBitmap) PURE;

+    STDMETHOD_(void, SetCursorPosition)(THIS_ int X,int Y,DWORD Flags) PURE;

+    STDMETHOD_(BOOL, ShowCursor)(THIS_ BOOL bShow) PURE;

+    STDMETHOD(CreateAdditionalSwapChain)(THIS_ D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DSwapChain8** pSwapChain) PURE;

+    STDMETHOD(Reset)(THIS_ D3DPRESENT_PARAMETERS* pPresentationParameters) PURE;

+    STDMETHOD(Present)(THIS_ CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion) PURE;

+    STDMETHOD(GetBackBuffer)(THIS_ UINT BackBuffer,D3DBACKBUFFER_TYPE Type,IDirect3DSurface8** ppBackBuffer) PURE;

+    STDMETHOD(GetRasterStatus)(THIS_ D3DRASTER_STATUS* pRasterStatus) PURE;

+    STDMETHOD_(void, SetGammaRamp)(THIS_ DWORD Flags,CONST D3DGAMMARAMP* pRamp) PURE;

+    STDMETHOD_(void, GetGammaRamp)(THIS_ D3DGAMMARAMP* pRamp) PURE;

+    STDMETHOD(CreateTexture)(THIS_ UINT Width,UINT Height,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DTexture8** ppTexture) PURE;

+    STDMETHOD(CreateVolumeTexture)(THIS_ UINT Width,UINT Height,UINT Depth,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DVolumeTexture8** ppVolumeTexture) PURE;

+    STDMETHOD(CreateCubeTexture)(THIS_ UINT EdgeLength,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DCubeTexture8** ppCubeTexture) PURE;

+    STDMETHOD(CreateVertexBuffer)(THIS_ UINT Length,DWORD Usage,DWORD FVF,D3DPOOL Pool,IDirect3DVertexBuffer8** ppVertexBuffer) PURE;

+    STDMETHOD(CreateIndexBuffer)(THIS_ UINT Length,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DIndexBuffer8** ppIndexBuffer) PURE;

+    STDMETHOD(CreateRenderTarget)(THIS_ UINT Width,UINT Height,D3DFORMAT Format,D3DMULTISAMPLE_TYPE MultiSample,BOOL Lockable,IDirect3DSurface8** ppSurface) PURE;

+    STDMETHOD(CreateDepthStencilSurface)(THIS_ UINT Width,UINT Height,D3DFORMAT Format,D3DMULTISAMPLE_TYPE MultiSample,IDirect3DSurface8** ppSurface) PURE;

+    STDMETHOD(CreateImageSurface)(THIS_ UINT Width,UINT Height,D3DFORMAT Format,IDirect3DSurface8** ppSurface) PURE;

+    STDMETHOD(CopyRects)(THIS_ IDirect3DSurface8* pSourceSurface,CONST RECT* pSourceRectsArray,UINT cRects,IDirect3DSurface8* pDestinationSurface,CONST POINT* pDestPointsArray) PURE;

+    STDMETHOD(UpdateTexture)(THIS_ IDirect3DBaseTexture8* pSourceTexture,IDirect3DBaseTexture8* pDestinationTexture) PURE;

+    STDMETHOD(GetFrontBuffer)(THIS_ IDirect3DSurface8* pDestSurface) PURE;

+    STDMETHOD(SetRenderTarget)(THIS_ IDirect3DSurface8* pRenderTarget,IDirect3DSurface8* pNewZStencil) PURE;

+    STDMETHOD(GetRenderTarget)(THIS_ IDirect3DSurface8** ppRenderTarget) PURE;

+    STDMETHOD(GetDepthStencilSurface)(THIS_ IDirect3DSurface8** ppZStencilSurface) PURE;

+    STDMETHOD(BeginScene)(THIS) PURE;

+    STDMETHOD(EndScene)(THIS) PURE;

+    STDMETHOD(Clear)(THIS_ DWORD Count,CONST D3DRECT* pRects,DWORD Flags,D3DCOLOR Color,float Z,DWORD Stencil) PURE;

+    STDMETHOD(SetTransform)(THIS_ D3DTRANSFORMSTATETYPE State,CONST D3DMATRIX* pMatrix) PURE;

+    STDMETHOD(GetTransform)(THIS_ D3DTRANSFORMSTATETYPE State,D3DMATRIX* pMatrix) PURE;

+    STDMETHOD(MultiplyTransform)(THIS_ D3DTRANSFORMSTATETYPE,CONST D3DMATRIX*) PURE;

+    STDMETHOD(SetViewport)(THIS_ CONST D3DVIEWPORT8* pViewport) PURE;

+    STDMETHOD(GetViewport)(THIS_ D3DVIEWPORT8* pViewport) PURE;

+    STDMETHOD(SetMaterial)(THIS_ CONST D3DMATERIAL8* pMaterial) PURE;

+    STDMETHOD(GetMaterial)(THIS_ D3DMATERIAL8* pMaterial) PURE;

+    STDMETHOD(SetLight)(THIS_ DWORD Index,CONST D3DLIGHT8*) PURE;

+    STDMETHOD(GetLight)(THIS_ DWORD Index,D3DLIGHT8*) PURE;

+    STDMETHOD(LightEnable)(THIS_ DWORD Index,BOOL Enable) PURE;

+    STDMETHOD(GetLightEnable)(THIS_ DWORD Index,BOOL* pEnable) PURE;

+    STDMETHOD(SetClipPlane)(THIS_ DWORD Index,CONST float* pPlane) PURE;

+    STDMETHOD(GetClipPlane)(THIS_ DWORD Index,float* pPlane) PURE;

+    STDMETHOD(SetRenderState)(THIS_ D3DRENDERSTATETYPE State,DWORD Value) PURE;

+    STDMETHOD(GetRenderState)(THIS_ D3DRENDERSTATETYPE State,DWORD* pValue) PURE;

+    STDMETHOD(BeginStateBlock)(THIS) PURE;

+    STDMETHOD(EndStateBlock)(THIS_ DWORD* pToken) PURE;

+    STDMETHOD(ApplyStateBlock)(THIS_ DWORD Token) PURE;

+    STDMETHOD(CaptureStateBlock)(THIS_ DWORD Token) PURE;

+    STDMETHOD(DeleteStateBlock)(THIS_ DWORD Token) PURE;

+    STDMETHOD(CreateStateBlock)(THIS_ D3DSTATEBLOCKTYPE Type,DWORD* pToken) PURE;

+    STDMETHOD(SetClipStatus)(THIS_ CONST D3DCLIPSTATUS8* pClipStatus) PURE;

+    STDMETHOD(GetClipStatus)(THIS_ D3DCLIPSTATUS8* pClipStatus) PURE;

+    STDMETHOD(GetTexture)(THIS_ DWORD Stage,IDirect3DBaseTexture8** ppTexture) PURE;

+    STDMETHOD(SetTexture)(THIS_ DWORD Stage,IDirect3DBaseTexture8* pTexture) PURE;

+    STDMETHOD(GetTextureStageState)(THIS_ DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) PURE;

+    STDMETHOD(SetTextureStageState)(THIS_ DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD Value) PURE;

+    STDMETHOD(ValidateDevice)(THIS_ DWORD* pNumPasses) PURE;

+    STDMETHOD(GetInfo)(THIS_ DWORD DevInfoID,void* pDevInfoStruct,DWORD DevInfoStructSize) PURE;

+    STDMETHOD(SetPaletteEntries)(THIS_ UINT PaletteNumber,CONST PALETTEENTRY* pEntries) PURE;

+    STDMETHOD(GetPaletteEntries)(THIS_ UINT PaletteNumber,PALETTEENTRY* pEntries) PURE;

+    STDMETHOD(SetCurrentTexturePalette)(THIS_ UINT PaletteNumber) PURE;

+    STDMETHOD(GetCurrentTexturePalette)(THIS_ UINT *PaletteNumber) PURE;

+    STDMETHOD(DrawPrimitive)(THIS_ D3DPRIMITIVETYPE PrimitiveType,UINT StartVertex,UINT PrimitiveCount) PURE;

+    STDMETHOD(DrawIndexedPrimitive)(THIS_ D3DPRIMITIVETYPE,UINT minIndex,UINT NumVertices,UINT startIndex,UINT primCount) PURE;

+    STDMETHOD(DrawPrimitiveUP)(THIS_ D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) PURE;

+    STDMETHOD(DrawIndexedPrimitiveUP)(THIS_ D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,UINT NumVertexIndices,UINT PrimitiveCount,CONST void* pIndexData,D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) PURE;

+    STDMETHOD(ProcessVertices)(THIS_ UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer8* pDestBuffer,DWORD Flags) PURE;

+    STDMETHOD(CreateVertexShader)(THIS_ CONST DWORD* pDeclaration,CONST DWORD* pFunction,DWORD* pHandle,DWORD Usage) PURE;

+    STDMETHOD(SetVertexShader)(THIS_ DWORD Handle) PURE;

+    STDMETHOD(GetVertexShader)(THIS_ DWORD* pHandle) PURE;

+    STDMETHOD(DeleteVertexShader)(THIS_ DWORD Handle) PURE;

+    STDMETHOD(SetVertexShaderConstant)(THIS_ DWORD Register,CONST void* pConstantData,DWORD ConstantCount) PURE;

+    STDMETHOD(GetVertexShaderConstant)(THIS_ DWORD Register,void* pConstantData,DWORD ConstantCount) PURE;

+    STDMETHOD(GetVertexShaderDeclaration)(THIS_ DWORD Handle,void* pData,DWORD* pSizeOfData) PURE;

+    STDMETHOD(GetVertexShaderFunction)(THIS_ DWORD Handle,void* pData,DWORD* pSizeOfData) PURE;

+    STDMETHOD(SetStreamSource)(THIS_ UINT StreamNumber,IDirect3DVertexBuffer8* pStreamData,UINT Stride) PURE;

+    STDMETHOD(GetStreamSource)(THIS_ UINT StreamNumber,IDirect3DVertexBuffer8** ppStreamData,UINT* pStride) PURE;

+    STDMETHOD(SetIndices)(THIS_ IDirect3DIndexBuffer8* pIndexData,UINT BaseVertexIndex) PURE;

+    STDMETHOD(GetIndices)(THIS_ IDirect3DIndexBuffer8** ppIndexData,UINT* pBaseVertexIndex) PURE;

+    STDMETHOD(CreatePixelShader)(THIS_ CONST DWORD* pFunction,DWORD* pHandle) PURE;

+    STDMETHOD(SetPixelShader)(THIS_ DWORD Handle) PURE;

+    STDMETHOD(GetPixelShader)(THIS_ DWORD* pHandle) PURE;

+    STDMETHOD(DeletePixelShader)(THIS_ DWORD Handle) PURE;

+    STDMETHOD(SetPixelShaderConstant)(THIS_ DWORD Register,CONST void* pConstantData,DWORD ConstantCount) PURE;

+    STDMETHOD(GetPixelShaderConstant)(THIS_ DWORD Register,void* pConstantData,DWORD ConstantCount) PURE;

+    STDMETHOD(GetPixelShaderFunction)(THIS_ DWORD Handle,void* pData,DWORD* pSizeOfData) PURE;

+    STDMETHOD(DrawRectPatch)(THIS_ UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) PURE;

+    STDMETHOD(DrawTriPatch)(THIS_ UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) PURE;

+    STDMETHOD(DeletePatch)(THIS_ UINT Handle) PURE;

+};

+

+typedef struct IDirect3DDevice8 *LPDIRECT3DDEVICE8, *PDIRECT3DDEVICE8;

+

+#if !defined(__cplusplus) || defined(CINTERFACE)

+#define IDirect3DDevice8_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)

+#define IDirect3DDevice8_AddRef(p) (p)->lpVtbl->AddRef(p)

+#define IDirect3DDevice8_Release(p) (p)->lpVtbl->Release(p)

+#define IDirect3DDevice8_TestCooperativeLevel(p) (p)->lpVtbl->TestCooperativeLevel(p)

+#define IDirect3DDevice8_GetAvailableTextureMem(p) (p)->lpVtbl->GetAvailableTextureMem(p)

+#define IDirect3DDevice8_ResourceManagerDiscardBytes(p,a) (p)->lpVtbl->ResourceManagerDiscardBytes(p,a)

+#define IDirect3DDevice8_GetDirect3D(p,a) (p)->lpVtbl->GetDirect3D(p,a)

+#define IDirect3DDevice8_GetDeviceCaps(p,a) (p)->lpVtbl->GetDeviceCaps(p,a)

+#define IDirect3DDevice8_GetDisplayMode(p,a) (p)->lpVtbl->GetDisplayMode(p,a)

+#define IDirect3DDevice8_GetCreationParameters(p,a) (p)->lpVtbl->GetCreationParameters(p,a)

+#define IDirect3DDevice8_SetCursorProperties(p,a,b,c) (p)->lpVtbl->SetCursorProperties(p,a,b,c)

+#define IDirect3DDevice8_SetCursorPosition(p,a,b,c) (p)->lpVtbl->SetCursorPosition(p,a,b,c)

+#define IDirect3DDevice8_ShowCursor(p,a) (p)->lpVtbl->ShowCursor(p,a)

+#define IDirect3DDevice8_CreateAdditionalSwapChain(p,a,b) (p)->lpVtbl->CreateAdditionalSwapChain(p,a,b)

+#define IDirect3DDevice8_Reset(p,a) (p)->lpVtbl->Reset(p,a)

+#define IDirect3DDevice8_Present(p,a,b,c,d) (p)->lpVtbl->Present(p,a,b,c,d)

+#define IDirect3DDevice8_GetBackBuffer(p,a,b,c) (p)->lpVtbl->GetBackBuffer(p,a,b,c)

+#define IDirect3DDevice8_GetRasterStatus(p,a) (p)->lpVtbl->GetRasterStatus(p,a)

+#define IDirect3DDevice8_SetGammaRamp(p,a,b) (p)->lpVtbl->SetGammaRamp(p,a,b)

+#define IDirect3DDevice8_GetGammaRamp(p,a) (p)->lpVtbl->GetGammaRamp(p,a)

+#define IDirect3DDevice8_CreateTexture(p,a,b,c,d,e,f,g) (p)->lpVtbl->CreateTexture(p,a,b,c,d,e,f,g)

+#define IDirect3DDevice8_CreateVolumeTexture(p,a,b,c,d,e,f,g,h) (p)->lpVtbl->CreateVolumeTexture(p,a,b,c,d,e,f,g,h)

+#define IDirect3DDevice8_CreateCubeTexture(p,a,b,c,d,e,f) (p)->lpVtbl->CreateCubeTexture(p,a,b,c,d,e,f)

+#define IDirect3DDevice8_CreateVertexBuffer(p,a,b,c,d,e) (p)->lpVtbl->CreateVertexBuffer(p,a,b,c,d,e)

+#define IDirect3DDevice8_CreateIndexBuffer(p,a,b,c,d,e) (p)->lpVtbl->CreateIndexBuffer(p,a,b,c,d,e)

+#define IDirect3DDevice8_CreateRenderTarget(p,a,b,c,d,e,f) (p)->lpVtbl->CreateRenderTarget(p,a,b,c,d,e,f)

+#define IDirect3DDevice8_CreateDepthStencilSurface(p,a,b,c,d,e) (p)->lpVtbl->CreateDepthStencilSurface(p,a,b,c,d,e)

+#define IDirect3DDevice8_CreateImageSurface(p,a,b,c,d) (p)->lpVtbl->CreateImageSurface(p,a,b,c,d)

+#define IDirect3DDevice8_CopyRects(p,a,b,c,d,e) (p)->lpVtbl->CopyRects(p,a,b,c,d,e)

+#define IDirect3DDevice8_UpdateTexture(p,a,b) (p)->lpVtbl->UpdateTexture(p,a,b)

+#define IDirect3DDevice8_GetFrontBuffer(p,a) (p)->lpVtbl->GetFrontBuffer(p,a)

+#define IDirect3DDevice8_SetRenderTarget(p,a,b) (p)->lpVtbl->SetRenderTarget(p,a,b)

+#define IDirect3DDevice8_GetRenderTarget(p,a) (p)->lpVtbl->GetRenderTarget(p,a)

+#define IDirect3DDevice8_GetDepthStencilSurface(p,a) (p)->lpVtbl->GetDepthStencilSurface(p,a)

+#define IDirect3DDevice8_BeginScene(p) (p)->lpVtbl->BeginScene(p)

+#define IDirect3DDevice8_EndScene(p) (p)->lpVtbl->EndScene(p)

+#define IDirect3DDevice8_Clear(p,a,b,c,d,e,f) (p)->lpVtbl->Clear(p,a,b,c,d,e,f)

+#define IDirect3DDevice8_SetTransform(p,a,b) (p)->lpVtbl->SetTransform(p,a,b)

+#define IDirect3DDevice8_GetTransform(p,a,b) (p)->lpVtbl->GetTransform(p,a,b)

+#define IDirect3DDevice8_MultiplyTransform(p,a,b) (p)->lpVtbl->MultiplyTransform(p,a,b)

+#define IDirect3DDevice8_SetViewport(p,a) (p)->lpVtbl->SetViewport(p,a)

+#define IDirect3DDevice8_GetViewport(p,a) (p)->lpVtbl->GetViewport(p,a)

+#define IDirect3DDevice8_SetMaterial(p,a) (p)->lpVtbl->SetMaterial(p,a)

+#define IDirect3DDevice8_GetMaterial(p,a) (p)->lpVtbl->GetMaterial(p,a)

+#define IDirect3DDevice8_SetLight(p,a,b) (p)->lpVtbl->SetLight(p,a,b)

+#define IDirect3DDevice8_GetLight(p,a,b) (p)->lpVtbl->GetLight(p,a,b)

+#define IDirect3DDevice8_LightEnable(p,a,b) (p)->lpVtbl->LightEnable(p,a,b)

+#define IDirect3DDevice8_GetLightEnable(p,a,b) (p)->lpVtbl->GetLightEnable(p,a,b)

+#define IDirect3DDevice8_SetClipPlane(p,a,b) (p)->lpVtbl->SetClipPlane(p,a,b)

+#define IDirect3DDevice8_GetClipPlane(p,a,b) (p)->lpVtbl->GetClipPlane(p,a,b)

+#define IDirect3DDevice8_SetRenderState(p,a,b) (p)->lpVtbl->SetRenderState(p,a,b)

+#define IDirect3DDevice8_GetRenderState(p,a,b) (p)->lpVtbl->GetRenderState(p,a,b)

+#define IDirect3DDevice8_BeginStateBlock(p) (p)->lpVtbl->BeginStateBlock(p)

+#define IDirect3DDevice8_EndStateBlock(p,a) (p)->lpVtbl->EndStateBlock(p,a)

+#define IDirect3DDevice8_ApplyStateBlock(p,a) (p)->lpVtbl->ApplyStateBlock(p,a)

+#define IDirect3DDevice8_CaptureStateBlock(p,a) (p)->lpVtbl->CaptureStateBlock(p,a)

+#define IDirect3DDevice8_DeleteStateBlock(p,a) (p)->lpVtbl->DeleteStateBlock(p,a)

+#define IDirect3DDevice8_CreateStateBlock(p,a,b) (p)->lpVtbl->CreateStateBlock(p,a,b)

+#define IDirect3DDevice8_SetClipStatus(p,a) (p)->lpVtbl->SetClipStatus(p,a)

+#define IDirect3DDevice8_GetClipStatus(p,a) (p)->lpVtbl->GetClipStatus(p,a)

+#define IDirect3DDevice8_GetTexture(p,a,b) (p)->lpVtbl->GetTexture(p,a,b)

+#define IDirect3DDevice8_SetTexture(p,a,b) (p)->lpVtbl->SetTexture(p,a,b)

+#define IDirect3DDevice8_GetTextureStageState(p,a,b,c) (p)->lpVtbl->GetTextureStageState(p,a,b,c)

+#define IDirect3DDevice8_SetTextureStageState(p,a,b,c) (p)->lpVtbl->SetTextureStageState(p,a,b,c)

+#define IDirect3DDevice8_ValidateDevice(p,a) (p)->lpVtbl->ValidateDevice(p,a)

+#define IDirect3DDevice8_GetInfo(p,a,b,c) (p)->lpVtbl->GetInfo(p,a,b,c)

+#define IDirect3DDevice8_SetPaletteEntries(p,a,b) (p)->lpVtbl->SetPaletteEntries(p,a,b)

+#define IDirect3DDevice8_GetPaletteEntries(p,a,b) (p)->lpVtbl->GetPaletteEntries(p,a,b)

+#define IDirect3DDevice8_SetCurrentTexturePalette(p,a) (p)->lpVtbl->SetCurrentTexturePalette(p,a)

+#define IDirect3DDevice8_GetCurrentTexturePalette(p,a) (p)->lpVtbl->GetCurrentTexturePalette(p,a)

+#define IDirect3DDevice8_DrawPrimitive(p,a,b,c) (p)->lpVtbl->DrawPrimitive(p,a,b,c)

+#define IDirect3DDevice8_DrawIndexedPrimitive(p,a,b,c,d,e) (p)->lpVtbl->DrawIndexedPrimitive(p,a,b,c,d,e)

+#define IDirect3DDevice8_DrawPrimitiveUP(p,a,b,c,d) (p)->lpVtbl->DrawPrimitiveUP(p,a,b,c,d)

+#define IDirect3DDevice8_DrawIndexedPrimitiveUP(p,a,b,c,d,e,f,g,h) (p)->lpVtbl->DrawIndexedPrimitiveUP(p,a,b,c,d,e,f,g,h)

+#define IDirect3DDevice8_ProcessVertices(p,a,b,c,d,e) (p)->lpVtbl->ProcessVertices(p,a,b,c,d,e)

+#define IDirect3DDevice8_CreateVertexShader(p,a,b,c,d) (p)->lpVtbl->CreateVertexShader(p,a,b,c,d)

+#define IDirect3DDevice8_SetVertexShader(p,a) (p)->lpVtbl->SetVertexShader(p,a)

+#define IDirect3DDevice8_GetVertexShader(p,a) (p)->lpVtbl->GetVertexShader(p,a)

+#define IDirect3DDevice8_DeleteVertexShader(p,a) (p)->lpVtbl->DeleteVertexShader(p,a)

+#define IDirect3DDevice8_SetVertexShaderConstant(p,a,b,c) (p)->lpVtbl->SetVertexShaderConstant(p,a,b,c)

+#define IDirect3DDevice8_GetVertexShaderConstant(p,a,b,c) (p)->lpVtbl->GetVertexShaderConstant(p,a,b,c)

+#define IDirect3DDevice8_GetVertexShaderDeclaration(p,a,b,c) (p)->lpVtbl->GetVertexShaderDeclaration(p,a,b,c)

+#define IDirect3DDevice8_GetVertexShaderFunction(p,a,b,c) (p)->lpVtbl->GetVertexShaderFunction(p,a,b,c)

+#define IDirect3DDevice8_SetStreamSource(p,a,b,c) (p)->lpVtbl->SetStreamSource(p,a,b,c)

+#define IDirect3DDevice8_GetStreamSource(p,a,b,c) (p)->lpVtbl->GetStreamSource(p,a,b,c)

+#define IDirect3DDevice8_SetIndices(p,a,b) (p)->lpVtbl->SetIndices(p,a,b)

+#define IDirect3DDevice8_GetIndices(p,a,b) (p)->lpVtbl->GetIndices(p,a,b)

+#define IDirect3DDevice8_CreatePixelShader(p,a,b) (p)->lpVtbl->CreatePixelShader(p,a,b)

+#define IDirect3DDevice8_SetPixelShader(p,a) (p)->lpVtbl->SetPixelShader(p,a)

+#define IDirect3DDevice8_GetPixelShader(p,a) (p)->lpVtbl->GetPixelShader(p,a)

+#define IDirect3DDevice8_DeletePixelShader(p,a) (p)->lpVtbl->DeletePixelShader(p,a)

+#define IDirect3DDevice8_SetPixelShaderConstant(p,a,b,c) (p)->lpVtbl->SetPixelShaderConstant(p,a,b,c)

+#define IDirect3DDevice8_GetPixelShaderConstant(p,a,b,c) (p)->lpVtbl->GetPixelShaderConstant(p,a,b,c)

+#define IDirect3DDevice8_GetPixelShaderFunction(p,a,b,c) (p)->lpVtbl->GetPixelShaderFunction(p,a,b,c)

+#define IDirect3DDevice8_DrawRectPatch(p,a,b,c) (p)->lpVtbl->DrawRectPatch(p,a,b,c)

+#define IDirect3DDevice8_DrawTriPatch(p,a,b,c) (p)->lpVtbl->DrawTriPatch(p,a,b,c)

+#define IDirect3DDevice8_DeletePatch(p,a) (p)->lpVtbl->DeletePatch(p,a)

+#else

+#define IDirect3DDevice8_QueryInterface(p,a,b) (p)->QueryInterface(a,b)

+#define IDirect3DDevice8_AddRef(p) (p)->AddRef()

+#define IDirect3DDevice8_Release(p) (p)->Release()

+#define IDirect3DDevice8_TestCooperativeLevel(p) (p)->TestCooperativeLevel()

+#define IDirect3DDevice8_GetAvailableTextureMem(p) (p)->GetAvailableTextureMem()

+#define IDirect3DDevice8_ResourceManagerDiscardBytes(p,a) (p)->ResourceManagerDiscardBytes(a)

+#define IDirect3DDevice8_GetDirect3D(p,a) (p)->GetDirect3D(a)

+#define IDirect3DDevice8_GetDeviceCaps(p,a) (p)->GetDeviceCaps(a)

+#define IDirect3DDevice8_GetDisplayMode(p,a) (p)->GetDisplayMode(a)

+#define IDirect3DDevice8_GetCreationParameters(p,a) (p)->GetCreationParameters(a)

+#define IDirect3DDevice8_SetCursorProperties(p,a,b,c) (p)->SetCursorProperties(a,b,c)

+#define IDirect3DDevice8_SetCursorPosition(p,a,b,c) (p)->SetCursorPosition(a,b,c)

+#define IDirect3DDevice8_ShowCursor(p,a) (p)->ShowCursor(a)

+#define IDirect3DDevice8_CreateAdditionalSwapChain(p,a,b) (p)->CreateAdditionalSwapChain(a,b)

+#define IDirect3DDevice8_Reset(p,a) (p)->Reset(a)

+#define IDirect3DDevice8_Present(p,a,b,c,d) (p)->Present(a,b,c,d)

+#define IDirect3DDevice8_GetBackBuffer(p,a,b,c) (p)->GetBackBuffer(a,b,c)

+#define IDirect3DDevice8_GetRasterStatus(p,a) (p)->GetRasterStatus(a)

+#define IDirect3DDevice8_SetGammaRamp(p,a,b) (p)->SetGammaRamp(a,b)

+#define IDirect3DDevice8_GetGammaRamp(p,a) (p)->GetGammaRamp(a)

+#define IDirect3DDevice8_CreateTexture(p,a,b,c,d,e,f,g) (p)->CreateTexture(a,b,c,d,e,f,g)

+#define IDirect3DDevice8_CreateVolumeTexture(p,a,b,c,d,e,f,g,h) (p)->CreateVolumeTexture(a,b,c,d,e,f,g,h)

+#define IDirect3DDevice8_CreateCubeTexture(p,a,b,c,d,e,f) (p)->CreateCubeTexture(a,b,c,d,e,f)

+#define IDirect3DDevice8_CreateVertexBuffer(p,a,b,c,d,e) (p)->CreateVertexBuffer(a,b,c,d,e)

+#define IDirect3DDevice8_CreateIndexBuffer(p,a,b,c,d,e) (p)->CreateIndexBuffer(a,b,c,d,e)

+#define IDirect3DDevice8_CreateRenderTarget(p,a,b,c,d,e,f) (p)->CreateRenderTarget(a,b,c,d,e,f)

+#define IDirect3DDevice8_CreateDepthStencilSurface(p,a,b,c,d,e) (p)->CreateDepthStencilSurface(a,b,c,d,e)

+#define IDirect3DDevice8_CreateImageSurface(p,a,b,c,d) (p)->CreateImageSurface(a,b,c,d)

+#define IDirect3DDevice8_CopyRects(p,a,b,c,d,e) (p)->CopyRects(a,b,c,d,e)

+#define IDirect3DDevice8_UpdateTexture(p,a,b) (p)->UpdateTexture(a,b)

+#define IDirect3DDevice8_GetFrontBuffer(p,a) (p)->GetFrontBuffer(a)

+#define IDirect3DDevice8_SetRenderTarget(p,a,b) (p)->SetRenderTarget(a,b)

+#define IDirect3DDevice8_GetRenderTarget(p,a) (p)->GetRenderTarget(a)

+#define IDirect3DDevice8_GetDepthStencilSurface(p,a) (p)->GetDepthStencilSurface(a)

+#define IDirect3DDevice8_BeginScene(p) (p)->BeginScene()

+#define IDirect3DDevice8_EndScene(p) (p)->EndScene()

+#define IDirect3DDevice8_Clear(p,a,b,c,d,e,f) (p)->Clear(a,b,c,d,e,f)

+#define IDirect3DDevice8_SetTransform(p,a,b) (p)->SetTransform(a,b)

+#define IDirect3DDevice8_GetTransform(p,a,b) (p)->GetTransform(a,b)

+#define IDirect3DDevice8_MultiplyTransform(p,a,b) (p)->MultiplyTransform(a,b)

+#define IDirect3DDevice8_SetViewport(p,a) (p)->SetViewport(a)

+#define IDirect3DDevice8_GetViewport(p,a) (p)->GetViewport(a)

+#define IDirect3DDevice8_SetMaterial(p,a) (p)->SetMaterial(a)

+#define IDirect3DDevice8_GetMaterial(p,a) (p)->GetMaterial(a)

+#define IDirect3DDevice8_SetLight(p,a,b) (p)->SetLight(a,b)

+#define IDirect3DDevice8_GetLight(p,a,b) (p)->GetLight(a,b)

+#define IDirect3DDevice8_LightEnable(p,a,b) (p)->LightEnable(a,b)

+#define IDirect3DDevice8_GetLightEnable(p,a,b) (p)->GetLightEnable(a,b)

+#define IDirect3DDevice8_SetClipPlane(p,a,b) (p)->SetClipPlane(a,b)

+#define IDirect3DDevice8_GetClipPlane(p,a,b) (p)->GetClipPlane(a,b)

+#define IDirect3DDevice8_SetRenderState(p,a,b) (p)->SetRenderState(a,b)

+#define IDirect3DDevice8_GetRenderState(p,a,b) (p)->GetRenderState(a,b)

+#define IDirect3DDevice8_BeginStateBlock(p) (p)->BeginStateBlock()

+#define IDirect3DDevice8_EndStateBlock(p,a) (p)->EndStateBlock(a)

+#define IDirect3DDevice8_ApplyStateBlock(p,a) (p)->ApplyStateBlock(a)

+#define IDirect3DDevice8_CaptureStateBlock(p,a) (p)->CaptureStateBlock(a)

+#define IDirect3DDevice8_DeleteStateBlock(p,a) (p)->DeleteStateBlock(a)

+#define IDirect3DDevice8_CreateStateBlock(p,a,b) (p)->CreateStateBlock(a,b)

+#define IDirect3DDevice8_SetClipStatus(p,a) (p)->SetClipStatus(a)

+#define IDirect3DDevice8_GetClipStatus(p,a) (p)->GetClipStatus(a)

+#define IDirect3DDevice8_GetTexture(p,a,b) (p)->GetTexture(a,b)

+#define IDirect3DDevice8_SetTexture(p,a,b) (p)->SetTexture(a,b)

+#define IDirect3DDevice8_GetTextureStageState(p,a,b,c) (p)->GetTextureStageState(a,b,c)

+#define IDirect3DDevice8_SetTextureStageState(p,a,b,c) (p)->SetTextureStageState(a,b,c)

+#define IDirect3DDevice8_ValidateDevice(p,a) (p)->ValidateDevice(a)

+#define IDirect3DDevice8_GetInfo(p,a,b,c) (p)->GetInfo(a,b,c)

+#define IDirect3DDevice8_SetPaletteEntries(p,a,b) (p)->SetPaletteEntries(a,b)

+#define IDirect3DDevice8_GetPaletteEntries(p,a,b) (p)->GetPaletteEntries(a,b)

+#define IDirect3DDevice8_SetCurrentTexturePalette(p,a) (p)->SetCurrentTexturePalette(a)

+#define IDirect3DDevice8_GetCurrentTexturePalette(p,a) (p)->GetCurrentTexturePalette(a)

+#define IDirect3DDevice8_DrawPrimitive(p,a,b,c) (p)->DrawPrimitive(a,b,c)

+#define IDirect3DDevice8_DrawIndexedPrimitive(p,a,b,c,d,e) (p)->DrawIndexedPrimitive(a,b,c,d,e)

+#define IDirect3DDevice8_DrawPrimitiveUP(p,a,b,c,d) (p)->DrawPrimitiveUP(a,b,c,d)

+#define IDirect3DDevice8_DrawIndexedPrimitiveUP(p,a,b,c,d,e,f,g,h) (p)->DrawIndexedPrimitiveUP(a,b,c,d,e,f,g,h)

+#define IDirect3DDevice8_ProcessVertices(p,a,b,c,d,e) (p)->ProcessVertices(a,b,c,d,e)

+#define IDirect3DDevice8_CreateVertexShader(p,a,b,c,d) (p)->CreateVertexShader(a,b,c,d)

+#define IDirect3DDevice8_SetVertexShader(p,a) (p)->SetVertexShader(a)

+#define IDirect3DDevice8_GetVertexShader(p,a) (p)->GetVertexShader(a)

+#define IDirect3DDevice8_DeleteVertexShader(p,a) (p)->DeleteVertexShader(a)

+#define IDirect3DDevice8_SetVertexShaderConstant(p,a,b,c) (p)->SetVertexShaderConstant(a,b,c)

+#define IDirect3DDevice8_GetVertexShaderConstant(p,a,b,c) (p)->GetVertexShaderConstant(a,b,c)

+#define IDirect3DDevice8_GetVertexShaderDeclaration(p,a,b,c) (p)->GetVertexShaderDeclaration(a,b,c)

+#define IDirect3DDevice8_GetVertexShaderFunction(p,a,b,c) (p)->GetVertexShaderFunction(a,b,c)

+#define IDirect3DDevice8_SetStreamSource(p,a,b,c) (p)->SetStreamSource(a,b,c)

+#define IDirect3DDevice8_GetStreamSource(p,a,b,c) (p)->GetStreamSource(a,b,c)

+#define IDirect3DDevice8_SetIndices(p,a,b) (p)->SetIndices(a,b)

+#define IDirect3DDevice8_GetIndices(p,a,b) (p)->GetIndices(a,b)

+#define IDirect3DDevice8_CreatePixelShader(p,a,b) (p)->CreatePixelShader(a,b)

+#define IDirect3DDevice8_SetPixelShader(p,a) (p)->SetPixelShader(a)

+#define IDirect3DDevice8_GetPixelShader(p,a) (p)->GetPixelShader(a)

+#define IDirect3DDevice8_DeletePixelShader(p,a) (p)->DeletePixelShader(a)

+#define IDirect3DDevice8_SetPixelShaderConstant(p,a,b,c) (p)->SetPixelShaderConstant(a,b,c)

+#define IDirect3DDevice8_GetPixelShaderConstant(p,a,b,c) (p)->GetPixelShaderConstant(a,b,c)

+#define IDirect3DDevice8_GetPixelShaderFunction(p,a,b,c) (p)->GetPixelShaderFunction(a,b,c)

+#define IDirect3DDevice8_DrawRectPatch(p,a,b,c) (p)->DrawRectPatch(a,b,c)

+#define IDirect3DDevice8_DrawTriPatch(p,a,b,c) (p)->DrawTriPatch(a,b,c)

+#define IDirect3DDevice8_DeletePatch(p,a) (p)->DeletePatch(a)

+#endif

+

+

+

+#undef INTERFACE

+#define INTERFACE IDirect3DSwapChain8

+

+DECLARE_INTERFACE_(IDirect3DSwapChain8, IUnknown)

+{

+    /*** IUnknown methods ***/

+    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;

+    STDMETHOD_(ULONG,AddRef)(THIS) PURE;

+    STDMETHOD_(ULONG,Release)(THIS) PURE;

+

+    /*** IDirect3DSwapChain8 methods ***/

+    STDMETHOD(Present)(THIS_ CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion) PURE;

+    STDMETHOD(GetBackBuffer)(THIS_ UINT BackBuffer,D3DBACKBUFFER_TYPE Type,IDirect3DSurface8** ppBackBuffer) PURE;

+};

+

+typedef struct IDirect3DSwapChain8 *LPDIRECT3DSWAPCHAIN8, *PDIRECT3DSWAPCHAIN8;

+

+#if !defined(__cplusplus) || defined(CINTERFACE)

+#define IDirect3DSwapChain8_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)

+#define IDirect3DSwapChain8_AddRef(p) (p)->lpVtbl->AddRef(p)

+#define IDirect3DSwapChain8_Release(p) (p)->lpVtbl->Release(p)

+#define IDirect3DSwapChain8_Present(p,a,b,c,d) (p)->lpVtbl->Present(p,a,b,c,d)

+#define IDirect3DSwapChain8_GetBackBuffer(p,a,b,c) (p)->lpVtbl->GetBackBuffer(p,a,b,c)

+#else

+#define IDirect3DSwapChain8_QueryInterface(p,a,b) (p)->QueryInterface(a,b)

+#define IDirect3DSwapChain8_AddRef(p) (p)->AddRef()

+#define IDirect3DSwapChain8_Release(p) (p)->Release()

+#define IDirect3DSwapChain8_Present(p,a,b,c,d) (p)->Present(a,b,c,d)

+#define IDirect3DSwapChain8_GetBackBuffer(p,a,b,c) (p)->GetBackBuffer(a,b,c)

+#endif

+

+

+

+#undef INTERFACE

+#define INTERFACE IDirect3DResource8

+

+DECLARE_INTERFACE_(IDirect3DResource8, IUnknown)

+{

+    /*** IUnknown methods ***/

+    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;

+    STDMETHOD_(ULONG,AddRef)(THIS) PURE;

+    STDMETHOD_(ULONG,Release)(THIS) PURE;

+

+    /*** IDirect3DResource8 methods ***/

+    STDMETHOD(GetDevice)(THIS_ IDirect3DDevice8** ppDevice) PURE;

+    STDMETHOD(SetPrivateData)(THIS_ REFGUID refguid,CONST void* pData,DWORD SizeOfData,DWORD Flags) PURE;

+    STDMETHOD(GetPrivateData)(THIS_ REFGUID refguid,void* pData,DWORD* pSizeOfData) PURE;

+    STDMETHOD(FreePrivateData)(THIS_ REFGUID refguid) PURE;

+    STDMETHOD_(DWORD, SetPriority)(THIS_ DWORD PriorityNew) PURE;

+    STDMETHOD_(DWORD, GetPriority)(THIS) PURE;

+    STDMETHOD_(void, PreLoad)(THIS) PURE;

+    STDMETHOD_(D3DRESOURCETYPE, GetType)(THIS) PURE;

+};

+

+typedef struct IDirect3DResource8 *LPDIRECT3DRESOURCE8, *PDIRECT3DRESOURCE8;

+

+#if !defined(__cplusplus) || defined(CINTERFACE)

+#define IDirect3DResource8_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)

+#define IDirect3DResource8_AddRef(p) (p)->lpVtbl->AddRef(p)

+#define IDirect3DResource8_Release(p) (p)->lpVtbl->Release(p)

+#define IDirect3DResource8_GetDevice(p,a) (p)->lpVtbl->GetDevice(p,a)

+#define IDirect3DResource8_SetPrivateData(p,a,b,c,d) (p)->lpVtbl->SetPrivateData(p,a,b,c,d)

+#define IDirect3DResource8_GetPrivateData(p,a,b,c) (p)->lpVtbl->GetPrivateData(p,a,b,c)

+#define IDirect3DResource8_FreePrivateData(p,a) (p)->lpVtbl->FreePrivateData(p,a)

+#define IDirect3DResource8_SetPriority(p,a) (p)->lpVtbl->SetPriority(p,a)

+#define IDirect3DResource8_GetPriority(p) (p)->lpVtbl->GetPriority(p)

+#define IDirect3DResource8_PreLoad(p) (p)->lpVtbl->PreLoad(p)

+#define IDirect3DResource8_GetType(p) (p)->lpVtbl->GetType(p)

+#else

+#define IDirect3DResource8_QueryInterface(p,a,b) (p)->QueryInterface(a,b)

+#define IDirect3DResource8_AddRef(p) (p)->AddRef()

+#define IDirect3DResource8_Release(p) (p)->Release()

+#define IDirect3DResource8_GetDevice(p,a) (p)->GetDevice(a)

+#define IDirect3DResource8_SetPrivateData(p,a,b,c,d) (p)->SetPrivateData(a,b,c,d)

+#define IDirect3DResource8_GetPrivateData(p,a,b,c) (p)->GetPrivateData(a,b,c)

+#define IDirect3DResource8_FreePrivateData(p,a) (p)->FreePrivateData(a)

+#define IDirect3DResource8_SetPriority(p,a) (p)->SetPriority(a)

+#define IDirect3DResource8_GetPriority(p) (p)->GetPriority()

+#define IDirect3DResource8_PreLoad(p) (p)->PreLoad()

+#define IDirect3DResource8_GetType(p) (p)->GetType()

+#endif

+

+

+

+

+#undef INTERFACE

+#define INTERFACE IDirect3DBaseTexture8

+

+DECLARE_INTERFACE_(IDirect3DBaseTexture8, IDirect3DResource8)

+{

+    /*** IUnknown methods ***/

+    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;

+    STDMETHOD_(ULONG,AddRef)(THIS) PURE;

+    STDMETHOD_(ULONG,Release)(THIS) PURE;

+

+    /*** IDirect3DResource8 methods ***/

+    STDMETHOD(GetDevice)(THIS_ IDirect3DDevice8** ppDevice) PURE;

+    STDMETHOD(SetPrivateData)(THIS_ REFGUID refguid,CONST void* pData,DWORD SizeOfData,DWORD Flags) PURE;

+    STDMETHOD(GetPrivateData)(THIS_ REFGUID refguid,void* pData,DWORD* pSizeOfData) PURE;

+    STDMETHOD(FreePrivateData)(THIS_ REFGUID refguid) PURE;

+    STDMETHOD_(DWORD, SetPriority)(THIS_ DWORD PriorityNew) PURE;

+    STDMETHOD_(DWORD, GetPriority)(THIS) PURE;

+    STDMETHOD_(void, PreLoad)(THIS) PURE;

+    STDMETHOD_(D3DRESOURCETYPE, GetType)(THIS) PURE;

+    STDMETHOD_(DWORD, SetLOD)(THIS_ DWORD LODNew) PURE;

+    STDMETHOD_(DWORD, GetLOD)(THIS) PURE;

+    STDMETHOD_(DWORD, GetLevelCount)(THIS) PURE;

+};

+

+typedef struct IDirect3DBaseTexture8 *LPDIRECT3DBASETEXTURE8, *PDIRECT3DBASETEXTURE8;

+

+#if !defined(__cplusplus) || defined(CINTERFACE)

+#define IDirect3DBaseTexture8_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)

+#define IDirect3DBaseTexture8_AddRef(p) (p)->lpVtbl->AddRef(p)

+#define IDirect3DBaseTexture8_Release(p) (p)->lpVtbl->Release(p)

+#define IDirect3DBaseTexture8_GetDevice(p,a) (p)->lpVtbl->GetDevice(p,a)

+#define IDirect3DBaseTexture8_SetPrivateData(p,a,b,c,d) (p)->lpVtbl->SetPrivateData(p,a,b,c,d)

+#define IDirect3DBaseTexture8_GetPrivateData(p,a,b,c) (p)->lpVtbl->GetPrivateData(p,a,b,c)

+#define IDirect3DBaseTexture8_FreePrivateData(p,a) (p)->lpVtbl->FreePrivateData(p,a)

+#define IDirect3DBaseTexture8_SetPriority(p,a) (p)->lpVtbl->SetPriority(p,a)

+#define IDirect3DBaseTexture8_GetPriority(p) (p)->lpVtbl->GetPriority(p)

+#define IDirect3DBaseTexture8_PreLoad(p) (p)->lpVtbl->PreLoad(p)

+#define IDirect3DBaseTexture8_GetType(p) (p)->lpVtbl->GetType(p)

+#define IDirect3DBaseTexture8_SetLOD(p,a) (p)->lpVtbl->SetLOD(p,a)

+#define IDirect3DBaseTexture8_GetLOD(p) (p)->lpVtbl->GetLOD(p)

+#define IDirect3DBaseTexture8_GetLevelCount(p) (p)->lpVtbl->GetLevelCount(p)

+#else

+#define IDirect3DBaseTexture8_QueryInterface(p,a,b) (p)->QueryInterface(a,b)

+#define IDirect3DBaseTexture8_AddRef(p) (p)->AddRef()

+#define IDirect3DBaseTexture8_Release(p) (p)->Release()

+#define IDirect3DBaseTexture8_GetDevice(p,a) (p)->GetDevice(a)

+#define IDirect3DBaseTexture8_SetPrivateData(p,a,b,c,d) (p)->SetPrivateData(a,b,c,d)

+#define IDirect3DBaseTexture8_GetPrivateData(p,a,b,c) (p)->GetPrivateData(a,b,c)

+#define IDirect3DBaseTexture8_FreePrivateData(p,a) (p)->FreePrivateData(a)

+#define IDirect3DBaseTexture8_SetPriority(p,a) (p)->SetPriority(a)

+#define IDirect3DBaseTexture8_GetPriority(p) (p)->GetPriority()

+#define IDirect3DBaseTexture8_PreLoad(p) (p)->PreLoad()

+#define IDirect3DBaseTexture8_GetType(p) (p)->GetType()

+#define IDirect3DBaseTexture8_SetLOD(p,a) (p)->SetLOD(a)

+#define IDirect3DBaseTexture8_GetLOD(p) (p)->GetLOD()

+#define IDirect3DBaseTexture8_GetLevelCount(p) (p)->GetLevelCount()

+#endif

+

+

+

+

+

+#undef INTERFACE

+#define INTERFACE IDirect3DTexture8

+

+DECLARE_INTERFACE_(IDirect3DTexture8, IDirect3DBaseTexture8)

+{

+    /*** IUnknown methods ***/

+    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;

+    STDMETHOD_(ULONG,AddRef)(THIS) PURE;

+    STDMETHOD_(ULONG,Release)(THIS) PURE;

+

+    /*** IDirect3DBaseTexture8 methods ***/

+    STDMETHOD(GetDevice)(THIS_ IDirect3DDevice8** ppDevice) PURE;

+    STDMETHOD(SetPrivateData)(THIS_ REFGUID refguid,CONST void* pData,DWORD SizeOfData,DWORD Flags) PURE;

+    STDMETHOD(GetPrivateData)(THIS_ REFGUID refguid,void* pData,DWORD* pSizeOfData) PURE;

+    STDMETHOD(FreePrivateData)(THIS_ REFGUID refguid) PURE;

+    STDMETHOD_(DWORD, SetPriority)(THIS_ DWORD PriorityNew) PURE;

+    STDMETHOD_(DWORD, GetPriority)(THIS) PURE;

+    STDMETHOD_(void, PreLoad)(THIS) PURE;

+    STDMETHOD_(D3DRESOURCETYPE, GetType)(THIS) PURE;

+    STDMETHOD_(DWORD, SetLOD)(THIS_ DWORD LODNew) PURE;

+    STDMETHOD_(DWORD, GetLOD)(THIS) PURE;

+    STDMETHOD_(DWORD, GetLevelCount)(THIS) PURE;

+    STDMETHOD(GetLevelDesc)(THIS_ UINT Level,D3DSURFACE_DESC *pDesc) PURE;

+    STDMETHOD(GetSurfaceLevel)(THIS_ UINT Level,IDirect3DSurface8** ppSurfaceLevel) PURE;

+    STDMETHOD(LockRect)(THIS_ UINT Level,D3DLOCKED_RECT* pLockedRect,CONST RECT* pRect,DWORD Flags) PURE;

+    STDMETHOD(UnlockRect)(THIS_ UINT Level) PURE;

+    STDMETHOD(AddDirtyRect)(THIS_ CONST RECT* pDirtyRect) PURE;

+};

+

+typedef struct IDirect3DTexture8 *LPDIRECT3DTEXTURE8, *PDIRECT3DTEXTURE8;

+

+#if !defined(__cplusplus) || defined(CINTERFACE)

+#define IDirect3DTexture8_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)

+#define IDirect3DTexture8_AddRef(p) (p)->lpVtbl->AddRef(p)

+#define IDirect3DTexture8_Release(p) (p)->lpVtbl->Release(p)

+#define IDirect3DTexture8_GetDevice(p,a) (p)->lpVtbl->GetDevice(p,a)

+#define IDirect3DTexture8_SetPrivateData(p,a,b,c,d) (p)->lpVtbl->SetPrivateData(p,a,b,c,d)

+#define IDirect3DTexture8_GetPrivateData(p,a,b,c) (p)->lpVtbl->GetPrivateData(p,a,b,c)

+#define IDirect3DTexture8_FreePrivateData(p,a) (p)->lpVtbl->FreePrivateData(p,a)

+#define IDirect3DTexture8_SetPriority(p,a) (p)->lpVtbl->SetPriority(p,a)

+#define IDirect3DTexture8_GetPriority(p) (p)->lpVtbl->GetPriority(p)

+#define IDirect3DTexture8_PreLoad(p) (p)->lpVtbl->PreLoad(p)

+#define IDirect3DTexture8_GetType(p) (p)->lpVtbl->GetType(p)

+#define IDirect3DTexture8_SetLOD(p,a) (p)->lpVtbl->SetLOD(p,a)

+#define IDirect3DTexture8_GetLOD(p) (p)->lpVtbl->GetLOD(p)

+#define IDirect3DTexture8_GetLevelCount(p) (p)->lpVtbl->GetLevelCount(p)

+#define IDirect3DTexture8_GetLevelDesc(p,a,b) (p)->lpVtbl->GetLevelDesc(p,a,b)

+#define IDirect3DTexture8_GetSurfaceLevel(p,a,b) (p)->lpVtbl->GetSurfaceLevel(p,a,b)

+#define IDirect3DTexture8_LockRect(p,a,b,c,d) (p)->lpVtbl->LockRect(p,a,b,c,d)

+#define IDirect3DTexture8_UnlockRect(p,a) (p)->lpVtbl->UnlockRect(p,a)

+#define IDirect3DTexture8_AddDirtyRect(p,a) (p)->lpVtbl->AddDirtyRect(p,a)

+#else

+#define IDirect3DTexture8_QueryInterface(p,a,b) (p)->QueryInterface(a,b)

+#define IDirect3DTexture8_AddRef(p) (p)->AddRef()

+#define IDirect3DTexture8_Release(p) (p)->Release()

+#define IDirect3DTexture8_GetDevice(p,a) (p)->GetDevice(a)

+#define IDirect3DTexture8_SetPrivateData(p,a,b,c,d) (p)->SetPrivateData(a,b,c,d)

+#define IDirect3DTexture8_GetPrivateData(p,a,b,c) (p)->GetPrivateData(a,b,c)

+#define IDirect3DTexture8_FreePrivateData(p,a) (p)->FreePrivateData(a)

+#define IDirect3DTexture8_SetPriority(p,a) (p)->SetPriority(a)

+#define IDirect3DTexture8_GetPriority(p) (p)->GetPriority()

+#define IDirect3DTexture8_PreLoad(p) (p)->PreLoad()

+#define IDirect3DTexture8_GetType(p) (p)->GetType()

+#define IDirect3DTexture8_SetLOD(p,a) (p)->SetLOD(a)

+#define IDirect3DTexture8_GetLOD(p) (p)->GetLOD()

+#define IDirect3DTexture8_GetLevelCount(p) (p)->GetLevelCount()

+#define IDirect3DTexture8_GetLevelDesc(p,a,b) (p)->GetLevelDesc(a,b)

+#define IDirect3DTexture8_GetSurfaceLevel(p,a,b) (p)->GetSurfaceLevel(a,b)

+#define IDirect3DTexture8_LockRect(p,a,b,c,d) (p)->LockRect(a,b,c,d)

+#define IDirect3DTexture8_UnlockRect(p,a) (p)->UnlockRect(a)

+#define IDirect3DTexture8_AddDirtyRect(p,a) (p)->AddDirtyRect(a)

+#endif

+

+

+

+

+

+#undef INTERFACE

+#define INTERFACE IDirect3DVolumeTexture8

+

+DECLARE_INTERFACE_(IDirect3DVolumeTexture8, IDirect3DBaseTexture8)

+{

+    /*** IUnknown methods ***/

+    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;

+    STDMETHOD_(ULONG,AddRef)(THIS) PURE;

+    STDMETHOD_(ULONG,Release)(THIS) PURE;

+

+    /*** IDirect3DBaseTexture8 methods ***/

+    STDMETHOD(GetDevice)(THIS_ IDirect3DDevice8** ppDevice) PURE;

+    STDMETHOD(SetPrivateData)(THIS_ REFGUID refguid,CONST void* pData,DWORD SizeOfData,DWORD Flags) PURE;

+    STDMETHOD(GetPrivateData)(THIS_ REFGUID refguid,void* pData,DWORD* pSizeOfData) PURE;

+    STDMETHOD(FreePrivateData)(THIS_ REFGUID refguid) PURE;

+    STDMETHOD_(DWORD, SetPriority)(THIS_ DWORD PriorityNew) PURE;

+    STDMETHOD_(DWORD, GetPriority)(THIS) PURE;

+    STDMETHOD_(void, PreLoad)(THIS) PURE;

+    STDMETHOD_(D3DRESOURCETYPE, GetType)(THIS) PURE;

+    STDMETHOD_(DWORD, SetLOD)(THIS_ DWORD LODNew) PURE;

+    STDMETHOD_(DWORD, GetLOD)(THIS) PURE;

+    STDMETHOD_(DWORD, GetLevelCount)(THIS) PURE;

+    STDMETHOD(GetLevelDesc)(THIS_ UINT Level,D3DVOLUME_DESC *pDesc) PURE;

+    STDMETHOD(GetVolumeLevel)(THIS_ UINT Level,IDirect3DVolume8** ppVolumeLevel) PURE;

+    STDMETHOD(LockBox)(THIS_ UINT Level,D3DLOCKED_BOX* pLockedVolume,CONST D3DBOX* pBox,DWORD Flags) PURE;

+    STDMETHOD(UnlockBox)(THIS_ UINT Level) PURE;

+    STDMETHOD(AddDirtyBox)(THIS_ CONST D3DBOX* pDirtyBox) PURE;

+};

+

+typedef struct IDirect3DVolumeTexture8 *LPDIRECT3DVOLUMETEXTURE8, *PDIRECT3DVOLUMETEXTURE8;

+

+#if !defined(__cplusplus) || defined(CINTERFACE)

+#define IDirect3DVolumeTexture8_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)

+#define IDirect3DVolumeTexture8_AddRef(p) (p)->lpVtbl->AddRef(p)

+#define IDirect3DVolumeTexture8_Release(p) (p)->lpVtbl->Release(p)

+#define IDirect3DVolumeTexture8_GetDevice(p,a) (p)->lpVtbl->GetDevice(p,a)

+#define IDirect3DVolumeTexture8_SetPrivateData(p,a,b,c,d) (p)->lpVtbl->SetPrivateData(p,a,b,c,d)

+#define IDirect3DVolumeTexture8_GetPrivateData(p,a,b,c) (p)->lpVtbl->GetPrivateData(p,a,b,c)

+#define IDirect3DVolumeTexture8_FreePrivateData(p,a) (p)->lpVtbl->FreePrivateData(p,a)

+#define IDirect3DVolumeTexture8_SetPriority(p,a) (p)->lpVtbl->SetPriority(p,a)

+#define IDirect3DVolumeTexture8_GetPriority(p) (p)->lpVtbl->GetPriority(p)

+#define IDirect3DVolumeTexture8_PreLoad(p) (p)->lpVtbl->PreLoad(p)

+#define IDirect3DVolumeTexture8_GetType(p) (p)->lpVtbl->GetType(p)

+#define IDirect3DVolumeTexture8_SetLOD(p,a) (p)->lpVtbl->SetLOD(p,a)

+#define IDirect3DVolumeTexture8_GetLOD(p) (p)->lpVtbl->GetLOD(p)

+#define IDirect3DVolumeTexture8_GetLevelCount(p) (p)->lpVtbl->GetLevelCount(p)

+#define IDirect3DVolumeTexture8_GetLevelDesc(p,a,b) (p)->lpVtbl->GetLevelDesc(p,a,b)

+#define IDirect3DVolumeTexture8_GetVolumeLevel(p,a,b) (p)->lpVtbl->GetVolumeLevel(p,a,b)

+#define IDirect3DVolumeTexture8_LockBox(p,a,b,c,d) (p)->lpVtbl->LockBox(p,a,b,c,d)

+#define IDirect3DVolumeTexture8_UnlockBox(p,a) (p)->lpVtbl->UnlockBox(p,a)

+#define IDirect3DVolumeTexture8_AddDirtyBox(p,a) (p)->lpVtbl->AddDirtyBox(p,a)

+#else

+#define IDirect3DVolumeTexture8_QueryInterface(p,a,b) (p)->QueryInterface(a,b)

+#define IDirect3DVolumeTexture8_AddRef(p) (p)->AddRef()

+#define IDirect3DVolumeTexture8_Release(p) (p)->Release()

+#define IDirect3DVolumeTexture8_GetDevice(p,a) (p)->GetDevice(a)

+#define IDirect3DVolumeTexture8_SetPrivateData(p,a,b,c,d) (p)->SetPrivateData(a,b,c,d)

+#define IDirect3DVolumeTexture8_GetPrivateData(p,a,b,c) (p)->GetPrivateData(a,b,c)

+#define IDirect3DVolumeTexture8_FreePrivateData(p,a) (p)->FreePrivateData(a)

+#define IDirect3DVolumeTexture8_SetPriority(p,a) (p)->SetPriority(a)

+#define IDirect3DVolumeTexture8_GetPriority(p) (p)->GetPriority()

+#define IDirect3DVolumeTexture8_PreLoad(p) (p)->PreLoad()

+#define IDirect3DVolumeTexture8_GetType(p) (p)->GetType()

+#define IDirect3DVolumeTexture8_SetLOD(p,a) (p)->SetLOD(a)

+#define IDirect3DVolumeTexture8_GetLOD(p) (p)->GetLOD()

+#define IDirect3DVolumeTexture8_GetLevelCount(p) (p)->GetLevelCount()

+#define IDirect3DVolumeTexture8_GetLevelDesc(p,a,b) (p)->GetLevelDesc(a,b)

+#define IDirect3DVolumeTexture8_GetVolumeLevel(p,a,b) (p)->GetVolumeLevel(a,b)

+#define IDirect3DVolumeTexture8_LockBox(p,a,b,c,d) (p)->LockBox(a,b,c,d)

+#define IDirect3DVolumeTexture8_UnlockBox(p,a) (p)->UnlockBox(a)

+#define IDirect3DVolumeTexture8_AddDirtyBox(p,a) (p)->AddDirtyBox(a)

+#endif

+

+

+

+

+

+#undef INTERFACE

+#define INTERFACE IDirect3DCubeTexture8

+

+DECLARE_INTERFACE_(IDirect3DCubeTexture8, IDirect3DBaseTexture8)

+{

+    /*** IUnknown methods ***/

+    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;

+    STDMETHOD_(ULONG,AddRef)(THIS) PURE;

+    STDMETHOD_(ULONG,Release)(THIS) PURE;

+

+    /*** IDirect3DBaseTexture8 methods ***/

+    STDMETHOD(GetDevice)(THIS_ IDirect3DDevice8** ppDevice) PURE;

+    STDMETHOD(SetPrivateData)(THIS_ REFGUID refguid,CONST void* pData,DWORD SizeOfData,DWORD Flags) PURE;

+    STDMETHOD(GetPrivateData)(THIS_ REFGUID refguid,void* pData,DWORD* pSizeOfData) PURE;

+    STDMETHOD(FreePrivateData)(THIS_ REFGUID refguid) PURE;

+    STDMETHOD_(DWORD, SetPriority)(THIS_ DWORD PriorityNew) PURE;

+    STDMETHOD_(DWORD, GetPriority)(THIS) PURE;

+    STDMETHOD_(void, PreLoad)(THIS) PURE;

+    STDMETHOD_(D3DRESOURCETYPE, GetType)(THIS) PURE;

+    STDMETHOD_(DWORD, SetLOD)(THIS_ DWORD LODNew) PURE;

+    STDMETHOD_(DWORD, GetLOD)(THIS) PURE;

+    STDMETHOD_(DWORD, GetLevelCount)(THIS) PURE;

+    STDMETHOD(GetLevelDesc)(THIS_ UINT Level,D3DSURFACE_DESC *pDesc) PURE;

+    STDMETHOD(GetCubeMapSurface)(THIS_ D3DCUBEMAP_FACES FaceType,UINT Level,IDirect3DSurface8** ppCubeMapSurface) PURE;

+    STDMETHOD(LockRect)(THIS_ D3DCUBEMAP_FACES FaceType,UINT Level,D3DLOCKED_RECT* pLockedRect,CONST RECT* pRect,DWORD Flags) PURE;

+    STDMETHOD(UnlockRect)(THIS_ D3DCUBEMAP_FACES FaceType,UINT Level) PURE;

+    STDMETHOD(AddDirtyRect)(THIS_ D3DCUBEMAP_FACES FaceType,CONST RECT* pDirtyRect) PURE;

+};

+

+typedef struct IDirect3DCubeTexture8 *LPDIRECT3DCUBETEXTURE8, *PDIRECT3DCUBETEXTURE8;

+

+#if !defined(__cplusplus) || defined(CINTERFACE)

+#define IDirect3DCubeTexture8_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)

+#define IDirect3DCubeTexture8_AddRef(p) (p)->lpVtbl->AddRef(p)

+#define IDirect3DCubeTexture8_Release(p) (p)->lpVtbl->Release(p)

+#define IDirect3DCubeTexture8_GetDevice(p,a) (p)->lpVtbl->GetDevice(p,a)

+#define IDirect3DCubeTexture8_SetPrivateData(p,a,b,c,d) (p)->lpVtbl->SetPrivateData(p,a,b,c,d)

+#define IDirect3DCubeTexture8_GetPrivateData(p,a,b,c) (p)->lpVtbl->GetPrivateData(p,a,b,c)

+#define IDirect3DCubeTexture8_FreePrivateData(p,a) (p)->lpVtbl->FreePrivateData(p,a)

+#define IDirect3DCubeTexture8_SetPriority(p,a) (p)->lpVtbl->SetPriority(p,a)

+#define IDirect3DCubeTexture8_GetPriority(p) (p)->lpVtbl->GetPriority(p)

+#define IDirect3DCubeTexture8_PreLoad(p) (p)->lpVtbl->PreLoad(p)

+#define IDirect3DCubeTexture8_GetType(p) (p)->lpVtbl->GetType(p)

+#define IDirect3DCubeTexture8_SetLOD(p,a) (p)->lpVtbl->SetLOD(p,a)

+#define IDirect3DCubeTexture8_GetLOD(p) (p)->lpVtbl->GetLOD(p)

+#define IDirect3DCubeTexture8_GetLevelCount(p) (p)->lpVtbl->GetLevelCount(p)

+#define IDirect3DCubeTexture8_GetLevelDesc(p,a,b) (p)->lpVtbl->GetLevelDesc(p,a,b)

+#define IDirect3DCubeTexture8_GetCubeMapSurface(p,a,b,c) (p)->lpVtbl->GetCubeMapSurface(p,a,b,c)

+#define IDirect3DCubeTexture8_LockRect(p,a,b,c,d,e) (p)->lpVtbl->LockRect(p,a,b,c,d,e)

+#define IDirect3DCubeTexture8_UnlockRect(p,a,b) (p)->lpVtbl->UnlockRect(p,a,b)

+#define IDirect3DCubeTexture8_AddDirtyRect(p,a,b) (p)->lpVtbl->AddDirtyRect(p,a,b)

+#else

+#define IDirect3DCubeTexture8_QueryInterface(p,a,b) (p)->QueryInterface(a,b)

+#define IDirect3DCubeTexture8_AddRef(p) (p)->AddRef()

+#define IDirect3DCubeTexture8_Release(p) (p)->Release()

+#define IDirect3DCubeTexture8_GetDevice(p,a) (p)->GetDevice(a)

+#define IDirect3DCubeTexture8_SetPrivateData(p,a,b,c,d) (p)->SetPrivateData(a,b,c,d)

+#define IDirect3DCubeTexture8_GetPrivateData(p,a,b,c) (p)->GetPrivateData(a,b,c)

+#define IDirect3DCubeTexture8_FreePrivateData(p,a) (p)->FreePrivateData(a)

+#define IDirect3DCubeTexture8_SetPriority(p,a) (p)->SetPriority(a)

+#define IDirect3DCubeTexture8_GetPriority(p) (p)->GetPriority()

+#define IDirect3DCubeTexture8_PreLoad(p) (p)->PreLoad()

+#define IDirect3DCubeTexture8_GetType(p) (p)->GetType()

+#define IDirect3DCubeTexture8_SetLOD(p,a) (p)->SetLOD(a)

+#define IDirect3DCubeTexture8_GetLOD(p) (p)->GetLOD()

+#define IDirect3DCubeTexture8_GetLevelCount(p) (p)->GetLevelCount()

+#define IDirect3DCubeTexture8_GetLevelDesc(p,a,b) (p)->GetLevelDesc(a,b)

+#define IDirect3DCubeTexture8_GetCubeMapSurface(p,a,b,c) (p)->GetCubeMapSurface(a,b,c)

+#define IDirect3DCubeTexture8_LockRect(p,a,b,c,d,e) (p)->LockRect(a,b,c,d,e)

+#define IDirect3DCubeTexture8_UnlockRect(p,a,b) (p)->UnlockRect(a,b)

+#define IDirect3DCubeTexture8_AddDirtyRect(p,a,b) (p)->AddDirtyRect(a,b)

+#endif

+

+

+

+

+#undef INTERFACE

+#define INTERFACE IDirect3DVertexBuffer8

+

+DECLARE_INTERFACE_(IDirect3DVertexBuffer8, IDirect3DResource8)

+{

+    /*** IUnknown methods ***/

+    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;

+    STDMETHOD_(ULONG,AddRef)(THIS) PURE;

+    STDMETHOD_(ULONG,Release)(THIS) PURE;

+

+    /*** IDirect3DResource8 methods ***/

+    STDMETHOD(GetDevice)(THIS_ IDirect3DDevice8** ppDevice) PURE;

+    STDMETHOD(SetPrivateData)(THIS_ REFGUID refguid,CONST void* pData,DWORD SizeOfData,DWORD Flags) PURE;

+    STDMETHOD(GetPrivateData)(THIS_ REFGUID refguid,void* pData,DWORD* pSizeOfData) PURE;

+    STDMETHOD(FreePrivateData)(THIS_ REFGUID refguid) PURE;

+    STDMETHOD_(DWORD, SetPriority)(THIS_ DWORD PriorityNew) PURE;

+    STDMETHOD_(DWORD, GetPriority)(THIS) PURE;

+    STDMETHOD_(void, PreLoad)(THIS) PURE;

+    STDMETHOD_(D3DRESOURCETYPE, GetType)(THIS) PURE;

+    STDMETHOD(Lock)(THIS_ UINT OffsetToLock,UINT SizeToLock,BYTE** ppbData,DWORD Flags) PURE;

+    STDMETHOD(Unlock)(THIS) PURE;

+    STDMETHOD(GetDesc)(THIS_ D3DVERTEXBUFFER_DESC *pDesc) PURE;

+};

+

+typedef struct IDirect3DVertexBuffer8 *LPDIRECT3DVERTEXBUFFER8, *PDIRECT3DVERTEXBUFFER8;

+

+#if !defined(__cplusplus) || defined(CINTERFACE)

+#define IDirect3DVertexBuffer8_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)

+#define IDirect3DVertexBuffer8_AddRef(p) (p)->lpVtbl->AddRef(p)

+#define IDirect3DVertexBuffer8_Release(p) (p)->lpVtbl->Release(p)

+#define IDirect3DVertexBuffer8_GetDevice(p,a) (p)->lpVtbl->GetDevice(p,a)

+#define IDirect3DVertexBuffer8_SetPrivateData(p,a,b,c,d) (p)->lpVtbl->SetPrivateData(p,a,b,c,d)

+#define IDirect3DVertexBuffer8_GetPrivateData(p,a,b,c) (p)->lpVtbl->GetPrivateData(p,a,b,c)

+#define IDirect3DVertexBuffer8_FreePrivateData(p,a) (p)->lpVtbl->FreePrivateData(p,a)

+#define IDirect3DVertexBuffer8_SetPriority(p,a) (p)->lpVtbl->SetPriority(p,a)

+#define IDirect3DVertexBuffer8_GetPriority(p) (p)->lpVtbl->GetPriority(p)

+#define IDirect3DVertexBuffer8_PreLoad(p) (p)->lpVtbl->PreLoad(p)

+#define IDirect3DVertexBuffer8_GetType(p) (p)->lpVtbl->GetType(p)

+#define IDirect3DVertexBuffer8_Lock(p,a,b,c,d) (p)->lpVtbl->Lock(p,a,b,c,d)

+#define IDirect3DVertexBuffer8_Unlock(p) (p)->lpVtbl->Unlock(p)

+#define IDirect3DVertexBuffer8_GetDesc(p,a) (p)->lpVtbl->GetDesc(p,a)

+#else

+#define IDirect3DVertexBuffer8_QueryInterface(p,a,b) (p)->QueryInterface(a,b)

+#define IDirect3DVertexBuffer8_AddRef(p) (p)->AddRef()

+#define IDirect3DVertexBuffer8_Release(p) (p)->Release()

+#define IDirect3DVertexBuffer8_GetDevice(p,a) (p)->GetDevice(a)

+#define IDirect3DVertexBuffer8_SetPrivateData(p,a,b,c,d) (p)->SetPrivateData(a,b,c,d)

+#define IDirect3DVertexBuffer8_GetPrivateData(p,a,b,c) (p)->GetPrivateData(a,b,c)

+#define IDirect3DVertexBuffer8_FreePrivateData(p,a) (p)->FreePrivateData(a)

+#define IDirect3DVertexBuffer8_SetPriority(p,a) (p)->SetPriority(a)

+#define IDirect3DVertexBuffer8_GetPriority(p) (p)->GetPriority()

+#define IDirect3DVertexBuffer8_PreLoad(p) (p)->PreLoad()

+#define IDirect3DVertexBuffer8_GetType(p) (p)->GetType()

+#define IDirect3DVertexBuffer8_Lock(p,a,b,c,d) (p)->Lock(a,b,c,d)

+#define IDirect3DVertexBuffer8_Unlock(p) (p)->Unlock()

+#define IDirect3DVertexBuffer8_GetDesc(p,a) (p)->GetDesc(a)

+#endif

+

+

+

+

+#undef INTERFACE

+#define INTERFACE IDirect3DIndexBuffer8

+

+DECLARE_INTERFACE_(IDirect3DIndexBuffer8, IDirect3DResource8)

+{

+    /*** IUnknown methods ***/

+    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;

+    STDMETHOD_(ULONG,AddRef)(THIS) PURE;

+    STDMETHOD_(ULONG,Release)(THIS) PURE;

+

+    /*** IDirect3DResource8 methods ***/

+    STDMETHOD(GetDevice)(THIS_ IDirect3DDevice8** ppDevice) PURE;

+    STDMETHOD(SetPrivateData)(THIS_ REFGUID refguid,CONST void* pData,DWORD SizeOfData,DWORD Flags) PURE;

+    STDMETHOD(GetPrivateData)(THIS_ REFGUID refguid,void* pData,DWORD* pSizeOfData) PURE;

+    STDMETHOD(FreePrivateData)(THIS_ REFGUID refguid) PURE;

+    STDMETHOD_(DWORD, SetPriority)(THIS_ DWORD PriorityNew) PURE;

+    STDMETHOD_(DWORD, GetPriority)(THIS) PURE;

+    STDMETHOD_(void, PreLoad)(THIS) PURE;

+    STDMETHOD_(D3DRESOURCETYPE, GetType)(THIS) PURE;

+    STDMETHOD(Lock)(THIS_ UINT OffsetToLock,UINT SizeToLock,BYTE** ppbData,DWORD Flags) PURE;

+    STDMETHOD(Unlock)(THIS) PURE;

+    STDMETHOD(GetDesc)(THIS_ D3DINDEXBUFFER_DESC *pDesc) PURE;

+};

+

+typedef struct IDirect3DIndexBuffer8 *LPDIRECT3DINDEXBUFFER8, *PDIRECT3DINDEXBUFFER8;

+

+#if !defined(__cplusplus) || defined(CINTERFACE)

+#define IDirect3DIndexBuffer8_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)

+#define IDirect3DIndexBuffer8_AddRef(p) (p)->lpVtbl->AddRef(p)

+#define IDirect3DIndexBuffer8_Release(p) (p)->lpVtbl->Release(p)

+#define IDirect3DIndexBuffer8_GetDevice(p,a) (p)->lpVtbl->GetDevice(p,a)

+#define IDirect3DIndexBuffer8_SetPrivateData(p,a,b,c,d) (p)->lpVtbl->SetPrivateData(p,a,b,c,d)

+#define IDirect3DIndexBuffer8_GetPrivateData(p,a,b,c) (p)->lpVtbl->GetPrivateData(p,a,b,c)

+#define IDirect3DIndexBuffer8_FreePrivateData(p,a) (p)->lpVtbl->FreePrivateData(p,a)

+#define IDirect3DIndexBuffer8_SetPriority(p,a) (p)->lpVtbl->SetPriority(p,a)

+#define IDirect3DIndexBuffer8_GetPriority(p) (p)->lpVtbl->GetPriority(p)

+#define IDirect3DIndexBuffer8_PreLoad(p) (p)->lpVtbl->PreLoad(p)

+#define IDirect3DIndexBuffer8_GetType(p) (p)->lpVtbl->GetType(p)

+#define IDirect3DIndexBuffer8_Lock(p,a,b,c,d) (p)->lpVtbl->Lock(p,a,b,c,d)

+#define IDirect3DIndexBuffer8_Unlock(p) (p)->lpVtbl->Unlock(p)

+#define IDirect3DIndexBuffer8_GetDesc(p,a) (p)->lpVtbl->GetDesc(p,a)

+#else

+#define IDirect3DIndexBuffer8_QueryInterface(p,a,b) (p)->QueryInterface(a,b)

+#define IDirect3DIndexBuffer8_AddRef(p) (p)->AddRef()

+#define IDirect3DIndexBuffer8_Release(p) (p)->Release()

+#define IDirect3DIndexBuffer8_GetDevice(p,a) (p)->GetDevice(a)

+#define IDirect3DIndexBuffer8_SetPrivateData(p,a,b,c,d) (p)->SetPrivateData(a,b,c,d)

+#define IDirect3DIndexBuffer8_GetPrivateData(p,a,b,c) (p)->GetPrivateData(a,b,c)

+#define IDirect3DIndexBuffer8_FreePrivateData(p,a) (p)->FreePrivateData(a)

+#define IDirect3DIndexBuffer8_SetPriority(p,a) (p)->SetPriority(a)

+#define IDirect3DIndexBuffer8_GetPriority(p) (p)->GetPriority()

+#define IDirect3DIndexBuffer8_PreLoad(p) (p)->PreLoad()

+#define IDirect3DIndexBuffer8_GetType(p) (p)->GetType()

+#define IDirect3DIndexBuffer8_Lock(p,a,b,c,d) (p)->Lock(a,b,c,d)

+#define IDirect3DIndexBuffer8_Unlock(p) (p)->Unlock()

+#define IDirect3DIndexBuffer8_GetDesc(p,a) (p)->GetDesc(a)

+#endif

+

+

+

+

+#undef INTERFACE

+#define INTERFACE IDirect3DSurface8

+

+DECLARE_INTERFACE_(IDirect3DSurface8, IUnknown)

+{

+    /*** IUnknown methods ***/

+    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;

+    STDMETHOD_(ULONG,AddRef)(THIS) PURE;

+    STDMETHOD_(ULONG,Release)(THIS) PURE;

+

+    /*** IDirect3DSurface8 methods ***/

+    STDMETHOD(GetDevice)(THIS_ IDirect3DDevice8** ppDevice) PURE;

+    STDMETHOD(SetPrivateData)(THIS_ REFGUID refguid,CONST void* pData,DWORD SizeOfData,DWORD Flags) PURE;

+    STDMETHOD(GetPrivateData)(THIS_ REFGUID refguid,void* pData,DWORD* pSizeOfData) PURE;

+    STDMETHOD(FreePrivateData)(THIS_ REFGUID refguid) PURE;

+    STDMETHOD(GetContainer)(THIS_ REFIID riid,void** ppContainer) PURE;

+    STDMETHOD(GetDesc)(THIS_ D3DSURFACE_DESC *pDesc) PURE;

+    STDMETHOD(LockRect)(THIS_ D3DLOCKED_RECT* pLockedRect,CONST RECT* pRect,DWORD Flags) PURE;

+    STDMETHOD(UnlockRect)(THIS) PURE;

+};

+

+typedef struct IDirect3DSurface8 *LPDIRECT3DSURFACE8, *PDIRECT3DSURFACE8;

+

+#if !defined(__cplusplus) || defined(CINTERFACE)

+#define IDirect3DSurface8_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)

+#define IDirect3DSurface8_AddRef(p) (p)->lpVtbl->AddRef(p)

+#define IDirect3DSurface8_Release(p) (p)->lpVtbl->Release(p)

+#define IDirect3DSurface8_GetDevice(p,a) (p)->lpVtbl->GetDevice(p,a)

+#define IDirect3DSurface8_SetPrivateData(p,a,b,c,d) (p)->lpVtbl->SetPrivateData(p,a,b,c,d)

+#define IDirect3DSurface8_GetPrivateData(p,a,b,c) (p)->lpVtbl->GetPrivateData(p,a,b,c)

+#define IDirect3DSurface8_FreePrivateData(p,a) (p)->lpVtbl->FreePrivateData(p,a)

+#define IDirect3DSurface8_GetContainer(p,a,b) (p)->lpVtbl->GetContainer(p,a,b)

+#define IDirect3DSurface8_GetDesc(p,a) (p)->lpVtbl->GetDesc(p,a)

+#define IDirect3DSurface8_LockRect(p,a,b,c) (p)->lpVtbl->LockRect(p,a,b,c)

+#define IDirect3DSurface8_UnlockRect(p) (p)->lpVtbl->UnlockRect(p)

+#else

+#define IDirect3DSurface8_QueryInterface(p,a,b) (p)->QueryInterface(a,b)

+#define IDirect3DSurface8_AddRef(p) (p)->AddRef()

+#define IDirect3DSurface8_Release(p) (p)->Release()

+#define IDirect3DSurface8_GetDevice(p,a) (p)->GetDevice(a)

+#define IDirect3DSurface8_SetPrivateData(p,a,b,c,d) (p)->SetPrivateData(a,b,c,d)

+#define IDirect3DSurface8_GetPrivateData(p,a,b,c) (p)->GetPrivateData(a,b,c)

+#define IDirect3DSurface8_FreePrivateData(p,a) (p)->FreePrivateData(a)

+#define IDirect3DSurface8_GetContainer(p,a,b) (p)->GetContainer(a,b)

+#define IDirect3DSurface8_GetDesc(p,a) (p)->GetDesc(a)

+#define IDirect3DSurface8_LockRect(p,a,b,c) (p)->LockRect(a,b,c)

+#define IDirect3DSurface8_UnlockRect(p) (p)->UnlockRect()

+#endif

+

+

+

+

+#undef INTERFACE

+#define INTERFACE IDirect3DVolume8

+

+DECLARE_INTERFACE_(IDirect3DVolume8, IUnknown)

+{

+    /*** IUnknown methods ***/

+    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;

+    STDMETHOD_(ULONG,AddRef)(THIS) PURE;

+    STDMETHOD_(ULONG,Release)(THIS) PURE;

+

+    /*** IDirect3DVolume8 methods ***/

+    STDMETHOD(GetDevice)(THIS_ IDirect3DDevice8** ppDevice) PURE;

+    STDMETHOD(SetPrivateData)(THIS_ REFGUID refguid,CONST void* pData,DWORD SizeOfData,DWORD Flags) PURE;

+    STDMETHOD(GetPrivateData)(THIS_ REFGUID refguid,void* pData,DWORD* pSizeOfData) PURE;

+    STDMETHOD(FreePrivateData)(THIS_ REFGUID refguid) PURE;

+    STDMETHOD(GetContainer)(THIS_ REFIID riid,void** ppContainer) PURE;

+    STDMETHOD(GetDesc)(THIS_ D3DVOLUME_DESC *pDesc) PURE;

+    STDMETHOD(LockBox)(THIS_ D3DLOCKED_BOX * pLockedVolume,CONST D3DBOX* pBox,DWORD Flags) PURE;

+    STDMETHOD(UnlockBox)(THIS) PURE;

+};

+

+typedef struct IDirect3DVolume8 *LPDIRECT3DVOLUME8, *PDIRECT3DVOLUME8;

+

+#if !defined(__cplusplus) || defined(CINTERFACE)

+#define IDirect3DVolume8_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)

+#define IDirect3DVolume8_AddRef(p) (p)->lpVtbl->AddRef(p)

+#define IDirect3DVolume8_Release(p) (p)->lpVtbl->Release(p)

+#define IDirect3DVolume8_GetDevice(p,a) (p)->lpVtbl->GetDevice(p,a)

+#define IDirect3DVolume8_SetPrivateData(p,a,b,c,d) (p)->lpVtbl->SetPrivateData(p,a,b,c,d)

+#define IDirect3DVolume8_GetPrivateData(p,a,b,c) (p)->lpVtbl->GetPrivateData(p,a,b,c)

+#define IDirect3DVolume8_FreePrivateData(p,a) (p)->lpVtbl->FreePrivateData(p,a)

+#define IDirect3DVolume8_GetContainer(p,a,b) (p)->lpVtbl->GetContainer(p,a,b)

+#define IDirect3DVolume8_GetDesc(p,a) (p)->lpVtbl->GetDesc(p,a)

+#define IDirect3DVolume8_LockBox(p,a,b,c) (p)->lpVtbl->LockBox(p,a,b,c)

+#define IDirect3DVolume8_UnlockBox(p) (p)->lpVtbl->UnlockBox(p)

+#else

+#define IDirect3DVolume8_QueryInterface(p,a,b) (p)->QueryInterface(a,b)

+#define IDirect3DVolume8_AddRef(p) (p)->AddRef()

+#define IDirect3DVolume8_Release(p) (p)->Release()

+#define IDirect3DVolume8_GetDevice(p,a) (p)->GetDevice(a)

+#define IDirect3DVolume8_SetPrivateData(p,a,b,c,d) (p)->SetPrivateData(a,b,c,d)

+#define IDirect3DVolume8_GetPrivateData(p,a,b,c) (p)->GetPrivateData(a,b,c)

+#define IDirect3DVolume8_FreePrivateData(p,a) (p)->FreePrivateData(a)

+#define IDirect3DVolume8_GetContainer(p,a,b) (p)->GetContainer(a,b)

+#define IDirect3DVolume8_GetDesc(p,a) (p)->GetDesc(a)

+#define IDirect3DVolume8_LockBox(p,a,b,c) (p)->LockBox(a,b,c)

+#define IDirect3DVolume8_UnlockBox(p) (p)->UnlockBox()

+#endif

+

+/****************************************************************************

+ * Flags for SetPrivateData method on all D3D8 interfaces

+ *

+ * The passed pointer is an IUnknown ptr. The SizeOfData argument to SetPrivateData

+ * must be set to sizeof(IUnknown*). Direct3D will call AddRef through this

+ * pointer and Release when the private data is destroyed. The data will be

+ * destroyed when another SetPrivateData with the same GUID is set, when

+ * FreePrivateData is called, or when the D3D8 object is freed.

+ ****************************************************************************/

+#define D3DSPD_IUNKNOWN                         0x00000001L

+

+/****************************************************************************

+ *

+ * Parameter for IDirect3D8 Enum and GetCaps8 functions to get the info for

+ * the current mode only.

+ *

+ ****************************************************************************/

+

+#define D3DCURRENT_DISPLAY_MODE                 0x00EFFFFFL

+

+/****************************************************************************

+ *

+ * Flags for IDirect3D8::CreateDevice's BehaviorFlags

+ *

+ ****************************************************************************/

+

+#define D3DCREATE_FPU_PRESERVE                  0x00000002L

+#define D3DCREATE_MULTITHREADED                 0x00000004L

+

+#define D3DCREATE_PUREDEVICE                    0x00000010L

+#define D3DCREATE_SOFTWARE_VERTEXPROCESSING     0x00000020L

+#define D3DCREATE_HARDWARE_VERTEXPROCESSING     0x00000040L

+#define D3DCREATE_MIXED_VERTEXPROCESSING        0x00000080L

+

+#define D3DCREATE_DISABLE_DRIVER_MANAGEMENT     0x00000100L

+

+

+/****************************************************************************

+ *

+ * Parameter for IDirect3D8::CreateDevice's iAdapter

+ *

+ ****************************************************************************/

+

+#define D3DADAPTER_DEFAULT                     0

+

+/****************************************************************************

+ *

+ * Flags for IDirect3D8::EnumAdapters

+ *

+ ****************************************************************************/

+

+#define D3DENUM_NO_WHQL_LEVEL                   0x00000002L

+

+/****************************************************************************

+ *

+ * Maximum number of back-buffers supported in DX8

+ *

+ ****************************************************************************/

+

+#define D3DPRESENT_BACK_BUFFERS_MAX             3L

+

+/****************************************************************************

+ *

+ * Flags for IDirect3DDevice8::SetGammaRamp

+ *

+ ****************************************************************************/

+

+#define D3DSGR_NO_CALIBRATION                  0x00000000L

+#define D3DSGR_CALIBRATE                       0x00000001L

+

+/****************************************************************************

+ *

+ * Flags for IDirect3DDevice8::SetCursorPosition

+ *

+ ****************************************************************************/

+

+#define D3DCURSOR_IMMEDIATE_UPDATE             0x00000001L

+

+/****************************************************************************

+ *

+ * Flags for DrawPrimitive/DrawIndexedPrimitive

+ *   Also valid for Begin/BeginIndexed

+ *   Also valid for VertexBuffer::CreateVertexBuffer

+ ****************************************************************************/

+

+

+/*

+ *  DirectDraw error codes

+ */

+#define _FACD3D  0x876

+#define MAKE_D3DHRESULT( code )  MAKE_HRESULT( 1, _FACD3D, code )

+

+/*

+ * Direct3D Errors

+ */

+#define D3D_OK                              S_OK

+

+#define D3DERR_WRONGTEXTUREFORMAT               MAKE_D3DHRESULT(2072)

+#define D3DERR_UNSUPPORTEDCOLOROPERATION        MAKE_D3DHRESULT(2073)

+#define D3DERR_UNSUPPORTEDCOLORARG              MAKE_D3DHRESULT(2074)

+#define D3DERR_UNSUPPORTEDALPHAOPERATION        MAKE_D3DHRESULT(2075)

+#define D3DERR_UNSUPPORTEDALPHAARG              MAKE_D3DHRESULT(2076)

+#define D3DERR_TOOMANYOPERATIONS                MAKE_D3DHRESULT(2077)

+#define D3DERR_CONFLICTINGTEXTUREFILTER         MAKE_D3DHRESULT(2078)

+#define D3DERR_UNSUPPORTEDFACTORVALUE           MAKE_D3DHRESULT(2079)

+#define D3DERR_CONFLICTINGRENDERSTATE           MAKE_D3DHRESULT(2081)

+#define D3DERR_UNSUPPORTEDTEXTUREFILTER         MAKE_D3DHRESULT(2082)

+#define D3DERR_CONFLICTINGTEXTUREPALETTE        MAKE_D3DHRESULT(2086)

+#define D3DERR_DRIVERINTERNALERROR              MAKE_D3DHRESULT(2087)

+

+#define D3DERR_NOTFOUND                         MAKE_D3DHRESULT(2150)

+#define D3DERR_MOREDATA                         MAKE_D3DHRESULT(2151)

+#define D3DERR_DEVICELOST                       MAKE_D3DHRESULT(2152)

+#define D3DERR_DEVICENOTRESET                   MAKE_D3DHRESULT(2153)

+#define D3DERR_NOTAVAILABLE                     MAKE_D3DHRESULT(2154)

+#define D3DERR_OUTOFVIDEOMEMORY                 MAKE_D3DHRESULT(380)

+#define D3DERR_INVALIDDEVICE                    MAKE_D3DHRESULT(2155)

+#define D3DERR_INVALIDCALL                      MAKE_D3DHRESULT(2156)

+#define D3DERR_DRIVERINVALIDCALL                MAKE_D3DHRESULT(2157)

+

+#ifdef __cplusplus

+};

+#endif

+

+#endif /* (DIRECT3D_VERSION >= 0x0800) */

+#endif /* _D3D_H_ */

+

diff --git a/include/Direct3D/d3d8caps.h b/include/Direct3D/d3d8caps.h
new file mode 100644
index 0000000..6a7dc11
--- /dev/null
+++ b/include/Direct3D/d3d8caps.h
@@ -0,0 +1,364 @@
+/*==========================================================================;

+ *

+ *  Copyright (C) Microsoft Corporation.  All Rights Reserved.

+ *

+ *  File:       d3d8caps.h

+ *  Content:    Direct3D capabilities include file

+ *

+ ***************************************************************************/

+

+#ifndef _D3D8CAPS_H

+#define _D3D8CAPS_H

+

+#ifndef DIRECT3D_VERSION

+#define DIRECT3D_VERSION         0x0800

+#endif  //DIRECT3D_VERSION

+

+// include this file content only if compiling for DX8 interfaces

+#if(DIRECT3D_VERSION >= 0x0800)

+

+#if defined(_X86_) || defined(_IA64_)

+#pragma pack(4)

+#endif

+

+typedef struct _D3DCAPS8

+{

+    /* Device Info */

+    D3DDEVTYPE  DeviceType;

+    UINT    AdapterOrdinal;

+

+    /* Caps from DX7 Draw */

+    DWORD   Caps;

+    DWORD   Caps2;

+    DWORD   Caps3;

+    DWORD   PresentationIntervals;

+

+    /* Cursor Caps */

+    DWORD   CursorCaps;

+

+    /* 3D Device Caps */

+    DWORD   DevCaps;

+

+    DWORD   PrimitiveMiscCaps;

+    DWORD   RasterCaps;

+    DWORD   ZCmpCaps;

+    DWORD   SrcBlendCaps;

+    DWORD   DestBlendCaps;

+    DWORD   AlphaCmpCaps;

+    DWORD   ShadeCaps;

+    DWORD   TextureCaps;

+    DWORD   TextureFilterCaps;          // D3DPTFILTERCAPS for IDirect3DTexture8's

+    DWORD   CubeTextureFilterCaps;      // D3DPTFILTERCAPS for IDirect3DCubeTexture8's

+    DWORD   VolumeTextureFilterCaps;    // D3DPTFILTERCAPS for IDirect3DVolumeTexture8's

+    DWORD   TextureAddressCaps;         // D3DPTADDRESSCAPS for IDirect3DTexture8's

+    DWORD   VolumeTextureAddressCaps;   // D3DPTADDRESSCAPS for IDirect3DVolumeTexture8's

+

+    DWORD   LineCaps;                   // D3DLINECAPS

+

+    DWORD   MaxTextureWidth, MaxTextureHeight;

+    DWORD   MaxVolumeExtent;

+

+    DWORD   MaxTextureRepeat;

+    DWORD   MaxTextureAspectRatio;

+    DWORD   MaxAnisotropy;

+    float   MaxVertexW;

+

+    float   GuardBandLeft;

+    float   GuardBandTop;

+    float   GuardBandRight;

+    float   GuardBandBottom;

+

+    float   ExtentsAdjust;

+    DWORD   StencilCaps;

+

+    DWORD   FVFCaps;

+    DWORD   TextureOpCaps;

+    DWORD   MaxTextureBlendStages;

+    DWORD   MaxSimultaneousTextures;

+

+    DWORD   VertexProcessingCaps;

+    DWORD   MaxActiveLights;

+    DWORD   MaxUserClipPlanes;

+    DWORD   MaxVertexBlendMatrices;

+    DWORD   MaxVertexBlendMatrixIndex;

+

+    float   MaxPointSize;

+

+    DWORD   MaxPrimitiveCount;          // max number of primitives per DrawPrimitive call

+    DWORD   MaxVertexIndex;

+    DWORD   MaxStreams;

+    DWORD   MaxStreamStride;            // max stride for SetStreamSource

+

+    DWORD   VertexShaderVersion;

+    DWORD   MaxVertexShaderConst;       // number of vertex shader constant registers

+

+    DWORD   PixelShaderVersion;

+    float   MaxPixelShaderValue;        // max value of pixel shader arithmetic component

+

+} D3DCAPS8;

+

+//

+// BIT DEFINES FOR D3DCAPS8 DWORD MEMBERS

+//

+

+//

+// Caps

+//

+#define D3DCAPS_READ_SCANLINE           0x00020000L

+

+//

+// Caps2

+//

+#define D3DCAPS2_NO2DDURING3DSCENE      0x00000002L

+#define D3DCAPS2_FULLSCREENGAMMA        0x00020000L

+#define D3DCAPS2_CANRENDERWINDOWED      0x00080000L

+#define D3DCAPS2_CANCALIBRATEGAMMA      0x00100000L

+#define D3DCAPS2_RESERVED               0x02000000L

+#define D3DCAPS2_CANMANAGERESOURCE      0x10000000L

+#define D3DCAPS2_DYNAMICTEXTURES        0x20000000L

+

+//

+// Caps3

+//

+#define D3DCAPS3_RESERVED               0x8000001fL

+

+// Indicates that the device can respect the ALPHABLENDENABLE render state

+// when fullscreen while using the FLIP or DISCARD swap effect.

+// COPY and COPYVSYNC swap effects work whether or not this flag is set.

+#define D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD   0x00000020L

+

+//

+// PresentationIntervals

+//

+#define D3DPRESENT_INTERVAL_DEFAULT     0x00000000L

+#define D3DPRESENT_INTERVAL_ONE         0x00000001L

+#define D3DPRESENT_INTERVAL_TWO         0x00000002L

+#define D3DPRESENT_INTERVAL_THREE       0x00000004L

+#define D3DPRESENT_INTERVAL_FOUR        0x00000008L

+#define D3DPRESENT_INTERVAL_IMMEDIATE   0x80000000L

+

+//

+// CursorCaps

+//

+// Driver supports HW color cursor in at least hi-res modes(height >=400)

+#define D3DCURSORCAPS_COLOR             0x00000001L

+// Driver supports HW cursor also in low-res modes(height < 400)

+#define D3DCURSORCAPS_LOWRES            0x00000002L

+

+//

+// DevCaps

+//

+#define D3DDEVCAPS_EXECUTESYSTEMMEMORY  0x00000010L /* Device can use execute buffers from system memory */

+#define D3DDEVCAPS_EXECUTEVIDEOMEMORY   0x00000020L /* Device can use execute buffers from video memory */

+#define D3DDEVCAPS_TLVERTEXSYSTEMMEMORY 0x00000040L /* Device can use TL buffers from system memory */

+#define D3DDEVCAPS_TLVERTEXVIDEOMEMORY  0x00000080L /* Device can use TL buffers from video memory */

+#define D3DDEVCAPS_TEXTURESYSTEMMEMORY  0x00000100L /* Device can texture from system memory */

+#define D3DDEVCAPS_TEXTUREVIDEOMEMORY   0x00000200L /* Device can texture from device memory */

+#define D3DDEVCAPS_DRAWPRIMTLVERTEX     0x00000400L /* Device can draw TLVERTEX primitives */

+#define D3DDEVCAPS_CANRENDERAFTERFLIP   0x00000800L /* Device can render without waiting for flip to complete */

+#define D3DDEVCAPS_TEXTURENONLOCALVIDMEM 0x00001000L /* Device can texture from nonlocal video memory */

+#define D3DDEVCAPS_DRAWPRIMITIVES2      0x00002000L /* Device can support DrawPrimitives2 */

+#define D3DDEVCAPS_SEPARATETEXTUREMEMORIES 0x00004000L /* Device is texturing from separate memory pools */

+#define D3DDEVCAPS_DRAWPRIMITIVES2EX    0x00008000L /* Device can support Extended DrawPrimitives2 i.e. DX7 compliant driver*/

+#define D3DDEVCAPS_HWTRANSFORMANDLIGHT  0x00010000L /* Device can support transformation and lighting in hardware and DRAWPRIMITIVES2EX must be also */

+#define D3DDEVCAPS_CANBLTSYSTONONLOCAL  0x00020000L /* Device supports a Tex Blt from system memory to non-local vidmem */

+#define D3DDEVCAPS_HWRASTERIZATION      0x00080000L /* Device has HW acceleration for rasterization */

+#define D3DDEVCAPS_PUREDEVICE           0x00100000L /* Device supports D3DCREATE_PUREDEVICE */

+#define D3DDEVCAPS_QUINTICRTPATCHES     0x00200000L /* Device supports quintic Beziers and BSplines */

+#define D3DDEVCAPS_RTPATCHES            0x00400000L /* Device supports Rect and Tri patches */

+#define D3DDEVCAPS_RTPATCHHANDLEZERO    0x00800000L /* Indicates that RT Patches may be drawn efficiently using handle 0 */

+#define D3DDEVCAPS_NPATCHES             0x01000000L /* Device supports N-Patches */

+

+//

+// PrimitiveMiscCaps

+//

+#define D3DPMISCCAPS_MASKZ              0x00000002L

+#define D3DPMISCCAPS_LINEPATTERNREP     0x00000004L

+#define D3DPMISCCAPS_CULLNONE           0x00000010L

+#define D3DPMISCCAPS_CULLCW             0x00000020L

+#define D3DPMISCCAPS_CULLCCW            0x00000040L

+#define D3DPMISCCAPS_COLORWRITEENABLE   0x00000080L

+#define D3DPMISCCAPS_CLIPPLANESCALEDPOINTS 0x00000100L /* Device correctly clips scaled points to clip planes */

+#define D3DPMISCCAPS_CLIPTLVERTS        0x00000200L /* device will clip post-transformed vertex primitives */

+#define D3DPMISCCAPS_TSSARGTEMP         0x00000400L /* device supports D3DTA_TEMP for temporary register */

+#define D3DPMISCCAPS_BLENDOP            0x00000800L /* device supports D3DRS_BLENDOP */

+#define D3DPMISCCAPS_NULLREFERENCE      0x00001000L /* Reference Device that doesnt render */

+

+//

+// LineCaps

+//

+#define D3DLINECAPS_TEXTURE             0x00000001L

+#define D3DLINECAPS_ZTEST               0x00000002L

+#define D3DLINECAPS_BLEND               0x00000004L

+#define D3DLINECAPS_ALPHACMP            0x00000008L

+#define D3DLINECAPS_FOG                 0x00000010L

+

+//

+// RasterCaps

+//

+#define D3DPRASTERCAPS_DITHER           0x00000001L

+#define D3DPRASTERCAPS_PAT              0x00000008L

+#define D3DPRASTERCAPS_ZTEST            0x00000010L

+#define D3DPRASTERCAPS_FOGVERTEX        0x00000080L

+#define D3DPRASTERCAPS_FOGTABLE         0x00000100L

+#define D3DPRASTERCAPS_ANTIALIASEDGES   0x00001000L

+#define D3DPRASTERCAPS_MIPMAPLODBIAS    0x00002000L

+#define D3DPRASTERCAPS_ZBIAS            0x00004000L

+#define D3DPRASTERCAPS_ZBUFFERLESSHSR   0x00008000L

+#define D3DPRASTERCAPS_FOGRANGE         0x00010000L

+#define D3DPRASTERCAPS_ANISOTROPY       0x00020000L

+#define D3DPRASTERCAPS_WBUFFER          0x00040000L

+#define D3DPRASTERCAPS_WFOG             0x00100000L

+#define D3DPRASTERCAPS_ZFOG             0x00200000L

+#define D3DPRASTERCAPS_COLORPERSPECTIVE 0x00400000L /* Device iterates colors perspective correct */

+#define D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE  0x00800000L

+

+//

+// ZCmpCaps, AlphaCmpCaps

+//

+#define D3DPCMPCAPS_NEVER               0x00000001L

+#define D3DPCMPCAPS_LESS                0x00000002L

+#define D3DPCMPCAPS_EQUAL               0x00000004L

+#define D3DPCMPCAPS_LESSEQUAL           0x00000008L

+#define D3DPCMPCAPS_GREATER             0x00000010L

+#define D3DPCMPCAPS_NOTEQUAL            0x00000020L

+#define D3DPCMPCAPS_GREATEREQUAL        0x00000040L

+#define D3DPCMPCAPS_ALWAYS              0x00000080L

+

+//

+// SourceBlendCaps, DestBlendCaps

+//

+#define D3DPBLENDCAPS_ZERO              0x00000001L

+#define D3DPBLENDCAPS_ONE               0x00000002L

+#define D3DPBLENDCAPS_SRCCOLOR          0x00000004L

+#define D3DPBLENDCAPS_INVSRCCOLOR       0x00000008L

+#define D3DPBLENDCAPS_SRCALPHA          0x00000010L

+#define D3DPBLENDCAPS_INVSRCALPHA       0x00000020L

+#define D3DPBLENDCAPS_DESTALPHA         0x00000040L

+#define D3DPBLENDCAPS_INVDESTALPHA      0x00000080L

+#define D3DPBLENDCAPS_DESTCOLOR         0x00000100L

+#define D3DPBLENDCAPS_INVDESTCOLOR      0x00000200L

+#define D3DPBLENDCAPS_SRCALPHASAT       0x00000400L

+#define D3DPBLENDCAPS_BOTHSRCALPHA      0x00000800L

+#define D3DPBLENDCAPS_BOTHINVSRCALPHA   0x00001000L

+

+//

+// ShadeCaps

+//

+#define D3DPSHADECAPS_COLORGOURAUDRGB       0x00000008L

+#define D3DPSHADECAPS_SPECULARGOURAUDRGB    0x00000200L

+#define D3DPSHADECAPS_ALPHAGOURAUDBLEND     0x00004000L

+#define D3DPSHADECAPS_FOGGOURAUD            0x00080000L

+

+//

+// TextureCaps

+//

+#define D3DPTEXTURECAPS_PERSPECTIVE         0x00000001L /* Perspective-correct texturing is supported */

+#define D3DPTEXTURECAPS_POW2                0x00000002L /* Power-of-2 texture dimensions are required - applies to non-Cube/Volume textures only. */

+#define D3DPTEXTURECAPS_ALPHA               0x00000004L /* Alpha in texture pixels is supported */

+#define D3DPTEXTURECAPS_SQUAREONLY          0x00000020L /* Only square textures are supported */

+#define D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE 0x00000040L /* Texture indices are not scaled by the texture size prior to interpolation */

+#define D3DPTEXTURECAPS_ALPHAPALETTE        0x00000080L /* Device can draw alpha from texture palettes */

+// Device can use non-POW2 textures if:

+//  1) D3DTEXTURE_ADDRESS is set to CLAMP for this texture's stage

+//  2) D3DRS_WRAP(N) is zero for this texture's coordinates

+//  3) mip mapping is not enabled (use magnification filter only)

+#define D3DPTEXTURECAPS_NONPOW2CONDITIONAL  0x00000100L

+#define D3DPTEXTURECAPS_PROJECTED           0x00000400L /* Device can do D3DTTFF_PROJECTED */

+#define D3DPTEXTURECAPS_CUBEMAP             0x00000800L /* Device can do cubemap textures */

+#define D3DPTEXTURECAPS_VOLUMEMAP           0x00002000L /* Device can do volume textures */

+#define D3DPTEXTURECAPS_MIPMAP              0x00004000L /* Device can do mipmapped textures */

+#define D3DPTEXTURECAPS_MIPVOLUMEMAP        0x00008000L /* Device can do mipmapped volume textures */

+#define D3DPTEXTURECAPS_MIPCUBEMAP          0x00010000L /* Device can do mipmapped cube maps */

+#define D3DPTEXTURECAPS_CUBEMAP_POW2        0x00020000L /* Device requires that cubemaps be power-of-2 dimension */

+#define D3DPTEXTURECAPS_VOLUMEMAP_POW2      0x00040000L /* Device requires that volume maps be power-of-2 dimension */

+

+//

+// TextureFilterCaps

+//

+#define D3DPTFILTERCAPS_MINFPOINT           0x00000100L /* Min Filter */

+#define D3DPTFILTERCAPS_MINFLINEAR          0x00000200L

+#define D3DPTFILTERCAPS_MINFANISOTROPIC     0x00000400L

+#define D3DPTFILTERCAPS_MIPFPOINT           0x00010000L /* Mip Filter */

+#define D3DPTFILTERCAPS_MIPFLINEAR          0x00020000L

+#define D3DPTFILTERCAPS_MAGFPOINT           0x01000000L /* Mag Filter */

+#define D3DPTFILTERCAPS_MAGFLINEAR          0x02000000L

+#define D3DPTFILTERCAPS_MAGFANISOTROPIC     0x04000000L

+#define D3DPTFILTERCAPS_MAGFAFLATCUBIC      0x08000000L

+#define D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC   0x10000000L

+

+//

+// TextureAddressCaps

+//

+#define D3DPTADDRESSCAPS_WRAP           0x00000001L

+#define D3DPTADDRESSCAPS_MIRROR         0x00000002L

+#define D3DPTADDRESSCAPS_CLAMP          0x00000004L

+#define D3DPTADDRESSCAPS_BORDER         0x00000008L

+#define D3DPTADDRESSCAPS_INDEPENDENTUV  0x00000010L

+#define D3DPTADDRESSCAPS_MIRRORONCE     0x00000020L

+

+//

+// StencilCaps

+//

+#define D3DSTENCILCAPS_KEEP             0x00000001L

+#define D3DSTENCILCAPS_ZERO             0x00000002L

+#define D3DSTENCILCAPS_REPLACE          0x00000004L

+#define D3DSTENCILCAPS_INCRSAT          0x00000008L

+#define D3DSTENCILCAPS_DECRSAT          0x00000010L

+#define D3DSTENCILCAPS_INVERT           0x00000020L

+#define D3DSTENCILCAPS_INCR             0x00000040L

+#define D3DSTENCILCAPS_DECR             0x00000080L

+

+//

+// TextureOpCaps

+//

+#define D3DTEXOPCAPS_DISABLE                    0x00000001L

+#define D3DTEXOPCAPS_SELECTARG1                 0x00000002L

+#define D3DTEXOPCAPS_SELECTARG2                 0x00000004L

+#define D3DTEXOPCAPS_MODULATE                   0x00000008L

+#define D3DTEXOPCAPS_MODULATE2X                 0x00000010L

+#define D3DTEXOPCAPS_MODULATE4X                 0x00000020L

+#define D3DTEXOPCAPS_ADD                        0x00000040L

+#define D3DTEXOPCAPS_ADDSIGNED                  0x00000080L

+#define D3DTEXOPCAPS_ADDSIGNED2X                0x00000100L

+#define D3DTEXOPCAPS_SUBTRACT                   0x00000200L

+#define D3DTEXOPCAPS_ADDSMOOTH                  0x00000400L

+#define D3DTEXOPCAPS_BLENDDIFFUSEALPHA          0x00000800L

+#define D3DTEXOPCAPS_BLENDTEXTUREALPHA          0x00001000L

+#define D3DTEXOPCAPS_BLENDFACTORALPHA           0x00002000L

+#define D3DTEXOPCAPS_BLENDTEXTUREALPHAPM        0x00004000L

+#define D3DTEXOPCAPS_BLENDCURRENTALPHA          0x00008000L

+#define D3DTEXOPCAPS_PREMODULATE                0x00010000L

+#define D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR     0x00020000L

+#define D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA     0x00040000L

+#define D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR  0x00080000L

+#define D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA  0x00100000L

+#define D3DTEXOPCAPS_BUMPENVMAP                 0x00200000L

+#define D3DTEXOPCAPS_BUMPENVMAPLUMINANCE        0x00400000L

+#define D3DTEXOPCAPS_DOTPRODUCT3                0x00800000L

+#define D3DTEXOPCAPS_MULTIPLYADD                0x01000000L

+#define D3DTEXOPCAPS_LERP                       0x02000000L

+

+//

+// FVFCaps

+//

+#define D3DFVFCAPS_TEXCOORDCOUNTMASK    0x0000ffffL /* mask for texture coordinate count field */

+#define D3DFVFCAPS_DONOTSTRIPELEMENTS   0x00080000L /* Device prefers that vertex elements not be stripped */

+#define D3DFVFCAPS_PSIZE                0x00100000L /* Device can receive point size */

+

+//

+// VertexProcessingCaps

+//

+#define D3DVTXPCAPS_TEXGEN              0x00000001L /* device can do texgen */

+#define D3DVTXPCAPS_MATERIALSOURCE7     0x00000002L /* device can do DX7-level colormaterialsource ops */

+#define D3DVTXPCAPS_DIRECTIONALLIGHTS   0x00000008L /* device can do directional lights */

+#define D3DVTXPCAPS_POSITIONALLIGHTS    0x00000010L /* device can do positional lights (includes point and spot) */

+#define D3DVTXPCAPS_LOCALVIEWER         0x00000020L /* device can do local viewer */

+#define D3DVTXPCAPS_TWEENING            0x00000040L /* device can do vertex tweening */

+#define D3DVTXPCAPS_NO_VSDT_UBYTE4      0x00000080L /* device does not support D3DVSDT_UBYTE4 */

+

+#pragma pack()

+

+#endif /* (DIRECT3D_VERSION >= 0x0800) */

+#endif /* _D3D8CAPS_H_ */

+

diff --git a/include/Direct3D/d3d8types.h b/include/Direct3D/d3d8types.h
new file mode 100644
index 0000000..0303762
--- /dev/null
+++ b/include/Direct3D/d3d8types.h
@@ -0,0 +1,1684 @@
+/*==========================================================================;

+ *

+ *  Copyright (C) Microsoft Corporation.  All Rights Reserved.

+ *

+ *  File:       d3d8types.h

+ *  Content:    Direct3D capabilities include file

+ *

+ ***************************************************************************/

+

+#ifndef _D3D8TYPES_H_

+#define _D3D8TYPES_H_

+

+#ifndef DIRECT3D_VERSION

+#define DIRECT3D_VERSION         0x0800

+#endif  //DIRECT3D_VERSION

+

+// include this file content only if compiling for DX8 interfaces

+#if(DIRECT3D_VERSION >= 0x0800)

+

+#include <float.h>

+

+#if _MSC_VER >= 1200

+#pragma warning(push)

+#endif

+#pragma warning(disable:4201) // anonymous unions warning

+#if defined(_X86_) || defined(_IA64_)

+#pragma pack(4)

+#endif

+

+// D3DCOLOR is equivalent to D3DFMT_A8R8G8B8

+#ifndef D3DCOLOR_DEFINED

+typedef DWORD D3DCOLOR;

+#define D3DCOLOR_DEFINED

+#endif

+

+// maps unsigned 8 bits/channel to D3DCOLOR

+#define D3DCOLOR_ARGB(a,r,g,b) \

+    ((D3DCOLOR)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff)))

+#define D3DCOLOR_RGBA(r,g,b,a) D3DCOLOR_ARGB(a,r,g,b)

+#define D3DCOLOR_XRGB(r,g,b)   D3DCOLOR_ARGB(0xff,r,g,b)

+

+// maps floating point channels (0.f to 1.f range) to D3DCOLOR

+#define D3DCOLOR_COLORVALUE(r,g,b,a) \

+    D3DCOLOR_RGBA((DWORD)((r)*255.f),(DWORD)((g)*255.f),(DWORD)((b)*255.f),(DWORD)((a)*255.f))

+

+

+#ifndef D3DVECTOR_DEFINED

+typedef struct _D3DVECTOR {

+    float x;

+    float y;

+    float z;

+} D3DVECTOR;

+#define D3DVECTOR_DEFINED

+#endif

+

+#ifndef D3DCOLORVALUE_DEFINED

+typedef struct _D3DCOLORVALUE {

+    float r;

+    float g;

+    float b;

+    float a;

+} D3DCOLORVALUE;

+#define D3DCOLORVALUE_DEFINED

+#endif

+

+#ifndef D3DRECT_DEFINED

+typedef struct _D3DRECT {

+    LONG x1;

+    LONG y1;

+    LONG x2;

+    LONG y2;

+} D3DRECT;

+#define D3DRECT_DEFINED

+#endif

+

+#ifndef D3DMATRIX_DEFINED

+typedef struct _D3DMATRIX {

+    union {

+        struct {

+            float        _11, _12, _13, _14;

+            float        _21, _22, _23, _24;

+            float        _31, _32, _33, _34;

+            float        _41, _42, _43, _44;

+

+        };

+        float m[4][4];

+    };

+} D3DMATRIX;

+#define D3DMATRIX_DEFINED

+#endif

+

+typedef struct _D3DVIEWPORT8 {

+    DWORD       X;

+    DWORD       Y;            /* Viewport Top left */

+    DWORD       Width;

+    DWORD       Height;       /* Viewport Dimensions */

+    float       MinZ;         /* Min/max of clip Volume */

+    float       MaxZ;

+} D3DVIEWPORT8;

+

+/*

+ * Values for clip fields.

+ */

+

+// Max number of user clipping planes, supported in D3D.

+#define D3DMAXUSERCLIPPLANES 32

+

+// These bits could be ORed together to use with D3DRS_CLIPPLANEENABLE

+//

+#define D3DCLIPPLANE0 (1 << 0)

+#define D3DCLIPPLANE1 (1 << 1)

+#define D3DCLIPPLANE2 (1 << 2)

+#define D3DCLIPPLANE3 (1 << 3)

+#define D3DCLIPPLANE4 (1 << 4)

+#define D3DCLIPPLANE5 (1 << 5)

+

+// The following bits are used in the ClipUnion and ClipIntersection

+// members of the D3DCLIPSTATUS8

+//

+

+#define D3DCS_LEFT        0x00000001L

+#define D3DCS_RIGHT       0x00000002L

+#define D3DCS_TOP         0x00000004L

+#define D3DCS_BOTTOM      0x00000008L

+#define D3DCS_FRONT       0x00000010L

+#define D3DCS_BACK        0x00000020L

+#define D3DCS_PLANE0      0x00000040L

+#define D3DCS_PLANE1      0x00000080L

+#define D3DCS_PLANE2      0x00000100L

+#define D3DCS_PLANE3      0x00000200L

+#define D3DCS_PLANE4      0x00000400L

+#define D3DCS_PLANE5      0x00000800L

+

+#define D3DCS_ALL (D3DCS_LEFT   | \

+                   D3DCS_RIGHT  | \

+                   D3DCS_TOP    | \

+                   D3DCS_BOTTOM | \

+                   D3DCS_FRONT  | \

+                   D3DCS_BACK   | \

+                   D3DCS_PLANE0 | \

+                   D3DCS_PLANE1 | \

+                   D3DCS_PLANE2 | \

+                   D3DCS_PLANE3 | \

+                   D3DCS_PLANE4 | \

+                   D3DCS_PLANE5)

+

+typedef struct _D3DCLIPSTATUS8 {

+    DWORD ClipUnion;

+    DWORD ClipIntersection;

+} D3DCLIPSTATUS8;

+

+typedef struct _D3DMATERIAL8 {

+    D3DCOLORVALUE   Diffuse;        /* Diffuse color RGBA */

+    D3DCOLORVALUE   Ambient;        /* Ambient color RGB */

+    D3DCOLORVALUE   Specular;       /* Specular 'shininess' */

+    D3DCOLORVALUE   Emissive;       /* Emissive color RGB */

+    float           Power;          /* Sharpness if specular highlight */

+} D3DMATERIAL8;

+

+typedef enum _D3DLIGHTTYPE {

+    D3DLIGHT_POINT          = 1,

+    D3DLIGHT_SPOT           = 2,

+    D3DLIGHT_DIRECTIONAL    = 3,

+    D3DLIGHT_FORCE_DWORD    = 0x7fffffff, /* force 32-bit size enum */

+} D3DLIGHTTYPE;

+

+typedef struct _D3DLIGHT8 {

+    D3DLIGHTTYPE    Type;            /* Type of light source */

+    D3DCOLORVALUE   Diffuse;         /* Diffuse color of light */

+    D3DCOLORVALUE   Specular;        /* Specular color of light */

+    D3DCOLORVALUE   Ambient;         /* Ambient color of light */

+    D3DVECTOR       Position;         /* Position in world space */

+    D3DVECTOR       Direction;        /* Direction in world space */

+    float           Range;            /* Cutoff range */

+    float           Falloff;          /* Falloff */

+    float           Attenuation0;     /* Constant attenuation */

+    float           Attenuation1;     /* Linear attenuation */

+    float           Attenuation2;     /* Quadratic attenuation */

+    float           Theta;            /* Inner angle of spotlight cone */

+    float           Phi;              /* Outer angle of spotlight cone */

+} D3DLIGHT8;

+

+/*

+ * Options for clearing

+ */

+#define D3DCLEAR_TARGET            0x00000001l  /* Clear target surface */

+#define D3DCLEAR_ZBUFFER           0x00000002l  /* Clear target z buffer */

+#define D3DCLEAR_STENCIL           0x00000004l  /* Clear stencil planes */

+

+/*

+ * The following defines the rendering states

+ */

+

+typedef enum _D3DSHADEMODE {

+    D3DSHADE_FLAT               = 1,

+    D3DSHADE_GOURAUD            = 2,

+    D3DSHADE_PHONG              = 3,

+    D3DSHADE_FORCE_DWORD        = 0x7fffffff, /* force 32-bit size enum */

+} D3DSHADEMODE;

+

+typedef enum _D3DFILLMODE {

+    D3DFILL_POINT               = 1,

+    D3DFILL_WIREFRAME           = 2,

+    D3DFILL_SOLID               = 3,

+    D3DFILL_FORCE_DWORD         = 0x7fffffff, /* force 32-bit size enum */

+} D3DFILLMODE;

+

+typedef struct _D3DLINEPATTERN {

+    WORD    wRepeatFactor;

+    WORD    wLinePattern;

+} D3DLINEPATTERN;

+

+typedef enum _D3DBLEND {

+    D3DBLEND_ZERO               = 1,

+    D3DBLEND_ONE                = 2,

+    D3DBLEND_SRCCOLOR           = 3,

+    D3DBLEND_INVSRCCOLOR        = 4,

+    D3DBLEND_SRCALPHA           = 5,

+    D3DBLEND_INVSRCALPHA        = 6,

+    D3DBLEND_DESTALPHA          = 7,

+    D3DBLEND_INVDESTALPHA       = 8,

+    D3DBLEND_DESTCOLOR          = 9,

+    D3DBLEND_INVDESTCOLOR       = 10,

+    D3DBLEND_SRCALPHASAT        = 11,

+    D3DBLEND_BOTHSRCALPHA       = 12,

+    D3DBLEND_BOTHINVSRCALPHA    = 13,

+    D3DBLEND_FORCE_DWORD        = 0x7fffffff, /* force 32-bit size enum */

+} D3DBLEND;

+

+typedef enum _D3DBLENDOP {

+    D3DBLENDOP_ADD              = 1,

+    D3DBLENDOP_SUBTRACT         = 2,

+    D3DBLENDOP_REVSUBTRACT      = 3,

+    D3DBLENDOP_MIN              = 4,

+    D3DBLENDOP_MAX              = 5,

+    D3DBLENDOP_FORCE_DWORD      = 0x7fffffff, /* force 32-bit size enum */

+} D3DBLENDOP;

+

+typedef enum _D3DTEXTUREADDRESS {

+    D3DTADDRESS_WRAP            = 1,

+    D3DTADDRESS_MIRROR          = 2,

+    D3DTADDRESS_CLAMP           = 3,

+    D3DTADDRESS_BORDER          = 4,

+    D3DTADDRESS_MIRRORONCE      = 5,

+    D3DTADDRESS_FORCE_DWORD     = 0x7fffffff, /* force 32-bit size enum */

+} D3DTEXTUREADDRESS;

+

+typedef enum _D3DCULL {

+    D3DCULL_NONE                = 1,

+    D3DCULL_CW                  = 2,

+    D3DCULL_CCW                 = 3,

+    D3DCULL_FORCE_DWORD         = 0x7fffffff, /* force 32-bit size enum */

+} D3DCULL;

+

+typedef enum _D3DCMPFUNC {

+    D3DCMP_NEVER                = 1,

+    D3DCMP_LESS                 = 2,

+    D3DCMP_EQUAL                = 3,

+    D3DCMP_LESSEQUAL            = 4,

+    D3DCMP_GREATER              = 5,

+    D3DCMP_NOTEQUAL             = 6,

+    D3DCMP_GREATEREQUAL         = 7,

+    D3DCMP_ALWAYS               = 8,

+    D3DCMP_FORCE_DWORD          = 0x7fffffff, /* force 32-bit size enum */

+} D3DCMPFUNC;

+

+typedef enum _D3DSTENCILOP {

+    D3DSTENCILOP_KEEP           = 1,

+    D3DSTENCILOP_ZERO           = 2,

+    D3DSTENCILOP_REPLACE        = 3,

+    D3DSTENCILOP_INCRSAT        = 4,

+    D3DSTENCILOP_DECRSAT        = 5,

+    D3DSTENCILOP_INVERT         = 6,

+    D3DSTENCILOP_INCR           = 7,

+    D3DSTENCILOP_DECR           = 8,

+    D3DSTENCILOP_FORCE_DWORD    = 0x7fffffff, /* force 32-bit size enum */

+} D3DSTENCILOP;

+

+typedef enum _D3DFOGMODE {

+    D3DFOG_NONE                 = 0,

+    D3DFOG_EXP                  = 1,

+    D3DFOG_EXP2                 = 2,

+    D3DFOG_LINEAR               = 3,

+    D3DFOG_FORCE_DWORD          = 0x7fffffff, /* force 32-bit size enum */

+} D3DFOGMODE;

+

+typedef enum _D3DZBUFFERTYPE {

+    D3DZB_FALSE                 = 0,

+    D3DZB_TRUE                  = 1, // Z buffering

+    D3DZB_USEW                  = 2, // W buffering

+    D3DZB_FORCE_DWORD           = 0x7fffffff, /* force 32-bit size enum */

+} D3DZBUFFERTYPE;

+

+// Primitives supported by draw-primitive API

+typedef enum _D3DPRIMITIVETYPE {

+    D3DPT_POINTLIST             = 1,

+    D3DPT_LINELIST              = 2,

+    D3DPT_LINESTRIP             = 3,

+    D3DPT_TRIANGLELIST          = 4,

+    D3DPT_TRIANGLESTRIP         = 5,

+    D3DPT_TRIANGLEFAN           = 6,

+    D3DPT_FORCE_DWORD           = 0x7fffffff, /* force 32-bit size enum */

+} D3DPRIMITIVETYPE;

+

+typedef enum _D3DTRANSFORMSTATETYPE {

+    D3DTS_VIEW          = 2,

+    D3DTS_PROJECTION    = 3,

+    D3DTS_TEXTURE0      = 16,

+    D3DTS_TEXTURE1      = 17,

+    D3DTS_TEXTURE2      = 18,

+    D3DTS_TEXTURE3      = 19,

+    D3DTS_TEXTURE4      = 20,

+    D3DTS_TEXTURE5      = 21,

+    D3DTS_TEXTURE6      = 22,

+    D3DTS_TEXTURE7      = 23,

+    D3DTS_FORCE_DWORD     = 0x7fffffff, /* force 32-bit size enum */

+} D3DTRANSFORMSTATETYPE;

+

+#define D3DTS_WORLDMATRIX(index) (D3DTRANSFORMSTATETYPE)(index + 256)

+#define D3DTS_WORLD  D3DTS_WORLDMATRIX(0)

+#define D3DTS_WORLD1 D3DTS_WORLDMATRIX(1)

+#define D3DTS_WORLD2 D3DTS_WORLDMATRIX(2)

+#define D3DTS_WORLD3 D3DTS_WORLDMATRIX(3)

+

+typedef enum _D3DRENDERSTATETYPE {

+    D3DRS_ZENABLE                   = 7,    /* D3DZBUFFERTYPE (or TRUE/FALSE for legacy) */

+    D3DRS_FILLMODE                  = 8,    /* D3DFILLMODE */

+    D3DRS_SHADEMODE                 = 9,    /* D3DSHADEMODE */

+    D3DRS_LINEPATTERN               = 10,   /* D3DLINEPATTERN */

+    D3DRS_ZWRITEENABLE              = 14,   /* TRUE to enable z writes */

+    D3DRS_ALPHATESTENABLE           = 15,   /* TRUE to enable alpha tests */

+    D3DRS_LASTPIXEL                 = 16,   /* TRUE for last-pixel on lines */

+    D3DRS_SRCBLEND                  = 19,   /* D3DBLEND */

+    D3DRS_DESTBLEND                 = 20,   /* D3DBLEND */

+    D3DRS_CULLMODE                  = 22,   /* D3DCULL */

+    D3DRS_ZFUNC                     = 23,   /* D3DCMPFUNC */

+    D3DRS_ALPHAREF                  = 24,   /* D3DFIXED */

+    D3DRS_ALPHAFUNC                 = 25,   /* D3DCMPFUNC */

+    D3DRS_DITHERENABLE              = 26,   /* TRUE to enable dithering */

+    D3DRS_ALPHABLENDENABLE          = 27,   /* TRUE to enable alpha blending */

+    D3DRS_FOGENABLE                 = 28,   /* TRUE to enable fog blending */

+    D3DRS_SPECULARENABLE            = 29,   /* TRUE to enable specular */

+    D3DRS_ZVISIBLE                  = 30,   /* TRUE to enable z checking */

+    D3DRS_FOGCOLOR                  = 34,   /* D3DCOLOR */

+    D3DRS_FOGTABLEMODE              = 35,   /* D3DFOGMODE */

+    D3DRS_FOGSTART                  = 36,   /* Fog start (for both vertex and pixel fog) */

+    D3DRS_FOGEND                    = 37,   /* Fog end      */

+    D3DRS_FOGDENSITY                = 38,   /* Fog density  */

+    D3DRS_EDGEANTIALIAS             = 40,   /* TRUE to enable edge antialiasing */

+    D3DRS_ZBIAS                     = 47,   /* LONG Z bias */

+    D3DRS_RANGEFOGENABLE            = 48,   /* Enables range-based fog */

+    D3DRS_STENCILENABLE             = 52,   /* BOOL enable/disable stenciling */

+    D3DRS_STENCILFAIL               = 53,   /* D3DSTENCILOP to do if stencil test fails */

+    D3DRS_STENCILZFAIL              = 54,   /* D3DSTENCILOP to do if stencil test passes and Z test fails */

+    D3DRS_STENCILPASS               = 55,   /* D3DSTENCILOP to do if both stencil and Z tests pass */

+    D3DRS_STENCILFUNC               = 56,   /* D3DCMPFUNC fn.  Stencil Test passes if ((ref & mask) stencilfn (stencil & mask)) is true */

+    D3DRS_STENCILREF                = 57,   /* Reference value used in stencil test */

+    D3DRS_STENCILMASK               = 58,   /* Mask value used in stencil test */

+    D3DRS_STENCILWRITEMASK          = 59,   /* Write mask applied to values written to stencil buffer */

+    D3DRS_TEXTUREFACTOR             = 60,   /* D3DCOLOR used for multi-texture blend */

+    D3DRS_WRAP0                     = 128,  /* wrap for 1st texture coord. set */

+    D3DRS_WRAP1                     = 129,  /* wrap for 2nd texture coord. set */

+    D3DRS_WRAP2                     = 130,  /* wrap for 3rd texture coord. set */

+    D3DRS_WRAP3                     = 131,  /* wrap for 4th texture coord. set */

+    D3DRS_WRAP4                     = 132,  /* wrap for 5th texture coord. set */

+    D3DRS_WRAP5                     = 133,  /* wrap for 6th texture coord. set */

+    D3DRS_WRAP6                     = 134,  /* wrap for 7th texture coord. set */

+    D3DRS_WRAP7                     = 135,  /* wrap for 8th texture coord. set */

+    D3DRS_CLIPPING                  = 136,

+    D3DRS_LIGHTING                  = 137,

+    D3DRS_AMBIENT                   = 139,

+    D3DRS_FOGVERTEXMODE             = 140,

+    D3DRS_COLORVERTEX               = 141,

+    D3DRS_LOCALVIEWER               = 142,

+    D3DRS_NORMALIZENORMALS          = 143,

+    D3DRS_DIFFUSEMATERIALSOURCE     = 145,

+    D3DRS_SPECULARMATERIALSOURCE    = 146,

+    D3DRS_AMBIENTMATERIALSOURCE     = 147,

+    D3DRS_EMISSIVEMATERIALSOURCE    = 148,

+    D3DRS_VERTEXBLEND               = 151,

+    D3DRS_CLIPPLANEENABLE           = 152,

+    D3DRS_SOFTWAREVERTEXPROCESSING  = 153,

+    D3DRS_POINTSIZE                 = 154,   /* float point size */

+    D3DRS_POINTSIZE_MIN             = 155,   /* float point size min threshold */

+    D3DRS_POINTSPRITEENABLE         = 156,   /* BOOL point texture coord control */

+    D3DRS_POINTSCALEENABLE          = 157,   /* BOOL point size scale enable */

+    D3DRS_POINTSCALE_A              = 158,   /* float point attenuation A value */

+    D3DRS_POINTSCALE_B              = 159,   /* float point attenuation B value */

+    D3DRS_POINTSCALE_C              = 160,   /* float point attenuation C value */

+    D3DRS_MULTISAMPLEANTIALIAS      = 161,  // BOOL - set to do FSAA with multisample buffer

+    D3DRS_MULTISAMPLEMASK           = 162,  // DWORD - per-sample enable/disable

+    D3DRS_PATCHEDGESTYLE            = 163,  // Sets whether patch edges will use float style tessellation

+    D3DRS_PATCHSEGMENTS             = 164,  // Number of segments per edge when drawing patches

+    D3DRS_DEBUGMONITORTOKEN         = 165,  // DEBUG ONLY - token to debug monitor

+    D3DRS_POINTSIZE_MAX             = 166,   /* float point size max threshold */

+    D3DRS_INDEXEDVERTEXBLENDENABLE  = 167,

+    D3DRS_COLORWRITEENABLE          = 168,  // per-channel write enable

+    D3DRS_TWEENFACTOR               = 170,   // float tween factor

+    D3DRS_BLENDOP                   = 171,   // D3DBLENDOP setting

+    D3DRS_POSITIONORDER             = 172,   // NPatch position interpolation order. D3DORDER_LINEAR or D3DORDER_CUBIC (default)

+    D3DRS_NORMALORDER               = 173,   // NPatch normal interpolation order. D3DORDER_LINEAR (default) or D3DORDER_QUADRATIC

+

+    D3DRS_FORCE_DWORD               = 0x7fffffff, /* force 32-bit size enum */

+} D3DRENDERSTATETYPE;

+

+// Values for material source

+typedef enum _D3DMATERIALCOLORSOURCE

+{

+    D3DMCS_MATERIAL         = 0,            // Color from material is used

+    D3DMCS_COLOR1           = 1,            // Diffuse vertex color is used

+    D3DMCS_COLOR2           = 2,            // Specular vertex color is used

+    D3DMCS_FORCE_DWORD      = 0x7fffffff,   // force 32-bit size enum

+} D3DMATERIALCOLORSOURCE;

+

+// Bias to apply to the texture coordinate set to apply a wrap to.

+#define D3DRENDERSTATE_WRAPBIAS                 128UL

+

+/* Flags to construct the WRAP render states */

+#define D3DWRAP_U   0x00000001L

+#define D3DWRAP_V   0x00000002L

+#define D3DWRAP_W   0x00000004L

+

+/* Flags to construct the WRAP render states for 1D thru 4D texture coordinates */

+#define D3DWRAPCOORD_0   0x00000001L    // same as D3DWRAP_U

+#define D3DWRAPCOORD_1   0x00000002L    // same as D3DWRAP_V

+#define D3DWRAPCOORD_2   0x00000004L    // same as D3DWRAP_W

+#define D3DWRAPCOORD_3   0x00000008L

+

+/* Flags to construct D3DRS_COLORWRITEENABLE */

+#define D3DCOLORWRITEENABLE_RED     (1L<<0)

+#define D3DCOLORWRITEENABLE_GREEN   (1L<<1)

+#define D3DCOLORWRITEENABLE_BLUE    (1L<<2)

+#define D3DCOLORWRITEENABLE_ALPHA   (1L<<3)

+

+/*

+ * State enumerants for per-stage texture processing.

+ */

+typedef enum _D3DTEXTURESTAGESTATETYPE

+{

+    D3DTSS_COLOROP        =  1, /* D3DTEXTUREOP - per-stage blending controls for color channels */

+    D3DTSS_COLORARG1      =  2, /* D3DTA_* (texture arg) */

+    D3DTSS_COLORARG2      =  3, /* D3DTA_* (texture arg) */

+    D3DTSS_ALPHAOP        =  4, /* D3DTEXTUREOP - per-stage blending controls for alpha channel */

+    D3DTSS_ALPHAARG1      =  5, /* D3DTA_* (texture arg) */

+    D3DTSS_ALPHAARG2      =  6, /* D3DTA_* (texture arg) */

+    D3DTSS_BUMPENVMAT00   =  7, /* float (bump mapping matrix) */

+    D3DTSS_BUMPENVMAT01   =  8, /* float (bump mapping matrix) */

+    D3DTSS_BUMPENVMAT10   =  9, /* float (bump mapping matrix) */

+    D3DTSS_BUMPENVMAT11   = 10, /* float (bump mapping matrix) */

+    D3DTSS_TEXCOORDINDEX  = 11, /* identifies which set of texture coordinates index this texture */

+    D3DTSS_ADDRESSU       = 13, /* D3DTEXTUREADDRESS for U coordinate */

+    D3DTSS_ADDRESSV       = 14, /* D3DTEXTUREADDRESS for V coordinate */

+    D3DTSS_BORDERCOLOR    = 15, /* D3DCOLOR */

+    D3DTSS_MAGFILTER      = 16, /* D3DTEXTUREFILTER filter to use for magnification */

+    D3DTSS_MINFILTER      = 17, /* D3DTEXTUREFILTER filter to use for minification */

+    D3DTSS_MIPFILTER      = 18, /* D3DTEXTUREFILTER filter to use between mipmaps during minification */

+    D3DTSS_MIPMAPLODBIAS  = 19, /* float Mipmap LOD bias */

+    D3DTSS_MAXMIPLEVEL    = 20, /* DWORD 0..(n-1) LOD index of largest map to use (0 == largest) */

+    D3DTSS_MAXANISOTROPY  = 21, /* DWORD maximum anisotropy */

+    D3DTSS_BUMPENVLSCALE  = 22, /* float scale for bump map luminance */

+    D3DTSS_BUMPENVLOFFSET = 23, /* float offset for bump map luminance */

+    D3DTSS_TEXTURETRANSFORMFLAGS = 24, /* D3DTEXTURETRANSFORMFLAGS controls texture transform */

+    D3DTSS_ADDRESSW       = 25, /* D3DTEXTUREADDRESS for W coordinate */

+    D3DTSS_COLORARG0      = 26, /* D3DTA_* third arg for triadic ops */

+    D3DTSS_ALPHAARG0      = 27, /* D3DTA_* third arg for triadic ops */

+    D3DTSS_RESULTARG      = 28, /* D3DTA_* arg for result (CURRENT or TEMP) */

+    D3DTSS_FORCE_DWORD   = 0x7fffffff, /* force 32-bit size enum */

+} D3DTEXTURESTAGESTATETYPE;

+

+// Values, used with D3DTSS_TEXCOORDINDEX, to specify that the vertex data(position

+// and normal in the camera space) should be taken as texture coordinates

+// Low 16 bits are used to specify texture coordinate index, to take the WRAP mode from

+//

+#define D3DTSS_TCI_PASSTHRU                             0x00000000

+#define D3DTSS_TCI_CAMERASPACENORMAL                    0x00010000

+#define D3DTSS_TCI_CAMERASPACEPOSITION                  0x00020000

+#define D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR          0x00030000

+

+/*

+ * Enumerations for COLOROP and ALPHAOP texture blending operations set in

+ * texture processing stage controls in D3DTSS.

+ */

+typedef enum _D3DTEXTUREOP

+{

+    // Control

+    D3DTOP_DISABLE              = 1,      // disables stage

+    D3DTOP_SELECTARG1           = 2,      // the default

+    D3DTOP_SELECTARG2           = 3,

+

+    // Modulate

+    D3DTOP_MODULATE             = 4,      // multiply args together

+    D3DTOP_MODULATE2X           = 5,      // multiply and  1 bit

+    D3DTOP_MODULATE4X           = 6,      // multiply and  2 bits

+

+    // Add

+    D3DTOP_ADD                  =  7,   // add arguments together

+    D3DTOP_ADDSIGNED            =  8,   // add with -0.5 bias

+    D3DTOP_ADDSIGNED2X          =  9,   // as above but left  1 bit

+    D3DTOP_SUBTRACT             = 10,   // Arg1 - Arg2, with no saturation

+    D3DTOP_ADDSMOOTH            = 11,   // add 2 args, subtract product

+                                        // Arg1 + Arg2 - Arg1*Arg2

+                                        // = Arg1 + (1-Arg1)*Arg2

+

+    // Linear alpha blend: Arg1*(Alpha) + Arg2*(1-Alpha)

+    D3DTOP_BLENDDIFFUSEALPHA    = 12, // iterated alpha

+    D3DTOP_BLENDTEXTUREALPHA    = 13, // texture alpha

+    D3DTOP_BLENDFACTORALPHA     = 14, // alpha from D3DRS_TEXTUREFACTOR

+

+    // Linear alpha blend with pre-multiplied arg1 input: Arg1 + Arg2*(1-Alpha)

+    D3DTOP_BLENDTEXTUREALPHAPM  = 15, // texture alpha

+    D3DTOP_BLENDCURRENTALPHA    = 16, // by alpha of current color

+

+    // Specular mapping

+    D3DTOP_PREMODULATE            = 17,     // modulate with next texture before use

+    D3DTOP_MODULATEALPHA_ADDCOLOR = 18,     // Arg1.RGB + Arg1.A*Arg2.RGB

+                                            // COLOROP only

+    D3DTOP_MODULATECOLOR_ADDALPHA = 19,     // Arg1.RGB*Arg2.RGB + Arg1.A

+                                            // COLOROP only

+    D3DTOP_MODULATEINVALPHA_ADDCOLOR = 20,  // (1-Arg1.A)*Arg2.RGB + Arg1.RGB

+                                            // COLOROP only

+    D3DTOP_MODULATEINVCOLOR_ADDALPHA = 21,  // (1-Arg1.RGB)*Arg2.RGB + Arg1.A

+                                            // COLOROP only

+

+    // Bump mapping

+    D3DTOP_BUMPENVMAP           = 22, // per pixel env map perturbation

+    D3DTOP_BUMPENVMAPLUMINANCE  = 23, // with luminance channel

+

+    // This can do either diffuse or specular bump mapping with correct input.

+    // Performs the function (Arg1.R*Arg2.R + Arg1.G*Arg2.G + Arg1.B*Arg2.B)

+    // where each component has been scaled and offset to make it signed.

+    // The result is replicated into all four (including alpha) channels.

+    // This is a valid COLOROP only.

+    D3DTOP_DOTPRODUCT3          = 24,

+

+    // Triadic ops

+    D3DTOP_MULTIPLYADD          = 25, // Arg0 + Arg1*Arg2

+    D3DTOP_LERP                 = 26, // (Arg0)*Arg1 + (1-Arg0)*Arg2

+

+    D3DTOP_FORCE_DWORD = 0x7fffffff,

+} D3DTEXTUREOP;

+

+/*

+ * Values for COLORARG0,1,2, ALPHAARG0,1,2, and RESULTARG texture blending

+ * operations set in texture processing stage controls in D3DRENDERSTATE.

+ */

+#define D3DTA_SELECTMASK        0x0000000f  // mask for arg selector

+#define D3DTA_DIFFUSE           0x00000000  // select diffuse color (read only)

+#define D3DTA_CURRENT           0x00000001  // select stage destination register (read/write)

+#define D3DTA_TEXTURE           0x00000002  // select texture color (read only)

+#define D3DTA_TFACTOR           0x00000003  // select D3DRS_TEXTUREFACTOR (read only)

+#define D3DTA_SPECULAR          0x00000004  // select specular color (read only)

+#define D3DTA_TEMP              0x00000005  // select temporary register color (read/write)

+#define D3DTA_COMPLEMENT        0x00000010  // take 1.0 - x (read modifier)

+#define D3DTA_ALPHAREPLICATE    0x00000020  // replicate alpha to color components (read modifier)

+

+//

+// Values for D3DTSS_***FILTER texture stage states

+//

+typedef enum _D3DTEXTUREFILTERTYPE

+{

+    D3DTEXF_NONE            = 0,    // filtering disabled (valid for mip filter only)

+    D3DTEXF_POINT           = 1,    // nearest

+    D3DTEXF_LINEAR          = 2,    // linear interpolation

+    D3DTEXF_ANISOTROPIC     = 3,    // anisotropic

+    D3DTEXF_FLATCUBIC       = 4,    // cubic

+    D3DTEXF_GAUSSIANCUBIC   = 5,    // different cubic kernel

+    D3DTEXF_FORCE_DWORD     = 0x7fffffff,   // force 32-bit size enum

+} D3DTEXTUREFILTERTYPE;

+

+/* Bits for Flags in ProcessVertices call */

+

+#define D3DPV_DONOTCOPYDATA     (1 << 0)

+

+//-------------------------------------------------------------------

+

+// Flexible vertex format bits

+//

+#define D3DFVF_RESERVED0        0x001

+#define D3DFVF_POSITION_MASK    0x00E

+#define D3DFVF_XYZ              0x002

+#define D3DFVF_XYZRHW           0x004

+#define D3DFVF_XYZB1            0x006

+#define D3DFVF_XYZB2            0x008

+#define D3DFVF_XYZB3            0x00a

+#define D3DFVF_XYZB4            0x00c

+#define D3DFVF_XYZB5            0x00e

+

+#define D3DFVF_NORMAL           0x010

+#define D3DFVF_PSIZE            0x020

+#define D3DFVF_DIFFUSE          0x040

+#define D3DFVF_SPECULAR         0x080

+

+#define D3DFVF_TEXCOUNT_MASK    0xf00

+#define D3DFVF_TEXCOUNT_SHIFT   8

+#define D3DFVF_TEX0             0x000

+#define D3DFVF_TEX1             0x100

+#define D3DFVF_TEX2             0x200

+#define D3DFVF_TEX3             0x300

+#define D3DFVF_TEX4             0x400

+#define D3DFVF_TEX5             0x500

+#define D3DFVF_TEX6             0x600

+#define D3DFVF_TEX7             0x700

+#define D3DFVF_TEX8             0x800

+

+#define D3DFVF_LASTBETA_UBYTE4  0x1000

+

+#define D3DFVF_RESERVED2        0xE000  // 4 reserved bits

+

+//---------------------------------------------------------------------

+// Vertex Shaders

+//

+

+/*

+

+Vertex Shader Declaration

+

+The declaration portion of a vertex shader defines the static external

+interface of the shader.  The information in the declaration includes:

+

+- Assignments of vertex shader input registers to data streams.  These

+assignments bind a specific vertex register to a single component within a

+vertex stream.  A vertex stream element is identified by a byte offset

+within the stream and a type.  The type specifies the arithmetic data type

+plus the dimensionality (1, 2, 3, or 4 values).  Stream data which is

+less than 4 values are always expanded out to 4 values with zero or more

+0.F values and one 1.F value.

+

+- Assignment of vertex shader input registers to implicit data from the

+primitive tessellator.  This controls the loading of vertex data which is

+not loaded from a stream, but rather is generated during primitive

+tessellation prior to the vertex shader.

+

+- Loading data into the constant memory at the time a shader is set as the

+current shader.  Each token specifies values for one or more contiguous 4

+DWORD constant registers.  This allows the shader to update an arbitrary

+subset of the constant memory, overwriting the device state (which

+contains the current values of the constant memory).  Note that these

+values can be subsequently overwritten (between DrawPrimitive calls)

+during the time a shader is bound to a device via the

+SetVertexShaderConstant method.

+

+

+Declaration arrays are single-dimensional arrays of DWORDs composed of

+multiple tokens each of which is one or more DWORDs.  The single-DWORD

+token value 0xFFFFFFFF is a special token used to indicate the end of the

+declaration array.  The single DWORD token value 0x00000000 is a NOP token

+with is ignored during the declaration parsing.  Note that 0x00000000 is a

+valid value for DWORDs following the first DWORD for multiple word tokens.

+

+[31:29] TokenType

+    0x0 - NOP (requires all DWORD bits to be zero)

+    0x1 - stream selector

+    0x2 - stream data definition (map to vertex input memory)

+    0x3 - vertex input memory from tessellator

+    0x4 - constant memory from shader

+    0x5 - extension

+    0x6 - reserved

+    0x7 - end-of-array (requires all DWORD bits to be 1)

+

+NOP Token (single DWORD token)

+    [31:29] 0x0

+    [28:00] 0x0

+

+Stream Selector (single DWORD token)

+    [31:29] 0x1

+    [28]    indicates whether this is a tessellator stream

+    [27:04] 0x0

+    [03:00] stream selector (0..15)

+

+Stream Data Definition (single DWORD token)

+    Vertex Input Register Load

+      [31:29] 0x2

+      [28]    0x0

+      [27:20] 0x0

+      [19:16] type (dimensionality and data type)

+      [15:04] 0x0

+      [03:00] vertex register address (0..15)

+    Data Skip (no register load)

+      [31:29] 0x2

+      [28]    0x1

+      [27:20] 0x0

+      [19:16] count of DWORDS to skip over (0..15)

+      [15:00] 0x0

+    Vertex Input Memory from Tessellator Data (single DWORD token)

+      [31:29] 0x3

+      [28]    indicates whether data is normals or u/v

+      [27:24] 0x0

+      [23:20] vertex register address (0..15)

+      [19:16] type (dimensionality)

+      [15:04] 0x0

+      [03:00] vertex register address (0..15)

+

+Constant Memory from Shader (multiple DWORD token)

+    [31:29] 0x4

+    [28:25] count of 4*DWORD constants to load (0..15)

+    [24:07] 0x0

+    [06:00] constant memory address (0..95)

+

+Extension Token (single or multiple DWORD token)

+    [31:29] 0x5

+    [28:24] count of additional DWORDs in token (0..31)

+    [23:00] extension-specific information

+

+End-of-array token (single DWORD token)

+    [31:29] 0x7

+    [28:00] 0x1fffffff

+

+The stream selector token must be immediately followed by a contiguous set of stream data definition tokens.  This token sequence fully defines that stream, including the set of elements within the stream, the order in which the elements appear, the type of each element, and the vertex register into which to load an element.

+Streams are allowed to include data which is not loaded into a vertex register, thus allowing data which is not used for this shader to exist in the vertex stream.  This skipped data is defined only by a count of DWORDs to skip over, since the type information is irrelevant.

+The token sequence:

+Stream Select: stream=0

+Stream Data Definition (Load): type=FLOAT3; register=3

+Stream Data Definition (Load): type=FLOAT3; register=4

+Stream Data Definition (Skip): count=2

+Stream Data Definition (Load): type=FLOAT2; register=7

+

+defines stream zero to consist of 4 elements, 3 of which are loaded into registers and the fourth skipped over.  Register 3 is loaded with the first three DWORDs in each vertex interpreted as FLOAT data.  Register 4 is loaded with the 4th, 5th, and 6th DWORDs interpreted as FLOAT data.  The next two DWORDs (7th and 8th) are skipped over and not loaded into any vertex input register.   Register 7 is loaded with the 9th and 10th DWORDS interpreted as FLOAT data.

+Placing of tokens other than NOPs between the Stream Selector and Stream Data Definition tokens is disallowed.

+

+*/

+

+typedef enum _D3DVSD_TOKENTYPE

+{

+    D3DVSD_TOKEN_NOP        = 0,    // NOP or extension

+    D3DVSD_TOKEN_STREAM,            // stream selector

+    D3DVSD_TOKEN_STREAMDATA,        // stream data definition (map to vertex input memory)

+    D3DVSD_TOKEN_TESSELLATOR,       // vertex input memory from tessellator

+    D3DVSD_TOKEN_CONSTMEM,          // constant memory from shader

+    D3DVSD_TOKEN_EXT,               // extension

+    D3DVSD_TOKEN_END = 7,           // end-of-array (requires all DWORD bits to be 1)

+    D3DVSD_FORCE_DWORD = 0x7fffffff,// force 32-bit size enum

+} D3DVSD_TOKENTYPE;

+

+#define D3DVSD_TOKENTYPESHIFT   29

+#define D3DVSD_TOKENTYPEMASK    (7 << D3DVSD_TOKENTYPESHIFT)

+

+#define D3DVSD_STREAMNUMBERSHIFT 0

+#define D3DVSD_STREAMNUMBERMASK (0xF << D3DVSD_STREAMNUMBERSHIFT)

+

+#define D3DVSD_DATALOADTYPESHIFT 28

+#define D3DVSD_DATALOADTYPEMASK (0x1 << D3DVSD_DATALOADTYPESHIFT)

+

+#define D3DVSD_DATATYPESHIFT 16

+#define D3DVSD_DATATYPEMASK (0xF << D3DVSD_DATATYPESHIFT)

+

+#define D3DVSD_SKIPCOUNTSHIFT 16

+#define D3DVSD_SKIPCOUNTMASK (0xF << D3DVSD_SKIPCOUNTSHIFT)

+

+#define D3DVSD_VERTEXREGSHIFT 0

+#define D3DVSD_VERTEXREGMASK (0x1F << D3DVSD_VERTEXREGSHIFT)

+

+#define D3DVSD_VERTEXREGINSHIFT 20

+#define D3DVSD_VERTEXREGINMASK (0xF << D3DVSD_VERTEXREGINSHIFT)

+

+#define D3DVSD_CONSTCOUNTSHIFT 25

+#define D3DVSD_CONSTCOUNTMASK (0xF << D3DVSD_CONSTCOUNTSHIFT)

+

+#define D3DVSD_CONSTADDRESSSHIFT 0

+#define D3DVSD_CONSTADDRESSMASK (0x7F << D3DVSD_CONSTADDRESSSHIFT)

+

+#define D3DVSD_CONSTRSSHIFT 16

+#define D3DVSD_CONSTRSMASK (0x1FFF << D3DVSD_CONSTRSSHIFT)

+

+#define D3DVSD_EXTCOUNTSHIFT 24

+#define D3DVSD_EXTCOUNTMASK (0x1F << D3DVSD_EXTCOUNTSHIFT)

+

+#define D3DVSD_EXTINFOSHIFT 0

+#define D3DVSD_EXTINFOMASK (0xFFFFFF << D3DVSD_EXTINFOSHIFT)

+

+#define D3DVSD_MAKETOKENTYPE(tokenType) ((tokenType << D3DVSD_TOKENTYPESHIFT) & D3DVSD_TOKENTYPEMASK)

+

+// macros for generation of CreateVertexShader Declaration token array

+

+// Set current stream

+// _StreamNumber [0..(MaxStreams-1)] stream to get data from

+//

+#define D3DVSD_STREAM( _StreamNumber ) \

+    (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKEN_STREAM) | (_StreamNumber))

+

+// Set tessellator stream

+//

+#define D3DVSD_STREAMTESSSHIFT 28

+#define D3DVSD_STREAMTESSMASK (1 << D3DVSD_STREAMTESSSHIFT)

+#define D3DVSD_STREAM_TESS( ) \

+    (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKEN_STREAM) | (D3DVSD_STREAMTESSMASK))

+

+// bind single vertex register to vertex element from vertex stream

+//

+// _VertexRegister [0..15] address of the vertex register

+// _Type [D3DVSDT_*] dimensionality and arithmetic data type

+

+#define D3DVSD_REG( _VertexRegister, _Type ) \

+    (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKEN_STREAMDATA) |            \

+     ((_Type) << D3DVSD_DATATYPESHIFT) | (_VertexRegister))

+

+// Skip _DWORDCount DWORDs in vertex

+//

+#define D3DVSD_SKIP( _DWORDCount ) \

+    (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKEN_STREAMDATA) | 0x10000000 | \

+     ((_DWORDCount) << D3DVSD_SKIPCOUNTSHIFT))

+

+// load data into vertex shader constant memory

+//

+// _ConstantAddress [0..95] - address of constant array to begin filling data

+// _Count [0..15] - number of constant vectors to load (4 DWORDs each)

+// followed by 4*_Count DWORDS of data

+//

+#define D3DVSD_CONST( _ConstantAddress, _Count ) \

+    (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKEN_CONSTMEM) | \

+     ((_Count) << D3DVSD_CONSTCOUNTSHIFT) | (_ConstantAddress))

+

+// enable tessellator generated normals

+//

+// _VertexRegisterIn  [0..15] address of vertex register whose input stream

+//                            will be used in normal computation

+// _VertexRegisterOut [0..15] address of vertex register to output the normal to

+//

+#define D3DVSD_TESSNORMAL( _VertexRegisterIn, _VertexRegisterOut ) \

+    (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKEN_TESSELLATOR) | \

+     ((_VertexRegisterIn) << D3DVSD_VERTEXREGINSHIFT) | \

+     ((0x02) << D3DVSD_DATATYPESHIFT) | (_VertexRegisterOut))

+

+// enable tessellator generated surface parameters

+//

+// _VertexRegister [0..15] address of vertex register to output parameters

+//

+#define D3DVSD_TESSUV( _VertexRegister ) \

+    (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKEN_TESSELLATOR) | 0x10000000 | \

+     ((0x01) << D3DVSD_DATATYPESHIFT) | (_VertexRegister))

+

+// Generates END token

+//

+#define D3DVSD_END() 0xFFFFFFFF

+

+// Generates NOP token

+#define D3DVSD_NOP() 0x00000000

+

+// bit declarations for _Type fields

+#define D3DVSDT_FLOAT1      0x00    // 1D float expanded to (value, 0., 0., 1.)

+#define D3DVSDT_FLOAT2      0x01    // 2D float expanded to (value, value, 0., 1.)

+#define D3DVSDT_FLOAT3      0x02    // 3D float expanded to (value, value, value, 1.)

+#define D3DVSDT_FLOAT4      0x03    // 4D float

+#define D3DVSDT_D3DCOLOR    0x04    // 4D packed unsigned bytes mapped to 0. to 1. range

+                                    // Input is in D3DCOLOR format (ARGB) expanded to (R, G, B, A)

+#define D3DVSDT_UBYTE4      0x05    // 4D unsigned byte

+#define D3DVSDT_SHORT2      0x06    // 2D signed short expanded to (value, value, 0., 1.)

+#define D3DVSDT_SHORT4      0x07    // 4D signed short

+

+// assignments of vertex input registers for fixed function vertex shader

+//

+#define D3DVSDE_POSITION        0

+#define D3DVSDE_BLENDWEIGHT     1

+#define D3DVSDE_BLENDINDICES    2

+#define D3DVSDE_NORMAL          3

+#define D3DVSDE_PSIZE           4

+#define D3DVSDE_DIFFUSE         5

+#define D3DVSDE_SPECULAR        6

+#define D3DVSDE_TEXCOORD0       7

+#define D3DVSDE_TEXCOORD1       8

+#define D3DVSDE_TEXCOORD2       9

+#define D3DVSDE_TEXCOORD3       10

+#define D3DVSDE_TEXCOORD4       11

+#define D3DVSDE_TEXCOORD5       12

+#define D3DVSDE_TEXCOORD6       13

+#define D3DVSDE_TEXCOORD7       14

+#define D3DVSDE_POSITION2       15

+#define D3DVSDE_NORMAL2         16

+

+// Maximum supported number of texture coordinate sets

+#define D3DDP_MAXTEXCOORD   8

+

+

+//

+// Instruction Token Bit Definitions

+//

+#define D3DSI_OPCODE_MASK       0x0000FFFF

+

+typedef enum _D3DSHADER_INSTRUCTION_OPCODE_TYPE

+{

+    D3DSIO_NOP          = 0,    // PS/VS

+    D3DSIO_MOV          ,       // PS/VS

+    D3DSIO_ADD          ,       // PS/VS

+    D3DSIO_SUB          ,       // PS

+    D3DSIO_MAD          ,       // PS/VS

+    D3DSIO_MUL          ,       // PS/VS

+    D3DSIO_RCP          ,       // VS

+    D3DSIO_RSQ          ,       // VS

+    D3DSIO_DP3          ,       // PS/VS

+    D3DSIO_DP4          ,       // PS/VS

+    D3DSIO_MIN          ,       // VS

+    D3DSIO_MAX          ,       // VS

+    D3DSIO_SLT          ,       // VS

+    D3DSIO_SGE          ,       // VS

+    D3DSIO_EXP          ,       // VS

+    D3DSIO_LOG          ,       // VS

+    D3DSIO_LIT          ,       // VS

+    D3DSIO_DST          ,       // VS

+    D3DSIO_LRP          ,       // PS

+    D3DSIO_FRC          ,       // VS

+    D3DSIO_M4x4         ,       // VS

+    D3DSIO_M4x3         ,       // VS

+    D3DSIO_M3x4         ,       // VS

+    D3DSIO_M3x3         ,       // VS

+    D3DSIO_M3x2         ,       // VS

+

+    D3DSIO_TEXCOORD     = 64,   // PS

+    D3DSIO_TEXKILL      ,       // PS

+    D3DSIO_TEX          ,       // PS

+    D3DSIO_TEXBEM       ,       // PS

+    D3DSIO_TEXBEML      ,       // PS

+    D3DSIO_TEXREG2AR    ,       // PS

+    D3DSIO_TEXREG2GB    ,       // PS

+    D3DSIO_TEXM3x2PAD   ,       // PS

+    D3DSIO_TEXM3x2TEX   ,       // PS

+    D3DSIO_TEXM3x3PAD   ,       // PS

+    D3DSIO_TEXM3x3TEX   ,       // PS

+    D3DSIO_TEXM3x3DIFF  ,       // PS

+    D3DSIO_TEXM3x3SPEC  ,       // PS

+    D3DSIO_TEXM3x3VSPEC ,       // PS

+    D3DSIO_EXPP         ,       // VS

+    D3DSIO_LOGP         ,       // VS

+    D3DSIO_CND          ,       // PS

+    D3DSIO_DEF          ,       // PS

+    D3DSIO_TEXREG2RGB   ,       // PS

+    D3DSIO_TEXDP3TEX    ,       // PS

+    D3DSIO_TEXM3x2DEPTH ,       // PS

+    D3DSIO_TEXDP3       ,       // PS

+    D3DSIO_TEXM3x3      ,       // PS

+    D3DSIO_TEXDEPTH     ,       // PS

+    D3DSIO_CMP          ,       // PS

+    D3DSIO_BEM          ,       // PS

+

+    D3DSIO_PHASE        = 0xFFFD,

+    D3DSIO_COMMENT      = 0xFFFE,

+    D3DSIO_END          = 0xFFFF,

+

+    D3DSIO_FORCE_DWORD  = 0x7fffffff,   // force 32-bit size enum

+} D3DSHADER_INSTRUCTION_OPCODE_TYPE;

+

+//

+// Co-Issue Instruction Modifier - if set then this instruction is to be

+// issued in parallel with the previous instruction(s) for which this bit

+// is not set.

+//

+#define D3DSI_COISSUE           0x40000000

+

+//

+// Parameter Token Bit Definitions

+//

+#define D3DSP_REGNUM_MASK       0x00001FFF

+

+// destination parameter write mask

+#define D3DSP_WRITEMASK_0       0x00010000  // Component 0 (X;Red)

+#define D3DSP_WRITEMASK_1       0x00020000  // Component 1 (Y;Green)

+#define D3DSP_WRITEMASK_2       0x00040000  // Component 2 (Z;Blue)

+#define D3DSP_WRITEMASK_3       0x00080000  // Component 3 (W;Alpha)

+#define D3DSP_WRITEMASK_ALL     0x000F0000  // All Components

+

+// destination parameter modifiers

+#define D3DSP_DSTMOD_SHIFT      20

+#define D3DSP_DSTMOD_MASK       0x00F00000

+

+typedef enum _D3DSHADER_PARAM_DSTMOD_TYPE

+{

+    D3DSPDM_NONE    = 0<<D3DSP_DSTMOD_SHIFT, // nop

+    D3DSPDM_SATURATE= 1<<D3DSP_DSTMOD_SHIFT, // clamp to 0. to 1. range

+    D3DSPDM_FORCE_DWORD  = 0x7fffffff,      // force 32-bit size enum

+} D3DSHADER_PARAM_DSTMOD_TYPE;

+

+// destination parameter 

+#define D3DSP_DSTSHIFT_SHIFT    24

+#define D3DSP_DSTSHIFT_MASK     0x0F000000

+

+// destination/source parameter register type

+#define D3DSP_REGTYPE_SHIFT     28

+#define D3DSP_REGTYPE_MASK      0x70000000

+

+typedef enum _D3DSHADER_PARAM_REGISTER_TYPE

+{

+    D3DSPR_TEMP     = 0<<D3DSP_REGTYPE_SHIFT, // Temporary Register File

+    D3DSPR_INPUT    = 1<<D3DSP_REGTYPE_SHIFT, // Input Register File

+    D3DSPR_CONST    = 2<<D3DSP_REGTYPE_SHIFT, // Constant Register File

+    D3DSPR_ADDR     = 3<<D3DSP_REGTYPE_SHIFT, // Address Register (VS)

+    D3DSPR_TEXTURE  = 3<<D3DSP_REGTYPE_SHIFT, // Texture Register File (PS)

+    D3DSPR_RASTOUT  = 4<<D3DSP_REGTYPE_SHIFT, // Rasterizer Register File

+    D3DSPR_ATTROUT  = 5<<D3DSP_REGTYPE_SHIFT, // Attribute Output Register File

+    D3DSPR_TEXCRDOUT= 6<<D3DSP_REGTYPE_SHIFT, // Texture Coordinate Output Register File

+    D3DSPR_FORCE_DWORD  = 0x7fffffff,         // force 32-bit size enum

+} D3DSHADER_PARAM_REGISTER_TYPE;

+

+// Register offsets in the Rasterizer Register File

+//

+typedef enum _D3DVS_RASTOUT_OFFSETS

+{

+    D3DSRO_POSITION = 0,

+    D3DSRO_FOG,

+    D3DSRO_POINT_SIZE,

+    D3DSRO_FORCE_DWORD  = 0x7fffffff,         // force 32-bit size enum

+} D3DVS_RASTOUT_OFFSETS;

+

+// Source operand addressing modes

+

+#define D3DVS_ADDRESSMODE_SHIFT 13

+#define D3DVS_ADDRESSMODE_MASK  (1 << D3DVS_ADDRESSMODE_SHIFT)

+

+typedef enum _D3DVS_ADDRESSMODE_TYPE

+{

+    D3DVS_ADDRMODE_ABSOLUTE  = (0 << D3DVS_ADDRESSMODE_SHIFT),

+    D3DVS_ADDRMODE_RELATIVE  = (1 << D3DVS_ADDRESSMODE_SHIFT),   // Relative to register A0

+    D3DVS_ADDRMODE_FORCE_DWORD = 0x7fffffff, // force 32-bit size enum

+} D3DVS_ADDRESSMODE_TYPE;

+

+// Source operand swizzle definitions

+//

+#define D3DVS_SWIZZLE_SHIFT     16

+#define D3DVS_SWIZZLE_MASK      0x00FF0000

+

+// The following bits define where to take component X from:

+

+#define D3DVS_X_X       (0 << D3DVS_SWIZZLE_SHIFT)

+#define D3DVS_X_Y       (1 << D3DVS_SWIZZLE_SHIFT)

+#define D3DVS_X_Z       (2 << D3DVS_SWIZZLE_SHIFT)

+#define D3DVS_X_W       (3 << D3DVS_SWIZZLE_SHIFT)

+

+// The following bits define where to take component Y from:

+

+#define D3DVS_Y_X       (0 << (D3DVS_SWIZZLE_SHIFT + 2))

+#define D3DVS_Y_Y       (1 << (D3DVS_SWIZZLE_SHIFT + 2))

+#define D3DVS_Y_Z       (2 << (D3DVS_SWIZZLE_SHIFT + 2))

+#define D3DVS_Y_W       (3 << (D3DVS_SWIZZLE_SHIFT + 2))

+

+// The following bits define where to take component Z from:

+

+#define D3DVS_Z_X       (0 << (D3DVS_SWIZZLE_SHIFT + 4))

+#define D3DVS_Z_Y       (1 << (D3DVS_SWIZZLE_SHIFT + 4))

+#define D3DVS_Z_Z       (2 << (D3DVS_SWIZZLE_SHIFT + 4))

+#define D3DVS_Z_W       (3 << (D3DVS_SWIZZLE_SHIFT + 4))

+

+// The following bits define where to take component W from:

+

+#define D3DVS_W_X       (0 << (D3DVS_SWIZZLE_SHIFT + 6))

+#define D3DVS_W_Y       (1 << (D3DVS_SWIZZLE_SHIFT + 6))

+#define D3DVS_W_Z       (2 << (D3DVS_SWIZZLE_SHIFT + 6))

+#define D3DVS_W_W       (3 << (D3DVS_SWIZZLE_SHIFT + 6))

+

+// Value when there is no swizzle (X is taken from X, Y is taken from Y,

+// Z is taken from Z, W is taken from W

+//

+#define D3DVS_NOSWIZZLE (D3DVS_X_X | D3DVS_Y_Y | D3DVS_Z_Z | D3DVS_W_W)

+

+// source parameter swizzle

+#define D3DSP_SWIZZLE_SHIFT     16

+#define D3DSP_SWIZZLE_MASK      0x00FF0000

+

+#define D3DSP_NOSWIZZLE \

+    ( (0 << (D3DSP_SWIZZLE_SHIFT + 0)) | \

+      (1 << (D3DSP_SWIZZLE_SHIFT + 2)) | \

+      (2 << (D3DSP_SWIZZLE_SHIFT + 4)) | \

+      (3 << (D3DSP_SWIZZLE_SHIFT + 6)) )

+

+// pixel-shader swizzle ops

+#define D3DSP_REPLICATERED \

+    ( (0 << (D3DSP_SWIZZLE_SHIFT + 0)) | \

+      (0 << (D3DSP_SWIZZLE_SHIFT + 2)) | \

+      (0 << (D3DSP_SWIZZLE_SHIFT + 4)) | \

+      (0 << (D3DSP_SWIZZLE_SHIFT + 6)) )

+

+#define D3DSP_REPLICATEGREEN \

+    ( (1 << (D3DSP_SWIZZLE_SHIFT + 0)) | \

+      (1 << (D3DSP_SWIZZLE_SHIFT + 2)) | \

+      (1 << (D3DSP_SWIZZLE_SHIFT + 4)) | \

+      (1 << (D3DSP_SWIZZLE_SHIFT + 6)) )

+

+#define D3DSP_REPLICATEBLUE \

+    ( (2 << (D3DSP_SWIZZLE_SHIFT + 0)) | \

+      (2 << (D3DSP_SWIZZLE_SHIFT + 2)) | \

+      (2 << (D3DSP_SWIZZLE_SHIFT + 4)) | \

+      (2 << (D3DSP_SWIZZLE_SHIFT + 6)) )

+

+#define D3DSP_REPLICATEALPHA \

+    ( (3 << (D3DSP_SWIZZLE_SHIFT + 0)) | \

+      (3 << (D3DSP_SWIZZLE_SHIFT + 2)) | \

+      (3 << (D3DSP_SWIZZLE_SHIFT + 4)) | \

+      (3 << (D3DSP_SWIZZLE_SHIFT + 6)) )

+

+// source parameter modifiers

+#define D3DSP_SRCMOD_SHIFT      24

+#define D3DSP_SRCMOD_MASK       0x0F000000

+

+typedef enum _D3DSHADER_PARAM_SRCMOD_TYPE

+{

+    D3DSPSM_NONE    = 0<<D3DSP_SRCMOD_SHIFT, // nop

+    D3DSPSM_NEG     = 1<<D3DSP_SRCMOD_SHIFT, // negate

+    D3DSPSM_BIAS    = 2<<D3DSP_SRCMOD_SHIFT, // bias

+    D3DSPSM_BIASNEG = 3<<D3DSP_SRCMOD_SHIFT, // bias and negate

+    D3DSPSM_SIGN    = 4<<D3DSP_SRCMOD_SHIFT, // sign

+    D3DSPSM_SIGNNEG = 5<<D3DSP_SRCMOD_SHIFT, // sign and negate

+    D3DSPSM_COMP    = 6<<D3DSP_SRCMOD_SHIFT, // complement

+    D3DSPSM_X2      = 7<<D3DSP_SRCMOD_SHIFT, // *2

+    D3DSPSM_X2NEG   = 8<<D3DSP_SRCMOD_SHIFT, // *2 and negate

+    D3DSPSM_DZ      = 9<<D3DSP_SRCMOD_SHIFT, // divide through by z component 

+    D3DSPSM_DW      = 10<<D3DSP_SRCMOD_SHIFT, // divide through by w component

+    D3DSPSM_FORCE_DWORD = 0x7fffffff,        // force 32-bit size enum

+} D3DSHADER_PARAM_SRCMOD_TYPE;

+

+// pixel shader version token

+#define D3DPS_VERSION(_Major,_Minor) (0xFFFF0000|((_Major)<<8)|(_Minor))

+

+// vertex shader version token

+#define D3DVS_VERSION(_Major,_Minor) (0xFFFE0000|((_Major)<<8)|(_Minor))

+

+// extract major/minor from version cap

+#define D3DSHADER_VERSION_MAJOR(_Version) (((_Version)>>8)&0xFF)

+#define D3DSHADER_VERSION_MINOR(_Version) (((_Version)>>0)&0xFF)

+

+// destination/source parameter register type

+#define D3DSI_COMMENTSIZE_SHIFT     16

+#define D3DSI_COMMENTSIZE_MASK      0x7FFF0000

+#define D3DSHADER_COMMENT(_DWordSize) \

+    ((((_DWordSize)<<D3DSI_COMMENTSIZE_SHIFT)&D3DSI_COMMENTSIZE_MASK)|D3DSIO_COMMENT)

+

+// pixel/vertex shader end token

+#define D3DPS_END()  0x0000FFFF

+#define D3DVS_END()  0x0000FFFF

+

+//---------------------------------------------------------------------

+

+// High order surfaces

+//

+typedef enum _D3DBASISTYPE

+{

+   D3DBASIS_BEZIER      = 0,

+   D3DBASIS_BSPLINE     = 1,

+   D3DBASIS_INTERPOLATE = 2,

+   D3DBASIS_FORCE_DWORD = 0x7fffffff,

+} D3DBASISTYPE;

+

+typedef enum _D3DORDERTYPE

+{

+   D3DORDER_LINEAR      = 1,

+   D3DORDER_QUADRATIC   = 2,

+   D3DORDER_CUBIC       = 3,

+   D3DORDER_QUINTIC     = 5,

+   D3DORDER_FORCE_DWORD = 0x7fffffff,

+} D3DORDERTYPE;

+

+typedef enum _D3DPATCHEDGESTYLE

+{

+   D3DPATCHEDGE_DISCRETE    = 0,

+   D3DPATCHEDGE_CONTINUOUS  = 1,

+   D3DPATCHEDGE_FORCE_DWORD = 0x7fffffff,

+} D3DPATCHEDGESTYLE;

+

+typedef enum _D3DSTATEBLOCKTYPE

+{

+    D3DSBT_ALL           = 1, // capture all state

+    D3DSBT_PIXELSTATE    = 2, // capture pixel state

+    D3DSBT_VERTEXSTATE   = 3, // capture vertex state

+    D3DSBT_FORCE_DWORD   = 0x7fffffff,

+} D3DSTATEBLOCKTYPE;

+

+// The D3DVERTEXBLENDFLAGS type is used with D3DRS_VERTEXBLEND state.

+//

+typedef enum _D3DVERTEXBLENDFLAGS

+{

+    D3DVBF_DISABLE  = 0,     // Disable vertex blending

+    D3DVBF_1WEIGHTS = 1,     // 2 matrix blending

+    D3DVBF_2WEIGHTS = 2,     // 3 matrix blending

+    D3DVBF_3WEIGHTS = 3,     // 4 matrix blending

+    D3DVBF_TWEENING = 255,   // blending using D3DRS_TWEENFACTOR

+    D3DVBF_0WEIGHTS = 256,   // one matrix is used with weight 1.0

+    D3DVBF_FORCE_DWORD = 0x7fffffff, // force 32-bit size enum

+} D3DVERTEXBLENDFLAGS;

+

+typedef enum _D3DTEXTURETRANSFORMFLAGS {

+    D3DTTFF_DISABLE         = 0,    // texture coordinates are passed directly

+    D3DTTFF_COUNT1          = 1,    // rasterizer should expect 1-D texture coords

+    D3DTTFF_COUNT2          = 2,    // rasterizer should expect 2-D texture coords

+    D3DTTFF_COUNT3          = 3,    // rasterizer should expect 3-D texture coords

+    D3DTTFF_COUNT4          = 4,    // rasterizer should expect 4-D texture coords

+    D3DTTFF_PROJECTED       = 256,  // texcoords to be divided by COUNTth element

+    D3DTTFF_FORCE_DWORD     = 0x7fffffff,

+} D3DTEXTURETRANSFORMFLAGS;

+

+// Macros to set texture coordinate format bits in the FVF id

+

+#define D3DFVF_TEXTUREFORMAT2 0         // Two floating point values

+#define D3DFVF_TEXTUREFORMAT1 3         // One floating point value

+#define D3DFVF_TEXTUREFORMAT3 1         // Three floating point values

+#define D3DFVF_TEXTUREFORMAT4 2         // Four floating point values

+

+#define D3DFVF_TEXCOORDSIZE3(CoordIndex) (D3DFVF_TEXTUREFORMAT3 << (CoordIndex*2 + 16))

+#define D3DFVF_TEXCOORDSIZE2(CoordIndex) (D3DFVF_TEXTUREFORMAT2)

+#define D3DFVF_TEXCOORDSIZE4(CoordIndex) (D3DFVF_TEXTUREFORMAT4 << (CoordIndex*2 + 16))

+#define D3DFVF_TEXCOORDSIZE1(CoordIndex) (D3DFVF_TEXTUREFORMAT1 << (CoordIndex*2 + 16))

+

+

+//---------------------------------------------------------------------

+

+/* Direct3D8 Device types */

+typedef enum _D3DDEVTYPE

+{

+    D3DDEVTYPE_HAL         = 1,

+    D3DDEVTYPE_REF         = 2,

+    D3DDEVTYPE_SW          = 3,

+

+    D3DDEVTYPE_FORCE_DWORD  = 0x7fffffff

+} D3DDEVTYPE;

+

+/* Multi-Sample buffer types */

+typedef enum _D3DMULTISAMPLE_TYPE

+{

+    D3DMULTISAMPLE_NONE            =  0,

+    D3DMULTISAMPLE_2_SAMPLES       =  2,

+    D3DMULTISAMPLE_3_SAMPLES       =  3,

+    D3DMULTISAMPLE_4_SAMPLES       =  4,

+    D3DMULTISAMPLE_5_SAMPLES       =  5,

+    D3DMULTISAMPLE_6_SAMPLES       =  6,

+    D3DMULTISAMPLE_7_SAMPLES       =  7,

+    D3DMULTISAMPLE_8_SAMPLES       =  8,

+    D3DMULTISAMPLE_9_SAMPLES       =  9,

+    D3DMULTISAMPLE_10_SAMPLES      = 10,

+    D3DMULTISAMPLE_11_SAMPLES      = 11,

+    D3DMULTISAMPLE_12_SAMPLES      = 12,

+    D3DMULTISAMPLE_13_SAMPLES      = 13,

+    D3DMULTISAMPLE_14_SAMPLES      = 14,

+    D3DMULTISAMPLE_15_SAMPLES      = 15,

+    D3DMULTISAMPLE_16_SAMPLES      = 16,

+

+    D3DMULTISAMPLE_FORCE_DWORD     = 0x7fffffff

+} D3DMULTISAMPLE_TYPE;

+

+/* Formats

+ * Most of these names have the following convention:

+ *      A = Alpha

+ *      R = Red

+ *      G = Green

+ *      B = Blue

+ *      X = Unused Bits

+ *      P = Palette

+ *      L = Luminance

+ *      U = dU coordinate for BumpMap

+ *      V = dV coordinate for BumpMap

+ *      S = Stencil

+ *      D = Depth (e.g. Z or W buffer)

+ *

+ *      Further, the order of the pieces are from MSB first; hence

+ *      D3DFMT_A8L8 indicates that the high byte of this two byte

+ *      format is alpha.

+ *

+ *      D16 indicates:

+ *           - An integer 16-bit value.

+ *           - An app-lockable surface.

+ *

+ *      All Depth/Stencil formats except D3DFMT_D16_LOCKABLE indicate:

+ *          - no particular bit ordering per pixel, and

+ *          - are not app lockable, and

+ *          - the driver is allowed to consume more than the indicated

+ *            number of bits per Depth channel (but not Stencil channel).

+ */

+#ifndef MAKEFOURCC

+    #define MAKEFOURCC(ch0, ch1, ch2, ch3)                              \

+                ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) |       \

+                ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24 ))

+#endif /* defined(MAKEFOURCC) */

+

+

+typedef enum _D3DFORMAT

+{

+    D3DFMT_UNKNOWN              =  0,

+

+    D3DFMT_R8G8B8               = 20,

+    D3DFMT_A8R8G8B8             = 21,

+    D3DFMT_X8R8G8B8             = 22,

+    D3DFMT_R5G6B5               = 23,

+    D3DFMT_X1R5G5B5             = 24,

+    D3DFMT_A1R5G5B5             = 25,

+    D3DFMT_A4R4G4B4             = 26,

+    D3DFMT_R3G3B2               = 27,

+    D3DFMT_A8                   = 28,

+    D3DFMT_A8R3G3B2             = 29,

+    D3DFMT_X4R4G4B4             = 30,

+    D3DFMT_A2B10G10R10          = 31,

+    D3DFMT_G16R16               = 34,

+

+    D3DFMT_A8P8                 = 40,

+    D3DFMT_P8                   = 41,

+

+    D3DFMT_L8                   = 50,

+    D3DFMT_A8L8                 = 51,

+    D3DFMT_A4L4                 = 52,

+

+    D3DFMT_V8U8                 = 60,

+    D3DFMT_L6V5U5               = 61,

+    D3DFMT_X8L8V8U8             = 62,

+    D3DFMT_Q8W8V8U8             = 63,

+    D3DFMT_V16U16               = 64,

+    D3DFMT_W11V11U10            = 65,

+    D3DFMT_A2W10V10U10          = 67,

+

+    D3DFMT_UYVY                 = MAKEFOURCC('U', 'Y', 'V', 'Y'),

+    D3DFMT_YUY2                 = MAKEFOURCC('Y', 'U', 'Y', '2'),

+    D3DFMT_DXT1                 = MAKEFOURCC('D', 'X', 'T', '1'),

+    D3DFMT_DXT2                 = MAKEFOURCC('D', 'X', 'T', '2'),

+    D3DFMT_DXT3                 = MAKEFOURCC('D', 'X', 'T', '3'),

+    D3DFMT_DXT4                 = MAKEFOURCC('D', 'X', 'T', '4'),

+    D3DFMT_DXT5                 = MAKEFOURCC('D', 'X', 'T', '5'),

+

+    D3DFMT_D16_LOCKABLE         = 70,

+    D3DFMT_D32                  = 71,

+    D3DFMT_D15S1                = 73,

+    D3DFMT_D24S8                = 75,

+    D3DFMT_D16                  = 80,

+    D3DFMT_D24X8                = 77,

+    D3DFMT_D24X4S4              = 79,

+

+

+    D3DFMT_VERTEXDATA           =100,

+    D3DFMT_INDEX16              =101,

+    D3DFMT_INDEX32              =102,

+

+    D3DFMT_FORCE_DWORD          =0x7fffffff

+} D3DFORMAT;

+

+/* Display Modes */

+typedef struct _D3DDISPLAYMODE

+{

+    UINT            Width;

+    UINT            Height;

+    UINT            RefreshRate;

+    D3DFORMAT       Format;

+} D3DDISPLAYMODE;

+

+/* Creation Parameters */

+typedef struct _D3DDEVICE_CREATION_PARAMETERS

+{

+    UINT            AdapterOrdinal;

+    D3DDEVTYPE      DeviceType;

+    HWND            hFocusWindow;

+    DWORD           BehaviorFlags;

+} D3DDEVICE_CREATION_PARAMETERS;

+

+

+/* SwapEffects */

+typedef enum _D3DSWAPEFFECT

+{

+    D3DSWAPEFFECT_DISCARD           = 1,

+    D3DSWAPEFFECT_FLIP              = 2,

+    D3DSWAPEFFECT_COPY              = 3,

+    D3DSWAPEFFECT_COPY_VSYNC        = 4,

+

+    D3DSWAPEFFECT_FORCE_DWORD       = 0x7fffffff

+} D3DSWAPEFFECT;

+

+/* Pool types */

+typedef enum _D3DPOOL {

+    D3DPOOL_DEFAULT                 = 0,

+    D3DPOOL_MANAGED                 = 1,

+    D3DPOOL_SYSTEMMEM               = 2,

+    D3DPOOL_SCRATCH                 = 3,

+

+    D3DPOOL_FORCE_DWORD             = 0x7fffffff

+} D3DPOOL;

+

+

+/* RefreshRate pre-defines */

+#define D3DPRESENT_RATE_DEFAULT         0x00000000

+#define D3DPRESENT_RATE_UNLIMITED       0x7fffffff

+

+

+/* Resize Optional Parameters */

+typedef struct _D3DPRESENT_PARAMETERS_

+{

+    UINT                BackBufferWidth;

+    UINT                BackBufferHeight;

+    D3DFORMAT           BackBufferFormat;

+    UINT                BackBufferCount;

+

+    D3DMULTISAMPLE_TYPE MultiSampleType;

+

+    D3DSWAPEFFECT       SwapEffect;

+    HWND                hDeviceWindow;

+    BOOL                Windowed;

+    BOOL                EnableAutoDepthStencil;

+    D3DFORMAT           AutoDepthStencilFormat;

+    DWORD               Flags;

+

+    /* Following elements must be zero for Windowed mode */

+    UINT                FullScreen_RefreshRateInHz;

+    UINT                FullScreen_PresentationInterval;

+

+} D3DPRESENT_PARAMETERS;

+

+// Values for D3DPRESENT_PARAMETERS.Flags

+

+#define D3DPRESENTFLAG_LOCKABLE_BACKBUFFER  0x00000001

+

+

+/* Gamma Ramp: Same as DX7 */

+

+typedef struct _D3DGAMMARAMP

+{

+    WORD                red  [256];

+    WORD                green[256];

+    WORD                blue [256];

+} D3DGAMMARAMP;

+

+/* Back buffer types */

+typedef enum _D3DBACKBUFFER_TYPE

+{

+    D3DBACKBUFFER_TYPE_MONO         = 0,

+    D3DBACKBUFFER_TYPE_LEFT         = 1,

+    D3DBACKBUFFER_TYPE_RIGHT        = 2,

+

+    D3DBACKBUFFER_TYPE_FORCE_DWORD  = 0x7fffffff

+} D3DBACKBUFFER_TYPE;

+

+

+/* Types */

+typedef enum _D3DRESOURCETYPE {

+    D3DRTYPE_SURFACE                =  1,

+    D3DRTYPE_VOLUME                 =  2,

+    D3DRTYPE_TEXTURE                =  3,

+    D3DRTYPE_VOLUMETEXTURE          =  4,

+    D3DRTYPE_CUBETEXTURE            =  5,

+    D3DRTYPE_VERTEXBUFFER           =  6,

+    D3DRTYPE_INDEXBUFFER            =  7,

+

+

+    D3DRTYPE_FORCE_DWORD            = 0x7fffffff

+} D3DRESOURCETYPE;

+

+/* Usages */

+#define D3DUSAGE_RENDERTARGET       (0x00000001L)

+#define D3DUSAGE_DEPTHSTENCIL       (0x00000002L)

+

+/* Usages for Vertex/Index buffers */

+#define D3DUSAGE_WRITEONLY          (0x00000008L)

+#define D3DUSAGE_SOFTWAREPROCESSING (0x00000010L)

+#define D3DUSAGE_DONOTCLIP          (0x00000020L)

+#define D3DUSAGE_POINTS             (0x00000040L)

+#define D3DUSAGE_RTPATCHES          (0x00000080L)

+#define D3DUSAGE_NPATCHES           (0x00000100L)

+#define D3DUSAGE_DYNAMIC            (0x00000200L)

+

+

+

+

+

+

+

+

+

+/* CubeMap Face identifiers */

+typedef enum _D3DCUBEMAP_FACES

+{

+    D3DCUBEMAP_FACE_POSITIVE_X     = 0,

+    D3DCUBEMAP_FACE_NEGATIVE_X     = 1,

+    D3DCUBEMAP_FACE_POSITIVE_Y     = 2,

+    D3DCUBEMAP_FACE_NEGATIVE_Y     = 3,

+    D3DCUBEMAP_FACE_POSITIVE_Z     = 4,

+    D3DCUBEMAP_FACE_NEGATIVE_Z     = 5,

+

+    D3DCUBEMAP_FACE_FORCE_DWORD    = 0x7fffffff

+} D3DCUBEMAP_FACES;

+

+

+/* Lock flags */

+

+#define D3DLOCK_READONLY           0x00000010L

+#define D3DLOCK_DISCARD             0x00002000L

+#define D3DLOCK_NOOVERWRITE        0x00001000L

+#define D3DLOCK_NOSYSLOCK          0x00000800L

+

+#define D3DLOCK_NO_DIRTY_UPDATE     0x00008000L

+

+

+

+

+

+

+/* Vertex Buffer Description */

+typedef struct _D3DVERTEXBUFFER_DESC

+{

+    D3DFORMAT           Format;

+    D3DRESOURCETYPE     Type;

+    DWORD               Usage;

+    D3DPOOL             Pool;

+    UINT                Size;

+

+    DWORD               FVF;

+

+} D3DVERTEXBUFFER_DESC;

+

+/* Index Buffer Description */

+typedef struct _D3DINDEXBUFFER_DESC

+{

+    D3DFORMAT           Format;

+    D3DRESOURCETYPE     Type;

+    DWORD               Usage;

+    D3DPOOL             Pool;

+    UINT                Size;

+} D3DINDEXBUFFER_DESC;

+

+

+/* Surface Description */

+typedef struct _D3DSURFACE_DESC

+{

+    D3DFORMAT           Format;

+    D3DRESOURCETYPE     Type;

+    DWORD               Usage;

+    D3DPOOL             Pool;

+    UINT                Size;

+

+    D3DMULTISAMPLE_TYPE MultiSampleType;

+    UINT                Width;

+    UINT                Height;

+} D3DSURFACE_DESC;

+

+typedef struct _D3DVOLUME_DESC

+{

+    D3DFORMAT           Format;

+    D3DRESOURCETYPE     Type;

+    DWORD               Usage;

+    D3DPOOL             Pool;

+    UINT                Size;

+

+    UINT                Width;

+    UINT                Height;

+    UINT                Depth;

+} D3DVOLUME_DESC;

+

+/* Structure for LockRect */

+typedef struct _D3DLOCKED_RECT

+{

+    INT                 Pitch;

+    void*               pBits;

+} D3DLOCKED_RECT;

+

+/* Structures for LockBox */

+typedef struct _D3DBOX

+{

+    UINT                Left;

+    UINT                Top;

+    UINT                Right;

+    UINT                Bottom;

+    UINT                Front;

+    UINT                Back;

+} D3DBOX;

+

+typedef struct _D3DLOCKED_BOX

+{

+    INT                 RowPitch;

+    INT                 SlicePitch;

+    void*               pBits;

+} D3DLOCKED_BOX;

+

+/* Structures for LockRange */

+typedef struct _D3DRANGE

+{

+    UINT                Offset;

+    UINT                Size;

+} D3DRANGE;

+

+/* Structures for high order primitives */

+typedef struct _D3DRECTPATCH_INFO

+{

+    UINT                StartVertexOffsetWidth;

+    UINT                StartVertexOffsetHeight;

+    UINT                Width;

+    UINT                Height;

+    UINT                Stride;

+    D3DBASISTYPE        Basis;

+    D3DORDERTYPE        Order;

+} D3DRECTPATCH_INFO;

+

+typedef struct _D3DTRIPATCH_INFO

+{

+    UINT                StartVertexOffset;

+    UINT                NumVertices;

+    D3DBASISTYPE        Basis;

+    D3DORDERTYPE        Order;

+} D3DTRIPATCH_INFO;

+

+/* Adapter Identifier */

+

+#define MAX_DEVICE_IDENTIFIER_STRING        512

+typedef struct _D3DADAPTER_IDENTIFIER8

+{

+    char            Driver[MAX_DEVICE_IDENTIFIER_STRING];

+    char            Description[MAX_DEVICE_IDENTIFIER_STRING];

+

+#ifdef _WIN32

+    LARGE_INTEGER   DriverVersion;            /* Defined for 32 bit components */

+#else

+    DWORD           DriverVersionLowPart;     /* Defined for 16 bit driver components */

+    DWORD           DriverVersionHighPart;

+#endif

+

+    DWORD           VendorId;

+    DWORD           DeviceId;

+    DWORD           SubSysId;

+    DWORD           Revision;

+

+    GUID            DeviceIdentifier;

+

+    DWORD           WHQLLevel;

+

+} D3DADAPTER_IDENTIFIER8;

+

+

+/* Raster Status structure returned by GetRasterStatus */

+typedef struct _D3DRASTER_STATUS

+{

+    BOOL            InVBlank;

+    UINT            ScanLine;

+} D3DRASTER_STATUS;

+

+

+

+/* Debug monitor tokens (DEBUG only)

+

+   Note that if D3DRS_DEBUGMONITORTOKEN is set, the call is treated as

+   passing a token to the debug monitor.  For example, if, after passing

+   D3DDMT_ENABLE/DISABLE to D3DRS_DEBUGMONITORTOKEN other token values

+   are passed in, the enabled/disabled state of the debug

+   monitor will still persist.

+

+   The debug monitor defaults to enabled.

+

+   Calling GetRenderState on D3DRS_DEBUGMONITORTOKEN is not of any use.

+*/

+typedef enum _D3DDEBUGMONITORTOKENS {

+    D3DDMT_ENABLE            = 0,    // enable debug monitor

+    D3DDMT_DISABLE           = 1,    // disable debug monitor

+    D3DDMT_FORCE_DWORD     = 0x7fffffff,

+} D3DDEBUGMONITORTOKENS;

+

+// GetInfo IDs

+

+#define D3DDEVINFOID_RESOURCEMANAGER    5           /* Used with D3DDEVINFO_RESOURCEMANAGER */

+#define D3DDEVINFOID_VERTEXSTATS        6           /* Used with D3DDEVINFO_D3DVERTEXSTATS */

+

+typedef struct _D3DRESOURCESTATS

+{

+// Data collected since last Present()

+    BOOL    bThrashing;             /* indicates if thrashing */

+    DWORD   ApproxBytesDownloaded;  /* Approximate number of bytes downloaded by resource manager */

+    DWORD   NumEvicts;              /* number of objects evicted */

+    DWORD   NumVidCreates;          /* number of objects created in video memory */

+    DWORD   LastPri;                /* priority of last object evicted */

+    DWORD   NumUsed;                /* number of objects set to the device */

+    DWORD   NumUsedInVidMem;        /* number of objects set to the device, which are already in video memory */

+// Persistent data

+    DWORD   WorkingSet;             /* number of objects in video memory */

+    DWORD   WorkingSetBytes;        /* number of bytes in video memory */

+    DWORD   TotalManaged;           /* total number of managed objects */

+    DWORD   TotalBytes;             /* total number of bytes of managed objects */

+} D3DRESOURCESTATS;

+

+#define D3DRTYPECOUNT (D3DRTYPE_INDEXBUFFER+1)

+

+typedef struct _D3DDEVINFO_RESOURCEMANAGER

+{

+    D3DRESOURCESTATS    stats[D3DRTYPECOUNT];

+} D3DDEVINFO_RESOURCEMANAGER, *LPD3DDEVINFO_RESOURCEMANAGER;

+

+typedef struct _D3DDEVINFO_D3DVERTEXSTATS

+{

+    DWORD   NumRenderedTriangles;       /* total number of triangles that are not clipped in this frame */

+    DWORD   NumExtraClippingTriangles;  /* Number of new triangles generated by clipping */

+} D3DDEVINFO_D3DVERTEXSTATS, *LPD3DDEVINFO_D3DVERTEXSTATS;

+

+

+#pragma pack()

+#if _MSC_VER >= 1200

+#pragma warning(pop)

+#else

+#pragma warning(default:4201)

+#endif

+

+#endif /* (DIRECT3D_VERSION >= 0x0800) */

+#endif /* _D3D8TYPES(P)_H_ */

+

diff --git a/src/D3D8/Capabilities.cpp b/src/D3D8/Capabilities.cpp
new file mode 100644
index 0000000..dd0ba20
--- /dev/null
+++ b/src/D3D8/Capabilities.cpp
@@ -0,0 +1,246 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Capabilities.hpp"
+
+#include "Main/Config.hpp"
+
+namespace D3D8
+{
+	bool Capabilities::Surface::RenderTarget::R8G8B8 = false;
+	bool Capabilities::Surface::RenderTarget::R5G6B5 = true;
+	bool Capabilities::Surface::RenderTarget::X1R5G5B5 = true;
+	bool Capabilities::Surface::RenderTarget::A1R5G5B5 = true;
+	bool Capabilities::Surface::RenderTarget::A4R4G4B4 = true;
+	bool Capabilities::Surface::RenderTarget::R3G3B2 = false;
+	bool Capabilities::Surface::RenderTarget::A8R3G3B2 = false;
+	bool Capabilities::Surface::RenderTarget::X4R4G4B4 = true;
+	bool Capabilities::Surface::RenderTarget::A8R8G8B8 = true;
+	bool Capabilities::Surface::RenderTarget::X8R8G8B8 = true;
+	bool Capabilities::Surface::RenderTarget::A8B8G8R8 = true;
+	bool Capabilities::Surface::RenderTarget::X8B8G8R8 = true;
+	bool Capabilities::Surface::RenderTarget::G16R16 = true;
+	bool Capabilities::Surface::RenderTarget::A2B10G10R10 = true;
+
+	bool Capabilities::Surface::DepthStencil::D32 = true;
+	bool Capabilities::Surface::DepthStencil::D24S8 = true;
+	bool Capabilities::Surface::DepthStencil::D24X8 = true;
+	bool Capabilities::Surface::DepthStencil::D16 = true;
+
+	bool Capabilities::Surface::A8 = true;
+	bool Capabilities::Surface::R5G6B5 = true;
+	bool Capabilities::Surface::X1R5G5B5 = true;
+	bool Capabilities::Surface::A1R5G5B5 = true;
+	bool Capabilities::Surface::A4R4G4B4 = true;
+	bool Capabilities::Surface::R3G3B2 = true;
+	bool Capabilities::Surface::A8R3G3B2 = true;
+	bool Capabilities::Surface::X4R4G4B4 = true;
+	bool Capabilities::Surface::R8G8B8 = false;
+	bool Capabilities::Surface::X8R8G8B8 = true;
+	bool Capabilities::Surface::A8R8G8B8 = true;
+	bool Capabilities::Surface::X8B8G8R8 = true;
+	bool Capabilities::Surface::A8B8G8R8 = true;
+	bool Capabilities::Surface::P8 = false;
+	bool Capabilities::Surface::A8P8 = false;
+	bool Capabilities::Surface::G16R16 = true;
+	bool Capabilities::Surface::A2B10G10R10 = true;
+	bool Capabilities::Surface::DXT1 = S3TC_SUPPORT;
+	bool Capabilities::Surface::DXT2 = S3TC_SUPPORT;
+	bool Capabilities::Surface::DXT3 = S3TC_SUPPORT;
+	bool Capabilities::Surface::DXT4 = S3TC_SUPPORT;
+	bool Capabilities::Surface::DXT5 = S3TC_SUPPORT;
+	bool Capabilities::Surface::V8U8 = true;
+	bool Capabilities::Surface::L6V5U5 = true;
+	bool Capabilities::Surface::X8L8V8U8 = true;
+	bool Capabilities::Surface::Q8W8V8U8 = true;
+	bool Capabilities::Surface::V16U16 = true;
+	bool Capabilities::Surface::A2W10V10U10 = true;
+	bool Capabilities::Surface::L8 = true;
+	bool Capabilities::Surface::A4L4 = true;
+	bool Capabilities::Surface::A8L8 = true;
+
+	bool Capabilities::Volume::A8 = true;
+	bool Capabilities::Volume::R5G6B5 = true;
+	bool Capabilities::Volume::X1R5G5B5 = true;
+	bool Capabilities::Volume::A1R5G5B5 = true;
+	bool Capabilities::Volume::A4R4G4B4 = true;
+	bool Capabilities::Volume::R3G3B2 = true;
+	bool Capabilities::Volume::A8R3G3B2 = true;
+	bool Capabilities::Volume::X4R4G4B4 = true;
+	bool Capabilities::Volume::R8G8B8 = false;
+	bool Capabilities::Volume::X8R8G8B8 = true;
+	bool Capabilities::Volume::A8R8G8B8 = true;
+	bool Capabilities::Volume::X8B8G8R8 = true;
+	bool Capabilities::Volume::A8B8G8R8 = true;
+	bool Capabilities::Volume::P8 = false;
+	bool Capabilities::Volume::A8P8 = false;
+	bool Capabilities::Volume::G16R16 = true;
+	bool Capabilities::Volume::A2B10G10R10 = true;
+	bool Capabilities::Volume::DXT1 = S3TC_SUPPORT;
+	bool Capabilities::Volume::DXT2 = S3TC_SUPPORT;
+	bool Capabilities::Volume::DXT3 = S3TC_SUPPORT;
+	bool Capabilities::Volume::DXT4 = S3TC_SUPPORT;
+	bool Capabilities::Volume::DXT5 = S3TC_SUPPORT;
+	bool Capabilities::Volume::V8U8 = true;
+	bool Capabilities::Volume::L6V5U5 = true;
+	bool Capabilities::Volume::X8L8V8U8 = true;
+	bool Capabilities::Volume::Q8W8V8U8 = true;
+	bool Capabilities::Volume::V16U16 = true;
+	bool Capabilities::Volume::A2W10V10U10 = true;
+	bool Capabilities::Volume::L8 = true;
+	bool Capabilities::Volume::A4L4 = true;
+	bool Capabilities::Volume::A8L8 = true;
+
+	bool Capabilities::CubeMap::RenderTarget::R8G8B8 = false;
+	bool Capabilities::CubeMap::RenderTarget::R5G6B5 = true;
+	bool Capabilities::CubeMap::RenderTarget::X1R5G5B5 = true;
+	bool Capabilities::CubeMap::RenderTarget::A1R5G5B5 = true;
+	bool Capabilities::CubeMap::RenderTarget::A4R4G4B4 = true;
+	bool Capabilities::CubeMap::RenderTarget::R3G3B2 = false;
+	bool Capabilities::CubeMap::RenderTarget::A8R3G3B2 = false;
+	bool Capabilities::CubeMap::RenderTarget::X4R4G4B4 = true;
+	bool Capabilities::CubeMap::RenderTarget::A8R8G8B8 = true;
+	bool Capabilities::CubeMap::RenderTarget::X8R8G8B8 = true;
+	bool Capabilities::CubeMap::RenderTarget::A8B8G8R8 = true;
+	bool Capabilities::CubeMap::RenderTarget::X8B8G8R8 = true;
+	bool Capabilities::CubeMap::RenderTarget::G16R16 = true;
+	bool Capabilities::CubeMap::RenderTarget::A2B10G10R10 = true;
+
+	bool Capabilities::CubeMap::DepthStencil::D32 = false;
+	bool Capabilities::CubeMap::DepthStencil::D24S8 = false;
+	bool Capabilities::CubeMap::DepthStencil::D24X8 = false;
+	bool Capabilities::CubeMap::DepthStencil::D16 = false;
+
+	bool Capabilities::CubeMap::A8 = true;
+	bool Capabilities::CubeMap::R5G6B5 = true;
+	bool Capabilities::CubeMap::X1R5G5B5 = true;
+	bool Capabilities::CubeMap::A1R5G5B5 = true;
+	bool Capabilities::CubeMap::A4R4G4B4 = true;
+	bool Capabilities::CubeMap::R3G3B2 = true;
+	bool Capabilities::CubeMap::A8R3G3B2 = true;
+	bool Capabilities::CubeMap::X4R4G4B4 = true;
+	bool Capabilities::CubeMap::R8G8B8 = false;
+	bool Capabilities::CubeMap::X8R8G8B8 = true;
+	bool Capabilities::CubeMap::A8R8G8B8 = true;
+	bool Capabilities::CubeMap::X8B8G8R8 = true;
+	bool Capabilities::CubeMap::A8B8G8R8 = true;
+	bool Capabilities::CubeMap::P8 = false;
+	bool Capabilities::CubeMap::A8P8 = false;
+	bool Capabilities::CubeMap::G16R16 = true;
+	bool Capabilities::CubeMap::A2B10G10R10 = true;
+	bool Capabilities::CubeMap::DXT1 = S3TC_SUPPORT;
+	bool Capabilities::CubeMap::DXT2 = S3TC_SUPPORT;
+	bool Capabilities::CubeMap::DXT3 = S3TC_SUPPORT;
+	bool Capabilities::CubeMap::DXT4 = S3TC_SUPPORT;
+	bool Capabilities::CubeMap::DXT5 = S3TC_SUPPORT;
+	bool Capabilities::CubeMap::V8U8 = true;
+	bool Capabilities::CubeMap::L6V5U5 = true;
+	bool Capabilities::CubeMap::X8L8V8U8 = true;
+	bool Capabilities::CubeMap::Q8W8V8U8 = true;
+	bool Capabilities::CubeMap::V16U16 = true;
+	bool Capabilities::CubeMap::A2W10V10U10 = true;
+	bool Capabilities::CubeMap::L8 = true;
+	bool Capabilities::CubeMap::A4L4 = true;
+	bool Capabilities::CubeMap::A8L8 = true;
+
+	bool Capabilities::VolumeTexture::A8 = true;
+	bool Capabilities::VolumeTexture::R5G6B5 = true;
+	bool Capabilities::VolumeTexture::X1R5G5B5 = true;
+	bool Capabilities::VolumeTexture::A1R5G5B5 = true;
+	bool Capabilities::VolumeTexture::A4R4G4B4 = true;
+	bool Capabilities::VolumeTexture::R3G3B2 = true;
+	bool Capabilities::VolumeTexture::A8R3G3B2 = true;
+	bool Capabilities::VolumeTexture::X4R4G4B4 = true;
+	bool Capabilities::VolumeTexture::R8G8B8 = false;
+	bool Capabilities::VolumeTexture::X8R8G8B8 = true;
+	bool Capabilities::VolumeTexture::A8R8G8B8 = true;
+	bool Capabilities::VolumeTexture::X8B8G8R8 = true;
+	bool Capabilities::VolumeTexture::A8B8G8R8 = true;
+	bool Capabilities::VolumeTexture::P8 = false;
+	bool Capabilities::VolumeTexture::A8P8 = false;
+	bool Capabilities::VolumeTexture::G16R16 = true;
+	bool Capabilities::VolumeTexture::A2B10G10R10 = true;
+	bool Capabilities::VolumeTexture::DXT1 = S3TC_SUPPORT;
+	bool Capabilities::VolumeTexture::DXT2 = S3TC_SUPPORT;
+	bool Capabilities::VolumeTexture::DXT3 = S3TC_SUPPORT;
+	bool Capabilities::VolumeTexture::DXT4 = S3TC_SUPPORT;
+	bool Capabilities::VolumeTexture::DXT5 = S3TC_SUPPORT;
+	bool Capabilities::VolumeTexture::V8U8 = true;
+	bool Capabilities::VolumeTexture::L6V5U5 = true;
+	bool Capabilities::VolumeTexture::X8L8V8U8 = true;
+	bool Capabilities::VolumeTexture::Q8W8V8U8 = true;
+	bool Capabilities::VolumeTexture::V16U16 = true;
+	bool Capabilities::VolumeTexture::A2W10V10U10 = true;
+	bool Capabilities::VolumeTexture::L8 = true;
+	bool Capabilities::VolumeTexture::A4L4 = true;
+	bool Capabilities::VolumeTexture::A8L8 = true;
+
+	bool Capabilities::Texture::RenderTarget::R8G8B8 = false;
+	bool Capabilities::Texture::RenderTarget::R5G6B5 = true;
+	bool Capabilities::Texture::RenderTarget::X1R5G5B5 = true;
+	bool Capabilities::Texture::RenderTarget::A1R5G5B5 = true;
+	bool Capabilities::Texture::RenderTarget::A4R4G4B4 = true;
+	bool Capabilities::Texture::RenderTarget::R3G3B2 = false;
+	bool Capabilities::Texture::RenderTarget::A8R3G3B2 = false;
+	bool Capabilities::Texture::RenderTarget::X4R4G4B4 = true;
+	bool Capabilities::Texture::RenderTarget::A8R8G8B8 = true;
+	bool Capabilities::Texture::RenderTarget::X8R8G8B8 = true;
+	bool Capabilities::Texture::RenderTarget::A8B8G8R8 = true;
+	bool Capabilities::Texture::RenderTarget::X8B8G8R8 = true;
+	bool Capabilities::Texture::RenderTarget::G16R16 = true;
+	bool Capabilities::Texture::RenderTarget::A2B10G10R10 = true;
+
+	bool Capabilities::Texture::DepthStencil::D32 = false;
+	bool Capabilities::Texture::DepthStencil::D24S8 = false;
+	bool Capabilities::Texture::DepthStencil::D24X8 = false;
+	bool Capabilities::Texture::DepthStencil::D16 = false;
+
+	bool Capabilities::Texture::A8 = true;
+	bool Capabilities::Texture::R5G6B5 = true;
+	bool Capabilities::Texture::X1R5G5B5 = true;
+	bool Capabilities::Texture::A1R5G5B5 = true;
+	bool Capabilities::Texture::A4R4G4B4 = true;
+	bool Capabilities::Texture::R3G3B2 = true;
+	bool Capabilities::Texture::A8R3G3B2 = true;
+	bool Capabilities::Texture::X4R4G4B4 = true;
+	bool Capabilities::Texture::R8G8B8 = false;
+	bool Capabilities::Texture::X8R8G8B8 = true;
+	bool Capabilities::Texture::A8R8G8B8 = true;
+	bool Capabilities::Texture::X8B8G8R8 = true;
+	bool Capabilities::Texture::A8B8G8R8 = true;
+	bool Capabilities::Texture::P8 = false;
+	bool Capabilities::Texture::A8P8 = false;
+	bool Capabilities::Texture::G16R16 = true;
+	bool Capabilities::Texture::A2B10G10R10 = true;
+	bool Capabilities::Texture::DXT1 = S3TC_SUPPORT;
+	bool Capabilities::Texture::DXT2 = S3TC_SUPPORT;
+	bool Capabilities::Texture::DXT3 = S3TC_SUPPORT;
+	bool Capabilities::Texture::DXT4 = S3TC_SUPPORT;
+	bool Capabilities::Texture::DXT5 = S3TC_SUPPORT;
+	bool Capabilities::Texture::V8U8 = true;
+	bool Capabilities::Texture::L6V5U5 = true;
+	bool Capabilities::Texture::X8L8V8U8 = true;
+	bool Capabilities::Texture::Q8W8V8U8 = true;
+	bool Capabilities::Texture::V16U16 = true;
+	bool Capabilities::Texture::A2W10V10U10 = true;
+	bool Capabilities::Texture::L8 = true;
+	bool Capabilities::Texture::A4L4 = true;
+	bool Capabilities::Texture::A8L8 = true;
+
+	unsigned int pixelShaderVersion = D3DPS_VERSION(1, 4);
+	unsigned int vertexShaderVersion = D3DVS_VERSION(1, 1);
+
+	unsigned int textureMemory = 256 * 1024 * 1024;
+	unsigned int maxAnisotropy = 16;
+}
diff --git a/src/D3D8/Capabilities.hpp b/src/D3D8/Capabilities.hpp
new file mode 100644
index 0000000..4980e38
--- /dev/null
+++ b/src/D3D8/Capabilities.hpp
@@ -0,0 +1,313 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Capabilities_hpp
+#define D3D8_Capabilities_hpp
+
+#include <d3d8.h>
+
+namespace D3D8
+{
+	struct Capabilities
+	{
+		struct Surface
+		{
+			struct RenderTarget
+			{
+				static bool R8G8B8;
+				static bool R5G6B5;
+				static bool X1R5G5B5;
+				static bool A1R5G5B5;
+				static bool A4R4G4B4;
+				static bool R3G3B2;
+				static bool A8R3G3B2;
+				static bool X4R4G4B4;
+				static bool A8R8G8B8;
+				static bool X8R8G8B8;
+				static bool A8B8G8R8;
+				static bool X8B8G8R8;
+				// Integer HDR formats
+				static bool G16R16;
+				static bool A2B10G10R10;
+			};
+
+			struct DepthStencil
+			{
+				static bool D32;
+				static bool D24S8;
+				static bool D24X8;
+				static bool D16;
+			};
+
+			static bool A8;
+			static bool R5G6B5;
+			static bool X1R5G5B5;
+			static bool A1R5G5B5;
+			static bool A4R4G4B4;
+			static bool R3G3B2;
+			static bool A8R3G3B2;
+			static bool X4R4G4B4;
+			static bool R8G8B8;
+			static bool X8R8G8B8;
+			static bool A8R8G8B8;
+			static bool A8B8G8R8;
+			static bool X8B8G8R8;
+			// Paletted formats
+			static bool P8;
+			static bool A8P8;
+			// Integer HDR formats
+			static bool G16R16;
+			static bool A2B10G10R10;
+			// Compressed formats
+			static bool DXT1;
+			static bool DXT2;
+			static bool DXT3;
+			static bool DXT4;
+			static bool DXT5;
+			// Bump map formats
+			static bool V8U8;
+			static bool L6V5U5;
+			static bool X8L8V8U8;
+			static bool Q8W8V8U8;
+			static bool V16U16;
+			static bool A2W10V10U10;
+			// Luminance formats
+			static bool L8;
+			static bool A4L4;
+			static bool A8L8;
+		};
+
+		struct Volume
+		{
+			static bool A8;
+			static bool R5G6B5;
+			static bool X1R5G5B5;
+			static bool A1R5G5B5;
+			static bool A4R4G4B4;
+			static bool R3G3B2;
+			static bool A8R3G3B2;
+			static bool X4R4G4B4;
+			static bool R8G8B8;
+			static bool X8R8G8B8;
+			static bool A8R8G8B8;
+			static bool A8B8G8R8;
+			static bool X8B8G8R8;
+			// Paletted formats
+			static bool P8;
+			static bool A8P8;
+			// Integer HDR formats
+			static bool G16R16;
+			static bool A2B10G10R10;
+			// Compressed formats
+			static bool DXT1;
+			static bool DXT2;
+			static bool DXT3;
+			static bool DXT4;
+			static bool DXT5;
+			// Bump map formats
+			static bool V8U8;
+			static bool L6V5U5;
+			static bool X8L8V8U8;
+			static bool Q8W8V8U8;
+			static bool V16U16;
+			static bool A2W10V10U10;
+			// Luminance formats
+			static bool L8;
+			static bool A4L4;
+			static bool A8L8;
+		};
+
+		struct CubeMap
+		{
+			struct RenderTarget
+			{
+				static bool R8G8B8;
+				static bool R5G6B5;
+				static bool X1R5G5B5;
+				static bool A1R5G5B5;
+				static bool A4R4G4B4;
+				static bool R3G3B2;
+				static bool A8R3G3B2;
+				static bool X4R4G4B4;
+				static bool A8R8G8B8;
+				static bool X8R8G8B8;
+				static bool A8B8G8R8;
+				static bool X8B8G8R8;
+				// Integer HDR formats
+				static bool G16R16;
+				static bool A2B10G10R10;
+			};
+
+			struct DepthStencil
+			{
+				static bool D32;
+				static bool D24S8;
+				static bool D24X8;
+				static bool D16;
+			};
+
+			static bool A8;
+			static bool R5G6B5;
+			static bool X1R5G5B5;
+			static bool A1R5G5B5;
+			static bool A4R4G4B4;
+			static bool R3G3B2;
+			static bool A8R3G3B2;
+			static bool X4R4G4B4;
+			static bool R8G8B8;
+			static bool X8R8G8B8;
+			static bool A8R8G8B8;
+			static bool A8B8G8R8;
+			static bool X8B8G8R8;
+			// Paletted formats
+			static bool P8;
+			static bool A8P8;
+			// Integer HDR formats
+			static bool G16R16;
+			static bool A2B10G10R10;
+			// Compressed formats
+			static bool DXT1;
+			static bool DXT2;
+			static bool DXT3;
+			static bool DXT4;
+			static bool DXT5;
+			// Bump map formats
+			static bool V8U8;
+			static bool L6V5U5;
+			static bool X8L8V8U8;
+			static bool Q8W8V8U8;
+			static bool V16U16;
+			static bool A2W10V10U10;
+			// Luminance formats
+			static bool L8;
+			static bool A4L4;
+			static bool A8L8;
+		};
+
+		struct VolumeTexture
+		{
+			static bool A8;
+			static bool R5G6B5;
+			static bool X1R5G5B5;
+			static bool A1R5G5B5;
+			static bool A4R4G4B4;
+			static bool R3G3B2;
+			static bool A8R3G3B2;
+			static bool X4R4G4B4;
+			static bool R8G8B8;
+			static bool X8R8G8B8;
+			static bool A8R8G8B8;
+			static bool A8B8G8R8;
+			static bool X8B8G8R8;
+			// Paletted formats
+			static bool P8;
+			static bool A8P8;
+			// Integer HDR formats
+			static bool G16R16;
+			static bool A2B10G10R10;
+			// Compressed formats
+			static bool DXT1;
+			static bool DXT2;
+			static bool DXT3;
+			static bool DXT4;
+			static bool DXT5;
+			// Bump map formats
+			static bool V8U8;
+			static bool L6V5U5;
+			static bool X8L8V8U8;
+			static bool Q8W8V8U8;
+			static bool V16U16;
+			static bool A2W10V10U10;
+			// Luminance formats
+			static bool L8;
+			static bool A4L4;
+			static bool A8L8;
+		};
+
+		struct Texture
+		{
+			struct RenderTarget
+			{
+				static bool R8G8B8;
+				static bool R5G6B5;
+				static bool X1R5G5B5;
+				static bool A1R5G5B5;
+				static bool A4R4G4B4;
+				static bool R3G3B2;
+				static bool A8R3G3B2;
+				static bool X4R4G4B4;
+				static bool A8R8G8B8;
+				static bool X8R8G8B8;
+				static bool A8B8G8R8;
+				static bool X8B8G8R8;
+				// Integer HDR formats
+				static bool G16R16;
+				static bool A2B10G10R10;
+			};
+
+			struct DepthStencil
+			{
+				static bool D32;
+				static bool D24S8;
+				static bool D24X8;
+				static bool D16;
+			};
+
+			static bool A8;
+			static bool R5G6B5;
+			static bool X1R5G5B5;
+			static bool A1R5G5B5;
+			static bool A4R4G4B4;
+			static bool R3G3B2;
+			static bool A8R3G3B2;
+			static bool X4R4G4B4;
+			static bool R8G8B8;
+			static bool X8R8G8B8;
+			static bool A8R8G8B8;
+			static bool A8B8G8R8;
+			static bool X8B8G8R8;
+			// Paletted formats
+			static bool P8;
+			static bool A8P8;
+			// Integer HDR formats
+			static bool G16R16;
+			static bool A2B10G10R10;
+			// Compressed formats
+			static bool DXT1;
+			static bool DXT2;
+			static bool DXT3;
+			static bool DXT4;
+			static bool DXT5;
+			// Bump map formats
+			static bool V8U8;
+			static bool L6V5U5;
+			static bool X8L8V8U8;
+			static bool Q8W8V8U8;
+			static bool V16U16;
+			static bool A2W10V10U10;
+			// Luminance formats
+			static bool L8;
+			static bool A4L4;
+			static bool A8L8;
+		};
+	};
+
+	extern unsigned int pixelShaderVersion;
+	extern unsigned int vertexShaderVersion;
+
+	extern unsigned int textureMemory;
+	extern unsigned int maxAnisotropy;
+}
+
+#endif   // D3D8_Capabilities_hpp
diff --git a/src/D3D8/D3D8.cpp b/src/D3D8/D3D8.cpp
new file mode 100644
index 0000000..307e48b
--- /dev/null
+++ b/src/D3D8/D3D8.cpp
@@ -0,0 +1,100 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3D8.hpp"
+
+#include "resource.h"
+
+#include <stdio.h>
+#include <assert.h>
+
+extern "C"
+{
+	HINSTANCE dllInstance;
+
+	int	__stdcall DllMain(HINSTANCE	instance, unsigned long	reason,	void *reserved)
+	{
+		dllInstance	= instance;
+
+		switch(reason)
+		{
+		case DLL_PROCESS_DETACH:
+			break;
+		case DLL_PROCESS_ATTACH:
+			DisableThreadLibraryCalls(instance);
+			break;
+		case DLL_THREAD_ATTACH:
+			break;
+		case DLL_THREAD_DETACH:
+			break;
+		default:
+			SetLastError(ERROR_INVALID_PARAMETER);
+			return FALSE;
+		}
+
+		return TRUE;
+	}
+
+	IDirect3D8 *__stdcall Direct3DCreate8(unsigned int version)
+	{
+		// D3D_SDK_VERSION check
+		if(version != 120 &&   // 8.0
+		   version != 220)     // 8.1
+		{
+			return 0;
+		}
+		
+		#ifndef NDEBUG
+			FILE *file = fopen("debug.txt", "w");   // Clear debug log
+			fclose(file);
+		#endif
+
+		IDirect3D8 *device = new D3D8::Direct3D8(version, dllInstance);
+
+		if(device)
+		{
+			device->AddRef();
+		}
+
+		return device;
+	}
+
+	int __stdcall CheckFullscreen()   // FIXME: __cdecl or __stdcall?
+	{
+		#ifndef NDEBUG
+		//	ASSERT(false);   // FIXME
+		#endif
+
+		return FALSE;
+	}
+
+    void __cdecl DebugSetMute(long mute)   // FIXME: Return type
+	{
+	//	ASSERT(false);   // FIXME
+	}
+
+    int __stdcall ValidatePixelShader(long *shader, int x, int y, int z)   // FIXME: __cdecl or __stdcall?   // FIXME: Argument meanings
+	{
+	//	ASSERT(false);   // FIXME
+
+		return TRUE;
+	}
+
+    int __stdcall ValidateVertexShader(long *shader, int x, int y, int z)   // FIXME: __cdecl or __stdcall?   // FIXME: Argument meanings
+	{
+	//	ASSERT(false);   // FIXME
+
+		return TRUE;
+	}
+}
diff --git a/src/D3D8/D3D8.rc b/src/D3D8/D3D8.rc
new file mode 100644
index 0000000..fa0a3f7
--- /dev/null
+++ b/src/D3D8/D3D8.rc
@@ -0,0 +1,89 @@
+// Microsoft Visual C++ generated resource script.
+//
+#include "resource1.h"
+
+#define APSTUDIO_READONLY_SYMBOLS
+/////////////////////////////////////////////////////////////////////////////
+//
+// Generated from the TEXTINCLUDE 2 resource.
+//
+#include "Windows.h"
+#include "../Common/Version.h"
+/////////////////////////////////////////////////////////////////////////////
+#undef APSTUDIO_READONLY_SYMBOLS
+
+/////////////////////////////////////////////////////////////////////////////
+// English (United States) resources
+
+#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
+LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
+#pragma code_page(1252)
+
+#ifdef APSTUDIO_INVOKED
+/////////////////////////////////////////////////////////////////////////////
+//
+// TEXTINCLUDE
+//
+
+1 TEXTINCLUDE 
+BEGIN
+    "resource1.h\0"
+END
+
+2 TEXTINCLUDE 
+BEGIN
+    "#include ""afxres.h""\r\n"
+    "#include ""../Common/Version.h""\0"
+END
+
+3 TEXTINCLUDE 
+BEGIN
+    "\0"
+END
+
+#endif    // APSTUDIO_INVOKED
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// Version
+//
+
+VS_VERSION_INFO VERSIONINFO
+ FILEVERSION MAJOR_VERSION,MINOR_VERSION,BUILD_VERSION,BUILD_REVISION
+ PRODUCTVERSION MAJOR_VERSION,MINOR_VERSION,BUILD_VERSION,BUILD_REVISION
+ FILEFLAGSMASK 0x1fL
+#ifdef _DEBUG
+ FILEFLAGS 0x9L
+#else
+ FILEFLAGS 0x8L
+#endif
+ FILEOS 0x4L
+ FILETYPE 0x2L
+ FILESUBTYPE 0x0L
+BEGIN
+    BLOCK "StringFileInfo"
+    BEGIN
+        BLOCK "040904b0"
+        BEGIN
+			VALUE "CompanyName", "Google Inc."
+            VALUE "FileDescription", "SwiftShader Direct3D 8 Renderer"
+            VALUE "FileVersion", VERSION_STRING
+            VALUE "InternalName", "D3D8"
+			VALUE "LegalCopyright", "Copyright (C) 2016 Google Inc."
+            VALUE "OriginalFilename", "d3d8.dll"
+            VALUE "PrivateBuild", REVISION_STRING
+            VALUE "ProductName", "SwiftShader Direct3D 8 Renderer"
+            VALUE "ProductVersion", VERSION_STRING
+        END
+    END
+    BLOCK "VarFileInfo"
+    BEGIN
+        VALUE "Translation", 0x409, 1200
+    END
+END
+
+#endif    // English (United States) resources
+/////////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/D3D8/D3D8.vcxproj b/src/D3D8/D3D8.vcxproj
new file mode 100644
index 0000000..2bb7bc2
--- /dev/null
+++ b/src/D3D8/D3D8.vcxproj
@@ -0,0 +1,282 @@
+<?xml version="1.0" encoding="utf-8"?>

+<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

+  <ItemGroup Label="ProjectConfigurations">

+    <ProjectConfiguration Include="Debug|Win32">

+      <Configuration>Debug</Configuration>

+      <Platform>Win32</Platform>

+    </ProjectConfiguration>

+    <ProjectConfiguration Include="Profile|Win32">

+      <Configuration>Profile</Configuration>

+      <Platform>Win32</Platform>

+    </ProjectConfiguration>

+    <ProjectConfiguration Include="Release|Win32">

+      <Configuration>Release</Configuration>

+      <Platform>Win32</Platform>

+    </ProjectConfiguration>

+  </ItemGroup>

+  <PropertyGroup Label="Globals">

+    <ProjectGuid>{F18D5ABF-CA3A-4B74-BDB2-4A1956C86F18}</ProjectGuid>

+    <Keyword>Win32Proj</Keyword>

+  </PropertyGroup>

+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />

+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">

+    <ConfigurationType>DynamicLibrary</ConfigurationType>

+    <CharacterSet>NotSet</CharacterSet>

+    <WholeProgramOptimization>true</WholeProgramOptimization>

+    <PlatformToolset>v140</PlatformToolset>

+  </PropertyGroup>

+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Profile|Win32'" Label="Configuration">

+    <ConfigurationType>DynamicLibrary</ConfigurationType>

+    <CharacterSet>NotSet</CharacterSet>

+    <WholeProgramOptimization>true</WholeProgramOptimization>

+    <PlatformToolset>v140</PlatformToolset>

+  </PropertyGroup>

+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">

+    <ConfigurationType>DynamicLibrary</ConfigurationType>

+    <CharacterSet>NotSet</CharacterSet>

+    <WholeProgramOptimization>false</WholeProgramOptimization>

+    <PlatformToolset>v140</PlatformToolset>

+  </PropertyGroup>

+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />

+  <ImportGroup Label="ExtensionSettings">

+  </ImportGroup>

+  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">

+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />

+    <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" />

+  </ImportGroup>

+  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Profile|Win32'" Label="PropertySheets">

+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />

+    <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" />

+  </ImportGroup>

+  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">

+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />

+    <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" />

+  </ImportGroup>

+  <PropertyGroup Label="UserMacros" />

+  <PropertyGroup>

+    <_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>

+    <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(Platform)\$(Configuration)\</OutDir>

+    <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(Platform)\$(Configuration)\</IntDir>

+    <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental>

+    <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(Platform)\$(Configuration)\</OutDir>

+    <OutDir Condition="'$(Configuration)|$(Platform)'=='Profile|Win32'">$(Platform)\$(Configuration)\</OutDir>

+    <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(Platform)\$(Configuration)\</IntDir>

+    <IntDir Condition="'$(Configuration)|$(Platform)'=='Profile|Win32'">$(Platform)\$(Configuration)\</IntDir>

+    <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental>

+    <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Profile|Win32'">false</LinkIncremental>

+    <IncludePath Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(SolutionDir)\include\Direct3D;$(DXSDK_DIR)\Include;$(IncludePath)</IncludePath>

+    <IncludePath Condition="'$(Configuration)|$(Platform)'=='Profile|Win32'">$(SolutionDir)\include\Direct3D;$(DXSDK_DIR)\Include;$(IncludePath)</IncludePath>

+    <IncludePath Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(SolutionDir)\include\Direct3D;$(DXSDK_DIR)\Include;$(IncludePath)</IncludePath>

+    <LibraryPath Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(DXSDK_DIR)Lib\x86;$(LibraryPath)</LibraryPath>

+    <LibraryPath Condition="'$(Configuration)|$(Platform)'=='Profile|Win32'">$(DXSDK_DIR)Lib\x86;$(LibraryPath)</LibraryPath>

+    <LibraryPath Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(DXSDK_DIR)Lib\x86;$(LibraryPath)</LibraryPath>

+  </PropertyGroup>

+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">

+    <ClCompile>

+      <Optimization>Disabled</Optimization>

+      <InlineFunctionExpansion>Default</InlineFunctionExpansion>

+      <IntrinsicFunctions>true</IntrinsicFunctions>

+      <FavorSizeOrSpeed>Neither</FavorSizeOrSpeed>

+      <OmitFramePointers>false</OmitFramePointers>

+      <AdditionalIncludeDirectories>..\;..\Main;..\Renderer;..\Shader;..\Common;..\SwiftAsm;..\libjpeg;..\SwiftShader;..\D3D9;..\Reactor;..\LLVM\include;..\LLVM\win32;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>

+      <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;D3D8DLL_EXPORTS;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>

+      <MinimalRebuild>true</MinimalRebuild>

+      <ExceptionHandling>

+      </ExceptionHandling>

+      <BasicRuntimeChecks>Default</BasicRuntimeChecks>

+      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>

+      <BufferSecurityCheck>false</BufferSecurityCheck>

+      <ForceConformanceInForLoopScope>true</ForceConformanceInForLoopScope>

+      <PrecompiledHeader>

+      </PrecompiledHeader>

+      <BrowseInformation>true</BrowseInformation>

+      <WarningLevel>Level3</WarningLevel>

+      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>

+      <ForcedIncludeFiles>%(ForcedIncludeFiles)</ForcedIncludeFiles>

+    </ClCompile>

+    <Link>

+      <AdditionalDependencies>dxguid.lib;WS2_32.lib;comctl32.lib;user32.lib;gdi32.lib;%(AdditionalDependencies)</AdditionalDependencies>

+      <OutputFile>$(OutDir)d3d8.dll</OutputFile>

+      <IgnoreSpecificDefaultLibraries>libci.lib;%(IgnoreSpecificDefaultLibraries)</IgnoreSpecificDefaultLibraries>

+      <ModuleDefinitionFile>d3d8.def</ModuleDefinitionFile>

+      <GenerateDebugInformation>true</GenerateDebugInformation>

+      <AssemblyDebug>true</AssemblyDebug>

+      <ProgramDatabaseFile>

+      </ProgramDatabaseFile>

+      <GenerateMapFile>true</GenerateMapFile>

+      <MapExports>true</MapExports>

+      <SubSystem>Windows</SubSystem>

+      <ImportLibrary>$(OutDir)D3D8.lib</ImportLibrary>

+      <TargetMachine>MachineX86</TargetMachine>

+    </Link>

+    <PostBuildEvent>

+      <Command>mkdir "$(SolutionDir)lib\$(Configuration)_$(Platform)\"

+copy "$(OutDir)d3d8.dll" "$(SolutionDir)lib\$(Configuration)_$(Platform)\"</Command>

+    </PostBuildEvent>

+    <ProjectReference>

+      <LinkLibraryDependencies>false</LinkLibraryDependencies>

+    </ProjectReference>

+  </ItemDefinitionGroup>

+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">

+    <ClCompile>

+      <Optimization>Full</Optimization>

+      <InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>

+      <IntrinsicFunctions>false</IntrinsicFunctions>

+      <FavorSizeOrSpeed>Size</FavorSizeOrSpeed>

+      <OmitFramePointers>true</OmitFramePointers>

+      <WholeProgramOptimization>true</WholeProgramOptimization>

+      <AdditionalIncludeDirectories>..\;..\Main;..\Renderer;..\Shader;..\Common;..\SwiftAsm;..\libjpeg;..\SwiftShader;..\D3D9;..\Reactor;..\LLVM\include;..\LLVM\win32;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>

+      <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;D3D8DLL_EXPORTS;_CRT_SECURE_NO_DEPRECATE;_SECURE_SCL=0;_HAS_EXCEPTIONS=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>

+      <ExceptionHandling>false</ExceptionHandling>

+      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>

+      <BufferSecurityCheck>false</BufferSecurityCheck>

+      <ForceConformanceInForLoopScope>true</ForceConformanceInForLoopScope>

+      <PrecompiledHeader>

+      </PrecompiledHeader>

+      <ExpandAttributedSource>false</ExpandAttributedSource>

+      <WarningLevel>Level3</WarningLevel>

+      <DebugInformationFormat>

+      </DebugInformationFormat>

+      <ForcedIncludeFiles>%(ForcedIncludeFiles)</ForcedIncludeFiles>

+      <EnableFiberSafeOptimizations>true</EnableFiberSafeOptimizations>

+      <StringPooling>true</StringPooling>

+      <FloatingPointExceptions>false</FloatingPointExceptions>

+    </ClCompile>

+    <Link>

+      <AdditionalDependencies>dxguid.lib;WS2_32.lib;comctl32.lib;user32.lib;gdi32.lib;%(AdditionalDependencies)</AdditionalDependencies>

+      <OutputFile>$(OutDir)d3d8.dll</OutputFile>

+      <IgnoreSpecificDefaultLibraries>libci.lib;%(IgnoreSpecificDefaultLibraries)</IgnoreSpecificDefaultLibraries>

+      <ModuleDefinitionFile>d3d8.def</ModuleDefinitionFile>

+      <GenerateDebugInformation>true</GenerateDebugInformation>

+      <SubSystem>Windows</SubSystem>

+      <OptimizeReferences>true</OptimizeReferences>

+      <EnableCOMDATFolding>true</EnableCOMDATFolding>

+      <SetChecksum>true</SetChecksum>

+      <ImportLibrary>$(OutDir)D3D8.lib</ImportLibrary>

+      <TargetMachine>MachineX86</TargetMachine>

+      <LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>

+    </Link>

+    <PostBuildEvent>

+      <Command>mkdir "$(SolutionDir)lib\$(Configuration)_$(Platform)\"

+copy "$(OutDir)d3d8.dll" "$(SolutionDir)lib\$(Configuration)_$(Platform)\"</Command>

+    </PostBuildEvent>

+    <ProjectReference>

+      <LinkLibraryDependencies>false</LinkLibraryDependencies>

+    </ProjectReference>

+  </ItemDefinitionGroup>

+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Profile|Win32'">

+    <ClCompile>

+      <Optimization>Full</Optimization>

+      <InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>

+      <IntrinsicFunctions>false</IntrinsicFunctions>

+      <FavorSizeOrSpeed>Size</FavorSizeOrSpeed>

+      <OmitFramePointers>false</OmitFramePointers>

+      <WholeProgramOptimization>false</WholeProgramOptimization>

+      <AdditionalIncludeDirectories>..\;..\Main;..\Renderer;..\Shader;..\Common;..\SwiftAsm;..\libjpeg;..\SwiftShader;..\D3D9;..\Reactor;..\LLVM\include;..\LLVM\win32;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>

+      <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;D3D8DLL_EXPORTS;_CRT_SECURE_NO_DEPRECATE;_SECURE_SCL=0;_HAS_EXCEPTIONS=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>

+      <ExceptionHandling>false</ExceptionHandling>

+      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>

+      <BufferSecurityCheck>false</BufferSecurityCheck>

+      <ForceConformanceInForLoopScope>true</ForceConformanceInForLoopScope>

+      <PrecompiledHeader>

+      </PrecompiledHeader>

+      <ExpandAttributedSource>false</ExpandAttributedSource>

+      <WarningLevel>Level3</WarningLevel>

+      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>

+      <ForcedIncludeFiles>%(ForcedIncludeFiles)</ForcedIncludeFiles>

+      <EnableFiberSafeOptimizations>true</EnableFiberSafeOptimizations>

+      <StringPooling>true</StringPooling>

+      <FloatingPointExceptions>false</FloatingPointExceptions>

+    </ClCompile>

+    <Link>

+      <AdditionalDependencies>dxguid.lib;WS2_32.lib;comctl32.lib;user32.lib;gdi32.lib;%(AdditionalDependencies)</AdditionalDependencies>

+      <OutputFile>$(OutDir)d3d8.dll</OutputFile>

+      <IgnoreSpecificDefaultLibraries>libci.lib;%(IgnoreSpecificDefaultLibraries)</IgnoreSpecificDefaultLibraries>

+      <ModuleDefinitionFile>d3d8.def</ModuleDefinitionFile>

+      <GenerateDebugInformation>true</GenerateDebugInformation>

+      <SubSystem>Windows</SubSystem>

+      <OptimizeReferences>true</OptimizeReferences>

+      <EnableCOMDATFolding>true</EnableCOMDATFolding>

+      <SetChecksum>true</SetChecksum>

+      <ImportLibrary>$(OutDir)D3D8.lib</ImportLibrary>

+      <TargetMachine>MachineX86</TargetMachine>

+      <LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>

+    </Link>

+    <PostBuildEvent>

+      <Command>mkdir "$(SolutionDir)lib\$(Configuration)_$(Platform)\"

+copy "$(OutDir)d3d8.dll" "$(SolutionDir)lib\$(Configuration)_$(Platform)\"</Command>

+    </PostBuildEvent>

+    <ProjectReference>

+      <LinkLibraryDependencies>false</LinkLibraryDependencies>

+    </ProjectReference>

+  </ItemDefinitionGroup>

+  <ItemGroup>

+    <ClCompile Include="Capabilities.cpp" />

+    <ClCompile Include="D3D8.cpp">

+      <AssemblerOutput Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">AssemblyAndSourceCode</AssemblerOutput>

+      <AssemblerOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">AssemblyAndMachineCode</AssemblerOutput>

+      <AssemblerOutput Condition="'$(Configuration)|$(Platform)'=='Profile|Win32'">AssemblyAndMachineCode</AssemblerOutput>

+      <ShowIncludes Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ShowIncludes>

+      <ShowIncludes Condition="'$(Configuration)|$(Platform)'=='Profile|Win32'">false</ShowIncludes>

+    </ClCompile>

+    <ClCompile Include="Direct3D8.cpp" />

+    <ClCompile Include="Direct3DBaseTexture8.cpp" />

+    <ClCompile Include="Direct3DCubeTexture8.cpp" />

+    <ClCompile Include="Direct3DDevice8.cpp" />

+    <ClCompile Include="Direct3DIndexBuffer8.cpp" />

+    <ClCompile Include="Direct3DPixelShader8.cpp" />

+    <ClCompile Include="Direct3DResource8.cpp" />

+    <ClCompile Include="Direct3DStateBlock8.cpp" />

+    <ClCompile Include="Direct3DSurface8.cpp" />

+    <ClCompile Include="Direct3DSwapChain8.cpp" />

+    <ClCompile Include="Direct3DTexture8.cpp" />

+    <ClCompile Include="Direct3DVertexBuffer8.cpp" />

+    <ClCompile Include="Direct3DVertexDeclaration8.cpp" />

+    <ClCompile Include="Direct3DVertexShader8.cpp" />

+    <ClCompile Include="Direct3DVolume8.cpp" />

+    <ClCompile Include="Direct3DVolumeTexture8.cpp" />

+    <ClCompile Include="Unknown.cpp" />

+  </ItemGroup>

+  <ItemGroup>

+    <ClInclude Include="Capabilities.hpp" />

+    <ClInclude Include="Debug.hpp" />

+    <ClInclude Include="Direct3D8.hpp" />

+    <ClInclude Include="Direct3DBaseTexture8.hpp" />

+    <ClInclude Include="Direct3DCubeTexture8.hpp" />

+    <ClInclude Include="Direct3DDevice8.hpp" />

+    <ClInclude Include="Direct3DIndexBuffer8.hpp" />

+    <ClInclude Include="Direct3DPixelShader8.hpp" />

+    <ClInclude Include="Direct3DResource8.hpp" />

+    <ClInclude Include="Direct3DStateBlock8.hpp" />

+    <ClInclude Include="Direct3DSurface8.hpp" />

+    <ClInclude Include="Direct3DSwapChain8.hpp" />

+    <ClInclude Include="Direct3DTexture8.hpp" />

+    <ClInclude Include="Direct3DVertexBuffer8.hpp" />

+    <ClInclude Include="Direct3DVertexDeclaration8.hpp" />

+    <ClInclude Include="Direct3DVertexShader8.hpp" />

+    <ClInclude Include="Direct3DVolume8.hpp" />

+    <ClInclude Include="Direct3DVolumeTexture8.hpp" />

+    <ClInclude Include="resource.h" />

+    <ClInclude Include="resource1.h" />

+    <ClInclude Include="Unknown.hpp" />

+  </ItemGroup>

+  <ItemGroup>

+    <ResourceCompile Include="D3D8.rc" />

+  </ItemGroup>

+  <ItemGroup>

+    <None Include="d3d8.def" />

+  </ItemGroup>

+  <ItemGroup>

+    <ProjectReference Include="..\SwiftShader\SwiftShader.vcxproj">

+      <Project>{7b02cb19-4cdf-4f79-bc9b-7f3f6164a003}</Project>

+      <Private>false</Private>

+      <ReferenceOutputAssembly>false</ReferenceOutputAssembly>

+      <CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>

+      <LinkLibraryDependencies>true</LinkLibraryDependencies>

+      <UseLibraryDependencyInputs>false</UseLibraryDependencyInputs>

+    </ProjectReference>

+  </ItemGroup>

+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

+  <ImportGroup Label="ExtensionTargets">

+  </ImportGroup>

+</Project>
\ No newline at end of file
diff --git a/src/D3D8/D3D8.vcxproj.filters b/src/D3D8/D3D8.vcxproj.filters
new file mode 100644
index 0000000..5348b4d
--- /dev/null
+++ b/src/D3D8/D3D8.vcxproj.filters
@@ -0,0 +1,149 @@
+<?xml version="1.0" encoding="utf-8"?>

+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

+  <ItemGroup>

+    <Filter Include="Source Files">

+      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>

+      <Extensions>cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>

+    </Filter>

+    <Filter Include="Header Files">

+      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>

+      <Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>

+    </Filter>

+    <Filter Include="Resource Files">

+      <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>

+      <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx</Extensions>

+    </Filter>

+  </ItemGroup>

+  <ItemGroup>

+    <ClCompile Include="Capabilities.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="D3D8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3D8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DBaseTexture8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DCubeTexture8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DDevice8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DIndexBuffer8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DPixelShader8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DResource8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DStateBlock8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DSurface8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DSwapChain8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DTexture8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DVertexBuffer8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DVertexDeclaration8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DVertexShader8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DVolume8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Direct3DVolumeTexture8.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="Unknown.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+  </ItemGroup>

+  <ItemGroup>

+    <ClInclude Include="Capabilities.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Debug.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3D8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DBaseTexture8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DCubeTexture8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DDevice8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DIndexBuffer8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DPixelShader8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DResource8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DStateBlock8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DSurface8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DSwapChain8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DTexture8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DVertexBuffer8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DVertexDeclaration8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DVertexShader8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DVolume8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Direct3DVolumeTexture8.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="resource.h">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="resource1.h">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="Unknown.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+  </ItemGroup>

+  <ItemGroup>

+    <ResourceCompile Include="D3D8.rc">

+      <Filter>Resource Files</Filter>

+    </ResourceCompile>

+  </ItemGroup>

+  <ItemGroup>

+    <None Include="d3d8.def" />

+  </ItemGroup>

+</Project>
\ No newline at end of file
diff --git a/src/D3D8/Debug.hpp b/src/D3D8/Debug.hpp
new file mode 100644
index 0000000..fa216b8
--- /dev/null
+++ b/src/D3D8/Debug.hpp
@@ -0,0 +1,126 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef Debug_hpp
+#define Debug_hpp
+
+#ifndef WIN32_LEAN_AND_MEAN
+	#define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+#include <d3d8.h>
+#include <stdio.h>
+#include <guiddef.h>
+#include <assert.h>
+
+void trace(const char *format, ...);
+
+#ifndef NDEBUG
+	#define TRACE(format, ...) trace("[0x%0.8X]%s("format")\n", this, __FUNCTION__, ##__VA_ARGS__)
+#else
+	#define TRACE(...) ((void)0)
+#endif
+
+#ifndef NDEBUG
+	#define ASSERT(expression) {if(!(expression)) trace("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); assert(expression);}
+#else
+	#define ASSERT assert
+#endif
+
+#ifndef NDEBUG
+	#define UNIMPLEMENTED() {trace("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__); ASSERT(false);}
+#else
+	#define UNIMPLEMENTED() ((void)0)
+#endif
+
+#ifndef NDEBUG
+	#define NOINTERFACE(iid) _NOINTERFACE(__FUNCTION__, iid)
+
+	inline long _NOINTERFACE(const char *function, const IID &iid)
+	{
+		trace("\t! No interface {0x%0.8X, 0x%0.4X, 0x%0.4X, 0x%0.2X, 0x%0.2X, 0x%0.2X, 0x%0.2X, 0x%0.2X, 0x%0.2X, 0x%0.2X, 0x%0.2X} for %s\n", iid.Data1, iid.Data2, iid.Data3, iid.Data4[0], iid.Data4[1], iid.Data4[2], iid.Data4[3], iid.Data4[4], iid.Data4[5], iid.Data4[6], iid.Data4[7], function);
+
+		return  E_NOINTERFACE;
+	}
+#else
+	#define NOINTERFACE(iid) E_NOINTERFACE
+#endif
+
+#ifndef NDEBUG
+	inline long INVALIDCALL()
+	{
+		trace("\t! D3DERR_INVALIDCALL\n");
+
+		return D3DERR_INVALIDCALL;
+	}
+#else
+	#define INVALIDCALL() D3DERR_INVALIDCALL
+#endif
+
+#ifndef NDEBUG
+	inline long OUTOFMEMORY()
+	{
+		trace("\t! E_OUTOFMEMORY\n");
+
+		return E_OUTOFMEMORY;
+	}
+#else
+	#define OUTOFMEMORY() E_OUTOFMEMORY
+#endif
+
+#ifndef NDEBUG
+	inline long OUTOFVIDEOMEMORY()
+	{
+		trace("\t! D3DERR_OUTOFVIDEOMEMORY\n");
+
+		return D3DERR_OUTOFVIDEOMEMORY;
+	}
+#else
+	#define OUTOFVIDEOMEMORY() D3DERR_OUTOFVIDEOMEMORY
+#endif
+
+#ifndef NDEBUG
+	inline long NOTAVAILABLE()
+	{
+		trace("\t! D3DERR_NOTAVAILABLE\n");
+
+		return D3DERR_NOTAVAILABLE;
+	}
+#else
+	#define NOTAVAILABLE() D3DERR_NOTAVAILABLE
+#endif
+
+#ifndef NDEBUG
+	inline long NOTFOUND()
+	{
+		trace("\t! D3DERR_NOTFOUND\n");
+
+		return D3DERR_NOTFOUND;
+	}
+#else
+	#define NOTFOUND() D3DERR_NOTFOUND
+#endif
+
+#ifndef NDEBUG
+	inline long MOREDATA()
+	{
+		trace("\t! D3DERR_MOREDATA\n");
+
+		return D3DERR_MOREDATA;
+	}
+#else
+	#define MOREDATA() D3DERR_MOREDATA
+#endif
+
+#endif   // Debug_hpp
diff --git a/src/D3D8/Direct3D8.cpp b/src/D3D8/Direct3D8.cpp
new file mode 100644
index 0000000..f4ce020
--- /dev/null
+++ b/src/D3D8/Direct3D8.cpp
@@ -0,0 +1,1074 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3D8.hpp"
+
+#include "Direct3DDevice8.hpp"
+#include "Capabilities.hpp"
+#include "Configurator.hpp"
+#include "Debug.hpp"
+#include "CPUID.hpp"
+#include "Version.h"
+#include "Config.hpp"
+
+#define COMPILE_MULTIMON_STUBS
+#include <multimon.h>
+#include <assert.h>
+
+namespace D3D8
+{
+	Direct3D8::Direct3D8(int version, const HINSTANCE instance) : version(version), instance(instance)
+	{
+		displayMode = 0;
+		numDisplayModes = 0;
+
+		DEVMODE devmode;
+		devmode.dmSize = sizeof(DEVMODE);
+
+		// Count number of display modes
+		while(EnumDisplaySettings(0, numDisplayModes, &devmode))
+		{
+			numDisplayModes++;
+		}
+
+		displayMode = new DEVMODE[numDisplayModes];
+
+		// Store display modes information
+		for(int i = 0; i < numDisplayModes; i++)
+		{
+			displayMode[i].dmSize = sizeof(DEVMODE);
+			EnumDisplaySettings(0, i, &displayMode[i]);
+		}
+
+		d3d8Lib = 0;
+		d3d8 = 0;
+
+		sw::Configurator ini("SwiftShader.ini");
+
+		int ps = ini.getInteger("Capabilities", "PixelShaderVersion", 14);
+		int vs = ini.getInteger("Capabilities", "VertexShaderVersion", 11);
+
+		     if(ps ==  0) pixelShaderVersion = D3DPS_VERSION(0, 0);
+		else if(ps <= 11) pixelShaderVersion = D3DPS_VERSION(1, 1);
+		else if(ps <= 12) pixelShaderVersion = D3DPS_VERSION(1, 2);
+		else if(ps <= 13) pixelShaderVersion = D3DPS_VERSION(1, 3);
+		else              pixelShaderVersion = D3DPS_VERSION(1, 4);
+
+		     if(vs ==  0) vertexShaderVersion = D3DVS_VERSION(0, 0);
+		else              vertexShaderVersion = D3DVS_VERSION(1, 1);
+
+		textureMemory = 1024 * 1024 * ini.getInteger("Capabilities", "TextureMemory", 256);
+	}
+
+	Direct3D8::~Direct3D8()
+	{
+		delete[] displayMode;
+		displayMode = 0;
+
+		if(d3d8)
+		{
+			d3d8->Release();
+			d3d8 = 0;
+		}
+
+		if(d3d8Lib)
+		{
+			FreeLibrary(d3d8Lib);
+			d3d8Lib = 0;
+		}
+	}
+
+	long Direct3D8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		if(iid == IID_IDirect3D8 ||
+		   iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3D8::AddRef()
+	{
+		TRACE("");
+
+		return Unknown::AddRef();
+	}
+
+	unsigned long Direct3D8::Release()
+	{
+		TRACE("");
+
+		return Unknown::Release();
+	}
+
+	long Direct3D8::CheckDepthStencilMatch(unsigned int adapter, D3DDEVTYPE deviceType, D3DFORMAT adapterFormat, D3DFORMAT renderTargetFormat, D3DFORMAT depthStencilFormat)
+	{
+		TRACE("");
+
+		if(deviceType != D3DDEVTYPE_HAL)
+		{
+			loadSystemD3D8();
+
+			if(d3d8)
+			{
+				return d3d8->CheckDepthStencilMatch(adapter, deviceType, adapterFormat, renderTargetFormat, depthStencilFormat);
+			}
+			else
+			{
+				return CheckDepthStencilMatch(adapter, D3DDEVTYPE_HAL, adapterFormat, renderTargetFormat, depthStencilFormat);
+			}
+		}
+
+		return D3D_OK;   // Any format combination is ok
+	}
+
+	long Direct3D8::CheckDeviceFormat(unsigned int adapter, D3DDEVTYPE deviceType, D3DFORMAT adapterFormat, unsigned long usage, D3DRESOURCETYPE resourceType, D3DFORMAT checkFormat)
+	{
+		TRACE("unsigned int adapter = %d, D3DDEVTYPE deviceType = %d, D3DFORMAT adapterFormat = %d, unsigned long usage = %d, D3DRESOURCETYPE resourceType = %d, D3DFORMAT checkFormat = %d", adapter, deviceType, adapterFormat, usage, resourceType, checkFormat);
+
+		if(deviceType != D3DDEVTYPE_HAL)
+		{
+			loadSystemD3D8();
+
+			if(d3d8)
+			{
+				return d3d8->CheckDeviceFormat(adapter, deviceType, adapterFormat, usage, resourceType, checkFormat);
+			}
+			else
+			{
+				return CheckDeviceFormat(adapter, D3DDEVTYPE_HAL, adapterFormat, usage, resourceType, checkFormat);
+			}
+		}
+
+		switch(resourceType)
+		{
+		case D3DRTYPE_SURFACE:
+			if(usage & D3DUSAGE_RENDERTARGET)
+			{
+				switch(checkFormat)
+				{
+				case D3DFMT_R8G8B8:			if(!Capabilities::Surface::RenderTarget::R8G8B8)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R5G6B5:			if(!Capabilities::Surface::RenderTarget::R5G6B5)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X1R5G5B5:		if(!Capabilities::Surface::RenderTarget::X1R5G5B5)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A1R5G5B5:		if(!Capabilities::Surface::RenderTarget::A1R5G5B5)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A4R4G4B4:		if(!Capabilities::Surface::RenderTarget::A4R4G4B4)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R3G3B2:			if(!Capabilities::Surface::RenderTarget::R3G3B2)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8R3G3B2:		if(!Capabilities::Surface::RenderTarget::A8R3G3B2)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X4R4G4B4:		if(!Capabilities::Surface::RenderTarget::X4R4G4B4)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8R8G8B8:		if(!Capabilities::Surface::RenderTarget::A8R8G8B8)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X8R8G8B8:		if(!Capabilities::Surface::RenderTarget::X8R8G8B8)		return NOTAVAILABLE();	else return D3D_OK;
+				// Integer HDR formats
+				case D3DFMT_G16R16:			if(!Capabilities::Surface::RenderTarget::G16R16)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A2B10G10R10:	if(!Capabilities::Surface::RenderTarget::A2B10G10R10)	return NOTAVAILABLE();	else return D3D_OK;
+				default:
+					return NOTAVAILABLE();
+				}
+			}
+			else if(usage & D3DUSAGE_DEPTHSTENCIL)
+			{
+				switch(checkFormat)
+				{
+				case D3DFMT_D32:			if(!Capabilities::Surface::DepthStencil::D32)			return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_D24S8:			if(!Capabilities::Surface::DepthStencil::D24S8)			return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_D24X8:			if(!Capabilities::Surface::DepthStencil::D24X8)			return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_D16:			if(!Capabilities::Surface::DepthStencil::D16)			return NOTAVAILABLE();	else return D3D_OK;
+				default:
+					return NOTAVAILABLE();
+				}
+			}
+			else
+			{
+				switch(checkFormat)
+				{
+				case D3DFMT_A8:				if(!Capabilities::Surface::A8)							return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R5G6B5:			if(!Capabilities::Surface::R5G6B5)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X1R5G5B5:		if(!Capabilities::Surface::X1R5G5B5)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A1R5G5B5:		if(!Capabilities::Surface::A1R5G5B5)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A4R4G4B4:		if(!Capabilities::Surface::A4R4G4B4)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R3G3B2:			if(!Capabilities::Surface::R3G3B2)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8R3G3B2:		if(!Capabilities::Surface::A8R3G3B2)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X4R4G4B4:		if(!Capabilities::Surface::X4R4G4B4)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R8G8B8:			if(!Capabilities::Surface::R8G8B8)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X8R8G8B8:		if(!Capabilities::Surface::X8R8G8B8)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8R8G8B8:		if(!Capabilities::Surface::A8R8G8B8)					return NOTAVAILABLE();	else return D3D_OK;
+				// Paletted formats
+				case D3DFMT_P8:				if(!Capabilities::Surface::P8)							return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8P8:			if(!Capabilities::Surface::A8P8)						return NOTAVAILABLE();	else return D3D_OK;
+				// Integer HDR formats
+				case D3DFMT_G16R16:			if(!Capabilities::Surface::G16R16)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A2B10G10R10:	if(!Capabilities::Surface::A2B10G10R10)					return NOTAVAILABLE();	else return D3D_OK;
+				// Compressed formats
+				case D3DFMT_DXT1:			if(!Capabilities::Surface::DXT1)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_DXT2:			if(!Capabilities::Surface::DXT2)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_DXT3:			if(!Capabilities::Surface::DXT3)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_DXT4:			if(!Capabilities::Surface::DXT4)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_DXT5:			if(!Capabilities::Surface::DXT5)						return NOTAVAILABLE();	else return D3D_OK;
+				// Bump map formats
+				case D3DFMT_V8U8:			if(!Capabilities::Surface::V8U8)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_L6V5U5:			if(!Capabilities::Surface::L6V5U5)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X8L8V8U8:		if(!Capabilities::Surface::X8L8V8U8)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_Q8W8V8U8:		if(!Capabilities::Surface::Q8W8V8U8)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_V16U16:			if(!Capabilities::Surface::V16U16)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A2W10V10U10:	if(!Capabilities::Surface::A2W10V10U10)					return NOTAVAILABLE();	else return D3D_OK;
+				// Luminance formats
+				case D3DFMT_L8:				if(!Capabilities::Surface::L8)							return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A4L4:			if(!Capabilities::Surface::A4L4)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8L8:			if(!Capabilities::Surface::A8L8)						return NOTAVAILABLE();	else return D3D_OK;
+				default:
+					return NOTAVAILABLE();
+				}
+			}
+		case D3DRTYPE_VOLUME:
+			switch(checkFormat)
+			{
+			case D3DFMT_A8:					if(!Capabilities::Volume::A8)							return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_R5G6B5:				if(!Capabilities::Volume::R5G6B5)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_X1R5G5B5:			if(!Capabilities::Volume::X1R5G5B5)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A1R5G5B5:			if(!Capabilities::Volume::A1R5G5B5)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A4R4G4B4:			if(!Capabilities::Volume::A4R4G4B4)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_R3G3B2:				if(!Capabilities::Volume::R3G3B2)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A8R3G3B2:			if(!Capabilities::Volume::A8R3G3B2)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_X4R4G4B4:			if(!Capabilities::Volume::X4R4G4B4)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_R8G8B8:				if(!Capabilities::Volume::R8G8B8)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_X8R8G8B8:			if(!Capabilities::Volume::X8R8G8B8)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A8R8G8B8:			if(!Capabilities::Volume::A8R8G8B8)						return NOTAVAILABLE();	else return D3D_OK;
+			// Paletted formats
+			case D3DFMT_P8:					if(!Capabilities::Volume::P8)							return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A8P8:				if(!Capabilities::Volume::A8P8)							return NOTAVAILABLE();	else return D3D_OK;
+			// Integer HDR formats
+			case D3DFMT_G16R16:				if(!Capabilities::Volume::G16R16)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A2B10G10R10:		if(!Capabilities::Volume::A2B10G10R10)					return NOTAVAILABLE();	else return D3D_OK;
+			// Compressed formats
+			case D3DFMT_DXT1:				if(!Capabilities::Volume::DXT1)							return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_DXT2:				if(!Capabilities::Volume::DXT2)							return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_DXT3:				if(!Capabilities::Volume::DXT3)							return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_DXT4:				if(!Capabilities::Volume::DXT4)							return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_DXT5:				if(!Capabilities::Volume::DXT5)							return NOTAVAILABLE();	else return D3D_OK;
+			// Bump map formats
+			case D3DFMT_V8U8:				if(!Capabilities::Volume::V8U8)							return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_L6V5U5:				if(!Capabilities::Volume::L6V5U5)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_X8L8V8U8:			if(!Capabilities::Volume::X8L8V8U8)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_Q8W8V8U8:			if(!Capabilities::Volume::Q8W8V8U8)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_V16U16:				if(!Capabilities::Volume::V16U16)						return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A2W10V10U10:		if(!Capabilities::Volume::A2W10V10U10)					return NOTAVAILABLE();	else return D3D_OK;
+			// Luminance formats
+			case D3DFMT_L8:					if(!Capabilities::Volume::L8)							return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A4L4:				if(!Capabilities::Volume::A4L4)							return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A8L8:				if(!Capabilities::Volume::A8L8)							return NOTAVAILABLE();	else return D3D_OK;
+			default:
+				return NOTAVAILABLE();
+			}
+		case D3DRTYPE_CUBETEXTURE:
+			if(usage & D3DUSAGE_RENDERTARGET)
+			{
+				switch(checkFormat)
+				{
+				case D3DFMT_R8G8B8:			if(!Capabilities::CubeMap::RenderTarget::R8G8B8)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R5G6B5:			if(!Capabilities::CubeMap::RenderTarget::R5G6B5)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X1R5G5B5:		if(!Capabilities::CubeMap::RenderTarget::X1R5G5B5)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A1R5G5B5:		if(!Capabilities::CubeMap::RenderTarget::A1R5G5B5)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A4R4G4B4:		if(!Capabilities::CubeMap::RenderTarget::A4R4G4B4)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R3G3B2:			if(!Capabilities::CubeMap::RenderTarget::R3G3B2)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8R3G3B2:		if(!Capabilities::CubeMap::RenderTarget::A8R3G3B2)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X4R4G4B4:		if(!Capabilities::CubeMap::RenderTarget::X4R4G4B4)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8R8G8B8:		if(!Capabilities::CubeMap::RenderTarget::A8R8G8B8)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X8R8G8B8:		if(!Capabilities::CubeMap::RenderTarget::X8R8G8B8)		return NOTAVAILABLE();	else return D3D_OK;
+				// Integer HDR formats
+				case D3DFMT_G16R16:			if(!Capabilities::CubeMap::RenderTarget::G16R16)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A2B10G10R10:	if(!Capabilities::CubeMap::RenderTarget::A2B10G10R10)	return NOTAVAILABLE();	else return D3D_OK;
+				default:
+					return NOTAVAILABLE();
+				}
+			}
+			else if(usage & D3DUSAGE_DEPTHSTENCIL)
+			{
+				switch(checkFormat)
+				{
+				case D3DFMT_D32:			if(!Capabilities::CubeMap::DepthStencil::D32)			return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_D24S8:			if(!Capabilities::CubeMap::DepthStencil::D24S8)			return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_D24X8:			if(!Capabilities::CubeMap::DepthStencil::D24X8)			return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_D16:			if(!Capabilities::CubeMap::DepthStencil::D16)			return NOTAVAILABLE();	else return D3D_OK;
+				default:
+					return NOTAVAILABLE();
+				}
+			}
+			else
+			{
+				switch(checkFormat)
+				{
+				case D3DFMT_A8:				if(!Capabilities::CubeMap::A8)							return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R5G6B5:			if(!Capabilities::CubeMap::R5G6B5)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X1R5G5B5:		if(!Capabilities::CubeMap::X1R5G5B5)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A1R5G5B5:		if(!Capabilities::CubeMap::A1R5G5B5)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A4R4G4B4:		if(!Capabilities::CubeMap::A4R4G4B4)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R3G3B2:			if(!Capabilities::CubeMap::R3G3B2)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8R3G3B2:		if(!Capabilities::CubeMap::A8R3G3B2)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X4R4G4B4:		if(!Capabilities::CubeMap::X4R4G4B4)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R8G8B8:			if(!Capabilities::CubeMap::R8G8B8)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X8R8G8B8:		if(!Capabilities::CubeMap::X8R8G8B8)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8R8G8B8:		if(!Capabilities::CubeMap::A8R8G8B8)					return NOTAVAILABLE();	else return D3D_OK;
+				// Paletted formats
+				case D3DFMT_P8:				if(!Capabilities::CubeMap::P8)							return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8P8:			if(!Capabilities::CubeMap::A8P8)						return NOTAVAILABLE();	else return D3D_OK;
+				// Integer HDR formats
+				case D3DFMT_G16R16:			if(!Capabilities::CubeMap::G16R16)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A2B10G10R10:	if(!Capabilities::CubeMap::A2B10G10R10)					return NOTAVAILABLE();	else return D3D_OK;
+				// Compressed formats
+				case D3DFMT_DXT1:			if(!Capabilities::CubeMap::DXT1)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_DXT2:			if(!Capabilities::CubeMap::DXT2)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_DXT3:			if(!Capabilities::CubeMap::DXT3)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_DXT4:			if(!Capabilities::CubeMap::DXT4)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_DXT5:			if(!Capabilities::CubeMap::DXT5)						return NOTAVAILABLE();	else return D3D_OK;
+				// Bump map formats
+				case D3DFMT_V8U8:			if(!Capabilities::CubeMap::V8U8)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_L6V5U5:			if(!Capabilities::CubeMap::L6V5U5)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X8L8V8U8:		if(!Capabilities::CubeMap::X8L8V8U8)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_Q8W8V8U8:		if(!Capabilities::CubeMap::Q8W8V8U8)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_V16U16:			if(!Capabilities::CubeMap::V16U16)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A2W10V10U10:	if(!Capabilities::CubeMap::A2W10V10U10)					return NOTAVAILABLE();	else return D3D_OK;
+				// Luminance formats
+				case D3DFMT_L8:				if(!Capabilities::CubeMap::L8)							return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A4L4:			if(!Capabilities::CubeMap::A4L4)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8L8:			if(!Capabilities::CubeMap::A8L8)						return NOTAVAILABLE();	else return D3D_OK;
+				default:
+					return NOTAVAILABLE();
+				}
+			}
+		case D3DRTYPE_VOLUMETEXTURE:
+			switch(checkFormat)
+			{
+			case D3DFMT_A8:					if(!Capabilities::VolumeTexture::A8)					return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_R5G6B5:				if(!Capabilities::VolumeTexture::R5G6B5)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_X1R5G5B5:			if(!Capabilities::VolumeTexture::X1R5G5B5)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A1R5G5B5:			if(!Capabilities::VolumeTexture::A1R5G5B5)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A4R4G4B4:			if(!Capabilities::VolumeTexture::A4R4G4B4)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_R3G3B2:				if(!Capabilities::VolumeTexture::R3G3B2)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A8R3G3B2:			if(!Capabilities::VolumeTexture::A8R3G3B2)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_X4R4G4B4:			if(!Capabilities::VolumeTexture::X4R4G4B4)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_R8G8B8:				if(!Capabilities::VolumeTexture::R8G8B8)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_X8R8G8B8:			if(!Capabilities::VolumeTexture::X8R8G8B8)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A8R8G8B8:			if(!Capabilities::VolumeTexture::A8R8G8B8)				return NOTAVAILABLE();	else return D3D_OK;
+			// Paletted formats
+			case D3DFMT_P8:					if(!Capabilities::VolumeTexture::P8)					return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A8P8:				if(!Capabilities::VolumeTexture::A8P8)					return NOTAVAILABLE();	else return D3D_OK;
+			// Integer HDR formats
+			case D3DFMT_G16R16:				if(!Capabilities::VolumeTexture::G16R16)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A2B10G10R10:		if(!Capabilities::VolumeTexture::A2B10G10R10)			return NOTAVAILABLE();	else return D3D_OK;
+			// Compressed formats
+			case D3DFMT_DXT1:				if(!Capabilities::VolumeTexture::DXT1)					return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_DXT2:				if(!Capabilities::VolumeTexture::DXT2)					return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_DXT3:				if(!Capabilities::VolumeTexture::DXT3)					return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_DXT4:				if(!Capabilities::VolumeTexture::DXT4)					return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_DXT5:				if(!Capabilities::VolumeTexture::DXT5)					return NOTAVAILABLE();	else return D3D_OK;
+			// Bump map formats
+			case D3DFMT_V8U8:				if(!Capabilities::VolumeTexture::V8U8)					return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_L6V5U5:				if(!Capabilities::VolumeTexture::L6V5U5)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_X8L8V8U8:			if(!Capabilities::VolumeTexture::X8L8V8U8)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_Q8W8V8U8:			if(!Capabilities::VolumeTexture::Q8W8V8U8)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_V16U16:				if(!Capabilities::VolumeTexture::V16U16)				return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A2W10V10U10:		if(!Capabilities::VolumeTexture::A2W10V10U10)			return NOTAVAILABLE();	else return D3D_OK;
+			// Luminance formats
+			case D3DFMT_L8:					if(!Capabilities::VolumeTexture::L8)					return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A4L4:				if(!Capabilities::VolumeTexture::A4L4)					return NOTAVAILABLE();	else return D3D_OK;
+			case D3DFMT_A8L8:				if(!Capabilities::VolumeTexture::A8L8)					return NOTAVAILABLE();	else return D3D_OK;
+			default:
+				return NOTAVAILABLE();
+			}
+		case D3DRTYPE_TEXTURE:
+			if(usage & D3DUSAGE_RENDERTARGET)
+			{
+				switch(checkFormat)
+				{
+				case D3DFMT_R8G8B8:			if(!Capabilities::Texture::RenderTarget::R8G8B8)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R5G6B5:			if(!Capabilities::Texture::RenderTarget::R5G6B5)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X1R5G5B5:		if(!Capabilities::Texture::RenderTarget::X1R5G5B5)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A1R5G5B5:		if(!Capabilities::Texture::RenderTarget::A1R5G5B5)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A4R4G4B4:		if(!Capabilities::Texture::RenderTarget::A4R4G4B4)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R3G3B2:			if(!Capabilities::Texture::RenderTarget::R3G3B2)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8R3G3B2:		if(!Capabilities::Texture::RenderTarget::A8R3G3B2)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X4R4G4B4:		if(!Capabilities::Texture::RenderTarget::X4R4G4B4)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8R8G8B8:		if(!Capabilities::Texture::RenderTarget::A8R8G8B8)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X8R8G8B8:		if(!Capabilities::Texture::RenderTarget::X8R8G8B8)		return NOTAVAILABLE();	else return D3D_OK;
+				// Integer HDR formats
+				case D3DFMT_G16R16:			if(!Capabilities::Texture::RenderTarget::G16R16)		return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A2B10G10R10:	if(!Capabilities::Texture::RenderTarget::A2B10G10R10)	return NOTAVAILABLE();	else return D3D_OK;
+				default:
+					return NOTAVAILABLE();
+				}
+			}
+			else if(usage & D3DUSAGE_DEPTHSTENCIL)
+			{
+				switch(checkFormat)
+				{
+				case D3DFMT_D32:			if(!Capabilities::Texture::DepthStencil::D32)			return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_D24S8:			if(!Capabilities::Texture::DepthStencil::D24S8)			return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_D24X8:			if(!Capabilities::Texture::DepthStencil::D24X8)			return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_D16:			if(!Capabilities::Texture::DepthStencil::D16)			return NOTAVAILABLE();	else return D3D_OK;
+				default:
+					return NOTAVAILABLE();
+				}
+			}
+			else
+			{
+				switch(checkFormat)
+				{
+				case D3DFMT_A8:				if(!Capabilities::Texture::A8)							return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R5G6B5:			if(!Capabilities::Texture::R5G6B5)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X1R5G5B5:		if(!Capabilities::Texture::X1R5G5B5)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A1R5G5B5:		if(!Capabilities::Texture::A1R5G5B5)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A4R4G4B4:		if(!Capabilities::Texture::A4R4G4B4)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R3G3B2:			if(!Capabilities::Texture::R3G3B2)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8R3G3B2:		if(!Capabilities::Texture::A8R3G3B2)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X4R4G4B4:		if(!Capabilities::Texture::X4R4G4B4)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_R8G8B8:			if(!Capabilities::Texture::R8G8B8)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X8R8G8B8:		if(!Capabilities::Texture::X8R8G8B8)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8R8G8B8:		if(!Capabilities::Texture::A8R8G8B8)					return NOTAVAILABLE();	else return D3D_OK;
+				// Paletted formats
+				case D3DFMT_P8:				if(!Capabilities::Texture::P8)							return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8P8:			if(!Capabilities::Texture::A8P8)						return NOTAVAILABLE();	else return D3D_OK;
+				// Integer HDR formats
+				case D3DFMT_G16R16:			if(!Capabilities::Texture::G16R16)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A2B10G10R10:	if(!Capabilities::Texture::A2B10G10R10)					return NOTAVAILABLE();	else return D3D_OK;
+				// Compressed formats
+				case D3DFMT_DXT1:			if(!Capabilities::Texture::DXT1)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_DXT2:			if(!Capabilities::Texture::DXT2)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_DXT3:			if(!Capabilities::Texture::DXT3)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_DXT4:			if(!Capabilities::Texture::DXT4)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_DXT5:			if(!Capabilities::Texture::DXT5)						return NOTAVAILABLE();	else return D3D_OK;
+				// Bump map formats
+				case D3DFMT_V8U8:			if(!Capabilities::Texture::V8U8)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_L6V5U5:			if(!Capabilities::Texture::L6V5U5)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_X8L8V8U8:		if(!Capabilities::Texture::X8L8V8U8)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_Q8W8V8U8:		if(!Capabilities::Texture::Q8W8V8U8)					return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_V16U16:			if(!Capabilities::Texture::V16U16)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A2W10V10U10:	if(!Capabilities::Texture::A2W10V10U10)					return NOTAVAILABLE();	else return D3D_OK;
+				// Luminance formats
+				case D3DFMT_L8:				if(!Capabilities::Texture::L8)							return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A4L4:			if(!Capabilities::Texture::A4L4)						return NOTAVAILABLE();	else return D3D_OK;
+				case D3DFMT_A8L8:			if(!Capabilities::Texture::A8L8)						return NOTAVAILABLE();	else return D3D_OK;
+				default:
+					return NOTAVAILABLE();
+				}
+			}
+		case D3DRTYPE_VERTEXBUFFER:
+			if(checkFormat == D3DFMT_VERTEXDATA)
+			{
+				return D3D_OK;
+			}
+			else
+			{
+				return NOTAVAILABLE();
+			}
+		case D3DRTYPE_INDEXBUFFER:
+			switch(checkFormat)
+			{
+			case D3DFMT_INDEX16:
+			case D3DFMT_INDEX32:
+				return D3D_OK;
+			default:
+				return NOTAVAILABLE();
+			};
+		default:
+			return NOTAVAILABLE();
+		}
+	}
+
+	long Direct3D8::CheckDeviceMultiSampleType(unsigned int adapter, D3DDEVTYPE deviceType, D3DFORMAT surfaceFormat, int windowed, D3DMULTISAMPLE_TYPE multiSampleType)
+	{
+		TRACE("");
+
+		if(deviceType != D3DDEVTYPE_HAL)
+		{
+			loadSystemD3D8();
+
+			if(d3d8)
+			{
+				return d3d8->CheckDeviceMultiSampleType(adapter, deviceType, surfaceFormat, windowed, multiSampleType);
+			}
+			else
+			{
+				return CheckDeviceMultiSampleType(adapter, D3DDEVTYPE_HAL, surfaceFormat, windowed, multiSampleType);
+			}
+		}
+
+		if(adapter >= GetAdapterCount())
+		{
+			return INVALIDCALL();
+		}
+
+		if(multiSampleType == D3DMULTISAMPLE_NONE ||
+		   multiSampleType == D3DMULTISAMPLE_2_SAMPLES ||
+		   multiSampleType == D3DMULTISAMPLE_4_SAMPLES ||
+		   multiSampleType == D3DMULTISAMPLE_8_SAMPLES ||
+		   multiSampleType == D3DMULTISAMPLE_16_SAMPLES)
+		{
+			if(surfaceFormat != D3DFMT_UNKNOWN)
+			{
+				return D3D_OK;	
+			}
+		}
+
+		return NOTAVAILABLE();
+	}
+
+	long Direct3D8::CheckDeviceType(unsigned int adapter, D3DDEVTYPE checkType, D3DFORMAT displayFormat, D3DFORMAT backBufferFormat, int windowed)
+	{
+		TRACE("");
+
+		if(checkType != D3DDEVTYPE_HAL)
+		{
+			loadSystemD3D8();
+
+			if(d3d8)
+			{
+				return d3d8->CheckDeviceType(adapter, checkType, displayFormat, backBufferFormat, windowed);
+			}
+			else
+			{
+				return CheckDeviceType(adapter, D3DDEVTYPE_HAL, displayFormat, backBufferFormat, windowed);
+			}
+		}
+
+		return D3D_OK;   // TODO
+	}
+
+	long Direct3D8::CreateDevice(unsigned int adapter, D3DDEVTYPE deviceType, HWND focusWindow, unsigned long behaviorFlags, D3DPRESENT_PARAMETERS *presentParameters, IDirect3DDevice8 **returnedDeviceInterface)
+	{
+		TRACE("");
+
+		if(deviceType != D3DDEVTYPE_HAL)
+		{
+			loadSystemD3D8();
+
+			if(d3d8)
+			{
+				return d3d8->CreateDevice(adapter, deviceType, focusWindow, behaviorFlags, presentParameters, returnedDeviceInterface);
+			}
+			else
+			{
+				return CreateDevice(adapter, D3DDEVTYPE_HAL, focusWindow, behaviorFlags, presentParameters, returnedDeviceInterface);
+			}
+		}
+
+		if(!focusWindow || !presentParameters || !returnedDeviceInterface)
+		{
+			*returnedDeviceInterface = NULL;
+			return INVALIDCALL();
+		}
+
+		if(!sw::CPUID::supportsSSE())
+		{
+			return NOTAVAILABLE();
+		}
+
+		*returnedDeviceInterface = new Direct3DDevice8(instance, this, adapter, deviceType, focusWindow, behaviorFlags, presentParameters);
+
+		if(*returnedDeviceInterface)
+		{
+			(*returnedDeviceInterface)->AddRef();
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3D8::EnumAdapterModes(unsigned int adapter, unsigned int index, D3DDISPLAYMODE *mode)
+	{
+		TRACE("");
+
+		if(adapter != D3DADAPTER_DEFAULT || !mode)
+		{
+			return INVALIDCALL();
+		}
+
+		for(int i = 0; i < numDisplayModes; i++)
+		{
+			if(index-- == 0)
+			{
+				mode->Width = displayMode[i].dmPelsWidth;
+				mode->Height = displayMode[i].dmPelsHeight;
+				mode->RefreshRate = displayMode[i].dmDisplayFrequency;
+
+				displayMode[i].dmBitsPerPel = 32;   // FIXME
+
+				switch(displayMode[i].dmBitsPerPel)
+				{
+				case 4:
+					mode->Format = D3DFMT_A4L4;
+					break;
+				case 8:
+					mode->Format = D3DFMT_P8;
+					break;
+				case 16:
+					mode->Format = D3DFMT_R5G6B5;
+					break;
+				case 24:
+					mode->Format = D3DFMT_R8G8B8;
+					break;
+				case 32:
+					mode->Format = D3DFMT_X8R8G8B8;
+					break;
+				default:
+					ASSERT(false);
+				}
+
+				return D3D_OK;
+			}
+		}
+
+		return INVALIDCALL();
+	}
+
+	unsigned int Direct3D8::GetAdapterCount()
+	{
+		TRACE("");
+
+		return 1;   // SwiftShader does not support multiple display adapters
+	}
+
+	long Direct3D8::GetAdapterDisplayMode(unsigned int adapter, D3DDISPLAYMODE *mode)
+	{
+		TRACE("");
+
+		if(adapter != D3DADAPTER_DEFAULT || !mode)
+		{
+			return INVALIDCALL();
+		}
+
+		HDC deviceContext = GetDC(0);
+			
+		mode->Width = ::GetDeviceCaps(deviceContext, HORZRES);
+		mode->Height = ::GetDeviceCaps(deviceContext, VERTRES);
+		mode->RefreshRate = ::GetDeviceCaps(deviceContext, VREFRESH);
+		unsigned int bpp = ::GetDeviceCaps(deviceContext, BITSPIXEL);
+
+		ReleaseDC(0, deviceContext);
+
+		bpp = 32;   // FIXME
+
+		switch(bpp)
+		{
+		case 32: mode->Format = D3DFMT_X8R8G8B8; break;
+		case 24: mode->Format = D3DFMT_R8G8B8; break;
+		case 16: mode->Format = D3DFMT_R5G6B5; break;
+		default:
+			ASSERT(false);   // Unexpected display mode color depth
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3D8::GetAdapterIdentifier(unsigned int adapter, unsigned long flags, D3DADAPTER_IDENTIFIER8 *identifier)
+	{
+		TRACE("");
+
+		if(!identifier)
+		{
+			return INVALIDCALL();
+		}
+	
+		unsigned short product = 'sw';
+		unsigned short version = 3;
+		unsigned short subVersion = 0;
+		unsigned short revision = BUILD_REVISION;
+		GUID guid = {0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
+
+		identifier->DriverVersion.HighPart = product << 16 | version;
+		identifier->DriverVersion.LowPart = subVersion << 16 | revision;
+		strcpy(identifier->Driver, "SwiftShader");
+		strcpy(identifier->Description, "Google SwiftShader 3D Renderer");
+		identifier->VendorId = 0;
+		identifier->DeviceId = 0;
+		identifier->SubSysId = 0;
+		identifier->Revision = 0;
+		identifier->DeviceIdentifier = guid;
+		identifier->WHQLLevel = 0;
+
+		return D3D_OK;
+	}
+
+	unsigned int Direct3D8::GetAdapterModeCount(unsigned int adapter)
+	{
+		TRACE("");
+
+		if(adapter != D3DADAPTER_DEFAULT)
+		{
+			return 0;
+		}
+
+		return numDisplayModes;
+	}
+
+	HMONITOR Direct3D8::GetAdapterMonitor(unsigned int adapter)
+	{
+		TRACE("");
+
+		POINT point = {0, 0};
+
+		return MonitorFromPoint(point, MONITOR_DEFAULTTOPRIMARY);   // FIXME: Ignores adapter parameter
+	}
+
+	long Direct3D8::GetDeviceCaps(unsigned int adapter, D3DDEVTYPE deviceType, D3DCAPS8 *capabilities)
+	{
+		TRACE("");
+
+		if(deviceType != D3DDEVTYPE_HAL)
+		{
+			loadSystemD3D8();
+
+			if(d3d8)
+			{
+				return d3d8->GetDeviceCaps(adapter, deviceType, capabilities);
+			}
+			else
+			{
+				return GetDeviceCaps(adapter, D3DDEVTYPE_HAL, capabilities);
+			}
+		}
+
+		if(!capabilities)
+		{
+			return INVALIDCALL();
+		}
+
+		D3DCAPS8 caps;
+		ZeroMemory(&caps, sizeof(D3DCAPS8));
+
+		// Device info
+		caps.DeviceType = D3DDEVTYPE_HAL;
+		caps.AdapterOrdinal = D3DADAPTER_DEFAULT;
+
+		// Caps from DX7
+		caps.Caps =	0;	//	D3DCAPS_READ_SCANLINE
+
+		caps.Caps2 =	//	D3DCAPS2_CANCALIBRATEGAMMA |	// The system has a calibrator installed that can automatically adjust the gamma ramp so that the result is identical on all systems that have a calibrator. To invoke the calibrator when setting new gamma levels, use the D3DSGR_CALIBRATE flag when calling IDirect3DDevice8::SetGammaRamp. Calibrating gamma ramps incurs some processing overhead and should not be used frequently.  
+							D3DCAPS2_CANRENDERWINDOWED |	// The driver is capable of rendering in windowed mode. 
+							D3DCAPS2_CANMANAGERESOURCE |	// The driver is capable of managing resources. On such drivers, D3DPOOL_MANAGED resources will be managed by the driver. To have Microsoft® Direct3D® override the driver so that Direct3D manages resources, use the D3DCREATE_DISABLE_DRIVER_MANAGEMENT flag when calling IDirect3D8::CreateDevice. 
+							D3DCAPS2_DYNAMICTEXTURES |		// The driver supports dynamic textures. 
+							D3DCAPS2_FULLSCREENGAMMA;		// The driver supports dynamic gamma ramp adjustment in full-screen mode.
+						//	D3DCAPS2_NO2DDURING3DSCENE;		// When the D3DCAPS2_NO2DDURING3DSCENE capability is set by the driver, it means that 2-D operations cannot be performed between calls to IDirect3DDevice8::BeginScene and IDirect3DDevice8::EndScene. 
+
+		caps.Caps3 = D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD;   //The device will work as expected with the D3DRS_ALPHABLENDENABLE render state when a full-screen application uses D3DSWAPEFFECT_FLIP or D3DRS_SWAPEFFECT_DISCARD. D3DRS_ALPHABLENDENABLE works as expected when using D3DSWAPEFFECT_COPY and D3DSWAPEFFECT_COPYSYNC
+
+		caps.PresentationIntervals =		D3DPRESENT_INTERVAL_IMMEDIATE |
+											D3DPRESENT_INTERVAL_ONE;
+										//	D3DPRESENT_INTERVAL_TWO;
+										//	D3DPRESENT_INTERVAL_THREE;
+										//	D3DPRESENT_INTERVAL_FOUR;
+
+		// Cursor caps
+		caps.CursorCaps =	D3DCURSORCAPS_COLOR	|	// A full-color cursor is supported in hardware. Specifically, this flag indicates that the driver supports at least a hardware color cursor in high-resolution modes (with scan lines greater than or equal to 400). 
+							D3DCURSORCAPS_LOWRES;	// A full-color cursor is supported in hardware. Specifically, this flag indicates that the driver supports a hardware color cursor in both high-resolution and low-resolution modes (with scan lines less than 400). 
+
+		// 3D Device caps
+		caps.DevCaps =		D3DDEVCAPS_CANBLTSYSTONONLOCAL |		// Device supports blits from system-memory textures to nonlocal video-memory textures. 
+							D3DDEVCAPS_CANRENDERAFTERFLIP |			// Device can queue rendering commands after a page flip. Applications do not change their behavior if this flag is set; this capability means that the device is relatively fast. 
+							D3DDEVCAPS_DRAWPRIMITIVES2 |			// Device can support DrawPrimitives2.
+							D3DDEVCAPS_DRAWPRIMITIVES2EX |			// Device can support extended DrawPrimitives2; that is, this is a DirectX 7.0-compliant driver.
+							D3DDEVCAPS_DRAWPRIMTLVERTEX |			// Device exports an IDirect3DDevice8::DrawPrimitive-aware hardware abstraction layer (HAL). 
+						//	D3DDEVCAPS_EXECUTESYSTEMMEMORY |		// Device can use execute buffers from system memory.
+						//	D3DDEVCAPS_EXECUTEVIDEOMEMORY |			// Device can use execute buffers from video memory. 
+							D3DDEVCAPS_HWRASTERIZATION |			// Device has hardware acceleration for scene rasterization. 
+							D3DDEVCAPS_HWTRANSFORMANDLIGHT |		// Device can support transformation and lighting in hardware. 
+						//	D3DDEVCAPS_NPATCHES |					// Device supports N patches. 
+							D3DDEVCAPS_PUREDEVICE |					// Device can support rasterization, transform, lighting, and shading in hardware. 
+						//	D3DDEVCAPS_QUINTICRTPATCHES |			// Device supports quintic Bézier curves and B-splines. 
+						//	D3DDEVCAPS_RTPATCHES |					// Device supports rectangular and triangular patches. 
+							D3DDEVCAPS_RTPATCHHANDLEZERO |			// When this device capability is set, the hardware architecture does not require caching of any information and uncached patches (handle zero) will be drawn as efficiently as cached ones. Note that setting D3DDEVCAPS_RTPATCHHANDLEZERO does not mean that a patch with handle zero can be drawn. A handle-zero patch can always be drawn whether this cap is set or not. 
+						//	D3DDEVCAPS_SEPARATETEXTUREMEMORIES |	// Device is texturing from separate memory pools. 
+							D3DDEVCAPS_TEXTURENONLOCALVIDMEM |		// Device can retrieve textures from non-local video memory. 
+							D3DDEVCAPS_TEXTURESYSTEMMEMORY |		// Device can retrieve textures from system memory. 
+							D3DDEVCAPS_TEXTUREVIDEOMEMORY |			// Device can retrieve textures from device memory. 
+							D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |		// Device can use buffers from system memory for transformed and lit vertices. 
+							D3DDEVCAPS_TLVERTEXVIDEOMEMORY;			// Device can use buffers from video memory for transformed and lit vertices.
+
+		caps.PrimitiveMiscCaps = 		D3DPMISCCAPS_BLENDOP | 					// Device supports the alpha-blending operations defined in the D3DBLENDOP enumerated type. 
+										D3DPMISCCAPS_CLIPPLANESCALEDPOINTS |	// Device correctly clips scaled points of size greater than 1.0 to user-defined clipping planes. 
+										D3DPMISCCAPS_CLIPTLVERTS |				// Device clips post-transformed vertex primitives. 
+										D3DPMISCCAPS_COLORWRITEENABLE |			// Device supports per-channel writes for the render target color buffer through the D3DRS_COLORWRITEENABLE state. 
+										D3DPMISCCAPS_CULLCCW |					// The driver supports counterclockwise culling through the D3DRS_CULLMODE state. (This applies only to triangle primitives.) This flag corresponds to the D3DCULL_CCW member of the D3DCULL enumerated type. 
+										D3DPMISCCAPS_CULLCW |					// The driver supports clockwise triangle culling through the D3DRS_CULLMODE state. (This applies only to triangle primitives.) This flag corresponds to the D3DCULL_CW member of the D3DCULL enumerated type. 
+										D3DPMISCCAPS_CULLNONE |					// The driver does not perform triangle culling. This corresponds to the D3DCULL_NONE member of the D3DCULL enumerated type. 
+									//	D3DPMISCCAPS_LINEPATTERNREP	|			// The driver can handle values other than 1 in the wRepeatFactor member of the D3DLINEPATTERN structure. (This applies only to line-drawing primitives.) 
+										D3DPMISCCAPS_MASKZ |					// Device can enable and disable modification of the depth buffer on pixel operations. 
+										D3DPMISCCAPS_TSSARGTEMP;				// Device supports D3DTA_TEMP for temporary register.
+
+		caps.RasterCaps =		D3DPRASTERCAPS_ANISOTROPY |				// Device supports anisotropic filtering. 
+							//	D3DPRASTERCAPS_ANTIALIASEDGES |			// Device can antialias lines forming the convex outline of objects. For more information, see D3DRS_EDGEANTIALIAS.
+								D3DPRASTERCAPS_COLORPERSPECTIVE |		// Device iterates colors perspective correctly. 
+							//	D3DPRASTERCAPS_DITHER |					// Device can dither to improve color resolution. 
+								D3DPRASTERCAPS_ZBIAS |					// Device supports legacy depth bias. For true depth bias, see D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS.
+								D3DPRASTERCAPS_FOGRANGE |				// Device supports range-based fog. In range-based fog, the distance of an object from the viewer is used to compute fog effects, not the depth of the object (that is, the z-coordinate) in the scene. 
+								D3DPRASTERCAPS_FOGTABLE |				// Device calculates the fog value by referring to a lookup table containing fog values that are indexed to the depth of a given pixel. 
+								D3DPRASTERCAPS_FOGVERTEX |				// Device calculates the fog value during the lighting operation and interpolates the fog value during rasterization. 
+								D3DPRASTERCAPS_MIPMAPLODBIAS |			// Device supports level of detail (LOD) bias adjustments. These bias adjustments enable an application to make a mipmap appear crisper or less sharp than it normally would. For more information about LOD bias in mipmaps, see D3DSAMP_MIPMAPLODBIAS.
+							//	D3DPRASTERCAPS_PAT |					// The driver can perform patterned drawing lines or fills with D3DRS_LINEPATTERN for the primitive being queried. 
+							//	D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE |	// Device provides limited multisample support through a stretch-blt implementation. When this capability is set, D3DRS_MULTISAMPLEANTIALIAS cannot be turned on and off in the middle of a scene. Multisample masking cannot be performed if this flag is set. 
+							//	D3DPRASTERCAPS_WBUFFER |				// Device supports depth buffering using w. 
+								D3DPRASTERCAPS_WFOG |					// Device supports w-based fog. W-based fog is used when a perspective projection matrix is specified, but affine projections still use z-based fog. The system considers a projection matrix that contains a nonzero value in the [3][4] element to be a perspective projection matrix. 
+								D3DPRASTERCAPS_ZBIAS |					// Device supports z-bias values. These are integer values assigned to polygons that allow physically coplanar polygons to appear separate. For more information, see D3DRS_ZBIAS. 
+							//	D3DPRASTERCAPS_ZBUFFERLESSHSR |			// Device can perform hidden-surface removal (HSR) without requiring the application to sort polygons and without requiring the allocation of a depth-buffer. This leaves more video memory for textures. The method used to perform HSR is hardware-dependent and is transparent to the application. Z-bufferless HSR is performed if no depth-buffer surface is associated with the rendering-target surface and the depth-buffer comparison test is enabled (that is, when the state value associated with the D3DRS_ZENABLE enumeration constant is set to TRUE).
+								D3DPRASTERCAPS_ZFOG;					// Device supports z-based fog. 
+							//	D3DPRASTERCAPS_ZTEST;					// Device can perform z-test operations. This effectively renders a primitive and indicates whether any z pixels have been rendered.
+
+		caps.ZCmpCaps =		D3DPCMPCAPS_ALWAYS |		// Always pass the z-test. 
+							D3DPCMPCAPS_EQUAL |			// Pass the z-test if the new z equals the current z. 
+							D3DPCMPCAPS_GREATER |		// Pass the z-test if the new z is greater than the current z. 
+							D3DPCMPCAPS_GREATEREQUAL |	// Pass the z-test if the new z is greater than or equal to the current z. 
+							D3DPCMPCAPS_LESS |			// Pass the z-test if the new z is less than the current z. 
+							D3DPCMPCAPS_LESSEQUAL |		// Pass the z-test if the new z is less than or equal to the current z. 
+							D3DPCMPCAPS_NEVER |			// Always fail the z-test. 
+							D3DPCMPCAPS_NOTEQUAL;		// Pass the z-test if the new z does not equal the current z.
+
+		caps.SrcBlendCaps =		D3DPBLENDCAPS_BOTHINVSRCALPHA |	// Source blend factor is (1-As,1-As,1-As,1-As) and destination blend factor is (As,As,As,As); the destination blend selection is overridden. 
+								D3DPBLENDCAPS_BOTHSRCALPHA |	// The driver supports the D3DBLEND_BOTHSRCALPHA blend mode. (This blend mode is obsolete. For more information, see D3DBLEND.) 
+								D3DPBLENDCAPS_DESTALPHA |		// Blend factor is (Ad, Ad, Ad, Ad). 
+								D3DPBLENDCAPS_DESTCOLOR |		// Blend factor is (Rd, Gd, Bd, Ad). 
+								D3DPBLENDCAPS_INVDESTALPHA |	// Blend factor is (1-Ad, 1-Ad, 1-Ad, 1-Ad). 
+								D3DPBLENDCAPS_INVDESTCOLOR |	// Blend factor is (1-Rd, 1-Gd, 1-Bd, 1-Ad). 
+								D3DPBLENDCAPS_INVSRCALPHA |		// Blend factor is (1-As, 1-As, 1-As, 1-As). 
+								D3DPBLENDCAPS_INVSRCCOLOR |		// Blend factor is (1-Rs, 1-Gs, 1-Bs, 1-As). 
+								D3DPBLENDCAPS_ONE |				// Blend factor is (1, 1, 1, 1). 
+								D3DPBLENDCAPS_SRCALPHA |		// Blend factor is (As, As, As, As). 
+								D3DPBLENDCAPS_SRCALPHASAT |		// Blend factor is (f, f, f, 1); f = min(As, 1-Ad). 
+								D3DPBLENDCAPS_SRCCOLOR |		// Blend factor is (Rs, Gs, Bs, As). 
+								D3DPBLENDCAPS_ZERO;				// Blend factor is (0, 0, 0, 0).
+
+		caps.DestBlendCaps = 	D3DPBLENDCAPS_BOTHINVSRCALPHA |	// Source blend factor is (1-As,1-As,1-As,1-As) and destination blend factor is (As,As,As,As); the destination blend selection is overridden. 
+								D3DPBLENDCAPS_BOTHSRCALPHA |	// The driver supports the D3DBLEND_BOTHSRCALPHA blend mode. (This blend mode is obsolete. For more information, see D3DBLEND.) 
+								D3DPBLENDCAPS_DESTALPHA |		// Blend factor is (Ad, Ad, Ad, Ad). 
+								D3DPBLENDCAPS_DESTCOLOR |		// Blend factor is (Rd, Gd, Bd, Ad). 
+								D3DPBLENDCAPS_INVDESTALPHA |	// Blend factor is (1-Ad, 1-Ad, 1-Ad, 1-Ad). 
+								D3DPBLENDCAPS_INVDESTCOLOR |	// Blend factor is (1-Rd, 1-Gd, 1-Bd, 1-Ad). 
+								D3DPBLENDCAPS_INVSRCALPHA |		// Blend factor is (1-As, 1-As, 1-As, 1-As). 
+								D3DPBLENDCAPS_INVSRCCOLOR |		// Blend factor is (1-Rs, 1-Gs, 1-Bs, 1-As). 
+								D3DPBLENDCAPS_ONE |				// Blend factor is (1, 1, 1, 1). 
+								D3DPBLENDCAPS_SRCALPHA |		// Blend factor is (As, As, As, As). 
+								D3DPBLENDCAPS_SRCALPHASAT |		// Blend factor is (f, f, f, 1); f = min(As, 1-Ad). 
+								D3DPBLENDCAPS_SRCCOLOR |		// Blend factor is (Rs, Gs, Bs, As). 
+								D3DPBLENDCAPS_ZERO;				// Blend factor is (0, 0, 0, 0).
+
+		caps.AlphaCmpCaps = D3DPCMPCAPS_ALWAYS |		// Always pass the apha-test. 
+							D3DPCMPCAPS_EQUAL |			// Pass the apha-test if the new apha equals the current apha. 
+							D3DPCMPCAPS_GREATER |		// Pass the apha-test if the new apha is greater than the current apha. 
+							D3DPCMPCAPS_GREATEREQUAL |	// Pass the apha-test if the new apha is greater than or equal to the current apha. 
+							D3DPCMPCAPS_LESS |			// Pass the apha-test if the new apha is less than the current apha. 
+							D3DPCMPCAPS_LESSEQUAL |		// Pass the apha-test if the new apha is less than or equal to the current apha. 
+							D3DPCMPCAPS_NEVER |			// Always fail the apha-test. 
+							D3DPCMPCAPS_NOTEQUAL;		// Pass the apha-test if the new apha does not equal the current apha.
+
+		caps.ShadeCaps =	D3DPSHADECAPS_ALPHAGOURAUDBLEND |	// Device can support an alpha component for Gouraud-blended transparency (the D3DSHADE_GOURAUD state for the D3DSHADEMODE enumerated type). In this mode, the alpha color component of a primitive is provided at vertices and interpolated across a face along with the other color components. 
+							D3DPSHADECAPS_COLORGOURAUDRGB |		// Device can support colored Gouraud shading in the RGB color model. In this mode, the color component for a primitive is provided at vertices and interpolated across a face along with the other color components. In the RGB lighting model, the red, green, and blue components are interpolated. 
+							D3DPSHADECAPS_FOGGOURAUD |			// Device can support fog in the Gouraud shading mode. 
+							D3DPSHADECAPS_SPECULARGOURAUDRGB;	// Device supports Gouraud shading of specular highlights.
+
+		caps.TextureCaps =		D3DPTEXTURECAPS_ALPHA |						// Alpha in texture pixels is supported. 
+								D3DPTEXTURECAPS_ALPHAPALETTE |				// Device can draw alpha from texture palettes. 
+								D3DPTEXTURECAPS_CUBEMAP |					// Supports cube textures.
+							//	D3DPTEXTURECAPS_CUBEMAP_POW2 |				// Device requires that cube texture maps have dimensions specified as powers of two. 
+								D3DPTEXTURECAPS_MIPCUBEMAP |				// Device supports mipmapped cube textures. 
+								D3DPTEXTURECAPS_MIPMAP |					// Device supports mipmapped textures. 
+								D3DPTEXTURECAPS_MIPVOLUMEMAP |				// Device supports mipmapped volume textures. 
+							//	D3DPTEXTURECAPS_NONPOW2CONDITIONAL |		// Conditionally supports the use of 2-D textures with dimensions that are not powers of two. A device that exposes this capability can use such a texture if all of the following requirements are met...
+								D3DPTEXTURECAPS_PERSPECTIVE |				// Perspective correction texturing is supported.
+							//	D3DPTEXTURECAPS_POW2 |						// All textures must have widths and heights specified as powers of two. This requirement does not apply to either cube textures or volume textures.
+								D3DPTEXTURECAPS_PROJECTED |					// Supports the D3DTTFF_PROJECTED texture transformation flag. When applied, the device divides transformed texture coordinates by the last texture coordinate. If this capability is present, then the projective divide occurs per pixel. If this capability is not present, but the projective divide needs to occur anyway, then it is performed on a per-vertex basis by the Direct3D runtime. 
+							//	D3DPTEXTURECAPS_SQUAREONLY |				// All textures must be square. 
+								D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |	// Texture indices are not scaled by the texture size prior to interpolation.
+								D3DPTEXTURECAPS_VOLUMEMAP;					// Device supports volume textures.
+							//	D3DPTEXTURECAPS_VOLUMEMAP_POW2;				// Device requires that volume texture maps have dimensions specified as powers of two. 
+
+		caps.TextureFilterCaps =	//	D3DPTFILTERCAPS_MAGFAFLATCUBIC |	// Device supports per-stage flat cubic filtering for magnifying textures. The flat cubic magnification filter is represented by the D3DTEXF_FLATCUBIC member of the D3DTEXTUREFILTERTYPE enumerated type. 
+									//	D3DPTFILTERCAPS_MAGFANISOTROPIC |	// Device supports per-stage anisotropic filtering for magnifying textures. The anisotropic magnification filter is represented by the D3DTEXF_ANISOTROPIC member of the D3DTEXTUREFILTERTYPE enumerated type. 
+									//	D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC |	// Device supports the per-stage Gaussian cubic filtering for magnifying textures. The Gaussian cubic magnification filter is represented by the D3DTEXF_GAUSSIANCUBIC member of the D3DTEXTUREFILTERTYPE enumerated type.
+										D3DPTFILTERCAPS_MAGFLINEAR |		// Device supports per-stage bilinear interpolation filtering for magnifying mipmaps. The bilinear interpolation mipmapping filter is represented by the D3DTEXF_LINEAR member of the D3DTEXTUREFILTERTYPE enumerated type. 
+										D3DPTFILTERCAPS_MAGFPOINT |			// Device supports per-stage point-sample filtering for magnifying textures. The point-sample magnification filter is represented by the D3DTEXF_POINT member of the D3DTEXTUREFILTERTYPE enumerated type. 
+										D3DPTFILTERCAPS_MINFANISOTROPIC |	// Device supports per-stage anisotropic filtering for minifying textures. The anisotropic minification filter is represented by the D3DTEXF_ANISOTROPIC member of the D3DTEXTUREFILTERTYPE enumerated type. 
+										D3DPTFILTERCAPS_MINFLINEAR |		// Device supports per-stage linear filtering for minifying textures. The linear minification filter is represented by the D3DTEXF_LINEAR member of the D3DTEXTUREFILTERTYPE enumerated type. 
+										D3DPTFILTERCAPS_MINFPOINT |			// Device supports per-stage point-sample filtering for minifying textures. The point-sample minification filter is represented by the D3DTEXF_POINT member of the D3DTEXTUREFILTERTYPE enumerated type. 
+										D3DPTFILTERCAPS_MIPFLINEAR |		// Device supports per-stage bilinear interpolation filtering for mipmaps. The bilinear interpolation mipmapping filter is represented by the D3DTEXF_LINEAR member of the D3DTEXTUREFILTERTYPE enumerated type. 
+										D3DPTFILTERCAPS_MIPFPOINT;			// Device supports per-stage point-sample filtering for mipmaps. The point-sample mipmapping filter is represented by the D3DTEXF_POINT member of the D3DTEXTUREFILTERTYPE enumerated type. 
+									
+		caps.CubeTextureFilterCaps =	//	D3DPTFILTERCAPS_MAGFAFLATCUBIC |	// Device supports per-stage flat cubic filtering for magnifying textures. The flat cubic magnification filter is represented by the D3DTEXF_FLATCUBIC member of the D3DTEXTUREFILTERTYPE enumerated type. 
+										//	D3DPTFILTERCAPS_MAGFANISOTROPIC |	// Device supports per-stage anisotropic filtering for magnifying textures. The anisotropic magnification filter is represented by the D3DTEXF_ANISOTROPIC member of the D3DTEXTUREFILTERTYPE enumerated type. 
+										//	D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC |	// Device supports the per-stage Gaussian cubic filtering for magnifying textures. The Gaussian cubic magnification filter is represented by the D3DTEXF_GAUSSIANCUBIC member of the D3DTEXTUREFILTERTYPE enumerated type.
+											D3DPTFILTERCAPS_MAGFLINEAR |		// Device supports per-stage bilinear interpolation filtering for magnifying mipmaps. The bilinear interpolation mipmapping filter is represented by the D3DTEXF_LINEAR member of the D3DTEXTUREFILTERTYPE enumerated type. 
+											D3DPTFILTERCAPS_MAGFPOINT |			// Device supports per-stage point-sample filtering for magnifying textures. The point-sample magnification filter is represented by the D3DTEXF_POINT member of the D3DTEXTUREFILTERTYPE enumerated type. 
+										//	D3DPTFILTERCAPS_MINFANISOTROPIC |	// Device supports per-stage anisotropic filtering for minifying textures. The anisotropic minification filter is represented by the D3DTEXF_ANISOTROPIC member of the D3DTEXTUREFILTERTYPE enumerated type. 
+											D3DPTFILTERCAPS_MINFLINEAR |		// Device supports per-stage linear filtering for minifying textures. The linear minification filter is represented by the D3DTEXF_LINEAR member of the D3DTEXTUREFILTERTYPE enumerated type. 
+											D3DPTFILTERCAPS_MINFPOINT |			// Device supports per-stage point-sample filtering for minifying textures. The point-sample minification filter is represented by the D3DTEXF_POINT member of the D3DTEXTUREFILTERTYPE enumerated type. 
+											D3DPTFILTERCAPS_MIPFLINEAR |		// Device supports per-stage bilinear interpolation filtering for mipmaps. The bilinear interpolation mipmapping filter is represented by the D3DTEXF_LINEAR member of the D3DTEXTUREFILTERTYPE enumerated type. 
+											D3DPTFILTERCAPS_MIPFPOINT;			// Device supports per-stage point-sample filtering for mipmaps. The point-sample mipmapping filter is represented by the D3DTEXF_POINT member of the D3DTEXTUREFILTERTYPE enumerated type. 
+									
+		caps.VolumeTextureFilterCaps = 	//	D3DPTFILTERCAPS_MAGFAFLATCUBIC |	// Device supports per-stage flat cubic filtering for magnifying textures. The flat cubic magnification filter is represented by the D3DTEXF_FLATCUBIC member of the D3DTEXTUREFILTERTYPE enumerated type. 
+										//	D3DPTFILTERCAPS_MAGFANISOTROPIC |	// Device supports per-stage anisotropic filtering for magnifying textures. The anisotropic magnification filter is represented by the D3DTEXF_ANISOTROPIC member of the D3DTEXTUREFILTERTYPE enumerated type. 
+										//	D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC |	// Device supports the per-stage Gaussian cubic filtering for magnifying textures. The Gaussian cubic magnification filter is represented by the D3DTEXF_GAUSSIANCUBIC member of the D3DTEXTUREFILTERTYPE enumerated type.
+											D3DPTFILTERCAPS_MAGFLINEAR |		// Device supports per-stage bilinear interpolation filtering for magnifying mipmaps. The bilinear interpolation mipmapping filter is represented by the D3DTEXF_LINEAR member of the D3DTEXTUREFILTERTYPE enumerated type. 
+											D3DPTFILTERCAPS_MAGFPOINT |			// Device supports per-stage point-sample filtering for magnifying textures. The point-sample magnification filter is represented by the D3DTEXF_POINT member of the D3DTEXTUREFILTERTYPE enumerated type. 
+										//	D3DPTFILTERCAPS_MINFANISOTROPIC |	// Device supports per-stage anisotropic filtering for minifying textures. The anisotropic minification filter is represented by the D3DTEXF_ANISOTROPIC member of the D3DTEXTUREFILTERTYPE enumerated type. 
+											D3DPTFILTERCAPS_MINFLINEAR |		// Device supports per-stage linear filtering for minifying textures. The linear minification filter is represented by the D3DTEXF_LINEAR member of the D3DTEXTUREFILTERTYPE enumerated type. 
+											D3DPTFILTERCAPS_MINFPOINT |			// Device supports per-stage point-sample filtering for minifying textures. The point-sample minification filter is represented by the D3DTEXF_POINT member of the D3DTEXTUREFILTERTYPE enumerated type. 
+											D3DPTFILTERCAPS_MIPFLINEAR |		// Device supports per-stage bilinear interpolation filtering for mipmaps. The bilinear interpolation mipmapping filter is represented by the D3DTEXF_LINEAR member of the D3DTEXTUREFILTERTYPE enumerated type. 
+											D3DPTFILTERCAPS_MIPFPOINT;			// Device supports per-stage point-sample filtering for mipmaps. The point-sample mipmapping filter is represented by the D3DTEXF_POINT member of the D3DTEXTUREFILTERTYPE enumerated type. 
+									
+		caps.TextureAddressCaps =	D3DPTADDRESSCAPS_BORDER |			// Device supports setting coordinates outside the range [0.0, 1.0] to the border color, as specified by the D3DSAMP_BORDERCOLOR texture-stage state. 
+									D3DPTADDRESSCAPS_CLAMP |			// Device can clamp textures to addresses. 
+									D3DPTADDRESSCAPS_INDEPENDENTUV |	// Device can separate the texture-addressing modes of the u and v coordinates of the texture. This ability corresponds to the D3DSAMP_ADDRESSU and D3DSAMP_ADDRESSV render-state values. 
+									D3DPTADDRESSCAPS_MIRROR |			// Device can mirror textures to addresses. 
+									D3DPTADDRESSCAPS_MIRRORONCE |		// Device can take the absolute value of the texture coordinate (thus, mirroring around 0) and then clamp to the maximum value. 
+									D3DPTADDRESSCAPS_WRAP;				// Device can wrap textures to addresses.
+
+		caps.VolumeTextureAddressCaps =	D3DPTADDRESSCAPS_BORDER |			// Device supports setting coordinates outside the range [0.0, 1.0] to the border color, as specified by the D3DSAMP_BORDERCOLOR texture-stage state. 
+										D3DPTADDRESSCAPS_CLAMP |			// Device can clamp textures to addresses. 
+										D3DPTADDRESSCAPS_INDEPENDENTUV |	// Device can separate the texture-addressing modes of the u and v coordinates of the texture. This ability corresponds to the D3DSAMP_ADDRESSU and D3DSAMP_ADDRESSV render-state values. 
+										D3DPTADDRESSCAPS_MIRROR |			// Device can mirror textures to addresses. 
+										D3DPTADDRESSCAPS_MIRRORONCE |		// Device can take the absolute value of the texture coordinate (thus, mirroring around 0) and then clamp to the maximum value. 
+										D3DPTADDRESSCAPS_WRAP;				// Device can wrap textures to addresses.
+
+		caps.LineCaps = D3DLINECAPS_ALPHACMP |	// Supports alpha-test comparisons. 
+						D3DLINECAPS_BLEND |		// Supports source-blending. 
+						D3DLINECAPS_FOG |		// Supports fog. 
+						D3DLINECAPS_TEXTURE |	// Supports texture-mapping. 
+						D3DLINECAPS_ZTEST;		// Supports z-buffer comparisons.
+		
+		caps.MaxTextureWidth = 1 << (sw::MIPMAP_LEVELS - 1);
+		caps.MaxTextureHeight = 1 << (sw::MIPMAP_LEVELS - 1);
+		caps.MaxVolumeExtent = 1 << (sw::MIPMAP_LEVELS - 1);
+		caps.MaxTextureRepeat = 8192;
+		caps.MaxTextureAspectRatio = 1 << (sw::MIPMAP_LEVELS - 1);
+		caps.MaxAnisotropy = maxAnisotropy;
+		caps.MaxVertexW = 1e+010;
+
+		caps.GuardBandLeft = -1e+008;
+		caps.GuardBandTop = -1e+008;
+		caps.GuardBandRight = 1e+008;
+		caps.GuardBandBottom = 1e+008;
+
+		caps.ExtentsAdjust = 0;
+
+		caps.StencilCaps =		D3DSTENCILCAPS_KEEP |		// Do not update the entry in the stencil buffer. This is the default value. 
+								D3DSTENCILCAPS_ZERO |		// Set the stencil-buffer entry to 0. 
+								D3DSTENCILCAPS_REPLACE |	// Replace the stencil-buffer entry with reference value. 
+								D3DSTENCILCAPS_INCRSAT |	// Increment the stencil-buffer entry, clamping to the maximum value.  
+								D3DSTENCILCAPS_DECRSAT |	// Decrement the stencil-buffer entry, clamping to zero. 
+								D3DSTENCILCAPS_INVERT |		// Invert the bits in the stencil-buffer entry. 
+								D3DSTENCILCAPS_INCR |		// Increment the stencil-buffer entry, wrapping to zero if the new value exceeds the maximum value.  
+								D3DSTENCILCAPS_DECR;		// Decrement the stencil-buffer entry, wrapping to the maximum value if the new value is less than zero. 
+		
+		caps.FVFCaps =		D3DFVFCAPS_DONOTSTRIPELEMENTS |		// It is preferable that vertex elements not be stripped. That is, if the vertex format contains elements that are not used with the current render states, there is no need to regenerate the vertices. If this capability flag is not present, stripping extraneous elements from the vertex format provides better performance. 
+							D3DFVFCAPS_PSIZE |					// Point size is determined by either the render state or the vertex data.
+						//	D3DFVFCAPS_TEXCOORDCOUNTMASK |		// Masks the low WORD of FVFCaps. These bits, cast to the WORD data type, describe the total number of texture coordinate sets that the device can simultaneously use for multiple texture blending. (You can use up to eight texture coordinate sets for any vertex, but the device can blend using only the specified number of texture coordinate sets.) 
+							8;
+
+		caps.TextureOpCaps =	D3DTEXOPCAPS_ADD |							// The D3DTOP_ADD texture-blending operation is supported. 
+								D3DTEXOPCAPS_ADDSIGNED |					// The D3DTOP_ADDSIGNED texture-blending operation is supported. 
+								D3DTEXOPCAPS_ADDSIGNED2X |					// The D3DTOP_ADDSIGNED2X texture-blending operation is supported. 
+								D3DTEXOPCAPS_ADDSMOOTH |					// The D3DTOP_ADDSMOOTH texture-blending operation is supported. 
+								D3DTEXOPCAPS_BLENDCURRENTALPHA |			// The D3DTOP_BLENDCURRENTALPHA texture-blending operation is supported. 
+								D3DTEXOPCAPS_BLENDDIFFUSEALPHA |			// The D3DTOP_BLENDDIFFUSEALPHA texture-blending operation is supported. 
+								D3DTEXOPCAPS_BLENDFACTORALPHA |				// The D3DTOP_BLENDFACTORALPHA texture-blending operation is supported. 
+								D3DTEXOPCAPS_BLENDTEXTUREALPHA |			// The D3DTOP_BLENDTEXTUREALPHA texture-blending operation is supported. 
+								D3DTEXOPCAPS_BLENDTEXTUREALPHAPM |			// The D3DTOP_BLENDTEXTUREALPHAPM texture-blending operation is supported. 
+								D3DTEXOPCAPS_BUMPENVMAP |					// The D3DTOP_BUMPENVMAP texture-blending operation is supported. 
+								D3DTEXOPCAPS_BUMPENVMAPLUMINANCE |			// The D3DTOP_BUMPENVMAPLUMINANCE texture-blending operation is supported. 
+								D3DTEXOPCAPS_DISABLE |						// The D3DTOP_DISABLE texture-blending operation is supported. 
+								D3DTEXOPCAPS_DOTPRODUCT3 |					// The D3DTOP_DOTPRODUCT3 texture-blending operation is supported. 
+								D3DTEXOPCAPS_LERP |							// The D3DTOP_LERP texture-blending operation is supported. 
+								D3DTEXOPCAPS_MODULATE |						// The D3DTOP_MODULATE texture-blending operation is supported. 
+								D3DTEXOPCAPS_MODULATE2X |					// The D3DTOP_MODULATE2X texture-blending operation is supported. 
+								D3DTEXOPCAPS_MODULATE4X |					// The D3DTOP_MODULATE4X texture-blending operation is supported. 
+								D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |		// The D3DTOP_MODULATEALPHA_ADDCOLOR texture-blending operation is supported. 
+								D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |		// The D3DTOP_MODULATECOLOR_ADDALPHA texture-blending operation is supported. 
+								D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR |	// The D3DTOP_MODULATEINVALPHA_ADDCOLOR texture-blending operation is supported. 
+								D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA |	// The D3DTOP_MODULATEINVCOLOR_ADDALPHA texture-blending operation is supported. 
+								D3DTEXOPCAPS_MULTIPLYADD |					// The D3DTOP_MULTIPLYADD texture-blending operation is supported. 
+								D3DTEXOPCAPS_PREMODULATE |					// The D3DTOP_PREMODULATE texture-blending operation is supported. 
+								D3DTEXOPCAPS_SELECTARG1 |					// The D3DTOP_SELECTARG1 texture-blending operation is supported. 
+								D3DTEXOPCAPS_SELECTARG2 |					// The D3DTOP_SELECTARG2 texture-blending operation is supported. 
+								D3DTEXOPCAPS_SUBTRACT;						// The D3DTOP_SUBTRACT texture-blending operation is supported. 
+
+		caps.MaxTextureBlendStages = 8;
+		caps.MaxSimultaneousTextures = 8;
+
+		caps.VertexProcessingCaps =		D3DVTXPCAPS_DIRECTIONALLIGHTS |			// Device can do directional lights. 
+										D3DVTXPCAPS_LOCALVIEWER |				// Device can do local viewer. 
+										D3DVTXPCAPS_MATERIALSOURCE7	|			// Device can do Microsoft® DirectX® 7.0 colormaterialsource operations. 
+										D3DVTXPCAPS_POSITIONALLIGHTS |			// Device can do positional lights (includes point and spot). 
+										D3DVTXPCAPS_TEXGEN |					// Device can do texgen. 
+										D3DVTXPCAPS_TWEENING;					// Device can do vertex tweening. 
+									//	D3DVTXPCAPS_NO_VSDT_UBYTE4;				// Device does not support the D3DVSDT_UBYTE4 vertex declaration type.  
+
+		caps.MaxActiveLights = 8;						// Maximum number of lights that can be active simultaneously. For a given physical device, this capability might vary across Direct3DDevice objects depending on the parameters supplied to IDirect3D8::CreateDevice.
+		caps.MaxUserClipPlanes = 6;						// Maximum number of user-defined clipping planes supported. This member can range from 0 through D3DMAXUSERCLIPPLANES. For a given physical device, this capability may vary across Direct3DDevice objects depending on the parameters supplied to IDirect3D8::CreateDevice. 
+		caps.MaxVertexBlendMatrices = 4;				// Maximum number of matrices that this device can apply when performing multimatrix vertex blending. For a given physical device, this capability may vary across Direct3DDevice objects depending on the parameters supplied to IDirect3D8::CreateDevice. 
+		caps.MaxVertexBlendMatrixIndex = 11;			// DWORD value that specifies the maximum matrix index that can be indexed into using the per-vertex indices. The number of matrices is MaxVertexBlendMatrixIndex + 1, which is the size of the matrix palette. If normals are present in the vertex data that needs to be blended for lighting, then the number of matrices is half the number specified by this capability flag. If MaxVertexBlendMatrixIndex is set to zero, the driver does not support indexed vertex blending. If this value is not zero then the valid range of indices is zero through MaxVertexBlendMatrixIndex.
+		caps.MaxPointSize = 8192.0f;					// Maximum size of a point primitive. If set to 1.0f then device does not support point size control. The range is greater than or equal to 1.0f.
+		caps.MaxPrimitiveCount = 1 << 21;				// Maximum number of primitives for each IDirect3DDevice8::DrawPrimitive call. Note that when Direct3D is working with a DirectX 6.0 or DirectX 7.0 driver, this field is set to 0xFFFF. This means that not only the number of primitives but also the number of vertices is limited by this value.
+		caps.MaxVertexIndex = 1 << 24;					// Maximum size of indices supported for hardware vertex processing. It is possible to create 32-bit index buffers by specifying D3DFMT_INDEX32; however, you will not be able to render with the index buffer unless this value is greater than 0x0000FFFF.
+		caps.MaxStreams = 16;							// Maximum number of concurrent data streams for IDirect3DDevice8::SetStreamSource. The valid range is 1 to 16. Note that if this value is 0, then the driver is not a DirectX 9.0 driver.
+		caps.MaxStreamStride = 65536;					// Maximum stride for IDirect3DDevice8::SetStreamSource.
+		caps.VertexShaderVersion = vertexShaderVersion;	// Two numbers that represent the vertex shader main and sub versions. For more information about the instructions supported for each vertex shader version, see Version 1_x, Version 2_0, Version 2_0 Extended, or Version 3_0.
+		caps.MaxVertexShaderConst = 256;				// The number of vertex shader Registers that are reserved for constants.
+		caps.PixelShaderVersion = pixelShaderVersion;	// Two numbers that represent the pixel shader main and sub versions. For more information about the instructions supported for each pixel shader version, see Version 1_x, Version 2_0, Version 2_0 Extended, or Version 3_0.
+		caps.MaxPixelShaderValue = 8.0;
+		
+		*capabilities = caps;
+
+		return D3D_OK;
+	}
+
+	long Direct3D8::RegisterSoftwareDevice(void *initializeFunction)
+	{
+		TRACE("");
+
+		loadSystemD3D8();
+
+		if(d3d8)
+		{
+			return d3d8->RegisterSoftwareDevice(initializeFunction);
+		}
+		else
+		{
+			return INVALIDCALL();
+		}
+	}
+
+	void Direct3D8::loadSystemD3D8()
+	{
+		if(d3d8)
+		{
+			return;
+		}
+
+		char d3d8Path[MAX_PATH + 16];
+		GetSystemDirectory(d3d8Path, MAX_PATH);
+		strcat(d3d8Path, "\\d3d8.dll");
+		d3d8Lib = LoadLibrary(d3d8Path);
+
+		if(d3d8Lib)
+		{
+			typedef IDirect3D8* (__stdcall *DIRECT3DCREATE8)(unsigned int);
+			DIRECT3DCREATE8 direct3DCreate8 = (DIRECT3DCREATE8)GetProcAddress(d3d8Lib, "Direct3DCreate8");
+			d3d8 = direct3DCreate8(D3D_SDK_VERSION);
+		}
+	}
+}
diff --git a/src/D3D8/Direct3D8.hpp b/src/D3D8/Direct3D8.hpp
new file mode 100644
index 0000000..1017846
--- /dev/null
+++ b/src/D3D8/Direct3D8.hpp
@@ -0,0 +1,71 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3D8_hpp
+#define D3D8_Direct3D8_hpp
+
+#include "Unknown.hpp"
+
+#include <stdio.h>
+#include <initguid.h>
+#include <d3d8.h>
+
+namespace D3D8
+{
+	class Direct3DDevice8;
+
+	class Direct3D8 : public IDirect3D8, protected Unknown
+	{
+	public:
+		Direct3D8(int version, const HINSTANCE instance);
+
+		virtual ~Direct3D8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3D8 methods
+		long __stdcall CheckDepthStencilMatch(unsigned int adapter, D3DDEVTYPE deviceType, D3DFORMAT adapterFormat, D3DFORMAT renderTargetFormat, D3DFORMAT depthStencilFormat);
+		long __stdcall CheckDeviceFormat(unsigned int adapter, D3DDEVTYPE deviceType, D3DFORMAT adapaterFormat, unsigned long usage, D3DRESOURCETYPE type, D3DFORMAT checkFormat);
+		long __stdcall CheckDeviceMultiSampleType(unsigned int adapter, D3DDEVTYPE deviceType, D3DFORMAT surfaceFormat, int windowed, D3DMULTISAMPLE_TYPE multiSampleType);
+		long __stdcall CheckDeviceType(unsigned int adapter, D3DDEVTYPE checkType, D3DFORMAT displayFormat, D3DFORMAT backBufferFormat, int windowed);
+		long __stdcall CreateDevice(unsigned int adapter, D3DDEVTYPE deviceType, HWND focusWindow, unsigned long behaviorFlags, D3DPRESENT_PARAMETERS *presentParameters, IDirect3DDevice8 **returnedDeviceInterface);
+		long __stdcall EnumAdapterModes(unsigned int adapter, unsigned int index, D3DDISPLAYMODE *mode);
+		unsigned int __stdcall GetAdapterCount();
+		long __stdcall GetAdapterDisplayMode(unsigned int adapter, D3DDISPLAYMODE *mode);
+		long __stdcall GetAdapterIdentifier(unsigned int adapter, unsigned long flags, D3DADAPTER_IDENTIFIER8 *identifier);
+		unsigned int __stdcall GetAdapterModeCount(unsigned int adapter);
+		HMONITOR __stdcall GetAdapterMonitor(unsigned int adapter);
+		long __stdcall GetDeviceCaps(unsigned int adapter, D3DDEVTYPE deviceType, D3DCAPS8 *caps);
+		long __stdcall RegisterSoftwareDevice(void *initializeFunction);
+
+	private:
+		void loadSystemD3D8();
+
+		// Creation parameters
+		const int version;
+		const HINSTANCE instance;
+
+		DEVMODE *displayMode;
+		int numDisplayModes;
+
+		// Real D3D8 library and IDirect3D8 object
+		HMODULE d3d8Lib;
+		IDirect3D8 *d3d8;
+	};
+}
+
+#endif   // D3D8_Direct3D8_hpp
diff --git a/src/D3D8/Direct3DBaseTexture8.cpp b/src/D3D8/Direct3DBaseTexture8.cpp
new file mode 100644
index 0000000..3a3f208
--- /dev/null
+++ b/src/D3D8/Direct3DBaseTexture8.cpp
@@ -0,0 +1,156 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DBaseTexture8.hpp"
+
+#include "Resource.hpp"
+#include "Debug.hpp"
+
+namespace D3D8
+{
+	Direct3DBaseTexture8::Direct3DBaseTexture8(Direct3DDevice8 *device, D3DRESOURCETYPE type, unsigned long levels, unsigned long usage) : Direct3DResource8(device, type, 0), levels(levels), usage(usage)
+	{
+		filterType = D3DTEXF_LINEAR;
+		LOD = 0;
+
+		resource = new sw::Resource(0);
+	}
+
+	Direct3DBaseTexture8::~Direct3DBaseTexture8()
+	{
+		resource->destruct();
+	}
+	
+	long Direct3DBaseTexture8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		if(iid == IID_IDirect3DBaseTexture8 ||
+		   iid == IID_IDirect3DResource8 ||
+		   iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DBaseTexture8::AddRef()
+	{
+		TRACE("");
+
+		return Direct3DResource8::AddRef();
+	}
+
+	unsigned long Direct3DBaseTexture8::Release()
+	{
+		TRACE("");
+
+		return Direct3DResource8::Release();
+	}
+
+	long Direct3DBaseTexture8::FreePrivateData(const GUID &guid)
+	{
+		TRACE("");
+
+		return Direct3DResource8::FreePrivateData(guid);
+	}
+
+	long Direct3DBaseTexture8::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
+	{
+		TRACE("");
+
+		return Direct3DResource8::GetPrivateData(guid, data, size);
+	}
+
+	void Direct3DBaseTexture8::PreLoad()
+	{
+		TRACE("");
+
+		Direct3DResource8::PreLoad();
+	}
+
+	long Direct3DBaseTexture8::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
+	{
+		TRACE("");
+
+		return Direct3DResource8::SetPrivateData(guid, data, size, flags);
+	}
+
+	long Direct3DBaseTexture8::GetDevice(IDirect3DDevice8 **device)
+	{
+		TRACE("");
+
+		return Direct3DResource8::GetDevice(device);
+	}
+
+	unsigned long Direct3DBaseTexture8::SetPriority(unsigned long newPriority)
+	{
+		TRACE("");
+
+		return Direct3DResource8::SetPriority(newPriority);
+	}
+
+	unsigned long Direct3DBaseTexture8::GetPriority()
+	{
+		TRACE("");
+
+		return Direct3DResource8::GetPriority();
+	}
+
+	D3DRESOURCETYPE Direct3DBaseTexture8::GetType()
+	{
+		TRACE("");
+
+		return Direct3DResource8::GetType();
+	}
+
+	unsigned long Direct3DBaseTexture8::GetLevelCount()
+	{
+		TRACE("");
+
+		return levels;
+	}
+
+	unsigned long Direct3DBaseTexture8::GetLOD()
+	{
+		TRACE("");
+
+		return LOD;
+	}
+
+	long Direct3DBaseTexture8::SetLOD(long newLOD)
+	{
+		TRACE("");
+
+		LOD = newLOD;
+
+		return 0;   // TODO
+	}
+
+	sw::Resource *Direct3DBaseTexture8::getResource() const
+	{
+		return resource;
+	}
+
+	unsigned long Direct3DBaseTexture8::getInternalLevelCount()
+	{
+		return levels;
+	}
+}
diff --git a/src/D3D8/Direct3DBaseTexture8.hpp b/src/D3D8/Direct3DBaseTexture8.hpp
new file mode 100644
index 0000000..ab69aba
--- /dev/null
+++ b/src/D3D8/Direct3DBaseTexture8.hpp
@@ -0,0 +1,73 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DBaseTexture8_hpp
+#define D3D8_Direct3DBaseTexture8_hpp
+
+#include "Direct3DResource8.hpp"
+
+#include <d3d8.h>
+
+namespace sw
+{
+	class Resource;
+}
+
+namespace D3D8
+{
+	class Direct3DBaseTexture8 : public IDirect3DBaseTexture8, public Direct3DResource8
+	{
+	public:
+		Direct3DBaseTexture8(Direct3DDevice8 *device, D3DRESOURCETYPE type, unsigned long levels, unsigned long usage);
+
+		virtual ~Direct3DBaseTexture8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3DResource8 methods
+		long __stdcall FreePrivateData(const GUID &guid);
+		long __stdcall GetPrivateData(const GUID &guid, void *data, unsigned long *size);
+		void __stdcall PreLoad();
+		long __stdcall SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags);
+		long __stdcall GetDevice(IDirect3DDevice8 **device);
+		unsigned long __stdcall SetPriority(unsigned long newPriority);
+		unsigned long __stdcall GetPriority();
+		D3DRESOURCETYPE __stdcall GetType();
+
+		// IDirect3DBaseTexture8 methods
+		unsigned long __stdcall GetLevelCount();
+		unsigned long __stdcall GetLOD();
+		long __stdcall SetLOD(long newLOD);
+
+		// Intenal methods
+		sw::Resource *getResource() const;
+		unsigned long getInternalLevelCount();
+
+	protected:
+		// Creation paramters
+		unsigned long levels;   // Recalculated when 0
+		const unsigned long usage;
+
+		sw::Resource *resource;
+
+	private:
+		D3DTEXTUREFILTERTYPE filterType;
+		unsigned long LOD;
+	};
+}
+
+#endif // D3D8_Direct3DBaseTexture8_hpp
diff --git a/src/D3D8/Direct3DCubeTexture8.cpp b/src/D3D8/Direct3DCubeTexture8.cpp
new file mode 100644
index 0000000..d8b4a3c
--- /dev/null
+++ b/src/D3D8/Direct3DCubeTexture8.cpp
@@ -0,0 +1,255 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DCubeTexture8.hpp"
+
+#include "Direct3DSurface8.hpp"
+#include "Resource.hpp"
+#include "Debug.hpp"
+
+#include <assert.h>
+
+namespace D3D8
+{
+	Direct3DCubeTexture8::Direct3DCubeTexture8(Direct3DDevice8 *device, unsigned int edgeLength, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool) : Direct3DBaseTexture8(device, D3DRTYPE_CUBETEXTURE, levels, usage), edgeLength(edgeLength), format(format), pool(pool)
+	{
+		if(levels == 0)
+		{
+			this->levels = sw::log2(sw::max((int)edgeLength, 1)) + 1;
+		}
+
+		for(unsigned int face = 0; face < 6; face++)
+		{
+			int width = edgeLength;
+			int height = edgeLength;
+
+			for(unsigned int level = 0; level < sw::MIPMAP_LEVELS; level++)
+			{
+				if(level < this->levels)
+				{					
+					surfaceLevel[face][level] = new Direct3DSurface8(device, this, width, height, format, pool, D3DMULTISAMPLE_NONE, true, usage);
+					surfaceLevel[face][level]->bind();
+				}
+				else
+				{
+					surfaceLevel[face][level] = 0;
+				}
+
+				width = sw::max(1, width / 2);
+				height = sw::max(1, height / 2);
+			}
+		}
+	}
+
+	Direct3DCubeTexture8::~Direct3DCubeTexture8()
+	{
+		resource->lock(sw::DESTRUCT);
+
+		for(unsigned int face = 0; face < 6; face++)
+		{
+			for(int level = 0; level < sw::MIPMAP_LEVELS; level++)
+			{
+				if(surfaceLevel[face][level])
+				{
+					surfaceLevel[face][level]->unbind();
+					surfaceLevel[face][level] = 0;
+				}
+			}
+		}
+
+		resource->unlock();
+	}
+
+	long Direct3DCubeTexture8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		if(iid == IID_IDirect3DCubeTexture8 ||
+		   iid == IID_IDirect3DBaseTexture8 ||
+		   iid == IID_IDirect3DResource8 ||
+		   iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DCubeTexture8::AddRef()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::AddRef();
+	}
+
+	unsigned long Direct3DCubeTexture8::Release()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::Release();
+	}
+
+	long Direct3DCubeTexture8::FreePrivateData(const GUID &guid)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::FreePrivateData(guid);
+	}
+
+	long Direct3DCubeTexture8::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetPrivateData(guid, data, size);
+	}
+
+	void Direct3DCubeTexture8::PreLoad()
+	{
+		TRACE("");
+
+		Direct3DBaseTexture8::PreLoad();
+	}
+
+	long Direct3DCubeTexture8::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::SetPrivateData(guid, data, size, flags);
+	}
+
+	long Direct3DCubeTexture8::GetDevice(IDirect3DDevice8 **device)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetDevice(device);
+	}
+
+	unsigned long Direct3DCubeTexture8::SetPriority(unsigned long newPriority)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::SetPriority(newPriority);
+	}
+
+	unsigned long Direct3DCubeTexture8::GetPriority()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetPriority();
+	}
+
+	D3DRESOURCETYPE Direct3DCubeTexture8::GetType()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetType();
+	}
+
+	unsigned long Direct3DCubeTexture8::GetLevelCount()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetLevelCount();
+	}
+
+	unsigned long Direct3DCubeTexture8::GetLOD()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetLOD();
+	}
+
+	unsigned long Direct3DCubeTexture8::SetLOD(unsigned long newLOD)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::SetLOD(newLOD);
+	}
+
+	long Direct3DCubeTexture8::AddDirtyRect(D3DCUBEMAP_FACES face, const RECT *dirtyRect)
+	{
+		TRACE("");
+
+	//	UNIMPLEMENTED();
+
+		return D3D_OK;
+	}
+
+	long Direct3DCubeTexture8::GetCubeMapSurface(D3DCUBEMAP_FACES face, unsigned int level , IDirect3DSurface8 **cubeMapSurface)
+	{
+		TRACE("");
+
+		*cubeMapSurface = 0;   // FIXME: Verify
+
+		if(face >= 6 || level >= GetLevelCount() || !surfaceLevel[face][level])
+		{
+			return INVALIDCALL();
+		}
+
+		surfaceLevel[face][level]->AddRef();
+		*cubeMapSurface = surfaceLevel[face][level];
+
+		return D3D_OK;
+	}
+
+	long Direct3DCubeTexture8::GetLevelDesc(unsigned int level, D3DSURFACE_DESC *description)
+	{
+		TRACE("");
+
+		if(!description || level >= GetLevelCount() || !surfaceLevel[0][level])
+		{
+			return INVALIDCALL();
+		}
+
+		surfaceLevel[0][level]->GetDesc(description);
+
+		return D3D_OK;
+	}
+
+	long Direct3DCubeTexture8::LockRect(D3DCUBEMAP_FACES face, unsigned int level, D3DLOCKED_RECT *lockedRect, const RECT *rect, unsigned long flags)
+	{
+		TRACE("");
+
+		if(!lockedRect || face >= 6 || level >= GetLevelCount() || !surfaceLevel[face][level])
+		{
+			return INVALIDCALL();
+		}
+
+		surfaceLevel[face][level]->LockRect(lockedRect, rect, flags);
+
+		return D3D_OK;
+	}
+
+	long Direct3DCubeTexture8::UnlockRect(D3DCUBEMAP_FACES face, unsigned int level)
+	{
+		TRACE("");
+
+		if(face >= 6 || level >= GetLevelCount() || !surfaceLevel[face][level])
+		{
+			return INVALIDCALL();
+		}
+
+		return surfaceLevel[face][level]->UnlockRect();
+	}
+
+	Direct3DSurface8 *Direct3DCubeTexture8::getInternalCubeMapSurface(D3DCUBEMAP_FACES face, unsigned int level)
+	{
+		return surfaceLevel[face][level];
+	}
+}
diff --git a/src/D3D8/Direct3DCubeTexture8.hpp b/src/D3D8/Direct3DCubeTexture8.hpp
new file mode 100644
index 0000000..9107152
--- /dev/null
+++ b/src/D3D8/Direct3DCubeTexture8.hpp
@@ -0,0 +1,75 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DCubeTexture8_hpp
+#define D3D8_Direct3DCubeTexture8_hpp
+
+#include "Direct3DBaseTexture8.hpp"
+
+#include "Config.hpp"
+
+#include <d3d8.h>
+
+namespace D3D8
+{
+	class Direct3DSurface8;
+
+	class Direct3DCubeTexture8 : public IDirect3DCubeTexture8, public Direct3DBaseTexture8
+	{
+	public:
+		Direct3DCubeTexture8(Direct3DDevice8 *device, unsigned int edgeLength, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool);
+
+		virtual ~Direct3DCubeTexture8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3DResource8 methods
+		long __stdcall FreePrivateData(const GUID &guid);
+		long __stdcall GetPrivateData(const GUID &guid, void *data, unsigned long *size);
+		void __stdcall PreLoad();
+		long __stdcall SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags);
+		long __stdcall GetDevice(IDirect3DDevice8 **device);
+		unsigned long __stdcall SetPriority(unsigned long newPriority);
+		unsigned long __stdcall GetPriority();
+		D3DRESOURCETYPE __stdcall GetType();
+
+		// IDirect3DBaseTexture methods
+		unsigned long __stdcall GetLevelCount();
+		unsigned long __stdcall GetLOD();
+		unsigned long __stdcall SetLOD(unsigned long newLOD);
+
+		// IDirect3DCubeTexture8 methods
+		long __stdcall AddDirtyRect(D3DCUBEMAP_FACES face, const RECT *dirtyRect);
+		long __stdcall GetCubeMapSurface(D3DCUBEMAP_FACES face, unsigned int level , IDirect3DSurface8 **cubeMapSurface);
+		long __stdcall GetLevelDesc(unsigned int level, D3DSURFACE_DESC *description);
+		long __stdcall LockRect(D3DCUBEMAP_FACES face, unsigned int level, D3DLOCKED_RECT *lockedRect, const RECT *rect, unsigned long flags);
+		long __stdcall UnlockRect(D3DCUBEMAP_FACES face, unsigned int level);
+
+		// Internal methods
+		Direct3DSurface8 *getInternalCubeMapSurface(D3DCUBEMAP_FACES face, unsigned int level);
+
+	private:
+		// Creation parameters
+		const unsigned int edgeLength;
+		const D3DFORMAT format;
+		const D3DPOOL pool;
+
+		Direct3DSurface8 *surfaceLevel[6][sw::MIPMAP_LEVELS];
+	};
+}
+
+#endif // D3D8_Direct3D8_hpp
diff --git a/src/D3D8/Direct3DDevice8.cpp b/src/D3D8/Direct3DDevice8.cpp
new file mode 100644
index 0000000..ff85a7a
--- /dev/null
+++ b/src/D3D8/Direct3DDevice8.cpp
@@ -0,0 +1,4926 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DDevice8.hpp"
+
+#include "Direct3D8.hpp"
+#include "Direct3DSurface8.hpp"
+#include "Direct3DIndexBuffer8.hpp"
+#include "Direct3DVertexBuffer8.hpp"
+#include "Direct3DTexture8.hpp"
+#include "Direct3DVolumeTexture8.hpp"
+#include "Direct3DCubeTexture8.hpp"
+#include "Direct3DSwapChain8.hpp"
+#include "Direct3DPixelShader8.hpp"
+#include "Direct3DVertexShader8.hpp"
+#include "Direct3DVolume8.hpp"
+
+#include "Debug.hpp"
+#include "Capabilities.hpp"
+#include "Renderer.hpp"
+#include "Config.hpp"
+#include "FrameBuffer.hpp"
+#include "Clipper.hpp"
+#include "Configurator.hpp"
+#include "Timer.hpp"
+#include "Resource.hpp"
+
+#include <assert.h>
+
+bool localShaderConstants = false;
+
+namespace D3D8
+{
+	inline unsigned long FtoDW(float f)   // FIXME: Deprecate
+	{
+		return (unsigned long&)f;
+	}
+
+	Direct3DDevice8::Direct3DDevice8(const HINSTANCE instance, Direct3D8 *d3d8, unsigned int adapter, D3DDEVTYPE deviceType, HWND focusWindow, unsigned long behaviourFlags, D3DPRESENT_PARAMETERS *presentParameters) : instance(instance), d3d8(d3d8), adapter(adapter), deviceType(deviceType), focusWindow(focusWindow), behaviourFlags(behaviourFlags), presentParameters(*presentParameters)
+	{
+		init = true;
+		recordState = false;
+
+		d3d8->AddRef();
+
+		context = new sw::Context();
+		renderer = new sw::Renderer(context, sw::Direct3D, false);
+
+		swapChain.push_back(0);
+		depthStencil = 0;
+		renderTarget = 0;
+
+		for(int i = 0; i < 8; i++)
+		{
+			texture[i] = 0;
+		}
+
+		cursor = 0;
+		unsigned char one[32 * 32 / sizeof(unsigned char)];
+		memset(one, 0xFFFFFFFF, sizeof(one));
+		unsigned char zero[32 * 32 / sizeof(unsigned char)] = {0};
+		nullCursor = CreateCursor(instance, 0, 0, 32, 32, one, zero);
+		win32Cursor = GetCursor();
+
+		Reset(presentParameters);
+
+		pixelShader.push_back(0);   // pixelShader[0] = 0
+		vertexShader.push_back(0);   // vertexShader[0] = 0
+		vertexShaderHandle = 0;
+		pixelShaderHandle = 0;
+
+		lightsDirty = true;
+				
+		for(int i = 0; i < 16; i++)
+		{
+			dataStream[i] = 0;
+			streamStride[i] = 0;
+		}
+
+		indexData = 0;
+		baseVertexIndex = 0;
+		declaration = 0;
+		FVF = 0;
+
+		D3DMATERIAL8 material;
+
+		material.Diffuse.r = 1.0f;
+		material.Diffuse.g = 1.0f;
+		material.Diffuse.b = 1.0f;
+		material.Diffuse.a = 0.0f;
+		material.Ambient.r = 0.0f;
+		material.Ambient.g = 0.0f;
+		material.Ambient.b = 0.0f;
+		material.Ambient.a = 0.0f;
+		material.Emissive.r = 0.0f;
+		material.Emissive.g = 0.0f;
+		material.Emissive.b = 0.0f;
+		material.Emissive.a = 0.0f;
+		material.Specular.r = 0.0f;
+		material.Specular.g = 0.0f;
+		material.Specular.b = 0.0f;
+		material.Specular.a = 0.0f;
+		material.Power = 0.0f;
+
+		SetMaterial(&material);
+
+		D3DMATRIX identity = {1, 0, 0, 0,
+			                  0, 1, 0, 0,
+							  0, 0, 1, 0,
+							  0, 0, 0, 1};
+
+		SetTransform(D3DTS_VIEW, &identity);
+		SetTransform(D3DTS_PROJECTION, &identity);
+		SetTransform(D3DTS_TEXTURE0, &identity);
+		SetTransform(D3DTS_TEXTURE1, &identity);
+		SetTransform(D3DTS_TEXTURE2, &identity);
+		SetTransform(D3DTS_TEXTURE3, &identity);
+		SetTransform(D3DTS_TEXTURE4, &identity);
+		SetTransform(D3DTS_TEXTURE5, &identity);
+		SetTransform(D3DTS_TEXTURE6, &identity);
+		SetTransform(D3DTS_TEXTURE7, &identity);
+
+		for(int i = 0; i < 12; i++)
+		{
+			SetTransform(D3DTS_WORLDMATRIX(i), &identity);
+		}
+
+		for(int i = 0; i < 8; i++)
+		{
+			float zero[4] = {0, 0, 0, 0};
+
+			SetPixelShaderConstant(i, zero, 1);
+		}
+
+		for(int i = 0; i < 256; i++)
+		{
+			float zero[4] = {0, 0, 0, 0};
+
+			SetVertexShaderConstant(i, zero, 1);
+		}
+
+		init = false;
+
+		if(!(behaviourFlags & D3DCREATE_FPU_PRESERVE))
+		{
+			configureFPU();
+		}
+	}
+
+	Direct3DDevice8::~Direct3DDevice8()
+	{
+		delete renderer;
+		renderer = 0;
+		delete context;
+		context = 0;
+
+		d3d8->Release();
+		d3d8 = 0;
+
+		for(unsigned int i = 0; i < swapChain.size(); i++)
+		{
+			if(swapChain[i])
+			{
+				swapChain[i]->unbind();
+				swapChain[i] = 0;
+			}
+		}
+		
+		if(depthStencil)
+		{
+			depthStencil->unbind();
+			depthStencil = 0;
+		}
+		
+		if(renderTarget)
+		{
+			renderTarget->unbind();
+			renderTarget = 0;
+		}
+
+		for(int i = 0; i < 8; i++)
+		{
+			if(texture[i])
+			{
+				texture[i]->unbind();
+				texture[i] = 0;
+			}
+		}
+
+		for(int i = 0; i < 16; i++)
+		{
+			if(dataStream[i])
+			{
+				dataStream[i]->unbind();
+				dataStream[i] = 0;
+			}
+		}
+
+		if(indexData)
+		{
+			indexData->unbind();
+			indexData = 0;
+		}
+
+		for(unsigned int i = 0; i < pixelShader.size(); i++)
+		{
+			if(pixelShader[i])
+			{
+				pixelShader[i]->unbind();
+				pixelShader[i] = 0;
+			}
+		}
+
+		for(unsigned int i = 0; i < vertexShader.size(); i++)
+		{
+			if(vertexShader[i])
+			{
+				vertexShader[i]->unbind();
+				vertexShader[i] = 0;
+			}
+		}
+
+		for(unsigned int i = 0; i < stateRecorder.size(); i++)
+		{
+			if(stateRecorder[i])
+			{
+				stateRecorder[i]->unbind();
+				stateRecorder[i] = 0;
+			}
+		}
+
+		palette.clear();
+
+		delete cursor;
+		DestroyCursor(nullCursor);
+	}
+
+	long Direct3DDevice8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		if(iid == IID_IDirect3DDevice8 ||
+		   iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DDevice8::AddRef()
+	{
+		TRACE("");
+
+		return Unknown::AddRef();
+	}
+	
+	unsigned long Direct3DDevice8::Release()
+	{
+		TRACE("");
+
+		return Unknown::Release();
+	}
+
+	long Direct3DDevice8::ApplyStateBlock(unsigned long token)
+	{
+		TRACE("");
+
+		stateRecorder[token]->Apply();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::BeginScene()
+	{
+		TRACE("");
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::BeginStateBlock()
+	{
+		TRACE("");
+
+		recordState = true;
+		Direct3DStateBlock8 *stateBlock = new Direct3DStateBlock8(this, (D3DSTATEBLOCKTYPE)0);
+		stateBlock->bind();
+		stateRecorder.push_back(stateBlock);
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CaptureStateBlock(unsigned long token)
+	{
+		TRACE("");
+
+		stateRecorder[token]->Capture();
+		
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::Clear(unsigned long count, const D3DRECT *rects, unsigned long flags, unsigned long color, float z, unsigned long stencil)
+	{
+		TRACE("unsigned long count = %d, const D3DRECT *rects = 0x%0.8p, unsigned long flags = 0x%0.8X, unsigned long color = 0x%0.8X, float z = %f, unsigned long stencil = %d", count, rects, flags, color, z, stencil);
+
+		if(!rects && count != 0)
+		{
+			return INVALIDCALL();
+		}
+
+		if(flags & (D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL) && !depthStencil)
+		{
+			return INVALIDCALL();
+		}
+
+		if(flags & D3DCLEAR_STENCIL)   // Check for stencil component
+		{
+			D3DSURFACE_DESC description;
+			depthStencil->GetDesc(&description);
+
+			switch(description.Format)
+			{
+			case D3DFMT_D15S1:
+			case D3DFMT_D24S8:
+			case D3DFMT_D24X8:
+			case D3DFMT_D24X4S4:
+				break;
+			case D3DFMT_D16_LOCKABLE:
+			case D3DFMT_D32:
+			case D3DFMT_D16:
+				return INVALIDCALL();
+			default:
+				ASSERT(false);
+			}
+		}
+
+		if(!rects)
+		{
+			count = 1;
+
+			D3DRECT rect;
+			rect.x1 = viewport.X;
+			rect.x2 = viewport.X + viewport.Width;
+			rect.y1 = viewport.Y;
+			rect.y2 = viewport.Y + viewport.Height;
+
+			rects = &rect;
+		}
+
+		for(unsigned int i = 0; i < count; i++)
+		{
+			sw::SliceRect clearRect(rects[i].x1, rects[i].y1, rects[i].x2, rects[i].y2, 0);
+
+			clearRect.clip(viewport.X, viewport.Y, viewport.X + viewport.Width, viewport.Y + viewport.Height);
+
+			if(flags & D3DCLEAR_TARGET)
+			{
+				if(renderTarget)
+				{
+					D3DSURFACE_DESC description;
+					renderTarget->GetDesc(&description);
+
+					float rgba[4];
+					rgba[0] = (float)(color & 0x00FF0000) / 0x00FF0000;
+					rgba[1] = (float)(color & 0x0000FF00) / 0x0000FF00;
+					rgba[2] = (float)(color & 0x000000FF) / 0x000000FF;
+					rgba[3] = (float)(color & 0xFF000000) / 0xFF000000;
+
+					renderer->clear(rgba, sw::FORMAT_A32B32G32R32F, renderTarget, clearRect, 0xF);
+				}
+			}
+
+			if(flags & D3DCLEAR_ZBUFFER)
+			{
+				z = sw::clamp01(z);
+				depthStencil->clearDepth(z, clearRect.x0, clearRect.y0, clearRect.width(), clearRect.height());
+			}
+
+			if(flags & D3DCLEAR_STENCIL)
+			{
+				depthStencil->clearStencil(stencil, 0xFF, clearRect.x0, clearRect.y0, clearRect.width(), clearRect.height());
+			}
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CopyRects(IDirect3DSurface8 *sourceSurface, const RECT *sourceRectsArray, unsigned int rects, IDirect3DSurface8 *destinationSurface, const POINT *destPointsArray)
+	{
+		TRACE("");
+
+		if(!sourceSurface || !destinationSurface)
+		{
+			return INVALIDCALL();
+		}
+
+		if(sourceRectsArray && rects == 0 || !sourceRectsArray && rects > 0)
+		{
+			return INVALIDCALL();   // FIXME: Verify REF behaviour
+		}
+
+		D3DSURFACE_DESC sourceDescription;
+		D3DSURFACE_DESC destDescription;
+
+		sourceSurface->GetDesc(&sourceDescription);
+		destinationSurface->GetDesc(&destDescription);
+
+		if(sourceDescription.Format != destDescription.Format)
+		{
+			return INVALIDCALL();
+		}
+
+		int sWidth = sourceDescription.Width;
+		int sHeight = sourceDescription.Height;
+		int dWidth = destDescription.Width;
+		int dHeight = destDescription.Height;
+
+		RECT sRect = {0, 0, sWidth, sHeight};
+		POINT dPoint = {0, 0};
+
+		if(!sourceRectsArray || !destPointsArray)
+		{
+			sourceRectsArray = &sRect;
+			destPointsArray = &dPoint;
+
+			rects = 1;
+		}
+
+		int bpp = 8 * Direct3DSurface8::bytes(sourceDescription.Format);
+
+		for(unsigned int i = 0; i < rects; i++)
+		{
+			const RECT &sRect = sourceRectsArray[i];
+			const POINT &dPoint = destPointsArray[i];
+
+			int rWidth = sRect.right - sRect.left;
+			int rHeight = sRect.bottom - sRect.top;
+
+			RECT dRect;
+
+			dRect.top = dPoint.y;
+			dRect.left = dPoint.x;
+			dRect.bottom = dPoint.y + rHeight;
+			dRect.right = dPoint.x + rWidth;
+
+			D3DLOCKED_RECT sourceLock;
+			D3DLOCKED_RECT destLock;
+
+			sourceSurface->LockRect(&sourceLock, &sRect, D3DLOCK_READONLY);
+			destinationSurface->LockRect(&destLock, &dRect, D3DLOCK_DISCARD);
+
+			for(int y = 0; y < rHeight; y++)
+			{
+				switch(sourceDescription.Format)
+				{
+				case D3DFMT_DXT1:
+				case D3DFMT_DXT2:
+				case D3DFMT_DXT3:
+				case D3DFMT_DXT4:
+				case D3DFMT_DXT5:
+					memcpy(destLock.pBits, sourceLock.pBits, rWidth * bpp / 8);
+					y += 3;   // Advance four lines at once
+					break;
+				default:
+					memcpy(destLock.pBits, sourceLock.pBits, rWidth * bpp / 8);
+				}
+
+				(char*&)sourceLock.pBits += sourceLock.Pitch;
+				(char*&)destLock.pBits += destLock.Pitch;
+			}
+
+			sourceSurface->UnlockRect();
+			destinationSurface->UnlockRect();
+		}
+		
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CreateAdditionalSwapChain(D3DPRESENT_PARAMETERS *presentParameters, IDirect3DSwapChain8 **swapChain)
+	{
+		TRACE("");
+
+		*swapChain = 0;
+
+		if(!presentParameters || !swapChain)
+		{
+			return INVALIDCALL();
+		}
+
+		if(presentParameters->BackBufferCount > 3)
+		{
+			return INVALIDCALL();   // Maximum of three back buffers
+		}
+
+		if(presentParameters->BackBufferCount == 0)
+		{
+			presentParameters->BackBufferCount = 1;
+		}
+
+		D3DPRESENT_PARAMETERS present = *presentParameters;
+
+		*swapChain = new Direct3DSwapChain8(this, &present);
+
+		if(!*swapChain)
+		{
+			return OUTOFMEMORY();
+		}
+
+		if(GetAvailableTextureMem() == 0)
+		{
+			delete *swapChain;
+
+			return OUTOFVIDEOMEMORY();
+		}
+
+		this->swapChain.push_back(static_cast<Direct3DSwapChain8*>(*swapChain));
+
+		(*swapChain)->AddRef();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CreateCubeTexture(unsigned int edgeLength, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **cubeTexture)
+	{
+		TRACE("");
+
+		*cubeTexture = 0;
+
+		if(edgeLength == 0 || d3d8->CheckDeviceFormat(adapter, deviceType, D3DFMT_X8R8G8B8, usage, D3DRTYPE_CUBETEXTURE, format) != D3D_OK)
+		{
+			return INVALIDCALL();
+		}
+
+		*cubeTexture = new Direct3DCubeTexture8(this, edgeLength, levels, usage, format, pool);
+
+		if(!*cubeTexture)
+		{
+			return OUTOFMEMORY();
+		}
+
+		if(GetAvailableTextureMem() == 0)
+		{
+			delete *cubeTexture;
+
+			return OUTOFVIDEOMEMORY();
+		}
+
+		(*cubeTexture)->AddRef();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CreateDepthStencilSurface(unsigned int width, unsigned int height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multiSample, IDirect3DSurface8 **surface)
+	{
+		TRACE("");
+
+		*surface = 0;
+
+		if(width == 0 || height == 0 || d3d8->CheckDeviceFormat(adapter, deviceType, D3DFMT_X8R8G8B8, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, format) != D3D_OK || height > sw::OUTLINE_RESOLUTION)
+		{
+			return INVALIDCALL();
+		}
+
+		*surface = new Direct3DSurface8(this, this, width, height, format, D3DPOOL_DEFAULT, multiSample, format == D3DFMT_D16_LOCKABLE, D3DUSAGE_DEPTHSTENCIL);
+
+		if(!*surface)
+		{
+			return OUTOFMEMORY();
+		}
+
+		if(GetAvailableTextureMem() == 0)
+		{
+			delete *surface;
+
+			return OUTOFVIDEOMEMORY();
+		}
+
+		(*surface)->AddRef();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CreateImageSurface(unsigned int width, unsigned int height, D3DFORMAT format, IDirect3DSurface8 **surface)
+	{
+		TRACE("");
+
+		*surface = 0;
+
+		if(width == 0 || height == 0 || d3d8->CheckDeviceFormat(adapter, deviceType, D3DFMT_X8R8G8B8, 0, D3DRTYPE_SURFACE, format) != D3D_OK)
+		{
+			return INVALIDCALL();
+		}
+
+		*surface = new Direct3DSurface8(this, this, width, height, format, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, true, 0);
+
+		if(!*surface)
+		{
+			return OUTOFMEMORY();
+		}
+
+		if(GetAvailableTextureMem() == 0)
+		{
+			delete *surface;
+
+			return OUTOFVIDEOMEMORY();
+		}
+
+		(*surface)->AddRef();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CreateIndexBuffer(unsigned int length, unsigned long usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **indexBuffer)
+	{
+		TRACE("");
+
+		*indexBuffer = new Direct3DIndexBuffer8(this, length, usage, format, pool);
+
+		if(!*indexBuffer)
+		{
+			return OUTOFMEMORY();
+		}
+
+		if(GetAvailableTextureMem() == 0)
+		{
+			delete *indexBuffer;
+
+			return OUTOFVIDEOMEMORY();
+		}
+
+		(*indexBuffer)->AddRef();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CreatePixelShader(const unsigned long *function, unsigned long *handle)
+	{
+		TRACE("");
+
+		if(!function || !handle || function[0] > pixelShaderVersion)
+		{
+			return INVALIDCALL();
+		}
+
+		unsigned int index;
+
+		for(index = 1; index < pixelShader.size(); index++)   // Skip NULL handle
+		{
+			if(pixelShader[index] == 0)
+			{
+				pixelShader[index] = new Direct3DPixelShader8(this, function);   // FIXME: Check for null
+
+				break;
+			}
+		}
+
+		if(index == pixelShader.size())
+		{
+			pixelShader.push_back(new Direct3DPixelShader8(this, function));
+		}
+
+		pixelShader[index]->AddRef();
+
+		*handle = index;
+		
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CreateRenderTarget(unsigned int width, unsigned int height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multiSample, int lockable, IDirect3DSurface8 **surface)
+	{
+		TRACE("");
+
+		*surface = 0;
+
+		if(width == 0 || height == 0 || d3d8->CheckDeviceFormat(adapter, deviceType, D3DFMT_X8R8G8B8, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, format) != D3D_OK || height > sw::OUTLINE_RESOLUTION)
+		{
+			return INVALIDCALL();
+		}
+
+		*surface = new Direct3DSurface8(this, this, width, height, format, D3DPOOL_DEFAULT, multiSample, lockable != FALSE, D3DUSAGE_RENDERTARGET);
+
+		if(!*surface)
+		{
+			return OUTOFMEMORY();
+		}
+
+		if(GetAvailableTextureMem() == 0)
+		{
+			delete *surface;
+
+			return OUTOFVIDEOMEMORY();
+		}
+
+		(*surface)->AddRef();
+		
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CreateStateBlock(D3DSTATEBLOCKTYPE type, unsigned long *token)
+	{
+		TRACE("");
+
+		if(!token)
+		{
+			return INVALIDCALL();
+		}
+
+		Direct3DStateBlock8 *stateBlock = new Direct3DStateBlock8(this, type);
+		stateBlock->bind();
+		stateRecorder.push_back(stateBlock);
+		*token = (unsigned long)(stateRecorder.size() - 1);
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CreateTexture(unsigned int width, unsigned int height, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool, IDirect3DTexture8 **texture)
+	{
+		TRACE("");
+
+		*texture = 0;
+
+		if(width == 0 || height == 0 || d3d8->CheckDeviceFormat(adapter, deviceType, D3DFMT_X8R8G8B8, usage, D3DRTYPE_TEXTURE, format) != D3D_OK)
+		{
+			return INVALIDCALL();
+		}
+
+		*texture = new Direct3DTexture8(this, width, height, levels, usage, format, pool);
+
+		if(!*texture)
+		{
+			return OUTOFMEMORY();
+		}
+
+		if(GetAvailableTextureMem() == 0)
+		{
+			delete *texture;
+
+			return OUTOFVIDEOMEMORY();
+		}
+
+		(*texture)->AddRef();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CreateVertexBuffer(unsigned int length, unsigned long usage, unsigned long FVF, D3DPOOL pool, IDirect3DVertexBuffer8 **vertexBuffer)
+	{
+		TRACE("");
+
+		*vertexBuffer = new Direct3DVertexBuffer8(this, length, usage, FVF, pool);
+
+		if(!*vertexBuffer)
+		{
+			return OUTOFMEMORY();
+		}
+
+		if(GetAvailableTextureMem() == 0)
+		{
+			delete *vertexBuffer;
+
+			return OUTOFVIDEOMEMORY();
+		}
+
+		(*vertexBuffer)->AddRef();
+		
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CreateVertexShader(const unsigned long *declaration, const unsigned long *function, unsigned long *handle, unsigned long usage)
+	{
+		TRACE("const unsigned long *declaration = 0x%0.8p, const unsigned long *function = 0x%0.8p, unsigned long *handle = 0x%0.8p, unsigned long usage = %d", declaration, function, handle, usage);
+
+		if(!declaration || !handle || (function && function[0] > vertexShaderVersion))
+		{
+			return INVALIDCALL();
+		}
+
+		unsigned int index;
+
+		for(index = 1; index < vertexShader.size(); index++)   // NOTE: skip NULL handle
+		{
+			if(vertexShader[index] == 0)
+			{
+				vertexShader[index] = new Direct3DVertexShader8(this, declaration, function);   // FIXME: Check for null
+
+				break;
+			}
+		}
+
+		if(index == vertexShader.size())
+		{
+			vertexShader.push_back(new Direct3DVertexShader8(this, declaration, function));
+		}
+
+		vertexShader[index]->AddRef();
+
+		*handle = (index << 16) + 1;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::CreateVolumeTexture(unsigned int width, unsigned int height, unsigned int depth, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool, IDirect3DVolumeTexture8 **volumeTexture)
+	{
+		TRACE("");
+
+		*volumeTexture = 0;
+
+		if(width == 0 || height == 0 || depth == 0 || d3d8->CheckDeviceFormat(adapter, deviceType, D3DFMT_X8R8G8B8, usage, D3DRTYPE_VOLUMETEXTURE, format) != D3D_OK)
+		{
+			return INVALIDCALL();
+		}
+
+		*volumeTexture = new Direct3DVolumeTexture8(this, width, height, depth, levels, usage, format, pool);
+
+		if(!*volumeTexture)
+		{
+			return OUTOFMEMORY();
+		}
+
+		if(GetAvailableTextureMem() == 0)
+		{
+			delete *volumeTexture;
+
+			return OUTOFVIDEOMEMORY();
+		}
+
+		(*volumeTexture)->AddRef();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::DeletePatch(unsigned int handle)
+	{
+		TRACE("");
+
+		UNIMPLEMENTED();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::DeleteStateBlock(unsigned long token)
+	{
+		TRACE("");
+
+		if(token >= stateRecorder.size() || !stateRecorder[token])
+		{
+			return INVALIDCALL();
+		}
+
+		stateRecorder[token]->unbind();
+		stateRecorder[token] = 0;
+		
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::DeleteVertexShader(unsigned long handle)
+	{
+		TRACE("");
+
+		unsigned int index = handle >> 16;
+
+		if(index >= vertexShader.size() || !vertexShader[index])
+		{
+			return INVALIDCALL();
+		}
+
+		vertexShader[index]->Release();
+		vertexShader[index] = 0;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::DrawIndexedPrimitive(D3DPRIMITIVETYPE type, unsigned int minIndex, unsigned int numVertices, unsigned int startIndex, unsigned int primitiveCount)
+	{
+		TRACE("");
+
+		if(!indexData)
+		{
+			return INVALIDCALL();
+		}
+
+		if(!bindData(indexData, baseVertexIndex) || !primitiveCount)
+		{
+			return D3D_OK;
+		}
+
+		unsigned int indexOffset = startIndex * (indexData->is32Bit() ? 4 : 2);   // FIXME: Doesn't take stream frequencies into account
+
+		sw::DrawType drawType;
+
+		if(indexData->is32Bit())
+		{
+			switch(type)
+			{
+			case D3DPT_POINTLIST:		drawType = sw::DRAW_INDEXEDPOINTLIST32;		break;
+			case D3DPT_LINELIST:		drawType = sw::DRAW_INDEXEDLINELIST32;			break;
+			case D3DPT_LINESTRIP:		drawType = sw::DRAW_INDEXEDLINESTRIP32;		break;
+			case D3DPT_TRIANGLELIST:	drawType = sw::DRAW_INDEXEDTRIANGLELIST32;		break;
+			case D3DPT_TRIANGLESTRIP:	drawType = sw::DRAW_INDEXEDTRIANGLESTRIP32;	break;
+			case D3DPT_TRIANGLEFAN:		drawType = sw::DRAW_INDEXEDTRIANGLEFAN32;		break;
+			default:
+				ASSERT(false);
+			}
+		}
+		else
+		{
+			switch(type)
+			{
+			case D3DPT_POINTLIST:		drawType = sw::DRAW_INDEXEDPOINTLIST16;		break;
+			case D3DPT_LINELIST:		drawType = sw::DRAW_INDEXEDLINELIST16;			break;
+			case D3DPT_LINESTRIP:		drawType = sw::DRAW_INDEXEDLINESTRIP16;		break;
+			case D3DPT_TRIANGLELIST:	drawType = sw::DRAW_INDEXEDTRIANGLELIST16;		break;
+			case D3DPT_TRIANGLESTRIP:	drawType = sw::DRAW_INDEXEDTRIANGLESTRIP16;	break;
+			case D3DPT_TRIANGLEFAN:		drawType = sw::DRAW_INDEXEDTRIANGLEFAN16;		break;
+			default:
+				ASSERT(false);
+			}
+		}
+
+		bindData(indexData, baseVertexIndex);
+
+		renderer->draw(drawType, indexOffset, primitiveCount);
+		
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::DeletePixelShader(unsigned long handle)
+	{
+		TRACE("");
+
+		if(handle >= pixelShader.size() || !pixelShader[handle])
+		{
+			return INVALIDCALL();
+		}
+
+		pixelShader[handle]->Release();
+		pixelShader[handle] = 0;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::DrawIndexedPrimitiveUP(D3DPRIMITIVETYPE type, unsigned int minIndex, unsigned int numVertices, unsigned int primitiveCount, const void *indexData, D3DFORMAT indexDataFormat, const void *vertexStreamZeroData, unsigned int vertexStreamZeroStride)
+	{
+		TRACE("");
+
+		if(!vertexStreamZeroData || !indexData)
+		{
+			return INVALIDCALL();
+		}
+
+		int length = (minIndex + numVertices) * vertexStreamZeroStride;
+		
+		Direct3DVertexBuffer8 *vertexBuffer = new Direct3DVertexBuffer8(this, length, 0, 0, D3DPOOL_DEFAULT);
+
+		unsigned char *data;
+		vertexBuffer->Lock(0, 0, &data, 0);
+		memcpy(data, vertexStreamZeroData, length);
+		vertexBuffer->Unlock();
+
+		SetStreamSource(0, vertexBuffer, vertexStreamZeroStride);
+
+		switch(type)
+		{
+		case D3DPT_POINTLIST:		length = primitiveCount;		break;
+		case D3DPT_LINELIST:		length = primitiveCount * 2;	break;
+		case D3DPT_LINESTRIP:		length = primitiveCount + 1;	break;
+		case D3DPT_TRIANGLELIST:	length = primitiveCount * 3;	break;
+		case D3DPT_TRIANGLESTRIP:	length = primitiveCount + 2;	break;
+		case D3DPT_TRIANGLEFAN:		length = primitiveCount + 2;	break;
+		default:
+			ASSERT(false);
+		}
+
+		length *= indexDataFormat == D3DFMT_INDEX32 ? 4 : 2;
+
+		Direct3DIndexBuffer8 *indexBuffer = new Direct3DIndexBuffer8(this, length, 0, indexDataFormat, D3DPOOL_DEFAULT);
+
+		indexBuffer->Lock(0, 0, &data, 0);
+		memcpy(data, indexData, length);
+		indexBuffer->Unlock();
+
+		SetIndices(indexBuffer, 0);
+
+		if(!bindData(indexBuffer, 0) || !primitiveCount)
+		{
+			vertexBuffer->Release();
+
+			return D3D_OK;
+		}
+
+		sw::DrawType drawType;
+
+		if(indexDataFormat == D3DFMT_INDEX32)
+		{
+			switch(type)
+			{
+			case D3DPT_POINTLIST:		drawType = sw::DRAW_INDEXEDPOINTLIST32;		break;
+			case D3DPT_LINELIST:		drawType = sw::DRAW_INDEXEDLINELIST32;			break;
+			case D3DPT_LINESTRIP:		drawType = sw::DRAW_INDEXEDLINESTRIP32;		break;
+			case D3DPT_TRIANGLELIST:	drawType = sw::DRAW_INDEXEDTRIANGLELIST32;		break;
+			case D3DPT_TRIANGLESTRIP:	drawType = sw::DRAW_INDEXEDTRIANGLESTRIP32;	break;
+			case D3DPT_TRIANGLEFAN:		drawType = sw::DRAW_INDEXEDTRIANGLEFAN32;		break;
+			default:
+				ASSERT(false);
+			}
+		}
+		else
+		{
+			switch(type)
+			{
+			case D3DPT_POINTLIST:		drawType = sw::DRAW_INDEXEDPOINTLIST16;		break;
+			case D3DPT_LINELIST:		drawType = sw::DRAW_INDEXEDLINELIST16;			break;
+			case D3DPT_LINESTRIP:		drawType = sw::DRAW_INDEXEDLINESTRIP16;		break;
+			case D3DPT_TRIANGLELIST:	drawType = sw::DRAW_INDEXEDTRIANGLELIST16;		break;
+			case D3DPT_TRIANGLESTRIP:	drawType = sw::DRAW_INDEXEDTRIANGLESTRIP16;	break;
+			case D3DPT_TRIANGLEFAN:		drawType = sw::DRAW_INDEXEDTRIANGLEFAN16;		break;
+			default:
+				ASSERT(false);
+			}
+		}
+
+		renderer->draw(drawType, 0, primitiveCount);
+
+		SetStreamSource(0, 0, 0);
+		SetIndices(0, 0);
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::DrawPrimitive(D3DPRIMITIVETYPE primitiveType, unsigned int startVertex, unsigned int primitiveCount)
+	{
+		TRACE("");
+
+		if(!bindData(0, startVertex) || !primitiveCount)
+		{
+			return D3D_OK;
+		}
+
+		sw::DrawType drawType;
+
+		switch(primitiveType)
+		{
+		case D3DPT_POINTLIST:		drawType = sw::DRAW_POINTLIST;		break;
+		case D3DPT_LINELIST:		drawType = sw::DRAW_LINELIST;		break;
+		case D3DPT_LINESTRIP:		drawType = sw::DRAW_LINESTRIP;		break;
+		case D3DPT_TRIANGLELIST:	drawType = sw::DRAW_TRIANGLELIST;	break;
+		case D3DPT_TRIANGLESTRIP:	drawType = sw::DRAW_TRIANGLESTRIP;	break;
+		case D3DPT_TRIANGLEFAN:		drawType = sw::DRAW_TRIANGLEFAN;	break;
+		default:
+			ASSERT(false);
+		}
+
+		renderer->draw(drawType, 0, primitiveCount);
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::DrawPrimitiveUP(D3DPRIMITIVETYPE primitiveType, unsigned int primitiveCount, const void *vertexStreamZeroData, unsigned int vertexStreamZeroStride)
+	{
+		TRACE("");
+
+		if(!vertexStreamZeroData)
+		{
+			return INVALIDCALL();
+		}
+
+		IDirect3DVertexBuffer8 *vertexBuffer = 0;
+		int length = 0;
+
+		switch(primitiveType)
+		{
+		case D3DPT_POINTLIST:		length = primitiveCount;		break;
+		case D3DPT_LINELIST:		length = primitiveCount * 2;	break;
+		case D3DPT_LINESTRIP:		length = primitiveCount + 1;	break;
+		case D3DPT_TRIANGLELIST:	length = primitiveCount * 3;	break;
+		case D3DPT_TRIANGLESTRIP:	length = primitiveCount + 2;	break;
+		case D3DPT_TRIANGLEFAN:		length = primitiveCount + 2;	break;
+		default:
+			ASSERT(false);
+		}
+
+		length *= vertexStreamZeroStride;
+
+		CreateVertexBuffer(length, 0, 0, D3DPOOL_DEFAULT, &vertexBuffer);
+		
+		unsigned char *data;
+		vertexBuffer->Lock(0, 0, &data, 0);
+		memcpy(data, vertexStreamZeroData, length);
+		vertexBuffer->Unlock();
+
+		SetStreamSource(0, vertexBuffer, vertexStreamZeroStride);
+
+		if(!bindData(0, 0) || !primitiveCount)
+		{
+			vertexBuffer->Release();
+
+			return D3D_OK;
+		}
+
+		sw::DrawType drawType;
+
+		switch(primitiveType)
+		{
+		case D3DPT_POINTLIST:		drawType = sw::DRAW_POINTLIST;		break;
+		case D3DPT_LINELIST:		drawType = sw::DRAW_LINELIST;		break;
+		case D3DPT_LINESTRIP:		drawType = sw::DRAW_LINESTRIP;		break;
+		case D3DPT_TRIANGLELIST:	drawType = sw::DRAW_TRIANGLELIST;	break;
+		case D3DPT_TRIANGLESTRIP:	drawType = sw::DRAW_TRIANGLESTRIP;	break;
+		case D3DPT_TRIANGLEFAN:		drawType = sw::DRAW_TRIANGLEFAN;	break;
+		default:
+			ASSERT(false);
+		}
+
+		renderer->draw(drawType, 0, primitiveCount);
+
+		SetStreamSource(0, 0, 0);
+		vertexBuffer->Release();
+		
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::DrawRectPatch(unsigned int handle, const float *numSegs, const D3DRECTPATCH_INFO *rectPatchInfo)
+	{
+		TRACE("");
+
+		if(!numSegs || !rectPatchInfo)
+		{
+			return INVALIDCALL();
+		}
+
+		UNIMPLEMENTED();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::DrawTriPatch(unsigned int handle, const float *numSegs, const D3DTRIPATCH_INFO *triPatchInfo)
+	{
+		TRACE("");
+
+		if(!numSegs || !triPatchInfo)
+		{
+			return INVALIDCALL();
+		}
+
+		UNIMPLEMENTED();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::EndScene()
+	{
+		TRACE("");
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::EndStateBlock(unsigned long *token)
+	{
+		TRACE("");
+
+		if(!token)
+		{
+			return INVALIDCALL();
+		}
+
+		recordState = false;
+		*token = (unsigned long)(stateRecorder.size() - 1);
+
+		return D3D_OK;
+	}
+
+	unsigned int Direct3DDevice8::GetAvailableTextureMem()
+	{
+		TRACE("");
+
+		int availableMemory = textureMemory - Direct3DResource8::getMemoryUsage();
+		if(availableMemory < 0) availableMemory = 0;
+
+		// Round to nearest MB
+		return (availableMemory + 0x80000) & 0xFFF00000;
+	}
+
+	long Direct3DDevice8::GetBackBuffer(unsigned int index, D3DBACKBUFFER_TYPE type, IDirect3DSurface8 **backBuffer)
+	{
+		TRACE("");
+
+		if(!backBuffer/* || type != D3DBACKBUFFER_TYPE_MONO*/)
+		{
+			return INVALIDCALL();
+		}
+
+		swapChain[index]->GetBackBuffer(index, type, backBuffer);
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetClipPlane(unsigned long index, float *plane)
+	{
+		TRACE("");
+
+		if(!plane || index >= 6)
+		{
+			return INVALIDCALL();
+		}
+
+		plane[0] = this->plane[index][0];
+		plane[1] = this->plane[index][1];
+		plane[2] = this->plane[index][2];
+		plane[3] = this->plane[index][3];
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetClipStatus(D3DCLIPSTATUS8 *clipStatus)
+	{
+		TRACE("");
+
+		if(!clipStatus)
+		{
+			return INVALIDCALL();
+		}
+
+		*clipStatus = this->clipStatus;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetCreationParameters(D3DDEVICE_CREATION_PARAMETERS *parameters)
+	{
+		TRACE("");
+
+		if(!parameters)
+		{
+			return INVALIDCALL();
+		}
+
+		parameters->AdapterOrdinal = adapter;
+		parameters->BehaviorFlags = behaviourFlags;
+		parameters->DeviceType = deviceType;
+		parameters->hFocusWindow = focusWindow;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetCurrentTexturePalette(unsigned int *paletteNumber)
+	{
+		TRACE("");
+
+		if(!paletteNumber)
+		{
+			return INVALIDCALL();
+		}
+
+		*paletteNumber = currentPalette;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetDepthStencilSurface(IDirect3DSurface8 **depthStencilSurface)
+	{
+		TRACE("");
+
+		if(!depthStencilSurface)
+		{
+			return INVALIDCALL();
+		}
+
+		*depthStencilSurface = depthStencil;
+
+		if(depthStencil)
+		{
+			depthStencil->AddRef();
+		}
+
+		return D3D_OK;   // FIXME: Return NOTFOUND() when no depthStencil?
+	}
+
+	long Direct3DDevice8::GetDeviceCaps(D3DCAPS8 *caps)
+	{
+		TRACE("");
+
+		return d3d8->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, caps);
+	}
+
+	long Direct3DDevice8::GetDirect3D(IDirect3D8 **d3d8)
+	{
+		TRACE("");
+
+		if(!d3d8)
+		{
+			return INVALIDCALL();
+		}
+
+		ASSERT(this->d3d8);
+
+		*d3d8 = this->d3d8;
+		this->d3d8->AddRef();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetDisplayMode(D3DDISPLAYMODE *mode)
+	{
+		TRACE("");
+
+		if(!mode)
+		{
+			return INVALIDCALL();
+		}
+
+		d3d8->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, mode);
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetFrontBuffer(IDirect3DSurface8 *destSurface)
+	{
+		TRACE("");
+
+		if(!destSurface)
+		{
+			return INVALIDCALL();
+		}
+
+		D3DLOCKED_RECT description;
+		destSurface->LockRect(&description, 0, 0);
+
+		swapChain[0]->screenshot(description.pBits);
+
+		destSurface->UnlockRect();
+		
+		return D3D_OK;
+	}
+
+	void Direct3DDevice8::GetGammaRamp(D3DGAMMARAMP *ramp)
+	{
+		TRACE("");
+
+		if(!ramp)
+		{
+			return;
+		}
+
+		swapChain[0]->getGammaRamp((sw::GammaRamp*)ramp);
+	}
+
+	long Direct3DDevice8::GetIndices(IDirect3DIndexBuffer8 **indexData, unsigned int *baseVertexIndex)
+	{
+		TRACE("");
+
+		if(!indexData || !baseVertexIndex)
+		{
+			return INVALIDCALL();
+		}
+
+		*indexData = this->indexData;
+
+		if(this->indexData)
+		{
+			this->indexData->AddRef();
+		}
+
+		*baseVertexIndex = this->baseVertexIndex;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetInfo(unsigned long devInfoID, void *devInfoStruct, unsigned long devInfoStructSize)
+	{
+		TRACE("");
+
+		if(!devInfoStruct || devInfoStructSize == 0)
+		{
+			return INVALIDCALL();
+		}
+
+		switch(devInfoID)
+		{
+		case 0: return E_FAIL;
+		case 1: return E_FAIL;
+		case 2: return E_FAIL;
+		case 3: return E_FAIL;
+		case 4: return S_FALSE;
+		case 5: UNIMPLEMENTED();   // FIXME: D3DDEVINFOID_RESOURCEMANAGER
+		case 6: UNIMPLEMENTED();   // FIXME: D3DDEVINFOID_D3DVERTEXSTATS
+		case 7: return E_FAIL;
+		}
+		
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetLight(unsigned long index, D3DLIGHT8 *light)
+	{
+		TRACE("");
+
+		if(!light)
+		{
+			return INVALIDCALL();
+		}
+
+		if(!this->light.exists(index))
+		{
+			return INVALIDCALL();
+		}
+
+		*light = this->light[index];
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetLightEnable(unsigned long index , int *enable)
+	{
+		TRACE("");
+
+		if(!enable)
+		{
+			return INVALIDCALL();
+		}
+
+		if(!light.exists(index))
+		{
+			return INVALIDCALL();
+		}
+
+		*enable = light[index].enable;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetMaterial(D3DMATERIAL8 *material)
+	{
+		TRACE("");
+
+		if(!material)
+		{
+			return INVALIDCALL();
+		}
+
+		*material = this->material;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetPaletteEntries(unsigned int paletteNumber, PALETTEENTRY *entries)
+	{
+		TRACE("");
+
+		if(paletteNumber > 0xFFFF || !entries)
+		{
+			return INVALIDCALL();
+		}
+
+		for(int i = 0; i < 256; i++)
+		{
+			entries[i] = palette[paletteNumber].entry[i];
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetPixelShader(unsigned long *handle)
+	{
+		TRACE("");
+
+		if(!handle)
+		{
+			return INVALIDCALL();
+		}
+
+		*handle = pixelShaderHandle;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetPixelShaderFunction(unsigned long handle, void *data, unsigned long *size)
+	{
+		TRACE("");
+
+		if(!data)
+		{
+			return INVALIDCALL();
+		}
+
+		UNIMPLEMENTED();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetPixelShaderConstant(unsigned long startRegister, void *constantData, unsigned long count)
+	{
+		TRACE("");
+
+		if(!constantData)
+		{
+			return INVALIDCALL();
+		}
+
+		for(unsigned int i = 0; i < count; i++)
+		{
+			((float*)constantData)[i * 4 + 0] = pixelShaderConstant[startRegister + i][0];
+			((float*)constantData)[i * 4 + 1] = pixelShaderConstant[startRegister + i][1];
+			((float*)constantData)[i * 4 + 2] = pixelShaderConstant[startRegister + i][2];
+			((float*)constantData)[i * 4 + 3] = pixelShaderConstant[startRegister + i][3];
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetRasterStatus(D3DRASTER_STATUS *rasterStatus)
+	{
+		TRACE("");
+
+		if(!rasterStatus)
+		{
+			return INVALIDCALL();
+		}
+
+		UNIMPLEMENTED();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetRenderState(D3DRENDERSTATETYPE state, unsigned long *value)
+	{
+		TRACE("");
+
+		if(!value)
+		{
+			return INVALIDCALL();
+		}
+
+		*value = renderState[state];
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetRenderTarget(IDirect3DSurface8 **renderTarget)
+	{
+		TRACE("");
+
+		if(!renderTarget)
+		{
+			return INVALIDCALL();
+		}
+
+		*renderTarget = this->renderTarget;
+		this->renderTarget->AddRef();
+		
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetStreamSource(unsigned int streamNumber, IDirect3DVertexBuffer8 **streamData, unsigned int *stride)
+	{
+		TRACE("");
+
+		if(streamNumber >= 16 || !streamData || !stride)
+		{
+			return INVALIDCALL();
+		}
+
+		*streamData = dataStream[streamNumber];
+		
+		if(dataStream[streamNumber])
+		{
+			dataStream[streamNumber]->AddRef();
+		}
+
+		*stride = 0;   // NOTE: Unimplemented
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetTexture(unsigned long stage, IDirect3DBaseTexture8 **texture)
+	{
+		TRACE("");
+
+		if(!texture || stage >= 8)
+		{
+			return INVALIDCALL();
+		}
+
+		*texture = this->texture[stage];
+		
+		if(this->texture[stage])
+		{
+			this->texture[stage]->AddRef();
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetTextureStageState(unsigned long stage, D3DTEXTURESTAGESTATETYPE state, unsigned long *value)
+	{
+		TRACE("");
+
+		if(!value  || stage < 0 || stage >= 8 || state < 0 || state > D3DTSS_RESULTARG)   // FIXME: Set *value to 0?
+		{
+			return INVALIDCALL();
+		}
+
+		*value = textureStageState[stage][state];
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetTransform(D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
+	{
+		TRACE("");
+
+		if(!matrix || state < 0 || state > 511)
+		{
+			return INVALIDCALL();
+		}
+
+		*matrix = this->matrix[state];
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetVertexShader(unsigned long *handle)
+	{
+		TRACE("");
+
+		if(!handle)
+		{
+			return INVALIDCALL();
+		}
+
+		*handle = vertexShaderHandle;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetVertexShaderConstant(unsigned long startRegister, void *constantData, unsigned long count)
+	{
+		TRACE("");
+
+		if(!constantData)
+		{
+			return INVALIDCALL();
+		}
+
+		for(unsigned int i = 0; i < count; i++)
+		{
+			((float*)constantData)[i * 4 + 0] = vertexShaderConstant[startRegister + i][0];
+			((float*)constantData)[i * 4 + 1] = vertexShaderConstant[startRegister + i][1];
+			((float*)constantData)[i * 4 + 2] = vertexShaderConstant[startRegister + i][2];
+			((float*)constantData)[i * 4 + 3] = vertexShaderConstant[startRegister + i][3];
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetVertexShaderDeclaration(unsigned long handle, void *data, unsigned long *size)
+	{
+		TRACE("");
+
+		if(!data || !size)
+		{
+			return INVALIDCALL();
+		}
+
+		UNIMPLEMENTED();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetVertexShaderFunction(unsigned long handle, void *data, unsigned long *size)
+	{
+		TRACE("");
+
+		if(!data || !size)
+		{
+			return INVALIDCALL();
+		}
+
+		UNIMPLEMENTED();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::GetViewport(D3DVIEWPORT8 *viewport)
+	{
+		TRACE("");
+
+		if(!viewport)
+		{
+			return INVALIDCALL();
+		}
+
+		*viewport = this->viewport;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::LightEnable(unsigned long index, int enable)
+	{
+		TRACE("");
+
+		if(!recordState)
+		{
+			if(!light.exists(index))   // Insert default light
+			{
+				D3DLIGHT8 light;
+
+				light.Type = D3DLIGHT_DIRECTIONAL;
+				light.Diffuse.r = 1;
+				light.Diffuse.g = 1;
+				light.Diffuse.b = 1;
+				light.Diffuse.a = 0;
+				light.Specular.r = 0;
+				light.Specular.g = 0;
+				light.Specular.b = 0;
+				light.Specular.a = 0;
+				light.Ambient.r = 0;
+				light.Ambient.g = 0;
+				light.Ambient.b = 0;
+				light.Ambient.a = 0;
+				light.Position.x = 0;
+				light.Position.y = 0;
+				light.Position.z = 0;
+				light.Direction.x = 0;
+				light.Direction.y = 0;
+				light.Direction.z = 1;
+				light.Range = 0;
+				light.Falloff = 0; 
+				light.Attenuation0 = 0;
+				light.Attenuation1 = 0;
+				light.Attenuation2 = 0;
+				light.Theta = 0;
+				light.Phi = 0;
+
+				SetLight(index, &light);
+			}
+
+			light[index].enable = (enable != FALSE);
+
+			lightsDirty = true;
+		}
+		else
+		{
+			stateRecorder.back()->lightEnable(index, enable);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::MultiplyTransform(D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
+	{
+		TRACE("");
+
+		if(!matrix)
+		{
+			return INVALIDCALL();
+		}
+
+		D3DMATRIX *current = &this->matrix[state];
+
+		sw::Matrix C(current->_11, current->_21, current->_31, current->_41,
+		             current->_12, current->_22, current->_32, current->_42,
+		             current->_13, current->_23, current->_33, current->_43,
+		             current->_14, current->_24, current->_34, current->_44);
+
+		sw::Matrix M(matrix->_11, matrix->_21, matrix->_31, matrix->_41,
+		             matrix->_12, matrix->_22, matrix->_32, matrix->_42,
+		             matrix->_13, matrix->_23, matrix->_33, matrix->_43,
+		             matrix->_14, matrix->_24, matrix->_34, matrix->_44);
+
+		switch(state)
+		{
+		case D3DTS_WORLD:
+			renderer->setModelMatrix(C * M);
+			break;
+		case D3DTS_VIEW:
+			renderer->setViewMatrix(C * M);
+			break;
+		case D3DTS_PROJECTION:
+			renderer->setProjectionMatrix(C * M);
+			break;
+		case D3DTS_TEXTURE0:
+			renderer->setTextureMatrix(0, C * M);
+			break;
+		case D3DTS_TEXTURE1:
+			renderer->setTextureMatrix(1, C * M);
+			break;
+		case D3DTS_TEXTURE2:
+			renderer->setTextureMatrix(2, C * M);
+			break;
+		case D3DTS_TEXTURE3:
+			renderer->setTextureMatrix(3, C * M);
+			break;
+		case D3DTS_TEXTURE4:
+			renderer->setTextureMatrix(4, C * M);
+			break;
+		case D3DTS_TEXTURE5:
+			renderer->setTextureMatrix(5, C * M);
+			break;
+		case D3DTS_TEXTURE6:
+			renderer->setTextureMatrix(6, C * M);
+			break;
+		case D3DTS_TEXTURE7:
+			renderer->setTextureMatrix(7, C * M);
+			break;
+		default:
+			if(state > 256 && state < 512)
+			{
+				renderer->setModelMatrix(C * M, state - 256);
+			}
+			else ASSERT(false);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::Present(const RECT *sourceRect, const RECT *destRect, HWND destWindowOverride, const RGNDATA *dirtyRegion)
+	{
+		TRACE("");
+
+		// NOTE: sourceRect and destRect can be null, dirtyRegion has to be null
+
+		HWND windowHandle = presentParameters.hDeviceWindow ? presentParameters.hDeviceWindow : focusWindow;
+
+		if(destWindowOverride && destWindowOverride != windowHandle)
+		{
+			UNIMPLEMENTED();
+		}
+
+		if(dirtyRegion)
+		{
+			return INVALIDCALL();
+		}
+
+		swapChain[0]->Present(sourceRect, destRect, destWindowOverride, dirtyRegion);
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::ProcessVertices(unsigned int srcStartIndex, unsigned int destIndex, unsigned int vertexCount, IDirect3DVertexBuffer8 *destBuffer, unsigned long flags)
+	{
+		TRACE("");
+
+		if(!destBuffer)
+		{
+			return INVALIDCALL();
+		}
+
+		UNIMPLEMENTED();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::Reset(D3DPRESENT_PARAMETERS *presentParameters)
+	{
+		TRACE("");
+
+		if(!presentParameters)
+		{
+			return INVALIDCALL();
+		}
+
+		if(swapChain[0])
+		{
+			swapChain[0]->unbind();
+			swapChain[0] = 0;
+		}
+		
+		if(depthStencil)
+		{
+			depthStencil->unbind();
+			depthStencil = 0;
+		}
+		
+		if(renderTarget)
+		{
+			renderTarget->unbind();
+			renderTarget = 0;
+		}
+
+		D3DPRESENT_PARAMETERS present = *presentParameters;
+
+		if(!swapChain[0])
+		{
+			swapChain[0] = new Direct3DSwapChain8(this, &present);
+			swapChain[0]->bind();
+		}
+		else
+		{
+			swapChain[0]->reset(&present);
+		}
+
+		HWND windowHandle = presentParameters->hDeviceWindow ? presentParameters->hDeviceWindow : focusWindow;
+
+		int width = 0;
+		int height = 0;
+
+		if(presentParameters->Windowed && (presentParameters->BackBufferHeight == 0 || presentParameters->BackBufferWidth == 0))
+		{
+			RECT rectangle;
+			GetClientRect(windowHandle, &rectangle);
+
+			width = rectangle.right - rectangle.left;
+			height = rectangle.bottom - rectangle.top;
+		}
+		else
+		{
+			width = presentParameters->BackBufferWidth;
+			height = presentParameters->BackBufferHeight;
+		}
+
+		if(presentParameters->EnableAutoDepthStencil != FALSE)
+		{
+			depthStencil = new Direct3DSurface8(this, this, width, height, presentParameters->AutoDepthStencilFormat, D3DPOOL_DEFAULT, presentParameters->MultiSampleType, presentParameters->AutoDepthStencilFormat == D3DFMT_D16_LOCKABLE, D3DUSAGE_DEPTHSTENCIL);
+			depthStencil->bind();
+		}
+
+		IDirect3DSurface8 *renderTarget;
+		swapChain[0]->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &renderTarget);
+		SetRenderTarget(renderTarget, depthStencil);
+		renderTarget->Release();
+
+		SetRenderState(D3DRS_ZENABLE, presentParameters->EnableAutoDepthStencil != FALSE ? D3DZB_TRUE : D3DZB_FALSE);
+		SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
+		SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
+		SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
+		SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
+		SetRenderState(D3DRS_LASTPIXEL, TRUE);
+		SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
+		SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
+		SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
+		SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
+		SetRenderState(D3DRS_ALPHAREF, 0);
+		SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_ALWAYS);
+		SetRenderState(D3DRS_DITHERENABLE, FALSE);
+		SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
+		SetRenderState(D3DRS_FOGENABLE, FALSE);
+		SetRenderState(D3DRS_SPECULARENABLE, FALSE);
+	//	SetRenderState(D3DRS_ZVISIBLE, 0);
+		SetRenderState(D3DRS_FOGCOLOR, 0);
+		SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_NONE);
+		SetRenderState(D3DRS_FOGSTART, FtoDW(0.0f));
+		SetRenderState(D3DRS_FOGEND, FtoDW(1.0f));
+		SetRenderState(D3DRS_FOGDENSITY, FtoDW(1.0f));
+		SetRenderState(D3DRS_EDGEANTIALIAS, FALSE);
+		SetRenderState(D3DRS_RANGEFOGENABLE, FALSE);
+		SetRenderState(D3DRS_ZBIAS, 0);
+		SetRenderState(D3DRS_STENCILENABLE, FALSE);
+		SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
+		SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
+		SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);
+		SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
+		SetRenderState(D3DRS_STENCILREF, 0);
+		SetRenderState(D3DRS_STENCILMASK, 0xFFFFFFFF);
+		SetRenderState(D3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
+		SetRenderState(D3DRS_TEXTUREFACTOR, 0xFFFFFFFF);
+		SetRenderState(D3DRS_WRAP0, 0);
+		SetRenderState(D3DRS_WRAP1, 0);
+		SetRenderState(D3DRS_WRAP2, 0);
+		SetRenderState(D3DRS_WRAP3, 0);
+		SetRenderState(D3DRS_WRAP4, 0);
+		SetRenderState(D3DRS_WRAP5, 0);
+		SetRenderState(D3DRS_WRAP6, 0);
+		SetRenderState(D3DRS_WRAP7, 0);
+		SetRenderState(D3DRS_CLIPPING, TRUE);
+		SetRenderState(D3DRS_LIGHTING, TRUE);
+		SetRenderState(D3DRS_AMBIENT, 0);
+		SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
+		SetRenderState(D3DRS_COLORVERTEX, TRUE);
+		SetRenderState(D3DRS_LOCALVIEWER, TRUE);
+		SetRenderState(D3DRS_NORMALIZENORMALS, FALSE);
+		SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
+		SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_COLOR2);
+		SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL);
+		SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
+		SetRenderState(D3DRS_VERTEXBLEND, D3DVBF_DISABLE);
+		SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
+		SetRenderState(D3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
+		SetRenderState(D3DRS_POINTSIZE, FtoDW(1.0f));
+		SetRenderState(D3DRS_POINTSIZE_MIN, FtoDW(0.0f));
+		SetRenderState(D3DRS_POINTSPRITEENABLE, FALSE);
+		SetRenderState(D3DRS_POINTSCALEENABLE, FALSE);
+		SetRenderState(D3DRS_POINTSCALE_A, FtoDW(1.0f));
+		SetRenderState(D3DRS_POINTSCALE_B, FtoDW(0.0f));
+		SetRenderState(D3DRS_POINTSCALE_C, FtoDW(0.0f));
+		SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
+		SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
+		SetRenderState(D3DRS_PATCHEDGESTYLE, D3DPATCHEDGE_DISCRETE);
+		SetRenderState(D3DRS_DEBUGMONITORTOKEN, D3DDMT_ENABLE);
+		SetRenderState(D3DRS_POINTSIZE_MAX, FtoDW(64.0f));
+		SetRenderState(D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
+		SetRenderState(D3DRS_COLORWRITEENABLE, 0x0000000F);
+		SetRenderState(D3DRS_TWEENFACTOR, FtoDW(0.0f));
+		SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
+		SetRenderState(D3DRS_POSITIONORDER, D3DORDER_CUBIC);
+		SetRenderState(D3DRS_NORMALORDER, D3DORDER_LINEAR);
+
+		for(int i = 0; i < 8; i++)
+		{
+			SetTexture(i, 0);
+
+			SetTextureStageState(i, D3DTSS_COLOROP, i == 0 ? D3DTOP_MODULATE : D3DTOP_DISABLE);
+			SetTextureStageState(i, D3DTSS_COLORARG1, D3DTA_TEXTURE);   // TODO: D3DTA_DIFFUSE when no texture assigned
+			SetTextureStageState(i, D3DTSS_COLORARG2, D3DTA_CURRENT);
+			SetTextureStageState(i, D3DTSS_ALPHAOP, i == 0 ? D3DTOP_SELECTARG1 : D3DTOP_DISABLE);
+			SetTextureStageState(i, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);   // TODO: D3DTA_DIFFUSE when no texture assigned
+			SetTextureStageState(i, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
+			SetTextureStageState(i, D3DTSS_BUMPENVMAT00, FtoDW(0.0f));
+			SetTextureStageState(i, D3DTSS_BUMPENVMAT01, FtoDW(0.0f));
+			SetTextureStageState(i, D3DTSS_BUMPENVMAT10, FtoDW(0.0f));
+			SetTextureStageState(i, D3DTSS_BUMPENVMAT11, FtoDW(0.0f));
+			SetTextureStageState(i, D3DTSS_TEXCOORDINDEX, i);
+			SetTextureStageState(i, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
+			SetTextureStageState(i, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
+			SetTextureStageState(i, D3DTSS_ADDRESSW, D3DTADDRESS_WRAP);
+			SetTextureStageState(i, D3DTSS_BORDERCOLOR, 0x00000000);
+			SetTextureStageState(i, D3DTSS_MAGFILTER, D3DTEXF_POINT);
+			SetTextureStageState(i, D3DTSS_MINFILTER, D3DTEXF_POINT);
+			SetTextureStageState(i, D3DTSS_MIPFILTER, D3DTEXF_NONE);
+			SetTextureStageState(i, D3DTSS_MIPMAPLODBIAS, 0);
+			SetTextureStageState(i, D3DTSS_MAXMIPLEVEL, 0);
+			SetTextureStageState(i, D3DTSS_MAXANISOTROPY, 1);
+			SetTextureStageState(i, D3DTSS_BUMPENVLSCALE, FtoDW(0.0f));
+			SetTextureStageState(i, D3DTSS_BUMPENVLOFFSET, FtoDW(0.0f));
+			SetTextureStageState(i, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
+			SetTextureStageState(i, D3DTSS_COLORARG0, D3DTA_CURRENT);
+			SetTextureStageState(i, D3DTSS_ALPHAARG0, D3DTA_CURRENT);
+			SetTextureStageState(i, D3DTSS_RESULTARG, D3DTA_CURRENT);
+		}
+
+		currentPalette = 0xFFFF;
+
+		delete cursor;
+		showCursor = false;
+
+		return D3D_OK;		
+	}
+
+	long Direct3DDevice8::ResourceManagerDiscardBytes(unsigned long bytes)
+	{
+		TRACE("");
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetClipPlane(unsigned long index, const float *plane)
+	{
+		TRACE("");
+
+		if(!plane || index > 6)
+		{
+			return INVALIDCALL();
+		}
+
+		if(!recordState)
+		{
+			this->plane[index][0] = plane[0];
+			this->plane[index][1] = plane[1];
+			this->plane[index][2] = plane[2];
+			this->plane[index][3] = plane[3];
+
+			renderer->setClipPlane(index, plane);
+		}
+		else
+		{
+			stateRecorder.back()->setClipPlane(index, plane);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetClipStatus(const D3DCLIPSTATUS8 *clipStatus)
+	{
+		TRACE("");
+
+		if(!clipStatus)
+		{
+			return INVALIDCALL();
+		}
+
+		this->clipStatus = *clipStatus;
+
+		UNIMPLEMENTED();
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetCurrentTexturePalette(unsigned int paletteNumber)
+	{
+		TRACE("");
+
+		if(paletteNumber > 0xFFFF || palette.find(paletteNumber) == palette.end())
+		{
+			return INVALIDCALL();
+		}
+
+		if(!recordState)
+		{
+			currentPalette = paletteNumber;
+
+			sw::Surface::setTexturePalette((unsigned int*)&palette[currentPalette]);
+		}
+		else
+		{
+			stateRecorder.back()->setCurrentTexturePalette(paletteNumber);
+		}
+
+		return D3D_OK;
+	}
+
+	void Direct3DDevice8::SetCursorPosition(int x, int y, unsigned long flags)
+	{
+		TRACE("");
+
+		POINT point = {x, y};
+		HWND window = focusWindow ? focusWindow : presentParameters.hDeviceWindow;
+		ScreenToClient(window, &point);
+
+		sw::FrameBuffer::setCursorPosition(point.x, point.y);
+	}
+
+	void Direct3DDevice8::SetCursorPosition(unsigned int x, unsigned int y, unsigned long flags)
+	{
+		SetCursorPosition((int)x, (int)y, flags);
+	}
+
+	long Direct3DDevice8::SetCursorProperties(unsigned int x0, unsigned int y0, IDirect3DSurface8 *cursorBitmap)
+	{
+		TRACE("");
+
+		if(!cursorBitmap)
+		{
+			return INVALIDCALL();
+		}
+
+		D3DSURFACE_DESC desc;
+		D3DLOCKED_RECT lock;
+
+		cursorBitmap->GetDesc(&desc);
+		cursorBitmap->LockRect(&lock, 0, 0);
+
+		delete cursor;
+		cursor = new sw::Surface(0, desc.Width, desc.Height, 1, sw::FORMAT_A8R8G8B8, false, false);
+
+		void *buffer = cursor->lockExternal(0, 0, 0, sw::LOCK_DISCARD, sw::PUBLIC);
+		memcpy(buffer, lock.pBits, desc.Width * desc.Height * sizeof(unsigned int));
+		cursor->unlockExternal();
+
+		cursorBitmap->UnlockRect();
+
+		sw::FrameBuffer::setCursorOrigin(x0, y0);
+
+		bindCursor();
+
+		return D3D_OK;
+	}
+
+	void Direct3DDevice8::SetGammaRamp(unsigned long flags, const D3DGAMMARAMP *ramp)
+	{
+		TRACE("");
+
+		if(!ramp)
+		{
+			return;
+		}
+
+		swapChain[0]->setGammaRamp((sw::GammaRamp*)ramp, flags & D3DSGR_CALIBRATE);
+		
+		return;
+	}
+
+	long Direct3DDevice8::SetLight(unsigned long index, const D3DLIGHT8 *light)
+	{
+		TRACE("");
+
+		if(!light)
+		{
+			return INVALIDCALL();
+		}
+
+		if(!recordState)
+		{
+			this->light[index] = *light;
+
+			lightsDirty = true;
+		}
+		else
+		{
+			stateRecorder.back()->setLight(index, light);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetMaterial(const D3DMATERIAL8 *material)
+	{
+		TRACE("");
+
+		if(!material)
+		{
+			return INVALIDCALL();   // FIXME: Correct behaviour?
+		}
+
+		if(!recordState)
+		{
+			this->material = *material;
+
+			renderer->setMaterialAmbient(sw::Color<float>(material->Ambient.r, material->Ambient.g, material->Ambient.b, material->Ambient.a));
+			renderer->setMaterialDiffuse(sw::Color<float>(material->Diffuse.r, material->Diffuse.g, material->Diffuse.b, material->Diffuse.a));
+			renderer->setMaterialEmission(sw::Color<float>(material->Emissive.r, material->Emissive.g, material->Emissive.b, material->Emissive.a));
+			renderer->setMaterialShininess(material->Power);
+			renderer->setMaterialSpecular(sw::Color<float>(material->Specular.r, material->Specular.g, material->Specular.b, material->Specular.a));
+		}
+		else
+		{
+			stateRecorder.back()->setMaterial(material);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetPaletteEntries(unsigned int paletteNumber, const PALETTEENTRY *entries)
+	{
+		TRACE("");
+
+		if(paletteNumber > 0xFFFF || !entries)
+		{
+			return INVALIDCALL();
+		}
+
+		for(int i = 0; i < 256; i++)
+		{
+			palette[paletteNumber].entry[i] = entries[i];
+		}
+
+		if(paletteNumber == currentPalette)
+		{
+			sw::Surface::setTexturePalette((unsigned int*)&palette[currentPalette]);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetPixelShader(unsigned long handle)
+	{
+		TRACE("");
+
+		if(!recordState)
+		{
+			if(pixelShader[handle])
+			{
+				pixelShader[handle]->bind();
+			}
+
+			if(pixelShader[pixelShaderHandle])
+			{
+				pixelShader[pixelShaderHandle]->unbind();
+			}
+
+			pixelShaderHandle = handle;
+
+			if(handle != 0)
+			{
+				renderer->setPixelShader(pixelShader[handle]->getPixelShader());
+			}
+			else
+			{
+				renderer->setPixelShader(0);
+			}
+		}
+		else
+		{
+			stateRecorder.back()->setPixelShader(handle);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetPixelShaderConstant(unsigned long startRegister, const void *constantData, unsigned long count)
+	{
+		TRACE("");
+
+		if(!recordState)
+		{
+			for(unsigned int i = 0; i < count; i++)
+			{
+				pixelShaderConstant[startRegister + i][0] = ((float*)constantData)[i * 4 + 0];
+				pixelShaderConstant[startRegister + i][1] = ((float*)constantData)[i * 4 + 1];
+				pixelShaderConstant[startRegister + i][2] = ((float*)constantData)[i * 4 + 2];
+				pixelShaderConstant[startRegister + i][3] = ((float*)constantData)[i * 4 + 3];
+			}
+
+			renderer->setPixelShaderConstantF(startRegister, (const float*)constantData, count);
+		}
+		else
+		{
+			stateRecorder.back()->setPixelShaderConstant(startRegister, constantData, count);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetRenderState(D3DRENDERSTATETYPE state, unsigned long value)
+	{
+		TRACE("D3DRENDERSTATETYPE state = %d, unsigned long value = %d", state, value);
+
+		if(state < D3DRS_ZENABLE || state > D3DRS_NORMALORDER)
+		{
+			return D3D_OK;   // FIXME: Warning
+		}
+
+		if(!recordState)
+		{
+			if(!init && renderState[state] == value)
+			{
+				return D3D_OK;
+			}
+
+			renderState[state] = value;
+
+			switch(state)
+			{		
+			case D3DRS_ZENABLE:
+				switch(value)
+				{
+				case D3DZB_TRUE:
+				case D3DZB_USEW:
+					renderer->setDepthBufferEnable(true);
+					break;
+				case D3DZB_FALSE:
+					renderer->setDepthBufferEnable(false);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_FILLMODE:
+				switch(value)
+				{
+				case D3DFILL_POINT:
+					renderer->setFillMode(sw::FILL_VERTEX);
+					break;
+				case D3DFILL_WIREFRAME:
+					renderer->setFillMode(sw::FILL_WIREFRAME);
+					break;
+				case D3DFILL_SOLID:
+					renderer->setFillMode(sw::FILL_SOLID);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_SHADEMODE:
+				switch(value)
+				{
+				case D3DSHADE_FLAT:
+					renderer->setShadingMode(sw::SHADING_FLAT);
+					break;
+				case D3DSHADE_GOURAUD:
+					renderer->setShadingMode(sw::SHADING_GOURAUD);
+					break;
+				case D3DSHADE_PHONG:
+					// FIXME: Unimplemented (should set gouraud)?
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_LINEPATTERN:
+				if(!init) UNIMPLEMENTED();
+				break;
+			case D3DRS_ZWRITEENABLE:
+				renderer->setDepthWriteEnable(value != FALSE);
+				break;
+			case D3DRS_ALPHATESTENABLE:
+				renderer->setAlphaTestEnable(value != FALSE);
+				break;
+			case D3DRS_LASTPIXEL:
+				if(!init) UNIMPLEMENTED();
+				break;
+			case D3DRS_SRCBLEND:
+				switch(value)
+				{
+				case D3DBLEND_ZERO:
+					renderer->setSourceBlendFactor(sw::BLEND_ZERO);
+					break;
+				case D3DBLEND_ONE:
+					renderer->setSourceBlendFactor(sw::BLEND_ONE);
+					break;
+				case D3DBLEND_SRCCOLOR:
+					renderer->setSourceBlendFactor(sw::BLEND_SOURCE);
+					break;
+				case D3DBLEND_INVSRCCOLOR:
+					renderer->setSourceBlendFactor(sw::BLEND_INVSOURCE);
+					break;
+				case D3DBLEND_SRCALPHA:
+					renderer->setSourceBlendFactor(sw::BLEND_SOURCEALPHA);
+					break;
+				case D3DBLEND_INVSRCALPHA:
+					renderer->setSourceBlendFactor(sw::BLEND_INVSOURCEALPHA);
+					break;
+				case D3DBLEND_DESTALPHA:
+					renderer->setSourceBlendFactor(sw::BLEND_DESTALPHA);
+					break;
+				case D3DBLEND_INVDESTALPHA:
+					renderer->setSourceBlendFactor(sw::BLEND_INVDESTALPHA);
+					break;
+				case D3DBLEND_DESTCOLOR:
+					renderer->setSourceBlendFactor(sw::BLEND_DEST);
+					break;
+				case D3DBLEND_INVDESTCOLOR:
+					renderer->setSourceBlendFactor(sw::BLEND_INVDEST);
+					break;
+				case D3DBLEND_SRCALPHASAT:
+					renderer->setSourceBlendFactor(sw::BLEND_SRCALPHASAT);
+					break;
+				case D3DBLEND_BOTHSRCALPHA:
+					renderer->setSourceBlendFactor(sw::BLEND_SOURCEALPHA);
+					renderer->setDestBlendFactor(sw::BLEND_INVSOURCEALPHA);
+					break;
+				case D3DBLEND_BOTHINVSRCALPHA:
+					renderer->setSourceBlendFactor(sw::BLEND_INVSOURCEALPHA);
+					renderer->setDestBlendFactor(sw::BLEND_SOURCEALPHA);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_DESTBLEND:
+				switch(value)
+				{
+				case D3DBLEND_ZERO:
+					renderer->setDestBlendFactor(sw::BLEND_ZERO);
+					break;
+				case D3DBLEND_ONE:
+					renderer->setDestBlendFactor(sw::BLEND_ONE);
+					break;
+				case D3DBLEND_SRCCOLOR:
+					renderer->setDestBlendFactor(sw::BLEND_SOURCE);
+					break;
+				case D3DBLEND_INVSRCCOLOR:
+					renderer->setDestBlendFactor(sw::BLEND_INVSOURCE);
+					break;
+				case D3DBLEND_SRCALPHA:
+					renderer->setDestBlendFactor(sw::BLEND_SOURCEALPHA);
+					break;
+				case D3DBLEND_INVSRCALPHA:
+					renderer->setDestBlendFactor(sw::BLEND_INVSOURCEALPHA);
+					break;
+				case D3DBLEND_DESTALPHA:
+					renderer->setDestBlendFactor(sw::BLEND_DESTALPHA);
+					break;
+				case D3DBLEND_INVDESTALPHA:
+					renderer->setDestBlendFactor(sw::BLEND_INVDESTALPHA);
+					break;
+				case D3DBLEND_DESTCOLOR:
+					renderer->setDestBlendFactor(sw::BLEND_DEST);
+					break;
+				case D3DBLEND_INVDESTCOLOR:
+					renderer->setDestBlendFactor(sw::BLEND_INVDEST);
+					break;
+				case D3DBLEND_SRCALPHASAT:
+					renderer->setDestBlendFactor(sw::BLEND_SRCALPHASAT);
+					break;
+				case D3DBLEND_BOTHSRCALPHA:
+					renderer->setSourceBlendFactor(sw::BLEND_SOURCEALPHA);
+					renderer->setDestBlendFactor(sw::BLEND_INVSOURCEALPHA);
+					break;
+				case D3DBLEND_BOTHINVSRCALPHA:
+					renderer->setSourceBlendFactor(sw::BLEND_INVSOURCEALPHA);
+					renderer->setDestBlendFactor(sw::BLEND_SOURCEALPHA);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_CULLMODE:	
+				switch(value)
+				{
+				case D3DCULL_NONE:
+					renderer->setCullMode(sw::CULL_NONE);
+					break;
+				case D3DCULL_CCW:
+					renderer->setCullMode(sw::CULL_COUNTERCLOCKWISE);
+					break;
+				case D3DCULL_CW:
+					renderer->setCullMode(sw::CULL_CLOCKWISE);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_ZFUNC:
+				switch(value)
+				{
+				case D3DCMP_NEVER:
+					renderer->setDepthCompare(sw::DEPTH_NEVER);
+					break;
+				case D3DCMP_LESS:
+					renderer->setDepthCompare(sw::DEPTH_LESS);
+					break;
+				case D3DCMP_EQUAL:
+					renderer->setDepthCompare(sw::DEPTH_EQUAL);
+					break;
+				case D3DCMP_LESSEQUAL:
+					renderer->setDepthCompare(sw::DEPTH_LESSEQUAL);
+					break;
+				case D3DCMP_GREATER:
+					renderer->setDepthCompare(sw::DEPTH_GREATER);
+					break;
+				case D3DCMP_NOTEQUAL:
+					renderer->setDepthCompare(sw::DEPTH_NOTEQUAL);
+					break;
+				case D3DCMP_GREATEREQUAL:
+					renderer->setDepthCompare(sw::DEPTH_GREATEREQUAL);
+					break;
+				case D3DCMP_ALWAYS:
+					renderer->setDepthCompare(sw::DEPTH_ALWAYS);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_ALPHAREF:
+				renderer->setAlphaReference(value & 0x000000FF);
+				break;
+			case D3DRS_ALPHAFUNC:
+				switch(value)
+				{
+				case D3DCMP_NEVER:
+					renderer->setAlphaCompare(sw::ALPHA_NEVER);
+					break;
+				case D3DCMP_LESS:
+					renderer->setAlphaCompare(sw::ALPHA_LESS);
+					break;
+				case D3DCMP_EQUAL:
+					renderer->setAlphaCompare(sw::ALPHA_EQUAL);
+					break;
+				case D3DCMP_LESSEQUAL:
+					renderer->setAlphaCompare(sw::ALPHA_LESSEQUAL);
+					break;
+				case D3DCMP_GREATER:
+					renderer->setAlphaCompare(sw::ALPHA_GREATER);
+					break;
+				case D3DCMP_NOTEQUAL:
+					renderer->setAlphaCompare(sw::ALPHA_NOTEQUAL);
+					break;
+				case D3DCMP_GREATEREQUAL:
+					renderer->setAlphaCompare(sw::ALPHA_GREATEREQUAL);
+					break;
+				case D3DCMP_ALWAYS:
+					renderer->setAlphaCompare(sw::ALPHA_ALWAYS);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_DITHERENABLE:
+			//	if(!init && value == 1) UNIMPLEMENTED();   // FIXME: Unimplemented
+				break;
+			case D3DRS_ALPHABLENDENABLE:
+				renderer->setAlphaBlendEnable(value != FALSE);
+				break;
+			case D3DRS_FOGENABLE:
+				renderer->setFogEnable(value != FALSE);
+				break;
+			case D3DRS_ZVISIBLE:
+				break;   // Not supported
+			case D3DRS_FOGCOLOR:
+				renderer->setFogColor(value);
+				break;
+			case D3DRS_FOGTABLEMODE:
+				switch(value)
+				{
+				case D3DFOG_NONE:
+					renderer->setPixelFogMode(sw::FOG_NONE);
+					break;
+				case D3DFOG_LINEAR:
+					renderer->setPixelFogMode(sw::FOG_LINEAR);
+					break;
+				case D3DFOG_EXP:
+					renderer->setPixelFogMode(sw::FOG_EXP);
+					break;
+				case D3DFOG_EXP2:
+					renderer->setPixelFogMode(sw::FOG_EXP2);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_FOGSTART:
+				renderer->setFogStart((float&)value);
+				break;
+			case D3DRS_FOGEND:
+				renderer->setFogEnd((float&)value);
+				break;
+			case D3DRS_FOGDENSITY:
+				renderer->setFogDensity((float&)value);
+				break;
+			case D3DRS_EDGEANTIALIAS:
+				if(!init) if(value != FALSE) UNIMPLEMENTED();
+				break;
+			case D3DRS_ZBIAS:
+				renderer->setDepthBias(-2.0e-6f * value);
+				renderer->setSlopeDepthBias(0.0f);
+				break;
+			case D3DRS_RANGEFOGENABLE:
+				renderer->setRangeFogEnable(value != FALSE);
+				break;
+			case D3DRS_SPECULARENABLE:
+				renderer->setSpecularEnable(value != FALSE);
+				break;
+			case D3DRS_STENCILENABLE:
+				renderer->setStencilEnable(value != FALSE);
+				break;
+			case D3DRS_STENCILFAIL:
+				switch(value)
+				{
+				case D3DSTENCILOP_KEEP:
+					renderer->setStencilFailOperation(sw::OPERATION_KEEP);
+					break;
+				case D3DSTENCILOP_ZERO:
+					renderer->setStencilFailOperation(sw::OPERATION_ZERO);
+					break;
+				case D3DSTENCILOP_REPLACE:
+					renderer->setStencilFailOperation(sw::OPERATION_REPLACE);
+					break;
+				case D3DSTENCILOP_INCRSAT:
+					renderer->setStencilFailOperation(sw::OPERATION_INCRSAT);
+					break;
+				case D3DSTENCILOP_DECRSAT:
+					renderer->setStencilFailOperation(sw::OPERATION_DECRSAT);
+					break;
+				case D3DSTENCILOP_INVERT:
+					renderer->setStencilFailOperation(sw::OPERATION_INVERT);
+					break;
+				case D3DSTENCILOP_INCR:
+					renderer->setStencilFailOperation(sw::OPERATION_INCR);
+					break;
+				case D3DSTENCILOP_DECR:
+					renderer->setStencilFailOperation(sw::OPERATION_DECR);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_STENCILZFAIL:
+				switch(value)
+				{
+				case D3DSTENCILOP_KEEP:
+					renderer->setStencilZFailOperation(sw::OPERATION_KEEP);
+					break;
+				case D3DSTENCILOP_ZERO:
+					renderer->setStencilZFailOperation(sw::OPERATION_ZERO);
+					break;
+				case D3DSTENCILOP_REPLACE:
+					renderer->setStencilZFailOperation(sw::OPERATION_REPLACE);
+					break;
+				case D3DSTENCILOP_INCRSAT:
+					renderer->setStencilZFailOperation(sw::OPERATION_INCRSAT);
+					break;
+				case D3DSTENCILOP_DECRSAT:
+					renderer->setStencilZFailOperation(sw::OPERATION_DECRSAT);
+					break;
+				case D3DSTENCILOP_INVERT:
+					renderer->setStencilZFailOperation(sw::OPERATION_INVERT);
+					break;
+				case D3DSTENCILOP_INCR:
+					renderer->setStencilZFailOperation(sw::OPERATION_INCR);
+					break;
+				case D3DSTENCILOP_DECR:
+					renderer->setStencilZFailOperation(sw::OPERATION_DECR);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_STENCILPASS:
+				switch(value)
+				{
+				case D3DSTENCILOP_KEEP:
+					renderer->setStencilPassOperation(sw::OPERATION_KEEP);
+					break;
+				case D3DSTENCILOP_ZERO:
+					renderer->setStencilPassOperation(sw::OPERATION_ZERO);
+					break;
+				case D3DSTENCILOP_REPLACE:
+					renderer->setStencilPassOperation(sw::OPERATION_REPLACE);
+					break;
+				case D3DSTENCILOP_INCRSAT:
+					renderer->setStencilPassOperation(sw::OPERATION_INCRSAT);
+					break;
+				case D3DSTENCILOP_DECRSAT:
+					renderer->setStencilPassOperation(sw::OPERATION_DECRSAT);
+					break;
+				case D3DSTENCILOP_INVERT:
+					renderer->setStencilPassOperation(sw::OPERATION_INVERT);
+					break;
+				case D3DSTENCILOP_INCR:
+					renderer->setStencilPassOperation(sw::OPERATION_INCR);
+					break;
+				case D3DSTENCILOP_DECR:
+					renderer->setStencilPassOperation(sw::OPERATION_DECR);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_STENCILFUNC:
+				switch(value)
+				{
+				case D3DCMP_NEVER:
+					renderer->setStencilCompare(sw::STENCIL_NEVER);
+					break;
+				case D3DCMP_LESS:
+					renderer->setStencilCompare(sw::STENCIL_LESS);
+					break;
+				case D3DCMP_EQUAL:
+					renderer->setStencilCompare(sw::STENCIL_EQUAL);
+					break;
+				case D3DCMP_LESSEQUAL:
+					renderer->setStencilCompare(sw::STENCIL_LESSEQUAL);
+					break;
+				case D3DCMP_GREATER:
+					renderer->setStencilCompare(sw::STENCIL_GREATER);
+					break;
+				case D3DCMP_NOTEQUAL:
+					renderer->setStencilCompare(sw::STENCIL_NOTEQUAL);
+					break;
+				case D3DCMP_GREATEREQUAL:
+					renderer->setStencilCompare(sw::STENCIL_GREATEREQUAL);
+					break;
+				case D3DCMP_ALWAYS:
+					renderer->setStencilCompare(sw::STENCIL_ALWAYS);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_STENCILREF:
+				renderer->setStencilReference(value);
+				renderer->setStencilReferenceCCW(value);
+				break;
+			case D3DRS_STENCILMASK:
+				renderer->setStencilMask(value);
+				renderer->setStencilMaskCCW(value);
+				break;
+			case D3DRS_STENCILWRITEMASK:
+				renderer->setStencilWriteMask(value);
+				renderer->setStencilWriteMaskCCW(value);
+				break;
+			case D3DRS_TEXTUREFACTOR:
+				renderer->setTextureFactor(value);
+				break;
+			case D3DRS_WRAP0:
+				renderer->setTextureWrap(0, value);
+				break;
+			case D3DRS_WRAP1:
+				renderer->setTextureWrap(1, value);
+				break;
+			case D3DRS_WRAP2:
+				renderer->setTextureWrap(2, value);
+				break;
+			case D3DRS_WRAP3:
+				renderer->setTextureWrap(3, value);
+				break;
+			case D3DRS_WRAP4:
+				renderer->setTextureWrap(4, value);
+				break;
+			case D3DRS_WRAP5:
+				renderer->setTextureWrap(5, value);
+				break;
+			case D3DRS_WRAP6:
+				renderer->setTextureWrap(6, value);
+				break;
+			case D3DRS_WRAP7:
+				renderer->setTextureWrap(7, value);
+				break;
+			case D3DRS_CLIPPING:
+				// Ignored, clipping is always performed
+				break;
+			case D3DRS_LIGHTING:
+				renderer->setLightingEnable(value != FALSE);
+				break;
+			case D3DRS_AMBIENT:
+				renderer->setGlobalAmbient(value);
+				break;
+			case D3DRS_FOGVERTEXMODE:
+				switch(value)
+				{
+				case D3DFOG_NONE:
+					renderer->setVertexFogMode(sw::FOG_NONE);
+					break;
+				case D3DFOG_LINEAR:
+					renderer->setVertexFogMode(sw::FOG_LINEAR);
+					break;
+				case D3DFOG_EXP:
+					renderer->setVertexFogMode(sw::FOG_EXP);
+					break;
+				case D3DFOG_EXP2:
+					renderer->setVertexFogMode(sw::FOG_EXP2);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_COLORVERTEX:
+				renderer->setColorVertexEnable(value != FALSE);
+				break;
+			case D3DRS_LOCALVIEWER:
+				renderer->setLocalViewer(value != FALSE);
+				break;
+			case D3DRS_NORMALIZENORMALS:
+				renderer->setNormalizeNormals(value != FALSE);
+				break;
+			case D3DRS_DIFFUSEMATERIALSOURCE:
+				switch(value)
+				{
+				case D3DMCS_MATERIAL:
+					renderer->setDiffuseMaterialSource(sw::MATERIAL_MATERIAL);
+					break;
+				case D3DMCS_COLOR1:
+					renderer->setDiffuseMaterialSource(sw::MATERIAL_COLOR1);
+					break;
+				case D3DMCS_COLOR2:
+					renderer->setDiffuseMaterialSource(sw::MATERIAL_COLOR2);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_SPECULARMATERIALSOURCE:
+				switch(value)
+				{
+				case D3DMCS_MATERIAL:
+					renderer->setSpecularMaterialSource(sw::MATERIAL_MATERIAL);
+					break;
+				case D3DMCS_COLOR1:
+					renderer->setSpecularMaterialSource(sw::MATERIAL_COLOR1);
+					break;
+				case D3DMCS_COLOR2:
+					renderer->setSpecularMaterialSource(sw::MATERIAL_COLOR2);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_AMBIENTMATERIALSOURCE:
+				switch(value)
+				{
+				case D3DMCS_MATERIAL:
+					renderer->setAmbientMaterialSource(sw::MATERIAL_MATERIAL);
+					break;
+				case D3DMCS_COLOR1:
+					renderer->setAmbientMaterialSource(sw::MATERIAL_COLOR1);
+					break;
+				case D3DMCS_COLOR2:
+					renderer->setAmbientMaterialSource(sw::MATERIAL_COLOR2);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_EMISSIVEMATERIALSOURCE:
+				switch(value)
+				{
+				case D3DMCS_MATERIAL:
+					renderer->setEmissiveMaterialSource(sw::MATERIAL_MATERIAL);
+					break;
+				case D3DMCS_COLOR1:
+					renderer->setEmissiveMaterialSource(sw::MATERIAL_COLOR1);
+					break;
+				case D3DMCS_COLOR2:
+					renderer->setEmissiveMaterialSource(sw::MATERIAL_COLOR2);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_VERTEXBLEND:
+				switch(value)
+				{
+				case D3DVBF_DISABLE:
+					renderer->setVertexBlendMatrixCount(0);
+					break;
+				case D3DVBF_1WEIGHTS:
+					renderer->setVertexBlendMatrixCount(2);
+					break;
+				case D3DVBF_2WEIGHTS:
+					renderer->setVertexBlendMatrixCount(3);
+					break;
+				case D3DVBF_3WEIGHTS:
+					renderer->setVertexBlendMatrixCount(4);
+					break;
+				case D3DVBF_TWEENING:
+					UNIMPLEMENTED();
+					break;
+				case D3DVBF_0WEIGHTS:
+					renderer->setVertexBlendMatrixCount(1);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_CLIPPLANEENABLE:
+				renderer->setClipFlags(value);
+				break;
+			case D3DRS_SOFTWAREVERTEXPROCESSING:
+				break;
+			case D3DRS_POINTSIZE:
+				renderer->setPointSize((float&)value);
+				break;
+			case D3DRS_POINTSIZE_MIN:
+				renderer->setPointSizeMin((float&)value);
+				break;
+			case D3DRS_POINTSPRITEENABLE:
+				renderer->setPointSpriteEnable(value != FALSE);
+				break;
+			case D3DRS_POINTSCALEENABLE:
+				renderer->setPointScaleEnable(value != FALSE);
+				break;
+			case D3DRS_POINTSCALE_A:
+				renderer->setPointScaleA((float&)value);
+				break;
+			case D3DRS_POINTSCALE_B:
+				renderer->setPointScaleB((float&)value);
+				break;
+			case D3DRS_POINTSCALE_C:
+				renderer->setPointScaleC((float&)value);
+				break;
+			case D3DRS_MULTISAMPLEANTIALIAS:
+			//	if(!init) UNIMPLEMENTED();
+				break;
+			case D3DRS_MULTISAMPLEMASK:
+				SetRenderTarget(renderTarget, depthStencil);   // Sets the multi-sample mask, if maskable
+				break;
+			case D3DRS_PATCHEDGESTYLE:
+			//	if(!init) UNIMPLEMENTED();
+				break;
+			case D3DRS_PATCHSEGMENTS:
+			//	UNIMPLEMENTED();   // FIXME
+				break;
+			case D3DRS_DEBUGMONITORTOKEN:
+				if(!init) UNIMPLEMENTED();
+				break;
+			case D3DRS_POINTSIZE_MAX:
+				renderer->setPointSizeMax((float&)value);
+				break;
+			case D3DRS_INDEXEDVERTEXBLENDENABLE:
+				renderer->setIndexedVertexBlendEnable(value != FALSE);
+				break;
+			case D3DRS_COLORWRITEENABLE:
+				renderer->setColorWriteMask(0, value);
+				break;
+			case D3DRS_TWEENFACTOR:
+				if(!init) UNIMPLEMENTED();
+				break;
+			case D3DRS_BLENDOP:
+				switch(value)
+				{
+				case D3DBLENDOP_ADD:
+					renderer->setBlendOperation(sw::BLENDOP_ADD);
+					break;
+				case D3DBLENDOP_SUBTRACT:
+					renderer->setBlendOperation(sw::BLENDOP_SUB);
+					break;
+				case D3DBLENDOP_REVSUBTRACT:
+					renderer->setBlendOperation(sw::BLENDOP_INVSUB);
+					break;
+				case D3DBLENDOP_MIN:
+					renderer->setBlendOperation(sw::BLENDOP_MIN);
+					break;
+				case D3DBLENDOP_MAX:
+					renderer->setBlendOperation(sw::BLENDOP_MAX);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DRS_POSITIONORDER:
+				if(!init) UNIMPLEMENTED();
+				break;
+			case D3DRS_NORMALORDER:
+				if(!init) UNIMPLEMENTED();
+				break;
+			default:
+				ASSERT(false);
+			}
+		}
+		else   // stateRecorder
+		{
+			stateRecorder.back()->setRenderState(state, value);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetRenderTarget(IDirect3DSurface8 *newRenderTarget, IDirect3DSurface8 *newDepthStencil)
+	{
+		TRACE("");
+
+		Direct3DSurface8 *renderTarget = static_cast<Direct3DSurface8*>(newRenderTarget);
+
+		if(renderTarget)   // FIXME: Check for D3DUSAGE_RENDERTARGET
+		{
+			renderTarget->bind();
+		}
+
+		if(this->renderTarget)
+		{
+			this->renderTarget->unbind();
+		}
+
+		this->renderTarget = renderTarget;
+
+		Direct3DSurface8 *depthStencil = static_cast<Direct3DSurface8*>(newDepthStencil);
+
+		if(depthStencil)   // FIXME: Check for D3DUSAGE_DEPTHSTENCIL and D3DPOOL_DEFAULT
+		{
+			depthStencil->bind();
+		}
+
+		if(this->depthStencil)
+		{
+			this->depthStencil->unbind();
+		}
+
+		this->depthStencil = depthStencil;
+
+		// Reset viewport to size of current render target
+		D3DSURFACE_DESC renderTargetDesc;
+		renderTarget->GetDesc(&renderTargetDesc);
+
+		D3DVIEWPORT8 viewport;
+		viewport.X = 0;
+		viewport.Y = 0;
+		viewport.Width = renderTargetDesc.Width;
+		viewport.Height = renderTargetDesc.Height;
+		viewport.MinZ = 0;
+		viewport.MaxZ = 1;
+
+		SetViewport(&viewport);
+
+		// Set the multi-sample mask, if maskable
+		if(renderTargetDesc.MultiSampleType != D3DMULTISAMPLE_NONE)
+		{
+			renderer->setMultiSampleMask(renderState[D3DRS_MULTISAMPLEMASK]);
+		}
+		else
+		{
+			renderer->setMultiSampleMask(0xFFFFFFFF);
+		}
+
+		renderer->setRenderTarget(0, renderTarget);
+		renderer->setDepthBuffer(depthStencil);
+		renderer->setStencilBuffer(depthStencil);
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetStreamSource(unsigned int stream, IDirect3DVertexBuffer8 *iVertexBuffer, unsigned int stride)
+	{
+		TRACE("");
+
+		Direct3DVertexBuffer8 *vertexBuffer = static_cast<Direct3DVertexBuffer8*>(iVertexBuffer);
+
+		if(!recordState)
+		{
+			if(vertexBuffer)
+			{
+				vertexBuffer->bind();
+			}
+
+			if(dataStream[stream])
+			{
+				dataStream[stream]->unbind();
+				streamStride[stream] = 0;
+			}
+
+			dataStream[stream] = vertexBuffer;
+			streamStride[stream] = stride;
+		}
+		else
+		{
+			stateRecorder.back()->setStreamSource(stream, vertexBuffer, stride);
+		}
+	
+		return D3D_OK;
+	}
+	
+	long Direct3DDevice8::SetTexture(unsigned long stage, IDirect3DBaseTexture8 *iBaseTexture)
+	{
+		TRACE("");
+
+		if(stage >= 8)
+		{
+			return INVALIDCALL();
+		}
+
+		Direct3DBaseTexture8 *baseTexture = dynamic_cast<Direct3DBaseTexture8*>(iBaseTexture);
+
+		if(!recordState)
+		{
+			if(texture[stage] == baseTexture)
+			{
+				return D3D_OK;
+			}
+
+			if(baseTexture)
+			{
+				baseTexture->bind();
+			}
+
+			if(texture[stage])
+			{
+				texture[stage]->unbind();
+			}
+
+			texture[stage] = baseTexture;
+		}
+		else
+		{
+			stateRecorder.back()->setTexture(stage, baseTexture);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetTextureStageState(unsigned long stage, D3DTEXTURESTAGESTATETYPE type, unsigned long value)
+	{
+		TRACE("unsigned long stage = %d, D3DTEXTURESTAGESTATETYPE type = %d, unsigned long value = %d", stage, type, value);
+
+		if(stage >= 8 || type < 0 || type > D3DTSS_RESULTARG)
+		{
+			return INVALIDCALL();
+		}
+
+		if(!recordState)
+		{
+			if(!init && textureStageState[stage][type] == value)
+			{
+				return D3D_OK;
+			}
+
+			textureStageState[stage][type] = value;
+
+			switch(type)
+			{
+			case D3DTSS_COLOROP:
+				switch(value)
+				{
+				case D3DTOP_DISABLE:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_DISABLE);
+					break;
+				case D3DTOP_SELECTARG1:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_SELECTARG1);
+					break;
+				case D3DTOP_SELECTARG2:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_SELECTARG2);
+					break;
+				case D3DTOP_MODULATE:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_MODULATE);
+					break;
+				case D3DTOP_MODULATE2X:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_MODULATE2X);
+					break;
+				case D3DTOP_MODULATE4X:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_MODULATE4X);
+					break;
+				case D3DTOP_ADD:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_ADD);
+					break;
+				case D3DTOP_ADDSIGNED:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_ADDSIGNED);
+					break;
+				case D3DTOP_ADDSIGNED2X:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_ADDSIGNED2X);
+					break;
+				case D3DTOP_SUBTRACT:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_SUBTRACT);
+					break;
+				case D3DTOP_ADDSMOOTH:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_ADDSMOOTH);
+					break;
+				case D3DTOP_BLENDDIFFUSEALPHA:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_BLENDDIFFUSEALPHA);
+					break;
+				case D3DTOP_BLENDTEXTUREALPHA:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_BLENDTEXTUREALPHA);
+					break;
+				case D3DTOP_BLENDFACTORALPHA:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_BLENDFACTORALPHA);
+					break;
+				case D3DTOP_BLENDTEXTUREALPHAPM:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_BLENDTEXTUREALPHAPM);
+					break;
+				case D3DTOP_BLENDCURRENTALPHA:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_BLENDCURRENTALPHA);
+					break;
+				case D3DTOP_PREMODULATE:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_PREMODULATE);
+					break;
+				case D3DTOP_MODULATEALPHA_ADDCOLOR:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_MODULATEALPHA_ADDCOLOR);
+					break;
+				case D3DTOP_MODULATECOLOR_ADDALPHA:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_MODULATECOLOR_ADDALPHA);
+					break;
+				case D3DTOP_MODULATEINVALPHA_ADDCOLOR:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR);
+					break;
+				case D3DTOP_MODULATEINVCOLOR_ADDALPHA:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA);
+					break;
+				case D3DTOP_BUMPENVMAP:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_BUMPENVMAP);
+					break;
+				case D3DTOP_BUMPENVMAPLUMINANCE:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_BUMPENVMAPLUMINANCE);
+					break;
+				case D3DTOP_DOTPRODUCT3:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_DOT3);
+					break;
+				case D3DTOP_MULTIPLYADD:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_MULTIPLYADD);
+					break;
+				case D3DTOP_LERP:
+					renderer->setStageOperation(stage, sw::TextureStage::STAGE_LERP);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_COLORARG1:
+				switch(value & D3DTA_SELECTMASK)
+				{
+				case D3DTA_DIFFUSE:
+					renderer->setFirstArgument(stage, sw::TextureStage::SOURCE_DIFFUSE);
+					break;
+				case D3DTA_CURRENT:
+					renderer->setFirstArgument(stage, sw::TextureStage::SOURCE_CURRENT);
+					break;
+				case D3DTA_TEXTURE:
+					renderer->setFirstArgument(stage, sw::TextureStage::SOURCE_TEXTURE);
+					break;
+				case D3DTA_TFACTOR:
+					renderer->setFirstArgument(stage, sw::TextureStage::SOURCE_TFACTOR);
+					break;
+				case D3DTA_SPECULAR:
+					renderer->setFirstArgument(stage, sw::TextureStage::SOURCE_SPECULAR);
+					break;
+				case D3DTA_TEMP:
+					renderer->setFirstArgument(stage, sw::TextureStage::SOURCE_TEMP);
+					break;
+				default:
+					ASSERT(false);
+				}
+
+				switch(value & ~D3DTA_SELECTMASK)
+				{
+				case 0:
+					renderer->setFirstModifier(stage, sw::TextureStage::MODIFIER_COLOR);
+					break;
+				case D3DTA_COMPLEMENT:
+					renderer->setFirstModifier(stage, sw::TextureStage::MODIFIER_INVCOLOR);
+					break;
+				case D3DTA_ALPHAREPLICATE:
+					renderer->setFirstModifier(stage, sw::TextureStage::MODIFIER_ALPHA);
+					break;
+				case D3DTA_COMPLEMENT | D3DTA_ALPHAREPLICATE:
+					renderer->setFirstModifier(stage, sw::TextureStage::MODIFIER_INVALPHA);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_COLORARG2:
+				switch(value & D3DTA_SELECTMASK)
+				{
+				case D3DTA_DIFFUSE:
+					renderer->setSecondArgument(stage, sw::TextureStage::SOURCE_DIFFUSE);
+					break;
+				case D3DTA_CURRENT:
+					renderer->setSecondArgument(stage, sw::TextureStage::SOURCE_CURRENT);
+					break;
+				case D3DTA_TEXTURE:
+					renderer->setSecondArgument(stage, sw::TextureStage::SOURCE_TEXTURE);
+					break;
+				case D3DTA_TFACTOR:
+					renderer->setSecondArgument(stage, sw::TextureStage::SOURCE_TFACTOR);						
+					break;
+				case D3DTA_SPECULAR:
+					renderer->setSecondArgument(stage, sw::TextureStage::SOURCE_SPECULAR);
+					break;
+				case D3DTA_TEMP:
+					renderer->setSecondArgument(stage, sw::TextureStage::SOURCE_TEMP);
+					break;
+				default:
+					ASSERT(false);
+				}
+
+				switch(value & ~D3DTA_SELECTMASK)
+				{
+				case 0:
+					renderer->setSecondModifier(stage, sw::TextureStage::MODIFIER_COLOR);
+					break;
+				case D3DTA_COMPLEMENT:
+					renderer->setSecondModifier(stage, sw::TextureStage::MODIFIER_INVCOLOR);
+					break;
+				case D3DTA_ALPHAREPLICATE:
+					renderer->setSecondModifier(stage, sw::TextureStage::MODIFIER_ALPHA);
+					break;
+				case D3DTA_COMPLEMENT | D3DTA_ALPHAREPLICATE:
+					renderer->setSecondModifier(stage, sw::TextureStage::MODIFIER_INVALPHA);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_ALPHAOP:
+				switch(value)
+				{
+				case D3DTOP_DISABLE:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_DISABLE);
+					break;
+				case D3DTOP_SELECTARG1:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_SELECTARG1);
+					break;
+				case D3DTOP_SELECTARG2:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_SELECTARG2);
+					break;
+				case D3DTOP_MODULATE:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_MODULATE);
+					break;
+				case D3DTOP_MODULATE2X:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_MODULATE2X);
+					break;
+				case D3DTOP_MODULATE4X:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_MODULATE4X);
+					break;
+				case D3DTOP_ADD:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_ADD);
+					break;
+				case D3DTOP_ADDSIGNED:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_ADDSIGNED);
+					break;
+				case D3DTOP_ADDSIGNED2X:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_ADDSIGNED2X);
+					break;
+				case D3DTOP_SUBTRACT:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_SUBTRACT);
+					break;
+				case D3DTOP_ADDSMOOTH:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_ADDSMOOTH);
+					break;
+				case D3DTOP_BLENDDIFFUSEALPHA:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_BLENDDIFFUSEALPHA);
+					break;
+				case D3DTOP_BLENDTEXTUREALPHA:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_BLENDTEXTUREALPHA);
+					break;
+				case D3DTOP_BLENDFACTORALPHA:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_BLENDFACTORALPHA);
+					break;
+				case D3DTOP_BLENDTEXTUREALPHAPM:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_BLENDTEXTUREALPHAPM);
+					break;
+				case D3DTOP_BLENDCURRENTALPHA:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_BLENDCURRENTALPHA);
+					break;
+				case D3DTOP_PREMODULATE:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_PREMODULATE);
+					break;
+				case D3DTOP_MODULATEALPHA_ADDCOLOR:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_MODULATEALPHA_ADDCOLOR);
+					break;
+				case D3DTOP_MODULATECOLOR_ADDALPHA:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_MODULATECOLOR_ADDALPHA);
+					break;
+				case D3DTOP_MODULATEINVALPHA_ADDCOLOR:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_MODULATEINVALPHA_ADDCOLOR);
+					break;
+				case D3DTOP_MODULATEINVCOLOR_ADDALPHA:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_MODULATEINVCOLOR_ADDALPHA);
+					break;
+				case D3DTOP_BUMPENVMAP:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_BUMPENVMAP);
+					break;
+				case D3DTOP_BUMPENVMAPLUMINANCE:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_BUMPENVMAPLUMINANCE);
+					break;
+				case D3DTOP_DOTPRODUCT3:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_DOT3);
+					break;
+				case D3DTOP_MULTIPLYADD:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_MULTIPLYADD);
+					break;
+				case D3DTOP_LERP:
+					renderer->setStageOperationAlpha(stage, sw::TextureStage::STAGE_LERP);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_ALPHAARG1:
+				switch(value & D3DTA_SELECTMASK)
+				{
+				case D3DTA_DIFFUSE:
+					renderer->setFirstArgumentAlpha(stage, sw::TextureStage::SOURCE_DIFFUSE);
+					break;
+				case D3DTA_CURRENT:
+					renderer->setFirstArgumentAlpha(stage, sw::TextureStage::SOURCE_CURRENT);
+					break;
+				case D3DTA_TEXTURE:
+					renderer->setFirstArgumentAlpha(stage, sw::TextureStage::SOURCE_TEXTURE);
+					break;
+				case D3DTA_TFACTOR:
+					renderer->setFirstArgumentAlpha(stage, sw::TextureStage::SOURCE_TFACTOR);
+					break;
+				case D3DTA_SPECULAR:
+					renderer->setFirstArgumentAlpha(stage, sw::TextureStage::SOURCE_SPECULAR);
+					break;
+				case D3DTA_TEMP:
+					renderer->setFirstArgumentAlpha(stage, sw::TextureStage::SOURCE_TEMP);
+					break;
+				default:
+					ASSERT(false);
+				}
+
+				switch(value & ~D3DTA_SELECTMASK)
+				{
+				case 0:
+					renderer->setFirstModifierAlpha(stage, sw::TextureStage::MODIFIER_COLOR);
+					break;
+				case D3DTA_COMPLEMENT:
+					renderer->setFirstModifierAlpha(stage, sw::TextureStage::MODIFIER_INVCOLOR);
+					break;
+				case D3DTA_ALPHAREPLICATE:
+					renderer->setFirstModifierAlpha(stage, sw::TextureStage::MODIFIER_ALPHA);
+					break;
+				case D3DTA_COMPLEMENT | D3DTA_ALPHAREPLICATE:
+					renderer->setSecondModifier(stage, sw::TextureStage::MODIFIER_INVALPHA);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_ALPHAARG2:
+				switch(value & D3DTA_SELECTMASK)
+				{
+				case D3DTA_DIFFUSE:
+					renderer->setSecondArgumentAlpha(stage, sw::TextureStage::SOURCE_DIFFUSE);
+					break;
+				case D3DTA_CURRENT:
+					renderer->setSecondArgumentAlpha(stage, sw::TextureStage::SOURCE_CURRENT);
+					break;
+				case D3DTA_TEXTURE:
+					renderer->setSecondArgumentAlpha(stage, sw::TextureStage::SOURCE_TEXTURE);
+					break;
+				case D3DTA_TFACTOR:
+					renderer->setSecondArgumentAlpha(stage, sw::TextureStage::SOURCE_TFACTOR);
+					break;
+				case D3DTA_SPECULAR:
+					renderer->setSecondArgumentAlpha(stage, sw::TextureStage::SOURCE_SPECULAR);
+					break;
+				case D3DTA_TEMP:
+					renderer->setSecondArgumentAlpha(stage, sw::TextureStage::SOURCE_TEMP);
+					break;
+				default:
+					ASSERT(false);
+				}
+
+				switch(value & ~D3DTA_SELECTMASK)
+				{
+				case 0:
+					renderer->setSecondModifierAlpha(stage, sw::TextureStage::MODIFIER_COLOR);
+					break;
+				case D3DTA_COMPLEMENT:
+					renderer->setSecondModifierAlpha(stage, sw::TextureStage::MODIFIER_INVCOLOR);
+					break;
+				case D3DTA_ALPHAREPLICATE:
+					renderer->setSecondModifierAlpha(stage, sw::TextureStage::MODIFIER_ALPHA);
+					break;
+				case D3DTA_COMPLEMENT | D3DTA_ALPHAREPLICATE:
+					renderer->setSecondModifierAlpha(stage, sw::TextureStage::MODIFIER_INVALPHA);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_BUMPENVMAT00:
+				renderer->setBumpmapMatrix(stage, 0, (float&)value);
+				break;
+			case D3DTSS_BUMPENVMAT01:
+				renderer->setBumpmapMatrix(stage, 1, (float&)value);
+				break;
+			case D3DTSS_BUMPENVMAT10:
+				renderer->setBumpmapMatrix(stage, 2, (float&)value);
+				break;
+			case D3DTSS_BUMPENVMAT11:
+				renderer->setBumpmapMatrix(stage, 3, (float&)value);
+				break;
+			case D3DTSS_TEXCOORDINDEX:
+				renderer->setTexCoordIndex(stage, value & 0xFFFF);
+
+				switch(value & 0xFFFF0000)
+				{
+				case D3DTSS_TCI_PASSTHRU:
+					renderer->setTexGen(stage, sw::TEXGEN_PASSTHRU);
+					break;
+				case D3DTSS_TCI_CAMERASPACENORMAL:
+					renderer->setTexCoordIndex(stage, stage);
+					renderer->setTexGen(stage, sw::TEXGEN_NORMAL);
+					break;
+				case D3DTSS_TCI_CAMERASPACEPOSITION:
+					renderer->setTexCoordIndex(stage, stage);
+					renderer->setTexGen(stage, sw::TEXGEN_POSITION);
+					break;
+				case D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
+					renderer->setTexCoordIndex(stage, stage);
+					renderer->setTexGen(stage, sw::TEXGEN_REFLECTION);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_ADDRESSU:
+				switch(value)
+				{
+				case D3DTADDRESS_WRAP:
+					renderer->setAddressingModeU(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_WRAP);
+					break;
+				case D3DTADDRESS_MIRROR:
+					renderer->setAddressingModeU(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_MIRROR);
+					break;
+				case D3DTADDRESS_CLAMP:
+					renderer->setAddressingModeU(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_CLAMP);
+					break;
+				case D3DTADDRESS_BORDER:
+					renderer->setAddressingModeU(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_BORDER);
+					break;
+				case D3DTADDRESS_MIRRORONCE:
+					renderer->setAddressingModeU(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_MIRRORONCE);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_ADDRESSV:
+				switch(value)
+				{
+				case D3DTADDRESS_WRAP:
+					renderer->setAddressingModeV(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_WRAP);
+					break;
+				case D3DTADDRESS_MIRROR:
+					renderer->setAddressingModeV(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_MIRROR);
+					break;
+				case D3DTADDRESS_CLAMP:
+					renderer->setAddressingModeV(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_CLAMP);
+					break;
+				case D3DTADDRESS_BORDER:
+					renderer->setAddressingModeV(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_BORDER);
+					break;
+				case D3DTADDRESS_MIRRORONCE:
+					renderer->setAddressingModeV(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_MIRRORONCE);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_BORDERCOLOR:
+				renderer->setBorderColor(sw::SAMPLER_PIXEL, stage, value);
+				break;
+			case D3DTSS_MAGFILTER:
+				// NOTE: SwiftShader does not differentiate between minification and magnification filter
+				switch(value)
+				{
+				case D3DTEXF_NONE:
+					renderer->setTextureFilter(sw::SAMPLER_PIXEL, stage, sw::FILTER_POINT);
+					break;
+				case D3DTEXF_POINT:
+					renderer->setTextureFilter(sw::SAMPLER_PIXEL, stage, sw::FILTER_POINT);
+					break;
+				case D3DTEXF_LINEAR:
+					renderer->setTextureFilter(sw::SAMPLER_PIXEL, stage, sw::FILTER_LINEAR);
+					break;
+				case D3DTEXF_ANISOTROPIC:
+					renderer->setTextureFilter(sw::SAMPLER_PIXEL, stage, sw::FILTER_ANISOTROPIC);
+					break;
+				case D3DTEXF_FLATCUBIC:
+					renderer->setTextureFilter(sw::SAMPLER_PIXEL, stage, sw::FILTER_LINEAR);   // NOTE: Unimplemented, fail silently
+					break;
+				case D3DTEXF_GAUSSIANCUBIC:
+					renderer->setTextureFilter(sw::SAMPLER_PIXEL, stage, sw::FILTER_LINEAR);   // NOTE: Unimplemented, fail silently
+					break;
+				default:
+					return INVALIDCALL();
+				};
+				break;
+			case D3DTSS_MINFILTER:
+				// NOTE: SwiftShader does not differentiate between minification and magnification filter
+				switch(value)
+				{
+				case D3DTEXF_NONE:
+					renderer->setTextureFilter(sw::SAMPLER_PIXEL, stage, sw::FILTER_POINT);
+					break;
+				case D3DTEXF_POINT:
+					renderer->setTextureFilter(sw::SAMPLER_PIXEL, stage, sw::FILTER_POINT);
+					break;
+				case D3DTEXF_LINEAR:
+					renderer->setTextureFilter(sw::SAMPLER_PIXEL, stage, sw::FILTER_LINEAR);
+					break;
+				case D3DTEXF_ANISOTROPIC:
+					renderer->setTextureFilter(sw::SAMPLER_PIXEL, stage, sw::FILTER_ANISOTROPIC);
+					break;
+				case D3DTEXF_FLATCUBIC:
+					renderer->setTextureFilter(sw::SAMPLER_PIXEL, stage, sw::FILTER_LINEAR);   // NOTE: Unimplemented, fail silently
+					break;
+				case D3DTEXF_GAUSSIANCUBIC:
+					renderer->setTextureFilter(sw::SAMPLER_PIXEL, stage, sw::FILTER_LINEAR);   // NOTE: Unimplemented, fail silently
+					break;
+				default:
+					return INVALIDCALL();
+				};
+				break;
+			case D3DTSS_MIPFILTER:
+				switch(value)
+				{
+				case D3DTEXF_NONE:
+					renderer->setMipmapFilter(sw::SAMPLER_PIXEL, stage, sw::MIPMAP_NONE);
+					break;
+				case D3DTEXF_POINT:
+					renderer->setMipmapFilter(sw::SAMPLER_PIXEL, stage, sw::MIPMAP_POINT);
+					break;
+				case D3DTEXF_LINEAR:
+					renderer->setMipmapFilter(sw::SAMPLER_PIXEL, stage, sw::MIPMAP_LINEAR);
+					break;
+				case D3DTEXF_ANISOTROPIC:
+					renderer->setMipmapFilter(sw::SAMPLER_PIXEL, stage, sw::MIPMAP_LINEAR);   // NOTE: Unimplemented, fail silently
+					break;
+				case D3DTEXF_FLATCUBIC:
+					renderer->setMipmapFilter(sw::SAMPLER_PIXEL, stage, sw::MIPMAP_LINEAR);   // NOTE: Unimplemented, fail silently
+					break;
+				case D3DTEXF_GAUSSIANCUBIC:
+					renderer->setMipmapFilter(sw::SAMPLER_PIXEL, stage, sw::MIPMAP_LINEAR);   // NOTE: Unimplemented, fail silently
+					break;
+				default:
+					return INVALIDCALL();
+				};
+				break;
+			case D3DTSS_MIPMAPLODBIAS:
+				{
+					float LOD = (float&)value - sw::log2((float)context->renderTarget[0]->getSuperSampleCount());   // FIXME: Update when render target changes
+					renderer->setMipmapLOD(sw::SAMPLER_PIXEL, stage, LOD);
+				}
+				break;
+			case D3DTSS_MAXMIPLEVEL:
+				break;
+			case D3DTSS_MAXANISOTROPY:
+				renderer->setMaxAnisotropy(sw::SAMPLER_PIXEL, stage, sw::clamp((unsigned int)value, (unsigned int)1, maxAnisotropy));
+				break;
+			case D3DTSS_BUMPENVLSCALE:
+				renderer->setLuminanceScale(stage, (float&)value);
+				break;
+			case D3DTSS_BUMPENVLOFFSET:
+				renderer->setLuminanceOffset(stage, (float&)value);
+				break;
+			case D3DTSS_TEXTURETRANSFORMFLAGS:
+				switch(value & ~D3DTTFF_PROJECTED)
+				{
+				case D3DTTFF_DISABLE:
+					renderer->setTextureTransform(stage, 0, (value &  D3DTTFF_PROJECTED) == D3DTTFF_PROJECTED);
+					break;
+				case D3DTTFF_COUNT1:
+					renderer->setTextureTransform(stage, 1, (value &  D3DTTFF_PROJECTED) == D3DTTFF_PROJECTED);
+					break;
+				case D3DTTFF_COUNT2:
+					renderer->setTextureTransform(stage, 2, (value &  D3DTTFF_PROJECTED) == D3DTTFF_PROJECTED);
+					break;
+				case D3DTTFF_COUNT3:
+					renderer->setTextureTransform(stage, 3, (value &  D3DTTFF_PROJECTED) == D3DTTFF_PROJECTED);
+					break;
+				case D3DTTFF_COUNT4:
+					renderer->setTextureTransform(stage, 4, (value &  D3DTTFF_PROJECTED) == D3DTTFF_PROJECTED);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_ADDRESSW:
+				switch(value)
+				{
+				case D3DTADDRESS_WRAP:
+					renderer->setAddressingModeW(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_WRAP);
+					break;
+				case D3DTADDRESS_MIRROR:
+					renderer->setAddressingModeW(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_MIRROR);
+					break;
+				case D3DTADDRESS_CLAMP:
+					renderer->setAddressingModeW(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_CLAMP);
+					break;
+				case D3DTADDRESS_BORDER:
+					renderer->setAddressingModeW(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_BORDER);
+					break;
+				case D3DTADDRESS_MIRRORONCE:
+					renderer->setAddressingModeW(sw::SAMPLER_PIXEL, stage, sw::ADDRESSING_MIRRORONCE);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_COLORARG0:
+				switch(value & D3DTA_SELECTMASK)
+				{
+				case D3DTA_CURRENT:
+					renderer->setThirdArgument(stage, sw::TextureStage::SOURCE_CURRENT);
+					break;
+				case D3DTA_DIFFUSE:
+					renderer->setThirdArgument(stage, sw::TextureStage::SOURCE_DIFFUSE);
+					break;
+				case D3DTA_SPECULAR:
+					renderer->setThirdArgument(stage, sw::TextureStage::SOURCE_SPECULAR);
+					break;
+				case D3DTA_TEMP:
+					renderer->setThirdArgument(stage, sw::TextureStage::SOURCE_TEMP);
+					break;
+				case D3DTA_TEXTURE:
+					renderer->setThirdArgument(stage, sw::TextureStage::SOURCE_TEXTURE);
+					break;
+				case D3DTA_TFACTOR:
+					renderer->setThirdArgument(stage, sw::TextureStage::SOURCE_TFACTOR);
+					break;
+				default:
+					ASSERT(false);
+				}
+
+				switch(value & ~D3DTA_SELECTMASK)
+				{
+				case 0:
+					renderer->setThirdModifier(stage, sw::TextureStage::MODIFIER_COLOR);
+					break;
+				case D3DTA_COMPLEMENT:
+					renderer->setThirdModifier(stage, sw::TextureStage::MODIFIER_INVCOLOR);
+					break;
+				case D3DTA_ALPHAREPLICATE:
+					renderer->setThirdModifier(stage, sw::TextureStage::MODIFIER_ALPHA);
+					break;
+				case D3DTA_COMPLEMENT | D3DTA_ALPHAREPLICATE:
+					renderer->setThirdModifier(stage, sw::TextureStage::MODIFIER_INVALPHA);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_ALPHAARG0:
+				switch(value & D3DTA_SELECTMASK)
+				{
+				case D3DTA_DIFFUSE:
+					renderer->setThirdArgumentAlpha(stage, sw::TextureStage::SOURCE_DIFFUSE);
+					break;
+				case D3DTA_CURRENT:
+					renderer->setThirdArgumentAlpha(stage, sw::TextureStage::SOURCE_CURRENT);
+					break;
+				case D3DTA_TEXTURE:
+					renderer->setThirdArgumentAlpha(stage, sw::TextureStage::SOURCE_TEXTURE);
+					break;
+				case D3DTA_TFACTOR:
+					renderer->setThirdArgumentAlpha(stage, sw::TextureStage::SOURCE_TFACTOR);
+					break;
+				case D3DTA_SPECULAR:
+					renderer->setThirdArgumentAlpha(stage, sw::TextureStage::SOURCE_SPECULAR);
+					break;
+				case D3DTA_TEMP:
+					renderer->setThirdArgumentAlpha(stage, sw::TextureStage::SOURCE_TEMP);
+					break;
+				default:
+					ASSERT(false);
+				}
+
+				switch(value & ~D3DTA_SELECTMASK)
+				{
+				case 0:
+					renderer->setThirdModifierAlpha(stage, sw::TextureStage::MODIFIER_COLOR);
+					break;
+				case D3DTA_COMPLEMENT:
+					renderer->setThirdModifierAlpha(stage, sw::TextureStage::MODIFIER_INVCOLOR);
+					break;
+				case D3DTA_ALPHAREPLICATE:
+					renderer->setThirdModifierAlpha(stage, sw::TextureStage::MODIFIER_ALPHA);
+					break;
+				case D3DTA_COMPLEMENT | D3DTA_ALPHAREPLICATE:
+					renderer->setThirdModifierAlpha(stage, sw::TextureStage::MODIFIER_INVALPHA);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			case D3DTSS_RESULTARG:
+				switch(value & D3DTA_SELECTMASK)
+				{
+				case D3DTA_CURRENT:
+					renderer->setDestinationArgument(stage, sw::TextureStage::DESTINATION_CURRENT);
+					break;
+				case D3DTA_TEMP:
+					renderer->setDestinationArgument(stage, sw::TextureStage::DESTINATION_TEMP);
+					break;
+				default:
+					ASSERT(false);
+				}
+				break;
+			default:
+				ASSERT(false);
+			}
+		}
+		else   // stateRecorder
+		{
+			stateRecorder.back()->setTextureStageState(stage, type, value);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetTransform(D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
+	{
+		TRACE("");
+
+		if(!matrix || state < 0 || state > 511)
+		{
+			return INVALIDCALL();
+		}
+
+		if(!recordState)
+		{
+			this->matrix[state] = *matrix;
+
+			sw::Matrix M(matrix->_11, matrix->_21, matrix->_31, matrix->_41,
+			             matrix->_12, matrix->_22, matrix->_32, matrix->_42,
+			             matrix->_13, matrix->_23, matrix->_33, matrix->_43,
+			             matrix->_14, matrix->_24, matrix->_34, matrix->_44);
+
+			switch(state)
+			{
+			case D3DTS_WORLD:
+				renderer->setModelMatrix(M);
+				break;
+			case D3DTS_VIEW:
+				renderer->setViewMatrix(M);
+				break;
+			case D3DTS_PROJECTION:
+				renderer->setProjectionMatrix(M);
+				break;
+			case D3DTS_TEXTURE0:
+				renderer->setTextureMatrix(0, M);
+				break;
+			case D3DTS_TEXTURE1:
+				renderer->setTextureMatrix(1, M);
+				break;
+			case D3DTS_TEXTURE2:
+				renderer->setTextureMatrix(2, M);
+				break;
+			case D3DTS_TEXTURE3:
+				renderer->setTextureMatrix(3, M);
+				break;
+			case D3DTS_TEXTURE4:
+				renderer->setTextureMatrix(4, M);
+				break;
+			case D3DTS_TEXTURE5:
+				renderer->setTextureMatrix(5, M);
+				break;
+			case D3DTS_TEXTURE6:
+				renderer->setTextureMatrix(6, M);
+				break;
+			case D3DTS_TEXTURE7:
+				renderer->setTextureMatrix(7, M);
+				break;
+			default:
+				if(state > 256 && state < 512)
+				{
+					renderer->setModelMatrix(M, state - 256);
+				}
+				else ASSERT(false);
+			}
+		}
+		else   // stateRecorder
+		{
+			stateRecorder.back()->setTransform(state, matrix);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetVertexShader(unsigned long handle)
+	{
+		TRACE("");
+
+		if(!recordState)
+		{
+			if(handle & 0x00000001)
+			{
+				unsigned int index = handle >> 16;
+
+				if(vertexShader[index])
+				{
+					vertexShader[index]->bind();
+				}
+
+				if(vertexShader[vertexShaderHandle >> 16])
+				{
+					vertexShader[vertexShaderHandle >> 16]->unbind();
+				}
+
+				vertexShaderHandle = handle;
+
+				Direct3DVertexShader8 *shader = vertexShader[index];
+				renderer->setVertexShader(shader->getVertexShader());
+				declaration = shader->getDeclaration();
+				
+				FVF = 0;
+			}
+			else
+			{
+				renderer->setVertexShader(0);
+				declaration = 0;
+				
+				FVF = handle;
+			}
+		}
+		else
+		{
+			stateRecorder.back()->setVertexShader(handle);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetVertexShaderConstant(unsigned long startRegister, const void *constantData, unsigned long count)
+	{
+		TRACE("");
+
+		if(!constantData)
+		{
+			return INVALIDCALL();
+		}
+
+		if(!recordState)
+		{
+			for(unsigned int i = 0; i < count; i++)
+			{
+				vertexShaderConstant[startRegister + i][0] = ((float*)constantData)[i * 4 + 0];
+				vertexShaderConstant[startRegister + i][1] = ((float*)constantData)[i * 4 + 1];
+				vertexShaderConstant[startRegister + i][2] = ((float*)constantData)[i * 4 + 2];
+				vertexShaderConstant[startRegister + i][3] = ((float*)constantData)[i * 4 + 3];
+			}
+
+			renderer->setVertexShaderConstantF(startRegister, (const float*)constantData, count);
+		}
+		else
+		{
+			stateRecorder.back()->setVertexShaderConstant(startRegister, constantData, count);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetViewport(const D3DVIEWPORT8 *viewport)
+	{
+		TRACE("");
+
+		if(!viewport)
+		{
+			return INVALIDCALL();
+		}
+
+		if(!recordState)
+		{
+			this->viewport = *viewport;
+		}
+		else
+		{
+			stateRecorder.back()->setViewport(viewport);
+		}
+		
+		return D3D_OK;
+	}
+
+	int Direct3DDevice8::ShowCursor(int show)
+	{
+		TRACE("");
+
+		int oldValue = showCursor ? TRUE : FALSE;
+
+		showCursor = show != FALSE && cursor;
+
+		bindCursor();
+
+		return oldValue;
+	}
+
+	long Direct3DDevice8::TestCooperativeLevel()
+	{
+		TRACE("");
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::UpdateTexture(IDirect3DBaseTexture8 *sourceTexture, IDirect3DBaseTexture8 *destinationTexture)
+	{
+		TRACE("");
+
+		if(!sourceTexture || !destinationTexture)
+		{
+			return INVALIDCALL();
+		}
+
+		D3DRESOURCETYPE type = sourceTexture->GetType();
+
+		if(type != destinationTexture->GetType())
+		{
+			return INVALIDCALL();
+		}
+
+		switch(type)
+		{
+		case D3DRTYPE_TEXTURE:
+			{
+				IDirect3DTexture8 *source;
+				IDirect3DTexture8 *dest;
+
+				sourceTexture->QueryInterface(IID_IDirect3DTexture8, (void**)&source);
+				destinationTexture->QueryInterface(IID_IDirect3DTexture8, (void**)&dest);
+
+				ASSERT(source && dest);
+
+				for(unsigned int level = 0; level < source->GetLevelCount() && level < dest->GetLevelCount(); level++)
+				{
+					IDirect3DSurface8 *sourceSurface;
+					IDirect3DSurface8 *destinationSurface;
+
+					source->GetSurfaceLevel(level, &sourceSurface);
+					dest->GetSurfaceLevel(level, &destinationSurface);
+
+					updateSurface(sourceSurface, 0, destinationSurface, 0);
+
+					sourceSurface->Release();
+					destinationSurface->Release();
+				}
+
+				source->Release();
+				dest->Release();
+			}
+			break;
+		case D3DRTYPE_VOLUMETEXTURE:
+			{
+				IDirect3DVolumeTexture8 *source;
+				IDirect3DVolumeTexture8 *dest;
+
+				sourceTexture->QueryInterface(IID_IDirect3DVolumeTexture8, (void**)&source);
+				destinationTexture->QueryInterface(IID_IDirect3DVolumeTexture8, (void**)&dest);
+
+				ASSERT(source && dest);
+
+				for(unsigned int level = 0; level < source->GetLevelCount() && level < dest->GetLevelCount(); level++)   // FIXME: Fail when source texture has fewer levels than the destination
+				{
+					IDirect3DVolume8 *sourceVolume;
+					IDirect3DVolume8 *destinationVolume;
+
+					source->GetVolumeLevel(level, &sourceVolume);
+					dest->GetVolumeLevel(level, &destinationVolume);
+
+					updateVolume(sourceVolume, destinationVolume);
+
+					sourceVolume->Release();
+					destinationVolume->Release();
+				}
+
+				source->Release();
+				dest->Release();
+			}
+			break;
+		case D3DRTYPE_CUBETEXTURE:
+			{
+				IDirect3DCubeTexture8 *source;
+				IDirect3DCubeTexture8 *dest;
+
+				sourceTexture->QueryInterface(IID_IDirect3DCubeTexture8, (void**)&source);
+				destinationTexture->QueryInterface(IID_IDirect3DCubeTexture8, (void**)&dest);
+
+				ASSERT(source && dest);
+
+				for(int face = 0; face < 6; face++)
+				{
+					for(unsigned int level = 0; level < source->GetLevelCount() && level < dest->GetLevelCount(); level++)
+					{
+						IDirect3DSurface8 *sourceSurface;
+						IDirect3DSurface8 *destinationSurface;
+
+						source->GetCubeMapSurface((D3DCUBEMAP_FACES)face, level, &sourceSurface);
+						dest->GetCubeMapSurface((D3DCUBEMAP_FACES)face, level, &destinationSurface);
+
+						updateSurface(sourceSurface, 0, destinationSurface, 0);
+
+						sourceSurface->Release();
+						destinationSurface->Release();
+					}
+				}
+
+				source->Release();
+				dest->Release();
+			}
+			break;
+		default:
+			ASSERT(false);
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::ValidateDevice(unsigned long *numPasses)
+	{
+		TRACE("");
+
+		if(!numPasses)
+		{
+			return INVALIDCALL();
+		}
+
+		*numPasses = 1;
+
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::updateSurface(IDirect3DSurface8 *sourceSurface, const RECT *sourceRect, IDirect3DSurface8 *destinationSurface, const POINT *destPoint)
+	{
+		TRACE("IDirect3DSurface8 *sourceSurface = 0x%0.8p, const RECT *sourceRect = 0x%0.8p, IDirect3DSurface8 *destinationSurface = 0x%0.8p, const POINT *destPoint = 0x%0.8p", sourceSurface, sourceRect, destinationSurface, destPoint);
+
+		if(!sourceSurface || !destinationSurface)
+		{
+			return INVALIDCALL();
+		}
+
+		D3DSURFACE_DESC sourceDescription;
+		D3DSURFACE_DESC destinationDescription;
+
+		sourceSurface->GetDesc(&sourceDescription);
+		destinationSurface->GetDesc(&destinationDescription);
+
+		RECT sRect;
+		RECT dRect;
+		
+		if(sourceRect && destPoint)
+		{
+			sRect.left = sourceRect->left;
+			sRect.top = sourceRect->top;
+			sRect.right = sourceRect->right;
+			sRect.bottom = sourceRect->bottom;
+
+			dRect.left = destPoint->x;
+			dRect.top = destPoint->y;
+			dRect.right = destPoint->x + sourceRect->right - sourceRect->left;
+			dRect.bottom = destPoint->y + sourceRect->bottom - sourceRect->top;
+		}
+		else
+		{
+			sRect.left = 0;
+			sRect.top = 0;
+			sRect.right = sourceDescription.Width;
+			sRect.bottom = sourceDescription.Height;
+
+			dRect.left = 0;
+			dRect.top = 0;
+			dRect.right = destinationDescription.Width;
+			dRect.bottom = destinationDescription.Height;
+		}
+
+		int sWidth = sRect.right - sRect.left;
+		int sHeight = sRect.bottom - sRect.top;
+
+		int dWidth = dRect.right - dRect.left;
+		int dHeight = dRect.bottom - dRect.top;
+
+		if(sourceDescription.MultiSampleType      != D3DMULTISAMPLE_NONE ||
+		   destinationDescription.MultiSampleType != D3DMULTISAMPLE_NONE ||
+		// sourceDescription.Pool      != D3DPOOL_SYSTEMMEM ||   // FIXME: Check back buffer and depth buffer memory pool flags
+		// destinationDescription.Pool != D3DPOOL_DEFAULT ||
+		   sourceDescription.Format != destinationDescription.Format ||
+		   sWidth  != dWidth ||
+		   sHeight != dHeight)
+		{
+			return INVALIDCALL();
+		}
+
+		D3DLOCKED_RECT sourceLock;
+		D3DLOCKED_RECT destinationLock;
+
+		sourceSurface->LockRect(&sourceLock, &sRect, D3DLOCK_READONLY);
+		destinationSurface->LockRect(&destinationLock, &dRect, 0);
+
+		unsigned int width;
+		unsigned int height;
+		unsigned int bytes;
+
+		switch(sourceDescription.Format)
+		{
+		case D3DFMT_DXT1:
+			width = (dWidth + 3) / 4;
+			height = (dHeight + 3) / 4;
+			bytes = width * 8;   // 64 bit per 4x4 block
+			break;
+		case D3DFMT_DXT2:
+		case D3DFMT_DXT3:
+		case D3DFMT_DXT4:
+		case D3DFMT_DXT5:
+			width = (dWidth + 3) / 4;
+			height = (dHeight + 3) / 4;
+			bytes = width * 16;   // 128 bit per 4x4 block
+			break;
+		default:
+			width = dWidth;
+			height = dHeight;
+			bytes = width * Direct3DSurface8::bytes(sourceDescription.Format);
+		}
+
+		for(unsigned int y = 0; y < height; y++)
+		{
+			memcpy(destinationLock.pBits, sourceLock.pBits, bytes);
+			
+			(byte*&)sourceLock.pBits += sourceLock.Pitch;
+			(byte*&)destinationLock.pBits += destinationLock.Pitch;
+		}
+
+		sourceSurface->UnlockRect();
+		destinationSurface->UnlockRect();
+		
+		return D3D_OK;
+	}
+
+	long Direct3DDevice8::SetIndices(IDirect3DIndexBuffer8 *iIndexBuffer, unsigned int baseVertexIndex)
+	{
+		TRACE("");
+
+		Direct3DIndexBuffer8 *indexBuffer = static_cast<Direct3DIndexBuffer8*>(iIndexBuffer);
+
+		if(!recordState)
+		{
+			if(indexBuffer)
+			{
+				indexBuffer->bind();
+			}
+
+			if(this->indexData)
+			{
+				this->indexData->unbind();
+			}
+
+			this->indexData = indexBuffer;
+			this->baseVertexIndex = baseVertexIndex;
+		}
+		else
+		{
+			stateRecorder.back()->setIndices(indexBuffer, baseVertexIndex);
+		}
+
+		return D3D_OK;
+	}
+
+	int Direct3DDevice8::FVFStride(unsigned long FVF)
+	{
+		int stride = 0;
+
+		switch(FVF & D3DFVF_POSITION_MASK)
+		{
+		case D3DFVF_XYZ:	stride += 12;	break;
+		case D3DFVF_XYZRHW:	stride += 16;	break;
+		case D3DFVF_XYZB1:	stride += 16;	break;
+		case D3DFVF_XYZB2:	stride += 20;	break;
+		case D3DFVF_XYZB3:	stride += 24;	break;
+		case D3DFVF_XYZB4:	stride += 28;	break;
+		case D3DFVF_XYZB5:	stride += 32;	break;
+		}
+
+		if(FVF & D3DFVF_NORMAL)		stride += 12;
+		if(FVF & D3DFVF_PSIZE)		stride += 4;
+		if(FVF & D3DFVF_DIFFUSE)	stride += 4;
+		if(FVF & D3DFVF_SPECULAR)	stride += 4;
+		
+		switch((FVF & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT)
+		{
+		case 8: stride += 4 + 4 * ((1 + (FVF >> 30)) % 4);
+		case 7: stride += 4 + 4 * ((1 + (FVF >> 28)) % 4);
+		case 6: stride += 4 + 4 * ((1 + (FVF >> 26)) % 4);
+		case 5: stride += 4 + 4 * ((1 + (FVF >> 24)) % 4);
+		case 4: stride += 4 + 4 * ((1 + (FVF >> 22)) % 4);
+		case 3: stride += 4 + 4 * ((1 + (FVF >> 20)) % 4);
+		case 2: stride += 4 + 4 * ((1 + (FVF >> 18)) % 4);
+		case 1: stride += 4 + 4 * ((1 + (FVF >> 16)) % 4);
+		case 0: break;
+		default:
+			ASSERT(false);
+		}
+
+		return stride;
+	}
+
+	int Direct3DDevice8::typeStride(unsigned char type)
+	{
+		static const int LUT[] =
+		{
+			4,	// D3DDECLTYPE_FLOAT1    =  0,  // 1D float expanded to (value, 0., 0., 1.)
+			8,	// D3DDECLTYPE_FLOAT2    =  1,  // 2D float expanded to (value, value, 0., 1.)
+			12,	// D3DDECLTYPE_FLOAT3    =  2,  // 3D float expanded to (value, value, value, 1.)
+			16,	// D3DDECLTYPE_FLOAT4    =  3,  // 4D float
+			4,	// D3DDECLTYPE_D3DCOLOR  =  4,  // 4D packed unsigned bytes mapped to 0. to 1. range. Input is in D3DCOLOR format (ARGB) expanded to (R, G, B, A)
+			4,	// D3DDECLTYPE_UBYTE4    =  5,  // 4D unsigned byte
+			4,	// D3DDECLTYPE_SHORT2    =  6,  // 2D signed short expanded to (value, value, 0., 1.)
+			8 	// D3DDECLTYPE_SHORT4    =  7,  // 4D signed short
+		};
+
+		if(type <= 7)
+		{
+			return LUT[type];
+		}
+		else ASSERT(false);
+
+		return 0;
+	}
+
+	bool Direct3DDevice8::bindData(Direct3DIndexBuffer8 *indexBuffer, int base)
+	{
+		if(!bindViewport())
+		{
+			return false;   // Zero-area target region
+		}
+
+		bindTextures();
+		bindStreams(base);
+		bindIndexBuffer(indexBuffer);
+		bindLights();
+
+		return true;
+	}
+
+	void Direct3DDevice8::bindStreams(int base)
+	{
+		renderer->resetInputStreams((FVF & D3DFVF_POSITION_MASK) == D3DFVF_XYZRHW);
+
+		int stride;
+
+		if(!declaration)   // Fixed-function vertex pipeline
+		{
+			const void *buffer = 0;
+
+			ASSERT(dataStream[0]);
+
+			Direct3DVertexBuffer8 *stream = dataStream[0];
+			sw::Resource *resource = stream->getResource();
+			buffer = (char*)resource->data();
+			stride = FVFStride(FVF);
+
+			ASSERT(stride == streamStride[0]);   // FIXME
+			ASSERT(buffer && stride);
+
+			(char*&)buffer += stride * base;
+
+			sw::Stream attribute(resource, buffer, stride);
+
+			switch(FVF & D3DFVF_POSITION_MASK)
+			{
+			case D3DFVF_XYZ:
+				renderer->setInputStream(sw::Position, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 3));
+				(char*&)buffer += 12;
+				break;
+			case D3DFVF_XYZRHW:
+				renderer->setInputStream(sw::PositionT, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 4));
+				(char*&)buffer += 16;
+				break;
+			case D3DFVF_XYZB1:
+				renderer->setInputStream(sw::Position, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 3));
+				(char*&)buffer += 12;
+
+				renderer->setInputStream(sw::BlendWeight, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 1));   // FIXME: Stream type depends on indexed blending active?
+				(char*&)buffer += 4;
+				break;
+			case D3DFVF_XYZB2:
+				renderer->setInputStream(sw::Position, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 3));
+				(char*&)buffer += 12;
+
+				renderer->setInputStream(sw::BlendWeight, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 1));   // FIXME: Stream type depends on indexed blending active?
+				(char*&)buffer += 8;
+				break;
+			case D3DFVF_XYZB3:
+				renderer->setInputStream(sw::Position, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 3));
+				(char*&)buffer += 12;
+
+				renderer->setInputStream(sw::BlendWeight, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 2));   // FIXME: Stream type depends on indexed blending active?
+				(char*&)buffer += 12;
+				break;
+			case D3DFVF_XYZB4:
+				renderer->setInputStream(sw::Position, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 3));
+				(char*&)buffer += 12;
+
+				renderer->setInputStream(sw::BlendWeight, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 3));   // FIXME: Stream type depends on indexed blending active?
+				(char*&)buffer += 16;
+				break;
+			case D3DFVF_XYZB5:
+				renderer->setInputStream(sw::Position, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 3));
+				(char*&)buffer += 12;
+
+				renderer->setInputStream(sw::BlendWeight, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 4));   // FIXME: Stream type depends on indexed blending active?
+				(char*&)buffer += 20;
+				break;
+			}
+
+			if(FVF & D3DFVF_LASTBETA_UBYTE4)
+			{
+				renderer->setInputStream(sw::BlendIndices, attribute.define((char*&)buffer - 4, sw::STREAMTYPE_INDICES, 1));
+			}
+			
+			if(FVF & D3DFVF_NORMAL)
+			{
+				renderer->setInputStream(sw::Normal, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 3));
+				(char*&)buffer += 12;
+			}
+			
+			if(FVF & D3DFVF_PSIZE)
+			{
+				renderer->setInputStream(sw::PointSize, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 1));
+				(char*&)buffer += 4;
+			}
+			
+			if(FVF & D3DFVF_DIFFUSE)
+			{
+				renderer->setInputStream(sw::Color0, attribute.define(buffer, sw::STREAMTYPE_COLOR, 4));
+				(char*&)buffer += 4;
+			}
+			
+			if(FVF & D3DFVF_SPECULAR)
+			{
+				renderer->setInputStream(sw::Color1, attribute.define(buffer, sw::STREAMTYPE_COLOR, 4));
+				(char*&)buffer += 4;
+			}
+
+			for(unsigned int i = 0; i < 8; i++)
+			{
+				if((FVF & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT >= i + 1)
+				{
+					renderer->setInputStream(sw::TexCoord0 + i, attribute.define(buffer, sw::STREAMTYPE_FLOAT, 1 + (1 + (FVF >> (16 + i * 2))) % 4));
+					(char*&)buffer += 4 + 4 * ((1 + (FVF >> (16 + i * 2))) % 4);
+				}
+			}
+		}
+		else
+		{
+			const unsigned long *element = declaration;
+			int stream = 0;
+			sw::Resource *resource;
+			const void *buffer = 0;
+
+			while(*element != 0xFFFFFFFF)
+			{
+				switch((*element & 0xE0000000) >> 29)
+				{
+				case 0:   // NOP
+					if(*element != 0x00000000)
+					{
+						ASSERT(false);
+					}
+					break;
+				case 1:   // Stream selector
+					stream = *element & 0x0000000F;
+					{
+						ASSERT(dataStream[stream]);   // Expected a stream
+
+						Direct3DVertexBuffer8 *streamBuffer = (Direct3DVertexBuffer8*)dataStream[stream];
+						resource = streamBuffer->getResource();
+						buffer = (char*)resource->data();
+
+						const unsigned long *streamElement = element + 1;
+						stride = 0;
+
+						while((*streamElement & 0xE0000000) >> 29 == 2)   // Data definition
+						{
+							if(*streamElement & 0x10000000)   // Data skip
+							{
+								int skip = (*streamElement & 0x000F0000) >> 16;
+
+								stride += 4 * skip;
+							}
+							else
+							{
+								stride += typeStride((unsigned char)((*streamElement & 0x000F0000) >> 16));
+							}
+
+							streamElement++;
+						}
+
+					//	ASSERT(stride == streamStride[stream]);   // FIXME: Probably just ignore
+
+						(char*&)buffer += stride * base;
+					}
+					break;
+				case 2:   // Data definition
+					if(*element & 0x10000000)   // Data skip
+					{
+						int skip = (*element & 0x000F0000) >> 16;
+
+						(char*&)buffer += 4 * skip;
+					}
+					else
+					{
+						int type = (*element & 0x000F0000) >> 16;
+						int index = (*element & 0x0000000F) >> 0;
+
+						sw::Stream attribute(resource, buffer, stride);
+
+						switch(type)
+						{
+						case D3DVSDT_FLOAT1:   attribute.define(sw::STREAMTYPE_FLOAT, 1); break;
+						case D3DVSDT_FLOAT2:   attribute.define(sw::STREAMTYPE_FLOAT, 2); break;
+						case D3DVSDT_FLOAT3:   attribute.define(sw::STREAMTYPE_FLOAT, 3); break;
+						case D3DVSDT_FLOAT4:   attribute.define(sw::STREAMTYPE_FLOAT, 4); break;
+						case D3DVSDT_D3DCOLOR: attribute.define(sw::STREAMTYPE_COLOR, 4); break;
+						case D3DVSDT_UBYTE4:   attribute.define(sw::STREAMTYPE_BYTE, 4);  break;
+						case D3DVSDT_SHORT2:   attribute.define(sw::STREAMTYPE_SHORT, 2); break;
+						case D3DVSDT_SHORT4:   attribute.define(sw::STREAMTYPE_SHORT, 4); break;
+						default:               attribute.define(sw::STREAMTYPE_FLOAT, 0); ASSERT(false);
+						}
+
+						switch(index)
+						{
+						case D3DVSDE_POSITION:     renderer->setInputStream(sw::Position, attribute);     break;
+						case D3DVSDE_BLENDWEIGHT:  renderer->setInputStream(sw::BlendWeight, attribute);  break;
+						case D3DVSDE_BLENDINDICES: renderer->setInputStream(sw::BlendIndices, attribute); break;
+						case D3DVSDE_NORMAL:       renderer->setInputStream(sw::Normal, attribute);       break;
+						case D3DVSDE_PSIZE:        renderer->setInputStream(sw::PointSize, attribute);    break;
+						case D3DVSDE_DIFFUSE:      renderer->setInputStream(sw::Color0, attribute);       break;
+						case D3DVSDE_SPECULAR:     renderer->setInputStream(sw::Color1, attribute);       break;
+						case D3DVSDE_TEXCOORD0:    renderer->setInputStream(sw::TexCoord0, attribute);    break;
+						case D3DVSDE_TEXCOORD1:    renderer->setInputStream(sw::TexCoord1, attribute);    break;
+						case D3DVSDE_TEXCOORD2:    renderer->setInputStream(sw::TexCoord2, attribute);    break;
+						case D3DVSDE_TEXCOORD3:    renderer->setInputStream(sw::TexCoord3, attribute);    break;
+						case D3DVSDE_TEXCOORD4:    renderer->setInputStream(sw::TexCoord4, attribute);    break;
+						case D3DVSDE_TEXCOORD5:    renderer->setInputStream(sw::TexCoord5, attribute);    break;
+						case D3DVSDE_TEXCOORD6:    renderer->setInputStream(sw::TexCoord6, attribute);    break;
+						case D3DVSDE_TEXCOORD7:    renderer->setInputStream(sw::TexCoord7, attribute);    break;
+					//	case D3DVSDE_POSITION2:    renderer->setInputStream(sw::Position1, attribute);    break;
+					//	case D3DVSDE_NORMAL2:      renderer->setInputStream(sw::Normal1, attribute);      break;
+						default:
+							ASSERT(false);
+						}
+
+						(char*&)buffer += typeStride(type);
+					}
+					break;
+				case 3:   // Tesselator data
+					UNIMPLEMENTED();
+					break;
+				case 4:   // Constant data
+					{
+						int count = (*element & 0x1E000000) >> 25;
+						int index = (*element & 0x0000007F) >> 0;
+
+						SetVertexShaderConstant(index, element + 1, count);
+
+						element += 4 * count;
+					}
+					break;
+				case 5:   // Extension
+					UNIMPLEMENTED();
+					break;
+				default:
+					ASSERT(false);
+				}
+
+				element++;
+			}
+		}
+	}
+
+	void Direct3DDevice8::bindIndexBuffer(Direct3DIndexBuffer8 *indexBuffer)
+	{
+		sw::Resource *resource = 0;
+
+		if(indexBuffer)
+		{
+			resource = indexBuffer->getResource();
+		}
+
+		renderer->setIndexBuffer(resource);
+	}
+
+	void Direct3DDevice8::bindLights()
+	{
+		if(!lightsDirty) return;
+
+		Lights::iterator i = light.begin();
+		int active = 0;
+
+		// Set and enable renderer lights
+		while(active < 8)
+		{
+			while(i != light.end() && !i->second.enable)
+			{
+				i++;
+			}
+
+			if(i == light.end())
+			{
+				break;
+			}
+
+			const Light &l = i->second;
+
+			sw::Point position(l.Position.x, l.Position.y, l.Position.z);
+			sw::Color<float> diffuse(l.Diffuse.r, l.Diffuse.g, l.Diffuse.b, l.Diffuse.a);
+			sw::Color<float> specular(l.Specular.r, l.Specular.g, l.Specular.b, l.Specular.a);
+			sw::Color<float> ambient(l.Ambient.r, l.Ambient.g, l.Ambient.b, l.Ambient.a);
+			sw::Vector direction(l.Direction.x, l.Direction.y, l.Direction.z);
+
+			renderer->setLightDiffuse(active, diffuse);
+			renderer->setLightSpecular(active, specular);
+			renderer->setLightAmbient(active, ambient);
+			
+			if(l.Type == D3DLIGHT_DIRECTIONAL)
+			{
+			//	goto next;   // FIXME
+
+				// FIXME: Unsupported, make it a positional light far away without falloff
+				renderer->setLightPosition(active, -1000 * direction);
+				renderer->setLightRange(active, l.Range);
+				renderer->setLightAttenuation(active, 1, 0, 0);
+			}
+			else if(l.Type == D3DLIGHT_SPOT)
+			{
+			//	goto next;   // FIXME
+
+				// FIXME: Unsupported, make it a positional light
+				renderer->setLightPosition(active, position);
+				renderer->setLightRange(active, l.Range);
+				renderer->setLightAttenuation(active, l.Attenuation0, l.Attenuation1, l.Attenuation2);
+			}
+			else
+			{
+				renderer->setLightPosition(active, position);
+				renderer->setLightRange(active, l.Range);
+				renderer->setLightAttenuation(active, l.Attenuation0, l.Attenuation1, l.Attenuation2);
+			}
+
+			renderer->setLightEnable(active, true);
+
+			active++;
+
+	//	next:   // FIXME
+			i++;
+		}
+
+		// Remaining lights are disabled
+		while(active < 8)
+		{
+			renderer->setLightEnable(active, false);
+
+			active++;
+		}
+
+		lightsDirty= false;
+	}
+
+	bool Direct3DDevice8::bindViewport()
+	{
+		if(viewport.Width == 0 || viewport.Height == 0)
+		{
+			return false;
+		}
+
+		sw::Viewport view;
+		view.x0 = (float)viewport.X;
+		view.y0 = (float)viewport.Y + viewport.Height;
+		view.width = (float)viewport.Width;
+		view.height = -(float)viewport.Height;
+		view.minZ = viewport.MinZ;
+		view.maxZ = viewport.MaxZ;
+
+		renderer->setViewport(view);
+
+		sw::Rect scissor;
+		scissor.x0 = viewport.X;
+		scissor.x1 = viewport.X + viewport.Width;
+		scissor.y0 = viewport.Y;
+		scissor.y1 = viewport.Y + viewport.Height;
+
+		renderer->setScissor(scissor);
+
+		return true;
+	}
+
+	void Direct3DDevice8::bindTextures()
+	{
+		for(int stage = 0; stage < 8; stage++)
+		{
+			Direct3DBaseTexture8 *baseTexture = texture[stage];
+			sw::Resource *resource = 0;
+
+			bool textureUsed = false;
+
+			if(pixelShader[pixelShaderHandle])
+			{
+				textureUsed = pixelShader[pixelShaderHandle]->getPixelShader()->usesSampler(stage);
+			}
+			else
+			{
+				textureUsed = true;   // FIXME: Check fixed-function use?
+			}
+
+			if(baseTexture && textureUsed)
+			{
+				resource = baseTexture->getResource();
+			}
+
+			renderer->setTextureResource(stage, resource);
+
+			if(baseTexture && textureUsed)
+			{
+				int levelCount = baseTexture->getInternalLevelCount();
+				
+				int textureLOD = baseTexture->GetLOD();
+				int stageLOD = textureStageState[stage][D3DTSS_MAXMIPLEVEL];
+				int LOD = textureLOD > stageLOD ? textureLOD : stageLOD;
+
+				if(textureStageState[stage][D3DTSS_MIPFILTER] == D3DTEXF_NONE)
+				{
+					LOD = 0;
+				}
+
+				switch(baseTexture->GetType())
+				{
+				case D3DRTYPE_TEXTURE:
+					{
+						Direct3DTexture8 *texture = dynamic_cast<Direct3DTexture8*>(baseTexture);
+						Direct3DSurface8 *surface;
+
+						for(int mipmapLevel = 0; mipmapLevel < sw::MIPMAP_LEVELS; mipmapLevel++)
+						{
+							int surfaceLevel = mipmapLevel;
+
+							if(surfaceLevel < LOD)
+							{
+								surfaceLevel = LOD;
+							}
+
+							if(surfaceLevel < 0)
+							{
+								surfaceLevel = 0;
+							}
+							else if(surfaceLevel >= levelCount)
+							{
+								surfaceLevel = levelCount - 1;
+							}
+
+							surface = texture->getInternalSurfaceLevel(surfaceLevel);
+							renderer->setTextureLevel(stage, 0, mipmapLevel, surface, sw::TEXTURE_2D);
+						}
+					}
+					break;
+				case D3DRTYPE_CUBETEXTURE:
+					for(int face = 0; face < 6; face++)
+					{
+						Direct3DCubeTexture8 *cubeTexture = dynamic_cast<Direct3DCubeTexture8*>(baseTexture);
+						Direct3DSurface8 *surface;
+
+						for(int mipmapLevel = 0; mipmapLevel < sw::MIPMAP_LEVELS; mipmapLevel++)
+						{
+							int surfaceLevel = mipmapLevel;
+
+							if(surfaceLevel < LOD)
+							{
+								surfaceLevel = LOD;
+							}
+
+							if(surfaceLevel < 0)
+							{
+								surfaceLevel = 0;
+							}
+							else if(surfaceLevel >= levelCount)
+							{
+								surfaceLevel = levelCount - 1;
+							}
+
+							surface = cubeTexture->getInternalCubeMapSurface((D3DCUBEMAP_FACES)face, surfaceLevel);
+							renderer->setTextureLevel(stage, face, mipmapLevel, surface, sw::TEXTURE_CUBE);
+						}
+					}
+					break;
+				case D3DRTYPE_VOLUMETEXTURE:
+					{
+						Direct3DVolumeTexture8 *volumeTexture = dynamic_cast<Direct3DVolumeTexture8*>(baseTexture);
+						Direct3DVolume8 *volume;
+
+						for(int mipmapLevel = 0; mipmapLevel < sw::MIPMAP_LEVELS; mipmapLevel++)
+						{
+							int surfaceLevel = mipmapLevel;
+
+							if(surfaceLevel < LOD)
+							{
+								surfaceLevel = LOD;
+							}
+
+							if(surfaceLevel < 0)
+							{
+								surfaceLevel = 0;
+							}
+							else if(surfaceLevel >= levelCount)
+							{
+								surfaceLevel = levelCount - 1;
+							}
+
+							volume = volumeTexture->getInternalVolumeLevel(surfaceLevel);
+							renderer->setTextureLevel(stage, 0, mipmapLevel, volume, sw::TEXTURE_3D);
+						}
+					}
+					break;
+				default:
+					UNIMPLEMENTED();
+				}
+			}
+			else
+			{
+				renderer->setTextureLevel(stage, 0, 0, 0, sw::TEXTURE_NULL);
+			}
+		}
+	}
+
+	void Direct3DDevice8::bindCursor()
+	{
+		if(showCursor)
+		{
+			sw::FrameBuffer::setCursorImage(cursor);
+
+			HCURSOR oldCursor = SetCursor(nullCursor);
+			
+			if(oldCursor != nullCursor)
+			{
+				win32Cursor = oldCursor;
+			}
+		}
+		else
+		{
+			sw::FrameBuffer::setCursorImage(0);
+
+			if(GetCursor() == nullCursor)
+			{
+				SetCursor(win32Cursor);
+			}
+		}
+	}
+
+	long Direct3DDevice8::updateVolume(IDirect3DVolume8 *sourceVolume, IDirect3DVolume8 *destinationVolume)
+	{
+		TRACE("IDirect3DVolume8 *sourceVolume = 0x%0.8p, IDirect3DVolume8 *destinationVolume = 0x%0.8p", sourceVolume, destinationVolume);
+
+		if(!sourceVolume || !destinationVolume)
+		{
+			return INVALIDCALL();
+		}
+
+		D3DVOLUME_DESC sourceDescription;
+		D3DVOLUME_DESC destinationDescription;
+
+		sourceVolume->GetDesc(&sourceDescription);
+		destinationVolume->GetDesc(&destinationDescription);
+
+		if(sourceDescription.Pool      != D3DPOOL_SYSTEMMEM ||
+		   destinationDescription.Pool != D3DPOOL_DEFAULT ||
+		   sourceDescription.Format != destinationDescription.Format ||
+		   sourceDescription.Width  != destinationDescription.Width ||
+		   sourceDescription.Height != destinationDescription.Height)
+		{
+			return INVALIDCALL();
+		}
+
+		D3DLOCKED_BOX sourceLock;
+		D3DLOCKED_BOX destinationLock;
+
+		sourceVolume->LockBox(&sourceLock, 0, 0);
+		destinationVolume->LockBox(&destinationLock, 0, 0);
+
+		if(sourceLock.RowPitch != destinationLock.RowPitch ||
+		   sourceLock.SlicePitch != destinationLock.SlicePitch)
+		{
+			UNIMPLEMENTED();
+		}
+
+		memcpy(destinationLock.pBits, sourceLock.pBits, sourceLock.SlicePitch * sourceDescription.Depth);
+
+		sourceVolume->UnlockBox();
+		destinationVolume->UnlockBox();
+		
+		return D3D_OK;
+	}
+
+	void Direct3DDevice8::configureFPU()
+	{
+		unsigned short cw;
+
+		__asm
+		{
+			fstcw cw
+			and cw, 0xFCFC   // Single-precision
+			or cw, 0x003F    // Mask all exceptions
+			and cw, 0xF3FF   // Round to nearest
+			fldcw cw
+		}
+	}
+}
diff --git a/src/D3D8/Direct3DDevice8.hpp b/src/D3D8/Direct3DDevice8.hpp
new file mode 100644
index 0000000..203849d
--- /dev/null
+++ b/src/D3D8/Direct3DDevice8.hpp
@@ -0,0 +1,272 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DDevice8_hpp
+#define D3D8_Direct3DDevice8_hpp
+
+#include "Unknown.hpp"
+
+#include "Direct3D8.hpp"
+#include "Direct3DStateBlock8.hpp"
+#include "Direct3DVertexDeclaration8.hpp"
+#include "Direct3DSwapChain8.hpp"
+
+#include "Stream.hpp"
+
+#include <d3d8.h>
+#include <vector>
+#include <list>
+#include <map>
+
+namespace sw
+{
+	class Renderer;
+	class Context;
+}
+
+namespace D3D8
+{
+	class Direct3DPixelShader8;
+	class Direct3DVertexShader8;
+	class Direct3DSurface8;
+	class Direct3DVertexBuffer8;
+	class Direct3DIndexBuffer8;
+
+	class Direct3DDevice8 : public IDirect3DDevice8, protected Unknown
+	{
+	public:
+		Direct3DDevice8(const HINSTANCE instance, Direct3D8 *d3d8, unsigned int adapter, D3DDEVTYPE deviceType, HWND focusWindow, unsigned long behaviourFlags, D3DPRESENT_PARAMETERS *presentParameters);
+
+		virtual ~Direct3DDevice8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3DDevice8 methods
+		long __stdcall ApplyStateBlock(unsigned long token);
+		long __stdcall BeginScene();
+		long __stdcall BeginStateBlock();
+		long __stdcall CaptureStateBlock(unsigned long token);
+		long __stdcall Clear(unsigned long count, const D3DRECT *rects, unsigned long flags, unsigned long color, float z, unsigned long stencil);
+		long __stdcall CopyRects(IDirect3DSurface8 *sourceSurface, const RECT *sourceRectsArray, unsigned int rects, IDirect3DSurface8 *destinationSurface, const POINT *destPointsArray);
+		long __stdcall CreateAdditionalSwapChain(D3DPRESENT_PARAMETERS *presentParameters, IDirect3DSwapChain8 **swapChain);
+		long __stdcall CreateCubeTexture(unsigned int edgeLength, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **cubeTexture);
+		long __stdcall CreateDepthStencilSurface(unsigned int width, unsigned int height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multiSample, IDirect3DSurface8 **surface);
+		long __stdcall CreateImageSurface(unsigned int width, unsigned int height, D3DFORMAT format, IDirect3DSurface8 **surface);
+		long __stdcall CreateIndexBuffer(unsigned int length, unsigned long usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **indexBuffer);
+		long __stdcall CreatePixelShader(const unsigned long *function, unsigned long *handle);
+		long __stdcall CreateRenderTarget(unsigned int width, unsigned int height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multiSample, int lockable, IDirect3DSurface8 **surface);
+		long __stdcall CreateStateBlock(D3DSTATEBLOCKTYPE type, unsigned long *token);
+		long __stdcall CreateTexture(unsigned int width, unsigned int height, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool, IDirect3DTexture8 **texture);
+		long __stdcall CreateVertexBuffer(unsigned int length, unsigned long usage, unsigned long FVF, D3DPOOL, IDirect3DVertexBuffer8 **vertexBuffer);
+		long __stdcall CreateVertexShader(const unsigned long *declaration, const unsigned long *function, unsigned long *handle, unsigned long usage);
+		long __stdcall CreateVolumeTexture(unsigned int width, unsigned int height, unsigned int depth, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool, IDirect3DVolumeTexture8 **volumeTexture);
+		long __stdcall DeletePatch(unsigned int handle);
+		long __stdcall DeletePixelShader(unsigned long handle);
+		long __stdcall DeleteStateBlock(unsigned long token);
+		long __stdcall DeleteVertexShader(unsigned long handle);
+		long __stdcall DrawIndexedPrimitive(D3DPRIMITIVETYPE type, unsigned int minIndex, unsigned int numVertices, unsigned int startIndex, unsigned int primitiveCount);
+		long __stdcall DrawIndexedPrimitiveUP(D3DPRIMITIVETYPE type, unsigned int minVertexIndex, unsigned int numVertexIndices, unsigned int PrimitiveCount, const void *indexData, D3DFORMAT indexDataFormat, const void *vertexStreamZeroData, unsigned int VertexStreamZeroStride);
+		long __stdcall DrawPrimitive(D3DPRIMITIVETYPE primitiveType, unsigned int startVertex, unsigned int primiveCount);
+		long __stdcall DrawPrimitiveUP(D3DPRIMITIVETYPE primitiveType, unsigned int primitiveCount, const void *vertexStreamZeroData, unsigned int vertexStreamZeroStride);
+		long __stdcall DrawRectPatch(unsigned int handle, const float *numSegs, const D3DRECTPATCH_INFO *rectPatchInfo);
+		long __stdcall DrawTriPatch(unsigned int handle, const float *numSegs, const D3DTRIPATCH_INFO *triPatchInfo);
+		long __stdcall EndScene();
+		long __stdcall EndStateBlock(unsigned long *token);
+		unsigned int __stdcall GetAvailableTextureMem();
+		long __stdcall GetBackBuffer(unsigned int index, D3DBACKBUFFER_TYPE type, IDirect3DSurface8 **backBuffer);
+		long __stdcall GetClipPlane(unsigned long index, float *plane);
+		long __stdcall GetClipStatus(D3DCLIPSTATUS8 *clipStatus);
+		long __stdcall GetCreationParameters(D3DDEVICE_CREATION_PARAMETERS *parameters);
+		long __stdcall GetCurrentTexturePalette(unsigned int *paletteNumber);
+		long __stdcall GetDepthStencilSurface(IDirect3DSurface8 **depthStencilSurface);
+		long __stdcall GetDeviceCaps(D3DCAPS8 *caps);
+		long __stdcall GetDirect3D(IDirect3D8 **D3D);
+		long __stdcall GetDisplayMode(D3DDISPLAYMODE *mode);
+		long __stdcall GetFrontBuffer(IDirect3DSurface8 *destSurface);
+		void __stdcall GetGammaRamp(D3DGAMMARAMP *ramp);
+		long __stdcall GetIndices(IDirect3DIndexBuffer8 **indexData, unsigned int *baseVertexIndex);
+		long __stdcall GetInfo(unsigned long devInfoID, void *devInfoStruct, unsigned long devInfoStructSize);
+		long __stdcall GetLight(unsigned long index, D3DLIGHT8 *p);
+		long __stdcall GetLightEnable(unsigned long index , int *enable);
+		long __stdcall GetMaterial(D3DMATERIAL8 *material);
+		long __stdcall GetPaletteEntries(unsigned int paletteNumber, PALETTEENTRY *entries);
+		long __stdcall GetPixelShader(unsigned long *handle);
+		long __stdcall GetPixelShaderFunction(unsigned long handle, void *data, unsigned long *sizeOfData);
+		long __stdcall GetPixelShaderConstant(unsigned long startRegister, void *constantData, unsigned long constantCount);
+		long __stdcall GetRasterStatus(D3DRASTER_STATUS *rasterStatus);
+		long __stdcall GetRenderState(D3DRENDERSTATETYPE State, unsigned long *value);
+		long __stdcall GetRenderTarget(IDirect3DSurface8 **renderTarget);
+		long __stdcall GetStreamSource(unsigned int streamNumber, IDirect3DVertexBuffer8 **streamData, unsigned int *stride);
+		long __stdcall GetTexture(unsigned long stage, IDirect3DBaseTexture8 **texture);
+		long __stdcall GetTextureStageState(unsigned long stage, D3DTEXTURESTAGESTATETYPE type, unsigned long *value);
+		long __stdcall GetTransform(D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix);
+		long __stdcall GetVertexShader(unsigned long *handle);
+		long __stdcall GetVertexShaderConstant(unsigned long startRegister, void *constantData, unsigned long constantCount);
+		long __stdcall GetVertexShaderDeclaration(unsigned long handle, void *data, unsigned long *size);
+		long __stdcall GetVertexShaderFunction(unsigned long handle, void *data, unsigned long *size);
+		long __stdcall GetViewport(D3DVIEWPORT8 *viewport);
+		long __stdcall LightEnable(unsigned long index, int enable);
+		long __stdcall MultiplyTransform(D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix);
+		long __stdcall Present(const RECT *sourceRect, const RECT *destRect, HWND destWindowOverride, const RGNDATA *dirtyRegion);
+		long __stdcall ProcessVertices(unsigned int srcStartIndex, unsigned int destIndex, unsigned int vertexCount, IDirect3DVertexBuffer8 *destBuffer, unsigned long flags);
+		long __stdcall Reset(D3DPRESENT_PARAMETERS *presentParameters);
+		long __stdcall ResourceManagerDiscardBytes(unsigned long bytes);
+		long __stdcall SetClipPlane(unsigned long index, const float *plane);
+		long __stdcall SetClipStatus(const D3DCLIPSTATUS8 *clipStatus);
+		long __stdcall SetCurrentTexturePalette(unsigned int paletteNumber);
+		void __stdcall SetCursorPosition(int x, int y, unsigned long flags);
+		void __stdcall SetCursorPosition(unsigned int x, unsigned int y, unsigned long flags);
+		long __stdcall SetCursorProperties(unsigned int x, unsigned int y, IDirect3DSurface8 *cursorBitmap);
+		void __stdcall SetGammaRamp(unsigned long flags, const D3DGAMMARAMP *ramp);
+		long __stdcall SetIndices(IDirect3DIndexBuffer8 *indexData, unsigned int baseVertexIndex);
+		long __stdcall SetLight(unsigned long index, const D3DLIGHT8 *light);
+		long __stdcall SetMaterial(const D3DMATERIAL8 *material);
+		long __stdcall SetPaletteEntries(unsigned int paletteNumber, const PALETTEENTRY *entries);
+		long __stdcall SetPixelShader(unsigned long shader);
+		long __stdcall SetPixelShaderConstant(unsigned long startRegister, const void *constantData, unsigned long constantCount);
+		long __stdcall SetRenderState(D3DRENDERSTATETYPE state, unsigned long value);
+		long __stdcall SetRenderTarget(IDirect3DSurface8 *renderTarget, IDirect3DSurface8 *newZStencil);
+		long __stdcall SetStreamSource(unsigned int streamNumber, IDirect3DVertexBuffer8 *streamData, unsigned int stride);
+		long __stdcall SetTexture(unsigned long stage, IDirect3DBaseTexture8 *texture);
+		long __stdcall SetTextureStageState(unsigned long stage, D3DTEXTURESTAGESTATETYPE type, unsigned long value);
+		long __stdcall SetTransform(D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix);
+		long __stdcall SetVertexShader(unsigned long handle);
+		long __stdcall SetVertexShaderConstant(unsigned long startRegister, const void *constantData, unsigned long constantCount);
+		long __stdcall SetViewport(const D3DVIEWPORT8 *viewport);
+		int __stdcall ShowCursor(int show);
+		long __stdcall TestCooperativeLevel();
+		long __stdcall UpdateTexture(IDirect3DBaseTexture8 *sourceTexture, IDirect3DBaseTexture8 *destinationTexture);
+		long __stdcall ValidateDevice(unsigned long *numPasses);
+
+		// Internal methods
+		long __stdcall updateSurface(IDirect3DSurface8 *sourceSurface, const RECT *sourceRect, IDirect3DSurface8 *destinationSurface, const POINT *destPoint);
+
+	private:
+		static int FVFStride(unsigned long FVF);
+		static int typeStride(unsigned char streamType);
+		static sw::StreamType streamType(int type);
+		bool bindData(Direct3DIndexBuffer8 *indexBuffer, int base);
+		void bindStreams(int base);
+		void bindIndexBuffer(Direct3DIndexBuffer8 *indexBuffer);
+		void bindLights();
+		bool bindViewport();
+		void bindTextures();
+		void bindCursor();
+
+		long updateVolume(IDirect3DVolume8 *sourceVolume, IDirect3DVolume8 *destinationVolume);
+		void configureFPU();
+
+		// Creation parameters
+		const HINSTANCE instance;
+		Direct3D8 *d3d8;
+		const unsigned int adapter;
+		const D3DDEVTYPE deviceType;
+		const HWND focusWindow;
+		const unsigned long behaviourFlags;
+		const D3DPRESENT_PARAMETERS presentParameters;
+		
+		HWND windowHandle;
+
+		D3DVIEWPORT8 viewport;
+		D3DMATRIX matrix[512];
+		Direct3DBaseTexture8 *texture[8];
+		D3DMATERIAL8 material;
+		float plane[6][4];
+		D3DCLIPSTATUS8 clipStatus;
+
+		struct Light : D3DLIGHT8
+		{
+			Light &operator=(const D3DLIGHT8 &light)
+			{
+				Type = light.Type;
+				Diffuse = light.Diffuse;
+				Specular = light.Specular;
+				Ambient = light.Ambient;
+				Position = light.Position;
+				Direction = light.Direction;
+				Range = light.Range;
+				Falloff = light.Falloff;
+				Attenuation0 = light.Attenuation0;
+				Attenuation1 = light.Attenuation1;
+				Attenuation2 = light.Attenuation2;
+				Theta = light.Theta;
+				Phi = light.Phi;
+
+				return *this;
+			}
+
+			bool enable;
+		};
+
+		struct Lights : std::map<int, Light>
+		{
+			bool exists(int index)
+			{
+				return find(index) != end();
+			}
+		};
+
+		Lights light;
+		bool lightsDirty;
+
+		Direct3DVertexBuffer8 *dataStream[16];
+		int streamStride[16];
+		Direct3DIndexBuffer8 *indexData;
+		unsigned int baseVertexIndex;
+
+		unsigned long FVF;
+
+		std::vector<Direct3DSwapChain8*> swapChain;
+
+		Direct3DSurface8 *renderTarget;
+		Direct3DSurface8 *depthStencil;
+
+		bool recordState;
+		std::vector<Direct3DStateBlock8*> stateRecorder;
+
+		unsigned long renderState[D3DRS_NORMALORDER + 1];
+		unsigned long textureStageState[8][D3DTSS_RESULTARG + 1];
+		bool init;   // TODO: Deprecate when all state changes implemented
+
+		std::vector<Direct3DPixelShader8*> pixelShader;
+		std::vector<Direct3DVertexShader8*> vertexShader;
+		unsigned long pixelShaderHandle;
+		unsigned long vertexShaderHandle;
+		const unsigned long *declaration;
+
+		float pixelShaderConstant[8][4];
+		float vertexShaderConstant[256][4];
+
+		struct Palette
+		{
+			PALETTEENTRY entry[256];
+		};
+
+		unsigned int currentPalette;
+		std::map<int, Palette> palette;
+
+		sw::Context *context;
+		sw::Renderer *renderer;
+
+		sw::Surface *cursor;
+		bool showCursor;
+		HCURSOR nullCursor;
+		HCURSOR win32Cursor;
+	};
+}
+
+#endif   // D3D8_Direct3DDevice8_hpp
diff --git a/src/D3D8/Direct3DIndexBuffer8.cpp b/src/D3D8/Direct3DIndexBuffer8.cpp
new file mode 100644
index 0000000..00e54d2
--- /dev/null
+++ b/src/D3D8/Direct3DIndexBuffer8.cpp
@@ -0,0 +1,191 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DIndexBuffer8.hpp"
+
+#include "Direct3DDevice8.hpp"
+#include "Resource.hpp"
+#include "Debug.hpp"
+
+#include <assert.h>
+
+namespace D3D8
+{
+	Direct3DIndexBuffer8::Direct3DIndexBuffer8(Direct3DDevice8 *device, unsigned int length, unsigned long usage, D3DFORMAT format, D3DPOOL pool) : Direct3DResource8(device, D3DRTYPE_INDEXBUFFER, length), length(length), usage(usage), format(format), pool(pool)
+	{
+		indexBuffer = new sw::Resource(length + 16);
+	}
+
+	Direct3DIndexBuffer8::~Direct3DIndexBuffer8()
+	{
+		indexBuffer->destruct();
+	}
+
+	long Direct3DIndexBuffer8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		if(iid == IID_IDirect3DIndexBuffer8 ||
+		   iid == IID_IDirect3DResource8 ||
+		   iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DIndexBuffer8::AddRef()
+	{
+		TRACE("");
+
+		return Direct3DResource8::AddRef();
+	}
+
+	unsigned long Direct3DIndexBuffer8::Release()
+	{
+		TRACE("");
+
+		return Direct3DResource8::Release();
+	}
+
+	long Direct3DIndexBuffer8::FreePrivateData(const GUID &guid)
+	{
+		TRACE("");
+
+		return Direct3DResource8::FreePrivateData(guid);
+	}
+
+	long Direct3DIndexBuffer8::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
+	{
+		TRACE("");
+
+		return Direct3DResource8::GetPrivateData(guid, data, size);
+	}
+
+	void Direct3DIndexBuffer8::PreLoad()
+	{
+		TRACE("");
+
+		Direct3DResource8::PreLoad();
+	}
+
+	long Direct3DIndexBuffer8::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
+	{
+		TRACE("");
+
+		return Direct3DResource8::SetPrivateData(guid, data, size, flags);
+	}
+
+	long Direct3DIndexBuffer8::GetDevice(IDirect3DDevice8 **device)
+	{
+		TRACE("");
+
+		return Direct3DResource8::GetDevice(device);
+	}
+
+	unsigned long Direct3DIndexBuffer8::SetPriority(unsigned long newPriority)
+	{
+		TRACE("");
+
+		return Direct3DResource8::SetPriority(newPriority);
+	}
+
+	unsigned long Direct3DIndexBuffer8::GetPriority()
+	{
+		TRACE("");
+
+		return Direct3DResource8::GetPriority();
+	}
+
+	D3DRESOURCETYPE Direct3DIndexBuffer8::GetType()
+	{
+		TRACE("");
+
+		return Direct3DResource8::GetType();
+	}
+
+	long Direct3DIndexBuffer8::GetDesc(D3DINDEXBUFFER_DESC *description)
+	{
+		TRACE("");
+
+		if(!description)
+		{
+			return INVALIDCALL();
+		}
+
+		description->Format = format;
+		description->Pool = pool;
+		description->Size = length;
+		description->Type = GetType();
+		description->Usage = usage;
+
+		return 0;
+	}
+
+	long Direct3DIndexBuffer8::Lock(unsigned int offset, unsigned int size, unsigned char **data, unsigned long flags)
+	{
+		TRACE("");
+
+		if(offset == 0 && size == 0)   // Lock whole buffer
+		{
+			size = length;
+		}
+
+		if(!data || offset + size > length)
+		{
+			return INVALIDCALL();
+		}
+
+		lockOffset = offset;
+		lockSize = size;
+
+		*data = (unsigned char*)indexBuffer->lock(sw::PUBLIC) + offset;
+		indexBuffer->unlock();
+
+		return D3D_OK;
+	}
+
+	long Direct3DIndexBuffer8::Unlock()
+	{
+		TRACE("");
+
+		return D3D_OK;
+	}
+
+	sw::Resource *Direct3DIndexBuffer8::getResource() const
+	{	
+		return indexBuffer;
+	}
+
+	bool Direct3DIndexBuffer8::is32Bit() const
+	{
+		switch(format)
+		{
+		case D3DFMT_INDEX16:
+			return false;
+		case D3DFMT_INDEX32:
+			return true;
+		default:
+			ASSERT(false);
+		}
+
+		return false;
+	}
+}
\ No newline at end of file
diff --git a/src/D3D8/Direct3DIndexBuffer8.hpp b/src/D3D8/Direct3DIndexBuffer8.hpp
new file mode 100644
index 0000000..8b241ab
--- /dev/null
+++ b/src/D3D8/Direct3DIndexBuffer8.hpp
@@ -0,0 +1,76 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DIndexBuffer8_hpp
+#define D3D8_Direct3DIndexBuffer8_hpp
+
+#include "Direct3DResource8.hpp"
+
+#include <d3d8.h>
+
+namespace sw
+{
+	class Resource;
+}
+
+namespace D3D8
+{
+	class Direct3DIndexBuffer8 : public IDirect3DIndexBuffer8, public Direct3DResource8
+	{
+	public:
+		Direct3DIndexBuffer8(Direct3DDevice8 *device, unsigned int length, unsigned long usage, D3DFORMAT format, D3DPOOL pool);
+
+		virtual ~Direct3DIndexBuffer8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3DResource8 methods
+		long __stdcall FreePrivateData(const GUID &guid);
+		long __stdcall GetPrivateData(const GUID &guid, void *data, unsigned long *size);
+		void __stdcall PreLoad();
+		long __stdcall SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags);
+		long __stdcall GetDevice(IDirect3DDevice8 **device);
+		unsigned long __stdcall SetPriority(unsigned long newPriority);
+		unsigned long __stdcall GetPriority();
+		D3DRESOURCETYPE __stdcall GetType();
+
+		// IDirect3DIndexBuffer8 methods
+		long __stdcall GetDesc(D3DINDEXBUFFER_DESC *description);
+		long __stdcall Lock(unsigned int offset, unsigned int size, unsigned char **data, unsigned long flags);
+		long __stdcall Unlock();
+		
+		// Internal methods
+		sw::Resource *getResource() const;
+		bool is32Bit() const;
+
+	private:
+		// Creation parameters
+		const unsigned int length;
+		const long usage;
+		const D3DFORMAT format;
+		const D3DPOOL pool;
+
+		bool locked;
+		unsigned int lockOffset;
+		unsigned int lockSize;
+		unsigned char *lockData;
+
+		sw::Resource *indexBuffer;
+	};
+}
+
+#endif // D3D8_Direct3DIndexBuffer8_hpp
diff --git a/src/D3D8/Direct3DPixelShader8.cpp b/src/D3D8/Direct3DPixelShader8.cpp
new file mode 100644
index 0000000..8757fbc
--- /dev/null
+++ b/src/D3D8/Direct3DPixelShader8.cpp
@@ -0,0 +1,83 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DPixelShader8.hpp"
+
+#include "Debug.hpp"
+
+namespace D3D8
+{
+	Direct3DPixelShader8::Direct3DPixelShader8(Direct3DDevice8 *device, const unsigned long *shaderToken) : device(device), pixelShader(shaderToken)
+	{
+		const unsigned long *token = shaderToken;
+
+		size = 0;
+
+		while(shaderToken[size] != 0x0000FFFF)
+		{
+			size++;
+		}
+
+		size++;
+
+		this->shaderToken = new unsigned long[size];
+		memcpy(this->shaderToken, shaderToken, size * sizeof(unsigned long));
+	}
+
+	Direct3DPixelShader8::~Direct3DPixelShader8()
+	{
+		delete[] shaderToken;
+		shaderToken = 0;
+	}
+
+	long Direct3DPixelShader8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		ASSERT(false);   // Internal object
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DPixelShader8::AddRef()
+	{
+		TRACE("");
+
+		return Unknown::AddRef();
+	}
+	
+	unsigned long Direct3DPixelShader8::Release()
+	{
+		TRACE("");
+
+		return Unknown::Release();
+	}
+
+	void Direct3DPixelShader8::GetFunction(void *data, unsigned int *size)
+	{
+		TRACE("");
+
+		if(data)
+		{
+			memcpy(data, shaderToken, this->size * 4);
+		}
+
+		*size = this->size * 4;
+	}
+
+	const sw::PixelShader *Direct3DPixelShader8::getPixelShader() const
+	{
+		return &pixelShader;
+	}
+}
\ No newline at end of file
diff --git a/src/D3D8/Direct3DPixelShader8.hpp b/src/D3D8/Direct3DPixelShader8.hpp
new file mode 100644
index 0000000..daf6a24
--- /dev/null
+++ b/src/D3D8/Direct3DPixelShader8.hpp
@@ -0,0 +1,54 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DPixelShader8_hpp
+#define D3D8_Direct3DPixelShader8_hpp
+
+#include "PixelShader.hpp"
+
+#include "Unknown.hpp"
+
+namespace D3D8
+{
+	class Direct3DDevice8;
+
+	class Direct3DPixelShader8 : public Unknown
+	{
+	public:
+		Direct3DPixelShader8(Direct3DDevice8 *device, const unsigned long *shaderToken);
+
+		virtual ~Direct3DPixelShader8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3DPixelShader8 methods
+		void __stdcall GetFunction(void *data, unsigned int *size);
+
+		// Internal methods
+		const sw::PixelShader *getPixelShader() const;
+
+	private:
+		// Creation parameters
+		Direct3DDevice8 *const device;
+		unsigned long *shaderToken;
+		unsigned int size;
+
+		sw::PixelShader pixelShader;
+	};
+}
+
+#endif   // D3D8_Direct3DPixelShader8_hpp
\ No newline at end of file
diff --git a/src/D3D8/Direct3DResource8.cpp b/src/D3D8/Direct3DResource8.cpp
new file mode 100644
index 0000000..7674d80
--- /dev/null
+++ b/src/D3D8/Direct3DResource8.cpp
@@ -0,0 +1,215 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DResource8.hpp"
+
+#include "Direct3DDevice8.hpp"
+#include "Debug.hpp"
+
+namespace D3D8
+{
+	unsigned int Direct3DResource8::memoryUsage = 0;
+
+	Direct3DResource8::PrivateData::PrivateData()
+	{
+		data = 0;
+	}
+
+	Direct3DResource8::PrivateData::PrivateData(const void *data, int size, bool managed)
+	{
+		this->size = size;
+		this->managed = managed;
+
+		this->data = (void*)new unsigned char[size];
+		memcpy(this->data, data, size);
+
+		if(managed)
+		{
+			((IUnknown*)data)->AddRef();
+		}
+	}
+
+	Direct3DResource8::PrivateData &Direct3DResource8::PrivateData::operator=(const PrivateData &privateData)
+	{
+		size = privateData.size;
+		managed = privateData.managed;
+
+		if(data)
+		{
+			if(managed)
+			{
+				((IUnknown*)data)->Release();
+			}
+
+			delete[] data;
+		}
+
+		data = (void*)new unsigned char[size];
+		memcpy(data, privateData.data, size);
+
+		return *this;
+	}
+
+	Direct3DResource8::PrivateData::~PrivateData()
+	{
+		if(data && managed)
+		{
+			((IUnknown*)data)->Release();
+		}
+
+		delete[] data;
+		data = 0;
+	}
+
+	Direct3DResource8::Direct3DResource8(Direct3DDevice8 *device, D3DRESOURCETYPE type, unsigned int size) : device(device), type(type), size(size)
+	{
+		priority = 0;
+
+		memoryUsage += size;
+	}
+
+	Direct3DResource8::~Direct3DResource8()
+	{
+		memoryUsage -= size;
+	}
+
+	long Direct3DResource8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		if(iid == IID_IDirect3DResource8 ||
+		   iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DResource8::AddRef()
+	{
+		TRACE("");
+
+		return Unknown::AddRef();
+	}
+
+	unsigned long Direct3DResource8::Release()
+	{
+		TRACE("");
+
+		return Unknown::Release();
+	}
+
+	long Direct3DResource8::GetDevice(IDirect3DDevice8 **device)
+	{
+		TRACE("");
+
+		if(!device)
+		{
+			return INVALIDCALL();
+		}
+
+		this->device->AddRef();
+		*device = this->device;
+
+		return D3D_OK;
+	}
+
+	long Direct3DResource8::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
+	{
+		TRACE("");
+
+		privateData[guid] = PrivateData(data, size, flags == D3DSPD_IUNKNOWN);
+
+		return D3D_OK;
+	}
+
+	long Direct3DResource8::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
+	{
+		TRACE("");
+
+		Iterator result = privateData.find(guid);
+
+		if(result == privateData.end())
+		{
+			return NOTFOUND();
+		}
+
+		if(result->second.size > *size)
+		{
+			return MOREDATA();
+		}
+
+		memcpy(data, result->second.data, result->second.size);
+
+		return D3D_OK;
+	}
+
+	long Direct3DResource8::FreePrivateData(const GUID &guid)
+	{
+		TRACE("");
+
+		Iterator result = privateData.find(guid);
+
+		if(result == privateData.end())
+		{
+			return D3DERR_NOTFOUND;
+		}
+		
+		privateData.erase(guid);
+
+		return D3D_OK;
+	}
+
+	unsigned long Direct3DResource8::SetPriority(unsigned long newPriority)
+	{
+		TRACE("");
+
+		unsigned long oldPriority = priority;
+		priority = newPriority;
+
+		return oldPriority;
+	}
+
+	unsigned long Direct3DResource8::GetPriority()
+	{
+		TRACE("");
+
+		return priority;
+	}
+
+	void Direct3DResource8::PreLoad()
+	{
+		TRACE("");
+
+		return;   // FIXME: Anything to do?
+	}
+
+	D3DRESOURCETYPE Direct3DResource8::GetType()
+	{
+		TRACE("");
+
+		return type;
+	}
+
+	unsigned int Direct3DResource8::getMemoryUsage()
+	{
+		return memoryUsage;
+	}
+}
\ No newline at end of file
diff --git a/src/D3D8/Direct3DResource8.hpp b/src/D3D8/Direct3DResource8.hpp
new file mode 100644
index 0000000..b80296a
--- /dev/null
+++ b/src/D3D8/Direct3DResource8.hpp
@@ -0,0 +1,92 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DResource8_hpp
+#define D3D8_Direct3DResource8_hpp
+
+#include "Unknown.hpp"
+
+#include <d3d8.h>
+
+#include <map>
+
+namespace D3D8
+{
+	class Direct3DDevice8;
+
+	class Direct3DResource8 : public IDirect3DResource8, public Unknown
+	{
+	public:
+		Direct3DResource8(Direct3DDevice8 *device, D3DRESOURCETYPE type, unsigned int size);
+
+		virtual ~Direct3DResource8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3DResource8 methods
+		long __stdcall GetDevice(IDirect3DDevice8 **device);
+		long __stdcall SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags);
+		long __stdcall GetPrivateData(const GUID &guid, void *data, unsigned long *size);
+		long __stdcall FreePrivateData(const GUID &guid);
+		unsigned long __stdcall SetPriority(unsigned long newPriority);
+		unsigned long __stdcall GetPriority();
+		void __stdcall PreLoad();
+		D3DRESOURCETYPE __stdcall GetType();
+
+		// Internal methods
+		static unsigned int getMemoryUsage();
+
+	protected:
+		// Creation parameters
+		Direct3DDevice8 *const device;
+		const D3DRESOURCETYPE type;
+		const unsigned int size;
+
+	private:
+		unsigned long priority;
+
+		struct PrivateData
+		{
+			PrivateData();
+			PrivateData(const void *data, int size, bool managed);
+
+			~PrivateData();
+
+			PrivateData &operator=(const PrivateData &privateData);
+
+			void *data;
+			unsigned long size;
+			bool managed;   // IUnknown interface
+		};
+
+		struct CompareGUID
+		{
+			bool operator()(const GUID& left, const GUID& right) const
+			{
+				return memcmp(&left, &right, sizeof(GUID)) < 0;
+			}
+		};
+
+		typedef std::map<GUID, PrivateData, CompareGUID> PrivateDataMap;
+		typedef PrivateDataMap::iterator Iterator;
+		PrivateDataMap privateData;
+
+		static unsigned int memoryUsage;
+	};
+}
+
+#endif   // D3D8_Direct3DResource8_hpp
diff --git a/src/D3D8/Direct3DStateBlock8.cpp b/src/D3D8/Direct3DStateBlock8.cpp
new file mode 100644
index 0000000..512c935
--- /dev/null
+++ b/src/D3D8/Direct3DStateBlock8.cpp
@@ -0,0 +1,743 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DStateBlock8.hpp"
+
+#include "Direct3DDevice8.hpp"
+#include "Direct3DBaseTexture8.hpp"
+#include "Direct3DVertexBuffer8.hpp"
+#include "Direct3DIndexBuffer8.hpp"
+#include "Debug.hpp"
+
+#include <assert.h>
+
+namespace D3D8
+{
+	Direct3DStateBlock8::Direct3DStateBlock8(Direct3DDevice8 *device, D3DSTATEBLOCKTYPE type) : device(device), type(type)
+	{
+		vertexShaderHandle = 0;
+		pixelShaderHandle = 0;
+		indexBuffer = 0;
+
+		for(int stream = 0; stream < 16; stream++)
+		{
+			streamSource[stream].vertexBuffer = 0;
+		}
+
+		for(int stage = 0; stage < 8; stage++)
+		{
+			texture[stage] = 0;
+		}
+
+		clear();
+
+		if(type == D3DSBT_PIXELSTATE || type == D3DSBT_ALL)
+		{
+			capturePixelRenderStates();
+			capturePixelTextureStates();
+			capturePixelShaderStates();
+		}
+		
+		if(type == D3DSBT_VERTEXSTATE || type == D3DSBT_ALL)
+		{
+			captureVertexRenderStates();
+			captureVertexTextureStates();
+			captureLightStates();
+			captureVertexShaderStates();
+		}
+
+		if(type == D3DSBT_ALL)   // Capture remaining states
+		{
+			captureTextures();
+			captureVertexTextures();
+			captureDisplacementTextures();
+			captureTexturePalette();
+			captureVertexStreams();
+			captureIndexBuffer();
+			captureViewport();
+			captureTransforms();
+			captureTextureTransforms();
+			captureClippingPlanes();
+			captureMaterial();
+		}
+	}
+
+	Direct3DStateBlock8::~Direct3DStateBlock8()
+	{
+		clear();
+	}
+
+	long Direct3DStateBlock8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		ASSERT(false);   // Internal object
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DStateBlock8::AddRef()
+	{
+		TRACE("");
+
+		return Unknown::AddRef();
+	}
+	
+	unsigned long Direct3DStateBlock8::Release()
+	{
+		TRACE("");
+
+		return Unknown::Release();
+	}
+
+	long Direct3DStateBlock8::Apply()
+	{
+		TRACE("");
+
+		if(vertexShaderCaptured)
+		{
+			device->SetVertexShader(vertexShaderHandle);
+		}
+
+		if(pixelShaderCaptured)
+		{
+			device->SetPixelShader(pixelShaderHandle);
+		}
+
+		if(indexBufferCaptured)
+		{
+			device->SetIndices(indexBuffer, baseVertexIndex);
+		}
+
+		for(int state = 0; state < D3DRS_NORMALORDER + 1; state++)
+		{
+			if(renderStateCaptured[state])
+			{
+				device->SetRenderState((D3DRENDERSTATETYPE)state, renderState[state]);
+			}
+		}
+
+		for(int stage = 0; stage < 8; stage++)
+		{
+			for(int state = 0; state < D3DTSS_RESULTARG + 1; state++)
+			{
+				if(textureStageStateCaptured[stage][state])
+				{
+					device->SetTextureStageState(stage, (D3DTEXTURESTAGESTATETYPE)state, textureStageState[stage][state]);
+				}
+			}
+		}
+
+		for(int stream = 0; stream < 16; stream++)
+		{
+			if(streamSourceCaptured[stream])
+			{
+				device->SetStreamSource(stream, streamSource[stream].vertexBuffer, streamSource[stream].stride);
+			}
+		}
+
+		for(int stage = 0; stage < 8; stage++)
+		{
+			if(textureCaptured[stage])
+			{
+				device->SetTexture(stage, texture[stage]);
+			}
+		}
+
+		for(int state = 0; state < 512; state++)
+		{
+			if(transformCaptured[state])
+			{
+				device->SetTransform((D3DTRANSFORMSTATETYPE)state, &transform[state]);
+			}
+		}
+
+		if(viewportCaptured)
+		{
+			device->SetViewport(&viewport);
+		}
+
+		for(int index = 0; index < 6; index++)
+		{
+			if(clipPlaneCaptured[index])
+			{
+				device->SetClipPlane(index, clipPlane[index]);
+			}
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DStateBlock8::Capture()
+	{
+		TRACE("");
+
+		if(vertexShaderCaptured)
+		{		
+			device->GetVertexShader(&vertexShaderHandle);
+		}
+
+		if(pixelShaderCaptured)
+		{		
+			device->GetPixelShader(&pixelShaderHandle);
+		}
+
+		if(indexBufferCaptured)
+		{
+			if(indexBuffer)
+			{
+				indexBuffer->Release();
+			}
+
+			device->GetIndices(reinterpret_cast<IDirect3DIndexBuffer8**>(&indexBuffer), &baseVertexIndex);
+		}
+
+		for(int state = 0; state < D3DRS_NORMALORDER + 1; state++)
+		{
+			if(renderStateCaptured[state])
+			{
+				device->GetRenderState((D3DRENDERSTATETYPE)state, &renderState[state]);
+			}
+		}
+
+		for(int stage = 0; stage < 8; stage++)
+		{
+			for(int state = 0; state < D3DTSS_RESULTARG + 1; state++)
+			{
+				if(textureStageStateCaptured[stage][state])
+				{
+					device->GetTextureStageState(stage, (D3DTEXTURESTAGESTATETYPE)state, &textureStageState[stage][state]);
+				}
+			}
+		}
+
+		for(int stream = 0; stream < 16; stream++)
+		{
+			if(streamSourceCaptured[stream])
+			{
+				if(streamSource[stream].vertexBuffer)
+				{
+					streamSource[stream].vertexBuffer->Release();
+				}
+
+				device->GetStreamSource(stream, reinterpret_cast<IDirect3DVertexBuffer8**>(&streamSource[stream].vertexBuffer), &streamSource[stream].stride);
+			}
+		}
+
+		for(int stage = 0; stage < 8; stage++)
+		{
+			if(textureCaptured[stage])
+			{
+				if(texture[stage])
+				{
+					texture[stage]->Release();
+				}
+
+				device->GetTexture(stage, reinterpret_cast<IDirect3DBaseTexture8**>(&texture[stage]));
+			}
+		}
+
+		for(int state = 0; state < 512; state++)
+		{
+			if(transformCaptured[state])
+			{
+				device->GetTransform((D3DTRANSFORMSTATETYPE)state, &transform[state]);
+			}
+		}
+
+		if(viewportCaptured)
+		{
+			device->GetViewport(&viewport);
+		}
+
+		for(int index = 0; index < 6; index++)
+		{
+			if(clipPlaneCaptured[index])
+			{
+				device->GetClipPlane(index, clipPlane[index]);
+			}
+		}
+
+		return D3D_OK;
+	}
+
+	long Direct3DStateBlock8::GetDevice(IDirect3DDevice8 **device)
+	{
+		TRACE("");
+
+		if(!device)
+		{
+			return INVALIDCALL();
+		}
+
+		this->device->AddRef();
+		*device = this->device;
+
+		return D3D_OK;
+	}
+
+	void Direct3DStateBlock8::lightEnable(unsigned long index, int enable)
+	{
+		UNIMPLEMENTED();
+	}
+
+	void Direct3DStateBlock8::setClipPlane(unsigned long index, const float *plane)
+	{
+		clipPlaneCaptured[index] = true;
+		clipPlane[index][0] = plane[0];
+		clipPlane[index][1] = plane[1];
+		clipPlane[index][2] = plane[2];
+		clipPlane[index][3] = plane[3];
+	}
+
+	void Direct3DStateBlock8::setCurrentTexturePalette(unsigned int paletteNumber)
+	{
+		UNIMPLEMENTED();
+	}
+
+	void Direct3DStateBlock8::setFVF(unsigned long FVF)
+	{
+		UNIMPLEMENTED();
+	}
+
+	void Direct3DStateBlock8::setIndices(Direct3DIndexBuffer8 *indexData, unsigned int baseVertexIndex)
+	{
+		if(indexData) indexData->AddRef();
+
+		indexBufferCaptured = true;
+		indexBuffer = indexData;
+		this->baseVertexIndex = baseVertexIndex;
+	}
+
+	void Direct3DStateBlock8::setLight(unsigned long index, const D3DLIGHT8 *light)
+	{
+		UNIMPLEMENTED();
+	}
+
+	void Direct3DStateBlock8::setMaterial(const D3DMATERIAL8 *material)
+	{
+		UNIMPLEMENTED();
+	}
+
+	void Direct3DStateBlock8::setPixelShader(unsigned long shaderHandle)		
+	{
+		pixelShaderCaptured = true;
+		pixelShaderHandle = shaderHandle;
+	}
+
+	void Direct3DStateBlock8::setPixelShaderConstant(unsigned int startRegister, const void *constantData, unsigned int count)
+	{
+		UNIMPLEMENTED();
+	}
+
+	void Direct3DStateBlock8::setRenderState(D3DRENDERSTATETYPE state, unsigned long value)
+	{
+		renderStateCaptured[state] = true;
+		renderState[state] = value;
+	}
+
+	void Direct3DStateBlock8::setScissorRect(const RECT *rect)
+	{
+		UNIMPLEMENTED();
+	}
+
+	void Direct3DStateBlock8::setStreamSource(unsigned int stream, Direct3DVertexBuffer8 *data, unsigned int stride)
+	{
+		if(data) data->AddRef();
+
+		streamSourceCaptured[stream] = true;
+		streamSource[stream].vertexBuffer = data;
+		streamSource[stream].stride = stride;
+	}
+
+	void Direct3DStateBlock8::setTexture(unsigned long stage, Direct3DBaseTexture8 *texture)
+	{
+		if(texture) texture->AddRef();
+
+		textureCaptured[stage] = true;
+		this->texture[stage] = texture;
+	}
+
+	void Direct3DStateBlock8::setTextureStageState(unsigned long stage, D3DTEXTURESTAGESTATETYPE type, unsigned long value)
+	{
+		textureStageStateCaptured[stage][type] = true;
+		textureStageState[stage][type] = value;
+	}
+
+	void Direct3DStateBlock8::setTransform(D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
+	{
+		transformCaptured[state] = true;
+		transform[state] = *matrix;
+	}
+
+	void Direct3DStateBlock8::setViewport(const D3DVIEWPORT8 *viewport)
+	{
+		viewportCaptured = true;
+		this->viewport = *viewport;
+	}
+
+	void Direct3DStateBlock8::setVertexShader(unsigned long shaderHandle)
+	{
+		vertexShaderCaptured = true;
+		vertexShaderHandle = shaderHandle;
+	}
+
+	void Direct3DStateBlock8::setVertexShaderConstant(unsigned int startRegister, const void *constantData, unsigned int count)
+	{
+		UNIMPLEMENTED();
+	}
+
+	void Direct3DStateBlock8::clear()
+	{
+		// Erase capture flags
+		vertexShaderCaptured = false;
+		pixelShaderCaptured = false;
+		indexBufferCaptured = false;
+
+		for(int state = 0; state < D3DRS_NORMALORDER + 1; state++)
+		{
+			renderStateCaptured[state] = false;
+		}
+
+		for(int stage = 0; stage < 8; stage++)
+		{
+			for(int state = 0; state < D3DTSS_RESULTARG + 1; state++)
+			{
+				textureStageStateCaptured[stage][state] = false;
+			}
+		}
+
+		for(int stream = 0; stream < 16; stream++)
+		{
+			streamSourceCaptured[stream] = false;
+		}
+
+		for(int stage = 0; stage < 8; stage++)
+		{
+			textureCaptured[stage] = false;
+		}
+
+		for(int state = 0; state < 512; state++)
+		{
+			transformCaptured[state] = false;
+		}
+
+		viewportCaptured = false;
+
+		for(int index = 0; index < 6; index++)
+		{
+			clipPlaneCaptured[index] = false;
+		}
+
+		// Release resources
+		vertexShaderHandle = 0;
+		pixelShaderHandle = 0;
+
+		if(indexBuffer)
+		{
+			indexBuffer->Release();
+			indexBuffer = 0;
+		}
+
+		for(int stream = 0; stream < 16; stream++)
+		{
+			if(streamSource[stream].vertexBuffer)
+			{
+				streamSource[stream].vertexBuffer->Release();
+				streamSource[stream].vertexBuffer = 0;
+			}
+		}
+
+		for(int stage = 0; stage < 8; stage++)
+		{
+			if(texture[stage])
+			{
+				texture[stage]->Release();
+				texture[stage] = 0;
+			}
+		}
+	}
+
+	void Direct3DStateBlock8::captureRenderState(D3DRENDERSTATETYPE state)
+	{
+		device->GetRenderState(state, &renderState[state]);
+		renderStateCaptured[state] = true;
+	}
+
+	void Direct3DStateBlock8::captureTextureStageState(unsigned long stage, D3DTEXTURESTAGESTATETYPE type)
+	{
+		device->GetTextureStageState(stage, type, &textureStageState[stage][type]);
+		textureStageStateCaptured[stage][type] = true;
+	}
+
+	void Direct3DStateBlock8::captureTransform(D3DTRANSFORMSTATETYPE state)
+	{
+		device->GetTransform(state, &transform[state]);
+		transformCaptured[state] = true;
+	}
+
+	void Direct3DStateBlock8::capturePixelRenderStates()
+	{
+		captureRenderState(D3DRS_ZENABLE);
+		captureRenderState(D3DRS_FILLMODE);
+		captureRenderState(D3DRS_SHADEMODE);
+		captureRenderState(D3DRS_ZWRITEENABLE);
+		captureRenderState(D3DRS_ALPHATESTENABLE);
+		captureRenderState(D3DRS_LASTPIXEL);
+		captureRenderState(D3DRS_SRCBLEND);
+		captureRenderState(D3DRS_DESTBLEND);
+		captureRenderState(D3DRS_ZFUNC);
+		captureRenderState(D3DRS_ALPHAREF);
+		captureRenderState(D3DRS_ALPHAFUNC);
+		captureRenderState(D3DRS_DITHERENABLE);
+		captureRenderState(D3DRS_FOGSTART);
+		captureRenderState(D3DRS_FOGEND);
+		captureRenderState(D3DRS_FOGDENSITY);
+		captureRenderState(D3DRS_ALPHABLENDENABLE);
+		captureRenderState(D3DRS_ZBIAS);
+		captureRenderState(D3DRS_STENCILENABLE);
+		captureRenderState(D3DRS_STENCILFAIL);
+		captureRenderState(D3DRS_STENCILZFAIL);
+		captureRenderState(D3DRS_STENCILPASS);
+		captureRenderState(D3DRS_STENCILFUNC);
+		captureRenderState(D3DRS_STENCILREF);
+		captureRenderState(D3DRS_STENCILMASK);
+		captureRenderState(D3DRS_STENCILWRITEMASK);
+		captureRenderState(D3DRS_TEXTUREFACTOR);
+		captureRenderState(D3DRS_WRAP0);
+		captureRenderState(D3DRS_WRAP1);
+		captureRenderState(D3DRS_WRAP2);
+		captureRenderState(D3DRS_WRAP3);
+		captureRenderState(D3DRS_WRAP4);
+		captureRenderState(D3DRS_WRAP5);
+		captureRenderState(D3DRS_WRAP6);
+		captureRenderState(D3DRS_WRAP7);
+		captureRenderState(D3DRS_COLORWRITEENABLE);
+		captureRenderState(D3DRS_BLENDOP);
+	}
+
+	void Direct3DStateBlock8::capturePixelTextureStates()
+	{
+		for(int stage = 0; stage < 8; stage++)
+		{
+			captureTextureStageState(stage, D3DTSS_COLOROP);
+			captureTextureStageState(stage, D3DTSS_COLORARG1);
+			captureTextureStageState(stage, D3DTSS_COLORARG2);
+			captureTextureStageState(stage, D3DTSS_ALPHAOP);
+			captureTextureStageState(stage, D3DTSS_ALPHAARG1);
+			captureTextureStageState(stage, D3DTSS_ALPHAARG2);
+			captureTextureStageState(stage, D3DTSS_BUMPENVMAT00);
+			captureTextureStageState(stage, D3DTSS_BUMPENVMAT01);
+			captureTextureStageState(stage, D3DTSS_BUMPENVMAT10);
+			captureTextureStageState(stage, D3DTSS_BUMPENVMAT11);
+			captureTextureStageState(stage, D3DTSS_TEXCOORDINDEX);
+			captureTextureStageState(stage, D3DTSS_BUMPENVLSCALE);
+			captureTextureStageState(stage, D3DTSS_BUMPENVLOFFSET);
+			captureTextureStageState(stage, D3DTSS_TEXTURETRANSFORMFLAGS);
+			captureTextureStageState(stage, D3DTSS_COLORARG0);
+			captureTextureStageState(stage, D3DTSS_ALPHAARG0);
+			captureTextureStageState(stage, D3DTSS_RESULTARG);
+		
+			captureTextureStageState(stage, D3DTSS_ADDRESSU);
+			captureTextureStageState(stage, D3DTSS_ADDRESSV);
+			captureTextureStageState(stage, D3DTSS_ADDRESSW); 
+			captureTextureStageState(stage, D3DTSS_BORDERCOLOR);
+			captureTextureStageState(stage, D3DTSS_MAGFILTER);
+			captureTextureStageState(stage, D3DTSS_MINFILTER);
+			captureTextureStageState(stage, D3DTSS_MIPFILTER);
+			captureTextureStageState(stage, D3DTSS_MIPMAPLODBIAS);
+			captureTextureStageState(stage, D3DTSS_MAXMIPLEVEL);
+			captureTextureStageState(stage, D3DTSS_MAXANISOTROPY);
+		}
+	}
+
+	void Direct3DStateBlock8::capturePixelShaderStates()
+	{
+		pixelShaderCaptured = true;
+		device->GetPixelShader(&pixelShaderHandle);
+
+		device->GetPixelShaderConstant(0, pixelShaderConstant, 8);
+	}
+
+	void Direct3DStateBlock8::captureVertexRenderStates()
+	{
+		captureRenderState(D3DRS_CULLMODE);
+		captureRenderState(D3DRS_FOGENABLE);
+		captureRenderState(D3DRS_FOGCOLOR);
+		captureRenderState(D3DRS_FOGTABLEMODE);
+		captureRenderState(D3DRS_FOGSTART);
+		captureRenderState(D3DRS_FOGEND);
+		captureRenderState(D3DRS_FOGDENSITY);
+		captureRenderState(D3DRS_RANGEFOGENABLE);
+		captureRenderState(D3DRS_AMBIENT);
+		captureRenderState(D3DRS_COLORVERTEX);
+		captureRenderState(D3DRS_FOGVERTEXMODE);
+		captureRenderState(D3DRS_CLIPPING);
+		captureRenderState(D3DRS_LIGHTING);
+		captureRenderState(D3DRS_LOCALVIEWER);
+		captureRenderState(D3DRS_EMISSIVEMATERIALSOURCE);
+		captureRenderState(D3DRS_AMBIENTMATERIALSOURCE);
+		captureRenderState(D3DRS_DIFFUSEMATERIALSOURCE);
+		captureRenderState(D3DRS_SPECULARMATERIALSOURCE);
+		captureRenderState(D3DRS_VERTEXBLEND);
+		captureRenderState(D3DRS_CLIPPLANEENABLE);
+		captureRenderState(D3DRS_POINTSIZE);
+		captureRenderState(D3DRS_POINTSIZE_MIN);
+		captureRenderState(D3DRS_POINTSPRITEENABLE);
+		captureRenderState(D3DRS_POINTSCALEENABLE);
+		captureRenderState(D3DRS_POINTSCALE_A);
+		captureRenderState(D3DRS_POINTSCALE_B);
+		captureRenderState(D3DRS_POINTSCALE_C);
+		captureRenderState(D3DRS_MULTISAMPLEANTIALIAS);
+		captureRenderState(D3DRS_MULTISAMPLEMASK);
+		captureRenderState(D3DRS_PATCHEDGESTYLE);
+		captureRenderState(D3DRS_POINTSIZE_MAX);
+		captureRenderState(D3DRS_INDEXEDVERTEXBLENDENABLE);
+		captureRenderState(D3DRS_TWEENFACTOR);
+		captureRenderState(D3DRS_NORMALIZENORMALS);
+		captureRenderState(D3DRS_SPECULARENABLE);
+		captureRenderState(D3DRS_SHADEMODE);
+	}
+
+	void Direct3DStateBlock8::captureVertexTextureStates()
+	{
+		for(int stage = 0; stage < 8; stage++)
+		{
+			captureTextureStageState(stage, D3DTSS_TEXCOORDINDEX);
+			captureTextureStageState(stage, D3DTSS_TEXTURETRANSFORMFLAGS);
+		}
+	}
+
+	void Direct3DStateBlock8::captureLightStates()
+	{
+		for(int index = 0; index < 8; index++)   // FIXME: Support unlimited index
+		{
+			device->GetLight(index, &light[index]);
+			lightCaptured[index] = true;
+		}
+
+		for(int index = 0; index < 8; index++)   // FIXME: Support unlimited index
+		{
+			lightEnableState[index] = false;
+			device->GetLightEnable(index, &lightEnableState[index]);
+			lightEnableCaptured[index] = true;
+		}
+	}
+
+	void Direct3DStateBlock8::captureVertexShaderStates()
+	{
+		vertexShaderCaptured = true;
+		device->GetVertexShader(&vertexShaderHandle);
+
+		device->GetVertexShaderConstant(0, vertexShaderConstant[0], 256);
+	}
+
+	void Direct3DStateBlock8::captureTextures()
+	{
+		for(int sampler = 0; sampler < 8; sampler++)
+		{
+			textureCaptured[sampler] = true;
+			device->GetTexture(sampler, reinterpret_cast<IDirect3DBaseTexture8**>(&texture[sampler]));
+
+			if(texture[sampler])
+			{
+				texture[sampler]->bind();
+				texture[sampler]->Release();
+			}
+		}
+	}
+
+	void Direct3DStateBlock8::captureVertexTextures()
+	{
+		// FIXME
+	}
+
+	void Direct3DStateBlock8::captureDisplacementTextures()
+	{
+		// FIXME	
+	}
+
+	void Direct3DStateBlock8::captureTexturePalette()
+	{
+		paletteNumberCaptured = true;
+		device->GetCurrentTexturePalette(&paletteNumber);
+	}
+
+	void Direct3DStateBlock8::captureVertexStreams()
+	{
+		for(int stream = 0; stream < 16; stream++)
+		{
+			streamSourceCaptured[stream] = true;
+			device->GetStreamSource(stream, reinterpret_cast<IDirect3DVertexBuffer8**>(&streamSource[stream].vertexBuffer),  &streamSource[stream].stride);
+			
+			if(streamSource[stream].vertexBuffer)
+			{
+				streamSource[stream].vertexBuffer->bind();
+				streamSource[stream].vertexBuffer->Release();
+			}
+		}
+	}
+
+	void Direct3DStateBlock8::captureIndexBuffer()
+	{
+		indexBufferCaptured = true;
+		device->GetIndices(reinterpret_cast<IDirect3DIndexBuffer8**>(&indexBuffer), &baseVertexIndex);
+
+		if(indexBuffer)
+		{
+			indexBuffer->bind();
+			indexBuffer->Release();
+		}
+	}
+
+	void Direct3DStateBlock8::captureViewport()
+	{
+		device->GetViewport(&viewport);
+		viewportCaptured = true;
+	}
+
+	void Direct3DStateBlock8::captureTransforms()
+	{
+		captureTransform(D3DTS_VIEW);
+		captureTransform(D3DTS_PROJECTION);
+		captureTransform(D3DTS_WORLD);
+	}
+
+	void Direct3DStateBlock8::captureTextureTransforms()
+	{
+		captureTransform(D3DTS_TEXTURE0);
+		captureTransform(D3DTS_TEXTURE1);
+		captureTransform(D3DTS_TEXTURE2);
+		captureTransform(D3DTS_TEXTURE3);
+		captureTransform(D3DTS_TEXTURE4);
+		captureTransform(D3DTS_TEXTURE5);
+		captureTransform(D3DTS_TEXTURE6);
+		captureTransform(D3DTS_TEXTURE7);
+	}
+
+	void Direct3DStateBlock8::captureClippingPlanes()
+	{
+		for(int index = 0; index < 6; index++)
+		{
+			device->GetClipPlane(index, (float*)&clipPlane[index]);
+			clipPlaneCaptured[index] = true;
+		}
+	}
+
+	void Direct3DStateBlock8::captureMaterial()
+	{
+		device->GetMaterial(&material);
+		materialCaptured = true;
+	}
+}
\ No newline at end of file
diff --git a/src/D3D8/Direct3DStateBlock8.hpp b/src/D3D8/Direct3DStateBlock8.hpp
new file mode 100644
index 0000000..744de2e
--- /dev/null
+++ b/src/D3D8/Direct3DStateBlock8.hpp
@@ -0,0 +1,161 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DStateBlock8_hpp
+#define D3D8_Direct3DStateBlock8_hpp
+
+#include "Unknown.hpp"
+
+#include <vector>
+
+#include <d3d8.h>
+
+namespace D3D8
+{
+	class Direct3DDevice8;
+	class Direct3DBaseTexture8;
+	class Direct3DVertexBuffer8;
+	class Direct3DIndexBuffer8;
+
+	class Direct3DStateBlock8 : public Unknown
+	{
+	public:
+		Direct3DStateBlock8(Direct3DDevice8 *device, D3DSTATEBLOCKTYPE type);
+
+		virtual ~Direct3DStateBlock8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3DStateBlock8 methods
+		long __stdcall Apply();
+		long __stdcall Capture();
+		long __stdcall GetDevice(IDirect3DDevice8 **device);
+
+		// Internal methods
+		void lightEnable(unsigned long index, int enable);
+		void setClipPlane(unsigned long index, const float *plane);
+		void setCurrentTexturePalette(unsigned int paletteNumber);
+		void setFVF(unsigned long FVF);
+		void setIndices(Direct3DIndexBuffer8 *indexData, unsigned int baseVertexIndex);
+		void setLight(unsigned long index, const D3DLIGHT8 *light);
+		void setMaterial(const D3DMATERIAL8 *material);
+		void setPixelShader(unsigned long shaderHandle);
+		void setPixelShaderConstant(unsigned int startRegister, const void *constantData, unsigned int count);
+		void setRenderState(D3DRENDERSTATETYPE state, unsigned long value);
+		void setScissorRect(const RECT *rect);
+		void setStreamSource(unsigned int stream, Direct3DVertexBuffer8 *data, unsigned int stride);
+		void setTexture(unsigned long stage, Direct3DBaseTexture8 *texture);
+		void setTextureStageState(unsigned long stage, D3DTEXTURESTAGESTATETYPE type, unsigned long value);
+		void setTransform(D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix);
+		void setViewport(const D3DVIEWPORT8 *viewport);
+		void setVertexShader(unsigned long shaderHandle);
+		void setVertexShaderConstant(unsigned int startRegister, const void *constantData, unsigned int count);
+
+	private:
+		// Individual states
+		void captureRenderState(D3DRENDERSTATETYPE state);
+		void captureTextureStageState(unsigned long stage, D3DTEXTURESTAGESTATETYPE type);
+		void captureTransform(D3DTRANSFORMSTATETYPE state);
+
+		// Pixel states
+		void capturePixelRenderStates();
+		void capturePixelTextureStates();
+		void capturePixelShaderStates();
+	
+		// Vertex states
+		void captureVertexRenderStates();
+		void captureVertexTextureStates();
+		void captureLightStates();
+		void captureVertexShaderStates();
+	
+		// All (remaining) states
+		void captureTextures();
+		void captureVertexTextures();
+		void captureDisplacementTextures();
+		void captureTexturePalette();
+		void captureVertexStreams();
+		void captureIndexBuffer();
+		void captureViewport();
+		void captureTransforms();
+		void captureTextureTransforms();
+		void captureClippingPlanes();
+		void captureMaterial();
+
+		// Creation parameters
+		Direct3DDevice8 *const device;
+		const D3DSTATEBLOCKTYPE type;
+
+		// State data
+		bool vertexShaderCaptured;
+		unsigned long vertexShaderHandle;
+
+		bool pixelShaderCaptured;
+		unsigned long pixelShaderHandle;
+
+		bool indexBufferCaptured;
+		Direct3DIndexBuffer8 *indexBuffer;
+		unsigned int baseVertexIndex;
+
+		bool renderStateCaptured[D3DRS_NORMALORDER + 1];
+		unsigned long renderState[D3DRS_NORMALORDER + 1];
+
+		bool textureStageStateCaptured[8][D3DTSS_RESULTARG + 1];
+		unsigned long textureStageState[8][D3DTSS_RESULTARG + 1];
+
+		bool streamSourceCaptured[16];
+		struct StreamSource
+		{
+			Direct3DVertexBuffer8 *vertexBuffer;
+			unsigned int stride;
+		};
+		StreamSource streamSource[16];
+
+		bool textureCaptured[8];
+		Direct3DBaseTexture8 *texture[8];
+
+		bool transformCaptured[512];
+		D3DMATRIX transform[512];
+
+		bool viewportCaptured;
+		D3DVIEWPORT8 viewport;
+
+		bool clipPlaneCaptured[6];
+		float clipPlane[6][4];
+
+		bool materialCaptured;
+		D3DMATERIAL8 material;
+
+		bool lightCaptured[8];   // FIXME: Unlimited index
+		D3DLIGHT8 light[8];
+
+		bool lightEnableCaptured[8];   // FIXME: Unlimited index
+		int lightEnableState[8];
+
+		float pixelShaderConstant[8][4];
+		float vertexShaderConstant[256][4];
+
+		bool scissorRectCaptured;
+		RECT scissorRect;
+
+		bool paletteNumberCaptured;
+		unsigned int paletteNumber;
+
+		void clear();
+	};
+}
+
+#endif   // D3D8_Direct3DStateBlock8_hpp
\ No newline at end of file
diff --git a/src/D3D8/Direct3DSurface8.cpp b/src/D3D8/Direct3DSurface8.cpp
new file mode 100644
index 0000000..2a4dea6
--- /dev/null
+++ b/src/D3D8/Direct3DSurface8.cpp
@@ -0,0 +1,280 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DSurface8.hpp"
+
+#include "Direct3DBaseTexture8.hpp"
+#include "Debug.hpp"
+
+#include <malloc.h>
+#include <assert.h>
+
+extern bool quadLayoutEnabled;
+
+namespace D3D8
+{
+	static sw::Resource *getParentResource(Unknown *container)
+	{
+		Direct3DBaseTexture8 *baseTexture = dynamic_cast<Direct3DBaseTexture8*>(container);
+
+		if(baseTexture)
+		{
+			return baseTexture->getResource();
+		}
+
+		return 0;
+	}
+
+	int sampleCount(D3DMULTISAMPLE_TYPE multiSample)
+	{
+		if(multiSample == D3DMULTISAMPLE_2_SAMPLES)
+		{
+			return 2;
+		}
+		else if(multiSample == D3DMULTISAMPLE_4_SAMPLES)
+		{
+			return 4;
+		}
+		else if(multiSample == D3DMULTISAMPLE_8_SAMPLES)
+		{
+			return 8;
+		}
+		else if(multiSample == D3DMULTISAMPLE_16_SAMPLES)
+		{
+			return 16;
+		}
+
+		return 1;
+	}
+
+	Direct3DSurface8::Direct3DSurface8(Direct3DDevice8 *device, Unknown *container, int width, int height, D3DFORMAT format, D3DPOOL pool, D3DMULTISAMPLE_TYPE multiSample, bool lockable, unsigned long usage) : Surface(getParentResource(container), width, height, sampleCount(multiSample), translateFormat(format), lockable, (usage & D3DUSAGE_RENDERTARGET) == D3DUSAGE_RENDERTARGET || (usage & D3DUSAGE_DEPTHSTENCIL) == D3DUSAGE_DEPTHSTENCIL), device(device), container(container), width(width), height(height), format(format), pool(pool), multiSample(multiSample), lockable(lockable), usage(usage)
+	{
+		parentTexture = dynamic_cast<Direct3DBaseTexture8*>(container);
+
+		resource = new Direct3DResource8(device, D3DRTYPE_SURFACE, memoryUsage(width, height, format));
+	}
+
+	Direct3DSurface8::~Direct3DSurface8()
+	{
+		resource->Release();
+	}
+
+	long Direct3DSurface8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		if(iid == IID_IDirect3DSurface8 ||
+		   iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DSurface8::AddRef()
+	{
+		TRACE("");
+
+		if(parentTexture)
+		{
+			return parentTexture->AddRef();
+		}
+
+		return Unknown::AddRef();
+	}
+
+	unsigned long Direct3DSurface8::Release()
+	{
+		TRACE("");
+
+		if(parentTexture)
+		{
+			return parentTexture->Release();
+		}
+
+		return Unknown::Release();
+	}
+
+	long Direct3DSurface8::FreePrivateData(const GUID &guid)
+	{
+		TRACE("");
+
+		return resource->FreePrivateData(guid);
+	}
+
+	long Direct3DSurface8::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
+	{
+		TRACE("");
+
+		return resource->GetPrivateData(guid, data, size);
+	}
+
+	long Direct3DSurface8::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
+	{
+		TRACE("");
+
+		return resource->SetPrivateData(guid, data, size, flags);
+	}
+
+	long Direct3DSurface8::GetDevice(IDirect3DDevice8 **device)
+	{
+		TRACE("");
+
+		return resource->GetDevice(device);
+	}
+
+	long Direct3DSurface8::LockRect(D3DLOCKED_RECT *lockedRect, const RECT *rect, unsigned long flags)
+	{
+		TRACE("");
+
+		if(!lockedRect)
+		{
+			return INVALIDCALL();
+		}
+
+		lockedRect->Pitch = getExternalPitchB();
+
+		sw::Lock lock = sw::LOCK_READWRITE;
+
+		if(flags & D3DLOCK_DISCARD)
+		{
+			lock = sw::LOCK_DISCARD;
+		}
+
+		if(flags & D3DLOCK_READONLY)
+		{
+			lock = sw::LOCK_READONLY;
+		}
+
+		if(rect)
+		{
+			lockedRect->pBits = lockExternal(rect->left, rect->top, 0, lock, sw::PUBLIC);
+		}
+		else
+		{
+			lockedRect->pBits = lockExternal(0, 0, 0, lock, sw::PUBLIC);
+		}
+
+		unlockExternal();
+
+		return D3D_OK;
+	}
+
+	long Direct3DSurface8::UnlockRect()
+	{
+		TRACE("");
+
+		return D3D_OK;
+	}
+
+	long Direct3DSurface8::GetContainer(const IID &iid, void **container)
+	{
+		TRACE("");
+
+		if(!container)
+		{
+			return INVALIDCALL();
+		}
+
+		long result = this->container->QueryInterface(iid, container);
+
+		if(result == S_OK)
+		{
+			return D3D_OK;
+		}
+
+		return INVALIDCALL();
+	}
+
+	long Direct3DSurface8::GetDesc(D3DSURFACE_DESC *desc)
+	{
+		TRACE("");
+
+		if(!desc)
+		{
+			return INVALIDCALL();
+		}
+
+		desc->Format = format;
+		desc->Pool = pool;
+		desc->Type = D3DRTYPE_SURFACE;
+		desc->Height = height;
+		desc->Width = width;
+		desc->Size = size(getWidth(), getHeight(), getDepth(), getExternalFormat());
+		desc->MultiSampleType = multiSample;
+		desc->Usage = usage;
+
+		return D3D_OK;
+	}
+
+	sw::Format Direct3DSurface8::translateFormat(D3DFORMAT format)
+	{
+		switch(format)
+		{
+		case D3DFMT_DXT1:			return sw::FORMAT_DXT1;
+		case D3DFMT_DXT2:			return sw::FORMAT_DXT3;
+		case D3DFMT_DXT3:			return sw::FORMAT_DXT3;
+		case D3DFMT_DXT4:			return sw::FORMAT_DXT5;
+		case D3DFMT_DXT5:			return sw::FORMAT_DXT5;
+		case D3DFMT_R3G3B2:			return sw::FORMAT_R3G3B2;
+		case D3DFMT_A8R3G3B2:		return sw::FORMAT_A8R3G3B2;
+		case D3DFMT_X4R4G4B4:		return sw::FORMAT_X4R4G4B4;
+		case D3DFMT_A4R4G4B4:		return sw::FORMAT_A4R4G4B4;
+		case D3DFMT_A8R8G8B8:		return sw::FORMAT_A8R8G8B8;
+		case D3DFMT_G16R16:			return sw::FORMAT_G16R16;
+		case D3DFMT_A2B10G10R10:	return sw::FORMAT_A2B10G10R10;
+		case D3DFMT_P8:				return sw::FORMAT_P8;
+		case D3DFMT_A8P8:			return sw::FORMAT_A8P8;
+		case D3DFMT_A8:				return sw::FORMAT_A8;
+		case D3DFMT_R5G6B5:			return sw::FORMAT_R5G6B5;
+		case D3DFMT_X1R5G5B5:		return sw::FORMAT_X1R5G5B5;
+		case D3DFMT_A1R5G5B5:		return sw::FORMAT_A1R5G5B5;
+		case D3DFMT_R8G8B8:			return sw::FORMAT_R8G8B8;
+		case D3DFMT_X8R8G8B8:		return sw::FORMAT_X8R8G8B8;
+		case D3DFMT_V8U8:			return sw::FORMAT_V8U8;
+		case D3DFMT_L6V5U5:			return sw::FORMAT_L6V5U5;
+		case D3DFMT_Q8W8V8U8:		return sw::FORMAT_Q8W8V8U8;
+		case D3DFMT_X8L8V8U8:		return sw::FORMAT_X8L8V8U8;
+		case D3DFMT_A2W10V10U10:	return sw::FORMAT_A2W10V10U10;
+		case D3DFMT_V16U16:			return sw::FORMAT_V16U16;
+		case D3DFMT_L8:				return sw::FORMAT_L8;
+		case D3DFMT_A4L4:			return sw::FORMAT_A4L4;
+		case D3DFMT_A8L8:			return sw::FORMAT_A8L8;
+		case D3DFMT_D16:			return sw::FORMAT_D16;
+		case D3DFMT_D32:			return sw::FORMAT_D32;
+		case D3DFMT_D24X8:			return sw::FORMAT_D24X8;
+		case D3DFMT_D24S8:			return sw::FORMAT_D24S8;
+		default:
+			ASSERT(false);
+		}
+
+		return sw::FORMAT_NULL;
+	}
+
+	int Direct3DSurface8::bytes(D3DFORMAT format)
+	{
+		return Surface::bytes(translateFormat(format));
+	}
+
+	unsigned int Direct3DSurface8::memoryUsage(int width, int height, D3DFORMAT format)
+	{
+		return Surface::size(width, height, 1, translateFormat(format));
+	}
+}
diff --git a/src/D3D8/Direct3DSurface8.hpp b/src/D3D8/Direct3DSurface8.hpp
new file mode 100644
index 0000000..2796480
--- /dev/null
+++ b/src/D3D8/Direct3DSurface8.hpp
@@ -0,0 +1,74 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DSurface8_hpp
+#define D3D8_Direct3DSurface8_hpp
+
+#include "Unknown.hpp"
+#include "Surface.hpp"
+
+#include <d3d8.h>
+
+namespace D3D8
+{
+	class Direct3DDevice8;
+	class Direct3DResource8;
+	class Direct3DBaseTexture8;
+
+	class Direct3DSurface8 : public IDirect3DSurface8, public Unknown, public sw::Surface
+	{
+	public:
+		Direct3DSurface8(Direct3DDevice8 *device, Unknown *container, int width, int height, D3DFORMAT format, D3DPOOL pool, D3DMULTISAMPLE_TYPE multiSample, bool lockable, unsigned long usage);
+
+		virtual ~Direct3DSurface8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();		
+
+		// IDirect3DSurface8 methods
+		long __stdcall GetDevice(IDirect3DDevice8 **device);
+		long __stdcall SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags);
+		long __stdcall GetPrivateData(const GUID &guid, void *data, unsigned long *size);
+		long __stdcall FreePrivateData(const GUID &guid);
+		long __stdcall GetContainer(const IID &iid, void **container);
+		long __stdcall GetDesc(D3DSURFACE_DESC *desc);
+		long __stdcall LockRect(D3DLOCKED_RECT *lockedRect, const RECT *rect, unsigned long Flags);
+		long __stdcall UnlockRect();
+
+		// Internal methods
+		static sw::Format translateFormat(D3DFORMAT format);
+		static int bytes(D3DFORMAT format);
+
+	private:
+		static unsigned int memoryUsage(int width, int height, D3DFORMAT format);   // FIXME: Surface::size
+
+		// Creation parameters
+		Direct3DDevice8 *const device;
+		Unknown *const container;
+		const int width;
+		const int height;
+		const D3DFORMAT format;
+		const D3DMULTISAMPLE_TYPE multiSample;
+		const D3DPOOL pool;
+		const bool lockable;
+		const unsigned long usage;
+
+		Direct3DBaseTexture8 *parentTexture;
+		Direct3DResource8 *resource;
+	};
+}
+
+#endif // D3D8_Direct3DSurface8_hpp
diff --git a/src/D3D8/Direct3DSwapChain8.cpp b/src/D3D8/Direct3DSwapChain8.cpp
new file mode 100644
index 0000000..a49bf11
--- /dev/null
+++ b/src/D3D8/Direct3DSwapChain8.cpp
@@ -0,0 +1,230 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DSwapChain8.hpp"
+
+#include "Direct3DDevice8.hpp"
+#include "Config.hpp"
+#include "Configurator.hpp"
+#include "Debug.hpp"
+
+#include "FrameBufferDD.hpp"
+#include "FrameBufferGDI.hpp"
+
+namespace D3D8
+{
+	Direct3DSwapChain8::Direct3DSwapChain8(Direct3DDevice8 *device, D3DPRESENT_PARAMETERS *presentParameters) : device(device), presentParameters(*presentParameters)
+	{
+		frameBuffer = 0;
+
+		for(int i = 0; i < 3; i++)
+		{
+			backBuffer[i] = 0;
+		}
+
+		reset(presentParameters);
+	}
+
+	Direct3DSwapChain8::~Direct3DSwapChain8()
+	{
+		release();
+	}
+
+	long Direct3DSwapChain8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		if(iid == IID_IDirect3DSwapChain8 ||
+		   iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DSwapChain8::AddRef()
+	{
+		TRACE("");
+
+		return Unknown::AddRef();
+	}
+
+	unsigned long Direct3DSwapChain8::Release()
+	{
+		TRACE("");
+
+		return Unknown::Release();
+	}
+
+	long Direct3DSwapChain8::Present(const RECT *sourceRect, const RECT *destRect, HWND destWindowOverride, const RGNDATA *dirtyRegion)
+	{
+		TRACE("");
+
+		#if PERF_PROFILE
+			profiler.nextFrame();
+		#endif
+
+		void *source = backBuffer[0]->lockInternal(0, 0, 0, sw::LOCK_READONLY, sw::PUBLIC);   // FIXME: External
+		sw::Format format = backBuffer[0]->getInternalFormat();
+		int stride = backBuffer[0]->getInternalPitchB();
+
+		if(!sourceRect && !destRect)   // FIXME: More cases?
+		{
+			frameBuffer->flip(destWindowOverride, source, format, stride);
+		}
+		else   // TODO: Check for SWAPEFFECT_COPY
+		{
+			sw::Rect sRect(0, 0, 0, 0);
+			sw::Rect dRect(0, 0, 0, 0);
+
+			if(sourceRect)
+			{
+				sRect.x0 = sourceRect->left;
+				sRect.y0 = sourceRect->top;
+				sRect.x1 = sourceRect->right;
+				sRect.y1 = sourceRect->bottom;
+			}
+
+			if(destRect)
+			{
+				dRect.x0 = destRect->left;
+				dRect.y0 = destRect->top;
+				dRect.x1 = destRect->right;
+				dRect.y1 = destRect->bottom;
+			}
+
+			frameBuffer->blit(destWindowOverride, source, sourceRect ? &sRect : nullptr, destRect ? &dRect : nullptr, format, stride);
+		}
+
+		backBuffer[0]->unlockInternal();   // FIXME: External
+
+		return D3D_OK;
+	}
+
+	long Direct3DSwapChain8::GetBackBuffer(unsigned int index, D3DBACKBUFFER_TYPE type, IDirect3DSurface8 **backBuffer)
+	{
+		TRACE("");
+
+		if(!backBuffer/* || type != D3DBACKBUFFER_TYPE_MONO*/)
+		{
+			return INVALIDCALL();
+		}
+
+		if(index >= 3 || this->backBuffer[index] == 0)
+		{
+			return INVALIDCALL();
+		}
+
+		this->backBuffer[index]->AddRef();
+		*backBuffer = this->backBuffer[index]; 
+
+		return D3D_OK;
+	}
+
+	void Direct3DSwapChain8::reset(D3DPRESENT_PARAMETERS *presentParameters)
+	{
+		release();
+
+		this->presentParameters = *presentParameters;
+
+		ASSERT(presentParameters->BackBufferCount <= 3);   // Maximum of three back buffers
+
+		if(presentParameters->BackBufferCount == 0)
+		{
+			presentParameters->BackBufferCount = 1;
+		}
+
+		D3DDEVICE_CREATION_PARAMETERS creationParameters;
+		device->GetCreationParameters(&creationParameters);
+
+		HWND windowHandle = presentParameters->hDeviceWindow ? presentParameters->hDeviceWindow : creationParameters.hFocusWindow;
+			
+		int width = 0;
+		int height = 0;
+
+		if(presentParameters->Windowed && (presentParameters->BackBufferHeight == 0 || presentParameters->BackBufferWidth == 0))
+		{
+			RECT rectangle;
+			GetClientRect(windowHandle, &rectangle);
+
+			width = rectangle.right - rectangle.left;
+			height = rectangle.bottom - rectangle.top;
+		}
+		else
+		{
+			width = presentParameters->BackBufferWidth;
+			height = presentParameters->BackBufferHeight;
+		}
+
+		frameBuffer = createFrameBufferWin(windowHandle, presentParameters->BackBufferWidth, presentParameters->BackBufferHeight, presentParameters->Windowed == FALSE, true);
+
+		lockable = presentParameters->Flags & D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
+
+		backBuffer[0] = 0;
+		backBuffer[1] = 0;
+		backBuffer[2] = 0;
+
+		for(int i = 0; i < (int)presentParameters->BackBufferCount; i++)
+		{
+			backBuffer[i] = new Direct3DSurface8(device, this, width, height, presentParameters->BackBufferFormat, D3DPOOL_DEFAULT, presentParameters->MultiSampleType, lockable, D3DUSAGE_RENDERTARGET);
+			backBuffer[i]->bind();
+		}
+	}
+
+	void Direct3DSwapChain8::release()
+	{
+		delete frameBuffer;
+		frameBuffer = 0;
+
+		for(int i = 0; i < 3; i++)
+		{
+			if(backBuffer[i])
+			{
+				backBuffer[i]->unbind();
+				backBuffer[i] = 0;
+			}
+		}
+	}
+
+	void Direct3DSwapChain8::screenshot(void *destBuffer)
+	{
+		frameBuffer->screenshot(destBuffer);
+	}
+
+	void Direct3DSwapChain8::setGammaRamp(sw::GammaRamp *gammaRamp, bool calibrate)
+	{
+		frameBuffer->setGammaRamp(gammaRamp, calibrate);
+	}
+
+	void Direct3DSwapChain8::getGammaRamp(sw::GammaRamp *gammaRamp)
+	{
+		frameBuffer->getGammaRamp(gammaRamp);
+	}
+
+	void *Direct3DSwapChain8::lockBackBuffer(int index)
+	{
+		return backBuffer[index]->lockInternal(0, 0, 0, sw::LOCK_READWRITE, sw::PUBLIC);   // FIXME: External
+	}
+	
+	void Direct3DSwapChain8::unlockBackBuffer(int index)
+	{
+		backBuffer[index]->unlockInternal();   // FIXME: External
+	}
+}
diff --git a/src/D3D8/Direct3DSwapChain8.hpp b/src/D3D8/Direct3DSwapChain8.hpp
new file mode 100644
index 0000000..2a0c194
--- /dev/null
+++ b/src/D3D8/Direct3DSwapChain8.hpp
@@ -0,0 +1,70 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DSwapChain8_hpp
+#define D3D8_Direct3DSwapChain8_hpp
+
+#include "Unknown.hpp"
+
+#include "Direct3DSurface8.hpp"
+
+#include "FrameBufferWin.hpp"
+
+#include <d3d8.h>
+
+namespace D3D8
+{
+	class Direct3DSwapChain8 : public IDirect3DSwapChain8, public Unknown
+	{
+	public:
+		Direct3DSwapChain8(Direct3DDevice8 *device, D3DPRESENT_PARAMETERS *presentParameters);
+
+		virtual ~Direct3DSwapChain8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3DSwapChain8 methods
+	    long __stdcall Present(const RECT *sourceRect, const RECT *destRect, HWND destWindowOverride, const RGNDATA *dirtyRegion);
+	    long __stdcall GetBackBuffer(unsigned int index, D3DBACKBUFFER_TYPE type, IDirect3DSurface8 **backBuffer);
+
+		// Internal methods
+		void reset(D3DPRESENT_PARAMETERS *presentParameters);
+		
+		void screenshot(void *destBuffer);
+		void setGammaRamp(sw::GammaRamp *gammaRamp, bool calibrate);
+		void getGammaRamp(sw::GammaRamp *gammaRamp);
+
+		void *lockBackBuffer(int index);
+		void unlockBackBuffer(int index);
+
+	private:
+		void release();
+
+		// Creation parameters
+		Direct3DDevice8 *const device;
+		D3DPRESENT_PARAMETERS presentParameters;
+
+		bool lockable;
+
+		sw::FrameBufferWin *frameBuffer;
+
+	public:   // FIXME
+		Direct3DSurface8 *backBuffer[3];   // NOTE: Up to three
+	};
+}
+
+#endif // D3D8_Direct3DSwapChain8_hpp
diff --git a/src/D3D8/Direct3DTexture8.cpp b/src/D3D8/Direct3DTexture8.cpp
new file mode 100644
index 0000000..c7d8341
--- /dev/null
+++ b/src/D3D8/Direct3DTexture8.cpp
@@ -0,0 +1,242 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DTexture8.hpp"
+
+#include "Direct3DSurface8.hpp"
+#include "Resource.hpp"
+#include "Debug.hpp"
+
+#include <assert.h>
+
+namespace D3D8
+{
+	Direct3DTexture8::Direct3DTexture8(Direct3DDevice8 *device, unsigned int width, unsigned int height, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool) : Direct3DBaseTexture8(device, D3DRTYPE_TEXTURE, levels, usage), width(width), height(height), format(format), pool(pool)
+	{
+		if(levels == 0)
+		{
+			this->levels = sw::log2(sw::max((int)width, (int)height, 1)) + 1;
+		}
+
+		for(unsigned int level = 0; level < sw::MIPMAP_LEVELS; level++)
+		{
+			if(level < this->levels)
+			{
+				surfaceLevel[level] = new Direct3DSurface8(device, this, width, height, format, pool, D3DMULTISAMPLE_NONE, true, usage);
+				surfaceLevel[level]->bind();
+			}
+			else
+			{
+				surfaceLevel[level] = 0;
+			}
+
+			width = sw::max(1, (int)width / 2);
+			height = sw::max(1, (int)height / 2);
+		}
+	}
+
+	Direct3DTexture8::~Direct3DTexture8()
+	{
+		resource->lock(sw::DESTRUCT);
+
+		for(int level = 0; level < sw::MIPMAP_LEVELS; level++)
+		{
+			if(surfaceLevel[level])
+			{
+				surfaceLevel[level]->unbind();
+				surfaceLevel[level] = 0;
+			}
+		}
+
+		resource->unlock();
+	}
+
+	long Direct3DTexture8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		if(iid == IID_IDirect3DTexture8 ||
+		   iid == IID_IDirect3DBaseTexture8 ||
+		   iid == IID_IDirect3DResource8 ||
+		   iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DTexture8::AddRef()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::AddRef();
+	}
+
+	unsigned long Direct3DTexture8::Release()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::Release();
+	}
+
+	long Direct3DTexture8::FreePrivateData(const GUID &guid)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::FreePrivateData(guid);
+	}
+
+	long Direct3DTexture8::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetPrivateData(guid, data, size);
+	}
+
+	void Direct3DTexture8::PreLoad()
+	{
+		TRACE("");
+
+		Direct3DBaseTexture8::PreLoad();
+	}
+
+	long Direct3DTexture8::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::SetPrivateData(guid, data, size, flags);
+	}
+
+	long Direct3DTexture8::GetDevice(IDirect3DDevice8 **device)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetDevice(device);
+	}
+
+	unsigned long Direct3DTexture8::SetPriority(unsigned long newPriority)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::SetPriority(newPriority);
+	}
+
+	unsigned long Direct3DTexture8::GetPriority()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetPriority();
+	}
+
+	D3DRESOURCETYPE Direct3DTexture8::GetType()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetType();
+	}
+
+	unsigned long Direct3DTexture8::GetLevelCount()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetLevelCount();
+	}
+
+	unsigned long Direct3DTexture8::GetLOD()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetLOD();
+	}
+
+	unsigned long Direct3DTexture8::SetLOD(unsigned long newLOD)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::SetLOD(newLOD);
+	}
+
+	long Direct3DTexture8::GetLevelDesc(unsigned int level, D3DSURFACE_DESC *description)
+	{	
+		TRACE("");
+
+		if(level >= GetLevelCount() || !surfaceLevel[level])
+		{
+			return INVALIDCALL();
+		}
+
+		return surfaceLevel[level]->GetDesc(description);
+	}
+
+	long Direct3DTexture8::LockRect(unsigned int level, D3DLOCKED_RECT *lockedRect, const RECT *rect, unsigned long flags)
+	{
+		TRACE("");
+
+		if(!lockedRect || level >= GetLevelCount() || !surfaceLevel[level])
+		{
+			return INVALIDCALL();
+		}
+
+		return surfaceLevel[level]->LockRect(lockedRect, rect, flags);
+	}
+
+	long Direct3DTexture8::GetSurfaceLevel(unsigned int level, IDirect3DSurface8 **surface)
+	{
+		TRACE("");
+
+		*surface = 0;   // FIXME: Verify
+
+		if(level >= GetLevelCount() || !surfaceLevel[level])
+		{
+			return INVALIDCALL();
+		}
+
+		surfaceLevel[level]->AddRef();
+		*surface = surfaceLevel[level];
+
+		return D3D_OK;
+	}		
+
+	long Direct3DTexture8::UnlockRect(unsigned int level)
+	{
+		TRACE("");
+
+		if(level >= GetLevelCount() || !surfaceLevel[level])
+		{
+			return INVALIDCALL();
+		}
+
+		return surfaceLevel[level]->UnlockRect();
+	}
+
+	long Direct3DTexture8::AddDirtyRect(const RECT *dirtyRect)
+	{
+		TRACE("");
+
+	//	UNIMPLEMENTED();
+
+		return D3D_OK;
+	}
+
+	Direct3DSurface8 *Direct3DTexture8::getInternalSurfaceLevel(unsigned int level)
+	{
+		return surfaceLevel[level];
+	}
+}
diff --git a/src/D3D8/Direct3DTexture8.hpp b/src/D3D8/Direct3DTexture8.hpp
new file mode 100644
index 0000000..c031a27
--- /dev/null
+++ b/src/D3D8/Direct3DTexture8.hpp
@@ -0,0 +1,76 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DTexture8_hpp
+#define D3D8_Direct3DTexture8_hpp
+
+#include "Direct3DBaseTexture8.hpp"
+
+#include "Direct3DSurface8.hpp"
+
+#include "Config.hpp"
+
+#include <d3d8.h>
+
+namespace D3D8
+{
+	class Direct3DTexture8 : public IDirect3DTexture8, public Direct3DBaseTexture8
+	{
+	public:	
+		Direct3DTexture8(Direct3DDevice8 *device, unsigned int width, unsigned int height, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool);
+
+		virtual ~Direct3DTexture8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3DResource8 methods
+		long __stdcall FreePrivateData(const GUID &guid);
+		long __stdcall GetPrivateData(const GUID &guid, void *data, unsigned long *size);
+		void __stdcall PreLoad();
+		long __stdcall SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags);
+		long __stdcall GetDevice(IDirect3DDevice8 **device);
+		unsigned long __stdcall SetPriority(unsigned long newPriority);
+		unsigned long __stdcall GetPriority();
+		D3DRESOURCETYPE __stdcall GetType();
+
+		// IDirect3DBaseTexture methods
+		unsigned long __stdcall GetLevelCount();
+		unsigned long __stdcall GetLOD();
+		unsigned long __stdcall SetLOD(unsigned long newLOD);
+
+		// IDirect3DTexture8 methods
+		long __stdcall GetLevelDesc(unsigned int level, D3DSURFACE_DESC *description);
+		long __stdcall LockRect(unsigned int level, D3DLOCKED_RECT *lockedRect, const RECT *rect, unsigned long flags);
+		long __stdcall GetSurfaceLevel(unsigned int level, IDirect3DSurface8 **surface);
+		long __stdcall UnlockRect(unsigned int level);
+		long __stdcall AddDirtyRect(const RECT *dirtyRect);		
+
+		// Internal methods
+		Direct3DSurface8 *getInternalSurfaceLevel(unsigned int level);
+
+	private:
+		// Creation parameters
+		const unsigned int width;
+		const unsigned int height;
+		const D3DFORMAT format;
+		const D3DPOOL pool;
+
+		Direct3DSurface8 *surfaceLevel[sw::MIPMAP_LEVELS];
+	};
+}
+
+#endif // D3D8_Direct3DTexture8_hpp
diff --git a/src/D3D8/Direct3DVertexBuffer8.cpp b/src/D3D8/Direct3DVertexBuffer8.cpp
new file mode 100644
index 0000000..bb748d1
--- /dev/null
+++ b/src/D3D8/Direct3DVertexBuffer8.cpp
@@ -0,0 +1,221 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DVertexBuffer8.hpp"
+
+#include "Direct3DDevice8.hpp"
+#include "Resource.hpp"
+#include "Debug.hpp"
+
+#include <assert.h>
+
+namespace D3D8
+{
+	Direct3DVertexBuffer8::Direct3DVertexBuffer8(Direct3DDevice8 *device, unsigned int length, unsigned long usage, long FVF, D3DPOOL pool) : Direct3DResource8(device, D3DRTYPE_VERTEXBUFFER, length), length(length), usage(usage), FVF(FVF), pool(pool)
+	{
+		if(FVF)
+		{
+			unsigned int stride = 0;
+
+			switch(FVF & D3DFVF_POSITION_MASK)
+			{
+			case D3DFVF_XYZ:	stride += 12;	break;
+			case D3DFVF_XYZRHW:	stride += 16;	break;
+			case D3DFVF_XYZB1:	stride += 16;	break;
+			case D3DFVF_XYZB2:	stride += 20;	break;
+			case D3DFVF_XYZB3:	stride += 24;	break;
+			case D3DFVF_XYZB4:	stride += 28;	break;
+			case D3DFVF_XYZB5:	stride += 32;	break;
+			}
+
+			if(FVF & D3DFVF_NORMAL)   stride += 12;
+			if(FVF & D3DFVF_PSIZE)    stride += 4;
+			if(FVF & D3DFVF_DIFFUSE)  stride += 4;
+			if(FVF & D3DFVF_SPECULAR) stride += 4;
+
+			switch((FVF & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT)
+			{
+			case 8: stride += 4 + 4 * ((1 + (FVF >> 30)) % 4);
+			case 7: stride += 4 + 4 * ((1 + (FVF >> 28)) % 4);
+			case 6: stride += 4 + 4 * ((1 + (FVF >> 26)) % 4);
+			case 5: stride += 4 + 4 * ((1 + (FVF >> 24)) % 4);
+			case 4: stride += 4 + 4 * ((1 + (FVF >> 22)) % 4);
+			case 3: stride += 4 + 4 * ((1 + (FVF >> 20)) % 4);
+			case 2: stride += 4 + 4 * ((1 + (FVF >> 18)) % 4);
+			case 1: stride += 4 + 4 * ((1 + (FVF >> 16)) % 4);
+			case 0: break;
+			default:
+				ASSERT(false);
+			}
+
+			ASSERT(length >= stride);       // FIXME
+			ASSERT(length % stride == 0);   // D3D vertex size calculated incorrectly   // FIXME
+		}
+
+		vertexBuffer = new sw::Resource(length + 192 + 1024);   // NOTE: Applications can 'overshoot' while writing vertices
+	}
+
+	Direct3DVertexBuffer8::~Direct3DVertexBuffer8()
+	{
+		vertexBuffer->destruct();
+	}
+
+	long Direct3DVertexBuffer8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		if(iid == IID_IDirect3DVertexBuffer8 ||
+		   iid == IID_IDirect3DResource8 ||
+		   iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DVertexBuffer8::AddRef()
+	{
+		TRACE("");
+
+		return Direct3DResource8::AddRef();
+	}
+
+	unsigned long Direct3DVertexBuffer8::Release()
+	{
+		TRACE("");
+
+		return Direct3DResource8::Release();
+	}
+
+	long Direct3DVertexBuffer8::FreePrivateData(const GUID &guid)
+	{
+		TRACE("");
+
+		return Direct3DResource8::FreePrivateData(guid);
+	}
+
+	long Direct3DVertexBuffer8::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
+	{
+		TRACE("");
+
+		return Direct3DResource8::GetPrivateData(guid, data, size);
+	}
+
+	void Direct3DVertexBuffer8::PreLoad()
+	{
+		TRACE("");
+
+		Direct3DResource8::PreLoad();
+	}
+
+	long Direct3DVertexBuffer8::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
+	{
+		TRACE("");
+
+		return Direct3DResource8::SetPrivateData(guid, data, size, flags);
+	}
+
+	long Direct3DVertexBuffer8::GetDevice(IDirect3DDevice8 **device)
+	{
+		TRACE("");
+
+		return Direct3DResource8::GetDevice(device);
+	}
+
+	unsigned long Direct3DVertexBuffer8::SetPriority(unsigned long newPriority)
+	{
+		TRACE("");
+
+		return Direct3DResource8::SetPriority(newPriority);
+	}
+
+	unsigned long Direct3DVertexBuffer8::GetPriority()
+	{
+		TRACE("");
+
+		return Direct3DResource8::GetPriority();
+	}
+
+	D3DRESOURCETYPE Direct3DVertexBuffer8::GetType()
+	{
+		TRACE("");
+
+		return Direct3DResource8::GetType();
+	}
+
+	long Direct3DVertexBuffer8::Lock(unsigned int offset, unsigned int size, unsigned char **data, unsigned long flags)
+	{
+		TRACE("");
+
+		if(offset == 0 && size == 0)   // Lock whole buffer
+		{
+			size = length;
+		}
+
+		if(!data || offset + size > length)
+		{
+			return INVALIDCALL();
+		}
+
+		lockOffset = offset;
+		lockSize = size;
+
+		*data = (unsigned char*)vertexBuffer->lock(sw::PUBLIC) + offset;
+		vertexBuffer->unlock();
+
+		return D3D_OK;
+	}
+
+	long Direct3DVertexBuffer8::Unlock()
+	{
+		TRACE("");
+
+		return D3D_OK;
+	}
+
+	long Direct3DVertexBuffer8::GetDesc(D3DVERTEXBUFFER_DESC *description)
+	{
+		TRACE("");
+
+		if(!description)
+		{
+			return INVALIDCALL();
+		}
+
+		description->FVF = FVF;
+		description->Format = D3DFMT_VERTEXDATA;
+		description->Pool = pool;
+		description->Size = length;
+		description->Type = D3DRTYPE_VERTEXBUFFER;
+		description->Usage = usage;
+
+		return D3D_OK;
+	}
+
+	int Direct3DVertexBuffer8::getLength() const
+	{
+		return length;
+	}
+
+	sw::Resource *Direct3DVertexBuffer8::getResource() const
+	{
+		return vertexBuffer;
+	}
+}
diff --git a/src/D3D8/Direct3DVertexBuffer8.hpp b/src/D3D8/Direct3DVertexBuffer8.hpp
new file mode 100644
index 0000000..435d012
--- /dev/null
+++ b/src/D3D8/Direct3DVertexBuffer8.hpp
@@ -0,0 +1,75 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DVertexBuffer8_hpp
+#define D3D8_Direct3DVertexBuffer8_hpp
+
+#include "Direct3DResource8.hpp"
+
+#include <d3d8.h>
+
+namespace sw
+{
+	class Resource;
+}
+
+namespace D3D8
+{
+	class Direct3DVertexBuffer8 : public IDirect3DVertexBuffer8, public Direct3DResource8
+	{
+	public:
+		Direct3DVertexBuffer8(Direct3DDevice8 *device, unsigned int length, unsigned long usage, long FVF, D3DPOOL pool);
+
+		virtual ~Direct3DVertexBuffer8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3DResource8 methods
+		long __stdcall FreePrivateData(const GUID &guid);
+		long __stdcall GetPrivateData(const GUID &guid, void *data, unsigned long *size);
+		void __stdcall PreLoad();
+		long __stdcall SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags);
+		long __stdcall GetDevice(IDirect3DDevice8 **device);
+		unsigned long __stdcall SetPriority(unsigned long newPriority);
+		unsigned long __stdcall GetPriority();
+		D3DRESOURCETYPE __stdcall GetType();
+
+		// IDirect3DVertexBuffer8 methods
+		long __stdcall Lock(unsigned int offset, unsigned int size, unsigned char **data, unsigned long flags);
+		long __stdcall Unlock();
+		long __stdcall GetDesc(D3DVERTEXBUFFER_DESC *description);
+
+		// Internal methods
+		int getLength() const;
+		sw::Resource *getResource() const;
+
+	private:
+		// Creation parameters
+		const unsigned int length;
+		const long usage;
+		const long FVF;
+		const D3DPOOL pool;
+
+		unsigned int lockOffset;
+		unsigned int lockSize;
+		unsigned char *lockData;
+
+		sw::Resource *vertexBuffer;
+	};
+}
+
+#endif // D3D8_Direct3DVertexBuffer8_hpp
diff --git a/src/D3D8/Direct3DVertexDeclaration8.cpp b/src/D3D8/Direct3DVertexDeclaration8.cpp
new file mode 100644
index 0000000..247af44
--- /dev/null
+++ b/src/D3D8/Direct3DVertexDeclaration8.cpp
@@ -0,0 +1,71 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DVertexDeclaration8.hpp"
+
+#include "Debug.hpp"
+
+#include <d3d8types.h>
+
+namespace D3D8
+{
+	Direct3DVertexDeclaration8::Direct3DVertexDeclaration8(Direct3DDevice8 *device, const unsigned long *vertexElement) : device(device)
+	{
+		int size = sizeof(unsigned long);
+		const unsigned long *element = vertexElement;
+
+		while(*element != 0xFFFFFFFF)
+		{
+			size += sizeof(unsigned long);
+			element++;
+		}
+
+		declaration = new unsigned long[size  / sizeof(unsigned long)];
+		memcpy(declaration, vertexElement, size);
+	}
+
+	Direct3DVertexDeclaration8::~Direct3DVertexDeclaration8()
+	{
+		delete[] declaration;
+		declaration = 0;
+	}
+
+	long Direct3DVertexDeclaration8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		ASSERT(false);   // Internal object
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DVertexDeclaration8::AddRef()
+	{
+		TRACE("");
+
+		return Unknown::AddRef();
+	}
+	
+	unsigned long Direct3DVertexDeclaration8::Release()
+	{
+		TRACE("");
+
+		return Unknown::Release();
+	}
+
+	const unsigned long *Direct3DVertexDeclaration8::getDeclaration() const
+	{
+		return declaration;
+	}
+}
\ No newline at end of file
diff --git a/src/D3D8/Direct3DVertexDeclaration8.hpp b/src/D3D8/Direct3DVertexDeclaration8.hpp
new file mode 100644
index 0000000..2b5b665
--- /dev/null
+++ b/src/D3D8/Direct3DVertexDeclaration8.hpp
@@ -0,0 +1,48 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DVertexDeclaration8_hpp
+#define D3D8_Direct3DVertexDeclaration8_hpp
+
+#include "Unknown.hpp"
+
+#include <d3d8.h>
+
+namespace D3D8
+{
+	class Direct3DDevice8;
+
+	class Direct3DVertexDeclaration8 : protected Unknown
+	{
+	public:
+		Direct3DVertexDeclaration8(Direct3DDevice8 *device, const unsigned long *vertexElements);
+
+		virtual ~Direct3DVertexDeclaration8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// Internal methods
+		const unsigned long *getDeclaration() const;
+
+	private:
+		// Creation parameters
+		Direct3DDevice8 *const device;
+		unsigned long *declaration;
+	};
+}
+
+#endif   // D3D8_Direct3DVertexDeclaration8_hpp
\ No newline at end of file
diff --git a/src/D3D8/Direct3DVertexShader8.cpp b/src/D3D8/Direct3DVertexShader8.cpp
new file mode 100644
index 0000000..d696507
--- /dev/null
+++ b/src/D3D8/Direct3DVertexShader8.cpp
@@ -0,0 +1,92 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DVertexShader8.hpp"
+
+#include "Debug.hpp"
+
+namespace D3D8
+{
+	Direct3DVertexShader8::Direct3DVertexShader8(Direct3DDevice8 *device, const unsigned long *declaration, const unsigned long *shaderToken) : device(device)
+	{
+		if(shaderToken)
+		{
+			vertexShader = new sw::VertexShader(shaderToken);
+
+			const unsigned long *token = shaderToken;
+			size = 0;
+
+			while(shaderToken[size] != 0x0000FFFF)
+			{
+				size++;
+			}
+
+			size++;
+
+			this->shaderToken = new unsigned long[size];
+			memcpy(this->shaderToken, shaderToken, size * sizeof(unsigned long));
+		}
+		else
+		{
+			vertexShader = 0;
+			this->shaderToken = 0;
+		}
+
+		this->declaration = new Direct3DVertexDeclaration8(device, declaration);
+	}
+
+	Direct3DVertexShader8::~Direct3DVertexShader8()
+	{
+		delete vertexShader;
+		vertexShader = 0;
+
+		delete[] shaderToken;
+		shaderToken = 0;
+
+		declaration->Release();
+	}
+
+	long Direct3DVertexShader8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		ASSERT(false);   // Internal object
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DVertexShader8::AddRef()
+	{
+		TRACE("");
+
+		return Unknown::AddRef();
+	}
+	
+	unsigned long Direct3DVertexShader8::Release()
+	{
+		TRACE("");
+
+		return Unknown::Release();
+	}
+
+	const sw::VertexShader *Direct3DVertexShader8::getVertexShader() const
+	{
+		return vertexShader;
+	}
+
+	const unsigned long *Direct3DVertexShader8::getDeclaration()
+	{
+		return declaration->getDeclaration();
+	}
+}
\ No newline at end of file
diff --git a/src/D3D8/Direct3DVertexShader8.hpp b/src/D3D8/Direct3DVertexShader8.hpp
new file mode 100644
index 0000000..046f7b4
--- /dev/null
+++ b/src/D3D8/Direct3DVertexShader8.hpp
@@ -0,0 +1,54 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DVertexShader8_hpp
+#define D3D8_Direct3DVertexShader8_hpp
+
+#include "VertexShader.hpp"
+#include "Direct3DVertexDeclaration8.hpp"
+
+#include "Unknown.hpp"
+
+namespace D3D8
+{
+	class Direct3DDevice8;
+
+	class Direct3DVertexShader8 : public Unknown
+	{
+	public:
+		Direct3DVertexShader8(Direct3DDevice8 *device, const unsigned long *declaration, const unsigned long *shaderToken);
+
+		virtual ~Direct3DVertexShader8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// Internal methods
+		const sw::VertexShader *getVertexShader() const;
+		const unsigned long *getDeclaration();
+
+	private:
+		// Creation parameters
+		Direct3DDevice8 *const device;
+		Direct3DVertexDeclaration8 *declaration;
+		unsigned long *shaderToken;
+		unsigned int size;
+
+		sw::VertexShader *vertexShader;
+	};
+}
+
+#endif   // D3D8_Direct3DVertexShader8_hpp
\ No newline at end of file
diff --git a/src/D3D8/Direct3DVolume8.cpp b/src/D3D8/Direct3DVolume8.cpp
new file mode 100644
index 0000000..3fa78ad
--- /dev/null
+++ b/src/D3D8/Direct3DVolume8.cpp
@@ -0,0 +1,189 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DVolume8.hpp"
+
+#include "Direct3DResource8.hpp"
+#include "Direct3DVolumeTexture8.hpp"
+#include "Direct3DSurface8.hpp"
+#include "Debug.hpp"
+
+#include <assert.h>
+
+namespace D3D8
+{
+	Direct3DVolume8::Direct3DVolume8(Direct3DDevice8 *device, Direct3DVolumeTexture8 *container, int width, int height, int depth, D3DFORMAT format, D3DPOOL pool, bool lockable, unsigned long usage) : Surface(container->getResource(), width, height, depth, translateFormat(format), lockable, false), container(container), width(width), height(height), depth(depth), format(format), pool(pool), lockable(lockable), usage(usage)
+	{
+		resource = new Direct3DResource8(device, D3DRTYPE_VOLUME, memoryUsage(width, height, depth, format));
+	}
+
+	Direct3DVolume8::~Direct3DVolume8()
+	{
+		resource->Release();
+	}
+
+	long __stdcall Direct3DVolume8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		if(iid == IID_IDirect3DVolume8 ||
+		   iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long __stdcall Direct3DVolume8::AddRef()
+	{
+		TRACE("");
+
+		return container->AddRef();
+	}
+
+	unsigned long __stdcall Direct3DVolume8::Release()
+	{
+		TRACE("");
+
+		return container->Release();
+	}
+
+	long Direct3DVolume8::FreePrivateData(const GUID &guid)
+	{
+		TRACE("");
+
+		return resource->FreePrivateData(guid);
+	}
+
+	long Direct3DVolume8::GetContainer(const IID &iid, void **container)
+	{
+		TRACE("");
+
+		if(!container)
+		{
+			return INVALIDCALL();
+		}
+
+		long result = this->container->QueryInterface(iid, container);
+
+		if(result == S_OK)
+		{
+			return D3D_OK;
+		}
+
+		return INVALIDCALL();
+	}
+
+	long Direct3DVolume8::GetDesc(D3DVOLUME_DESC *description)
+	{
+		TRACE("");
+
+		if(!description)
+		{
+			return INVALIDCALL();
+		}
+
+		description->Format = format;
+		description->Type = D3DRTYPE_VOLUME;
+		description->Usage = usage;
+		description->Pool = pool;
+		description->Width = width;
+		description->Height = height;
+		description->Depth = depth;
+
+		return D3D_OK;
+	}
+
+	long Direct3DVolume8::GetDevice(IDirect3DDevice8 **device)
+	{
+		TRACE("");
+
+		return resource->GetDevice(device);
+	}
+
+	long Direct3DVolume8::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
+	{
+		TRACE("");
+
+		return resource->GetPrivateData(guid, data, size);
+	}
+
+	long Direct3DVolume8::LockBox(D3DLOCKED_BOX *lockedVolume, const D3DBOX *box, unsigned long flags)
+	{
+		TRACE("");
+
+		if(!lockedVolume)
+		{
+			return INVALIDCALL();
+		}
+
+		lockedVolume->RowPitch = pitchB(getWidth(), getExternalFormat(), false);
+		lockedVolume->SlicePitch = sliceB(getWidth(), getHeight(), getExternalFormat(), false);
+
+		sw::Lock lock = sw::LOCK_READWRITE;
+
+		if(flags & D3DLOCK_DISCARD)
+		{
+			lock = sw::LOCK_DISCARD;
+		}
+
+		if(flags & D3DLOCK_READONLY)
+		{
+			lock = sw::LOCK_READONLY;
+		}
+
+		if(box)
+		{
+			lockedVolume->pBits = lockExternal(box->Left, box->Top, box->Front, lock, sw::PUBLIC);
+		}
+		else
+		{
+			lockedVolume->pBits = lockExternal(0, 0, 0, lock, sw::PUBLIC);
+		}
+
+		unlockExternal();
+		
+		return D3D_OK;
+	}
+
+	long Direct3DVolume8::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
+	{
+		TRACE("");
+		
+		return SetPrivateData(guid, data, size, flags);
+	}
+
+	long Direct3DVolume8::UnlockBox()
+	{
+		TRACE("");
+
+		return D3D_OK;
+	}
+
+	sw::Format Direct3DVolume8::translateFormat(D3DFORMAT format)
+	{
+		return Direct3DSurface8::translateFormat(format);
+	}
+
+	unsigned int Direct3DVolume8::memoryUsage(int width, int height, int depth, D3DFORMAT format)
+	{
+		return Surface::size(width, height, depth, translateFormat(format));
+	}
+}
diff --git a/src/D3D8/Direct3DVolume8.hpp b/src/D3D8/Direct3DVolume8.hpp
new file mode 100644
index 0000000..9b85fc7
--- /dev/null
+++ b/src/D3D8/Direct3DVolume8.hpp
@@ -0,0 +1,70 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DVolume8_hpp
+#define D3D8_Direct3DVolume8_hpp
+
+#include "Unknown.hpp"
+
+#include "Surface.hpp"
+
+#include <d3d8.h>
+
+namespace D3D8
+{
+	class Direct3DDevice8;
+	class Direct3DResource8;
+	class Direct3DVolumeTexture8;
+
+	class Direct3DVolume8 : public IDirect3DVolume8, public Unknown, public sw::Surface
+	{
+	public:
+		Direct3DVolume8(Direct3DDevice8 *device, Direct3DVolumeTexture8 *container, int width, int height, int depth, D3DFORMAT format, D3DPOOL pool, bool locakble, unsigned long usage);
+
+		virtual ~Direct3DVolume8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3DVolume8 methods
+		long __stdcall FreePrivateData(const GUID &guid);
+		long __stdcall GetContainer(const IID &iid, void **container);
+		long __stdcall GetDesc(D3DVOLUME_DESC *description);
+		long __stdcall GetDevice(IDirect3DDevice8 **device);
+		long __stdcall GetPrivateData(const GUID &guid, void *data, unsigned long *size);
+		long __stdcall LockBox(D3DLOCKED_BOX *lockedVolume, const D3DBOX *box, unsigned long flags);
+		long __stdcall SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags);
+		long __stdcall UnlockBox();
+
+	private:
+		static sw::Format translateFormat(D3DFORMAT format);
+		static unsigned int memoryUsage(int width, int height, int depth, D3DFORMAT format);
+
+		// Creation parameters
+		Direct3DVolumeTexture8 *const container;
+		const int width;
+		const int height;
+		const int depth;
+		const D3DFORMAT format;
+		const D3DPOOL pool;
+		const bool lockable;
+		const unsigned long usage;
+
+		Direct3DResource8 *resource;
+	};
+}
+
+#endif // D3D8_Direct3DVolume8_hpp
diff --git a/src/D3D8/Direct3DVolumeTexture8.cpp b/src/D3D8/Direct3DVolumeTexture8.cpp
new file mode 100644
index 0000000..90cecde
--- /dev/null
+++ b/src/D3D8/Direct3DVolumeTexture8.cpp
@@ -0,0 +1,250 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Direct3DVolumeTexture8.hpp"
+
+#include "Direct3DVolume8.hpp"
+#include "Resource.hpp"
+#include "Debug.hpp"
+
+#include <assert.h>
+
+namespace D3D8
+{
+	Direct3DVolumeTexture8::Direct3DVolumeTexture8(Direct3DDevice8 *device, unsigned int width, unsigned int height, unsigned int depth, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool) : Direct3DBaseTexture8(device, D3DRTYPE_VOLUMETEXTURE, levels, usage), width(width), height(height), depth(depth), format(format), pool(pool)
+	{
+		if(levels == 0)
+		{
+			this->levels = sw::log2(sw::max((int)width, (int)height, (int)depth, 1)) + 1;
+		}
+
+		for(unsigned int level = 0; level < sw::MIPMAP_LEVELS; level++)
+		{
+			if(level < this->levels)
+			{
+				volumeLevel[level] = new Direct3DVolume8(device, this, width, height, depth, format, pool, true, usage);
+				volumeLevel[level]->bind();
+			}
+			else
+			{
+				volumeLevel[level] = 0;
+			}
+
+			width = sw::max(1, (int)width / 2);
+			height = sw::max(1, (int)height / 2);
+			depth = sw::max(1, (int)depth / 2);
+		}
+	}
+
+	Direct3DVolumeTexture8::~Direct3DVolumeTexture8()
+	{
+		resource->lock(sw::DESTRUCT);
+
+		for(int level = 0; level < sw::MIPMAP_LEVELS; level++)
+		{
+			if(volumeLevel[level])
+			{
+				volumeLevel[level]->unbind();
+				volumeLevel[level] = 0;
+			}
+		}
+
+		resource->unlock();
+	}
+
+	long Direct3DVolumeTexture8::QueryInterface(const IID &iid, void **object)
+	{
+		TRACE("");
+
+		if(iid == IID_IDirect3DVolumeTexture8 ||
+		   iid == IID_IDirect3DBaseTexture8 ||
+		   iid == IID_IDirect3DResource8 ||
+		   iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Direct3DVolumeTexture8::AddRef()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::AddRef();
+	}
+
+	unsigned long Direct3DVolumeTexture8::Release()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::Release();
+	}
+
+	long Direct3DVolumeTexture8::FreePrivateData(const GUID &guid)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::FreePrivateData(guid);
+	}
+
+	long Direct3DVolumeTexture8::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetPrivateData(guid, data, size);
+	}
+
+	void Direct3DVolumeTexture8::PreLoad()
+	{
+		TRACE("");
+
+		Direct3DBaseTexture8::PreLoad();
+	}
+
+	long Direct3DVolumeTexture8::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::SetPrivateData(guid, data, size, flags);
+	}
+
+	long Direct3DVolumeTexture8::GetDevice(IDirect3DDevice8 **device)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetDevice(device);
+	}
+
+	unsigned long Direct3DVolumeTexture8::SetPriority(unsigned long newPriority)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::SetPriority(newPriority);
+	}
+
+	unsigned long Direct3DVolumeTexture8::GetPriority()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetPriority();
+	}
+
+	D3DRESOURCETYPE Direct3DVolumeTexture8::GetType()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetType();
+	}
+
+	unsigned long Direct3DVolumeTexture8::GetLevelCount()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetLevelCount();
+	}
+
+	unsigned long Direct3DVolumeTexture8::GetLOD()
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::GetLOD();
+	}
+
+	unsigned long Direct3DVolumeTexture8::SetLOD(unsigned long newLOD)
+	{
+		TRACE("");
+
+		return Direct3DBaseTexture8::SetLOD(newLOD);
+	}
+
+	long Direct3DVolumeTexture8::GetVolumeLevel(unsigned int level, IDirect3DVolume8 **volume)
+	{
+		TRACE("");
+
+		*volume = 0;   // FIXME: Verify
+
+		if(level >= GetLevelCount() || !volumeLevel[level])
+		{
+			return INVALIDCALL();
+		}
+
+		volumeLevel[level]->AddRef();
+		*volume = volumeLevel[level];
+
+		return D3D_OK;
+	}
+
+	long Direct3DVolumeTexture8::LockBox(unsigned int level, D3DLOCKED_BOX *lockedVolume, const D3DBOX *box, unsigned long flags)
+	{
+		TRACE("");
+
+		if(!lockedVolume || level >= GetLevelCount() || !volumeLevel[level])
+		{
+			return INVALIDCALL();
+		}
+
+		return volumeLevel[level]->LockBox(lockedVolume, box, flags);
+	}
+
+	long Direct3DVolumeTexture8::UnlockBox(unsigned int level)
+	{
+		TRACE("");
+
+		if(level >= GetLevelCount() || !volumeLevel[level])
+		{
+			return INVALIDCALL();
+		}
+
+		return volumeLevel[level]->UnlockBox();
+	}
+
+	long Direct3DVolumeTexture8::AddDirtyBox(const D3DBOX *dirtyBox)
+	{
+		TRACE("");
+
+		if(!dirtyBox)
+		{
+			return INVALIDCALL();
+		}
+
+		UNIMPLEMENTED();
+
+		return D3D_OK;
+	}
+
+	long Direct3DVolumeTexture8::GetLevelDesc(unsigned int level, D3DVOLUME_DESC *description)
+	{
+		TRACE("");
+
+		if(!description || level >= GetLevelCount() || !volumeLevel[level])
+		{
+			return INVALIDCALL();
+		}
+
+		volumeLevel[level]->GetDesc(description);
+
+		return D3D_OK;
+	}
+
+	Direct3DVolume8 *Direct3DVolumeTexture8::getInternalVolumeLevel(unsigned int level)
+	{
+		return volumeLevel[level];
+	}
+}
diff --git a/src/D3D8/Direct3DVolumeTexture8.hpp b/src/D3D8/Direct3DVolumeTexture8.hpp
new file mode 100644
index 0000000..fb0fbd7
--- /dev/null
+++ b/src/D3D8/Direct3DVolumeTexture8.hpp
@@ -0,0 +1,77 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Direct3DVolumeTexture8_hpp
+#define D3D8_Direct3DVolumeTexture8_hpp
+
+#include "Direct3DBaseTexture8.hpp"
+
+#include "Config.hpp"
+
+#include <d3d8.h>
+
+namespace D3D8
+{
+	class Direct3DVolume8;
+
+	class Direct3DVolumeTexture8 : public IDirect3DVolumeTexture8, public Direct3DBaseTexture8
+	{
+	public:
+		Direct3DVolumeTexture8(Direct3DDevice8 *device, unsigned int width, unsigned int height, unsigned int depth, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool);
+
+		virtual ~Direct3DVolumeTexture8();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// IDirect3DResource8 methods
+		long __stdcall FreePrivateData(const GUID &guid);
+		long __stdcall GetPrivateData(const GUID &guid, void *data, unsigned long *size);
+		void __stdcall PreLoad();
+		long __stdcall SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags);
+		long __stdcall GetDevice(IDirect3DDevice8 **device);
+		unsigned long __stdcall SetPriority(unsigned long newPriority);
+		unsigned long __stdcall GetPriority();
+		D3DRESOURCETYPE __stdcall GetType();
+
+		// IDirect3DBaseTexture methods
+		unsigned long __stdcall GetLevelCount();
+		unsigned long __stdcall GetLOD();
+		unsigned long __stdcall SetLOD(unsigned long newLOD);
+
+		// IDirect3DVolumeTexture8 methods
+		long __stdcall GetLevelDesc(unsigned int level, D3DVOLUME_DESC *description);
+		long __stdcall GetVolumeLevel(unsigned int level, IDirect3DVolume8 **volume);
+		long __stdcall LockBox(unsigned int level, D3DLOCKED_BOX *lockedVolume, const D3DBOX *box, unsigned long flags);
+		long __stdcall UnlockBox(unsigned int level);
+		long __stdcall AddDirtyBox(const D3DBOX *dirtyBox);
+
+		// Internal methods
+		Direct3DVolume8 *getInternalVolumeLevel(unsigned int level);
+
+	private:
+		// Creation parameters
+		const unsigned int width;
+		const unsigned int height;
+		const unsigned int depth;
+		const D3DFORMAT format;
+		const D3DPOOL pool;
+
+		Direct3DVolume8 *volumeLevel[sw::MIPMAP_LEVELS];
+	};
+}
+
+#endif // D3D8_Direct3DVolumeTexture8_hpp
diff --git a/src/D3D8/Unknown.cpp b/src/D3D8/Unknown.cpp
new file mode 100644
index 0000000..09ce03f
--- /dev/null
+++ b/src/D3D8/Unknown.cpp
@@ -0,0 +1,88 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Unknown.hpp"
+
+#include "Debug.hpp"
+
+#include <assert.h>
+
+namespace D3D8
+{
+	Unknown::Unknown()
+	{
+		referenceCount = 0;
+		bindCount = 0;
+	}
+
+	Unknown::~Unknown()
+	{
+		ASSERT(referenceCount == 0);
+		ASSERT(bindCount == 0);
+	}
+
+	long Unknown::QueryInterface(const IID &iid, void **object)
+	{
+		if(iid == IID_IUnknown)
+		{
+			AddRef();
+			*object = this;
+
+			return S_OK;
+		}
+		
+		*object = 0;
+
+		return NOINTERFACE(iid);
+	}
+
+	unsigned long Unknown::AddRef()
+	{
+		return InterlockedIncrement(&referenceCount);
+	}
+
+	unsigned long Unknown::Release()
+	{
+		int current = referenceCount;
+		
+		if(referenceCount > 0)
+		{
+			current = InterlockedDecrement(&referenceCount);
+		}
+
+		if(referenceCount == 0 && bindCount == 0)
+		{
+			delete this;
+		}
+
+		return current;
+	}
+
+	void Unknown::bind()
+	{
+		InterlockedIncrement(&bindCount);
+	}
+
+	void Unknown::unbind()
+	{
+		ASSERT(bindCount > 0);
+
+		InterlockedDecrement(&bindCount);
+
+		if(referenceCount == 0 && bindCount == 0)
+		{
+			delete this;
+		}
+	}
+}
\ No newline at end of file
diff --git a/src/D3D8/Unknown.hpp b/src/D3D8/Unknown.hpp
new file mode 100644
index 0000000..478a035
--- /dev/null
+++ b/src/D3D8/Unknown.hpp
@@ -0,0 +1,44 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef D3D8_Unknown_hpp
+#define D3D8_Unknown_hpp
+
+#include <unknwn.h>
+
+namespace D3D8
+{
+	class Unknown : IUnknown
+	{
+	public:
+		Unknown();
+
+		virtual ~Unknown();
+
+		// IUnknown methods
+		long __stdcall QueryInterface(const IID &iid, void **object);
+		unsigned long __stdcall AddRef();
+		unsigned long __stdcall Release();
+
+		// Internal methods
+		virtual void bind();
+		virtual void unbind();
+
+	private:
+		volatile long referenceCount;
+		volatile long bindCount;
+	};
+}
+
+#endif   // D3D8_Unknown_hpp
diff --git a/src/D3D8/d3d8.def b/src/D3D8/d3d8.def
new file mode 100644
index 0000000..7586a78
--- /dev/null
+++ b/src/D3D8/d3d8.def
@@ -0,0 +1,7 @@
+LIBRARY D3D8
+EXPORTS
+	CheckFullscreen			@1
+    DebugSetMute			@4
+    Direct3DCreate8			@5
+    ValidatePixelShader		@2
+    ValidateVertexShader	@3
diff --git a/src/D3D8/resource.h b/src/D3D8/resource.h
new file mode 100644
index 0000000..27a9f6b
--- /dev/null
+++ b/src/D3D8/resource.h
@@ -0,0 +1,28 @@
+//{{NO_DEPENDENCIES}}
+// Microsoft Visual C++ generated include file.
+// Used by D3D8-dll.rc
+//
+#define IDD_DIALOG1                     104
+#define IDD_DIALOG2                     105
+#define IDC_CHECK1                      1001
+#define IDC_SLIDER1                     1002
+#define IDC_CHECK2                      1003
+#define IDC_PROGRESS2                   1006
+#define IDC_PROGRESS3                   1007
+#define IDC_PROGRESS4                   1008
+#define IDC_PROGRESS5                   1009
+#define IDC_DATETIMEPICKER1             1011
+#define IDC_CHECK3                      1012
+#define IDC_EDIT1                       1013
+#define IDC_EDIT2                       1014
+
+// Next default values for new objects
+// 
+#ifdef APSTUDIO_INVOKED
+#ifndef APSTUDIO_READONLY_SYMBOLS
+#define _APS_NEXT_RESOURCE_VALUE        106
+#define _APS_NEXT_COMMAND_VALUE         40001
+#define _APS_NEXT_CONTROL_VALUE         1015
+#define _APS_NEXT_SYMED_VALUE           101
+#endif
+#endif
diff --git a/src/D3D8/resource1.h b/src/D3D8/resource1.h
new file mode 100644
index 0000000..ecf3584
--- /dev/null
+++ b/src/D3D8/resource1.h
@@ -0,0 +1,14 @@
+//{{NO_DEPENDENCIES}}
+// Microsoft Visual C++ generated include file.
+// Used by D3D8.rc
+
+// Next default values for new objects
+// 
+#ifdef APSTUDIO_INVOKED
+#ifndef APSTUDIO_READONLY_SYMBOLS
+#define _APS_NEXT_RESOURCE_VALUE        101
+#define _APS_NEXT_COMMAND_VALUE         40001
+#define _APS_NEXT_CONTROL_VALUE         1001
+#define _APS_NEXT_SYMED_VALUE           101
+#endif
+#endif
diff --git a/src/D3D9/D3D9.rc b/src/D3D9/D3D9.rc
index 9245d0c..d0ea0d0 100644
--- a/src/D3D9/D3D9.rc
+++ b/src/D3D9/D3D9.rc
@@ -7,7 +7,7 @@
 //
 // Generated from the TEXTINCLUDE 2 resource.
 //
-#include "windows.h"
+#include "Windows.h"
 #include "../Common/Version.h"
 /////////////////////////////////////////////////////////////////////////////
 #undef APSTUDIO_READONLY_SYMBOLS
diff --git a/src/D3D9/Direct3DDevice9.cpp b/src/D3D9/Direct3DDevice9.cpp
index ddc6606..515ab4b 100644
--- a/src/D3D9/Direct3DDevice9.cpp
+++ b/src/D3D9/Direct3DDevice9.cpp
@@ -343,7 +343,7 @@
 
 		TRACE("unsigned long count = %d, const D3DRECT *rects = 0x%0.8p, unsigned long flags = 0x%0.8X, unsigned long color = 0x%0.8X, float z = %f, unsigned long stencil = %d", count, rects, flags, color, z, stencil);
 
-		if(rects == 0 && count != 0)
+		if(!rects && count != 0)
 		{
 			return INVALIDCALL();
 		}
diff --git a/src/D3D9/Direct3DSwapChain9.cpp b/src/D3D9/Direct3DSwapChain9.cpp
index b3d1586..4b510aa 100644
--- a/src/D3D9/Direct3DSwapChain9.cpp
+++ b/src/D3D9/Direct3DSwapChain9.cpp
@@ -184,7 +184,7 @@
 				dRect.y1 = destRect->bottom;
 			}
 
-			frameBuffer->blit(window, source, sourceRect ? &sRect : 0, destRect ? &dRect : 0, format, stride);
+			frameBuffer->blit(window, source, sourceRect ? &sRect : nullptr, destRect ? &dRect : nullptr, format, stride);
 		}
 
 		backBuffer[0]->unlockInternal();   // FIXME: External