include/extensions/PxParticleExt.h

File members: include/extensions/PxParticleExt.h

// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 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.
//
// Copyright (c) 2008-2023 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.

#ifndef PX_PARTICLE_EXT_H
#define PX_PARTICLE_EXT_H
#include "PxParticleSystem.h"
#include "PxParticleBuffer.h"
#include "foundation/PxArray.h"
#include "foundation/PxHashMap.h"
#include "foundation/PxUserAllocated.h"
#include "PxAttachment.h"

#if !PX_DOXYGEN
namespace physx
{
#endif

namespace ExtGpu
{

struct PxParticleBufferDesc
{
    PxVec4* positions;
    PxVec4* velocities;
    PxU32*  phases;
    PxParticleVolume* volumes;
    PxU32   numActiveParticles;
    PxU32   maxParticles;
    PxU32   numVolumes;
    PxU32   maxVolumes;

    PxParticleBufferDesc() : positions(NULL), velocities(NULL), phases(NULL), volumes(NULL), numActiveParticles(0), maxParticles(0), numVolumes(0), maxVolumes(0) { }
};

struct PxParticleAndDiffuseBufferDesc : public PxParticleBufferDesc
{
    PxDiffuseParticleParams diffuseParams;
    PxU32 maxDiffuseParticles;
    PxU32 maxActiveDiffuseParticles;

    PxParticleAndDiffuseBufferDesc() : PxParticleBufferDesc() { }
};

struct PxParticleRigidDesc
{
    PxParticleRigidDesc() : rigidOffsets(NULL), rigidCoefficients(NULL), rigidTranslations(NULL), rigidRotations(NULL),
        rigidLocalPositions(NULL), rigidLocalNormals(NULL), maxRigids(0), numActiveRigids(0) { }

    PxU32*      rigidOffsets;
    PxReal*     rigidCoefficients;
    PxVec4*     rigidTranslations;
    PxQuat*     rigidRotations;
    PxVec4*     rigidLocalPositions;
    PxVec4*     rigidLocalNormals;
    PxU32       maxRigids;
    PxU32       numActiveRigids;
};

class PxParticleClothBufferHelper
{
public:
    virtual void release() = 0;

    virtual PxU32 getMaxCloths() const = 0;
    virtual PxU32 getNumCloths() const = 0;
    virtual PxU32 getMaxSprings() const = 0;
    virtual PxU32 getNumSprings() const = 0;
    virtual PxU32 getMaxTriangles() const = 0;
    virtual PxU32 getNumTriangles() const = 0;
    virtual PxU32 getMaxParticles() const = 0;
    virtual PxU32 getNumParticles() const = 0;

    virtual void addCloth(const PxParticleCloth& particleCloth,
        const PxU32* triangles, const PxU32 numTriangles,
        const PxParticleSpring* springs, const PxU32 numSprings, const PxVec4* restPositions, const PxU32 numParticles) = 0;

    virtual void addCloth(const PxReal blendScale, const PxReal restVolume, const PxReal pressure,
        const PxU32* triangles, const PxU32 numTriangles,
        const PxParticleSpring* springs, const PxU32 numSprings,
        const PxVec4* restPositions, const PxU32 numParticles) = 0;

    virtual PxParticleClothDesc& getParticleClothDesc() = 0;

protected:
    virtual ~PxParticleClothBufferHelper() {}
};

struct PxParticleVolumeMesh
{
    PxU32 startIndex;
    PxU32 count;
};

class PxParticleVolumeBufferHelper
{
public:
    virtual void release() = 0;

    virtual PxU32 getMaxVolumes() const = 0;
    virtual PxU32 getNumVolumes() const = 0;
    virtual PxU32 getMaxTriangles() const = 0;
    virtual PxU32 getNumTriangles() const = 0;

    virtual PxParticleVolume* getParticleVolumes() = 0;
    virtual PxParticleVolumeMesh* getParticleVolumeMeshes() = 0;
    virtual PxU32* getTriangles() = 0;

    virtual void addVolume(const PxParticleVolume& volume, const PxParticleVolumeMesh& volumeMesh, const PxU32* triangles, const PxU32 numTriangles) = 0;

    virtual void addVolume(const PxU32 particleOffset, const PxU32 numParticles, const PxU32* triangles, const PxU32 numTriangles) = 0;

protected:
    virtual ~PxParticleVolumeBufferHelper() {}
};

class PxParticleRigidBufferHelper
{
public:
    virtual void release() = 0;

    virtual PxU32 getMaxRigids() const = 0;
    virtual PxU32 getNumRigids() const = 0;
    virtual PxU32 getMaxParticles() const = 0;
    virtual PxU32 getNumParticles() const = 0;

    virtual void addRigid(const PxVec3& translation, const PxQuat& rotation, const PxReal coefficient,
        const PxVec4* localPositions, const PxVec4* localNormals, PxU32 numParticles) = 0;

    virtual PxParticleRigidDesc& getParticleRigidDesc() = 0;

protected:
    virtual ~PxParticleRigidBufferHelper() {}
};

class PxParticleAttachmentBuffer : public PxUserAllocated
{
    PxArray<PxParticleRigidAttachment> mAttachments;
    PxArray<PxParticleRigidFilterPair> mFilters;
    PxHashMap<PxRigidActor*, PxU32> mReferencedBodies;
    PxArray<PxRigidActor*> mNewReferencedBodies;
    PxArray<PxRigidActor*> mDestroyedRefrencedBodies;

    PxParticleBuffer& mParticleBuffer;

    PxParticleRigidAttachment* mDeviceAttachments;
    PxParticleRigidFilterPair* mDeviceFilters;
    PxU32 mNumDeviceAttachments;
    PxU32 mNumDeviceFilters;

    PxCudaContextManager* mCudaContextManager;

    PxParticleSystem& mParticleSystem;

    bool mDirty;

    PX_NOCOPY(PxParticleAttachmentBuffer)

public:

    PxParticleAttachmentBuffer(PxParticleBuffer& particleBuffer, PxParticleSystem& particleSystem);

    ~PxParticleAttachmentBuffer();

    // adds attachment to attachment buffer - localPose is in actor space for attachments to all types of rigids.
    void addRigidAttachment(PxRigidActor* rigidBody, const PxU32 particleID, const PxVec3& localPose, PxConeLimitedConstraint* coneLimit = NULL);
    bool removeRigidAttachment(PxRigidActor* rigidBody, const PxU32 particleID);
    void addRigidFilter(PxRigidActor* rigidBody, const PxU32 particleID);
    bool removeRigidFilter(PxRigidActor* rigidBody, const PxU32 particleID);

    void copyToDevice(CUstream stream = 0);
};

PxParticleRigidBufferHelper*            PxCreateParticleRigidBufferHelper(PxU32 maxRigids, PxU32 maxParticles, PxCudaContextManager* cudaContextManager);

PxParticleClothBufferHelper*            PxCreateParticleClothBufferHelper(const PxU32 maxCloths, const PxU32 maxTriangles, const PxU32 maxSprings, const PxU32 maxParticles, PxCudaContextManager* cudaContextManager);

PxParticleVolumeBufferHelper*           PxCreateParticleVolumeBufferHelper(PxU32 maxVolumes, PxU32 maxTriangles, PxCudaContextManager* cudaContextManager);

PxParticleAttachmentBuffer*             PxCreateParticleAttachmentBuffer(PxParticleBuffer& particleBuffer, PxParticleSystem& particleSystem);

PxParticleBuffer*                       PxCreateAndPopulateParticleBuffer(const ExtGpu::PxParticleBufferDesc& desc, PxCudaContextManager* cudaContextManager);

PxParticleAndDiffuseBuffer*             PxCreateAndPopulateParticleAndDiffuseBuffer(const ExtGpu::PxParticleAndDiffuseBufferDesc& desc, PxCudaContextManager* cudaContextManager);

PxParticleClothBuffer*                  PxCreateAndPopulateParticleClothBuffer(const ExtGpu::PxParticleBufferDesc& desc, const PxParticleClothDesc& clothDesc,
                                            PxPartitionedParticleCloth& output, PxCudaContextManager* cudaContextManager);

PxParticleRigidBuffer*                  PxCreateAndPopulateParticleRigidBuffer(const ExtGpu::PxParticleBufferDesc& desc, const ExtGpu::PxParticleRigidDesc& rigidDesc,
                                            PxCudaContextManager* cudaContextManager);

} // namespace ExtGpu

#if !PX_DOXYGEN
} // namespace physx
#endif

#endif