码迷,mamicode.com
首页 > 其他好文 > 详细

DirectX11笔记7:模板Stenciling

时间:2015-12-06 07:17:56      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

跟以前的一样,对于模板缓冲区,也是声明定义一系列的结构体,并在渲染的时候 打开/关闭 他们

在使用CreateRenderTargetView创建视图之后,声明定义深度模板缓冲区:

    ID3D11Texture2D *depthStencilBuffer(NULL);
    //测试多重采样抗锯齿等级
    UINT x4MsaaQuality;
    dev->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM, 4, &x4MsaaQuality);
    printf("CheckMultisampleQualityLevels:%d\n", x4MsaaQuality);
    //
    D3D11_TEXTURE2D_DESC dsDesc;
    dsDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    dsDesc.Width = 1200;
    dsDesc.Height = 600;
    dsDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    dsDesc.MipLevels = 1;
    dsDesc.ArraySize = 1;
    dsDesc.CPUAccessFlags = 0;
    dsDesc.SampleDesc.Count = (x4MsaaQuality<1?1:4);
    dsDesc.SampleDesc.Quality = (x4MsaaQuality<1 ? 0 : x4MsaaQuality - 1);
    dsDesc.MiscFlags = 0;
    dsDesc.Usage = D3D11_USAGE_DEFAULT;
    hr = dev->CreateTexture2D(&dsDesc, 0, &depthStencilBuffer);
    if (FAILED(hr))
    {
        return false;
    }
    hr = dev->CreateDepthStencilView(depthStencilBuffer, 0, &depthStencilView);
    if (FAILED(hr))
    {
        return false;
    }
    devContext->OMSetRenderTargets(1, &backBuffer, depthStencilView);
    depthStencilBuffer->Release();

 

 结构:CheckMultisampleQualityLevels验证多重采样抗锯齿的等级------CreateTexture2D创建2D贴图缓冲区------CreateDepthStencilView创建缓冲区视图------OMSetRenderTargets进行绑定。

主要记录一下

D3D11_TEXTURE2D_DESC这个结构:

 

typedef struct D3D11_TEXTURE2D_DESC {
  UINT             Width;
  UINT             Height;
  UINT             MipLevels;
  UINT             ArraySize;
  DXGI_FORMAT      Format;
  DXGI_SAMPLE_DESC SampleDesc;
  D3D11_USAGE      Usage;
  UINT             BindFlags;
  UINT             CPUAccessFlags;
  UINT             MiscFlags;
} D3D11_TEXTURE2D_DESC;

 

需要注意的元素: 

Width与Height:贴图的高与宽,要与上面创建的视图缓冲区与下面的视窗大小相匹配。

 BindFlags:这里我使用DXGI_FORMAT_D24_UNORM_S8_UINT,24位用于深度缓冲区,8位用于模板缓冲区。

 SampleDesc:这是一个结构体

typedef struct DXGI_SAMPLE_DESC {
  UINT Count;
  UINT Quality;
} DXGI_SAMPLE_DESC;

MSDN有这样的解释:

The image quality level. The higher the quality, the lower the performance. 
The valid range is between zero and one less than the level returned by ID3D10Device::CheckMultisampleQualityLevels for Direct3D 10
or ID3D11Device::CheckMultisampleQualityLevels for Direct3D 11.

也可以见书中4.1.7关于多重采样的内容。

注意,这里的多重采样的元素设置错误,可能会导致无法绘制的BUG。

 

然后是设定模板测试:

代码

HRESULT DrawFunction::CreateStencilDesc(void)
{
    HRESULT hr = S_OK;//ret
    D3D11_DEPTH_STENCIL_DESC stencilDesc;
    ZeroMemory(&stencilDesc, sizeof(stencilDesc));
    stencilDesc.DepthEnable = FALSE;
    stencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
    stencilDesc.DepthFunc = D3D11_COMPARISON_ALWAYS;
    stencilDesc.StencilEnable = TRUE;
    stencilDesc.StencilReadMask = 0xff;
    stencilDesc.StencilWriteMask = 0xff;
    stencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
    stencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
    stencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_REPLACE;
    stencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
    stencilDesc.BackFace = stencilDesc.FrontFace;

    hr = dev->CreateDepthStencilState(&stencilDesc, &pStencilReplace);
    stencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
    stencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_EQUAL;
    stencilDesc.BackFace = stencilDesc.FrontFace;
    hr = dev->CreateDepthStencilState(&stencilDesc, &pStencilTest);

    return hr;
}

 

 结构体:

typedef struct D3D11_DEPTH_STENCIL_DESC {
  BOOL                       DepthEnable;
  D3D11_DEPTH_WRITE_MASK     DepthWriteMask;
  D3D11_COMPARISON_FUNC      DepthFunc;
  BOOL                       StencilEnable;
  UINT8                      StencilReadMask;
  UINT8                      StencilWriteMask;
  D3D11_DEPTH_STENCILOP_DESC FrontFace;
  D3D11_DEPTH_STENCILOP_DESC BackFace;
} D3D11_DEPTH_STENCIL_DESC;

 

 需要注意的元素:

掩码元素StencilReadMask与StencilWriteMask,就是指当读取/写入模板元素时,用数据与掩码进行AND与运算。代码中我设置为0xFF表示不做任何变化。

 

//模板未更新完,先放一个简单的demon,在之后学习的实例的时候再进行补充。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

 

 

 

 

工程文件

 

DirectX11笔记7:模板Stenciling

标签:

原文地址:http://www.cnblogs.com/Windogs/p/5022789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!