Skip to content

Commit 59492a4

Browse files
committed
Add renderer feature flag for Lua DPI awareness
The runtime is aware of the operating system display scaling factor and presents a virtual coordinate space for the Lua application where it scales the sizes and positions of the window and cursor without allowing the application any choice. This is a hack as scaling up the window contents by the desired amount leads to a low quality image. In order to draw at true resolution the Lua program needs to both be able to tell what the scaling factor is to adjust all UI dimensions, but also able indicate to the runtime that it wishes to be free of the virtualization and instead use the real coordinate system. As it's a very big task both in time and design-wise to make the UI scale the runtime needs to be able either launch in legacy mode but also in a mode with true visuals. This is achieved here by making `RenderInit` take a sequence of feature flags as it's called from `Launch.lua`. This is early enough that most of the Lua application has yet to run and thus gets the chance to fetch the proper values as it starts up while the runtime also gets this request early enough to influence the initialization of the renderer and allocation of resources. This change introduces a flag string of `"DPI_AWARE"` that instructs the runtime to go into a mode where it reports the true scale factor in `GetScreenScale`, provides a full-resolution render target and doesn't rescale the screen size or mouse positions anymore. For alternative runtimes like `pobfrontend` any flags are ignored and their runtime continue to work as they always have, or can start honoring the flag if it fits their environment. # Conflicts: # engine/render.h # ui_api.cpp
1 parent a0bd786 commit 59492a4

6 files changed

Lines changed: 50 additions & 11 deletions

File tree

engine/render.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
// Classes
99
// =======
1010

11+
// Renderer feature flags
12+
enum r_featureFlag_e {
13+
F_DPI_AWARE = 0x1, // App understands DPI, do not virtualize screen size/positions
14+
};
15+
1116
// Font alignment
1217
enum r_fontAlign_e {
1318
F_LEFT,
@@ -59,7 +64,7 @@ class r_IRenderer {
5964
static r_IRenderer* GetHandle(sys_IMain* sysHnd);
6065
static void FreeHandle(r_IRenderer* hnd);
6166

62-
virtual void Init() = 0;
67+
virtual void Init(r_featureFlag_e features) = 0;
6368
virtual void Shutdown() = 0;
6469

6570
virtual void BeginFrame() = 0;

engine/render/r_main.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -870,10 +870,12 @@ void main(void) {
870870
// Init/Shutdown
871871
// =============
872872

873-
void r_renderer_c::Init()
873+
void r_renderer_c::Init(r_featureFlag_e features)
874874
{
875875
sys->con->PrintFunc("Render Init");
876876

877+
apiDpiAware = !!(features & F_DPI_AWARE);
878+
877879
timer_c timer;
878880
timer.Start();
879881

@@ -1776,15 +1778,24 @@ int r_renderer_c::VirtualScreenHeight() {
17761778
}
17771779

17781780
float r_renderer_c::VirtualScreenScaleFactor() {
1779-
return sys->video->vid.dpiScale;
1781+
if (apiDpiAware) {
1782+
return sys->video->vid.dpiScale;
1783+
}
1784+
return 1.0f;
17801785
}
17811786

17821787
int r_renderer_c::VirtualMap(int properValue) {
1783-
return (int)(properValue / VirtualScreenScaleFactor());
1788+
if (apiDpiAware) {
1789+
return properValue;
1790+
}
1791+
return (int)(properValue / sys->video->vid.dpiScale);
17841792
}
17851793

17861794
int r_renderer_c::VirtualUnmap(int mappedValue) {
1787-
return (int)(mappedValue * VirtualScreenScaleFactor());
1795+
if (apiDpiAware) {
1796+
return mappedValue;
1797+
}
1798+
return (int)(mappedValue * sys->video->vid.dpiScale);
17881799
}
17891800

17901801
// =====

engine/render/r_main.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class r_layer_c {
6767
class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
6868
public:
6969
// Interface
70-
void Init();
70+
void Init(r_featureFlag_e features);
7171
void Shutdown();
7272

7373
void BeginFrame();
@@ -169,6 +169,7 @@ class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
169169
GLuint blitSampleLocColour = 0;
170170
};
171171

172+
bool apiDpiAware{};
172173
RenderTarget rttMain[2];
173174
int presentRtt = 0;
174175

ui_api.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
** imgHandle:SetLoadingPriority(pri)
3232
** width, height = imgHandle:ImageSize()
3333
**
34-
** RenderInit()
34+
** RenderInit(["flag1"[, "flag2"...]]) flag:{"DPI_AWARE"}
3535
** width, height = GetScreenSize()
36+
** scaleFactor = GetScreenScale()
3637
** SetClearColor(red, green, blue[, alpha])
3738
** SetDrawLayer({layer|nil}[, subLayer)
3839
** GetDrawLayer()
@@ -278,7 +279,20 @@ static int l_imgHandleImageSize(lua_State* L)
278279
static int l_RenderInit(lua_State* L)
279280
{
280281
ui_main_c* ui = GetUIPtr(L);
281-
ui->RenderInit();
282+
int n = lua_gettop(L);
283+
bool dpiAware = false;
284+
for (int i = 1; i <= n; ++i) {
285+
ui->LAssert(L, lua_isstring(L, i), "RenderInit() argument %d: expected string, got %s", i, luaL_typename(L, i));
286+
char const* str = lua_tostring(L, i);
287+
if (strcmp(str, "DPI_AWARE") == 0) {
288+
dpiAware = true;
289+
}
290+
}
291+
r_featureFlag_e features{};
292+
if (dpiAware) {
293+
features = (r_featureFlag_e)(features | F_DPI_AWARE);
294+
}
295+
ui->RenderInit(features);
282296
return 0;
283297
}
284298

@@ -290,6 +304,13 @@ static int l_GetScreenSize(lua_State* L)
290304
return 2;
291305
}
292306

307+
static int l_GetScreenScale(lua_State* L)
308+
{
309+
ui_main_c* ui = GetUIPtr(L);
310+
lua_pushnumber(L, ui->renderer->VirtualScreenScaleFactor());
311+
return 1;
312+
}
313+
293314
static int l_SetClearColor(lua_State* L)
294315
{
295316
ui_main_c* ui = GetUIPtr(L);
@@ -1345,6 +1366,7 @@ int ui_main_c::InitAPI(lua_State* L)
13451366
// Rendering
13461367
ADDFUNC(RenderInit);
13471368
ADDFUNC(GetScreenSize);
1369+
ADDFUNC(GetScreenScale);
13481370
ADDFUNC(SetClearColor);
13491371
ADDFUNC(SetDrawLayer);
13501372
ADDFUNC(GetDrawLayer);

ui_main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void ui_main_c::Init(int argc, char** argv)
237237
}
238238
}
239239

240-
void ui_main_c::RenderInit()
240+
void ui_main_c::RenderInit(r_featureFlag_e features)
241241
{
242242
if (renderer) {
243243
return;
@@ -252,7 +252,7 @@ void ui_main_c::RenderInit()
252252

253253
// Initialise renderer
254254
renderer = r_IRenderer::GetHandle(sys);
255-
renderer->Init();
255+
renderer->Init(features);
256256

257257
// Create UI console handler
258258
conUI = ui_IConsole::GetHandle(this);

ui_main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ui_main_c: public ui_IMain {
4949

5050
static int InitAPI(lua_State* L);
5151

52-
void RenderInit();
52+
void RenderInit(r_featureFlag_e features);
5353
void ScriptInit();
5454
void ScriptShutdown();
5555

0 commit comments

Comments
 (0)