D3D1渲染技术之采样

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jxw167/article/details/82893355

在前面的博客,我们看到除了纹理数据之外,还有两个与使用纹理相关的关键概念:纹理过滤和寻址模式。 采样纹理资源时使用的过滤器和寻址模式由采样器对象定义, 应用程序通常需要多个采样器对象以不同方式对纹理进行采样。

创建采样器

正如我们将在下面看到的,采样器用于着色器, 为了将采样器绑定到着色器以供使用,我们需要将描述符绑定到采样器对象。 以下代码显示了示例根签名,以便第二个槽采用绑定到采样器寄存器槽0的一个采样器描述符的表。

CD3DX12_DESCRIPTOR_RANGE descRange[3];
descRange[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0);
descRange[1].Init(D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, 1, 0);
descRange[2].Init(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, 0);
 
CD3DX12_ROOT_PARAMETER rootParameters[3];
rootParameters[0].InitAsDescriptorTable(1, &descRange[0], D3D12_SHADER_VISIBILITY_PIXEL);
rootParameters[1].InitAsDescriptorTable(1, &descRange[1], D3D12_SHADER_VISIBILITY_PIXEL);
rootParameters[2].InitAsDescriptorTable(1, &descRange[2], D3D12_SHADER_VISIBILITY_ALL);
 
CD3DX12_ROOT_SIGNATURE_DESC descRootSignature;
descRootSignature.Init(3, rootParameters, 0, nullptr,
  D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);

如果我们要设置sampler描述符,我们需要一个采样器堆, 通过填写D3D12_DESCRIPTOR_HEAP_DESC实例并指定堆类型D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER来创建采样器堆:

D3D12_DESCRIPTOR_HEAP_DESC descHeapSampler = {};
descHeapSampler.NumDescriptors = 1;
descHeapSampler.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER;
descHeapSampler.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
 
ComPtr<ID3D12DescriptorHeap> mSamplerDescriptorHeap;
ThrowIfFailed(mDevice->CreateDescriptorHeap(&descHeapSampler,
  __uuidof(ID3D12DescriptorHeap), (void**)&mSamplerDescriptorHeap));

一旦我们有一个采样器堆,我们就可以创建采样器描述符。 在这里我们通过填写D3D12_SAMPLER_DESC对象来指定寻址模式和过滤器类型以及其他参数:

typedef struct D3D12_SAMPLER_DESC
{
  D3D12_FILTER Filter;
  D3D12_TEXTURE_ADDRESS_MODE AddressU;
  D3D12_TEXTURE_ADDRESS_MODE AddressV;
  D3D12_TEXTURE_ADDRESS_MODE AddressW;
  FLOAT MipLODBias;
   UINT MaxAnisotropy;
  D3D12_COMPARISON_FUNC ComparisonFunc;
  FLOAT BorderColor[ 4 ];
  FLOAT MinLOD;
  FLOAT MaxLOD;
} D3D12_SAMPLER_DESC;

我们可以在SDK文档中查找D3D12_FILTER枚举类型,以下示例说明如何为堆中的采样器创建描述符,该采样器使用线性过滤,换行寻址模式和其他参数的典型默认值:

D3D12_SAMPLER_DESC samplerDesc = {};
samplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
samplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
samplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D12_FLOAT32_MAX;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
 md3dDevice->CreateSampler(&samplerDesc, 
  mSamplerDescriptorHeap->GetCPUDescriptorHandleForHeapStart());

以下代码显示如何将采样器描述符绑定到根签名参数槽以供着色器程序使用:

commandList->SetGraphicsRootDescriptorTable(1, 
  samplerDescriptorHeap->GetGPUDescriptorHandleForHeapStart());

静态采样

事实证明,图形应用程序通常只使用少数采样器, 因此,Direct3D提供了一个特殊的快捷方式来定义一个采样器数组并设置它们,而无需经历创建采样器堆的过程。 CD3DX12_ROOT_SIGNATURE_DESC类的Init函数有两个参数,允许我们定义应用程序可以使用的所谓静态采样器数组。 静态采样器由D3D12_STATIC_SAMPLER_DESC结构描述, 此结构与D3D12_SAMPLER_DESC非常相似,但有以下例外:
1、border color 可能有一些限制,具体来说,静态采样器的border color必须是以下成员:

enum D3D12_STATIC_BORDER_COLOR
{
  D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK   = 0,
  D3D12_STATIC_BORDER_COLOR_OPAQUE_BLACK   = ( 
    D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK + 1 ) ,
  D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE   = ( 
    D3D12_STATIC_BORDER_COLOR_OPAQUE_BLACK + 1 ) 
}D3D12_STATIC_BORDER_COLOR;

2、它包含用于指定着色器寄存器,寄存器空间和着色器可见性的其他字段,通常将其指定为采样器堆的一部分。
此外,只能定义2032个静态采样器,这对于大多数应用来说已经足够了。
但是,如果确实需要更多,则可以使用非静态采样器并通过采样器堆。

我们在演示中使用静态采样器, 以下代码显示了我们如何定义静态采样器。 请注意,我们在演示中不需要所有这些静态采样器,但我们仍然定义它们,以便在需要它们时它们就在那里。 无论如何,它只是少数,定义一些可能使用或不使用的额外采样器并没有什么坏处。

std::array<const CD3DX12_STATIC_SAMPLER_DESC, 6>    TexColumnsApp::GetStaticSamplers()
{
  // Applications usually only need a handful of samplers. So just define them 
  // all up front and keep them available as part of the root signature. 
 
  const CD3DX12_STATIC_SAMPLER_DESC pointWrap(
    0, // shaderRegister
    D3D12_FILTER_MIN_MAG_MIP_POINT, // filter
    D3D12_TEXTURE_ADDRESS_MODE_WRAP, // addressU
    D3D12_TEXTURE_ADDRESS_MODE_WRAP, // addressV
    D3D12_TEXTURE_ADDRESS_MODE_WRAP); // addressW
const CD3DX12_STATIC_SAMPLER_DESC pointClamp(
    1, // shaderRegister
    D3D12_FILTER_MIN_MAG_MIP_POINT, // filter
    D3D12_TEXTURE_ADDRESS_MODE_CLAMP, // addressU
    D3D12_TEXTURE_ADDRESS_MODE_CLAMP, // addressV
    D3D12_TEXTURE_ADDRESS_MODE_CLAMP); // addressW
 
  const CD3DX12_STATIC_SAMPLER_DESC linearWrap(
    2, // shaderRegister
    D3D12_FILTER_MIN_MAG_MIP_LINEAR, // filter
    D3D12_TEXTURE_ADDRESS_MODE_WRAP, // addressU
    D3D12_TEXTURE_ADDRESS_MODE_WRAP, // addressV
    D3D12_TEXTURE_ADDRESS_MODE_WRAP); // addressW
 
  const CD3DX12_STATIC_SAMPLER_DESC linearClamp(
    3, // shaderRegister
    D3D12_FILTER_MIN_MAG_MIP_LINEAR, // filter
D3D12_TEXTURE_ADDRESS_MODE_CLAMP, // addressU
    D3D12_TEXTURE_ADDRESS_MODE_CLAMP, // addressV
    D3D12_TEXTURE_ADDRESS_MODE_CLAMP); // addressW
 
  const CD3DX12_STATIC_SAMPLER_DESC anisotropicWrap(
    4, // shaderRegister
    D3D12_FILTER_ANISOTROPIC, // filter
    D3D12_TEXTURE_ADDRESS_MODE_WRAP, // addressU
    D3D12_TEXTURE_ADDRESS_MODE_WRAP, // addressV
    D3D12_TEXTURE_ADDRESS_MODE_WRAP, // addressW
    0.0f,               // mipLODBias
    8);                // maxAnisotropy
 
  const CD3DX12_STATIC_SAMPLER_DESC anisotropicClamp(
    5, // shaderRegister
    D3D12_FILTER_ANISOTROPIC, // filter
     D3D12_TEXTURE_ADDRESS_MODE_CLAMP, // addressU
    D3D12_TEXTURE_ADDRESS_MODE_CLAMP, // addressV
    D3D12_TEXTURE_ADDRESS_MODE_CLAMP, // addressW
    0.0f,               // mipLODBias
    8);                // maxAnisotropy
 
  return { 
    pointWrap, pointClamp,
    linearWrap, linearClamp, 
    anisotropicWrap, anisotropicClamp };
}
 
void TexColumnsApp::BuildRootSignature()
{
  CD3DX12_DESCRIPTOR_RANGE texTable;
  texTable.Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0);
 
  // Root parameter can be a table, root descriptor or root constants.
  CD3DX12_ROOT_PARAMETER slotRootParameter[4];
 
  slotRootParameter[0].InitAsDescriptorTable(1, 
    &texTable, D3D12_SHADER_VISIBILITY_PIXEL);
  slotRootParameter[1].InitAsConstantBufferView(0);
  slotRootParameter[2].InitAsConstantBufferView(1);
  slotRootParameter[3].InitAsConstantBufferView(2);
 
  auto staticSamplers = GetStaticSamplers();
 
  // A root signature is an array of root parameters.
  CD3DX12_ROOT_SIGNATURE_DESC rootSigDesc(4, slotRootParameter,
    (UINT)staticSamplers.size(), staticSamplers.data(),
    D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
 
  // create a root signature with a single slot which points to a
  // descriptor range consisting of a single constant buffer
  ComPtr<ID3DBlob> serializedRootSig = nullptr;
  ComPtr<ID3DBlob> errorBlob = nullptr;
  HRESULT hr = D3D12SerializeRootSignature(&rootSigDesc, D3D_ROOT_SIGNATURE_VERSION_1,
    serializedRootSig.GetAddressOf(), errorBlob.GetAddressOf());
 
  if(errorBlob != nullptr)
  {
    ::OutputDebugStringA((char*)errorBlob->GetBufferPointer());
  }
  ThrowIfFailed(hr);
 
  ThrowIfFailed(md3dDevice->CreateRootSignature(
    0,
    serializedRootSig->GetBufferPointer(),
    serializedRootSig->GetBufferSize(),
    IID_PPV_ARGS(mRootSignature.GetAddressOf())));
}

猜你喜欢

转载自blog.csdn.net/jxw167/article/details/82893355