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

时间:2012-01-10 15:33:03

标签: hlsl directx-9 pixel-shader directx-10

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

  

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

我的猜测是我没有以正确的方式将二维和/或三维纹理(着色器资源)发送到着色器;或者不要以正确的(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;
}

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

1 个答案:

答案 0 :(得分:3)

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