이제 본격적으로 Directx11을 이용해서 렌더링을 시작해보자.

DirectX11 을 초기화하기 이전에 우리 프로젝트의 구서을 간단하게 설명해보겠습니다.

Application 클래스는 우리 프로그램의 실행프로그램을 클래스로 래핑 해둔 클래스입니다.

class Application
	{public:
		virtual ~Application() = default;

//  Runs the main engine loopvoid Run();

//  This is where the critical initializations happen (before any rendering or anything else)virtual void Initialize();

//  This is where application-wide updates get executed once per frame.//  RenderPath::Update is also called from here for the active componentvirtual void Update(float dt);

//  This is where application-wide updates get executed in a fixed timestep based manner.//  RenderPath::FixedUpdate is also called from here for the active componentvirtual void FixedUpdate();

//  This is where application-wide rendering happens to offscreen buffers.//  RenderPath::Render is also called from here for the active componentvirtual void Render();

// You need to call this before calling Run() or Initialize() if you want to rendervoid SetWindow(HWND hwnd, UINT width, UINT height);

		UINT GetWidth() { return mWidth; }
		UINT GetHeight() { return mHegith; }
		HWND GetHwnd() { return mHwnd; }

	private:
		bool initialized = false;
		std::unique_ptr<ya::graphics::GraphicsDevice> graphicsDevice;

		HWND mHwnd;
		UINT mWidth;
		UINT mHegith;
	};

그리고 Application 안에 GrapchicDevice(DirectX11) 을 가지고 있습니다.

Graphic device class

#pragma once
#include <d3d11.h>
#include <d3dcompiler.h>

#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dcompiler.lib")

#include "CommonInclude.h"

//https://github.com/kevinmoran/BeginnerDirect3D11
//

namespace ya::graphics
{
	class GraphicDevice_DX11
	{
	public:
		GraphicDevice_DX11();
		~GraphicDevice_DX11();

		void Initialize();
		void Draw();

	private:
		Microsoft::WRL::ComPtr<ID3D11Device> mDevice;
		Microsoft::WRL::ComPtr<ID3D11DeviceContext> mContext;
		Microsoft::WRL::ComPtr<ID3D11Texture2D> mRenderTarget;
		Microsoft::WRL::ComPtr<ID3D11RenderTargetView>	mRenderTargetView;
		Microsoft::WRL::ComPtr<ID3D11Texture2D>			mDepthStencil;
		Microsoft::WRL::ComPtr<ID3D11DepthStencilView>	mDepthStencilView;

		Microsoft::WRL::ComPtr<IDXGISwapChain>	mSwapChain;
		Microsoft::WRL::ComPtr<ID3D11SamplerState> mSamplers;
	};
}

ComPtr 객체는 스마트 포인터 처럼 일일히 delete를 직접 해줄 필요 없이 자동으로 delete해주는 포인터를 담을수 있는 객체이다. Directx11에서는 Comptr을 사용하면 조금더 편하게 프로그래밍을 진행할수 있다.

우선적으로 Dx11에서는 우선 세가지의 오브젝트를 만들어야 합니다.

Device, Immediate Context, Swap Chain

ID3D11Device

Untitled

3D 그래픽 카드와 연결되는 기본 디바이스 객체

ID3D11DeviceContext (레스터라이제이션 파이프라인 객체)

Dx11에서는 디바이스 객체에 직접 접근하지 않고, 이 객체를 이용해서 디바이스에 명령을 내립니다.

Dx11에서는 멀티코어/멀티스레드를 최적화 하기 위해서 Device의 하위에 Context를 분리하여 사용합니다.

IDXGISwapChain