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
|
#include "UnityPrefix.h"
#include "D3D11Window.h"
#include "D3D11Context.h"
#include "Runtime/GfxDevice/GfxDevice.h"
#include "Runtime/Math/ColorSpaceConversion.h"
#include "Runtime/Misc/QualitySettings.h"
#if UNITY_EDITOR
static D3D11Window* s_CurrentD3D11Window = NULL;
static int s_CurrentD3D11AA = 0;
void SetNoRenderTextureActiveEditor(); // RenderTexture.cpp
void InternalDestroyRenderSurfaceD3D11 (RenderSurfaceD3D11* rs, TexturesD3D11* textures); // RenderTextureD3D11.cpp
bool InitD3D11RenderDepthSurface (RenderDepthSurfaceD3D11& rs, TexturesD3D11* textures, bool sampleOnly);
D3D11Window::D3D11Window (HWND window, int width, int height, DepthBufferFormat depthFormat, int antiAlias)
: GfxDeviceWindow(window, width, height, depthFormat, antiAlias)
, m_SwapChain(NULL)
, m_AntiAlias(0)
{
Reshape (width, height, depthFormat, antiAlias);
}
D3D11Window::~D3D11Window()
{
if (s_CurrentD3D11Window == this)
{
s_CurrentD3D11Window = NULL;
s_CurrentD3D11AA = 0;
}
InternalDestroyRenderSurfaceD3D11 (&m_DepthStencil, NULL);
InternalDestroyRenderSurfaceD3D11 (&m_BackBuffer, NULL);
SAFE_RELEASE (m_SwapChain);
}
bool D3D11Window::Reshape (int width, int height, DepthBufferFormat depthFormat, int antiAlias)
{
if (!GfxDeviceWindow::Reshape(width, height, depthFormat, antiAlias))
return false;
// release old
SAFE_RELEASE(m_DepthStencil.m_Texture);
SAFE_RELEASE(m_DepthStencil.m_SRView);
SAFE_RELEASE(m_DepthStencil.m_DSView);
m_BackBuffer.Reset();
SAFE_RELEASE(m_SwapChain);
// pick supported AA level
if (antiAlias == -1)
antiAlias = GetQualitySettings().GetCurrent().antiAliasing;
while (antiAlias > 1 && !(gGraphicsCaps.d3d11.msaa & (1<<antiAlias)))
--antiAlias;
antiAlias = std::max(antiAlias, 1);
HRESULT hr;
const bool sRGB = GetActiveColorSpace() == kLinearColorSpace;
const bool createSRV = (gGraphicsCaps.d3d11.featureLevel >= kDX11Level10_0); // swap chain can't have SRV before 10.0, it seems
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory (&sd, sizeof(DXGI_SWAP_CHAIN_DESC));
sd.BufferCount = 1;
sd.BufferDesc.Width = m_Width;
sd.BufferDesc.Height = m_Height;
sd.BufferDesc.Format = sRGB ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 0;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
if (createSRV)
sd.BufferUsage |= DXGI_USAGE_SHADER_INPUT;
sd.OutputWindow = m_Window;
sd.SampleDesc.Count = antiAlias;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
hr = GetDXGIFactory()->CreateSwapChain (GetD3D11Device(), &sd, &m_SwapChain);
Assert(SUCCEEDED(hr));
if (FAILED(hr))
{
printf_console ("d3d11: swap chain: w=%i h=%i fmt=%i\n",
sd.BufferDesc.Width, sd.BufferDesc.Height, sd.BufferDesc.Format);
printf_console ("d3d11: failed to create swap chain [0x%x]\n", hr);
m_InvalidState = true;
return !m_InvalidState;
}
//@TODO: false if AA is used?
m_CanUseBlitOptimization = true;
m_AntiAlias = 0;
// Swap Chain
ID3D11Texture2D* backBufferTexture;
hr = m_SwapChain->GetBuffer (0, __uuidof(*backBufferTexture), (void**)&backBufferTexture);
Assert(SUCCEEDED(hr));
// Set the primary backbuffer view
ID3D11RenderTargetView* rtView;
D3D11_RENDER_TARGET_VIEW_DESC rtDesc;
rtDesc.Format = sRGB ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM;
rtDesc.ViewDimension = antiAlias > 1 ? D3D11_RTV_DIMENSION_TEXTURE2DMS : D3D11_RTV_DIMENSION_TEXTURE2D;
rtDesc.Texture2D.MipSlice = 0;
hr = GetD3D11Device()->CreateRenderTargetView (backBufferTexture, &rtDesc, &rtView);
Assert(SUCCEEDED(hr));
// Set the secondary backbuffer view
ID3D11RenderTargetView* rtViewSecondary;
D3D11_RENDER_TARGET_VIEW_DESC rtDescSecondary;
rtDescSecondary.Format = !sRGB ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM;
rtDescSecondary.ViewDimension = antiAlias > 1 ? D3D11_RTV_DIMENSION_TEXTURE2DMS : D3D11_RTV_DIMENSION_TEXTURE2D;
rtDescSecondary.Texture2D.MipSlice = 0;
hr = GetD3D11Device()->CreateRenderTargetView (backBufferTexture, &rtDescSecondary, &rtViewSecondary);
Assert(SUCCEEDED(hr));
// Create shader resource view
if (createSRV)
{
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.Format = sd.BufferDesc.Format;
srvDesc.ViewDimension = antiAlias > 1 ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MipLevels = 1;
ID3D11ShaderResourceView* srView = NULL;
hr = GetD3D11Device()->CreateShaderResourceView (backBufferTexture, &srvDesc, &m_BackBuffer.m_SRView);
Assert (SUCCEEDED(hr));
}
//Pass through flags
m_BackBuffer.m_Texture = backBufferTexture;
m_BackBuffer.SetRTV (0, 0, false, rtView);
m_BackBuffer.SetRTV (0, 0, true, rtViewSecondary);
m_BackBuffer.width = sd.BufferDesc.Width;
m_BackBuffer.height = sd.BufferDesc.Height;
m_BackBuffer.samples = antiAlias;
m_BackBuffer.format = kRTFormatARGB32;
m_BackBuffer.backBuffer = true;
m_BackBuffer.flags = sRGB ? (m_BackBuffer.flags | kSurfaceCreateSRGB) : (m_BackBuffer.flags & ~kSurfaceCreateSRGB);
// Depth stencil
m_DepthStencil.width = m_Width;
m_DepthStencil.height = m_Height;
m_DepthStencil.samples = antiAlias;
m_DepthStencil.depthFormat = depthFormat;
m_DepthStencil.backBuffer = true;
bool dsOk = InitD3D11RenderDepthSurface (m_DepthStencil, NULL, false);
Assert (dsOk);
return !m_InvalidState;
}
void D3D11Window::SetAsActiveWindow ()
{
GfxDevice& device = GetRealGfxDevice();
device.SetRenderTargets(1, &GetBackBuffer(), GetDepthStencil());
device.SetActiveRenderTexture(NULL);
device.SetCurrentWindowSize(m_Width, m_Height);
device.SetInvertProjectionMatrix(false);
s_CurrentD3D11Window = this;
s_CurrentD3D11AA = m_AntiAlias;
}
bool D3D11Window::BeginRendering()
{
if (!GfxDeviceWindow::BeginRendering())
return false;
SetAsActiveWindow ();
GfxDevice& device = GetRealGfxDevice();
if (device.IsInsideFrame())
{
ErrorString ("GUI Window tries to begin rendering while something else has not finished rendering! Either you have a recursive OnGUI rendering, or previous OnGUI did not clean up properly.");
}
device.SetInsideFrame(true);
return true;
}
bool D3D11Window::EndRendering( bool presentContent )
{
if (!GfxDeviceWindow::EndRendering(presentContent))
return false;
GfxDevice& device = GetRealGfxDevice();
Assert(device.IsInsideFrame());
device.SetInsideFrame(false);
s_CurrentD3D11Window = NULL;
HRESULT hr;
if (m_SwapChain && presentContent)
{
HRESULT hr = m_SwapChain->Present (0, 0);
Assert (SUCCEEDED(hr));
}
return true;
}
RenderSurfaceHandle D3D11Window::GetBackBuffer()
{
RenderSurfaceHandle handle;
handle.object = &m_BackBuffer;
return handle;
}
RenderSurfaceHandle D3D11Window::GetDepthStencil()
{
RenderSurfaceHandle handle;
handle.object = &m_DepthStencil;
return handle;
}
#endif
|