Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ void EveChildInstancedMeshes::UpdateAudioGeometry( bool allowAudioGeometry )
}

const bool moved = m_audioWorldTransform != m_worldTransform;
if( !m_audioGeometryDirty && !m_audioGeometryPending && !moved )
const float minSize = GetEveAudioOcclusionMinSize();
const bool sizeLimitChanged = m_audioMinSize != minSize;
if( !m_audioGeometryDirty && !m_audioGeometryPending && !moved && !sizeLimitChanged )
{
return;
}
Expand Down Expand Up @@ -110,6 +112,7 @@ void EveChildInstancedMeshes::UpdateAudioGeometry( bool allowAudioGeometry )
}

m_audioWorldTransform = m_worldTransform;
m_audioMinSize = minSize;
m_audioGeometryPending = false;
for( AudioInstance& audioInstance : m_audioInstances )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ BLUE_CLASS( EveChildInstancedMeshes ) :
ITr2AudGeometryPtr m_audioGeometry;
std::vector<AudioInstance> m_audioInstances;
Matrix m_audioWorldTransform = IdentityMatrix();
float m_audioMinSize = 0.0f;
bool m_audioGeometryDirty = true;
// a geometry resource is still loading, so its instances are not registered yet
bool m_audioGeometryPending = false;
Expand Down
89 changes: 86 additions & 3 deletions trinity/Eve/SpaceObject/Children/EveChildMesh.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Copyright © 2023 CCP ehf.

#include "StdAfx.h"
Expand All @@ -23,6 +23,25 @@
{
constexpr float s_instanceScreenSizeThreshold = 1.f;

bool IsAffineTransform( const Matrix& m )
{
return m._14 == 0.f && m._24 == 0.f && m._34 == 0.f && m._44 == 1.f;
}

uint64_t GetAudioGeometryId( TriGeometryRes* geometry, unsigned int meshIndex )
{
if( !geometry || !geometry->IsGood() )
{
return 0;
}
const AudioGeometryResData* audioGeometry = geometry->GetAudioGeometry( meshIndex );
if( !audioGeometry )
{
return 0;
}
return audioGeometry->m_id;
}

}

Tr2SuballocatedBuffer g_bakedMorphTargetBuffer( "Baked Morph Target Buffer", Tr2GpuUsage::SHADER_RESOURCE | Tr2GpuUsage::UNORDERED_ACCESS, SHARED_BUFFER_BLOCK_SIZE, SHARED_BUFFER_MAX_SIZE );
Expand Down Expand Up @@ -850,6 +869,7 @@
void EveChildMesh::SetInstanceTransforms( std::vector<Matrix> instances )
{
m_instanceTransforms = instances;
m_audioInstancesDirty = true;
}

float EveChildMesh::GetSortValue()
Expand Down Expand Up @@ -989,7 +1009,7 @@
}
m_reverseWinding = IsMirrored( m_worldTransform );

m_audioOccluder.SetTransform( m_worldTransform );
UpdateAudioGeometryTransforms();

// need to update the data we get from the parent to be relevant to us!
m_parentOverlayEffects = nullptr;
Expand Down Expand Up @@ -1238,20 +1258,83 @@
{
if( !allowAudioGeometry || !g_eveIsAudioOcclusionGeometryEnabled || !m_mesh )
{
m_audioOccluder.Release();
UnregisterAudioGeometry();
return;
}

if( !m_audioGeometry )
{
m_audioGeometry = EveAudioOccluder::CreateAudioGeometry();
}
m_audioOccluder.Update( m_audioGeometry, m_mesh->GetGeometryResource(), m_mesh->GetMeshIndex(), m_worldTransform );

if( !m_audioGeometry )
{
UnregisterAudioGeometry();
return;
}

if( !m_instancedMesh )
{
m_audioInstanceOccluders.clear();
m_audioInstancesDirty = true;
m_audioOccluder.Update( m_audioGeometry, m_mesh->GetGeometryResource(), m_mesh->GetMeshIndex(), m_worldTransform );
return;
}

m_audioOccluder.Release();

TriGeometryRes* geometry = m_mesh->GetGeometryResource();
const uint64_t geometryId = GetAudioGeometryId( geometry, m_mesh->GetMeshIndex() );
const float minSize = GetEveAudioOcclusionMinSize();
const bool changed = m_audioInstancesDirty || m_audioInstancesGeometryId != geometryId || m_audioInstancesMinSize != minSize
|| m_audioInstancesRegisteredTransform != m_worldTransform;
Comment on lines +1289 to +1290

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-format suggestion

Suggested change
const bool changed = m_audioInstancesDirty || m_audioInstancesGeometryId != geometryId || m_audioInstancesMinSize != minSize
|| m_audioInstancesRegisteredTransform != m_worldTransform;
const bool changed = m_audioInstancesDirty || m_audioInstancesGeometryId != geometryId || m_audioInstancesMinSize != minSize || m_audioInstancesRegisteredTransform != m_worldTransform;

if( !changed )
{
return;
}

m_audioInstancesDirty = false;
m_audioInstancesGeometryId = geometryId;
m_audioInstancesMinSize = minSize;
m_audioInstancesRegisteredTransform = m_worldTransform;
m_audioInstancesPlacedTransform = m_worldTransform;

m_audioInstanceOccluders.resize( m_instanceTransforms.size() );
for( size_t i = 0; i < m_audioInstanceOccluders.size(); ++i )
{
if( IsAffineTransform( m_instanceTransforms[i] ) )
{
m_audioInstanceOccluders[i].Update( m_audioGeometry, geometry, m_mesh->GetMeshIndex(), m_instanceTransforms[i] * m_worldTransform );
}
else
{
m_audioInstanceOccluders[i].Release();
}
}
}

void EveChildMesh::UpdateAudioGeometryTransforms()
{
m_audioOccluder.SetTransform( m_worldTransform );

if( m_audioInstanceOccluders.empty() || m_audioInstancesPlacedTransform == m_worldTransform )
{
return;
}
m_audioInstancesPlacedTransform = m_worldTransform;

const size_t count = std::min( m_audioInstanceOccluders.size(), m_instanceTransforms.size() );
for( size_t i = 0; i < count; ++i )
{
m_audioInstanceOccluders[i].SetTransform( m_instanceTransforms[i] * m_worldTransform );
}
}

void EveChildMesh::UnregisterAudioGeometry()
{
m_audioOccluder.Release();
m_audioInstanceOccluders.clear();
m_audioInstancesDirty = true;
}

void EveChildMesh::SetOrigin( Origin origin )
Expand Down
13 changes: 12 additions & 1 deletion trinity/Eve/SpaceObject/Children/EveChildMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,19 @@ BLUE_CLASS( EveChildMesh ) :

ITr2AudGeometryPtr m_audioGeometry;
EveAudioOccluder m_audioOccluder;

// One per instance transform, in the same order
std::vector<EveAudioOccluder> m_audioInstanceOccluders;
Matrix m_audioInstancesRegisteredTransform = IdentityMatrix();
Matrix m_audioInstancesPlacedTransform = IdentityMatrix();
uint64_t m_audioInstancesGeometryId = 0;
float m_audioInstancesMinSize = 0.0f;
bool m_audioInstancesDirty = true;

// An instanced mesh gets one occluder per affine instance transform and none without instances. Its occluders are
// only updated when the child, instances, geometry or size limit changed, and retried until carbon-audio loads.
void UpdateAudioGeometry( bool allowAudioGeometry );
// Moves the occluders with the child. New instance transforms are registered by UpdateAudioGeometry().
void UpdateAudioGeometryTransforms();
void UnregisterAudioGeometry();

void UpdateRtMesh();
Expand Down
1 change: 1 addition & 0 deletions trinity/Eve/SpaceObject/EveSpaceObject2_Blue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ const Be::ClassInfo* EveSpaceObject2::ExposeToBlue()
Be::READ | Be::PERSIST )

MAP_PROPERTY( "audioGeometry", GetAudioGeometry, SetAudioGeometry, "Audio geometry interface for Wwise Spatial Audio occlusion and diffraction processing." )
MAP_ATTRIBUTE( "isAudioOccluder", m_isAudioOccluder, "Whether this object and the meshes it carries block sound for Wwise Spatial Audio. False for ships, which only listen; a ship drawn with another model class, such as a Creation, gets it set by the game.", Be::READWRITE )

MAP_ATTRIBUTE( "impactOverlay", m_impactOverlay, "object for rendering damage/impact fx on this space object.", Be::READWRITE | Be::PERSIST )
MAP_ATTRIBUTE( "decals", m_decals, "list of all decals on this space object.", Be::READ | Be::PERSIST )
Expand Down
35 changes: 34 additions & 1 deletion trinity/Eve/SpaceObject/Utils/EveAudioOccluder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,44 @@
#include "StdAfx.h"
#include "EveAudioOccluder.h"
#include "Resources/TriGeometryRes.h"
#include "TriSettingsRegistrar.h"

#include <algorithm>
#include <atomic>
#include <limits>

static float s_eveAudioOcclusionMinSize = 500.0f;
TRI_REGISTER_SETTING( "eveAudioOcclusionMinSize", s_eveAudioOcclusionMinSize );

static std::atomic<uint64_t> s_nextInstanceId{ 1 };

float GetEveAudioOcclusionMinSize()
{
if( s_eveAudioOcclusionMinSize > 0.0f )
{
return s_eveAudioOcclusionMinSize;
}
return 0.0f;
}

static float OccluderSize( const Tr2AudGeometryData& data, const Matrix& m )
{
const Vector3 box = data.m_maxBounds - data.m_minBounds;
const bool hasBounds = box.x >= 0.0f && box.y >= 0.0f && box.z >= 0.0f;
if( !hasBounds )
{
return std::numeric_limits<float>::max();
}

float sides[3] = {
box.x * Length( Vector3( m._11, m._12, m._13 ) ),
box.y * Length( Vector3( m._21, m._22, m._23 ) ),
box.z * Length( Vector3( m._31, m._32, m._33 ) ),
};
std::sort( sides, sides + 3 );
return sides[1];
}

EveAudioOccluder::EveAudioOccluder() :
m_instanceId( s_nextInstanceId++ )
{
Expand Down Expand Up @@ -59,7 +92,7 @@ void EveAudioOccluder::Update( ITr2AudGeometry* audio, TriGeometryRes* geometry,
audioGeometry = geometry->GetAudioGeometry( meshIndex );
}

if( !audioGeometry )
if( !audioGeometry || OccluderSize( audioGeometry->m_data, worldTransform ) < GetEveAudioOcclusionMinSize() )
{
Release();
return;
Expand Down
7 changes: 6 additions & 1 deletion trinity/Eve/SpaceObject/Utils/EveAudioOccluder.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

BLUE_DECLARE( TriGeometryRes );

// The eveAudioOcclusionMinSize setting in meters, 0 (no limit) when not positive. A mesh's size is the middle side of
// its world bounding box, so long thin meshes count as small. Meshes with unset bounds always pass.
float GetEveAudioOcclusionMinSize();

// Represents one instance of a mesh's audio geometry in carbon-audio and keeps its transform up to date.
class EveAudioOccluder
{
Expand All @@ -19,7 +23,8 @@ class EveAudioOccluder
EveAudioOccluder& operator=( const EveAudioOccluder& ) = delete;

// Registers the mesh once its audio geometry is loaded and registers it again when the resource reloads.
// Releases when there is nothing to register. Reads the resource, so call it from the synchronous update.
// Releases when there is nothing to register or the mesh is smaller than GetEveAudioOcclusionMinSize().
// Reads the resource, so call it from the synchronous update.
void Update( ITr2AudGeometry* audio, TriGeometryRes* geometry, unsigned int meshIndex, const Matrix& worldTransform );

// Moves the registered instance. Does not touch the resource.
Expand Down
5 changes: 5 additions & 0 deletions trinity/Resources/TriGeometryRes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,11 @@ static std::unique_ptr<AudioGeometryResData> ExtractAudioGeometry( Tr2CmfContent
Tr2AudGeometryData& data = audioGeometry->m_data;

const cmf::AudioOcclusionMesh& occlusionMesh = cmfMesh.audioOcclusionMesh;
if( occlusionMesh.vertices.empty() && occlusionMesh.indices.empty() && occlusionMesh.bounds.IsInitialized() )
{
return nullptr;
}

if( !occlusionMesh.vertices.empty() && !occlusionMesh.indices.empty() )
{
data.m_vertices.assign( occlusionMesh.vertices.begin(), occlusionMesh.vertices.end() );
Expand Down
1 change: 1 addition & 0 deletions trinity/Resources/TriGeometryRes.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ BLUE_CLASS( TriGeometryRes ) :
int GetLodIndexForScreenSize( unsigned int meshIx, float screenSize ) const;


// Null when the mesh has none, including when the CMF marks it as not occluding (bounds, no mesh).
const AudioGeometryResData* GetAudioGeometry( unsigned int meshIx ) const;

unsigned int GetSkeletonCount() const;
Expand Down
Loading