optimizing lighting performance a little

This commit is contained in:
Dylan K. Taylor 2019-07-08 15:39:57 +01:00
parent 007aee72f8
commit c42817f02f
4 changed files with 31 additions and 25 deletions

View File

@ -27,13 +27,8 @@ use pocketmine\block\BlockFactory;
use function max; use function max;
class BlockLightUpdate extends LightUpdate{ class BlockLightUpdate extends LightUpdate{
protected function updateLightArrayRef() : void{
public function getLight(int $x, int $y, int $z) : int{ $this->currentLightArray = $this->subChunkHandler->currentSubChunk->getBlockLightArray();
return $this->subChunkHandler->currentSubChunk->getBlockLight($x & 0x0f, $y & 0x0f, $z & 0x0f);
}
public function setLight(int $x, int $y, int $z, int $level) : void{
$this->subChunkHandler->currentSubChunk->setBlockLight($x & 0x0f, $y & 0x0f, $z & 0x0f, $level);
} }
public function recalculateNode(int $x, int $y, int $z) : void{ public function recalculateNode(int $x, int $y, int $z) : void{

View File

@ -25,6 +25,7 @@ namespace pocketmine\world\light;
use pocketmine\block\BlockFactory; use pocketmine\block\BlockFactory;
use pocketmine\world\ChunkManager; use pocketmine\world\ChunkManager;
use pocketmine\world\format\LightArray;
use pocketmine\world\utils\SubChunkIteratorManager; use pocketmine\world\utils\SubChunkIteratorManager;
use pocketmine\world\World; use pocketmine\world\World;
use function max; use function max;
@ -50,17 +51,19 @@ abstract class LightUpdate{
/** @var SubChunkIteratorManager */ /** @var SubChunkIteratorManager */
protected $subChunkHandler; protected $subChunkHandler;
/** @var LightArray|null */
protected $currentLightArray = null;
public function __construct(ChunkManager $world){ public function __construct(ChunkManager $world){
$this->world = $world; $this->world = $world;
$this->removalQueue = new \SplQueue(); $this->removalQueue = new \SplQueue();
$this->spreadQueue = new \SplQueue(); $this->spreadQueue = new \SplQueue();
$this->subChunkHandler = new SubChunkIteratorManager($this->world); $this->subChunkHandler = new SubChunkIteratorManager($this->world);
$this->subChunkHandler->onSubChunkChange(\Closure::fromCallable([$this, 'updateLightArrayRef']));
} }
abstract protected function getLight(int $x, int $y, int $z) : int; abstract protected function updateLightArrayRef() : void;
abstract protected function setLight(int $x, int $y, int $z, int $level) : void;
abstract public function recalculateNode(int $x, int $y, int $z) : void; abstract public function recalculateNode(int $x, int $y, int $z) : void;
@ -74,7 +77,7 @@ abstract class LightUpdate{
[$x, $y, $z + 1], [$x, $y, $z + 1],
[$x, $y, $z - 1] [$x, $y, $z - 1]
] as [$x1, $y1, $z1]){ ] as [$x1, $y1, $z1]){
if($this->subChunkHandler->moveTo($x1, $y1, $z1, false) and ($adjacent = max($adjacent, $this->getLight($x1, $y1, $z1))) === 15){ if($this->subChunkHandler->moveTo($x1, $y1, $z1, false) and ($adjacent = max($adjacent, $this->currentLightArray->get($x1 & 0xf, $y1 & 0xf, $z1 & 0xf))) === 15){
break; break;
} }
} }
@ -88,10 +91,10 @@ abstract class LightUpdate{
private function prepareNodes() : void{ private function prepareNodes() : void{
foreach($this->updateNodes as $blockHash => [$x, $y, $z, $newLevel]){ foreach($this->updateNodes as $blockHash => [$x, $y, $z, $newLevel]){
if($this->subChunkHandler->moveTo($x, $y, $z, false)){ if($this->subChunkHandler->moveTo($x, $y, $z, false)){
$oldLevel = $this->getLight($x, $y, $z); $oldLevel = $this->currentLightArray->get($x & 0xf, $y & 0xf, $z & 0xf);
if($oldLevel !== $newLevel){ if($oldLevel !== $newLevel){
$this->setLight($x, $y, $z, $newLevel); $this->currentLightArray->set($x & 0xf, $y & 0xf, $z & 0xf, $newLevel);
if($oldLevel < $newLevel){ //light increased if($oldLevel < $newLevel){ //light increased
$this->spreadVisited[$blockHash] = true; $this->spreadVisited[$blockHash] = true;
$this->spreadQueue->enqueue([$x, $y, $z]); $this->spreadQueue->enqueue([$x, $y, $z]);
@ -135,7 +138,7 @@ abstract class LightUpdate{
continue; continue;
} }
$newAdjacentLight = $this->getLight($x, $y, $z); $newAdjacentLight = $this->currentLightArray->get($x & 0xf, $y & 0xf, $z & 0xf);
if($newAdjacentLight <= 0){ if($newAdjacentLight <= 0){
continue; continue;
} }
@ -158,10 +161,10 @@ abstract class LightUpdate{
} }
protected function computeRemoveLight(int $x, int $y, int $z, int $oldAdjacentLevel) : void{ protected function computeRemoveLight(int $x, int $y, int $z, int $oldAdjacentLevel) : void{
$current = $this->getLight($x, $y, $z); $current = $this->currentLightArray->get($x & 0xf, $y & 0xf, $z & 0xf);
if($current !== 0 and $current < $oldAdjacentLevel){ if($current !== 0 and $current < $oldAdjacentLevel){
$this->setLight($x, $y, $z, 0); $this->currentLightArray->set($x & 0xf, $y & 0xf, $z & 0xf, 0);
if(!isset($this->removalVisited[$index = World::blockHash($x, $y, $z)])){ if(!isset($this->removalVisited[$index = World::blockHash($x, $y, $z)])){
$this->removalVisited[$index] = true; $this->removalVisited[$index] = true;
@ -178,11 +181,11 @@ abstract class LightUpdate{
} }
protected function computeSpreadLight(int $x, int $y, int $z, int $newAdjacentLevel) : void{ protected function computeSpreadLight(int $x, int $y, int $z, int $newAdjacentLevel) : void{
$current = $this->getLight($x, $y, $z); $current = $this->currentLightArray->get($x & 0xf, $y & 0xf, $z & 0xf);
$potentialLight = $newAdjacentLevel - BlockFactory::$lightFilter[$this->subChunkHandler->currentSubChunk->getFullBlock($x & 0x0f, $y & 0x0f, $z & 0x0f)]; $potentialLight = $newAdjacentLevel - BlockFactory::$lightFilter[$this->subChunkHandler->currentSubChunk->getFullBlock($x & 0x0f, $y & 0x0f, $z & 0x0f)];
if($current < $potentialLight){ if($current < $potentialLight){
$this->setLight($x, $y, $z, $potentialLight); $this->currentLightArray->set($x & 0xf, $y & 0xf, $z & 0xf, $potentialLight);
if(!isset($this->spreadVisited[$index = World::blockHash($x, $y, $z)]) and $potentialLight > 1){ if(!isset($this->spreadVisited[$index = World::blockHash($x, $y, $z)]) and $potentialLight > 1){
$this->spreadVisited[$index] = true; $this->spreadVisited[$index] = true;

View File

@ -27,13 +27,8 @@ use pocketmine\block\BlockFactory;
use function max; use function max;
class SkyLightUpdate extends LightUpdate{ class SkyLightUpdate extends LightUpdate{
protected function updateLightArrayRef() : void{
public function getLight(int $x, int $y, int $z) : int{ $this->currentLightArray = $this->subChunkHandler->currentSubChunk->getBlockSkyLightArray();
return $this->subChunkHandler->currentSubChunk->getBlockSkyLight($x & 0x0f, $y & 0x0f, $z & 0x0f);
}
public function setLight(int $x, int $y, int $z, int $level) : void{
$this->subChunkHandler->currentSubChunk->setBlockSkyLight($x & 0x0f, $y & 0x0f, $z & 0x0f, $level);
} }
public function recalculateNode(int $x, int $y, int $z) : void{ public function recalculateNode(int $x, int $y, int $z) : void{

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\world\utils; namespace pocketmine\world\utils;
use pocketmine\utils\Utils;
use pocketmine\world\ChunkManager; use pocketmine\world\ChunkManager;
use pocketmine\world\format\Chunk; use pocketmine\world\format\Chunk;
use pocketmine\world\format\EmptySubChunk; use pocketmine\world\format\EmptySubChunk;
@ -44,6 +45,9 @@ class SubChunkIteratorManager{
/** @var int */ /** @var int */
protected $currentZ; protected $currentZ;
/** @var \Closure|null */
private $onSubChunkChangeFunc = null;
public function __construct(ChunkManager $world){ public function __construct(ChunkManager $world){
$this->world = $world; $this->world = $world;
} }
@ -65,13 +69,22 @@ class SubChunkIteratorManager{
$this->currentSubChunk = $this->currentChunk->getSubChunk($y >> 4, $create); $this->currentSubChunk = $this->currentChunk->getSubChunk($y >> 4, $create);
if($this->currentSubChunk instanceof EmptySubChunk){ if($this->currentSubChunk instanceof EmptySubChunk){
$this->currentSubChunk = null;
return false; return false;
} }
if($this->onSubChunkChangeFunc !== null){
($this->onSubChunkChangeFunc)();
}
} }
return true; return true;
} }
public function onSubChunkChange(\Closure $callback) : void{
Utils::validateCallableSignature(function(){}, $callback);
$this->onSubChunkChangeFunc = $callback;
}
public function invalidate() : void{ public function invalidate() : void{
$this->currentChunk = null; $this->currentChunk = null;
$this->currentSubChunk = null; $this->currentSubChunk = null;