BsWin32RenderWindow.cpp 18 KB

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