Attribute VB_Name = "DirectX9"
Option Explicit

Public Declare Function D3DCOLOR Lib "dx_vb" Alias "ARGB_" (ByVal A As Byte, ByVal R As Byte, ByVal G As Byte, ByVal B As Byte) As Long
Public Declare Function D3DColorValueFromColor Lib "dx_vb" Alias "D3DColorValue_" (ByVal c As Long) As D3DCOLORVALUE

' CONST ===========================================================

Public Const Pi As Single = 3.141593!
Public Const D3D_TRUE As Long = 1
Public Const D3D_FALSE As Long = 0

Public Enum HRESULT
  D3DERR_DEVICELOST = &H88760868
  D3DERR_DEVICENOTRESET = &H88760869
  D3DERR_DRIVERINTERNALERROR = &H88760827
End Enum

Public Enum D3DVERTEXBLENDFLAGS
  D3DVBF_DISABLE = 0
  D3DVBF_1WEIGHTS = 1
  D3DVBF_2WEIGHTS = 2
  D3DVBF_3WEIGHTS = 3
  D3DVBF_TWEENING = 255
  D3DVBF_0WEIGHTS = 256
End Enum

Public 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
End Enum

Public Enum D3DTEXTURETRANSFORMFLAGS
  D3DTTFF_DISABLE = 0
  D3DTTFF_COUNT1 = 1
  D3DTTFF_COUNT2 = 2
  D3DTTFF_COUNT3 = 3
  D3DTTFF_COUNT4 = 4
  D3DTTFF_PROJECTED = 256
End Enum

Public Enum D3DUSAGE
  D3DUSAGE_NONE = &H0
  D3DUSAGE_RENDERTARGET = &H1
  D3DUSAGE_DEPTHSTENCIL = &H2
  D3DUSAGE_DYNAMIC = &H200
  D3DUSAGE_AUTOGENMIPMAP = &H400
End Enum

Public Enum D3DTEXTUREADDRESS
  D3DTADDRESS_WRAP = 1
  D3DTADDRESS_MIRROR = 2
  D3DTADDRESS_CLAMP = 3
  D3DTADDRESS_BORDER = 4
  D3DTADDRESS_MIRRORONCE = 5
End Enum

Public Enum D3DTEXTUREFILTERTYPE
  D3DTEXF_NONE = 0
  D3DTEXF_POINT = 1
  D3DTEXF_LINEAR = 2
  D3DTEXF_ANISOTROPIC = 3
  D3DTEXF_PYRAMIDALQUAD = 6
  D3DTEXF_GAUSSIANQUAD = 7
  D3DTEXF_CONVOLUTIONMONO = 8
End Enum

Public Enum D3DTA
  D3DTA_SELECTMASK = &HF
  D3DTA_DIFFUSE = &H0
  D3DTA_CURRENT = &H1
  D3DTA_TEXTURE = &H2
  D3DTA_TFACTOR = &H3
  D3DTA_SPECULAR = &H4
  D3DTA_TEMP = &H5
  D3DTA_CONSTANT = &H6
  D3DTA_COMPLEMENT = &H10
  D3DTA_ALPHAREPLICATE = &H20
End Enum

Public Enum D3DTEXTUREOP
  D3DTOP_DISABLE = 1
  D3DTOP_SELECTARG1 = 2
  D3DTOP_SELECTARG2 = 3
  D3DTOP_MODULATE = 4
  D3DTOP_MODULATE2X = 5
  D3DTOP_MODULATE4X = 6
  D3DTOP_ADD = 7
  D3DTOP_ADDSIGNED = 8
  D3DTOP_ADDSIGNED2X = 9
  D3DTOP_SUBTRACT = 10
  D3DTOP_ADDSMOOTH = 11
  D3DTOP_BLENDDIFFUSEALPHA = 12
  D3DTOP_BLENDTEXTUREALPHA = 13
  D3DTOP_BLENDFACTORALPHA = 14
  D3DTOP_BLENDTEXTUREALPHAPM = 15
  D3DTOP_BLENDCURRENTALPHA = 16
  D3DTOP_PREMODULATE = 17
  D3DTOP_MODULATEALPHA_ADDCOLOR = 18
  D3DTOP_MODULATECOLOR_ADDALPHA = 19
  D3DTOP_MODULATEINVALPHA_ADDCOLOR = 20
  D3DTOP_MODULATEINVCOLOR_ADDALPHA = 21
  D3DTOP_BUMPENVMAP = 22
  D3DTOP_BUMPENVMAPLUMINANCE = 23
  D3DTOP_DOTPRODUCT3 = 24
  D3DTOP_MULTIPLYADD = 25
  D3DTOP_LERP = 26
End Enum

Public Enum D3DBLENDOP
  D3DBLENDOP_ADD = 1
  D3DBLENDOP_SUBTRACT = 2
  D3DBLENDOP_REVSUBTRACT = 3
  D3DBLENDOP_MIN = 4
  D3DBLENDOP_MAX = 5
End Enum

Public 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_BLENDFACTOR = 14
  D3DBLEND_INVBLENDFACTOR = 15
  D3DBLEND_SRCCOLOR2 = 16
  D3DBLEND_INVSRCCOLOR2 = 17
End Enum

Public Enum D3DTEXTURESTAGESTATETYPE
  D3DTSS_COLOROP = 1
  D3DTSS_COLORARG1 = 2
  D3DTSS_COLORARG2 = 3
  D3DTSS_ALPHAOP = 4
  D3DTSS_ALPHAARG1 = 5
  D3DTSS_ALPHAARG2 = 6
  D3DTSS_BUMPENVMAT00 = 7
  D3DTSS_BUMPENVMAT01 = 8
  D3DTSS_BUMPENVMAT10 = 9
  D3DTSS_BUMPENVMAT11 = 10
  D3DTSS_TEXCOORDINDEX = 11
  D3DTSS_BUMPENVLSCALE = 22
  D3DTSS_BUMPENVLOFFSET = 23
  D3DTSS_TEXTURETRANSFORMFLAGS = 24
  D3DTSS_COLORARG0 = 26
  D3DTSS_ALPHAARG0 = 27
  D3DTSS_RESULTARG = 28
  D3DTSS_CONSTANT = 32
End Enum

Public Enum D3DSAMPLERSTATETYPE
  D3DSAMP_ADDRESSU = 1
  D3DSAMP_ADDRESSV = 2
  D3DSAMP_ADDRESSW = 3
  D3DSAMP_BORDERCOLOR = 4
  D3DSAMP_MAGFILTER = 5
  D3DSAMP_MINFILTER = 6
  D3DSAMP_MIPFILTER = 7
  D3DSAMP_MIPMAPLODBIAS = 8
  D3DSAMP_MAXMIPLEVEL = 9
  D3DSAMP_MAXANISOTROPY = 10
  D3DSAMP_SRGBTEXTURE = 11
  D3DSAMP_ELEMENTINDEX = 12
  D3DSAMP_DMAPOFFSET = 13
End Enum

Public 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_WORLD = 256
End Enum

Public 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_A8B8G8R8 = 32
  D3DFMT_X8B8G8R8 = 33
  D3DFMT_G16R16 = 34
  D3DFMT_A2R10G10B10 = 35
  D3DFMT_A16B16G16R16 = 36

  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_A2W10V10U10 = 67

  D3DFMT_UYVY = &H59565955
  D3DFMT_R8G8_B8G8 = &H47424752
  D3DFMT_YUY2 = &H32595559
  D3DFMT_G8R8_G8B8 = &H42475247
  D3DFMT_DXT1 = &H31545844
  D3DFMT_DXT2 = &H32545844
  D3DFMT_DXT3 = &H33545844
  D3DFMT_DXT4 = &H34545844
  D3DFMT_DXT5 = &H35545844

  D3DFMT_D16_LOCKABLE = 70
  D3DFMT_D32 = 71
  D3DFMT_D15S1 = 73
  D3DFMT_D24S8 = 75
  D3DFMT_D24X8 = 77
  D3DFMT_D24X4S4 = 79
  D3DFMT_D16 = 80
  D3DFMT_D32F_LOCKABLE = 82
  D3DFMT_D24FS8 = 83
  D3DFMT_D32_LOCKABLE = 84
  D3DFMT_S8_LOCKABLE = 85
  D3DFMT_L16 = 81
  D3DFMT_VERTEXDATA = 100
  D3DFMT_INDEX16 = 101
  D3DFMT_INDEX32 = 102
  D3DFMT_Q16W16V16U16 = 110
  D3DFMT_MULTI2_ARGB8 = &H3154454D
  D3DFMT_R16F = 111
  D3DFMT_G16R16F = 112
  D3DFMT_A16B16G16R16F = 113
  D3DFMT_R32F = 114
  D3DFMT_G32R32F = 115
  D3DFMT_A32B32G32R32F = 116
  D3DFMT_CxV8U8 = 117
  D3DFMT_A1 = 118
  D3DFMT_BINARYBUFFER = 199
End Enum

Public Enum D3DMULTISAMPLE_TYPE
  D3DMULTISAMPLE_NONE = 0
  D3DMULTISAMPLE_NONMASKABLE = 1
  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
End Enum

Public Enum D3DSWAPEFFECT
  D3DSWAPEFFECT_DISCARD = 1
  D3DSWAPEFFECT_FLIP = 2
  D3DSWAPEFFECT_COPY = 3
End Enum

Public Enum CONST_D3DCREATEFLAGS
  D3DCREATE_FPU_PRESERVE = 2
  D3DCREATE_MULTITHREADED = 4
  D3DCREATE_PUREDEVICE = 16
  D3DCREATE_SOFTWARE_VERTEXPROCESSING = 32
  D3DCREATE_HARDWARE_VERTEXPROCESSING = 64
  D3DCREATE_MIXED_VERTEXPROCESSING = 128
  D3DCREATE_DISABLE_DRIVER_MANAGEMENT = &H100
  D3DCREATE_ADAPTERGROUP_DEVICE = &H200
  D3DCREATE_DISABLE_DRIVER_MANAGEMENT_EX = &H400
  D3DCREATE_NOWINDOWCHANGES = &H800
  D3DCREATE_DISABLE_PSGP_THREADING = &H2000
  D3DCREATE_ENABLE_PRESENTSTATS = &H4000
  D3DCREATE_DISABLE_PRINTSCREEN = &H8000&
  D3DCREATE_SCREENSAVER = &H10000000
End Enum

Public Enum D3DDEVTYPE
  D3DDEVTYPE_HAL = 1
  D3DDEVTYPE_REF = 2
  D3DDEVTYPE_SW = 3
  D3DDEVTYPE_NULLREF = 4
End Enum

Public Enum PRESENTATION_INTERVAL
  D3DPRESENT_DEFAULT = &H0
  D3DPRESENT_ONE = &H1
  D3DPRESENT_TWO = &H2
  D3DPRESENT_THREE = &H4
  D3DPRESENT_FOUR = &H8
  D3DPRESENT_IMMEDIATE = &H80000000
End Enum

Public Enum CONST_D3DCLEARFLAGS
  D3DCLEAR_TARGET = &H1
  D3DCLEAR_ZBUFFER = &H2
  D3DCLEAR_STENCIL = &H4
End Enum

Public Enum CONST_D3DFVF
  D3DFVF_RESERVED0 = &H1
  D3DFVF_POSITION_MASK = &H400E
  D3DFVF_XYZ = &H2
  D3DFVF_XYZRHW = &H4
  D3DFVF_XYZB1 = &H6
  D3DFVF_XYZB2 = &H8
  D3DFVF_XYZB3 = &HA
  D3DFVF_XYZB4 = &HC
  D3DFVF_XYZB5 = &HE
  D3DFVF_XYZW = &H4002
  D3DFVF_NORMAL = &H10
  D3DFVF_PSIZE = &H20
  D3DFVF_DIFFUSE = &H40
  D3DFVF_SPECULAR = &H80
  D3DFVF_TEXCOUNT_MASK = &HF00
  D3DFVF_TEXCOUNT_SHIFT = 8
  D3DFVF_TEX0 = &H0
  D3DFVF_TEX1 = &H100
  D3DFVF_TEX2 = &H200
  D3DFVF_TEX3 = &H300
  D3DFVF_TEX4 = &H400
  D3DFVF_TEX5 = &H500
  D3DFVF_TEX6 = &H600
  D3DFVF_TEX7 = &H700
  D3DFVF_TEX8 = &H800
  D3DFVF_LASTBETA_UBYTE4 = &H1000
  D3DFVF_LASTBETA_D3DCOLORVALUE = &H8000&
  D3DFVF_RESERVED2 = &H6000
End Enum

Public Enum D3DPOOL
  D3DPOOL_DEFAULT = 0
  D3DPOOL_MANAGED = 1
  D3DPOOL_SYSTEMMEM = 2
  D3DPOOL_SCRATCH = 3
End Enum

Public Enum D3DPRIMITIVETYPE
  D3DPT_POINTLIST = 1
  D3DPT_LINELIST = 2
  D3DPT_LINESTRIP = 3
  D3DPT_TRIANGLELIST = 4
  D3DPT_TRIANGLESTRIP = 5
  D3DPT_TRIANGLEFAN = 6
End Enum

Public Enum D3DRENDERSTATETYPE
  D3DRS_ZENABLE = 7
  D3DRS_FILLMODE = 8
  D3DRS_SHADEMODE = 9
  D3DRS_ZWRITEENABLE = 14
  D3DRS_ALPHATESTENABLE = 15
  D3DRS_LASTPIXEL = 16
  D3DRS_SRCBLEND = 19
  D3DRS_DESTBLEND = 20
  D3DRS_CULLMODE = 22
  D3DRS_ZFUNC = 23
  D3DRS_ALPHAREF = 24
  D3DRS_ALPHAFUNC = 25
  D3DRS_DITHERENABLE = 26
  D3DRS_ALPHABLENDENABLE = 27
  D3DRS_FOGENABLE = 28
  D3DRS_SPECULARENABLE = 29
  D3DRS_FOGCOLOR = 34
  D3DRS_FOGTABLEMODE = 35
  D3DRS_FOGSTART = 36
  D3DRS_FOGEND = 37
  D3DRS_FOGDENSITY = 38
  D3DRS_RANGEFOGENABLE = 48
  D3DRS_STENCILENABLE = 52
  D3DRS_STENCILFAIL = 53
  D3DRS_STENCILZFAIL = 54
  D3DRS_STENCILPASS = 55
  D3DRS_STENCILFUNC = 56
  D3DRS_STENCILREF = 57
  D3DRS_STENCILMASK = 58
  D3DRS_STENCILWRITEMASK = 59
  D3DRS_TEXTUREFACTOR = 60
  D3DRS_WRAP0 = 128
  D3DRS_WRAP1 = 129
  D3DRS_WRAP2 = 130
  D3DRS_WRAP3 = 131
  D3DRS_WRAP4 = 132
  D3DRS_WRAP5 = 133
  D3DRS_WRAP6 = 134
  D3DRS_WRAP7 = 135
  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_POINTSIZE = 154
  D3DRS_POINTSIZE_MIN = 155
  D3DRS_POINTSPRITEENABLE = 156
  D3DRS_POINTSCALEENABLE = 157
  D3DRS_POINTSCALE_A = 158
  D3DRS_POINTSCALE_B = 159
  D3DRS_POINTSCALE_C = 160
  D3DRS_MULTISAMPLEANTIALIAS = 161
  D3DRS_MULTISAMPLEMASK = 162
  D3DRS_PATCHEDGESTYLE = 163
  D3DRS_DEBUGMONITORTOKEN = 165
  D3DRS_POINTSIZE_MAX = 166
  D3DRS_INDEXEDVERTEXBLENDENABLE = 167
  D3DRS_COLORWRITEENABLE = 168
  D3DRS_TWEENFACTOR = 170
  D3DRS_BLENDOP = 171
  D3DRS_POSITIONDEGREE = 172
  D3DRS_NORMALDEGREE = 173
  D3DRS_SCISSORTESTENABLE = 174
  D3DRS_SLOPESCALEDEPTHBIAS = 175
  D3DRS_ANTIALIASEDLINEENABLE = 176
  D3DRS_MINTESSELLATIONLEVEL = 178
  D3DRS_MAXTESSELLATIONLEVEL = 179
  D3DRS_ADAPTIVETESS_X = 180
  D3DRS_ADAPTIVETESS_Y = 181
  D3DRS_ADAPTIVETESS_Z = 182
  D3DRS_ADAPTIVETESS_W = 183
  D3DRS_ENABLEADAPTIVETESSELLATION = 184
  D3DRS_TWOSIDEDSTENCILMODE = 185
  D3DRS_CCW_STENCILFAIL = 186
  D3DRS_CCW_STENCILZFAIL = 187
  D3DRS_CCW_STENCILPASS = 188
  D3DRS_CCW_STENCILFUNC = 189
  D3DRS_COLORWRITEENABLE1 = 190
  D3DRS_COLORWRITEENABLE2 = 191
  D3DRS_COLORWRITEENABLE3 = 192
  D3DRS_BLENDFACTOR = 193
  D3DRS_SRGBWRITEENABLE = 194
  D3DRS_DEPTHBIAS = 195
  D3DRS_WRAP8 = 198
  D3DRS_WRAP9 = 199
  D3DRS_WRAP10 = 200
  D3DRS_WRAP11 = 201
  D3DRS_WRAP12 = 202
  D3DRS_WRAP13 = 203
  D3DRS_WRAP14 = 204
  D3DRS_WRAP15 = 205
  D3DRS_SEPARATEALPHABLENDENABLE = 206
  D3DRS_SRCBLENDALPHA = 207
  D3DRS_DESTBLENDALPHA = 208
  D3DRS_BLENDOPALPHA = 209
End Enum

Public Enum D3DZBUFFERTYPE
  D3DZB_FALSE = 0
  D3DZB_TRUE = 1
  D3DZB_USEW = 2
End Enum

Public Enum D3DFILLMODE
  D3DFILL_POINT = 1
  D3DFILL_WIREFRAME = 2
  D3DFILL_SOLID = 3
End Enum

Public Enum D3DCULL
  D3DCULL_NONE = 1
  D3DCULL_CW = 2
  D3DCULL_CCW = 3
End Enum

Public Enum D3DLIGHTTYPE
  D3DLIGHT_POINT = 1
  D3DLIGHT_SPOT = 2
  D3DLIGHT_DIRECTIONAL = 3
End Enum

Public Enum D3DDECLTYPE
  D3DDECLTYPE_FLOAT1 = 0
  D3DDECLTYPE_FLOAT2 = 1
  D3DDECLTYPE_FLOAT3 = 2
  D3DDECLTYPE_FLOAT4 = 3
  D3DDECLTYPE_D3DCOLORVALUE = 4
  D3DDECLTYPE_UBYTE4 = 5
  D3DDECLTYPE_SHORT2 = 6
  D3DDECLTYPE_SHORT4 = 7
  D3DDECLTYPE_UBYTE4N = 8
  D3DDECLTYPE_SHORT2N = 9
  D3DDECLTYPE_SHORT4N = 10
  D3DDECLTYPE_USHORT2N = 11
  D3DDECLTYPE_USHORT4N = 12
  D3DDECLTYPE_UDEC3 = 13
  D3DDECLTYPE_DEC3N = 14
  D3DDECLTYPE_FLOAT16_2 = 15
  D3DDECLTYPE_FLOAT16_4 = 16
  D3DDECLTYPE_UNUSED = 17
End Enum

Public Enum D3DDECLMETHOD
  D3DDECLMETHOD_DEFAULT = 0
  D3DDECLMETHOD_PARTIALU = 1
  D3DDECLMETHOD_PARTIALV = 2
  D3DDECLMETHOD_CROSSUV = 3
  D3DDECLMETHOD_UV = 4
  D3DDECLMETHOD_LOOKUP = 5
  D3DDECLMETHOD_LOOKUPPRESAMPLED = 6
End Enum

Public Enum D3DDECLUSAGE
  D3DDECLUSAGE_POSITION = 0
  D3DDECLUSAGE_BLENDWEIGHT = 1
  D3DDECLUSAGE_BLENDINDICES = 2
  D3DDECLUSAGE_NORMAL = 3
  D3DDECLUSAGE_PSIZE = 4
  D3DDECLUSAGE_TEXCOORD = 5
  D3DDECLUSAGE_TANGENT = 6
  D3DDECLUSAGE_BINORMAL = 7
  D3DDECLUSAGE_TESSFACTOR = 8
  D3DDECLUSAGE_POSITIONT = 9
  D3DDECLUSAGE_COLOR = 10
  D3DDECLUSAGE_FOG = 11
  D3DDECLUSAGE_DEPTH = 12
  D3DDECLUSAGE_SAMPLE = 13
End Enum

Public Enum D3DRESOURCETYPE
  D3DRTYPE_SURFACE = 1
  D3DRTYPE_VOLUME = 2
  D3DRTYPE_TEXTURE = 3
  D3DRTYPE_VOLUMETEXTURE = 4
  D3DRTYPE_CubeTexture = 5
  D3DRTYPE_VERTEXBUFFER = 6
  D3DRTYPE_INDEXBUFFER = 7
End Enum

Public Enum D3DPTFILTERCAPS
  D3DPTFILTERCAPS_MINFPOINT = &H100&
  D3DPTFILTERCAPS_MINFLINEAR = &H200&
  D3DPTFILTERCAPS_MINFANISOTROPIC = &H400&
  D3DPTFILTERCAPS_MINFPYRAMIDALQUAD = &H800&
  D3DPTFILTERCAPS_MINFGAUSSIANQUAD = &H1000&
  D3DPTFILTERCAPS_MIPFPOINT = &H10000
  D3DPTFILTERCAPS_MIPFLINEAR = &H20000
  D3DPTFILTERCAPS_MAGFPOINT = &H1000000
  D3DPTFILTERCAPS_MAGFLINEAR = &H2000000
  D3DPTFILTERCAPS_MAGFANISOTROPIC = &H4000000
  D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD = &H8000000
  D3DPTFILTERCAPS_MAGFGAUSSIANQUAD = &H10000000
End Enum

Public Enum D3DPTEXTURECAPS
  D3DPTEXTURECAPS_PERSPECTIVE = &H1&
  D3DPTEXTURECAPS_POW2 = &H2&
  D3DPTEXTURECAPS_ALPHA = &H4&
  D3DPTEXTURECAPS_SQUAREONLY = &H20&
  D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE = &H40&
  D3DPTEXTURECAPS_ALPHAPALETTE = &H80&
  D3DPTEXTURECAPS_NONPOW2CONDITIONAL = &H100&
  D3DPTEXTURECAPS_PROJECTED = &H400&
  D3DPTEXTURECAPS_CUBEMAP = &H800&
  D3DPTEXTURECAPS_VOLUMEMAP = &H2000&
  D3DPTEXTURECAPS_MIPMAP = &H4000&
  D3DPTEXTURECAPS_MIPVOLUMEMAP = &H8000&
  D3DPTEXTURECAPS_MIPCUBEMAP = &H10000
  D3DPTEXTURECAPS_CUBEMAP_POW2 = &H20000
  D3DPTEXTURECAPS_VOLUMEMAP_POW2 = &H40000
  D3DPTEXTURECAPS_NOPROJECTEDBUMPENV = &H200000
End Enum

' TYPES ===========================================================

Public Type D3DPRESENT_PARAMETERS
  BackBufferWidth As Long
  BackBufferHeight As Long
  BackBufferFormat As D3DFORMAT
  BackBufferCount As Long
  MultiSampleType As D3DMULTISAMPLE_TYPE
  MultiSampleQuality As Long
  SwapEffect As D3DSWAPEFFECT
  hDeviceWindow As Long
  Windowed As Long
  EnableAutoDepthStencil As Long
  AutoDepthStencilFormat As D3DFORMAT
  Flags As Long
  FullScreen_RefreshRateInHz As Long
  PresentationInterval As PRESENTATION_INTERVAL
End Type

Public Type D3DMATRIX
  m11 As Single: m12 As Single: m13 As Single: m14 As Single
  m21 As Single: m22 As Single: m23 As Single: m24 As Single
  m31 As Single: m32 As Single: m33 As Single: m34 As Single
  m41 As Single: m42 As Single: m43 As Single: m44 As Single
End Type

Public Type D3DVECTOR2
  x As Single
  y As Single
End Type

Public Type D3DVECTOR
  x As Single
  y As Single
  z As Single
End Type

Public Type D3DVECTOR4
  x As Single
  y As Single
  z As Single
  w As Single
End Type

Public Type D3DPLANE
  A As Single
  B As Single
  c As Single
  d As Single
End Type

Public Type D3DQUATERNION
  x As Single
  y As Single
  z As Single
  w As Single
End Type

Public Type D3DCOLORVALUE
  R As Single
  G As Single
  B As Single
  A As Single
End Type

Type D3DVIEWPORT9
  x As Long
  y As Long
  Width As Long
  Height As Long
  MinZ As Single
  MaxZ As Single
End Type

Public Type D3DMATERIAL9
  Diffuse As D3DCOLORVALUE
  Ambient As D3DCOLORVALUE
  Specular As D3DCOLORVALUE
  Emissive As D3DCOLORVALUE
  Power As Single
End Type

Public Type D3DLIGHT9
  Type As D3DLIGHTTYPE
  Diffuse As D3DCOLORVALUE
  Specular As D3DCOLORVALUE
  Ambient As D3DCOLORVALUE
  Position As D3DVECTOR
  Direction As D3DVECTOR
  Range As Single
  Falloff As Single
  Attenuation0 As Single
  Attenuation1 As Single
  Attenuation2 As Single
  Theta As Single
  Phi As Single
End Type

Public Type D3DVERTEXELEMENT9
  Stream As Integer
  Offset As Integer
  dType As Byte
  Method As Byte
  Usage As Byte
  UsageIndex As Byte
End Type

Public Type D3DRECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type

Public Type D3DBOX
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
  Front As Long
  Back As Long
End Type

Public Type D3DSURFACE_DESC
  Format As D3DFORMAT
  rType As D3DRESOURCETYPE
  Usage As D3DUSAGE
  Pool As D3DPOOL
  MultiSampleType As D3DMULTISAMPLE_TYPE
  MultiSampleQuality As Long
  Width As Long
  Height As Long
End Type

Public Type D3DVSHADERCAPS2_0
  Caps As Long
  DynamicFlowControlDepth As Long
  NumTemps As Long
  StaticFlowControlDepth As Long
End Type

Public Type D3DPSHADERCAPS2_0
  Caps As Long
  DynamicFlowControlDepth As Long
  NumTemps As Long
  StaticFlowControlDepth As Long
  NumInstructionSlots As Long
End Type

Public Type D3DCAPS9
  DeviceType As D3DDEVTYPE
  AdapterOrdinal As Long
  
  Caps As Long
  Caps2 As Long
  Caps3 As Long
  PresentationIntervals As Long
  
  CursorCaps As Long
  
  DevCaps As Long
  
  PrimitiveMiscCaps As Long
  RasterCaps As Long
  ZCmpCaps As Long
  SrcBlendCaps As Long
  DestBlendCaps As Long
  AlphaCmpCaps As Long
  ShadeCaps As Long
  TextureCaps As D3DPTEXTURECAPS
  TextureFilterCaps As D3DPTFILTERCAPS
  CubeTextureFilterCaps As D3DPTFILTERCAPS
  VolumeTextureFilterCaps As D3DPTFILTERCAPS
  TextureAddressCaps As Long
  VolumeTextureAddressCaps As Long
  
  LineCaps As Long
  
  MaxTextureWidth As Long
  MaxTextureHeight As Long
  MaxVolumeExtent As Long
  
  MaxTextureRepeat As Long
  MaxTextureAspectRatio As Long
  MaxAnisotropy As Long
  MaxVertexW As Single
  
  GuardBandLeft As Single
  GuardBandTop As Single
  GuardBandRight As Single
  GuardBandBottom As Single
  
  ExtentsAdjust As Single
  StencilCaps As Long
  
  FVFCaps As Long
  TextureOpCaps As Long
  MaxTextureBlendStages As Long
  MaxSimultaneousTextures As Long
  
  VertexProcessingCaps As Long
  MaxActiveLights As Long
  MaxUserClipPlanes As Long
  MaxVertexBlendMatrices As Long
  MaxVertexBlendMatrixIndex As Long
  
  MaxPointSize As Single
  
  MaxPrimitiveCount As Long
  MaxVertexIndex As Long
  MaxStreams As Long
  MaxStreamStride As Long
  
  VertexShaderVersion As Long
  MaxVertexShaderConst As Long
  
  PixelShaderVersion As Long
  PixelShader1xMaxValue As Single
  
  DevCaps2 As Long
  
  MaxNpatchTessellationLevel As Single
  Reserved5 As Long
  
  MasterAdapterOrdinal As Long
  AdapterOrdinalInGroup As Long
  NumberOfAdaptersInGroup As Long
  DeclTypes As Long
  NumSimultaneousRTs As Long
  StretchRectFilterCaps As Long
  VS20Caps As D3DVSHADERCAPS2_0
  PS20Caps As D3DPSHADERCAPS2_0
  VertexTextureFilterCaps As Long
  MaxVShaderInstructionsExecuted As Long
  MaxPShaderInstructionsExecuted As Long
  MaxVertexShader30InstructionSlots As Long
  MaxPixelShader30InstructionSlots As Long
End Type

' DECLARES ========================================================

Private Declare Sub d3d_Create Lib "dx_vb" (ByRef pD3d As Long)

' FUNCTIONS =======================================================

Public Function D3DFVF_TEXCOORDSIZE(ByVal CoordIndex As Long, ByVal Size As Long) As CONST_D3DFVF
  Dim d As Long

  d = (Size + 2) And 3
  D3DFVF_TEXCOORDSIZE = d * 2 ^ (CoordIndex * 2 + 16)
End Function

Public Function VertexElement(Stream As Integer, Offset As Integer, dType As D3DDECLTYPE, Method As D3DDECLMETHOD, Usage As D3DDECLUSAGE, UsageIndex As Long) As D3DVERTEXELEMENT9
  VertexElement.Stream = Stream
  VertexElement.Offset = Offset
  VertexElement.dType = dType
  VertexElement.Method = Method
  VertexElement.Usage = Usage
  VertexElement.UsageIndex = UsageIndex
End Function

Public Function VertexElementEnd() As D3DVERTEXELEMENT9
  VertexElementEnd = VertexElement(&HFF, 0, D3DDECLTYPE_UNUSED, 0, 0, 0)
End Function

Public Function D3DTS_WORLDMATRIX(ByVal Index As Long) As D3DTRANSFORMSTATETYPE
  D3DTS_WORLDMATRIX = Index + 256
End Function

Public Function CreateDirect3D() As Direct3D9
Dim pD3d As Long
  d3d_Create pD3d
  If pD3d <> 0 Then
    Set CreateDirect3D = New Direct3D9
    CreateDirect3D.Ptr = pD3d
  End If
End Function

Public Function Vec2(ByVal x As Single, ByVal y As Single) As D3DVECTOR2
  Vec2.x = x
  Vec2.y = y
End Function

Public Function Vec3(ByVal x As Single, ByVal y As Single, ByVal z As Single) As D3DVECTOR
  Vec3.x = x
  Vec3.y = y
  Vec3.z = z
End Function

Public Function Vec4(ByVal x As Single, ByVal y As Single, ByVal z As Single, ByVal w As Single) As D3DVECTOR4
  Vec4.x = x
  Vec4.y = y
  Vec4.z = z
  Vec4.w = w
End Function

Public Function D3DCOLORVALUE(ByVal R As Single, ByVal G As Single, ByVal B As Single, ByVal A As Single) As D3DCOLORVALUE
  D3DCOLORVALUE.R = R
  D3DCOLORVALUE.G = G
  D3DCOLORVALUE.B = B
  D3DCOLORVALUE.A = A
End Function

Public Function D3DColorFromColorValue(c As D3DCOLORVALUE) As Long
  D3DColorFromColorValue = D3DCOLOR(c.A, c.R, c.G, c.B)
End Function
