Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TileScheduler.h
Go to the documentation of this file.
1#pragma once
2
4#include "Math/math.h"
5#include "Math/constant.h"
7
8#include <Common/assertion.h>
9
10#include <algorithm>
11
12namespace ph
13{
14
19{
20public:
21 enum class EOrigin
22 {
27 };
28
30
32 std::size_t numWorkers,
33 const WorkUnit& totalWorkUnit,
34 const math::Vector2S& tileSize);
35
37 std::size_t numWorkers,
38 const WorkUnit& totalWorkUnit,
39 const math::Vector2S& tileSize,
40 EOrigin origin,
41 std::size_t prioriAxis);
42
43private:
44 GridScheduler m_grid;
45
46 void scheduleOne(WorkUnit* out_workUnit) override;
47
48 constexpr static GridScheduler::EOrigin toGridOrigin(EOrigin origin);
49};
50
51// In-header Implementations:
52
55 , m_grid()
56{}
57
59 const std::size_t numWorkers,
60 const WorkUnit& totalWorkUnit,
61 const math::Vector2S& tileSize) :
62
64 numWorkers,
65 totalWorkUnit,
66 tileSize,
67 EOrigin::LowerLeft,
68 math::constant::X_AXIS)// default to X-first as it is likely more cache friendly
69{}
70
72 const std::size_t numWorkers,
73 const WorkUnit& totalWorkUnit,
74 const math::Vector2S& tileSize,
75 const EOrigin origin,
76 const std::size_t prioriAxis)
77
78 : WorkScheduler(numWorkers, totalWorkUnit)
79
80 , m_grid()
81{
82 PH_ASSERT_MSG(tileSize.product() > 0, tileSize.toString());
83
84 // Since grid scheduler slices work region evenly, we need to round up the
85 // grid size to make each slice of size `tileSize`
86 math::TVector2<int64> gridSize(
87 math::ceil_div(totalWorkUnit.getWidth(), static_cast<int64>(tileSize.x())),
88 math::ceil_div(totalWorkUnit.getHeight(), static_cast<int64>(tileSize.y())));
89 gridSize.mulLocal(math::TVector2<int64>(tileSize));
90
91 const Region gridWorkRegion(
92 totalWorkUnit.getRegion().getMinVertex(),
93 totalWorkUnit.getRegion().getMinVertex() + gridSize);
94
95 m_grid = GridScheduler(
96 numWorkers,
97 WorkUnit(gridWorkRegion, totalWorkUnit.getDepth()),
98 math::Vector2S(gridWorkRegion.getExtents()) / tileSize,
99 toGridOrigin(origin),
100 prioriAxis);
101}
102
103inline void TileScheduler::scheduleOne(WorkUnit* const out_workUnit)
104{
105 PH_ASSERT(out_workUnit);
106
107 // Tile coordinates are always in the canonical Cartesian space, mapping
108 // is performed later on.
109
110 WorkUnit gridWorkUnit;
111 if(m_grid.schedule(&gridWorkUnit))
112 {
113 Region tileWorkRegion = gridWorkUnit.getRegion();
114 tileWorkRegion.intersectWith(m_totalWorkUnit.getRegion());
115
116 if(!tileWorkRegion.isEmpty())
117 {
118 *out_workUnit = WorkUnit(tileWorkRegion, gridWorkUnit.getDepth());
119 return;
120 }
121 }
122
123 *out_workUnit = WorkUnit();
124}
125
126inline constexpr GridScheduler::EOrigin TileScheduler::toGridOrigin(const EOrigin origin)
127{
128 switch(origin)
129 {
134
135 default: return GridScheduler::EOrigin::LowerLeft;
136 }
137}
138
139}// end namespace ph
Definition GridScheduler.h:20
EOrigin
Definition GridScheduler.h:23
Definition TileScheduler.h:19
TileScheduler()
Definition TileScheduler.h:53
EOrigin
Definition TileScheduler.h:22
A manager that distributes a fixed amount of work to workers.
Definition WorkScheduler.h:21
bool schedule(WorkUnit *out_workUnit)
Get some amount of work.
Definition WorkScheduler.h:85
WorkUnit m_totalWorkUnit
Definition WorkScheduler.h:55
Represents some amount of work.
Definition WorkUnit.h:17
Region getRegion() const
Definition WorkUnit.h:72
int64 getHeight() const
Definition WorkUnit.h:67
std::size_t getDepth() const
Definition WorkUnit.h:77
int64 getWidth() const
Definition WorkUnit.h:62
TVector2< T > getExtents() const
Get the side lengths of the bound.
Definition TAABB2D.ipp:150
TAABB2D & intersectWith(const TAABB2D &other)
Definition TAABB2D.ipp:86
bool isEmpty() const
Definition TAABB2D.ipp:193
const TVector2< T > & getMinVertex() const
Definition TAABB2D.ipp:120
T & x()
Definition TVector2.ipp:38
T & y()
Definition TVector2.ipp:44
T product() const
Definition TArithmeticArrayBase.ipp:358
std::string toString() const
Definition TArithmeticArrayBase.ipp:825
Derived & mulLocal(const Derived &rhs)
Definition TArithmeticArrayBase.ipp:112
Miscellaneous math utilities.
The root for all renderer implementations.
Definition EEngineProject.h:6