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
|
Result Direct3D::Init(Config* pConfig,
char* pcWindowTitle,
HWND hOwnWindow, // = NULL
HICON hIcon, // = NULL
bool bShowCursor) // = false
{
m_bInitialized = false;
m_bOwnWindow = false;
m_hWindow = NULL;
m_pIDXGIFactory = NULL;
m_pD3D11Device = NULL;
m_pD3DImmediateContext = NULL;
m_pSwapChain = NULL;
m_pRenderTargetView = NULL;
m_pDepthStencilView = NULL;
m_pDepthStencilBuffer = NULL;
m_pBackBuffer = NULL;
m_pStandardEffect = NULL;
m_pTmpEffect = NULL;
m_bMulitSampling = false;
if(!hIcon) hIcon = LoadIcon(NoxetEngine::g_DLLHandle, MAKEINTRESOURCE(IDI_ICON1));
HRESULT hResult;
WNDCLASSEX WindowClass = {sizeof(WNDCLASSEX), CS_CLASSDC, WindowProc, 0, 0,
NoxetEngine::g_DLLHandle, hIcon, LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)),
NULL, NULL, "Direct3D Window Class", NULL};
if(!pConfig) LOG_ERROR_NULL_POINTER("pConfig", NXE_ERROR);
m_vScreenSize.x = (float)(pConfig->Direct3D.VideoMode.Width);
m_vScreenSize.y = (float)(pConfig->Direct3D.VideoMode.Height);
m_bOwnWindow = hOwnWindow != NULL;
if(m_bOwnWindow) m_hWindow = hOwnWindow;
if(!g_hWindow)
{
if(!m_bOwnWindow)
{
if(!RegisterClassEx(&WindowClass))
{
// Fehler
LOG_ERROR("Could not register window class", NXE_ERROR);
}
int iWidth = pConfig->Direct3D.VideoMode.Width;
int iHeight = pConfig->Direct3D.VideoMode.Height;
RECT Rect;
SetRect(&Rect, 0, 0, iWidth, iHeight);
AdjustWindowRect(&Rect, WS_VISIBLE | WS_OVERLAPPEDWINDOW, FALSE);
iWidth = Rect.right - Rect.left;
iHeight = Rect.bottom - Rect.top;
// Fenster erstellen
m_hWindow = CreateWindow("Direct3D Window Class",
pcWindowTitle,
WS_VISIBLE | WS_OVERLAPPEDWINDOW,
GetSystemMetrics(SM_CXSCREEN) / 2 - iWidth / 2,
GetSystemMetrics(SM_CYSCREEN) / 2 - iHeight / 2,
iWidth,
iHeight,
NULL,
NULL,
NoxetEngine::g_DLLHandle,
NULL);
if(!m_hWindow)
{
ERROR_BOX("Could not create window", "Error");
LOG_ERROR("Could not create window", NXE_ERROR);
}
}
g_hWindow = m_hWindow;
}
else
m_hWindow = g_hWindow;
UINT createDeviceFlags = 0;
#ifdef _DEBUG
#ifdef D3D11_DEVICE_DEBUG
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
#endif
hResult = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&m_pIDXGIFactory);
if(FAILED(hResult))
LOG_ERROR_DIRECTX("CreateDXGIFactory1", hResult, NXE_ERROR);
IDXGIAdapter1* pAdapter;
hResult = m_pIDXGIFactory->EnumAdapters1(pConfig->Direct3D.iAdapter, &pAdapter);
if(FAILED(hResult))
LOG_ERROR_DIRECTX("EnumAdapters1", hResult, NXE_ERROR);
D3D_FEATURE_LEVEL featureLevel;
hResult = D3D11CreateDevice(
pAdapter,
D3D_DRIVER_TYPE_UNKNOWN,
0,
createDeviceFlags,
0, 0,
D3D11_SDK_VERSION,
&m_pD3D11Device,
&featureLevel,
&m_pD3DImmediateContext);
if(FAILED(hResult))
{
ERROR_BOX("D3D11CreateDevice Failed.", "Error");
SAFE_RELEASE(pAdapter);
LOG_ERROR_DIRECTX("D3D11CreateDevice", hResult, NXE_ERROR);
}
if(featureLevel != D3D_FEATURE_LEVEL_11_0)
{
LOG_WARNING("Direct3D Feature Level 11 unsupported");
if(featureLevel != D3D_FEATURE_LEVEL_10_1 && featureLevel != D3D_FEATURE_LEVEL_10_0)
{
ERROR_BOX("DirectX 10.0 not supported!", "Error");
LOG_ERROR_MESSAGE("DirectX 10.0 not supported!");
SAFE_RELEASE(pAdapter);
return NXE_ERROR;
}
}
if(!m_pD3D11Device || !m_pD3DImmediateContext)
{
ERROR_BOX("D3D11CreateDevice Failed.", "Error");
SAFE_RELEASE(pAdapter);
LOG_ERROR("D3D11CreateDevice Failed.", NXE_ERROR);
}
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(DXGI_SWAP_CHAIN_DESC));
sd.BufferDesc.Width = (int)m_vScreenSize.x;
sd.BufferDesc.Height = (int)m_vScreenSize.y;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferCount = 1;
sd.OutputWindow = m_hWindow;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.Flags = /*DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH*/0;
sd.Windowed = pConfig->Direct3D.bWindowed;
if(pConfig->Direct3D.MultiSamplingSamples == 0)
{
pConfig->Direct3D.MultiSamplingSamples = 1;
pConfig->Direct3D.MultiSamplingQuality = 0;
}
sd.SampleDesc.Count = pConfig->Direct3D.MultiSamplingSamples;
sd.SampleDesc.Quality = pConfig->Direct3D.MultiSamplingQuality;
if(pConfig->Direct3D.bVSync)
{
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
}
else
{
sd.BufferDesc.RefreshRate.Numerator = 0;
sd.BufferDesc.RefreshRate.Denominator = 1;
}
hResult = m_pIDXGIFactory->CreateSwapChain(m_pD3D11Device, &sd, &m_pSwapChain);
if(FAILED(hResult))
LOG_ERROR_DIRECTX("m_pIDXGIFactory->CreateSwapChain", hResult, NXE_ERROR);
m_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&m_pBackBuffer);
m_pD3D11Device->CreateRenderTargetView(m_pBackBuffer, 0, &m_pRenderTargetView);
D3D11_TEXTURE2D_DESC depthStencilDesc;
ZeroMemory(&depthStencilDesc, sizeof(D3D11_TEXTURE2D_DESC));
depthStencilDesc.Width = (int)m_vScreenSize.x;
depthStencilDesc.Height = (int)m_vScreenSize.y;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.SampleDesc.Count = pConfig->Direct3D.MultiSamplingSamples;
depthStencilDesc.SampleDesc.Quality = pConfig->Direct3D.MultiSamplingQuality;
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;
hResult = m_pD3D11Device->CreateTexture2D(&depthStencilDesc, 0, &m_pDepthStencilBuffer);
if(FAILED(hResult))
LOG_ERROR_DIRECTX("m_pD3D11Device->CreateTexture2D", hResult, NXE_ERROR);
hResult = m_pD3D11Device->CreateDepthStencilView(m_pDepthStencilBuffer, 0, &m_pDepthStencilView);
if(FAILED(hResult))
LOG_ERROR_DIRECTX("m_pD3D11Device->CreateDepthStencilView", hResult, NXE_ERROR);
m_pD3DImmediateContext->OMSetRenderTargets(1, &m_pRenderTargetView, m_pDepthStencilView);
ZeroMemory(&m_ScreenViewport, sizeof(D3D11_VIEWPORT));
m_ScreenViewport.TopLeftX = 0;
m_ScreenViewport.TopLeftY = 0;
m_ScreenViewport.Width = m_vScreenSize.x;
m_ScreenViewport.Height = m_vScreenSize.y;
m_ScreenViewport.MinDepth = 0.0f;
m_ScreenViewport.MaxDepth = 1.0f;
m_pD3DImmediateContext->RSSetViewports(1, &m_ScreenViewport);
m_OwnConfig = *pConfig;
NXETerminal::DebugWrite("Init Direct3D 11 (11.0) with IDXGIFactory 1.1");
NXETerminal::DebugWrite("Create rasterizer states...");
m_Fill = D3D11_FILL_SOLID;
m_Cull = D3D11_CULL_BACK;
m_bMulitSampling = pConfig->Direct3D.MultiSamplingSamples > 1 ? true:false;
m_pTmpEffect = new Effect;
SAFE_RELEASE(pAdapter);
ShowCursor(bShowCursor);
string sDummyShader = "struct VertexIn"
"{"
"float3 Pos : POSITION;"
"float3 Normal : NORMAL;"
"float4 Tangent : TANGENT;"
"float2 Tex : TEXCOORD;"
"};"
"struct VertexOut"
"{"
"float4 Pos : SV_POSITION;"
"};"
"VertexOut VS(VertexIn vin)"
"{"
"VertexOut vout;"
"vout.Pos.xyz = vin.Pos;"
"vout.Pos.w = 1.0f;"
"return vout;"
"}"
"float4 PS(VertexOut pin) : SV_Target"
"{"
"return float4(1.0f,1.0f,1.0f,1.0f);"
"}"
"technique11 DummyTech"
"{"
"pass P0"
"{"
"SetVertexShader( CompileShader( vs_5_0, VS() ) );"
"SetGeometryShader( NULL );"
"SetPixelShader( CompileShader( ps_5_0, PS() ) );"
"}"
"}";
int s = strlen(sDummyShader.c_str());
m_pTmpEffect->InitFromString(sDummyShader, s);
return NXE_OK;
}
|