Skip to content

Commit 9d485ca

Browse files
committed
Move ResourceArray to its own header
- Add docs.
1 parent 5e3b44a commit 9d485ca

File tree

4 files changed

+95
-34
lines changed

4 files changed

+95
-34
lines changed

lib/scene/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ target_sources(${PROJECT_NAME} PRIVATE
4343
include/${target_prefix}/scene/mesh.hpp
4444
include/${target_prefix}/scene/node_data.hpp
4545
include/${target_prefix}/scene/node.hpp
46+
include/${target_prefix}/scene/resource_array.hpp
4647
include/${target_prefix}/scene/scene_resources.hpp
4748
include/${target_prefix}/scene/scene.hpp
4849

lib/scene/include/facade/scene/material.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ struct TextureStore {
3939
};
4040

4141
///
42-
/// \brief Base Material: stores render parameters for a mesh.
42+
/// \brief Base class for concrete materials.
4343
///
4444
class MaterialBase {
4545
public:
4646
///
47-
/// \brief The alpha blend mode.
47+
/// \brief Alpha blend mode.
4848
///
4949
enum class AlphaMode : std::uint32_t { eOpaque = 0, eBlend, eMask };
5050

@@ -64,9 +64,15 @@ class MaterialBase {
6464
///
6565
virtual void write_sets(Pipeline& pipeline, TextureStore const& store) const = 0;
6666

67+
///
68+
/// \brief Name of this instance.
69+
///
6770
std::string name{"(Unnamed)"};
6871
};
6972

73+
///
74+
/// \brief Value-semantic strategy wrapper for concrete materials.
75+
///
7076
class Material {
7177
public:
7278
using AlphaMode = MaterialBase::AlphaMode;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#pragma once
2+
#include <facade/scene/id.hpp>
3+
#include <facade/util/ptr.hpp>
4+
#include <span>
5+
#include <utility>
6+
#include <vector>
7+
8+
namespace facade {
9+
///
10+
/// \brief Wrapper over an array of resources, each identified by the index as its Id.
11+
///
12+
/// Only Scene has access to the underlying vector (via friend). Other types can operate
13+
/// on const / non-const spans, but cannot modify the array itself.
14+
///
15+
template <typename T>
16+
class ResourceArray {
17+
public:
18+
///
19+
/// \brief Obtain an immutable view into the underlying array.
20+
/// \returns Immutable span
21+
///
22+
std::span<T const> view() const { return m_array; }
23+
///
24+
/// \brief Obtain a mutable view into the underlying array.
25+
/// \returns Mutable span
26+
///
27+
std::span<T> view() { return m_array; }
28+
29+
///
30+
/// \brief Obtain an immutable pointer to the element at index id.
31+
/// \param id Id (index) of element
32+
/// \returns nullptr if id is out of bounds
33+
///
34+
Ptr<T const> find(Id<T> id) const {
35+
if (id >= m_array.size()) { return {}; }
36+
return &m_array[id];
37+
}
38+
39+
///
40+
/// \brief Obtain a mutable pointer to the element at index id.
41+
/// \param id Id (index) of element
42+
/// \returns nullptr if id is out of bounds
43+
///
44+
Ptr<T> find(Id<T> index) { return const_cast<Ptr<T>>(std::as_const(*this).find(index)); }
45+
46+
///
47+
/// \brief Obtain an immutable reference to the element at index id.
48+
/// \param id Id (index) of element
49+
/// \returns const reference to element
50+
///
51+
/// id must be valid / in range
52+
///
53+
T const& operator[](Id<T> id) const {
54+
auto ret = find(id);
55+
assert(ret);
56+
return *ret;
57+
}
58+
59+
///
60+
/// \brief Obtain an immutable reference to the element at index id.
61+
/// \param id Id (index) of element
62+
/// \returns const reference to element
63+
///
64+
/// id must be valid / in range
65+
///
66+
T& operator[](Id<T> index) { return const_cast<T&>(std::as_const(*this).operator[](index)); }
67+
68+
///
69+
/// \brief Obtain the size of the underlying array.
70+
///
71+
constexpr std::size_t size() const { return m_array.size(); }
72+
///
73+
/// \brief Check if the underlying array is empty.
74+
///
75+
constexpr bool empty() const { return m_array.empty(); }
76+
77+
private:
78+
std::vector<T> m_array{};
79+
80+
friend class Scene;
81+
};
82+
} // namespace facade

lib/scene/include/facade/scene/scene_resources.hpp

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,14 @@
22
#include <facade/scene/camera.hpp>
33
#include <facade/scene/material.hpp>
44
#include <facade/scene/mesh.hpp>
5-
#include <facade/util/ptr.hpp>
5+
#include <facade/scene/resource_array.hpp>
66
#include <facade/vk/static_mesh.hpp>
77
#include <facade/vk/texture.hpp>
8-
#include <span>
98

109
namespace facade {
11-
template <typename T>
12-
class ResourceArray {
13-
public:
14-
std::span<T> view() { return m_array; }
15-
std::span<T const> view() const { return m_array; }
16-
17-
Ptr<T const> find(Id<T> index) const {
18-
if (index >= m_array.size()) { return {}; }
19-
return &m_array[index];
20-
}
21-
22-
Ptr<T> find(Id<T> index) { return const_cast<Ptr<T>>(std::as_const(*this).find(index)); }
23-
24-
T const& operator[](Id<T> index) const {
25-
auto ret = find(index);
26-
assert(ret);
27-
return *ret;
28-
}
29-
30-
T& operator[](Id<T> index) { return const_cast<T&>(std::as_const(*this).operator[](index)); }
31-
32-
constexpr std::size_t size() const { return m_array.size(); }
33-
constexpr bool empty() const { return m_array.empty(); }
34-
35-
private:
36-
std::vector<T> m_array{};
37-
38-
friend class Scene;
39-
};
40-
10+
///
11+
/// \brief Collection of all ResourceArray instances used by Scene.
12+
///
4113
struct SceneResources {
4214
ResourceArray<Camera> cameras{};
4315
ResourceArray<Sampler> samplers{};

0 commit comments

Comments
 (0)