BsWin32RenderWindow.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Win32/BsWin32RenderWindow.h"
  4. #include "Win32/BsWin32Platform.h"
  5. #include "Win32/BsWin32Window.h"
  6. #include "Win32/BsWin32VideoModeInfo.h"
  7. #include "Corethread/BsCoreThread.h"
  8. #include "Profiling/BsRenderStats.h"
  9. #include "Managers/BsRenderWindowManager.h"
  10. #include "BsVulkanRenderAPI.h"
  11. #include "BsVulkanDevice.h"
  12. #include "BsVulkanSwapChain.h"
  13. #include "BsVulkanDevice.h"
  14. #include "BsVulkanCommandBuffer.h"
  15. #include "Managers/BsVulkanCommandBufferManager.h"
  16. #include "BsVulkanQueue.h"
  17. #include "Math/BsMath.h"
  18. namespace bs
  19. {
  20. Win32RenderWindowProperties::Win32RenderWindowProperties(const RENDER_WINDOW_DESC& desc)
  21. :RenderWindowProperties(desc)
  22. { }
  23. Win32RenderWindow::Win32RenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId)
  24. : RenderWindow(desc, windowId), mProperties(desc)
  25. {
  26. }
  27. void Win32RenderWindow::getCustomAttribute(const String& name, void* pData) const
  28. {
  29. if (name == "WINDOW")
  30. {
  31. UINT64 *pHwnd = (UINT64*)pData;
  32. *pHwnd = (UINT64)getHWnd();
  33. return;
  34. }
  35. }
  36. Vector2I Win32RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  37. {
  38. POINT pos;
  39. pos.x = screenPos.x;
  40. pos.y = screenPos.y;
  41. ScreenToClient(getHWnd(), &pos);
  42. return Vector2I(pos.x, pos.y);
  43. }
  44. Vector2I Win32RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  45. {
  46. POINT pos;
  47. pos.x = windowPos.x;
  48. pos.y = windowPos.y;
  49. ClientToScreen(getHWnd(), &pos);
  50. return Vector2I(pos.x, pos.y);
  51. }
  52. SPtr<ct::Win32RenderWindow> Win32RenderWindow::getCore() const
  53. {
  54. return std::static_pointer_cast<ct::Win32RenderWindow>(mCoreSpecific);
  55. }
  56. HWND Win32RenderWindow::getHWnd() const
  57. {
  58. blockUntilCoreInitialized();
  59. return getCore()->_getWindowHandle();
  60. }
  61. void Win32RenderWindow::syncProperties()
  62. {
  63. ScopedSpinLock lock(getCore()->mLock);
  64. mProperties = getCore()->mSyncedProperties;
  65. }
  66. namespace ct
  67. {
  68. Win32RenderWindow::Win32RenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, VulkanRenderAPI& renderAPI)
  69. : RenderWindow(desc, windowId), mProperties(desc), mSyncedProperties(desc), mWindow(nullptr), mIsChild(false)
  70. , mShowOnSwap(false), mDisplayFrequency(0), mRenderAPI(renderAPI), mRequiresNewBackBuffer(true)
  71. { }
  72. Win32RenderWindow::~Win32RenderWindow()
  73. {
  74. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  75. presentDevice->waitIdle();
  76. Win32RenderWindowProperties& props = mProperties;
  77. props.mActive = false;
  78. if (mWindow != nullptr)
  79. {
  80. bs_delete(mWindow);
  81. mWindow = nullptr;
  82. }
  83. mSwapChain = nullptr;
  84. vkDestroySurfaceKHR(mRenderAPI._getInstance(), mSurface, gVulkanAllocator);
  85. }
  86. void Win32RenderWindow::initialize()
  87. {
  88. Win32RenderWindowProperties& props = mProperties;
  89. // Create a window
  90. WINDOW_DESC windowDesc;
  91. windowDesc.border = mDesc.border;
  92. windowDesc.enableDoubleClick = mDesc.enableDoubleClick;
  93. windowDesc.fullscreen = mDesc.fullscreen;
  94. windowDesc.width = mDesc.videoMode.getWidth();
  95. windowDesc.height = mDesc.videoMode.getHeight();
  96. windowDesc.hidden = mDesc.hidden || mDesc.hideUntilSwap;
  97. windowDesc.left = mDesc.left;
  98. windowDesc.top = mDesc.top;
  99. windowDesc.outerDimensions = mDesc.outerDimensions;
  100. windowDesc.title = mDesc.title;
  101. windowDesc.toolWindow = mDesc.toolWindow;
  102. windowDesc.creationParams = this;
  103. windowDesc.modal = mDesc.modal;
  104. windowDesc.wndProc = &Win32Platform::_win32WndProc;
  105. #ifdef BS_STATIC_LIB
  106. windowDesc.module = GetModuleHandle(NULL);
  107. #else
  108. windowDesc.module = GetModuleHandle("BansheeVulkanRenderAPI.dll");
  109. #endif
  110. NameValuePairList::const_iterator opt;
  111. opt = mDesc.platformSpecific.find("parentWindowHandle");
  112. if (opt != mDesc.platformSpecific.end())
  113. windowDesc.parent = (HWND)parseUINT64(opt->second);
  114. opt = mDesc.platformSpecific.find("externalWindowHandle");
  115. if (opt != mDesc.platformSpecific.end())
  116. windowDesc.external = (HWND)parseUINT64(opt->second);
  117. const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
  118. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  119. if (numOutputs > 0)
  120. {
  121. UINT32 actualMonitorIdx = std::min(mDesc.videoMode.getOutputIdx(), numOutputs - 1);
  122. const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  123. windowDesc.monitor = outputInfo.getMonitorHandle();
  124. }
  125. // Must be set before creating a window, since wndProc will call ShowWindow if needed after creation
  126. if (!windowDesc.external)
  127. {
  128. mShowOnSwap = mDesc.hideUntilSwap;
  129. props.mHidden = mDesc.hideUntilSwap || mDesc.hidden;
  130. }
  131. mWindow = bs_new<Win32Window>(windowDesc);
  132. mIsChild = windowDesc.parent != nullptr;
  133. mDisplayFrequency = Math::roundToInt(mDesc.videoMode.getRefreshRate());
  134. // Update local properties
  135. props.mIsFullScreen = mDesc.fullscreen && !mIsChild;
  136. props.mColorDepth = 32;
  137. props.mActive = true;
  138. props.mWidth = mWindow->getWidth();
  139. props.mHeight = mWindow->getHeight();
  140. props.mTop = mWindow->getTop();
  141. props.mLeft = mWindow->getLeft();
  142. props.mHwGamma = mDesc.gamma;
  143. props.mMultisampleCount = 1;
  144. // Create Vulkan surface
  145. VkWin32SurfaceCreateInfoKHR surfaceCreateInfo;
  146. surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
  147. surfaceCreateInfo.pNext = nullptr;
  148. surfaceCreateInfo.flags = 0;
  149. surfaceCreateInfo.hwnd = mWindow->getHWnd();
  150. surfaceCreateInfo.hinstance = windowDesc.module;
  151. VkInstance instance = mRenderAPI._getInstance();
  152. VkResult result = vkCreateWin32SurfaceKHR(instance, &surfaceCreateInfo, gVulkanAllocator, &mSurface);
  153. assert(result == VK_SUCCESS);
  154. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  155. VkPhysicalDevice physicalDevice = presentDevice->getPhysical();
  156. mPresentQueueFamily = presentDevice->getQueueFamily(GQT_GRAPHICS);
  157. VkBool32 supportsPresent;
  158. vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, mPresentQueueFamily, mSurface, &supportsPresent);
  159. if(!supportsPresent)
  160. {
  161. // Note: Not supporting present only queues at the moment
  162. // Note: Also present device can only return one family of graphics queue, while there could be more (some of
  163. // which support present)
  164. BS_EXCEPT(RenderingAPIException, "Cannot find a graphics queue that also supports present operations.");
  165. }
  166. uint32_t numFormats;
  167. result = vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, mSurface, &numFormats, nullptr);
  168. assert(result == VK_SUCCESS);
  169. assert(numFormats > 0);
  170. VkSurfaceFormatKHR* surfaceFormats = bs_stack_alloc<VkSurfaceFormatKHR>(numFormats);
  171. result = vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, mSurface, &numFormats, surfaceFormats);
  172. assert(result == VK_SUCCESS);
  173. // If there is no preferred format, use standard RGBA
  174. if ((numFormats == 1) && (surfaceFormats[0].format == VK_FORMAT_UNDEFINED))
  175. {
  176. if (mDesc.gamma)
  177. mColorFormat = VK_FORMAT_R8G8B8A8_SRGB;
  178. else
  179. mColorFormat = VK_FORMAT_B8G8R8A8_UNORM;
  180. mColorSpace = surfaceFormats[0].colorSpace;
  181. }
  182. else
  183. {
  184. bool foundFormat = false;
  185. VkFormat wantedFormatsUNORM[] =
  186. {
  187. VK_FORMAT_R8G8B8A8_UNORM,
  188. VK_FORMAT_B8G8R8A8_UNORM,
  189. VK_FORMAT_A8B8G8R8_UNORM_PACK32,
  190. VK_FORMAT_A8B8G8R8_UNORM_PACK32,
  191. VK_FORMAT_R8G8B8_UNORM,
  192. VK_FORMAT_B8G8R8_UNORM
  193. };
  194. VkFormat wantedFormatsSRGB[] =
  195. {
  196. VK_FORMAT_R8G8B8A8_SRGB,
  197. VK_FORMAT_B8G8R8A8_SRGB,
  198. VK_FORMAT_A8B8G8R8_SRGB_PACK32,
  199. VK_FORMAT_A8B8G8R8_SRGB_PACK32,
  200. VK_FORMAT_R8G8B8_SRGB,
  201. VK_FORMAT_B8G8R8_SRGB
  202. };
  203. UINT32 numWantedFormats;
  204. VkFormat* wantedFormats;
  205. if (mDesc.gamma)
  206. {
  207. numWantedFormats = sizeof(wantedFormatsSRGB) / sizeof(wantedFormatsSRGB[0]);
  208. wantedFormats = wantedFormatsSRGB;
  209. }
  210. else
  211. {
  212. numWantedFormats = sizeof(wantedFormatsUNORM) / sizeof(wantedFormatsUNORM[0]);
  213. wantedFormats = wantedFormatsUNORM;
  214. }
  215. for(UINT32 i = 0; i < numWantedFormats; i++)
  216. {
  217. for(UINT32 j = 0; j < numFormats; j++)
  218. {
  219. if(surfaceFormats[j].format == wantedFormats[i])
  220. {
  221. mColorFormat = surfaceFormats[j].format;
  222. mColorSpace = surfaceFormats[j].colorSpace;
  223. foundFormat = true;
  224. break;
  225. }
  226. }
  227. if (foundFormat)
  228. break;
  229. }
  230. // If we haven't found anything, fall back to first available
  231. if(!foundFormat)
  232. {
  233. mColorFormat = surfaceFormats[0].format;
  234. mColorSpace = surfaceFormats[0].colorSpace;
  235. if (mDesc.gamma)
  236. LOGERR("Cannot find a valid sRGB format for a render window surface, falling back to a default format.");
  237. }
  238. }
  239. mDepthFormat = VK_FORMAT_D24_UNORM_S8_UINT;
  240. bs_stack_free(surfaceFormats);
  241. // Create swap chain
  242. mSwapChain = bs_shared_ptr_new<VulkanSwapChain>();
  243. mSwapChain->rebuild(presentDevice, mSurface, props.mWidth, props.mHeight, props.mVSync, mColorFormat, mColorSpace,
  244. mDesc.depthBuffer, mDepthFormat);
  245. // Make the window full screen if required
  246. if (!windowDesc.external)
  247. {
  248. if (props.mIsFullScreen)
  249. {
  250. DEVMODE displayDeviceMode;
  251. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  252. displayDeviceMode.dmSize = sizeof(DEVMODE);
  253. displayDeviceMode.dmBitsPerPel = props.mColorDepth;
  254. displayDeviceMode.dmPelsWidth = props.mWidth;
  255. displayDeviceMode.dmPelsHeight = props.mHeight;
  256. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  257. if (mDisplayFrequency)
  258. {
  259. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  260. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  261. if (ChangeDisplaySettingsEx(NULL, &displayDeviceMode, NULL, CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  262. {
  263. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings with user display frequency failed.");
  264. }
  265. }
  266. if (ChangeDisplaySettingsEx(NULL, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  267. {
  268. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed.");
  269. }
  270. }
  271. }
  272. {
  273. ScopedSpinLock lock(mLock);
  274. mSyncedProperties = props;
  275. }
  276. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  277. RenderWindow::initialize();
  278. }
  279. void Win32RenderWindow::acquireBackBuffer()
  280. {
  281. // We haven't presented the current back buffer yet, so just use that one
  282. if (!mRequiresNewBackBuffer)
  283. return;
  284. mSwapChain->acquireBackBuffer();
  285. mRequiresNewBackBuffer = false;
  286. }
  287. void Win32RenderWindow::swapBuffers(UINT32 syncMask)
  288. {
  289. THROW_IF_NOT_CORE_THREAD;
  290. if (mShowOnSwap)
  291. setHidden(false);
  292. // Get a command buffer on which we'll submit
  293. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  294. // Assuming present queue is always graphics
  295. assert(presentDevice->getQueueFamily(GQT_GRAPHICS) == mPresentQueueFamily);
  296. // Find an appropriate queue to execute on
  297. VulkanQueue* queue = presentDevice->getQueue(GQT_GRAPHICS, 0);
  298. UINT32 queueMask = presentDevice->getQueueMask(GQT_GRAPHICS, 0);
  299. // Ignore myself
  300. syncMask &= ~queueMask;
  301. UINT32 deviceIdx = presentDevice->getIndex();
  302. VulkanCommandBufferManager& cbm = static_cast<VulkanCommandBufferManager&>(CommandBufferManager::instance());
  303. UINT32 numSemaphores;
  304. cbm.getSyncSemaphores(deviceIdx, syncMask, mSemaphoresTemp, numSemaphores);
  305. // Wait on present (i.e. until the back buffer becomes available), if we haven't already done so
  306. const SwapChainSurface& surface = mSwapChain->getBackBuffer();
  307. if(surface.needsWait)
  308. {
  309. mSemaphoresTemp[numSemaphores] = mSwapChain->getBackBuffer().sync;
  310. numSemaphores++;
  311. mSwapChain->notifyBackBufferWaitIssued();
  312. }
  313. queue->present(mSwapChain.get(), mSemaphoresTemp, numSemaphores);
  314. mRequiresNewBackBuffer = true;
  315. }
  316. void Win32RenderWindow::move(INT32 left, INT32 top)
  317. {
  318. THROW_IF_NOT_CORE_THREAD;
  319. Win32RenderWindowProperties& props = mProperties;
  320. if (!props.mIsFullScreen)
  321. {
  322. mWindow->move(left, top);
  323. props.mTop = mWindow->getTop();
  324. props.mLeft = mWindow->getLeft();
  325. {
  326. ScopedSpinLock lock(mLock);
  327. mSyncedProperties.mTop = props.mTop;
  328. mSyncedProperties.mLeft = props.mLeft;
  329. }
  330. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  331. }
  332. }
  333. void Win32RenderWindow::resize(UINT32 width, UINT32 height)
  334. {
  335. THROW_IF_NOT_CORE_THREAD;
  336. Win32RenderWindowProperties& props = mProperties;
  337. if (!props.mIsFullScreen)
  338. {
  339. mWindow->resize(width, height);
  340. props.mWidth = mWindow->getWidth();
  341. props.mHeight = mWindow->getHeight();
  342. {
  343. ScopedSpinLock lock(mLock);
  344. mSyncedProperties.mWidth = props.mWidth;
  345. mSyncedProperties.mHeight = props.mHeight;
  346. }
  347. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  348. }
  349. }
  350. void Win32RenderWindow::setActive(bool state)
  351. {
  352. THROW_IF_NOT_CORE_THREAD;
  353. mWindow->setActive(state);
  354. RenderWindow::setActive(state);
  355. }
  356. void Win32RenderWindow::setHidden(bool hidden)
  357. {
  358. THROW_IF_NOT_CORE_THREAD;
  359. mShowOnSwap = false;
  360. mWindow->setHidden(hidden);
  361. RenderWindow::setHidden(hidden);
  362. }
  363. void Win32RenderWindow::minimize()
  364. {
  365. THROW_IF_NOT_CORE_THREAD;
  366. mWindow->minimize();
  367. }
  368. void Win32RenderWindow::maximize()
  369. {
  370. THROW_IF_NOT_CORE_THREAD;
  371. mWindow->maximize();
  372. }
  373. void Win32RenderWindow::restore()
  374. {
  375. THROW_IF_NOT_CORE_THREAD;
  376. mWindow->restore();
  377. }
  378. void Win32RenderWindow::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  379. {
  380. THROW_IF_NOT_CORE_THREAD;
  381. if (mIsChild)
  382. return;
  383. const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
  384. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  385. if (numOutputs == 0)
  386. return;
  387. Win32RenderWindowProperties& props = mProperties;
  388. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  389. const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  390. mDisplayFrequency = Math::roundToInt(refreshRate);
  391. props.mIsFullScreen = true;
  392. DEVMODE displayDeviceMode;
  393. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  394. displayDeviceMode.dmSize = sizeof(DEVMODE);
  395. displayDeviceMode.dmBitsPerPel = props.mColorDepth;
  396. displayDeviceMode.dmPelsWidth = width;
  397. displayDeviceMode.dmPelsHeight = height;
  398. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  399. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
  400. HMONITOR hMonitor = outputInfo.getMonitorHandle();
  401. MONITORINFOEX monitorInfo;
  402. memset(&monitorInfo, 0, sizeof(MONITORINFOEX));
  403. monitorInfo.cbSize = sizeof(MONITORINFOEX);
  404. GetMonitorInfo(hMonitor, &monitorInfo);
  405. if (ChangeDisplaySettingsEx(monitorInfo.szDevice, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  406. {
  407. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed");
  408. }
  409. props.mTop = monitorInfo.rcMonitor.top;
  410. props.mLeft = monitorInfo.rcMonitor.left;
  411. props.mWidth = width;
  412. props.mHeight = height;
  413. SetWindowLong(mWindow->getHWnd(), GWL_STYLE, WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  414. SetWindowLong(mWindow->getHWnd(), GWL_EXSTYLE, 0);
  415. SetWindowPos(mWindow->getHWnd(), HWND_TOP, props.mLeft, props.mTop, width, height, SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
  416. }
  417. void Win32RenderWindow::setFullscreen(const VideoMode& mode)
  418. {
  419. THROW_IF_NOT_CORE_THREAD;
  420. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  421. }
  422. void Win32RenderWindow::setWindowed(UINT32 width, UINT32 height)
  423. {
  424. THROW_IF_NOT_CORE_THREAD;
  425. Win32RenderWindowProperties& props = mProperties;
  426. if (!props.mIsFullScreen)
  427. return;
  428. props.mIsFullScreen = false;
  429. props.mWidth = width;
  430. props.mHeight = height;
  431. // Drop out of fullscreen
  432. ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);
  433. UINT32 winWidth = width;
  434. UINT32 winHeight = height;
  435. RECT rect;
  436. SetRect(&rect, 0, 0, winWidth, winHeight);
  437. AdjustWindowRect(&rect, mWindow->getStyle(), false);
  438. winWidth = rect.right - rect.left;
  439. winHeight = rect.bottom - rect.top;
  440. // Deal with centering when switching down to smaller resolution
  441. HMONITOR hMonitor = MonitorFromWindow(mWindow->getHWnd(), MONITOR_DEFAULTTONEAREST);
  442. MONITORINFO monitorInfo;
  443. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  444. monitorInfo.cbSize = sizeof(MONITORINFO);
  445. GetMonitorInfo(hMonitor, &monitorInfo);
  446. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  447. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  448. INT32 left = screenw > INT32(winWidth) ? ((screenw - INT32(winWidth)) / 2) : 0;
  449. INT32 top = screenh > INT32(winHeight) ? ((screenh - INT32(winHeight)) / 2) : 0;
  450. SetWindowLong(mWindow->getHWnd(), GWL_STYLE, mWindow->getStyle() | WS_VISIBLE);
  451. SetWindowLong(mWindow->getHWnd(), GWL_EXSTYLE, mWindow->getStyleEx());
  452. SetWindowPos(mWindow->getHWnd(), HWND_NOTOPMOST, left, top, winWidth, winHeight,
  453. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  454. {
  455. ScopedSpinLock lock(mLock);
  456. mSyncedProperties.mWidth = props.mWidth;
  457. mSyncedProperties.mHeight = props.mHeight;
  458. }
  459. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  460. }
  461. HWND Win32RenderWindow::_getWindowHandle() const
  462. {
  463. return mWindow->getHWnd();
  464. }
  465. void Win32RenderWindow::getCustomAttribute(const String& name, void* data) const
  466. {
  467. if (name == "FB")
  468. {
  469. VulkanFramebuffer** fb = (VulkanFramebuffer**)data;
  470. *fb = mSwapChain->getBackBuffer().framebuffer;
  471. return;
  472. }
  473. if(name == "SC")
  474. {
  475. VulkanSwapChain** sc = (VulkanSwapChain**)data;
  476. *sc = mSwapChain.get();
  477. return;
  478. }
  479. if(name == "WINDOW")
  480. {
  481. UINT64 *pWnd = (UINT64*)data;
  482. *pWnd = (UINT64)mWindow->getHWnd();
  483. return;
  484. }
  485. RenderWindow::getCustomAttribute(name, data);
  486. }
  487. void Win32RenderWindow::_windowMovedOrResized()
  488. {
  489. THROW_IF_NOT_CORE_THREAD;
  490. if (!mWindow)
  491. return;
  492. mWindow->_windowMovedOrResized();
  493. Win32RenderWindowProperties& props = mProperties;
  494. if (!props.mIsFullScreen) // Fullscreen is handled directly by this object
  495. {
  496. props.mTop = mWindow->getTop();
  497. props.mLeft = mWindow->getLeft();
  498. props.mWidth = mWindow->getWidth();
  499. props.mHeight = mWindow->getHeight();
  500. }
  501. // Resize swap chain
  502. //// Need to make sure nothing is using the swap buffer before we re-create it
  503. // Note: Optionally I can detect exactly on which queues (if any) are the swap chain images used on, and only wait
  504. // on those
  505. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  506. presentDevice->waitIdle();
  507. mSwapChain->rebuild(presentDevice, mSurface, props.mWidth, props.mHeight, props.mVSync, mColorFormat, mColorSpace,
  508. mDesc.depthBuffer, mDepthFormat);
  509. RenderWindow::_windowMovedOrResized();
  510. }
  511. void Win32RenderWindow::syncProperties()
  512. {
  513. ScopedSpinLock lock(mLock);
  514. mProperties = mSyncedProperties;
  515. }
  516. }
  517. }