NvBlastActor.h
Go to the documentation of this file.
1 // This code contains NVIDIA Confidential Information and is disclosed to you
2 // under a form of NVIDIA software license agreement provided separately to you.
3 //
4 // Notice
5 // NVIDIA Corporation and its licensors retain all intellectual property and
6 // proprietary rights in and to this software and related documentation and
7 // any modifications thereto. Any use, reproduction, disclosure, or
8 // distribution of this software and related documentation without an express
9 // license agreement from NVIDIA Corporation is strictly prohibited.
10 //
11 // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
12 // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
13 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
14 // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
15 //
16 // Information and code furnished is believed to be accurate and reliable.
17 // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
18 // information or for any infringement of patents or other rights of third parties that may
19 // result from its use. No license is granted by implication or otherwise under any patent
20 // or patent rights of NVIDIA Corporation. Details are subject to change without notice.
21 // This code supersedes and replaces all information previously supplied.
22 // NVIDIA Corporation products are not authorized for use as critical
23 // components in life support devices or systems without express written approval of
24 // NVIDIA Corporation.
25 //
26 // Copyright (c) 2016-2020 NVIDIA Corporation. All rights reserved.
27 
28 
29 #ifndef NVBLASTACTOR_H
30 #define NVBLASTACTOR_H
31 
32 
33 #include "NvBlastAsset.h"
34 #include "NvBlastDLink.h"
35 #include "NvBlastIteratorBase.h"
36 #include "NvBlastSupportGraph.h"
37 #include "NvBlastFamilyGraph.h"
39 
40 #include <cstring>
41 
42 
43 namespace Nv
44 {
45 namespace Blast
46 {
47 
48 // Forward declarations
49 class FamilyGraph;
50 struct FamilyHeader;
51 
58 class Actor : public NvBlastActor
59 {
60  friend struct FamilyHeader;
61 
62  friend void updateVisibleChunksFromSupportChunk<>(Actor*, IndexDLink<uint32_t>*, uint32_t*, uint32_t, uint32_t, const NvBlastChunk*, uint32_t);
63 
64 public:
65  Actor() : m_familyOffset(0), m_firstVisibleChunkIndex(UINT32_MAX), m_visibleChunkCount(0), m_firstGraphNodeIndex(UINT32_MAX), m_graphNodeCount(0), m_leafChunkCount(0) {}
66 
68 
75 
81  const Asset* getAsset() const;
82 
90  bool isActive() const;
91 
97  bool isSubSupportChunk() const;
98 
105  bool isSingleSupportChunk() const;
106 
112  uint32_t getIndex() const;
113 
120  uint32_t getVisibleChunkCount() const;
121 
127  uint32_t getFirstVisibleChunkIndex() const;
128 
135  uint32_t getGraphNodeCount() const;
136 
142  uint32_t getLeafChunkCount() const;
143 
149  uint32_t getFirstGraphNodeIndex() const;
150 
156  uint32_t getFirstSubsupportChunkIndex() const;
157 
163  const SupportGraph* getGraph() const;
164 
171  FamilyGraph* getFamilyGraph() const;
172 
178  NvBlastChunk* getChunks() const;
179 
185  NvBlastBond* getBonds() const;
186 
194  float* getLowerSupportChunkHealths() const;
195 
201  float* getSubsupportChunkHealths() const;
202 
208  float* getBondHealths() const;
209 
215  const uint32_t* getGraphNodeIndexLinks() const;
216 
217 
219 
233  class VisibleChunkIt : public DListIt<uint32_t>
234  {
235  public:
237  VisibleChunkIt(const Actor& actor);
238  };
239 
253  class GraphNodeIt : public LListIt<uint32_t>
254  {
255  public:
257  GraphNodeIt(const Actor& actor);
258  };
259 
260 
262 
275  static Actor* create(NvBlastFamily* family, const NvBlastActorDesc* desc, void* scratch, NvBlastLog logFn);
276 
285  static size_t createRequiredScratch(const NvBlastFamily* family);
286 
298  static Actor* deserialize(NvBlastFamily* family, const void* buffer, NvBlastLog logFn);
299 
309  uint32_t serialize(void* buffer, uint32_t bufferSize, NvBlastLog logFn) const;
310 
318  uint32_t serializationRequiredStorage(NvBlastLog logFn) const;
319 
326  bool release();
327 
328 
330 
334  void generateFracture(NvBlastFractureBuffers* commandBuffers, const NvBlastDamageProgram& program, const void* programParams, NvBlastLog logFn, NvBlastTimers* timers) const;
335 
339  uint32_t damageBond(uint32_t nodeIndex0, uint32_t nodeIndex1, float healthDamage);
340 
344  void damageBond(uint32_t nodeIndex0, uint32_t nodeIndex1, uint32_t bondIndex, float healthDamage);
345 
349  uint32_t damageBond(const NvBlastBondFractureData& cmd);
350 
354  void applyFracture(NvBlastFractureBuffers* eventBuffers, const NvBlastFractureBuffers* commands, NvBlastLog logFn, NvBlastTimers* timers);
355 
361  size_t splitRequiredScratch() const;
362 
366  uint32_t split(NvBlastActorSplitEvent* result, uint32_t newActorsMaxCount, void* scratch, NvBlastLog logFn, NvBlastTimers* timers);
367 
377  uint32_t findIslands(void* scratch);
378 
395  uint32_t partition(Actor** newActors, uint32_t newActorsSize, NvBlastLog logFn);
396 
401 
416  uint32_t partitionSingleLowerSupportChunk(Actor** newActors, uint32_t newActorsSize, NvBlastLog logFn);
417 
432  uint32_t partitionMultipleGraphNodes(Actor** newActors, uint32_t newActorsSize, NvBlastLog logFn);
433 
437  bool isBoundToWorld() const;
438 
442  bool isSplitRequired() const;
443 
444 private:
445 
447 
455  uint32_t m_familyOffset;
456 
461  uint32_t m_firstVisibleChunkIndex;
462 
466  uint32_t m_visibleChunkCount;
467 
472  uint32_t m_firstGraphNodeIndex;
473 
477  uint32_t m_graphNodeCount;
478 
482  uint32_t m_leafChunkCount;
483 };
484 
485 } // namespace Blast
486 } // namespace Nv
487 
488 
489 #include "NvBlastFamily.h"
490 
491 
492 namespace Nv
493 {
494 namespace Blast
495 {
496 
498 
500 {
502  return (FamilyHeader*)((uintptr_t)this - (uintptr_t)m_familyOffset);
503 }
504 
505 
507 {
508  return getFamilyHeader()->m_asset;
509 }
510 
511 
513 {
514  return m_familyOffset != 0;
515 }
516 
517 
519 {
520  return m_graphNodeCount == 0;
521 }
522 
523 
525 {
526  return m_graphNodeCount == 1;
527 }
528 
529 
530 NV_INLINE uint32_t Actor::getIndex() const
531 {
533  const FamilyHeader* header = getFamilyHeader();
534  NVBLAST_ASSERT(header != nullptr);
535  const size_t index = this - header->getActors();
536  NVBLAST_ASSERT(index <= UINT32_MAX);
537  return (uint32_t)index;
538 }
539 
540 
542 {
543  return m_visibleChunkCount;
544 }
545 
546 
548 {
549  return m_firstVisibleChunkIndex;
550 }
551 
552 
554 {
555  return m_graphNodeCount;
556 }
557 
558 
560 {
561  return m_leafChunkCount;
562 }
563 
564 
566 {
567  return m_firstGraphNodeIndex;
568 }
569 
571 {
573 }
574 
576 {
577  return &getAsset()->m_graph;
578 }
579 
581 {
582  return getFamilyHeader()->getFamilyGraph();
583 }
584 
586 {
587  return getAsset()->getChunks();
588 }
589 
591 {
592  return getAsset()->getBonds();
593 }
594 
596 {
597  return getFamilyHeader()->getLowerSupportChunkHealths();
598 }
599 
601 {
603 }
604 
606 {
607  return getFamilyHeader()->getBondHealths();
608 }
609 
611 {
612  return getFamilyHeader()->getGraphNodeIndexLinks();
613 }
614 
615 
617 {
618  // Do nothing if this actor is not currently active.
619  if (!isActive())
620  {
621  return false;
622  }
623 
624  FamilyHeader* header = getFamilyHeader();
625 
626  // Clear the graph node list
627  uint32_t* graphNodeIndexLinks = getFamilyHeader()->getGraphNodeIndexLinks();
628  while (!isInvalidIndex(m_firstGraphNodeIndex))
629  {
630  const uint32_t graphNodeIndex = m_firstGraphNodeIndex;
631  m_firstGraphNodeIndex = graphNodeIndexLinks[m_firstGraphNodeIndex];
632  graphNodeIndexLinks[graphNodeIndex] = invalidIndex<uint32_t>();
633  --m_graphNodeCount;
634  }
635  NVBLAST_ASSERT(m_graphNodeCount == 0);
636 
637  const Asset* asset = getAsset();
638 
639  // Clear the visible chunk list
640  IndexDLink<uint32_t>* visibleChunkIndexLinks = header->getVisibleChunkIndexLinks();
641  uint32_t* chunkActorIndices = header->getChunkActorIndices();
642  while (!isInvalidIndex(m_firstVisibleChunkIndex))
643  {
644  // Descendants of the visible actor may be accessed again if the actor is deserialized. Clear subtree.
645  for (Asset::DepthFirstIt i(*asset, m_firstVisibleChunkIndex, true); (bool)i; ++i)
646  {
647  chunkActorIndices[(uint32_t)i] = invalidIndex<uint32_t>();
648  }
649  IndexDList<uint32_t>().removeListHead(m_firstVisibleChunkIndex, visibleChunkIndexLinks);
650  --m_visibleChunkCount;
651  }
652  NVBLAST_ASSERT(m_visibleChunkCount == 0);
653 
654  // Clear the leaf chunk count
655  m_leafChunkCount = 0;
656 
657  // This invalidates the actor and decrements the reference count
658  header->returnActor(*this);
659 
660  return true;
661 }
662 
663 
664 NV_INLINE uint32_t Actor::partition(Actor** newActors, uint32_t newActorsSize, NvBlastLog logFn)
665 {
666  NVBLASTLL_CHECK(newActorsSize == 0 || newActors != nullptr, logFn, "Nv::Blast::Actor::partition: NULL newActors pointer array input with non-zero newActorCount.", return 0);
667 
668  // Call one of two partition functions depending on the actor's support status
669  return m_graphNodeCount <= 1 ?
670  partitionSingleLowerSupportChunk(newActors, newActorsSize, logFn) : // This actor will partition into subsupport chunks
671  partitionMultipleGraphNodes(newActors, newActorsSize, logFn); // This actor will partition into support chunks
672 }
673 
674 
676 {
677  const SupportGraph& graph = *getGraph();
678 
679  if (graph.m_nodeCount == 0)
680  {
681  return false; // This shouldn't happen
682  }
683 
684  const uint32_t lastGraphChunkIndex = graph.getChunkIndices()[graph.m_nodeCount - 1];
685 
686  if (!isInvalidIndex(lastGraphChunkIndex))
687  {
688  return false; // There is no world node
689  }
690 
691  return getFamilyGraph()->getIslandIds()[graph.m_nodeCount - 1] == getIndex();
692 }
693 
694 
696 {
698 
699  if (getGraphNodeCount() <= 1)
700  {
702  float* chunkHealths = getLowerSupportChunkHealths();
703  if (chunkHealths[chunkHealthIndex] <= 0.0f)
704  {
705  const uint32_t chunkIndex = m_graphNodeCount == 0 ? m_firstVisibleChunkIndex : getGraph()->getChunkIndices()[m_firstGraphNodeIndex];
706  if (!isInvalidIndex(chunkIndex))
707  {
708  const NvBlastChunk& chunk = getChunks()[chunkIndex];
709  uint32_t childCount = chunk.childIndexStop - chunk.firstChildIndex;
710  return childCount > 0;
711  }
712  }
713  }
714  else
715  {
716  uint32_t* firstDirtyNodeIndices = getFamilyGraph()->getFirstDirtyNodeIndices();
717  if (!isInvalidIndex(firstDirtyNodeIndices[getIndex()]))
718  {
719  return true;
720  }
721 
722  }
723  return false;
724 }
725 
726 
728 
729 NV_INLINE Actor::VisibleChunkIt::VisibleChunkIt(const Actor& actor) : DListIt<uint32_t>(actor.m_firstVisibleChunkIndex, actor.getFamilyHeader()->getVisibleChunkIndexLinks())
730 {
731 }
732 
733 
735 
736 NV_INLINE Actor::GraphNodeIt::GraphNodeIt(const Actor& actor) : LListIt<uint32_t>(actor.m_firstGraphNodeIndex, actor.getFamilyHeader()->getGraphNodeIndexLinks())
737 {
738 }
739 
740 
742 
743 #if NVBLASTLL_CHECK_PARAMS
744 
747 static inline bool isValid(const NvBlastFractureBuffers* buffers)
748 {
749  if (buffers->chunkFractureCount != 0 && buffers->chunkFractures == nullptr)
750  return false;
751 
752  if (buffers->bondFractureCount != 0 && buffers->bondFractures == nullptr)
753  return false;
754 
755  return true;
756 }
757 #endif
758 
759 
760 } // namespace Blast
761 } // namespace Nv
762 
763 
764 #endif // ifndef NVBLASTACTOR_H
uint32_t getFirstSubsupportChunkIndex() const
Definition: NvBlastActor.h:570
void applyFracture(NvBlastFractureBuffers *eventBuffers, const NvBlastFractureBuffers *commands, NvBlastLog logFn, NvBlastTimers *timers)
Definition: NvBlastTypes.h:395
static Actor * create(NvBlastFamily *family, const NvBlastActorDesc *desc, void *scratch, NvBlastLog logFn)
#define UINT32_MAX
Definition: NvBlastPreprocessorInternal.h:68
uint32_t partitionMultipleGraphNodes(Actor **newActors, uint32_t newActorsSize, NvBlastLog logFn)
Definition: NvBlastFamilyGraph.h:58
NvBlastChunk * getChunks() const
Definition: NvBlastActor.h:585
bool isActive() const
Definition: NvBlastActor.h:512
Definition: NvBlastSupportGraph.h:76
float * getBondHealths() const
Definition: NvBlastActor.h:605
Definition: NvBlastTypes.h:189
uint32_t damageBond(uint32_t nodeIndex0, uint32_t nodeIndex1, float healthDamage)
FamilyHeader * getFamilyHeader() const
Definition: NvBlastActor.h:499
const Asset * getAsset() const
Definition: NvBlastActor.h:506
uint32_t getVisibleChunkCount() const
Definition: NvBlastActor.h:541
uint32_t split(NvBlastActorSplitEvent *result, uint32_t newActorsMaxCount, void *scratch, NvBlastLog logFn, NvBlastTimers *timers)
NvBlastBond * getBonds() const
Definition: NvBlastActor.h:590
uint32_t chunkFractureCount
available elements in chunkFractures
Definition: NvBlastTypes.h:471
Definition: NvBlastTypes.h:453
size_t splitRequiredScratch() const
NV_INLINE bool isInvalidIndex(T index)
Definition: NvBlastIndexFns.h:57
GraphNodeIt(const Actor &actor)
Definition: NvBlastActor.h:736
Actor()
Definition: NvBlastActor.h:65
void updateVisibleChunksFromGraphNodes()
const uint32_t * getGraphNodeIndexLinks() const
Definition: NvBlastActor.h:610
void(* NvBlastLog)(int type, const char *msg, const char *file, int line)
Definition: NvBlastTypes.h:63
NvBlastChunkFractureData * chunkFractures
memory to be filled by fracture functions
Definition: NvBlastTypes.h:473
uint32_t getLeafChunkCount() const
Definition: NvBlastActor.h:559
Definition: NvBlastFamily.h:61
void generateFracture(NvBlastFractureBuffers *commandBuffers, const NvBlastDamageProgram &program, const void *programParams, NvBlastLog logFn, NvBlastTimers *timers) const
#define NVBLASTLL_CHECK(_expr, _logFn, _msg, _onFail)
Definition: NvBlastPreprocessorInternal.h:59
#define NVBLAST_ASSERT(exp)
Definition: NvBlastAssert.h:37
Definition: NvBlastTypes.h:85
bool isSplitRequired() const
Definition: NvBlastActor.h:695
bool isSingleSupportChunk() const
Definition: NvBlastActor.h:524
Definition: NvBlastTypes.h:584
NvBlastBondFractureData * bondFractures
memory to be filled by fracture functions
Definition: NvBlastTypes.h:472
static size_t createRequiredScratch(const NvBlastFamily *family)
bool isBoundToWorld() const
Definition: NvBlastActor.h:675
Definition: NvBlastTypes.h:481
uint32_t childIndexStop
Definition: NvBlastTypes.h:177
uint32_t m_firstSubsupportChunkIndex
Definition: NvBlastAsset.h:207
const Asset * m_asset
Definition: NvBlastFamily.h:148
uint32_t bondFractureCount
available elements in bondFractures
Definition: NvBlastTypes.h:470
const SupportGraph * getGraph() const
Definition: NvBlastActor.h:575
float * getSubsupportChunkHealths() const
Definition: NvBlastActor.h:600
Definition: NvBlastAsset.h:253
uint32_t findIslands(void *scratch)
Definition: NvBlastActor.h:233
uint32_t serializationRequiredStorage(NvBlastLog logFn) const
uint32_t getFirstVisibleChunkIndex() const
Definition: NvBlastActor.h:547
#define NV_INLINE
Definition: NvPreprocessor.h:350
uint32_t m_nodeCount
Definition: NvBlastSupportGraph.h:81
FamilyGraph * getFamilyGraph() const
Definition: NvBlastActor.h:580
Definition: NvBlastIteratorBase.h:121
Definition: NvBlastDLink.h:50
Definition: NvBlastTypes.h:152
uint32_t partition(Actor **newActors, uint32_t newActorsSize, NvBlastLog logFn)
Definition: NvBlastActor.h:664
float * getSubsupportChunkHealths() const
Definition: NvBlastFamily.h:113
SupportGraph m_graph
Definition: NvBlastAsset.h:197
Definition: NvBlastIteratorBase.h:88
Definition: NvBlastTypes.h:468
Definition: NvBlastActor.h:58
bool release()
Definition: NvBlastActor.h:616
uint32_t getIndex() const
Definition: NvBlastActor.h:530
VisibleChunkIt(const Actor &actor)
Definition: NvBlastActor.h:729
uint32_t getFirstGraphNodeIndex() const
Definition: NvBlastActor.h:565
uint32_t getGraphNodeCount() const
Definition: NvBlastActor.h:553
static Actor * deserialize(NvBlastFamily *family, const void *buffer, NvBlastLog logFn)
void returnActor(Actor &actor)
Definition: NvBlastFamily.h:348
uint32_t serialize(void *buffer, uint32_t bufferSize, NvBlastLog logFn) const
Definition: NvBlastArray.h:37
uint32_t partitionSingleLowerSupportChunk(Actor **newActors, uint32_t newActorsSize, NvBlastLog logFn)
float * getLowerSupportChunkHealths() const
Definition: NvBlastActor.h:595
bool isSubSupportChunk() const
Definition: NvBlastActor.h:518
Definition: NvBlastTypes.h:387
Definition: NvBlastAsset.h:45
Definition: NvBlastActor.h:253
Definition: NvBlastTypes.h:376
uint32_t firstChildIndex
Definition: NvBlastTypes.h:172