Unravel Engine C++ Reference
Loading...
Searching...
No Matches
graphics.cpp
Go to the documentation of this file.
1#include "graphics.h"
2#include "bgfx/bgfx.h"
3#include "eviction.h"
4#include "uniform.h"
5#include <bimg/bimg.h>
6#include <bx/file.h>
7#include <algorithm>
8#include <array>
9#include <map>
10namespace gfx
11{
12namespace
13{
14auto get_loggers() -> std::map<std::string, std::function<void(const std::string&, const char*, uint16_t)>>&
15{
16 static std::map<std::string, std::function<void(const std::string&, const char*, uint16_t)>> loggers;
17 return loggers;
18}
19
20struct context_data
21{
22 bool initted = false;
23 uint32_t frame{};
24
28
29} s_context;
30
31auto create_fallback_texture() -> texture_handle
32{
33 constexpr std::uint16_t dim = 4;
34 std::array<std::uint8_t, dim * dim * 4> pixels{};
35 for(std::size_t i = 0; i < dim * dim; ++i)
36 {
37 pixels[i * 4 + 0] = 255;
38 pixels[i * 4 + 1] = 0;
39 pixels[i * 4 + 2] = 255;
40 pixels[i * 4 + 3] = 255;
41 }
42 const memory_view* mem = copy(pixels.data(), static_cast<std::uint32_t>(pixels.size()));
43 return create_texture_2d(dim, dim, false, 1, texture_format::RGBA8, BGFX_TEXTURE_NONE | BGFX_SAMPLER_NONE, mem);
44}
45
46} // namespace
47
48void set_trace_logger(const std::function<void(const std::string&, const char*, uint16_t)>& logger)
49{
50 get_loggers()["trace"] = logger;
51}
52
53void set_debug_logger(const std::function<void(const std::string&, const char*, uint16_t)>& logger)
54{
55 get_loggers()["debug"] = logger;
56}
57
58void set_info_logger(const std::function<void(const std::string&, const char*, uint16_t)>& logger)
59{
60 get_loggers()["info"] = logger;
61}
62void set_warning_logger(const std::function<void(const std::string&, const char*, uint16_t)>& logger)
63{
64 get_loggers()["warning"] = logger;
65}
66void set_error_logger(const std::function<void(const std::string&, const char*, uint16_t)>& logger)
67{
68 get_loggers()["error"] = logger;
69}
70
71void log(const std::string& category, const std::string& log_msg, const char* _filePath, uint16_t _line)
72{
73 if(get_loggers()[category])
74 {
75 get_loggers()[category](log_msg, _filePath, _line);
76 }
77}
78
79namespace
80{
81struct profiler_hook_state
82{
86};
87
88auto profiler_hooks() -> profiler_hook_state&
89{
90 static profiler_hook_state s{};
91 return s;
92}
93
94} // namespace
95
96struct gfx_callback final : public bgfx::CallbackI
97{
98 ~gfx_callback() = default;
99
100 void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) final
101 {
102 char temp[8192];
103 char* out = temp;
104 int32_t len = vsnprintf(out, sizeof(temp), _format, _argList);
105 if((int32_t)sizeof(temp) < len)
106 {
107 out = (char*)alloca(len + 1);
108 len = vsnprintf(out, len, _format, _argList);
109 }
110 out[len] = '\0';
111
112 bx::StringView out_view(out, len);
113 // Determine log type based on prefix
114 if (bx::strCmp(out_view, "WARN ") == 0)
115 {
116 log("warning", out_view.getPtr() + 5, _filePath, _line);
117 }
118 else
119 {
120 log("debug", out, _filePath, _line);
121 }
122 }
123
124 void profilerBegin(const char* _name, uint32_t _abgr, const char* _filePath, uint16_t _line) final
125 {
126 if(const auto& fn = profiler_hooks().on_begin)
127 {
128 fn(_name, _abgr, _filePath, _line);
129 }
130 }
131
132 void profilerBeginLiteral(const char* _name, uint32_t _abgr, const char* _filePath, uint16_t _line) final
133 {
134 if(const auto& fn = profiler_hooks().on_begin_literal)
135 {
136 fn(_name, _abgr, _filePath, _line);
137 }
138 }
139
140 void profilerEnd() final
141 {
142 if(const auto& fn = profiler_hooks().on_end)
143 {
144 fn();
145 }
146 }
147 void fatal(const char* _filePath, uint16_t _line, bgfx::Fatal::Enum _code, const char* _str) final
148 {
149 switch(_code)
150 {
151 case bgfx::Fatal::InvalidShader:
152 {
153 std::string error_msg = "[Invalid Shader] " + std::string(_str);
154 log("error", error_msg, _filePath, _line);
155 break;
156 }
157 case bgfx::Fatal::UnableToInitialize:
158 {
159 std::string error_msg = "[Unable To Initialize] " + std::string(_str);
160 log("error", error_msg, _filePath, _line);
161 break;
162 }
163 case bgfx::Fatal::UnableToCreateTexture:
164 {
165 std::string error_msg = "[Unable To Create Texture] " + std::string(_str);
166 log("error", error_msg, _filePath, _line);
167 break;
168 }
169 case bgfx::Fatal::DeviceLost:
170 {
171 std::string error_msg = "[Device Lost] " + std::string(_str);
172 log("error", error_msg, _filePath, _line);
173 break;
174 }
175 case bgfx::Fatal::DebugCheck:
176 {
177 std::string error_msg = "[Debug Check] " + std::string(_str);
178 log("error", error_msg, _filePath, _line);
179 break;
180 }
181 default:
182 {
183 log("error", _str, _filePath, _line);
184 break;
185 }
186 }
187
188 }
189
190 uint32_t cacheReadSize(uint64_t /*_id*/) final
191 {
192 return 0;
193 }
194
195 bool cacheRead(uint64_t /*_id*/, void* /*_data*/, uint32_t /*_size*/) final
196 {
197 return false;
198 }
199
200 void cacheWrite(uint64_t /*_id*/, const void* /*_data*/, uint32_t /*_size*/) final
201 {
202 }
203
204 virtual void screenShot(
205 const char* _filePath
206 , uint32_t _width
207 , uint32_t _height
208 , uint32_t _pitch
209 , bgfx::TextureFormat::Enum _format
210 , const void* _data
211 , uint32_t _size
212 , bool _yflip) override
213 {
214 const std::string full_path = std::string(_filePath) + ".png";
215
216 bx::FileWriter writer;
217 if (bx::open(&writer, full_path.c_str()))
218 {
219 //if (image_type == ImageType::Png)
220 {
221 bimg::imageWritePng(&writer,
222 _width,
223 _height,
224 _pitch,
225 _data,
226 static_cast<bimg::TextureFormat::Enum>(_format),
227 _yflip,
228 nullptr);
229 }
230 // else
231 // {
232 // bimg::imageWriteTga(&writer, _width, _height, _pitch, _data, false, _yflip);
233 // }
234
235 bx::close(&writer);
236 }
237 }
238
239 void captureBegin(uint32_t /*_width*/,
240 uint32_t /*_height*/,
241 uint32_t /*_pitch*/,
242 texture_format /*_format*/,
243 bool /*_yflip*/) final
244 {
245 }
246
247 void captureEnd() final
248 {
249 }
250
251 void captureFrame(const void* /*_data*/, uint32_t /*_size*/) final
252 {
253 }
254};
255
259{
260 auto& h = profiler_hooks();
261 h.on_begin = std::move(on_begin);
262 h.on_begin_literal = std::move(on_begin_literal);
263 h.on_end = std::move(on_end);
264}
265
267{
268 auto& h = profiler_hooks();
269 h.on_begin = {};
270 h.on_begin_literal = {};
271 h.on_end = {};
272}
273
275{
276 bgfx::setPlatformData(_data);
277}
278
280{
282 if(s_context.initted)
283 {
285 if(bgfx::isValid(s_context.fallback_texture))
286 {
287 gfx::destroy(s_context.fallback_texture);
288 s_context.fallback_texture = {bgfx::kInvalidHandle};
289 }
291 bgfx::destroy(s_context.u_world);
292 bgfx::shutdown();
293 }
294 s_context = {};
295}
296
298{
299 s_context.allocation_policy = policy;
300}
301
303{
304 return s_context.allocation_policy;
305}
306
308{
309 return s_context.fallback_texture;
310}
311
312bool init(init_type init_data)
313{
314 if(init_data.callback == nullptr)
315 {
316 static gfx_callback callback;
317 init_data.callback = &callback;
318 }
319 s_context.frame = 0;
320 s_context.initted = bgfx::init(init_data);
321 s_context.u_world = create_uniform("u_world", bgfx::UniformType::Mat4, get_max_blend_transforms());
322
323 bgfx::frame();
324 // Initialize after the first frame so the backend has reported its GPU memory budget.
325 switch(eviction::init())
326 {
328 log("info", "GPU resource eviction enabled.", __FILE__, __LINE__);
329 break;
331 log("info", "GPU resource eviction disabled: backend does not need eviction.", __FILE__, __LINE__);
332 break;
334 log("info",
335 "GPU resource eviction disabled: backend does not report a GPU memory budget.",
336 __FILE__,
337 __LINE__);
338 break;
340 log("warning", "GPU resource eviction failed to initialize.", __FILE__, __LINE__);
341 break;
342 }
343 if(s_context.initted)
344 {
345 s_context.fallback_texture = create_fallback_texture();
346 if(bgfx::isValid(s_context.fallback_texture))
347 {
348 set_name(s_context.fallback_texture, "gfx-fallback");
349 }
350 }
351 return s_context.initted;
352}
353
354void vertex_pack(const float _input[4],
355 bool _inputNormalized,
356 attribute _attr,
357 const vertex_layout& _decl,
358 void* _data,
359 uint32_t _index)
360{
361 bgfx::vertexPack(_input, _inputNormalized, _attr, _decl, _data, _index);
362}
363
364void vertex_unpack(float _output[4], attribute _attr, const vertex_layout& _decl, const void* _data, uint32_t _index)
365{
366 bgfx::vertexUnpack(_output, _attr, _decl, _data, _index);
367}
368
369void vertex_convert(const vertex_layout& _destDecl,
370 void* _destData,
371 const vertex_layout& _srcDecl,
372 const void* _srcData,
373 uint32_t _num)
374{
375 bgfx::vertexConvert(_destDecl, _destData, _srcDecl, _srcData, _num);
376}
377
379 void* _dst,
380 uint32_t _dstSize,
381 const void* _indices,
382 uint32_t _numIndices,
383 bool _index32)
384{
385 return bgfx::topologyConvert(_conversion, _dst, _dstSize, _indices, _numIndices, _index32);
386}
387
389 void* _dst,
390 uint32_t _dstSize,
391 const float _dir[],
392 const float _pos[],
393 const void* _vertices,
394 uint32_t _stride,
395 const void* _indices,
396 uint32_t _numIndices,
397 bool _index32)
398{
399 bgfx::topologySortTriList(_sort, _dst, _dstSize, _dir, _pos, _vertices, _stride, _indices, _numIndices, _index32);
400}
401
403{
404 return bgfx::createEmbeddedShader(_es, _type, _name);
405}
406
407uint8_t get_supported_renderers(uint8_t _max, renderer_type* _enum)
408{
409 return bgfx::getSupportedRenderers(_max, _enum);
410}
411
413{
414 return bgfx::getRendererName(_type);
415}
416
417void reset(uint32_t _width, uint32_t _height, uint32_t _flags)
418{
419 bgfx::reset(_width, _height, _flags);
420}
421
423{
424 return bgfx::begin();
425}
426
427void end(encoder* _encoder)
428{
429 bgfx::end(_encoder);
430}
431
432uint32_t frame(uint8_t _flags)
433{
434 s_context.frame = bgfx::frame(_flags);
435 eviction::set_frame(s_context.frame);
437 return s_context.frame;
438}
439
441{
442 return bgfx::getRendererType();
443}
444
446{
447 return bgfx::getCaps();
448}
449
451{
452 return bgfx::getStats();
453}
454
455const memory_view* alloc(uint32_t _size)
456{
457 return bgfx::alloc(_size);
458}
459
460const memory_view* copy(const void* _data, uint32_t _size)
461{
462 return bgfx::copy(_data, _size);
463}
464
465const memory_view* make_ref(const void* _data, uint32_t _size, release_fn _releaseFn, void* _userData)
466{
467 return bgfx::makeRef(_data, _size, _releaseFn, _userData);
468}
469
470void set_debug(uint32_t _debug)
471{
472 bgfx::setDebug(_debug);
473}
474
475void dbg_text_clear(uint8_t _attr, bool _small)
476{
477 bgfx::dbgTextClear(_attr, _small);
478}
479
480void dbg_text_printf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format)
481{
482 bgfx::dbgTextPrintf(_x, _y, _attr, _format);
483}
484
485void dbg_text_vprintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, va_list _argList)
486{
487 bgfx::dbgTextPrintfVargs(_x, _y, _attr, _format, _argList);
488}
489
490void dbg_text_image(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const void* _data, uint16_t _pitch)
491{
492 bgfx::dbgTextImage(_x, _y, _width, _height, _data, _pitch);
493}
494
496{
497 return bgfx::createIndexBuffer(_mem, _flags);
498}
499
500void set_name(index_buffer_handle _handle, const char* _name, int32_t _len)
501{
502 bgfx::setName(_handle, _name, _len);
503}
504
506{
507 bgfx::destroy(_handle);
508}
509
510vertex_buffer_handle create_vertex_buffer(const memory_view* _mem, const vertex_layout& _decl, uint16_t _flags)
511{
512 return bgfx::createVertexBuffer(_mem, _decl, _flags);
513}
514
516{
517 bgfx::destroy(_handle);
518}
519
520void set_name(vertex_buffer_handle _handle, const char* _name, int32_t _len)
521{
522 bgfx::setName(_handle, _name, _len);
523}
524
526{
527 return bgfx::createDynamicIndexBuffer(_num, _flags);
528}
529
531{
532 return bgfx::createDynamicIndexBuffer(_mem, _flags);
533}
534
535void update(dynamic_index_buffer_handle _handle, uint32_t _startIndex, const memory_view* _mem)
536{
537 bgfx::update(_handle, _startIndex, _mem);
538}
539
541{
542 bgfx::destroy(_handle);
543}
544
546{
547 return bgfx::createDynamicVertexBuffer(_num, _decl, _flags);
548}
549
551 const vertex_layout& _decl,
552 uint16_t _flags)
553{
554 return bgfx::createDynamicVertexBuffer(_mem, _decl, _flags);
555}
556
557void update(dynamic_vertex_buffer_handle _handle, uint32_t _startVertex, const memory_view* _mem)
558{
559 bgfx::update(_handle, _startVertex, _mem);
560}
561
563{
564 bgfx::destroy(_handle);
565}
566
567uint32_t get_avail_transient_index_buffer(uint32_t _num, bool _index32)
568{
569 return bgfx::getAvailTransientIndexBuffer(_num, _index32);
570}
571
572uint32_t get_avail_transient_vertex_buffer(uint32_t _num, const vertex_layout& _decl)
573{
574 return bgfx::getAvailTransientVertexBuffer(_num, _decl);
575}
576
577uint32_t get_avail_instance_data_buffer(uint32_t _num, uint16_t _stride)
578{
579 return bgfx::getAvailInstanceDataBuffer(_num, _stride);
580}
581
582void alloc_transient_index_buffer(transient_index_buffer* _tib, uint32_t _num, bool _index32)
583{
584 bgfx::allocTransientIndexBuffer(_tib, _num, _index32);
585}
586
588{
589 bgfx::allocTransientVertexBuffer(_tvb, _num, _decl);
590}
591
593 const vertex_layout& _decl,
594 uint32_t _numVertices,
596 uint32_t _numIndices)
597{
598 return bgfx::allocTransientBuffers(_tvb, _decl, _numVertices, _tib, _numIndices);
599}
600
601void alloc_instance_data_buffer(instance_data_buffer* _idb, uint32_t _num, uint16_t _stride)
602{
603 bgfx::allocInstanceDataBuffer(_idb, _num, _stride);
604}
605
607{
608 return bgfx::createIndirectBuffer(_num);
609}
610
612{
613 bgfx::destroy(_handle);
614}
615
617{
618 return bgfx::createShader(_mem);
619}
620
621uint16_t get_shader_uniforms(shader_handle _handle, uniform_handle* _uniforms, uint16_t _max)
622{
623 return bgfx::getShaderUniforms(_handle, _uniforms, _max);
624}
625
627{
628 bgfx::getUniformInfo(_handle, _info);
629}
630
631void set_name(shader_handle _handle, const char* _name, int32_t _len)
632{
633 bgfx::setName(_handle, _name, _len);
634}
635
637{
638 bgfx::destroy(_handle);
639}
640
642{
643 return bgfx::createProgram(_vsh, _fsh, _destroyShaders);
644}
645
646program_handle create_program(shader_handle _csh, bool _destroyShaders)
647{
648 return bgfx::createProgram(_csh, _destroyShaders);
649}
650
652{
653 bgfx::destroy(_handle);
654}
655
656bool is_texture_valid(uint16_t _depth, bool _cubeMap, uint16_t _numLayers, texture_format _format, uint64_t _flags)
657{
658 return bgfx::isTextureValid(_depth, _cubeMap, _numLayers, _format, _flags);
659}
660
662 uint16_t _width,
663 uint16_t _height,
664 uint16_t _depth,
665 bool _cubeMap,
666 bool _hasMips,
667 uint16_t _numLayers,
668 texture_format _format)
669{
670 bgfx::calcTextureSize(_info, _width, _height, _depth, _cubeMap, _hasMips, _numLayers, _format);
671}
672
673texture_handle create_texture(const memory_view* _mem, uint64_t _flags, uint8_t _skip, texture_info* _info)
674{
675 return bgfx::createTexture(_mem, _flags, _skip, _info);
676}
677
679 uint16_t _height,
680 bool _hasMips,
681 uint16_t _numLayers,
682 texture_format _format,
683 uint64_t _flags,
684 const memory_view* _mem)
685{
686 return bgfx::createTexture2D(_width, _height, _hasMips, _numLayers, _format, _flags, _mem);
687}
688
690 bool _hasMips,
691 uint16_t _numLayers,
692 texture_format _format,
693 uint64_t _flags)
694{
695 return bgfx::createTexture2D(_ratio, _hasMips, _numLayers, _format, _flags);
696}
697
699 uint16_t _height,
700 uint16_t _depth,
701 bool _hasMips,
702 texture_format _format,
703 uint64_t _flags,
704 const memory_view* _mem)
705{
706 return bgfx::createTexture3D(_width, _height, _depth, _hasMips, _format, _flags, _mem);
707}
708
710 bool _hasMips,
711 uint16_t _numLayers,
712 texture_format _format,
713 uint64_t _flags,
714 const memory_view* _mem)
715{
716 return bgfx::createTextureCube(_size, _hasMips, _numLayers, _format, _flags, _mem);
717}
718
720 uint16_t _layer,
721 uint8_t _mip,
722 uint16_t _x,
723 uint16_t _y,
724 uint16_t _width,
725 uint16_t _height,
726 const memory_view* _mem,
727 uint16_t _pitch)
728{
729 bgfx::updateTexture2D(_handle, _layer, _mip, _x, _y, _width, _height, _mem, _pitch);
730}
731
733 uint8_t _mip,
734 uint16_t _x,
735 uint16_t _y,
736 uint16_t _z,
737 uint16_t _width,
738 uint16_t _height,
739 uint16_t _depth,
740 const memory_view* _mem)
741{
742 bgfx::updateTexture3D(_handle, _mip, _x, _y, _z, _width, _height, _depth, _mem);
743}
744
746 uint16_t _layer,
747 uint8_t _side,
748 uint8_t _mip,
749 uint16_t _x,
750 uint16_t _y,
751 uint16_t _width,
752 uint16_t _height,
753 const memory_view* _mem,
754 uint16_t _pitch)
755{
756 bgfx::updateTextureCube(_handle, _layer, _side, _mip, _x, _y, _width, _height, _mem, _pitch);
757}
758
759uint32_t read_texture(texture_handle _handle, void* _data, uint16_t _layer, uint8_t _mip)
760{
761 return bgfx::readTexture(_handle, _data, _layer, _mip);
762}
763
764void set_name(texture_handle _handle, const char* _name, int32_t _len)
765{
766 bgfx::setName(_handle, _name, _len);
767}
768
770{
771 bgfx::destroy(_handle);
772}
773
775 uint16_t _height,
776 texture_format _format,
777 uint64_t _textureFlags)
778{
779 return bgfx::createFrameBuffer(_width, _height, _format, _textureFlags);
780}
781
783{
784 return bgfx::createFrameBuffer(_ratio, _format, _textureFlags);
785}
786
787frame_buffer_handle create_frame_buffer(uint8_t _num, const texture_handle* _handles, bool _destroyTextures)
788{
789 return bgfx::createFrameBuffer(_num, _handles, _destroyTextures);
790}
791
792frame_buffer_handle create_frame_buffer(uint8_t _num, const attachment* _attachment, bool _destroyTextures)
793{
794 return bgfx::createFrameBuffer(_num, _attachment, _destroyTextures);
795}
796
798 uint16_t _width,
799 uint16_t _height,
800 texture_format _format,
801 texture_format _depthFormat)
802{
803 return bgfx::createFrameBuffer(_nwh, _width, _height, _format, _depthFormat);
804}
805
806texture_handle get_texture(frame_buffer_handle _handle, uint8_t _attachment)
807{
808 return bgfx::getTexture(_handle, _attachment);
809}
810
811void set_name(frame_buffer_handle _handle, const char* _name, int32_t _len)
812{
813 bgfx::setName(_handle, _name, _len);
814}
815
817{
818 bgfx::destroy(_handle);
819}
820
821uniform_handle create_uniform(const char* _name, uniform_type _type, uint16_t _num)
822{
823 return bgfx::createUniform(_name, _type, _num);
824}
825
827{
828 bgfx::destroy(_handle);
829}
830
832{
833 return bgfx::createOcclusionQuery();
834}
835
837{
838 return bgfx::getResult(_handle, _result);
839}
840
842{
843 bgfx::destroy(_handle);
844}
845
846void set_palette_color(uint8_t _index, uint32_t _rgba)
847{
848 bgfx::setPaletteColor(_index, _rgba);
849}
850
851void set_palette_color(uint8_t _index, const float _rgba[])
852{
853 bgfx::setPaletteColor(_index, _rgba);
854}
855
856void set_palette_color(uint8_t _index, float _r, float _g, float _b, float _a)
857{
858 bgfx::setPaletteColor(_index, _r, _g, _b, _a);
859}
860
861void set_view_name(view_id _id, const char* _name)
862{
863 bgfx::setViewName(_id, _name);
864}
865
866void set_view_rect(view_id _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
867{
868 bgfx::setViewRect(_id, _x, _y, _width, _height);
869}
870
871void set_view_rect(view_id _id, uint16_t _x, uint16_t _y, backbuffer_ratio _ratio)
872{
873 bgfx::setViewRect(_id, _x, _y, _ratio);
874}
875
876void set_view_scissor(view_id _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
877{
878 bgfx::setViewScissor(_id, _x, _y, _width, _height);
879}
880
881void set_view_clear(view_id _id, uint16_t _flags, uint32_t _rgba, float _depth, uint8_t _stencil)
882{
883 bgfx::setViewClear(_id, _flags, _rgba, _depth, _stencil);
884}
885
887 uint16_t _flags,
888 float _depth,
889 uint8_t _stencil,
890 uint8_t _0,
891 uint8_t _1,
892 uint8_t _2,
893 uint8_t _3,
894 uint8_t _4,
895 uint8_t _5,
896 uint8_t _6,
897 uint8_t _7)
898{
899 bgfx::setViewClear(_id, _flags, _depth, _stencil, _0, _1, _2, _3, _4, _5, _6, _7);
900}
901
903{
904 bgfx::setViewMode(_id, _mode);
905}
906
908{
909 bgfx::setViewFrameBuffer(_id, _handle);
910}
911
912void set_view_transform(view_id _id, const void* _view, const void* _proj)
913{
914 bgfx::setViewTransform(_id, _view, _proj);
915}
916
917void set_view_order(view_id _id, uint16_t _num, const view_id* _order)
918{
919 bgfx::setViewOrder(_id, _num, _order);
920}
921
923{
924 bgfx::setViewShadingRate(_id, _shadingRate);
925}
926
928{
929 bgfx::resetView(_id);
930}
931
932void set_marker(const char* _marker)
933{
934 bgfx::setMarker(_marker);
935}
936
937void set_state(uint64_t _state, uint32_t _rgba)
938{
939 bgfx::setState(_state, _rgba);
940}
941
942void set_condition(occlusion_query_handle _handle, bool _visible)
943{
944 bgfx::setCondition(_handle, _visible);
945}
946
947void set_stencil(uint32_t _fstencil, uint32_t _bstencil)
948{
949 bgfx::setStencil(_fstencil, _bstencil);
950}
951
952uint16_t set_scissor(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
953{
954 return bgfx::setScissor(_x, _y, _width, _height);
955}
956
957void set_scissor(uint16_t _cache)
958{
959 bgfx::setScissor(_cache);
960}
961
962uint32_t set_transform(const void* _mtx, uint16_t _num)
963{
964 return bgfx::setTransform(_mtx, _num);
965}
966
967uint32_t alloc_transform(transform* _transform, uint16_t _num)
968{
969 return bgfx::allocTransform(_transform, _num);
970}
971
972void set_transform(uint32_t _cache, uint16_t _num)
973{
974 bgfx::setTransform(_cache, _num);
975}
976
977void set_uniform(uniform_handle _handle, const void* _value, uint16_t _num)
978{
979 bgfx::setUniform(_handle, _value, _num);
980}
981
982void set_view_uniform(view_id _id, uniform_handle _handle, const void* _value, uint16_t _num)
983{
984 bgfx::setViewUniform(_id, _handle, _value, _num);
985}
986
987void set_frame_uniform(uniform_handle _handle, const void* _value, uint16_t _num)
988{
989 bgfx::setFrameUniform(_handle, _value, _num);
990}
991
993{
994 bgfx::setIndexBuffer(_handle);
995}
996
997void set_index_buffer(index_buffer_handle _handle, uint32_t _firstIndex, uint32_t _numIndices)
998{
999 bgfx::setIndexBuffer(_handle, _firstIndex, _numIndices);
1000}
1001
1003{
1004 bgfx::setIndexBuffer(_handle);
1005}
1006
1007void set_index_buffer(dynamic_index_buffer_handle _handle, uint32_t _firstIndex, uint32_t _numIndices)
1008{
1009 bgfx::setIndexBuffer(_handle, _firstIndex, _numIndices);
1010}
1011
1013{
1014 bgfx::setIndexBuffer(_tib);
1015}
1016
1017void set_index_buffer(const transient_index_buffer* _tib, uint32_t _firstIndex, uint32_t _numIndices)
1018{
1019 bgfx::setIndexBuffer(_tib, _firstIndex, _numIndices);
1020}
1021
1022void set_vertex_buffer(uint8_t _stream, vertex_buffer_handle _handle)
1023{
1024 bgfx::setVertexBuffer(_stream, _handle);
1025}
1026
1027void set_vertex_buffer(uint8_t _stream, vertex_buffer_handle _handle, uint32_t _startVertex, uint32_t _numVertices)
1028{
1029 bgfx::setVertexBuffer(_stream, _handle, _startVertex, _numVertices);
1030}
1031
1033{
1034 bgfx::setVertexBuffer(_stream, _handle);
1035}
1036
1037void set_vertex_buffer(uint8_t _stream,
1039 uint32_t _startVertex,
1040 uint32_t _numVertices)
1041{
1042 bgfx::setVertexBuffer(_stream, _handle, _startVertex, _numVertices);
1043}
1044
1045void set_vertex_buffer(uint8_t _stream, const transient_vertex_buffer* _tvb)
1046{
1047 bgfx::setVertexBuffer(_stream, _tvb);
1048}
1049
1050void set_vertex_buffer(uint8_t _stream,
1051 const transient_vertex_buffer* _tvb,
1052 uint32_t _startVertex,
1053 uint32_t _numVertices)
1054{
1055 bgfx::setVertexBuffer(_stream, _tvb, _startVertex, _numVertices);
1056}
1057
1058void set_vertex_count(uint32_t _numVertices)
1059{
1060 bgfx::setVertexCount(_numVertices);
1061}
1062
1063void set_instance_data_buffer(const instance_data_buffer* _idb, uint32_t _start, uint32_t _num)
1064{
1065 bgfx::setInstanceDataBuffer(_idb, _start, _num);
1066}
1067
1068void set_instance_data_buffer(vertex_buffer_handle _handle, uint32_t _startVertex, uint32_t _num)
1069{
1070 bgfx::setInstanceDataBuffer(_handle, _startVertex, _num);
1071}
1072
1073void set_instance_data_buffer(dynamic_vertex_buffer_handle _handle, uint32_t _startVertex, uint32_t _num)
1074{
1075 bgfx::setInstanceDataBuffer(_handle, _startVertex, _num);
1076}
1077
1078void set_texture(uint8_t _stage, uniform_handle _sampler, texture_handle _handle, uint32_t _flags)
1079{
1080 bgfx::setTexture(_stage, _sampler, _handle, _flags);
1081}
1082
1084{
1085 bgfx::touch(_id);
1086}
1087
1088void submit(view_id _id, program_handle _handle, int32_t _depth, bool _preserveState)
1089{
1090 bgfx::submit(_id, _handle, _depth, _preserveState ? BGFX_DISCARD_NONE : BGFX_DISCARD_ALL);
1091}
1092
1094 program_handle _program,
1095 occlusion_query_handle _occlusionQuery,
1096 int32_t _depth,
1097 bool _preserveState)
1098{
1099 bgfx::submit(_id, _program, _occlusionQuery, _depth, _preserveState ? BGFX_DISCARD_NONE : BGFX_DISCARD_ALL);
1100}
1101
1103 program_handle _handle,
1104 indirect_buffer_handle _indirectHandle,
1105 uint16_t _start,
1106 uint16_t _num,
1107 int32_t _depth,
1108 bool _preserveState)
1109{
1110 bgfx::submit(_id, _handle, _indirectHandle, _start, _num, _depth, _preserveState ? BGFX_DISCARD_NONE : BGFX_DISCARD_ALL);
1111}
1112
1113void set_image(uint8_t _stage, texture_handle _handle, uint8_t _mip, access _access, texture_format _format)
1114{
1115 bgfx::setImage(_stage, _handle, _mip, _access, _format);
1116}
1117
1118void set_buffer(uint8_t _stage, index_buffer_handle _handle, access _access)
1119{
1120 bgfx::setBuffer(_stage, _handle, _access);
1121}
1122
1123void set_buffer(uint8_t _stage, vertex_buffer_handle _handle, access _access)
1124{
1125 bgfx::setBuffer(_stage, _handle, _access);
1126}
1127
1128void set_buffer(uint8_t _stage, dynamic_index_buffer_handle _handle, access _access)
1129{
1130 bgfx::setBuffer(_stage, _handle, _access);
1131}
1132
1133void set_buffer(uint8_t _stage, dynamic_vertex_buffer_handle _handle, access _access)
1134{
1135 bgfx::setBuffer(_stage, _handle, _access);
1136}
1137
1138void set_buffer(uint8_t _stage, indirect_buffer_handle _handle, access _access)
1139{
1140 bgfx::setBuffer(_stage, _handle, _access);
1141}
1142
1143void dispatch(view_id _id, program_handle _handle, uint32_t _numX, uint32_t _numY, uint32_t _numZ)
1144{
1145 bgfx::dispatch(_id, _handle, _numX, _numY, _numZ);
1146}
1147
1149 program_handle _handle,
1150 indirect_buffer_handle _indirectHandle,
1151 uint16_t _start,
1152 uint16_t _num)
1153{
1154 bgfx::dispatch(_id, _handle, _indirectHandle, _start, _num);
1155}
1156
1157void discard(uint8_t _flags)
1158{
1159 bgfx::discard();
1160}
1161
1162void blit(view_id _id,
1163 texture_handle _dst,
1164 uint16_t _dstX,
1165 uint16_t _dstY,
1166 texture_handle _src,
1167 uint16_t _srcX,
1168 uint16_t _srcY,
1169 uint16_t _width,
1170 uint16_t _height)
1171{
1172 bgfx::blit(_id, _dst, _dstX, _dstY, _src, _srcX, _srcY, _width, _height);
1173}
1174
1175void blit(view_id _id,
1176 texture_handle _dst,
1177 uint8_t _dstMip,
1178 uint16_t _dstX,
1179 uint16_t _dstY,
1180 uint16_t _dstZ,
1181 texture_handle _src,
1182 uint8_t _srcMip,
1183 uint16_t _srcX,
1184 uint16_t _srcY,
1185 uint16_t _srcZ,
1186 uint16_t _width,
1187 uint16_t _height,
1188 uint16_t _depth)
1189{
1190 bgfx::blit(_id, _dst, _dstMip, _dstX, _dstY, _dstZ, _src, _srcMip, _srcX, _srcY, _srcZ, _width, _height, _depth);
1191}
1192
1193void request_screen_shot(frame_buffer_handle _handle, const char* _filePath)
1194{
1195 bgfx::requestScreenShot(_handle, _filePath);
1196}
1197
1198void frames(int _count, int32_t _flags)
1199{
1200 for(int i = 0; i < _count; ++i)
1201 {
1202 frame(_flags);
1203
1204 s_context.frame = bgfx::frame(_flags);
1205 eviction::set_frame(s_context.frame);
1207 }
1208}
1209
1210auto screen_quad(float dest_width, float dest_height, float depth, float width, float height) -> uint64_t
1211{
1212 float texture_half = get_half_texel();
1213 bool origin_bottom_left = is_origin_bottom_left();
1214
1215 if(3 == getAvailTransientVertexBuffer(3, pos_texcoord0_vertex::get_layout()))
1216 {
1219 auto vertex = reinterpret_cast<pos_texcoord0_vertex*>(vb.data);
1220
1221 const float minx = -width;
1222 const float maxx = width;
1223 const float miny = 0.0f;
1224 const float maxy = height * 2.0f;
1225
1226 const float texel_half_w = texture_half / dest_width;
1227 const float texel_half_h = texture_half / dest_height;
1228 const float minu = -1.0f + texel_half_w;
1229 const float maxu = 1.0f + texel_half_h;
1230
1231 const float zz = depth;
1232
1233 float minv = texel_half_h;
1234 float maxv = 2.0f + texel_half_h;
1235
1236 if(origin_bottom_left)
1237 {
1238 float temp = minv;
1239 minv = maxv;
1240 maxv = temp;
1241
1242 minv -= 1.0f;
1243 maxv -= 1.0f;
1244 }
1245
1246 vertex[0].x = minx;
1247 vertex[0].y = miny;
1248 vertex[0].z = zz;
1249 vertex[0].u = minu;
1250 vertex[0].v = minv;
1251
1252 vertex[1].x = maxx;
1253 vertex[1].y = miny;
1254 vertex[1].z = zz;
1255 vertex[1].u = maxu;
1256 vertex[1].v = minv;
1257
1258 vertex[2].x = maxx;
1259 vertex[2].y = maxy;
1260 vertex[2].z = zz;
1261 vertex[2].u = maxu;
1262 vertex[2].v = maxv;
1263
1264 bgfx::setVertexBuffer(0, &vb);
1265 }
1266
1267 return 0;
1268}
1269
1270auto clip_fullscreen_triangle(float depth, float width, float height) -> uint64_t
1271{
1272 bool origin_bottom_left = is_origin_bottom_left();
1273
1274 if(3 != getAvailTransientVertexBuffer(3, pos_texcoord0_vertex::get_layout()))
1275 {
1276 return 0;
1277 }
1278
1281 auto vertex = reinterpret_cast<pos_texcoord0_vertex*>(vb.data);
1282
1283 const float minx = -width;
1284 const float maxx = width;
1285 const float miny = -height;
1286 const float maxy = height;
1287
1288 const float minu = 0.0f;
1289 const float maxu = 1.0f;
1290
1291 const float zz = depth;
1292
1293 float minv = 1.0f;
1294 float maxv = 0.0f;
1295
1296 if(origin_bottom_left)
1297 {
1298 minv = 1.0f - minv;
1299 maxv = 1.0f - maxv;
1300 }
1301
1302 // One triangle covering [-width,width] x [-height,height] with extrapolated UVs (no quad diagonal).
1303 // v0: BL, v1: bottom edge extrapolated past BR, v2: left edge extrapolated past TL.
1304 vertex[0].x = minx;
1305 vertex[0].y = miny;
1306 vertex[0].z = zz;
1307 vertex[0].u = minu;
1308 vertex[0].v = minv;
1309
1310 vertex[1].x = maxx + 2.0f * width;
1311 vertex[1].y = miny;
1312 vertex[1].z = zz;
1313 vertex[1].u = minu + 2.0f * (maxu - minu);
1314 vertex[1].v = minv;
1315
1316 vertex[2].x = minx;
1317 vertex[2].y = maxy + 2.0f * height;
1318 vertex[2].z = zz;
1319 vertex[2].u = minu;
1320 vertex[2].v = minv + 2.0f * (maxv - minv);
1321
1322 bgfx::setVertexBuffer(0, &vb);
1323
1324 return BGFX_STATE_PT_TRISTRIP;
1325}
1326
1327auto clip_quad(float depth, float width, float height) -> uint64_t
1328{
1329 // float texture_half = get_half_texel();
1330 bool origin_bottom_left = is_origin_bottom_left();
1331
1332 if(4 == getAvailTransientVertexBuffer(4, pos_texcoord0_vertex::get_layout()))
1333 {
1336 auto vertex = reinterpret_cast<pos_texcoord0_vertex*>(vb.data);
1337
1338 const float minx = -width;
1339 const float maxx = width;
1340 const float miny = -height;
1341 const float maxy = height;
1342
1343 // const float texel_half_w = texture_half;
1344 // const float texel_half_h = texture_half;
1345 const float minu = 0.0f;
1346 const float maxu = 1.0f;
1347
1348 const float zz = depth;
1349
1350 float minv = 1.0f;
1351 float maxv = 0.0f;
1352
1353 if(origin_bottom_left)
1354 {
1355 minv = 1.0f - minv;
1356 maxv = 1.0f - maxv;
1357 }
1358
1359 vertex[0].x = minx;
1360 vertex[0].y = maxy;
1361 vertex[0].z = zz;
1362 vertex[0].u = minu;
1363 vertex[0].v = maxv;
1364
1365 vertex[1].x = maxx;
1366 vertex[1].y = maxy;
1367 vertex[1].z = zz;
1368 vertex[1].u = maxu;
1369 vertex[1].v = maxv;
1370
1371 vertex[2].x = minx;
1372 vertex[2].y = miny;
1373 vertex[2].z = zz;
1374 vertex[2].u = minu;
1375 vertex[2].v = minv;
1376
1377 vertex[3].x = maxx;
1378 vertex[3].y = miny;
1379 vertex[3].z = zz;
1380 vertex[3].u = maxu;
1381 vertex[3].v = minv;
1382
1383 bgfx::setVertexBuffer(0, &vb);
1384 }
1385
1386 return BGFX_STATE_PT_TRISTRIP;
1387}
1388
1389uint64_t clip_quad_ex(const clip_quad_def& def)
1390{
1391 // float texture_half = get_half_texel();
1392 bool origin_bottom_left = is_origin_bottom_left();
1393
1394 if(4 == getAvailTransientVertexBuffer(4, pos_texcoord0_vertex::get_layout()))
1395 {
1398 auto vertex = reinterpret_cast<pos_texcoord0_vertex*>(vb.data);
1399
1400 // Apply position offset to the quad geometry
1401 const float minx = -def.width + def.offset_x;
1402 const float maxx = def.width + def.offset_x;
1403 const float miny = -def.height + def.offset_y;
1404 const float maxy = def.height + def.offset_y;
1405
1406 // const float texel_half_w = texture_half;
1407 // const float texel_half_h = texture_half;
1408 const float minu = 0.0f;
1409 const float maxu = 1.0f;
1410
1411 const float zz = def.depth;
1412
1413 float minv = 1.0f;
1414 float maxv = 0.0f;
1415
1416 vertex[0].x = minx;
1417 vertex[0].y = maxy;
1418 vertex[0].z = zz;
1419 vertex[0].u = minu;
1420 vertex[0].v = maxv;
1421
1422 vertex[1].x = maxx;
1423 vertex[1].y = maxy;
1424 vertex[1].z = zz;
1425 vertex[1].u = maxu;
1426 vertex[1].v = maxv;
1427
1428 vertex[2].x = minx;
1429 vertex[2].y = miny;
1430 vertex[2].z = zz;
1431 vertex[2].u = minu;
1432 vertex[2].v = minv;
1433
1434 vertex[3].x = maxx;
1435 vertex[3].y = miny;
1436 vertex[3].z = zz;
1437 vertex[3].u = maxu;
1438 vertex[3].v = minv;
1439
1440 // Apply UV offset and scaling
1441 for(int i = 0; i < 4; i++)
1442 {
1443 vertex[i].u = (vertex[i].u * def.uv_scaling_x) + def.uv_offset_x;
1444 vertex[i].v = (vertex[i].v * def.uv_scaling_y) + def.uv_offset_y;
1445 if(origin_bottom_left)
1446 {
1447 vertex[i].v = 1.0f - vertex[i].v;
1448 }
1449 }
1450
1451 bgfx::setVertexBuffer(0, &vb);
1452 }
1453
1454 return BGFX_STATE_PT_TRISTRIP;
1455}
1456
1457void get_size_from_ratio(backbuffer_ratio _ratio, uint16_t& _width, uint16_t& _height)
1458{
1459 auto stats = bgfx::getStats();
1460 _width = stats->width;
1461 _height = stats->height;
1462 switch(_ratio)
1463 {
1464 case backbuffer_ratio::Half:
1465 _width /= 2;
1466 _height /= 2;
1467 break;
1468 case backbuffer_ratio::Quarter:
1469 _width /= 4;
1470 _height /= 4;
1471 break;
1472 case backbuffer_ratio::Eighth:
1473 _width /= 8;
1474 _height /= 8;
1475 break;
1476 case backbuffer_ratio::Sixteenth:
1477 _width /= 16;
1478 _height /= 16;
1479 break;
1480 case backbuffer_ratio::Double:
1481 _width *= 2;
1482 _height *= 2;
1483 break;
1484
1485 default:
1486 break;
1487 }
1488
1489 _width = std::max<uint16_t>(1, _width);
1490 _height = std::max<uint16_t>(1, _height);
1491}
1492
1493auto get_renderer_filename_extension(renderer_type _type) -> const std::string&
1494{
1495 static const std::map<renderer_type, std::string> types = {{renderer_type::Direct3D11, ".dxbc"},
1496 {renderer_type::Direct3D12, ".dxil"},
1497 {renderer_type::Gnm, ".pssl"},
1498 {renderer_type::Metal, ".metal"},
1499 {renderer_type::Nvn, ".nvn"},
1500
1501 {renderer_type::OpenGL, ".gl"},
1502 {renderer_type::OpenGLES, ".essl"},
1503 {renderer_type::Vulkan, ".spirv"}};
1504
1505 const auto it = types.find(_type);
1506 if(it != types.cend())
1507 {
1508 return it->second;
1509 }
1510 static std::string unknown = ".unknown";
1511 return unknown;
1512}
1513
1514
1515
1517{
1518 return get_renderer_filename_extension(bgfx::getRendererType());
1519}
1520
1521auto get_renderer_platform_supported_filename_extensions() -> const std::set<std::string>&
1522{
1523#if BX_PLATFORM_WINDOWS
1524 static const std::set<std::string> supported = {get_renderer_filename_extension(renderer_type::Direct3D11),
1525 get_renderer_filename_extension(renderer_type::Direct3D12),
1526 get_renderer_filename_extension(renderer_type::OpenGL),
1527 get_renderer_filename_extension(renderer_type::Vulkan)};
1528 return supported;
1529#else
1530 static const std::set<std::string> supported = {get_current_renderer_filename_extension()};
1531 return supported;
1532#endif
1533}
1534
1535
1537{
1538 for(int i = renderer_type::Noop; i < renderer_type::Count; ++i)
1539 {
1540 auto rtype = renderer_type(i);
1541 if(get_renderer_filename_extension(rtype) == _type)
1542 {
1543 return rtype;
1544 }
1545 }
1546
1547 return get_renderer_type();
1548}
1549
1550
1551
1553{
1554 //We handle this in shaders.
1555 return false;//get_caps()->homogeneousDepth;
1556}
1557
1559{
1560 return get_caps()->originBottomLeft;
1561}
1562
1564{
1565 return 128;
1566}
1567
1568auto get_half_texel() -> float
1569{
1570 float half_texel = 0.0f;
1571 return half_texel;
1572}
1573
1574auto is_supported(uint64_t flag) -> bool
1575{
1576 const auto caps = gfx::get_caps();
1577 bool supported = 0 != (caps->supported & flag);
1578 return supported;
1579}
1580
1581bool check_avail_transient_buffers(uint32_t _numVertices,
1582 const vertex_layout& _layout,
1583 uint32_t _numIndices,
1584 bool _index32)
1585{
1586 return _numVertices == bgfx::getAvailTransientVertexBuffer(_numVertices, _layout) &&
1587 (0 == _numIndices || _numIndices == bgfx::getAvailTransientIndexBuffer(_numIndices, _index32));
1588}
1589
1591{
1592 return s_context.frame;
1593}
1594
1595void set_world_transform(const void* _mtx, uint16_t _num)
1596{
1597 bgfx::setUniform(s_context.u_world, _mtx, _num);
1598}
1599
1600
1601namespace
1602{
1603constexpr uint64_t k_gpu_row_pitch_alignment = 256;
1605constexpr uint64_t k_gpu_min_image_allocation = 4096;
1606
1607auto align_up_u64(uint64_t value, uint64_t alignment) -> uint64_t
1608{
1609 if(alignment == 0)
1610 {
1611 return value;
1612 }
1613 return (value + alignment - 1) & ~(alignment - 1);
1614}
1615
1617auto estimate_row_pitch_layout_size(const texture_info& info) -> uint64_t
1618{
1619 const auto format = static_cast<bimg::TextureFormat::Enum>(info.format);
1620 if(info.bitsPerPixel == 0 || bimg::isCompressed(format) || bimg::isDepth(format))
1621 {
1622 return info.storageSize;
1623 }
1624
1625 const uint64_t bytes_per_pixel = (static_cast<uint64_t>(info.bitsPerPixel) + 7) / 8;
1626 const uint32_t faces = static_cast<uint32_t>(info.numLayers) * (info.cubeMap ? 6u : 1u);
1627
1628 uint32_t width = info.width;
1629 uint32_t height = info.height;
1630 uint32_t depth = info.depth > 0 ? info.depth : 1u;
1631
1632 uint64_t total = 0;
1633 for(uint32_t mip = 0; mip < info.numMips; ++mip)
1634 {
1635 const uint32_t mip_w = bx::max<uint32_t>(1u, width);
1636 const uint32_t mip_h = bx::max<uint32_t>(1u, height);
1637 const uint32_t mip_d = bx::max<uint32_t>(1u, depth);
1638
1639 const uint64_t row_bytes = static_cast<uint64_t>(mip_w) * bytes_per_pixel;
1640 const uint64_t pitch = align_up_u64(row_bytes, k_gpu_row_pitch_alignment);
1641 total += pitch * mip_h * mip_d * faces;
1642
1643 width = bx::max<uint32_t>(1u, width >> 1);
1644 height = bx::max<uint32_t>(1u, height >> 1);
1645 depth = bx::max<uint32_t>(1u, depth >> 1);
1646 }
1647 return total;
1648}
1649
1650} // namespace
1651
1652uint64_t estimate_texture_gpu_size(const texture_info& _info, uint64_t _flags)
1653{
1654 // bgfx tracks textureMemoryUsed as the sum of storageSize per texture. For eviction bookkeeping
1655 // we start there, then uplift uncompressed layouts to row-pitch size (256-byte alignment) which
1656 // better matches committed image allocations. We do NOT apply D3D12 placement alignment (64 KiB)
1657 // — bgfx uses committed resources / dedicated Vk allocations, not suballocated heap placement.
1658 uint64_t size = std::max<uint64_t>(_info.storageSize, estimate_row_pitch_layout_size(_info));
1659
1660 // MSAA render targets allocate sample_count x the single-sample surface.
1661 const uint64_t msaa_field = (_flags & BGFX_TEXTURE_RT_MSAA_MASK) >> BGFX_TEXTURE_RT_MSAA_SHIFT;
1662 if(msaa_field >= 2)
1663 {
1664 size *= (uint64_t(1) << (msaa_field - 1));
1665 }
1666
1667 // Committed image allocations are typically rounded to at least one page (4 KiB) on device-local
1668 // heaps; cap the uplift so tiny textures are not reported as 64 KiB each.
1669 if(size > 0 && size < k_gpu_min_image_allocation)
1670 {
1671 size = k_gpu_min_image_allocation;
1672 }
1673 return size;
1674}
1675
1676
1677} // namespace gfx
uint32_t width
uint32_t height
gfx::texture_format format
uniform_handle u_world
Definition graphics.cpp:25
uint32_t frame
Definition graphics.cpp:23
texture_handle fallback_texture
Definition graphics.cpp:26
bool initted
Definition graphics.cpp:22
allocation_failure_policy allocation_policy
Definition graphics.cpp:27
gfx_profiler_begin_hook on_begin
Definition graphics.cpp:83
gfx_profiler_begin_literal_hook on_begin_literal
Definition graphics.cpp:84
gfx_profiler_end_hook on_end
Definition graphics.cpp:85
uint32_t flag
std::vector< uint8_t > pixels
Definition mcp_async.cpp:28
void clear_queued_allocations()
Definition eviction.cpp:811
auto init(const config &cfg) -> init_status
Definition eviction.cpp:645
void shutdown()
Definition eviction.cpp:655
@ failed
Initialization failed (graphics not ready).
@ ok
Initialized and active.
@ unsupported
Backend does not report a GPU memory budget; eviction is disabled.
@ unnecessary
Eviction is not needed for this backend (driver manages residency).
void set_frame(std::uint64_t frame)
Set the global frame counter (call once per presented frame).
Definition eviction.cpp:958
void alloc_transient_index_buffer(transient_index_buffer *_tib, uint32_t _num, bool _index32)
Definition graphics.cpp:582
bgfx::ReleaseFn release_fn
Definition graphics.h:38
uint32_t read_texture(texture_handle _handle, void *_data, uint16_t _layer, uint8_t _mip)
Definition graphics.cpp:759
void get_uniform_info(uniform_handle _handle, uniform_info &_info)
Definition graphics.cpp:626
bgfx::FrameBufferHandle frame_buffer_handle
Definition graphics.h:45
void dispatch(view_id _id, program_handle _handle, uint32_t _numX, uint32_t _numY, uint32_t _numZ)
bgfx::Init init_type
Definition graphics.h:19
void alloc_instance_data_buffer(instance_data_buffer *_idb, uint32_t _num, uint16_t _stride)
Definition graphics.cpp:601
void submit(view_id _id, program_handle _handle, int32_t _depth, bool _preserveState)
void set_marker(const char *_marker)
Definition graphics.cpp:932
const memory_view * alloc(uint32_t _size)
Definition graphics.cpp:455
bgfx::Stats stats
Definition graphics.h:31
void set_frame_uniform(uniform_handle _handle, const void *_value, uint16_t _num)
Definition graphics.cpp:987
uint64_t clip_quad_ex(const clip_quad_def &def)
void alloc_transient_vertex_buffer(transient_vertex_buffer *_tvb, uint32_t _num, const vertex_layout &_decl)
Definition graphics.cpp:587
void set_view_scissor(view_id _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
Definition graphics.cpp:876
void vertex_convert(const vertex_layout &_destDecl, void *_destData, const vertex_layout &_srcDecl, const void *_srcData, uint32_t _num)
Definition graphics.cpp:369
bgfx::VertexLayout vertex_layout
Definition vertex_decl.h:8
void set_view_uniform(view_id _id, uniform_handle _handle, const void *_value, uint16_t _num)
Definition graphics.cpp:982
void set_view_mode(view_id _id, view_mode _mode)
Definition graphics.cpp:902
void set_name(index_buffer_handle _handle, const char *_name, int32_t _len)
Definition graphics.cpp:500
void frames(int _count, int32_t _flags)
renderer_type get_renderer_type()
Definition graphics.cpp:440
std::function< void()> gfx_profiler_end_hook
Definition graphics.h:671
uint32_t alloc_transform(transform *_transform, uint16_t _num)
Definition graphics.cpp:967
void clear_profiler_hooks()
Definition graphics.cpp:266
bgfx::TopologyConvert::Enum topology_conversion
Definition graphics.h:35
void set_view_clear(view_id _id, uint16_t _flags, uint32_t _rgba, float _depth, uint8_t _stencil)
Definition graphics.cpp:881
texture_handle create_texture(const memory_view *_mem, uint64_t _flags, uint8_t _skip, texture_info *_info)
Definition graphics.cpp:673
uint16_t set_scissor(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
Definition graphics.cpp:952
void reset_view(view_id _id)
Definition graphics.cpp:927
bgfx::TextureFormat::Enum texture_format
Definition format.h:10
uint32_t get_avail_transient_index_buffer(uint32_t _num, bool _index32)
Definition graphics.cpp:567
void set_state(uint64_t _state, uint32_t _rgba)
Definition graphics.cpp:937
void update_texture_2d(texture_handle _handle, uint16_t _layer, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const memory_view *_mem, uint16_t _pitch)
Definition graphics.cpp:719
bgfx::BackbufferRatio::Enum backbuffer_ratio
Definition graphics.h:22
uint32_t topology_convert(topology_conversion _conversion, void *_dst, uint32_t _dstSize, const void *_indices, uint32_t _numIndices, bool _index32)
Definition graphics.cpp:378
void touch(view_id _id)
bgfx::Attrib::Enum attribute
Definition vertex_decl.h:9
bgfx::DynamicVertexBufferHandle dynamic_vertex_buffer_handle
Definition graphics.h:44
void set_view_frame_buffer(view_id _id, frame_buffer_handle _handle)
Definition graphics.cpp:907
void set_debug(uint32_t _debug)
Definition graphics.cpp:470
bool alloc_transient_buffers(transient_vertex_buffer *_tvb, const vertex_layout &_decl, uint32_t _numVertices, transient_index_buffer *_tib, uint32_t _numIndices)
Definition graphics.cpp:592
void shutdown()
Definition graphics.cpp:279
texture_handle create_texture_cube(uint16_t _size, bool _hasMips, uint16_t _numLayers, texture_format _format, uint64_t _flags, const memory_view *_mem)
Definition graphics.cpp:709
texture_handle create_texture_3d(uint16_t _width, uint16_t _height, uint16_t _depth, bool _hasMips, texture_format _format, uint64_t _flags, const memory_view *_mem)
Definition graphics.cpp:698
bgfx::Attachment attachment
Definition graphics.h:28
void set_view_rect(view_id _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
Definition graphics.cpp:866
bool init(init_type init_data)
Definition graphics.cpp:312
bgfx::ShaderHandle shader_handle
Definition graphics.h:50
void update_texture_3d(texture_handle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _z, uint16_t _width, uint16_t _height, uint16_t _depth, const memory_view *_mem)
Definition graphics.cpp:732
indirect_buffer_handle create_indirect_buffer(uint32_t _num)
Definition graphics.cpp:606
void set_view_name(view_id _id, const char *_name)
Definition graphics.cpp:861
const memory_view * make_ref(const void *_data, uint32_t _size, release_fn _releaseFn, void *_userData)
Definition graphics.cpp:465
uniform_handle create_uniform(const char *_name, uniform_type _type, uint16_t _num)
Definition graphics.cpp:821
bgfx::Access::Enum access
Definition graphics.h:32
bgfx::TransientVertexBuffer transient_vertex_buffer
Definition graphics.h:56
void set_view_order(view_id _id, uint16_t _num, const view_id *_order)
Definition graphics.cpp:917
void set_stencil(uint32_t _fstencil, uint32_t _bstencil)
Definition graphics.cpp:947
bool is_texture_valid(uint16_t _depth, bool _cubeMap, uint16_t _numLayers, texture_format _format, uint64_t _flags)
Definition graphics.cpp:656
void topology_sort_tri_list(topology_sort _sort, void *_dst, uint32_t _dstSize, const float _dir[], const float _pos[], const void *_vertices, uint32_t _stride, const void *_indices, uint32_t _numIndices, bool _index32)
Definition graphics.cpp:388
void set_view_shading_rate(view_id _id, shading_rate _shadingRate)
Definition graphics.cpp:922
void set_info_logger(const std::function< void(const std::string &, const char *, uint16_t)> &logger)
Definition graphics.cpp:58
shader_handle create_embedded_shader(const embedded_shader *_es, renderer_type _type, const char *_name)
Definition graphics.cpp:402
auto is_supported(uint64_t flag) -> bool
void vertex_pack(const float _input[4], bool _inputNormalized, attribute _attr, const vertex_layout &_decl, void *_data, uint32_t _index)
Definition graphics.cpp:354
uint8_t get_supported_renderers(uint8_t _max, renderer_type *_enum)
Definition graphics.cpp:407
encoder * begin()
Definition graphics.cpp:422
bgfx::UniformHandle uniform_handle
Definition graphics.h:52
shader_handle create_shader(const memory_view *_mem)
Definition graphics.cpp:616
void set_world_transform(const void *_mtx, uint16_t _num)
bgfx::TransientIndexBuffer transient_index_buffer
Definition graphics.h:57
void set_debug_logger(const std::function< void(const std::string &, const char *, uint16_t)> &logger)
Definition graphics.cpp:53
uint32_t set_transform(const void *_mtx, uint16_t _num)
Definition graphics.cpp:962
auto clip_fullscreen_triangle(float depth, float width, float height) -> uint64_t
const stats * get_stats()
Definition graphics.cpp:450
program_handle create_program(shader_handle _vsh, shader_handle _fsh, bool _destroyShaders)
Definition graphics.cpp:641
bgfx::TopologySort::Enum topology_sort
Definition graphics.h:36
bgfx::UniformInfo uniform_info
Definition graphics.h:27
void set_buffer(uint8_t _stage, index_buffer_handle _handle, access _access)
void get_size_from_ratio(backbuffer_ratio _ratio, uint16_t &_width, uint16_t &_height)
void log(const std::string &category, const std::string &log_msg, const char *_filePath, uint16_t _line)
Definition graphics.cpp:71
bgfx::TextureHandle texture_handle
Definition graphics.h:51
bgfx::InstanceDataBuffer instance_data_buffer
Definition graphics.h:58
void dbg_text_vprintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char *_format, va_list _argList)
Definition graphics.cpp:485
void dbg_text_image(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const void *_data, uint16_t _pitch)
Definition graphics.cpp:490
auto screen_quad(float dest_width, float dest_height, float depth, float width, float height) -> uint64_t
auto get_allocation_failure_policy() -> allocation_failure_policy
Definition graphics.cpp:302
occlusion_query_handle create_occlusion_query()
Definition graphics.cpp:831
void set_instance_data_buffer(const instance_data_buffer *_idb, uint32_t _start, uint32_t _num)
void set_view_transform(view_id _id, const void *_view, const void *_proj)
Definition graphics.cpp:912
void set_vertex_buffer(uint8_t _stream, vertex_buffer_handle _handle)
bgfx::OcclusionQueryResult::Enum occlusion_query_result
Definition graphics.h:34
const caps * get_caps()
Definition graphics.cpp:445
uint32_t get_avail_instance_data_buffer(uint32_t _num, uint16_t _stride)
Definition graphics.cpp:577
const char * get_renderer_name(renderer_type _type)
Definition graphics.cpp:412
static const uint16_t invalid_handle
Definition graphics.h:18
uint16_t get_shader_uniforms(shader_handle _handle, uniform_handle *_uniforms, uint16_t _max)
Definition graphics.cpp:621
bgfx::UniformType::Enum uniform_type
Definition graphics.h:25
void blit(view_id _id, texture_handle _dst, uint16_t _dstX, uint16_t _dstY, texture_handle _src, uint16_t _srcX, uint16_t _srcY, uint16_t _width, uint16_t _height)
void reset(uint32_t _width, uint32_t _height, uint32_t _flags)
Definition graphics.cpp:417
void calc_texture_size(texture_info &_info, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, bool _hasMips, uint16_t _numLayers, texture_format _format)
Definition graphics.cpp:661
auto get_half_texel() -> float
auto get_renderer_platform_supported_filename_extensions() -> const std::set< std::string > &
void request_screen_shot(frame_buffer_handle _handle, const char *_filePath)
bgfx::ViewId view_id
Definition graphics.h:20
bgfx::OcclusionQueryHandle occlusion_query_handle
Definition graphics.h:48
std::function< void(const char *name, uint32_t abgr, const char *file_path, uint16_t line)> gfx_profiler_begin_hook
Definition graphics.h:668
auto get_renderer_based_on_filename_extension(const std::string &_type) -> renderer_type
texture_handle create_texture_2d(uint16_t _width, uint16_t _height, bool _hasMips, uint16_t _numLayers, texture_format _format, uint64_t _flags, const memory_view *_mem)
Definition graphics.cpp:678
texture_handle get_texture(frame_buffer_handle _handle, uint8_t _attachment)
Definition graphics.cpp:806
auto get_renderer_filename_extension(renderer_type _type) -> const std::string &
void discard(uint8_t _flags)
void dbg_text_printf(uint16_t _x, uint16_t _y, uint8_t _attr, const char *_format)
Definition graphics.cpp:480
void set_warning_logger(const std::function< void(const std::string &, const char *, uint16_t)> &logger)
Definition graphics.cpp:62
void destroy(index_buffer_handle _handle)
Definition graphics.cpp:505
auto fallback_texture() -> texture_handle
Definition graphics.cpp:307
void set_trace_logger(const std::function< void(const std::string &, const char *, uint16_t)> &logger)
Definition graphics.cpp:48
bgfx::IndirectBufferHandle indirect_buffer_handle
Definition graphics.h:47
dynamic_vertex_buffer_handle create_dynamic_vertex_buffer(uint32_t _num, const vertex_layout &_decl, uint16_t _flags)
Definition graphics.cpp:545
const memory_view * copy(const void *_data, uint32_t _size)
Definition graphics.cpp:460
void set_profiler_hooks(gfx_profiler_begin_hook on_begin, gfx_profiler_begin_literal_hook on_begin_literal, gfx_profiler_end_hook on_end)
Definition graphics.cpp:256
auto get_max_blend_transforms() -> uint32_t
void update_texture_cube(texture_handle _handle, uint16_t _layer, uint8_t _side, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const memory_view *_mem, uint16_t _pitch)
Definition graphics.cpp:745
bgfx::RendererType::Enum renderer_type
Definition graphics.h:21
bgfx::EmbeddedShader embedded_shader
Definition graphics.h:55
void set_palette_color(uint8_t _index, uint32_t _rgba)
Definition graphics.cpp:846
void set_image(uint8_t _stage, texture_handle _handle, uint8_t _mip, access _access, texture_format _format)
void set_platform_data(const platform_data &_data)
Definition graphics.cpp:274
bgfx::DynamicIndexBufferHandle dynamic_index_buffer_handle
Definition graphics.h:43
vertex_buffer_handle create_vertex_buffer(const memory_view *_mem, const vertex_layout &_decl, uint16_t _flags)
Definition graphics.cpp:510
void deinit_uniform_cache()
Definition uniform.cpp:60
auto get_current_renderer_filename_extension() -> const std::string &
gfx_profiler_begin_hook gfx_profiler_begin_literal_hook
Definition graphics.h:670
bgfx::Caps caps
Definition graphics.h:30
bgfx::ShadingRate::Enum shading_rate
Definition graphics.h:40
void set_uniform(uniform_handle _handle, const void *_value, uint16_t _num)
Definition graphics.cpp:977
bgfx::IndexBufferHandle index_buffer_handle
Definition graphics.h:46
bgfx::ProgramHandle program_handle
Definition graphics.h:49
void update(dynamic_index_buffer_handle _handle, uint32_t _startIndex, const memory_view *_mem)
Definition graphics.cpp:535
void set_allocation_failure_policy(allocation_failure_policy policy)
Definition graphics.cpp:297
bgfx::TextureInfo texture_info
Definition graphics.h:24
void dbg_text_clear(uint8_t _attr, bool _small)
Definition graphics.cpp:475
bgfx::VertexBufferHandle vertex_buffer_handle
Definition graphics.h:53
void set_vertex_count(uint32_t _numVertices)
dynamic_index_buffer_handle create_dynamic_index_buffer(uint32_t _num, uint16_t _flags)
Definition graphics.cpp:525
auto is_homogeneous_depth() -> bool
void set_error_logger(const std::function< void(const std::string &, const char *, uint16_t)> &logger)
Definition graphics.cpp:66
bool check_avail_transient_buffers(uint32_t _numVertices, const vertex_layout &_layout, uint32_t _numIndices, bool _index32)
bgfx::ViewMode::Enum view_mode
Definition graphics.h:33
void set_index_buffer(index_buffer_handle _handle)
Definition graphics.cpp:992
bgfx::Transform transform
Definition graphics.h:42
auto is_origin_bottom_left() -> bool
uint64_t estimate_texture_gpu_size(const texture_info &_info, uint64_t _flags)
index_buffer_handle create_index_buffer(const memory_view *_mem, uint16_t _flags)
Definition graphics.cpp:495
bgfx::PlatformData platform_data
Definition graphics.h:29
frame_buffer_handle create_frame_buffer(uint16_t _width, uint16_t _height, texture_format _format, uint64_t _textureFlags)
Definition graphics.cpp:774
uint32_t get_avail_transient_vertex_buffer(uint32_t _num, const vertex_layout &_decl)
Definition graphics.cpp:572
bgfx::Memory memory_view
Definition graphics.h:23
occlusion_query_result get_result(occlusion_query_handle _handle, int32_t *_result)
Definition graphics.cpp:836
allocation_failure_policy
Definition graphics.h:71
@ skip_with_fallback
Skip the allocation; texture::native_handle binds the fallback texture.
uint32_t get_render_frame()
auto clip_quad(float depth, float width, float height) -> uint64_t
bgfx::Encoder encoder
Definition graphics.h:39
void set_texture(uint8_t _stage, uniform_handle _sampler, texture_handle _handle, uint32_t _flags)
void set_condition(occlusion_query_handle _handle, bool _visible)
Definition graphics.cpp:942
void vertex_unpack(float _output[4], attribute _attr, const vertex_layout &_decl, const void *_data, uint32_t _index)
Definition graphics.cpp:364
void end(encoder *_encoder)
Definition graphics.cpp:427
virtual void screenShot(const char *_filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, bgfx::TextureFormat::Enum _format, const void *_data, uint32_t _size, bool _yflip) override
Definition graphics.cpp:204
void profilerBegin(const char *_name, uint32_t _abgr, const char *_filePath, uint16_t _line) final
Definition graphics.cpp:124
void profilerEnd() final
Definition graphics.cpp:140
void traceVargs(const char *_filePath, uint16_t _line, const char *_format, va_list _argList) final
Definition graphics.cpp:100
bool cacheRead(uint64_t, void *, uint32_t) final
Definition graphics.cpp:195
void fatal(const char *_filePath, uint16_t _line, bgfx::Fatal::Enum _code, const char *_str) final
Definition graphics.cpp:147
void profilerBeginLiteral(const char *_name, uint32_t _abgr, const char *_filePath, uint16_t _line) final
Definition graphics.cpp:132
void captureEnd() final
Definition graphics.cpp:247
void captureBegin(uint32_t, uint32_t, uint32_t, texture_format, bool) final
Definition graphics.cpp:239
uint32_t cacheReadSize(uint64_t) final
Definition graphics.cpp:190
void captureFrame(const void *, uint32_t) final
Definition graphics.cpp:251
~gfx_callback()=default
void cacheWrite(uint64_t, const void *, uint32_t) final
Definition graphics.cpp:200
static auto get_layout() -> const vertex_layout &
Definition vertex_decl.h:15
float size