Skip to content

Commit 6938e14

Browse files
committed
Rework the native scale handling for new MoltenVK
1 parent abefeeb commit 6938e14

File tree

7 files changed

+74
-82
lines changed

7 files changed

+74
-82
lines changed

lib/graphics_engine/include/ge_vulkan_driver.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ namespace GE
8686
//! sets a viewport
8787
virtual void setViewPort(const core::rect<s32>& area);
8888

89-
//! gets the area of the current viewport
90-
virtual const core::rect<s32>& getViewPort() const { return m_viewport; }
91-
9289
//! updates hardware buffer if needed
9390
virtual bool updateHardwareBuffer(SHWBufferLink *HWBuffer) { return false; }
9491

@@ -369,7 +366,7 @@ namespace GE
369366
{ return m_separate_rtt_texture; }
370367
void handleDeletedTextures();
371368
void addRTTPolyCount(unsigned count) { m_rtt_polycount += count; }
372-
SDL_Window* getSDLWindow() const { return m_window; }
369+
SDL_Window* getSDLWindow() const { return m_params.m_sdl_window; }
373370
void clearDrawCallsCache();
374371
void addDrawCallToCache(std::unique_ptr<GEVulkanDrawCall>& dc);
375372
std::unique_ptr<GEVulkanDrawCall> getDrawCallFromCache();
@@ -506,13 +503,11 @@ namespace GE
506503
uint32_t m_image_index;
507504
video::SColor m_clear_color, m_rtt_clear_color;
508505
core::rect<s32> m_clip;
509-
core::rect<s32> m_viewport;
510506
core::matrix4 m_pre_rotation_matrix;
511507

512508
video::ITexture* m_white_texture;
513509
video::ITexture* m_transparent_texture;
514510

515-
SDL_Window* m_window;
516511
bool m_disable_wait_idle;
517512

518513
IrrlichtDevice* m_irrlicht_device;

lib/graphics_engine/src/ge_vulkan_driver.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@ GEVulkanDriver::GEVulkanDriver(const SIrrlichtCreationParameters& params,
531531
m_transparent_texture = NULL;
532532
m_pre_rotation_matrix = core::matrix4(core::matrix4::EM4CONST_IDENTITY);
533533

534-
m_window = window;
535534
m_disable_wait_idle = false;
536535
g_schedule_pausing_rendering.store(false);
537536
g_paused_rendering.store(false);
@@ -569,10 +568,6 @@ GEVulkanDriver::GEVulkanDriver(const SIrrlichtCreationParameters& params,
569568

570569
if (SDL_Vulkan_CreateSurface(window, m_vk->instance, &m_vk->surface) == SDL_FALSE)
571570
throw std::runtime_error("SDL_Vulkan_CreateSurface failed");
572-
int w, h = 0;
573-
SDL_Vulkan_GetDrawableSize(window, &w, &h);
574-
ScreenSize.Width = w;
575-
ScreenSize.Height = h;
576571

577572
m_device_extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
578573
findPhysicalDevice();
@@ -1242,7 +1237,7 @@ void GEVulkanDriver::createSwapChain()
12421237
}
12431238

12441239
int w, h = 0;
1245-
SDL_Vulkan_GetDrawableSize(m_window, &w, &h);
1240+
SDL_Vulkan_GetDrawableSize(m_params.m_sdl_window, &w, &h);
12461241
VkExtent2D max_extent = m_surface_capabilities.maxImageExtent;
12471242
VkExtent2D min_extent = m_surface_capabilities.minImageExtent;
12481243
VkExtent2D actual_extent =
@@ -1252,6 +1247,11 @@ void GEVulkanDriver::createSwapChain()
12521247
std::max(
12531248
std::min((unsigned)h, max_extent.height), min_extent.height)
12541249
};
1250+
#if defined(__APPLE__)
1251+
// MoltenVK stores the correctly rounded, high-dpi screen size in
1252+
// currentExtent using half-to-even rounding
1253+
actual_extent = m_surface_capabilities.currentExtent;
1254+
#endif
12551255

12561256
VkSwapchainCreateInfoKHR create_info = {};
12571257
create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
@@ -1684,6 +1684,7 @@ void GEVulkanDriver::copyBuffer(VkBuffer src_buffer, VkBuffer dst_buffer,
16841684
// ----------------------------------------------------------------------------
16851685
void GEVulkanDriver::OnResize(const core::dimension2d<u32>& size)
16861686
{
1687+
m_params.WindowSize = size;
16871688
CNullDriver::OnResize(size);
16881689
if (g_paused_rendering.load() == false)
16891690
{
@@ -2210,7 +2211,7 @@ void GEVulkanDriver::setViewPort(const core::rect<s32>& area)
22102211
vp.clipAgainst(rendert);
22112212
if (vp.getHeight() > 0 && vp.getWidth() > 0)
22122213
{
2213-
m_viewport = vp;
2214+
ViewPort = vp;
22142215
if (m_irrlicht_device->getSceneManager() &&
22152216
m_irrlicht_device->getSceneManager()->getActiveCamera())
22162217
{
@@ -2363,7 +2364,7 @@ void GEVulkanDriver::createSwapChainRelated(bool handle_surface)
23632364
waitIdle();
23642365
if (handle_surface)
23652366
{
2366-
if (SDL_Vulkan_CreateSurface(m_window, m_vk->instance, &m_vk->surface) == SDL_FALSE)
2367+
if (SDL_Vulkan_CreateSurface(m_params.m_sdl_window, m_vk->instance, &m_vk->surface) == SDL_FALSE)
23672368
throw std::runtime_error("SDL_Vulkan_CreateSurface failed");
23682369
}
23692370
updateSurfaceInformation(m_physical_device, &m_surface_capabilities,

lib/irrlicht/include/SIrrCreationParameters.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#if !defined(SERVER_ONLY) && defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
1616
#include "SDL_video.h"
17+
#else
18+
#define SDL_Window void
1719
#endif
1820

1921
namespace irr
@@ -58,7 +60,9 @@ namespace irr
5860
UsePerformanceTimer(true),
5961
ForceLegacyDevice(false),
6062
ShadersPath(""),
61-
SDK_version_do_not_use(IRRLICHT_SDK_VERSION)
63+
SDK_version_do_not_use(IRRLICHT_SDK_VERSION),
64+
PrivateData(NULL),
65+
m_sdl_window(NULL)
6266
{
6367
}
6468

@@ -94,6 +98,7 @@ namespace irr
9498
ShadersPath = other.ShadersPath;
9599
PrivateData = other.PrivateData;
96100
WindowPosition = other.WindowPosition;
101+
m_sdl_window = other.m_sdl_window;
97102
return *this;
98103
}
99104

@@ -327,6 +332,7 @@ namespace irr
327332
Java RE. */
328333
void *PrivateData;
329334

335+
SDL_Window* m_sdl_window;
330336
};
331337

332338

lib/irrlicht/source/Irrlicht/CIrrDeviceSDL.cpp

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
6969
TopPadding(0), BottomPadding(0), LeftPadding(0), RightPadding(0),
7070
InitialOrientation(0), WindowHasFocus(false), WindowMinimized(false),
7171
Resizable(false), AccelerometerIndex(-1), AccelerometerInstance(-1),
72-
GyroscopeIndex(-1), GyroscopeInstance(-1), NativeScaleX(1.0f),
73-
NativeScaleY(1.0f)
72+
GyroscopeIndex(-1), GyroscopeInstance(-1)
7473
{
7574
#ifdef _DEBUG
7675
setDebugName("CIrrDeviceSDL");
@@ -187,11 +186,6 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
187186
}
188187
else
189188
return;
190-
updateNativeScale(&Width, &Height);
191-
Width = (u32)((f32)Width * NativeScaleX);
192-
Height = (u32)((f32)Height * NativeScaleY);
193-
CreationParams.WindowSize.Width = Width;
194-
CreationParams.WindowSize.Height = Height;
195189
}
196190

197191
// create cursor control
@@ -213,30 +207,6 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
213207
}
214208

215209

216-
void CIrrDeviceSDL::updateNativeScale(u32* saving_width, u32* saving_height)
217-
{
218-
int width, height = 0;
219-
SDL_GetWindowSize(Window, &width, &height);
220-
int real_width = width;
221-
int real_height = height;
222-
if (CreationParams.DriverType == video::EDT_OPENGL ||
223-
CreationParams.DriverType == video::EDT_OGLES2)
224-
{
225-
SDL_GL_GetDrawableSize(Window, &real_width, &real_height);
226-
}
227-
else if (CreationParams.DriverType == video::EDT_VULKAN)
228-
{
229-
SDL_Vulkan_GetDrawableSize(Window, &real_width, &real_height);
230-
}
231-
NativeScaleX = (f32)real_width / (f32)width;
232-
NativeScaleY = (f32)real_height / (f32)height;
233-
if (saving_width)
234-
*saving_width = width;
235-
if (saving_height)
236-
*saving_height = height;
237-
}
238-
239-
240210
//! destructor
241211
CIrrDeviceSDL::~CIrrDeviceSDL()
242212
{
@@ -422,6 +392,7 @@ extern "C" void update_swap_interval(int swap_interval)
422392

423393
bool CIrrDeviceSDL::createWindow()
424394
{
395+
CreationParams.m_sdl_window = NULL;
425396
// Ignore alpha size here, this follow irr_driver.cpp:450
426397
// Try 32 and, upon failure, 24 then 16 bit per pixels
427398
if (CreationParams.DriverType == video::EDT_OPENGL ||
@@ -520,6 +491,7 @@ bool CIrrDeviceSDL::createWindow()
520491
return false;
521492
}
522493
}
494+
CreationParams.m_sdl_window = Window;
523495
return true;
524496
}
525497

@@ -723,10 +695,6 @@ void CIrrDeviceSDL::createDriver()
723695
try
724696
{
725697
VideoDriver = video::createVulkanDriver(CreationParams, FileSystem, Window, this);
726-
// SDL_Vulkan_GetDrawableSize only works after driver is created
727-
updateNativeScale(&Width, &Height);
728-
Width = (u32)((f32)Width * NativeScaleX);
729-
Height = (u32)((f32)Height * NativeScaleY);
730698
}
731699
catch (std::exception& e)
732700
{
@@ -845,8 +813,8 @@ bool CIrrDeviceSDL::run()
845813
irrevent.TouchInput.ID = getTouchId(SDL_event.tfinger.fingerId);
846814
if (SDL_event.type == SDL_FINGERUP)
847815
removeTouchId(SDL_event.tfinger.fingerId);
848-
irrevent.TouchInput.X = SDL_event.tfinger.x * Width;
849-
irrevent.TouchInput.Y = SDL_event.tfinger.y * Height;
816+
irrevent.TouchInput.X = SDL_event.tfinger.x * getRealScreenSize().Width;
817+
irrevent.TouchInput.Y = SDL_event.tfinger.y * getRealScreenSize().Height;
850818
postEventFromUser(irrevent);
851819
break;
852820

@@ -871,8 +839,8 @@ bool CIrrDeviceSDL::run()
871839
case SDL_MOUSEMOTION:
872840
irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
873841
irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
874-
MouseX = irrevent.MouseInput.X = SDL_event.motion.x * NativeScaleX;
875-
MouseY = irrevent.MouseInput.Y = SDL_event.motion.y * NativeScaleY;
842+
MouseX = irrevent.MouseInput.X = SDL_event.motion.x * getNativeScaleX();
843+
MouseY = irrevent.MouseInput.Y = SDL_event.motion.y * getNativeScaleY();
876844
irrevent.MouseInput.ButtonStates = MouseButtonStates;
877845

878846
postEventFromUser(irrevent);
@@ -882,8 +850,8 @@ bool CIrrDeviceSDL::run()
882850
case SDL_MOUSEBUTTONUP:
883851

884852
irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
885-
irrevent.MouseInput.X = SDL_event.button.x * NativeScaleX;
886-
irrevent.MouseInput.Y = SDL_event.button.y * NativeScaleY;
853+
irrevent.MouseInput.X = SDL_event.button.x * getNativeScaleX();
854+
irrevent.MouseInput.Y = SDL_event.button.y * getNativeScaleY();
887855

888856
irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
889857

@@ -1060,13 +1028,10 @@ bool CIrrDeviceSDL::run()
10601028

10611029
void CIrrDeviceSDL::handleNewSize(u32 width, u32 height)
10621030
{
1063-
updateNativeScale();
1064-
u32 new_width = width * NativeScaleX;
1065-
u32 new_height = height * NativeScaleY;
1066-
if (new_width != Width || new_height != Height)
1031+
if (width != Width || height != Height)
10671032
{
1068-
Width = new_width;
1069-
Height = new_height;
1033+
Width = width;
1034+
Height = height;
10701035
if (VideoDriver)
10711036
VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));
10721037
reset_network_body();
@@ -1597,7 +1562,7 @@ extern "C" int Android_getMovedHeight();
15971562
s32 CIrrDeviceSDL::getMovedHeight() const
15981563
{
15991564
#if defined(IOS_STK)
1600-
return SDL_GetMovedHeightByScreenKeyboard() * NativeScaleY;
1565+
return SDL_GetMovedHeightByScreenKeyboard() * getNativeScaleY();
16011566
#elif defined(ANDROID)
16021567
return Android_getMovedHeight();
16031568
#else
@@ -1610,7 +1575,7 @@ extern "C" int Android_getKeyboardHeight();
16101575
u32 CIrrDeviceSDL::getOnScreenKeyboardHeight() const
16111576
{
16121577
#if defined(IOS_STK)
1613-
return SDL_GetScreenKeyboardHeight() * NativeScaleY;
1578+
return SDL_GetScreenKeyboardHeight() * getNativeScaleY();
16141579
#elif defined(ANDROID)
16151580
return Android_getKeyboardHeight();
16161581
#else
@@ -1621,13 +1586,17 @@ u32 CIrrDeviceSDL::getOnScreenKeyboardHeight() const
16211586

16221587
f32 CIrrDeviceSDL::getNativeScaleX() const
16231588
{
1624-
return NativeScaleX;
1589+
if (VideoDriver)
1590+
return (f32)VideoDriver->getScreenSize().Width / (f32)Width;
1591+
return 1.0f;
16251592
}
16261593

16271594

16281595
f32 CIrrDeviceSDL::getNativeScaleY() const
16291596
{
1630-
return NativeScaleY;
1597+
if (VideoDriver)
1598+
return (f32)VideoDriver->getScreenSize().Height / (f32)Height;
1599+
return 1.0f;
16311600
}
16321601

16331602

@@ -1701,6 +1670,15 @@ void CIrrDeviceSDL::createGUIAndVulkanScene()
17011670
}
17021671

17031672

1673+
const core::dimension2du& CIrrDeviceSDL::getRealScreenSize() const
1674+
{
1675+
static core::dimension2du ss;
1676+
if (VideoDriver)
1677+
return VideoDriver->getScreenSize();
1678+
return ss;
1679+
}
1680+
1681+
17041682
} // end namespace irr
17051683

17061684
#endif // _IRR_COMPILE_WITH_SDL_DEVICE_

lib/irrlicht/source/Irrlicht/CIrrDeviceSDL.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class MoltenVK;
182182
//! Sets the new position of the cursor.
183183
virtual void setPosition(f32 x, f32 y)
184184
{
185-
setPosition((s32)(x*Device->Width), (s32)(y*Device->Height));
185+
setPosition((s32)(x*Device->getRealScreenSize().Width), (s32)(y*Device->getRealScreenSize().Height));
186186
}
187187

188188
//! Sets the new position of the cursor.
@@ -208,8 +208,8 @@ class MoltenVK;
208208
virtual core::position2d<f32> getRelativePosition()
209209
{
210210
updateCursorPos();
211-
return core::position2d<f32>(CursorPos.X / (f32)Device->Width,
212-
CursorPos.Y / (f32)Device->Height);
211+
return core::position2d<f32>(CursorPos.X / (f32)Device->getRealScreenSize().Width,
212+
CursorPos.Y / (f32)Device->getRealScreenSize().Height);
213213
}
214214

215215
virtual void setReferenceRect(core::rect<s32>* rect=0)
@@ -225,12 +225,12 @@ class MoltenVK;
225225

226226
if (CursorPos.X < 0)
227227
CursorPos.X = 0;
228-
if (CursorPos.X > (s32)Device->Width)
229-
CursorPos.X = Device->Width;
228+
if (CursorPos.X > (s32)Device->getRealScreenSize().Width)
229+
CursorPos.X = Device->getRealScreenSize().Width;
230230
if (CursorPos.Y < 0)
231231
CursorPos.Y = 0;
232-
if (CursorPos.Y > (s32)Device->Height)
233-
CursorPos.Y = Device->Height;
232+
if (CursorPos.Y > (s32)Device->getRealScreenSize().Height)
233+
CursorPos.Y = Device->getRealScreenSize().Height;
234234
}
235235

236236
CIrrDeviceSDL* Device;
@@ -305,8 +305,6 @@ class MoltenVK;
305305
s32 GyroscopeIndex;
306306
s32 GyroscopeInstance;
307307

308-
f32 NativeScaleX, NativeScaleY;
309-
310308
struct SKeyMap
311309
{
312310
SKeyMap() {}
@@ -332,7 +330,7 @@ class MoltenVK;
332330
MoltenVK* m_moltenvk;
333331
#endif
334332
void createGUIAndVulkanScene();
335-
void updateNativeScale(u32* saving_width = NULL, u32* saving_height = NULL);
333+
const core::dimension2du& getRealScreenSize() const;
336334
};
337335

338336
} // end namespace irr

0 commit comments

Comments
 (0)