-
Notifications
You must be signed in to change notification settings - Fork 44
/
util.lua
431 lines (379 loc) · 9.34 KB
/
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
-- A util library for bgfx examples
local util = {}
local bgfx = require "bgfx"
local math3d = require "math3d"
local adapter = require "mathadapter"
local function save_ppm(filename, data, width, height, pitch)
local f = assert(io.open(filename, "wb"))
f:write(string.format("P3\n%d %d\n255\n",width, height))
local line = 0
for i = 0, height-1 do
for j = 0, width-1 do
local r,g,b,a = string.unpack("BBBB",data,i*pitch+j*4+1)
f:write(r," ",g," ",b," ")
line = line + 1
if line > 8 then
f:write "\n"
line = 0
end
end
end
f:close()
end
local function save_screenshot(filename)
local name , width, height, pitch, data = bgfx.get_screenshot()
if name then
local size = #data
if size < width * height * 4 then
-- not RGBA
return
end
print("Save screenshot to ", filename)
save_ppm(filename, data, width, height, pitch)
end
end
local shader_path
local function init_shader_path(caps)
local path = {
NOOP = "dx9",
DIRECT3D9 = "dx9",
DIRECT3D11 = "dx11",
DIRECT3D12 = "dx11",
GNM = "pssl",
METAL = "metal",
OPENGL = "glsl",
OPENGLES = "essl",
VULKAN = "spirv",
}
shader_path = "shaders/".. (assert(path[caps.rendererType])) .."/"
end
do
local function load_shader(name)
local filename = shader_path .. name .. ".bin"
local f = assert(io.open(filename, "rb"))
local data = f:read "a"
f:close()
local h = bgfx.create_shader(data)
bgfx.set_name(h, filename)
return h
end
local function load_shader_uniforms(name)
local h = load_shader(name)
local uniforms = bgfx.get_shader_uniforms(h)
return h, uniforms
end
local function uniform_info(uniforms, handles)
for _, h in ipairs(handles) do
local name, type, num = bgfx.get_uniform_info(h)
if uniforms[name] == nil then
uniforms[name] = { handle = h, name = name, type = type, num = num }
end
end
end
local function programLoadEx(vs,fs, uniform)
local vsid, u1 = load_shader_uniforms(vs)
local fsid, u2
if fs then
fsid, u2 = load_shader_uniforms(fs)
end
uniform_info(uniform, u1)
if u2 then
uniform_info(uniform, u2)
end
return bgfx.create_program(vsid, fsid, true), uniform
end
function util.programLoad(vs,fs, uniform)
if uniform then
return programLoadEx(vs,fs, uniform)
else
local vsid = load_shader(vs)
local fsid = fs and load_shader(fs)
return bgfx.create_program(vsid, fsid, true)
end
end
function util.computeLoad(cs)
local csid = load_shader(cs)
return bgfx.create_program(csid, true)
end
function util.shaderLoad(s)
return load_shader(s)
end
end
do
local mesh_decode = {}
local vb_header = "<" .. string.rep("f", 4+6+16)
-- local vb_data = { "!", "", nil, nil }
-- local ib_data = { "", nil, nil }
local function read_mesh_header(group, data, offset)
local tmp = { string.unpack(vb_header, data, offset) }
group.sphere = { table.unpack(tmp,1,4) }
group.aabb = { table.unpack(tmp,5,10) }
group.obb = { table.unpack(tmp,11,26) }
return tmp[27]
end
mesh_decode["VB \1"] = function(mesh, group, data, offset)
offset = read_mesh_header(mesh, data, offset)
local stride, numVertices
mesh.vdecl, stride, offset = bgfx.vertex_layout(data, offset)
numVertices, offset = string.unpack("<I2", data, offset)
local size = stride * numVertices
local vb_data = bgfx.memory_buffer(data, offset, size)
group.vb = bgfx.create_vertex_buffer(vb_data, mesh.vdecl)
return offset + size
end
mesh_decode["IB \0"] = function(mesh, group, data, offset)
local numIndices
numIndices, offset = string.unpack("<I4", data, offset)
local size = numIndices * 2
local ib_data = bgfx.memory_buffer(data, offset, size)
group.ib = bgfx.create_index_buffer(ib_data)
return offset + size
end
mesh_decode["IBC\0"] = function(mesh, group, data, offset)
error "Unsupport Compressed IB"
end
mesh_decode["PRI\0"] = function(mesh, group, data, offset)
local material, num
material, num, offset = string.unpack("<s2I2", data, offset) -- no used
group.prim = {}
for i=1,num do
local p = {}
p.name, p.startIndex, p.numIndices, p.startVertex, p.numVertices, offset = string.unpack("<s2I4I4I4I4", data, offset)
offset = read_mesh_header(p, data, offset)
table.insert(group.prim, p)
end
local tmp = {}
for k,v in pairs(group) do
group[k] = nil
tmp[k] = v
end
table.insert(mesh.group, tmp)
return offset
end
function util.meshLoad(filename)
local f = assert(io.open(filename,"rb"))
local data = f:read "a"
f:close()
local mesh = { group = {} }
local offset = 1
local group = {}
while true do
local tag = data:sub(offset, offset+3)
if tag == "" then
break
end
local decoder = mesh_decode[tag]
if not decoder then
error ("Invalid tag " .. tag)
end
offset = decoder(mesh, group, data, offset + 4)
end
return mesh
end
end
function util.meshUnload(mesh)
for _,group in ipairs(mesh.group) do
bgfx.destroy(group.ib)
bgfx.destroy(group.vb)
end
end
function util.meshSubmit(mesh, id, prog)
local g = mesh.group
local n = #g
for i=1,n do
local group = g[i]
bgfx.set_index_buffer(group.ib)
bgfx.set_vertex_buffer(group.vb)
bgfx.submit(id, prog, 0, i ~= n and "" or "ivs")
end
end
function util.meshSubmitState(mesh, state, mtx)
bgfx.set_transform(mtx)
bgfx.set_state(state.state)
for _, texture in ipairs(state.textures) do
bgfx.set_texture(texture.stage,texture.sampler,texture.texture,texture.flags)
end
local g = mesh.group
local n = #g
for i=1,n do
local group = g[i]
bgfx.set_index_buffer(group.ib)
bgfx.set_vertex_buffer(group.vb)
bgfx.submit(state.viewId, state.program, 0, i ~= n and "" or "ivs")
end
end
function util.textureLoad(filename, info)
local f = assert(io.open(filename, "rb"))
local imgdata = f:read "a"
f:close()
local h = bgfx.create_texture(imgdata, info)
bgfx.set_name(h, filename)
return h
end
local function use_iup()
local init_flag
local iup = require "iuplua"
function util.init(args)
local canvas = assert(args.canvas)
local function init()
bgfx.init {
renderer = args.renderer,
format = args.format,
width = args.width,
height = args.height,
reset = args.reset,
debug = args.debug,
profile = args.profile,
pushlog = args.pushlog,
pushlog_context = args.pushlog_context,
loglevel = args.loglevel,
numBackBuffers = args.numBackBuffers,
maxFrameLatency = args.maxFrameLatency,
debugTextScale = args.debugTextScale,
-- platform data
ndt = args.ndt,
nwh = iup.GetAttributeData(canvas,"HWND"),
context = args.context,
backBuffer = args.backBuffer,
backBufferDS = args.backBufferDS,
}
util.caps = bgfx.get_caps()
math3d.set_homogeneous_depth(util.caps.homogeneousDepth)
init_shader_path(util.caps)
init_flag = true
bgfx.set_debug "T"
end
function canvas:resize_cb(w,h)
if init_flag == nil then
init()
if args.init then
args.init(w,h)
end
init_flag = true
end
if args.resize then
args.resize(w,h)
end
end
local debug
function canvas:keypress_cb(key, press)
if press == 0 then
return
end
if key == iup.K_F1 then
debug = not debug
bgfx.set_debug(debug and "ST" or "T")
elseif key == iup.K_F12 then
bgfx.request_screenshot()
end
end
end
function util.run(f)
iup.SetIdle(function ()
assert(init_flag)
save_screenshot "screenshot.ppm"
local ok , err = xpcall(f, debug.traceback)
if not ok then
print(err)
iup.SetIdle()
end
return iup.DEFAULT
end)
iup.MainLoop()
iup.Close()
if init_flag then
bgfx.shutdown()
end
end
end
local function use_sdl()
local init_flag
local sdl = require "sdlwnd"
local EVENT = {}
function util.init(args)
sdl.init(args)
local function init()
-- prevent creation of a renderer thread
bgfx.set_platform_data {
nwh = sdl.handle()
}
bgfx.render_frame()
bgfx.init {
renderer = args.renderer,
format = args.format,
width = args.width,
height = args.height,
reset = args.reset,
debug = args.debug,
profile = args.profile,
pushlog = args.pushlog,
pushlog_context = args.pushlog_context,
loglevel = args.loglevel,
numBackBuffers = args.numBackBuffers,
maxFrameLatency = args.maxFrameLatency,
-- platform data
ndt = args.ndt,
nwh = sdl.handle(),
context = args.context,
backBuffer = args.backBuffer,
backBufferDS = args.backBufferDS,
}
util.caps = bgfx.get_caps()
math3d.set_homogeneous_depth(util.caps.homogeneousDepth)
init_shader_path(util.caps)
init_flag = true
bgfx.set_debug "T"
end
init()
if args.init then
args.init()
end
EVENT.RESIZE = args.resize
end
function EVENT.QUIT()
return true
end
do
local debug
function EVENT.KEY(key, press)
if not press then
return
end
if key == "F1" then
debug = not debug
bgfx.set_debug(debug and "ST" or "T")
elseif key == "F12" then
bgfx.request_screenshot()
end
end
end
function EVENT.MOTION(x, y)
end
function EVENT.BUTTON(x, y, button, pressed, click)
end
local function dispatch(name, ...)
if name then
return not EVENT[name](...)
else
return true
end
end
function util.run(f)
while dispatch(sdl.event()) do
save_screenshot "screenshot.ppm"
local ok , err = xpcall(f, debug.traceback)
if not ok then
print(err)
break
end
sdl.frame()
end
bgfx.shutdown()
end
end
if SDL then
use_sdl()
else
use_iup()
end
return util