summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/scripts/bindings-d.lua
blob: 21c81faa876f81dc26612a3ac3c9b437ca710271 (plain) (blame)
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
local codegen = require "codegen"
local idl = codegen.idl "bgfx.idl"

local d_types_template = [[
/*
 *
 * AUTO GENERATED! DO NOT EDIT!
 *
 */

module bindbc.bgfx.types;

public import core.stdc.stdarg : va_list;

enum expandEnum(EnumType, string fqnEnumType = EnumType.stringof) = (){
	string expandEnum;
	foreach(m; __traits(allMembers, EnumType)){
		expandEnum ~= "alias " ~ m ~ " = " ~ fqnEnumType ~ "." ~ m ~ ";";
	}
	return expandEnum;
}();

extern(C) @nogc nothrow:

$version

alias bgfx_view_id_t = ushort;

//NOTE: TEMPORARY fix to some missing preprocessor function-macros...
static BGFX_STATE_BLEND_FUNC_SEPARATE(ulong _srcRGB, ulong _dstRGB, ulong _srcA, ulong _dstA){
	return (0UL
			| ( ( cast(ulong)_srcRGB | ( cast(ulong)_dstRGB<<4) )   )
			| ( ( cast(ulong)_srcA   | ( cast(ulong)_dstA  <<4) )<<8)
		);
}

/// Blend equation separate.
static BGFX_STATE_BLEND_EQUATION_SEPARATE(ulong _equationRGB, ulong _equationA){ return ( cast(ulong)_equationRGB | (cast(ulong)_equationA<<3) ); }

/// Blend function.
static BGFX_STATE_BLEND_FUNC(ulong _src, ulong _dst){ return BGFX_STATE_BLEND_FUNC_SEPARATE(_src, _dst, _src, _dst); }

/// Blend equation.
static BGFX_STATE_BLEND_EQUATION(ulong _equation){ return BGFX_STATE_BLEND_EQUATION_SEPARATE(_equation, _equation); }

/// Utility predefined blend modes.

/// Additive blending.
static BGFX_STATE_BLEND_ADD(){ return (0 | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE)); }

/// Alpha blend.
static BGFX_STATE_BLEND_ALPHA(){ return (0 | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)); }

$types
]]

local d_funcs_template = [[
/*
 *
 * AUTO GENERATED! DO NOT EDIT!
 *
 */

module bindbc.bgfx.funcs;

private import bindbc.bgfx.types;

extern(C) @nogc nothrow:

version(BindBgfx_Static)
{
	$funcs_static
}
else
{
	__gshared
	{
		$funcs_dynamic
	}
}
]]

local function hasPrefix(str, prefix)
	return prefix == "" or str:sub(1, #prefix) == prefix
end

local function hasSuffix(str, suffix)
	return suffix == "" or str:sub(-#suffix) == suffix
end

local function to_underscorecase(name)
	local tmp = {}
	for v in name:gmatch "[_%u][%l%d]*" do
		if v:byte() == 95 then	-- '_'
			v = v:sub(2)	-- remove _
		end
		tmp[#tmp+1] = v
	end
	return table.concat(tmp, "_")
end

local function convert_array(member)
	if string.find(member.array, "::") then
		local name = "bgfx_" .. to_underscorecase(string.gsub(member.array, "::.*", "")):lower() .. "_t"
		return "[" .. name .. ".BGFX_" .. to_underscorecase(member.array):upper() .. "]"
	else
		return member.array
	end
end

local function convert_type(arg)
	local ctype = arg.ctype:gsub("%s%*", "*")
	if arg.fulltype == "bx::AllocatorI*" or arg.fulltype == "CallbackI*" or arg.fulltype == "ReleaseFn" then
		ctype = "void*"
	elseif hasPrefix(ctype, "uint64_t") then
		ctype = ctype:gsub("uint64_t", "ulong")
	elseif hasPrefix(ctype, "int64_t") then
		ctype = ctype:gsub("int64_t", "long")
	elseif hasPrefix(ctype, "uint32_t") then
		ctype = ctype:gsub("uint32_t", "uint")
	elseif hasPrefix(ctype, "int32_t") then
		ctype = ctype:gsub("int32_t", "int")
	elseif hasPrefix(ctype, "uint16_t") then
		ctype = ctype:gsub("uint16_t", "ushort")
	elseif hasPrefix(ctype, "uint8_t") then
		ctype = ctype:gsub("uint8_t", "ubyte")
	elseif hasPrefix(ctype, "uintptr_t") then
		ctype = ctype:gsub("uintptr_t", "ulong")
	elseif hasPrefix(ctype, "const ") and hasSuffix(ctype, "*") then
		ctype = "const(" .. string.sub(ctype:gsub("const ", "", 1), 1, -2) .. ")*"
	end

	if arg.array ~= nil then
		ctype = ctype .. convert_array(arg)
	end

	return ctype
end

local function convert_struct_type(arg)
	return convert_type(arg)
end

local function convert_ret_type(arg)
	return convert_type(arg)
end

local function convert_name(arg)
	if arg == "debug" then
		return arg .. "_"
	end
	return arg
end

local function convert_struct_member(member)
	return convert_struct_type(member) .. " " .. convert_name(member.name)
end

local function gen_version()
	return "enum uint BGFX_API_VERSION = " .. (idl._version or 0) .. ";"
end

local converter = {}
local yield = coroutine.yield
local gen = {}

function gen.gen(template)
	local indent = ""
	local idx = 1;
	local r = template:gsub("$([a-zA-Z_]+)", function(what)
		local tmp = {}

		local ind_end = template:find("$"..what, idx, true)
		local ind_start = ind_end
		for j = 1, ind_end-1 do
			local i = 1+ind_end-1-j
			local c = string.sub(template, i, i)
			if c ~= ' ' and c ~= '\t' then
				ind_start = i
				break
			end
		end

		indent = string.sub(template, ind_start+1, ind_end-1)

		local what_idl = what:gsub("funcs_dynamic", "funcs"):gsub("funcs_static", "funcs")

		if (what == "version") then
			return gen_version()
		end

		for _, object in ipairs(idl[what_idl]) do
			local co = coroutine.create(converter[what])
			local any
			while true do
				local ok, v = coroutine.resume(co, object)
				assert(ok, debug.traceback(co, v))
				if not v then
					break
				end
				table.insert(tmp, v)
				any = true
			end
			if any and tmp[#tmp] ~= "" then
				table.insert(tmp, "")
			end
		end
		return table.concat(tmp, "\n" .. indent)
	end)
	return r
end

function gen.gen_types()
	return gen.gen(d_types_template)
end

function gen.gen_funcs()
	return gen.gen(d_funcs_template)
end

function converter.types(typ)
	if typ.comments ~= nil then
		if #typ.comments == 1 then
			yield("/// " .. typ.comments[1])
		else
			yield("/**")
			for _, comment in ipairs(typ.comments) do
				yield(" * " .. comment)
			end
			yield(" */")
		end
	end

	if typ.handle then
		yield("struct " .. typ.cname .. " { ushort idx; }")
	elseif typ.enum then
		local prefix = "BGFX_" .. to_underscorecase(string.gsub(typ.name, "::Enum", "")):upper()
		yield("enum " .. typ.cname)
		yield("{")
		for idx, enum in ipairs(typ.enum) do
			local comments = ""
			if enum.comment ~= nil then
				if #enum.comment == 1 then
					comments = " /// " .. enum.comment[1];
				else
					yield("\t/**")
					for _, comment in ipairs(enum.comment) do
						yield("\t * " .. comment)
					end
					yield("\t */")
				end
			end

			local enumname
			if enum.underscore then
				enumname = to_underscorecase(enum.name):upper()
			else
				enumname = enum.name:upper()
			end

			yield("\t" .. prefix .. "_" .. enumname .. "," .. comments)
		end
		yield("");
		--yield("\t" .. prefix .. "_COUNT = " .. #typ.enum)
		yield("\t" .. prefix .. "_COUNT")
		yield("}")
		yield("mixin(expandEnum!" .. typ.cname .. ");")

	elseif typ.bits ~= nil then
		local prefix = "BGFX_" .. to_underscorecase(typ.name):upper()
		local enumType = "uint"
		format = "%u"
		if typ.bits == 64 then
			format = "0x%016x"
			enumType = "ulong"
		elseif typ.bits == 32 then
			format = "0x%08x"
			enumType = "uint"
		elseif typ.bits == 16 then
			format = "0x%04x"
			enumType = "ushort"
		elseif typ.bits == 8 then
			format = "0x%02x"
			enumType = "ubyte"
		end

		for idx, flag in ipairs(typ.flag) do
			local value = flag.value
			if value ~= nil then
				value = string.format(flag.format or format, value)
			else
				for _, name in ipairs(flag) do
					local fixedname = prefix .. "_" .. to_underscorecase(name):upper()
					if value ~= nil then
						value = value .. " | " .. fixedname
					else
						value = fixedname
					end
				end
			end
			local comments = ""
			if flag.comment ~= nil then
				if #flag.comment == 1 then
					comments = " /// " .. flag.comment[1]
				else
					yield("/**")
					for _, comment in ipairs(flag.comment) do
						yield(" * " .. comment)
					end
					yield(" */")
				end
			end
			yield("enum " .. enumType .. " " .. prefix .. "_" .. to_underscorecase(flag.name):upper() .. " = " .. value .. ";" .. comments)
		end

		if typ.shift then
			local name = prefix .. "_SHIFT"
			local value = typ.shift
			local comments = ""
			if typ.desc then
				comments = string.format(" /// %s bit shift", typ.desc)
			end
			yield("enum " .. enumType .. " " .. name .. " = " .. value .. ";" .. comments)
		end
		if typ.range then
			local name = prefix .. "_MASK"
			local value = string.format(format, typ.mask)
			local comments = ""
			if typ.desc then
				comments = string.format(" /// %s bit mask", typ.desc)
			end
			yield("enum " .. enumType .. " " .. name .. " = " .. value .. ";" .. comments)
		end

		if typ.helper then
			yield(string.format(
				"%s %s (%s v) { return (v << %s) & %s; }",
				enumType,
				prefix,
				enumType,
				(prefix .. "_SHIFT"),
				(prefix .. "_MASK")))
		end
	elseif typ.struct ~= nil then
		yield("struct " .. typ.cname)
		yield("{")

		for _, member in ipairs(typ.struct) do
			local comments = ""
			if member.comment ~= nil then
				if #member.comment == 1 then
					comments = " /// " .. member.comment[1]
				else
					yield("\n\t/**")
					for _, comment in ipairs(member.comment) do
						yield("\t * " .. comment)
					end
					yield("\t */")
				end
			end
			yield("\t" .. convert_struct_member(member) .. ";" .. comments)
		end

		yield("}")
	end
end

function converter.funcs_static(func)
	return converter.funcs(func, true)
end

function converter.funcs_dynamic(func)
	return converter.funcs(func, false)
end

function converter.funcs(func, static)
	if func.cpponly then
		return
	end

	if func.comments ~= nil then
		yield("/**")
		for _, line in ipairs(func.comments) do
			local line = line:gsub("@remarks", "Remarks:")
			line = line:gsub("@remark", "Remarks:")
			line = line:gsub("@(%l)(%l+)", function(a, b) return a:upper()..b..":" end)
			yield(" * " .. line)
		end

		local hasParamsComments = false
		for _, arg in ipairs(func.args) do
			if arg.comment ~= nil then
				hasParamsComments = true
				break
			end
		end

		if hasParamsComments then
			yield(" * Params:")
		end

		for _, arg in ipairs(func.args) do
			if arg.comment ~= nil then
				yield(" * " .. convert_name(arg.name) .. " = " .. arg.comment[1])
				for i, comment in ipairs(arg.comment) do
					if (i > 1) then
						yield(" * " .. comment)
					end
				end
			end
		end

		yield(" */")
	end

	local args = {}
	if func.this ~= nil then
		args[1] = convert_type(func.this_type) .. " _this"
	end
	for _, arg in ipairs(func.args) do
		table.insert(args, convert_type(arg) .. " " .. convert_name(arg.name))
	end

	if static then
		yield(convert_ret_type(func.ret) .. " bgfx_" .. func.cname .. "(" .. table.concat(args, ", ") .. ");")
	else
		yield("alias da_bgfx_" .. func.cname .. " = " .. convert_ret_type(func.ret) .. " function(" .. table.concat(args, ", ") .. ");")
		yield("da_bgfx_" .. func.cname .. " bgfx_" .. func.cname .. ";")
	end
end

-- printtable("idl types", idl.types)
-- printtable("idl funcs", idl.funcs)

function gen.write(codes, outputfile)
	local out = assert(io.open(outputfile, "wb"))
	out:write(codes)
	out:close()
	print("Generating: " .. outputfile)
end

if (...) == nil then
	-- run `lua bindings-d.lua` in command line
	print(gen.gen_types())
	print(gen.gen_funcs())
end

return gen