Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
partitioner.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef PARTITIONER_H
20 #define PARTITIONER_H
21 
22 #include <memory>
23 #include <set>
24 #include <map>
25 #include <vector>
26 
27 #include "generic/containers/contiguous_map.h"
28 #include "generic/property.h"
29 #include "generic/managed.h"
30 #include "renderers/renderer.h"
31 #include "types.h"
32 #include "interfaces.h"
33 
34 namespace smlt {
35 
36 class StageNode;
37 class SubActor;
38 
39 enum WriteOperation {
40  WRITE_OPERATION_ADD,
41  WRITE_OPERATION_UPDATE,
42  WRITE_OPERATION_REMOVE,
43  WRITE_OPERATION_MAX
44 };
45 
46 struct StagedWrite {
47  WriteOperation operation;
48  AABB new_bounds;
49  StageNode* node = nullptr;
50 };
51 
52 #define MAX_STAGED_WRITES 1024
53 
55  public RefCounted<Partitioner> {
56 
57 public:
58  struct WriteSlots {
59  StagedWrite slot[WRITE_OPERATION_MAX];
60  uint8_t bits = 0;
61  };
62 
63  Partitioner(Stage* ss):
64  stage_(ss) {}
65 
66  void add_stage_node(StageNode* node) {
67  StagedWrite write;
68  write.operation = WRITE_OPERATION_ADD;
69  write.node = node;
70  stage_write(node, write);
71  }
72 
73  void update_stage_node(StageNode* node, const AABB& bounds) {
74  StagedWrite write;
75  write.operation = WRITE_OPERATION_UPDATE;
76  write.new_bounds = bounds;
77  write.node = node;
78  stage_write(node, write);
79  }
80 
81  void remove_stage_node(StageNode* node) {
82  StagedWrite write;
83  write.operation = WRITE_OPERATION_REMOVE;
84  write.node = node;
85  stage_write(node, write);
86  }
87 
88  void _apply_writes();
89 
90  virtual void lights_and_geometry_visible_from(
91  CameraID camera_id,
92  std::vector<LightID>& lights_out,
93  std::vector<StageNode*>& geom_out
94  ) = 0;
95 
96  virtual MeshID debug_mesh_id() { return MeshID(); }
97 protected:
98  Stage* get_stage() const { return stage_; }
99 
100  virtual void apply_staged_write(const UniqueIDKey& key, const StagedWrite& write) = 0;
101 
102  void stage_write(StageNode* node, const StagedWrite& op);
103 
104 private:
105  Stage* stage_;
106 
107  thread::Mutex staging_lock_;
108 
109  std::vector<StageNode*> staged_writes_;
110  std::unordered_map<StageNode*, UniqueIDKey> removed_nodes_;
111 
112 protected:
113  Property<decltype(&Partitioner::stage_)> stage = { this, &Partitioner::stage_ };
114 
115 };
116 
117 }
118 
119 namespace std {
120  DEFINE_ENUM_HASH(smlt::WriteOperation);
121 }
122 
123 #endif // PARTITIONER_H
smlt::StagedWrite
Definition: partitioner.h:46
smlt::Partitioner::WriteSlots
Definition: partitioner.h:58
smlt::Stage
Definition: stage.h:80
smlt::Partitioner
Definition: partitioner.h:55
smlt::RefCounted
Definition: managed.h:65
smlt
Definition: animation.cpp:25
smlt::AABB
Definition: aabb.h:22
smlt::StageNode
Definition: stage_node.h:62
std
Extensions to the C++ standard library.
Definition: unique_id.h:200