Source

glLoadGen / modules / glload_util.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
local util = require "util"
local common = require "CommonStyle"

local data = {}
data.cpp = {}

data.internalPrefix = "_int_"
data.headerDirectory = "include/glload/"
data.includeDirectory = "glload/" --For inclusions from source.
data.sourceDirectory = "source/"



function data.CreateIncludeGuardWriters(tbl, name, Func)
	tbl["WriteBlockBegin" .. name] = function(hFile, ...)
		local includeGuard = Func(...)
		hFile:fmt("#ifndef %s\n", includeGuard)
		hFile:fmt("#define %s\n", includeGuard)
	end
	tbl["WriteBlockEnd" .. name] = function(hFile, ...)
		local includeGuard = Func(...)
		hFile:fmt("#endif /*%s*/\n", includeGuard)
	end
end


-----------------------------------------------
-- Header filenames
function data.GetLoaderBasename(spec, options)
	return spec.FilePrefix() .. "load.h"
end

function data.GetVersionCoreBasename(version, spec, options)
	return spec.FilePrefix() .. version:gsub("%.", "_") .. ".h"
end

function data.GetVersionCompBasename(version, spec, options)
	return spec.FilePrefix() .. version:gsub("%.", "_") .. "_comp.h"
end

function data.GetAllBasename(spec, options)
	return spec.FilePrefix() .. "all.h"
end

function data.GetCoreBasename(spec, options)
	return spec.FilePrefix() .. "core.h"
end

function data.GetTypeHeaderBasename(spec, options)
	return data.internalPrefix .. spec.FilePrefix() .. "type.h"
end

function data.GetExtsHeaderBasename(spec, options)
	return data.internalPrefix .. spec.FilePrefix() .. "exts.h"
end

function data.GetCoreHeaderBasename(version, spec, options)
	return data.internalPrefix .. spec.FilePrefix() .. version:gsub("%.", "_") .. ".h"
end

function data.GetRemHeaderBasename(version, spec, options)
	return data.internalPrefix .. spec.FilePrefix() .. version:gsub("%.", "_") .. "_rem.h"
end

function data.GetExtVariableName(extName, spec, options)
	return spec.FuncNamePrefix() .. "ext_" .. extName
end

function data.GetEnumeratorName(enum, spec, options)
	return spec.EnumNamePrefix() .. enum.name
end

function data.GetFuncTypedefName(func, spec, options)
	local temp = "PFN" .. spec.FuncNamePrefix() .. func.name .. "PROC"
	return temp:upper()
end

--Three parameters: the return value, the typedef name, and the params
function data.GetTypedefFormat(spec)
	return "typedef %s (" .. spec.GetCodegenPtrType() .. " * %s)(%s);\n"
end

function data.GetFuncPtrName(func, spec, options)
	return "_funcptr_" .. spec.FuncNamePrefix() .. func.name
end

function data.GetTypeHdrFileIncludeGuard(spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_TYPE" .. "_H"
end

function data.GetExtFileIncludeGuard(spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_EXTENSIONS" .. "_H"
end

function data.GetCoreHdrFileIncludeGuard(version, spec, options, removed)
	if(removed) then
		return spec.GetIncludeGuardString() .. "_GEN_CORE_REM" .. version:gsub("%.", "_") .. "_H"
	else
		return spec.GetIncludeGuardString() .. "_GEN_CORE_" .. version:gsub("%.", "_") .. "_H"
	end
end

function data.GetInclFileIncludeGuard(version, spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_" .. version:gsub("%.", "_") .. "_H"
end

function data.GetInclFileCompIncludeGuard(version, spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_" .. version:gsub("%.", "_") .. "COMP_H"
end

function data.GetInclFileAllIncludeGuard(spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_ALL_H"
end

function data.GetInclFileCoreIncludeGuard(spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_CORE_H"
end

function data.GetInclFileLoaderIncludeGuard(spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_LOAD_FUNCTIONS_H"
end

function data.GetBeginExternBlock()
	return [[
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus*/
]]
end

function data.GetEndExternBlock()
	return [[
#ifdef __cplusplus
}
#endif /*__cplusplus*/
]]
end

function data.WriteLoaderFuncBegin(hFile, funcName)
	hFile:fmt("static int %s()\n", funcName)
	hFile:write("{\n")
	hFile:inc()
	hFile:write("int numFailed = 0;\n")
end

function data.WriteLoaderFuncEnd(hFile)
	hFile:write("return numFailed;\n")
	hFile:dec()
	hFile:write("}\n")
end

function data.GetLoadExtensionFuncName(extName, spec, options)
	return "LoadExt_" .. extName
end

function data.GetLoadCoreFuncName(version, spec, options)
	return "LoadCore_Version_" .. version:gsub("%.", "_")
end

function data.GetLoadCoreCompFuncName(version, spec, options)
	return "LoadCore_Version_" .. version:gsub("%.", "_") .. "_Comp"
end

function data.GetLoadAllCoreFuncName(version, spec, options)
	return "LoadVersion_" .. version:gsub("%.", "_")
end

function data.GetLoadAllCoreCompFuncName(version, spec, options)
	return "LoadVersion_" .. version:gsub("%.", "_") .. "_Comp"
end

function data.GetMapTableStructName(spec, options)
	return string.format("%s%sStrToExtMap", options.prefix, spec.DeclPrefix())
end

function data.GetInMainFuncLoader(hFile, func, spec, options)
	local ret = ""
	ret = ret .. string.format('%s = (%s)%s("%s%s");\n',
		data.GetFuncPtrName(func, spec, options),
		data.GetFuncTypedefName(func, spec, options),
		common.GetProcAddressName(spec),
		spec.FuncNamePrefix(), func.name)
	ret = ret .. string.format('if(!%s) return %s;\n',
		data.GetFuncPtrName(func, spec, options),
		"0")
	return ret
end


local hdr_extra_spec =
{
	wgl = "",
	glX = "",
	gl = [=[
/**
This function retrieves the major GL version number. Only works after LoadFunctions has been called.
**/
int $<prefix>GetMajorVersion();

/**
This function retrieves the minor GL version number. Only works after LoadFunctions has been called.
**/
int $<prefix>GetMinorVersion();

/**Returns non-zero if the current GL version is greater than or equal to the given version.**/
int $<prefix>IsVersionGEQ(int testMajorVersion, int testMinorVersion);
]=],
}

local hdr_desc =
{
	wgl = [[Loads function pointers for WGL extensions.]],
	glX = [[Loads function pointers for GLX extensions.]],
	gl = [[Loads function pointers for OpenGL. This function will also load the core OpenGL functions (ie: not in extensions). It will only load the version and profile specified by the current OpenGL context. So if you get a 3.2 compatibility context, then it will load only try to load 3.2 compatibility in addition to any available extensions.]],
}

local hdr_pattern = 
[=[
/**
\file
\brief C header to include if you want to initialize $<specname>.

**/

/**\addtogroup module_glload_cinter**/
/**@{**/

/**
\brief The loading status returned by the loading functions.

**/
enum
{
	$<prefix>LOAD_FAILED = 0,
	$<prefix>LOAD_SUCCEEDED,
};

#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus*/

/**
\brief Loads all of the function pointers available.

$<desc>

\return Will return $<prefix>LOAD_FAILED if the loading failed entirely and nothing was loaded. Returns $<prefix>LOAD_SUCCEEDED if the loading process worked as planned. If it is neither, then the (return value - $<prefix>LOAD_SUCCEEDED) is the number of core functions that fialed to load.
**/
int $<prefix>LoadFunctions($<params>);

$<extra>
/**@}**/

#ifdef __cplusplus
}
#endif /*__cplusplus*/
]=]

function data.GetLoaderHeaderString(spec, options)
	local ret = hdr_pattern
	ret = ret:gsub("%$%<extra%>", hdr_extra_spec[options.spec])
	ret = ret:gsub("%$%<specname%>", spec.DisplayName())
	ret = ret:gsub("%$%<prefix%>", spec.DeclPrefix())
	ret = ret:gsub("%$%<desc%>", hdr_desc[options.spec])
	ret = ret:gsub("%$%<params%>", spec.GetLoaderParams())
	return ret
end


----------------------------------------------------------------------------
-- CPP-specific
data.cpp = util.DeepCopyTable(data)

function data.cpp.GetLoadTestBasename(spec, options)
	return data.internalPrefix .. "load_test.hpp"
end

function data.cpp.GetLoaderBasename(spec, options)
	return spec.FilePrefix() .. "load.hpp"
end

function data.cpp.GetVersionCoreBasename(version, spec, options)
	return spec.FilePrefix() .. version:gsub("%.", "_") .. ".hpp"
end

function data.cpp.GetVersionCompBasename(version, spec, options)
	return spec.FilePrefix() .. version:gsub("%.", "_") .. "_comp.hpp"
end

function data.cpp.GetAllBasename(spec, options)
	return spec.FilePrefix() .. "all.hpp"
end

function data.cpp.GetCoreBasename(spec, options)
	return spec.FilePrefix() .. "core.hpp"
end

function data.cpp.GetTypeHeaderBasename(spec, options)
	return data.internalPrefix .. spec.FilePrefix() .. "type.hpp"
end

function data.cpp.GetExtsHeaderBasename(spec, options)
	return data.internalPrefix .. spec.FilePrefix() .. "exts.hpp"
end

function data.cpp.GetCoreHeaderBasename(version, spec, options)
	return data.internalPrefix .. spec.FilePrefix() .. version:gsub("%.", "_") .. ".hpp"
end

function data.cpp.GetRemHeaderBasename(version, spec, options)
	return data.internalPrefix .. spec.FilePrefix() .. version:gsub("%.", "_") .. "_rem.hpp"
end

function data.cpp.GetLoadTestFileIncludeGuard(spec, options)
	return "GENERATED_LOAD_TEST_HPP"
end

function data.cpp.GetTypeHdrFileIncludeGuard(spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_TYPE" .. "_HPP"
end

function data.cpp.GetExtFileIncludeGuard(spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_EXTENSIONS" .. "_HPP"
end

function data.cpp.GetCoreHdrFileIncludeGuard(version, spec, options, removed)
	if(removed) then
		return spec.GetIncludeGuardString() .. "_GEN_CORE_REM" .. version:gsub("%.", "_") .. "_HPP"
	else
		return spec.GetIncludeGuardString() .. "_GEN_CORE_" .. version:gsub("%.", "_") .. "_HPP"
	end
end

function data.cpp.GetInclFileIncludeGuard(version, spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_" .. version:gsub("%.", "_") .. "_HPP"
end

function data.cpp.GetInclFileCompIncludeGuard(version, spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_" .. version:gsub("%.", "_") .. "COMP_HPP"
end

function data.cpp.GetInclFileAllIncludeGuard(spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_ALL_HPP"
end

function data.cpp.GetInclFileCoreIncludeGuard(spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_CORE_HPP"
end

function data.cpp.GetInclFileLoaderIncludeGuard(spec, options)
	return spec.GetIncludeGuardString() .. "_GEN_LOAD_FUNCTIONS_HPP"
end

function data.cpp.GetExtVariableName(extName, spec, options)
	return "var_" .. extName
end

function data.cpp.WriteNamespaceBegin(hFile, namespace)
	hFile:fmt("namespace %s\n", namespace)
	hFile:write("{\n")
	hFile:inc()
end

function data.cpp.WriteNamespaceEnd(hFile)
	hFile:dec()
	hFile:write("}\n")
end

function data.cpp.LoadTestClassDef()
	return [[
class LoadTest
{
private:
	//Safe bool idiom. Joy!
	typedef void (LoadTest::*bool_type)() const;
	void big_long_name_that_really_doesnt_matter() const {}
	
public:
	operator bool_type() const
	{
		return m_isLoaded ? &LoadTest::big_long_name_that_really_doesnt_matter : 0;
	}
	
	int GetNumMissing() const {return m_numMissing;}
	
	LoadTest() : m_isLoaded(false), m_numMissing(0) {}
	LoadTest(bool isLoaded, int numMissing) : m_isLoaded(isLoaded), m_numMissing(isLoaded ? numMissing : 0) {}
private:
	bool m_isLoaded;
	int m_numMissing;
};
]]
end

function data.cpp.GetFuncTypedefName(func, spec, options)
	local temp = "Proc_" .. spec.FuncNamePrefix() .. func.name
	return temp
end

function data.cpp.GetFuncPtrTypedefNamespace()
	return "_detail"
end

function data.cpp.GetCppEnumName(enum)
	return common.GetCppEnumName(enum)
end

local cpp_hdr_extra_spec =
{
	wgl = "",
	glX = "",
	gl = [=[
		/**
		This function retrieves the major GL version number. Only works after LoadFunctions has been called.
		**/
		int GetMajorVersion();

		/**
		This function retrieves the minor GL version number. Only works after LoadFunctions has been called.
		**/
		int GetMinorVersion();

		/**Returns non-zero if the current GL version is greater than or equal to the given version.**/
		int IsVersionGEQ(int testMajorVersion, int testMinorVersion);
]=],
}

local hdr_pattern = 
[=[
/**
\file
\brief C++ header to include if you want to initialize $<specname>.

**/

#include "$<loadtest>"

///\addtogroup module_glload_cppinter
///@{

///The core namespace for the C++ interface for the OpenGL initialization functions.
namespace glload
{
	/**
	\brief Loads all of the function pointers available.

$<desc>

	\return A sys::LoadTest object that defines whether the loading was successful.
	**/
	glload::LoadTest LoadFunctions($<params>);

$<extra>
}
///@}
]=]

function data.cpp.GetLoaderHeaderString(spec, options)
	local ret = hdr_pattern
	ret = ret:gsub("%$%<extra%>", cpp_hdr_extra_spec[options.spec])
	ret = ret:gsub("%$%<specname%>", spec.DisplayName())
	ret = ret:gsub("%$%<prefix%>", spec.DeclPrefix())
	ret = ret:gsub("%$%<desc%>", hdr_desc[options.spec])
	ret = ret:gsub("%$%<params%>", spec.GetLoaderParams())
	ret = ret:gsub("%$%<funcspec%>", spec.FuncNamePrefix())	
	ret = ret:gsub("%$<loadtest>", data.cpp.GetLoadTestBasename(spec, options))
	return ret
end





return data
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.