2014-05-04 02:18:28 +04:00
|
|
|
/*
|
2020-01-15 08:37:06 +03:00
|
|
|
* Copyright 2011-2020 Branimir Karadzic. All rights reserved.
|
2016-01-01 11:11:04 +03:00
|
|
|
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
|
2014-05-04 02:18:28 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BGFX_UTILS_H_HEADER_GUARD
|
|
|
|
#define BGFX_UTILS_H_HEADER_GUARD
|
|
|
|
|
2017-04-01 07:01:08 +03:00
|
|
|
#include <bx/pixelformat.h>
|
2015-09-19 06:16:24 +03:00
|
|
|
#include <bgfx/bgfx.h>
|
2017-04-04 08:42:27 +03:00
|
|
|
#include <bimg/bimg.h>
|
2019-04-23 19:20:10 +03:00
|
|
|
#include "bounds.h"
|
|
|
|
|
|
|
|
#include <tinystl/allocator.h>
|
|
|
|
#include <tinystl/vector.h>
|
|
|
|
namespace stl = tinystl;
|
|
|
|
|
2014-05-04 02:18:28 +04:00
|
|
|
|
2017-03-10 08:20:45 +03:00
|
|
|
///
|
2015-01-23 08:01:09 +03:00
|
|
|
void* load(const char* _filePath, uint32_t* _size = NULL);
|
2017-03-10 08:20:45 +03:00
|
|
|
|
|
|
|
///
|
2015-06-02 03:45:40 +03:00
|
|
|
void unload(void* _ptr);
|
2017-03-10 08:20:45 +03:00
|
|
|
|
|
|
|
///
|
2014-05-11 07:51:44 +04:00
|
|
|
bgfx::ShaderHandle loadShader(const char* _name);
|
2017-03-10 08:20:45 +03:00
|
|
|
|
|
|
|
///
|
2014-05-04 02:18:28 +04:00
|
|
|
bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName);
|
2017-03-10 08:20:45 +03:00
|
|
|
|
|
|
|
///
|
2019-03-24 08:49:42 +03:00
|
|
|
bgfx::TextureHandle loadTexture(const char* _name, uint64_t _flags = BGFX_TEXTURE_NONE|BGFX_SAMPLER_NONE, uint8_t _skip = 0, bgfx::TextureInfo* _info = NULL, bimg::Orientation::Enum* _orientation = NULL);
|
2017-03-10 08:20:45 +03:00
|
|
|
|
|
|
|
///
|
2017-04-04 08:42:27 +03:00
|
|
|
bimg::ImageContainer* imageLoad(const char* _filePath, bgfx::TextureFormat::Enum _dstFormat);
|
2017-03-10 08:20:45 +03:00
|
|
|
|
|
|
|
///
|
2019-08-17 20:35:21 +03:00
|
|
|
void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexLayout _layout, const uint16_t* _indices, uint32_t _numIndices);
|
2014-05-04 02:18:28 +04:00
|
|
|
|
2016-12-23 03:18:44 +03:00
|
|
|
/// Returns true if both internal transient index and vertex buffer have
|
|
|
|
/// enough space.
|
|
|
|
///
|
|
|
|
/// @param[in] _numVertices Number of vertices.
|
2019-08-17 20:35:21 +03:00
|
|
|
/// @param[in] _layout Vertex layout.
|
2016-12-23 03:18:44 +03:00
|
|
|
/// @param[in] _numIndices Number of indices.
|
|
|
|
///
|
2019-08-17 20:35:21 +03:00
|
|
|
inline bool checkAvailTransientBuffers(uint32_t _numVertices, const bgfx::VertexLayout& _layout, uint32_t _numIndices)
|
2016-12-23 03:18:44 +03:00
|
|
|
{
|
2019-08-17 20:35:21 +03:00
|
|
|
return _numVertices == bgfx::getAvailTransientVertexBuffer(_numVertices, _layout)
|
2018-01-09 04:54:15 +03:00
|
|
|
&& (0 == _numIndices || _numIndices == bgfx::getAvailTransientIndexBuffer(_numIndices) )
|
2016-12-23 03:18:44 +03:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:01:08 +03:00
|
|
|
///
|
|
|
|
inline uint32_t encodeNormalRgba8(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
|
|
|
|
{
|
|
|
|
const float src[] =
|
|
|
|
{
|
|
|
|
_x * 0.5f + 0.5f,
|
|
|
|
_y * 0.5f + 0.5f,
|
|
|
|
_z * 0.5f + 0.5f,
|
|
|
|
_w * 0.5f + 0.5f,
|
|
|
|
};
|
|
|
|
uint32_t dst;
|
|
|
|
bx::packRgba8(&dst, src);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2017-03-10 08:20:45 +03:00
|
|
|
///
|
2015-01-08 09:36:36 +03:00
|
|
|
struct MeshState
|
|
|
|
{
|
|
|
|
struct Texture
|
|
|
|
{
|
|
|
|
uint32_t m_flags;
|
|
|
|
bgfx::UniformHandle m_sampler;
|
|
|
|
bgfx::TextureHandle m_texture;
|
|
|
|
uint8_t m_stage;
|
|
|
|
};
|
|
|
|
|
|
|
|
Texture m_textures[4];
|
|
|
|
uint64_t m_state;
|
|
|
|
bgfx::ProgramHandle m_program;
|
|
|
|
uint8_t m_numTextures;
|
2017-11-24 22:21:51 +03:00
|
|
|
bgfx::ViewId m_viewId;
|
2015-01-08 09:36:36 +03:00
|
|
|
};
|
|
|
|
|
2019-04-23 19:20:10 +03:00
|
|
|
struct Primitive
|
|
|
|
{
|
|
|
|
uint32_t m_startIndex;
|
|
|
|
uint32_t m_numIndices;
|
|
|
|
uint32_t m_startVertex;
|
|
|
|
uint32_t m_numVertices;
|
|
|
|
|
|
|
|
Sphere m_sphere;
|
|
|
|
Aabb m_aabb;
|
|
|
|
Obb m_obb;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef stl::vector<Primitive> PrimitiveArray;
|
|
|
|
|
|
|
|
struct Group
|
|
|
|
{
|
|
|
|
Group();
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
bgfx::VertexBufferHandle m_vbh;
|
|
|
|
bgfx::IndexBufferHandle m_ibh;
|
|
|
|
uint16_t m_numVertices;
|
|
|
|
uint8_t* m_vertices;
|
|
|
|
uint32_t m_numIndices;
|
|
|
|
uint16_t* m_indices;
|
|
|
|
Sphere m_sphere;
|
|
|
|
Aabb m_aabb;
|
|
|
|
Obb m_obb;
|
|
|
|
PrimitiveArray m_prims;
|
|
|
|
};
|
|
|
|
typedef stl::vector<Group> GroupArray;
|
|
|
|
|
|
|
|
struct Mesh
|
|
|
|
{
|
|
|
|
void load(bx::ReaderSeekerI* _reader, bool _ramcopy);
|
|
|
|
void unload();
|
|
|
|
void submit(bgfx::ViewId _id, bgfx::ProgramHandle _program, const float* _mtx, uint64_t _state) const;
|
|
|
|
void submit(const MeshState*const* _state, uint8_t _numPasses, const float* _mtx, uint16_t _numMatrices) const;
|
|
|
|
|
2019-08-17 20:35:21 +03:00
|
|
|
bgfx::VertexLayout m_layout;
|
2019-04-23 19:20:10 +03:00
|
|
|
GroupArray m_groups;
|
|
|
|
};
|
2015-01-08 09:36:36 +03:00
|
|
|
|
2017-03-10 08:20:45 +03:00
|
|
|
///
|
2019-04-23 19:20:10 +03:00
|
|
|
Mesh* meshLoad(const char* _filePath, bool _ramcopy = false);
|
2017-03-10 08:20:45 +03:00
|
|
|
|
|
|
|
///
|
2014-05-04 02:18:28 +04:00
|
|
|
void meshUnload(Mesh* _mesh);
|
2015-01-08 09:36:36 +03:00
|
|
|
|
2017-03-10 08:20:45 +03:00
|
|
|
///
|
2015-01-08 09:36:36 +03:00
|
|
|
MeshState* meshStateCreate();
|
2017-03-10 08:20:45 +03:00
|
|
|
|
|
|
|
///
|
2015-01-08 09:36:36 +03:00
|
|
|
void meshStateDestroy(MeshState* _meshState);
|
|
|
|
|
2017-03-10 08:20:45 +03:00
|
|
|
///
|
2017-11-24 22:21:51 +03:00
|
|
|
void meshSubmit(const Mesh* _mesh, bgfx::ViewId _id, bgfx::ProgramHandle _program, const float* _mtx, uint64_t _state = BGFX_STATE_MASK);
|
2017-03-10 08:20:45 +03:00
|
|
|
|
|
|
|
///
|
2015-01-08 09:36:36 +03:00
|
|
|
void meshSubmit(const Mesh* _mesh, const MeshState*const* _state, uint8_t _numPasses, const float* _mtx, uint16_t _numMatrices = 1);
|
2014-05-04 02:18:28 +04:00
|
|
|
|
2017-03-10 08:20:45 +03:00
|
|
|
///
|
2015-10-24 06:57:04 +03:00
|
|
|
struct Args
|
|
|
|
{
|
2017-06-30 08:23:18 +03:00
|
|
|
Args(int _argc, const char* const* _argv);
|
2015-10-24 06:57:04 +03:00
|
|
|
|
|
|
|
bgfx::RendererType::Enum m_type;
|
|
|
|
uint16_t m_pciId;
|
|
|
|
};
|
|
|
|
|
2014-05-04 02:18:28 +04:00
|
|
|
#endif // BGFX_UTILS_H_HEADER_GUARD
|