3
4

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-06

今回からDirectX12の中身に触れていきます。
デバイスの生成を今回は行っていこうと思います。

まず今後作っていくクラスの基底クラスを定義します。

Obj.h
# include <d3d12.h>
// 解放マクロ
# define Release(X) { if ((X) != nullptr) (X)->Release(); (X) = nullptr; }
classObj
{
public:
   // コンストラクタ
   Obj() : result(S_OK) {
   }
   // デストラクタ
   virtual ~Obj() {
   }

protected:
   // 参照結果
   HRESULT result;
};

それではこの基底クラスを元にデバイスを使っていきたいと思います。

Device.h
# include “Obj.h”
class Device :
   public Obj
{
public:
   // コンストラクタ
   Device();
   // デストラクタ
   ~Device();

   // デバイスの取得
   ID3D12Device* Get(void) const {
      return dev;
   }
private:
   // デバイスの生成
   HRESULT Create(void);

   // デバイス
   ID3D12Device* dev;
   // 機能レベル
   D3D_FEATURE_LEVEL level;
};
Device.cpp
# include “Device.h”

// 機能レベルの一覧
D3D_FEATURE_LEVEL levels[] = {
   D3D_FEATURE_LEVEL_12_1,
   D3D_FEATURE_LEVEL_12_0,
   D3D_FEATURE_LEVEL_11_1,
   D3D_FEATURE_LEVEL_11_0
};

// コンストラクタ
Device::Device() : 
   dev(nullptr)
{
   Create();
}

// デストラクタ
Device::~Device()
{
   Release(dev);
}

// デバイスの生成
HRESULT Device::Create(void)
{
   //使用PCの機能レベルにあったもので作成します。
   for (auto& i : levels)
   {
      result = D3D12CreateDevice(nullptr, i, IID_PPV_ARGS(&dev));
      if (result == S_OK) 
      {
         //マッチした機能レベルを格納してます。
         level = i;
         break;
      }
   }
   return result;
}

これでデバイスの生成は終了です。
機能レベルを直打ちでなくどの機能レベルで作成できるのかをループで回してあげることで、
PCに依存されずに作成が可能になります。

次回はコマンド周りを作っていこうと思います。

3
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?