Skip to content

Commit

Permalink
Compressed vertex from 64 to 32 bytes
Browse files Browse the repository at this point in the history
Normals to 4 bytes octohedral
  • Loading branch information
sultim-t committed Nov 12, 2023
1 parent 222e611 commit e4d250a
Show file tree
Hide file tree
Showing 16 changed files with 372 additions and 168 deletions.
13 changes: 8 additions & 5 deletions Include/RTGL1/RTGL1.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ typedef struct RgViewport
} RgViewport;

typedef uint32_t RgColor4DPacked32;
typedef uint32_t RgNormalPacked32;



Expand Down Expand Up @@ -366,11 +367,11 @@ typedef uint32_t RgMeshInfoFlags;

typedef struct RgPrimitiveVertex
{
float position[ 3 ]; uint32_t _padding0;
float normal[ 3 ]; uint32_t _padding1;
float tangent[ 4 ];
float texCoord[ 2 ];
RgColor4DPacked32 color; uint32_t _padding2;
float position[ 3 ];
RgNormalPacked32 normalPacked;
float texCoord[ 2 ];
RgColor4DPacked32 color;
uint32_t _pad0;
} RgPrimitiveVertex;

// Can be linked after RgMeshPrimitiveInfo.
Expand Down Expand Up @@ -1106,6 +1107,7 @@ typedef RgBool32 ( RGAPI_PTR* PFN_rgUtilIsUpscaleTechniqueAvailable
typedef const char* ( RGAPI_PTR* PFN_rgUtilGetResultDescription )( RgResult result );
typedef RgColor4DPacked32 ( RGAPI_PTR* PFN_rgUtilPackColorByte4D )( uint8_t r, uint8_t g, uint8_t b, uint8_t a );
typedef RgColor4DPacked32 ( RGAPI_PTR* PFN_rgUtilPackColorFloat4D )( float r, float g, float b, float a );
typedef RgNormalPacked32 ( RGAPI_PTR* PFN_rgUtilPackNormal )( float x, float y, float z );
typedef void ( RGAPI_PTR* PFN_rgUtilExportAsTGA )( const void* pPixels, uint32_t width, uint32_t height, const char* pPath );


Expand Down Expand Up @@ -1142,6 +1144,7 @@ typedef struct RgInterface
PFN_rgUtilGetResultDescription rgUtilGetResultDescription;
PFN_rgUtilPackColorByte4D rgUtilPackColorByte4D;
PFN_rgUtilPackColorFloat4D rgUtilPackColorFloat4D;
PFN_rgUtilPackNormal rgUtilPackNormal;
PFN_rgUtilExportAsTGA rgUtilExportAsTGA;
} RgInterface;

Expand Down
30 changes: 21 additions & 9 deletions Source/ASManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@

namespace
{
constexpr uint32_t AdditionalTexCoordMaxCount = MAX_STATIC_VERTEX_COUNT;

bool IsFastBuild( RTGL1::VertexCollectorFilterTypeFlags filter )
{
using FT = RTGL1::VertexCollectorFilterTypeFlagBits;
Expand Down Expand Up @@ -107,20 +105,34 @@ RTGL1::ASManager::ASManager( VkDevice _device,
{
const uint32_t maxVertsPerLayer[] = {
MAX_STATIC_VERTEX_COUNT,
_enableTexCoordLayer1 ? AdditionalTexCoordMaxCount : 0,
_enableTexCoordLayer2 ? AdditionalTexCoordMaxCount : 0,
_enableTexCoordLayer3 ? AdditionalTexCoordMaxCount : 0,
_enableTexCoordLayer1 ? uint32_t{ MAX_STATIC_VERTEX_COUNT } : 0,
_enableTexCoordLayer2 ? uint32_t{ MAX_STATIC_VERTEX_COUNT } : 0,
_enableTexCoordLayer3 ? uint32_t{ MAX_STATIC_VERTEX_COUNT } : 0,
};

collectorStatic = std::make_unique< VertexCollector >(
device, *allocator, maxVertsPerLayer, false, "Static" );
}
{
const uint32_t maxVertsPerLayer[] = {
MAX_DYNAMIC_VERTEX_COUNT,
_enableTexCoordLayer1 ? uint32_t{ MAX_DYNAMIC_VERTEX_COUNT } : 0,
_enableTexCoordLayer2 ? uint32_t{ MAX_DYNAMIC_VERTEX_COUNT } : 0,
_enableTexCoordLayer3 ? uint32_t{ MAX_DYNAMIC_VERTEX_COUNT } : 0,
};

for( auto& c : collectorDynamic )
{
c = collectorDynamic[ 0 ] ? VertexCollector::CreateWithSameDeviceLocalBuffers(
*( collectorDynamic[ 0 ] ), *allocator, "Dynamic" )
: std::make_unique< VertexCollector >(
device, *allocator, maxVertsPerLayer, true, "Dynamic" );
if( !collectorDynamic[ 0 ] )
{
c = std::make_unique< VertexCollector >(
device, *allocator, maxVertsPerLayer, true, "Dynamic" );
continue;
}

// share buffers with 0
c = VertexCollector::CreateWithSameDeviceLocalBuffers(
*( collectorDynamic[ 0 ] ), *allocator, "Dynamic" );
}
}

Expand Down
9 changes: 4 additions & 5 deletions Source/Generated/GenerateShaderCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,11 @@ def evalConst():
# then it'll be represented as an array with size (count*dimensions).

VERTEX_STRUCT = [
(TYPE_FLOAT32, 4, "position", 1),
(TYPE_FLOAT32, 4, "normal", 1),
(TYPE_FLOAT32, 4, "tangent", 1),
(TYPE_FLOAT32, 3, "position", 1),
(TYPE_UINT32 , 1, "normalPacked", 1),
(TYPE_FLOAT32, 2, "texCoord", 1),
(TYPE_UINT32, 1, "color", 1),
(TYPE_UINT32, 1, "_padding", 1),
(TYPE_UINT32, 1, "_pad0", 1),
]

# Must be careful with std140 offsets! They are set manually.
Expand Down Expand Up @@ -710,7 +709,7 @@ def evalConst():
# breakType -- if member's type is not primitive and its count>0 then
# it'll be represented as an array of primitive types
STRUCTS = {
"ShVertex": (VERTEX_STRUCT, False, STRUCT_ALIGNMENT_STD430, 0),
"ShVertex": (VERTEX_STRUCT, False, STRUCT_ALIGNMENT_STD140, 0),
"ShGlobalUniform": (GLOBAL_UNIFORM_STRUCT, False, STRUCT_ALIGNMENT_STD140, STRUCT_BREAK_TYPE_ONLY_C),
"ShGeometryInstance": (GEOM_INSTANCE_STRUCT, False, STRUCT_ALIGNMENT_STD430, 0),
"ShTonemapping": (TONEMAPPING_STRUCT, False, 0, 0),
Expand Down
7 changes: 3 additions & 4 deletions Source/Generated/ShaderCommonC.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,11 @@ namespace RTGL1

struct ShVertex
{
float position[4];
float normal[4];
float tangent[4];
float position[3];
uint32_t normalPacked;
float texCoord[2];
uint32_t color;
uint32_t _padding;
uint32_t _pad0;
};

struct ShGlobalUniform
Expand Down
7 changes: 3 additions & 4 deletions Source/Generated/ShaderCommonGLSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,11 @@

struct ShVertex
{
vec4 position;
vec4 normal;
vec4 tangent;
vec3 position;
uint normalPacked;
vec2 texCoord;
uint color;
uint _padding;
uint _pad0;
};

struct ShGlobalUniform
Expand Down
88 changes: 44 additions & 44 deletions Source/GltfExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,19 @@ std::string GetGltfBinURI( const std::filesystem::path& gltfPath )
}


auto MakeWeghtedNormal( const RgPrimitiveVertex& v0,
const RgPrimitiveVertex& v1,
const RgPrimitiveVertex& v2 ) -> RgFloat3D
// Need because of the packed normals..
struct RgPrimitiveVertex_Unpacked
{
float position[ 3 ];
float normal[ 3 ];
float texCoord[ 2 ];
RgColor4DPacked32 color;
};


auto MakeWeghtedNormal( const RgPrimitiveVertex_Unpacked& v0,
const RgPrimitiveVertex_Unpacked& v1,
const RgPrimitiveVertex_Unpacked& v2 ) -> RgFloat3D
{
const float* a = v0.position;
const float* b = v1.position;
Expand All @@ -149,9 +159,9 @@ auto MakeWeghtedNormal( const RgPrimitiveVertex& v0,
return RTGL1::Utils::Cross( e1, e2 );
}

void CalculateNormals( std::vector< RgPrimitiveVertex >& verts,
std::vector< uint32_t >& indices,
bool invert )
void CalculateNormals( std::vector< RgPrimitiveVertex_Unpacked >& verts,
std::vector< uint32_t >& indices,
bool invert )
{
auto triCount = [ & ]() {
return indices.empty() ? verts.size() / 3 : indices.size() / 3;
Expand All @@ -164,7 +174,7 @@ void CalculateNormals( std::vector< RgPrimitiveVertex >& verts,
indices[ tri * 3 + 2 ] };
};

auto sumNormal = []( RgPrimitiveVertex& v, const RgFloat3D& n ) {
auto sumNormal = []( RgPrimitiveVertex_Unpacked& v, const RgFloat3D& n ) {
v.normal[ 0 ] += n.data[ 0 ];
v.normal[ 1 ] += n.data[ 1 ];
v.normal[ 2 ] += n.data[ 2 ];
Expand Down Expand Up @@ -244,19 +254,29 @@ struct DeepCopyOfPrimitive
extAttachedLight = *e;
}

std::span fromVertices( c.pVertices, c.vertexCount );
std::span fromIndices( c.pIndices, c.indexCount );
auto fromVertices = std::span{ c.pVertices, c.vertexCount };
auto fromIndices = std::span{ c.pIndices, c.indexCount };
// for copying
static_assert(
std::is_same_v< decltype( c.pVertices ), decltype( pVertices )::const_pointer > );
static_assert(
std::is_same_v< decltype( c.pIndices ), decltype( pIndices )::const_pointer > );
static_assert( std::is_trivially_copyable_v< decltype( pVertices )::value_type > );
static_assert( std::is_trivially_copyable_v< decltype( pIndices )::value_type > );

// deep copy
pTextureName = Utils::SafeCstr( c.pTextureName );
pVertices.assign( fromVertices.begin(), fromVertices.end() );
pVertices.resize( fromVertices.size() );
for( size_t i = 0; i < fromVertices.size(); i++ )
{
const RgPrimitiveVertex& src = fromVertices[ i ];
const RgFloat3D normalUnpacked = Utils::UnpackNormal( src.normalPacked );

pVertices[ i ] = RgPrimitiveVertex_Unpacked{
.position = { RG_ACCESS_VEC3( src.position ) },
.normal = { RG_ACCESS_VEC3( normalUnpacked.data ) },
.texCoord = { RG_ACCESS_VEC2( src.texCoord ) },
.color = src.color,
};
}
pIndices.assign( fromIndices.begin(), fromIndices.end() );

if( !( c.flags & RG_MESH_PRIMITIVE_DONT_GENERATE_NORMALS ) )
Expand Down Expand Up @@ -312,7 +332,7 @@ struct DeepCopyOfPrimitive
DeepCopyOfPrimitive( const DeepCopyOfPrimitive& other ) = delete;
DeepCopyOfPrimitive& operator=( const DeepCopyOfPrimitive& other ) = delete;

std::span< const RgPrimitiveVertex > Vertices() const
std::span< const RgPrimitiveVertex_Unpacked > Vertices() const
{
return { pVertices.begin(), pVertices.end() };
}
Expand Down Expand Up @@ -345,7 +365,7 @@ struct DeepCopyOfPrimitive
static void FixupPointers( DeepCopyOfPrimitive& inout )
{
inout.info.pTextureName = inout.pTextureName.data();
inout.info.pVertices = inout.pVertices.data();
inout.info.pVertices = nullptr; // because of RgPrimitiveVertex_Unpacked
inout.info.pIndices = inout.pIndices.data();

assert( inout.info.vertexCount == inout.pVertices.size() );
Expand Down Expand Up @@ -375,9 +395,9 @@ struct DeepCopyOfPrimitive
std::optional< RgMeshPrimitiveAttachedLightEXT > extAttachedLight;

// to maintain lifetimes
std::string pTextureName;
std::vector< RgPrimitiveVertex > pVertices;
std::vector< uint32_t > pIndices;
std::string pTextureName;
std::vector< RgPrimitiveVertex_Unpacked > pVertices;
std::vector< uint32_t > pIndices;
};
}

Expand Down Expand Up @@ -478,7 +498,7 @@ auto MakeAccessors( size_t vertexCount,
.component_type = cgltf_component_type_r_32f,
.normalized = false,
.type = cgltf_type_vec3,
.offset = offsetof( RgPrimitiveVertex, position ),
.offset = offsetof( RgPrimitiveVertex_Unpacked, position ),
.count = vertexCount,
.buffer_view = &correspondingViews[ BUFFER_VIEW_VERTICES ],
.has_min = false,
Expand All @@ -492,57 +512,43 @@ auto MakeAccessors( size_t vertexCount,
.component_type = cgltf_component_type_r_32f,
.normalized = false,
.type = cgltf_type_vec3,
.offset = offsetof( RgPrimitiveVertex, normal ),
.offset = offsetof( RgPrimitiveVertex_Unpacked, normal ),
.count = vertexCount,
.buffer_view = &correspondingViews[ BUFFER_VIEW_VERTICES ],
.has_min = true,
.min = { -1.f, -1.f, -1.f },
.has_max = true,
.max = { 1.f, 1.f, 1.f },
},
#define ACCESSOR_TANGENT 2
cgltf_accessor{
.name = nullptr,
.component_type = cgltf_component_type_r_32f,
.normalized = false,
.type = cgltf_type_vec4,
.offset = offsetof( RgPrimitiveVertex, tangent ),
.count = vertexCount,
.buffer_view = &correspondingViews[ BUFFER_VIEW_VERTICES ],
.has_min = true,
.min = { -1.f, -1.f, -1.f, -1.f },
.has_max = true,
.max = { 1.f, 1.f, 1.f, 1.f },
},
#define ACCESSOR_TEXCOORD 3
#define ACCESSOR_TEXCOORD 2
cgltf_accessor{
.name = nullptr,
.component_type = cgltf_component_type_r_32f,
.normalized = false,
.type = cgltf_type_vec2,
.offset = offsetof( RgPrimitiveVertex, texCoord ),
.offset = offsetof( RgPrimitiveVertex_Unpacked, texCoord ),
.count = vertexCount,
.buffer_view = &correspondingViews[ BUFFER_VIEW_VERTICES ],
.has_min = false,
.min = {},
.has_max = false,
.max = {},
},
#define ACCESSOR_COLOR 4
#define ACCESSOR_COLOR 3
cgltf_accessor{
.name = nullptr,
.component_type = cgltf_component_type_r_8u,
.normalized = false,
.type = cgltf_type_vec4,
.offset = offsetof( RgPrimitiveVertex, color ),
.offset = offsetof( RgPrimitiveVertex_Unpacked, color ),
.count = vertexCount,
.buffer_view = &correspondingViews[ BUFFER_VIEW_VERTICES ],
.has_min = false,
.min = {},
.has_max = false,
.max = {},
},
#define ACCESSOR_INDEX 5
#define ACCESSOR_INDEX 4
cgltf_accessor{
.name = nullptr,
.component_type = cgltf_component_type_r_32u,
Expand Down Expand Up @@ -586,12 +592,6 @@ auto MakeVertexAttributes( std::span< cgltf_accessor > correspondingAccessors )
.index = 0,
.data = &correspondingAccessors[ ACCESSOR_NORMAL ],
},
cgltf_attribute{
.name = const_cast< char* >( "TANGENT" ),
.type = cgltf_attribute_type_tangent,
.index = 0,
.data = &correspondingAccessors[ ACCESSOR_TANGENT ],
},
cgltf_attribute{
.name = const_cast< char* >( "TEXCOORD_0" ),
.type = cgltf_attribute_type_texcoord,
Expand Down
Loading

0 comments on commit e4d250a

Please sign in to comment.