Unravel Engine C++ Reference
Loading...
Searching...
No Matches
bgfx_utils.h
Go to the documentation of this file.
1/*
2 * Copyright 2011-2023 Branimir Karadzic. All rights reserved.
3 * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
4 */
5
6#ifndef BGFX_UTILS_H_HEADER_GUARD
7#define BGFX_UTILS_H_HEADER_GUARD
8
9#include <bgfx/bgfx.h>
10#include <bimg/bimg.h>
11#include <bimg/encode.h>
12#include <bimg/decode.h>
13
14#include <bx/bounds.h>
15#include <bx/pixelformat.h>
16#include <bx/string.h>
17#include <bx/filepath.h>
18
19#include <tinystl/allocator.h>
20#include <tinystl/vector.h>
21
22#include <functional>
23
24namespace stl = tinystl;
25
28using TexturePreCreateFn = std::function<bool(const bgfx::TextureInfo& _info)>;
29
30bool saveToFile(bgfx::ViewId viewId, const bx::FilePath& _filePath, bgfx::FrameBufferHandle fbo, uint32_t width, uint32_t height);
32void* load(const bx::FilePath& _filePath, uint32_t* _size = NULL);
33
35void unload(void* _ptr);
36
38bgfx::ShaderHandle loadShader(const bx::StringView& _name);
39bgfx::ShaderHandle loadShader(const bx::FilePath& _filePath);
41bgfx::ProgramHandle loadProgram(const bx::StringView& _vsName, const bx::StringView& _fsName);
42
44bgfx::TextureHandle loadTexture(const bx::FilePath& _filePath,
45 uint64_t _flags = BGFX_TEXTURE_NONE | BGFX_SAMPLER_NONE,
46 uint8_t _skip = 0,
47 bgfx::TextureInfo* _info = NULL,
48 bimg::Orientation::Enum* _orientation = NULL,
49 bx::Error* _err = NULL,
50 const TexturePreCreateFn& _preCreate = nullptr);
51
55bgfx::TextureHandle loadTexture(const void* _data,
56 uint32_t _size,
57 uint64_t _flags = BGFX_TEXTURE_NONE | BGFX_SAMPLER_NONE,
58 uint8_t _skip = 0,
59 bgfx::TextureInfo* _info = NULL,
60 bimg::Orientation::Enum* _orientation = NULL,
61 const char* _name = NULL,
62 bx::Error* _err = NULL,
63 const TexturePreCreateFn& _preCreate = nullptr);
64
66bimg::ImageContainer* imageLoad(const void* data, uint32_t size, bgfx::TextureFormat::Enum _dstFormat = bgfx::TextureFormat::Count);
67bimg::ImageContainer* imageLoad(const bx::FilePath& _filePath, bgfx::TextureFormat::Enum _dstFormat = bgfx::TextureFormat::Count);
68
70bool imageParseInfo(const bx::FilePath& _filePath, bimg::ImageContainer& _info, bx::Error* _err = NULL);
71bool imageParseInfo(const void* _data, uint32_t _size, bimg::ImageContainer& _info, bx::Error* _err = NULL);
72
73bool imageSave(const char* saveAs, bimg::ImageContainer* image);
74
78bool imageFlipTangentSpaceNormalY(bimg::ImageContainer*& _image);
79
81bool imagePrepareNormalMapBakePng(bimg::ImageContainer*& _image);
82
84void calcTangents(void* _vertices,
85 uint16_t _numVertices,
86 bgfx::VertexLayout _layout,
87 const uint16_t* _indices,
88 uint32_t _numIndices);
89
97inline bool checkAvailTransientBuffers(uint32_t _numVertices, const bgfx::VertexLayout& _layout, uint32_t _numIndices)
98{
99 return _numVertices == bgfx::getAvailTransientVertexBuffer(_numVertices, _layout) &&
100 (0 == _numIndices || _numIndices == bgfx::getAvailTransientIndexBuffer(_numIndices));
101}
102
104inline uint32_t encodeNormalRgba8(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
105{
106 const float src[] = {
107 _x * 0.5f + 0.5f,
108 _y * 0.5f + 0.5f,
109 _z * 0.5f + 0.5f,
110 _w * 0.5f + 0.5f,
111 };
112 uint32_t dst;
113 bx::packRgba8(&dst, src);
114 return dst;
115}
116
117#endif // BGFX_UTILS_H_HEADER_GUARD
uint32_t width
uint32_t height
uint32_t encodeNormalRgba8(float _x, float _y=0.0f, float _z=0.0f, float _w=0.0f)
Definition bgfx_utils.h:104
bgfx::ShaderHandle loadShader(const bx::StringView &_name)
bimg::ImageContainer * imageLoad(const void *data, uint32_t size, bgfx::TextureFormat::Enum _dstFormat=bgfx::TextureFormat::Count)
void calcTangents(void *_vertices, uint16_t _numVertices, bgfx::VertexLayout _layout, const uint16_t *_indices, uint32_t _numIndices)
std::function< bool(const bgfx::TextureInfo &_info)> TexturePreCreateFn
Definition bgfx_utils.h:28
bgfx::ProgramHandle loadProgram(const bx::StringView &_vsName, const bx::StringView &_fsName)
bool imageParseInfo(const bx::FilePath &_filePath, bimg::ImageContainer &_info, bx::Error *_err=NULL)
Parse image metadata without decoding pixel data.
bool imageSave(const char *saveAs, bimg::ImageContainer *image)
bool imageFlipTangentSpaceNormalY(bimg::ImageContainer *&_image)
void * load(const bx::FilePath &_filePath, uint32_t *_size=NULL)
bool imagePrepareNormalMapBakePng(bimg::ImageContainer *&_image)
Prepare a flipped normal map for PNG bake export (RGBA8, opaque alpha when unused).
bgfx::TextureHandle loadTexture(const bx::FilePath &_filePath, uint64_t _flags=BGFX_TEXTURE_NONE|BGFX_SAMPLER_NONE, uint8_t _skip=0, bgfx::TextureInfo *_info=NULL, bimg::Orientation::Enum *_orientation=NULL, bx::Error *_err=NULL, const TexturePreCreateFn &_preCreate=nullptr)
void unload(void *_ptr)
bool saveToFile(bgfx::ViewId viewId, const bx::FilePath &_filePath, bgfx::FrameBufferHandle fbo, uint32_t width, uint32_t height)
bool checkAvailTransientBuffers(uint32_t _numVertices, const bgfx::VertexLayout &_layout, uint32_t _numIndices)
Definition bgfx_utils.h:97