3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DirectX12 事始め 深度ステンシルの生成

Last updated at Posted at 2018-08-11

今回は、深度ステンシルを作って行きます。
深度ステンシルとは、3D制作などでよく聞くZバッファのことです。
どちらの描画を前に置くかなどを決めるためにあります。

Depth.h
# include "Descriptor.h"
class Window;
class Depth :
   public Descriptor
{
public:
   // コンストラクタ
   Depth(Window* win, Device* dev, List* list);
   // デストラクタ
   ~Depth();

   // 深度ステンシルのセット
   void SetDepth(void);
   // リソースの取得
   ID3D12Resource* GetResource(void) const {
      return resource;
   }
private:
   // リソースの生成
   HRESULT CreateResource(void);
   // リソースビューの生成
   HRESULT CreateView(void);

   // ウィンドウ
   Window* win;
   // リソース
   ID3D12Resource * resource;
};
Depth.cpp
# include "Depth.h"
# include "Window.h"
# include "Device.h"
# include "List.h"

// コンストラクタ
Depth::Depth(Window* win, Device* dev, List* list) :
   win(win)
{
   this->dev  = dev;
   this->list = list;

   CreateResource();
   CreateView();
}

// デストラクタ
Depth::~Depth()
{
   Release(resource);
   Release(heap);
   delete list;
   delete dev;
   delete win;
}
// リソースの生成
HRESULT Depth::CreateResource(void)
{
   result = CreateHeap(D3D12_DESCRIPTOR_HEAP_TYPE_DSV, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 1);

   //プロパティ設定用構造体の設定
   D3D12_HEAP_PROPERTIES prop = {};
   prop.CPUPageProperty      = D3D12_CPU_PAGE_PROPERTY::D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
   prop.CreationNodeMask     = 1;
   prop.MemoryPoolPreference = D3D12_MEMORY_POOL::D3D12_MEMORY_POOL_UNKNOWN;
   prop.Type                 = D3D12_HEAP_TYPE::D3D12_HEAP_TYPE_DEFAULT;
   prop.VisibleNodeMask      = 1;

   //リソース設定用構造体の設定
   D3D12_RESOURCE_DESC desc = {};
   desc.Dimension          = D3D12_RESOURCE_DIMENSION::D3D12_RESOURCE_DIMENSION_TEXTURE2D;
   desc.Alignment          = 0;
   desc.Width              = win->GetX();
   desc.Height             = win->GetY();
   desc.DepthOrArraySize   = 1;
   desc.MipLevels          = 0;
   desc.Format             = DXGI_FORMAT::DXGI_FORMAT_D32_FLOAT;
   desc.SampleDesc.Count   = 1;
   desc.SampleDesc.Quality = 0;
   desc.Flags              = D3D12_RESOURCE_FLAGS::D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
   desc.Layout             = D3D12_TEXTURE_LAYOUT::D3D12_TEXTURE_LAYOUT_UNKNOWN;

   //クリア値設定用構造体の設定
   D3D12_CLEAR_VALUE clear = {};
   clear.Format               = DXGI_FORMAT::DXGI_FORMAT_D32_FLOAT;
   clear.DepthStencil.Depth   = 1.0f;
   clear.DepthStencil.Stencil = 0;

   // リソース生成
   result = dev.lock()->Get()->CreateCommittedResource(&prop, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_DEPTH_WRITE, &clear, IID_PPV_ARGS(&resource));

   return result;
}
// リソースビューの生成
HRESULT Depth::CreateView(void)
{
   //深度ステンシルビュー設定用構造体の設定
   D3D12_DEPTH_STENCIL_VIEW_DESC desc = {};
   desc.Format        = DXGI_FORMAT_D32_FLOAT;
   desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
   desc.Flags         = D3D12_DSV_FLAG_NONE;

   //深度ステンシルビュー生成
   dev->Get()->CreateDepthStencilView(resource, &desc, GetHeap()->GetCPUDescriptorHandleForHeapStart());

   return result;
}
// 深度ステンシルのセット
void Depth::SetDepth(void)
{
   //深度ステンシルヒープの先頭ハンドルの取得
   D3D12_CPU_DESCRIPTOR_HANDLE d_handle = heap->GetCPUDescriptorHandleForHeapStart();

   //深度ステンシルビューのクリア
   list->GetList()->ClearDepthStencilView(d_handle, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
}

これで深度ステンシルの生成も終了です。
今更ですが、DirectX12を使いやすくしてくれるd3dx12.hというものがあるのですが、
自分はそのヘッダーを使わずに行っていますので、結構長い文章になっています。
もし、もっと短く書きたい方は使ってみてはいかがでしょうか。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?