medfall

A super great game engine
Log | Files | Refs

commit b6efc6f10d63ccadf4ed51f1a9f60afe755598ae
parent 0aa80c3b51a15fea0c061ff523cf6e1dcb60a7e6
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Tue May 16 03:51:35 +0300

Fix alignment in renderer_ub_easy

Diffstat:
renderer.h | 35++++++++++++++++++++++++-----------
1 file changed, 24 insertions(+), 11 deletions(-)
diff --git a/renderer.h b/renderer.h @@ -204,9 +204,11 @@ Texture renderer_new_texture( TextureConfig config ); void renderer_delete_texture( Texture texture ); Mesh renderer_new_mesh( MeshConfig config ); -void renderer_draw_mesh( const Mesh & mesh, RenderState state ); void renderer_delete_mesh( const Mesh & mesh ); +void renderer_draw_mesh( const Mesh & mesh, const RenderState & state ); +void renderer_draw_instances( const Mesh & mesh, const RenderState & state, u32 num_instances, VB vb ); + /* * cheesy renderer_ub_data helper */ @@ -217,28 +219,39 @@ void renderer_delete_mesh( const Mesh & mesh ); // take up 16 bytes. template< typename T > -constexpr size_t renderer_ub_size() { - return sizeof( T ); +constexpr size_t renderer_ubo_alignment() { + return align4( sizeof( T ) ); +} + +template<> +constexpr size_t renderer_ubo_alignment< v3 >() { + return sizeof( v4 ); +} + +template< typename T > +constexpr size_t renderer_ub_size( size_t size ) { + return sizeof( T ) + align_power_of_2( size, renderer_ubo_alignment< T >() ); } template< typename S, typename T, typename... Rest > -constexpr size_t renderer_ub_size() { - return align4( sizeof( S ) ) + renderer_ub_size< T, Rest... >(); +constexpr size_t renderer_ub_size( size_t size ) { + return renderer_ub_size< T, Rest... >( sizeof( S ) + align_power_of_2( size, renderer_ubo_alignment< S >() ) ); } -inline void renderer_ub_easy_helper( const char * buf ) { } +inline void renderer_ub_easy_helper( char * buf, size_t len ) { } template< typename T, typename... Rest > -inline void renderer_ub_easy_helper( char * buf, const T & first, Rest... rest ) { - memcpy( buf, &first, sizeof( first ) ); - renderer_ub_easy_helper( buf + align4( sizeof( first ) ), rest... ); +inline void renderer_ub_easy_helper( char * buf, size_t len, const T & first, Rest... rest ) { + len = align_power_of_2( len, renderer_ubo_alignment< T >() ); + memcpy( buf + len, &first, sizeof( first ) ); + renderer_ub_easy_helper( buf, len + sizeof( first ), rest... ); } template< typename... Rest > inline void renderer_ub_easy( UB ub, Rest... rest ) { - constexpr size_t buf_size = renderer_ub_size< Rest... >(); + constexpr size_t buf_size = renderer_ub_size< Rest... >( 0 ); char buf[ buf_size ]; memset( buf, 0, sizeof( buf ) ); - renderer_ub_easy_helper( buf, rest... ); + renderer_ub_easy_helper( buf, 0, rest... ); renderer_ub_data( ub, buf, sizeof( buf ) ); }