-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathluabgfx.c
5568 lines (5148 loc) · 154 KB
/
luabgfx.c
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define LUA_LIB
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <bgfx/c99/bgfx.h>
#include <assert.h>
#include "luabgfx.h"
#include "simplelock.h"
#include "bgfx_interface.h"
#include "bgfx_alloc.h"
#include "transient_buffer.h"
#if BX_PLATFORM_ANDROID
#include <android/log.h>
#endif
#if BGFX_API_VERSION != 127
# error BGFX_API_VERSION mismatch
#endif
#if _MSC_VER > 0
#include <malloc.h>
# ifdef USING_ALLOCA_FOR_VLA
# define VLA(_TYPE, _VAR, _SIZE) _TYPE _VAR = (_TYPE*)_alloca(sizeof(_TYPE) * (_SIZE))
# else//!USING_ALLOCA_FOR_VLA
# define V(_SIZE) 4096
# endif //USING_ALLOCA_FOR_VLA
#else //!(_MSC_VER > 0)
# ifdef USING_ALLOCA_FOR_VLA
# define VLA(_TYPE, _VAR, _SIZE) _TYPE _VAR[(_SIZE)]
# else //!USING_ALLOCA_FOR_VLA
# define V(_SIZE) (_SIZE)
# endif //USING_ALLOCA_FOR_VLA
#endif //_MSC_VER > 0
// screenshot queue length
#define MAX_SCREENSHOT 16
// 64K log ring buffer
#define MAX_LOGBUFFER (64*1024)
static int tag_encoder = 0;
#define ENCODER ((void *)&tag_encoder)
struct encoder_holder {
bgfx_encoder_t *encoder;
};
static inline bgfx_encoder_t *
get_encoder(lua_State *L) {
#define DUMMY_ENCODER ((bgfx_encoder_t *)~0)
if (lua_rawgetp(L, LUA_REGISTRYINDEX, ENCODER) != LUA_TUSERDATA) {
luaL_error(L, "Call bgfx.encoder_init first");
}
struct encoder_holder *E = (struct encoder_holder *)lua_touserdata(L, -1);
lua_pop(L, 1);
bgfx_encoder_t * encoder = E->encoder;
if (encoder == NULL) {
luaL_error(L, "Call bgfx.encoder_begin first");
// never here
return DUMMY_ENCODER;
}
return encoder;
}
#define ENCODER_API(APINAME) \
static inline int APINAME##_(lua_State *L, bgfx_encoder_t *encoder); \
static int APINAME(lua_State *L) { return APINAME##_(L, NULL); } \
static int APINAME##_encoder(lua_State *L) { return APINAME##_(L, get_encoder(L)); } \
static inline int APINAME##_(lua_State *L, bgfx_encoder_t *encoder)
struct screenshot {
uint32_t width;
uint32_t height;
uint32_t pitch;
uint32_t size;
void *data;
char *name;
};
struct screenshot_queue {
spinlock_t lock;
unsigned int head;
unsigned int tail;
struct screenshot *q[MAX_SCREENSHOT];
};
struct log_cache {
spinlock_t lock;
uint64_t head;
uint64_t tail;
char log[MAX_LOGBUFFER];
};
typedef void (*bgfx_pushlog)(void* context, const char *file, uint16_t line, const char *format, va_list ap);
struct callback {
bgfx_callback_interface_t base;
struct screenshot_queue ss;
uint32_t filterlog;
bgfx_pushlog pushlog;
void* pushlog_context;
};
static int
memory_tostring(lua_State *L) {
struct memory *mem = (struct memory *)lua_touserdata(L, 1);
if (mem->constant) {
lua_getuservalue(L, 1);
size_t sz;
const char *str = lua_tolstring(L, -1, &sz);
if (str == (const char *)mem->data && sz == mem->size) {
return 1;
}
}
lua_pushlstring(L, mem->data, mem->size);
return 1;
}
// base 1
static int
memory_read(lua_State *L) {
struct memory *mem = (struct memory *)lua_touserdata(L, 1);
int index = luaL_checkinteger(L, 2)-1;
if (index < 0 || index >= mem->size) {
return 0;
}
uint8_t * data = (uint8_t *)mem->data;
lua_pushinteger(L, data[index]);
return 1;
}
static int
memory_size(lua_State *L){
struct memory *mem = (struct memory *)lua_touserdata(L, 1);
lua_pushinteger(L, mem->size);
return 1;
}
static int
memory_write(lua_State *L) {
struct memory *mem = (struct memory *)lua_touserdata(L, 1);
if (mem->constant) {
return luaL_error(L, "Can't write to constant memory object");
}
int index = luaL_checkinteger(L, 2)-1;
if (index < 0) {
return luaL_error(L, "Invalid index %d", index+1);
}
uint8_t * data = (uint8_t *)mem->data;
switch (lua_type(L, 3)) {
case LUA_TNUMBER :
if (index >= mem->size) {
return luaL_error(L, "Out of range %d/%d", index+1, (unsigned)mem->size);
}
data[index] = luaL_checkinteger(L, 3);
break;
case LUA_TSTRING: {
size_t sz;
const char * buf = lua_tolstring(L, 3, &sz);
if (index + sz > mem->size) {
return luaL_error(L, "Out of range (%d+%d)/%d", index+1, (unsigned)sz, (unsigned)mem->size);
}
memcpy(data+index, buf, sz);
break; }
default:
luaL_error(L, "Type error : %s (Need integer or string)", lua_typename(L, lua_type(L, 3)));
}
return 0;
}
static int
memory_keepalive(lua_State *L) {
lua_getuservalue(L, 1);
struct memory *mem = (struct memory *)lua_touserdata(L, -1);
if (mem->ref == 0)
return 0;
// keep alive
lua_newuserdatauv(L, 0, 1);
luaL_getmetatable(L, "BGFX_MEMORY_REF");
lua_setmetatable(L, -2);
lua_pushvalue(L, -2);
lua_setiuservalue(L, -2, 1);
return 0;
}
static int
memory_release(lua_State *L) {
struct memory *mem = (struct memory *)lua_touserdata(L, 1);
if (mem->ref == 0)
return 0;
// keep self alive
lua_newuserdatauv(L, 0, 1);
if (luaL_newmetatable(L, "BGFX_MEMORY_REF")) {
lua_pushcfunction(L, memory_keepalive);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
lua_pushvalue(L, 1);
lua_setiuservalue(L, -2, 1);
return 0;
}
static struct memory *
memory_new(lua_State *L) {
struct memory *mem = (struct memory *)lua_newuserdatauv(L, sizeof(*mem), 1);
mem->data = NULL;
mem->size = 0;
mem->ref = 0;
mem->constant = 0;
if (luaL_newmetatable(L, "BGFX_MEMORY")) {
luaL_Reg l[] = {
{ "__tostring", memory_tostring },
{ "__index", memory_read },
{ "__len", memory_size},
{ "__newindex", memory_write },
{ "__gc", memory_release },
{ NULL, NULL },
};
luaL_setfuncs(L, l, 0);
}
lua_setmetatable(L, -2);
return mem;
}
// pop data (data != NULL) from stack, and push memory object on the stack
static void *
newMemory(lua_State *L, void *data, size_t size) {
if (data == NULL) {
data = lua_newuserdatauv(L, size, 0);
}
struct memory *mem = memory_new(L);
lua_insert(L, -2);
if (lua_type(L, -1) == LUA_TSTRING) {
mem->constant = 1;
}
// mount data -> mem
lua_setiuservalue(L, -2, 1);
mem->data = data;
mem->size = size;
return data;
}
static void
releaseMemory(void *ptr, void *ud) {
(void)ptr; // unused ptr
struct memory * mem = (struct memory *)ud;
atom_dec(&mem->ref);
}
static const bgfx_memory_t *
bgfxMemory(lua_State *L, int idx) {
struct memory *mem = (struct memory *)luaL_checkudata(L, idx, "BGFX_MEMORY");
atom_inc(&mem->ref);
return BGFX(make_ref_release)(mem->data, mem->size, releaseMemory, (void *)mem);
}
static void *
getfield(lua_State *L, const char *key) {
lua_getfield(L, 1, key);
void * ud = lua_touserdata(L, -1);
lua_pop(L, 1);
return ud;
}
static int
getfield_int(lua_State *L, const char *key) {
lua_getfield(L, 1, key);
int v = lua_tointeger(L, -1);
lua_pop(L, 1);
return v;
}
static int
lsetPlatformData(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
bgfx_platform_data_t bpdt;
memset(&bpdt, 0, sizeof(bpdt));
bpdt.ndt = getfield(L, "ndt");
bpdt.nwh = getfield(L, "nwh");
bpdt.context = getfield(L, "context");
bpdt.backBuffer = getfield(L, "backBuffer");
bpdt.backBufferDS = getfield(L, "backBufferDS");
bpdt.type = (bgfx_native_window_handle_type_t)getfield_int(L, "type");
BGFX(set_platform_data)(&bpdt);
return 0;
}
static bgfx_renderer_type_t
renderer_type_id(lua_State *L, int index) {
const char * type = luaL_checkstring(L, index);
bgfx_renderer_type_t id;
#define RENDERER_TYPE_ID(x) else if (strcmp(type, #x) == 0) id = BGFX_RENDERER_TYPE_##x
if (0) ;
RENDERER_TYPE_ID(NOOP);
RENDERER_TYPE_ID(DIRECT3D11);
RENDERER_TYPE_ID(DIRECT3D12);
RENDERER_TYPE_ID(METAL);
RENDERER_TYPE_ID(VULKAN);
else return luaL_error(L, "Invalid renderer type %s", type);
return id;
}
static const char*
fatal_code_str(bgfx_fatal_t code) {
switch (code) {
case BGFX_FATAL_DEBUG_CHECK:
return "DebugCheck";
case BGFX_FATAL_INVALID_SHADER:
return "InvalidShader";
case BGFX_FATAL_UNABLE_TO_INITIALIZE:
return "UnableToInitialize";
case BGFX_FATAL_UNABLE_TO_CREATE_TEXTURE:
return "UnableToCreateTexture";
case BGFX_FATAL_DEVICE_LOST:
return "DeviceLost";
default:
return "Unknown";
}
}
static void
cb_fatal(bgfx_callback_interface_t *self, const char* filePath, uint16_t line, bgfx_fatal_t code, const char *str) {
#if BX_PLATFORM_ANDROID
__android_log_print(ANDROID_LOG_FATAL, "bgfx", "Fatal error at %s(%d): [%s] %s\n", filePath, line, fatal_code_str(code), str);
#else
fprintf(stderr, "Fatal error at %s(%d): [%s] %s\n", filePath, line, fatal_code_str(code), str);
fflush(stderr);
#endif
abort();
}
#define PREFIX(str, cstr) (strncmp(str, cstr"", sizeof(cstr)-1) == 0)
static int
trace_filter(const char *format, int level) {
if (level > 4)
return level;
if (!PREFIX(format, "BGFX "))
return 1;
if (level <= 1)
return 0;
format += 5; // skip "BGFX "
if (PREFIX(format, "ASSERT ")) {
return 2;
}
if (level <=2)
return 0;
if (PREFIX(format, "WARN ")) {
return 3;
}
if (level <=3)
return 0;
return 4;
}
static void
cb_trace_vargs(bgfx_callback_interface_t *self, const char *file, uint16_t line, const char *format, va_list ap) {
struct callback * cb = (struct callback *)self;
if (cb->filterlog > 0 && trace_filter(format, cb->filterlog)) {
if (cb->pushlog) {
cb->pushlog(cb->pushlog_context, file, line, format, ap);
return;
}
char tmp[MAX_LOGBUFFER];
int n = sprintf(tmp, "%s (%d): ", file, line);
n += vsnprintf(tmp+n, sizeof(tmp)-n, format, ap);
if (n > MAX_LOGBUFFER) {
// truncated
n = MAX_LOGBUFFER;
}
#if BX_PLATFORM_ANDROID
__android_log_write(ANDROID_LOG_INFO, "bgfx", tmp);
#else
fputs(tmp, stdout);
fflush(stdout);
#endif
}
}
// todo: bgfx callback
static void
cb_profiler_begin(bgfx_callback_interface_t *self, const char* _name, uint32_t _abgr, const char* _filePath, uint16_t _line) {
}
static void
cb_profiler_begin_literal(bgfx_callback_interface_t *self, const char* _name, uint32_t _abgr, const char* _filePath, uint16_t _line) {
}
static void
cb_profiler_end(bgfx_callback_interface_t *self) {
}
static uint32_t
cb_cache_read_size(bgfx_callback_interface_t *self, uint64_t id) {
return 0;
}
static bool
cb_cache_read(bgfx_callback_interface_t *self, uint64_t id, void *data, uint32_t size) {
return false;
}
static void
cb_cache_write(bgfx_callback_interface_t *self, uint64_t id, const void *data, uint32_t size) {
}
static struct screenshot *
ss_pop(struct screenshot_queue *queue) {
struct screenshot *r;
spin_lock(queue);
if (queue->head == queue->tail) {
r = NULL;
} else {
r = queue->q[queue->tail % MAX_SCREENSHOT];
++queue->tail;
}
spin_unlock(queue);
return r;
}
// succ return NULL
static struct screenshot *
ss_push(struct screenshot_queue *queue, struct screenshot *s) {
spin_lock(queue);
if ((queue->head - queue->tail) != MAX_SCREENSHOT) {
queue->q[queue->head % MAX_SCREENSHOT] = s;
++queue->head;
s = NULL;
}
spin_unlock(queue);
return s;
}
static void
ss_free(struct screenshot * s) {
if (s == NULL)
return;
free(s->name);
free(s->data);
free(s);
}
static void
cb_screen_shot(bgfx_callback_interface_t *self, const char* file, uint32_t width, uint32_t height, uint32_t pitch, const void* data, uint32_t size, bool yflip) {
struct screenshot *s = malloc(sizeof(*s));
size_t fn_sz = strlen(file);
s->name = malloc(fn_sz + 1);
memcpy(s->name, file, fn_sz+1);
s->data = malloc(size);
s->width = width;
s->height = height;
s->pitch = pitch;
s->size = size;
uint8_t * dst = s->data;
const uint8_t * src = (const uint8_t *)data;
int line_pitch = pitch;
if (yflip) {
src += (height-1) * pitch;
line_pitch = - (int32_t)pitch;
}
uint32_t pixwidth = size / height;
uint32_t i;
for (i=0;i<height;i++) {
memcpy(dst, src, pixwidth);
dst += pixwidth;
src += line_pitch;
}
struct callback *cb = (struct callback *)self;
s = ss_push(&cb->ss, s);
ss_free(s);
}
static void
cb_capture_begin(bgfx_callback_interface_t *self, uint32_t width, uint32_t height, uint32_t pitch, bgfx_texture_format_t format, bool yflip) {
}
static void
cb_capture_end(bgfx_callback_interface_t *self) {
}
static void
cb_capture_frame(bgfx_callback_interface_t* self, const void* _data, uint32_t _size) {
}
static uint32_t
reset_flags(lua_State *L, int index) {
const char * flags = lua_tostring(L, index);
uint32_t f = BGFX_RESET_NONE;
if (flags) {
int i;
for (i=0;flags[i];i++) {
switch(flags[i]) {
case 'f' : f |= BGFX_RESET_FULLSCREEN; break;
case 'v' : f |= BGFX_RESET_VSYNC; break;
case 'a' : f |= BGFX_RESET_MAXANISOTROPY; break;
case 'c' : f |= BGFX_RESET_CAPTURE; break;
case 'u' : f |= BGFX_RESET_FLUSH_AFTER_RENDER; break;
case 'i' : f |= BGFX_RESET_FLIP_AFTER_RENDER; break;
case 's' : f |= BGFX_RESET_SRGB_BACKBUFFER; break;
case 'm' :
++i;
switch (flags[i]) {
case '2' : f |= BGFX_RESET_MSAA_X2; break;
case '4' : f |= BGFX_RESET_MSAA_X4; break;
case '8' : f |= BGFX_RESET_MSAA_X8; break;
case 'x' : f |= BGFX_RESET_MSAA_X16; break;
default:
return luaL_error(L, "Invalid MSAA %c", flags[i]);
}
break;
case 'h': f |= BGFX_RESET_HDR10; break;
case 'p': f |= BGFX_RESET_HIDPI; break;
case 'd': f |= BGFX_RESET_DEPTH_CLAMP; break;
case 'z': f |= BGFX_RESET_SUSPEND; break;
default:
return luaL_error(L, "Invalid reset flag %c", flags[i]);
}
}
}
return f;
}
static void
read_uint32(lua_State *L, int index, const char * key, uint32_t *v) {
if (lua_getfield(L, index, key) != LUA_TNIL) {
*v = luaL_checkinteger(L, -1);
}
lua_pop(L, 1);
}
static void
read_boolean(lua_State *L, int index, const char *key, bool *v) {
int t = lua_getfield(L, index, key);
if (t == LUA_TBOOLEAN || t == LUA_TNIL) {
*v = lua_toboolean(L, -1);
} else {
luaL_error(L, ".%s need boolean, it's %s", key, lua_typename(L, t));
}
lua_pop(L, 1);
}
static bgfx_texture_format_t
texture_format_from_string(lua_State *L, int idx) {
lua_getfield(L, LUA_REGISTRYINDEX, "BGFX_TF");
lua_pushvalue(L, idx);
if (lua_rawget(L, -2) != LUA_TNUMBER) {
luaL_error(L, "Invalid texture format %s", lua_tostring(L, idx));
}
int id = lua_tointeger(L, -1);
lua_pop(L, 2);
return (bgfx_texture_format_t)id;
}
static int
linit(lua_State *L) {
static bgfx_callback_vtbl_t vtbl = {
cb_fatal,
cb_trace_vargs,
cb_profiler_begin,
cb_profiler_begin_literal,
cb_profiler_end,
cb_cache_read_size,
cb_cache_read,
cb_cache_write,
cb_screen_shot,
cb_capture_begin,
cb_capture_end,
cb_capture_frame,
};
struct callback *cb = lua_newuserdatauv(L, sizeof(*cb), 0);
memset(cb, 0, sizeof(*cb));
lua_setfield(L, LUA_REGISTRYINDEX, "bgfx_cb");
cb->base.vtbl = &vtbl;
bgfx_init_t init;
init.type = BGFX_RENDERER_TYPE_COUNT;
init.vendorId = BGFX_PCI_ID_NONE;
init.deviceId = 0;
init.resolution.format = BGFX_TEXTURE_FORMAT_RGBA8;
init.resolution.width = 1280;
init.resolution.height = 720;
init.resolution.reset = BGFX_RESET_NONE; // reset flags
init.resolution.numBackBuffers = 2;
init.resolution.maxFrameLatency = 0;
init.resolution.debugTextScale = 0;
init.limits.maxEncoders = 8; // BGFX_CONFIG_DEFAULT_MAX_ENCODERS;
init.limits.minResourceCbSize = (64<<10); // BGFX_CONFIG_MIN_RESOURCE_setter_buffer_SIZE
init.limits.transientVbSize = (6<<20); // BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE
init.limits.transientIbSize = (2<<20); // BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE;
init.callback = &cb->base;
init.allocator = NULL;
init.capabilities = UINT64_MAX;
init.debug = false;
init.profile = false;
init.platformData.ndt = NULL;
init.platformData.nwh = NULL;
init.platformData.context = NULL;
init.platformData.backBuffer = NULL;
init.platformData.backBufferDS = NULL;
init.platformData.type = BGFX_NATIVE_WINDOW_HANDLE_TYPE_DEFAULT;
if (!lua_isnoneornil(L, 1)) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 1);
if (lua_getfield(L, 1, "renderer") == LUA_TSTRING) {
init.type = renderer_type_id(L, 2);
}
lua_pop(L, 1);
if (lua_getfield(L, 1, "format") == LUA_TSTRING) {
init.resolution.format = texture_format_from_string(L, 2);
}
lua_pop(L, 1);
read_uint32(L, 1, "width", &init.resolution.width);
read_uint32(L, 1, "height", &init.resolution.height);
if (lua_getfield(L, 1, "reset") == LUA_TSTRING) {
init.resolution.reset = reset_flags(L, -1);
}
lua_getfield(L, 1, "numBackBuffers");
init.resolution.numBackBuffers = luaL_optinteger(L, -1, 2);
lua_pop(L, 1);
lua_getfield(L, 1, "debugTextScale");
init.resolution.debugTextScale = luaL_optinteger(L, -1, 0);
lua_pop(L, 1);
lua_getfield(L, 1, "maxFrameLatency");
init.resolution.maxFrameLatency = luaL_optinteger(L, -1, 0);
lua_pop(L, 1);
if (lua_getfield(L, 1, "maxEncoders") == LUA_TNUMBER) {
init.limits.maxEncoders = luaL_checkinteger(L, -1);
}
lua_pop(L, 1);
read_uint32(L, 1, "minResourceCbSize", &init.limits.minResourceCbSize);
read_uint32(L, 1, "transientVbSize", &init.limits.transientVbSize);
read_uint32(L, 1, "transientIbSize", &init.limits.transientIbSize);
read_boolean(L, 1, "debug", &init.debug);
read_boolean(L, 1, "profile", &init.profile);
read_uint32(L, 1, "loglevel", &cb->filterlog);
cb->pushlog = getfield(L, "pushlog");
cb->pushlog_context = getfield(L, "pushlog_context");
init.platformData.ndt = getfield(L, "ndt");
init.platformData.nwh = getfield(L, "nwh");
init.platformData.context = getfield(L, "context");
init.platformData.backBuffer = getfield(L, "backBuffer");
init.platformData.backBufferDS = getfield(L, "backBufferDS");
init.platformData.type = (bgfx_native_window_handle_type_t)getfield_int(L, "type");
//if (init.debug) {
luabgfx_getalloc(&init.allocator);
//}
}
if (!BGFX(init)(&init)) {
return luaL_error(L, "bgfx init failed");
}
return 0;
}
static void
push_renderer_type(lua_State *L, bgfx_renderer_type_t t) {
const char *st;
#define RENDERER_TYPE(x) case BGFX_RENDERER_TYPE_##x : st = #x; break
switch(t) {
RENDERER_TYPE(NOOP);
RENDERER_TYPE(DIRECT3D11);
RENDERER_TYPE(DIRECT3D12);
RENDERER_TYPE(METAL);
RENDERER_TYPE(VULKAN);
default: {
luaL_error(L, "Unknown renderer type %d", t);
return;
}
}
lua_pushstring(L, st);
}
static void
push_supported(lua_State *L, uint64_t supported) {
#define CAPSNAME(v) { BGFX_CAPS_##v, #v },
struct {
uint64_t caps;
const char *name;
} flags[] = {
CAPSNAME(ALPHA_TO_COVERAGE) // Alpha to coverage is supported.
CAPSNAME(BLEND_INDEPENDENT) // Blend independent is supported.
CAPSNAME(COMPUTE) // Compute shaders are supported.
CAPSNAME(CONSERVATIVE_RASTER) // Conservative rasterization is supported.
CAPSNAME(DRAW_INDIRECT) // Draw indirect is supported.
CAPSNAME(FRAGMENT_DEPTH) // Fragment depth is accessible in fragment shader.
CAPSNAME(FRAGMENT_ORDERING) // Fragment ordering is available in fragment shader.
CAPSNAME(GRAPHICS_DEBUGGER) // Graphics debugger is present.
CAPSNAME(HDR10) // HDR10 rendering is supported.
CAPSNAME(HIDPI) // HiDPI rendering is supported.
CAPSNAME(IMAGE_RW) // Image Read/Write is supported.
CAPSNAME(INDEX32) // 32-bit indices are supported.
CAPSNAME(INSTANCING) // Instancing is supported.
CAPSNAME(OCCLUSION_QUERY) // Occlusion query is supported.
CAPSNAME(RENDERER_MULTITHREADED)// Renderer is on separate thread.
CAPSNAME(SWAP_CHAIN) // Multiple windows are supported.
CAPSNAME(TEXTURE_2D_ARRAY) // 2D texture array is supported.
CAPSNAME(TEXTURE_3D) // 3D textures are supported.
CAPSNAME(TEXTURE_BLIT) // Texture blit is supported.
CAPSNAME(TEXTURE_COMPARE_ALL) // All texture compare modes are supported.
CAPSNAME(TEXTURE_COMPARE_LEQUAL)// Texture compare less equal mode is supported.
CAPSNAME(TEXTURE_CUBE_ARRAY) // Cubemap texture array is supported.
CAPSNAME(TEXTURE_DIRECT_ACCESS) // CPU direct access to GPU texture memory.
CAPSNAME(TEXTURE_READ_BACK) // Read-back texture is supported.
CAPSNAME(VERTEX_ATTRIB_HALF) // Vertex attribute half-float is supported.
CAPSNAME(VERTEX_ATTRIB_UINT10) // Vertex attribute 10_10_10_2 is supported.
CAPSNAME(VERTEX_ID) // Rendering with VertexID only is supported.
CAPSNAME(PRIMITIVE_ID) // PrimitiveID is available in fragment shader.
CAPSNAME(VIEWPORT_LAYER_ARRAY) // Viewport layer is available in vertex shader.
CAPSNAME(DRAW_INDIRECT_COUNT) // Draw indirect with indirect count is supported.
};
int n = sizeof(flags) / sizeof(flags[0]);
lua_createtable(L, 0, n);
int i;
for (i=0;i<n;i++) {
lua_pushboolean(L, supported & flags[i].caps);
lua_setfield(L, -2, flags[i].name);
}
}
static void
push_vendor_id(lua_State *L, uint16_t vid) {
if (vid == BGFX_PCI_ID_SOFTWARE_RASTERIZER) {
lua_pushstring(L, "SOFTWARE_RASTERIZER");
} else if (vid == BGFX_PCI_ID_AMD) {
lua_pushstring(L, "AMD");
} else if (vid == BGFX_PCI_ID_INTEL) {
lua_pushstring(L, "INTEL");
} else if (vid == BGFX_PCI_ID_NVIDIA) {
lua_pushstring(L, "NVIDIA");
} else {
lua_pushinteger(L, vid);
}
}
static int
find_texture_format(const uint16_t *formats, int index) {
int i;
uint16_t c = formats[index];
for (i=0;i<index;i++) {
if (c == formats[i]) {
return i+1;
}
}
return 0;
}
#define TFNAME(v) #v,
static const char * c_texture_formats[] = {
TFNAME(BC1) /** ( 0) DXT1 R5G6B5A1 */
TFNAME(BC2) /** ( 1) DXT3 R5G6B5A4 */
TFNAME(BC3) /** ( 2) DXT5 R5G6B5A8 */
TFNAME(BC4) /** ( 3) LATC1/ATI1 R8 */
TFNAME(BC5) /** ( 4) LATC2/ATI2 RG8 */
TFNAME(BC6H) /** ( 5) BC6H RGB16F */
TFNAME(BC7) /** ( 6) BC7 RGB 4-7 bits per color channel, 0-8 bits alpha */
TFNAME(ETC1) /** ( 7) ETC1 RGB8 */
TFNAME(ETC2) /** ( 8) ETC2 RGB8 */
TFNAME(ETC2A) /** ( 9) ETC2 RGBA8 */
TFNAME(ETC2A1) /** (10) ETC2 RGB8A1 */
TFNAME(PTC12) /** (11) PVRTC1 RGB 2BPP */
TFNAME(PTC14) /** (12) PVRTC1 RGB 4BPP */
TFNAME(PTC12A) /** (13) PVRTC1 RGBA 2BPP */
TFNAME(PTC14A) /** (14) PVRTC1 RGBA 4BPP */
TFNAME(PTC22) /** (15) PVRTC2 RGBA 2BPP */
TFNAME(PTC24) /** (16) PVRTC2 RGBA 4BPP */
TFNAME(ATC) /** (17) ATC RGB 4BPP */
TFNAME(ATCE) /** (18) ATCE RGBA 8 BPP explicit alpha */
TFNAME(ATCI) /** (19) ATCI RGBA 8 BPP interpolated alpha */
TFNAME(ASTC4X4) /** (20) ASTC 4x4 8.0 BPP */
TFNAME(ASTC5x4) /** (21) ASTC 5x4 6.40 BPP */
TFNAME(ASTC5x5) /** (22) ASTC 5x5 5.12 BPP */
TFNAME(ASTC6x5) /** (23) ASTC 6x5 4.27 BPP */
TFNAME(ASTC6x6) /** (24) ASTC 6x6 3.56 BPP */
TFNAME(ASTC8x5) /** (25) ASTC 8x5 3.20 BPP */
TFNAME(ASTC8x6) /** (26) ASTC 8x6 2.67 BPP */
TFNAME(ASTC8x8) /** (27) ASTC 8x8 2.00 BPP */
TFNAME(ASTC10x5) /** (28) ASTC 10x5 2.56 BPP */
TFNAME(ASTC10x6) /** (29) ASTC 10x6 2.13 BPP */
TFNAME(ASTC10x8) /** (30) ASTC 10x8 1.60 BPP */
TFNAME(ASTC10x10) /** (31) ASTC 10x10 1.28 BPP */
TFNAME(ASTC12x10) /** (32) ASTC 12x10 1.07 BPP */
TFNAME(ASTC12x12) /** (33) ASTC 12x12 0.89 BPP */
TFNAME(UNKNOWN) /** (34) Compressed formats above. */
TFNAME(R1) /** (35) */
TFNAME(A8) /** (36) */
TFNAME(R8) /** (37) */
TFNAME(R8I) /** (38) */
TFNAME(R8U) /** (39) */
TFNAME(R8S) /** (40) */
TFNAME(R16) /** (41) */
TFNAME(R16I) /** (42) */
TFNAME(R16U) /** (43) */
TFNAME(R16F) /** (44) */
TFNAME(R16S) /** (45) */
TFNAME(R32I) /** (46) */
TFNAME(R32U) /** (47) */
TFNAME(R32F) /** (48) */
TFNAME(RG8) /** (49) */
TFNAME(RG8I) /** (50) */
TFNAME(RG8U) /** (51) */
TFNAME(RG8S) /** (52) */
TFNAME(RG16) /** (53) */
TFNAME(RG16I) /** (54) */
TFNAME(RG16U) /** (55) */
TFNAME(RG16F) /** (56) */
TFNAME(RG16S) /** (57) */
TFNAME(RG32I) /** (58) */
TFNAME(RG32U) /** (59) */
TFNAME(RG32F) /** (60) */
TFNAME(RGB8) /** (61) */
TFNAME(RGB8I) /** (62) */
TFNAME(RGB8U) /** (63) */
TFNAME(RGB8S) /** (64) */
TFNAME(RGB9E5F) /** (65) */
TFNAME(BGRA8) /** (66) */
TFNAME(RGBA8) /** (67) */
TFNAME(RGBA8I) /** (68) */
TFNAME(RGBA8U) /** (69) */
TFNAME(RGBA8S) /** (70) */
TFNAME(RGBA16) /** (71) */
TFNAME(RGBA16I) /** (72) */
TFNAME(RGBA16U) /** (73) */
TFNAME(RGBA16F) /** (74) */
TFNAME(RGBA16S) /** (75) */
TFNAME(RGBA32I) /** (76) */
TFNAME(RGBA32U) /** (77) */
TFNAME(RGBA32F) /** (78) */
TFNAME(B5G6R5) /** (79) */
TFNAME(R5G6B5) /** (80) */
TFNAME(BGRA4) /** (81) */
TFNAME(RGBA4) /** (82) */
TFNAME(BGR5A1) /** (83) */
TFNAME(RGB5A1) /** (84) */
TFNAME(RGB10A2) /** (85) */
TFNAME(RG11B10F) /** (86) */
TFNAME(UNKNOWNDEPTH) /** (87) Depth formats below. */
TFNAME(D16) /** (88) */
TFNAME(D24) /** (89) */
TFNAME(D24S8) /** (90) */
TFNAME(D32) /** (91) */
TFNAME(D16F) /** (92) */
TFNAME(D24F) /** (93) */
TFNAME(D32F) /** (94) */
TFNAME(D0S8) /** (95) */
};
static void
push_texture_formats(lua_State *L, const uint16_t *formats) {
#define CAPSTF(v) { BGFX_CAPS_FORMAT_TEXTURE_##v, #v },
struct {
uint16_t caps;
const char * name;
} caps_texture_format[] = {
CAPSTF(2D) //Texture format is supported.
CAPSTF(2D_SRGB) //Texture as sRGB format is supported.
CAPSTF(2D_EMULATED) //Texture format is emulated.
CAPSTF(3D) //Texture format is supported.
CAPSTF(3D_SRGB) //Texture as sRGB format is supported.
CAPSTF(3D_EMULATED) //Texture format is emulated.
CAPSTF(CUBE) //Texture format is supported.
CAPSTF(CUBE_SRGB) //Texture as sRGB format is supported.
CAPSTF(CUBE_EMULATED) //Texture format is emulated.
CAPSTF(VERTEX) //Texture format can be used from vertex shader.
CAPSTF(IMAGE_READ) //Texture format can be used as image and read from.
CAPSTF(IMAGE_WRITE) //Texture format can be used as image and written to.
CAPSTF(FRAMEBUFFER) //Texture format can be used as frame buffer.
CAPSTF(FRAMEBUFFER_MSAA) //Texture format can be used as MSAA frame buffer.
CAPSTF(MSAA) //Texture can be sampled as MSAA.
CAPSTF(MIP_AUTOGEN) //Texture format supports auto-generated mips.
};
lua_createtable(L, 0, BGFX_TEXTURE_FORMAT_COUNT);
int i,j;
int ncaps = sizeof(caps_texture_format) / sizeof(caps_texture_format[0]);
for (i=0;i<BGFX_TEXTURE_FORMAT_COUNT;i++) {
uint16_t c = formats[i];
int sameindex = find_texture_format(formats, i);
if (sameindex) {
lua_getfield(L, -1, c_texture_formats[sameindex-1]);
} else {
lua_newtable(L);
for (j=0;j<ncaps;j++) {
if (c & caps_texture_format[j].caps) {
lua_pushboolean(L, 1);
lua_setfield(L, -2, caps_texture_format[j].name);
}
}
}
lua_setfield(L, -2, c_texture_formats[i]);
}
}
static void
push_limits(lua_State *L, const bgfx_caps_limits_t *lim) {
lua_createtable(L, 0, 23);
#define PUSH_LIMIT(what) lua_pushinteger(L, lim->what); lua_setfield(L, -2, #what);
PUSH_LIMIT(maxDrawCalls)
PUSH_LIMIT(maxBlits)
PUSH_LIMIT(maxTextureSize)
PUSH_LIMIT(maxTextureLayers)
PUSH_LIMIT(maxViews)
PUSH_LIMIT(maxFrameBuffers)
PUSH_LIMIT(maxFBAttachments)
PUSH_LIMIT(maxPrograms)
PUSH_LIMIT(maxShaders)
PUSH_LIMIT(maxTextures)
PUSH_LIMIT(maxTextureSamplers)
PUSH_LIMIT(maxComputeBindings)
PUSH_LIMIT(maxVertexLayouts)
PUSH_LIMIT(maxVertexStreams)
PUSH_LIMIT(maxIndexBuffers)
PUSH_LIMIT(maxVertexBuffers)
PUSH_LIMIT(maxDynamicIndexBuffers)
PUSH_LIMIT(maxDynamicVertexBuffers)
PUSH_LIMIT(maxUniforms)
PUSH_LIMIT(maxOcclusionQueries)
PUSH_LIMIT(maxEncoders)
PUSH_LIMIT(transientVbSize)
PUSH_LIMIT(transientIbSize)
}
static int
lgetCaps(lua_State *L) {
const bgfx_caps_t * caps = BGFX(get_caps)();
lua_createtable(L, 0, 9);
push_renderer_type(L, caps->rendererType);
lua_setfield(L, -2, "rendererType");
push_supported(L, caps->supported);
lua_setfield(L, -2, "supported");
push_vendor_id(L, caps->vendorId);
lua_setfield(L, -2, "vendorId");
lua_pushinteger(L, caps->deviceId);
lua_setfield(L, -2, "deviceId");
lua_pushboolean(L, caps->homogeneousDepth);
lua_setfield(L, -2, "homogeneousDepth");
lua_pushboolean(L, caps->originBottomLeft);
lua_setfield(L, -2, "originBottomLeft");
lua_createtable(L, caps->numGPUs, 0);
int i;
for (i=0;i<caps->numGPUs;i++) {
lua_createtable(L, 0, 2);
push_vendor_id(L, caps->gpu[i].vendorId);
lua_setfield(L, -2, "vendorId");
lua_pushinteger(L, caps->gpu[i].deviceId);
lua_setfield(L, -2, "deviceId");
lua_seti(L, -2, i+1);
}
lua_setfield(L, -2, "gpu");
push_texture_formats(L, caps->formats);
lua_setfield(L, -2, "formats");
push_limits(L, &caps->limits);
lua_setfield(L, -2, "limits");
return 1;
}
static int
lgetStats(lua_State *L) {
size_t sz;
const char * what = luaL_checklstring(L, 1, &sz);
lua_settop(L, 2);
if (!lua_istable(L, 2)) {
lua_settop(L, 1);
lua_createtable(L, 0, sz);
}
const bgfx_stats_t * stat = BGFX(get_stats)();
int i;
#define PUSHSTAT(v) lua_pushinteger(L, stat->v); lua_setfield(L, 2, #v)