Rise
The Vieneo Province
Loading...
Searching...
No Matches
ClientRuntime11.cpp
Go to the documentation of this file.
1#include "ClientRuntime11.h"
2#include <string>
3#include <set>
4#include <mutex>
5#include <intrin.h>
6namespace Rendering
7{
8 namespace {Window11* host=nullptr;RenderDevice* renderer=nullptr;}
9 void AttachClientRuntime(Window11* window,RenderDevice* device){host=window;renderer=device;}
11 Window11* ClientHost(){return host;}
12 HWND ClientWindow(){return host ? host->Handle() : nullptr;}
13 HINSTANCE ClientInstance(){return GetModuleHandleW(nullptr);}
14 double ClientTime(){return host ? host->Time() : 0;}
15 double ClientAbsoluteTime(){LARGE_INTEGER now{},frequency{};QueryPerformanceFrequency(&frequency);QueryPerformanceCounter(&now);return double(now.QuadPart)/double(frequency.QuadPart);}
16 float ClientFPS(){return host ? host->FPS() : 0;}
17 bool ClientWindowed(){return !host || !host->Fullscreen();}
18 HRESULT ClientToggleFullscreen(){return host ? host->SetFullscreen(!host->Fullscreen()) : E_UNEXPECTED;}
19 const wchar_t* ClientDeviceStats()
20 {
21 static std::wstring text;
22 text=L"Direct3D 11 - ";text+=host && host->Device().Device() ? host->Device().Adapter().Description : L"initializing";return text.c_str();
23 }
24 const wchar_t* ClientFrameStats()
25 {
26 static wchar_t text[256];const auto* size=ClientBackBufferDescription();
27 swprintf_s(text,L"%.1f fps (%ux%u)",ClientFPS(),size->Width,size->Height);
28 return text;
29 }
31 {
32 static SurfaceDescription description;
33 if(host){const auto& depth=host->Device().Depth().Description();description={ImageFormat::RGBA8,depth.Width,depth.Height,depth.SampleDesc.Count,depth.SampleDesc.Quality};}
34 return &description;
35 }
36 const wchar_t* ErrorString(HRESULT code)
37 {
38 static thread_local wchar_t text[1024];
39 if(!FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,nullptr,DWORD(code),0,text,UINT(std::size(text)),nullptr))
40 swprintf_s(text,L"HRESULT 0x%08X",UINT(code));return text;
41 }
42 HRESULT TraceFailure(const wchar_t* operation,HRESULT code)
43 {
44 wchar_t text[1400];swprintf_s(text,L"%s: %s\n",operation ? operation : L"Rendering",ErrorString(code));OutputDebugStringW(text);
45 // Keep bounded call-site evidence when no debugger is attached. A
46 // failed draw should remain diagnosable in an alpha tester's log.
47 const auto caller=reinterpret_cast<uintptr_t>(_ReturnAddress())-reinterpret_cast<uintptr_t>(GetModuleHandleW(nullptr));
48 static std::mutex mutex;static std::set<std::pair<uintptr_t,HRESULT>> reported;
49 const std::lock_guard<std::mutex> guard(mutex);
50 if(reported.size()<256 && reported.emplace(caller,code).second)
51 {
52 wchar_t path[80];swprintf_s(path,L"render-errors-%lu.log",GetCurrentProcessId());FILE* output=nullptr;
53 if(_wfopen_s(&output,path,L"a")==0)
54 {fprintf(output,"caller RVA 0x%08llX HRESULT 0x%08lX: %ls",static_cast<unsigned long long>(caller),static_cast<unsigned long>(code),text);fclose(output);}
55 }
56 return code;
57 }
58 HRESULT LoadTextureResource(RenderDevice* owner,HMODULE module,LPCWSTR id,RenderTexture** result,UINT levels)
59 {
60 if(!owner || !result)return E_INVALIDARG;*result=nullptr;
61 HRSRC resource=FindResourceW(module,id,RT_RCDATA);
62 const bool bitmap=resource==nullptr;
63 if(bitmap)resource=FindResourceW(module,id,RT_BITMAP);
64 if(!resource)return HRESULT_FROM_WIN32(GetLastError());
65 HGLOBAL loaded=LoadResource(module,resource);if(!loaded)return HRESULT_FROM_WIN32(GetLastError());
66 const void* bytes=LockResource(loaded);if(!bytes)return E_FAIL;
67 const DWORD size=SizeofResource(module,resource);
68 if(!bitmap)return LoadTextureMemory(owner,bytes,size,result,levels);
69 // BITMAP resources contain the DIB without the on-disk file header.
70 if(size<sizeof(BITMAPINFOHEADER))return E_INVALIDARG;
71 BITMAPINFOHEADER dib{};memcpy(&dib,bytes,sizeof(dib));
72 if(dib.biSize<sizeof(dib) || dib.biSize>size || dib.biBitCount>32)return E_INVALIDARG;
73 const DWORD colors=dib.biClrUsed ? dib.biClrUsed : dib.biBitCount<=8 ? 1u<<dib.biBitCount : 0;
74 const uint64_t offset=uint64_t(dib.biSize)+uint64_t(colors)*sizeof(RGBQUAD)
75 +(dib.biSize==sizeof(dib) && dib.biCompression==BI_BITFIELDS ? 3*sizeof(DWORD) : 0);
76 if(offset>size || size>MAXDWORD-sizeof(BITMAPFILEHEADER))return E_INVALIDARG;
77 BITMAPFILEHEADER header{};header.bfType=0x4D42;header.bfSize=size+sizeof(header);header.bfOffBits=DWORD(offset+sizeof(header));
78 std::vector<uint8_t> file(header.bfSize);memcpy(file.data(),&header,sizeof(header));memcpy(file.data()+sizeof(header),bytes,size);
79 return LoadTextureMemory(owner,file.data(),file.size(),result,levels);
80 }
81}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
HRESULT LoadTextureMemory(RenderDevice *owner, const void *data, UINT bytes, RenderTexture **result, UINT levels=0)
HWND ClientWindow()
void AttachClientRuntime(Window11 *window, RenderDevice *device)
float ClientFPS()
HRESULT TraceFailure(const wchar_t *operation, HRESULT code)
Window11 * ClientHost()
HRESULT LoadTextureResource(RenderDevice *owner, HMODULE module, LPCWSTR id, RenderTexture **result, UINT levels)
double ClientTime()
const wchar_t * ClientFrameStats()
double ClientAbsoluteTime()
const wchar_t * ErrorString(HRESULT code)
RenderDevice * ClientRenderer()
const SurfaceDescription * ClientBackBufferDescription()
HINSTANCE ClientInstance()
HRESULT ClientToggleFullscreen()
const wchar_t * ClientDeviceStats()
bool ClientWindowed()