【问题标题】:Using shader resources in HLSL (Port DX9->DX10)在 HLSL 中使用着色器资源(端口 DX9->DX10)
【发布时间】:2012-01-10 15:33:03
【问题描述】:

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

D3D10:错误:ID3D10Device::DrawIndexed:在着色器代码中声明的视图尺寸与绑定到像素着色器单元插槽 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++和shader代码变成如下:

// 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;
}

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-12
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多