Direct3D Devices
这一章讲述了Direct3D的设备对象,它提供了渲染流水线的一个抽象层。流水线的操作是通过设备的属性方法配置的。渲染在设备上的图片是要么2D拷贝操作,要么通过三维场景渲染。 设备的属性和方法控制了整个图形流水线,属性一般是通过Get和Set方法管理。每个设备都有一组它自己的capabilities。设备也是resource对象的容器,Direct3D通过IDirect3DR
这一章讲述了Direct3D的设备对象,它提供了渲染流水线的一个抽象层。流水线的操作是通过设备的属性方法配置的。渲染在设备上的图片是要么2D拷贝操作,要么通过三维场景渲染。
设备的属性和方法控制了整个图形流水线,属性一般是通过Get和Set方法管理。每个设备都有一组它自己的capabilities。设备也是resource对象的容器,Direct3D通过IDirect3DResource9接口管理资源。图形流水线的很多行为都是通过Render state控制的。应用程序可能需要频繁的设置render states,但是使用state block来保存一组设备属性(包括render state),使应用程序的设置更加方便和容易。
IDirect3DDevice9
IDrect3DDevice9控制流水线的行为,它包含大量的属性和方法。下面我们将探讨它的每个属性和方法。device 对象代表了渲染流水线。我们也可以从流水线里取到图片,提供流水线场景数据,然后将场景渲染到图片上。我们能够把流水线分成几个大的section:顶点数据Assembly,顶点处理,primitive Assembly以光栅化,像素处理,frame buffer和Video scan out。IDirect3DDevice9的接口如下:
| Read-Only Properties | |
| GetAvailableTextureMem | 可用的纹理内存,单位是KB |
| GetBackBuffer | back buffer image surface 接口 |
| GetCreationParameters | 设备的创建参数 |
| GetDirect3D | creating IDirect3D9接口 |
| GetDisplayMode | 视频显示模式 |
| GetFrontBufferData | front buffer 的拷贝 |
| GetNumberOfSwapChains | Number of swap chain |
| GetRasterStatus | Raster display status |
| GetRenderTargetData | surface data in a render target |
| GetSwapChain | swap chain on the device |
| Write-only Properties | |
| SetCursorPosition | cursor position |
| SetCursorProperties | cursor image and hot spot |
| SetDialogBoxMode | GDI compatability flag |
| Properties | |
| GetClipPlane | user-defined clipping plane |
| SetClipPlane | |
| GetClipStatus | clip status of rendered primitives |
| SetClipStatus | |
| GetCurrentTexturePalette | current texture palette index |
| SetCurrentTexturePalette | |
| GetDepthStencilSurface | Depth surface 接口 |
| SetDepthStencilSurface | |
| GetFVF | Flexible vertex format |
| SetFVF | |
| GetGammaRamp | Gamma correction lookup table |
| SetGammaRamp | |
| GetIndices | current index buffer |
| SetIndices | |
| GetLight | light definition |
| SetLight | |
| GetLightEnable | light enabled flag |
| LightEnable | |
| GetMaterial | Primitive material properties |
| SetMaterial | |
| GetNPatchMode | N-Patch tessellation mode |
| SetNPatchMode | |
| GetPaletteEntries | Texture palette entries |
| SetPaletteEntries | |
| GetPixelShader | pixel shader program handle |
| SetPixelShader | |
| GetPixelShaderConstantB | Boolean constant registers |
| SetPixelShaderConstantB | |
| GetPixelShaderConstantF | floating-pont constant registers |
| SetPixelShaderConstantF | |
| GetPixelShaderConstantI | integer constant registers |
| SetPixelShaderConstantI | |
| GerRenderState | Pipeline control values |
| SetRenderState | |
| GetRenderTarget | Target surface for rendered pixels |
| SetRenderTarget | |
| GetSamplerState | Texture sampler control values |
| SetSamplerState | |
| GetScissorRect | Scissor test rectangle |
| SetScissorRect | |
| GetSoftwareVertexProcessing | Vertex processing control |
| SetSoftwareVertexProcessing | |
| GetStreamSource | source vertex data buffers |
| SetStreamSource | |
| GetStreamSourceFreq | stream source sampling frequency |
| SetStreamSourceFreq | |
| GetTexture | Texture resources used by each stage |
| SetTexture | |
| GetTextureStageState | Texture stage control values |
| SetTextureStageState | |
| GetTransform | tranformation matrix |
| SetTransform | |
| GetVertexDeclaration | Vertex component declaration |
| SetVertexDeclaration | |
| GetVertexShader | vertex shader program handle |
| SetVertexShader | |
| GetVertexShaderConstantB | boolean constant registers |
| SetVertexShaderConstantB | |
| GetVertexShaderConstantF | floating-point constant registers |
| SetVertexShaderConstantF | |
| GetVertexShaderConstantI | Integer constant registers |
| SetVertexShaderConstantI | |
| GetViewport | Rendering viewport extent |
| SetViewport | |
| Methods | |
| BeginScene | Make the start of a scene |
| BeginStateBlock | Make device state for capture |
| Clear | Clear the current viewport on the device |
| ColorFill | Fills a rectangular area with a color |
| CreateAdditionalSwapChain | Create an additional swap chain on a windowed device |
| CreateCubeTexture | Create a cube map texture sources |
| CreateDepthStencilSurface | Create a depth/stencil surface resource |
| CreateIndexBuffer | Create an index buffer resource |
| CreateOffScreenPlainSurface | Create an off-screen surface resource |
| CreatePixelShader | Create a pixel shader |
| CreateQuery | Create a device query |
| CreateRenderTarget | Create a render target resource |
| CreateStateBlock | Create a state block |
| CreateTexture | Create a texture resource |
| CreateVertexBuffer | Create a vertex buffer resource |
| CreateVertexDeclaration | Create a vertex declaration |
| CreateVertexShader | Create a vertex shader |
| CreateVolumeTexture | Create a volume texture resource |
| DeletePatch | Destroy a cached patch tesselation |
| DrawIndexedPrimitive | Draw indexed primitives from the current streams. |
| DrawIndexedPrimitiveUP | Draw indexed primitives from user data. |
| DrawPrimitiveUP | Draw primitives from usr data. |
| DrawRectData | Rectangular higher order surface patch |
| DrawTriPatch | Triangular higher order surface patch. |
| EndScene | Make the end of a scene |
| EndStateBlock | Capture device state changes |
| EvictManagedResources | Flush managed resources from the device |
| MultiplyTransform | Post-multiply onto a device transformation matrix |
| Present | Presents a rendered image for display |
| ProcessVertices | Process vertices in software |
| Reset | Resets the device's display characteristcs |
| ShowCursor | Displays or hides the cursor |
| StretchRect | Device memory blit with stretching |
| TestCooperativeLevel | Checks exclusive ownership of the device |
| UpdateSurface | system to device memory surfaces |
| UpdateTexture | system to device memory texture transfer. |
| ValidateDevice | Validate current device state. |
Device methods by pipeline section
| Vertex Assembly | |
| BeginScene | |
| CreateIndexBuffer | |
| CreateVertexBuffer | |
| DeletePatch | |
| DrawIndexedPrimitive | |
| DrawIndexedPrimitiveUP | |
| DrawPrimitive | |
| DrawPrimitiveUP | |
| DrawRectPatch | |
| DrawTriPatch | |
| EndScene | |
| SetIndices | |
| SetNPatchMode | |
| SetRenderState | |
| SetStreamSource | |
| SetStreamSourceFreq | |
| SetVertexDeclaration | |
| Vertex Processing | CreateVertexDeclaration |
| CreateVertexShader | |
| LightEnable | |
| MultiplyTransform | |
| ProcessVertices | |
| SetClipPlane | |
| SetClipStatus | |
| SetFVF | |
| SetLight | |
| SetMaterial | |
| SetRenderState | |
| SetSoftwareVertexProcessing | |
| SetTransform | |
| SetVertexShader | |
| SetVertexShaderConstantB | |
| SetVertexShaderConstantF | |
| SetVertexShaderConstantI | |
| SetViewPort | |
| Primitive Rasterization | SetRenderState |
| Pixel Processing | CreatePixelShader |
| MultiplyTransform | |
| SetCurrentTexturePalette | |
| SetPaletteEntries | |
| SetPixelShader | |
| SetPixelShaderConstantB | |
| SetPixelShaderConstantF | |
| SetPixelShaderConstantI | |
| SetRenderState | |
| SetSamplerState | |
| SetTexture | |
| SetTextureStageState | |
| SetTransform | |
| Frame Buffer | Clear |
| ColorFill | |
| CreateAdditionalSwapChain | |
| CreateCubeTexture | |
| CreateDepthStencilSurface | |
| CreateOffscreenPlainSurface | |
| CreateRenderTarget | |
| CreateTexture | |
| CreateVolumeTexture | |
| EvictManagedResources | |
| GetAvailableTextureMem | |
| GetBackBuffer | |
| GetFrontBufferData | |
| GetNumberOfSwapChains | |
| GetRenderTargetData | |
| GetSwapChain | |
| Reset | |
| SetDepthStencilSurface | |
| SetDialogBoxMode | |
| SetRenderState | |
| SetRenderTarget | |
| SettScissorRect | |
| SetViewPort | |
| StretchRect | |
| UpdateSurface | |
| UpdateTexture | |
| Video Scan out | GetDisplayMode |
| GetRasterStatus | |
| Present | |
| SetCursorPosition | |
| SetCursorProperties | |
| SetGammaRamp | |
| ShowCursor |
Capabilities
一旦创建了设备,某些capabilities允许轻微的变化。 在某个设备上调用GetDeviceCaps可以返回一个真实设备对象的capabilities,但是如果在Direct3D对象上调用GetDeviceCaps则返回设备通用的一些capabilities。
HRESULT GetDeviceCaps(D3DCAPS9 * value);
typedef struct _D3DCAPS9
{
D3DDEVTYPE DeviceType;
UINT AdapterOrdinal;
DWORD Caps;
DWORD Caps2;
DWORD Caps3;
DWORD PresentationIntervals;
DWORD CursorCaps;
DWORD DevCaps;
DWORD PrimitiveMiscCaps;
DWORD RasterCaps;
DWORD ZCmpCaps;
DWORD SrcBlendCaps;
DWORD DestBlendCaps;
DWORD AlphaCmpCaps;
DWORD ShadeCaps;
DWORD TextureCaps;
DWORD TextureFilterCaps;
DWORD CubeTextureFilterCaps;
DWORD VolumeTextureFilterCaps;
DWORD TextureAddressCaps;
DWORD VolumeTextureAddressCaps;
DWORD LineCaps;
DWORD MaxTextureWidth;
DWORD MaxTextureHeight;
DWORD MaxVolumeExtent;
DWORD MaxTextureRepeat;
DWORD MaxTextureAspectRatio;
DWORD MaxAnisotropy;
float MaxVertexW;
float GuardBandLeft;
float GuardBandTop;
float GuardBandRight;
float GuardBandBottom;
float ExtentAdjust;
DWORD StencilCaps;
DWORD FVFCaps;
DWORD TextureOpCaps;
DWORD MaxTextureBlendStages;
DWORD MaxSimultaneousTextures;
DWORD VertexProcessingCaps;
DWORD MaxActiveLights;
DWORD MaxUserClipPlanes;
DWORD MaxVertexBlendMatrices;
DWORD MaxVertexBlendMatrixIndex;
float MaxPointSize;
DWORD MaxPrimitiveCount;
DWORD MaxVertexIndex;
DWORD MaxStreams;
DWORD MaxStreamStride;
DWORD VertexShaderVersion;
DWORD MaxVertexShaderConst;
DWORD PixelShaderVersion;
float PixelShader1XMaxValue;
DWORD DevCaps2;
float MaxNpatchTessellationLevel;
DWORD Reserved5;
UINT MasterAdapterOrdinal;
UINT AdapterOrdinalInGroup;
UINT NumberOfAdaptersInGroup;
UINT DeclTypes;
DWORD NumSimultaneousRTs;
DWORD StretchRectFilterCaps;
D3DVSHADERCAPS2_0 VS20Caps;
D3DPSSHADERCAPS2_0 PS20Caps;
DWORD VertexTextureFilterCaps;
DWORD MaxVShaderInstructionsExecuted;
DWORD MaxPShaderInstructionsExecuted;
DWORD MaxVertexShader30InstructionSlots;
DWORD MaxPixelShader30InstructionsSlots;
}
Device Capabilities By pipleline section
| Vertex Assembly | Dev Caps: |
| Draw Prim TL Vertex | |
| Quintic RT Patches | |
| RT Patches | |
| RT Patch Handle Zero | |
| TL Vertex System Memory | |
| TL Vertex Video Memory | |
| Dev Caps 2: | |
| Adaptive Tess N Patch | |
| Adaptive Tess RT Patch | |
| D Map N Patch | |
| Stream Offset | |
| Vertex Elements Can share Stream offset | |
| Decl Types | |
| FVF Caps | |
| Max Primitive Count | |
| Max Streams | |
| Max Stream Stride | |
| Max Vertex Index | |
| Max N Patch Tessellation Level | |
| Vertex Processing | Dev Caps: |
| HW Transform And Light | |
| Pure Device | |
| Max Active Lights | |
| Max User Clip Planes | |
| Max Vertex Blend Matrices | |
| Max Vertex Blend Matrices Index | |
| Max Vertex Shader 30 Instruction Slots | |
| Max Vertex Shader Const | |
| Max V Shader Instruction Executed | |
| Primitive Misc Caps: | |
| Clip Plane Scaled Points | |
| Clip TL Verts | |
| Fog Vertex Clamped | |
| Raster Caps | |
| Fog Range | |
| Fog Vertex | |
| Vertex Processing Caps | |
| Vertex Shader Version | |
| Vertex Texture Filter Caps | |
| VS 20 Caps | |
| Primitive Processing | Max User Clip Planes |
| Primitive Misc Caps: | |
| Clip Plane Scaled Points | |
| Clip TL Verts | |
| Cull CW | |
| Cull CCW | |
| Cull None | |
| Primitive Rasterization | Dev Caps: |
| HW Rasterization | |
| Pure Hardware | |
| Extents Adjust | |
| Guard Band Left | |
| Guard Band Top | |
| Guard Band Right | |
| Guard Band Bottom | |
| Line Caps: | |
| Antialias | |
| Texture | |
| Max Point Size | |
| Max Vertex W | |
| Raster Caps: | |
| Color Perspective | |
| Shade Caps | |
| Texture Caps: | |
| Projected | |
| Pixel Processing | Caps 2: |
| Can Auto Gen Mipmap | |
| Cube Texture Filter Caps | |
| Dev Caps: | |
| Separate Texture Memories | |
| Texture Non Local Vid Mem | |
| Texture System Memory | |
| Texture Video Memory | |
| Max Anisotropy | |
| Max Pixel Shader 30 Instruction Slots | |
| Max P Shader Instructions Executed | |
| Max Simultaneous Textures | |
| Max Texture Aspect Ratio | |
| Max Texture Blend Stages | |
| Max Texture Height | |
| Max Texture Repeat | |
| Max Texture Width | |
| Max Volume Extent | |
| Pixel Shader Version | |
| Pixel Shader 1* max Value | |
| Primitive Misc Caps: | |
| Per Stage Constant | |
| TSS Arg Temp | |
| PS 20 Caps | |
| Raster Caps | |
| Anisotropy | |
| Mipmap LOD Bias | |
| Frame Buffer | Alpha Cmp Caps |
| Caps 3: | |
| Alpha FullScreen Flip or Discard | |
| Copy To System Mem | |
| Copy To Vid Mem | |
| Dest Blend Caps | |
| Line Caps: | |
| Alpha Cmp | |
| Blend | |
| Fog | |
| Z Test | |
| Num Simultaneous RTS | |
| Primitive Misc Caps | |
| Blend Op | |
| Color Write Enable | |
| Independent Write Masks | |
| Mask Z | |
| MRT Independent Bit Depths | |
| MRT Post Pixel Shader Blending | |
| Separate Alpha Blend | |
| Vide Scan out | Adapter Ordinal In Group |
| Caps: | |
| Read Scan line | |
| Caps 2 | |
| Can Calibrate Gamma | |
| Full Screen Gamma | |
| Caps 3 | |
| Linear To sRGB Presentation | |
| Cursor Caps | |
| Dev Caps | |
| Can Render after Flip | |
| Master Adapter Ordinal | |
| Number of Adapters in Group | |
| Presentation Intervals |
Resources
设备与资源对象协同工作来执行场景渲染。 资源对象通常是场景数据的容器。两维和三维的纹理分别使用surface和volume来存放他们的内容。 back buffer,depth/stencil buffer 和设备的render target属性也是使用surfaces来存放数据。
基类接口包括IDirect3DVolume9和IDirect3DResource9。Volume对象并不是继承IDirect3DResource9的,所以它并不参与资源管理。剩下的资源都是继承于IDirect3DResource9。另外所有的Texture对象都是继承于IDirect3DBaseTexture9。
IDirect3DResource9
IDirect3DResource9是所有资源对象的基本接口,它给可管理的资源提供了资源内存管理功能。它只能管理创建在D3DPOOL_MANAGED池里面的资源。Managed的资源都有一个整数优先级,数字越大优先级越高。优先级越低的资源就可能先被discarded。 Non-Managed 资源优先级总是0。在同样的优先级,Direct3D总是优先discard那些最近使用最少的资源。在渲染的时候如果应用程序使用的资源内存超过了显卡的资源,Direct3D将使用最近使用最多的策略来释放资源。应用程序也可以显示的调用资源管理器来释放资源,EvictManageResource可以完成这个操作。释放的设备内存不必是连续的,分配一个对象可能会由于设备内存不足而失败,即使资源管理器已经释放了足够的空间。
创建在D3DPOOL_DEFAULT的资源不能进行管理,并且不能被资源管理器释放。应用程序只能手工的分配,拷贝和释放default pool里面的内存。
当纹理加载到设备里面是,你可以先使用GetAvailableTextureMem来查询可用纹理内存空间的估计值,然后根据值来调节纹理的detail。这个是值只是一个粗略的估计值不是一个准确的值。
interface IDirect3DResource9 : IUnknown
{
//read -only properties
HRESULT GetDevice(IDirect3DDevic9 ** value);
D3DRESOURCETYPE GetType();
//read /write properties
DWORD GetPriority();
DWORD SetPriority();
HRESULT GetPrivateData(REFGUID data_guid, void* value, DWORD * size);
HRESULT SetPrivateData(REFGUID data_guid, const void * value, DWORD size, DWORD flags);
//methods
HRESULT FreePrivateData(RFEGUID data_guid);
void PreLoad();
}
GetDevice方法返回拥有这个资源的设备。资源不能跨设备共享。资源类型可以通过GetType获取。根据这个类型我们就可以知道这个资源IID,然后使用QueryInterface就可以得到具体的资源接口。GetPrivateDat和SetPrivateData可以将任意的数据快绑定到任何Direct3D资源上。
Miscellaneous Properties
有几个设备属性与图形流水线没有直接的联系。 GetCreationParameters方法使用D3DDEVICE_CREATION_PARAMETERS结构体返回设备创建的参数。
HRESULT GetCreationParameters(D3DDEVICE_CREATION_PARAMETERS * value);
HRESULT GetDirect3D(IDirect3D** value);
typedef struct _D3DDEVICE_CREATION_PARAMETERS
{
UINT AdapterOrdinal;
D3DDEVTYPE DeviceType;
HWND hFocusWindow;
DWORD BehaviorFlags;
}D3DDEVICE_CREATION_PARAMETERS;
创建设备的IDirect3D实例可以通过GetDirect3D获取。
设备查询
设备查询就是从设备的驱动层获取信息。主要用途是渲染统计和设备的时间通知。只要设备查询对象和设备对象存在,你就可以向设备查询最新的信息。可以通过CreateQuery创建查询对象,D3DQUERYTPE是查询信息的类别。
typedef enum _D3DQUERYTYPE
{
D3DQUERYTYPE_BANDWIDTHTIMINGS = 17,
D3DQUERYTYPE_CACHEUTILIZATION = 18,
D3DQUERYTYPE_EVENT = 8,
D3DQUERYTYPE_INTERFACETIMINGS = 14,
D3DQUERYTYPE_OCCLUSION = 9,
D3DQUERYTYPE_PIPELINETIMINGS = 13,
D3DQUERYTYPE_PIXELTIMINGS = 16,
D3DQUERYTYPE_RESOURCEMANAGER = 5,
D3DQUERYTYPE_TIMESTAMP = 10,
D3DQUERYTYPE_TIMESTAMPFDISJOINT = 11,
D3DQUERYTYPE_TIMESTAMPFREQ = 12,
D3DQUERYTYPE_VCACHE = 4,
D3DQUERYTYPE_VERTEXSTATS = 6,
D3DQUERYTYPE_VERTEXTIMING = 15
} D3DQUERYTYPE;
HRESULT CreateQuery(D3DQUREYTYPE kind, IDirect3DQuery9 ** result);
interface IDirect3DQuery9 : IUnknown
{
//read-only properties
HRESULT GetDevice(IDirect3DDevice9 ** value);
D3DQUERYTYPE GetType();
HRESULT GetData(void * data, DWORD size,DWORD flags);
DWORD GetDataSize();
//methods
HRESULT ISSUE(DWORD flags);
}
一个查询对象通常有三种状态:signaled, building, 或者issued。Issue 方法用于将一个状态转换标记为有信号。当设备驱动返回了查询信息时,它也可以改变一个查询对象的状态。查询信息将通过渲染命令流发送给设备。Issue的flags参数将用于标识查询的开始和结束。
#define D3DISSUE_END (1<<0)
#define D3DISSUE_BEGIN (1<<1)
在查询开始和结束之间,查询对象可以汇报被发布的primitive的情况。顶点cache,资源管理统计和事件查询都有一个隐式的开始,应用程序issue查询对象结束。隐式查询开始如下表。统计查询自从present调用后收集的数据,然而时间和顶点cache查询返回自设备创建后的数据。对于occlusion, disjoint timestamp和timing 查询,应用程序只能显示的指定查询开始和结束。
| Query Type | Beginning |
| Vertex Cache | CreateDevice |
| Resource Manager Statistics | Present |
| Vertex Statistics | Present |
| Event | CreateDevice |
为了取得查询的结果,GetDataSize和GetData方法可以获得查询信息的拷贝。每种查询结果都对应一个数据结构。如下表,
| Query Type | Data Type |
| Bandwidth Timings | D3DDEVINFO_D3D9BANDWIDTHTIMINGS |
| Cache Utilization | D3DDEVINFO_D3D9CACHEUTILIZATION |
| Event | BOOL |
| Interface Timings | D3DDEVINFO_D3D9INTERFACETIMINGS |
| Occlusion | DWORD |
| Pipeline Timings | D3DDEVINFO_D3D9INTERFACETIMINGS |
| Pixel Shader Timings | D3DDEVINFO_D3D9STAGETIMINGS |
| Resource Manager Statistics | D3DDEVINFO_RESOURCEMANAGER |
| Timestamp | UINT64 |
| Timestamp Disjoint | BOOL |
| Timestamp Frequency | UINT64 |
| Vertex Cache | D3DDEVINFO_CACHE |
| Vertex Shader Timings | D3DDEVINFO_D3D9STAGETIMING |
| Vertex Statistics | D3DDEVINFO_D3DVERTEXSTATS |
GetData的flags参数可以让你同步刷新设备的命令队列。
事件查询可以用来当命令流里的某个特定点被驱动处理的时候通知应用程序,Occlusion查询返回传递到depth测试的像素数目。如果值为0,标识这组primitive全部被obscured。应用程序使用这个信息来cull occlueded物体。
顶点统计和资源管理查询只在debug时支持,retail runtime不支持这两个查询。
- 资源管理统计查询
资源管理统计查询返回一个D3DDEVINFO_RESOURCEMANAGER结构体,它包含了一个D3DRESOURCESTATS结构体数组,每个资源类型对应一个D3DRESOURCESTATS。D3DRTYPECOUNT定义了资源类型数目。
#define D3DRTYPECOUNT (D3DRTYPE_INDEXBUFFER + 1)
typedef struct _D3DDEVINFO_RESOURCEMANAGER
{
D3DRESOURCESTATS stats[D3DRTYPECOUNT];
} D3DDEVINFO_RESOURCEMANAGER;
D3DRESOURCESTAS不仅包括自从上次调用Present后的统计数据外,还包含自从设备Reset后保存的统计数据。每个资源的统计数据暗示着D3DPOOL_MANAGED池里的资源的performance。 其他池里面的资源不在这个这个统计数组里面。
typedef struct _D3DRESOURCESTATS
{
// Data collected since last Present()
BOOL bThrashing;
DWORD ApproxBytesDownloaded;
DWORD NumEvicts;
DWORD NumVidCreates;
DWORD LastPri;
DWORD NumUsed;
DWORD NumUsedVidMem;
// Persistent data
DWORD Workingset;
DWORD WorkingSetBytes;
DWORD TotalManaged;
DWORD TotalBytes;
} D3DRESOURCESTATS;
Persistent数据设备内存working set和一些汇总数据。Working set成员是video内存资源对象的数目。WorkingSetBytes是working set的大小。TotalManaged 成员是可管理资源对象的数目,TotalBytes是他们总的大小。
bThrashing成员设置是否资源管理器thrashing。当资源管理器不断的把资源流化到video内存,thrashing 就发生了。例如,假定场景里面有很多物体,每个都使用纹理集合中的一个纹理。如果物体按照纹理排序顺序绘制,极有可能是必需的纹理将会装载到设备内存里面。如果物体绘制的顺序不根据纹理顺序,资源管理器将会花很多时间在working set的不同纹理之间来回thrashing。
ApproxBytesDownloaded ,NumEvicts和NumVidCreates成员描述了自从上次调用Present后资源管理器的资源消耗行为。LastPri给出了上一次从设备内存里面回收的资源对象的优先级。NumUsed和NumUsedInVidMem分别表示已经设置到设备的资源对象的数目和已经present到设备内存上资源对象的数目。
- 顶点统计查询
设备也能统计自上次调用Present以来的顶点处理,如三角形的数目以及clipping后增加的三角形数目。
typedef struct _D3DDEVINFO_D3DVERTEXSTATS
{
DWORD NumRenderedTriangles;
DWORD NumberExtraClippingTriangles;
}D3DDEVINFO_D3DVERTEXSTATS;
- 顶点cache查询
顶点cache查询返回设备上顶点cache的大小等信息。顶点cache是一块临近GPU的内存cache,它避免访问一些最近使用的顶点内存。这个查询返回的是D3DDEVINFO_VCACHE结构体。
typdef struct _D3DDEVINFO_VCACHE
{
DWORD Pattern //必须是四字符代码CACH
DWORD OptMethod; //当它的值是0的时候,应用程序必须使用尽可能长的三角形步长来最大化顶点cache。 如果值是1表示应用程序将使用CacheSize大小的顶点Cache来优化。
DWORD CacheSize;
DWORD MagicNumber;// 当OptMethod 是的时候,它决定了重启三角形步长的时间。
} D3DDEVINFO_VCACHE, *LPD3DDEVINFO_VCACHE;
- PIX 相关查询
PIX是DirectX应用程序的性能测量工具。余下的查询类型返回性能测量的数据。一个驱动可能不支持所有查询。如果值是0,它将忽略测量的结果。cache利用查询,返回texel和顶点cache的命中率。
typedef struct _D3DDEVINFO_D3D9CACHEUTILIZATION
{
float TextureCacheHitRate;
float PostTransformVertexCacheHitRate;
}D3DDEVINFO_D3D9CACHEUTILIZATION;
所有的时间查询返回都是流水线各个部分的查询时间消耗情况。这个测量反应查询开始和结束之间的百分比。
typedef struct _D3DDEVINFO_D3D9BANDWIDTHTIMINGS
{
float MaxBandwidthUtilized;
float FrontEndUploadMemoryUtilizedPercent;
float VertexRateUtilizedPercent;
float TriangleSetupRateUtilizedPercent;
float FillRateUtilizedPercent;
}D3DDEVINFO_D3D9BANDWIDTHTIMINGS;
typedef struct _D3DDEVINFO_D3D9INTERFACETIMINGS
{
float WaitingForGPUToUseApplicationResourceTimePercent;
float WaitingForGPUToAcceptMoreCommandsTimePercent;
float WaitingForGPUToStayWithinLatencyTimePercent;
float WaitingForGPUExclusiveResourceTimePercent;
float WaitingForGPUOtherTimePercent;
} D3DDEVINFO_D3D9INTERFACETIMINGS;
typedef struct _D3DDEVINFO_D3D9PIPELINETIMINGS
{
float VertexProcessingTimePercent;
float PixelProcessingTimePercent;
float OtherGPUProcessingTimePercent;
float GPUIdleTimePercent;
} D3DDEVINFO_D3D9PIPELINETIMINGS;
顶点shader timing和pixel shader timing查询都返回了D3DDEVINFO_D3D9STAGETIMINGS结构体里面的信息。
typdef struct _D3DDEVINFO_D3D9STAGETIMINGS
{
float MemoryProcessingPercent;
float ComputationProcessingPercent;
}D3DDEVINFO_D3D9STAGETIMINGS;
设备状态
设备对象的属性控制渲染流水线的行为,它的方法给流水线供应数据。设备的渲染,采样器,纹理阶段的状态属性控制了很多流水线行为,总结如下表:
| Vertex Assembly | RS Patch Edge Style | RS Adaptive Tess X, Y,Z,W |
| RS Position Degree | RS Enable Adaptive Tessellation | |
| RS Normal Degree | RS Max Tessellation Level | |
| RS Color Vertex | RS Min Tessellation Level | |
| RS Point Size | SS D Map offset | |
| Vertex Processing | RS Tween Factor | RS Clipping |
| RS Vertex Blend | RS Indexed Vertex Blend Enable | |
| TSS Tex Coord Index | RS Ambient Material Source | |
| RS FOG Vertex Mode | RS Diffuse Material Source | |
| RS Range Fog Enable | RS Specular Material Source | |
| RS Ambient | RS Emissive Material Source | |
| RS Local Viewer | RS Lighting | |
| RS Specular Enable | RS Normailze Normals | |
| RS Shade Mode | TSS Texture Transform Flags | |
| RS Cull Mode | RS Clip Plane Enable | |
| Pixel Processing | SS Border Color | |
| RS Fill Mode | SS Mag Filter | |
| RS Last Pixel | SS Min Filter | |
| SS Mip Filter | ||
| RS Specular Enable | SS Mip Map LOD Bias | |
| RS Texture Factor | SS Max Mip level | |
| RS Wrap 0——15 | SS Max Anisotrophy | |
| SS Address U, V, W | TSS Color Arg 0-2 | |
| TSS Color Op | TSS Constant | |
| TSS Alpha Arg 0-2 | RS Fog Table Mode | |
| TSS Alpha Op | RS Fog Density | |
| TSS Result Arg | RS Fog End | |
| TSS Bump Env Mat 00-11 | RS Fog Start | |
| TSS Bump Env L Scale | RS Fog Color | |
| TSS Bump Env L Offset | RS Fog Enable | |
| RS Depth Bias | RS Slope Scale Depth Bias | |
| SS sRGB Texture | RS Antialiased Line Enable | |
| Frame Buffer | RS Alpha Ref | RS Alpha Test Enable |
| RS Alpha Func | RS Z Enable | |
| RS Z Func | ||
| RS Stencil Enable | RS Stencil Func | |
| RS Stencil Fail | RS Stencil Z Fail | |
| RS Stencil Pass | RS Alpha Blend Enable | |
| RS Src Blend | RS Dest Blend | |
| RS Blend Op | RS Dither Enable | |
| RS Color Write Enable | RS Stencil Write Mask | |
| RS Z Write Enable | RS Multi Sample Antialias | |
| RS Multi Sample Mask | RS Scissor Test Enable | |
| RS Src Blend Alpha | RS Separate Alpha Blend Enable | |
| RS Dest Blend Alpha | RS Blend Op Alpha | |
| RS Blend Factor | RS CCW Stencil Fail | |
| RS CCW Stencil Z Fail | RS CCW Stencil Pass | |
| RS sRGB Write Enable | RS CCW Stencil Func | |
| SS Element Index | RS Color Write Enable 1-3 | |
| RS Two Sided Stencil Mode |
注意:RS,SS和TSS 分别表示render, sampler和texture stage states。
Render state, sampler state和纹理阶段state都是32位的数量,并且携带一个名字和值。名字取值于D3DRENDERSTATETYPE, D3DSAMPLERSTATETYPE和D3DTEXTURESTAGESTATETYPE 枚举值。这些属性是通过GetRenderState, SetRenderState, GetSamplerState,SetSamplerState,GetTextureStageState,SetTextureStageState方法进行管理。
HRESULT GetRenderState(D3DRENDERSTATETYPE kind, DWORD * value);
HRESULT SetRenderState(D3DRENDERSTATETYPE kind, DWORD * value);
HRESULT GetSamplerState(DWORD stage, D3DSAMPLERSTATETPE kind, DWORD * value);
HRESULT SetSamplerState(DWORD stage, D3DSAMPLERSTATETYPE kind, DWORD * value);
HRESULT GetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE kind, DWORD* value);
HRESUL SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE kind, DWORD value);
typedef enum _D3DRENDERSTATETYPE
{
D3DRS_ALPHABLENDENABLE = 27,
D3DRS_ALPHAFUNC = 25,
D3DRS_ALPHAREF = 24,
D3DRS_ALPHATESTENABLE = 15,
D3DRS_AMBIENT = 139,
D3DRS_AMBIENTMATERIALSOURCE = 147,
D3DRS_BLENDOP = 171,
D3DRS_CLIPPING = 136,
D3DRS_CLIPPLANEENABLE = 152,
D3DRS_COLORWRITEENABLE = 168,
D3DRS_COLORVERTEX = 141,
D3DRS_CULLMODE = 22,
D3DRS_DEBUGMONITORTOKEN= 165,
D3DRS_DESTBLEND=20,
D3DRS_DIFFUSEMATERIALSOURCE = 145,
D3DRS_DITHERENABLE = 26,
D3DRS_EDGEANTIALIAS = 40,
D3DRS_EMISSIVEMATERIALSOURCE = 148,
D3DRS_FILLMODE = 8,
D3DRS_FOGCOLOR = 34,
D3DRS_FOGDENSITY = 38,
D3DRS_FOGENABLE =28,
D3DRS_FOGEND = 37,
D3DRS_FOGSTART = 36,
D3DRS_FOGTABLEMODE = 35,
D3DRS_FOGVERTEXMOE = 140,
D3DRS_INDEXEDVERTEXBLENDENABLE = 167,
D3DRS_LASTPIXEL= 16,
D3DRS_LIGHTING = 137,
D3DRS_LINEPATTERN = 10,
D3DRS_LOCALVIEWER = 142,
D3DRS_NORMALIZENORMALS = 143,
D3DRS_MULTISAMPLEANTIALIAS = 161,
D3DRS_NORMALORDER = 173,
D3DRS_PATCHEDGESTYLE = 163,
D3DRS_PATCHSEGMENTS = 164,
D3DRS_POINTSCALE_A = 158,
D3DRS_POINTSCALE_B = 159,
D3DRS_POINTSCALE_C = 160,
D3DRS_POINTSCALEENABLE= 157,
D3DRS_POINTSIZE = 154,
D3DRS_POINTSIZE_MAX = 166,
D3DRS_POINTSIZE_MIN = 155,
D3DRS_POINTSPRITEENABLE = 156,
D3DRS_POSITIONNOORDER = 172,
D3DRS_RANGEFOGENABLE = 48,
D3DRS_SHADEMODE = 9,
D3DRS_SOFTWAREVERTEXPROCESSING = 153,
D3DRS_SPECULARENABLE = 29,
D3DRS_SPECULARMATERIALSOURCE = 146,
D3DRS_SRCBLEND = 19,
D3DRS_STENCILENABLE = 52,
D3DRS_STENCILFAIL = 53,
D3DRS_STENCILFUNC = 56,
D3DRS_STENCILMASK = 58,
D3DRS_STENCILPASS = 55,
D3DRS_STENCILREF = 57,
D3DRS_STENCILWRITEMASK = 59,
D3DRS_STENCILZFAIL = 54,
D3DRS_TEXTUREFACTOR = 60,
D3DRS_TWEENFACTOR = 170,
D3DRS_VERTEXBLEND = 151,
D3DRS_WRAP0 = 128,
D3DRS_WRAP1 = 129,
D3DRS_WRAP2 = 130,
D3DRS_WRAP3 = 131,
D3DRS_WRAP4 = 132,
D3DRS_WRAP5 = 133,
D3DRS_WRAP6 = 134,
D3DRS_WRAP7 = 136,
D3DRS_ZBIAS = 47,
D3DRS_ZENABLE = 7,
D3DRS_ZFUNC = 23,
D3DRS_ZVISIBLE = 30,
D3DRS_ZWRITEENABLE = 14
} D3DRENDERSTATETYPE;
typedef enum _D3DSAMPLERSTATETYPE
{
D3DSAMP_ADDRESSU = 1,
D3DSAMP_ADDRESSV = 2,
D3DSAMP_ADDRESSW = 3,
D3DSAMP_BORDERCOLOR = 4,
D3DSAMP_DMAPOFFSET = 13,
D3DSAMP_ELEMENTINDEX = 12,
D3DSAMP_MAGFILTER = 5,
D3DSAMP_MAXANISOTROPY = 10,
D3DSAMP_MAXMIPLEVEL = 9,
D3DSAMP_MINFILTER = 6,
D3DSAMP_MIPFILTER = 7,
D3DSAMP_MIPMAPLODBIAS = 8,
D3DSAMP_SRGBTEXTURE = 11
} D3DSAMPLERSTATETYPE;
typedef enum _D3DTEXTURESTAGESTATETYPE
{
D3DTSS_ADDRESSU = 13,
D3DTSS_ADDRESSV = 14,
D3DTSS_ADDRESSW = 25,
D3DTSS_ALPHAARG0 = 27,
D3DTSS_ALPHAARG1 = 5,
D3DTSS_ALPHAARG2 = 6,
D3DTSS_ALPHAOP = 4,
D3DTSS_BORDERCOLOR = 15,
D3DTSS_BUMPENVLOFFSET = 23,
D3DTSS_BMPENVLSCALE = 22,
D3DTSS_BUMPENVMAT00 = 7,
D3DTSS_BUMPENVMAT01 = 8,
D3DTSS_BUMPENVMAT10 = 9,
D3DTSS_BUMPENVMAT11 = 10,
D3DTSS_COLORARG0 = 26,
D3DTSS_COLORARG1 = 2,
D3DTSS_COLORARG2 = 3,
D3DTSS_COLOROP = 1,
D3DTSS_MAGFILTER = 16,
D3DTSS_MAXANISOTROPY = 21,
D3DTSS_MAXMIPLEVEL = 20,
D3DTSS_MINFILTER = 17,
D3DTSS_MIPFILTER = 18,
D3DTSS_MIPMAPLODBIAS = 19,
D3DTSS_RESULTARG = 28,
D3DTSS_TEXCOORDINDEX = 11,
D3DTSS_TEXTURETRANSFORMFLAGS = 24
}
所有的状态值都是32位,API把他们当做成DWORD。但是每个渲染,采样和纹理阶段状态值将绑定到一个32为的数据类型,但是它不一定是DWORD。这些类型的大部分都是枚举值,但是有些状态值是浮点数。因为Direct3D使用了IEEE的单精度浮点值,它也是32位的。但是当调用属性方法的时候,他必须转换到DWORD。一般使用reinterpret_cast方法来进行转换。
| Device State | Data Type |
| RS Adaptive Tess X, Y,Z, W | float |
| RS Alpha Blend Enable | BOOL |
| RS ALPHA Func | D3DCMPFUNC |
| RS ALPHA Ref | [0,255] |
| RS ALPHA Test Enable | BOOL |
| RS Ambient | D3DCOLOR |
| RS Ambient Material Source | D3DMATERIALCOLORSOURCE |
| RS Antialiased Line Enable | BOOL |
| RS Blend Factor | float |
| RS Blend Op | D3DBLENDOP |
| RS Blend Op Alpha | D3DBLENDOP |
| RS CCW Stencil Fail | D3DSTENCILOP |
| RS CCW Stencil Func | D3DCMPFUNC |
| RS CCW Stencil Pass | D3DSTENCILOP |
| RS CCW Stencil Z Fail | D3DSTENCILOP |
| RS Clipping | BOOL |
| RS Clip Plane Enable | BOOL |
| RS Color Write Enable | D3DCOLORWRITEENABLE |
| RS Color Write Enable 1-3 | D3DCOLORWRITEENABLE |
| RS Color Vertex | BOOL |
| RS Cull Mode | D3DCULL |
| RS Debug Monitor Token | D3DDEBUGMONITORTOKENS |
| RS Depth Bias | float |
| RS Dest Blend | D3DBLEND |
| RS Dest Blend Alpha | D3DBlEND |
| RS Diffuse material Source | D3DMATERIALCOLORSOURCE |
| RS Dither Enable | BOOL |
| RS Emissive Material Source | D3DMATERIALCOLORSOURCE |
| RS Enable Adpative Tessellation | BOOL |
| RS Fill Mode | D3DFILLMODE |
| RS Fog Color | D3DCOLOR |
| RS Fog Density | float |
| RS Fog Enable | BOOL |
| RS FOG END | float |
| RS FOG Start | float |
| RS FOG Table Mode | D3DFOGMODE |
| RS FOG Vertex Mode | D3DFOGMODE |
| RS Indexed Vertex Blend Enable | BOOL |
| RS Last Pixel | BOOL |
| RS Lighting | BOOL |
| RS Local Viewer | BOOL |
| RS Max Tessellation Level | DWORD |
| RS Min Tessellation Level | DWORD |
| RS Normal Degree | D3DDEGREETYPE |
| RS Normalize Normals | BOOL |
| RS Multi Sample Antialias | BOOL |
| RS Multi Sample Mask | [0,2~m-1] |
| RS Patch Edge Style | D3DPATCHEDGESTYLE |
| RS Point Scale A, B, C | float |
| RS Point Scale Enable | BOOL |
| RS Point Size | float |
| RS Point Size Max | float |
| RS Point Size min | float |
| RS Point Sprite Enable | BOOL |
| RS Position Degree | D3DDEGREETYPE |
| RS Range Fog Enable | BOOL |
| RS Scissor Test Enable | BOOL |
| RS Separate Alpha Blend Enable | BOOL |
| RS Shade Mode | D3DSHADEMODE |
| RS Slope Scale Depth Bias | float |
| RS Specular Enable | BOOL |
| RS Specular Material Source | D3DMATERIALCOLORSOURCE |
| RS Src Blend | D3DBLEND |
| RS Src Blend Alpha | D3DBLEND |
| RS sRGB Write Enable | BOOL |
| RS Stencil Enable | BOOL |
| RS Stencil Fail | D3DSTENCILOP |
| RS Stencil Mask | [0,2~s -1] |
| RS Stencil Pass | D3DSTENCILOP |
| RS Stencil Ref | [0,2~s -1] |
| RS Stencil Write Mask | [0,2~s-1] |
| RS Stencil Z Fail | D3DSTENCILOP |
| RS Texture Factor | D3DCOLOR |
| RS Tween Factor | float |
| RS Two Sided Stencil Mode | BOOL |
| RS Vertex Blend | D3DVERTEXBLENDFLAGS |
| RS Wrap 0 - 15 | DEDWRAPCOORD |
| RS Z Enable | D3DZBUFFERTYPE |
| RS Z Func | D3DCMPFUNC |
| RS Z Write Enable | BOOL |
| SS Address U, V,W | D3DTEXTUREADDRESS |
| SS Border Color | D3DCOLOR |
| SS D Map Offset | DWORD |
| SS ELEMENT Index | DWORD |
| SS Mag Filter | D3DTEXTUREFILTERTYPE |
| SS Max Anisotropy | DWORD |
| SS Max Mip Level | [0,l-1] |
| SS Min Filter | D3DTEXTUREFILTERTYPE |
| SS Mip Filter | D3DTEXTUREFILTERTYPE |
| SS Mip Map LOD Bias | float |
| SS sRGB Texture | BOOL |
| TSS Alpha Arg 0 -2 | D3DTEXTUREARG |
| TSS ALPHA Op | D3DTEXTUREOP |
| TSS BUMP Env L Offset | float |
| TSS Bump Env L Scale | float |
| TSS Color Arg 0-2 | D3DTEXTUREARG |
| TSS Color OP | D3DTEXTUREOP |
| TSS Constant | float |
| TSS Result Arg | D3DTextureARG |
| TSS Tex Coord Index | [0,15] D3DTSS_TCI |
| TSS Texture Transform Flags | D3DTEXTURETRANSFORMFLAGS |
- Miscellaneous Render state
RS Debug Monitor Token 并不能控制渲染,也并不与流水线的某个具体环节有任何关系。RS Debug Monitor Token 控制了当使用debug runtime时从流水线返回的调试信息。Debug token是通过D3DDEBUGMONITORTOKENS枚举定义的。
typedef enum _D3DDEBUGMONITORTOKENS
{
D3DDMT_ENABLE = 0,
D3DDMT_DISABLE = 1
} D3DDEBUGMONITORTOKENS;
- Setting group of State
通常在Direct3D应用程序里面,有一组渲染和纹理阶段状态需要设置。如果写每个SetRenderState和SetTextureStageState太冗长。 我们能使用group of state减少单个的调用。
Device State Blocks
状态block都是com对象, 为你的应用程序提供了一种方法cache一组设备属性为以后使用。比如, 两个状态block可以分用于编辑器里未选中和已选中物体的外表。IDirect3DStateBlock9用于管理状态block。每个状态block都关联一个设备。一旦一个状态block被创建,Apply方法将设备属性应用到设备。Capture捕捉state block的当前值。
interface IDirect3DStateBlock9: IUnknown
{
HRESULT GetDevice(IDirect3DDevice ** value);
HRESULT Apply();
HRESULT Capture();
}
有两种方法创建一个状态block, 然后使用特定的属性值来填充它。创建State block的第一种方法是调用CreateStateBlock。
HRESULT CreateStateBlock(D3DSTATEBLOCKTYPE kind, IDirect3DStateBlock9 ** result);
typedef enum _D3DSTATEBLOCKTYPE
{
D3DSBT_ALL = 1,
D3DSBT_PIXELSTATE = 2,
D3DSBT_VERTEXSTATE = 3
}D3DSTATEBLOCKTYPE;
获取一个状态block对象的第二种方法是调用BeginStateBlock,设置设备属性,然后调用EndStateBlock。一旦BeginStateBlock调用以后,下面的方法可以被它捕捉他们的状态。
LightEnable, SetSamplerState, SetClipPlane, SetStreamSource, SetIndices, SetTexture, SetLight, SetTextureStageState, SetMaterial, SetTransform, SetPixelShader, SetVertexShader, SetPixelShaderConstantB, SetVertexShaderConstantB, SetPixelShaderConstantF, SetVertexShaderConstantF, SetPixelShaderConstantI, SetVertexShaderConstantI, SetRenderState, SetViewPort。
当设备重新获得的时候,状态block需要销毁然后重建。
Pure Devices
当创建设备的时候,我们可以请求一个pure的硬件设备,它执行更少的设备状态管理。Pure设备将会有更好的性能优势,因为runtime和driver并不比保存一份non-queryable状态。如果D3DCAPS::DevCaps的D3DDEVCAPS_PUREDEVICE设置,它将支持一个pure设备。
更多推荐




所有评论(0)