-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Display Helpers #1566
Draft
colincornaby
wants to merge
3
commits into
H-uru:master
Choose a base branch
from
colincornaby:display-helper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Display Helpers #1566
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
Sources/Plasma/Apps/plClient/Mac-Cocoa/plMacDisplayHelper.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/*==LICENSE==* | ||
|
||
CyanWorlds.com Engine - MMOG client, server and tools | ||
Copyright (C) 2011 Cyan Worlds, Inc. | ||
|
||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
Additional permissions under GNU GPL version 3 section 7 | ||
|
||
If you modify this Program, or any covered work, by linking or | ||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, | ||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent | ||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK | ||
(or a modified version of those libraries), | ||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, | ||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG | ||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the | ||
licensors of this Program grant you additional | ||
permission to convey the resulting work. Corresponding Source for a | ||
non-source form of such a combination shall include the source code for | ||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered | ||
work. | ||
|
||
You can contact Cyan Worlds, Inc. by email [email protected] | ||
or by snail mail at: | ||
Cyan Worlds, Inc. | ||
14617 N Newport Hwy | ||
Mead, WA 99021 | ||
|
||
*==LICENSE==*/ | ||
|
||
#ifndef plMacDisplayHelper_hpp | ||
#define plMacDisplayHelper_hpp | ||
|
||
#include <stdio.h> | ||
// Currently requires Metal to query attached GPU capabilities | ||
// Capability check will also work for GL - but will need something | ||
// different for older GPUs. | ||
#include <AppKit/AppKit.h> | ||
#include <Metal/Metal.hpp> | ||
#include <QuartzCore/QuartzCore.h> | ||
|
||
#include "plPipeline/hsG3DDeviceSelector.h" | ||
|
||
class plMacDisplayHelper: public plDisplayHelper | ||
{ | ||
public: | ||
CGDirectDisplayID CurrentDisplay() { return fCurrentDisplay; } | ||
// we need NSScreen to query for non rectangular screen geometry | ||
void SetCurrentScreen(NSScreen* screen); | ||
|
||
plDisplayMode DefaultDisplayMode() override { return fDefaultDisplayMode; }; | ||
void GetSupportedDisplayModes(std::vector<plDisplayMode> *res, int ColorDepth = 32) const override; | ||
std::vector<plDisplayMode> GetDisplayModes() const override { return fDisplayModes; } | ||
|
||
MTL::Device* RenderDevice() { return fRenderDevice; } | ||
|
||
plMacDisplayHelper(); | ||
plMacDisplayHelper(hsWindowHndl window); | ||
~plMacDisplayHelper(); | ||
private: | ||
CGDirectDisplayID fCurrentDisplay; | ||
MTL::Device* fRenderDevice = nullptr; | ||
plDisplayMode fDefaultDisplayMode; | ||
std::vector<plDisplayMode> fDisplayModes; | ||
}; | ||
|
||
#endif /* plMacDisplayHelper_hpp */ |
157 changes: 157 additions & 0 deletions
157
Sources/Plasma/Apps/plClient/Mac-Cocoa/plMacDisplayHelper.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/*==LICENSE==* | ||
|
||
CyanWorlds.com Engine - MMOG client, server and tools | ||
Copyright (C) 2011 Cyan Worlds, Inc. | ||
|
||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
Additional permissions under GNU GPL version 3 section 7 | ||
|
||
If you modify this Program, or any covered work, by linking or | ||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, | ||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent | ||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK | ||
(or a modified version of those libraries), | ||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, | ||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG | ||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the | ||
licensors of this Program grant you additional | ||
permission to convey the resulting work. Corresponding Source for a | ||
non-source form of such a combination shall include the source code for | ||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered | ||
work. | ||
|
||
You can contact Cyan Worlds, Inc. by email [email protected] | ||
or by snail mail at: | ||
Cyan Worlds, Inc. | ||
14617 N Newport Hwy | ||
Mead, WA 99021 | ||
|
||
*==LICENSE==*/ | ||
|
||
#include "plPipeline.h" | ||
#include "plMacDisplayHelper.h" | ||
|
||
// CGDirectDisplayCopyCurrentMetalDevice only declared in Metal.h? | ||
#include <Metal/Metal.h> | ||
|
||
void plMacDisplayHelper::SetCurrentScreen(NSScreen* screen) | ||
{ | ||
fCurrentDisplay = [screen.deviceDescription[@"NSScreenNumber"] intValue]; | ||
|
||
// Calculate the region actually available for full screen | ||
NSRect currentResolution = [screen frame]; | ||
if (@available(macOS 12.0, *)) { | ||
NSEdgeInsets currentSafeAreaInsets = [screen safeAreaInsets]; | ||
// Sigh... Origin doesn't matter but lets do it for inspectability | ||
currentResolution.origin.x += currentSafeAreaInsets.left; | ||
currentResolution.origin.y += currentSafeAreaInsets.top; | ||
currentResolution.size.width -= currentSafeAreaInsets.left + currentSafeAreaInsets.right; | ||
currentResolution.size.height -= currentSafeAreaInsets.top + currentSafeAreaInsets.bottom; | ||
} | ||
|
||
float safeAspectRatio = currentResolution.size.width / currentResolution.size.height; | ||
|
||
fDisplayModes.clear(); | ||
|
||
CFArrayRef displayModes = CGDisplayCopyAllDisplayModes(fCurrentDisplay, nullptr); | ||
for(int i=0; i< CFArrayGetCount(displayModes); i++) | ||
{ | ||
// Now filter out the ones that are taller than the safe area aspect ratio | ||
// This could break in interesting ways if Apple ships displays that have unsafe | ||
// areas along the side - but will prevent us stripping any aspect ratios that don't | ||
// match the display entirely. | ||
// Asked for better guidance here from Apple - FB13375033 | ||
CGDisplayModeRef mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(displayModes, i); | ||
|
||
float modeAspectRatio = float(CGDisplayModeGetWidth(mode)) / float(CGDisplayModeGetHeight(mode)); | ||
if(modeAspectRatio < safeAspectRatio) | ||
{ | ||
continue; | ||
} | ||
|
||
// aspect ratio is good - add the mode to the list | ||
plDisplayMode plasmaMode; | ||
plasmaMode.ColorDepth = 32; | ||
plasmaMode.Width = int(CGDisplayModeGetWidth(mode)); | ||
plasmaMode.Height = int(CGDisplayModeGetHeight(mode)); | ||
|
||
fDisplayModes.push_back(plasmaMode); | ||
} | ||
CFRelease(displayModes); | ||
|
||
// we're going to look for a good default mode, but | ||
// in case we somehow don't match at least pick one. | ||
|
||
fDefaultDisplayMode = fDisplayModes.front(); | ||
|
||
fRenderDevice->release(); | ||
fRenderDevice = (__bridge MTL::Device *)(CGDirectDisplayCopyCurrentMetalDevice(fCurrentDisplay)); | ||
|
||
// now inspect the GPU and figure out a good default resolution | ||
// This code is in Metal (for now) - but it should also work | ||
// under OpenGL/Mac as long as the GPU also supports Metal. | ||
if(fRenderDevice->supportsFamily(MTL::GPUFamilyMetal3)) | ||
{ | ||
// if it's a Metal 3 GPU - it should be good | ||
// Pick the native display resolution | ||
// (Re-picking the first one here for clarity) | ||
fDefaultDisplayMode = fDisplayModes.front(); | ||
} | ||
else | ||
{ | ||
// time to go down the rabit hole | ||
int maxWidth = INT_MAX; | ||
if(fRenderDevice->lowPower()) | ||
{ | ||
// integrated - not Apple Silicon, we know it's not very good | ||
maxWidth = 1080; | ||
} | ||
else if(fRenderDevice->recommendedMaxWorkingSetSize() < 4000000000) | ||
{ | ||
// if it has less than around 4 gigs of VRAM it might still be performance | ||
// limited | ||
maxWidth = 1400; | ||
} | ||
|
||
for (auto & mode : fDisplayModes) { | ||
if(mode.Width <= maxWidth) { | ||
fDefaultDisplayMode = mode; | ||
abort(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void plMacDisplayHelper::GetSupportedDisplayModes(std::vector<plDisplayMode> *res, int ColorDepth) const | ||
{ | ||
*res = fDisplayModes; | ||
} | ||
|
||
plMacDisplayHelper::plMacDisplayHelper() | ||
{ | ||
|
||
}; | ||
|
||
plMacDisplayHelper::plMacDisplayHelper(hsWindowHndl window) | ||
: plMacDisplayHelper() | ||
{ | ||
NSWindow* nsWindow = (__bridge NSWindow*)(window); | ||
SetCurrentScreen(nsWindow.screen); | ||
} | ||
|
||
plMacDisplayHelper::~plMacDisplayHelper() | ||
{ | ||
fRenderDevice->release(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably use
emplace_back()
to construct in place (avoids copying).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this - we'd need to define a constructor on plDisplayMode. There is an alternative - but I'm not sure it avoids a copy.
FYI - C++20 emplace_back can initialize structs directly without a constructor.