我試圖將我的DX9卷渲染器移植到DX10版本。目前,我被困在下面的錯誤:使用HLSL中的着色器資源(端口DX9-> DX10)
D3D10: ERROR: ID3D10Device::DrawIndexed: The view dimension declared in the shader code does not match the view type bound to slot 0 of the Pixel Shader unit. This is invalid if the shader actually uses the view (e.g. it is not skipped due to shader code branching). [ EXECUTION ERROR #354: DEVICE_DRAW_VIEW_DIMENSION_MISMATCH ]
我的猜測是,我不是在發送2D和/或3D紋理(着色器資源),以正確的方式着色器;或者不要以正確的(dx10)方式使用它們。 DX9代碼類似於以下內容(爲了這個問題而簡化):
HRESULT hr; int nVertexShaderIndex = 0;
// Setup the 2D Dependent Lookup Texture
hr = m_pDevice->SetTexture(0, lookupTexture); // lookupTexture is a LPDIRECT3DTEXTURE9
if (hr != D3D_OK) {
//handle error
}
m_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
m_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
m_pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
m_pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
// Maximum Intensity
m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); // Enable Alpha blend
m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); // 1 * SRC color
m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); // 1 * DST color
m_pDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_MAX); // MAX blend
m_pDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); // Disable Z
帶有實際數據的3D卷紋理以類似的方式發送。相應的像素着色器代碼:
PS_OUTPUT Main(VS_OUTPUT vsIn,
uniform sampler2D lookupTexture : TEXUNIT0,
uniform sampler3D dataTexture : TEXUNIT1)
{
PS_OUTPUT psOut;
float dataValue;
psOut.color = SampleWith2DLookup(vsIn.TexCoord0,
lookupTexture,
dataTexture,
dataValue);
return psOut;
}
float4 LookupIn2DTexture(float value,
uniform sampler2D lookupTexture)
{
float2 lutCoord;
float4 outColor;
// Build a 2D Coordinate for lookup
lutCoord[0] = value;
lutCoord[1] = 0.0f;
outColor = tex2D(lookupTexture, lutCoord);
return(outColor);
}
float4 SampleWith2DLookup(const float3 TexCoord,
uniform sampler2D lookupTexture,
uniform sampler3D dataTexture,
out float dataValue)
{
float value;
float4 outputColor;
value = Sample(TexCoord, dataTexture);
outputColor = LookupIn2DTexture(value, lookupTexture);
dataValue = value;
return(outputColor);
}
在DX10我們可以簡化一些着色器代碼的(據我所知)。我創建一個空的紋理,並用map()/ unmap()填充這個紋理。接下來,我將它作爲着色器資源綁定到我的PS。在C++和着色器代碼成爲了以下內容:
// CREATE THE EMPTY TEXTURE
D3D10_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Width = 4096;
desc.Height = 1;
desc.ArraySize = 1;
desc.MipLevels = 1;
desc.Format = GetHardwareResourceFormatDX10();
desc.Usage = D3D10_USAGE_DYNAMIC;
desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
desc.SampleDesc.Count = 1;
hr = m_pDeviceDX10->CreateTexture2D(&desc, NULL, &lookupTexture);
綁定到着色器:
// SEND TO SHADER
ID3D10ShaderResourceView* pTexDepSurface = NULL;
D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc;
D3D10_TEXTURE2D_DESC desc;
pTexDep->GetDesc(&desc);
srvDesc.Format = desc.Format;
srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = desc.MipLevels;
srvDesc.Texture2D.MostDetailedMip = desc.MipLevels -1;
hr = m_pDeviceDX10->CreateShaderResourceView(pTexDep, &srvDesc, &pTexDepSurface);
if (FAILED(hr)) {
//handle here
}
m_pDeviceDX10->PSSetShaderResources(0,1, &pTexDepSurface);
使用着色器:
Texture2D LookupTexture : register(t0);
SamplerState LookupSampler : register(s0);
Texture2D VolumeTexture : register(t1);
SamplerState VolumeSampler : register(s1);
PS_OUTPUT Main(VS_OUTPUT vsIn,
uniform sampler2D lookupTexture : TEXUNIT0,
uniform sampler3D dataTexture : TEXUNIT1)
{
PS_OUTPUT psOut;
float dataValue;
dataValue = VolumeTexture.Sample(VolumeSampler,vsIn.TexCoord0);
psOut.color = LookupTexture.Sample(LookupSampler,dataValue);
return psOut;
}
請注意,這只是一個猜測是錯誤的是由此代碼引入。如果上面的代碼看起來正確,請回復(評論)。在這種情況下,尋找解決方案的新方向將受到重視。