summaryrefslogtreecommitdiff
path: root/Runtime/GfxDevice/VramLimits.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Runtime/GfxDevice/VramLimits.cpp')
-rw-r--r--Runtime/GfxDevice/VramLimits.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/Runtime/GfxDevice/VramLimits.cpp b/Runtime/GfxDevice/VramLimits.cpp
new file mode 100644
index 0000000..36bfe50
--- /dev/null
+++ b/Runtime/GfxDevice/VramLimits.cpp
@@ -0,0 +1,39 @@
+#include "UnityPrefix.h"
+#include "VramLimits.h"
+#include "Runtime/Shaders/GraphicsCaps.h"
+
+
+int ChooseSuitableFSAALevel( int width, int height, int backbufferBPP, int frontbufferBPP, int depthBPP, int fsaa )
+{
+ // no AA support?
+ if( !gGraphicsCaps.hasMultiSample )
+ return 0;
+
+ // figure out appropriate AA level based on VRAM and screen size
+ int vramKB = int(gGraphicsCaps.videoMemoryMB * 1024);
+
+ const int vramAllowedByPortion = int(vramKB * 0.5f); // allow max. 50% of total VRAM for screen
+ const int vramAllowedMax = 256 * 1024; // max 256MB for screen
+ // Make sure at least this amount of free of VRAM left after the screen.
+ // E.g. on a 32MB VRAM PPC MacMini, going 1280x960 2xAA still corrupts the screen (VRAM taken: 23.5MB).
+ // So we need to keep somewhat more free than 8MB... 16MB works.
+ const int vramAllowedWithKeepingSomeFree = int(vramKB - 16*1024);
+
+ int vramAllowedKB = std::min(vramAllowedByPortion, std::min(vramAllowedMax,vramAllowedWithKeepingSomeFree));
+ int vramNeededKB;
+ do {
+ vramNeededKB = width * height * (std::max(fsaa,1) * (backbufferBPP+depthBPP) + frontbufferBPP) / 1024;
+ if( vramNeededKB < vramAllowedKB )
+ break;
+
+ #if !UNITY_RELEASE
+ printf_console("Screen %ix%i at %ixAA won't fit, reducing AA (needed mem=%i allowedmem=%i)\n", width, height, fsaa, vramNeededKB, vramAllowedKB );
+ #endif
+ fsaa /= 2;
+ } while( fsaa > 1 );
+
+ if( fsaa == 1 )
+ fsaa = 0;
+
+ return fsaa;
+}