From 1e03fe246d983c53508eaa3a3a0934497edb978d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 3 Oct 2014 22:22:28 -0700 Subject: [PATCH] Updated ib-compress. --- .../ib-compress/indexbuffercompression.cpp | 88 +++++++++++-------- 3rdparty/ib-compress/indexbuffercompression.h | 37 ++------ .../indexbuffercompressionformat.h | 14 +++ .../ib-compress/indexbufferdecompression.cpp | 41 +++++---- .../ib-compress/indexbufferdecompression.h | 14 +-- .../ib-compress/indexcompressionconstants.h | 15 ++-- 3rdparty/ib-compress/readbitstream.h | 3 +- 3rdparty/ib-compress/writebitstream.h | 3 +- 8 files changed, 107 insertions(+), 108 deletions(-) create mode 100644 3rdparty/ib-compress/indexbuffercompressionformat.h diff --git a/3rdparty/ib-compress/indexbuffercompression.cpp b/3rdparty/ib-compress/indexbuffercompression.cpp index e4fffa7a..d81f0250 100644 --- a/3rdparty/ib-compress/indexbuffercompression.cpp +++ b/3rdparty/ib-compress/indexbuffercompression.cpp @@ -22,15 +22,15 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "IndexBufferCompression.h" -#include "WriteBitstream.h" -#include "IndexCompressionConstants.h" +#include "indexbuffercompression.h" +#include "writebitstream.h" +#include "indexcompressionconstants.h" #include #ifdef _MSC_VER #define IBC_INLINE __forceinline #else -#define IBC_INLINE __attribute__((always_inline)) +#define IBC_INLINE inline #endif // Individual vertex type classifications. @@ -187,11 +187,11 @@ static IBC_INLINE VertexClassification ClassifyVertex( uint32_t vertex, const ui } template -void CompressIndexBuffer2( const Ty* triangles, - uint32_t triangleCount, - uint32_t* vertexRemap, - uint32_t vertexCount, - WriteBitstream& output ) +void CompressTriangleCodes1( const Ty* triangles, + uint32_t triangleCount, + uint32_t* vertexRemap, + uint32_t vertexCount, + WriteBitstream& output ) { Edge edgeFifo[ EDGE_FIFO_SIZE ]; uint32_t vertexFifo[ VERTEX_FIFO_SIZE ]; @@ -558,7 +558,7 @@ static IBC_INLINE void OutputVertex( uint32_t vertex, // no remap, so remap to the current high watermark and output a new vertex code. vertexRemap[ vertex ] = newVertexCount; - output.Write( IB_NEW_VERTEX, IB_CODE_BITS ); + output.Write( IB_NEW_VERTEX, IB_VERTEX_CODE_BITS ); ++newVertexCount; @@ -577,7 +577,7 @@ static IBC_INLINE void OutputVertex( uint32_t vertex, if ( vertexFifo[ vertexCursor & VERTEX_FIFO_MASK ] == vertex ) { // found a cached vertex, so write out the code for a cached vertex, as the relative index into the fifo. - output.Write( IB_CACHED_VERTEX, IB_CODE_BITS ); + output.Write( IB_CACHED_VERTEX, IB_VERTEX_CODE_BITS ); output.Write( ( verticesRead - 1 ) - vertexCursor, CACHED_VERTEX_BITS ); return; @@ -585,7 +585,7 @@ static IBC_INLINE void OutputVertex( uint32_t vertex, } // no cached vertex found, so write out a free vertex - output.Write( IB_FREE_VERTEX, IB_CODE_BITS ); + output.Write( IB_FREE_VERTEX, IB_VERTEX_CODE_BITS ); // free vertices are relative to the latest new vertex. uint32_t vertexOutput = ( newVertexCount - 1 ) - vertexRemap[ vertex ]; @@ -602,11 +602,11 @@ static IBC_INLINE void OutputVertex( uint32_t vertex, } template -void CompressIndexBuffer( const Ty* triangles, - uint32_t triangleCount, - uint32_t* vertexRemap, - uint32_t vertexCount, - WriteBitstream& output ) +void CompressIndiceCodes1( const Ty* triangles, + uint32_t triangleCount, + uint32_t* vertexRemap, + uint32_t vertexCount, + WriteBitstream& output ) { Edge edgeFifo[ EDGE_FIFO_SIZE ]; uint32_t vertexFifo[ VERTEX_FIFO_SIZE ]; @@ -633,7 +633,7 @@ void CompressIndexBuffer( const Ty* triangles, int32_t edgeCursor = edgesRead - 1; bool foundEdge = false; - int32_t freeVertex; + int32_t freeVertex = -1; // should not be negative 1 if found, this is not used as a signal, but for debugging. // Probe back through the edge fifo to see if one of the triangle edges is in the FIFO for ( ; edgeCursor >= lowestEdgeCursor; --edgeCursor ) @@ -664,7 +664,7 @@ void CompressIndexBuffer( const Ty* triangles, // we found an edge so write it out, then output the vertex if ( foundEdge ) { - output.Write( IB_CACHED_EDGE, IB_CODE_BITS ); + output.Write( IB_CACHED_EDGE, IB_VERTEX_CODE_BITS ); output.Write( ( edgesRead - 1 ) - edgeCursor, CACHED_EDGE_BITS ); const Edge& edge = edgeFifo[ edgeCursor & EDGE_FIFO_MASK ]; @@ -744,38 +744,48 @@ void CompressIndexBuffer( const Ty* triangles, } } +template +void CompressIndexBuffer( const Ty* triangles, + uint32_t triangleCount, + uint32_t* vertexRemap, + uint32_t vertexCount, + IndexBufferCompressionFormat format, + WriteBitstream& output ) +{ + output.WriteVInt( format ); + + switch ( format ) + { + case IBCF_PER_INDICE_1: + + CompressIndiceCodes1( triangles, triangleCount, vertexRemap, vertexCount, output ); + break; + + case IBCF_PER_TRIANGLE_1: + + CompressTriangleCodes1( triangles, triangleCount, vertexRemap, vertexCount, output ); + break; + } +} + void CompressIndexBuffer( const uint16_t* triangles, uint32_t triangleCount, uint32_t* vertexRemap, - uint32_t vertexCount, + uint32_t vertexCount, + IndexBufferCompressionFormat format, WriteBitstream& output ) { - CompressIndexBuffer( triangles, triangleCount, vertexRemap, vertexCount, output ); + + CompressIndexBuffer( triangles, triangleCount, vertexRemap, vertexCount, format, output ); } void CompressIndexBuffer( const uint32_t* triangles, uint32_t triangleCount, uint32_t* vertexRemap, uint32_t vertexCount, + IndexBufferCompressionFormat format, WriteBitstream& output ) { - CompressIndexBuffer( triangles, triangleCount, vertexRemap, vertexCount, output ); + CompressIndexBuffer( triangles, triangleCount, vertexRemap, vertexCount, format, output ); } -void CompressIndexBuffer2( const uint16_t* triangles, - uint32_t triangleCount, - uint32_t* vertexRemap, - uint32_t vertexCount, - WriteBitstream& output ) -{ - CompressIndexBuffer2( triangles, triangleCount, vertexRemap, vertexCount, output ); -} - -void CompressIndexBuffer2( const uint32_t* triangles, - uint32_t triangleCount, - uint32_t* vertexRemap, - uint32_t vertexCount, - WriteBitstream& output ) -{ - CompressIndexBuffer2( triangles, triangleCount, vertexRemap, vertexCount, output ); -} diff --git a/3rdparty/ib-compress/indexbuffercompression.h b/3rdparty/ib-compress/indexbuffercompression.h index 03a7f9e6..e61acb8e 100644 --- a/3rdparty/ib-compress/indexbuffercompression.h +++ b/3rdparty/ib-compress/indexbuffercompression.h @@ -27,14 +27,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #pragma once #include -#include "WriteBitstream.h" +#include "writebitstream.h" +#include "indexbuffercompressionformat.h" // Compress an index buffer, writing the results out to a bitstream and providing a vertex remapping (which will be in pre-transform cache optimised // order). // -// This version has slightly worse compression and the matching decompression has worse performance than CompressIndexBuffer2, but it supports degenerate triangles -// (that have duplicate vertex indices). Output should be decompressed with DecompressIndexBuffer. It also changes the order of the vertices in each triangle less. -// // It works by outputting a code (along with any required index symbols) per vertex. // // Parameters: @@ -44,37 +42,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // where indexing with the old vertex index will get you the new one. // It should be allocated as a with at least vertexCount entries. // [in] vertexCount - The number of vertices in the mesh. This should be less than 0xFFFFFFFF/2^32 - 1. +// [in] format - The compression format to use for encoding - note the format will be encoded with the compressed data so the decompressor can select the correct algorithm. // [in] output - The stream that the compressed data will be written to. Note that we will not flush/finish the stream // in case something else is going to be written after, so WriteBitstream::Finish will need to be called after this. -void CompressIndexBuffer( const uint16_t* triangles, uint32_t triangleCount, uint32_t* vertexRemap, uint32_t vertexCount, WriteBitstream& output ); +void CompressIndexBuffer( const uint16_t* triangles, uint32_t triangleCount, uint32_t* vertexRemap, uint32_t vertexCount, IndexBufferCompressionFormat format, WriteBitstream& output ); // Same as above but 32bit indices. -void CompressIndexBuffer( const uint32_t* triangles, uint32_t triangleCount, uint32_t* vertexRemap, uint32_t vertexCount, WriteBitstream& output ); - - -// Compress an index buffer, writing the results out to a bitstream and providing a vertex remapping (which will be in pre-transform cache optimised -// order). -// -// This version has slightly better compression and the matching decompression has better performance than CompressIndexBuffer, but it does not supports degenerate triangles -// (that have duplicate vertex indices). Output should be decompressed with DecompressIndexBuffer2. It changes the order of the vertices in each triangle more. -// -// This version also has compression optimisations that allow it to handle strip/fan cases a lot better compression wise. -// -// This works by outputting a code per triangle (along with the required index symbols). -// -// Parameters: -// [in] triangles - A typical triangle list index buffer (3 indices to vertices per triangle). -// [in] triangle count - The number of triangles to process. -// [out] vertexRemap - This will be populated with re-mappings that map old vertices to new vertices, -// where indexing with the old vertex index will get you the new one. -// It should be allocated as a with at least vertexCount entries. -// [in] vertexCount - The number of vertices in the mesh. This should be less than 0xFFFFFFFF/2^32 - 1. -// [in] output - The stream that the compressed data will be written to. Note that we will not flush/finish the stream -// in case something else is going to be written after, so WriteBitstream::Finish will need to be called after this. -void CompressIndexBuffer2( const uint16_t* triangles, uint32_t triangleCount, uint32_t* vertexRemap, uint32_t vertexCount, WriteBitstream& output ); - -// Same as above but 32bit indices -void CompressIndexBuffer2( const uint32_t* triangles, uint32_t triangleCount, uint32_t* vertexRemap, uint32_t vertexCount, WriteBitstream& output ); - +void CompressIndexBuffer( const uint32_t* triangles, uint32_t triangleCount, uint32_t* vertexRemap, uint32_t vertexCount, IndexBufferCompressionFormat format, WriteBitstream& output ); #endif // -- INDEX_BUFFER_COMPRESSION_H__ \ No newline at end of file diff --git a/3rdparty/ib-compress/indexbuffercompressionformat.h b/3rdparty/ib-compress/indexbuffercompressionformat.h new file mode 100644 index 00000000..a558d626 --- /dev/null +++ b/3rdparty/ib-compress/indexbuffercompressionformat.h @@ -0,0 +1,14 @@ +#ifndef INDEX_BUFFER_COMPRESSION_FORMAT_H__ +#define INDEX_BUFFER_COMPRESSION_FORMAT_H__ +#pragma once + +enum IndexBufferCompressionFormat +{ + // Per indice encoding - handles degenerates, but has worse compression/decompression speed and compression. + IBCF_PER_INDICE_1 = 0, + + // Per triangle encoding - better compression/speed, does not handle degenerates. + IBCF_PER_TRIANGLE_1 = 1 +}; + +#endif // -- INDEX_BUFFER_COMPRESSION_FORMAT_H__ \ No newline at end of file diff --git a/3rdparty/ib-compress/indexbufferdecompression.cpp b/3rdparty/ib-compress/indexbufferdecompression.cpp index 529b5eaa..f64be77d 100644 --- a/3rdparty/ib-compress/indexbufferdecompression.cpp +++ b/3rdparty/ib-compress/indexbufferdecompression.cpp @@ -22,13 +22,14 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "IndexBufferDecompression.h" -#include "ReadBitstream.h" -#include "IndexCompressionConstants.h" +#include "indexbufferdecompression.h" +#include "readbitstream.h" +#include "indexcompressionconstants.h" +#include "indexbuffercompressionformat.h" #include template -void DecompressIndexBuffer2( Ty* triangles, uint32_t triangleCount, ReadBitstream& input ) +void DecompressTriangleCodes1( Ty* triangles, uint32_t triangleCount, ReadBitstream& input ) { Edge edgeFifo[ EDGE_FIFO_SIZE ]; uint32_t vertexFifo[ VERTEX_FIFO_SIZE ]; @@ -341,7 +342,7 @@ void DecompressIndexBuffer2( Ty* triangles, uint32_t triangleCount, ReadBitstrea } template -void DecompressIndexBuffer( Ty* triangles, uint32_t triangleCount, ReadBitstream& input ) +void DecompressIndiceCodes1( Ty* triangles, uint32_t triangleCount, ReadBitstream& input ) { Edge edgeFifo[ EDGE_FIFO_SIZE ]; uint32_t vertexFifo[ VERTEX_FIFO_SIZE ]; @@ -359,7 +360,7 @@ void DecompressIndexBuffer( Ty* triangles, uint32_t triangleCount, ReadBitstream while ( readVertex < 3 ) { - IndexBufferCodes code = static_cast< IndexBufferCodes >( input.Read( IB_CODE_BITS ) ); + IndexBufferCodes code = static_cast< IndexBufferCodes >( input.Read( IB_VERTEX_CODE_BITS ) ); switch ( code ) { @@ -447,9 +448,24 @@ void DecompressIndexBuffer( Ty* triangles, uint32_t triangleCount, ReadBitstream } } -void DecompressIndexBuffer( uint16_t* triangles, uint32_t triangleCount, ReadBitstream& input ) +template < typename Ty > +void DecompressIndexBuffer( Ty* triangles, uint32_t triangleCount, ReadBitstream& input ) { - DecompressIndexBuffer( triangles, triangleCount, input ); + IndexBufferCompressionFormat format = static_cast< IndexBufferCompressionFormat >( input.ReadVInt() ); + + switch ( format ) + { + case IBCF_PER_INDICE_1: + + DecompressIndiceCodes1( triangles, triangleCount, input ); + break; + + case IBCF_PER_TRIANGLE_1: + + DecompressTriangleCodes1( triangles, triangleCount, input ); + break; + + } } void DecompressIndexBuffer( uint32_t* triangles, uint32_t triangleCount, ReadBitstream& input ) @@ -457,12 +473,7 @@ void DecompressIndexBuffer( uint32_t* triangles, uint32_t triangleCount, ReadBit DecompressIndexBuffer( triangles, triangleCount, input ); } -void DecompressIndexBuffer2( uint16_t* triangles, uint32_t triangleCount, ReadBitstream& input ) +void DecompressIndexBuffer( uint16_t* triangles, uint32_t triangleCount, ReadBitstream& input ) { - DecompressIndexBuffer2( triangles, triangleCount, input ); + DecompressIndexBuffer( triangles, triangleCount, input ); } - -void DecompressIndexBuffer2( uint32_t* triangles, uint32_t triangleCount, ReadBitstream& input ) -{ - DecompressIndexBuffer2( triangles, triangleCount, input ); -} \ No newline at end of file diff --git a/3rdparty/ib-compress/indexbufferdecompression.h b/3rdparty/ib-compress/indexbufferdecompression.h index 96149d3b..ffb6138c 100644 --- a/3rdparty/ib-compress/indexbufferdecompression.h +++ b/3rdparty/ib-compress/indexbufferdecompression.h @@ -27,7 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #pragma once #include -#include "ReadBitstream.h" +#include "readbitstream.h" // Compress an index buffer, writing the results out to a bitstream and providing a vertex remapping (which will be in pre-transform cache optimised // order. @@ -40,16 +40,4 @@ void DecompressIndexBuffer( uint16_t* triangles, uint32_t triangleCount, ReadBit // Same as above but 32 bit indices. void DecompressIndexBuffer( uint32_t* triangles, uint32_t triangleCount, ReadBitstream& input ); - -// Compress an index buffer, writing the results out to a bitstream and providing a vertex remapping (which will be in pre-transform cache optimised -// order. -// Parameters: -// [out] triangles - Triangle list index buffer (3 indices to vertices per triangle), output from the decompression - 16bit indices -// [in] triangle count - The number of triangles to decompress. -// [in] input - The bit stream that the compressed data will be read from. -void DecompressIndexBuffer2( uint16_t* triangles, uint32_t triangleCount, ReadBitstream& input ); - -// Same as above but 32bit indices -void DecompressIndexBuffer2( uint32_t* triangles, uint32_t triangleCount, ReadBitstream& input ); - #endif // -- INDEX_BUFFER_DECOMPRESSION_H__ \ No newline at end of file diff --git a/3rdparty/ib-compress/indexcompressionconstants.h b/3rdparty/ib-compress/indexcompressionconstants.h index 4a2afef6..24cf346f 100644 --- a/3rdparty/ib-compress/indexcompressionconstants.h +++ b/3rdparty/ib-compress/indexcompressionconstants.h @@ -29,13 +29,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include // Constant fifo and code sizes. -const int VERTEX_FIFO_SIZE = 32; -const int VERTEX_FIFO_MASK = VERTEX_FIFO_SIZE - 1; -const int EDGE_FIFO_SIZE = 32; -const int EDGE_FIFO_MASK = EDGE_FIFO_SIZE - 1; -const int CACHED_EDGE_BITS = 5; -const int CACHED_VERTEX_BITS = 5; -const int IB_CODE_BITS = 2; +const int VERTEX_FIFO_SIZE = 32; +const int VERTEX_FIFO_MASK = VERTEX_FIFO_SIZE - 1; +const int EDGE_FIFO_SIZE = 32; +const int EDGE_FIFO_MASK = EDGE_FIFO_SIZE - 1; +const int CACHED_EDGE_BITS = 5; +const int CACHED_VERTEX_BITS = 5; + +const int IB_VERTEX_CODE_BITS = 2; const int IB_TRIANGLE_CODE_BITS = 4; diff --git a/3rdparty/ib-compress/readbitstream.h b/3rdparty/ib-compress/readbitstream.h index 06cf88d3..4b6c81de 100644 --- a/3rdparty/ib-compress/readbitstream.h +++ b/3rdparty/ib-compress/readbitstream.h @@ -27,6 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #pragma once #include +#include #ifdef _MSC_VER @@ -34,7 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #else -#define RBS_INLINE __attribute__((always_inline)) +#define RBS_INLINE inline #endif diff --git a/3rdparty/ib-compress/writebitstream.h b/3rdparty/ib-compress/writebitstream.h index 0c2ed22f..35c0ac67 100644 --- a/3rdparty/ib-compress/writebitstream.h +++ b/3rdparty/ib-compress/writebitstream.h @@ -27,12 +27,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #pragma once #include +#include #include #ifdef _MSC_VER #define WBS_INLINE __forceinline #else -#define WBS_INLINE __attribute__((always_inline)) +#define WBS_INLINE inline #endif // Very simple bitstream for writing that will grow to accomodate written bits.