PxCudaContextManager.h
Go to the documentation of this file.
1 //
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions
4 // are met:
5 // * Redistributions of source code must retain the above copyright
6 // notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright
8 // notice, this list of conditions and the following disclaimer in the
9 // documentation and/or other materials provided with the distribution.
10 // * Neither the name of NVIDIA CORPORATION nor the names of its
11 // contributors may be used to endorse or promote products derived
12 // from this software without specific prior written permission.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
15 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
27 
28 
29 #ifndef PXCUDACONTEXTMANAGER_PXCUDACONTEXTMANAGER_H
30 #define PXCUDACONTEXTMANAGER_PXCUDACONTEXTMANAGER_H
31 
33 
34 #if PX_SUPPORT_GPU_PHYSX
35 
38 #include "foundation/PxFlags.h"
39 #include "task/PxTaskDefine.h"
41 
42 /* Forward decl to avoid inclusion of cuda.h */
43 typedef struct CUctx_st *CUcontext;
44 typedef struct CUgraphicsResource_st *CUgraphicsResource;
45 typedef int CUdevice;
46 
47 namespace physx
48 {
49 
51 struct PxCudaInteropMode
52 {
56  enum Enum
57  {
58  NO_INTEROP = 0,
59  D3D10_INTEROP,
60  D3D11_INTEROP,
61  OGL_INTEROP,
62 
63  COUNT
64  };
65 };
66 
67 struct PxCudaInteropRegisterFlag
68 {
69  enum Enum
70  {
71  eNONE = 0x00,
72  eREAD_ONLY = 0x01,
73  eWRITE_DISCARD = 0x02,
74  eSURFACE_LDST = 0x04,
75  eTEXTURE_GATHER = 0x08
76  };
77 };
78 
84 typedef PxFlags<PxCudaInteropRegisterFlag::Enum, uint32_t> PxCudaInteropRegisterFlags;
85 PX_FLAGS_OPERATORS(PxCudaInteropRegisterFlag::Enum, uint32_t)
86 
87 class PxCudaContextManagerDesc
89 {
90 public:
112  CUcontext *ctx;
113 
121  void *graphicsDevice;
122 
123 #if PX_SUPPORT_GPU_PHYSX
124 
131  const char* appGUID;
132 #endif
133 
140  PxCudaInteropMode::Enum interopMode;
141 
142 
158  uint32_t memoryBaseSize[PxCudaBufferMemorySpace::COUNT];
159 
169  uint32_t memoryPageSize[PxCudaBufferMemorySpace::COUNT];
170 
177  uint32_t maxMemorySize[PxCudaBufferMemorySpace::COUNT];
178 
179  PX_INLINE PxCudaContextManagerDesc()
180  {
181  ctx = NULL;
182  interopMode = PxCudaInteropMode::NO_INTEROP;
183  graphicsDevice = 0;
184 #if PX_SUPPORT_GPU_PHYSX
185  appGUID = NULL;
186 #endif
187  for(uint32_t i = 0; i < PxCudaBufferMemorySpace::COUNT; i++)
188  {
189  memoryBaseSize[i] = 0;
190  memoryPageSize[i] = 2 * 1024*1024;
191  maxMemorySize[i] = UINT32_MAX;
192  }
193  }
194 };
195 
196 
208 class PxCudaContextManager
209 {
210 public:
220  virtual void acquireContext() = 0;
221 
228  virtual void releaseContext() = 0;
229 
233  virtual CUcontext getContext() = 0;
234 
241  virtual PxCudaMemoryManager *getMemoryManager() = 0;
242 
250  virtual bool contextIsValid() const = 0;
251 
252  /* Query CUDA context and device properties, without acquiring context */
253 
254  virtual bool supportsArchSM10() const = 0;
255  virtual bool supportsArchSM11() const = 0;
256  virtual bool supportsArchSM12() const = 0;
257  virtual bool supportsArchSM13() const = 0;
258  virtual bool supportsArchSM20() const = 0;
259  virtual bool supportsArchSM30() const = 0;
260  virtual bool supportsArchSM35() const = 0;
261  virtual bool supportsArchSM50() const = 0;
262  virtual bool supportsArchSM52() const = 0;
263  virtual bool supportsArchSM60() const = 0;
264  virtual bool isIntegrated() const = 0;
265  virtual bool canMapHostMemory() const = 0;
266  virtual int getDriverVersion() const = 0;
267  virtual size_t getDeviceTotalMemBytes() const = 0;
268  virtual int getMultiprocessorCount() const = 0;
269  virtual unsigned int getClockRate() const = 0;
270  virtual int getSharedMemPerBlock() const = 0;
271  virtual int getSharedMemPerMultiprocessor() const = 0;
272  virtual unsigned int getMaxThreadsPerBlock() const = 0;
273  virtual const char *getDeviceName() const = 0;
274  virtual CUdevice getDevice() const = 0;
275  virtual PxCudaInteropMode::Enum getInteropMode() const = 0;
276 
277  virtual void setUsingConcurrentStreams(bool) = 0;
278  virtual bool getUsingConcurrentStreams() const = 0;
279  /* End query methods that don't require context to be acquired */
280 
301  virtual bool registerResourceInCudaGL(CUgraphicsResource &resource, uint32_t buffer, PxCudaInteropRegisterFlags flags = PxCudaInteropRegisterFlags()) = 0;
302 
323  virtual bool registerResourceInCudaD3D(CUgraphicsResource &resource, void *resourcePointer, PxCudaInteropRegisterFlags flags = PxCudaInteropRegisterFlags()) = 0;
324 
332  virtual bool unregisterResourceInCuda(CUgraphicsResource resource) = 0;
333 
341  virtual int usingDedicatedGPU() const = 0;
342 
356  virtual void release() = 0;
357 
358 protected:
359 
363  virtual ~PxCudaContextManager() {}
364 };
365 
369 class PxScopedCudaLock
370 {
371 public:
375  PxScopedCudaLock(PxCudaContextManager& ctx) : mCtx(&ctx)
376  {
377  mCtx->acquireContext();
378  }
379 
383  ~PxScopedCudaLock()
384  {
385  mCtx->releaseContext();
386  }
387 
388 protected:
389 
393  PxCudaContextManager* mCtx;
394 };
395 
396 } // end physx namespace
397 
398 #endif // PX_SUPPORT_GPU_PHYSX
399 #endif // PXCUDACONTEXTMANAGER_PXCUDACONTEXTMANAGER_H
Definition: GuContactBuffer.h:37
PxU16 flags
a set of Px1DConstraintFlags
Definition: PxConstraintDesc.h:110
Container for bitfield flag variables associated with a specific enum type.
Definition: PxFlags.h:73
#define PX_INLINE
Definition: PxPreprocessor.h:336