-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathbgfx_p.h
5401 lines (4501 loc) · 149 KB
/
bgfx_p.h
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
/*
* Copyright 2011-2025 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
*/
#ifndef BGFX_P_H_HEADER_GUARD
#define BGFX_P_H_HEADER_GUARD
#include <bx/platform.h>
#ifndef BX_CONFIG_DEBUG
# error "BX_CONFIG_DEBUG must be defined in build script!"
#endif // BX_CONFIG_DEBUG
#define BGFX_CONFIG_DEBUG BX_CONFIG_DEBUG
#if BX_CONFIG_DEBUG
# define BX_TRACE _BGFX_TRACE
# define BX_WARN _BGFX_WARN
# define BX_ASSERT _BGFX_ASSERT
#endif // BX_CONFIG_DEBUG
#include <bgfx/bgfx.h>
#include "config.h"
#include <inttypes.h>
// Check handle, cannot be bgfx::kInvalidHandle and must be valid.
#define BGFX_CHECK_HANDLE(_desc, _handleAlloc, _handle) \
BX_ASSERT(isValid(_handle) \
&& _handleAlloc.isValid(_handle.idx) \
, "Invalid handle. %s handle: %d (max %d)" \
, _desc \
, _handle.idx \
, _handleAlloc.getMaxHandles() \
)
// Check handle, it's ok to be bgfx::kInvalidHandle or must be valid.
#define BGFX_CHECK_HANDLE_INVALID_OK(_desc, _handleAlloc, _handle) \
BX_ASSERT(!isValid(_handle) \
|| _handleAlloc.isValid(_handle.idx) \
, "Invalid handle. %s handle: %d (max %d)" \
, _desc \
, _handle.idx \
, _handleAlloc.getMaxHandles() \
)
#if BGFX_CONFIG_MULTITHREADED
# define BGFX_MUTEX_SCOPE(_mutex) bx::MutexScope BX_CONCATENATE(mutexScope, __LINE__)(_mutex)
#else
# define BGFX_MUTEX_SCOPE(_mutex) BX_NOOP()
#endif // BGFX_CONFIG_MULTITHREADED
#if BGFX_CONFIG_PROFILER
# define BGFX_PROFILER_SCOPE(_name, _abgr) ProfilerScope BX_CONCATENATE(profilerScope, __LINE__)(_name, _abgr, __FILE__, uint16_t(__LINE__) )
# define BGFX_PROFILER_BEGIN(_name, _abgr) g_callback->profilerBegin(_name, _abgr, __FILE__, uint16_t(__LINE__) )
# define BGFX_PROFILER_BEGIN_LITERAL(_name, _abgr) g_callback->profilerBeginLiteral(_name, _abgr, __FILE__, uint16_t(__LINE__) )
# define BGFX_PROFILER_END() g_callback->profilerEnd()
# define BGFX_PROFILER_SET_CURRENT_THREAD_NAME(_name) BX_NOOP()
#else
# define BGFX_PROFILER_SCOPE(_name, _abgr) BX_NOOP()
# define BGFX_PROFILER_BEGIN(_name, _abgr) BX_NOOP()
# define BGFX_PROFILER_BEGIN_LITERAL(_name, _abgr) BX_NOOP()
# define BGFX_PROFILER_END() BX_NOOP()
# define BGFX_PROFILER_SET_CURRENT_THREAD_NAME(_name) BX_NOOP()
#endif // BGFX_PROFILER_SCOPE
namespace bgfx
{
#if BX_COMPILER_CLANG_ANALYZER
void __attribute__( (analyzer_noreturn) ) fatal(const char* _filePath, uint16_t _line, Fatal::Enum _code, const char* _format, ...);
#else
void fatal(const char* _filePath, uint16_t _line, Fatal::Enum _code, const char* _format, ...);
#endif // BX_COMPILER_CLANG_ANALYZER
void trace(const char* _filePath, uint16_t _line, const char* _format, ...);
inline bool operator==(const VertexLayoutHandle& _lhs, const VertexLayoutHandle& _rhs) { return _lhs.idx == _rhs.idx; }
inline bool operator==(const UniformHandle& _lhs, const UniformHandle& _rhs) { return _lhs.idx == _rhs.idx; }
}
#define _BGFX_TRACE(_format, ...) \
BX_MACRO_BLOCK_BEGIN \
bgfx::trace(__FILE__, uint16_t(__LINE__), "BGFX " _format "\n", ##__VA_ARGS__); \
BX_MACRO_BLOCK_END
#define _BGFX_WARN(_condition, _format, ...) \
BX_MACRO_BLOCK_BEGIN \
if (!BX_IGNORE_C4127(_condition) ) \
{ \
BX_TRACE("WARN " _format, ##__VA_ARGS__); \
} \
BX_MACRO_BLOCK_END
#define _BGFX_ASSERT(_condition, _format, ...) \
BX_MACRO_BLOCK_BEGIN \
if (!BX_IGNORE_C4127(_condition) \
&& bx::assertFunction(bx::Location::current(), "ASSERT " #_condition " -> " _format, ##__VA_ARGS__) ) \
{ \
bgfx::fatal(__FILE__, uint16_t(__LINE__), bgfx::Fatal::DebugCheck, _format, ##__VA_ARGS__); \
} \
BX_MACRO_BLOCK_END
#define BGFX_FATAL(_condition, _err, _format, ...) \
BX_MACRO_BLOCK_BEGIN \
if (!BX_IGNORE_C4127(_condition) ) \
{ \
fatal(__FILE__, uint16_t(__LINE__), _err, _format, ##__VA_ARGS__); \
} \
BX_MACRO_BLOCK_END
#define BGFX_ERROR_CHECK(_condition, _err, _result, _msg, _format, ...) \
if (!BX_IGNORE_C4127(_condition) ) \
{ \
BX_ERROR_SET(_err, _result, _msg); \
BX_TRACE("%S: 0x%08x '%S' - " _format \
, &bxErrorScope.getName() \
, _err->get().code \
, &_err->getMessage() \
, ##__VA_ARGS__ \
); \
return; \
}
#include <bx/allocator.h>
#include <bx/bx.h>
#include <bx/cpu.h>
#include <bx/debug.h>
#include <bx/endian.h>
#include <bx/error.h>
#include <bx/float4x4_t.h>
#include <bx/handlealloc.h>
#include <bx/hash.h>
#include <bx/math.h>
#include <bx/mutex.h>
#include <bx/os.h>
#include <bx/readerwriter.h>
#include <bx/ringbuffer.h>
#include <bx/sort.h>
#include <bx/string.h>
#include <bx/thread.h>
#include <bx/timer.h>
#include <bx/uint32_t.h>
#include <bgfx/platform.h>
#include <bimg/bimg.h>
#include "shader.h"
#include "vertexlayout.h"
#include "version.h"
#define BGFX_CHUNK_MAGIC_TEX BX_MAKEFOURCC('T', 'E', 'X', 0x0)
#define BGFX_CLEAR_COLOR_USE_PALETTE UINT16_C(0x8000)
#define BGFX_CLEAR_MASK (0 \
| BGFX_CLEAR_COLOR \
| BGFX_CLEAR_DEPTH \
| BGFX_CLEAR_STENCIL \
| BGFX_CLEAR_COLOR_USE_PALETTE \
)
#if BGFX_CONFIG_USE_TINYSTL
namespace bgfx
{
struct TinyStlAllocator
{
static void* static_allocate(size_t _bytes);
static void static_deallocate(void* _ptr, size_t /*_bytes*/);
};
} // namespace bgfx
# define TINYSTL_ALLOCATOR bgfx::TinyStlAllocator
# include <tinystl/string.h>
# include <tinystl/unordered_map.h>
# include <tinystl/unordered_set.h>
# include <tinystl/vector.h>
namespace tinystl
{
template<typename T, typename Alloc = TINYSTL_ALLOCATOR>
class list : public vector<T, Alloc>
{
public:
void push_front(const T& _value)
{
this->insert(this->begin(), _value);
}
void pop_front()
{
this->erase(this->begin() );
}
void sort()
{
bx::quickSort(
this->begin()
, uint32_t(this->end() - this->begin() )
, sizeof(T)
, [](const void* _a, const void* _b) -> int32_t {
const T& lhs = *(const T*)(_a);
const T& rhs = *(const T*)(_b);
return lhs < rhs ? -1 : 1;
});
}
};
} // namespace tinystl
namespace stl = tinystl;
#else
# include <list>
# include <string>
# include <unordered_map>
# include <unordered_set>
# include <vector>
namespace stl = std;
#endif // BGFX_CONFIG_USE_TINYSTL
#if BX_PLATFORM_ANDROID
# include <android/native_window.h>
#endif // BX_PLATFORM_*
#define BGFX_MAX_COMPUTE_BINDINGS BGFX_CONFIG_MAX_TEXTURE_SAMPLERS
#define BGFX_SAMPLER_INTERNAL_DEFAULT UINT32_C(0x10000000)
#define BGFX_SAMPLER_INTERNAL_SHARED UINT32_C(0x20000000)
#define BGFX_RESET_INTERNAL_FORCE UINT32_C(0x80000000)
#define BGFX_STATE_INTERNAL_SCISSOR UINT64_C(0x2000000000000000)
#define BGFX_STATE_INTERNAL_OCCLUSION_QUERY UINT64_C(0x4000000000000000)
#define BGFX_SUBMIT_INTERNAL_NONE UINT8_C(0x00)
#define BGFX_SUBMIT_INTERNAL_INDEX32 UINT8_C(0x40)
#define BGFX_SUBMIT_INTERNAL_OCCLUSION_VISIBLE UINT8_C(0x80)
#define BGFX_SUBMIT_INTERNAL_RESERVED_MASK UINT8_C(0xff)
#define BGFX_RENDERER_NOOP_NAME "Noop"
#define BGFX_RENDERER_AGC_NAME "AGC"
#define BGFX_RENDERER_DIRECT3D11_NAME "Direct3D 11"
#define BGFX_RENDERER_DIRECT3D12_NAME "Direct3D 12"
#define BGFX_RENDERER_GNM_NAME "GNM"
#define BGFX_RENDERER_METAL_NAME "Metal"
#define BGFX_RENDERER_NVN_NAME "NVN"
#define BGFX_RENDERER_VULKAN_NAME "Vulkan"
#if BGFX_CONFIG_RENDERER_OPENGL
# if BGFX_CONFIG_RENDERER_OPENGL >= 31 && BGFX_CONFIG_RENDERER_OPENGL <= 33
# if BGFX_CONFIG_RENDERER_OPENGL == 31
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 3.1"
# elif BGFX_CONFIG_RENDERER_OPENGL == 32
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 3.2"
# else
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 3.3"
# endif // 31+
# elif BGFX_CONFIG_RENDERER_OPENGL >= 40 && BGFX_CONFIG_RENDERER_OPENGL <= 46
# if BGFX_CONFIG_RENDERER_OPENGL == 40
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.0"
# elif BGFX_CONFIG_RENDERER_OPENGL == 41
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.1"
# elif BGFX_CONFIG_RENDERER_OPENGL == 42
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.2"
# elif BGFX_CONFIG_RENDERER_OPENGL == 43
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.3"
# elif BGFX_CONFIG_RENDERER_OPENGL == 44
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.4"
# elif BGFX_CONFIG_RENDERER_OPENGL == 45
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.5"
# else
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.6"
# endif // 40+
# else
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 2.1"
# endif // BGFX_CONFIG_RENDERER_OPENGL
#elif BGFX_CONFIG_RENDERER_OPENGLES
# if BGFX_CONFIG_RENDERER_OPENGLES == 30
# define BGFX_RENDERER_OPENGL_NAME "OpenGL ES 3.0"
# elif BGFX_CONFIG_RENDERER_OPENGLES == 31
# define BGFX_RENDERER_OPENGL_NAME "OpenGL ES 3.1"
# elif BGFX_CONFIG_RENDERER_OPENGLES >= 32
# define BGFX_RENDERER_OPENGL_NAME "OpenGL ES 3.2"
# else
# define BGFX_RENDERER_OPENGL_NAME "OpenGL ES 2.0"
# endif // BGFX_CONFIG_RENDERER_OPENGLES
#else
# define BGFX_RENDERER_OPENGL_NAME "OpenGL"
#endif //
namespace bgfx
{
extern InternalData g_internalData;
extern PlatformData g_platformData;
extern bool g_platformDataChangedSinceReset;
extern void isFrameBufferValid(uint8_t _num, const Attachment* _attachment, bx::Error* _err);
extern void isIdentifierValid(const bx::StringView& _name, bx::Error* _err);
#if BGFX_CONFIG_MAX_DRAW_CALLS < (64<<10)
typedef uint16_t RenderItemCount;
#else
typedef uint32_t RenderItemCount;
#endif // BGFX_CONFIG_MAX_DRAW_CALLS < (64<<10)
///
struct Handle
{
///
struct TypeName
{
const char* abrvName;
const char* fullName;
};
///
enum Enum
{
DynamicIndexBuffer,
DynamicVertexBuffer,
FrameBuffer,
IndexBuffer,
IndirectBuffer,
OcclusionQuery,
Program,
Shader,
Texture,
Uniform,
VertexBuffer,
VertexLayout,
Count
};
template<typename Ty>
static constexpr Enum toEnum();
constexpr Handle()
: idx(kInvalidHandle)
, type(Count)
{
}
template<typename Ty>
constexpr Handle(Ty _handle)
: idx(_handle.idx)
, type(uint16_t(toEnum<Ty>() ) )
{
}
template<typename Ty>
constexpr Ty to() const
{
if (type == toEnum<Ty>() )
{
return Ty{ idx };
}
BX_ASSERT(type == toEnum<Ty>(), "Handle type %s, cannot be converted to %s."
, getTypeName().fullName
, getTypeName(toEnum<Ty>() ).fullName
);
return { kInvalidHandle };
}
Enum getType() const
{
return Enum(type);
}
static const TypeName& getTypeName(Handle::Enum _enum);
const TypeName& getTypeName() const
{
return getTypeName(getType() );
}
bool isBuffer() const
{
return false
|| type == DynamicIndexBuffer
|| type == DynamicVertexBuffer
|| type == IndexBuffer
|| type == IndirectBuffer
|| type == VertexBuffer
;
}
bool isTexture() const
{
return type == Texture;
}
uint16_t idx;
uint16_t type;
};
#define IMPLEMENT_HANDLE(_name) \
template<> \
inline constexpr Handle::Enum Handle::toEnum<_name##Handle>() \
{ \
return Handle::_name; \
} \
IMPLEMENT_HANDLE(DynamicIndexBuffer);
IMPLEMENT_HANDLE(DynamicVertexBuffer);
IMPLEMENT_HANDLE(FrameBuffer);
IMPLEMENT_HANDLE(IndexBuffer);
IMPLEMENT_HANDLE(IndirectBuffer);
IMPLEMENT_HANDLE(OcclusionQuery);
IMPLEMENT_HANDLE(Program);
IMPLEMENT_HANDLE(Shader);
IMPLEMENT_HANDLE(Texture);
IMPLEMENT_HANDLE(Uniform);
IMPLEMENT_HANDLE(VertexBuffer);
IMPLEMENT_HANDLE(VertexLayout);
#undef IMPLEMENT_HANDLE
inline bool isValid(const VertexLayout& _layout)
{
return 0 != _layout.m_stride;
}
struct Condition
{
enum Enum
{
LessEqual,
GreaterEqual,
};
};
bool windowsVersionIs(Condition::Enum _op, uint32_t _version, uint32_t _build = UINT32_MAX);
constexpr bool isShaderType(uint32_t _magic, char _type)
{
return uint32_t(_type) == (_magic & BX_MAKEFOURCC(0xff, 0, 0, 0) );
}
inline bool isShaderBin(uint32_t _magic)
{
return BX_MAKEFOURCC(0, 'S', 'H', 0) == (_magic & BX_MAKEFOURCC(0, 0xff, 0xff, 0) )
&& (isShaderType(_magic, 'C') || isShaderType(_magic, 'F') || isShaderType(_magic, 'V') )
;
}
inline bool isShaderVerLess(uint32_t _magic, uint8_t _version)
{
return (_magic & BX_MAKEFOURCC(0, 0, 0, 0xff) ) < BX_MAKEFOURCC(0, 0, 0, _version);
}
const char* getShaderTypeName(uint32_t _magic);
struct Clear
{
void set(uint16_t _flags, uint32_t _rgba, float _depth, uint8_t _stencil)
{
m_flags = _flags;
m_index[0] = uint8_t(_rgba>>24);
m_index[1] = uint8_t(_rgba>>16);
m_index[2] = uint8_t(_rgba>> 8);
m_index[3] = uint8_t(_rgba>> 0);
m_depth = _depth;
m_stencil = _stencil;
}
void set(uint16_t _flags, float _depth, uint8_t _stencil, uint8_t _0, uint8_t _1, uint8_t _2, uint8_t _3, uint8_t _4, uint8_t _5, uint8_t _6, uint8_t _7)
{
m_flags = (_flags & ~BGFX_CLEAR_COLOR)
| (0xff != (_0&_1&_2&_3&_4&_5&_6&_7) ? BGFX_CLEAR_COLOR|BGFX_CLEAR_COLOR_USE_PALETTE : 0)
;
m_index[0] = _0;
m_index[1] = _1;
m_index[2] = _2;
m_index[3] = _3;
m_index[4] = _4;
m_index[5] = _5;
m_index[6] = _6;
m_index[7] = _7;
m_depth = _depth;
m_stencil = _stencil;
}
uint8_t m_index[8];
float m_depth;
uint8_t m_stencil;
uint16_t m_flags;
};
struct Rect
{
Rect()
{
}
Rect(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
: m_x(_x)
, m_y(_y)
, m_width(_width)
, m_height(_height)
{
}
void clear()
{
m_x = 0;
m_y = 0;
m_width = 0;
m_height = 0;
}
bool isZero() const
{
uint64_t ui64 = *( (uint64_t*)this);
return UINT64_C(0) == ui64;
}
bool isZeroArea() const
{
return 0 == m_width
|| 0 == m_height
;
}
void set(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
{
m_x = _x;
m_y = _y;
m_width = _width;
m_height = _height;
}
void setIntersect(const Rect& _a, const Rect& _b)
{
const uint16_t sx = bx::max<uint16_t>(_a.m_x, _b.m_x);
const uint16_t sy = bx::max<uint16_t>(_a.m_y, _b.m_y);
const uint16_t ex = bx::min<uint16_t>(_a.m_x + _a.m_width, _b.m_x + _b.m_width );
const uint16_t ey = bx::min<uint16_t>(_a.m_y + _a.m_height, _b.m_y + _b.m_height);
m_x = sx;
m_y = sy;
m_width = (uint16_t)bx::uint32_satsub(ex, sx);
m_height = (uint16_t)bx::uint32_satsub(ey, sy);
}
void intersect(const Rect& _a)
{
setIntersect(*this, _a);
}
uint16_t m_x;
uint16_t m_y;
uint16_t m_width;
uint16_t m_height;
};
struct TextureCreate
{
TextureFormat::Enum m_format;
uint16_t m_width;
uint16_t m_height;
uint16_t m_depth;
uint16_t m_numLayers;
uint8_t m_numMips;
bool m_cubeMap;
const Memory* m_mem;
};
extern const uint32_t g_uniformTypeSize[UniformType::Count+1];
extern CallbackI* g_callback;
extern bx::AllocatorI* g_allocator;
extern Caps g_caps;
typedef bx::StringT<&g_allocator> String;
struct ProfilerScope
{
ProfilerScope(const char* _name, uint32_t _abgr, const char* _filePath, uint16_t _line)
{
g_callback->profilerBeginLiteral(_name, _abgr, _filePath, _line);
}
~ProfilerScope()
{
g_callback->profilerEnd();
}
};
void setGraphicsDebuggerPresent(bool _present);
bool isGraphicsDebuggerPresent();
void release(const Memory* _mem);
const char* getAttribName(Attrib::Enum _attr);
const char* getAttribNameShort(Attrib::Enum _attr);
void getTextureSizeFromRatio(BackbufferRatio::Enum _ratio, uint16_t& _width, uint16_t& _height);
TextureFormat::Enum getViableTextureFormat(const bimg::ImageContainer& _imageContainer);
const char* getName(TextureFormat::Enum _fmt);
const char* getName(UniformHandle _handle);
const char* getName(ShaderHandle _handle);
const char* getName(Topology::Enum _topology);
template<typename Ty>
inline void release(Ty)
{
}
template<>
inline void release(Memory* _mem)
{
release( (const Memory*)_mem);
}
inline uint64_t packStencil(uint32_t _fstencil, uint32_t _bstencil)
{
return (uint64_t(_bstencil)<<32)|uint64_t(_fstencil);
}
inline uint32_t unpackStencil(uint8_t _0or1, uint64_t _stencil)
{
return uint32_t( (_stencil >> (32*_0or1) ) );
}
inline bool needBorderColor(uint64_t _flags)
{
return BGFX_SAMPLER_U_BORDER == (_flags & BGFX_SAMPLER_U_BORDER)
|| BGFX_SAMPLER_V_BORDER == (_flags & BGFX_SAMPLER_V_BORDER)
|| BGFX_SAMPLER_W_BORDER == (_flags & BGFX_SAMPLER_W_BORDER)
;
}
inline uint8_t calcNumMips(bool _hasMips, uint16_t _width, uint16_t _height, uint16_t _depth = 1)
{
if (_hasMips)
{
const uint32_t max = bx::max(_width, _height, _depth);
const uint32_t num = 1 + bx::floorLog2(max);
return uint8_t(num);
}
return 1;
}
/// Dump vertex layout info into debug output.
void dump(const VertexLayout& _layout);
/// Dump resolution and reset info into debug output.
void dump(const Resolution& _resolution);
struct TextVideoMem
{
TextVideoMem()
: m_mem(NULL)
, m_size(0)
, m_width(0)
, m_height(0)
, m_small(false)
{
resize(false, 1, 1);
clear();
}
~TextVideoMem()
{
bx::free(g_allocator, m_mem);
}
void resize(bool _small, uint32_t _width, uint32_t _height)
{
uint32_t width = bx::uint32_imax(1, _width/8);
uint32_t height = bx::uint32_imax(1, _height/(_small ? 8 : 16) );
if (NULL == m_mem
|| m_width != width
|| m_height != height
|| m_small != _small)
{
m_small = _small;
m_width = bx::narrowCast<uint16_t>(width);
m_height = bx::narrowCast<uint16_t>(height);
uint32_t size = m_size;
m_size = m_width * m_height;
m_mem = (MemSlot*)bx::realloc(g_allocator, m_mem, m_size * sizeof(MemSlot) );
if (size < m_size)
{
bx::memSet(&m_mem[size], 0, (m_size-size) * sizeof(MemSlot) );
}
}
}
void clear(uint8_t _attr = 0)
{
MemSlot* mem = m_mem;
bx::memSet(mem, 0, m_size * sizeof(MemSlot) );
if (_attr != 0)
{
for (uint32_t ii = 0, num = m_size; ii < num; ++ii)
{
mem[ii].attribute = _attr;
}
}
}
void printfVargs(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, va_list _argList);
void printf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...)
{
va_list argList;
va_start(argList, _format);
printfVargs(_x, _y, _attr, _format, argList);
va_end(argList);
}
void image(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const void* _data, uint16_t _pitch)
{
if (_x < m_width && _y < m_height)
{
MemSlot* dst = &m_mem[_y*m_width+_x];
const uint8_t* src = (const uint8_t*)_data;
const uint32_t width = bx::min<uint32_t>(m_width, _width +_x)-_x;
const uint32_t height = bx::min<uint32_t>(m_height, _height+_y)-_y;
const uint32_t dstPitch = m_width;
for (uint32_t ii = 0; ii < height; ++ii)
{
for (uint32_t jj = 0; jj < width; ++jj)
{
dst[jj].character = src[jj*2];
dst[jj].attribute = src[jj*2+1];
}
src += _pitch;
dst += dstPitch;
}
}
}
struct MemSlot
{
uint8_t attribute;
uint8_t character;
};
MemSlot* m_mem;
uint32_t m_size;
uint16_t m_width;
uint16_t m_height;
bool m_small;
};
struct TextVideoMemBlitter
{
void init(uint8_t scale);
void shutdown();
TextureHandle m_texture;
TransientVertexBuffer* m_vb;
TransientIndexBuffer* m_ib;
VertexLayout m_layout;
ProgramHandle m_program;
uint8_t m_scale;
};
struct RendererContextI;
extern void blit(RendererContextI* _renderCtx, TextVideoMemBlitter& _blitter, const TextVideoMem& _mem);
inline void blit(RendererContextI* _renderCtx, TextVideoMemBlitter& _blitter, const TextVideoMem* _mem)
{
blit(_renderCtx, _blitter, *_mem);
}
template <uint32_t maxKeys>
struct UpdateBatchT
{
UpdateBatchT()
: m_num(0)
{
}
void add(uint32_t _key, uint32_t _value)
{
uint32_t num = m_num++;
m_keys[num] = _key;
m_values[num] = _value;
}
bool sort()
{
if (0 < m_num)
{
uint32_t* tempKeys = (uint32_t*)BX_STACK_ALLOC(sizeof(m_keys) );
uint32_t* tempValues = (uint32_t*)BX_STACK_ALLOC(sizeof(m_values) );
bx::radixSort(m_keys, tempKeys, m_values, tempValues, m_num);
return true;
}
return false;
}
bool isFull() const
{
return m_num >= maxKeys;
}
void reset()
{
m_num = 0;
}
uint32_t m_num;
uint32_t m_keys[maxKeys];
uint32_t m_values[maxKeys];
};
struct ClearQuad
{
ClearQuad()
{
for (uint32_t ii = 0; ii < BX_COUNTOF(m_program); ++ii)
{
m_program[ii].idx = kInvalidHandle;
}
}
void init();
void shutdown();
VertexBufferHandle m_vb;
VertexLayout m_layout;
ProgramHandle m_program[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS];
};
struct PredefinedUniform
{
enum Enum
{
ViewRect,
ViewTexel,
View,
InvView,
Proj,
InvProj,
ViewProj,
InvViewProj,
Model,
ModelView,
ModelViewProj,
AlphaRef,
Count
};
uint32_t m_loc;
uint16_t m_count;
uint8_t m_type;
};
const char* getUniformTypeName(UniformType::Enum _enum);
UniformType::Enum nameToUniformTypeEnum(const char* _name);
const char* getPredefinedUniformName(PredefinedUniform::Enum _enum);
PredefinedUniform::Enum nameToPredefinedUniformEnum(const bx::StringView& _name);
class CommandBuffer
{
BX_CLASS(CommandBuffer
, NO_COPY
);
public:
CommandBuffer()
: m_buffer(NULL)
, m_pos(0)
, m_size(0)
, m_minCapacity(0)
{
resize();
finish();
}
~CommandBuffer()
{
bx::free(g_allocator, m_buffer);
}
void init(uint32_t _minCapacity)
{
m_minCapacity = bx::alignUp(_minCapacity, 1024);
resize();
}
enum Enum
{
RendererInit,
RendererShutdownBegin,
CreateVertexLayout,
CreateIndexBuffer,
CreateVertexBuffer,
CreateDynamicIndexBuffer,
UpdateDynamicIndexBuffer,
CreateDynamicVertexBuffer,
UpdateDynamicVertexBuffer,
CreateShader,
CreateProgram,
CreateTexture,
UpdateTexture,
ResizeTexture,
CreateFrameBuffer,
CreateUniform,
UpdateViewName,
InvalidateOcclusionQuery,
SetName,
End,
RendererShutdownEnd,
DestroyVertexLayout,
DestroyIndexBuffer,
DestroyVertexBuffer,
DestroyDynamicIndexBuffer,
DestroyDynamicVertexBuffer,
DestroyShader,
DestroyProgram,
DestroyTexture,
DestroyFrameBuffer,
DestroyUniform,
ReadTexture,
};
void resize(uint32_t _capacity = 0)
{
m_capacity = bx::alignUp(bx::max(_capacity, m_minCapacity), 1024);
m_buffer = (uint8_t*)bx::realloc(g_allocator, m_buffer, m_capacity);
}
void write(const void* _data, uint32_t _size)
{
BX_ASSERT(m_size == 0, "Called write outside start/finish (m_size: %d)?", m_size);
if (m_pos + _size > m_capacity)
{
resize(m_capacity + (16<<10) );
}
bx::memCopy(&m_buffer[m_pos], _data, _size);
m_pos += _size;
}
template<typename Type>
void write(const Type& _in)
{
align(BX_ALIGNOF(Type) );
write(reinterpret_cast<const uint8_t*>(&_in), sizeof(Type) );
}
void write(const bx::StringView& _str)
{
const uint16_t len = bx::narrowCast<uint16_t>(_str.getLength()+1);
write(len);
write(_str.getPtr(), len-1);
write('\0');
}
void read(void* _data, uint32_t _size)
{
BX_ASSERT(m_pos + _size <= m_size
, "CommandBuffer::read error (pos: %d-%d, size: %d)."
, m_pos
, m_pos + _size
, m_size
);
bx::memCopy(_data, &m_buffer[m_pos], _size);
m_pos += _size;
}
template<typename Type>
void read(Type& _in)
{
align(BX_ALIGNOF(Type) );
read(reinterpret_cast<uint8_t*>(&_in), sizeof(Type) );
}
const uint8_t* skip(uint32_t _size)
{
BX_ASSERT(m_pos + _size <= m_size
, "CommandBuffer::skip error (pos: %d-%d, size: %d)."
, m_pos
, m_pos + _size
, m_size
);
const uint8_t* result = &m_buffer[m_pos];
m_pos += _size;
return result;
}
template<typename Type>
void skip()
{
align(BX_ALIGNOF(Type) );
skip(sizeof(Type) );
}
void align(uint32_t _alignment)
{
const uint32_t mask = _alignment-1;
const uint32_t pos = (m_pos+mask) & (~mask);