-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathpremake5.lua
289 lines (223 loc) · 7.25 KB
/
premake5.lua
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
local BGFX_PATH = path.join(path.getabsolute(".."), "bgfx")
if not os.isdir(BGFX_PATH) then
print("bgfx not found at " .. BGFX_PATH)
os.exit()
end
local BX_PATH = path.join(path.getabsolute(".."), "bx")
if not os.isdir(BX_PATH) then
print("bx not found at " .. BX_PATH)
os.exit()
end
local IOQ3_PATH = path.join(path.getabsolute(".."), "ioq3")
local RENDERER_PATH = path.getabsolute(".")
if os.is("windows") then
if not os.isdir(IOQ3_PATH) then
print("ioquake3 not found at " .. IOQ3_PATH)
os.exit()
end
end
newaction
{
trigger = "shaders",
description = "Compile shaders",
onStart = function()
local renderers = nil
if os.is("windows") then
renderers = { "gl", "d3d11" }
else
renderers = { "gl" }
end
local tempOutputFilename = "build/tempoutput"
local outputSourceFilename = "build/Shader.cpp"
function compileShader(input, type, permutation, defines)
io.write("Compiling " .. input .. "_" .. type)
if permutation == nil then
io.write("\n")
else
io.write(" " .. permutation .. "\n")
end
local inputFilename = "shaders/" .. input .. "_" .. type .. ".sc"
-- Compile the shader for all renderers.
for _,renderer in pairs(renderers) do
local command = nil
if os.is("windows") then
command = "shaderc.exe"
elseif os.is("linux") then
command = "`./shaderc64"
end
local variableName = input .. "_"
if permutation ~= nil then
variableName = variableName .. permutation .. "_"
end
variableName = variableName .. type .. "_" .. renderer
command = command .. " -i \"shaders;" .. path.join(BGFX_PATH, "src") .. "\" -f \"" .. inputFilename .. "\" -o \"" .. tempOutputFilename .. "\" --varyingdef shaders/varying.def.sc --bin2c \"" .. variableName .. "\" --type " .. type
if defines ~= nil then
command = command .. " --define " .. defines
end
if renderer == "gl" then
command = command .. " --platform linux -p 140"
elseif renderer == "d3d9" or renderer == "d3d11" then
command = command .. " --platform windows"
if type == "fragment" then
command = command .. " -p ps_"
else
command = command .. " -p vs_"
end
if renderer == "d3d9" then
command = command .. "3_0"
elseif renderer == "d3d11" then
command = command .. "4_0"
end
command = command .. " -O 3"
end
if os.is("linux") then
command = command .. "`"
end
if os.execute(command) ~= 0 then
local message = "\n" .. input .. " " .. type
if permutation ~= nil then
message = message .. " " .. permutation
end
message = message .. " " .. renderer .. "\n" .. command
error(message)
end
-- Append the temp output file to the real output file.
local tempFile = io.open(tempOutputFilename, "r")
local tempContent = tempFile:read("*all")
tempFile:close()
local outputFile = io.open(outputSourceFilename, "a")
outputFile:write("\n")
outputFile:write(tempContent)
outputFile:close()
end
end
local fragmentShaders =
{
{ "AdaptedLuminance" },
{ "Depth" },
{ "Depth", "AlphaTest", "USE_ALPHA_TEST" },
{ "Fog" },
{ "FXAA" },
{ "Generic" },
{ "Generic", "AlphaTest", "USE_ALPHA_TEST" },
{ "Generic", "AlphaTestSoftSprite", "USE_ALPHA_TEST;USE_SOFT_SPRITE" },
{ "Generic", "SoftSprite", "USE_SOFT_SPRITE" },
{ "LinearDepth" },
{ "Luminance" },
{ "LuminanceDownsample" },
{ "Texture" },
{ "TextureSingleChannel" },
{ "TextureColor" },
{ "ToneMap" }
}
local vertexShaders =
{
{ "Depth" },
{ "Depth", "AlphaTest", "USE_ALPHA_TEST" },
{ "Fog" },
{ "Generic" },
{ "Texture" }
}
function compileAllShaders()
for _,v in pairs(fragmentShaders) do
compileShader(v[1], "fragment", v[2], v[3])
end
for _,v in pairs(vertexShaders) do
compileShader(v[1], "vertex", v[2], v[3])
end
end
-- Make sure the build directory exists
os.mkdir("build")
-- Delete the output file so we can append to it.
os.remove(outputSourceFilename)
-- Compile the shaders.
local ok, message = pcall(compileAllShaders)
if not ok then
print(message)
return
end
-- Generate shader ID enums, writing them to the output header file.
function writeEnum(of, data, name)
of:write("struct " .. name .. "\n")
of:write("{\n")
of:write("\tenum Enum\n")
of:write("\t{\n")
for _,v in pairs(data) do
of:write("\t\t" .. v[1])
if v[2] ~= nil then
of:write("_" .. v[2])
end
of:write(",\n")
end
of:write("\t\tNum\n")
of:write("\t};\n")
of:write("};\n\n")
end
local outputHeaderFilename = "build/Shader.h"
local outputHeaderFile = io.open(outputHeaderFilename, "w")
writeEnum(outputHeaderFile, fragmentShaders, "FragmentShaderId")
writeEnum(outputHeaderFile, vertexShaders, "VertexShaderId")
outputHeaderFile:close()
-- Generate functions to map shader ID enums to source strings, appending them to the output source file.
function writeSourceMap(of, data, renderer, name, nameLower)
of:write(string.format("\nstatic std::array<const bgfx::Memory *, %sShaderId::Num> Get%sShaderSourceMap_%s()\n", name, name, renderer))
of:write("{\n")
of:write(string.format("\tstd::array<const bgfx::Memory *, %sShaderId::Num> mem;\n", name))
for _,v in pairs(data) do
local id = v[1]
if v[2] ~= nil then
id = id .. "_" .. v[2]
end
local source = string.format("%s_%s_%s", id, nameLower, renderer)
of:write(string.format("\tmem[%sShaderId::%s] = bgfx::makeRef(%s, sizeof(%s));\n", name, id, source, source))
end
of:write("\treturn mem;\n")
of:write("}\n")
end
local outputSourceFile = io.open(outputSourceFilename, "a")
for _,renderer in pairs(renderers) do
writeSourceMap(outputSourceFile, fragmentShaders, renderer, "Fragment", "fragment")
writeSourceMap(outputSourceFile, vertexShaders, renderer, "Vertex", "vertex")
end
outputSourceFile:close()
print("Done.")
end,
}
solution "renderer_bgfx"
configurations { "Release", "Debug" }
location "build"
if os.is64bit() and not os.is("windows") then
platforms { "x86_64", "x86" }
else
platforms { "x86", "x86_64" }
end
startproject "renderer_bgfx"
configuration "platforms:x86"
architecture "x86"
configuration "platforms:x86_64"
architecture "x86_64"
configuration "Debug"
optimize "Debug"
defines { "_DEBUG" }
flags "Symbols"
configuration { "Debug", "x86" }
targetdir "build/bin_x86_debug"
configuration { "Debug", "x86_64" }
targetdir "build/bin_x64_debug"
configuration "Release"
optimize "Full"
defines "NDEBUG"
configuration { "Release", "x86" }
targetdir "build/bin_x86"
configuration { "Release", "x86_64" }
targetdir "build/bin_x64"
configuration "vs*"
defines { "_CRT_SECURE_NO_DEPRECATE" }
configuration { "vs*", "x86_64" }
defines { "_WIN64", "__WIN64__" }
dofile("renderer_bgfx.lua")
if os.is("windows") then
createRendererProject(BGFX_PATH, BX_PATH, RENDERER_PATH, path.join(IOQ3_PATH, "code/SDL2/include"), path.join(IOQ3_PATH, "code/libs/win32/libSDL2"), path.join(IOQ3_PATH, "code/libs/win64/libSDL264"))
else
createRendererProject(BGFX_PATH, BX_PATH, RENDERER_PATH, nil, nil, nil)
end