GL Profile Suite / tests / codegen.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
local gen = require("generate")

local codegen = {}
codegen._suites = {}

local function assert_suite(operation)
	assert(codegen._curr, "Attempt to " .. operation .. " when a suite is not current");
	assert(codegen._curr._type == "suite", "Attempt to " .. operation .. " when a test is current");
end

local function assert_test(operation)
	assert(codegen._curr, "Attempt to " .. operation .. " when a test is not current");
	assert(codegen._curr._type == "test", "Attempt to " .. operation .. " when a suite is current");
end

local function get_suite_or_assert(operation)
	assert(codegen._curr, "Attempt to " .. operation .. " when a suite is not current");
	if(codegen._curr._type == "test") then
		return codegen._curr._suite
	else
		return codegen._curr
	end
end

local function assert_enum(enumValues, testValue, operation)
	assert(enumValues[testValue], testValue .. " is not a legal " .. operation .. " value")
end

--Used for setting enumerated flags into a list of flags.
local function set_flags(flagDataList, flagEnum, newFlags, operation)
	if(type(newFlags) == "string") then
		print(newFlags)
		assert_enum(flagEnum, newFlags, operation)
		flagDataList[newFlags] = true
	elseif(type(newFlags) == "table") then
		for _,flag in ipairs(newFlags) do
			assert_enum(flagEnum, flag, operation)
			flagDataList[flag] = true
		end
	else
		assert(nil, "You must provide a string or a list of fields when trying to set the " .. operation .. " flags")
	end
end

--Use the current directory as the base directory for all file operations.
--Suites must have a `_type` field equal to "suite".
local function create_new_suite(name)
	local suite = {}
	suite._type = "suite"
	suite._base_dir = os.getcwd()
	suite._tests = {}
	
	suite.name = name
	suite.desc = "No Description"
	suite.params = {}
	suite.timings = {}
	suite.includes = {}
	suite.variables = {}
	suite.initialize = {}
	return suite
end

--Use the current directory as the base directory for all file operations.
--Tests must have a `_type` field equal to "test".
--Test has its current suite as the `_suite` field.
local function create_new_test(name)
	local test = {}
	test._type = "test"
	test._suite = codegen._curr
	test._base_dir = os.getcwd()
	
	test.name = name
	test.desc = "No Description"
	test.flags = {}
	test.render_flags = {}
	test.includes = {}
	test.variables = {}
	test.initialize = {}
	return test
end

-------------------------------
-- Suite interface functions.

--Interface functions for doing codegen work.
function codegen.suite(name)
	name = tostring(name)
	assert(name, "Name of a suite must be a string")
	
	if(not codegen._suites[name]) then
		codegen._suites[name] = create_new_suite(name)
	end
	
	codegen._curr = codegen._suites[name]
	return codegen._curr
end

--Sets the discription for a suite or test.
function codegen.description(desc)
	assert(codegen._curr, "Attempt to set a description for a suite/test that doesn't exist.")
	
	codegen._curr.desc = tostring(desc)
end

codegen.desc = codegen.description

function codegen.range(first, count)
	assert(count > 0, "Ranges must have a size > 0.")
	local rng = {}
	rng.first = tonumber(first)
	rng.count = tonumber(count)
	rng._rng = "range"
	return rng
end

function codegen.half_open_range(first, last)
	assert(last > first, "Half-open ranges must have last be larger than first")
	return codegen.range(first, last - first)
end

function codegen.closed_range(first, last)
	assert(last >= first, "Closed ranges must have last be larger than first")
	return codegen.range(first, last - first + 1)
end

function codegen.params(paramList)
	assert_suite("set parameters")
	assert(type(paramList) == "table", "Parameters are a table of named values")
	
	for k,v in pairs(paramList) do
		assert(type(k) == "string", "Parameter names must be strings.")
		assert(k:match("^[%a%_][%w%_]*$"), "Parameter names must be valid Lua and C++ identifiers.")
		--TODO: verify that v is a valid number range or series of numbers.
		codegen._curr.params[k] = v;
	end
end

local legalTimings = {cpu_time = true, gpu_time = true, gpu_latency = true}

function codegen.timings(timings)
	assert_suite("set timings")
	
	set_flags(codegen._curr.timings, legalTimings, timings, "timings")
end

function codegen.executions(numExecutions)
	assert_suite("set default execution count")
	numExecutions = tonumber(numExecutions)
	assert(type(numExecutions) == "number")
	
	codegen._curr.executions = math.floor(numExecutions)
end

local function InsertTableOfStrings(output, input, funcName)
	if(type(input) == "string") then
		output[#output + 1] = input
	elseif(type(input) == "table") then
		for _,v in ipairs(input) do
			output[#output + 1] = v
		end
	else
		assert(nil, funcName .. " takes a string or a table of strings.");
	end

end

function codegen.includes(includes)
	assert(codegen._curr, "Attempt to set includes for a suite/test that doesn't exist.")
	
	InsertTableOfStrings(codegen._curr.includes, includes, "includes")
end

function codegen.variables(vars)
	assert(codegen._curr, "Attempt to set variables for a suite/test that doesn't exist.")

	for varname, vartype in pairs(vars) do
		codegen._curr.variables[varname] = vartype
	end
end

codegen.vars = codegen.variables

function codegen.initialize(init_cmds)
	assert(codegen._curr, "Attempt to set initialization data for a suite/test that doesn't exist.")
	
	InsertTableOfStrings(codegen._curr.initialize, init_cmds, "initialize")
end

codegen.init = codegen.initialize


--------------------------------
-- Test specific functions.

function codegen.test(name)
	assert_suite("create a test")
	
	name = tostring(name)
	assert(name, "Name of a test must be a string")
	
	if(not codegen._curr._tests[name]) then
		codegen._curr._tests[name] = create_new_test(name)
	end
	
	codegen._curr = codegen._curr._tests[name]
	return codegen._curr
end

local legalFlags = {cache_flush = true, finish = true, swap = true}

function codegen.flags(flags)
	assert_test("set flags")
	set_flags(codegen._curr.flags, legalFlags, flags, "flags")
end

local legalRenderFlags = {discard = true}

function codegen.render_flags(render_flags)
	assert_test("set render flags")
	set_flags(codegen._curr.render_flags, legalRenderFlags, render_flags, "render_flags")
end

--Build the environment that the codegen-loaded scripts execute within.
local copyFromGlobal =
{
	--Take these packages.
	"debug",
	"io",
	"os",
	"package",
	"string",
	"table",
	"coroutine",
	
	--Functions
	"assert",
	"collectgarbage",
	"error",
	"getfenv",
	"getmetatable",
	"ipairs",
	"load",
	"loadstring",
	"module",
	"next",
	"pairs",
	"pcall",
	"print",
	"rawequal",
	"rawget",
	"rawset",
	"require",
	"select",
	"setfenv",
	"setmetatable",
	"tonumber",
	"tostring",
	"type",
	"unpack",
	"xpcall",
}

--Copy functions/modules from the Lua envrionment.
codegen._env = {}
for _, copy in ipairs(copyFromGlobal) do
	codegen._env[copy] = _G[copy]
end

--Add our functions from the codegen space to the environment.
for name, copy in pairs(codegen) do
	if(not name:match("^%_")) then
		codegen._env[name] = copy
	end
end

--Emulate lua dofile, but change the current directory.
--Also, change the environment, so they can't see Premake4 (outside of Premake's
--additions to global tables).
function codegen.dofile(filename)
	local oldcwd = os.getcwd()
	local fullPathname = path.getabsolute(filename)
	
	local hFile = io.open(fullPathname, "rt")
	local fileString = hFile:read("*a")
	hFile:close()
	
	os.chdir(path.getdirectory(fullPathname))
	local chunk = assert(loadstring(fileString, path.getname(filename)))
	setfenv(chunk, codegen._env)
	chunk()
	os.chdir(oldcwd)
end

local function WriteInclude(hFile, hdrsWritten, include)
	if(type(include) == "table") then
		for _,file in ipairs(include) do
			WriteInclude(hFile, hdrsWritten, file)
		end
	else
		if(not hdrsWritten[include]) then
			hFile:write("#include ", include, "\n")
			hdrsWritten[include] = true
		end
	end
end

local function WriteIncludes(hFile, suite)
	local hdrsWritten = {}
	
	--Write the standard headers.
	WriteInclude(hFile, hdrsWritten, gen.GetStandardIncludes())
	--TODO: Use OpenGL version/profile that was asked for.
	WriteInclude(hFile, hdrsWritten, gen.GetOpenGLIncludes(3, 3))
	--TODO: Use FreeGLUT only if asked.
	WriteInclude(hFile, hdrsWritten, gen.GetFreeglutIncludes())

	--Now include what the user wanted.
	WriteInclude(hFile, hdrsWritten, suite.includes)
	
	for testname, test in pairs(suite._tests) do
		WriteInclude(hFile, hdrsWritten, test.includes)
	end
end

local function WriteAndFormatInitStmts(hFile, suite, stmt)
	--TODO: Load a file if stmt is a file.
	--TODO: do the formatting.
	
	hFile:write("\t\t", stmt:gsub("%\n", "\n\t\t"), "\n\n")
end

local specialVarTypes =
{
	["gl buffer object"] =
		{type = "GLuint", destroy = "glDeleteBuffers(1, &%s);"},
	["gl vao"] =
		{type = "GLuint", destroy = "glDeleteVertexArrays(1, &%s);"},
	["gl program"] =
		{type = "GLuint", destroy = "glDeleteProgram(%s);"},
	["gl list buffer object"] = 
		{type = "std::vector<GLuint>", destroy = "util::DestroyBufferObjects(%s);"},
	["gl list vao"] =
		{type = "std::vector<GLuint>", destroy = "util::DestroyVertexArrays(%s);"},
	["gl list program"] =
		{type = "std::vector<GLuint>", destroy = "util::DestroyPrograms(%s);"},
}

local function WriteSuiteClass(hFile, suite)
	hFile:write("struct ", gen.GetSuiteClassname(suite.name), "\n{\n")
	
	--Write variables first.
	for varname, vartype in pairs(suite.variables) do
		hFile:write("\t")
		if(specialVarTypes[vartype]) then
			hFile:write(specialVarTypes[vartype].type)
		else
			hFile:write(vartype)
		end
		
		hFile:write(" ", varname, ";\n")
	end
	hFile:write("\n")
	
	--Write the constructor.
	hFile:write("\t", gen.GetSuiteClassname(suite.name), "()\n\t{\n")
	for _, stmt in ipairs(suite.initialize) do
		WriteAndFormatInitStmts(hFile, suite, stmt)
	end
	hFile:write("\t}\n\n")
	
	--Write the destructor.
	hFile:write("\t~", gen.GetSuiteClassname(suite.name), "()\n\t{\n")
	for varname, vartype in pairs(suite.variables) do
		if(specialVarTypes[vartype]) then
			hFile:write("\t\t")
			hFile:write(string.format(specialVarTypes[vartype].destroy, varname))
			hFile:write("\n")
		end
	end
	hFile:write("\t}\n")
	
	
	hFile:write("};\n");
end

function codegen.gencode()
	for name, suite in pairs(codegen._suites) do
		local mainFilename = suite._base_dir .. "/" .. gen.GetMainFilename(name)

		local hFile = io.open(mainFilename, "w+")
		gen.WritePreamble(hFile, name)
		
		WriteIncludes(hFile, suite)
		hFile:write("\n\n")
		
		--TODO: Write basic parameter data.
		
		WriteSuiteClass(hFile, suite)
		hFile:write("\n\n")
		
		
		hFile:close()

		--[[
		print("Suite:", gen.GetMainFilename(name), suite._base_dir)
		for testname, test in pairs(suite._tests) do
			print("\tTest name:", testname)
		end
		]]
	end
end

return codegen
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.