Skip to content

Commit

Permalink
Use commons-pool
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Apr 12, 2024
1 parent 8a4ae73 commit 470d674
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 112 deletions.
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* version 2.1 of the License, or (at your option) any later version.
*/

// last updated: 2024/3/16
// last updated: 2024/3/30

plugins {
`java-platform`
Expand Down Expand Up @@ -84,6 +84,7 @@ val coreVersion: String by rootProject
val clientVersion: String by rootProject

val annotationsVersion: String by rootProject
val commonsPoolVersion: String by rootProject
val gsonVersion: String by rootProject
val jomlVersion: String by rootProject
val logbackVersion: String by rootProject
Expand Down Expand Up @@ -135,6 +136,7 @@ subprojects {
implementation("org.joml:joml:$jomlVersion")
implementation("ch.qos.logback:logback-classic:$logbackVersion")
implementation("com.google.code.gson:gson:$gsonVersion")
implementation("org.apache.commons:commons-pool2:$commonsPoolVersion")
}
}

Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ coreVersion=0.1.0-SNAPSHOT
clientVersion=0.1.0-SNAPSHOT

annotationsVersion=24.1.0
commonsPoolVersion=2.12.0
gsonVersion=2.10.1
jomlVersion=1.10.5
logbackVersion=1.4.14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public ChunkCompileTask(GameRenderer gameRenderer, WorldRenderer worldRenderer,
}

@Override
public ChunkVertexData call() {
public ChunkVertexData call() throws Exception {
final var pool = worldRenderer.vertexBuilderPool();
final DefaultVertexBuilder builder = pool.acquire();
final DefaultVertexBuilder builder = pool.borrowObject();
builder.reset();
final int cx = chunk.x();
final int cy = chunk.y();
Expand Down Expand Up @@ -79,7 +79,7 @@ public ChunkVertexData call() {
builder.shouldReallocateVertexData(),
builder.shouldReallocateIndexData()
);
pool.release(builder);
pool.returnObject(builder);
return data;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import io.github.xenfork.freeworld.world.block.BlockType;
import io.github.xenfork.freeworld.world.chunk.Chunk;
import io.github.xenfork.freeworld.world.chunk.ChunkPos;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.jetbrains.annotations.NotNull;
import org.joml.*;

Expand All @@ -39,7 +43,22 @@ public final class WorldRenderer implements GLResource, WorldListener {
private final GameRenderer gameRenderer;
private final World world;
private final ExecutorService executor;
private final VertexBuilderPool<DefaultVertexBuilder> vertexBuilderPool = new VertexBuilderPool<>(WorldRenderer::createVertexBuilder);
private final GenericObjectPool<DefaultVertexBuilder> vertexBuilderPool = new GenericObjectPool<>(new BasePooledObjectFactory<>() {
@Override
public DefaultVertexBuilder create() {
return createVertexBuilder();
}

@Override
public void activateObject(PooledObject<DefaultVertexBuilder> p) {
p.getObject().reset();
}

@Override
public PooledObject<DefaultVertexBuilder> wrap(DefaultVertexBuilder obj) {
return new DefaultPooledObject<>(obj);
}
});
private final ClientChunk[] chunks;
private final FrustumIntersection frustumIntersection = new FrustumIntersection();
private final FrustumRayBuilder frustumRayBuilder = new FrustumRayBuilder();
Expand Down Expand Up @@ -83,13 +102,16 @@ private static DefaultVertexBuilder createVertexBuilder() {

public void compileChunks() {
for (ClientChunk chunk : chunks) {
if (chunk.shouldRecompile && !chunk.submitted) {
if (chunk.dirty) {
if (chunk.future != null && chunk.future.state() == Future.State.RUNNING) {
chunk.future.cancel(false);
}
final Chunk chunk1 = world.getChunk(chunk.x(), chunk.y(), chunk.z());
if (chunk1 != null) {
chunk.copyFrom(chunk1);
}
chunk.future = executor.submit(new ChunkCompileTask(gameRenderer, this, chunk));
chunk.submitted = true;
chunk.dirty = false;
}
}
}
Expand Down Expand Up @@ -207,13 +229,14 @@ private ClientChunk getChunkByAbsolutePos(int x, int y, int z) {
);
}

public VertexBuilderPool<DefaultVertexBuilder> vertexBuilderPool() {
public GenericObjectPool<DefaultVertexBuilder> vertexBuilderPool() {
return vertexBuilderPool;
}

@Override
public void close(GLStateMgr gl) {
executor.close();
vertexBuilderPool.close();
for (ClientChunk chunk : chunks) {
chunk.close(gl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
*/
public final class ClientChunk extends Chunk implements GLResource {
public Future<ChunkVertexData> future = null;
public boolean submitted = false;
public boolean shouldRecompile = true;
/**
* Is this chunk changed?
*/
public boolean dirty = true;
private int indexCount = 0;
private int vao = 0;
private int vbo = 0;
Expand All @@ -40,43 +42,39 @@ public ClientChunk(World world, int x, int y, int z) {
}

public void render(GLStateMgr gl) {
if (shouldRecompile && submitted) {
try {
if (future.isDone()) {
final ChunkVertexData data = future.get();
try {
final MemorySegment vertexData = data.vertexData();
final MemorySegment indexData = data.indexData();
indexCount = data.indexCount();
if (vao == 0) vao = gl.genVertexArrays();
if (vbo == 0) vbo = gl.genBuffers();
if (ebo == 0) ebo = gl.genBuffers();
gl.setVertexArrayBinding(vao);
gl.setArrayBufferBinding(vbo);
if (data.shouldReallocateVertexData()) {
gl.bufferData(GL15C.ARRAY_BUFFER, vertexData, GL15C.DYNAMIC_DRAW);
final VertexLayout layout = data.vertexLayout();
layout.enableAttribs(gl);
layout.specifyAttribPointers(gl);
} else {
gl.bufferSubData(GL15C.ARRAY_BUFFER, 0L, vertexData);
}
gl.bindBuffer(GL15C.ELEMENT_ARRAY_BUFFER, ebo);
if (data.shouldReallocateIndexData()) {
gl.bufferData(GL15C.ELEMENT_ARRAY_BUFFER, indexData, GL15C.DYNAMIC_DRAW);
} else {
gl.bufferSubData(GL15C.ELEMENT_ARRAY_BUFFER, 0L, indexData);
}
} finally {
data.arena().close();
try {
if (future != null && future.state() == Future.State.SUCCESS) {
final ChunkVertexData data = future.get();
try {
final MemorySegment vertexData = data.vertexData();
final MemorySegment indexData = data.indexData();
indexCount = data.indexCount();
if (vao == 0) vao = gl.genVertexArrays();
if (vbo == 0) vbo = gl.genBuffers();
if (ebo == 0) ebo = gl.genBuffers();
gl.setVertexArrayBinding(vao);
gl.setArrayBufferBinding(vbo);
if (data.shouldReallocateVertexData()) {
gl.bufferData(GL15C.ARRAY_BUFFER, vertexData, GL15C.DYNAMIC_DRAW);
final VertexLayout layout = data.vertexLayout();
layout.enableAttribs(gl);
layout.specifyAttribPointers(gl);
} else {
gl.bufferSubData(GL15C.ARRAY_BUFFER, 0L, vertexData);
}
shouldRecompile = false;
submitted = false;
future = null;
gl.bindBuffer(GL15C.ELEMENT_ARRAY_BUFFER, ebo);
if (data.shouldReallocateIndexData()) {
gl.bufferData(GL15C.ELEMENT_ARRAY_BUFFER, indexData, GL15C.DYNAMIC_DRAW);
} else {
gl.bufferSubData(GL15C.ELEMENT_ARRAY_BUFFER, 0L, indexData);
}
} finally {
data.arena().close();
}
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
future = null;
}
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
if (vao != 0) {
gl.setVertexArrayBinding(vao);
Expand All @@ -87,7 +85,7 @@ public void render(GLStateMgr gl) {
@Override
public void markDirty() {
super.markDirty();
shouldRecompile = true;
dirty = true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
requires overrungl.glfw;
requires overrungl.opengl;
requires overrungl.stb;
requires org.apache.commons.pool2;
requires static org.jetbrains.annotations;
}

0 comments on commit 470d674

Please sign in to comment.