-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRenderTarget.cpp
More file actions
234 lines (201 loc) · 6.65 KB
/
RenderTarget.cpp
File metadata and controls
234 lines (201 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "stdafx.h"
#include "RenderTarget.h"
#include "bb_lib\halton.h"
namespace happy
{
RenderTarget::RenderTarget(RenderingContext *context, unsigned width, unsigned height, bool hiresEffects)
: m_pRenderContext(context)
, m_pOutputTarget(nullptr)
{
for (int i = 0; i < MultiSamples; ++i)
{
m_Jitter[i].x = halton(3, i) - 0.5f;
m_Jitter[i].y = halton(2, i) - 0.5f;
}
if (width > 0 && height > 0)
resize(width, height, hiresEffects);
}
void RenderTarget::resize(unsigned width, unsigned height, bool hiresEffects)
{
ID3D11Device& device = *m_pRenderContext->getDevice();
m_ViewPort.Width = (float)width;
m_ViewPort.Height = (float)height;
m_ViewPort.MinDepth = 0.0f;
m_ViewPort.MaxDepth = 1.0f;
m_ViewPort.TopLeftX = 0;
m_ViewPort.TopLeftY = 0;
m_BlurViewPort = m_ViewPort;
if (!hiresEffects)
{
m_BlurViewPort.Width = (float)width / 2.0f;
m_BlurViewPort.Height = (float)height / 2.0f;
}
// Create G-Buffer
vector<unsigned char> texture(width * height * 8, 0);
D3D11_TEXTURE2D_DESC texDesc;
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
texDesc.CPUAccessFlags = 0;
texDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA data;
data.pSysMem = texture.data();
data.SysMemPitch = width * 8;
for (size_t i = 0; i < GBuf_ChannelCount; ++i)
{
texDesc.Width = width;
texDesc.Height = height;
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
if (i == GBuf_DepthStencilIdx)
{
continue;
}
else if (i == GBuf_OcclusionIdx)
{
if (!hiresEffects)
{
texDesc.Width = width / 2;
texDesc.Height = height / 2;
}
texDesc.Format = DXGI_FORMAT_R16_UNORM;
}
else if (i == GBuf_Graphics1Idx)
{
texDesc.Format = DXGI_FORMAT_R11G11B10_FLOAT;
}
else if (i == GBuf_VelocityIdx)
{
texDesc.Format = DXGI_FORMAT_R16G16_FLOAT;
}
m_GraphicsBuffer[i].rtv.Reset();
m_GraphicsBuffer[i].srv.Reset();
ComPtr<ID3D11Texture2D> tex;
THROW_ON_FAIL(device.CreateTexture2D(&texDesc, &data, &tex));
THROW_ON_FAIL(device.CreateRenderTargetView(tex.Get(), nullptr, m_GraphicsBuffer[i].rtv.GetAddressOf()));
THROW_ON_FAIL(device.CreateShaderResourceView(tex.Get(), nullptr, m_GraphicsBuffer[i].srv.GetAddressOf()));
}
ComPtr<ID3D11Texture2D> depthStencilTex;
texDesc.Width = width;
texDesc.Height = height;
texDesc.Format = DXGI_FORMAT_R24G8_TYPELESS;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_DEPTH_STENCIL;
THROW_ON_FAIL(device.CreateTexture2D(&texDesc, &data, &depthStencilTex));
// depth-stencil buffer object
{
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
dsvDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
dsvDesc.Texture2D.MipSlice = 0;
dsvDesc.Flags = 0;
m_pDepthBufferView.Reset();
THROW_ON_FAIL(device.CreateDepthStencilView(depthStencilTex.Get(), &dsvDesc, m_pDepthBufferView.GetAddressOf()));
}
// depth-stencil buffer object (read only)
{
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
dsvDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
dsvDesc.Texture2D.MipSlice = 0;
dsvDesc.Flags = D3D11_DSV_READ_ONLY_DEPTH | D3D11_DSV_READ_ONLY_STENCIL;
m_pDepthBufferViewReadOnly.Reset();
THROW_ON_FAIL(device.CreateDepthStencilView(depthStencilTex.Get(), &dsvDesc, m_pDepthBufferViewReadOnly.GetAddressOf()));
}
// depth buffer view
{
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MipLevels = -1;
m_GraphicsBuffer[GBuf_DepthStencilIdx].srv.Reset();
THROW_ON_FAIL(device.CreateShaderResourceView(depthStencilTex.Get(), &srvDesc, m_GraphicsBuffer[GBuf_DepthStencilIdx].srv.GetAddressOf()));
}
// post buffer
for (unsigned int i = 0; i < 2; ++i)
{
ComPtr<ID3D11Texture2D> tex;
texDesc.Width = width;
texDesc.Height = height;
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
m_PostBuffer[i].rtv.Reset();
m_PostBuffer[i].srv.Reset();
THROW_ON_FAIL(device.CreateTexture2D(&texDesc, &data, &tex));
THROW_ON_FAIL(device.CreateRenderTargetView(tex.Get(), nullptr, m_PostBuffer[i].rtv.GetAddressOf()));
THROW_ON_FAIL(device.CreateShaderResourceView(tex.Get(), nullptr, m_PostBuffer[i].srv.GetAddressOf()));
}
// history buffer
for (unsigned int i = 0; i < 2; ++i)
{
ComPtr<ID3D11Texture2D> tex;
texDesc.Width = width;
texDesc.Height = height;
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
m_HistoryBuffer[i].rtv.Reset();
m_HistoryBuffer[i].srv.Reset();
THROW_ON_FAIL(device.CreateTexture2D(&texDesc, &data, &tex));
THROW_ON_FAIL(device.CreateRenderTargetView(tex.Get(), nullptr, m_HistoryBuffer[i].rtv.GetAddressOf()));
THROW_ON_FAIL(device.CreateShaderResourceView(tex.Get(), nullptr, m_HistoryBuffer[i].srv.GetAddressOf()));
}
}
const RenderingContext* RenderTarget::getContext() const
{
return m_pRenderContext;
}
const bb::mat4& RenderTarget::getView() const
{
return m_View;
}
const bb::mat4& RenderTarget::getProjection() const
{
return m_Projection;
}
const float RenderTarget::getWidth() const
{
return m_ViewPort.Width;
}
const float RenderTarget::getHeight() const
{
return m_ViewPort.Height;
}
const array<float, 4>& RenderTarget::getViewport() const
{
return m_ViewPortArray;
}
void RenderTarget::setViewport(array<float, 4>& viewport)
{
m_ViewPortArray = viewport;
}
void RenderTarget::setView(bb::mat4 &view)
{
m_View = view;
}
void RenderTarget::setProjection(bb::mat4 &projection)
{
m_Projection = projection;
}
void RenderTarget::setOutput(ID3D11RenderTargetView* target)
{
m_pOutputTarget = target;
}
ID3D11RenderTargetView* RenderTarget::historyRTV() const
{
unsigned i = (m_LastUsedHistoryBuffer + 1) % 2;
return m_HistoryBuffer[i].rtv.Get();
}
ID3D11ShaderResourceView* RenderTarget::historySRV() const
{
unsigned i = m_LastUsedHistoryBuffer % 2;
return m_HistoryBuffer[i].srv.Get();
}
ID3D11ShaderResourceView* RenderTarget::currentSRV() const
{
unsigned i = (m_LastUsedHistoryBuffer + 1) % 2;
return m_HistoryBuffer[i].srv.Get();
}
}