Skip to content

Instantly share code, notes, and snippets.

@wispborne
Last active March 13, 2020 13:58
Show Gist options
  • Select an option

  • Save wispborne/0ed1ec311b3eee023a08bb291ef7c102 to your computer and use it in GitHub Desktop.

Select an option

Save wispborne/0ed1ec311b3eee023a08bb291ef7c102 to your computer and use it in GitHub Desktop.
Roughly calculates VRAM used by starsector mods
import os
import math
from scipy import ndimage
from PIL import Image
dirPath = os.path.dirname(os.path.realpath(__file__))
debug = False
# Instructions for use:
# Place in Starsector/mods directory.
# Install Python.
# Run `pip install scipy Pillow`
# Run `python ./Calculate_VRAM.py`
# Created by Dark Revenant
# Edited to show data per mod by Wisp
def main():
finalReadout = ""
print os.walk(dirPath).next()[1]
for modDir in os.walk(dirPath).next()[1]:
fileList = []
for root, dirs, files in os.walk(modDir):
for f in files:
fullPath = os.path.join(root, f)
if os.path.isfile(fullPath):
fileList.append([root, f])
totalBytes = 0
didAlreadyCountBackgroundImage = False
for root, f in fileList:
fullPath = os.path.join(root, f)
relPath = os.path.relpath(fullPath, dirPath)
try:
data = ndimage.imread(fullPath)
except IOError:
print ("Skipped " + relPath)
continue
if len(data.shape) < 3:
try:
img = Image.open(fullPath)
except IOError:
print( "Skipped " + relPath)
continue
w, h = img.size
d = len(img.getbands())
else:
w, h, d = data.shape
if "backgrounds" in os.path.split(root):
if didAlreadyCountBackgroundImage:
# If mod adds a larger-than-vanilla background, count it as increasing VRAM, but only once
# because only one background is loaded at a time
print ("Skipped extra background " + relPath)
continue
if w <= 2048:
# If file is a standard size background, then it should not increase VRAM use
# because only one background is loaded at a time
print ("Skipped vanilla-sized background " + relPath)
continue
m = 1
mStr = "1"
didAlreadyCountBackgroundImage = True
else:
m = 4.0 / 3.0
mStr = "4/3"
texW = 1
while texW < w:
texW *= 2
texH = 1
while texH < h:
texH *= 2
bytes = int(math.ceil(texW * texH * d * m))
totalBytes += bytes
if debug:
print (f + ": " + str(w) + "x" + str(h) + "x" + str(d) + " (m=" + mStr + ")")
print (" --> " + str(texW) + " * " + str(texH) + " * " + str(d) + " * " + mStr + " = " + str(bytes) + " bytes")
if totalBytes > 0:
finalReadout += "\n\n" + modDir + ":\n" + ("%d" % totalBytes) + " B"
if totalBytes >= 1024:
finalReadout += ("\n%.3f" % (totalBytes / 1024.0)) + " kiB"
if totalBytes >= 1048576:
finalReadout += ("\n%.3f" % (totalBytes / 1048576.0)) + " MiB"
if totalBytes >= 1073741824:
finalReadout += ("\n%.3f" % (totalBytes / 1073741824.0)) + " GiB"
print(finalReadout)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment