7#include <d3d11sdklayers.h>
12 namespace {
constexpr wchar_t ClassName[]=L
"Rise.Native11.Window";}
15 char detail[64]{};sprintf_s(detail,
" HRESULT=0x%08lx",
static_cast<unsigned long>(result));
16 status=std::string(operation)+detail;
21 LARGE_INTEGER now{};QueryPerformanceCounter(&now);
29 if(!QueryPerformanceFrequency(&
frequency) || !QueryPerformanceCounter(&
started))
return E_FAIL;
32 WNDCLASSEXW type{
sizeof(type)};type.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;type.lpfnWndProc=
Procedure;
33 type.hInstance=
options.
instance;type.hCursor=LoadCursorW(
nullptr,IDC_ARROW);type.lpszClassName=ClassName;
34 type.hIcon=LoadIconW(
options.
instance,MAKEINTRESOURCEW(101));type.hIconSm=type.hIcon;
35 if(!RegisterClassExW(&type) && GetLastError()!=ERROR_CLASS_ALREADY_EXISTS)
return Failure(
"register native window",HRESULT_FROM_WIN32(GetLastError()));
38 if(!AdjustWindowRectEx(&bounds,style,FALSE,0))
return Failure(
"size native window",HRESULT_FROM_WIN32(GetLastError()));
39 int x=CW_USEDEFAULT,y=CW_USEDEFAULT;
44 MONITORINFO monitor{
sizeof(monitor)};
45 if(!GetMonitorInfoW(MonitorFromPoint(POINT{0,0},MONITOR_DEFAULTTOPRIMARY),&monitor))
46 return Failure(
"locate startup monitor",HRESULT_FROM_WIN32(GetLastError()));
47 x=monitor.rcWork.left+(monitor.rcWork.right-monitor.rcWork.left-(bounds.right-bounds.left))/2;
48 y=monitor.rcWork.top+(monitor.rcWork.bottom-monitor.rcWork.top-(bounds.bottom-bounds.top))/2;
52 bounds.right-bounds.left,bounds.bottom-bounds.top,
nullptr,
nullptr,
options.
instance,
this);
54 if(!
window)
return Failure(
"create native window",HRESULT_FROM_WIN32(GetLastError()));
55 RECT client{};GetClientRect(
window,&client);
width=UINT(client.right);
height=UINT(client.bottom);
93 if((samples!=1 && samples!=2 && samples!=4 && samples!=8) || !
device.
SupportsSamples(samples))
return E_INVALIDARG;
108 struct RecoveryGuard {
bool& value;RecoveryGuard(
bool& v):value(v){value=
true;}~RecoveryGuard(){value=
false;}} recovery(
recovering);
125 if(FAILED(hr))
return Failure(
"fullscreen",hr);
127 return Resize(UINT(client.right),UINT(client.bottom));
133 LARGE_INTEGER now{};QueryPerformanceCounter(&now);
138 struct FrameGuard {
bool& active;FrameGuard(
bool& value):active(value){active=
true;}~FrameGuard(){active=
false;}} guard(
inFrame);
143 const auto sample=[&]{LARGE_INTEGER value{};QueryPerformanceCounter(&value);
return double(value.QuadPart)/
frequency.QuadPart;};
144 const double updateStarted=sample();
146 const double updateFinished=sample();
152 const double drawFinished=sample();
154 const double presentFinished=sample();
158 sprintf_s(delay,
"Frame delay: update=%.1f ms, render submission=%.1f ms, Present=%.1f ms; %ux%u MSAA=%u",
159 (updateFinished-updateStarted)*1000,(drawFinished-updateFinished)*1000,
166 Microsoft::WRL::ComPtr<ID3D11InfoQueue> queue;
167 if(SUCCEEDED(
device.
Device()->QueryInterface(IID_PPV_ARGS(&queue))))
169 static std::set<std::string> reported;
170 for(UINT64 i=0;i<queue->GetNumStoredMessagesAllowedByRetrievalFilter();++i)
172 SIZE_T bytes=0;queue->GetMessage(i,
nullptr,&bytes);std::vector<uint8_t> storage(bytes);
173 auto* message=
reinterpret_cast<D3D11_MESSAGE*
>(storage.data());
174 if(SUCCEEDED(queue->GetMessage(i,message,&bytes)) && message->Severity<=D3D11_MESSAGE_SEVERITY_WARNING
175 && reported.size()<100 && reported.insert(message->pDescription).second)
176 callbacks.
report(std::string(
"D3D11 validation: ")+message->pDescription,message->Severity<=D3D11_MESSAGE_SEVERITY_ERROR ? E_FAIL : S_FALSE);
178 queue->ClearStoredMessages();
186 if(FAILED(hr))
return Failure(
"native frame",hr);
190 if(FAILED(shown))
return Failure(
"present game window",shown);
195 const double fpsTime=
Time();
204 while(PeekMessageW(&message,
nullptr,0,0,PM_REMOVE))
207 TranslateMessage(&message);DispatchMessageW(&message);
210 if(
Suspended()){MsgWaitForMultipleObjectsEx(0,
nullptr,100,QS_ALLINPUT,MWMO_INPUTAVAILABLE);
continue;}
211 if(
occluded)MsgWaitForMultipleObjectsEx(0,
nullptr,50,QS_ALLINPUT,MWMO_INPUTAVAILABLE);
240 auto* host=
reinterpret_cast<Window11*
>(GetWindowLongPtrW(handle,GWLP_USERDATA));
241 if(message==WM_NCCREATE)
243 host=
static_cast<Window11*
>(
reinterpret_cast<CREATESTRUCTW*
>(lParam)->lpCreateParams);
244 SetWindowLongPtrW(handle,GWLP_USERDATA,
reinterpret_cast<LONG_PTR
>(host));host->window=handle;
246 if(!host)
return DefWindowProcW(handle,message,wParam,lParam);
249 try {
return host->Message(handle,message,wParam,lParam);}
250 catch(
const std::exception& error){host->Failure(error.what(),E_FAIL);host->Close(1);
return 0;}
251 catch(...){host->Failure(
"unhandled native window callback",E_FAIL);host->Close(1);
return 0;}
256 if(message==WM_NCDESTROY)
258 SetWindowLongPtrW(handle,GWLP_USERDATA,0);
window=
nullptr;
running=
false;
259 return DefWindowProcW(handle,message,wParam,lParam);
267 const LRESULT result=
callbacks.
message(handle,message,wParam,lParam,handled);
if(handled)
return result;
274 case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
279 if(wParam==VK_F10 && !GetMenu(handle))
return 0;
281 case WM_ERASEBKGND:
return 1;
282 case WM_PAINT:{PAINTSTRUCT paint{};BeginPaint(
window,&paint);EndPaint(
window,&paint);
return 0;}
284 case WM_DESTROY:
running=
false;
return 0;
286 return DefWindowProcW(handle,message,wParam,lParam);
SCRIPT_CONTROL SCRIPT_STATE const void int
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
UINT SelectSamples(UINT requested) const
HRESULT AttachWindow(HWND window, UINT width, UINT height, UINT requestedSamples)
bool SupportsSamples(UINT count) const
ID3D11DeviceContext * Context() const
IDXGISwapChain1 * SwapChain() const
ID3D11Device * Device() const
HRESULT Present(UINT syncInterval=1)
const std::string & Status() const
HRESULT SetFullscreen(bool fullscreen)
HRESULT Resize(UINT width, UINT height, UINT requestedSamples)
HRESULT Create(const DeviceOptions11 &options={})
HRESULT RemovalReason() const
const SceneDepth11 & Depth() const
const std::string & Status() const
HRESULT Initialize(Device11 &device, const std::filesystem::path &shaderDirectory={})
static FrameProfiler11 & Get()
const D3D11_TEXTURE2D_DESC & Description() const
LRESULT Message(HWND, UINT, WPARAM, LPARAM)
HRESULT Failure(const char *operation, HRESULT result)
void StopCallbacksAndWindow()
HRESULT Create(const WindowOptions11 &options, Callbacks callbacks)
HRESULT SetSamples(UINT samples)
HRESULT Resize(UINT width, UINT height)
static LRESULT CALLBACK Procedure(HWND, UINT, WPARAM, LPARAM)
HRESULT SetFullscreen(bool value)
bool IsDeviceRemovalError11(HRESULT result)
std::string MemoryDiagnostics11(ID3D11Device *device)
std::string DriverDiagnostics11(ID3D11Device *device)
std::function< HRESULT(UINT, UINT)> resize
std::function< void()> destroy
std::function< void(double, float)> move
std::function< LRESULT(HWND, UINT, WPARAM, LPARAM, bool &)> message
std::function< HRESULT()> presented
std::function< void(const std::string &, HRESULT)> report
std::function< HRESULT()> create
std::function< void(UINT, bool, bool)> keyboard
std::function< void()> releaseSizeDependent
std::function< HRESULT(double, float)> draw