Started working on multi-processing

This commit is contained in:
josua 2020-06-18 12:49:38 +10:00
parent 01d221c94a
commit 0098d22927
8 changed files with 148 additions and 169 deletions

View File

@ -1,5 +1,6 @@
package projectzombie; package projectzombie;
import java.io.IOException;
import java.util.Random; import java.util.Random;
import mainloop.manager.MainloopManager; import mainloop.manager.MainloopManager;
@ -28,6 +29,7 @@ import projectzombie.settings.Environment;
import projectzombie.settings.Settings; import projectzombie.settings.Settings;
import projectzombie.time.GameTimer; import projectzombie.time.GameTimer;
import projectzombie.time.NoSleep; import projectzombie.time.NoSleep;
import projectzombie.worker.Worker;
import projectzombie.world.World; import projectzombie.world.World;
import projectzombie.world.chunk.ChunkEventHandler; import projectzombie.world.chunk.ChunkEventHandler;
@ -57,8 +59,10 @@ public class Main
BossBars.clear(); BossBars.clear();
} }
public static void main(String[] args) public static void main(String[] args) throws IOException
{ {
Worker worker = new Worker();
// Initialize cheats, settings, and environment // Initialize cheats, settings, and environment
Environment.init(args); Environment.init(args);
Cheats.init(args); Cheats.init(args);

View File

@ -1,7 +1,10 @@
package projectzombie.display; package projectzombie.display;
import static org.lwjgl.opengl.GL11.GL_FLOAT;
import static org.lwjgl.opengl.GL11.GL_RGBA; import static org.lwjgl.opengl.GL11.GL_RGBA;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE; import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
@ -12,6 +15,7 @@ import org.lwjgl.opengl.GL33;
import gl_engine.graphics.GraphicsHelpers; import gl_engine.graphics.GraphicsHelpers;
import gl_engine.graphics.GraphicsShader; import gl_engine.graphics.GraphicsShader;
import gl_engine.texture.TextureRef3D;
import mainloop.task.IMainloopTask; import mainloop.task.IMainloopTask;
import projectzombie.display.lighting.TileLighting; import projectzombie.display.lighting.TileLighting;
import projectzombie.init.Resources; import projectzombie.init.Resources;
@ -23,6 +27,7 @@ import projectzombie.input.KeyCharCallback;
import projectzombie.input.MouseButtonCallback; import projectzombie.input.MouseButtonCallback;
import projectzombie.input.ScrollWheelCallback; import projectzombie.input.ScrollWheelCallback;
import projectzombie.mainloop.MainloopEventHandler; import projectzombie.mainloop.MainloopEventHandler;
import projectzombie.model.Model;
public class DisplayWindow implements IMainloopTask public class DisplayWindow implements IMainloopTask
{ {
@ -37,6 +42,8 @@ public class DisplayWindow implements IMainloopTask
public static int fps = 0; public static int fps = 0;
public GraphicsShader environmentRenderer; public GraphicsShader environmentRenderer;
private GraphicsShader lightmapRenderer;
private int lightmapVao;
public int glsl_color; public int glsl_color;
public int glsl_tex_cut; public int glsl_tex_cut;
@ -130,6 +137,29 @@ public class DisplayWindow implements IMainloopTask
GL33.glUniform1ui(glsl_atlas, 1); GL33.glUniform1ui(glsl_atlas, 1);
GL33.glUniform1ui(glsl_lightmap, 0); GL33.glUniform1ui(glsl_lightmap, 0);
lightmapRenderer = new GraphicsShader("/resources/shader/lightmapRenderer");
lightmapRenderer.use();
lightmapVao = GL33.glGenVertexArrays();
GL33.glBindVertexArray(lightmapVao);
int vbo = GL33.glGenBuffers();
GL33.glBindBuffer(GL33.GL_ARRAY_BUFFER, vbo);
GL33.glBufferData(GL33.GL_ARRAY_BUFFER, new float[] {
-0.95f, -0.95f,
-0.65f, -0.95f,
-0.65f, -0.65f,
-0.95f, -0.95f,
-0.95f, -0.65f,
-0.65f, -0.65f,
}, GL33.GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, false, Float.BYTES * 2, 0);
glEnableVertexAttribArray(0);
} }
public void render() public void render()
@ -141,6 +171,8 @@ public class DisplayWindow implements IMainloopTask
width = w[0]; width = w[0];
height = h[0]; height = h[0];
environmentRenderer.use();
// Bind the texture atlas // Bind the texture atlas
GL33.glActiveTexture(GL33.GL_TEXTURE1); GL33.glActiveTexture(GL33.GL_TEXTURE1);
Resources.ATLAS.bind(); Resources.ATLAS.bind();
@ -155,6 +187,15 @@ public class DisplayWindow implements IMainloopTask
// Check if the matrix count is ok // Check if the matrix count is ok
//GlHelpers.checkMatrixCount(); //GlHelpers.checkMatrixCount();
lightmapRenderer.use();
GL33.glActiveTexture(GL33.GL_TEXTURE0);
//GL33.glDisable(GL33.GL_DEPTH_TEST);
GL33.glBindVertexArray(lightmapVao);
GL33.glDrawArrays(GL33.GL_TRIANGLES, 0, 6);
// Swap the framebuffers and poll events // Swap the framebuffers and poll events
GLFW.glfwSwapBuffers(window); GLFW.glfwSwapBuffers(window);
GLFW.glfwPollEvents(); GLFW.glfwPollEvents();

View File

@ -22,7 +22,7 @@ import projectzombie.world.chunk.Chunk;
import projectzombie.world.chunk.ChunkEventHandler; import projectzombie.world.chunk.ChunkEventHandler;
import projectzombie.world.layer.Layer; import projectzombie.world.layer.Layer;
public class TileLighting implements IMainloopTask public class TileLighting
{ {
public static boolean lighting_dirty = false; public static boolean lighting_dirty = false;
public static int lightmap; public static int lightmap;
@ -56,172 +56,6 @@ public class TileLighting implements IMainloopTask
return; return;
} }
// Create the chunk collection
ChunkLightingCollection chunks = new ChunkLightingCollection();
Range4i range = new Range4i(
Chunk.RENDER_DISTANCE*3,
Chunk.RENDER_DISTANCE*3,
Chunk.CHUNK_SIZE.mx,
Chunk.CHUNK_SIZE.my);
// Do daylight and tile light calculations
MainloopHelpers.loopAsync(0, range.maxValue(),
(it) -> {
if(!ChunkEventHandler.loaded) {
return;
}
Vec4i upto = Vec4i.fromId(range, it);
Vec2i cpos = new Vec2i(
upto.x + MathHelpers.floor(player.pos.x / 16) - chunks.RENDER_DISTANCE,
upto.y + MathHelpers.floor(player.pos.y / 16) - chunks.RENDER_DISTANCE);
Chunk chunk = layer.chunks.get(cpos);
Vec2i tpos = new Vec2i(cpos.x * 16 + upto.z, cpos.y * 16 + upto.m);
int tid = tpos.getId(Chunk.CHUNK_SIZE);
TileState tile_f = chunk.getFrontTile(tid);
TileState tile_b = chunk.getBackTile(tid);
if(tile_f.tile.passNaturalLight && tile_b.tile.passNaturalLight) {
addLightToTiles(chunks, layer, tpos, 0, 1, true);
}
if(tile_f.tile.emitsLight || tile_b.tile.emitsLight) {
addLightToTiles(chunks, layer, tpos, 0, MathHelpers.biggest(
tile_f.tile.getLightLevel(tile_f, tpos),
tile_b.tile.getLightLevel(tile_b, tpos)
), false);
}
},
() ->
{
int size = chunks.RENDER_DISTANCE * 32 + 16;
ByteBuffer tex_data = ByteBuffer.allocateDirect(size * size * 4);
for(int cx=-chunks.RENDER_DISTANCE;cx<=chunks.RENDER_DISTANCE;cx++) {
for(int cy=-chunks.RENDER_DISTANCE;cy<=chunks.RENDER_DISTANCE;cy++)
{
Vec2i cpos = new Vec2i(
cx + MathHelpers.floor(player.pos.x / 16),
cy + MathHelpers.floor(player.pos.y / 16));
Chunk chunk = layer.chunks.get(cpos);
ChunkLightingTemp chunk_t = chunks.getChunkwCPos(cpos);
for(int x=0;x<16;x++) {
for(int y=0;y<16;y++)
{
Vec2i tpos = new Vec2i(cpos.x * 16 + x, cpos.y * 16 + y);
int tid = tpos.getId(Chunk.CHUNK_SIZE);
int id = (((cx + chunks.RENDER_DISTANCE) * 16 + x) +
((cy + chunks.RENDER_DISTANCE) * 16 + y) * size) * 4;
chunk.setDaylightLevel(chunk_t.getDaylightLevel(tid), tid);
chunk.setLightLevel(chunk_t.getLightLevel(tid), tid);
tex_data.put(id+0, (byte)(chunk_t.getDaylightLevel(tid) * 255));
tex_data.put(id+1, (byte)(chunk_t.getLightLevel(tid) * 255));
}
}
}
}
GL33.glBindTexture(GL33.GL_TEXTURE_2D, lightmap);
GL33.glTexImage2D(GL33.GL_TEXTURE_2D, 0, GL_RGBA, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data);
GL33.glGenerateMipmap(GL33.GL_TEXTURE_2D);
});
}
private static void addLightToTiles(
ChunkLightingCollection chunks,
Layer layer, Vec2i lpos,
int it, double light,
boolean daylightMode
) {
if(
MathHelpers.floor(lpos.squareDistance(new Vec2i(
MathHelpers.floor(Main.player.pos.x),
MathHelpers.floor(Main.player.pos.y))) / 16)
> Chunk.RENDER_DISTANCE
) {
return;
}
if(light <= 0) {
return;
}
Chunk chunk = layer.getChunk(lpos);
ChunkLightingTemp chunk_t = chunks.getChunk(lpos);
int lid = lpos.getId(Chunk.CHUNK_SIZE);
// Don't calculate anything on empty chunks
if(chunk == Chunk.CHUNK_EMPTY) {
return;
}
TileState tile_f = chunk.getFrontTile(lid);
TileState tile_b = chunk.getBackTile(lid);
// Set the light level
if(daylightMode) {
if(tile_f.tile.passNaturalLight && tile_b.tile.passNaturalLight && it != 0) {
return;
}
if(light <= chunk_t.getDaylightLevel(lid)) return;
chunk_t.setDaylightLevel(light, lid);
} else {
if((tile_f.tile.emitsLight || tile_b.tile.emitsLight) && it == 1) {
double light_external = MathHelpers.biggest(
tile_f.tile.getLightLevel(tile_f, lpos),
tile_b.tile.getLightLevel(tile_b, lpos));
if(light_external >= light) {
return;
};
}
if(light <= chunk_t.getLightLevel(lid)) return;
chunk_t.setLightLevel(light, lid);
}
// Calculate the dissipation
double dissipation = MathHelpers.biggest(
tile_b.tile.getLightDissipation(tile_b),
tile_f.tile.getLightDissipation(tile_f));
// Get all the adjacent positions of the light tiles to flow onto
Vec2i positions[] = {
new Vec2i(lpos.x+1, lpos.y),
new Vec2i(lpos.x-1, lpos.y),
new Vec2i(lpos.x, lpos.y+1),
new Vec2i(lpos.x, lpos.y-1)
};
// Add the light to all the adjacent positions
for(Vec2i position : positions) {
addLightToTiles(chunks, layer, position, it + 1, light - dissipation, daylightMode);
}
}
@Override
public boolean MainLoopDelay(long millis) {
return millis > 10000;
}
@Override
public boolean MainLoopRepeat() {
return false;
}
@Override
public void MainLoopUpdate() {
lighting_dirty = true;
} }
} }

View File

@ -0,0 +1,79 @@
package projectzombie.worker;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.ProcessBuilder.Redirect;
import java.nio.ByteBuffer;
import bdf.data.BdfDatabase;
import bdf.types.BdfIndent;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
public class Worker
{
ProcessBuilder pb;
Process process;
InputStream in;
OutputStream out;
public Worker() throws IOException
{
pb = new ProcessBuilder("java", "-jar", "worker.jar");
pb.redirectError(Redirect.INHERIT);
process = pb.start();
in = process.getInputStream();
out = process.getOutputStream();
byte[] lights = new byte[] {
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)255, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)255, (byte)50,
};
BdfObject bdf = new BdfObject();
BdfNamedList nl = bdf.getNamedList();
nl.set("task", BdfObject.withString("light"));
nl.set("light", BdfObject.withByteArray(lights));
nl.set("w", BdfObject.withInteger(4));
nl.set("h", BdfObject.withInteger(4));
byte[] data = bdf.serialize().getBytes();
ByteBuffer size_buff = ByteBuffer.allocate(4);
size_buff.putInt(0, data.length);
out.write(size_buff.array());
out.write(data);
out.write('\n');
in.read(size_buff.array());
data = new byte[size_buff.getInt(0)];
in.read(data);
in.read(new byte[1]);
bdf = new BdfObject(new BdfDatabase(data));
System.out.println(bdf.serializeHumanReadable(new BdfIndent(" ", "\n")));
}
}

View File

@ -11,7 +11,7 @@ uniform vec4 color;
void main() void main()
{ {
FragColor = texture(atlas, pTexture) * color * vec4(pLighting, 1); FragColor = texture(atlas, pTexture) * color;// * vec4(pLighting, 1);
discard(FragColor.a == 0 || (pPos.x > tex_cut.y && tex_cut.x > 0.5)); discard(FragColor.a == 0 || (pPos.x > tex_cut.y && tex_cut.x > 0.5));
} }

View File

@ -0,0 +1,10 @@
#version 330
uniform sampler2D lightmap;
in vec2 TexCoord;
out vec4 FragColor;
void main()
{
FragColor = vec4(texture(lightmap, TexCoord).rgb, 1);
}

View File

@ -0,0 +1,11 @@
#version 330
layout (location = 0) in vec2 aPos;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos, 0, 1);
TexCoord = aPos;
}

BIN
worker.jar Normal file

Binary file not shown.