BsWin32RenderWindow.cpp 19 KB

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