这一章讲述了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
GetBackBufferback buffer image surface 接口
GetCreationParameters设备的创建参数
GetDirect3Dcreating IDirect3D9接口
GetDisplayMode视频显示模式
GetFrontBufferDatafront buffer 的拷贝
GetNumberOfSwapChainsNumber of swap chain
GetRasterStatusRaster display status
GetRenderTargetDatasurface data in a render target
GetSwapChainswap chain on the device
  
Write-only Properties 
SetCursorPositioncursor position
SetCursorPropertiescursor image and hot spot
SetDialogBoxModeGDI compatability flag
  
Properties 
GetClipPlaneuser-defined clipping plane
SetClipPlane 
GetClipStatusclip status of rendered primitives
SetClipStatus 
GetCurrentTexturePalettecurrent texture palette index
SetCurrentTexturePalette 
GetDepthStencilSurfaceDepth surface 接口
SetDepthStencilSurface 
GetFVFFlexible vertex format
SetFVF 
GetGammaRampGamma correction lookup table
SetGammaRamp 
GetIndicescurrent index buffer
SetIndices 
GetLightlight definition
SetLight 
GetLightEnablelight enabled flag
LightEnable 
GetMaterialPrimitive material properties
SetMaterial  
GetNPatchModeN-Patch tessellation mode
SetNPatchMode 
GetPaletteEntriesTexture palette entries
SetPaletteEntries 
GetPixelShaderpixel shader program handle
SetPixelShader 
GetPixelShaderConstantBBoolean constant registers
SetPixelShaderConstantB 
GetPixelShaderConstantFfloating-pont constant registers
SetPixelShaderConstantF 
GetPixelShaderConstantIinteger constant registers
SetPixelShaderConstantI 
GerRenderStatePipeline control values
SetRenderState 
GetRenderTargetTarget surface for rendered pixels
SetRenderTarget 
GetSamplerStateTexture sampler control values
SetSamplerState 
GetScissorRectScissor test rectangle
SetScissorRect 
GetSoftwareVertexProcessingVertex processing control
SetSoftwareVertexProcessing 
GetStreamSourcesource vertex data buffers
SetStreamSource 
GetStreamSourceFreqstream source sampling frequency
SetStreamSourceFreq 
GetTextureTexture resources used by each stage
SetTexture 
GetTextureStageStateTexture stage control values
SetTextureStageState 
GetTransformtranformation matrix
SetTransform 
GetVertexDeclarationVertex component declaration
SetVertexDeclaration 
GetVertexShadervertex shader program handle
SetVertexShader 
GetVertexShaderConstantBboolean constant registers
SetVertexShaderConstantB 
GetVertexShaderConstantFfloating-point constant registers
SetVertexShaderConstantF 
GetVertexShaderConstantIInteger constant registers
SetVertexShaderConstantI 
GetViewportRendering viewport extent
SetViewport 
Methods 
BeginSceneMake the start of a scene
BeginStateBlockMake device state for capture
ClearClear the current viewport on the device
ColorFillFills a rectangular area with a color
CreateAdditionalSwapChainCreate an additional swap chain on a windowed device
CreateCubeTextureCreate a cube map texture sources
CreateDepthStencilSurfaceCreate a depth/stencil surface resource
CreateIndexBufferCreate an index buffer resource
CreateOffScreenPlainSurfaceCreate an off-screen surface resource
CreatePixelShaderCreate a pixel shader
CreateQueryCreate a device query
CreateRenderTargetCreate a render target resource
CreateStateBlockCreate a state block
CreateTextureCreate a texture resource
CreateVertexBufferCreate a vertex buffer resource
CreateVertexDeclarationCreate a vertex declaration
CreateVertexShaderCreate a vertex shader
CreateVolumeTextureCreate a volume texture resource
DeletePatchDestroy a cached patch tesselation
DrawIndexedPrimitiveDraw indexed primitives from the current streams.
DrawIndexedPrimitiveUPDraw indexed primitives from user data.
DrawPrimitiveUPDraw primitives from usr data.
DrawRectDataRectangular higher order surface patch
DrawTriPatchTriangular higher order surface patch.
EndSceneMake the end of a scene
EndStateBlockCapture device state changes
EvictManagedResourcesFlush managed resources from the device
MultiplyTransformPost-multiply onto a device transformation matrix
PresentPresents a rendered image for display
ProcessVerticesProcess vertices in software
ResetResets the device's display characteristcs
ShowCursorDisplays or hides the cursor
StretchRectDevice memory blit with stretching
TestCooperativeLevelChecks exclusive ownership of the device
UpdateSurfacesystem to device memory surfaces
UpdateTexturesystem to device memory texture transfer.
ValidateDeviceValidate 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 ProcessingCreateVertexDeclaration
 CreateVertexShader
 LightEnable
 MultiplyTransform
 ProcessVertices
 SetClipPlane
 SetClipStatus
 SetFVF
 SetLight
 SetMaterial
 SetRenderState
 SetSoftwareVertexProcessing
 SetTransform
 SetVertexShader
 SetVertexShaderConstantB
 SetVertexShaderConstantF
 SetVertexShaderConstantI
 SetViewPort
Primitive RasterizationSetRenderState
Pixel  ProcessingCreatePixelShader
 MultiplyTransform
 SetCurrentTexturePalette
 SetPaletteEntries
 SetPixelShader
 SetPixelShaderConstantB
 SetPixelShaderConstantF
 SetPixelShaderConstantI
 SetRenderState
 SetSamplerState
 SetTexture
 SetTextureStageState
 SetTransform
Frame BufferClear
 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 outGetDisplayMode
 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 AssemblyDev 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 ProcessingDev 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 ProcessingMax User Clip Planes
 Primitive Misc Caps:
       Clip Plane Scaled Points
       Clip TL Verts
       Cull CW
       Cull CCW
       Cull None
Primitive  RasterizationDev 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 ProcessingCaps 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 BufferAlpha 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 outAdapter 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 TypeBeginning
Vertex CacheCreateDevice
Resource Manager StatisticsPresent
Vertex StatisticsPresent
Event CreateDevice

     为了取得查询的结果,GetDataSize和GetData方法可以获得查询信息的拷贝。每种查询结果都对应一个数据结构。如下表,

Query TypeData Type
Bandwidth TimingsD3DDEVINFO_D3D9BANDWIDTHTIMINGS
Cache UtilizationD3DDEVINFO_D3D9CACHEUTILIZATION
EventBOOL
Interface TimingsD3DDEVINFO_D3D9INTERFACETIMINGS
OcclusionDWORD
Pipeline TimingsD3DDEVINFO_D3D9INTERFACETIMINGS
Pixel Shader TimingsD3DDEVINFO_D3D9STAGETIMINGS
Resource Manager StatisticsD3DDEVINFO_RESOURCEMANAGER
TimestampUINT64
Timestamp DisjointBOOL
Timestamp FrequencyUINT64
Vertex CacheD3DDEVINFO_CACHE
Vertex Shader TimingsD3DDEVINFO_D3D9STAGETIMING
Vertex StatisticsD3DDEVINFO_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 AssemblyRS Patch Edge StyleRS Adaptive Tess X, Y,Z,W
 RS Position DegreeRS Enable Adaptive Tessellation
 RS Normal DegreeRS Max Tessellation Level
 RS Color VertexRS Min  Tessellation Level
 RS Point SizeSS D Map offset
Vertex ProcessingRS Tween FactorRS Clipping
 RS Vertex BlendRS Indexed Vertex Blend Enable
 TSS Tex Coord IndexRS Ambient Material Source
 RS FOG Vertex ModeRS Diffuse Material Source
 RS Range Fog EnableRS Specular Material Source
 RS AmbientRS Emissive Material Source
 RS Local ViewerRS Lighting
 RS Specular EnableRS Normailze Normals
 RS Shade ModeTSS Texture Transform Flags
 RS Cull Mode RS Clip Plane Enable
Pixel Processing SS Border Color
 RS Fill ModeSS Mag Filter
 RS Last PixelSS Min Filter
  SS Mip Filter
 RS Specular EnableSS Mip Map LOD Bias
 RS Texture FactorSS Max Mip level
 RS Wrap 0——15SS Max Anisotrophy
 SS Address U, V, WTSS Color Arg 0-2
 TSS Color OpTSS Constant
 TSS Alpha Arg 0-2RS Fog Table Mode
 TSS Alpha OpRS Fog Density
 TSS Result ArgRS Fog End
 TSS Bump Env Mat 00-11RS Fog Start
 TSS Bump Env L ScaleRS Fog Color
 TSS Bump Env L OffsetRS Fog Enable
 RS Depth BiasRS Slope Scale Depth Bias
 SS sRGB TextureRS Antialiased Line Enable
Frame BufferRS Alpha RefRS Alpha Test Enable
 RS Alpha FuncRS Z Enable
 RS Z  Func 
 RS Stencil EnableRS Stencil Func
 RS Stencil FailRS Stencil Z Fail
 RS Stencil PassRS Alpha Blend Enable
 RS Src BlendRS Dest Blend
 RS Blend OpRS Dither Enable
 RS Color Write EnableRS Stencil Write Mask
 RS Z Write EnableRS Multi Sample Antialias
 RS Multi Sample MaskRS Scissor Test Enable
 RS Src Blend AlphaRS Separate Alpha Blend Enable
 RS Dest Blend AlphaRS Blend Op Alpha
 RS Blend FactorRS CCW Stencil Fail
 RS CCW Stencil Z FailRS CCW Stencil Pass
 RS sRGB Write EnableRS CCW Stencil Func
 SS Element IndexRS 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 StateData Type
RS Adaptive Tess X, Y,Z, Wfloat
RS Alpha Blend EnableBOOL
RS ALPHA FuncD3DCMPFUNC
RS ALPHA Ref[0,255]
RS ALPHA Test EnableBOOL
RS AmbientD3DCOLOR
RS Ambient Material SourceD3DMATERIALCOLORSOURCE
RS Antialiased Line EnableBOOL
RS Blend Factorfloat
RS Blend OpD3DBLENDOP
RS Blend Op AlphaD3DBLENDOP
RS CCW Stencil FailD3DSTENCILOP
RS CCW Stencil Func D3DCMPFUNC
RS CCW Stencil PassD3DSTENCILOP
RS CCW Stencil Z Fail D3DSTENCILOP
RS ClippingBOOL
RS Clip Plane EnableBOOL
RS Color Write EnableD3DCOLORWRITEENABLE
RS Color Write Enable 1-3D3DCOLORWRITEENABLE
RS Color VertexBOOL
RS Cull ModeD3DCULL
RS Debug  Monitor TokenD3DDEBUGMONITORTOKENS
RS Depth Biasfloat
RS Dest BlendD3DBLEND
RS Dest Blend Alpha D3DBlEND
RS Diffuse material SourceD3DMATERIALCOLORSOURCE
RS Dither EnableBOOL
RS Emissive Material SourceD3DMATERIALCOLORSOURCE
RS Enable Adpative TessellationBOOL
RS Fill ModeD3DFILLMODE
RS Fog ColorD3DCOLOR
RS Fog Densityfloat
RS Fog EnableBOOL
RS FOG ENDfloat
RS FOG Startfloat
RS FOG Table ModeD3DFOGMODE
RS FOG Vertex Mode D3DFOGMODE
RS Indexed Vertex Blend EnableBOOL
RS Last PixelBOOL
RS LightingBOOL
RS Local ViewerBOOL
RS Max Tessellation LevelDWORD
RS Min Tessellation LevelDWORD
RS Normal DegreeD3DDEGREETYPE
RS Normalize NormalsBOOL
RS Multi Sample AntialiasBOOL
RS Multi Sample Mask[0,2~m-1]
RS Patch Edge StyleD3DPATCHEDGESTYLE
RS Point Scale A, B, Cfloat
RS Point Scale Enable BOOL
RS Point Sizefloat
RS Point Size Maxfloat
RS Point Size minfloat
RS Point Sprite EnableBOOL
RS Position DegreeD3DDEGREETYPE
RS Range Fog Enable BOOL
RS Scissor Test EnableBOOL
RS Separate Alpha Blend Enable BOOL
RS Shade Mode  D3DSHADEMODE
RS Slope Scale Depth  Biasfloat
RS Specular EnableBOOL
RS Specular Material Source D3DMATERIALCOLORSOURCE
RS Src BlendD3DBLEND
RS Src Blend AlphaD3DBLEND
RS sRGB Write EnableBOOL
RS Stencil EnableBOOL
RS Stencil FailD3DSTENCILOP
RS  Stencil Mask[0,2~s -1]
RS Stencil PassD3DSTENCILOP
RS Stencil Ref[0,2~s -1]
RS Stencil Write Mask[0,2~s-1]
RS Stencil Z FailD3DSTENCILOP
RS Texture FactorD3DCOLOR
RS Tween Factorfloat
RS Two Sided Stencil ModeBOOL
RS Vertex BlendD3DVERTEXBLENDFLAGS
RS Wrap 0 - 15DEDWRAPCOORD
RS Z EnableD3DZBUFFERTYPE
RS Z FuncD3DCMPFUNC
RS Z Write EnableBOOL
SS Address U, V,WD3DTEXTUREADDRESS
SS Border ColorD3DCOLOR
SS D Map OffsetDWORD
SS ELEMENT IndexDWORD
SS Mag FilterD3DTEXTUREFILTERTYPE
SS Max AnisotropyDWORD
SS Max Mip Level[0,l-1]
SS Min FilterD3DTEXTUREFILTERTYPE
SS Mip FilterD3DTEXTUREFILTERTYPE
SS Mip Map LOD Biasfloat
SS sRGB TextureBOOL
TSS Alpha Arg 0 -2D3DTEXTUREARG
TSS ALPHA OpD3DTEXTUREOP
TSS BUMP Env L Offsetfloat
TSS Bump Env L Scalefloat
TSS Color Arg 0-2D3DTEXTUREARG
TSS Color OP D3DTEXTUREOP
TSS Constantfloat
TSS Result Arg D3DTextureARG
TSS Tex Coord Index[0,15] D3DTSS_TCI
TSS Texture Transform FlagsD3DTEXTURETRANSFORMFLAGS

 

  • 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设备。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐