紋理內容常常是存在sRGB格式中的。關于這個格式的細節是可以被找到的。通常,像素管線假定顏色是線性的以便融合(blending)操作可以在線性空間中進行。因為sRGB中的內容是Gamma較正,所以融合操作在線性空間中處理會導致錯誤的結果。顯卡在讀到有關sRGB內容的時候便會取消Gamma較正以避免錯誤的發生。然后當輸出像素的時候再將像素信息寫回sRGB格式中。在這種情況下,所有像素管線中的操作就可以都在心線性空間中進行。
Gamma校正
在D3D9中。
可以指明一張紋理是不是Gamma 2.2(sRGB) 較正.驅動程序將會在SetTexture的時候將其轉換到線性的Gamma以進行融合操作。或者采樣器將會在查詢的時候將其變為線性數據。
可以指明像素管線在輸出到渲染目標的時候是否將Gamma校正變換回sRGB空間。
所有其它顏色(clear color, material color, vertex color, etc)都被假定為線性空間中。應用程序可以用像素著色器指令對寫入到幀緩存中的顏色進行Gamma校正。線性化操作只對RGB通道有效,忽略ALPHA通道。
不是所有的表面格式都可以線性化。只有通過
IDirect3D9::CheckDeviceFormat檢測(參數為D3DUSAGE_QUERY_SRGBREAD )的格式才可以被線性化。除此之外,采樣狀態D3DSAMP_SRGBTEXTURE 也會被忽略。只有無符號紋理格式支持這種變換。無符號紋理格式是指僅包含有R G B 和 L成分的紋理格式。如果包含ALPHA通道,那它將被忽略。如果混合的格式支持sRGB線性化,那么只有無符號通道有效。理想情況是硬件在紋理過濾前實現線性化。但在D3D9中,硬件只有在紋理過濾后才允許線性化。
不是所有的表面都可以被寫進sRGB空間,只有通過用D3DUSAGE_QUERY_SRGBWRITE進行
IDirect3D9::CheckDeviceFormat 測試的表面格式才能進行線性化。另外,渲染狀態中的D3DRS_SRGBWRITEENABLE標志將會被忽略。每個通道8位的無符號RGB格式是比較適合的格式。
理想地,硬件將會在線性空間上進行幀緩存融合操作。但實際上硬件只能在像素管線處理后,幀緩存融合前進行。這意味著在sRGB中進行幀緩存融合操作會導致錯誤的結果。當清除渲染目標時。D3DRS_SRGBWRITEENABLE 標志 is Honored.對于硬件支持多渲染目標或多元素紋理的情況,只有第一個渲染目標和第一個元素會被寫入緩存。
API變化
API Changes
// New sampler state (DWORD) 新的采樣器狀態
// If this is nonzero, the texture is linearized on lookup.
如果它非0,紋理在查詢是線性化。
D3DSAMP_SRGBTEXTURE // Default FALSE 默認為假
// New render state (DWORD)
新的渲染狀態
D3DRS_SRGBWRITEENABLE // Default FALSE 默認為假
// New usage flags
新的使用標志
D3DUSAGE_QUERY_SRGBWRITE
D3DUSAGE_QUERY_SRGBREAD
窗口下的交換鏈
為了進行正確的融合操作,應用程序保存他們的交換鏈在線性空間中的后臺緩沖區是非常必要的。因為桌面通常情況下是不在線性空間的。所以需要在后臺緩沖區內容顯示前進行Gamma校正。
應用程序可以通過新增額外的緩沖區來自我校正,并把他自已正確的結果從線性空間復制到后臺緩沖區。當驅動將Gamma校正作為部分顯示的時候,是可以避免使用額外的緩沖區的。
許多時候,需要處理設備丟失問題,而通常情況下,RESET會因為一些小問題而導致失敗,下面我就把gamedev上的一貼子翻譯一下,只翻譯中間那小段
摘自:
http://www.gamedev.net/community/forums/topic.asp?topic_id=146731
1) One of the parameters you pass is probably not possible on the hardware, e.g. a depth buffer format which won't work with the back buffer format.
你傳入的D3DPRESENT_PARAMETERS和你的硬件不符,可能是深度格式與你的后臺緩沖格式不匹配。通常情況下我們是將先前的D3DPRESENT_PARAMETERS保存,RESET的時候傳入,若是這種情況,則不必擔心這個問題
2) The debug D3D runtime will tell you exactly "why":
把DirectX Control Pannel中的Direct3D開為調試模式,運行過后,編譯器的信息提示框里會輸出原因,多半是因為位于D3DPOOL_DEFAULT中的內容未釋放完而導致的
a. When you install the DirectX SDK you get the option to install the debug or retail runtime, if you're developing software, always choose debug.
安裝SDK的時候,你可以選則是調式還是運行模式,如果你是軟件開發,通常選擇為調式
b. Go to the control panel and open the DirectX applet.
到SDK中把DirectX Control Pannel小程序打開
c. Go to the Direct3D tab and put the "debug output level" slider to maximum.
把DirectX Control Pannel中的Direct3D開為調試模式
d. Run your application in the debugger (if using MSVC, press F5) and repeat whatever process causes it to fail.
在調試狀態下運行你的程序,重復處理導致你出錯的地方
e. Once it fails, close the app if necessary and return to MSVC, now look in the "output" pane (usually at the bottom). D3D will tell you about everything noteworthy, from information about its DLL being attached to your application, to warnings about things which may harm performance to the full reason why it gave an error.
如果發現失敗了,就關掉調試,在輸出信息面板中D3D將會告訴你是什么原因導致你失敗的。
f. If your application creates its D3D device in PURE mode, creating it in non-PURE mode should enable more checking and reporting.
如果你的程序創建的時候的D3D設備是PURE模式,那在創建的時候改為非PURE模式,這樣你在上面的控制面板中得到的信息會更多。
In DirectX 8, support for rendering to multiple windows is provided through the creation of additional swap chains. However, there are currently no examples of this in the SDK, and the documentation is a bit vague. This article is provided to fill the gaps, and will explain the steps you need to take to write an application that will render multiple views in separate windows.
在DX8中,對多窗口的支持是通過創建更多的Swap Chains來提供的。SDK中沒有相關的例子而且文檔也只是泛泛而談。這篇文章就是為了解決這個問題,它將向您展示應當如何一步步地實現在多個分離窗口中渲染多個視圖。
Step 1 - Setting Up The Parent Frame
第一步:設置父框架窗口
In an application with multiple views, we start with a top level frame that will contain child windows in its client area to display various views. Once the parent frame parent frame has been created, we create our Direct3D device interface, specifying windowed mode and setting the top level window handle as the focus window:
在多視圖的應用程序中,我們需要從最高層次的框架——這個框架將包含所有在他用戶區之內的子視圖窗口——開始我們的旅程。當父框架創建的時候,我們需要創建Direct3D Device接口,為其指定使用窗口模式,而且設置這最高層次的窗口句柄作為“焦點窗口”的句柄:
g_pD3D=Direct3DCreate8(D3D_SDK_VERSION);
if (!g_pD3D) return -1;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
// Use the current display mode. 使用當前的顯示模式
D3DDISPLAYMODE mode;
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT , &mode))) {
SAFE_RELEASE(g_pD3D);
return -1;
}
d3dpp.BackBufferFormat = mode.Format;
d3dpp.BackBufferWidth = mode.Width;
d3dpp.BackBufferHeight = mode.Height;
d3dpp.EnableAutoDepthStencil=TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
// m_hWnd is handle to top level window m_hWnd是最高層窗口的句柄
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice) ) ) {
SAFE_RELEASE(g_pD3D);
return -1;
}
Note that for simplicity the above code does not test depth format, instead choosing a fixed format. Your application should determine a compatible depth format for the format of the rendering target.
注意上面代碼處于簡單考慮并沒有去測試深度緩存的格式(?depth format),而只是選擇了一個確定的格式(D3DFMT_D16)。您的程序應該為需要渲染的Render Target選擇一個可接受的深度緩存格式。
The device has a frame buffer, which the child views will be rendered into, as well as a depth buffer which will be shared among the views. The frame buffer and depth buffer are sized to the full screen resolution, to allow for the fact that the window may later be resized. Otherwise, window size changes would require resetting the device and re-creating the swap chains.
Device都需要有幀緩存,這樣子視圖才能進行渲染,同時,深度緩沖也應當被不同的視圖進行共享。幀緩存和深度緩存都被設置為全屏幕大小,以考慮到可能窗口會被改變大小的情況。如果不的話,窗口改變大小的時候,就需要Reset Device和重新創建Swap Chain。
Step 2 - Setting Up View Windows
第二步:設置子視圖窗口
Now we are ready to create our view windows, and associate them with swap chains that can be rendered to the device. Once the windows have been created, the following code generates a swap chain for the child window:
現在我們可以準備創建我們的子窗口也就是視圖窗口,并把它們與交換鏈關聯以使得他們可以被渲染到Device上。當窗口創建后,下面的代碼將為子窗口創建一個交換鏈:
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
// Use the current display mode. 使用當前的顯示模式
D3DDISPLAYMODE mode;
g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT , &mode);
d3dpp.BackBufferFormat = mode.Format;
// m_hWnd contains child window handle m_hWnd儲存子窗口的句柄
d3dpp.hDeviceWindow=m_hWnd;
// m_pSwapChain is IDirect3DSwapChain * m_pSwapChain是一個IDirect3DSwapChain*對象
g_pd3dDevice->CreateAdditionalSwapChain(&d3dpp, &m_pSwapChain);
After executing this code, the m_pSwapChain variable will contain a pointer to an IDirect3DSwapChain interface, which contains a frame buffer corresponding to the client area of the child window. This process is performed for each view window, so that that there is a swap chain for each view window.
經過這些代碼之后,m_pSwapChain變量就儲存了IDirect3DSwapChain接口的指針,這個接口將儲存子窗口視圖區所對應的幀緩沖。
Step 3 - Rendering a View
第三步:渲染視圖
Prior to rendering each view, we must direct the device to render to the appropriate frame buffer, using the SetRenderTarget() method. We pass the back buffer from the window's swap chain, while using the depth buffer that was originally created with the device:
在渲染每個視圖窗口之前,我們必須使得Device來渲染對應的幀緩沖,這我們就需要用到SetRenderTarget方法。我們向其中傳入子窗口SwapChain交換鏈的后備緩沖BackBuffer,以及使用最開始跟著Device一起創建的深度緩沖。
LPDIRECT3DSURFACE8 pBack=NULL,pStencil=NULL;
m_pSwapChain->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&pBack);
g_pd3dDevice->GetDepthStencilSurface(&pStencil);
g_pd3dDevice->SetRenderTarget(pBack,pStencil);
pBack->Release();
pStencil->Release();
Note that we release the stencil and backbuffer pointers after we use them, because the GetBackBuffer() and GetDepthStencilSurface() functions call AddRef() on these interfaces to increment their reference counters. Failing to release them would lead to a memory leak.
注意我們必須Release掉Stencil和BackBuffer的指針,因為GetBackBuffer和GetDepthStencilSurface這兩個函數都會調用COM的AddRef方法,來增加相應COM接口的引用計數,因此如果不刪除它們,將會導致內存泄露。
We are now ready to render the view. Rendering is performed within a scene in the normal manner, except that we call Present() on the swap chain interface rather than the device interface:
我們現在已經做好準備渲染視圖窗口了。渲染的方法看起來和我們平常用的方法差不多,只是有一點:我們現在需要調用Swap Chain的接口,而不是Device的接口。
g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0x00000000,1.0,0);
if (SUCCEEDED(g_pd3dDevice->BeginScene())) {
// rendering code goes here 渲染代碼寫在這里
g_pd3dDevice->EndScene();
}
m_pSwapChain->Present(NULL,NULL,NULL,NULL);
Step 4 - Handling Resize of Child Views
第四步,子窗口的Resize問題
DirectX will automatically deal with changes in the child view by using a stretch blit to present the swap chain if the dimensions have client area is not the same size as the swap chain's frame buffer. However, this may not be desirable, as it will cause aliasing if the client area is increased in size.
如果窗口的視圖區大小和SwapChain的大小不一,那么DirectX將通過Stretch Blit來自動處理圖像的伸縮變化。盡管這可能并不令人期待,因為這在視圖區變大的時候將導致圖像的模糊。