-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaderCompiler.py
97 lines (77 loc) · 2.85 KB
/
shaderCompiler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pathlib
import os
import time
from sys import platform, exit
if platform != "win32":
import pwd
COMPDIR_FILE = "compDir.save"
def getUsername():
return pwd.getpwuid(os.getuid())[0]
def findConsistingInFolder(folder, searchKey):
for root, _, files in os.walk(folder):
for f in files:
if f.find(searchKey) == 0:
return root + "/" + f
def getCompilerExe():
try:
f = open(COMPDIR_FILE, "r")
compDir = f.read()
f.close()
print(f"Found compiler dir : {compDir}\n")
return compDir
except FileNotFoundError:
print("Could not find compiler specifier file will search for it now\n")
if platform == "darwin":
searchPath = f"/Users/{getUsername()}/VulkanSDK"
compDir = findConsistingInFolder(searchPath, "glslc")
elif platform == "linux" or platform == "linux2":
compDir = "/usr/bin/glslc"
elif platform == "win32":
searchPath = "C:/VulkanSDK"
compDir = findConsistingInFolder(searchPath, "glslc")
else:
exit("Could not recognize the platform")
print(f"Found compiler dir : {compDir}\n")
f = open(COMPDIR_FILE, "w")
f.write(compDir)
f.close()
return compDir;
compilerExe = getCompilerExe()
pathOfFile = os.path.dirname(os.path.abspath(__file__))
shaderFolders = ["assets/shaders"] # folders to check for shader files
fileMap = {}
def getChangedFiles():
global fileMap, shaderFolders
changedFiles = []
for shaderFolder in shaderFolders:
l = os.walk(pathOfFile + "/" + shaderFolder)
for files in l:
for file in files[2]:
if file.split(".")[-1] == "spv":
continue
filePath = files[0] + "/" + file
fileN = pathlib.Path(filePath)
if filePath not in fileMap:
changedFiles.append(filePath)
else:
if (fileN.stat().st_mtime != fileMap[filePath]):
changedFiles.append(filePath)
fileMap[filePath] = fileN.stat().st_mtime
return changedFiles
def getFolder(fullLocation):
return "/".join(fullLocation.split("/")[:-1])
def getName(fullLocation):
return "".join(fullLocation.split("/")[-1].split(".")[:-1])
def compileShaders(shaderFiles):
for shaderFile in shaderFiles:
# -O optimizes the shader for better performance
commandString = compilerExe + " -O " + shaderFile + " -o " + shaderFile + ".spv"
print(commandString)
os.system(commandString)
while 1:
changedFiles = getChangedFiles()
if (len(changedFiles) > 0):
print(len(changedFiles), "Shader file changed. Compling now...\n")
compileShaders(changedFiles)
print("\nCompilation done\n")
time.sleep(0.1)