PxGpuCopyDescQueue.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 #ifndef PXCUDACONTEXTMANAGER_PXGPUCOPYDESCQUEUE_H
29 #define PXCUDACONTEXTMANAGER_PXGPUCOPYDESCQUEUE_H
30 
32 
33 #if PX_SUPPORT_GPU_PHYSX
34 
35 #include "foundation/PxAssert.h"
36 #include "task/PxTaskDefine.h"
37 #include "task/PxGpuDispatcher.h"
40 
41 /* forward decl to avoid including <cuda.h> */
42 typedef struct CUstream_st* CUstream;
43 
44 namespace physx
45 {
46 
48 
50 class PxGpuCopyDescQueue
51 {
52 public:
54  PxGpuCopyDescQueue(PxGpuDispatcher& d)
55  : mDispatcher(d)
56  , mBuffer(0)
57  , mStream(0)
58  , mReserved(0)
59  , mOccupancy(0)
60  , mFlushed(0)
61  {
62  }
63 
65  ~PxGpuCopyDescQueue()
66  {
67  if (mBuffer)
68  {
69  mDispatcher.getCudaContextManager()->getMemoryManager()->free(PxCudaBufferMemorySpace::T_PINNED_HOST, (size_t) mBuffer);
70  }
71  }
72 
78  void reset(CUstream stream, uint32_t reserveSize)
79  {
80  if (reserveSize > mReserved)
81  {
82  if (mBuffer)
83  {
84  mDispatcher.getCudaContextManager()->getMemoryManager()->free(
85  PxCudaBufferMemorySpace::T_PINNED_HOST,
86  (size_t) mBuffer);
87  mReserved = 0;
88  }
89  mBuffer = (PxGpuCopyDesc*) mDispatcher.getCudaContextManager()->getMemoryManager()->alloc(
90  PxCudaBufferMemorySpace::T_PINNED_HOST,
91  reserveSize * sizeof(PxGpuCopyDesc),
92  PX_ALLOC_INFO("PxGpuCopyDescQueue", GPU_UTIL));
93  if (mBuffer)
94  {
95  mReserved = reserveSize;
96  }
97  }
98 
99  mOccupancy = 0;
100  mFlushed = 0;
101  mStream = stream;
102  }
103 
105  void enqueue(PxGpuCopyDesc& desc)
106  {
107  PX_ASSERT(desc.isValid());
108  if (desc.bytes == 0)
109  {
110  return;
111  }
112 
113  if (mOccupancy < mReserved)
114  {
115  mBuffer[ mOccupancy++ ] = desc;
116  }
117  else
118  {
119  mDispatcher.launchCopyKernel(&desc, 1, mStream);
120  }
121  }
122 
124  void flushEnqueued()
125  {
126  if (mOccupancy > mFlushed)
127  {
128  mDispatcher.launchCopyKernel(mBuffer + mFlushed, mOccupancy - mFlushed, mStream);
129  mFlushed = mOccupancy;
130  }
131  }
132 
133 private:
134  PxGpuDispatcher& mDispatcher;
135  PxGpuCopyDesc* mBuffer;
136  CUstream mStream;
137  uint32_t mReserved;
138  uint32_t mOccupancy;
139  uint32_t mFlushed;
140 
141  void operator=(const PxGpuCopyDescQueue&); // prevent a warning...
142 };
143 
145 
146 } // end physx namespace
147 
148 #endif // PX_SUPPORT_GPU_PHYSX
149 #endif // PXCUDACONTEXTMANAGER_PXGPUCOPYDESCQUEUE_H
Definition: GuContactBuffer.h:37
struct CUstream_st * CUstream
Definition: PxGpuDispatcher.h:35
#define PX_POP_PACK
Definition: PxPreprocessor.h:343
#define PX_ASSERT(exp)
Definition: PxAssert.h:61
#define PX_PUSH_PACK_DEFAULT
Definition: PxPreprocessor.h:342