bgfx/examples/common/bgfx_utils.cpp

807 lines
20 KiB
C++
Raw Normal View History

2014-05-04 02:18:28 +04:00
/*
2023-01-14 21:05:12 +03:00
* Copyright 2011-2023 Branimir Karadzic. All rights reserved.
2022-01-15 22:59:06 +03:00
* License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
2014-05-04 02:18:28 +04:00
*/
#include "common.h"
2014-10-17 20:45:45 +04:00
#include <tinystl/allocator.h>
#include <tinystl/vector.h>
#include <tinystl/string.h>
namespace stl = tinystl;
2014-05-04 02:18:28 +04:00
#include <bgfx/bgfx.h>
2015-10-24 06:57:04 +03:00
#include <bx/commandline.h>
2016-06-01 03:45:58 +03:00
#include <bx/endian.h>
2017-07-16 07:01:08 +03:00
#include <bx/math.h>
2015-10-24 06:57:04 +03:00
#include <bx/readerwriter.h>
#include <bx/string.h>
2014-05-04 02:18:28 +04:00
#include "entry/entry.h"
#include <meshoptimizer/src/meshoptimizer.h>
2014-05-04 02:18:28 +04:00
2015-01-08 09:36:36 +03:00
#include "bgfx_utils.h"
2017-04-04 08:42:27 +03:00
#include <bimg/decode.h>
2015-11-07 09:03:06 +03:00
void* load(bx::FileReaderI* _reader, bx::AllocatorI* _allocator, const char* _filePath, uint32_t* _size)
2014-05-04 02:18:28 +04:00
{
2016-02-01 03:00:02 +03:00
if (bx::open(_reader, _filePath) )
2014-05-04 02:18:28 +04:00
{
uint32_t size = (uint32_t)bx::getSize(_reader);
2015-06-02 03:45:40 +03:00
void* data = BX_ALLOC(_allocator, size);
bx::read(_reader, data, size, bx::ErrorAssert{});
2014-05-04 02:18:28 +04:00
bx::close(_reader);
2015-01-23 08:01:09 +03:00
if (NULL != _size)
{
*_size = size;
}
2014-05-04 02:18:28 +04:00
return data;
}
2015-12-09 07:34:31 +03:00
else
{
DBG("Failed to open: %s.", _filePath);
}
2014-05-04 02:18:28 +04:00
2015-01-23 08:01:09 +03:00
if (NULL != _size)
{
*_size = 0;
}
2016-02-01 03:00:02 +03:00
2014-05-04 02:18:28 +04:00
return NULL;
}
2015-01-23 08:01:09 +03:00
void* load(const char* _filePath, uint32_t* _size)
2014-05-04 02:18:28 +04:00
{
2015-06-02 03:45:40 +03:00
return load(entry::getFileReader(), entry::getAllocator(), _filePath, _size);
}
void unload(void* _ptr)
{
BX_FREE(entry::getAllocator(), _ptr);
2014-05-04 02:18:28 +04:00
}
static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const char* _filePath)
{
2016-02-01 03:00:02 +03:00
if (bx::open(_reader, _filePath) )
2014-05-04 02:18:28 +04:00
{
uint32_t size = (uint32_t)bx::getSize(_reader);
const bgfx::Memory* mem = bgfx::alloc(size+1);
bx::read(_reader, mem->data, size, bx::ErrorAssert{});
2014-05-04 02:18:28 +04:00
bx::close(_reader);
mem->data[mem->size-1] = '\0';
return mem;
}
2015-12-09 07:34:31 +03:00
DBG("Failed to load %s.", _filePath);
2014-05-04 02:18:28 +04:00
return NULL;
}
2015-11-07 09:03:06 +03:00
static void* loadMem(bx::FileReaderI* _reader, bx::AllocatorI* _allocator, const char* _filePath, uint32_t* _size)
{
2016-02-01 03:00:02 +03:00
if (bx::open(_reader, _filePath) )
{
uint32_t size = (uint32_t)bx::getSize(_reader);
void* data = BX_ALLOC(_allocator, size);
bx::read(_reader, data, size, bx::ErrorAssert{});
bx::close(_reader);
if (NULL != _size)
{
*_size = size;
}
return data;
}
2015-12-09 07:34:31 +03:00
DBG("Failed to load %s.", _filePath);
return NULL;
}
static bgfx::ShaderHandle loadShader(bx::FileReaderI* _reader, const char* _name)
2014-05-04 02:18:28 +04:00
{
char filePath[512];
2016-10-01 10:08:37 +03:00
const char* shaderPath = "???";
2014-05-04 02:18:28 +04:00
switch (bgfx::getRendererType() )
{
2016-10-01 10:08:37 +03:00
case bgfx::RendererType::Noop:
case bgfx::RendererType::Direct3D9: shaderPath = "shaders/dx9/"; break;
2014-05-04 02:18:28 +04:00
case bgfx::RendererType::Direct3D11:
2016-10-01 10:08:37 +03:00
case bgfx::RendererType::Direct3D12: shaderPath = "shaders/dx11/"; break;
2021-08-25 05:35:09 +03:00
case bgfx::RendererType::Agc:
2016-10-01 10:08:37 +03:00
case bgfx::RendererType::Gnm: shaderPath = "shaders/pssl/"; break;
case bgfx::RendererType::Metal: shaderPath = "shaders/metal/"; break;
2019-03-13 03:01:02 +03:00
case bgfx::RendererType::Nvn: shaderPath = "shaders/nvn/"; break;
2016-10-01 10:08:37 +03:00
case bgfx::RendererType::OpenGL: shaderPath = "shaders/glsl/"; break;
case bgfx::RendererType::OpenGLES: shaderPath = "shaders/essl/"; break;
case bgfx::RendererType::Vulkan: shaderPath = "shaders/spirv/"; break;
case bgfx::RendererType::WebGPU: shaderPath = "shaders/spirv/"; break;
2016-10-01 10:08:37 +03:00
case bgfx::RendererType::Count:
2020-06-16 20:06:18 +03:00
BX_ASSERT(false, "You should not be here!");
2014-05-04 02:18:28 +04:00
break;
}
2017-04-17 07:56:17 +03:00
bx::strCopy(filePath, BX_COUNTOF(filePath), shaderPath);
bx::strCat(filePath, BX_COUNTOF(filePath), _name);
bx::strCat(filePath, BX_COUNTOF(filePath), ".bin");
2014-05-04 02:18:28 +04:00
bgfx::ShaderHandle handle = bgfx::createShader(loadMem(_reader, filePath) );
bgfx::setName(handle, _name);
return handle;
2014-05-04 02:18:28 +04:00
}
bgfx::ShaderHandle loadShader(const char* _name)
2014-05-04 02:18:28 +04:00
{
return loadShader(entry::getFileReader(), _name);
}
2014-05-04 02:18:28 +04:00
bgfx::ProgramHandle loadProgram(bx::FileReaderI* _reader, const char* _vsName, const char* _fsName)
{
bgfx::ShaderHandle vsh = loadShader(_reader, _vsName);
bgfx::ShaderHandle fsh = BGFX_INVALID_HANDLE;
if (NULL != _fsName)
{
fsh = loadShader(_reader, _fsName);
}
2014-05-04 02:18:28 +04:00
return bgfx::createProgram(vsh, fsh, true /* destroy shaders when program is destroyed */);
}
bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName)
{
return loadProgram(entry::getFileReader(), _vsName, _fsName);
}
static void imageReleaseCb(void* _ptr, void* _userData)
{
BX_UNUSED(_ptr);
2017-04-04 08:42:27 +03:00
bimg::ImageContainer* imageContainer = (bimg::ImageContainer*)_userData;
bimg::imageFree(imageContainer);
}
bgfx::TextureHandle loadTexture(bx::FileReaderI* _reader, const char* _filePath, uint64_t _flags, uint8_t _skip, bgfx::TextureInfo* _info, bimg::Orientation::Enum* _orientation)
2014-05-04 02:18:28 +04:00
{
BX_UNUSED(_skip);
2015-05-24 20:25:47 +03:00
bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
uint32_t size;
void* data = load(_reader, entry::getAllocator(), _filePath, &size);
2015-05-24 20:25:47 +03:00
if (NULL != data)
{
2017-04-04 08:42:27 +03:00
bimg::ImageContainer* imageContainer = bimg::imageParse(entry::getAllocator(), data, size);
if (NULL != imageContainer)
2016-06-01 03:45:58 +03:00
{
2017-07-20 08:53:48 +03:00
if (NULL != _orientation)
{
*_orientation = imageContainer->m_orientation;
}
const bgfx::Memory* mem = bgfx::makeRef(
imageContainer->m_data
, imageContainer->m_size
, imageReleaseCb
, imageContainer
);
unload(data);
if (imageContainer->m_cubeMap)
{
handle = bgfx::createTextureCube(
uint16_t(imageContainer->m_width)
, 1 < imageContainer->m_numMips
, imageContainer->m_numLayers
2017-04-04 08:42:27 +03:00
, bgfx::TextureFormat::Enum(imageContainer->m_format)
, _flags
, mem
);
}
2017-06-20 23:31:22 +03:00
else if (1 < imageContainer->m_depth)
{
handle = bgfx::createTexture3D(
uint16_t(imageContainer->m_width)
, uint16_t(imageContainer->m_height)
, uint16_t(imageContainer->m_depth)
, 1 < imageContainer->m_numMips
, bgfx::TextureFormat::Enum(imageContainer->m_format)
, _flags
, mem
);
}
2018-06-12 00:43:17 +03:00
else if (bgfx::isTextureValid(0, false, imageContainer->m_numLayers, bgfx::TextureFormat::Enum(imageContainer->m_format), _flags) )
{
handle = bgfx::createTexture2D(
uint16_t(imageContainer->m_width)
, uint16_t(imageContainer->m_height)
, 1 < imageContainer->m_numMips
, imageContainer->m_numLayers
2017-04-04 08:42:27 +03:00
, bgfx::TextureFormat::Enum(imageContainer->m_format)
, _flags
, mem
);
}
2018-06-12 00:43:17 +03:00
if (bgfx::isValid(handle) )
{
bgfx::setName(handle, _filePath);
}
if (NULL != _info)
{
bgfx::calcTextureSize(
*_info
, uint16_t(imageContainer->m_width)
, uint16_t(imageContainer->m_height)
2017-04-28 07:09:44 +03:00
, uint16_t(imageContainer->m_depth)
, imageContainer->m_cubeMap
, 1 < imageContainer->m_numMips
, imageContainer->m_numLayers
2017-04-04 08:42:27 +03:00
, bgfx::TextureFormat::Enum(imageContainer->m_format)
);
}
2015-05-24 20:25:47 +03:00
}
}
2014-05-04 02:18:28 +04:00
return handle;
2014-05-04 02:18:28 +04:00
}
bgfx::TextureHandle loadTexture(const char* _name, uint64_t _flags, uint8_t _skip, bgfx::TextureInfo* _info, bimg::Orientation::Enum* _orientation)
2014-05-04 02:18:28 +04:00
{
2017-07-20 08:53:48 +03:00
return loadTexture(entry::getFileReader(), _name, _flags, _skip, _info, _orientation);
2014-05-04 02:18:28 +04:00
}
2017-04-04 08:42:27 +03:00
bimg::ImageContainer* imageLoad(const char* _filePath, bgfx::TextureFormat::Enum _dstFormat)
{
uint32_t size = 0;
void* data = loadMem(entry::getFileReader(), entry::getAllocator(), _filePath, &size);
2017-04-04 08:42:27 +03:00
return bimg::imageParse(entry::getAllocator(), data, size, bimg::TextureFormat::Enum(_dstFormat) );
}
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
{
struct PosTexcoord
{
float m_x;
float m_y;
float m_z;
float m_pad0;
float m_u;
float m_v;
float m_pad1;
float m_pad2;
};
float* tangents = new float[6*_numVertices];
2017-02-09 06:55:31 +03:00
bx::memSet(tangents, 0, 6*_numVertices*sizeof(float) );
2014-05-04 02:18:28 +04:00
PosTexcoord v0;
PosTexcoord v1;
PosTexcoord v2;
for (uint32_t ii = 0, num = _numIndices/3; ii < num; ++ii)
{
const uint16_t* indices = &_indices[ii*3];
uint32_t i0 = indices[0];
uint32_t i1 = indices[1];
uint32_t i2 = indices[2];
2019-08-17 20:35:21 +03:00
bgfx::vertexUnpack(&v0.m_x, bgfx::Attrib::Position, _layout, _vertices, i0);
bgfx::vertexUnpack(&v0.m_u, bgfx::Attrib::TexCoord0, _layout, _vertices, i0);
2014-05-04 02:18:28 +04:00
2019-08-17 20:35:21 +03:00
bgfx::vertexUnpack(&v1.m_x, bgfx::Attrib::Position, _layout, _vertices, i1);
bgfx::vertexUnpack(&v1.m_u, bgfx::Attrib::TexCoord0, _layout, _vertices, i1);
2014-05-04 02:18:28 +04:00
2019-08-17 20:35:21 +03:00
bgfx::vertexUnpack(&v2.m_x, bgfx::Attrib::Position, _layout, _vertices, i2);
bgfx::vertexUnpack(&v2.m_u, bgfx::Attrib::TexCoord0, _layout, _vertices, i2);
2014-05-04 02:18:28 +04:00
const float bax = v1.m_x - v0.m_x;
const float bay = v1.m_y - v0.m_y;
const float baz = v1.m_z - v0.m_z;
const float bau = v1.m_u - v0.m_u;
const float bav = v1.m_v - v0.m_v;
const float cax = v2.m_x - v0.m_x;
const float cay = v2.m_y - v0.m_y;
const float caz = v2.m_z - v0.m_z;
const float cau = v2.m_u - v0.m_u;
const float cav = v2.m_v - v0.m_v;
const float det = (bau * cav - bav * cau);
const float invDet = 1.0f / det;
const float tx = (bax * cav - cax * bav) * invDet;
const float ty = (bay * cav - cay * bav) * invDet;
const float tz = (baz * cav - caz * bav) * invDet;
const float bx = (cax * bau - bax * cau) * invDet;
const float by = (cay * bau - bay * cau) * invDet;
const float bz = (caz * bau - baz * cau) * invDet;
for (uint32_t jj = 0; jj < 3; ++jj)
{
float* tanu = &tangents[indices[jj]*6];
float* tanv = &tanu[3];
tanu[0] += tx;
tanu[1] += ty;
tanu[2] += tz;
tanv[0] += bx;
tanv[1] += by;
tanv[2] += bz;
}
}
for (uint32_t ii = 0; ii < _numVertices; ++ii)
{
2018-12-22 05:02:39 +03:00
const bx::Vec3 tanu = bx::load<bx::Vec3>(&tangents[ii*6]);
const bx::Vec3 tanv = bx::load<bx::Vec3>(&tangents[ii*6 + 3]);
2014-05-04 02:18:28 +04:00
2018-12-11 06:39:00 +03:00
float nxyzw[4];
2019-08-17 20:35:21 +03:00
bgfx::vertexUnpack(nxyzw, bgfx::Attrib::Normal, _layout, _vertices, ii);
2014-05-04 02:18:28 +04:00
2018-12-22 05:02:39 +03:00
const bx::Vec3 normal = bx::load<bx::Vec3>(nxyzw);
2018-12-11 06:39:00 +03:00
const float ndt = bx::dot(normal, tanu);
const bx::Vec3 nxt = bx::cross(normal, tanu);
const bx::Vec3 tmp = bx::sub(tanu, bx::mul(normal, ndt) );
2014-05-04 02:18:28 +04:00
float tangent[4];
2018-12-11 06:39:00 +03:00
bx::store(tangent, bx::normalize(tmp) );
tangent[3] = bx::dot(nxt, tanv) < 0.0f ? -1.0f : 1.0f;
2014-05-04 02:18:28 +04:00
2019-08-17 20:35:21 +03:00
bgfx::vertexPack(tangent, true, bgfx::Attrib::Tangent, _layout, _vertices, ii);
2014-05-04 02:18:28 +04:00
}
delete [] tangents;
2015-01-08 09:36:36 +03:00
}
2014-05-04 02:18:28 +04:00
Group::Group()
2014-05-04 02:18:28 +04:00
{
reset();
}
2014-05-04 02:18:28 +04:00
void Group::reset()
2014-05-04 02:18:28 +04:00
{
m_vbh.idx = bgfx::kInvalidHandle;
m_ibh.idx = bgfx::kInvalidHandle;
m_numVertices = 0;
m_vertices = NULL;
m_numIndices = 0;
m_indices = NULL;
m_prims.clear();
}
2014-05-04 02:18:28 +04:00
namespace bgfx
{
int32_t read(bx::ReaderI* _reader, bgfx::VertexLayout& _layout, bx::Error* _err);
}
void Mesh::load(bx::ReaderSeekerI* _reader, bool _ramcopy)
2014-05-04 02:18:28 +04:00
{
2020-07-06 07:30:52 +03:00
constexpr uint32_t kChunkVertexBuffer = BX_MAKEFOURCC('V', 'B', ' ', 0x1);
constexpr uint32_t kChunkVertexBufferCompressed = BX_MAKEFOURCC('V', 'B', 'C', 0x0);
constexpr uint32_t kChunkIndexBuffer = BX_MAKEFOURCC('I', 'B', ' ', 0x0);
constexpr uint32_t kChunkIndexBufferCompressed = BX_MAKEFOURCC('I', 'B', 'C', 0x1);
constexpr uint32_t kChunkPrimitive = BX_MAKEFOURCC('P', 'R', 'I', 0x0);
2014-05-04 02:18:28 +04:00
using namespace bx;
using namespace bgfx;
2020-07-06 07:30:52 +03:00
Group group;
2020-07-06 07:30:52 +03:00
bx::AllocatorI* allocator = entry::getAllocator();
2020-07-06 07:30:52 +03:00
uint32_t chunk;
bx::Error err;
while (4 == bx::read(_reader, chunk, &err)
2020-07-06 07:30:52 +03:00
&& err.isOk() )
{
switch (chunk)
2014-05-04 02:18:28 +04:00
{
2020-07-06 07:30:52 +03:00
case kChunkVertexBuffer:
{
read(_reader, group.m_sphere, &err);
read(_reader, group.m_aabb, &err);
read(_reader, group.m_obb, &err);
2020-07-06 07:30:52 +03:00
read(_reader, m_layout, &err);
2020-07-06 07:30:52 +03:00
2019-08-17 20:35:21 +03:00
uint16_t stride = m_layout.getStride();
2020-07-06 07:30:52 +03:00
read(_reader, group.m_numVertices, &err);
const bgfx::Memory* mem = bgfx::alloc(group.m_numVertices*stride);
read(_reader, mem->data, mem->size, &err);
2020-07-06 07:30:52 +03:00
if (_ramcopy)
2014-05-04 02:18:28 +04:00
{
group.m_vertices = (uint8_t*)BX_ALLOC(allocator, group.m_numVertices*stride);
bx::memCopy(group.m_vertices, mem->data, mem->size);
2014-05-04 02:18:28 +04:00
}
2020-07-06 07:30:52 +03:00
2019-08-17 20:35:21 +03:00
group.m_vbh = bgfx::createVertexBuffer(mem, m_layout);
}
2014-05-04 02:18:28 +04:00
break;
2020-07-06 07:30:52 +03:00
case kChunkVertexBufferCompressed:
{
read(_reader, group.m_sphere, &err);
read(_reader, group.m_aabb, &err);
read(_reader, group.m_obb, &err);
2020-07-06 07:30:52 +03:00
read(_reader, m_layout, &err);
2020-07-06 07:30:52 +03:00
2019-08-17 20:35:21 +03:00
uint16_t stride = m_layout.getStride();
2020-07-06 07:30:52 +03:00
read(_reader, group.m_numVertices, &err);
2020-07-06 07:30:52 +03:00
const bgfx::Memory* mem = bgfx::alloc(group.m_numVertices*stride);
2020-07-06 07:30:52 +03:00
uint32_t compressedSize;
bx::read(_reader, compressedSize, &err);
2020-07-06 07:30:52 +03:00
void* compressedVertices = BX_ALLOC(allocator, compressedSize);
bx::read(_reader, compressedVertices, compressedSize, &err);
2020-07-06 07:30:52 +03:00
meshopt_decodeVertexBuffer(mem->data, group.m_numVertices, stride, (uint8_t*)compressedVertices, compressedSize);
2020-07-06 07:30:52 +03:00
BX_FREE(allocator, compressedVertices);
2020-07-06 07:30:52 +03:00
if (_ramcopy)
{
group.m_vertices = (uint8_t*)BX_ALLOC(allocator, group.m_numVertices*stride);
bx::memCopy(group.m_vertices, mem->data, mem->size);
}
2020-07-06 07:30:52 +03:00
2019-08-17 20:35:21 +03:00
group.m_vbh = bgfx::createVertexBuffer(mem, m_layout);
}
break;
2020-07-06 07:30:52 +03:00
case kChunkIndexBuffer:
{
read(_reader, group.m_numIndices, &err);
const bgfx::Memory* mem = bgfx::alloc(group.m_numIndices*2);
read(_reader, mem->data, mem->size, &err);
2020-07-06 07:30:52 +03:00
if (_ramcopy)
2014-05-04 02:18:28 +04:00
{
group.m_indices = (uint16_t*)BX_ALLOC(allocator, group.m_numIndices*2);
bx::memCopy(group.m_indices, mem->data, mem->size);
2014-05-04 02:18:28 +04:00
}
group.m_ibh = bgfx::createIndexBuffer(mem);
}
break;
2020-07-06 07:30:52 +03:00
case kChunkIndexBufferCompressed:
{
bx::read(_reader, group.m_numIndices, &err);
2020-07-06 07:30:52 +03:00
const bgfx::Memory* mem = bgfx::alloc(group.m_numIndices*2);
2020-07-06 07:30:52 +03:00
uint32_t compressedSize;
bx::read(_reader, compressedSize, &err);
2020-07-06 07:30:52 +03:00
void* compressedIndices = BX_ALLOC(allocator, compressedSize);
2020-07-06 07:30:52 +03:00
bx::read(_reader, compressedIndices, compressedSize, &err);
2020-07-06 07:30:52 +03:00
meshopt_decodeIndexBuffer(mem->data, group.m_numIndices, 2, (uint8_t*)compressedIndices, compressedSize);
2020-07-06 07:30:52 +03:00
BX_FREE(allocator, compressedIndices);
2020-07-06 07:30:52 +03:00
if (_ramcopy)
{
group.m_indices = (uint16_t*)BX_ALLOC(allocator, group.m_numIndices*2);
bx::memCopy(group.m_indices, mem->data, mem->size);
}
2020-07-06 07:30:52 +03:00
group.m_ibh = bgfx::createIndexBuffer(mem);
}
break;
2020-07-06 07:30:52 +03:00
case kChunkPrimitive:
{
uint16_t len;
read(_reader, len, &err);
2020-07-06 07:30:52 +03:00
stl::string material;
material.resize(len);
read(_reader, const_cast<char*>(material.c_str() ), len, &err);
2020-07-06 07:30:52 +03:00
uint16_t num;
read(_reader, num, &err);
2020-07-06 07:30:52 +03:00
for (uint32_t ii = 0; ii < num; ++ii)
2014-05-04 02:18:28 +04:00
{
read(_reader, len, &err);
2020-07-06 07:30:52 +03:00
stl::string name;
name.resize(len);
read(_reader, const_cast<char*>(name.c_str() ), len, &err);
2020-07-06 07:30:52 +03:00
Primitive prim;
read(_reader, prim.m_startIndex, &err);
read(_reader, prim.m_numIndices, &err);
read(_reader, prim.m_startVertex, &err);
read(_reader, prim.m_numVertices, &err);
read(_reader, prim.m_sphere, &err);
read(_reader, prim.m_aabb, &err);
read(_reader, prim.m_obb, &err);
2020-07-06 07:30:52 +03:00
group.m_prims.push_back(prim);
2014-05-04 02:18:28 +04:00
}
2020-07-06 07:30:52 +03:00
m_groups.push_back(group);
group.reset();
}
2014-05-04 02:18:28 +04:00
break;
2020-07-06 07:30:52 +03:00
2014-05-04 02:18:28 +04:00
default:
DBG("%08x at %d", chunk, bx::skip(_reader, 0) );
break;
}
}
}
2014-05-04 02:18:28 +04:00
void Mesh::unload()
{
bx::AllocatorI* allocator = entry::getAllocator();
2020-07-06 07:30:52 +03:00
for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
2014-05-04 02:18:28 +04:00
{
const Group& group = *it;
bgfx::destroy(group.m_vbh);
2020-07-06 07:30:52 +03:00
if (bgfx::isValid(group.m_ibh) )
2014-05-04 02:18:28 +04:00
{
bgfx::destroy(group.m_ibh);
}
2020-07-06 07:30:52 +03:00
if (NULL != group.m_vertices)
{
BX_FREE(allocator, group.m_vertices);
}
2014-05-04 02:18:28 +04:00
2020-07-06 07:30:52 +03:00
if (NULL != group.m_indices)
{
BX_FREE(allocator, group.m_indices);
2014-05-04 02:18:28 +04:00
}
}
m_groups.clear();
}
2014-05-04 02:18:28 +04:00
void Mesh::submit(bgfx::ViewId _id, bgfx::ProgramHandle _program, const float* _mtx, uint64_t _state) const
{
if (BGFX_STATE_MASK == _state)
2014-05-04 02:18:28 +04:00
{
_state = 0
2020-07-06 07:30:52 +03:00
| BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CCW
| BGFX_STATE_MSAA
;
}
2020-07-06 07:30:52 +03:00
bgfx::setTransform(_mtx);
bgfx::setState(_state);
2020-07-06 07:30:52 +03:00
for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
{
const Group& group = *it;
2020-07-06 07:30:52 +03:00
bgfx::setIndexBuffer(group.m_ibh);
bgfx::setVertexBuffer(0, group.m_vbh);
2021-10-28 03:00:39 +03:00
bgfx::submit(
_id
, _program
, 0
, BGFX_DISCARD_INDEX_BUFFER
| BGFX_DISCARD_VERTEX_STREAMS
);
}
2021-10-28 03:00:39 +03:00
bgfx::discard();
}
void Mesh::submit(const MeshState*const* _state, uint8_t _numPasses, const float* _mtx, uint16_t _numMatrices) const
{
uint32_t cached = bgfx::setTransform(_mtx, _numMatrices);
2020-07-06 07:30:52 +03:00
for (uint32_t pass = 0; pass < _numPasses; ++pass)
{
bgfx::setTransform(cached, _numMatrices);
2020-07-06 07:30:52 +03:00
const MeshState& state = *_state[pass];
bgfx::setState(state.m_state);
2020-07-06 07:30:52 +03:00
for (uint8_t tex = 0; tex < state.m_numTextures; ++tex)
2014-05-21 08:15:48 +04:00
{
const MeshState::Texture& texture = state.m_textures[tex];
2020-07-06 07:30:52 +03:00
bgfx::setTexture(
texture.m_stage
, texture.m_sampler
, texture.m_texture
, texture.m_flags
);
2014-05-21 08:15:48 +04:00
}
2020-07-06 07:30:52 +03:00
2014-05-04 02:18:28 +04:00
for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
{
const Group& group = *it;
2020-07-06 07:30:52 +03:00
2014-05-04 02:18:28 +04:00
bgfx::setIndexBuffer(group.m_ibh);
2017-05-14 21:48:59 +03:00
bgfx::setVertexBuffer(0, group.m_vbh);
2020-07-06 07:30:52 +03:00
bgfx::submit(
state.m_viewId
, state.m_program
, 0
2021-10-28 03:00:39 +03:00
, BGFX_DISCARD_INDEX_BUFFER
| BGFX_DISCARD_VERTEX_STREAMS
2020-07-06 07:30:52 +03:00
);
2014-05-04 02:18:28 +04:00
}
2021-10-28 03:00:39 +03:00
bgfx::discard(0
| BGFX_DISCARD_BINDINGS
| BGFX_DISCARD_STATE
| BGFX_DISCARD_TRANSFORM
);
2014-05-04 02:18:28 +04:00
}
2021-10-28 03:00:39 +03:00
bgfx::discard();
}
2014-05-04 02:18:28 +04:00
Mesh* meshLoad(bx::ReaderSeekerI* _reader, bool _ramcopy)
2014-05-04 02:18:28 +04:00
{
Mesh* mesh = new Mesh;
mesh->load(_reader, _ramcopy);
2014-05-04 02:18:28 +04:00
return mesh;
}
Mesh* meshLoad(const char* _filePath, bool _ramcopy)
2014-05-04 02:18:28 +04:00
{
bx::FileReaderI* reader = entry::getFileReader();
2016-02-01 03:00:02 +03:00
if (bx::open(reader, _filePath) )
{
Mesh* mesh = meshLoad(reader, _ramcopy);
2016-02-01 03:00:02 +03:00
bx::close(reader);
return mesh;
}
return NULL;
2014-05-04 02:18:28 +04:00
}
void meshUnload(Mesh* _mesh)
{
_mesh->unload();
delete _mesh;
}
2015-01-08 09:36:36 +03:00
MeshState* meshStateCreate()
{
MeshState* state = (MeshState*)BX_ALLOC(entry::getAllocator(), sizeof(MeshState) );
return state;
}
void meshStateDestroy(MeshState* _meshState)
{
BX_FREE(entry::getAllocator(), _meshState);
}
void meshSubmit(const Mesh* _mesh, bgfx::ViewId _id, bgfx::ProgramHandle _program, const float* _mtx, uint64_t _state)
2014-05-04 02:18:28 +04:00
{
2014-05-21 08:15:48 +04:00
_mesh->submit(_id, _program, _mtx, _state);
2014-05-04 02:18:28 +04: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)
{
_mesh->submit(_state, _numPasses, _mtx, _numMatrices);
}
2015-10-24 06:57:04 +03:00
2021-04-26 06:23:36 +03:00
struct RendererTypeRemap
{
bx::StringView name;
bgfx::RendererType::Enum type;
};
static RendererTypeRemap s_rendererTypeRemap[] =
{
{ "d3d11", bgfx::RendererType::Direct3D11 },
{ "d3d12", bgfx::RendererType::Direct3D12 },
{ "d3d9", bgfx::RendererType::Direct3D9 },
{ "gl", bgfx::RendererType::OpenGL },
{ "mtl", bgfx::RendererType::Metal },
{ "noop", bgfx::RendererType::Noop },
{ "vk", bgfx::RendererType::Vulkan },
};
bx::StringView getName(bgfx::RendererType::Enum _type)
{
for (uint32_t ii = 0; ii < BX_COUNTOF(s_rendererTypeRemap); ++ii)
{
const RendererTypeRemap& remap = s_rendererTypeRemap[ii];
if (_type == remap.type)
{
return remap.name;
}
}
return "";
}
bgfx::RendererType::Enum getType(const bx::StringView& _name)
{
for (uint32_t ii = 0; ii < BX_COUNTOF(s_rendererTypeRemap); ++ii)
{
const RendererTypeRemap& remap = s_rendererTypeRemap[ii];
if (0 == bx::strCmpI(_name, remap.name) )
{
return remap.type;
}
}
return bgfx::RendererType::Count;
}
2017-06-30 08:23:18 +03:00
Args::Args(int _argc, const char* const* _argv)
2015-10-24 06:57:04 +03:00
: m_type(bgfx::RendererType::Count)
, m_pciId(BGFX_PCI_ID_NONE)
{
bx::CommandLine cmdLine(_argc, (const char**)_argv);
if (cmdLine.hasArg("gl") )
{
m_type = bgfx::RendererType::OpenGL;
}
2016-02-17 07:38:59 +03:00
else if (cmdLine.hasArg("vk") )
2015-10-24 06:57:04 +03:00
{
2016-02-17 07:38:59 +03:00
m_type = bgfx::RendererType::Vulkan;
}
else if (cmdLine.hasArg("noop") )
{
2016-09-28 08:07:53 +03:00
m_type = bgfx::RendererType::Noop;
2015-10-24 06:57:04 +03:00
}
2022-09-15 06:59:59 +03:00
if (cmdLine.hasArg("d3d9") )
2015-10-24 06:57:04 +03:00
{
2022-09-15 06:59:59 +03:00
m_type = bgfx::RendererType::Direct3D9;
}
else if (cmdLine.hasArg("d3d11") )
{
m_type = bgfx::RendererType::Direct3D11;
}
else if (cmdLine.hasArg("d3d12") )
{
m_type = bgfx::RendererType::Direct3D12;
2015-10-24 06:57:04 +03:00
}
else if (BX_ENABLED(BX_PLATFORM_OSX) )
{
if (cmdLine.hasArg("mtl") )
{
m_type = bgfx::RendererType::Metal;
}
}
if (cmdLine.hasArg("amd") )
{
m_pciId = BGFX_PCI_ID_AMD;
}
else if (cmdLine.hasArg("nvidia") )
{
m_pciId = BGFX_PCI_ID_NVIDIA;
}
else if (cmdLine.hasArg("intel") )
{
m_pciId = BGFX_PCI_ID_INTEL;
}
else if (cmdLine.hasArg("sw") )
{
m_pciId = BGFX_PCI_ID_SOFTWARE_RASTERIZER;
}
}