Open3D (C++ API)  0.18.0+90810a3
RendererHandle.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <fmt/format.h>
11 
12 #include <array>
13 #include <cstdint>
14 #include <functional>
15 #include <type_traits>
16 
17 namespace open3d {
18 
19 namespace visualization {
20 namespace rendering {
21 
22 // If you add entry here, don't forget to update TypeToString!
23 enum class EntityType : std::uint16_t {
24  None = 0,
25 
26  View,
27  Scene,
28 
29  Geometry,
30  Light,
32  Skybox,
33  Camera,
34  Material,
36  Texture,
38 
41 
42  Count
43 };
44 
45 // RenderEntityHandle - handle type for entities inside Renderer
46 // Can be used in STL containers as key
48  static const char* TypeToString(EntityType type);
49 
50  static const std::uint16_t kBadId = 0;
52 
53  inline size_t Hash() const {
54  return static_cast<std::uint16_t>(type) << 16 | id;
55  }
56 
57  bool operator==(const REHandle_abstract& other) const {
58  return id == other.id && type == other.type;
59  }
60 
61  bool operator!=(const REHandle_abstract& other) const {
62  return !operator==(other);
63  }
64 
65  bool operator<(const REHandle_abstract& other) const {
66  return Hash() < other.Hash();
67  }
68 
69  explicit operator bool() const { return id != kBadId; }
70 
72 
73  std::uint16_t GetId() const { return id; }
74 
75 protected:
76  REHandle_abstract(const EntityType aType, const std::uint16_t aId)
77  : type(aType), id(aId) {}
78 
79  static std::array<std::uint16_t, static_cast<size_t>(EntityType::Count)>
81 
82  std::uint16_t id = kBadId;
83 };
84 
85 std::ostream& operator<<(std::ostream& os, const REHandle_abstract& uid);
86 
87 // REHandle is used for specification of handle types to prevent
88 // errors with passing, assigning or comparison of different kinds of handles
89 template <EntityType entityType>
90 struct REHandle : public REHandle_abstract {
91  static const REHandle kBad;
92 
93  static REHandle Next() {
94  const auto index = static_cast<std::uint16_t>(entityType);
95  auto id = ++uid_table[index];
96  if (id == REHandle_abstract::kBadId) {
99  }
100 
101  return REHandle(id);
102  }
103 
104  static REHandle Concretize(const REHandle_abstract& abstract) {
105  if (abstract.type != entityType) {
106  // assert("Incompatible render uid types!\n");
107  return REHandle();
108  }
109 
110  return REHandle(abstract.GetId());
111  }
112 
114  REHandle(const REHandle& other) : REHandle_abstract(entityType, other.id) {}
115  // Don't use this constructor unless you know what you are doing
116  explicit REHandle(std::uint16_t id) : REHandle_abstract(entityType, id) {}
117 
118  REHandle& operator=(const REHandle& other) {
119  id = other.id;
120  return *this;
121  }
122 };
123 
124 template <EntityType entityType>
125 const REHandle<entityType> REHandle<entityType>::kBad;
126 
140 
141 } // namespace rendering
142 } // namespace visualization
143 } // namespace open3d
144 
146 namespace std {
147 template <>
148 class hash<open3d::visualization::rendering::REHandle_abstract> {
149 public:
151  uid) const {
152  return uid.Hash();
153  }
154 };
155 } // namespace std
156 
157 namespace fmt {
158 template <typename T>
159 struct formatter<
160  T,
161  std::enable_if_t<std::is_base_of<open3d::visualization::rendering::
162  REHandle_abstract,
163  T>::value,
164  char>> {
165  template <typename FormatContext>
167  FormatContext& ctx) -> decltype(ctx.out()) {
168  return format_to(ctx.out(), "[{}, {}, hash: {}]",
170  TypeToString(uid.type),
171  uid.GetId(), uid.Hash());
172  }
173 
174  template <typename ParseContext>
175  constexpr auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
176  return ctx.begin();
177  }
178 };
179 } // namespace fmt
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:195
Definition: DLPack.h:195
REHandle< EntityType::IndexBuffer > IndexBufferHandle
Definition: RendererHandle.h:139
std::ostream & operator<<(std::ostream &os, const REHandle_abstract &uid)
Definition: RendererHandle.cpp:19
REHandle< EntityType::Material > MaterialHandle
Definition: RendererHandle.h:134
REHandle< EntityType::VertexBuffer > VertexBufferHandle
Definition: RendererHandle.h:138
REHandle< EntityType::Scene > SceneHandle
Definition: RendererHandle.h:128
EntityType
Definition: RendererHandle.h:23
REHandle< EntityType::MaterialInstance > MaterialInstanceHandle
Definition: RendererHandle.h:135
REHandle< EntityType::Geometry > GeometryHandle
Definition: RendererHandle.h:129
REHandle< EntityType::Camera > CameraHandle
Definition: RendererHandle.h:133
REHandle< EntityType::RenderTarget > RenderTargetHandle
Definition: RendererHandle.h:137
REHandle< EntityType::Light > LightHandle
Definition: RendererHandle.h:130
REHandle< EntityType::Texture > TextureHandle
Definition: RendererHandle.h:136
REHandle< EntityType::Skybox > SkyboxHandle
Definition: RendererHandle.h:132
REHandle< EntityType::View > ViewHandle
Definition: RendererHandle.h:127
REHandle< EntityType::IndirectLight > IndirectLightHandle
Definition: RendererHandle.h:131
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Device.h:111
bool operator!=(const REHandle_abstract &other) const
Definition: RendererHandle.h:61
std::uint16_t GetId() const
Definition: RendererHandle.h:73
bool operator<(const REHandle_abstract &other) const
Definition: RendererHandle.h:65
const EntityType type
Definition: RendererHandle.h:51
std::uint16_t id
Definition: RendererHandle.h:82
size_t Hash() const
Definition: RendererHandle.h:53
bool operator==(const REHandle_abstract &other) const
Definition: RendererHandle.h:57
REHandle_abstract(const EntityType aType, const std::uint16_t aId)
Definition: RendererHandle.h:76
REHandle_abstract()
Definition: RendererHandle.h:71
static const char * TypeToString(EntityType type)
Definition: RendererHandle.cpp:25
static std::array< std::uint16_t, static_cast< size_t >EntityType::Count)> uid_table
Definition: RendererHandle.h:80
static const std::uint16_t kBadId
Definition: RendererHandle.h:50
Definition: RendererHandle.h:90
REHandle(std::uint16_t id)
Definition: RendererHandle.h:116
static REHandle Next()
Definition: RendererHandle.h:93
REHandle(const REHandle &other)
Definition: RendererHandle.h:114
static REHandle Concretize(const REHandle_abstract &abstract)
Definition: RendererHandle.h:104
REHandle()
Definition: RendererHandle.h:113
REHandle & operator=(const REHandle &other)
Definition: RendererHandle.h:118
static const REHandle kBad
Definition: RendererHandle.h:91