glLoadGen / modules / StylePointerC.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
local common = require "CommonStyle"
local struct = require "Structure"
local util = require "util"


local my_style = {}
my_style.header = {}
my_style.source = {}

function my_style.WriteLargeHeading(hFile, headingName)
	hFile:write("/*", string.rep("*", #headingName), "*/\n")
	hFile:write("/*", headingName, "*/\n")
end

function my_style.WriteSmallHeading(hFile, headingName)
	hFile:write("/*", headingName, "*/\n")
end

----------------------------------------------------------------
-- Header file construction

function my_style.header.GetFilename(basename, options)
	return basename .. ".h"
end


local function GetIncludeGuard(hFile, spec, options)
	local str = "POINTER_C_GENERATED_HEADER_" ..
		spec.GetIncludeGuardString() .. "_H"

	if(#options.prefix > 0) then
		return options.prefix:upper() .. "_" .. str
	end
	
	return str
end

function my_style.header.WriteBlockBeginIncludeGuard(hFile, spec, options)
	local inclGuard = GetIncludeGuard(hFile, spec, options)
	
	hFile:fmt("#ifndef %s\n", inclGuard)
	hFile:fmt("#define %s\n", inclGuard)
end

function my_style.header.WriteBlockEndIncludeGuard(hFile, spec, options)
	local inclGuard = GetIncludeGuard(hFile, spec, options)
	
	hFile:fmt("#endif //%s\n", inclGuard)
end

function my_style.header.WriteInit(hFile, spec, options)
	hFile:rawwrite(spec.GetHeaderInit())
end

function my_style.header.WriteStdTypedefs(hFile, specData, options)
	local defArray = common.GetStdTypedefs()
	
	--Use include-guards for the typedefs, since they're common among
	--headers in this style.
	
	hFile:write("#ifndef GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS\n")
	hFile:write("#define GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS\n")
	hFile:write("\n")
	hFile:inc()
	
	for _, def in ipairs(defArray) do
		hFile:write(def)
	end
	
	hFile:dec()
	hFile:write("\n")
	hFile:write("#endif /*GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS*/\n")
	hFile:write("\n")
end

function my_style.header.WriteSpecTypedefs(hFile, specData, options)
	hFile:push()
	common.WritePassthruData(hFile, specData.funcData.passthru)
	hFile:pop()
end

function my_style.header.WriteBlockBeginDecl(hFile, specData, options)
	common.WriteExternCStart(hFile)
end

function my_style.header.WriteBlockBeginExtVarDecl(hFile, spec, options)
end

function my_style.header.WriteBlockEndExtVarDecl(hFile, spec, options)
end

local function GetExtVariableName(extName, spec, options)
	return options.prefix .. spec.DeclPrefix() .. "ext_" .. extName
end

function my_style.header.WriteExtVariableDecl(hFile, extName, specData, spec, options)
	hFile:write("extern int ", GetExtVariableName(extName, spec, options), ";\n");
end

function my_style.header.WriteBlockBeginEnumDecl(hFile, specData, options) end

function my_style.header.WriteBlockEndEnumDecl(hFile, specData, options) end

local function GetEnumName(enum, spec, options)
	return spec.EnumNamePrefix() .. enum.name
end

function my_style.header.WriteEnumDecl(hFile, enum, enumTable, spec, options, enumSeen)
	if(enumSeen[enum.name]) then
		hFile:fmt("/*Copied %s%s From: %s*/\n",
			spec.EnumNamePrefix(),
			enum.name,
			enumSeen[enum.name])
	else
		hFile:fmt("#define %s %s\n",
			GetEnumName(enum, spec, options),
			common.ResolveEnumValue(enum, enumTable))
	end
end

function my_style.header.WriteBlockBeginFuncDecl(hFile, specData, options)
end

local function GetFuncPtrName(func, spec, options)
	return options.prefix .. "_ptrc_".. spec.FuncNamePrefix() .. func.name
end

local function GetFuncPtrType(hFile, func, typemap, spec, options)
	return string.format("%s (%s *)(%s)",
		common.GetFuncReturnType(func, typemap),
		spec.GetCodegenPtrType(),
		common.GetFuncParamList(func, typemap))
end

local function GetFuncPtrDef(hFile, func, typemap, spec, options)
	return string.format("%s (%s *%s)(%s)",
		common.GetFuncReturnType(func, typemap),
		spec.GetCodegenPtrType(),
		GetFuncPtrName(func, spec, options),
		common.GetFuncParamList(func, typemap))
end

function my_style.header.WriteFuncDecl(hFile, func, typemap, spec, options)
	--Declare the function pointer.
	hFile:write("extern ",
		GetFuncPtrDef(hFile, func, typemap, spec, options),
		";\n")
	
	--#define it to the proper OpenGL name.
	hFile:fmt("#define %s %s\n",
		common.GetOpenGLFuncName(func, spec),
		GetFuncPtrName(func, spec, options))
end

function my_style.header.WriteBlockEndFuncDecl(hFile, specData, options)
end

function my_style.header.WriteBlockBeginExtFuncDecl(hFile, extName,
	spec, options)
	hFile:fmt("#ifndef %s\n", spec.ExtNamePrefix() .. extName)
	hFile:fmt("#define %s 1\n", spec.ExtNamePrefix() .. extName)
end

function my_style.header.WriteBlockEndExtFuncDecl(hFile, extName,
	spec, options)
	hFile:fmt("#endif /*%s*/ \n", spec.ExtNamePrefix() .. extName)
end

function my_style.header.WriteBlockBeginSysDecl(hFile, spec, options)
end

function my_style.header.WriteBlockEndSysDecl(hFile, spec, options)
end

local function GetStatusCodeEnumName(spec, options)
	return string.format("%s%sLoadStatus", options.prefix, spec.DeclPrefix())
end

local function GetStatusCodeName(name, spec, options)
	return string.format("%s%s%s", options.prefix, spec.DeclPrefix(), name)
end

function my_style.header.WriteUtilityDecls(hFile, spec, options)
	hFile:fmt("enum %s\n", GetStatusCodeEnumName(spec, options))
	hFile:write("{\n")
	hFile:inc()
		hFile:write(GetStatusCodeName("LOAD_FAILED", spec, options), " = 0,\n")
		hFile:write(GetStatusCodeName("LOAD_SUCCEEDED", spec, options), " = 1,\n")
	hFile:dec()
	hFile:write("};\n")
end

local function DecorateFuncName(name, spec, options)
	return string.format("%s%s%s", options.prefix, spec.DeclPrefix(), name)
end

local function GetLoaderFuncName(spec, options)
	return DecorateFuncName("LoadFunctions", spec, options)
end

function my_style.header.WriteMainLoaderFuncDecl(hFile, spec, options)
	hFile:fmt("int %s(%s);\n",
		GetLoaderFuncName(spec, options),
		spec.GetLoaderParams())
end

function my_style.header.WriteVersioningFuncDecls(hFile, spec, options)
	--Only for GL
	if(options.spec ~= "gl") then
		return
	end
	
	hFile:fmt("int %s();\n", DecorateFuncName("GetMinorVersion", spec, options))
	hFile:fmt("int %s();\n", DecorateFuncName("GetMajorVersion", spec, options))
	hFile:fmt("int %s(int majorVersion, int minorVersion);\n",
		DecorateFuncName("IsVersionGEQ", spec, options))
end

function my_style.header.WriteBlockEndDecl(hFile, specData, options)
	common.WriteExternCEnd(hFile)
end

--------------------------------------------------
-- Source file construction functions.

function my_style.source.GetFilename(basename, options)
	return basename .. ".c"
end

function my_style.source.WriteIncludes(hFile, basename, spec, options)
	hFile:writeblock([[
#include <stdlib.h>
#include <string.h>
]])
	local base = util.ParsePath(my_style.header.GetFilename(basename, options))
	hFile:fmt('#include "%s"\n', base)
end

function my_style.source.WriteLoaderData(hFile, spec, options)
	hFile:writeblock(spec.GetLoaderFunc())
end

function my_style.source.WriteBlockBeginDef(hFile, spec, options) end
function my_style.source.WriteBlockEndDef(hFile, spec, options) end

function my_style.source.WriteBlockBeginExtVarDef(hFile, spec, options)
end

function my_style.source.WriteBlockEndExtVarDef(hFile, spec, options)
end

function my_style.source.WriteExtVariableDef(hFile, extName, specData, spec, options)
	hFile:fmt("int %s = %s;\n", GetExtVariableName(extName, spec, options),
		GetStatusCodeName("LOAD_FAILED", spec, options));
end

function my_style.source.WriteBlockBeginExtFuncDef(hFile, extName, spec, options)
end

function my_style.source.WriteBlockEndExtFuncDef(hFile, extName, spec, options)
end

function my_style.source.WriteFuncDef(hFile, func, typemap, spec, options, funcSeen)
	--Declare the function pointer, if not already declared.
	if(funcSeen[func.name]) then return end
	
	hFile:fmt("%s = NULL;\n",
		GetFuncPtrDef(hFile, func, typemap, spec, options))
end

local function GetExtLoaderFuncName(extName, spec, options)
	return "Load_" .. extName;
end

function my_style.source.WriteBlockBeginExtLoader(hFile, extName, spec, options)
	hFile:fmt("static int %s()\n", GetExtLoaderFuncName(extName, spec, options))
	hFile:write("{\n")
	hFile:inc()
	hFile:write("int numFailed = 0;\n")
end

function my_style.source.WriteBlockEndExtLoader(hFile, extName, spec, options)
	hFile:write("return numFailed;\n")
	hFile:dec()
	hFile:write("}\n")
end

function my_style.source.WriteExtFuncLoader(hFile, func, typemap, spec, options)
	hFile:fmt('%s = (%s)%s("%s%s");\n',
		GetFuncPtrName(func, spec, options),
		GetFuncPtrType(hFile, func, typemap, spec, options),
		common.GetProcAddressName(spec),
		spec.FuncNamePrefix(), func.name)
	hFile:fmt('if(!%s) numFailed++;\n', GetFuncPtrName(func, spec, options))
end

function my_style.source.WriteBlockBeginCoreFuncDef(hFile, spec, options)
end

function my_style.source.WriteBlockEndCoreFuncDef(hFile, spec, options)
end

local function GetCoreLoaderFuncName(spec, options)
	return "Load_Version_" .. options.version:gsub("%.", "_")
end

function my_style.source.WriteBlockBeginCoreLoader(hFile, spec, options)
	hFile:fmt("static int %s()\n", GetCoreLoaderFuncName(spec, options))
	hFile:write("{\n")
	hFile:inc()
	hFile:write("int numFailed = 0;\n")
end

function my_style.source.WriteBlockEndCoreLoader(hFile, version, spec, options)
	hFile:write("return numFailed;\n")
	hFile:dec()
	hFile:write("}\n")
end

function my_style.source.WriteCoreFuncLoader(hFile, func, typemap, spec, options)
	hFile:fmt('%s = (%s)%s("%s%s");\n',
		GetFuncPtrName(func, spec, options),
		GetFuncPtrType(hFile, func, typemap, spec, options),
		common.GetProcAddressName(spec),
		spec.FuncNamePrefix(), func.name)

	--Special hack for DSA_EXT functions in core functions.
	--They do not count against the loaded count.
	if(func.name:match("EXT$")) then
		hFile:write("/*An EXT_direct_state_access-based function. Don't count it.*/")
	else
		hFile:fmt('if(!%s) numFailed++;\n', GetFuncPtrName(func, spec, options))
	end
end

function my_style.source.WriteExtStringFuncDef(hFile, specData, spec, options, funcSeen)
	if(funcSeen[spec.GetExtStringFuncName()]) then
		return
	end
	
	--Check to see if its something we have to load.
	local function FindFuncName(funcName)
		for _, func in ipairs(specData.funcData.functions) do
			if(func.name == funcName) then
				return func
			end
		end
		
		return nil
	end
	
	local extStringFunc = FindFuncName(spec.GetExtStringFuncName())

	if(extStringFunc) then
		hFile:write("\n")
		hFile:fmt("static %s = NULL;\n",
			GetFuncPtrDef(hFile, extStringFunc, specData.typemap, spec, options))
		hFile:write("\n")
	end
end

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

local function GetMapTableVarName()
	return "ExtensionMap"
end

function my_style.source.WriteBlockBeginSysDef(hFile, spec, options)
end

function my_style.source.WriteBlockEndSysDef(hFile, spec, options)
end

function my_style.source.WriteUtilityDefs(hFile, specData, spec, options)
	--Write the struct for the mapping table.
	local mapStructName = string.format("%s%sStrToExtMap_s", options.prefix, spec.DeclPrefix())
	common.WriteCMappingTable(hFile, specData, spec, options,
		GetMapTableStructName(spec, options),
		GetMapTableVarName(),
		GetExtVariableName,
		GetExtLoaderFuncName)
	hFile:write "\n"
	
	--Write function to find map entry by name.
	common.WriteCFindExtEntryFunc(hFile, specData, spec, options,
		GetMapTableStructName(spec, options),
		GetMapTableVarName())
	hFile:write "\n"

	--Write the function to clear the extension variables.
	common.WriteCClearExtensionVarsFunc(hFile, specData, spec, options,
		GetExtVariableName,
		GetStatusCodeName("LOAD_FAILED", spec, options))
	hFile:write "\n"
	
	--Write a function that loads an extension by name.
	common.WriteCLoadExtByNameFunc(hFile, specData, spec, options,
		GetMapTableStructName(spec, options),
		GetStatusCodeName("LOAD_SUCCEEDED", spec, options))
	hFile:write "\n"
end

local function WriteAncillaryFuncs(hFile, specData, spec, options)
	local indexed = spec.GetIndexedExtStringFunc(options);
	if(indexed) then
		common.FixupIndexedList(specData, indexed)
		hFile:writeblock(common.GetProcExtsFromExtListFunc(
			hFile, specData, spec, options,
			indexed, GetFuncPtrName, GetEnumName))
	else
		hFile:writeblock(common.GetProcessExtsFromStringFunc("LoadExtByName(%s)"))
	end
	
	hFile:write "\n"

	return indexed
end

local function WriteInMainFuncLoader(hFile, func, spec, options)
	hFile:fmt('%s = %s("%s%s");\n',
		GetFuncPtrName(func, spec, options),
		common.GetProcAddressName(spec),
		spec.FuncNamePrefix(), func.name)
	hFile:fmt('if(!%s) return %s;\n',
		GetFuncPtrName(func, spec, options),
		GetStatusCodeName("LOAD_FAILED", spec, options))
end


function my_style.source.WriteMainLoaderFunc(hFile, specData, spec, options)
	local indexed = WriteAncillaryFuncs(hFile, specData, spec, options)

	--Write the function that calls the extension and core loaders.
	hFile:fmt("int %s(%s)\n",
		GetLoaderFuncName(spec, options),
		spec.GetLoaderParams())
	hFile:write("{\n")
	hFile:inc()

	if(options.version) then
		hFile:write("int numFailed = 0;\n")
	end

	hFile:write("ClearExtensionVars();\n")
	hFile:write("\n")

	--Load the extension, using runtime-facilities to tell what is available.
	if(indexed) then
		WriteInMainFuncLoader(hFile, indexed[1], spec, options)
		WriteInMainFuncLoader(hFile, indexed[3], spec, options)
		hFile:write("\n")
		hFile:write("ProcExtsFromExtList();\n")
	else
		local extListName, needLoad = spec.GetExtStringFuncName()
		if(needLoad) then
			for _, func in ipairs(specData.funcData.functions) do
				if(extListName == func.name) then
					extListName = func
				end
			end
			
			WriteInMainFuncLoader(hFile, extListName, spec, options)
			
			extListName = GetFuncPtrName(extListName, spec, options);
		end

		local function EnumResolve(enumName)
			return GetEnumName(specData.enumtable[enumName], spec, options)
		end
		
		hFile:write "\n"
		hFile:fmt("ProcExtsFromExtString((const char *)%s(%s));\n",
			extListName,
			spec.GetExtStringParamList(EnumResolve))
	end
	
	if(options.version) then
		hFile:fmt("numFailed = %s();\n",
			GetCoreLoaderFuncName(spec, options))
		hFile:write "\n"
		
		hFile:fmtblock([[
if(numFailed == 0)
	return %s;
else
	return %s + numFailed;
]],
			GetStatusCodeName("LOAD_SUCCEEDED", spec, options),
			GetStatusCodeName("LOAD_SUCCEEDED", spec, options))
	else
		hFile:fmt("return %s;\n",
			GetStatusCodeName("LOAD_SUCCEEDED", spec, options))
	end
	
	hFile:dec()
	hFile:write("}\n")
end

function my_style.source.WriteVersioningFuncs(hFile, specData, spec, options)
	hFile:fmt("static int g_major_version = 0;\n")
	hFile:fmt("static int g_minor_version = 0;\n")
	hFile:write "\n"
	
	if(tonumber(options.version) >= 3.0) then
		hFile:writeblock([[
static void GetGLVersion()
{
	glGetIntegerv(GL_MAJOR_VERSION, &g_major_version);
	glGetIntegerv(GL_MINOR_VERSION, &g_minor_version);
}
]])
	else
		hFile:writeblock(common.GetParseVersionFromString())
		hFile:write "\n"
		
		hFile:writeblock([[
static void GetGLVersion()
{
	ParseVersionFromString(&g_major_version, &g_minor_version, glGetString(GL_VERSION));
}
]])
	end
	
	hFile:write "\n"
	hFile:fmt("int %s()\n", DecorateFuncName("GetMinorVersion", spec, options))
	hFile:writeblock([[
{
	if(g_major_version == 0)
		GetGLVersion();
	return g_major_version;
}
]])
	hFile:write "\n"

	hFile:fmt("int %s()\n", DecorateFuncName("GetMajorVersion", spec, options))
	hFile:writeblock([[
{
	if(g_major_version == 0) //Yes, check the major version to get the minor one.
		GetGLVersion();
	return g_minor_version;
}
]])
	hFile:write "\n"
	
	hFile:fmt("int %s(int majorVersion, int minorVersion)\n",
		DecorateFuncName("IsVersionGEQ", spec, options))
	hFile:writeblock([[
{
	if(g_major_version == 0)
		GetGLVersion();
		
	if(majorVersion > g_major_version) return 1;
	if(majorVersion < g_major_version) return 0;
	if(minorVersion >= g_minor_version) return 1;
	return 0;
}
]])

end



--------------------------------------------------
-- Style retrieval machinery

local function Create()
	return common.DeepCopyTable(my_style), common.GetStandardStructure()
end

return { Create = Create }
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.