RavEngine
Loading...
Searching...
No Matches
NpMaterialManager.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29
30#ifndef NP_MATERIALMANAGER
31#define NP_MATERIALMANAGER
32
33#include "foundation/PxMemory.h"
34#include "CmIDPool.h"
35
36namespace physx
37{
38 template<class Material>
40 {
41 public:
43 {
44 const PxU32 matCount = 128;
45 mMaterials = reinterpret_cast<Material**>(PX_ALLOC(sizeof(Material*) * matCount, "NpMaterialManager::initialise"));
46 mMaxMaterials = matCount;
47 PxMemZero(mMaterials, sizeof(Material*)*mMaxMaterials);
48 }
49
51
52 void releaseMaterials()
53 {
54 for(PxU32 i=0; i<mMaxMaterials; ++i)
55 {
56 if(mMaterials[i])
57 {
58 const PxU32 handle(mMaterials[i]->mMaterial.mMaterialIndex);
59 mHandleManager.freeID(handle);
60 mMaterials[i]->release();
61 mMaterials[i] = NULL;
62 }
63 }
64 PX_FREE(mMaterials);
65 }
66
67 bool setMaterial(Material& mat)
68 {
69 const PxU32 poolID = mHandleManager.getNewID();
70 if (poolID >= MATERIAL_INVALID_HANDLE)
71 return false;
72
73 const PxU16 materialIndex = PxTo16(poolID);
74
75 if(materialIndex >= mMaxMaterials)
76 resize();
77
78 mMaterials[materialIndex] = &mat;
79 mat.mMaterial.mMaterialIndex = materialIndex;
80 return true;
81 }
82
83 void updateMaterial(Material& mat)
84 {
85 mMaterials[mat.mMaterial.mMaterialIndex] = &mat;
86 }
87
88 PX_FORCE_INLINE PxU32 getNumMaterials() const
89 {
90 return mHandleManager.getNumUsedID();
91 }
92
93 void removeMaterial(Material& mat)
94 {
95 const PxU16 handle = mat.mMaterial.mMaterialIndex;
96 if(handle != MATERIAL_INVALID_HANDLE)
97 {
98 mMaterials[handle] = NULL;
99 mHandleManager.freeID(PxU32(handle));
100 }
101 }
102
103 PX_FORCE_INLINE Material* getMaterial(const PxU32 index) const
104 {
105 PX_ASSERT(index < mMaxMaterials);
106 return mMaterials[index];
107 }
108
109 PX_FORCE_INLINE PxU32 getMaxSize() const
110 {
111 return mMaxMaterials;
112 }
113
114 PX_FORCE_INLINE Material** getMaterials() const
115 {
116 return mMaterials;
117 }
118
119 private:
120 void resize()
121 {
122 const PxU32 numMaterials = mMaxMaterials;
123 mMaxMaterials = PxMin(mMaxMaterials*2, PxU32(MATERIAL_INVALID_HANDLE));
124
125 Material** mat = reinterpret_cast<Material**>(PX_ALLOC(sizeof(Material*)*mMaxMaterials, "NpMaterialManager::resize"));
126 PxMemZero(mat, sizeof(Material*)*mMaxMaterials);
127 for(PxU32 i=0; i<numMaterials; ++i)
128 mat[i] = mMaterials[i];
129
130 PX_FREE(mMaterials);
131
132 mMaterials = mat;
133 }
134
135 Cm::IDPool mHandleManager;
136 Material** mMaterials;
137 PxU32 mMaxMaterials;
138 };
139
140 template<class Material>
142 {
143 public:
144 NpMaterialManagerIterator(const NpMaterialManager<Material>& manager) : mManager(manager), mIndex(0)
145 {
146 }
147
148 bool getNextMaterial(Material*& np)
149 {
150 const PxU32 maxSize = mManager.getMaxSize();
151 PxU32 index = mIndex;
152 while(index < maxSize && mManager.getMaterial(index)==NULL)
153 index++;
154 np = NULL;
155 if(index < maxSize)
156 np = mManager.getMaterial(index++);
157 mIndex = index;
158 return np!=NULL;
159 }
160
161 private:
163 const NpMaterialManager<Material>& mManager;
164 PxU32 mIndex;
165 };
166}
167
168#endif
Definition CmIDPool.h:177
Definition NpMaterialManager.h:142
Definition NpMaterialManager.h:40
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_FORCE_INLINE void * PxMemZero(void *dest, PxU32 count)
Sets the bytes of the provided buffer to zero.
Definition PxMemory.h:53
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMin(T a, T b)
The return value is the lesser of the two specified values.
Definition PxMath.h:88