Program Listing for include/cudamanager/PxCudaContextManager.h

↰ Return to documentation for include/cudamanager/PxCudaContextManager.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.

#ifndef PX_CUDA_CONTEXT_MANAGER_H
#define PX_CUDA_CONTEXT_MANAGER_H

#include "foundation/PxPreprocessor.h"

#if PX_SUPPORT_GPU_PHYSX

#include "foundation/PxSimpleTypes.h"
#include "foundation/PxErrorCallback.h"
#include "foundation/PxFlags.h"

#include "PxCudaTypes.h"

#if !PX_DOXYGEN
namespace physx
{
#endif

class PxCudaContext;

struct PxCudaInteropMode
{
    enum Enum
    {
        NO_INTEROP = 0,
        D3D10_INTEROP,
        D3D11_INTEROP,
        OGL_INTEROP,

        COUNT
    };
};

struct PxCudaInteropRegisterFlag
{
    enum Enum
    {
        eNONE           = 0x00,
        eREAD_ONLY      = 0x01,
        eWRITE_DISCARD  = 0x02,
        eSURFACE_LDST   = 0x04,
        eTEXTURE_GATHER = 0x08
    };
};

class PxDeviceAllocatorCallback
{
public:

    virtual bool memAlloc(void** ptr, size_t size) = 0;

    virtual bool memFree(void* ptr) = 0;

protected:
    virtual ~PxDeviceAllocatorCallback() {}
};
typedef PxFlags<PxCudaInteropRegisterFlag::Enum, uint32_t> PxCudaInteropRegisterFlags;
PX_FLAGS_OPERATORS(PxCudaInteropRegisterFlag::Enum, uint32_t)

class PxCudaContextManagerDesc
{
public:
    CUcontext*  ctx;

    void*   graphicsDevice;

    const char* appGUID;

    PxDeviceAllocatorCallback*  deviceAllocator;

    PxCudaInteropMode::Enum interopMode;

    PX_INLINE PxCudaContextManagerDesc() :
        ctx             (NULL),
        graphicsDevice  (NULL),
        appGUID         (NULL),
        deviceAllocator (NULL),
        interopMode     (PxCudaInteropMode::NO_INTEROP)
    {
    }
};

struct PxKernelIndex
{
    PxU32 moduleIndex;
    const char* functionName;
};

class PxCudaContextManager
{
public:
    template<typename T>
    void clearDeviceBufferAsync(T* deviceBuffer, PxU32 numElements, CUstream stream, PxI32 value = 0)
    {
        clearDeviceBufferAsyncInternal(deviceBuffer, numElements * sizeof(T), stream, value);
    }

    template<typename T>
    void copyDToH(T* hostBuffer, const T* deviceBuffer, PxU32 numElements)
    {
        copyDToHInternal(hostBuffer, deviceBuffer, numElements * sizeof(T));
    }

    template<typename T>
    void copyHToD(T* deviceBuffer, const T* hostBuffer, PxU32 numElements)
    {
        copyHToDInternal(deviceBuffer, hostBuffer, numElements * sizeof(T));
    }

    template<typename T>
    void copyDToHAsync(T* hostBuffer, const T* deviceBuffer, PxU32 numElements, CUstream stream)
    {
        copyDToHAsyncInternal(hostBuffer, deviceBuffer, numElements * sizeof(T), stream);
    }

    template<typename T>
    void copyHToDAsync(T* deviceBuffer, const T* hostBuffer, PxU32 numElements, CUstream stream)
    {
        copyHToDAsyncInternal(deviceBuffer, hostBuffer, numElements * sizeof(T), stream);
    }

    template<typename T>
    void copyDToDAsync(T* dstDeviceBuffer, const T* srcDeviceBuffer, PxU32 numElements, CUstream stream)
    {
        copyDToDAsyncInternal(dstDeviceBuffer, srcDeviceBuffer, numElements * sizeof(T), stream);
    }

    template<typename T>
    void allocDeviceBuffer(T*& deviceBuffer, PxU32 numElements, const char* filename = __FILE__, PxI32 line = __LINE__)
    {
        void* ptr = allocDeviceBufferInternal(numElements * sizeof(T), filename, line);
        deviceBuffer = reinterpret_cast<T*>(ptr);
    }

    template<typename T>
    T* allocDeviceBuffer(PxU32 numElements, const char* filename = __FILE__, PxI32 line = __LINE__)
    {
        void* ptr = allocDeviceBufferInternal(numElements * sizeof(T), filename, line);
        return reinterpret_cast<T*>(ptr);
    }

    template<typename T>
    void freeDeviceBuffer(T*& deviceBuffer)
    {
        freeDeviceBufferInternal(deviceBuffer);
        deviceBuffer = NULL;
    }

    template<typename T>
    void allocPinnedHostBuffer(T*& pinnedHostBuffer, PxU32 numElements, const char* filename = __FILE__, PxI32 line = __LINE__)
    {
        void* ptr = allocPinnedHostBufferInternal(numElements * sizeof(T), filename, line);
        pinnedHostBuffer = reinterpret_cast<T*>(ptr);
    }

    template<typename T>
    T* allocPinnedHostBuffer(PxU32 numElements, const char* filename = __FILE__, PxI32 line = __LINE__)
    {
        void* ptr = allocPinnedHostBufferInternal(numElements * sizeof(T), filename, line);
        return reinterpret_cast<T*>(ptr);
    }

    template<typename T>
    void freePinnedHostBuffer(T*& pinnedHostBuffer)
    {
        freePinnedHostBufferInternal(pinnedHostBuffer);
        pinnedHostBuffer = NULL;
    }

    virtual CUdeviceptr getMappedDevicePtr(void* pinnedHostBuffer) = 0;

    virtual void acquireContext() = 0;

    virtual void releaseContext() = 0;

    virtual CUcontext getContext() = 0;

    virtual PxCudaContext* getCudaContext() = 0;

    virtual bool contextIsValid() const = 0;

    /* Query CUDA context and device properties, without acquiring context */

    virtual bool supportsArchSM10() const = 0;
    virtual bool supportsArchSM11() const = 0;
    virtual bool supportsArchSM12() const = 0;
    virtual bool supportsArchSM13() const = 0;
    virtual bool supportsArchSM20() const = 0;
    virtual bool supportsArchSM30() const = 0;
    virtual bool supportsArchSM35() const = 0;
    virtual bool supportsArchSM50() const = 0;
    virtual bool supportsArchSM52() const = 0;
    virtual bool supportsArchSM60() const = 0;
    virtual bool isIntegrated() const = 0;
    virtual bool canMapHostMemory() const = 0;
    virtual int  getDriverVersion() const = 0;
    virtual size_t getDeviceTotalMemBytes() const = 0;
    virtual int getMultiprocessorCount() const = 0;
    virtual unsigned int getClockRate() const = 0;
    virtual int  getSharedMemPerBlock() const = 0;
    virtual int  getSharedMemPerMultiprocessor() const = 0;
    virtual unsigned int getMaxThreadsPerBlock() const = 0;
    virtual const char *getDeviceName() const = 0;
    virtual CUdevice getDevice() const = 0;
    virtual PxCudaInteropMode::Enum getInteropMode() const = 0;

    virtual void setUsingConcurrentStreams(bool) = 0;
    virtual bool getUsingConcurrentStreams() const = 0;
    /* End query methods that don't require context to be acquired */

    virtual bool registerResourceInCudaGL(CUgraphicsResource& resource, uint32_t buffer, PxCudaInteropRegisterFlags flags = PxCudaInteropRegisterFlags()) = 0;

    virtual bool registerResourceInCudaD3D(CUgraphicsResource& resource, void* resourcePointer, PxCudaInteropRegisterFlags flags = PxCudaInteropRegisterFlags()) = 0;

    virtual bool unregisterResourceInCuda(CUgraphicsResource resource) = 0;

    virtual int usingDedicatedGPU() const = 0;

    virtual CUmodule* getCuModules() = 0;

    virtual void release() = 0;

protected:

    virtual ~PxCudaContextManager() {}

    virtual void* allocDeviceBufferInternal(PxU32 numBytes, const char* filename = NULL, PxI32 line = -1) = 0;
    virtual void* allocPinnedHostBufferInternal(PxU32 numBytes, const char* filename = NULL, PxI32 line = -1) = 0;

    virtual void freeDeviceBufferInternal(void* deviceBuffer) = 0;
    virtual void freePinnedHostBufferInternal(void* pinnedHostBuffer) = 0;

    virtual void clearDeviceBufferAsyncInternal(void* deviceBuffer, PxU32 numBytes, CUstream stream, PxI32 value) = 0;

    virtual void copyDToHAsyncInternal(void* hostBuffer, const void* deviceBuffer, PxU32 numBytes, CUstream stream) = 0;
    virtual void copyHToDAsyncInternal(void* deviceBuffer, const void* hostBuffer, PxU32 numBytes, CUstream stream) = 0;
    virtual void copyDToDAsyncInternal(void* dstDeviceBuffer, const void* srcDeviceBuffer, PxU32 numBytes, CUstream stream) = 0;

    virtual void copyDToHInternal(void* hostBuffer, const void* deviceBuffer, PxU32 numBytes) = 0;
    virtual void copyHToDInternal(void* deviceBuffer, const void* hostBuffer, PxU32 numBytes) = 0;
};

#define PX_DEVICE_ALLOC(cudaContextManager, deviceBuffer, numElements) cudaContextManager->allocDeviceBuffer(deviceBuffer, numElements, __FILE__, __LINE__)
#define PX_DEVICE_ALLOC_T(T, cudaContextManager, numElements) cudaContextManager->allocDeviceBuffer<T>(numElements, __FILE__, __LINE__)
#define PX_DEVICE_FREE(cudaContextManager, deviceBuffer) cudaContextManager->freeDeviceBuffer(deviceBuffer);

#define PX_PINNED_HOST_ALLOC(cudaContextManager, pinnedHostBuffer, numElements) cudaContextManager->allocPinnedHostBuffer(pinnedHostBuffer, numElements, __FILE__, __LINE__)
#define PX_PINNED_HOST_ALLOC_T(T, cudaContextManager, numElements) cudaContextManager->allocPinnedHostBuffer<T>(numElements, __FILE__, __LINE__)
#define PX_PINNED_HOST_FREE(cudaContextManager, pinnedHostBuffer) cudaContextManager->freePinnedHostBuffer(pinnedHostBuffer);

class PxScopedCudaLock
{
public:
    PxScopedCudaLock(PxCudaContextManager& ctx) : mCtx(&ctx)
    {
        mCtx->acquireContext();
    }

    ~PxScopedCudaLock()
    {
        mCtx->releaseContext();
    }

protected:

    PxCudaContextManager* mCtx;
};

#if !PX_DOXYGEN
} // namespace physx
#endif

#endif // PX_SUPPORT_GPU_PHYSX
#endif