4
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 事始め まとめ

Posted at

今回は、今までのまとめとして描画の実行をやって行きます。
今まで作ったクラスをまとめるためのクラスを作ります。

Union.h
# include <d3d12.h>

class Window;
class Device;
class Queue;
class List;
class Swap;
class Render;
class Depth;
class Fence;
class Root;
class Pipe;
class Constant;
class Triangle;

class Union
{
public:
   // デストラクタ
   ~Union();

   // インスタンス変数の取得
   static Union& Get(void) {
      static Union instance;
      return instance;
   }
   // クラスのインスタンス化
   void Create(void);
   // メッセージの確認
   bool CheckMsg(void);
   // 描画準備
   void Set(void);
   // 実行
   void Do(void);
private:
   // コンストラクタ
   Union();
   Union(const Union&) {
   }
   void operator=(const Union&) {
   }
   // ビューポートのセット
   void ViewPort(void);
   // シザーのセット
   void Scissor(void);
   // バリアのセット
   void Barrier(D3D12_RESOURCE_STATES befor, D3D12_RESOURCE_STATES affter);

   // メッセージ
   MSG msg;
   // ビューポート
   D3D12_VIEWPORT viewPort;
   // シザー
   RECT scissor;
   // バリア
   D3D12_RESOURCE_BARRIER barrier;

   // ウィンドウ
   Window* win;
   // デバイス
   Device* dev;
   // コマンドキュー
   Queue* queue;
   // コマンドリスト
   List* list;
   // スワップチェイン
   Swap* swap;
   // レンダーターゲット
   Render* render;
   // 深度ステンシル
   Depth* depth;
   // フェンス
   Fence* fence;
   // ルートシグネチャ
   Root* root;
   // パイプライン
   Pipe* pipe;
   // 定数バッファ
   Constant* constant;
   // 三角形
   Triangle* tri;
};
Union.cpp
# include "Union.h"
# include "Window.h"
# include "Device.h"
# include "Queue.h"
# include "List.h"
# include "Swap.h"
# include "Render.h"
# include "Depth.h"
# include "Fence.h"
# include "Root.h"
# include "Pipe.h"
# include "Constant.h"
# include "Triangle.h"

# pragma comment(lib, "d3d12.lib")

// コンストラクタ
Union::Union()
{
   msg = {};
   viewPort = {};
   scissor = {};
   barrier = {};
}

// デストラクタ
Union::~Union()
{
   delete tri;
   delete constant;
   delete pile;
   delete Root;
   delete fence;
   delete depth;
   delete render;
   delete swap;
   delete list;
   delete queue;
   delete dev;
   delete win;
}
// クラスのインスタンス化
void Union::Create(void)
{
   win      = new Window();
   dev      = new Device();
   queue    = new Queue(dev);
   list     = new List(dev);
   swap     = new Swap(win, queue);
   render   = new Render(dev, list, swap);
   depth    = new Depth(win, dev, list);
   fence    = new Fence(dev, queue);
   root     = new Root(dev);
   root->ComVertex(L"Shader.hlsl", "VS");
   root->ComPixel(L"Shader.hlsl", "PS");
   pipe     = new Pipe(dev, swap, root);
   constant = new Constant(dev, list);
   tri      = new Tri(dev, list);

   ViewPort();
   Scissor();
}
// ビューポートのセット
void Union::ViewPort(void)
{
   viewPort.Height   = static_cast<FLOAT>(win->GetY());
   viewPort.MaxDepth = 1.0f;
   viewPort.MinDepth = 0.0f;
   viewPort.TopLeftX = 0.0f;
   viewPort.TopLeftY = 0.0f;
   viewPort.Width    = static_cast<FLOAT>(win->GetX());
}
// シザーのセット
void Union::Scissor(void)
{
   scissor.bottom = static_cast<LONG>(win->GetY());
   scissor.left   = 0;
   scissor.right  = static_cast<LONG>(win->GetX());
   scissor.top    = 0;
}
// バリアのセット
void Union::Barrier(D3D12_RESOURCE_STATES befor, D3D12_RESOURCE_STATES affter)
{
   barrier.Type                   = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
   barrier.Flags                  = D3D12_RESOURCE_BARRIER_FLAG_NONE;
   barrier.Transition.pResource   = render->GetResource(swap->Get()->GetCurrentBackBufferIndex());
   barrier.Transition.StateBefore = befor;
   barrier.Transition.StateAfter  = affter;
   barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_FLAG_NONE;

   //バリア設置
   list->GetList()->ResourceBarrier(1, &barrier);
}
// メッセージの確認
bool Union::CheckMsg(void)
{
   if (msg.message == WM_QUIT)
   {
      return false;
   }
   else
   {
      //呼び出し側スレッドが所有しているウィンドウに送信されたメッセージの保留されている物を取得
      if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
      {
         //仮想キーメッセージを文字メッセージに変換
         TranslateMessage(&msg);
         //1つのウィドウプロシージャにメッセージを送出する
         DispatchMessage(&msg);
      }
   }

   return true;
}
// 描画準備
void Union::Set(void)
{
   constant->UpDataWVP();

   list->GetAllo()->Reset();
   list->GetList()->Reset(list->GetAllo(), Pipe->Get());

   list->GetList()->SetGraphicsRootSignature(root->Get());

   list->GetList()->SetPipelineState(pipe->Get());

   constant->SetConstant();

   list->GetList()->RSSetViewports(1, &viewPort);

   list->GetList()->RSSetScissorRects(1, &scissor);

   Barrier(D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET);

   render->SetRender(depth->GetHeap());

   depth->SetDepth();

   tri->Draw();
}

/*本来はSetとDo関数の間で描画をする*/

// 実行
void Union::Do(void)
{	
   Barrier(D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);

   list->GetList()->Close();

   //コマンド実行.
   ID3D12CommandList* ppCmdLists[] = { list->GetList() };
   queue->Get()->ExecuteCommandLists(_countof(ppCmdLists), ppCmdLists);

   swap->Present();

   fence->Wait();
}
Main.cpp
# include "Union.h"
# include <iostream>

int main()
{
   Union::Get().Create();

   while (Union::Get().CheckMsg())
   {
      Union::Get().Set();

      Union::Get().Do();
   }

   return 0;
}

以上で、まとめの終了です。
次はテクスチャマッピングをやります。

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