最近要做项工作,需要将游戏代码中原本的DirectX8.1SDK库升级到9.0。
整理思路如下:
一、命名统一问题
D8和D9内部细节本来就有很多变化,但先从表面上来看,D8和D9的类型函数名字,都存在或多或少的
差异,为了整合以前的代码,如果将类型名字一一作改变,那代码变动量将十分的巨大。通过宏命名方式
将会节省很多时间,举例如下:
#ifdef D3DX9
#include <d3dx9.h>
typedef IDirect3DDevice9 Device;
typedef IDirect3D9 Direct3D;
typedef IDirect3DVertexBuffer9 VertexBuffer ;
typedef IDirect3DIndexBuffer9 IndexBuffer;
typedef IDirect3DTexture9 Texture ;
typedef D3DMATERIAL9 Material;
typedef IDirect3DSurface9 Surface;
typedef void YVOID;
#define Direct3DCreate Direct3DCreate9
#endif
#ifdef D3DX8
#include <d3dx8.h>
typedef BYTE YVOID;
typedef IDirect3DDevice8 Device;
typedef IDirect3D8 Direct3D;
typedef IDirect3DVertexBuffer8 VertexBuffer ;
typedef IDirect3DIndexBuffer8 IndexBuffer;
typedef IDirect3DTexture8 Texture ;
typedef D3DMATERIAL8 Material;
typedef IDirect3DSurface8 Surface;
#define Direct3DCreate Direct3DCreate8
#endif
可以将其放入一个公用的文件中,如Common.h文件,然后需要用到的包含此文件。同理,D8和D9所需要的库也不通,可通过同样的方式,将其库导入。
ifdef D3DX9
#pragma comment(lib , "d3d9")
#pragma comment(lib , "d3dx9")
#endif
#ifdef D3DX8
#pragma comment(lib , "d3d8")
#pragma comment(lib , "d3dx8")
#endif
原游戏代码虽然十分巨大,通过宏命名后,将会使代码结构变的简单,如果再想更改到别的版本将会省很多工作量。剩下的大部分工作,将是对各个函数,方法属性的细节作版本不同的细微调整。
二、 细节变化
1、函数参数变化:
SetIndices少了参数BaseVertexIndex,,加到DrawIndexedPrimitive了。
DrawIndexedPrimitive增加第二个参数BaseVertexIndex,第一个顶点相对于顶点缓冲首地址的偏移数
CreateTexture
CreateVolumeTexture
CreateCubeTexture
CreateVertexBuffer
CreateIndexBuffer
CreateRenderTarget
CreateDepthStencilSurface 增加最后一个参数pSharedHandle,Reserverd.
GetBackBuffer
GetDisplayMode增加第一个参数iSwapChain,交换链
SetStreamSource增加了第三个参数OffsetInBytes
GetGammaRamp
SetGammaRamp
GetAdapterModeCount
EnumAdapterModes增加第二参数D3DFORMART
GetAdapterIdentifier:第二参数默认值改变,宏正好相反
D8:GetAdapterIdentifier(D3DADAPTER_DEFAULT, D3DENUM_NO_WHQL_LEVEL , &identifier)=
D9:GetAdapterIdentifier(D3DADAPTER_DEFAULT,0 ,&identifier)
D8:GetAdapterIdentifier(D3DADAPTER_DEFAULT, 0 , &identifier)=
D9:GetAdapterIdentifier(D3DADAPTER_DEFAULT,D3DENUM_WHQL_LEVEL ,&identifier)
CheckDeviceType
GetDeviceCaps模式有所变化,具体见SDK,举例如下:
//CheckDeviceType函数区别, CheckDeviceType和GetDeviceCaps配合使用验证适配器上的设备对希望使用的功能的支持度
#ifdef D3DX8
if(FAILED(m_pD3D->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,currentmode.Format,currentmode.Format,true)))
return E_FAIL;
if(FAILED(m_pD3D->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,currentmode.Format,currentmode.Format,true)))
return E_FAIL;
if(FAILED(m_pD3D->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_SW,currentmode.Format,currentmode.Format,true)))
{
//return E_FAIL;//调用失败
}
#endif
#ifdef D3DX9
if(FAILED(m_pD3D->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,currentmode.Format,D3DFMT_UNKNOWN,true))) //D3DFMT_UNKNOWN for window mode
return E_FAIL;
if(FAILED(m_pD3D->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,currentmode.Format,D3DFMT_UNKNOWN,true)))
return E_FAIL;
if(FAILED(m_pD3D->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_SW,currentmode.Format,D3DFMT_UNKNOWN,true)))
{
// return E_FAIL; //失败,第3方插件没有?
}
if(FAILED(m_pD3D->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_NULLREF,currentmode.Format,D3DFMT_UNKNOWN,true)))
{
// return E_FAIL; //永远失败
}
#endif
//D3DDEVTYPE D9比D8多出D3DDEVTYPE_NULLREF这个类型,但所以使用此类型的设备创建的对象都将无效;而且原本无用的D3DDEVTYPE_SW类型有了新作用
D3dcaps caps;
#ifdef D3DX8
if(FAILED(m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&caps)))
return E_FAIL;
if(FAILED(m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,&caps)))
return E_FAIL;
if(FAILED(m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_SW,&caps)))//D8中无意义
{
// return E_FAIL;
}
#endif
#ifdef D3DX9
if(FAILED(m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&caps)))
return E_FAIL;
if(FAILED(m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,&caps)))
return E_FAIL;
if(FAILED(m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_SW,&caps)))//经过IDirect3D9::RegisterSoftwareDevice注册过的第3方设备插件
{
// return E_FAIL; //如果没注册则调用失败
}
if(FAILED(m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_NULLREF,&caps)))//如上解释,任何调用都会失败
{
// return E_FAIL;
}
#endif
CheckDeviceMultiSampleType增加一个参数pQualityLevels ,可以为NULL
SetRenderTarget:
SetRenderTarget(pRenderTarget,pNewZStencil) →功能分割成两个函数
SetRenderTarget(RenderTargetIndex,pRenderTarget)
SetDepthStencilSurface(pNewZStencil)
GetRenderTarget多了个参数RenderTargetIndex
D3DXAssembleShader 参数增加pDefines,顺序也变了,具体见SDK
2、函数名字改变,同样的功能变更到其他函数中:D8→D9
CreateImageSurface→
CreateOffscreenPlainSurface
SetRenderState(D3DRS_SOFTWAREVERTEXPROCESSING ,BOOL b)→
SetSoftwareVertexProcessing(BOOL b)
SetTextureStageState(一些参数, )→
SetSamplerState(参数, )
参数:
D3DTSS_ADDRESSU D3DTSS_ADDRESSV D3DTSS_ADDRESSW D3DTSS_BORDERCOLOR
D3DTSS_MAGFILTER D3DTSS_MINFILTER D3DTSS_MIPFILTER
D3DTSS_MIPMAPLODBIAS D3DTSS_MAXMIPLEVEL D3DTSS_MAXANISOTROPY D3DTSS_FORCE_DWORD →
D3DSAMP_前缀的
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, 原来没有
D3DSAMP_FORCE_DWORD = 0x7fffffff,
SetVertexShader→
①固定管线FIXED-FUNCTION PIPELINE ,SetFVF
②着色器VertexShader,SetVertexShader
Remark:D9要使用SHADER必须先调用SetVertexDeclaration 再调用SetVertexShader
所以得先通过CreateVertexDeclaration()创建个IDirect3DVertexDeclaration9对象
例如:
VertexElement = {0 , 0 ,D3DDECLTYPE_FLOAT3 , D3DDECLMETHOD_DEFAULT ,D3DDECLUSAGE_POSITION , 0};
CreateVertexDeclaration(VertexElement,&VertexDeclaration);
SetVertexShaderConstant→
SetVertexShaderConstantF
ResourceManagerDiscardBytes→
EvictManagedResources
3、属性,结构改变:
D3DPRESENT_PARAMETERS结构变化如下:
d3dpp.FullScreen_PresentationInterval→
d3dpp.PresentationInterval
GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm)
d3dpp.BackBufferFormat = d3ddm.Format →
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN 在D8里设为D3DFMT_UNKNOWN 有时候会出错,获取不了当前显示模式。
D3DRENDERSTATETYPE结构变动挺多,具体SDK吧:举个例子
D3DRS_ZBIAS → D3DRS_DEPTHBIAS
D3DTEXTUREFILTERTYPE变动了一些
D3DTEXF_GAUSSIANCUBIC →
D3DTEXF_GAUSSIANQUAD