Add performance
This commit is contained in:
parent
69577a9f6a
commit
aabe5b12f4
11 changed files with 212 additions and 38 deletions
|
@ -4,5 +4,8 @@
|
|||
<classpathentry kind="src" path="res"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/LWJGL 2.9.3"/>
|
||||
<classpathentry kind="lib" path="lib/affinity-1.6.jar" sourcepath="lib/affinity-1.6-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/jna-4.0.0.jar"/>
|
||||
<classpathentry kind="lib" path="lib/jna-platform-3.4.0.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
BIN
VBO/lib/affinity-1.6-sources.jar
Normal file
BIN
VBO/lib/affinity-1.6-sources.jar
Normal file
Binary file not shown.
BIN
VBO/lib/affinity-1.6.jar
Normal file
BIN
VBO/lib/affinity-1.6.jar
Normal file
Binary file not shown.
BIN
VBO/lib/jna-4.0.0.jar
Normal file
BIN
VBO/lib/jna-4.0.0.jar
Normal file
Binary file not shown.
BIN
VBO/lib/jna-platform-3.4.0.jar
Normal file
BIN
VBO/lib/jna-platform-3.4.0.jar
Normal file
Binary file not shown.
|
@ -5,13 +5,18 @@ import world.*;
|
|||
public class Game {
|
||||
|
||||
private World world;
|
||||
private int update = 0;
|
||||
|
||||
public Game(){
|
||||
world= new World(0);
|
||||
}
|
||||
|
||||
public void update(){
|
||||
world.update();
|
||||
if(update >= 2){
|
||||
world.update();
|
||||
update = 0;
|
||||
}
|
||||
update++;
|
||||
}
|
||||
|
||||
public void render(){
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
package main;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import org.lwjgl.input.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import rendering.*;
|
||||
import vanilla.java.affinity.*;
|
||||
import vanilla.java.affinity.impl.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
|
@ -16,8 +20,9 @@ public class Main {
|
|||
private static final String TITLE = "Test VBO";
|
||||
private static final int width = 1280, height = 720;
|
||||
|
||||
private static Thread[] threadArray;
|
||||
|
||||
private static AffinityLock al;
|
||||
|
||||
private static Game game;
|
||||
|
||||
/**
|
||||
|
@ -25,13 +30,16 @@ public class Main {
|
|||
* @Info Fonction principal
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// mainPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
|
||||
// AffinityLock.cpuLayout(new NoCpuLayout(Runtime.getRuntime().availableProcessors() + 8));
|
||||
al = AffinityLock.acquireLock();
|
||||
System.out.println(AffinityLock.cpuLayout().coresPerSocket());
|
||||
try {
|
||||
Display.setTitle(TITLE);
|
||||
Display.setDisplayMode(new DisplayMode(width, height));
|
||||
Display.setResizable(true);
|
||||
Mouse.setGrabbed(true);
|
||||
Display.create();
|
||||
threadArray = new Thread[Runtime.getRuntime().availableProcessors()];
|
||||
game = new Game();
|
||||
loop();
|
||||
} catch (Exception e) {
|
||||
|
@ -39,16 +47,10 @@ public class Main {
|
|||
}
|
||||
}
|
||||
|
||||
public static Thread addThread(Thread t){
|
||||
for(Thread c : threadArray){
|
||||
c = t;
|
||||
try {
|
||||
c.join();
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public static Runnable addThread(Runnable t,String name){
|
||||
// mainPool.execute(t);
|
||||
new Thread(t, name).start();
|
||||
// System.out.println("Details" + AffinityLock.dumpLocks());
|
||||
return t;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package world;
|
||||
|
||||
import vanilla.java.affinity.*;
|
||||
import blocks.*;
|
||||
import main.*;
|
||||
import math.*;
|
||||
|
@ -14,12 +15,15 @@ public class Chunk {
|
|||
private boolean IsLoad = false;
|
||||
private boolean IsGenerated = false;
|
||||
private boolean IsCurrentGenerate = false;
|
||||
private boolean IsDestroy = false;
|
||||
private WorldNoise worldNoise;
|
||||
|
||||
public Chunk(int x, int y, int z, World world) {
|
||||
public Chunk(int x, int y, int z, World world,WorldNoise worldNoise) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.world = world;
|
||||
this.worldNoise = worldNoise;
|
||||
this.blocks = new Block[SIZE][SIZE][SIZE];
|
||||
vbo = new VBO();
|
||||
}
|
||||
|
@ -35,7 +39,7 @@ public class Chunk {
|
|||
public void createChunk(World world) {
|
||||
this.world = world;
|
||||
|
||||
Main.addThread((new Thread(new Generate(this, world)))).start();
|
||||
Main.addThread(new Generate(this, world),"Create Chunk");
|
||||
IsCurrentGenerate = true;
|
||||
}
|
||||
|
||||
|
@ -84,6 +88,7 @@ public class Chunk {
|
|||
|
||||
public void destroyChunk() {
|
||||
vbo.destroyVBO();
|
||||
IsDestroy = false;
|
||||
}
|
||||
|
||||
public void loopChunk(int x, int y, int z) {
|
||||
|
@ -107,7 +112,13 @@ public class Chunk {
|
|||
// up + 1 = down - 1 = y
|
||||
// left - 1 = right + 1 = x
|
||||
// front - 1 = back + 1 = z
|
||||
|
||||
// up = true;
|
||||
// down = true;
|
||||
// left = true;
|
||||
// right = true;
|
||||
// back = true;
|
||||
// front = true;
|
||||
|
||||
if (up) {
|
||||
// aa ab bb ba
|
||||
float[] a = new float[] { 1, 1, 1, 1 };
|
||||
|
@ -260,6 +271,20 @@ public class Chunk {
|
|||
public boolean isCurrentGenerate(){
|
||||
return IsCurrentGenerate;
|
||||
}
|
||||
|
||||
public boolean isDestroy() {
|
||||
return IsDestroy;
|
||||
}
|
||||
|
||||
public WorldNoise getWorldNoise() {
|
||||
return worldNoise;
|
||||
}
|
||||
|
||||
public void setWorldNoise(WorldNoise worldNoise) {
|
||||
this.worldNoise = worldNoise;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
class Generate implements Runnable {
|
||||
|
@ -271,27 +296,63 @@ class Generate implements Runnable {
|
|||
this.chunk = chunk;
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
/*
|
||||
* boolean grounded = noise.getNoise(xx, zz) > yy - 1 && noise.getNoise(xx, zz) < yy;
|
||||
|
||||
float percentOfSpawnTree = 0.005f;
|
||||
if(random.nextFloat() < percentOfSpawnTree && grounded){
|
||||
if(random.nextInt(2) == 0)
|
||||
Tree.addOak(world, xx, yy, zz);
|
||||
else
|
||||
Tree.addFir(world, xx, yy, zz);
|
||||
}(non-Javadoc)
|
||||
* @see java.lang.Runnable#run()
|
||||
*/
|
||||
|
||||
public void run() {
|
||||
for (int i = 0; i < chunk.SIZE; i++) {
|
||||
for (int j = 0; j < 2; j++) {
|
||||
for (int k = 0; k < chunk.SIZE; k++) {
|
||||
chunk.blocks[i][j][k] = Block.GRASS;
|
||||
}
|
||||
}
|
||||
}
|
||||
synchronized (world.chunks) {
|
||||
for (int i = 0; i < chunk.SIZE; i++) {
|
||||
for (int j = 0; j < 2; j++) {
|
||||
for (int k = 0; k < chunk.SIZE; k++) {
|
||||
chunk.loopChunk(i, j, k);
|
||||
AffinityLock al = null;
|
||||
int cpuId = 0;
|
||||
try{
|
||||
al = AffinityLock.acquireCore();
|
||||
}catch(Exception e){}
|
||||
long current = System.currentTimeMillis();
|
||||
boolean IsError = true;
|
||||
Noise noise = new Noise(world.seed, 20, 5);
|
||||
for (int x = 0; x < chunk.SIZE; x++) {
|
||||
for (int z = 0; z < chunk.SIZE; z++) {
|
||||
for (int y = 0; y < chunk.SIZE; y++) {
|
||||
int xx = chunk.getX() * chunk.SIZE + x;
|
||||
int yy = chunk.getY() * chunk.SIZE + y;
|
||||
int zz = chunk.getZ() * chunk.SIZE + z;
|
||||
if(noise.getNoise(xx, zz) > yy){
|
||||
chunk.blocks[x][y][z] = Block.GRASS;
|
||||
}else{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
chunk.setGenerated(true);
|
||||
System.out.println(Thread.currentThread().getName());
|
||||
while(IsError){
|
||||
IsError = false;
|
||||
try{
|
||||
synchronized (world.chunks) {
|
||||
for (int i = 0; i < chunk.SIZE; i++) {
|
||||
for (int j = 0; j < chunk.SIZE; j++) {
|
||||
for (int k = 0; k < chunk.SIZE; k++) {
|
||||
chunk.loopChunk(i, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
chunk.setGenerated(true);
|
||||
}catch (Exception e){
|
||||
IsError = true;
|
||||
}
|
||||
}
|
||||
System.out.println(Thread.currentThread().getName() + " | " + (System.currentTimeMillis()-current) + " | " + cpuId);
|
||||
Thread.currentThread().stop();
|
||||
al.release();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
62
VBO/src/world/Noise.java
Normal file
62
VBO/src/world/Noise.java
Normal file
|
@ -0,0 +1,62 @@
|
|||
package world;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import math.Vector2f;
|
||||
|
||||
public class Noise {
|
||||
|
||||
private long seed;
|
||||
private Random rand;
|
||||
private int octave;
|
||||
private float amplitude;
|
||||
|
||||
public Noise(long seed,int octave, float amplitude){
|
||||
this.seed = seed;
|
||||
this.octave = octave;
|
||||
this.amplitude = amplitude;
|
||||
|
||||
rand = new Random();
|
||||
}
|
||||
|
||||
public float getNoise(float x,float z){
|
||||
int xmin = (int)(double)x/octave;
|
||||
int xmax = (int) xmin + 1;
|
||||
int zmin = (int)(double)z/octave;
|
||||
int zmax = (int) zmin + 1;
|
||||
|
||||
Vector2f a = new Vector2f(xmin,zmin);
|
||||
Vector2f b = new Vector2f(xmax,zmin);
|
||||
Vector2f c = new Vector2f(xmax,zmax);
|
||||
Vector2f d = new Vector2f(xmin,zmax);
|
||||
|
||||
float ra = (float)noise(a);
|
||||
float rb = (float)noise(b);
|
||||
float rc = (float)noise(c);
|
||||
float rd = (float)noise(d);
|
||||
|
||||
float t = (x - xmin * octave) / octave;
|
||||
|
||||
float i1 = interpolate(ra,rb,t);
|
||||
float i2 = interpolate(rd,rc,t);
|
||||
|
||||
float h = interpolate(i1,i2,(z - zmin * octave) / octave);
|
||||
return h * amplitude;
|
||||
}
|
||||
|
||||
private float interpolate(float a, float b, float t){
|
||||
float ft = (float) (t * Math.PI);
|
||||
float f = (float )((1f - Math.cos(ft)) * 0.5f);
|
||||
float ret = a * ( 1f - f) + b * f;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private double noise(Vector2f coord){
|
||||
double var = 10000 * (Math.sin(coord.getX() + Math.cos(coord.getY())) + Math.tan(seed));
|
||||
rand.setSeed((long)var);
|
||||
double ret = rand.nextDouble();
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
|
@ -11,16 +11,18 @@ public class World {
|
|||
public long seed;
|
||||
public final int SIZE = 1,HEIGHT = 1;
|
||||
public static final float GRAVITY = 1;
|
||||
public static final int VIEW_CHUNK = 4;
|
||||
public static final int VIEW_CHUNK = 2;
|
||||
public static WorldNoise worldNoise;
|
||||
|
||||
public ArrayList<Chunk> chunks = new ArrayList<Chunk>();
|
||||
|
||||
public World(long seed){
|
||||
this.seed= seed;
|
||||
this.worldNoise = new WorldNoise(seed);
|
||||
for(int x = 0;x < SIZE;x++){
|
||||
for(int y = 0;y < HEIGHT;y++){
|
||||
for(int z = 0;z < SIZE;z++){
|
||||
Chunk ch = new Chunk(x,y,z,this);
|
||||
Chunk ch = new Chunk(x,y,z,this,worldNoise);
|
||||
chunks.add(ch);
|
||||
}
|
||||
}
|
||||
|
@ -41,24 +43,28 @@ public class World {
|
|||
for(int k = 0;k <= delta_z;k++){
|
||||
for(int j = 0; j < HEIGHT; j++){
|
||||
if(getChunk((xa + i), 0, (za + k)) == null){
|
||||
Chunk ch = new Chunk((xa + i),j,(za + k),this);
|
||||
Chunk ch = new Chunk((xa + i),j,(za + k),this,worldNoise);
|
||||
chunks.add(ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ArrayList<Chunk> removeList = new ArrayList<Chunk>();
|
||||
for(int i = 0; i < chunks.size();i++){
|
||||
Chunk c = chunks.get(i);
|
||||
if(c.getPosition().getX() < xa || c.getPosition().getX() > xb || c.getPosition().getZ() < za || c.getPosition().getZ() > zb){
|
||||
c.destroyChunk();
|
||||
chunks.remove(i);
|
||||
break;
|
||||
removeList.add(c);
|
||||
}
|
||||
}
|
||||
for(Chunk c: removeList){
|
||||
removeByChunk(c);
|
||||
}
|
||||
removeList.clear();
|
||||
for(Chunk c : chunks){
|
||||
if(!c.isLoaded() && !c.isGenerated() && !c.isCurrentGenerate())c.createChunk(this);
|
||||
if(!c.isLoaded() && c.isGenerated())c.loadBufferData();
|
||||
c.update();
|
||||
if(!c.isLoaded() && !c.isGenerated() && !c.isCurrentGenerate() && !c.isDestroy())c.createChunk(this);
|
||||
if(!c.isLoaded() && c.isGenerated() && !c.isDestroy())c.loadBufferData();
|
||||
if(!c.isDestroy())c.update();
|
||||
}
|
||||
System.gc();
|
||||
}
|
||||
|
@ -70,6 +76,14 @@ public class World {
|
|||
}
|
||||
}
|
||||
|
||||
public void removeByChunk(Chunk ch){
|
||||
for(int i = 0;i < chunks.size();i++){
|
||||
if(chunks.get(i).equals(ch)){
|
||||
chunks.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Chunk getChunk(int xc, int yc, int zc) {
|
||||
Chunk c = null;
|
||||
Object[] chunk = chunks.toArray();
|
||||
|
@ -94,6 +108,16 @@ public class World {
|
|||
int xb = x % Chunk.SIZE;
|
||||
int yb = y % Chunk.SIZE;
|
||||
int zb = z % Chunk.SIZE;
|
||||
|
||||
// if(xb < 0){
|
||||
// xb = -xb;
|
||||
// }
|
||||
// if(yb < 0){
|
||||
// yb = -yb;
|
||||
// }
|
||||
// if(zb < 0){
|
||||
// zb = -zb;
|
||||
// }
|
||||
|
||||
return chunk.getBlock(xb, yb, zb);
|
||||
}
|
||||
|
|
17
VBO/src/world/WorldNoise.java
Normal file
17
VBO/src/world/WorldNoise.java
Normal file
|
@ -0,0 +1,17 @@
|
|||
package world;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class WorldNoise {
|
||||
|
||||
public Noise noise;
|
||||
public Noise colorVariationNoise;
|
||||
public Random random;
|
||||
|
||||
public WorldNoise(long seed){
|
||||
noise = new Noise(seed, 20, 5);
|
||||
colorVariationNoise = new Noise(seed, 40, 2);
|
||||
random = new Random(seed);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue