BsWin32RenderWindow.cpp 18 KB

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