要渲染看起來真實(shí)的場景,最好是使用高分辨率而且顏色豐富的紋理,但這樣的紋理可能會(huì)耗費(fèi)大量的內(nèi)存,例如,一張每像素16位顏色的256
x 256紋理將使用128KB的內(nèi)存。如果在該紋理中使用多級(jí)漸進(jìn)紋理,還需要額外的43KB內(nèi)存。一個(gè)使用50張這種紋理的場景將需要8MB的內(nèi)存,如果需要更強(qiáng)的真實(shí)性,可以使用每像素32位顏色的512
x 512紋理,但那就需要8倍的內(nèi)存。
為了減少紋理消耗的系統(tǒng)帶寬和內(nèi)存空間,Direct3D支持紋理壓縮和實(shí)時(shí)解壓,即DXT紋理壓縮。壓縮后的紋理被存儲(chǔ)在Direct3D紋理指針中,當(dāng)Direct3D渲染物體時(shí),Direct3D引擎自動(dòng)對(duì)紋理進(jìn)行解壓。應(yīng)用DXT壓縮紋理不僅可以節(jié)省內(nèi)存空間,而且能有效地降低紋理傳輸帶寬,提高圖形系統(tǒng)的整體性能。
隨著DirectX對(duì)紋理壓縮格式的推廣,目前大部分顯卡都支持DXT壓縮紋理,而且DXT壓縮紋理在圖形質(zhì)量和運(yùn)行速度之間取得了很好的平衡。
DXT紋理壓縮格式
DXT是一種DirectDraw表面,它以壓縮形式存儲(chǔ)圖形數(shù)據(jù),該表面可以節(jié)省大量的系統(tǒng)帶寬和內(nèi)存。即使不直接使用DXT表面渲染,也可以通過DXT格式創(chuàng)建紋理的方法節(jié)省磁盤空間。Direct3D提供了D3DFMT_DXT1
~ D3DFMT_DXT5共5種壓縮紋理格式。其中,D3DFMT_DXT1支持15位RGB和1位alpha圖形格式,D3DFMT_DXT2、D3DFMT_DXT3支持12位RGB和4位alpha,D3DFMT_DXT4、D3DFMT_DXT5則采取了線性插值方式生成alpha。
DXT1格式的壓縮比例是4 : 1(4x4塊16位RGB紋理元素可壓縮為64位,2個(gè)16位RGB565值和16個(gè)2位索引),這樣的壓縮比并不很高,但足以有效地將3D加速卡用于存儲(chǔ)紋理的容量提高4倍,如下圖所示:

Direct3D支持5中DXT格式壓縮:
FOURCC |
Description |
Alpha
premultiplied? |
DXT1 |
Opaque/1-bit alpha |
N/A |
DXT2 |
Explicit alpha |
Yes |
DXT3 |
Explicit alpha |
No |
DXT4 |
Interpolated alpha |
Yes |
DXT5 |
Interpolated alpha |
No |
使用DXT壓縮紋理
使用DXT壓縮紋理前,必須先調(diào)用IDirect3D9::CheckDeviceFormat(),將表面格式設(shè)置為D3DFMT_DXT1
~ D3DFMT_DXT5中的任意值,查詢當(dāng)前設(shè)備是否支持DXT壓縮紋理:
// check whether device support DXT compressed texture
if(FAILED(pD3D->CheckDeviceFormat(pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, 0, D3DRTYPE_TEXTURE,
D3DFMT_DXT1)))
return false;
如果當(dāng)前設(shè)備支持DXT壓縮紋理,就可以直接創(chuàng)建DXT壓縮紋理,并可以通過IDirect3DDevice9::SetTexture()函數(shù)設(shè)置DXT壓縮紋理,將其映射到物體表面。如果當(dāng)前設(shè)備不支持DXT壓縮紋理,仍然可以DXT壓縮格式存儲(chǔ)紋理,但在使用該紋理渲染圖形前,必須將DXT壓縮格式的紋理轉(zhuǎn)換為當(dāng)前設(shè)備支持的格式。
在使用函數(shù)IDirect3DDevice9::CreateTexture()創(chuàng)建紋理時(shí),將參數(shù)Format設(shè)置為壓縮紋理格式D3DFMT_DXT1
~ D3DFMT_DXT5中的任意值,可以創(chuàng)建指定壓縮格式的紋理,也可以通過函數(shù)D3DXCreateTextureFromFileEx()從磁盤文件中生成DXT壓縮紋理:
V_RETURN(D3DXCreateTextureFromFileExW(pd3dDevice, L"texture.jpg", 0, 0, 5, 0,
D3DFMT_DXT1, D3DPOOL_MANAGED,
D3DX_DEFAULT, D3DX_DEFAULT, 0xFF000000, NULL,
NULL, &g_texture));
可以通過DirectX SDK提供的紋理編輯工具"DirectX
Texture Editor",創(chuàng)建壓縮格式的紋理文件(后綴為DDS的紋理文件),或?qū)⑵渌袷降募y理文件轉(zhuǎn)換為壓縮格式的紋理文件。
在創(chuàng)建好壓縮紋理之后,就可以和前面一樣設(shè)置紋理及各項(xiàng)紋理渲染狀態(tài),最后進(jìn)行渲染。如果當(dāng)前設(shè)備支持DXT壓縮紋理,那么在使用DXT紋理渲染圖形時(shí),Direct3D引擎會(huì)自動(dòng)進(jìn)行紋理壓縮,如果當(dāng)前設(shè)備不支持DXT壓縮紋理,這時(shí)使用DXT壓縮紋理和使用普通的非壓縮紋理完全相同。
當(dāng)使用大量紋理貼圖渲染復(fù)雜場景時(shí),通過使用DXT壓縮紋理可以有效提高程序運(yùn)行性能。
紋理管理
如果顯卡中只有64MB的視頻內(nèi)存,就應(yīng)該考慮將哪些紋理加載和保留在內(nèi)存中。可以編寫一個(gè)算法讓計(jì)算機(jī)來自動(dòng)考慮這些問題,那么這個(gè)算法的任務(wù)是什么呢?它需要跟蹤可用的紋理內(nèi)存總數(shù),并了解哪些紋理是經(jīng)常用到的,而哪些紋理是很少用到的。至少,這個(gè)紋理管理算法必須能決定哪些現(xiàn)有的紋理資源可以通過一張紋理圖來重新加載,以及應(yīng)該銷毀哪些表面,然后由新的紋理資源取而代之。
Direct3D有一個(gè)自動(dòng)的紋理管理系統(tǒng),當(dāng)通過CreateTexture()創(chuàng)建一個(gè)紋理對(duì)象時(shí),將Pool參數(shù)指定為D3DPOOL_MANAGED,就是向Direct3D請求該系統(tǒng)的支持。紋理管理器通過一個(gè)時(shí)間戳來跟蹤紋理的使用情況,這個(gè)時(shí)間戳用于記錄每個(gè)紋理對(duì)象最后被使用的時(shí)間。紋理管理器通過"最近最少使用"算法來決定哪些紋理應(yīng)從視頻內(nèi)存中移除,若有兩個(gè)紋理對(duì)象同時(shí)滿足移除條件,則根據(jù)紋理屬性來做進(jìn)一步的判斷。如果它們具有相同的優(yōu)先級(jí),則移除最近最少使用的紋理,如果它們具有相同的時(shí)間戳,則移除低優(yōu)先級(jí)的紋理。
通過為紋理表面調(diào)用IDirect3DResource9::SetPriority()函數(shù),可以給托管的紋理賦予一個(gè)優(yōu)先級(jí),該函數(shù)聲明如下:
Assigns the resource-management priority for this
resource.
DWORD SetPriority(
DWORD PriorityNew
);
Parameters
- PriorityNew
- [in] DWORD value that specifies the new
resource-management priority for the resource.
Return Values
Returns the previous priority value for the resource.
Remarks
SetPriority is used for priority control of managed
resources. This method returns 0 on non-managed resources.
Priorities are used to determine when managed resources
are to be removed from memory. A resource assigned a low priority is removed
before a resource with a high priority. If two resources have the same priority,
the resource that was used more recently is kept in memory; the other resource
is removed. Managed resources have a default priority of 0.
原圖:

DXT壓縮后:

主程序:
#include "dxstdafx.h"
#include "resource.h"
#pragma warning(disable : 4127)
#define IDC_TOGGLE_FULLSCREEN 1
#define IDC_TOGGLE_REF 2
#define IDC_CHANGE_DEVICE 3
#define release_com(p) do { if(p) { (p)->Release(); (p) = NULL; } } while(0)
struct sCustomVertex
{
float x, y, z;
float u, v;
};
#define D3DFVF_CUSTOM_VERTEX (D3DFVF_XYZ | D3DFVF_TEX1)
ID3DXFont* g_font;
ID3DXSprite* g_text_sprite;
bool g_show_help;
CDXUTDialogResourceManager g_dlg_resource_manager;
CD3DSettingsDlg g_settings_dlg;
CDXUTDialog g_button_dlg;
IDirect3DVertexBuffer9* g_vertex_buffer;
IDirect3DTexture9* g_texture;
//--------------------------------------------------------------------------------------
// Rejects any devices that aren't acceptable by returning false
//--------------------------------------------------------------------------------------
bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat,
D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
{
// Typically want to skip backbuffer formats that don't support alpha blending
IDirect3D9* pD3D = DXUTGetD3DObject();
if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat,
D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
return false;
// check whether device support DXT compressed texture
if(FAILED(pD3D->CheckDeviceFormat(pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, 0, D3DRTYPE_TEXTURE,
D3DFMT_DXT1)))
return false;
return true;
}
//--------------------------------------------------------------------------------------
// Before a device is created, modify the device settings as needed.
//--------------------------------------------------------------------------------------
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
{
// If video card does not support hardware vertex processing, then uses sofaware vertex processing.
if((pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0)
pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
static bool is_first_time = true;
if(is_first_time)
{
is_first_time = false;
// if using reference device, then pop a warning message box.
if(pDeviceSettings->DeviceType == D3DDEVTYPE_REF)
DXUTDisplaySwitchingToREFWarning();
}
return true;
}
//--------------------------------------------------------------------------------------
// Create any D3DPOOL_MANAGED resources here
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice,
const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
void* pUserContext )
{
HRESULT hr;
V_RETURN(g_dlg_resource_manager.OnCreateDevice(pd3dDevice));
V_RETURN(g_settings_dlg.OnCreateDevice(pd3dDevice));
D3DXCreateFont(pd3dDevice, 18, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_font);
V_RETURN(D3DXCreateTextureFromFileExW(pd3dDevice, L"texture.jpg", 0, 0, 5, 0, D3DFMT_DXT1, D3DPOOL_MANAGED,
D3DX_DEFAULT, D3DX_DEFAULT, 0xFF000000, NULL, NULL, &g_texture));
// create vertex buffer and fill data
sCustomVertex vertices[] =
{
{ -3, -3, 0.0f, 0.0f, 1.0f },
{ -3, 3, 0.0f, 0.0f, 0.0f },
{ 3, -3, 0.0f, 1.0f, 1.0f },
{ 3, 3, 0.0f, 1.0f, 0.0f }
};
pd3dDevice->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_MANAGED, &g_vertex_buffer, NULL);
void* ptr;
g_vertex_buffer->Lock(0, sizeof(vertices), (void**)&ptr, 0);
memcpy(ptr, vertices, sizeof(vertices));
g_vertex_buffer->Unlock();
return S_OK;
}
//--------------------------------------------------------------------------------------
// Create any D3DPOOL_DEFAULT resources here
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice,
const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
void* pUserContext )
{
HRESULT hr;
V_RETURN(g_dlg_resource_manager.OnResetDevice());
V_RETURN(g_settings_dlg.OnResetDevice());
V_RETURN(g_font->OnResetDevice());
V_RETURN(D3DXCreateSprite(pd3dDevice, &g_text_sprite));
// set dialog position and size
g_button_dlg.SetLocation(pBackBufferSurfaceDesc->Width - 170, 0);
g_button_dlg.SetSize(170, 170);
// setup view matrix
D3DXMATRIX mat_view;
D3DXVECTOR3 eye(0.0f, 0.0f, -8.0f);
D3DXVECTOR3 at(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&mat_view, &eye, &at, &up);
pd3dDevice->SetTransform(D3DTS_VIEW, &mat_view);
// set projection matrix
D3DXMATRIX mat_proj;
float aspect = (float)pBackBufferSurfaceDesc->Width / pBackBufferSurfaceDesc->Height;
D3DXMatrixPerspectiveFovLH(&mat_proj, D3DX_PI/4, aspect, 1.0f, 100.0f);
pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat_proj);
// set texture and texture stage state and sample state
pd3dDevice->SetTexture(0, g_texture);
pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
pd3dDevice->SetStreamSource(0, g_vertex_buffer, 0, sizeof(sCustomVertex));
pd3dDevice->SetFVF(D3DFVF_CUSTOM_VERTEX);
return S_OK;
}
//--------------------------------------------------------------------------------------
// Release resources created in the OnResetDevice callback here
//--------------------------------------------------------------------------------------
void CALLBACK OnLostDevice( void* pUserContext )
{
g_dlg_resource_manager.OnLostDevice();
g_settings_dlg.OnLostDevice();
g_font->OnLostDevice();
release_com(g_text_sprite);
}
//--------------------------------------------------------------------------------------
// Release resources created in the OnCreateDevice callback here
//--------------------------------------------------------------------------------------
void CALLBACK OnDestroyDevice( void* pUserContext )
{
g_dlg_resource_manager.OnDestroyDevice();
g_settings_dlg.OnDestroyDevice();
release_com(g_font);
release_com(g_vertex_buffer);
release_com(g_texture);
}
//--------------------------------------------------------------------------------------
// Handle updates to the scene
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
}
//--------------------------------------------------------------------------------------
// Render the helper information
//--------------------------------------------------------------------------------------
void RenderText()
{
CDXUTTextHelper text_helper(g_font, g_text_sprite, 20);
text_helper.Begin();
// show frame and device states
text_helper.SetInsertionPos(5, 5);
text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 0.475f, 0.0f, 1.0f) );
text_helper.DrawTextLine( DXUTGetFrameStats(true) );
text_helper.DrawTextLine( DXUTGetDeviceStats() );
// show helper information
const D3DSURFACE_DESC* surface_desc = DXUTGetBackBufferSurfaceDesc();
if(g_show_help)
{
text_helper.SetInsertionPos(10, surface_desc->Height - 15 * 6);
text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 0.475f, 0.0f, 1.0f) );
text_helper.DrawTextLine(L"Controls (F1 to hide):");
text_helper.SetInsertionPos(40, surface_desc->Height - 15 * 4);
text_helper.DrawTextLine(L"Quir: ESC");
}
else
{
text_helper.SetInsertionPos(10, surface_desc->Height - 15 * 4);
text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) );
text_helper.DrawTextLine(L"Press F1 for help");
}
text_helper.End();
}
//--------------------------------------------------------------------------------------
// Render the scene
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
HRESULT hr;
if(g_settings_dlg.IsActive())
{
g_settings_dlg.OnRender(fElapsedTime);
return;
}
// Clear the render target and the zbuffer
V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0) );
// Render the scene
if( SUCCEEDED( pd3dDevice->BeginScene() ) )
{
pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
RenderText();
V(g_button_dlg.OnRender(fElapsedTime));
V( pd3dDevice->EndScene() );
}
}
//--------------------------------------------------------------------------------------
// Handle messages to the application
//--------------------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
bool* pbNoFurtherProcessing, void* pUserContext )
{
*pbNoFurtherProcessing = g_dlg_resource_manager.MsgProc(hWnd, uMsg, wParam, lParam);
if(*pbNoFurtherProcessing)
return 0;
if(g_settings_dlg.IsActive())
{
g_settings_dlg.MsgProc(hWnd, uMsg, wParam, lParam);
return 0;
}
*pbNoFurtherProcessing = g_button_dlg.MsgProc(hWnd, uMsg, wParam, lParam);
if(*pbNoFurtherProcessing)
return 0;
return 0;
}
//--------------------------------------------------------------------------------------
// Handle keybaord event
//--------------------------------------------------------------------------------------
void CALLBACK OnKeyboardProc(UINT charater, bool is_key_down, bool is_alt_down, void* user_context)
{
if(is_key_down)
{
switch(charater)
{
case VK_F1:
g_show_help = !g_show_help;
break;
}
}
}
//--------------------------------------------------------------------------------------
// Handle events for controls
//--------------------------------------------------------------------------------------
void CALLBACK OnGUIEvent(UINT event, int control_id, CDXUTControl* control, void* user_context)
{
switch(control_id)
{
case IDC_TOGGLE_FULLSCREEN:
DXUTToggleFullScreen();
break;
case IDC_TOGGLE_REF:
DXUTToggleREF();
break;
case IDC_CHANGE_DEVICE:
g_settings_dlg.SetActive(true);
break;
}
}
//--------------------------------------------------------------------------------------
// Initialize dialogs
//--------------------------------------------------------------------------------------
void InitDialogs()
{
g_settings_dlg.Init(&g_dlg_resource_manager);
g_button_dlg.Init(&g_dlg_resource_manager);
g_button_dlg.SetCallback(OnGUIEvent);
int x = 35, y = 10, width = 125, height = 22;
g_button_dlg.AddButton(IDC_TOGGLE_FULLSCREEN, L"Toggle full screen", x, y, width, height);
g_button_dlg.AddButton(IDC_TOGGLE_REF, L"Toggle REF (F3)", x, y += 24, width, height);
g_button_dlg.AddButton(IDC_CHANGE_DEVICE, L"Change device (F2)", x, y += 24, width, height, VK_F2);
}
//--------------------------------------------------------------------------------------
// Initialize everything and go into a render loop
//--------------------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
{
// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
// Set the callback functions
DXUTSetCallbackDeviceCreated( OnCreateDevice );
DXUTSetCallbackDeviceReset( OnResetDevice );
DXUTSetCallbackDeviceLost( OnLostDevice );
DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
DXUTSetCallbackMsgProc( MsgProc );
DXUTSetCallbackFrameRender( OnFrameRender );
DXUTSetCallbackFrameMove( OnFrameMove );
DXUTSetCallbackKeyboard(OnKeyboardProc);
// TODO: Perform any application-level initialization here
InitDialogs();
// Initialize DXUT and create the desired Win32 window and Direct3D device for the application
DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
DXUTCreateWindow( L"TEX Compressed Texture" );
DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 800, 600, IsDeviceAcceptable, ModifyDeviceSettings );
// Start the render loop
DXUTMainLoop();
// TODO: Perform any application-level cleanup here
return DXUTGetExitCode();
}
下載示例工程