• Home
  • About
    • 게임 개발자 유정룡 photo

      게임 개발자 유정룡

      포트폴리오

    • Learn More
    • Email
    • Github
    • Bitbucket
  • Projects
    • All Projects
    • All Tags

Direct3D11 공부 41일차(Multiple Render Target)

03 Aug 2021

Reading time ~2 minutes

Multiple Render Target

Shader연산을 한번의 Pass에 여러가지 Texture를 연산하는 방법이다.

Shader안에 Structure를 만들어 값을 받아온다.

Shader

103_Mrt.fx
#include "00_Global.fx"
#include "00_Light.fx"

float2 PixelSize;

struct VertexOutput
{
    float4 Position : SV_Position;
    float2 Uv : UV;
};

VertexOutput VS(float4 Position : Position)
{
    VertexOutput output;
    
    output.Position = Position;
    output.Uv.x = Position.x * 0.5f + 0.5f;
    output.Uv.y = -Position.y * 0.5f + 0.5f;
    
    return output;
}

struct PixelOutput
{
    float4 Color0 : SV_Target0;
    float4 Color1 : SV_Target1;
    float4 Color2 : SV_Target2;
};

PixelOutput PS_Diffuse(VertexOutput input)
{
    PixelOutput output;
    
    output.Color0 = DiffuseMap.Sample(LinearSampler, input.Uv);
    output.Color1 = 1 - DiffuseMap.Sample(LinearSampler, input.Uv);
    
    float3 color = DiffuseMap.Sample(LinearSampler, input.Uv).rgb;
    float average = (color.r + color.g + color.b) / 3;
    output.Color2 = float4(average, average, average, 1.0f);
    
    return output;
}

float4 PS(VertexOutput input) : SV_Target
{
    return DiffuseMap.Sample(LinearSampler, input.Uv);;
}

technique11 T0
{
    P_VP(P0, VS, PS_Diffuse)
    P_VP(P1, VS, PS)
}

PixelOutput이라는 구조체를 만들어서 값을 받아온다.

RenderTarget

void RenderTarget::PreRender(RenderTarget ** target, UINT count, DepthStencil * depthStencil)
{
	vector<ID3D11RenderTargetView*> rtvs;
	for (UINT i = 0; i < count; i++)
	{
		ID3D11RenderTargetView* rtv = target[i]->RTV();
		rtvs.push_back(rtv);

		D3D::GetDC()->ClearRenderTargetView(rtv, Color(0, 0, 0, 1));
	}
	D3D::GetDC()->ClearDepthStencilView(depthStencil->DSV(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1, 0);
	D3D::GetDC()->OMSetRenderTargets(rtvs.size(), &rtvs[0], depthStencil->DSV());
}

랜더 타겟의 갯수를 받아온다. 함수를 추가해주자

MTR Demo

MrtDemo.cpp
void MrtDemo::Initialize()
{
	.
	.
	.
	renderTarget = new RenderTarget((UINT)width, (UINT)height);
	mrt[0] = new RenderTarget((UINT)width, (UINT)height);
	mrt[1] = new RenderTarget((UINT)width, (UINT)height);
	mrt[2] = new RenderTarget((UINT)width, (UINT)height);
	depthStencil = new DepthStencil((UINT)width, (UINT)height);
	viewport = new Viewport(D3D::Width(), D3D::Height());

	render2D = new Render2D();
	render2D->GetTransform()->Scale(355.0f, 200.0f, 1);
	render2D->GetTransform()->Position(200.0f ,150.0f , 0);

	postEffect = new PostEffect(L"103_Mrt.fxo");
	postEffect->SRV(renderTarget->SRV());
}
void MrtDemo::Update()
{
	.
	.
	.
	// MRT Rendering
	{
		RenderTarget::PreRender(mrt, 3, depthStencil);
		viewport->RSSetViewport();

		postEffect->SRV(renderTarget->SRV());
		postEffect->Render();
	}
}
void MrtDemo::PostRender()
{
	static UINT index = 0;
	ImGui::InputInt("Mrt Index", (int*)&index);
	index %= 3;

	postEffect->SRV(mrt[index]->SRV());
	postEffect->Render();
	render2D->SRV(renderTarget->SRV());
	render2D->Render();
}

위에 쉐이더에 있는 구조체 대로 값을 받아오고 그 값대로 설정해준다.

  • Color0
  • Color1
  • Color2


DirectX Share Tweet +1