RuntimeMeshComponent-Examples icon indicating copy to clipboard operation
RuntimeMeshComponent-Examples copied to clipboard

Examples should call URuntimeMeshProvider::Shutdown in Actor EndPlay

Open connorjak opened this issue 4 years ago • 0 comments

Not doing so can result in the URuntimeMesh's GPU buffers sticking around for quite a while until the URuntimeMesh finally gets garbage-collected for being unreachable.


Copy-pasted from my comment on the Discord:

I fixed my DEVICE REMOVED, REASON: HUNG issues! As it turns out, the symptom of the error was a VRAM overflow. I was destroying the actor that held the mesh immediately when it wasn't required. The URuntimeMesh object that a URuntimeMeshProvider subclass maintains wasn't being Reset() until the garbage collector eventually got around to deleting unreachable objects (happens infrequently). So, between garbage collections, I was basically leaking VRAM at 100s of MB/s when being aggressive with it.

I fixed this by calling URuntimeMeshProvider::Shutdown() on my provider (which ends up calling URuntimeMesh::Reset() during its corresponding actor's EndPlay().

//.h
UCLASS()
class SPACECRAFT_API APlanetQuadtreeSurface : public AActor
{
	GENERATED_BODY()

private:
	URuntimeMeshProviderCollision* collisionProvider;

	//NOTE: can move back to public if necessary
	PlanetContext context;

public:
	UPROPERTY(VisibleAnywhere)
	URuntimeMeshComponent* runtimeMeshComp;

	UPROPERTY(VisibleAnywhere)
	URuntimeMesh* runtimeMesh;

	UPROPERTY(VisibleAnywhere)
	UPlanetSurfaceMeshProvider* surfaceProvider;

//.cpp

void APlanetQuadtreeSurface::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	Super::EndPlay(EndPlayReason);

	// Deallocating the mesh buffers is something we can't wait on the
	// UObject garbage collection to do.
	surfaceProvider->Shutdown();

}

connorjak avatar Jun 18 '21 02:06 connorjak