2011-07-22 69 views
0

我是DirectX10的新手。現在我正在開發一個Direct10應用程序。它將根據用戶的輸入混合兩個手動填充的紋理。當前的實現是多個紋理不顯示

  1. 創建兩個空的紋理,其用法爲D3D10_USAGE_STAGING。
  2. 創建兩個資源着色器視圖以綁定到像素着色器,因爲着色器需要它。
  3. 通過調用CopyResource將紋理複製到GPU內存。

現在的問題是,我只能看到第一個紋理,但我沒有看到第二個。它在我看來,綁定不適用於第二個紋理。

我不知道它有什麼問題。任何人都可以在這裏給我點亮嗎?

謝謝, 馬歇爾

類COverlayTexture需要負責創建的質感,創建資源視圖,填充與來自另一應用程序了映射的位圖紋理並結合至像素着色器資源視圖。

HRESULT COverlayTexture::Initialize(VOID) 
{ 
D3D10_TEXTURE2D_DESC texDesStaging; 
texDesStaging.Width = m_width; 
texDesStaging.Height = m_height; 
texDesStaging.Usage = D3D10_USAGE_STAGING; 
texDesStaging.BindFlags = 0; 
texDesStaging.ArraySize = 1; 
texDesStaging.MipLevels = 1; 
texDesStaging.SampleDesc.Count = 1; 
texDesStaging.SampleDesc.Quality = 0; 
texDesStaging.MiscFlags = 0; 
texDesStaging.Format = DXGI_FORMAT_B8G8R8A8_UNORM; 
texDesStaging.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; 
HR(m_Device->CreateTexture2D(&texDesStaging, NULL, &m_pStagingResource)); 

D3D10_TEXTURE2D_DESC texDesShader; 
texDesShader.Width = m_width; 
texDesShader.Height = m_height; 
texDesShader.BindFlags = D3D10_BIND_SHADER_RESOURCE; 
texDesShader.ArraySize = 1; 
texDesShader.MipLevels = 1; 
texDesShader.SampleDesc.Count = 1; 
texDesShader.SampleDesc.Quality = 0; 
texDesShader.MiscFlags = 0;  
texDesShader.Format = DXGI_FORMAT_B8G8R8A8_UNORM; 
texDesShader.Usage = D3D10_USAGE_DEFAULT;  
texDesShader.CPUAccessFlags = 0; 
HR(m_Device->CreateTexture2D(&texDesShader, NULL, &m_pShaderResource)); 

D3D10_SHADER_RESOURCE_VIEW_DESC viewDesc; 
ZeroMemory(&viewDesc, sizeof(viewDesc)); 
viewDesc.Format = texDesShader.Format; 
viewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; 
viewDesc.Texture2D.MipLevels = texDesShader.MipLevels; 
HR(m_Device->CreateShaderResourceView(m_pShaderResource, &viewDesc, &m_pShaderResourceView)); 
} 

HRESULT COverlayTexture::Render(VOID) 
{ 
m_Device->PSSetShaderResources(0, 1, m_pShaderResourceView); 

D3D10_MAPPED_TEXTURE2D lockedRect; 
m_pStagingResource->Map(0, D3D10_MAP_WRITE, 0, &lockedRect); 

// Fill in the texture with the bitmap mapped from shared memory view 

m_pStagingResource->Unmap(0); 

m_Device->CopyResource(m_pShaderResource, m_pStagingResource); 
} 

我使用類COverlayTexture其中的每一個分別填充其自己的位圖,以它的質地,並用序列COverlayTexture呈現的兩個實例[1]然後COverlayTexture [0]。

COverlayTexture* pOverlayTexture[2]; 

for(int i = 1; i < 0; i++) 
{ 
    pOverlayTexture[i]->Render() 
} 

在FX文件的共混物,狀態設置爲definedas下面:

BlendState AlphaBlend 
{ 
AlphaToCoverageEnable = FALSE; 
BlendEnable[0] = TRUE; 
     SrcBlend = SRC_ALPHA; 
     DestBlend = INV_SRC_ALPHA; 
     BlendOp = ADD; 
     BlendOpAlpha = ADD; 
     SrcBlendAlpha = ONE; 
DestBlendAlpha = ZERO; 
RenderTargetWriteMask[0] = 0x0f; 
}; 

在FX文件中的像素着色器定義如下:再次

Texture2D txDiffuse; 
float4 PS(PS_INPUT input) : SV_Target 
{ 
float4 ret = txDiffuse.Sample(samLinear, input.Tex); 
return ret; 
} 

感謝。

編輯保羅:

非常感謝,保羅。問題是該對象的哪個實例應該綁定到a​​lpha紋理或漫反射紋理。作爲測試,我將COverlayTexture [0]與Alpha和COverlayTexture [1]綁定到漫反射紋理。

Texture2D txDiffuse[2]; 
float4 PS(PS_INPUT input) : SV_Target 
{ 
float4 ret = txDiffuse[1].Sample(samLinear, input.Tex); 
float alpha = txDiffuse[0].Sample(samLinear, input.Tex).x; 

return float4(ret.xyz, alpha); 
} 

我爲兩個資源視圖調用了PSSetShaderResources。

g_pShaderResourceViews[0] = overlay[0].m_pShaderResourceView; 
g_pShaderResourceViews[1] = overlay[1].m_pShaderResourceView; 
m_Device->PSSetShaderResources(0, 2, g_pShaderResourceViews); 

結果是我什麼也沒看到。我也嘗試了通道x,y,z,w。

+0

你叫'之前繪製另一個SetTexture'? – Djole

+0

我認爲它與DX9中的一樣,但在DX10中它似乎更復雜一些。據我所知,沒有任何'SetTexture',請查看http://takinginitiative.net/2008/11/29/directx-10-tutorial-3-textures/ – Djole

回答

0

發表一些更多的代碼。

我不知道你是如何混合這兩個紋理。如果你想在像素着色器中混合使用它們,你需要對它們進行採樣然後添加它們(或者你需要的任何操作)。

你如何添加紋理?通過設置ID3D11BlendState或像素着色器?

編輯:

你不需要在每一個類中的兩個紋理:如果您想寫信給你的紋理您的使用情況應該是D3D10_USAGE_DYNAMIC。當你這樣做時,你也可以將這個紋理作爲着色器資源,所以你不需要執行m_Device->CopyResource(m_pShaderResource, m_pStagingResource);步驟。

由於您使用的是alpha混合,您必須控制像素着色器(像素着色器返回的float4的w分量)中的alpha值輸出。

綁定兩個紋理應用到像素着色器,並使用一個紋理值作爲alpha分量:

Texture2D txDiffuse; 
Texture2D txAlpha; 
float4 PS(PS_INPUT input) : SV_Target 
{ 
    float4 ret = txDiffuse.Sample(samLinear, input.Tex); 
    float alpha=txAlpha.Sample(samLinear,input.Tex).x; // Choose the proper channel 
    return float4(ret.xyz,alpha); // Alpha is the 4th component 
} 
+0

對不起,用錯誤的術語來描述問題。我目前的實現如下: – Marshall

+0

1.從CPU內存創建紋理[0] - >填充紋理[0] - >創建資源視圖綁定到像素着色器 - >從CPU內存複製資源到GPU內存。 – Marshall

+0

2.從CPU內存創建紋理[1] - >填充紋理[1] - >創建資源視圖以綁定像素着色器 - > CopyResource從CPU內存到GPU內存。 3.致電禮物。像素着色器如前所述。我也調用ID3D11BlendState,但它不起作用。看起來紋理[1]不能綁定到像素着色器。 – Marshall