在 HLSL 中使用着色器资源(端口 DX9->DX10)



我正在尝试将DX9卷渲染器移植到DX10版本。目前,我陷入了以下错误:

D3D10:错误:ID3D10Device::DrawIndexed:着色器代码中声明的视图维度与绑定到Pixel shader单元的插槽0的视图类型不匹配。如果着色器实际使用视图,则此选项无效(例如,由于着色器代码分支而未跳过视图)。[执行错误#354:DEVICE_DRAW_VIEW_DIMENSION_MISMATCH]

我的猜测是,我没有以正确的方式将2D和/或3D纹理(着色器资源)发送到着色器;或者不要以正确的(dx10)方式使用它们。DX9代码如下(为了这个问题而简化):

HRESULT小时;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;
}

请注意,这只是一个有根据的猜测,错误是由这段代码引入的。如果上面的代码对你来说是正确的,请也这样回复(在评论中)。在这种情况下,寻找解决方案的新方向将受到重视。

经过一天的工作,我确实发现了自己的问题;我忘记重新编译更新的着色器。所以仍然加载了DX9版本而不是DX10版本的着色器,这非常愚蠢,但也是非常常见的错误。

最新更新