-
Notifications
You must be signed in to change notification settings - Fork 78
Add VisibilityExtimator #465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
bluepilledgreat
wants to merge
6
commits into
ReMinecraftPE:master
Choose a base branch
from
bluepilledgreat:add-visibilityextimator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a65043
Add VisibilityExtimator
bluepilledgreat 76f8fcc
Update CMakeLists.txt
bluepilledgreat 988740f
Remove enum types
bluepilledgreat cd32796
Update CMakeLists.txt
bluepilledgreat 2d82efa
Fix switch errors
bluepilledgreat da9b662
Add assertion to VisibilityNode::from
bluepilledgreat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| #include "client/renderer/VisibilityExtimator.hpp" | ||
|
|
||
| VisibilityNode::VisibilityNode(bool empty) | ||
| { | ||
| if (empty) | ||
| setEmpty(); | ||
| else | ||
| setOpaque(); | ||
| } | ||
|
|
||
| void VisibilityNode::setEmpty() | ||
| { | ||
| for (int i = 0; i < Facing::MAX; i++) | ||
| { | ||
| m_visibility[i].setFull(); | ||
| } | ||
| } | ||
|
|
||
| void VisibilityNode::setOpaque() | ||
| { | ||
| for (int i = 0; i < Facing::MAX; i++) | ||
| { | ||
| m_visibility[i].setEmpty(); | ||
| } | ||
| } | ||
|
|
||
| void VisibilityNode::connect(const ByteMask& group) | ||
| { | ||
| for (uint8_t i = 0; i < Facing::MAX; i++) | ||
| { | ||
| if (group.contains(1 << i)) | ||
| { | ||
| connect(i, group); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void VisibilityNode::connect(uint8_t A, const ByteMask& connected) | ||
| { | ||
| for (uint8_t i = 0; i < Facing::MAX; i++) | ||
| { | ||
| if (connected.contains(1 << i)) | ||
| { | ||
| connect(A, i); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void VisibilityNode::connect(uint8_t A, uint8_t B) | ||
| { | ||
| if (A != B) | ||
| { | ||
| m_visibility[A].add(1 << B); | ||
| m_visibility[B].add(1 << A); | ||
| } | ||
| } | ||
|
|
||
| uint8_t& VisibilityExtimator::_at(const ChunkTilePos& p) | ||
| { | ||
| size_t index = p.y + (p.x << 8) + (p.z << 4); | ||
| assert(index < TILES_SIZE); | ||
| return m_tiles[index]; | ||
| } | ||
|
|
||
| uint8_t& VisibilityExtimator::_atWorld(const TilePos& t) | ||
| { | ||
| return _at(t - m_origin); | ||
| } | ||
|
|
||
| uint8_t* VisibilityExtimator::_at(const ChunkTilePos& pos, ByteMask& set) | ||
| { | ||
| if (pos.x > 128) | ||
| { | ||
| set.add(1 << Facing::WEST); | ||
| } | ||
| else if (pos.x > 15) | ||
| { | ||
| set.add(1 << Facing::EAST); | ||
| } | ||
| else if (pos.y > 128) | ||
| { | ||
| set.add(1 << Facing::DOWN); | ||
| } | ||
| else if (pos.y > 15) | ||
| { | ||
| set.add(1 << Facing::UP); | ||
| } | ||
| else if (pos.z > 128) | ||
| { | ||
| set.add(1 << Facing::NORTH); | ||
| } | ||
| else if (pos.z > 15) | ||
| { | ||
| set.add(1 << Facing::SOUTH); | ||
| } | ||
| else | ||
| { | ||
| return &_at(pos); | ||
| } | ||
|
|
||
| return nullptr; | ||
| } | ||
|
|
||
| void VisibilityExtimator::_visit(const ChunkTilePos& p, ByteMask& set) | ||
| { | ||
| uint8_t* tileState = _at(p, set); | ||
| if (tileState && *tileState == TS_EMPTY) | ||
| { | ||
| m_floodQueue.push_back(p); | ||
| } | ||
| } | ||
|
|
||
| ByteMask VisibilityExtimator::_floodFill(const ChunkTilePos& startPos) | ||
| { | ||
| ByteMask mask; | ||
|
|
||
| m_floodQueue.push_back(startPos); | ||
|
|
||
| while (!m_floodQueue.empty()) | ||
| { | ||
| const ChunkTilePos& pos = m_floodQueue.front(); | ||
|
|
||
| uint8_t& tileState = _at(pos); | ||
| if (tileState == TS_EMPTY) | ||
| { | ||
| tileState = TS_EMPTY_MARKED; | ||
|
|
||
| for (uint8_t face = 0; face < Facing::MAX; face += 3) | ||
| { | ||
| ChunkTilePos checkPos = pos + ChunkTilePos(face, face + 1, face + 2); | ||
| _visit(checkPos, mask); | ||
| } | ||
| } | ||
|
|
||
| m_floodQueue.pop_front(); | ||
| } | ||
|
|
||
| return mask; | ||
| } | ||
|
|
||
| void VisibilityExtimator::setTile(const TilePos& pos, const Tile* t) | ||
| { | ||
| if (t->isSolid()) | ||
| { | ||
| _atWorld(pos) = TS_OPAQUE; | ||
| m_emptyTiles--; | ||
| } | ||
| } | ||
|
|
||
| void VisibilityExtimator::start(const RenderChunk& parent) | ||
| { | ||
| m_origin = parent.m_pos; | ||
|
|
||
| memset(m_tiles, TS_EMPTY, TILES_SIZE); | ||
| m_emptyTiles = TILES_SIZE; | ||
| } | ||
|
|
||
| VisibilityNode VisibilityExtimator::finish() | ||
| { | ||
| if (isAllEmpty()) | ||
| return VisibilityNode(true); | ||
|
|
||
| VisibilityNode node(false); | ||
|
|
||
| ChunkTilePos p; | ||
| for (; p.x <= 15; p.x++) | ||
| { | ||
| for (; p.z <= 15; p.z++) | ||
| { | ||
| for (; p.y <= 15; p.y++) | ||
| { | ||
| if (_at(p) != TS_EMPTY) | ||
| continue; | ||
|
|
||
| ByteMask mask = _floodFill(p); | ||
| if (mask) | ||
| node.connect(mask); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return node; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| #pragma once | ||
|
|
||
| #include <deque> | ||
|
|
||
| #include "common/Stopwatch.hpp" | ||
| #include "common/ByteMask.hpp" | ||
| #include "world/level/TilePos.hpp" | ||
| #include "world/level/levelgen/chunk/ChunkTilePos.hpp" | ||
| #include "world/tile/Tile.hpp" | ||
| #include "client/renderer/RenderChunk.hpp" | ||
|
|
||
| class VisibilityNode | ||
| { | ||
| protected: | ||
| ByteMask m_visibility[Facing::MAX]; | ||
|
|
||
| public: | ||
| VisibilityNode(bool empty); | ||
| void setEmpty(); | ||
| void setOpaque(); | ||
| void connect(const ByteMask& group); | ||
| void connect(uint8_t A, const ByteMask& connected); | ||
| void connect(uint8_t A, uint8_t B); | ||
| const ByteMask& from(uint8_t facing) const | ||
| { | ||
| assert(facing < Facing::MAX); | ||
| return m_visibility[facing]; | ||
| } | ||
| bool compare(VisibilityNode& other) const; | ||
| }; | ||
|
|
||
| class VisibilityExtimator | ||
| { | ||
| protected: | ||
| static const int TILES_SIZE = 4096; | ||
| static const int TILE_COUNT_EMPTY_THRESHOLD = 3840; | ||
|
|
||
| protected: | ||
| enum TileState | ||
| { | ||
| TS_EMPTY, | ||
| TS_OPAQUE, | ||
| TS_EMPTY_MARKED | ||
| }; | ||
|
|
||
| public: | ||
| // TODO | ||
| //static ThreadLocal<VisibilityExtimator> pool; | ||
|
|
||
| public: | ||
| #ifdef _DEBUG | ||
| Stopwatch m_timer; | ||
| #endif | ||
| protected: | ||
| TilePos m_origin; | ||
| int m_emptyTiles; | ||
| uint8_t m_tiles[TILES_SIZE]; | ||
| std::deque<ChunkTilePos> m_floodQueue; | ||
|
|
||
| public: | ||
| VisibilityExtimator() | ||
| : m_origin(), | ||
| m_emptyTiles(0), | ||
| m_tiles() | ||
| { | ||
| } | ||
| public: | ||
| void start(const RenderChunk& parent); | ||
| void setTile(const TilePos& pos, const Tile* t); | ||
| bool isAllOpaque() const | ||
| { | ||
| return m_emptyTiles == 0; | ||
| } | ||
| bool isAllEmpty() const | ||
| { | ||
| return m_emptyTiles >= TILE_COUNT_EMPTY_THRESHOLD; | ||
| } | ||
| VisibilityNode finish(); | ||
| protected: | ||
| uint8_t* _at(const ChunkTilePos& pos, ByteMask& set); | ||
| uint8_t& _at(const ChunkTilePos& p); | ||
| uint8_t& _atWorld(const TilePos& t); | ||
| void _visit(const ChunkTilePos& p, ByteMask& set); | ||
| ByteMask _floodFill(const ChunkTilePos& startPos); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
Facing::COUNT?