RavEngine
Loading...
Searching...
No Matches
PxsMaterialManager.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#ifndef PXS_MATERIAL_MANAGER_H
30#define PXS_MATERIAL_MANAGER_H
31
32#include "PxsMaterialCore.h"
33#include "PxsFEMSoftBodyMaterialCore.h"
34#include "PxsFEMClothMaterialCore.h"
35#include "PxsPBDMaterialCore.h"
36#include "PxsFLIPMaterialCore.h"
37#include "PxsMPMMaterialCore.h"
38#include "foundation/PxAlignedMalloc.h"
39
40namespace physx
41{
43 {
44 PxU16 mMaterialIndex0;
45 PxU16 mMaterialIndex1;
46 };
47
48 template<class MaterialCore>
50 {
51 public:
53 {
54 const PxU32 matCount = 128;
55 materials = reinterpret_cast<MaterialCore*>(physx::PxAlignedAllocator<16>().allocate(sizeof(MaterialCore)*matCount, __FILE__, __LINE__));
56 maxMaterials = matCount;
57 for(PxU32 i=0; i<matCount; ++i)
58 {
59 materials[i].mMaterialIndex = MATERIAL_INVALID_HANDLE;
60 }
61 }
62
64 {
65 physx::PxAlignedAllocator<16>().deallocate(materials);
66 }
67
68 void setMaterial(MaterialCore* mat)
69 {
70 const PxU16 materialIndex = mat->mMaterialIndex;
71 resize(PxU32(materialIndex) + 1);
72 materials[materialIndex] = *mat;
73 }
74
75 void updateMaterial(MaterialCore* mat)
76 {
77 materials[mat->mMaterialIndex] =*mat;
78 }
79
80 void removeMaterial(MaterialCore* mat)
81 {
82 mat->mMaterialIndex = MATERIAL_INVALID_HANDLE;
83 }
84
85 PX_FORCE_INLINE MaterialCore* getMaterial(const PxU32 index)const
86 {
87 PX_ASSERT(index < maxMaterials);
88 return &materials[index];
89 }
90
91 PxU32 getMaxSize()const
92 {
93 return maxMaterials;
94 }
95
96 void resize(PxU32 minValueForMax)
97 {
98 if(maxMaterials>=minValueForMax)
99 return;
100
101 const PxU32 numMaterials = maxMaterials;
102
103 maxMaterials = (minValueForMax+31)&~31;
104 MaterialCore* mat = reinterpret_cast<MaterialCore*>(physx::PxAlignedAllocator<16>().allocate(sizeof(MaterialCore)*maxMaterials, __FILE__, __LINE__));
105 for(PxU32 i=0; i<numMaterials; ++i)
106 mat[i] = materials[i];
107
108 for(PxU32 i = numMaterials; i < maxMaterials; ++i)
109 mat[i].mMaterialIndex = MATERIAL_INVALID_HANDLE;
110
111 physx::PxAlignedAllocator<16>().deallocate(materials);
112
113 materials = mat;
114 }
115
116 MaterialCore* materials;//make sure materials's start address is 16 bytes align
117 PxU32 maxMaterials;
118 PxU32 mPad;
119#if !PX_P64_FAMILY
120 PxU32 mPad2;
121#endif
122 };
123
124 //This class is used for forward declaration
125 class PxsMaterialManager : public PxsMaterialManagerT<PxsMaterialCore>
126 {
127 };
128
129 class PxsFEMMaterialManager : public PxsMaterialManagerT<PxsFEMSoftBodyMaterialCore>
130 {
131 };
132
133 class PxsFEMClothMaterialManager : public PxsMaterialManagerT<PxsFEMClothMaterialCore>
134 {
135 };
136
137 class PxsPBDMaterialManager : public PxsMaterialManagerT<PxsPBDMaterialCore>
138 {
139 };
140
141 class PxsFLIPMaterialManager : public PxsMaterialManagerT<PxsFLIPMaterialCore>
142 {
143 };
144
145 class PxsMPMMaterialManager : public PxsMaterialManagerT<PxsMPMMaterialCore>
146 {
147 };
148
149 class PxsCustomMaterialManager : public PxsMaterialManagerT<PxsCustomMaterialCore>
150 {
151 };
152
153 template<class MaterialCore>
155 {
156
157 public:
158 PxsMaterialManagerIterator(PxsMaterialManagerT<MaterialCore>& manager) : mManager(manager), mIndex(0)
159 {
160 }
161
162 bool getNextMaterial(MaterialCore*& materialCore)
163 {
164 const PxU32 maxSize = mManager.getMaxSize();
165 PxU32 index = mIndex;
166 while(index < maxSize && mManager.getMaterial(index)->mMaterialIndex == MATERIAL_INVALID_HANDLE)
167 index++;
168 materialCore = NULL;
169 if(index < maxSize)
170 materialCore = mManager.getMaterial(index++);
171 mIndex = index;
172 return materialCore!=NULL;
173 }
174
175 private:
178 PxU32 mIndex;
179 };
180
181}
182
183#endif
Definition PxAlignedMalloc.h:54
Definition PxsMaterialManager.h:150
Definition PxsMaterialManager.h:134
Definition PxsMaterialManager.h:130
Definition PxsMaterialManager.h:142
Definition PxsMaterialManager.h:146
Definition PxsMaterialManager.h:155
Definition PxsMaterialManager.h:50
Definition PxsMaterialManager.h:126
Definition PxsMaterialManager.h:138
#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
Definition PxsMaterialManager.h:43