1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
// This code contains NVIDIA Confidential Information and is disclosed to you
// under a form of NVIDIA software license agreement provided separately to you.
//
// Notice
// NVIDIA Corporation and its licensors retain all intellectual property and
// proprietary rights in and to this software and related documentation and
// any modifications thereto. Any use, reproduction, disclosure, or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA Corporation is strictly prohibited.
//
// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Information and code furnished is believed to be accurate and reliable.
// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2016-2020 NVIDIA Corporation. All rights reserved.
#ifndef NVBLASTCHUNKHIERARCHY_H
#define NVBLASTCHUNKHIERARCHY_H
#include "NvBlastIndexFns.h"
#include "NvBlastDLink.h"
#include "NvBlast.h"
#include "NvBlastAssert.h"
#include "NvBlastIteratorBase.h"
namespace Nv
{
namespace Blast
{
/**
Chunk hierarchy depth-first iterator. Traverses subtree with root given by startChunkIndex.
Will not traverse chunks with index at or beyond chunkIndexLimit.
*/
class ChunkDepthFirstIt : public IteratorBase<uint32_t>
{
public:
/** Constructed from a chunk array. */
ChunkDepthFirstIt(const NvBlastChunk* chunks, uint32_t startChunkIndex, uint32_t chunkIndexLimit) :
IteratorBase<uint32_t>(startChunkIndex), m_chunks(chunks), m_stop(startChunkIndex), m_limit(chunkIndexLimit)
{
if (m_curr >= m_limit)
{
m_curr = invalidIndex<uint32_t>();
}
}
/** Pre-increment. Only use if valid() == true. */
uint32_t operator ++ ()
{
NVBLAST_ASSERT(!isInvalidIndex(m_curr));
const NvBlastChunk* chunk = m_chunks + m_curr;
if (chunk->childIndexStop > chunk->firstChildIndex && chunk->firstChildIndex < m_limit)
{
m_curr = chunk->firstChildIndex;
}
else
{
for (;;)
{
if (m_curr == m_stop)
{
m_curr = invalidIndex<uint32_t>();
break;
}
NVBLAST_ASSERT(!isInvalidIndex(chunk->parentChunkIndex)); // This should not be possible with this search
const NvBlastChunk* parentChunk = m_chunks + chunk->parentChunkIndex;
if (++m_curr < parentChunk->childIndexStop)
{
break; // Sibling chunk is valid, that's the next chunk
}
m_curr = chunk->parentChunkIndex;
chunk = parentChunk;
}
}
return m_curr;
}
private:
const NvBlastChunk* m_chunks;
uint32_t m_stop;
uint32_t m_limit;
};
/**
Enumerates chunk indices in a subtree with root given by chunkIndex, in breadth-first order.
Will not traverse chunks with index at or beyond chunkIndexLimit.
Returns the number of indices written to the chunkIndex array
*/
NV_INLINE uint32_t enumerateChunkHierarchyBreadthFirst
(
uint32_t* chunkIndices,
uint32_t chunkIndicesSize,
const NvBlastChunk* chunks,
uint32_t chunkIndex,
bool includeRoot = true,
uint32_t chunkIndexLimit = invalidIndex<uint32_t>()
)
{
if (chunkIndicesSize == 0)
{
return 0;
}
uint32_t chunkIndexCount = 0;
bool rootHandled = false;
if (includeRoot)
{
chunkIndices[chunkIndexCount++] = chunkIndex;
rootHandled = true;
}
for (uint32_t curr = 0; !rootHandled || curr < chunkIndexCount;)
{
const NvBlastChunk& chunk = chunks[rootHandled ? chunkIndices[curr] : chunkIndex];
if (chunk.firstChildIndex < chunkIndexLimit)
{
const uint32_t childIndexStop = chunk.childIndexStop < chunkIndexLimit ? chunk.childIndexStop : chunkIndexLimit;
const uint32_t childIndexBufferStop = chunk.firstChildIndex + (chunkIndicesSize - chunkIndexCount);
const uint32_t stop = childIndexStop < childIndexBufferStop ? childIndexStop : childIndexBufferStop;
for (uint32_t childIndex = chunk.firstChildIndex; childIndex < stop; ++childIndex)
{
chunkIndices[chunkIndexCount++] = childIndex;
}
}
if (rootHandled)
{
++curr;
}
rootHandled = true;
}
return chunkIndexCount;
}
/**
VisibilityRep must have m_firstVisibleChunkIndex and m_visibleChunkCount fields
*/
template<class VisibilityRep>
void updateVisibleChunksFromSupportChunk
(
VisibilityRep* actors,
IndexDLink<uint32_t>* visibleChunkIndexLinks,
uint32_t* chunkActorIndices,
uint32_t actorIndex,
uint32_t supportChunkIndex,
const NvBlastChunk* chunks,
uint32_t upperSupportChunkCount
)
{
uint32_t chunkIndex = supportChunkIndex;
uint32_t chunkActorIndex = chunkActorIndices[supportChunkIndex];
uint32_t newChunkActorIndex = actorIndex;
VisibilityRep& thisActor = actors[actorIndex];
do
{
if (chunkActorIndex == newChunkActorIndex)
{
break; // Nothing to do
}
const uint32_t parentChunkIndex = chunks[chunkIndex].parentChunkIndex;
const uint32_t parentChunkActorIndex = parentChunkIndex != invalidIndex<uint32_t>() ? chunkActorIndices[parentChunkIndex] : invalidIndex<uint32_t>();
const bool chunkVisible = chunkActorIndex != parentChunkActorIndex;
// If the chunk is visible, it needs to be removed from its old actor's visibility list
if (chunkVisible && !isInvalidIndex(chunkActorIndex))
{
VisibilityRep& chunkActor = actors[chunkActorIndex];
IndexDList<uint32_t>().removeFromList(chunkActor.m_firstVisibleChunkIndex, visibleChunkIndexLinks, chunkIndex);
--chunkActor.m_visibleChunkCount;
}
// Now update the chunk's actor index
const uint32_t oldChunkActorIndex = chunkActorIndices[chunkIndex];
chunkActorIndices[chunkIndex] = newChunkActorIndex;
if (newChunkActorIndex != invalidIndex<uint32_t>() && parentChunkActorIndex != newChunkActorIndex)
{
// The chunk is now visible. Add it to this actor's visibility list
IndexDList<uint32_t>().insertListHead(thisActor.m_firstVisibleChunkIndex, visibleChunkIndexLinks, chunkIndex);
++thisActor.m_visibleChunkCount;
// Remove its children from this actor's visibility list
if (actorIndex != oldChunkActorIndex)
{
const NvBlastChunk& chunk = chunks[chunkIndex];
if (chunk.firstChildIndex < upperSupportChunkCount) // Only need to deal with upper-support children
{
for (uint32_t childChunkIndex = chunk.firstChildIndex; childChunkIndex < chunk.childIndexStop; ++childChunkIndex)
{
if (chunkActorIndices[childChunkIndex] == actorIndex)
{
IndexDList<uint32_t>().removeFromList(thisActor.m_firstVisibleChunkIndex, visibleChunkIndexLinks, childChunkIndex);
--thisActor.m_visibleChunkCount;
}
}
}
}
}
if (parentChunkIndex != invalidIndex<uint32_t>())
{
// If all of its siblings have the same index, then the parent will too. Otherwise, the parent will have an invalid index and its children will be visible
const NvBlastChunk& parentChunk = chunks[parentChunkIndex];
bool uniform = true;
for (uint32_t childChunkIndex = parentChunk.firstChildIndex; uniform && childChunkIndex < parentChunk.childIndexStop; ++childChunkIndex)
{
uniform = (newChunkActorIndex == chunkActorIndices[childChunkIndex]);
}
if (!uniform)
{
newChunkActorIndex = invalidIndex<uint32_t>();
for (uint32_t childChunkIndex = parentChunk.firstChildIndex; childChunkIndex < parentChunk.childIndexStop; ++childChunkIndex)
{
const uint32_t childChunkActorIndex = chunkActorIndices[childChunkIndex];
if (childChunkActorIndex != invalidIndex<uint32_t>() && childChunkActorIndex == parentChunkActorIndex)
{
// The child was invisible. Add it to its actor's visibility list
VisibilityRep& childChunkActor = actors[childChunkActorIndex];
IndexDList<uint32_t>().insertListHead(childChunkActor.m_firstVisibleChunkIndex, visibleChunkIndexLinks, childChunkIndex);
++childChunkActor.m_visibleChunkCount;
}
}
}
}
// Climb the hierarchy
chunkIndex = parentChunkIndex;
chunkActorIndex = parentChunkActorIndex;
} while (chunkIndex != invalidIndex<uint32_t>());
}
} // namespace Blast
} // namespace Nv
#endif // ifndef NVBLASTCHUNKHIERARCHY_H
|