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-2018 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 
50 class PxGpuDispatcher;
51 
52 
54 struct PxCudaInteropMode
55 {
59  enum Enum
60  {
61  NO_INTEROP = 0,
62  D3D10_INTEROP,
63  D3D11_INTEROP,
64  OGL_INTEROP,
65 
66  COUNT
67  };
68 };
69 
70 struct PxCudaInteropRegisterFlag
71 {
72  enum Enum
73  {
74  eNONE = 0x00,
75  eREAD_ONLY = 0x01,
76  eWRITE_DISCARD = 0x02,
77  eSURFACE_LDST = 0x04,
78  eTEXTURE_GATHER = 0x08
79  };
80 };
81 
87 typedef PxFlags<PxCudaInteropRegisterFlag::Enum, uint32_t> PxCudaInteropRegisterFlags;
88 PX_FLAGS_OPERATORS(PxCudaInteropRegisterFlag::Enum, uint32_t)
89 
90 class PxCudaContextManagerDesc
92 {
93 public:
115  CUcontext *ctx;
116 
124  void *graphicsDevice;
125 
126 #if PX_SUPPORT_GPU_PHYSX
127 
134  const char* appGUID;
135 #endif
136 
143  PxCudaInteropMode::Enum interopMode;
144 
145 
161  uint32_t memoryBaseSize[PxCudaBufferMemorySpace::COUNT];
162 
172  uint32_t memoryPageSize[PxCudaBufferMemorySpace::COUNT];
173 
180  uint32_t maxMemorySize[PxCudaBufferMemorySpace::COUNT];
181 
182  PX_INLINE PxCudaContextManagerDesc()
183  {
184  ctx = NULL;
185  interopMode = PxCudaInteropMode::NO_INTEROP;
186  graphicsDevice = 0;
187 #if PX_SUPPORT_GPU_PHYSX
188  appGUID = NULL;
189 #endif
190  for(uint32_t i = 0; i < PxCudaBufferMemorySpace::COUNT; i++)
191  {
192  memoryBaseSize[i] = 0;
193  memoryPageSize[i] = 2 * 1024*1024;
194  maxMemorySize[i] = UINT32_MAX;
195  }
196  }
197 };
198 
199 
217 class PxCudaContextManager
218 {
219 public:
235  virtual void acquireContext() = 0;
236 
244  virtual void releaseContext() = 0;
245 
249  virtual CUcontext getContext() = 0;
250 
257  virtual PxCudaMemoryManager *getMemoryManager() = 0;
258 
263  virtual class physx::PxGpuDispatcher *getGpuDispatcher() = 0;
264 
274  virtual bool contextIsValid() const = 0;
275 
276  /* Query CUDA context and device properties, without acquiring context */
277 
278  virtual bool supportsArchSM10() const = 0;
279  virtual bool supportsArchSM11() const = 0;
280  virtual bool supportsArchSM12() const = 0;
281  virtual bool supportsArchSM13() const = 0;
282  virtual bool supportsArchSM20() const = 0;
283  virtual bool supportsArchSM30() const = 0;
284  virtual bool supportsArchSM35() const = 0;
285  virtual bool supportsArchSM50() const = 0;
286  virtual bool supportsArchSM52() const = 0;
287  virtual bool supportsArchSM60() const = 0;
288  virtual bool isIntegrated() const = 0;
289  virtual bool canMapHostMemory() const = 0;
290  virtual int getDriverVersion() const = 0;
291  virtual size_t getDeviceTotalMemBytes() const = 0;
292  virtual int getMultiprocessorCount() const = 0;
293  virtual unsigned int getClockRate() const = 0;
294  virtual int getSharedMemPerBlock() const = 0;
295  virtual int getSharedMemPerMultiprocessor() const = 0;
296  virtual unsigned int getMaxThreadsPerBlock() const = 0;
297  virtual const char *getDeviceName() const = 0;
298  virtual CUdevice getDevice() const = 0;
299  virtual PxCudaInteropMode::Enum getInteropMode() const = 0;
300 
301  virtual void setUsingConcurrentStreams(bool) = 0;
302  virtual bool getUsingConcurrentStreams() const = 0;
303  /* End query methods that don't require context to be acquired */
304 
325  virtual bool registerResourceInCudaGL(CUgraphicsResource &resource, uint32_t buffer, PxCudaInteropRegisterFlags flags = PxCudaInteropRegisterFlags()) = 0;
326 
347  virtual bool registerResourceInCudaD3D(CUgraphicsResource &resource, void *resourcePointer, PxCudaInteropRegisterFlags flags = PxCudaInteropRegisterFlags()) = 0;
348 
356  virtual bool unregisterResourceInCuda(CUgraphicsResource resource) = 0;
357 
365  virtual int usingDedicatedGPU() const = 0;
366 
382  virtual void release() = 0;
383 
384 protected:
385 
389  virtual ~PxCudaContextManager() {}
390 };
391 
395 class PxScopedCudaLock
396 {
397 public:
401  PxScopedCudaLock(PxCudaContextManager& ctx) : mCtx(&ctx)
402  {
403  mCtx->acquireContext();
404  }
405 
409  ~PxScopedCudaLock()
410  {
411  mCtx->releaseContext();
412  }
413 
414 protected:
415 
419  PxCudaContextManager* mCtx;
420 };
421 
422 } // end physx namespace
423 
424 #endif // PX_SUPPORT_GPU_PHYSX
425 #endif // PXCUDACONTEXTMANAGER_PXCUDACONTEXTMANAGER_H
Definition: GuContactBuffer.h:37
A GpuTask dispatcher.
Definition: PxGpuDispatcher.h:71
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:349