Added index buffer compression support.

This commit is contained in:
Branimir Karadžić 2014-12-31 16:11:07 -08:00
parent 53594ec5d4
commit c6efb167ba
9 changed files with 188 additions and 253 deletions

View file

@ -524,8 +524,6 @@ void CompressTriangleCodes1( const Ty* triangles,
verticesRead += 3; verticesRead += 3;
break; break;
} }
default: break;
} }
// populate the edge fifo with the 3 most recent edges // populate the edge fifo with the 3 most recent edges

View file

@ -98,8 +98,8 @@ inline ReadBitstream::ReadBitstream( const uint8_t* buffer, size_t bufferSize )
RBS_INLINE uint32_t ReadBitstream::Read( uint32_t bitCount ) RBS_INLINE uint32_t ReadBitstream::Read( uint32_t bitCount )
{ {
uint64_t mask = ( 1 << bitCount ) - 1; uint64_t mask = ( uint64_t( 1 ) << bitCount ) - 1;
uint32_t result = static_cast< uint32_t >( ( m_bitBuffer >> ( 64 - m_bitsLeft ) ) & mask ); uint32_t result = static_cast< uint32_t >( ( m_bitBuffer >> ( 64 - m_bitsLeft ) & ( m_bitsLeft == 0 ? 0 : 0xFFFFFFFFFFFFFFFF ) ) & mask );
if ( m_bitsLeft < bitCount ) if ( m_bitsLeft < bitCount )
{ {

View file

@ -96,7 +96,7 @@ private:
WBS_INLINE void WriteBitstream::Write( uint32_t value, uint32_t bitCount ) WBS_INLINE void WriteBitstream::Write( uint32_t value, uint32_t bitCount )
{ {
m_bitBuffer |= static_cast<uint64_t>( value ) << ( 64 - m_bitsLeft ); m_bitBuffer |= ( static_cast<uint64_t>( value ) << ( 64 - m_bitsLeft ) ) & ( m_bitsLeft == 0 ? 0 : 0xFFFFFFFFFFFFFFFF );
if ( bitCount > m_bitsLeft ) if ( bitCount > m_bitsLeft )
{ {

View file

@ -100,208 +100,9 @@ struct Uniforms
bgfx::UniformHandle u_rgbDiff; bgfx::UniformHandle u_rgbDiff;
bgfx::UniformHandle u_rgbSpec; bgfx::UniformHandle u_rgbSpec;
}; };
static Uniforms s_uniforms; static Uniforms s_uniforms;
struct Aabb
{
float m_min[3];
float m_max[3];
};
struct Obb
{
float m_mtx[16];
};
struct Sphere
{
float m_center[3];
float m_radius;
};
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 std::vector<Primitive> PrimitiveArray;
struct Group
{
Group()
{
reset();
}
void reset()
{
m_vbh.idx = bgfx::invalidHandle;
m_ibh.idx = bgfx::invalidHandle;
m_prims.clear();
}
bgfx::VertexBufferHandle m_vbh;
bgfx::IndexBufferHandle m_ibh;
Sphere m_sphere;
Aabb m_aabb;
Obb m_obb;
PrimitiveArray m_prims;
};
namespace bgfx
{
int32_t read(bx::ReaderI* _reader, bgfx::VertexDecl& _decl);
}
struct Mesh
{
void load(const char* _filePath)
{
#define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x1)
#define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
#define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
bx::CrtFileReader reader;
reader.open(_filePath);
Group group;
uint32_t chunk;
while (4 == bx::read(&reader, chunk) )
{
switch (chunk)
{
case BGFX_CHUNK_MAGIC_VB:
{
bx::read(&reader, group.m_sphere);
bx::read(&reader, group.m_aabb);
bx::read(&reader, group.m_obb);
bgfx::read(&reader, m_decl);
uint16_t stride = m_decl.getStride();
uint16_t numVertices;
bx::read(&reader, numVertices);
const bgfx::Memory* mem = bgfx::alloc(numVertices*stride);
bx::read(&reader, mem->data, mem->size);
group.m_vbh = bgfx::createVertexBuffer(mem, m_decl);
}
break;
case BGFX_CHUNK_MAGIC_IB:
{
uint32_t numIndices;
bx::read(&reader, numIndices);
const bgfx::Memory* mem = bgfx::alloc(numIndices*2);
bx::read(&reader, mem->data, mem->size);
group.m_ibh = bgfx::createIndexBuffer(mem);
}
break;
case BGFX_CHUNK_MAGIC_PRI:
{
uint16_t len;
bx::read(&reader, len);
std::string material;
material.resize(len);
bx::read(&reader, const_cast<char*>(material.c_str() ), len);
uint16_t num;
bx::read(&reader, num);
for (uint32_t ii = 0; ii < num; ++ii)
{
bx::read(&reader, len);
std::string name;
name.resize(len);
bx::read(&reader, const_cast<char*>(name.c_str() ), len);
Primitive prim;
bx::read(&reader, prim.m_startIndex);
bx::read(&reader, prim.m_numIndices);
bx::read(&reader, prim.m_startVertex);
bx::read(&reader, prim.m_numVertices);
bx::read(&reader, prim.m_sphere);
bx::read(&reader, prim.m_aabb);
bx::read(&reader, prim.m_obb);
group.m_prims.push_back(prim);
}
m_groups.push_back(group);
group.reset();
}
break;
default:
DBG("%08x at %d", chunk, reader.seek() );
break;
}
}
reader.close();
}
void unload()
{
for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
{
const Group& group = *it;
bgfx::destroyVertexBuffer(group.m_vbh);
if (bgfx::isValid(group.m_ibh) )
{
bgfx::destroyIndexBuffer(group.m_ibh);
}
}
m_groups.clear();
}
void submit(uint8_t _view, bgfx::ProgramHandle _program, float* _mtx)
{
for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
{
const Group& group = *it;
// Set uniforms.
s_uniforms.submitPerDrawUniforms();
// Set model matrix for rendering.
bgfx::setTransform(_mtx);
bgfx::setProgram(_program);
bgfx::setIndexBuffer(group.m_ibh);
bgfx::setVertexBuffer(group.m_vbh);
// Set render states.
bgfx::setState(0
| BGFX_STATE_RGB_WRITE
| BGFX_STATE_ALPHA_WRITE
| BGFX_STATE_DEPTH_WRITE
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CCW
| BGFX_STATE_MSAA
);
// Submit primitive for rendering to view 0.
bgfx::submit(_view);
}
}
bgfx::VertexDecl m_decl;
typedef std::vector<Group> GroupArray;
GroupArray m_groups;
};
struct PosColorTexCoord0Vertex struct PosColorTexCoord0Vertex
{ {
float m_x; float m_x;
@ -469,8 +270,8 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::ProgramHandle programMesh = loadProgram("vs_ibl_mesh", "fs_ibl_mesh"); bgfx::ProgramHandle programMesh = loadProgram("vs_ibl_mesh", "fs_ibl_mesh");
bgfx::ProgramHandle programSky = loadProgram("vs_ibl_skybox", "fs_ibl_skybox"); bgfx::ProgramHandle programSky = loadProgram("vs_ibl_skybox", "fs_ibl_skybox");
Mesh meshBunny; Mesh* meshBunny;
meshBunny.load("meshes/bunny.bin"); meshBunny = meshLoad("meshes/pony.bin");
struct Settings struct Settings
{ {
@ -710,14 +511,14 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::setTexture(4, u_texCube, lightProbes[currentLightProbe].m_tex); bgfx::setTexture(4, u_texCube, lightProbes[currentLightProbe].m_tex);
bgfx::setTexture(5, u_texCubeIrr, lightProbes[currentLightProbe].m_texIrr); bgfx::setTexture(5, u_texCubeIrr, lightProbes[currentLightProbe].m_texIrr);
meshBunny.submit(1, programMesh, mtx); meshSubmit(meshBunny, 1, programMesh, mtx);
// Advance to next frame. Rendering thread will be kicked to // Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives. // process submitted rendering primitives.
bgfx::frame(); bgfx::frame();
} }
meshBunny.unload(); meshUnload(meshBunny);
// Cleanup. // Cleanup.
bgfx::destroyProgram(programMesh); bgfx::destroyProgram(programMesh);

View file

@ -29,5 +29,11 @@ GEOMETRY_BIN=$(addprefix $(RUNTIME_DIR)/meshes/, $(addsuffix .bin, $(basename $(
all: $(GEOMETRY_BIN) all: $(GEOMETRY_BIN)
clean:
@echo Cleaning...
@-rm -vf $(GEOMETRY_BIN)
rebuild: clean all
$(RUNTIME_DIR)/meshes/%.bin : %.obj $(RUNTIME_DIR)/meshes/%.bin : %.obj
$(SILENT) $(GEOMETRYC) -f $(<) -o $(@) --packnormal 1 $(SILENT) $(GEOMETRYC) -f $(<) -o $(@) --packnormal 1

View file

@ -14,6 +14,7 @@ namespace stl = tinystl;
#include <bx/readerwriter.h> #include <bx/readerwriter.h>
#include <bx/fpumath.h> #include <bx/fpumath.h>
#include "entry/entry.h" #include "entry/entry.h"
#include <ib-compress/indexbufferdecompression.h>
void* load(bx::FileReaderI* _reader, const char* _filePath) void* load(bx::FileReaderI* _reader, const char* _filePath)
{ {
@ -279,6 +280,7 @@ struct Mesh
{ {
#define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x1) #define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x1)
#define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0) #define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
#define BGFX_CHUNK_MAGIC_IBC BX_MAKEFOURCC('I', 'B', 'C', 0x0)
#define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0) #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
using namespace bx; using namespace bx;
@ -320,6 +322,30 @@ struct Mesh
} }
break; break;
case BGFX_CHUNK_MAGIC_IBC:
{
uint32_t numIndices;
bx::read(_reader, numIndices);
const bgfx::Memory* mem = bgfx::alloc(numIndices*2);
uint32_t compressedSize;
bx::read(_reader, compressedSize);
bx::CrtAllocator allocator;
void* compressedIndices = BX_ALLOC(&allocator, compressedSize);
bx::read(_reader, compressedIndices, compressedSize);
ReadBitstream rbs( (const uint8_t*)compressedIndices, compressedSize);
DecompressIndexBuffer( (uint16_t*)mem->data, numIndices / 3, rbs);
BX_FREE(&allocator, compressedIndices);
group.m_ibh = bgfx::createIndexBuffer(mem);
}
break;
case BGFX_CHUNK_MAGIC_PRI: case BGFX_CHUNK_MAGIC_PRI:
{ {
uint16_t len; uint16_t len;

View file

@ -14,6 +14,8 @@ project ("example-common")
} }
files { files {
BGFX_DIR .. "3rdparty/ib-compress/**.cpp",
BGFX_DIR .. "3rdparty/ib-compress/**.h",
BGFX_DIR .. "examples/common/**.cpp", BGFX_DIR .. "examples/common/**.cpp",
BGFX_DIR .. "examples/common/**.h", BGFX_DIR .. "examples/common/**.h",
} }

View file

@ -10,8 +10,7 @@ project "geometryc"
includedirs { includedirs {
BX_DIR .. "include", BX_DIR .. "include",
BGFX_DIR .. "include", BGFX_DIR .. "include",
BGFX_DIR .. "3rdparty/forsyth-too", BGFX_DIR .. "3rdparty",
BGFX_DIR .. "3rdparty/ib-compress",
BGFX_DIR .. "examples/common", BGFX_DIR .. "examples/common",
} }

View file

@ -15,9 +15,11 @@
#include <tinystl/allocator.h> #include <tinystl/allocator.h>
#include <tinystl/unordered_map.h> #include <tinystl/unordered_map.h>
#include <tinystl/unordered_set.h>
namespace stl = tinystl; namespace stl = tinystl;
#include <forsythtriangleorderoptimizer.h> #include <forsyth-too/forsythtriangleorderoptimizer.h>
#include <ib-compress/indexbuffercompression.h>
#if 0 #if 0
# define BX_TRACE(_format, ...) \ # define BX_TRACE(_format, ...) \
@ -116,6 +118,7 @@ static uint32_t s_obbSteps = 17;
#define BGFX_CHUNK_MAGIC_GEO BX_MAKEFOURCC('G', 'E', 'O', 0x0) #define BGFX_CHUNK_MAGIC_GEO BX_MAKEFOURCC('G', 'E', 'O', 0x0)
#define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x1) #define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x1)
#define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0) #define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
#define BGFX_CHUNK_MAGIC_IBC BX_MAKEFOURCC('I', 'B', 'C', 0x0)
#define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0) #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
long int fsize(FILE* _file) long int fsize(FILE* _file)
@ -135,6 +138,36 @@ void triangleReorder(uint16_t* _indices, uint32_t _numIndices, uint32_t _numVert
delete [] newIndexList; delete [] newIndexList;
} }
void triangleCompress(bx::WriterI* _writer, uint16_t* _indices, uint32_t _numIndices, uint8_t* _vertexData, uint32_t _numVertices, uint16_t _stride)
{
uint32_t* vertexRemap = (uint32_t*)malloc(_numVertices*sizeof(uint32_t) );
WriteBitstream writer;
CompressIndexBuffer(_indices, _numIndices/3, vertexRemap, _numVertices, IBCF_PER_TRIANGLE_1, writer);
writer.Finish();
printf( "uncompressed: %10d, compressed: %10d, ratio: %0.2f%%\n"
, _numIndices * 2
, writer.ByteSize()
, 100.0f - float( writer.ByteSize() ) / float( _numIndices * 2 )*100.0f
);
BX_UNUSED(_vertexData, _stride);
uint8_t* outVertexData = (uint8_t*)malloc(_numVertices*_stride);
for (uint32_t ii = 0; ii < _numVertices; ++ii)
{
if (UINT32_MAX != vertexRemap[ii])
{
memcpy(&outVertexData[vertexRemap[ii]*_stride], &_vertexData[ii*_stride], _stride);
}
}
memcpy(_vertexData, outVertexData, _numVertices*_stride);
free(outVertexData);
free(vertexRemap);
bx::write(_writer, writer.RawData(), (uint32_t)writer.ByteSize() );
}
void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexDecl _decl, const uint16_t* _indices, uint32_t _numIndices) void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexDecl _decl, const uint16_t* _indices, uint32_t _numIndices)
{ {
struct PosTexcoord struct PosTexcoord
@ -262,7 +295,17 @@ void write(bx::WriterI* _writer, const void* _vertices, uint32_t _numVertices, u
bx::write(_writer, obb); bx::write(_writer, obb);
} }
void write(bx::WriterI* _writer, const uint8_t* _vertices, uint32_t _numVertices, const bgfx::VertexDecl& _decl, const uint16_t* _indices, uint32_t _numIndices, const std::string& _material, const PrimitiveArray& _primitives) void write(bx::WriterI* _writer
, const uint8_t* _vertices
, uint32_t _numVertices
, const bgfx::VertexDecl& _decl
, const uint16_t* _indices
, uint32_t _numIndices
, const uint8_t* _compressedIndices
, uint32_t _compressedSize
, const std::string& _material
, const PrimitiveArray& _primitives
)
{ {
using namespace bx; using namespace bx;
using namespace bgfx; using namespace bgfx;
@ -276,9 +319,19 @@ void write(bx::WriterI* _writer, const uint8_t* _vertices, uint32_t _numVertices
write(_writer, uint16_t(_numVertices) ); write(_writer, uint16_t(_numVertices) );
write(_writer, _vertices, _numVertices*stride); write(_writer, _vertices, _numVertices*stride);
if (NULL != _compressedIndices)
{
write(_writer, BGFX_CHUNK_MAGIC_IBC);
write(_writer, _numIndices);
write(_writer, _compressedSize);
write(_writer, _compressedIndices, _compressedSize);
}
else
{
write(_writer, BGFX_CHUNK_MAGIC_IB); write(_writer, BGFX_CHUNK_MAGIC_IB);
write(_writer, _numIndices); write(_writer, _numIndices);
write(_writer, _indices, _numIndices*2); write(_writer, _indices, _numIndices*2);
}
write(_writer, BGFX_CHUNK_MAGIC_PRI); write(_writer, BGFX_CHUNK_MAGIC_PRI);
uint16_t nameLen = uint16_t(_material.size() ); uint16_t nameLen = uint16_t(_material.size() );
@ -336,6 +389,7 @@ void help(const char* _error = NULL)
" 0 - unpacked 8 bytes (default).\n" " 0 - unpacked 8 bytes (default).\n"
" 1 - packed 4 bytes.\n" " 1 - packed 4 bytes.\n"
" --tangent Calculate tangent vectors (packing mode is the same as normal).\n" " --tangent Calculate tangent vectors (packing mode is the same as normal).\n"
" -c, --compress Compress indices.\n"
"\n" "\n"
"For additional information, see https://github.com/bkaradzic/bgfx\n" "For additional information, see https://github.com/bkaradzic/bgfx\n"
@ -384,6 +438,8 @@ int main(int _argc, const char* _argv[])
scale = (float)atof(scaleArg); scale = (float)atof(scaleArg);
} }
bool compress = cmdLine.hasArg('c', "compress");
cmdLine.hasArg(s_obbSteps, '\0', "obb"); cmdLine.hasArg(s_obbSteps, '\0', "obb");
s_obbSteps = bx::uint32_min(bx::uint32_max(s_obbSteps, 1), 90); s_obbSteps = bx::uint32_min(bx::uint32_max(s_obbSteps, 1), 90);
@ -760,6 +816,9 @@ int main(int _argc, const char* _argv[])
uint32_t positionOffset = decl.getOffset(bgfx::Attrib::Position); uint32_t positionOffset = decl.getOffset(bgfx::Attrib::Position);
uint32_t color0Offset = decl.getOffset(bgfx::Attrib::Color0); uint32_t color0Offset = decl.getOffset(bgfx::Attrib::Color0);
bx::CrtAllocator crtAllocator;
bx::MemoryBlock memBlock(&crtAllocator);
uint32_t ii = 0; uint32_t ii = 0;
for (GroupArray::const_iterator groupIt = groups.begin(); groupIt != groups.end(); ++groupIt, ++ii) for (GroupArray::const_iterator groupIt = groups.begin(); groupIt != groups.end(); ++groupIt, ++ii)
{ {
@ -775,20 +834,42 @@ int main(int _argc, const char* _argv[])
primitives.push_back(prim); primitives.push_back(prim);
} }
triReorderElapsed -= bx::getHPCounter();
for (PrimitiveArray::const_iterator primIt = primitives.begin(); primIt != primitives.end(); ++primIt)
{
const Primitive& prim = *primIt;
triangleReorder(indexData + prim.m_startIndex, prim.m_numIndices, numVertices, 32);
}
triReorderElapsed += bx::getHPCounter();
if (hasTangent) if (hasTangent)
{ {
calcTangents(vertexData, numVertices, decl, indexData, numIndices); calcTangents(vertexData, numVertices, decl, indexData, numIndices);
} }
write(&writer, vertexData, numVertices, decl, indexData, numIndices, material, primitives); bx::MemoryWriter memWriter(&memBlock);
triReorderElapsed -= bx::getHPCounter();
for (PrimitiveArray::const_iterator primIt = primitives.begin(); primIt != primitives.end(); ++primIt)
{
const Primitive& prim = *primIt;
triangleReorder(indexData + prim.m_startIndex, prim.m_numIndices, numVertices, 32);
if (compress)
{
triangleCompress(&memWriter
, indexData + prim.m_startIndex
, prim.m_numIndices
, vertexData + prim.m_startVertex
, numVertices
, stride
);
}
}
triReorderElapsed += bx::getHPCounter();
write(&writer
, vertexData
, numVertices
, decl
, indexData
, numIndices
, (uint8_t*)memBlock.more()
, memBlock.getSize()
, material
, primitives
);
primitives.clear(); primitives.clear();
for (Index3Map::iterator indexIt = indexMap.begin(); indexIt != indexMap.end(); ++indexIt) for (Index3Map::iterator indexIt = indexMap.begin(); indexIt != indexMap.end(); ++indexIt)
@ -873,20 +954,42 @@ int main(int _argc, const char* _argv[])
if (0 < primitives.size() ) if (0 < primitives.size() )
{ {
triReorderElapsed -= bx::getHPCounter();
for (PrimitiveArray::const_iterator primIt = primitives.begin(); primIt != primitives.end(); ++primIt)
{
const Primitive& prim = *primIt;
triangleReorder(indexData + prim.m_startIndex, prim.m_numIndices, numVertices, 32);
}
triReorderElapsed += bx::getHPCounter();
if (hasTangent) if (hasTangent)
{ {
calcTangents(vertexData, numVertices, decl, indexData, numIndices); calcTangents(vertexData, numVertices, decl, indexData, numIndices);
} }
write(&writer, vertexData, numVertices, decl, indexData, numIndices, material, primitives); bx::MemoryWriter memWriter(&memBlock);
triReorderElapsed -= bx::getHPCounter();
for (PrimitiveArray::const_iterator primIt = primitives.begin(); primIt != primitives.end(); ++primIt)
{
const Primitive& prim = *primIt;
triangleReorder(indexData + prim.m_startIndex, prim.m_numIndices, numVertices, 32);
if (compress)
{
triangleCompress(&memWriter
, indexData + prim.m_startIndex
, prim.m_numIndices
, vertexData + prim.m_startVertex
, numVertices
, stride
);
}
}
triReorderElapsed += bx::getHPCounter();
write(&writer
, vertexData
, numVertices
, decl
, indexData
, numIndices
, (uint8_t*)memBlock.more()
, memBlock.getSize()
, material
, primitives
);
} }
printf("size: %d\n", uint32_t(writer.seek() ) ); printf("size: %d\n", uint32_t(writer.seek() ) );