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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
#include "UnityPrefix.h"
#include "D3D9Window.h"
#include "GfxDeviceD3D9.h"
#include "RenderTextureD3D.h"
#include "Runtime/Misc/QualitySettings.h"
#include "Runtime/Threads/ThreadSharedObject.h"
#include "Runtime/GfxDevice/GfxDevice.h"
#if UNITY_EDITOR
bool IsD3D9DeviceLost();
void SetD3D9DeviceLost( bool lost );
static bool s_OldHasDepthFlag = false;
static D3D9Window* s_CurrentD3DWindow = NULL;
static int s_CurrentD3DFSAALevel = 0;
int GetCurrentD3DFSAALevel() { return s_CurrentD3DFSAALevel; }
void SetNoRenderTextureActiveEditor(); // RenderTexture.cpp
D3D9Window::D3D9Window(IDirect3DDevice9* device, HWND window, int width, int height, DepthBufferFormat depthFormat, int antiAlias )
: GfxDeviceWindow(window, width, height, depthFormat, antiAlias)
, m_SwapChain(NULL)
, m_FSAALevel(0)
{
m_Device = device;
Reshape( width, height, depthFormat, antiAlias );
}
D3D9Window::~D3D9Window()
{
if( s_CurrentD3DWindow == this )
{
s_CurrentD3DWindow = NULL;
s_CurrentD3DFSAALevel = 0;
}
DestroyRenderSurfaceD3D9(&m_DepthStencil);
DestroyRenderSurfaceD3D9(&m_BackBuffer);
SAFE_RELEASE(m_SwapChain);
}
bool D3D9Window::Reshape( int width, int height, DepthBufferFormat depthFormat, int antiAlias )
{
if(GfxDeviceWindow::Reshape(width, height, depthFormat, antiAlias)==false)return false;
#if ENABLE_D3D_WINDOW_LOGGING
printf_console("D3Dwindow %x Reshape %ix%i d=%i aa=%i\n", this, width, height, depthFormat, antiAlias);
#endif
// release old
m_DepthStencil.Release();
m_BackBuffer.Release();
SAFE_RELEASE(m_SwapChain);
HRESULT hr;
// Choose presentation params
if( antiAlias == -1 )
antiAlias = GetQualitySettings().GetCurrent().antiAliasing;
D3DDISPLAYMODE mode;
hr = GetD3DObject()->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &mode );
D3DPRESENT_PARAMETERS params;
ZeroMemory( ¶ms, sizeof(params) );
params.BackBufferWidth = m_Width;
params.BackBufferHeight = m_Height;
params.BackBufferCount = 1;
params.hDeviceWindow = m_Window;
params.FullScreen_RefreshRateInHz = 0;
params.Windowed = TRUE;
params.SwapEffect = D3DSWAPEFFECT_COPY;
params.BackBufferFormat = D3DFMT_A8R8G8B8;
params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
params.EnableAutoDepthStencil = FALSE;
GetD3DFormatCaps()->FindBestPresentationParams( width, height, mode.Format, true, 0, antiAlias, params );
if( params.MultiSampleType != D3DMULTISAMPLE_NONE ) {
params.SwapEffect = D3DSWAPEFFECT_DISCARD;
m_CanUseBlitOptimization = false;
} else {
m_CanUseBlitOptimization = true;
}
m_FSAALevel = (params.MultiSampleType == D3DMULTISAMPLE_NONMASKABLE) ? params.MultiSampleQuality : params.MultiSampleType;
hr = m_Device->CreateAdditionalSwapChain( ¶ms, &m_SwapChain );
if( FAILED(hr) ) {
printf_console( "d3d: swap chain: swap=%i vsync=%x w=%i h=%i fmt=%i bbcount=%i dsformat=%i pflags=%x\n",
params.SwapEffect, params.PresentationInterval,
params.BackBufferWidth, params.BackBufferHeight, params.BackBufferFormat, params.BackBufferCount,
params.AutoDepthStencilFormat, params.Flags );
printf_console( "d3d: failed to create swap chain [%s]\n", GetD3D9Error(hr) );
m_InvalidState = true;
return !m_InvalidState;
}
IDirect3DSurface9* backBuffer = NULL;
hr = m_SwapChain->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer );
if( FAILED(hr) ) {
AssertString( "Failed to get back buffer for D3DWindow" );
m_SwapChain->Release();
m_SwapChain = NULL;
m_InvalidState = true;
return !m_InvalidState;
}
m_BackBuffer.backBuffer = true;
m_DepthStencil.backBuffer = true;
m_BackBuffer.m_Surface = backBuffer;
m_BackBuffer.width = params.BackBufferWidth;
m_BackBuffer.height = params.BackBufferHeight;
m_BackBuffer.format = kRTFormatARGB32;
// Depth format
bool needsDepth = false;
m_DepthStencilFormat = D3DFMT_UNKNOWN;
switch( depthFormat ) {
case kDepthFormatNone:
needsDepth = false;
m_DepthStencilFormat = D3DFMT_UNKNOWN;
break;
case kDepthFormat16:
needsDepth = true;
m_DepthStencilFormat = D3DFMT_D16;
break;
case kDepthFormat24:
needsDepth = true;
m_DepthStencilFormat = D3DFMT_D24S8;
break;
default:
ErrorString("Unknown depth format");
}
if( needsDepth )
{
D3D9DepthStencilTexture depthStencil = CreateDepthStencilTextureD3D9 (m_Device, m_Width, m_Height, m_DepthStencilFormat, params.MultiSampleType, params.MultiSampleQuality, FALSE);
m_Device->SetRenderState (D3DRS_ZENABLE, TRUE);
if (!depthStencil.m_Surface)
{
AssertString( "Failed to create depth/stencil for D3DWindow" );
m_SwapChain->Release();
m_SwapChain = NULL;
REGISTER_EXTERNAL_GFX_DEALLOCATION(m_BackBuffer.m_Surface);
m_BackBuffer.m_Surface->Release();
m_BackBuffer.m_Surface = NULL;
m_InvalidState = true;
return !m_InvalidState;
}
m_DepthStencil.m_Surface = depthStencil.m_Surface;
m_DepthStencil.m_Texture = depthStencil.m_Texture;
m_DepthStencil.width = m_Width;
m_DepthStencil.height = m_Height;
m_DepthStencil.depthFormat = depthFormat;
}
return !m_InvalidState;
}
void D3D9Window::SetAsActiveWindow ()
{
GetRealGfxDevice().SetRenderTargets(1, &GetBackBuffer(), GetDepthStencil());
GetRealGfxDevice().SetActiveRenderTexture(NULL);
GetRealGfxDevice().SetCurrentWindowSize(m_Width, m_Height);
GetRealGfxDevice().SetInvertProjectionMatrix(false);
s_OldHasDepthFlag = g_D3DHasDepthStencil;
g_D3DHasDepthStencil = (m_DepthStencil.m_Surface != NULL);
s_CurrentD3DWindow = this;
s_CurrentD3DFSAALevel = m_FSAALevel;
// not entirely correct but better not touch anything if we don't have depth
if(m_DepthStencil.m_Surface != NULL)
g_D3DDepthStencilFormat = m_DepthStencilFormat;
}
bool D3D9Window::BeginRendering()
{
if (GfxDeviceWindow::BeginRendering())
{
HRESULT hr;
// Handle lost devices
if (!GetRealGfxDevice().IsValidState())
{
return false;
}
// begin scene
if (IsD3D9DeviceLost())
{
ErrorString ("GUI Window tries to begin rendering while D3D9 device is lost!");
}
GfxDeviceD3D9& device = static_cast<GfxDeviceD3D9&>( 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.");
}
m_Device->BeginScene();
SetAsActiveWindow ();
device.SetInsideFrame(true);
return true;
}
else
{
#if ENABLE_D3D_WINDOW_LOGGING
printf_console("D3Dwindow %ix%i BeginRendering: invalid state\n", m_Width, m_Height);
#endif
return false;
}
}
bool D3D9Window::EndRendering( bool presentContent )
{
if(GfxDeviceWindow::EndRendering(presentContent))
{
g_D3DHasDepthStencil = s_OldHasDepthFlag;
s_CurrentD3DWindow = NULL;
s_CurrentD3DWindow = 0;
if( IsD3D9DeviceLost() )
return false;
HRESULT hr;
GfxDeviceD3D9& device = static_cast<GfxDeviceD3D9&>( GetRealGfxDevice() );
Assert( device.IsInsideFrame() );
hr = m_Device->EndScene();
device.SetInsideFrame(false);
if( m_SwapChain && presentContent )
{
hr = m_SwapChain->Present( NULL, NULL, NULL, NULL, 0 );
device.PushEventQuery();
// When D3DERR_DRIVERINTERNALERROR is returned from Present(),
// the application can do one of the following, try recovering just as
// from the lost device.
if( hr == D3DERR_DEVICELOST || hr == D3DERR_DRIVERINTERNALERROR )
{
SetD3D9DeviceLost( true );
return false;
}
}
return true;
}
else
{
return false;
}
}
RenderSurfaceHandle D3D9Window::GetBackBuffer()
{
RenderSurfaceHandle handle;
handle.object = &m_BackBuffer;
return handle;
}
RenderSurfaceHandle D3D9Window::GetDepthStencil()
{
RenderSurfaceHandle handle;
handle.object = &m_DepthStencil;
return handle;
}
#endif
|