d3d example minecraft engine porting, some fixes and optimizations

This commit is contained in:
Vadim Lopatin 2016-03-22 10:40:59 +03:00
parent 73f63090f5
commit 3671b1f2cb
6 changed files with 83 additions and 79 deletions

View File

@ -339,7 +339,7 @@
<useArrayBounds>0</useArrayBounds> <useArrayBounds>0</useArrayBounds>
<noboundscheck>0</noboundscheck> <noboundscheck>0</noboundscheck>
<useSwitchError>0</useSwitchError> <useSwitchError>0</useSwitchError>
<useUnitTests>1</useUnitTests> <useUnitTests>0</useUnitTests>
<useInline>0</useInline> <useInline>0</useInline>
<release>0</release> <release>0</release>
<preservePaths>0</preservePaths> <preservePaths>0</preservePaths>

View File

@ -44,7 +44,7 @@
<pic>0</pic> <pic>0</pic>
<cov>0</cov> <cov>0</cov>
<nofloat>0</nofloat> <nofloat>0</nofloat>
<Dversion>2.043</Dversion> <Dversion>2</Dversion>
<ignoreUnsupportedPragmas>0</ignoreUnsupportedPragmas> <ignoreUnsupportedPragmas>0</ignoreUnsupportedPragmas>
<allinst>0</allinst> <allinst>0</allinst>
<stackStomp>0</stackStomp> <stackStomp>0</stackStomp>

View File

@ -122,13 +122,18 @@ class UiWidget : VerticalLayout {
} }
World w = new World(); World w = new World();
for (int x = -100; x < 100; x++) for (int x = -1000; x < 1000; x++)
for (int z = -100; z < 100; z++) for (int z = -1000; z < 1000; z++)
w.setCell(x, 10, z, 1); w.setCell(x, 10, z, 1);
Position position = Position(Vector3d(0, 13, 0), Vector3d(0, 1, 0)); w.setCell(0, 11, 10, 2);
w.setCell(5, 11, 15, 2);
Position position = Position(Vector3d(0, 13, 0), Vector3d(0, 0, 1));
CellVisitor visitor = new TestVisitor(); CellVisitor visitor = new TestVisitor();
Log.d("Testing cell visitor"); Log.d("Testing cell visitor");
long ts = currentTimeMillis;
w.visitVisibleCells(position, visitor); w.visitVisibleCells(position, visitor);
long duration = currentTimeMillis - ts;
Log.d("DiamondVisitor finished in ", duration, " ms");
destroy(w); destroy(w);
} }
@ -211,14 +216,14 @@ class UiWidget : VerticalLayout {
} }
class TestVisitor : CellVisitor { class TestVisitor : CellVisitor {
void newDirection(ref Position camPosition) { //void newDirection(ref Position camPosition) {
Log.d("TestVisitor.newDirection"); // Log.d("TestVisitor.newDirection");
} //}
void visitFace(World world, ref Position camPosition, Vector3d pos, cell_t cell, Dir face) { //void visitFace(World world, ref Position camPosition, Vector3d pos, cell_t cell, Dir face) {
Log.d("TestVisitor.visitFace ", pos, " cell=", cell, " face=", face); // Log.d("TestVisitor.visitFace ", pos, " cell=", cell, " face=", face);
} //}
void visit(World world, ref Position camPosition, Vector3d pos, cell_t cell, int visibleFaces) { void visit(World world, ref Position camPosition, Vector3d pos, cell_t cell, int visibleFaces) {
Log.d("TestVisitor.visit ", pos, " cell=", cell); //Log.d("TestVisitor.visit ", pos, " cell=", cell);
} }
} }

View File

@ -66,15 +66,15 @@ public:
// block type definitions // block type definitions
__gshared BlockDef BLOCK_DEFS[256]; __gshared BlockDef[256] BLOCK_DEFS;
// faster check for block->canPass() // faster check for block->canPass()
__gshared bool BLOCK_TYPE_CAN_PASS[256]; __gshared bool[256] BLOCK_TYPE_CAN_PASS;
// faster check for block->isOpaque() // faster check for block->isOpaque()
__gshared bool BLOCK_TYPE_OPAQUE[256]; __gshared bool[256] BLOCK_TYPE_OPAQUE;
// faster check for block->isVisible() // faster check for block->isVisible()
__gshared bool BLOCK_TYPE_VISIBLE[256]; __gshared bool[256] BLOCK_TYPE_VISIBLE;
// faster check for block->isVisible() // faster check for block->isVisible()
__gshared bool BLOCK_TERRAIN_SMOOTHING[256]; __gshared bool[256] BLOCK_TERRAIN_SMOOTHING;
/// registers new block type /// registers new block type
void registerBlockType(BlockDef def) { void registerBlockType(BlockDef def) {

View File

@ -265,7 +265,7 @@ public:
newsize <<= 1; newsize <<= 1;
_data.length = newsize; _data.length = newsize;
for (int i = oldsize; i < newsize; i++) for (int i = oldsize; i < newsize; i++)
_data[i] = T.init; _data.ptr[i] = T.init;
_data.assumeSafeAppend(); _data.assumeSafeAppend();
} }
} }
@ -275,29 +275,29 @@ public:
/// append single item by ref /// append single item by ref
void append(ref const T value) { void append(ref const T value) {
if (_length >= _data.length) if (_length >= _data.length)
reserve(_data.length == 0 ? 64 : _data.length * 2 - _length); reserve(cast(int)(_data.length == 0 ? 64 : _data.length * 2 - _length));
_data[_length++] = value; _data.ptr[_length++] = value;
} }
/// append single item by value /// append single item by value
void append(T value) { void append(T value) {
if (_length >= _data.length) if (_length >= _data.length)
reserve(_data.length == 0 ? 64 : _data.length * 2 - _length); reserve(cast(int)(_data.length == 0 ? 64 : _data.length * 2 - _length));
_data[_length++] = value; _data.ptr[_length++] = value;
} }
/// append single item w/o check /// append single item w/o check
void appendNoCheck(ref const T value) { void appendNoCheck(ref const T value) {
_data[_length++] = value; _data.ptr[_length++] = value;
} }
/// append single item w/o check /// append single item w/o check
void appendNoCheck(T value) { void appendNoCheck(T value) {
_data[_length++] = value; _data.ptr[_length++] = value;
} }
/// appends same value several times, return pointer to appended items /// appends same value several times, return pointer to appended items
T* append(ref const T value, int count) { T* append(ref const T value, int count) {
reserve(count); reserve(count);
int startLen = _length; int startLen = _length;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
_data[_length++] = value; _data.ptr[_length++] = value;
return _data.ptr + startLen; return _data.ptr + startLen;
} }
/// appends same value several times, return pointer to appended items /// appends same value several times, return pointer to appended items
@ -305,20 +305,20 @@ public:
reserve(count); reserve(count);
int startLen = _length; int startLen = _length;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
_data[_length++] = value; _data.ptr[_length++] = value;
return _data.ptr + startLen; return _data.ptr + startLen;
} }
void clear() { void clear() {
_length = 0; _length = 0;
} }
T get(int index) { T get(int index) {
return _data[index]; return _data.ptr[index];
} }
void set(int index, T value) { void set(int index, T value) {
_data[index] = value; _data.ptr[index] = value;
} }
ref T opIndex(int index) { ref T opIndex(int index) {
return _data[index]; return _data.ptr[index];
} }
} }
@ -439,7 +439,7 @@ public:
T get(int x, int y) { T get(int x, int y) {
if (x < -_size || x >= _size || y < -_size || y >= _size) if (x < -_size || x >= _size || y < -_size || y >= _size)
return null; return null;
return _data[calcIndex(x, y)]; return _data.ptr[calcIndex(x, y)];
} }
void set(int x, int y, T v) { void set(int x, int y, T v) {
if (x < -_size || x >= _size || y < -_size || y >= _size) { if (x < -_size || x >= _size || y < -_size || y >= _size) {
@ -453,9 +453,9 @@ public:
resize(newSizeShift); resize(newSizeShift);
} }
int index = calcIndex(x, y); int index = calcIndex(x, y);
if (_data[index]) if (_data.ptr[index])
destroy(_data[index]); destroy(_data.ptr[index]);
_data[index] = v; _data.ptr[index] = v;
} }
~this() { ~this() {
foreach(ref v; _data) foreach(ref v; _data)
@ -710,20 +710,20 @@ struct Direction {
/// returns number of bits to store integer /// returns number of bits to store integer
int bitsFor(int n) { int bitsFor(int n) {
int res; int res;
for (res = 0; n > 0; res++) for (res = 0; n > 0; res++)
n >>= 1; n >>= 1;
return res; return res;
} }
/// returns 0 for 0, 1 for negatives, 2 for positives /// returns 0 for 0, 1 for negatives, 2 for positives
int mySign(int n) { int mySign(int n) {
if (n > 0) if (n > 0)
return 1; return 1;
else if (n < 0) else if (n < 0)
return -1; return -1;
else else
return 0; return 0;
} }
immutable ulong RANDOM_MULTIPLIER = ((cast(ulong)1 << 48) - 1); immutable ulong RANDOM_MULTIPLIER = ((cast(ulong)1 << 48) - 1);
@ -748,11 +748,11 @@ struct Random {
int nextInt(int n); int nextInt(int n);
} }
const Vector3d DIRECTION_VECTORS[6] = [ const Vector3d[6] DIRECTION_VECTORS = [
Vector3d(0, 0, -1), Vector3d(0, 0, -1),
Vector3d(0, 0, 1), Vector3d(0, 0, 1),
Vector3d(-1, 0, 0), Vector3d(-1, 0, 0),
Vector3d(1, 0, 0), Vector3d(1, 0, 0),
Vector3d(0, 1, 0), Vector3d(0, 1, 0),
Vector3d(0, -1, 0) Vector3d(0, -1, 0)
]; ];

View File

@ -20,22 +20,22 @@ immutable int CHUNK_DY_MASK = (CHUNK_DY - 1);
// Layer is 256x16x16 CHUNK_DY layers = CHUNK_DY * (CHUNK_DX_SHIFT x CHUNK_DX_SHIFT) cells // Layer is 256x16x16 CHUNK_DY layers = CHUNK_DY * (CHUNK_DX_SHIFT x CHUNK_DX_SHIFT) cells
struct ChunkLayer { struct ChunkLayer {
private: private:
cell_t cells[CHUNK_DX * CHUNK_DX]; cell_t[CHUNK_DX * CHUNK_DX] cells;
public: public:
cell_t* ptr(int x, int z) { cell_t* ptr(int x, int z) {
return &cells[(z << CHUNK_DX_SHIFT) + x]; return &cells.ptr[(z << CHUNK_DX_SHIFT) + x];
} }
cell_t get(int x, int z) { cell_t get(int x, int z) {
return cells[(z << CHUNK_DX_SHIFT) + x]; return cells.ptr[(z << CHUNK_DX_SHIFT) + x];
} }
void set(int x, int z, cell_t cell) { void set(int x, int z, cell_t cell) {
cells[(z << CHUNK_DX_SHIFT) + x] = cell; cells.ptr[(z << CHUNK_DX_SHIFT) + x] = cell;
} }
} }
struct Chunk { struct Chunk {
private: private:
ChunkLayer * layers[CHUNK_DY]; ChunkLayer*[CHUNK_DY] layers;
int bottomLayer = - 1; int bottomLayer = - 1;
int topLayer = -1; int topLayer = -1;
public: public:
@ -62,10 +62,10 @@ public:
} }
void set(int x, int y, int z, cell_t cell) { void set(int x, int y, int z, cell_t cell) {
int layerIndex = y & CHUNK_DY_MASK; int layerIndex = y & CHUNK_DY_MASK;
ChunkLayer * layer = layers[layerIndex]; ChunkLayer * layer = layers.ptr[layerIndex];
if (!layer) { if (!layer) {
layer = new ChunkLayer(); layer = new ChunkLayer();
layers[layerIndex] = layer; layers.ptr[layerIndex] = layer;
if (topLayer == -1 || topLayer < layerIndex) if (topLayer == -1 || topLayer < layerIndex)
topLayer = layerIndex; topLayer = layerIndex;
if (bottomLayer == -1 || bottomLayer > layerIndex) if (bottomLayer == -1 || bottomLayer > layerIndex)
@ -155,8 +155,7 @@ public:
Chunk * p; Chunk * p;
if (lastChunkX == chunkx && lastChunkZ == chunkz) { if (lastChunkX == chunkx && lastChunkZ == chunkz) {
p = lastChunk; p = lastChunk;
} } else {
else {
p = chunks.get(chunkx, chunkz); p = chunks.get(chunkx, chunkz);
lastChunkX = chunkx; lastChunkX = chunkx;
lastChunkZ = chunkz; lastChunkZ = chunkz;
@ -181,34 +180,34 @@ public:
} }
interface CellVisitor { interface CellVisitor {
void newDirection(ref Position camPosition); //void newDirection(ref Position camPosition);
void visitFace(World world, ref Position camPosition, Vector3d pos, cell_t cell, Dir face); //void visitFace(World world, ref Position camPosition, Vector3d pos, cell_t cell, Dir face);
void visit(World world, ref Position camPosition, Vector3d pos, cell_t cell, int visibleFaces); void visit(World world, ref Position camPosition, Vector3d pos, cell_t cell, int visibleFaces);
} }
struct DiamondVisitor { struct DiamondVisitor {
int maxDist; int maxDist;
int maxDistBits; int maxDistBits;
int dist; int dist;
World world; World world;
Position * position; Position * position;
Vector3d pos0; Vector3d pos0;
CellVisitor visitor; CellVisitor visitor;
CellArray visited; CellArray visited;
cell_t * visited_ptr; cell_t * visited_ptr;
Vector3dArray oldcells; Vector3dArray oldcells;
Vector3dArray newcells; Vector3dArray newcells;
ubyte visitedOccupied; ubyte visitedOccupied;
ubyte visitedEmpty; ubyte visitedEmpty;
int m0; int m0;
int m0mask; int m0mask;
void init(World w, Position * pos, CellVisitor v) { void init(World w, Position * pos, CellVisitor v) {
world = w; world = w;
position = pos; position = pos;
visitor = v; visitor = v;
pos0 = position.pos; pos0 = position.pos;
} }
void visitCell(Vector3d v) { void visitCell(Vector3d v) {
//CRLog::trace("visitCell(%d %d %d) dist=%d", v.x, v.y, v.z, myAbs(v.x) + myAbs(v.y) + myAbs(v.z)); //CRLog::trace("visitCell(%d %d %d) dist=%d", v.x, v.y, v.z, myAbs(v.x) + myAbs(v.y) + myAbs(v.z));
//int occupied = visitedOccupied; //int occupied = visitedOccupied;
@ -288,7 +287,7 @@ struct DiamondVisitor {
newcells.clear(); newcells.clear();
visitedOccupied += 2; visitedOccupied += 2;
visitedEmpty += 2; visitedEmpty += 2;
//CRLog::trace("dist: %d cells: %d", dist, oldcells.length()); //CRLog::trace("dist: %d cells: %d", dist, oldcells.length());
for (int i = 0; i < oldcells.length(); i++) { for (int i = 0; i < oldcells.length(); i++) {
Vector3d pt = oldcells[i]; Vector3d pt = oldcells[i];
int sx = mySign(pt.x); int sx = mySign(pt.x);